blob: 0533fbfcd7c80fc519fe1f67a9d9a56daeaf4d6f [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 */
16
17#include "dex/compiler_ir.h"
18#include "dex/compiler_internals.h"
19#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
buzbee0d829482013-10-11 15:24:55 -070045// TODO: need to do some work to split out targets with
Brian Carlstrom7940e442013-07-12 13:46:57 -070046// condition codes and those without
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070047LIR* Mir2Lir::GenCheck(ConditionCode c_code, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 DCHECK_NE(cu_->instruction_set, kMips);
49 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_);
50 LIR* branch = OpCondBranch(c_code, tgt);
51 // Remember branch target - will process later
52 throw_launchpads_.Insert(tgt);
53 return branch;
54}
55
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070056LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, int reg, int imm_val, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg, imm_val);
58 LIR* branch;
59 if (c_code == kCondAl) {
60 branch = OpUnconditionalBranch(tgt);
61 } else {
62 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
63 }
64 // Remember branch target - will process later
65 throw_launchpads_.Insert(tgt);
66 return branch;
67}
68
69/* Perform null-check on a register. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070070LIR* Mir2Lir::GenNullCheck(int s_reg, int m_reg, int opt_flags) {
Ian Rogersa9a82542013-10-04 11:17:26 -070071 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 return NULL;
73 }
74 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
75}
76
77/* Perform check on two registers */
78LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070079 ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
81 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
82 // Remember branch target - will process later
83 throw_launchpads_.Insert(tgt);
84 return branch;
85}
86
87void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
88 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070089 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 ConditionCode cond;
91 switch (opcode) {
92 case Instruction::IF_EQ:
93 cond = kCondEq;
94 break;
95 case Instruction::IF_NE:
96 cond = kCondNe;
97 break;
98 case Instruction::IF_LT:
99 cond = kCondLt;
100 break;
101 case Instruction::IF_GE:
102 cond = kCondGe;
103 break;
104 case Instruction::IF_GT:
105 cond = kCondGt;
106 break;
107 case Instruction::IF_LE:
108 cond = kCondLe;
109 break;
110 default:
111 cond = static_cast<ConditionCode>(0);
112 LOG(FATAL) << "Unexpected opcode " << opcode;
113 }
114
115 // Normalize such that if either operand is constant, src2 will be constant
116 if (rl_src1.is_const) {
117 RegLocation rl_temp = rl_src1;
118 rl_src1 = rl_src2;
119 rl_src2 = rl_temp;
120 cond = FlipComparisonOrder(cond);
121 }
122
123 rl_src1 = LoadValue(rl_src1, kCoreReg);
124 // Is this really an immediate comparison?
125 if (rl_src2.is_const) {
126 // If it's already live in a register or not easily materialized, just keep going
127 RegLocation rl_temp = UpdateLoc(rl_src2);
128 if ((rl_temp.location == kLocDalvikFrame) &&
129 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
130 // OK - convert this to a compare immediate and branch
Bill Buzbee86ec5202014-02-26 19:03:09 +0000131 OpCmpImmBranch(cond, rl_src1.low_reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 return;
133 }
134 }
135 rl_src2 = LoadValue(rl_src2, kCoreReg);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000136 OpCmpBranch(cond, rl_src1.low_reg, rl_src2.low_reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137}
138
139void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700140 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 ConditionCode cond;
142 rl_src = LoadValue(rl_src, kCoreReg);
143 switch (opcode) {
144 case Instruction::IF_EQZ:
145 cond = kCondEq;
146 break;
147 case Instruction::IF_NEZ:
148 cond = kCondNe;
149 break;
150 case Instruction::IF_LTZ:
151 cond = kCondLt;
152 break;
153 case Instruction::IF_GEZ:
154 cond = kCondGe;
155 break;
156 case Instruction::IF_GTZ:
157 cond = kCondGt;
158 break;
159 case Instruction::IF_LEZ:
160 cond = kCondLe;
161 break;
162 default:
163 cond = static_cast<ConditionCode>(0);
164 LOG(FATAL) << "Unexpected opcode " << opcode;
165 }
Bill Buzbee86ec5202014-02-26 19:03:09 +0000166 OpCmpImmBranch(cond, rl_src.low_reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167}
168
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700169void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
171 if (rl_src.location == kLocPhysReg) {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000172 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000174 LoadValueDirect(rl_src, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 }
Bill Buzbee86ec5202014-02-26 19:03:09 +0000176 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 StoreValueWide(rl_dest, rl_result);
178}
179
180void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700182 rl_src = LoadValue(rl_src, kCoreReg);
183 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
184 OpKind op = kOpInvalid;
185 switch (opcode) {
186 case Instruction::INT_TO_BYTE:
187 op = kOp2Byte;
188 break;
189 case Instruction::INT_TO_SHORT:
190 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700192 case Instruction::INT_TO_CHAR:
193 op = kOp2Char;
194 break;
195 default:
196 LOG(ERROR) << "Bad int conversion type";
197 }
Bill Buzbee86ec5202014-02-26 19:03:09 +0000198 OpRegReg(op, rl_result.low_reg, rl_src.low_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700199 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200}
201
202/*
203 * Let helper function take care of everything. Will call
204 * Array::AllocFromCode(type_idx, method, count);
205 * Note: AllocFromCode will handle checks for errNegativeArraySize.
206 */
207void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700208 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700210 ThreadOffset func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800211 const DexFile* dex_file = cu_->dex_file;
212 CompilerDriver* driver = cu_->compiler_driver;
213 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800215 bool is_type_initialized; // Ignored as an array does not have an initializer.
216 bool use_direct_type_ptr;
217 uintptr_t direct_type_ptr;
218 if (kEmbedClassInCode &&
219 driver->CanEmbedTypeInCode(*dex_file, type_idx,
220 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
221 // The fast path.
222 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800223 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800224 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
225 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
226 } else {
227 // Use the direct pointer.
228 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
229 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
230 }
231 } else {
232 // The slow path.
233 DCHECK_EQ(func_offset.Int32Value(), -1);
234 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
235 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
236 }
237 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700239 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800240 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 RegLocation rl_result = GetReturn(false);
243 StoreValue(rl_dest, rl_result);
244}
245
246/*
247 * Similar to GenNewArray, but with post-allocation initialization.
248 * Verifier guarantees we're dealing with an array class. Current
249 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
250 * Current code also throws internal unimp if not 'L', '[' or 'I'.
251 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700252void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 int elems = info->num_arg_words;
254 int type_idx = info->index;
255 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700256 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
258 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700259 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700261 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 }
263 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
264 FreeTemp(TargetReg(kArg2));
265 FreeTemp(TargetReg(kArg1));
266 /*
267 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
268 * return region. Because AllocFromCode placed the new array
269 * in kRet0, we'll just lock it into place. When debugger support is
270 * added, it may be necessary to additionally copy all return
271 * values to a home location in thread-local storage
272 */
273 LockTemp(TargetReg(kRet0));
274
275 // TODO: use the correct component size, currently all supported types
276 // share array alignment with ints (see comment at head of function)
277 size_t component_size = sizeof(int32_t);
278
279 // Having a range of 0 is legal
280 if (info->is_range && (elems > 0)) {
281 /*
282 * Bit of ugliness here. We're going generate a mem copy loop
283 * on the register range, but it is possible that some regs
284 * in the range have been promoted. This is unlikely, but
285 * before generating the copy, we'll just force a flush
286 * of any regs in the source range that have been promoted to
287 * home location.
288 */
289 for (int i = 0; i < elems; i++) {
290 RegLocation loc = UpdateLoc(info->args[i]);
291 if (loc.location == kLocPhysReg) {
292 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
Bill Buzbee86ec5202014-02-26 19:03:09 +0000293 loc.low_reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 }
295 }
296 /*
297 * TUNING note: generated code here could be much improved, but
298 * this is an uncommon operation and isn't especially performance
299 * critical.
300 */
301 int r_src = AllocTemp();
302 int r_dst = AllocTemp();
303 int r_idx = AllocTemp();
304 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700305 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 case kThumb2:
307 r_val = TargetReg(kLr);
308 break;
309 case kX86:
310 FreeTemp(TargetReg(kRet0));
311 r_val = AllocTemp();
312 break;
313 case kMips:
314 r_val = AllocTemp();
315 break;
316 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
317 }
318 // Set up source pointer
319 RegLocation rl_first = info->args[0];
320 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
321 // Set up the target pointer
322 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
323 mirror::Array::DataOffset(component_size).Int32Value());
324 // Set up the loop counter (known to be > 0)
325 LoadConstant(r_idx, elems - 1);
326 // Generate the copy loop. Going backwards for convenience
327 LIR* target = NewLIR0(kPseudoTargetLabel);
328 // Copy next element
329 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
330 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
331 FreeTemp(r_val);
332 OpDecAndBranch(kCondGe, r_idx, target);
333 if (cu_->instruction_set == kX86) {
334 // Restore the target pointer
335 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
336 -mirror::Array::DataOffset(component_size).Int32Value());
337 }
338 } else if (!info->is_range) {
339 // TUNING: interleave
340 for (int i = 0; i < elems; i++) {
341 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
342 StoreBaseDisp(TargetReg(kRet0),
343 mirror::Array::DataOffset(component_size).Int32Value() +
Bill Buzbee86ec5202014-02-26 19:03:09 +0000344 i * 4, rl_arg.low_reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 // If the LoadValue caused a temp to be allocated, free it
Bill Buzbee86ec5202014-02-26 19:03:09 +0000346 if (IsTemp(rl_arg.low_reg)) {
347 FreeTemp(rl_arg.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 }
349 }
350 }
351 if (info->result.location != kLocInvalid) {
352 StoreValue(info->result, GetReturn(false /* not fp */));
353 }
354}
355
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800356//
357// Slow path to ensure a class is initialized for sget/sput.
358//
359class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
360 public:
361 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
362 int storage_index, int r_base) :
363 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
364 r_base_(r_base) {
365 }
366
367 void Compile() {
368 LIR* unresolved_target = GenerateTargetLabel();
369 uninit_->target = unresolved_target;
370 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
371 storage_index_, true);
372 // Copy helper's result into r_base, a no-op on all but MIPS.
373 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
374
375 m2l_->OpUnconditionalBranch(cont_);
376 }
377
378 private:
379 LIR* const uninit_;
380 const int storage_index_;
381 const int r_base_;
382};
383
Ian Rogers9c86a022014-02-21 16:40:21 +0000384void Mir2Lir::GenSput(uint32_t field_idx, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700385 bool is_object) {
Ian Rogers9c86a022014-02-21 16:40:21 +0000386 int field_offset;
387 int storage_index;
388 bool is_volatile;
389 bool is_referrers_class;
390 bool is_initialized;
391 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
392 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), true,
393 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
394 if (fast_path && !SLOW_FIELD_PATH) {
395 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800396 int r_base;
Ian Rogers9c86a022014-02-21 16:40:21 +0000397 if (is_referrers_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 // Fast path, static storage base is this method's class
399 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800400 r_base = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000401 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800402 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000403 if (IsTemp(rl_method.low_reg)) {
404 FreeTemp(rl_method.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 }
406 } else {
407 // Medium path, static storage base in a different class which requires checks that the other
408 // class is initialized.
409 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Ian Rogers9c86a022014-02-21 16:40:21 +0000410 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 // May do runtime call so everything to home locations.
412 FlushAllRegs();
413 // Using fixed register to sync with possible call to runtime support.
414 int r_method = TargetReg(kArg1);
415 LockTemp(r_method);
416 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800417 r_base = TargetReg(kArg0);
418 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800420 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
421 r_base);
422 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Ian Rogers9c86a022014-02-21 16:40:21 +0000423 sizeof(int32_t*) * storage_index, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800424 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Ian Rogers9c86a022014-02-21 16:40:21 +0000425 if (!is_initialized) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800426 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800427
428 // The slow path is invoked if the r_base is NULL or the class pointed
429 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800430 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
431 int r_tmp = TargetReg(kArg2);
432 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800433 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800434 mirror::Class::StatusOffset().Int32Value(),
435 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800436 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800437
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800438 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
439 unresolved_branch, uninit_branch, cont,
Ian Rogers9c86a022014-02-21 16:40:21 +0000440 storage_index, r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800441
442 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 FreeTemp(r_method);
445 }
446 // rBase now holds static storage base
447 if (is_long_or_double) {
448 rl_src = LoadValueWide(rl_src, kAnyReg);
449 } else {
450 rl_src = LoadValue(rl_src, kAnyReg);
451 }
Ian Rogers9c86a022014-02-21 16:40:21 +0000452 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 GenMemBarrier(kStoreStore);
454 }
455 if (is_long_or_double) {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000456 StoreBaseDispWide(r_base, field_offset, rl_src.low_reg,
457 rl_src.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000459 StoreWordDisp(r_base, field_offset, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 }
Ian Rogers9c86a022014-02-21 16:40:21 +0000461 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 GenMemBarrier(kStoreLoad);
463 }
464 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000465 MarkGCCard(rl_src.low_reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800467 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 } else {
469 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700470 ThreadOffset setter_offset =
471 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
472 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
473 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Ian Rogers9c86a022014-02-21 16:40:21 +0000474 CallRuntimeHelperImmRegLocation(setter_offset, field_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 }
476}
477
Ian Rogers9c86a022014-02-21 16:40:21 +0000478void Mir2Lir::GenSget(uint32_t field_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700479 bool is_long_or_double, bool is_object) {
Ian Rogers9c86a022014-02-21 16:40:21 +0000480 int field_offset;
481 int storage_index;
482 bool is_volatile;
483 bool is_referrers_class;
484 bool is_initialized;
485 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
486 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), false,
487 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
488 if (fast_path && !SLOW_FIELD_PATH) {
489 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800490 int r_base;
Ian Rogers9c86a022014-02-21 16:40:21 +0000491 if (is_referrers_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 // Fast path, static storage base is this method's class
493 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800494 r_base = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000495 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800496 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 } else {
498 // Medium path, static storage base in a different class which requires checks that the other
499 // class is initialized
Ian Rogers9c86a022014-02-21 16:40:21 +0000500 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 // May do runtime call so everything to home locations.
502 FlushAllRegs();
503 // Using fixed register to sync with possible call to runtime support.
504 int r_method = TargetReg(kArg1);
505 LockTemp(r_method);
506 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800507 r_base = TargetReg(kArg0);
508 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800510 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
511 r_base);
512 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Ian Rogers9c86a022014-02-21 16:40:21 +0000513 sizeof(int32_t*) * storage_index, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800514 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Ian Rogers9c86a022014-02-21 16:40:21 +0000515 if (!is_initialized) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800516 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800517
518 // The slow path is invoked if the r_base is NULL or the class pointed
519 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800520 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
521 int r_tmp = TargetReg(kArg2);
522 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800523 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800524 mirror::Class::StatusOffset().Int32Value(),
525 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800526 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800527
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800528 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
529 unresolved_branch, uninit_branch, cont,
Ian Rogers9c86a022014-02-21 16:40:21 +0000530 storage_index, r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800531
532 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 FreeTemp(r_method);
535 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800536 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Ian Rogers9c86a022014-02-21 16:40:21 +0000538 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 GenMemBarrier(kLoadLoad);
540 }
541 if (is_long_or_double) {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000542 LoadBaseDispWide(r_base, field_offset, rl_result.low_reg,
543 rl_result.high_reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000545 LoadWordDisp(r_base, field_offset, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800547 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 if (is_long_or_double) {
549 StoreValueWide(rl_dest, rl_result);
550 } else {
551 StoreValue(rl_dest, rl_result);
552 }
553 } else {
554 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700555 ThreadOffset getterOffset =
556 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
557 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
558 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Ian Rogers9c86a022014-02-21 16:40:21 +0000559 CallRuntimeHelperImm(getterOffset, field_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 if (is_long_or_double) {
561 RegLocation rl_result = GetReturnWide(rl_dest.fp);
562 StoreValueWide(rl_dest, rl_result);
563 } else {
564 RegLocation rl_result = GetReturn(rl_dest.fp);
565 StoreValue(rl_dest, rl_result);
566 }
567 }
568}
569
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800570// Generate code for all slow paths.
571void Mir2Lir::HandleSlowPaths() {
572 int n = slow_paths_.Size();
573 for (int i = 0; i < n; ++i) {
574 LIRSlowPath* slowpath = slow_paths_.Get(i);
575 slowpath->Compile();
576 }
577 slow_paths_.Reset();
578}
579
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700580void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700582 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 for (int i = 0; i < num_elems; i++) {
584 ResetRegPool();
585 ResetDefTracking();
586 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700587 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 current_dalvik_offset_ = lab->operands[1];
589 AppendLIR(lab);
590 int r_tgt = CallHelperSetup(helper_offset);
591 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
592 OpUnconditionalBranch(resume_lab);
593 }
594}
595
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700596void Mir2Lir::HandleIntrinsicLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 int num_elems = intrinsic_launchpads_.Size();
598 for (int i = 0; i < num_elems; i++) {
599 ResetRegPool();
600 ResetDefTracking();
601 LIR* lab = intrinsic_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700602 CallInfo* info = reinterpret_cast<CallInfo*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 current_dalvik_offset_ = info->offset;
604 AppendLIR(lab);
605 // NOTE: GenInvoke handles MarkSafepointPC
606 GenInvoke(info);
buzbee0d829482013-10-11 15:24:55 -0700607 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 if (resume_lab != NULL) {
609 OpUnconditionalBranch(resume_lab);
610 }
611 }
612}
613
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700614void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 int num_elems = throw_launchpads_.Size();
616 for (int i = 0; i < num_elems; i++) {
617 ResetRegPool();
618 ResetDefTracking();
619 LIR* lab = throw_launchpads_.Get(i);
620 current_dalvik_offset_ = lab->operands[1];
621 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700622 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 int v1 = lab->operands[2];
624 int v2 = lab->operands[3];
625 bool target_x86 = (cu_->instruction_set == kX86);
626 switch (lab->operands[0]) {
627 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700628 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700630 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
632 if (target_x86) {
633 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
634 } else {
635 OpRegCopy(TargetReg(kArg1), v1);
636 }
637 // Make sure the following LoadConstant doesn't mess with kArg1.
638 LockTemp(TargetReg(kArg1));
639 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700640 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 break;
642 case kThrowArrayBounds:
643 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
644 if (v2 != TargetReg(kArg0)) {
645 OpRegCopy(TargetReg(kArg0), v1);
646 if (target_x86) {
647 // x86 leaves the array pointer in v2, so load the array length that the handler expects
648 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
649 } else {
650 OpRegCopy(TargetReg(kArg1), v2);
651 }
652 } else {
653 if (v1 == TargetReg(kArg1)) {
654 // Swap v1 and v2, using kArg2 as a temp
655 OpRegCopy(TargetReg(kArg2), v1);
656 if (target_x86) {
657 // x86 leaves the array pointer in v2; load the array length that the handler expects
658 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
659 } else {
660 OpRegCopy(TargetReg(kArg1), v2);
661 }
662 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
663 } else {
664 if (target_x86) {
665 // x86 leaves the array pointer in v2; load the array length that the handler expects
666 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
667 } else {
668 OpRegCopy(TargetReg(kArg1), v2);
669 }
670 OpRegCopy(TargetReg(kArg0), v1);
671 }
672 }
Ian Rogers848871b2013-08-05 10:56:33 -0700673 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 break;
675 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700676 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 break;
678 case kThrowNoSuchMethod:
679 OpRegCopy(TargetReg(kArg0), v1);
680 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700681 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 break;
683 case kThrowStackOverflow:
Ian Rogers848871b2013-08-05 10:56:33 -0700684 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 // Restore stack alignment
686 if (target_x86) {
687 OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
688 } else {
689 OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
690 }
691 break;
692 default:
693 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
694 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000695 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 int r_tgt = CallHelperSetup(func_offset);
697 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
698 }
699}
700
Ian Rogers9c86a022014-02-21 16:40:21 +0000701void Mir2Lir::GenIGet(uint32_t field_idx, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700703 bool is_object) {
Ian Rogers9c86a022014-02-21 16:40:21 +0000704 int field_offset;
705 bool is_volatile;
706
707 bool fast_path = FastInstance(field_idx, false, &field_offset, &is_volatile);
708
709 if (fast_path && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 RegLocation rl_result;
711 RegisterClass reg_class = oat_reg_class_by_size(size);
Ian Rogers9c86a022014-02-21 16:40:21 +0000712 DCHECK_GE(field_offset, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 rl_obj = LoadValue(rl_obj, kCoreReg);
714 if (is_long_or_double) {
715 DCHECK(rl_dest.wide);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000716 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 if (cu_->instruction_set == kX86) {
718 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000719 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
720 LoadBaseDispWide(rl_obj.low_reg, field_offset, rl_result.low_reg,
721 rl_result.high_reg, rl_obj.s_reg_low);
Ian Rogers9c86a022014-02-21 16:40:21 +0000722 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 GenMemBarrier(kLoadLoad);
724 }
725 } else {
726 int reg_ptr = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000727 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000729 LoadBaseDispWide(reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
Ian Rogers9c86a022014-02-21 16:40:21 +0000730 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 GenMemBarrier(kLoadLoad);
732 }
733 FreeTemp(reg_ptr);
734 }
735 StoreValueWide(rl_dest, rl_result);
736 } else {
737 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000738 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
739 LoadBaseDisp(rl_obj.low_reg, field_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 kWord, rl_obj.s_reg_low);
Ian Rogers9c86a022014-02-21 16:40:21 +0000741 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 GenMemBarrier(kLoadLoad);
743 }
744 StoreValue(rl_dest, rl_result);
745 }
746 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700747 ThreadOffset getterOffset =
748 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
749 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
750 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Ian Rogers9c86a022014-02-21 16:40:21 +0000751 CallRuntimeHelperImmRegLocation(getterOffset, field_idx, rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 if (is_long_or_double) {
753 RegLocation rl_result = GetReturnWide(rl_dest.fp);
754 StoreValueWide(rl_dest, rl_result);
755 } else {
756 RegLocation rl_result = GetReturn(rl_dest.fp);
757 StoreValue(rl_dest, rl_result);
758 }
759 }
760}
761
Ian Rogers9c86a022014-02-21 16:40:21 +0000762void Mir2Lir::GenIPut(uint32_t field_idx, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700764 bool is_object) {
Ian Rogers9c86a022014-02-21 16:40:21 +0000765 int field_offset;
766 bool is_volatile;
767
768 bool fast_path = FastInstance(field_idx, true, &field_offset, &is_volatile);
769 if (fast_path && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 RegisterClass reg_class = oat_reg_class_by_size(size);
Ian Rogers9c86a022014-02-21 16:40:21 +0000771 DCHECK_GE(field_offset, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 rl_obj = LoadValue(rl_obj, kCoreReg);
773 if (is_long_or_double) {
774 int reg_ptr;
775 rl_src = LoadValueWide(rl_src, kAnyReg);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000776 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 reg_ptr = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000778 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
Ian Rogers9c86a022014-02-21 16:40:21 +0000779 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 GenMemBarrier(kStoreStore);
781 }
Bill Buzbee86ec5202014-02-26 19:03:09 +0000782 StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
Ian Rogers9c86a022014-02-21 16:40:21 +0000783 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 GenMemBarrier(kLoadLoad);
785 }
786 FreeTemp(reg_ptr);
787 } else {
788 rl_src = LoadValue(rl_src, reg_class);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000789 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
Ian Rogers9c86a022014-02-21 16:40:21 +0000790 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 GenMemBarrier(kStoreStore);
792 }
Bill Buzbee86ec5202014-02-26 19:03:09 +0000793 StoreBaseDisp(rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
Ian Rogers9c86a022014-02-21 16:40:21 +0000794 if (is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 GenMemBarrier(kLoadLoad);
796 }
797 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee86ec5202014-02-26 19:03:09 +0000798 MarkGCCard(rl_src.low_reg, rl_obj.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 }
800 }
801 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700802 ThreadOffset setter_offset =
803 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
804 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
805 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Ian Rogers9c86a022014-02-21 16:40:21 +0000806 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_idx, rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 }
808}
809
Ian Rogersa9a82542013-10-04 11:17:26 -0700810void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
811 RegLocation rl_src) {
812 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
813 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
814 (opt_flags & MIR_IGNORE_NULL_CHECK));
815 ThreadOffset helper = needs_range_check
816 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
817 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
818 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
819 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
820}
821
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700822void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 RegLocation rl_method = LoadCurrMethod();
824 int res_reg = AllocTemp();
825 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
826 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
827 *cu_->dex_file,
828 type_idx)) {
829 // Call out to helper which resolves type and verifies access.
830 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700831 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Bill Buzbee86ec5202014-02-26 19:03:09 +0000832 type_idx, rl_method.low_reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 RegLocation rl_result = GetReturn(false);
834 StoreValue(rl_dest, rl_result);
835 } else {
836 // We're don't need access checks, load type from dex cache
837 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700838 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000839 LoadWordDisp(rl_method.low_reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 int32_t offset_of_type =
841 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
842 * type_idx);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000843 LoadWordDisp(res_reg, offset_of_type, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
845 type_idx) || SLOW_TYPE_PATH) {
846 // Slow path, at runtime test if type is null and if so initialize
847 FlushAllRegs();
Bill Buzbee86ec5202014-02-26 19:03:09 +0000848 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.low_reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800849 LIR* cont = NewLIR0(kPseudoTargetLabel);
850
851 // Object to generate the slow path for class resolution.
852 class SlowPath : public LIRSlowPath {
853 public:
854 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
855 const RegLocation& rl_method, const RegLocation& rl_result) :
856 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
857 rl_method_(rl_method), rl_result_(rl_result) {
858 }
859
860 void Compile() {
861 GenerateTargetLabel();
862
863 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
Bill Buzbee86ec5202014-02-26 19:03:09 +0000864 rl_method_.low_reg, true);
865 m2l_->OpRegCopy(rl_result_.low_reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800866
867 m2l_->OpUnconditionalBranch(cont_);
868 }
869
870 private:
871 const int type_idx_;
872 const RegLocation rl_method_;
873 const RegLocation rl_result_;
874 };
875
876 // Add to list for future.
877 AddSlowPath(new (arena_) SlowPath(this, branch, cont,
878 type_idx, rl_method, rl_result));
879
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800881 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 // Fast path, we're done - just store result
883 StoreValue(rl_dest, rl_result);
884 }
885 }
886}
887
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700888void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 /* NOTE: Most strings should be available at compile time */
890 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
891 (sizeof(mirror::String*) * string_idx);
892 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
893 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
894 // slow path, resolve string if not in dex cache
895 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700896 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800897
898 // If the Method* is already in a register, we can save a copy.
899 RegLocation rl_method = mir_graph_->GetMethodLoc();
900 int r_method;
901 if (rl_method.location == kLocPhysReg) {
902 // A temp would conflict with register use below.
Bill Buzbee86ec5202014-02-26 19:03:09 +0000903 DCHECK(!IsTemp(rl_method.low_reg));
904 r_method = rl_method.low_reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800905 } else {
906 r_method = TargetReg(kArg2);
907 LoadCurrMethodDirect(r_method);
908 }
909 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
910 TargetReg(kArg0));
911
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800914 if (cu_->instruction_set == kThumb2 ||
915 cu_->instruction_set == kMips) {
916 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800917 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800918 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
919 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800921
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800922 // Object to generate the slow path for string resolution.
923 class SlowPath : public LIRSlowPath {
924 public:
925 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
926 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
927 }
928
929 void Compile() {
930 GenerateTargetLabel();
931
932 int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
933
934 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
935 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
936 m2l_->MarkSafepointPC(call_inst);
937 m2l_->FreeTemp(r_tgt);
938
939 m2l_->OpUnconditionalBranch(cont_);
940 }
941
942 private:
943 int r_method_;
944 };
945
946 // Add to list for future.
947 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 } else {
949 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800950 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
951 LoadConstant(TargetReg(kArg1), string_idx);
952 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
Ian Rogers7655f292013-07-29 11:07:13 -0700953 TargetReg(kArg1), true);
Mark Mendell766e9292014-01-27 07:55:47 -0800954 LIR* target = NewLIR0(kPseudoTargetLabel);
955 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 }
957 GenBarrier();
958 StoreValue(rl_dest, GetReturn(false));
959 } else {
960 RegLocation rl_method = LoadCurrMethod();
961 int res_reg = AllocTemp();
962 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000963 LoadWordDisp(rl_method.low_reg,
Brian Carlstromea46f952013-07-30 01:26:50 -0700964 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Bill Buzbee86ec5202014-02-26 19:03:09 +0000965 LoadWordDisp(res_reg, offset_of_string, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 StoreValue(rl_dest, rl_result);
967 }
968}
969
970/*
971 * Let helper function take care of everything. Will
972 * call Class::NewInstanceFromCode(type_idx, method);
973 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700974void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 FlushAllRegs(); /* Everything to home location */
976 // alloc will always check for resolution, do we also need to verify
977 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700978 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800979 const DexFile* dex_file = cu_->dex_file;
980 CompilerDriver* driver = cu_->compiler_driver;
981 if (driver->CanAccessInstantiableTypeWithoutChecks(
982 cu_->method_idx, *dex_file, type_idx)) {
983 bool is_type_initialized;
984 bool use_direct_type_ptr;
985 uintptr_t direct_type_ptr;
986 if (kEmbedClassInCode &&
987 driver->CanEmbedTypeInCode(*dex_file, type_idx,
988 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
989 // The fast path.
990 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800991 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800992 if (!is_type_initialized) {
993 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
994 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
995 } else {
996 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
997 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
998 }
999 } else {
1000 // Use the direct pointer.
1001 if (!is_type_initialized) {
1002 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1003 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1004 } else {
1005 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1006 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1007 }
1008 }
1009 } else {
1010 // The slow path.
1011 DCHECK_EQ(func_offset.Int32Value(), -1);
1012 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1013 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1014 }
1015 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001017 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001018 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 RegLocation rl_result = GetReturn(false);
1021 StoreValue(rl_dest, rl_result);
1022}
1023
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001024void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -07001026 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027}
1028
1029// For final classes there are no sub-classes to check and so we can answer the instance-of
1030// question with simple comparisons.
1031void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1032 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001033 // X86 has its own implementation.
1034 DCHECK_NE(cu_->instruction_set, kX86);
1035
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation object = LoadValue(rl_src, kCoreReg);
1037 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001038 int result_reg = rl_result.low_reg;
1039 if (result_reg == object.low_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 result_reg = AllocTypedTemp(false, kCoreReg);
1041 }
1042 LoadConstant(result_reg, 0); // assume false
Bill Buzbee86ec5202014-02-26 19:03:09 +00001043 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044
1045 int check_class = AllocTypedTemp(false, kCoreReg);
1046 int object_class = AllocTypedTemp(false, kCoreReg);
1047
1048 LoadCurrMethodDirect(check_class);
1049 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001050 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 check_class);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001052 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001054 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 check_class);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001056 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 int32_t offset_of_type =
1058 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1059 (sizeof(mirror::Class*) * type_idx);
1060 LoadWordDisp(check_class, offset_of_type, check_class);
1061 }
1062
1063 LIR* ne_branchover = NULL;
1064 if (cu_->instruction_set == kThumb2) {
1065 OpRegReg(kOpCmp, check_class, object_class); // Same?
1066 OpIT(kCondEq, ""); // if-convert the test
1067 LoadConstant(result_reg, 1); // .eq case - load true
1068 } else {
1069 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1070 LoadConstant(result_reg, 1); // eq case - load true
1071 }
1072 LIR* target = NewLIR0(kPseudoTargetLabel);
1073 null_branchover->target = target;
1074 if (ne_branchover != NULL) {
1075 ne_branchover->target = target;
1076 }
1077 FreeTemp(object_class);
1078 FreeTemp(check_class);
1079 if (IsTemp(result_reg)) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001080 OpRegCopy(rl_result.low_reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 FreeTemp(result_reg);
1082 }
1083 StoreValue(rl_dest, rl_result);
1084}
1085
1086void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1087 bool type_known_abstract, bool use_declaring_class,
1088 bool can_assume_type_is_in_dex_cache,
1089 uint32_t type_idx, RegLocation rl_dest,
1090 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001091 // X86 has its own implementation.
1092 DCHECK_NE(cu_->instruction_set, kX86);
1093
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 FlushAllRegs();
1095 // May generate a call - use explicit registers
1096 LockCallTemps();
1097 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1098 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1099 if (needs_access_check) {
1100 // Check we have access to type_idx and if not throw IllegalAccessError,
1101 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001102 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 type_idx, true);
1104 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1105 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1106 } else if (use_declaring_class) {
1107 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1108 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001109 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 } else {
1111 // Load dex cache entry into class_reg (kArg2)
1112 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1113 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001114 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 int32_t offset_of_type =
1116 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1117 * type_idx);
1118 LoadWordDisp(class_reg, offset_of_type, class_reg);
1119 if (!can_assume_type_is_in_dex_cache) {
1120 // Need to test presence of type in dex cache at runtime
1121 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1122 // Not resolved
1123 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001124 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001125 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1127 // Rejoin code paths
1128 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1129 hop_branch->target = hop_target;
1130 }
1131 }
1132 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1133 RegLocation rl_result = GetReturn(false);
1134 if (cu_->instruction_set == kMips) {
1135 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
Bill Buzbee86ec5202014-02-26 19:03:09 +00001136 LoadConstant(rl_result.low_reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 }
1138 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1139
1140 /* load object->klass_ */
1141 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1142 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1143 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1144 LIR* branchover = NULL;
1145 if (type_known_final) {
1146 // rl_result == ref == null == 0.
1147 if (cu_->instruction_set == kThumb2) {
1148 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1149 OpIT(kCondEq, "E"); // if-convert the test
Bill Buzbee86ec5202014-02-26 19:03:09 +00001150 LoadConstant(rl_result.low_reg, 1); // .eq case - load true
1151 LoadConstant(rl_result.low_reg, 0); // .ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001153 LoadConstant(rl_result.low_reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001155 LoadConstant(rl_result.low_reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157 } else {
1158 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001159 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 if (!type_known_abstract) {
1161 /* Uses conditional nullification */
1162 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1163 OpIT(kCondEq, "EE"); // if-convert the test
1164 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1165 }
1166 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1167 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1168 FreeTemp(r_tgt);
1169 } else {
1170 if (!type_known_abstract) {
1171 /* Uses branchovers */
Bill Buzbee86ec5202014-02-26 19:03:09 +00001172 LoadConstant(rl_result.low_reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1174 }
Mark Mendell6607d972014-02-10 06:54:18 -08001175 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1176 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1177 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1178 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 }
1180 }
1181 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001182 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 /* branch targets here */
1184 LIR* target = NewLIR0(kPseudoTargetLabel);
1185 StoreValue(rl_dest, rl_result);
1186 branch1->target = target;
1187 if (branchover != NULL) {
1188 branchover->target = target;
1189 }
1190}
1191
1192void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1193 bool type_known_final, type_known_abstract, use_declaring_class;
1194 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1195 *cu_->dex_file,
1196 type_idx,
1197 &type_known_final,
1198 &type_known_abstract,
1199 &use_declaring_class);
1200 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1201 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1202
1203 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1204 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1205 } else {
1206 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1207 use_declaring_class, can_assume_type_is_in_dex_cache,
1208 type_idx, rl_dest, rl_src);
1209 }
1210}
1211
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001212void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 bool type_known_final, type_known_abstract, use_declaring_class;
1214 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1215 *cu_->dex_file,
1216 type_idx,
1217 &type_known_final,
1218 &type_known_abstract,
1219 &use_declaring_class);
1220 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1221 // of the exception throw path.
1222 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001223 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 // Verifier type analysis proved this check cast would never cause an exception.
1225 return;
1226 }
1227 FlushAllRegs();
1228 // May generate a call - use explicit registers
1229 LockCallTemps();
1230 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1231 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1232 if (needs_access_check) {
1233 // Check we have access to type_idx and if not throw IllegalAccessError,
1234 // returns Class* in kRet0
1235 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001236 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 type_idx, TargetReg(kArg1), true);
1238 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1239 } else if (use_declaring_class) {
1240 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001241 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001242 } else {
1243 // Load dex cache entry into class_reg (kArg2)
1244 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001245 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 int32_t offset_of_type =
1247 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1248 (sizeof(mirror::Class*) * type_idx);
1249 LoadWordDisp(class_reg, offset_of_type, class_reg);
1250 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1251 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001252 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1253 LIR* cont = NewLIR0(kPseudoTargetLabel);
1254
1255 // Slow path to initialize the type. Executed if the type is NULL.
1256 class SlowPath : public LIRSlowPath {
1257 public:
1258 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1259 const int class_reg) :
1260 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1261 class_reg_(class_reg) {
1262 }
1263
1264 void Compile() {
1265 GenerateTargetLabel();
1266
1267 // Call out to helper, which will return resolved type in kArg0
1268 // InitializeTypeFromCode(idx, method)
1269 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1270 m2l_->TargetReg(kArg1), true);
1271 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1272 m2l_->OpUnconditionalBranch(cont_);
1273 }
1274 public:
1275 const int type_idx_;
1276 const int class_reg_;
1277 };
1278
1279 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1280 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 }
1282 }
1283 // At this point, class_reg (kArg2) has class
1284 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001285
1286 // Slow path for the case where the classes are not equal. In this case we need
1287 // to call a helper function to do the check.
1288 class SlowPath : public LIRSlowPath {
1289 public:
1290 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1291 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1292 }
1293
1294 void Compile() {
1295 GenerateTargetLabel();
1296
1297 if (load_) {
1298 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1299 m2l_->TargetReg(kArg1));
1300 }
1301 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1302 m2l_->TargetReg(kArg1), true);
1303
1304 m2l_->OpUnconditionalBranch(cont_);
1305 }
1306
1307 private:
1308 bool load_;
1309 };
1310
1311 if (type_known_abstract) {
1312 // Easier case, run slow path if target is non-null (slow path will load from target)
1313 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1314 LIR* cont = NewLIR0(kPseudoTargetLabel);
1315 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1316 } else {
1317 // Harder, more common case. We need to generate a forward branch over the load
1318 // if the target is null. If it's non-null we perform the load and branch to the
1319 // slow path if the classes are not equal.
1320
1321 /* Null is OK - continue */
1322 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1323 /* load object->klass_ */
1324 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1325 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1326 TargetReg(kArg1));
1327
1328 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1329 LIR* cont = NewLIR0(kPseudoTargetLabel);
1330
1331 // Add the slow path that will not perform load since this is already done.
1332 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1333
1334 // Set the null check to branch to the continuation.
1335 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 }
1337}
1338
1339void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001340 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 RegLocation rl_result;
1342 if (cu_->instruction_set == kThumb2) {
1343 /*
1344 * NOTE: This is the one place in the code in which we might have
1345 * as many as six live temporary registers. There are 5 in the normal
1346 * set for Arm. Until we have spill capabilities, temporarily add
1347 * lr to the temp set. It is safe to do this locally, but note that
1348 * lr is used explicitly elsewhere in the code generator and cannot
1349 * normally be used as a general temp register.
1350 */
1351 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1352 FreeTemp(TargetReg(kLr)); // and make it available
1353 }
1354 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1355 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1356 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1357 // The longs may overlap - use intermediate temp if so
Bill Buzbee86ec5202014-02-26 19:03:09 +00001358 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 int t_reg = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +00001360 OpRegRegReg(first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1361 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1362 OpRegCopy(rl_result.low_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 FreeTemp(t_reg);
1364 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001365 OpRegRegReg(first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1366 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg,
1367 rl_src2.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 }
1369 /*
1370 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1371 * following StoreValueWide might need to allocate a temp register.
1372 * To further work around the lack of a spill capability, explicitly
1373 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1374 * Remove when spill is functional.
1375 */
1376 FreeRegLocTemps(rl_result, rl_src1);
1377 FreeRegLocTemps(rl_result, rl_src2);
1378 StoreValueWide(rl_dest, rl_result);
1379 if (cu_->instruction_set == kThumb2) {
1380 Clobber(TargetReg(kLr));
1381 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1382 }
1383}
1384
1385
1386void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001387 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001388 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389
1390 switch (opcode) {
1391 case Instruction::SHL_LONG:
1392 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001393 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 break;
1395 case Instruction::SHR_LONG:
1396 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001397 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 break;
1399 case Instruction::USHR_LONG:
1400 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001401 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 break;
1403 default:
1404 LOG(FATAL) << "Unexpected case";
1405 }
1406 FlushAllRegs(); /* Send everything to home location */
1407 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1408 RegLocation rl_result = GetReturnWide(false);
1409 StoreValueWide(rl_dest, rl_result);
1410}
1411
1412
1413void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001414 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001415 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 OpKind op = kOpBkpt;
1417 bool is_div_rem = false;
1418 bool check_zero = false;
1419 bool unary = false;
1420 RegLocation rl_result;
1421 bool shift_op = false;
1422 switch (opcode) {
1423 case Instruction::NEG_INT:
1424 op = kOpNeg;
1425 unary = true;
1426 break;
1427 case Instruction::NOT_INT:
1428 op = kOpMvn;
1429 unary = true;
1430 break;
1431 case Instruction::ADD_INT:
1432 case Instruction::ADD_INT_2ADDR:
1433 op = kOpAdd;
1434 break;
1435 case Instruction::SUB_INT:
1436 case Instruction::SUB_INT_2ADDR:
1437 op = kOpSub;
1438 break;
1439 case Instruction::MUL_INT:
1440 case Instruction::MUL_INT_2ADDR:
1441 op = kOpMul;
1442 break;
1443 case Instruction::DIV_INT:
1444 case Instruction::DIV_INT_2ADDR:
1445 check_zero = true;
1446 op = kOpDiv;
1447 is_div_rem = true;
1448 break;
1449 /* NOTE: returns in kArg1 */
1450 case Instruction::REM_INT:
1451 case Instruction::REM_INT_2ADDR:
1452 check_zero = true;
1453 op = kOpRem;
1454 is_div_rem = true;
1455 break;
1456 case Instruction::AND_INT:
1457 case Instruction::AND_INT_2ADDR:
1458 op = kOpAnd;
1459 break;
1460 case Instruction::OR_INT:
1461 case Instruction::OR_INT_2ADDR:
1462 op = kOpOr;
1463 break;
1464 case Instruction::XOR_INT:
1465 case Instruction::XOR_INT_2ADDR:
1466 op = kOpXor;
1467 break;
1468 case Instruction::SHL_INT:
1469 case Instruction::SHL_INT_2ADDR:
1470 shift_op = true;
1471 op = kOpLsl;
1472 break;
1473 case Instruction::SHR_INT:
1474 case Instruction::SHR_INT_2ADDR:
1475 shift_op = true;
1476 op = kOpAsr;
1477 break;
1478 case Instruction::USHR_INT:
1479 case Instruction::USHR_INT_2ADDR:
1480 shift_op = true;
1481 op = kOpLsr;
1482 break;
1483 default:
1484 LOG(FATAL) << "Invalid word arith op: " << opcode;
1485 }
1486 if (!is_div_rem) {
1487 if (unary) {
1488 rl_src1 = LoadValue(rl_src1, kCoreReg);
1489 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001490 OpRegReg(op, rl_result.low_reg, rl_src1.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 } else {
1492 if (shift_op) {
1493 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001494 rl_src2 = LoadValue(rl_src2, kCoreReg);
1495 t_reg = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +00001496 OpRegRegImm(kOpAnd, t_reg, rl_src2.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 rl_src1 = LoadValue(rl_src1, kCoreReg);
1498 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001499 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 FreeTemp(t_reg);
1501 } else {
1502 rl_src1 = LoadValue(rl_src1, kCoreReg);
1503 rl_src2 = LoadValue(rl_src2, kCoreReg);
1504 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001505 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 }
1507 }
1508 StoreValue(rl_dest, rl_result);
1509 } else {
Dave Allison70202782013-10-22 17:52:19 -07001510 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 -07001511 if (cu_->instruction_set == kMips) {
1512 rl_src1 = LoadValue(rl_src1, kCoreReg);
1513 rl_src2 = LoadValue(rl_src2, kCoreReg);
1514 if (check_zero) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001515 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 }
Bill Buzbee86ec5202014-02-26 19:03:09 +00001517 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001518 done = true;
1519 } else if (cu_->instruction_set == kThumb2) {
1520 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1521 // Use ARM SDIV instruction for division. For remainder we also need to
1522 // calculate using a MUL and subtract.
1523 rl_src1 = LoadValue(rl_src1, kCoreReg);
1524 rl_src2 = LoadValue(rl_src2, kCoreReg);
1525 if (check_zero) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001526 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001527 }
Bill Buzbee86ec5202014-02-26 19:03:09 +00001528 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001529 done = true;
1530 }
1531 }
1532
1533 // If we haven't already generated the code use the callout function.
1534 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001535 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 FlushAllRegs(); /* Send everything to home location */
1537 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1538 int r_tgt = CallHelperSetup(func_offset);
1539 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1540 if (check_zero) {
1541 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1542 }
Dave Allison70202782013-10-22 17:52:19 -07001543 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001544 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 if (op == kOpDiv)
1546 rl_result = GetReturn(false);
1547 else
1548 rl_result = GetReturnAlt();
1549 }
1550 StoreValue(rl_dest, rl_result);
1551 }
1552}
1553
1554/*
1555 * The following are the first-level codegen routines that analyze the format
1556 * of each bytecode then either dispatch special purpose codegen routines
1557 * or produce corresponding Thumb instructions directly.
1558 */
1559
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001561static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 x &= x - 1;
1563 return (x & (x - 1)) == 0;
1564}
1565
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1567// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001568bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001569 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1571 return false;
1572 }
1573 // No divide instruction for Arm, so check for more special cases
1574 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001575 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 }
1577 int k = LowestSetBit(lit);
1578 if (k >= 30) {
1579 // Avoid special cases.
1580 return false;
1581 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 rl_src = LoadValue(rl_src, kCoreReg);
1583 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001584 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 int t_reg = AllocTemp();
1586 if (lit == 2) {
1587 // Division by 2 is by far the most common division by constant.
Bill Buzbee86ec5202014-02-26 19:03:09 +00001588 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1589 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1590 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001592 OpRegRegImm(kOpAsr, t_reg, rl_src.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001594 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1595 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 }
1597 } else {
1598 int t_reg1 = AllocTemp();
1599 int t_reg2 = AllocTemp();
1600 if (lit == 2) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001601 OpRegRegImm(kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1602 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001604 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001606 OpRegRegImm(kOpAsr, t_reg1, rl_src.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001608 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001610 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
1612 }
1613 StoreValue(rl_dest, rl_result);
1614 return true;
1615}
1616
1617// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1618// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001619bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 // Can we simplify this multiplication?
1621 bool power_of_two = false;
1622 bool pop_count_le2 = false;
1623 bool power_of_two_minus_one = false;
1624 if (lit < 2) {
1625 // Avoid special cases.
1626 return false;
1627 } else if (IsPowerOfTwo(lit)) {
1628 power_of_two = true;
1629 } else if (IsPopCountLE2(lit)) {
1630 pop_count_le2 = true;
1631 } else if (IsPowerOfTwo(lit + 1)) {
1632 power_of_two_minus_one = true;
1633 } else {
1634 return false;
1635 }
1636 rl_src = LoadValue(rl_src, kCoreReg);
1637 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1638 if (power_of_two) {
1639 // Shift.
Bill Buzbee86ec5202014-02-26 19:03:09 +00001640 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 } else if (pop_count_le2) {
1642 // Shift and add and shift.
1643 int first_bit = LowestSetBit(lit);
1644 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1645 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1646 } else {
1647 // Reverse subtract: (src << (shift + 1)) - src.
1648 DCHECK(power_of_two_minus_one);
1649 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1650 int t_reg = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +00001651 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1652 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
1654 StoreValue(rl_dest, rl_result);
1655 return true;
1656}
1657
1658void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001659 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 RegLocation rl_result;
1661 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1662 int shift_op = false;
1663 bool is_div = false;
1664
1665 switch (opcode) {
1666 case Instruction::RSUB_INT_LIT8:
1667 case Instruction::RSUB_INT: {
1668 rl_src = LoadValue(rl_src, kCoreReg);
1669 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1670 if (cu_->instruction_set == kThumb2) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001671 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001673 OpRegReg(kOpNeg, rl_result.low_reg, rl_src.low_reg);
1674 OpRegImm(kOpAdd, rl_result.low_reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 }
1676 StoreValue(rl_dest, rl_result);
1677 return;
1678 }
1679
1680 case Instruction::SUB_INT:
1681 case Instruction::SUB_INT_2ADDR:
1682 lit = -lit;
1683 // Intended fallthrough
1684 case Instruction::ADD_INT:
1685 case Instruction::ADD_INT_2ADDR:
1686 case Instruction::ADD_INT_LIT8:
1687 case Instruction::ADD_INT_LIT16:
1688 op = kOpAdd;
1689 break;
1690 case Instruction::MUL_INT:
1691 case Instruction::MUL_INT_2ADDR:
1692 case Instruction::MUL_INT_LIT8:
1693 case Instruction::MUL_INT_LIT16: {
1694 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1695 return;
1696 }
1697 op = kOpMul;
1698 break;
1699 }
1700 case Instruction::AND_INT:
1701 case Instruction::AND_INT_2ADDR:
1702 case Instruction::AND_INT_LIT8:
1703 case Instruction::AND_INT_LIT16:
1704 op = kOpAnd;
1705 break;
1706 case Instruction::OR_INT:
1707 case Instruction::OR_INT_2ADDR:
1708 case Instruction::OR_INT_LIT8:
1709 case Instruction::OR_INT_LIT16:
1710 op = kOpOr;
1711 break;
1712 case Instruction::XOR_INT:
1713 case Instruction::XOR_INT_2ADDR:
1714 case Instruction::XOR_INT_LIT8:
1715 case Instruction::XOR_INT_LIT16:
1716 op = kOpXor;
1717 break;
1718 case Instruction::SHL_INT_LIT8:
1719 case Instruction::SHL_INT:
1720 case Instruction::SHL_INT_2ADDR:
1721 lit &= 31;
1722 shift_op = true;
1723 op = kOpLsl;
1724 break;
1725 case Instruction::SHR_INT_LIT8:
1726 case Instruction::SHR_INT:
1727 case Instruction::SHR_INT_2ADDR:
1728 lit &= 31;
1729 shift_op = true;
1730 op = kOpAsr;
1731 break;
1732 case Instruction::USHR_INT_LIT8:
1733 case Instruction::USHR_INT:
1734 case Instruction::USHR_INT_2ADDR:
1735 lit &= 31;
1736 shift_op = true;
1737 op = kOpLsr;
1738 break;
1739
1740 case Instruction::DIV_INT:
1741 case Instruction::DIV_INT_2ADDR:
1742 case Instruction::DIV_INT_LIT8:
1743 case Instruction::DIV_INT_LIT16:
1744 case Instruction::REM_INT:
1745 case Instruction::REM_INT_2ADDR:
1746 case Instruction::REM_INT_LIT8:
1747 case Instruction::REM_INT_LIT16: {
1748 if (lit == 0) {
1749 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1750 return;
1751 }
buzbee11b63d12013-08-27 07:34:17 -07001752 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001754 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 (opcode == Instruction::DIV_INT_LIT16)) {
1756 is_div = true;
1757 } else {
1758 is_div = false;
1759 }
buzbee11b63d12013-08-27 07:34:17 -07001760 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1761 return;
1762 }
Dave Allison70202782013-10-22 17:52:19 -07001763
1764 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 if (cu_->instruction_set == kMips) {
1766 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001767 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001768 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001769 } else if (cu_->instruction_set == kX86) {
1770 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1771 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001772 } else if (cu_->instruction_set == kThumb2) {
1773 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1774 // Use ARM SDIV instruction for division. For remainder we also need to
1775 // calculate using a MUL and subtract.
1776 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee86ec5202014-02-26 19:03:09 +00001777 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001778 done = true;
1779 }
1780 }
1781
1782 if (!done) {
1783 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1785 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001786 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1788 if (is_div)
1789 rl_result = GetReturn(false);
1790 else
1791 rl_result = GetReturnAlt();
1792 }
1793 StoreValue(rl_dest, rl_result);
1794 return;
1795 }
1796 default:
1797 LOG(FATAL) << "Unexpected opcode " << opcode;
1798 }
1799 rl_src = LoadValue(rl_src, kCoreReg);
1800 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001801 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 if (shift_op && (lit == 0)) {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001803 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001805 OpRegRegImm(op, rl_result.low_reg, rl_src.low_reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 }
1807 StoreValue(rl_dest, rl_result);
1808}
1809
1810void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001811 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 RegLocation rl_result;
1813 OpKind first_op = kOpBkpt;
1814 OpKind second_op = kOpBkpt;
1815 bool call_out = false;
1816 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001817 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 int ret_reg = TargetReg(kRet0);
1819
1820 switch (opcode) {
1821 case Instruction::NOT_LONG:
1822 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1823 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1824 // Check for destructive overlap
Bill Buzbee86ec5202014-02-26 19:03:09 +00001825 if (rl_result.low_reg == rl_src2.high_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 int t_reg = AllocTemp();
Bill Buzbee86ec5202014-02-26 19:03:09 +00001827 OpRegCopy(t_reg, rl_src2.high_reg);
1828 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1829 OpRegReg(kOpMvn, rl_result.high_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 FreeTemp(t_reg);
1831 } else {
Bill Buzbee86ec5202014-02-26 19:03:09 +00001832 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1833 OpRegReg(kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 }
1835 StoreValueWide(rl_dest, rl_result);
1836 return;
1837 case Instruction::ADD_LONG:
1838 case Instruction::ADD_LONG_2ADDR:
1839 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001840 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 return;
1842 }
1843 first_op = kOpAdd;
1844 second_op = kOpAdc;
1845 break;
1846 case Instruction::SUB_LONG:
1847 case Instruction::SUB_LONG_2ADDR:
1848 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001849 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 return;
1851 }
1852 first_op = kOpSub;
1853 second_op = kOpSbc;
1854 break;
1855 case Instruction::MUL_LONG:
1856 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001857 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001858 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001859 return;
1860 } else {
1861 call_out = true;
1862 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001863 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 }
1865 break;
1866 case Instruction::DIV_LONG:
1867 case Instruction::DIV_LONG_2ADDR:
1868 call_out = true;
1869 check_zero = true;
1870 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001871 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 break;
1873 case Instruction::REM_LONG:
1874 case Instruction::REM_LONG_2ADDR:
1875 call_out = true;
1876 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001877 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1879 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1880 break;
1881 case Instruction::AND_LONG_2ADDR:
1882 case Instruction::AND_LONG:
1883 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001884 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 }
1886 first_op = kOpAnd;
1887 second_op = kOpAnd;
1888 break;
1889 case Instruction::OR_LONG:
1890 case Instruction::OR_LONG_2ADDR:
1891 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001892 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 return;
1894 }
1895 first_op = kOpOr;
1896 second_op = kOpOr;
1897 break;
1898 case Instruction::XOR_LONG:
1899 case Instruction::XOR_LONG_2ADDR:
1900 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001901 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 return;
1903 }
1904 first_op = kOpXor;
1905 second_op = kOpXor;
1906 break;
1907 case Instruction::NEG_LONG: {
1908 GenNegLong(rl_dest, rl_src2);
1909 return;
1910 }
1911 default:
1912 LOG(FATAL) << "Invalid long arith op";
1913 }
1914 if (!call_out) {
1915 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1916 } else {
1917 FlushAllRegs(); /* Send everything to home location */
1918 if (check_zero) {
1919 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1920 int r_tgt = CallHelperSetup(func_offset);
1921 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1922 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1923 // NOTE: callout here is not a safepoint
1924 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1925 } else {
1926 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1927 }
1928 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1929 if (ret_reg == TargetReg(kRet0))
1930 rl_result = GetReturnWide(false);
1931 else
1932 rl_result = GetReturnWideAlt();
1933 StoreValueWide(rl_dest, rl_result);
1934 }
1935}
1936
Ian Rogers848871b2013-08-05 10:56:33 -07001937void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001938 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 /*
1940 * Don't optimize the register usage since it calls out to support
1941 * functions
1942 */
1943 FlushAllRegs(); /* Send everything to home location */
1944 if (rl_src.wide) {
1945 LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1946 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1947 } else {
1948 LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1949 }
1950 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1951 if (rl_dest.wide) {
1952 RegLocation rl_result;
1953 rl_result = GetReturnWide(rl_dest.fp);
1954 StoreValueWide(rl_dest, rl_result);
1955 } else {
1956 RegLocation rl_result;
1957 rl_result = GetReturn(rl_dest.fp);
1958 StoreValue(rl_dest, rl_result);
1959 }
1960}
1961
1962/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001963void Mir2Lir::GenSuspendTest(int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1965 return;
1966 }
1967 FlushAllRegs();
1968 LIR* branch = OpTestSuspend(NULL);
1969 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001970 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1971 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 branch->target = target;
1973 suspend_launchpads_.Insert(target);
1974}
1975
1976/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001977void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1979 OpUnconditionalBranch(target);
1980 return;
1981 }
1982 OpTestSuspend(target);
1983 LIR* launch_pad =
buzbee0d829482013-10-11 15:24:55 -07001984 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1985 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 FlushAllRegs();
1987 OpUnconditionalBranch(launch_pad);
1988 suspend_launchpads_.Insert(launch_pad);
1989}
1990
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001991/* Call out to helper assembly routine that will null check obj and then lock it. */
1992void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1993 FlushAllRegs();
1994 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
1995}
1996
1997/* Call out to helper assembly routine that will null check obj and then unlock it. */
1998void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
1999 FlushAllRegs();
2000 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2001}
2002
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002003/* Generic code for generating a wide constant into a VR. */
2004void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2005 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee86ec5202014-02-26 19:03:09 +00002006 LoadConstantWide(rl_result.low_reg, rl_result.high_reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002007 StoreValueWide(rl_dest, rl_result);
2008}
2009
Brian Carlstrom7940e442013-07-12 13:46:57 -07002010} // namespace art