blob: 901d72285413379918b00cfb5276f12d50d8dd1e [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
131 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);
136 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 }
166 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) {
172 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
173 } else {
174 LoadValueDirect(rl_src, rl_result.low_reg);
175 }
176 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
177 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 }
198 OpRegReg(op, rl_result.low_reg, rl_src.low_reg);
199 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),
293 loc.low_reg, kWord);
294 }
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() +
344 i * 4, rl_arg.low_reg, kWord);
345 // If the LoadValue caused a temp to be allocated, free it
346 if (IsTemp(rl_arg.low_reg)) {
347 FreeTemp(rl_arg.low_reg);
348 }
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
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384void 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) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800387 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 bool is_volatile;
389 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800390 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700392 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), true,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800393 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 if (fast_path && !SLOW_FIELD_PATH) {
395 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800396 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 if (is_referrers_class) {
398 // 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();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800402 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 if (IsTemp(rl_method.low_reg)) {
404 FreeTemp(rl_method.low_reg);
405 }
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 Rogers5ddb4102014-01-07 08:58:46 -0800410 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() +
423 sizeof(int32_t*) * storage_index, r_base);
424 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
425 if (!is_initialized) {
426 // 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,
440 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 }
452 if (is_volatile) {
453 GenMemBarrier(kStoreStore);
454 }
455 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800456 StoreBaseDispWide(r_base, field_offset, rl_src.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 rl_src.high_reg);
458 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800459 StoreWordDisp(r_base, field_offset, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 }
461 if (is_volatile) {
462 GenMemBarrier(kStoreLoad);
463 }
464 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800465 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));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 CallRuntimeHelperImmRegLocation(setter_offset, field_idx, rl_src, true);
475 }
476}
477
478void 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) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800481 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 bool is_volatile;
483 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800484 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700486 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), false,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800487 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 if (fast_path && !SLOW_FIELD_PATH) {
489 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800490 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 if (is_referrers_class) {
492 // 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();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 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 Rogers5ddb4102014-01-07 08:58:46 -0800500 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() +
513 sizeof(int32_t*) * storage_index, r_base);
514 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
515 if (!is_initialized) {
516 // 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,
530 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);
538 if (is_volatile) {
539 GenMemBarrier(kLoadLoad);
540 }
541 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800542 LoadBaseDispWide(r_base, field_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 rl_result.high_reg, INVALID_SREG);
544 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800545 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));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 CallRuntimeHelperImm(getterOffset, field_idx, true);
560 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
701void Mir2Lir::GenIGet(uint32_t field_idx, int opt_flags, OpSize size,
702 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700703 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 int field_offset;
705 bool is_volatile;
706
Ian Rogers9b297bf2013-09-06 11:11:25 -0700707 bool fast_path = FastInstance(field_idx, false, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708
709 if (fast_path && !SLOW_FIELD_PATH) {
710 RegLocation rl_result;
711 RegisterClass reg_class = oat_reg_class_by_size(size);
712 DCHECK_GE(field_offset, 0);
713 rl_obj = LoadValue(rl_obj, kCoreReg);
714 if (is_long_or_double) {
715 DCHECK(rl_dest.wide);
716 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
717 if (cu_->instruction_set == kX86) {
718 rl_result = EvalLoc(rl_dest, reg_class, true);
719 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);
722 if (is_volatile) {
723 GenMemBarrier(kLoadLoad);
724 }
725 } else {
726 int reg_ptr = AllocTemp();
727 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
728 rl_result = EvalLoc(rl_dest, reg_class, true);
729 LoadBaseDispWide(reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
730 if (is_volatile) {
731 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);
738 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
739 LoadBaseDisp(rl_obj.low_reg, field_offset, rl_result.low_reg,
740 kWord, rl_obj.s_reg_low);
741 if (is_volatile) {
742 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));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 CallRuntimeHelperImmRegLocation(getterOffset, field_idx, rl_obj, true);
752 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
762void Mir2Lir::GenIPut(uint32_t field_idx, int opt_flags, OpSize size,
763 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700764 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 int field_offset;
766 bool is_volatile;
767
Ian Rogers9b297bf2013-09-06 11:11:25 -0700768 bool fast_path = FastInstance(field_idx, true, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 if (fast_path && !SLOW_FIELD_PATH) {
770 RegisterClass reg_class = oat_reg_class_by_size(size);
771 DCHECK_GE(field_offset, 0);
772 rl_obj = LoadValue(rl_obj, kCoreReg);
773 if (is_long_or_double) {
774 int reg_ptr;
775 rl_src = LoadValueWide(rl_src, kAnyReg);
776 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
777 reg_ptr = AllocTemp();
778 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
779 if (is_volatile) {
780 GenMemBarrier(kStoreStore);
781 }
782 StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
783 if (is_volatile) {
784 GenMemBarrier(kLoadLoad);
785 }
786 FreeTemp(reg_ptr);
787 } else {
788 rl_src = LoadValue(rl_src, reg_class);
789 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
790 if (is_volatile) {
791 GenMemBarrier(kStoreStore);
792 }
793 StoreBaseDisp(rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
794 if (is_volatile) {
795 GenMemBarrier(kLoadLoad);
796 }
797 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
798 MarkGCCard(rl_src.low_reg, rl_obj.low_reg);
799 }
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));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_idx, rl_obj, rl_src, true);
807 }
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),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 type_idx, rl_method.low_reg, true);
833 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();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 LoadWordDisp(rl_method.low_reg, dex_cache_offset, res_reg);
840 int32_t offset_of_type =
841 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
842 * type_idx);
843 LoadWordDisp(res_reg, offset_of_type, rl_result.low_reg);
844 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();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800848 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.low_reg, 0, NULL);
849 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_,
864 rl_method_.low_reg, true);
865 m2l_->OpRegCopy(rl_result_.low_reg, m2l_->TargetReg(kRet0));
866
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.
903 DCHECK(!IsTemp(rl_method.low_reg));
904 r_method = rl_method.low_reg;
905 } 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);
963 LoadWordDisp(rl_method.low_reg,
Brian Carlstromea46f952013-07-30 01:26:50 -0700964 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 LoadWordDisp(res_reg, offset_of_string, rl_result.low_reg);
966 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);
1038 int result_reg = rl_result.low_reg;
1039 if (result_reg == object.low_reg) {
1040 result_reg = AllocTypedTemp(false, kCoreReg);
1041 }
1042 LoadConstant(result_reg, 0); // assume false
1043 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL);
1044
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);
1052 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
1053 } 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);
1056 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
1057 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)) {
1080 OpRegCopy(rl_result.low_reg, result_reg);
1081 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) {
1091 FlushAllRegs();
1092 // May generate a call - use explicit registers
1093 LockCallTemps();
1094 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1095 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1096 if (needs_access_check) {
1097 // Check we have access to type_idx and if not throw IllegalAccessError,
1098 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001099 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 type_idx, true);
1101 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1102 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1103 } else if (use_declaring_class) {
1104 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1105 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001106 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
1108 // Load dex cache entry into class_reg (kArg2)
1109 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1110 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001111 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 int32_t offset_of_type =
1113 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1114 * type_idx);
1115 LoadWordDisp(class_reg, offset_of_type, class_reg);
1116 if (!can_assume_type_is_in_dex_cache) {
1117 // Need to test presence of type in dex cache at runtime
1118 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1119 // Not resolved
1120 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001121 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001122 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1124 // Rejoin code paths
1125 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1126 hop_branch->target = hop_target;
1127 }
1128 }
1129 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1130 RegLocation rl_result = GetReturn(false);
1131 if (cu_->instruction_set == kMips) {
1132 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
1133 LoadConstant(rl_result.low_reg, 0);
1134 }
1135 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1136
1137 /* load object->klass_ */
1138 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1139 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1140 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1141 LIR* branchover = NULL;
1142 if (type_known_final) {
1143 // rl_result == ref == null == 0.
1144 if (cu_->instruction_set == kThumb2) {
1145 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1146 OpIT(kCondEq, "E"); // if-convert the test
1147 LoadConstant(rl_result.low_reg, 1); // .eq case - load true
1148 LoadConstant(rl_result.low_reg, 0); // .ne case - load false
1149 } else {
1150 LoadConstant(rl_result.low_reg, 0); // ne case - load false
1151 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
1152 LoadConstant(rl_result.low_reg, 1); // eq case - load true
1153 }
1154 } else {
1155 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001156 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 if (!type_known_abstract) {
1158 /* Uses conditional nullification */
1159 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1160 OpIT(kCondEq, "EE"); // if-convert the test
1161 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1162 }
1163 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1164 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1165 FreeTemp(r_tgt);
1166 } else {
1167 if (!type_known_abstract) {
1168 /* Uses branchovers */
1169 LoadConstant(rl_result.low_reg, 1); // assume true
1170 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1171 }
1172 if (cu_->instruction_set != kX86) {
Ian Rogers848871b2013-08-05 10:56:33 -07001173 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1175 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1176 FreeTemp(r_tgt);
1177 } else {
1178 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
Ian Rogers848871b2013-08-05 10:56:33 -07001179 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 }
1181 }
1182 }
1183 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001184 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 /* branch targets here */
1186 LIR* target = NewLIR0(kPseudoTargetLabel);
1187 StoreValue(rl_dest, rl_result);
1188 branch1->target = target;
1189 if (branchover != NULL) {
1190 branchover->target = target;
1191 }
1192}
1193
1194void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1195 bool type_known_final, type_known_abstract, use_declaring_class;
1196 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1197 *cu_->dex_file,
1198 type_idx,
1199 &type_known_final,
1200 &type_known_abstract,
1201 &use_declaring_class);
1202 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1203 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1204
1205 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1206 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1207 } else {
1208 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1209 use_declaring_class, can_assume_type_is_in_dex_cache,
1210 type_idx, rl_dest, rl_src);
1211 }
1212}
1213
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001214void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 bool type_known_final, type_known_abstract, use_declaring_class;
1216 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1217 *cu_->dex_file,
1218 type_idx,
1219 &type_known_final,
1220 &type_known_abstract,
1221 &use_declaring_class);
1222 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1223 // of the exception throw path.
1224 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001225 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 // Verifier type analysis proved this check cast would never cause an exception.
1227 return;
1228 }
1229 FlushAllRegs();
1230 // May generate a call - use explicit registers
1231 LockCallTemps();
1232 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1233 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1234 if (needs_access_check) {
1235 // Check we have access to type_idx and if not throw IllegalAccessError,
1236 // returns Class* in kRet0
1237 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001238 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 type_idx, TargetReg(kArg1), true);
1240 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1241 } else if (use_declaring_class) {
1242 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001243 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
1245 // Load dex cache entry into class_reg (kArg2)
1246 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001247 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 int32_t offset_of_type =
1249 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1250 (sizeof(mirror::Class*) * type_idx);
1251 LoadWordDisp(class_reg, offset_of_type, class_reg);
1252 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1253 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001254 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1255 LIR* cont = NewLIR0(kPseudoTargetLabel);
1256
1257 // Slow path to initialize the type. Executed if the type is NULL.
1258 class SlowPath : public LIRSlowPath {
1259 public:
1260 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1261 const int class_reg) :
1262 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1263 class_reg_(class_reg) {
1264 }
1265
1266 void Compile() {
1267 GenerateTargetLabel();
1268
1269 // Call out to helper, which will return resolved type in kArg0
1270 // InitializeTypeFromCode(idx, method)
1271 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1272 m2l_->TargetReg(kArg1), true);
1273 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1274 m2l_->OpUnconditionalBranch(cont_);
1275 }
1276 public:
1277 const int type_idx_;
1278 const int class_reg_;
1279 };
1280
1281 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1282 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 }
1284 }
1285 // At this point, class_reg (kArg2) has class
1286 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001287
1288 // Slow path for the case where the classes are not equal. In this case we need
1289 // to call a helper function to do the check.
1290 class SlowPath : public LIRSlowPath {
1291 public:
1292 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1293 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1294 }
1295
1296 void Compile() {
1297 GenerateTargetLabel();
1298
1299 if (load_) {
1300 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1301 m2l_->TargetReg(kArg1));
1302 }
1303 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1304 m2l_->TargetReg(kArg1), true);
1305
1306 m2l_->OpUnconditionalBranch(cont_);
1307 }
1308
1309 private:
1310 bool load_;
1311 };
1312
1313 if (type_known_abstract) {
1314 // Easier case, run slow path if target is non-null (slow path will load from target)
1315 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1316 LIR* cont = NewLIR0(kPseudoTargetLabel);
1317 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1318 } else {
1319 // Harder, more common case. We need to generate a forward branch over the load
1320 // if the target is null. If it's non-null we perform the load and branch to the
1321 // slow path if the classes are not equal.
1322
1323 /* Null is OK - continue */
1324 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1325 /* load object->klass_ */
1326 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1327 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1328 TargetReg(kArg1));
1329
1330 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1331 LIR* cont = NewLIR0(kPseudoTargetLabel);
1332
1333 // Add the slow path that will not perform load since this is already done.
1334 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1335
1336 // Set the null check to branch to the continuation.
1337 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 }
1339}
1340
1341void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001342 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 RegLocation rl_result;
1344 if (cu_->instruction_set == kThumb2) {
1345 /*
1346 * NOTE: This is the one place in the code in which we might have
1347 * as many as six live temporary registers. There are 5 in the normal
1348 * set for Arm. Until we have spill capabilities, temporarily add
1349 * lr to the temp set. It is safe to do this locally, but note that
1350 * lr is used explicitly elsewhere in the code generator and cannot
1351 * normally be used as a general temp register.
1352 */
1353 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1354 FreeTemp(TargetReg(kLr)); // and make it available
1355 }
1356 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1357 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1358 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1359 // The longs may overlap - use intermediate temp if so
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001360 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 int t_reg = AllocTemp();
1362 OpRegRegReg(first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1363 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1364 OpRegCopy(rl_result.low_reg, t_reg);
1365 FreeTemp(t_reg);
1366 } else {
1367 OpRegRegReg(first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1368 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg,
1369 rl_src2.high_reg);
1370 }
1371 /*
1372 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1373 * following StoreValueWide might need to allocate a temp register.
1374 * To further work around the lack of a spill capability, explicitly
1375 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1376 * Remove when spill is functional.
1377 */
1378 FreeRegLocTemps(rl_result, rl_src1);
1379 FreeRegLocTemps(rl_result, rl_src2);
1380 StoreValueWide(rl_dest, rl_result);
1381 if (cu_->instruction_set == kThumb2) {
1382 Clobber(TargetReg(kLr));
1383 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1384 }
1385}
1386
1387
1388void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001389 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001390 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391
1392 switch (opcode) {
1393 case Instruction::SHL_LONG:
1394 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001395 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 break;
1397 case Instruction::SHR_LONG:
1398 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001399 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 break;
1401 case Instruction::USHR_LONG:
1402 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001403 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 break;
1405 default:
1406 LOG(FATAL) << "Unexpected case";
1407 }
1408 FlushAllRegs(); /* Send everything to home location */
1409 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1410 RegLocation rl_result = GetReturnWide(false);
1411 StoreValueWide(rl_dest, rl_result);
1412}
1413
1414
1415void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001416 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001417 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418 OpKind op = kOpBkpt;
1419 bool is_div_rem = false;
1420 bool check_zero = false;
1421 bool unary = false;
1422 RegLocation rl_result;
1423 bool shift_op = false;
1424 switch (opcode) {
1425 case Instruction::NEG_INT:
1426 op = kOpNeg;
1427 unary = true;
1428 break;
1429 case Instruction::NOT_INT:
1430 op = kOpMvn;
1431 unary = true;
1432 break;
1433 case Instruction::ADD_INT:
1434 case Instruction::ADD_INT_2ADDR:
1435 op = kOpAdd;
1436 break;
1437 case Instruction::SUB_INT:
1438 case Instruction::SUB_INT_2ADDR:
1439 op = kOpSub;
1440 break;
1441 case Instruction::MUL_INT:
1442 case Instruction::MUL_INT_2ADDR:
1443 op = kOpMul;
1444 break;
1445 case Instruction::DIV_INT:
1446 case Instruction::DIV_INT_2ADDR:
1447 check_zero = true;
1448 op = kOpDiv;
1449 is_div_rem = true;
1450 break;
1451 /* NOTE: returns in kArg1 */
1452 case Instruction::REM_INT:
1453 case Instruction::REM_INT_2ADDR:
1454 check_zero = true;
1455 op = kOpRem;
1456 is_div_rem = true;
1457 break;
1458 case Instruction::AND_INT:
1459 case Instruction::AND_INT_2ADDR:
1460 op = kOpAnd;
1461 break;
1462 case Instruction::OR_INT:
1463 case Instruction::OR_INT_2ADDR:
1464 op = kOpOr;
1465 break;
1466 case Instruction::XOR_INT:
1467 case Instruction::XOR_INT_2ADDR:
1468 op = kOpXor;
1469 break;
1470 case Instruction::SHL_INT:
1471 case Instruction::SHL_INT_2ADDR:
1472 shift_op = true;
1473 op = kOpLsl;
1474 break;
1475 case Instruction::SHR_INT:
1476 case Instruction::SHR_INT_2ADDR:
1477 shift_op = true;
1478 op = kOpAsr;
1479 break;
1480 case Instruction::USHR_INT:
1481 case Instruction::USHR_INT_2ADDR:
1482 shift_op = true;
1483 op = kOpLsr;
1484 break;
1485 default:
1486 LOG(FATAL) << "Invalid word arith op: " << opcode;
1487 }
1488 if (!is_div_rem) {
1489 if (unary) {
1490 rl_src1 = LoadValue(rl_src1, kCoreReg);
1491 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1492 OpRegReg(op, rl_result.low_reg, rl_src1.low_reg);
1493 } else {
1494 if (shift_op) {
1495 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001496 rl_src2 = LoadValue(rl_src2, kCoreReg);
1497 t_reg = AllocTemp();
1498 OpRegRegImm(kOpAnd, t_reg, rl_src2.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 rl_src1 = LoadValue(rl_src1, kCoreReg);
1500 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1501 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1502 FreeTemp(t_reg);
1503 } else {
1504 rl_src1 = LoadValue(rl_src1, kCoreReg);
1505 rl_src2 = LoadValue(rl_src2, kCoreReg);
1506 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1507 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1508 }
1509 }
1510 StoreValue(rl_dest, rl_result);
1511 } else {
Dave Allison70202782013-10-22 17:52:19 -07001512 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 -07001513 if (cu_->instruction_set == kMips) {
1514 rl_src1 = LoadValue(rl_src1, kCoreReg);
1515 rl_src2 = LoadValue(rl_src2, kCoreReg);
1516 if (check_zero) {
1517 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1518 }
1519 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001520 done = true;
1521 } else if (cu_->instruction_set == kThumb2) {
1522 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1523 // Use ARM SDIV instruction for division. For remainder we also need to
1524 // calculate using a MUL and subtract.
1525 rl_src1 = LoadValue(rl_src1, kCoreReg);
1526 rl_src2 = LoadValue(rl_src2, kCoreReg);
1527 if (check_zero) {
1528 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1529 }
1530 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
1531 done = true;
1532 }
1533 }
1534
1535 // If we haven't already generated the code use the callout function.
1536 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001537 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 FlushAllRegs(); /* Send everything to home location */
1539 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1540 int r_tgt = CallHelperSetup(func_offset);
1541 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1542 if (check_zero) {
1543 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1544 }
Dave Allison70202782013-10-22 17:52:19 -07001545 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001546 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 if (op == kOpDiv)
1548 rl_result = GetReturn(false);
1549 else
1550 rl_result = GetReturnAlt();
1551 }
1552 StoreValue(rl_dest, rl_result);
1553 }
1554}
1555
1556/*
1557 * The following are the first-level codegen routines that analyze the format
1558 * of each bytecode then either dispatch special purpose codegen routines
1559 * or produce corresponding Thumb instructions directly.
1560 */
1561
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001563static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 x &= x - 1;
1565 return (x & (x - 1)) == 0;
1566}
1567
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1569// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001570bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001571 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1573 return false;
1574 }
1575 // No divide instruction for Arm, so check for more special cases
1576 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001577 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 }
1579 int k = LowestSetBit(lit);
1580 if (k >= 30) {
1581 // Avoid special cases.
1582 return false;
1583 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 rl_src = LoadValue(rl_src, kCoreReg);
1585 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001586 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 int t_reg = AllocTemp();
1588 if (lit == 2) {
1589 // Division by 2 is by far the most common division by constant.
1590 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1591 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1592 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1593 } else {
1594 OpRegRegImm(kOpAsr, t_reg, rl_src.low_reg, 31);
1595 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
1596 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1597 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1598 }
1599 } else {
1600 int t_reg1 = AllocTemp();
1601 int t_reg2 = AllocTemp();
1602 if (lit == 2) {
1603 OpRegRegImm(kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1604 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1605 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
1606 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1607 } else {
1608 OpRegRegImm(kOpAsr, t_reg1, rl_src.low_reg, 31);
1609 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
1610 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1611 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
1612 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1613 }
1614 }
1615 StoreValue(rl_dest, rl_result);
1616 return true;
1617}
1618
1619// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1620// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001621bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 // Can we simplify this multiplication?
1623 bool power_of_two = false;
1624 bool pop_count_le2 = false;
1625 bool power_of_two_minus_one = false;
1626 if (lit < 2) {
1627 // Avoid special cases.
1628 return false;
1629 } else if (IsPowerOfTwo(lit)) {
1630 power_of_two = true;
1631 } else if (IsPopCountLE2(lit)) {
1632 pop_count_le2 = true;
1633 } else if (IsPowerOfTwo(lit + 1)) {
1634 power_of_two_minus_one = true;
1635 } else {
1636 return false;
1637 }
1638 rl_src = LoadValue(rl_src, kCoreReg);
1639 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1640 if (power_of_two) {
1641 // Shift.
1642 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
1643 } else if (pop_count_le2) {
1644 // Shift and add and shift.
1645 int first_bit = LowestSetBit(lit);
1646 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1647 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1648 } else {
1649 // Reverse subtract: (src << (shift + 1)) - src.
1650 DCHECK(power_of_two_minus_one);
1651 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1652 int t_reg = AllocTemp();
1653 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1654 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1655 }
1656 StoreValue(rl_dest, rl_result);
1657 return true;
1658}
1659
1660void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001661 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 RegLocation rl_result;
1663 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1664 int shift_op = false;
1665 bool is_div = false;
1666
1667 switch (opcode) {
1668 case Instruction::RSUB_INT_LIT8:
1669 case Instruction::RSUB_INT: {
1670 rl_src = LoadValue(rl_src, kCoreReg);
1671 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1672 if (cu_->instruction_set == kThumb2) {
1673 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1674 } else {
1675 OpRegReg(kOpNeg, rl_result.low_reg, rl_src.low_reg);
1676 OpRegImm(kOpAdd, rl_result.low_reg, lit);
1677 }
1678 StoreValue(rl_dest, rl_result);
1679 return;
1680 }
1681
1682 case Instruction::SUB_INT:
1683 case Instruction::SUB_INT_2ADDR:
1684 lit = -lit;
1685 // Intended fallthrough
1686 case Instruction::ADD_INT:
1687 case Instruction::ADD_INT_2ADDR:
1688 case Instruction::ADD_INT_LIT8:
1689 case Instruction::ADD_INT_LIT16:
1690 op = kOpAdd;
1691 break;
1692 case Instruction::MUL_INT:
1693 case Instruction::MUL_INT_2ADDR:
1694 case Instruction::MUL_INT_LIT8:
1695 case Instruction::MUL_INT_LIT16: {
1696 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1697 return;
1698 }
1699 op = kOpMul;
1700 break;
1701 }
1702 case Instruction::AND_INT:
1703 case Instruction::AND_INT_2ADDR:
1704 case Instruction::AND_INT_LIT8:
1705 case Instruction::AND_INT_LIT16:
1706 op = kOpAnd;
1707 break;
1708 case Instruction::OR_INT:
1709 case Instruction::OR_INT_2ADDR:
1710 case Instruction::OR_INT_LIT8:
1711 case Instruction::OR_INT_LIT16:
1712 op = kOpOr;
1713 break;
1714 case Instruction::XOR_INT:
1715 case Instruction::XOR_INT_2ADDR:
1716 case Instruction::XOR_INT_LIT8:
1717 case Instruction::XOR_INT_LIT16:
1718 op = kOpXor;
1719 break;
1720 case Instruction::SHL_INT_LIT8:
1721 case Instruction::SHL_INT:
1722 case Instruction::SHL_INT_2ADDR:
1723 lit &= 31;
1724 shift_op = true;
1725 op = kOpLsl;
1726 break;
1727 case Instruction::SHR_INT_LIT8:
1728 case Instruction::SHR_INT:
1729 case Instruction::SHR_INT_2ADDR:
1730 lit &= 31;
1731 shift_op = true;
1732 op = kOpAsr;
1733 break;
1734 case Instruction::USHR_INT_LIT8:
1735 case Instruction::USHR_INT:
1736 case Instruction::USHR_INT_2ADDR:
1737 lit &= 31;
1738 shift_op = true;
1739 op = kOpLsr;
1740 break;
1741
1742 case Instruction::DIV_INT:
1743 case Instruction::DIV_INT_2ADDR:
1744 case Instruction::DIV_INT_LIT8:
1745 case Instruction::DIV_INT_LIT16:
1746 case Instruction::REM_INT:
1747 case Instruction::REM_INT_2ADDR:
1748 case Instruction::REM_INT_LIT8:
1749 case Instruction::REM_INT_LIT16: {
1750 if (lit == 0) {
1751 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1752 return;
1753 }
buzbee11b63d12013-08-27 07:34:17 -07001754 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001756 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 (opcode == Instruction::DIV_INT_LIT16)) {
1758 is_div = true;
1759 } else {
1760 is_div = false;
1761 }
buzbee11b63d12013-08-27 07:34:17 -07001762 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1763 return;
1764 }
Dave Allison70202782013-10-22 17:52:19 -07001765
1766 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 if (cu_->instruction_set == kMips) {
1768 rl_src = LoadValue(rl_src, kCoreReg);
1769 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001770 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001771 } else if (cu_->instruction_set == kX86) {
1772 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1773 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001774 } else if (cu_->instruction_set == kThumb2) {
1775 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1776 // Use ARM SDIV instruction for division. For remainder we also need to
1777 // calculate using a MUL and subtract.
1778 rl_src = LoadValue(rl_src, kCoreReg);
1779 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
1780 done = true;
1781 }
1782 }
1783
1784 if (!done) {
1785 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1787 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001788 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001789 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1790 if (is_div)
1791 rl_result = GetReturn(false);
1792 else
1793 rl_result = GetReturnAlt();
1794 }
1795 StoreValue(rl_dest, rl_result);
1796 return;
1797 }
1798 default:
1799 LOG(FATAL) << "Unexpected opcode " << opcode;
1800 }
1801 rl_src = LoadValue(rl_src, kCoreReg);
1802 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001803 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 if (shift_op && (lit == 0)) {
1805 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
1806 } else {
1807 OpRegRegImm(op, rl_result.low_reg, rl_src.low_reg, lit);
1808 }
1809 StoreValue(rl_dest, rl_result);
1810}
1811
1812void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001813 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 RegLocation rl_result;
1815 OpKind first_op = kOpBkpt;
1816 OpKind second_op = kOpBkpt;
1817 bool call_out = false;
1818 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001819 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 int ret_reg = TargetReg(kRet0);
1821
1822 switch (opcode) {
1823 case Instruction::NOT_LONG:
1824 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1825 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1826 // Check for destructive overlap
1827 if (rl_result.low_reg == rl_src2.high_reg) {
1828 int t_reg = AllocTemp();
1829 OpRegCopy(t_reg, rl_src2.high_reg);
1830 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1831 OpRegReg(kOpMvn, rl_result.high_reg, t_reg);
1832 FreeTemp(t_reg);
1833 } else {
1834 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1835 OpRegReg(kOpMvn, rl_result.high_reg, rl_src2.high_reg);
1836 }
1837 StoreValueWide(rl_dest, rl_result);
1838 return;
1839 case Instruction::ADD_LONG:
1840 case Instruction::ADD_LONG_2ADDR:
1841 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001842 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 return;
1844 }
1845 first_op = kOpAdd;
1846 second_op = kOpAdc;
1847 break;
1848 case Instruction::SUB_LONG:
1849 case Instruction::SUB_LONG_2ADDR:
1850 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001851 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 return;
1853 }
1854 first_op = kOpSub;
1855 second_op = kOpSbc;
1856 break;
1857 case Instruction::MUL_LONG:
1858 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001859 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001860 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 return;
1862 } else {
1863 call_out = true;
1864 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001865 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001866 }
1867 break;
1868 case Instruction::DIV_LONG:
1869 case Instruction::DIV_LONG_2ADDR:
1870 call_out = true;
1871 check_zero = true;
1872 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001873 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001874 break;
1875 case Instruction::REM_LONG:
1876 case Instruction::REM_LONG_2ADDR:
1877 call_out = true;
1878 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001879 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1881 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1882 break;
1883 case Instruction::AND_LONG_2ADDR:
1884 case Instruction::AND_LONG:
1885 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001886 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 }
1888 first_op = kOpAnd;
1889 second_op = kOpAnd;
1890 break;
1891 case Instruction::OR_LONG:
1892 case Instruction::OR_LONG_2ADDR:
1893 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001894 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 return;
1896 }
1897 first_op = kOpOr;
1898 second_op = kOpOr;
1899 break;
1900 case Instruction::XOR_LONG:
1901 case Instruction::XOR_LONG_2ADDR:
1902 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001903 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904 return;
1905 }
1906 first_op = kOpXor;
1907 second_op = kOpXor;
1908 break;
1909 case Instruction::NEG_LONG: {
1910 GenNegLong(rl_dest, rl_src2);
1911 return;
1912 }
1913 default:
1914 LOG(FATAL) << "Invalid long arith op";
1915 }
1916 if (!call_out) {
1917 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1918 } else {
1919 FlushAllRegs(); /* Send everything to home location */
1920 if (check_zero) {
1921 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1922 int r_tgt = CallHelperSetup(func_offset);
1923 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1924 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1925 // NOTE: callout here is not a safepoint
1926 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1927 } else {
1928 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1929 }
1930 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1931 if (ret_reg == TargetReg(kRet0))
1932 rl_result = GetReturnWide(false);
1933 else
1934 rl_result = GetReturnWideAlt();
1935 StoreValueWide(rl_dest, rl_result);
1936 }
1937}
1938
Ian Rogers848871b2013-08-05 10:56:33 -07001939void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001940 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 /*
1942 * Don't optimize the register usage since it calls out to support
1943 * functions
1944 */
1945 FlushAllRegs(); /* Send everything to home location */
1946 if (rl_src.wide) {
1947 LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1948 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1949 } else {
1950 LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1951 }
1952 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1953 if (rl_dest.wide) {
1954 RegLocation rl_result;
1955 rl_result = GetReturnWide(rl_dest.fp);
1956 StoreValueWide(rl_dest, rl_result);
1957 } else {
1958 RegLocation rl_result;
1959 rl_result = GetReturn(rl_dest.fp);
1960 StoreValue(rl_dest, rl_result);
1961 }
1962}
1963
1964/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001965void Mir2Lir::GenSuspendTest(int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1967 return;
1968 }
1969 FlushAllRegs();
1970 LIR* branch = OpTestSuspend(NULL);
1971 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001972 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1973 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 branch->target = target;
1975 suspend_launchpads_.Insert(target);
1976}
1977
1978/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001979void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1981 OpUnconditionalBranch(target);
1982 return;
1983 }
1984 OpTestSuspend(target);
1985 LIR* launch_pad =
buzbee0d829482013-10-11 15:24:55 -07001986 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1987 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988 FlushAllRegs();
1989 OpUnconditionalBranch(launch_pad);
1990 suspend_launchpads_.Insert(launch_pad);
1991}
1992
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001993/* Call out to helper assembly routine that will null check obj and then lock it. */
1994void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1995 FlushAllRegs();
1996 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
1997}
1998
1999/* Call out to helper assembly routine that will null check obj and then unlock it. */
2000void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2001 FlushAllRegs();
2002 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2003}
2004
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002005/* Generic code for generating a wide constant into a VR. */
2006void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2007 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2008 LoadConstantWide(rl_result.low_reg, rl_result.high_reg, value);
2009 StoreValueWide(rl_dest, rl_result);
2010}
2011
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012} // namespace art