blob: a0e946d708fe8a31cdfec035a3e8f3f94a88c589 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom7940e442013-07-12 13:46:57 -070016#include "dex/compiler_ir.h"
17#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070018#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
buzbee2700f7e2014-03-07 09:46:20 -080045LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
46 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 LIR* branch;
48 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080049 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
50 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 branch = OpUnconditionalBranch(tgt);
52 } else {
buzbee2700f7e2014-03-07 09:46:20 -080053 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
55 }
56 // Remember branch target - will process later
57 throw_launchpads_.Insert(tgt);
58 return branch;
59}
60
Mingyao Yange643a172014-04-08 11:02:52 -070061void Mir2Lir::GenDivZeroException() {
62 LIR* branch = OpUnconditionalBranch(nullptr);
63 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070067 LIR* branch = OpCondBranch(c_code, nullptr);
68 AddDivZeroCheckSlowPath(branch);
69}
70
Mingyao Yange643a172014-04-08 11:02:52 -070071void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
72 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070073 AddDivZeroCheckSlowPath(branch);
74}
75
76void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
77 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
78 public:
79 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
80 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
81 }
82
Mingyao Yange643a172014-04-08 11:02:52 -070083 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070084 m2l_->ResetRegPool();
85 m2l_->ResetDefTracking();
86 GenerateTargetLabel();
87 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
88 }
89 };
90
91 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
92}
Dave Allisonb373e092014-02-20 16:06:36 -080093
Mingyao Yange643a172014-04-08 11:02:52 -070094LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
95 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
96 public:
97 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
98 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
99 }
100
101 void Compile() OVERRIDE {
102 m2l_->ResetRegPool();
103 m2l_->ResetDefTracking();
104 GenerateTargetLabel();
105 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
106 }
107 };
108
109 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
110 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
111 return branch;
112}
113
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800115LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800116 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700117 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 }
Dave Allisonb373e092014-02-20 16:06:36 -0800119 return nullptr;
120}
121
Dave Allisonf9439142014-03-27 15:10:22 -0700122/* Perform an explicit null-check on a register. */
123LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
124 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
125 return NULL;
126 }
Mingyao Yange643a172014-04-08 11:02:52 -0700127 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700128}
129
Dave Allisonb373e092014-02-20 16:06:36 -0800130void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
131 if (!Runtime::Current()->ExplicitNullChecks()) {
132 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
133 return;
134 }
135 MarkSafepointPC(last_lir_insn_);
136 }
137}
138
139void Mir2Lir::MarkPossibleStackOverflowException() {
140 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
141 MarkSafepointPC(last_lir_insn_);
142 }
143}
144
buzbee2700f7e2014-03-07 09:46:20 -0800145void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800146 if (!Runtime::Current()->ExplicitNullChecks()) {
147 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
148 return;
149 }
150 // Force an implicit null check by performing a memory operation (load) from the given
151 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800152 RegStorage tmp = AllocTemp();
153 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700154 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800155 FreeTemp(tmp);
156 MarkSafepointPC(load);
157 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158}
159
160/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800161LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700162 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800163 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
164 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
166 // Remember branch target - will process later
167 throw_launchpads_.Insert(tgt);
168 return branch;
169}
170
171void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
172 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 ConditionCode cond;
175 switch (opcode) {
176 case Instruction::IF_EQ:
177 cond = kCondEq;
178 break;
179 case Instruction::IF_NE:
180 cond = kCondNe;
181 break;
182 case Instruction::IF_LT:
183 cond = kCondLt;
184 break;
185 case Instruction::IF_GE:
186 cond = kCondGe;
187 break;
188 case Instruction::IF_GT:
189 cond = kCondGt;
190 break;
191 case Instruction::IF_LE:
192 cond = kCondLe;
193 break;
194 default:
195 cond = static_cast<ConditionCode>(0);
196 LOG(FATAL) << "Unexpected opcode " << opcode;
197 }
198
199 // Normalize such that if either operand is constant, src2 will be constant
200 if (rl_src1.is_const) {
201 RegLocation rl_temp = rl_src1;
202 rl_src1 = rl_src2;
203 rl_src2 = rl_temp;
204 cond = FlipComparisonOrder(cond);
205 }
206
207 rl_src1 = LoadValue(rl_src1, kCoreReg);
208 // Is this really an immediate comparison?
209 if (rl_src2.is_const) {
210 // If it's already live in a register or not easily materialized, just keep going
211 RegLocation rl_temp = UpdateLoc(rl_src2);
212 if ((rl_temp.location == kLocDalvikFrame) &&
213 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
214 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800215 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 return;
217 }
218 }
219 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800220 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221}
222
223void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700224 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 ConditionCode cond;
226 rl_src = LoadValue(rl_src, kCoreReg);
227 switch (opcode) {
228 case Instruction::IF_EQZ:
229 cond = kCondEq;
230 break;
231 case Instruction::IF_NEZ:
232 cond = kCondNe;
233 break;
234 case Instruction::IF_LTZ:
235 cond = kCondLt;
236 break;
237 case Instruction::IF_GEZ:
238 cond = kCondGe;
239 break;
240 case Instruction::IF_GTZ:
241 cond = kCondGt;
242 break;
243 case Instruction::IF_LEZ:
244 cond = kCondLe;
245 break;
246 default:
247 cond = static_cast<ConditionCode>(0);
248 LOG(FATAL) << "Unexpected opcode " << opcode;
249 }
buzbee2700f7e2014-03-07 09:46:20 -0800250 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700253void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
255 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800256 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800258 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 }
buzbee2700f7e2014-03-07 09:46:20 -0800260 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 StoreValueWide(rl_dest, rl_result);
262}
263
264void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700265 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700266 rl_src = LoadValue(rl_src, kCoreReg);
267 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
268 OpKind op = kOpInvalid;
269 switch (opcode) {
270 case Instruction::INT_TO_BYTE:
271 op = kOp2Byte;
272 break;
273 case Instruction::INT_TO_SHORT:
274 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700276 case Instruction::INT_TO_CHAR:
277 op = kOp2Char;
278 break;
279 default:
280 LOG(ERROR) << "Bad int conversion type";
281 }
buzbee2700f7e2014-03-07 09:46:20 -0800282 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700283 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
286/*
287 * Let helper function take care of everything. Will call
288 * Array::AllocFromCode(type_idx, method, count);
289 * Note: AllocFromCode will handle checks for errNegativeArraySize.
290 */
291void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700294 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800295 const DexFile* dex_file = cu_->dex_file;
296 CompilerDriver* driver = cu_->compiler_driver;
297 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800299 bool is_type_initialized; // Ignored as an array does not have an initializer.
300 bool use_direct_type_ptr;
301 uintptr_t direct_type_ptr;
302 if (kEmbedClassInCode &&
303 driver->CanEmbedTypeInCode(*dex_file, type_idx,
304 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
305 // The fast path.
306 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800307 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700308 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800309 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
310 } else {
311 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800313 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
314 }
315 } else {
316 // The slow path.
317 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700318 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800319 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
320 }
321 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700323 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800324 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 RegLocation rl_result = GetReturn(false);
327 StoreValue(rl_dest, rl_result);
328}
329
330/*
331 * Similar to GenNewArray, but with post-allocation initialization.
332 * Verifier guarantees we're dealing with an array class. Current
333 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
334 * Current code also throws internal unimp if not 'L', '[' or 'I'.
335 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700336void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 int elems = info->num_arg_words;
338 int type_idx = info->index;
339 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700340 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
342 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700343 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700345 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 }
347 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
348 FreeTemp(TargetReg(kArg2));
349 FreeTemp(TargetReg(kArg1));
350 /*
351 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
352 * return region. Because AllocFromCode placed the new array
353 * in kRet0, we'll just lock it into place. When debugger support is
354 * added, it may be necessary to additionally copy all return
355 * values to a home location in thread-local storage
356 */
357 LockTemp(TargetReg(kRet0));
358
359 // TODO: use the correct component size, currently all supported types
360 // share array alignment with ints (see comment at head of function)
361 size_t component_size = sizeof(int32_t);
362
363 // Having a range of 0 is legal
364 if (info->is_range && (elems > 0)) {
365 /*
366 * Bit of ugliness here. We're going generate a mem copy loop
367 * on the register range, but it is possible that some regs
368 * in the range have been promoted. This is unlikely, but
369 * before generating the copy, we'll just force a flush
370 * of any regs in the source range that have been promoted to
371 * home location.
372 */
373 for (int i = 0; i < elems; i++) {
374 RegLocation loc = UpdateLoc(info->args[i]);
375 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700376 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
378 }
379 /*
380 * TUNING note: generated code here could be much improved, but
381 * this is an uncommon operation and isn't especially performance
382 * critical.
383 */
buzbee2700f7e2014-03-07 09:46:20 -0800384 RegStorage r_src = AllocTemp();
385 RegStorage r_dst = AllocTemp();
386 RegStorage r_idx = AllocTemp();
387 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700388 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 case kThumb2:
390 r_val = TargetReg(kLr);
391 break;
392 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700393 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 FreeTemp(TargetReg(kRet0));
395 r_val = AllocTemp();
396 break;
397 case kMips:
398 r_val = AllocTemp();
399 break;
400 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
401 }
402 // Set up source pointer
403 RegLocation rl_first = info->args[0];
404 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
405 // Set up the target pointer
406 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
407 mirror::Array::DataOffset(component_size).Int32Value());
408 // Set up the loop counter (known to be > 0)
409 LoadConstant(r_idx, elems - 1);
410 // Generate the copy loop. Going backwards for convenience
411 LIR* target = NewLIR0(kPseudoTargetLabel);
412 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700413 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
414 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 FreeTemp(r_val);
416 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700417 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 // Restore the target pointer
419 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
420 -mirror::Array::DataOffset(component_size).Int32Value());
421 }
422 } else if (!info->is_range) {
423 // TUNING: interleave
424 for (int i = 0; i < elems; i++) {
425 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700426 Store32Disp(TargetReg(kRet0),
427 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800429 if (IsTemp(rl_arg.reg)) {
430 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 }
432 }
433 }
434 if (info->result.location != kLocInvalid) {
435 StoreValue(info->result, GetReturn(false /* not fp */));
436 }
437}
438
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800439//
440// Slow path to ensure a class is initialized for sget/sput.
441//
442class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
443 public:
buzbee2700f7e2014-03-07 09:46:20 -0800444 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
445 RegStorage r_base) :
446 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
447 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800448 }
449
450 void Compile() {
451 LIR* unresolved_target = GenerateTargetLabel();
452 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700453 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800454 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800455 // Copy helper's result into r_base, a no-op on all but MIPS.
456 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
457
458 m2l_->OpUnconditionalBranch(cont_);
459 }
460
461 private:
462 LIR* const uninit_;
463 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800464 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800465};
466
Vladimir Markobe0e5462014-02-26 11:24:15 +0000467void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000469 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
470 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
471 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
472 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800473 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000474 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 // Fast path, static storage base is this method's class
476 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800477 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700478 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800479 if (IsTemp(rl_method.reg)) {
480 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 }
482 } else {
483 // Medium path, static storage base in a different class which requires checks that the other
484 // class is initialized.
485 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000486 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 // May do runtime call so everything to home locations.
488 FlushAllRegs();
489 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800490 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 LockTemp(r_method);
492 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800493 r_base = TargetReg(kArg0);
494 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700495 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
496 LoadRefDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000497 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800498 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000499 if (!field_info.IsInitialized() &&
500 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800501 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800502
503 // The slow path is invoked if the r_base is NULL or the class pointed
504 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800505 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800506 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800507 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800508 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800509 mirror::Class::StatusOffset().Int32Value(),
510 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800511 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800512
buzbee2700f7e2014-03-07 09:46:20 -0800513 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000514 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800515
516 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 FreeTemp(r_method);
519 }
520 // rBase now holds static storage base
521 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700522 RegisterClass register_kind = kAnyReg;
523 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
524 // Force long/double volatile stores into SSE registers to avoid tearing.
525 register_kind = kFPReg;
526 }
527 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 } else {
529 rl_src = LoadValue(rl_src, kAnyReg);
530 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000531 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800532 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 GenMemBarrier(kStoreStore);
534 }
535 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800536 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
buzbee695d13a2014-04-19 13:32:20 -0700537 } else if (rl_src.ref) {
538 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 } else {
buzbee695d13a2014-04-19 13:32:20 -0700540 Store32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000542 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800543 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 GenMemBarrier(kStoreLoad);
545 }
546 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800547 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800549 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 } else {
551 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700552 ThreadOffset<4> setter_offset =
553 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
554 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
555 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000556 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 }
558}
559
Vladimir Markobe0e5462014-02-26 11:24:15 +0000560void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700561 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000562 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
563 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
564 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
565 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800566 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000567 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 // Fast path, static storage base is this method's class
569 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800570 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700571 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 } else {
573 // Medium path, static storage base in a different class which requires checks that the other
574 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000575 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 // May do runtime call so everything to home locations.
577 FlushAllRegs();
578 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800579 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 LockTemp(r_method);
581 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800582 r_base = TargetReg(kArg0);
583 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700584 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
585 LoadRefDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
586 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800587 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000588 if (!field_info.IsInitialized() &&
589 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800591
592 // The slow path is invoked if the r_base is NULL or the class pointed
593 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800594 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800595 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800596 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800597 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800598 mirror::Class::StatusOffset().Int32Value(),
599 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601
buzbee2700f7e2014-03-07 09:46:20 -0800602 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000603 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604
605 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 FreeTemp(r_method);
608 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700610 RegisterClass result_reg_kind = kAnyReg;
611 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
612 // Force long/double volatile loads into SSE registers to avoid tearing.
613 result_reg_kind = kFPReg;
614 }
615 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800616
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800618 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
buzbee695d13a2014-04-19 13:32:20 -0700619 } else if (rl_result.ref) {
620 LoadRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 } else {
buzbee695d13a2014-04-19 13:32:20 -0700622 Load32Disp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800624 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800625
626 if (field_info.IsVolatile()) {
627 // Without context sensitive analysis, we must issue the most conservative barriers.
628 // In this case, either a load or store may follow so we issue both barriers.
629 GenMemBarrier(kLoadLoad);
630 GenMemBarrier(kLoadStore);
631 }
632
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 if (is_long_or_double) {
634 StoreValueWide(rl_dest, rl_result);
635 } else {
636 StoreValue(rl_dest, rl_result);
637 }
638 } else {
639 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700640 ThreadOffset<4> getterOffset =
641 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
642 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
643 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000644 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 if (is_long_or_double) {
646 RegLocation rl_result = GetReturnWide(rl_dest.fp);
647 StoreValueWide(rl_dest, rl_result);
648 } else {
649 RegLocation rl_result = GetReturn(rl_dest.fp);
650 StoreValue(rl_dest, rl_result);
651 }
652 }
653}
654
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800655// Generate code for all slow paths.
656void Mir2Lir::HandleSlowPaths() {
657 int n = slow_paths_.Size();
658 for (int i = 0; i < n; ++i) {
659 LIRSlowPath* slowpath = slow_paths_.Get(i);
660 slowpath->Compile();
661 }
662 slow_paths_.Reset();
663}
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700667 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 for (int i = 0; i < num_elems; i++) {
669 ResetRegPool();
670 ResetDefTracking();
671 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700672 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 current_dalvik_offset_ = lab->operands[1];
674 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800675 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
677 OpUnconditionalBranch(resume_lab);
678 }
679}
680
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700681void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 int num_elems = throw_launchpads_.Size();
683 for (int i = 0; i < num_elems; i++) {
684 ResetRegPool();
685 ResetDefTracking();
686 LIR* lab = throw_launchpads_.Get(i);
687 current_dalvik_offset_ = lab->operands[1];
688 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700689 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 int v1 = lab->operands[2];
Brian Carlstrom7fff5442014-04-17 23:11:17 -0700691 int v2 = lab->operands[3];
692 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 switch (lab->operands[0]) {
Brian Carlstrom7fff5442014-04-17 23:11:17 -0700694 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
695 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
696 if (target_x86) {
697 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
698 mirror::Array::LengthOffset().Int32Value());
699 } else {
700 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
701 }
702 // Make sure the following LoadConstant doesn't mess with kArg1.
703 LockTemp(TargetReg(kArg1));
704 LoadConstant(TargetReg(kArg0), v2);
705 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
706 break;
707 case kThrowArrayBounds:
708 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
709 if (v2 != TargetReg(kArg0).GetReg()) {
710 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
711 if (target_x86) {
712 // x86 leaves the array pointer in v2, so load the array length that the handler expects
713 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
714 mirror::Array::LengthOffset().Int32Value());
715 } else {
716 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
717 }
718 } else {
719 if (v1 == TargetReg(kArg1).GetReg()) {
720 // Swap v1 and v2, using kArg2 as a temp
721 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
722 if (target_x86) {
723 // x86 leaves the array pointer in v2; load the array length that the handler expects
724 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
725 mirror::Array::LengthOffset().Int32Value());
726 } else {
727 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
728 }
729 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
730 } else {
731 if (target_x86) {
732 // x86 leaves the array pointer in v2; load the array length that the handler expects
733 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
734 mirror::Array::LengthOffset().Int32Value());
735 } else {
736 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
737 }
738 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
739 }
740 }
741 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
742 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800744 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700746 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 default:
749 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
750 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000751 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800752 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700753 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 }
755}
756
Vladimir Markobe0e5462014-02-26 11:24:15 +0000757void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700759 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000760 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
761 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
762 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 RegLocation rl_result;
764 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000765 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 rl_obj = LoadValue(rl_obj, kCoreReg);
767 if (is_long_or_double) {
768 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800769 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700770 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700771 RegisterClass result_reg_kind = kAnyReg;
772 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
773 // Force long/double volatile loads into SSE registers to avoid tearing.
774 result_reg_kind = kFPReg;
775 }
776 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800777 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
778 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800779 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000780 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800781 // Without context sensitive analysis, we must issue the most conservative barriers.
782 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800784 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 }
786 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800787 RegStorage reg_ptr = AllocTemp();
788 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800790 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700791 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000792 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800793 // Without context sensitive analysis, we must issue the most conservative barriers.
794 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800796 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 }
798 FreeTemp(reg_ptr);
799 }
800 StoreValueWide(rl_dest, rl_result);
801 } else {
802 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800803 GenNullCheck(rl_obj.reg, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700804 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, k32,
buzbee2700f7e2014-03-07 09:46:20 -0800805 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800806 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000807 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800808 // Without context sensitive analysis, we must issue the most conservative barriers.
809 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800811 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 }
813 StoreValue(rl_dest, rl_result);
814 }
815 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700816 ThreadOffset<4> getterOffset =
817 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
818 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
819 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000820 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 if (is_long_or_double) {
822 RegLocation rl_result = GetReturnWide(rl_dest.fp);
823 StoreValueWide(rl_dest, rl_result);
824 } else {
825 RegLocation rl_result = GetReturn(rl_dest.fp);
826 StoreValue(rl_dest, rl_result);
827 }
828 }
829}
830
Vladimir Markobe0e5462014-02-26 11:24:15 +0000831void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700833 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000834 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
835 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
836 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000838 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 rl_obj = LoadValue(rl_obj, kCoreReg);
840 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700841 RegisterClass src_reg_kind = kAnyReg;
842 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
843 // Force long/double volatile stores into SSE registers to avoid tearing.
844 src_reg_kind = kFPReg;
845 }
846 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800847 GenNullCheck(rl_obj.reg, opt_flags);
848 RegStorage reg_ptr = AllocTemp();
849 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000850 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800851 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 GenMemBarrier(kStoreStore);
853 }
buzbee2700f7e2014-03-07 09:46:20 -0800854 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800855 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000856 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800857 // A load might follow the volatile store so insert a StoreLoad barrier.
858 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 }
860 FreeTemp(reg_ptr);
861 } else {
862 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800863 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000864 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800865 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 GenMemBarrier(kStoreStore);
867 }
buzbee695d13a2014-04-19 13:32:20 -0700868 Store32Disp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800869 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000870 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800871 // A load might follow the volatile store so insert a StoreLoad barrier.
872 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 }
874 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800875 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 }
877 }
878 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700879 ThreadOffset<4> setter_offset =
880 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
881 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
882 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000883 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
884 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 }
886}
887
Ian Rogersa9a82542013-10-04 11:17:26 -0700888void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
889 RegLocation rl_src) {
890 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
891 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
892 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700893 ThreadOffset<4> helper = needs_range_check
894 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
895 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
896 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700897 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
898}
899
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700900void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800902 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
904 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
905 *cu_->dex_file,
906 type_idx)) {
907 // Call out to helper which resolves type and verifies access.
908 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700909 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800910 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 RegLocation rl_result = GetReturn(false);
912 StoreValue(rl_dest, rl_result);
913 } else {
914 // We're don't need access checks, load type from dex cache
915 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700916 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700917 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 int32_t offset_of_type =
919 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
920 * type_idx);
buzbee695d13a2014-04-19 13:32:20 -0700921 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
923 type_idx) || SLOW_TYPE_PATH) {
924 // Slow path, at runtime test if type is null and if so initialize
925 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800926 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800927 LIR* cont = NewLIR0(kPseudoTargetLabel);
928
929 // Object to generate the slow path for class resolution.
930 class SlowPath : public LIRSlowPath {
931 public:
932 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
933 const RegLocation& rl_method, const RegLocation& rl_result) :
934 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
935 rl_method_(rl_method), rl_result_(rl_result) {
936 }
937
938 void Compile() {
939 GenerateTargetLabel();
940
Ian Rogersdd7624d2014-03-14 17:43:00 -0700941 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800942 rl_method_.reg, true);
943 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800944
945 m2l_->OpUnconditionalBranch(cont_);
946 }
947
948 private:
949 const int type_idx_;
950 const RegLocation rl_method_;
951 const RegLocation rl_result_;
952 };
953
954 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800955 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800956
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 // Fast path, we're done - just store result
960 StoreValue(rl_dest, rl_result);
961 }
962 }
963}
964
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700965void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 /* NOTE: Most strings should be available at compile time */
967 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
968 (sizeof(mirror::String*) * string_idx);
969 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
970 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
971 // slow path, resolve string if not in dex cache
972 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700973 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800974
975 // If the Method* is already in a register, we can save a copy.
976 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800977 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800978 if (rl_method.location == kLocPhysReg) {
979 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800980 DCHECK(!IsTemp(rl_method.reg));
981 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800982 } else {
983 r_method = TargetReg(kArg2);
984 LoadCurrMethodDirect(r_method);
985 }
buzbee695d13a2014-04-19 13:32:20 -0700986 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
987 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800988
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700990 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800991 if (cu_->instruction_set == kThumb2 ||
992 cu_->instruction_set == kMips) {
993 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800994 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800995 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
996 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800998
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800999 // Object to generate the slow path for string resolution.
1000 class SlowPath : public LIRSlowPath {
1001 public:
buzbee2700f7e2014-03-07 09:46:20 -08001002 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001003 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
1004 }
1005
1006 void Compile() {
1007 GenerateTargetLabel();
1008
Dave Allisond6ed6422014-04-09 23:36:15 +00001009 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001010
Dave Allisond6ed6422014-04-09 23:36:15 +00001011 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
1012 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
1013 m2l_->MarkSafepointPC(call_inst);
1014 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001015
1016 m2l_->OpUnconditionalBranch(cont_);
1017 }
1018
1019 private:
buzbee2700f7e2014-03-07 09:46:20 -08001020 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001021 };
1022
1023 // Add to list for future.
1024 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001026 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001027 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1028 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001029 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001030 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001031 LIR* target = NewLIR0(kPseudoTargetLabel);
1032 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 }
1034 GenBarrier();
1035 StoreValue(rl_dest, GetReturn(false));
1036 } else {
1037 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001038 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001040 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1041 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 StoreValue(rl_dest, rl_result);
1043 }
1044}
1045
1046/*
1047 * Let helper function take care of everything. Will
1048 * call Class::NewInstanceFromCode(type_idx, method);
1049 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001050void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 FlushAllRegs(); /* Everything to home location */
1052 // alloc will always check for resolution, do we also need to verify
1053 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001054 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001055 const DexFile* dex_file = cu_->dex_file;
1056 CompilerDriver* driver = cu_->compiler_driver;
1057 if (driver->CanAccessInstantiableTypeWithoutChecks(
1058 cu_->method_idx, *dex_file, type_idx)) {
1059 bool is_type_initialized;
1060 bool use_direct_type_ptr;
1061 uintptr_t direct_type_ptr;
1062 if (kEmbedClassInCode &&
1063 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1064 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1065 // The fast path.
1066 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001067 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001069 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1071 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001072 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001073 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1074 }
1075 } else {
1076 // Use the direct pointer.
1077 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001078 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001079 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1080 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001081 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001082 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1083 }
1084 }
1085 } else {
1086 // The slow path.
1087 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001088 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001089 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1090 }
1091 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001093 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001094 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 RegLocation rl_result = GetReturn(false);
1097 StoreValue(rl_dest, rl_result);
1098}
1099
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001100void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001102 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103}
1104
1105// For final classes there are no sub-classes to check and so we can answer the instance-of
1106// question with simple comparisons.
1107void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1108 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001109 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001110 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001111
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 RegLocation object = LoadValue(rl_src, kCoreReg);
1113 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001114 RegStorage result_reg = rl_result.reg;
1115 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 result_reg = AllocTypedTemp(false, kCoreReg);
1117 }
1118 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001119 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120
buzbee2700f7e2014-03-07 09:46:20 -08001121 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1122 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123
1124 LoadCurrMethodDirect(check_class);
1125 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001126 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1127 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 } else {
buzbee695d13a2014-04-19 13:32:20 -07001129 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1130 check_class);
1131 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 int32_t offset_of_type =
1133 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1134 (sizeof(mirror::Class*) * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001135 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 }
1137
1138 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001139 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 if (cu_->instruction_set == kThumb2) {
1141 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001142 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001144 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 } else {
1146 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1147 LoadConstant(result_reg, 1); // eq case - load true
1148 }
1149 LIR* target = NewLIR0(kPseudoTargetLabel);
1150 null_branchover->target = target;
1151 if (ne_branchover != NULL) {
1152 ne_branchover->target = target;
1153 }
1154 FreeTemp(object_class);
1155 FreeTemp(check_class);
1156 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001157 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 FreeTemp(result_reg);
1159 }
1160 StoreValue(rl_dest, rl_result);
1161}
1162
1163void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1164 bool type_known_abstract, bool use_declaring_class,
1165 bool can_assume_type_is_in_dex_cache,
1166 uint32_t type_idx, RegLocation rl_dest,
1167 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001168 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001169 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001170
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 FlushAllRegs();
1172 // May generate a call - use explicit registers
1173 LockCallTemps();
1174 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001175 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 if (needs_access_check) {
1177 // Check we have access to type_idx and if not throw IllegalAccessError,
1178 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001179 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 type_idx, true);
1181 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1182 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1183 } else if (use_declaring_class) {
1184 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001185 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001186 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 } else {
1188 // Load dex cache entry into class_reg (kArg2)
1189 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001190 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1191 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 int32_t offset_of_type =
1193 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1194 * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001195 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 if (!can_assume_type_is_in_dex_cache) {
1197 // Need to test presence of type in dex cache at runtime
1198 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1199 // Not resolved
1200 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001201 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001202 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1204 // Rejoin code paths
1205 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1206 hop_branch->target = hop_target;
1207 }
1208 }
1209 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1210 RegLocation rl_result = GetReturn(false);
1211 if (cu_->instruction_set == kMips) {
1212 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001213 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 }
1215 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1216
1217 /* load object->klass_ */
1218 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001219 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1221 LIR* branchover = NULL;
1222 if (type_known_final) {
1223 // rl_result == ref == null == 0.
1224 if (cu_->instruction_set == kThumb2) {
1225 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001226 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001227 LoadConstant(rl_result.reg, 1); // .eq case - load true
1228 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001229 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001231 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001233 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 }
1235 } else {
1236 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001237 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001238 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 if (!type_known_abstract) {
1240 /* Uses conditional nullification */
1241 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001242 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1244 }
1245 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1246 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001247 if (it != nullptr) {
1248 OpEndIT(it);
1249 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 FreeTemp(r_tgt);
1251 } else {
1252 if (!type_known_abstract) {
1253 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001254 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1256 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001257 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001258 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1259 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1260 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 }
1262 }
1263 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001264 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 /* branch targets here */
1266 LIR* target = NewLIR0(kPseudoTargetLabel);
1267 StoreValue(rl_dest, rl_result);
1268 branch1->target = target;
1269 if (branchover != NULL) {
1270 branchover->target = target;
1271 }
1272}
1273
1274void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1275 bool type_known_final, type_known_abstract, use_declaring_class;
1276 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1277 *cu_->dex_file,
1278 type_idx,
1279 &type_known_final,
1280 &type_known_abstract,
1281 &use_declaring_class);
1282 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1283 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1284
1285 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1286 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1287 } else {
1288 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1289 use_declaring_class, can_assume_type_is_in_dex_cache,
1290 type_idx, rl_dest, rl_src);
1291 }
1292}
1293
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001294void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 bool type_known_final, type_known_abstract, use_declaring_class;
1296 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1297 *cu_->dex_file,
1298 type_idx,
1299 &type_known_final,
1300 &type_known_abstract,
1301 &use_declaring_class);
1302 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1303 // of the exception throw path.
1304 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001305 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 // Verifier type analysis proved this check cast would never cause an exception.
1307 return;
1308 }
1309 FlushAllRegs();
1310 // May generate a call - use explicit registers
1311 LockCallTemps();
1312 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001313 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 if (needs_access_check) {
1315 // Check we have access to type_idx and if not throw IllegalAccessError,
1316 // returns Class* in kRet0
1317 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001318 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 type_idx, TargetReg(kArg1), true);
1320 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1321 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001322 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1323 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 } else {
1325 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001326 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1327 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 int32_t offset_of_type =
1329 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1330 (sizeof(mirror::Class*) * type_idx);
buzbee695d13a2014-04-19 13:32:20 -07001331 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1333 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001334 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1335 LIR* cont = NewLIR0(kPseudoTargetLabel);
1336
1337 // Slow path to initialize the type. Executed if the type is NULL.
1338 class SlowPath : public LIRSlowPath {
1339 public:
1340 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001341 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001342 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1343 class_reg_(class_reg) {
1344 }
1345
1346 void Compile() {
1347 GenerateTargetLabel();
1348
1349 // Call out to helper, which will return resolved type in kArg0
1350 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001351 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001352 m2l_->TargetReg(kArg1), true);
1353 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1354 m2l_->OpUnconditionalBranch(cont_);
1355 }
1356 public:
1357 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001358 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001359 };
1360
buzbee2700f7e2014-03-07 09:46:20 -08001361 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 }
1363 }
1364 // At this point, class_reg (kArg2) has class
1365 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001366
1367 // Slow path for the case where the classes are not equal. In this case we need
1368 // to call a helper function to do the check.
1369 class SlowPath : public LIRSlowPath {
1370 public:
1371 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1372 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1373 }
1374
1375 void Compile() {
1376 GenerateTargetLabel();
1377
1378 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001379 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1380 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001382 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001383 m2l_->TargetReg(kArg1), true);
1384
1385 m2l_->OpUnconditionalBranch(cont_);
1386 }
1387
1388 private:
1389 bool load_;
1390 };
1391
1392 if (type_known_abstract) {
1393 // Easier case, run slow path if target is non-null (slow path will load from target)
1394 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1395 LIR* cont = NewLIR0(kPseudoTargetLabel);
1396 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1397 } else {
1398 // Harder, more common case. We need to generate a forward branch over the load
1399 // if the target is null. If it's non-null we perform the load and branch to the
1400 // slow path if the classes are not equal.
1401
1402 /* Null is OK - continue */
1403 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1404 /* load object->klass_ */
1405 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001406 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001407
1408 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1409 LIR* cont = NewLIR0(kPseudoTargetLabel);
1410
1411 // Add the slow path that will not perform load since this is already done.
1412 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1413
1414 // Set the null check to branch to the continuation.
1415 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 }
1417}
1418
1419void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001420 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 RegLocation rl_result;
1422 if (cu_->instruction_set == kThumb2) {
1423 /*
1424 * NOTE: This is the one place in the code in which we might have
1425 * as many as six live temporary registers. There are 5 in the normal
1426 * set for Arm. Until we have spill capabilities, temporarily add
1427 * lr to the temp set. It is safe to do this locally, but note that
1428 * lr is used explicitly elsewhere in the code generator and cannot
1429 * normally be used as a general temp register.
1430 */
1431 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1432 FreeTemp(TargetReg(kLr)); // and make it available
1433 }
1434 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1435 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1436 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1437 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001438 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1439 RegStorage t_reg = AllocTemp();
1440 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1441 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1442 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 FreeTemp(t_reg);
1444 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001445 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1446 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 }
1448 /*
1449 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1450 * following StoreValueWide might need to allocate a temp register.
1451 * To further work around the lack of a spill capability, explicitly
1452 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1453 * Remove when spill is functional.
1454 */
1455 FreeRegLocTemps(rl_result, rl_src1);
1456 FreeRegLocTemps(rl_result, rl_src2);
1457 StoreValueWide(rl_dest, rl_result);
1458 if (cu_->instruction_set == kThumb2) {
1459 Clobber(TargetReg(kLr));
1460 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1461 }
1462}
1463
1464
1465void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001466 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001467 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468
1469 switch (opcode) {
1470 case Instruction::SHL_LONG:
1471 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001472 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 break;
1474 case Instruction::SHR_LONG:
1475 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001476 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 break;
1478 case Instruction::USHR_LONG:
1479 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001480 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 break;
1482 default:
1483 LOG(FATAL) << "Unexpected case";
1484 }
1485 FlushAllRegs(); /* Send everything to home location */
1486 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1487 RegLocation rl_result = GetReturnWide(false);
1488 StoreValueWide(rl_dest, rl_result);
1489}
1490
1491
1492void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001493 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001494 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 OpKind op = kOpBkpt;
1496 bool is_div_rem = false;
1497 bool check_zero = false;
1498 bool unary = false;
1499 RegLocation rl_result;
1500 bool shift_op = false;
1501 switch (opcode) {
1502 case Instruction::NEG_INT:
1503 op = kOpNeg;
1504 unary = true;
1505 break;
1506 case Instruction::NOT_INT:
1507 op = kOpMvn;
1508 unary = true;
1509 break;
1510 case Instruction::ADD_INT:
1511 case Instruction::ADD_INT_2ADDR:
1512 op = kOpAdd;
1513 break;
1514 case Instruction::SUB_INT:
1515 case Instruction::SUB_INT_2ADDR:
1516 op = kOpSub;
1517 break;
1518 case Instruction::MUL_INT:
1519 case Instruction::MUL_INT_2ADDR:
1520 op = kOpMul;
1521 break;
1522 case Instruction::DIV_INT:
1523 case Instruction::DIV_INT_2ADDR:
1524 check_zero = true;
1525 op = kOpDiv;
1526 is_div_rem = true;
1527 break;
1528 /* NOTE: returns in kArg1 */
1529 case Instruction::REM_INT:
1530 case Instruction::REM_INT_2ADDR:
1531 check_zero = true;
1532 op = kOpRem;
1533 is_div_rem = true;
1534 break;
1535 case Instruction::AND_INT:
1536 case Instruction::AND_INT_2ADDR:
1537 op = kOpAnd;
1538 break;
1539 case Instruction::OR_INT:
1540 case Instruction::OR_INT_2ADDR:
1541 op = kOpOr;
1542 break;
1543 case Instruction::XOR_INT:
1544 case Instruction::XOR_INT_2ADDR:
1545 op = kOpXor;
1546 break;
1547 case Instruction::SHL_INT:
1548 case Instruction::SHL_INT_2ADDR:
1549 shift_op = true;
1550 op = kOpLsl;
1551 break;
1552 case Instruction::SHR_INT:
1553 case Instruction::SHR_INT_2ADDR:
1554 shift_op = true;
1555 op = kOpAsr;
1556 break;
1557 case Instruction::USHR_INT:
1558 case Instruction::USHR_INT_2ADDR:
1559 shift_op = true;
1560 op = kOpLsr;
1561 break;
1562 default:
1563 LOG(FATAL) << "Invalid word arith op: " << opcode;
1564 }
1565 if (!is_div_rem) {
1566 if (unary) {
1567 rl_src1 = LoadValue(rl_src1, kCoreReg);
1568 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001569 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 } else {
1571 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001572 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001573 RegStorage t_reg = AllocTemp();
1574 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 rl_src1 = LoadValue(rl_src1, kCoreReg);
1576 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001577 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 FreeTemp(t_reg);
1579 } else {
1580 rl_src1 = LoadValue(rl_src1, kCoreReg);
1581 rl_src2 = LoadValue(rl_src2, kCoreReg);
1582 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001583 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
1585 }
1586 StoreValue(rl_dest, rl_result);
1587 } else {
Dave Allison70202782013-10-22 17:52:19 -07001588 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 if (cu_->instruction_set == kMips) {
1590 rl_src1 = LoadValue(rl_src1, kCoreReg);
1591 rl_src2 = LoadValue(rl_src2, kCoreReg);
1592 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001593 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 }
buzbee2700f7e2014-03-07 09:46:20 -08001595 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001596 done = true;
1597 } else if (cu_->instruction_set == kThumb2) {
1598 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1599 // Use ARM SDIV instruction for division. For remainder we also need to
1600 // calculate using a MUL and subtract.
1601 rl_src1 = LoadValue(rl_src1, kCoreReg);
1602 rl_src2 = LoadValue(rl_src2, kCoreReg);
1603 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001604 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001605 }
buzbee2700f7e2014-03-07 09:46:20 -08001606 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001607 done = true;
1608 }
1609 }
1610
1611 // If we haven't already generated the code use the callout function.
1612 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001613 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 FlushAllRegs(); /* Send everything to home location */
1615 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001616 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1618 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001619 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
Dave Allison70202782013-10-22 17:52:19 -07001621 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001622 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 if (op == kOpDiv)
1624 rl_result = GetReturn(false);
1625 else
1626 rl_result = GetReturnAlt();
1627 }
1628 StoreValue(rl_dest, rl_result);
1629 }
1630}
1631
1632/*
1633 * The following are the first-level codegen routines that analyze the format
1634 * of each bytecode then either dispatch special purpose codegen routines
1635 * or produce corresponding Thumb instructions directly.
1636 */
1637
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001639static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 x &= x - 1;
1641 return (x & (x - 1)) == 0;
1642}
1643
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1645// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001646bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001647 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1649 return false;
1650 }
1651 // No divide instruction for Arm, so check for more special cases
1652 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001653 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 }
1655 int k = LowestSetBit(lit);
1656 if (k >= 30) {
1657 // Avoid special cases.
1658 return false;
1659 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 rl_src = LoadValue(rl_src, kCoreReg);
1661 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001662 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001663 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 if (lit == 2) {
1665 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001666 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1667 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1668 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001670 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001672 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1673 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 }
1675 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001676 RegStorage t_reg1 = AllocTemp();
1677 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001679 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1680 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001682 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001684 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001686 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001688 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 }
1690 }
1691 StoreValue(rl_dest, rl_result);
1692 return true;
1693}
1694
1695// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1696// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001697bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001698 if (lit < 0) {
1699 return false;
1700 }
1701 if (lit == 0) {
1702 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1703 LoadConstant(rl_result.reg, 0);
1704 StoreValue(rl_dest, rl_result);
1705 return true;
1706 }
1707 if (lit == 1) {
1708 rl_src = LoadValue(rl_src, kCoreReg);
1709 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1710 OpRegCopy(rl_result.reg, rl_src.reg);
1711 StoreValue(rl_dest, rl_result);
1712 return true;
1713 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001714 // There is RegRegRegShift on Arm, so check for more special cases
1715 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001716 return EasyMultiply(rl_src, rl_dest, lit);
1717 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 // Can we simplify this multiplication?
1719 bool power_of_two = false;
1720 bool pop_count_le2 = false;
1721 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001722 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 power_of_two = true;
1724 } else if (IsPopCountLE2(lit)) {
1725 pop_count_le2 = true;
1726 } else if (IsPowerOfTwo(lit + 1)) {
1727 power_of_two_minus_one = true;
1728 } else {
1729 return false;
1730 }
1731 rl_src = LoadValue(rl_src, kCoreReg);
1732 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1733 if (power_of_two) {
1734 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001735 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 } else if (pop_count_le2) {
1737 // Shift and add and shift.
1738 int first_bit = LowestSetBit(lit);
1739 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1740 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1741 } else {
1742 // Reverse subtract: (src << (shift + 1)) - src.
1743 DCHECK(power_of_two_minus_one);
1744 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001745 RegStorage t_reg = AllocTemp();
1746 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1747 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 }
1749 StoreValue(rl_dest, rl_result);
1750 return true;
1751}
1752
1753void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001754 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 RegLocation rl_result;
1756 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1757 int shift_op = false;
1758 bool is_div = false;
1759
1760 switch (opcode) {
1761 case Instruction::RSUB_INT_LIT8:
1762 case Instruction::RSUB_INT: {
1763 rl_src = LoadValue(rl_src, kCoreReg);
1764 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1765 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001766 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001768 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1769 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 }
1771 StoreValue(rl_dest, rl_result);
1772 return;
1773 }
1774
1775 case Instruction::SUB_INT:
1776 case Instruction::SUB_INT_2ADDR:
1777 lit = -lit;
1778 // Intended fallthrough
1779 case Instruction::ADD_INT:
1780 case Instruction::ADD_INT_2ADDR:
1781 case Instruction::ADD_INT_LIT8:
1782 case Instruction::ADD_INT_LIT16:
1783 op = kOpAdd;
1784 break;
1785 case Instruction::MUL_INT:
1786 case Instruction::MUL_INT_2ADDR:
1787 case Instruction::MUL_INT_LIT8:
1788 case Instruction::MUL_INT_LIT16: {
1789 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1790 return;
1791 }
1792 op = kOpMul;
1793 break;
1794 }
1795 case Instruction::AND_INT:
1796 case Instruction::AND_INT_2ADDR:
1797 case Instruction::AND_INT_LIT8:
1798 case Instruction::AND_INT_LIT16:
1799 op = kOpAnd;
1800 break;
1801 case Instruction::OR_INT:
1802 case Instruction::OR_INT_2ADDR:
1803 case Instruction::OR_INT_LIT8:
1804 case Instruction::OR_INT_LIT16:
1805 op = kOpOr;
1806 break;
1807 case Instruction::XOR_INT:
1808 case Instruction::XOR_INT_2ADDR:
1809 case Instruction::XOR_INT_LIT8:
1810 case Instruction::XOR_INT_LIT16:
1811 op = kOpXor;
1812 break;
1813 case Instruction::SHL_INT_LIT8:
1814 case Instruction::SHL_INT:
1815 case Instruction::SHL_INT_2ADDR:
1816 lit &= 31;
1817 shift_op = true;
1818 op = kOpLsl;
1819 break;
1820 case Instruction::SHR_INT_LIT8:
1821 case Instruction::SHR_INT:
1822 case Instruction::SHR_INT_2ADDR:
1823 lit &= 31;
1824 shift_op = true;
1825 op = kOpAsr;
1826 break;
1827 case Instruction::USHR_INT_LIT8:
1828 case Instruction::USHR_INT:
1829 case Instruction::USHR_INT_2ADDR:
1830 lit &= 31;
1831 shift_op = true;
1832 op = kOpLsr;
1833 break;
1834
1835 case Instruction::DIV_INT:
1836 case Instruction::DIV_INT_2ADDR:
1837 case Instruction::DIV_INT_LIT8:
1838 case Instruction::DIV_INT_LIT16:
1839 case Instruction::REM_INT:
1840 case Instruction::REM_INT_2ADDR:
1841 case Instruction::REM_INT_LIT8:
1842 case Instruction::REM_INT_LIT16: {
1843 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001844 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001845 return;
1846 }
buzbee11b63d12013-08-27 07:34:17 -07001847 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001849 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 (opcode == Instruction::DIV_INT_LIT16)) {
1851 is_div = true;
1852 } else {
1853 is_div = false;
1854 }
buzbee11b63d12013-08-27 07:34:17 -07001855 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1856 return;
1857 }
Dave Allison70202782013-10-22 17:52:19 -07001858
1859 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 if (cu_->instruction_set == kMips) {
1861 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001862 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001863 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001864 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001865 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1866 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001867 } else if (cu_->instruction_set == kThumb2) {
1868 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1869 // Use ARM SDIV instruction for division. For remainder we also need to
1870 // calculate using a MUL and subtract.
1871 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001872 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001873 done = true;
1874 }
1875 }
1876
1877 if (!done) {
1878 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1880 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001881 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001882 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1883 if (is_div)
1884 rl_result = GetReturn(false);
1885 else
1886 rl_result = GetReturnAlt();
1887 }
1888 StoreValue(rl_dest, rl_result);
1889 return;
1890 }
1891 default:
1892 LOG(FATAL) << "Unexpected opcode " << opcode;
1893 }
1894 rl_src = LoadValue(rl_src, kCoreReg);
1895 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001896 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001898 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001900 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001901 }
1902 StoreValue(rl_dest, rl_result);
1903}
1904
1905void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001906 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 RegLocation rl_result;
1908 OpKind first_op = kOpBkpt;
1909 OpKind second_op = kOpBkpt;
1910 bool call_out = false;
1911 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001912 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001913 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914
1915 switch (opcode) {
1916 case Instruction::NOT_LONG:
1917 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1918 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1919 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001920 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1921 RegStorage t_reg = AllocTemp();
1922 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1923 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1924 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 FreeTemp(t_reg);
1926 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001927 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1928 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 }
1930 StoreValueWide(rl_dest, rl_result);
1931 return;
1932 case Instruction::ADD_LONG:
1933 case Instruction::ADD_LONG_2ADDR:
1934 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001935 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 return;
1937 }
1938 first_op = kOpAdd;
1939 second_op = kOpAdc;
1940 break;
1941 case Instruction::SUB_LONG:
1942 case Instruction::SUB_LONG_2ADDR:
1943 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001944 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 return;
1946 }
1947 first_op = kOpSub;
1948 second_op = kOpSbc;
1949 break;
1950 case Instruction::MUL_LONG:
1951 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001952 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001953 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 return;
1955 } else {
1956 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001957 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001958 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 }
1960 break;
1961 case Instruction::DIV_LONG:
1962 case Instruction::DIV_LONG_2ADDR:
1963 call_out = true;
1964 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001965 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001966 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 break;
1968 case Instruction::REM_LONG:
1969 case Instruction::REM_LONG_2ADDR:
1970 call_out = true;
1971 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001972 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001974 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 break;
1976 case Instruction::AND_LONG_2ADDR:
1977 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001978 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001979 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 }
1981 first_op = kOpAnd;
1982 second_op = kOpAnd;
1983 break;
1984 case Instruction::OR_LONG:
1985 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001986 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001987 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988 return;
1989 }
1990 first_op = kOpOr;
1991 second_op = kOpOr;
1992 break;
1993 case Instruction::XOR_LONG:
1994 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001995 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001996 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 return;
1998 }
1999 first_op = kOpXor;
2000 second_op = kOpXor;
2001 break;
2002 case Instruction::NEG_LONG: {
2003 GenNegLong(rl_dest, rl_src2);
2004 return;
2005 }
2006 default:
2007 LOG(FATAL) << "Invalid long arith op";
2008 }
2009 if (!call_out) {
2010 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
2011 } else {
2012 FlushAllRegs(); /* Send everything to home location */
2013 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08002014 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
2015 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
2016 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2017 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07002018 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08002019 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 // NOTE: callout here is not a safepoint
2021 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2022 } else {
2023 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2024 }
2025 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002026 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 rl_result = GetReturnWide(false);
2028 else
2029 rl_result = GetReturnWideAlt();
2030 StoreValueWide(rl_dest, rl_result);
2031 }
2032}
2033
Ian Rogersdd7624d2014-03-14 17:43:00 -07002034void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002035 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 /*
2037 * Don't optimize the register usage since it calls out to support
2038 * functions
2039 */
2040 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2042 if (rl_dest.wide) {
2043 RegLocation rl_result;
2044 rl_result = GetReturnWide(rl_dest.fp);
2045 StoreValueWide(rl_dest, rl_result);
2046 } else {
2047 RegLocation rl_result;
2048 rl_result = GetReturn(rl_dest.fp);
2049 StoreValue(rl_dest, rl_result);
2050 }
2051}
2052
2053/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002054void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002055 if (Runtime::Current()->ExplicitSuspendChecks()) {
2056 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2057 return;
2058 }
2059 FlushAllRegs();
2060 LIR* branch = OpTestSuspend(NULL);
2061 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2062 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2063 current_dalvik_offset_);
2064 branch->target = target;
2065 suspend_launchpads_.Insert(target);
2066 } else {
2067 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2068 return;
2069 }
2070 FlushAllRegs(); // TODO: needed?
2071 LIR* inst = CheckSuspendUsingLoad();
2072 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074}
2075
2076/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002077void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002078 if (Runtime::Current()->ExplicitSuspendChecks()) {
2079 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2080 OpUnconditionalBranch(target);
2081 return;
2082 }
2083 OpTestSuspend(target);
2084 LIR* launch_pad =
2085 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2086 current_dalvik_offset_);
2087 FlushAllRegs();
2088 OpUnconditionalBranch(launch_pad);
2089 suspend_launchpads_.Insert(launch_pad);
2090 } else {
2091 // For the implicit suspend check, just perform the trigger
2092 // load and branch to the target.
2093 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2094 OpUnconditionalBranch(target);
2095 return;
2096 }
2097 FlushAllRegs();
2098 LIR* inst = CheckSuspendUsingLoad();
2099 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102}
2103
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002104/* Call out to helper assembly routine that will null check obj and then lock it. */
2105void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2106 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002107 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002108}
2109
2110/* Call out to helper assembly routine that will null check obj and then unlock it. */
2111void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2112 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002113 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002114}
2115
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002116/* Generic code for generating a wide constant into a VR. */
2117void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2118 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002119 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002120 StoreValueWide(rl_dest, rl_result);
2121}
2122
Brian Carlstrom7940e442013-07-12 13:46:57 -07002123} // namespace art