blob: 7329038536745940c94b6f809ec79997c4222c99 [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) {
223 // Use the literal pool and a PC-relative load from a data word.
224 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
225 if (data_target == nullptr) {
226 data_target = AddWordData(&class_literal_list_, type_idx);
227 }
228 LIR* load_pc_rel = OpPcRelLoad(TargetReg(kArg0), data_target);
229 AppendLIR(load_pc_rel);
230 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
231 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
232 } else {
233 // Use the direct pointer.
234 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
235 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
236 }
237 } else {
238 // The slow path.
239 DCHECK_EQ(func_offset.Int32Value(), -1);
240 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
241 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
242 }
243 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700245 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800246 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 RegLocation rl_result = GetReturn(false);
249 StoreValue(rl_dest, rl_result);
250}
251
252/*
253 * Similar to GenNewArray, but with post-allocation initialization.
254 * Verifier guarantees we're dealing with an array class. Current
255 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
256 * Current code also throws internal unimp if not 'L', '[' or 'I'.
257 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700258void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 int elems = info->num_arg_words;
260 int type_idx = info->index;
261 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700262 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
264 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700265 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700267 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 }
269 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
270 FreeTemp(TargetReg(kArg2));
271 FreeTemp(TargetReg(kArg1));
272 /*
273 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
274 * return region. Because AllocFromCode placed the new array
275 * in kRet0, we'll just lock it into place. When debugger support is
276 * added, it may be necessary to additionally copy all return
277 * values to a home location in thread-local storage
278 */
279 LockTemp(TargetReg(kRet0));
280
281 // TODO: use the correct component size, currently all supported types
282 // share array alignment with ints (see comment at head of function)
283 size_t component_size = sizeof(int32_t);
284
285 // Having a range of 0 is legal
286 if (info->is_range && (elems > 0)) {
287 /*
288 * Bit of ugliness here. We're going generate a mem copy loop
289 * on the register range, but it is possible that some regs
290 * in the range have been promoted. This is unlikely, but
291 * before generating the copy, we'll just force a flush
292 * of any regs in the source range that have been promoted to
293 * home location.
294 */
295 for (int i = 0; i < elems; i++) {
296 RegLocation loc = UpdateLoc(info->args[i]);
297 if (loc.location == kLocPhysReg) {
298 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
299 loc.low_reg, kWord);
300 }
301 }
302 /*
303 * TUNING note: generated code here could be much improved, but
304 * this is an uncommon operation and isn't especially performance
305 * critical.
306 */
307 int r_src = AllocTemp();
308 int r_dst = AllocTemp();
309 int r_idx = AllocTemp();
310 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700311 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 case kThumb2:
313 r_val = TargetReg(kLr);
314 break;
315 case kX86:
316 FreeTemp(TargetReg(kRet0));
317 r_val = AllocTemp();
318 break;
319 case kMips:
320 r_val = AllocTemp();
321 break;
322 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
323 }
324 // Set up source pointer
325 RegLocation rl_first = info->args[0];
326 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
327 // Set up the target pointer
328 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
329 mirror::Array::DataOffset(component_size).Int32Value());
330 // Set up the loop counter (known to be > 0)
331 LoadConstant(r_idx, elems - 1);
332 // Generate the copy loop. Going backwards for convenience
333 LIR* target = NewLIR0(kPseudoTargetLabel);
334 // Copy next element
335 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
336 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
337 FreeTemp(r_val);
338 OpDecAndBranch(kCondGe, r_idx, target);
339 if (cu_->instruction_set == kX86) {
340 // Restore the target pointer
341 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
342 -mirror::Array::DataOffset(component_size).Int32Value());
343 }
344 } else if (!info->is_range) {
345 // TUNING: interleave
346 for (int i = 0; i < elems; i++) {
347 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
348 StoreBaseDisp(TargetReg(kRet0),
349 mirror::Array::DataOffset(component_size).Int32Value() +
350 i * 4, rl_arg.low_reg, kWord);
351 // If the LoadValue caused a temp to be allocated, free it
352 if (IsTemp(rl_arg.low_reg)) {
353 FreeTemp(rl_arg.low_reg);
354 }
355 }
356 }
357 if (info->result.location != kLocInvalid) {
358 StoreValue(info->result, GetReturn(false /* not fp */));
359 }
360}
361
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800362//
363// Slow path to ensure a class is initialized for sget/sput.
364//
365class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
366 public:
367 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
368 int storage_index, int r_base) :
369 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
370 r_base_(r_base) {
371 }
372
373 void Compile() {
374 LIR* unresolved_target = GenerateTargetLabel();
375 uninit_->target = unresolved_target;
376 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
377 storage_index_, true);
378 // Copy helper's result into r_base, a no-op on all but MIPS.
379 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
380
381 m2l_->OpUnconditionalBranch(cont_);
382 }
383
384 private:
385 LIR* const uninit_;
386 const int storage_index_;
387 const int r_base_;
388};
389
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390void Mir2Lir::GenSput(uint32_t field_idx, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700391 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800393 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 bool is_volatile;
395 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800396 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700398 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), true,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800399 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 if (fast_path && !SLOW_FIELD_PATH) {
401 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800402 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 if (is_referrers_class) {
404 // Fast path, static storage base is this method's class
405 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800406 r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800408 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 if (IsTemp(rl_method.low_reg)) {
410 FreeTemp(rl_method.low_reg);
411 }
412 } else {
413 // Medium path, static storage base in a different class which requires checks that the other
414 // class is initialized.
415 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800416 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 // May do runtime call so everything to home locations.
418 FlushAllRegs();
419 // Using fixed register to sync with possible call to runtime support.
420 int r_method = TargetReg(kArg1);
421 LockTemp(r_method);
422 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800423 r_base = TargetReg(kArg0);
424 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800426 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
427 r_base);
428 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
429 sizeof(int32_t*) * storage_index, r_base);
430 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
431 if (!is_initialized) {
432 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800433
434 // The slow path is invoked if the r_base is NULL or the class pointed
435 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800436 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
437 int r_tmp = TargetReg(kArg2);
438 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800439 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800440 mirror::Class::StatusOffset().Int32Value(),
441 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800442 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800443
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800444 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
445 unresolved_branch, uninit_branch, cont,
446 storage_index, r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800447
448 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 FreeTemp(r_method);
451 }
452 // rBase now holds static storage base
453 if (is_long_or_double) {
454 rl_src = LoadValueWide(rl_src, kAnyReg);
455 } else {
456 rl_src = LoadValue(rl_src, kAnyReg);
457 }
458 if (is_volatile) {
459 GenMemBarrier(kStoreStore);
460 }
461 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800462 StoreBaseDispWide(r_base, field_offset, rl_src.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 rl_src.high_reg);
464 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800465 StoreWordDisp(r_base, field_offset, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 if (is_volatile) {
468 GenMemBarrier(kStoreLoad);
469 }
470 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800471 MarkGCCard(rl_src.low_reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800473 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 } else {
475 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700476 ThreadOffset setter_offset =
477 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
478 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
479 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 CallRuntimeHelperImmRegLocation(setter_offset, field_idx, rl_src, true);
481 }
482}
483
484void Mir2Lir::GenSget(uint32_t field_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700485 bool is_long_or_double, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800487 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 bool is_volatile;
489 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800490 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700492 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), false,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800493 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 if (fast_path && !SLOW_FIELD_PATH) {
495 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800496 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 if (is_referrers_class) {
498 // Fast path, static storage base is this method's class
499 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800500 r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800502 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 } else {
504 // Medium path, static storage base in a different class which requires checks that the other
505 // class is initialized
Ian Rogers5ddb4102014-01-07 08:58:46 -0800506 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 // May do runtime call so everything to home locations.
508 FlushAllRegs();
509 // Using fixed register to sync with possible call to runtime support.
510 int r_method = TargetReg(kArg1);
511 LockTemp(r_method);
512 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800513 r_base = TargetReg(kArg0);
514 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800516 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
517 r_base);
518 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
519 sizeof(int32_t*) * storage_index, r_base);
520 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
521 if (!is_initialized) {
522 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800523
524 // The slow path is invoked if the r_base is NULL or the class pointed
525 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800526 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
527 int r_tmp = TargetReg(kArg2);
528 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800529 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800530 mirror::Class::StatusOffset().Int32Value(),
531 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800532 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800533
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
535 unresolved_branch, uninit_branch, cont,
536 storage_index, r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800537
538 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 FreeTemp(r_method);
541 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800542 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
544 if (is_volatile) {
545 GenMemBarrier(kLoadLoad);
546 }
547 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800548 LoadBaseDispWide(r_base, field_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 rl_result.high_reg, INVALID_SREG);
550 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800551 LoadWordDisp(r_base, field_offset, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800553 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 if (is_long_or_double) {
555 StoreValueWide(rl_dest, rl_result);
556 } else {
557 StoreValue(rl_dest, rl_result);
558 }
559 } else {
560 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700561 ThreadOffset getterOffset =
562 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
563 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
564 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 CallRuntimeHelperImm(getterOffset, field_idx, true);
566 if (is_long_or_double) {
567 RegLocation rl_result = GetReturnWide(rl_dest.fp);
568 StoreValueWide(rl_dest, rl_result);
569 } else {
570 RegLocation rl_result = GetReturn(rl_dest.fp);
571 StoreValue(rl_dest, rl_result);
572 }
573 }
574}
575
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800576// Generate code for all slow paths.
577void Mir2Lir::HandleSlowPaths() {
578 int n = slow_paths_.Size();
579 for (int i = 0; i < n; ++i) {
580 LIRSlowPath* slowpath = slow_paths_.Get(i);
581 slowpath->Compile();
582 }
583 slow_paths_.Reset();
584}
585
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700586void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700588 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 for (int i = 0; i < num_elems; i++) {
590 ResetRegPool();
591 ResetDefTracking();
592 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700593 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 current_dalvik_offset_ = lab->operands[1];
595 AppendLIR(lab);
596 int r_tgt = CallHelperSetup(helper_offset);
597 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
598 OpUnconditionalBranch(resume_lab);
599 }
600}
601
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700602void Mir2Lir::HandleIntrinsicLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 int num_elems = intrinsic_launchpads_.Size();
604 for (int i = 0; i < num_elems; i++) {
605 ResetRegPool();
606 ResetDefTracking();
607 LIR* lab = intrinsic_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700608 CallInfo* info = reinterpret_cast<CallInfo*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 current_dalvik_offset_ = info->offset;
610 AppendLIR(lab);
611 // NOTE: GenInvoke handles MarkSafepointPC
612 GenInvoke(info);
buzbee0d829482013-10-11 15:24:55 -0700613 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 if (resume_lab != NULL) {
615 OpUnconditionalBranch(resume_lab);
616 }
617 }
618}
619
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700620void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 int num_elems = throw_launchpads_.Size();
622 for (int i = 0; i < num_elems; i++) {
623 ResetRegPool();
624 ResetDefTracking();
625 LIR* lab = throw_launchpads_.Get(i);
626 current_dalvik_offset_ = lab->operands[1];
627 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700628 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 int v1 = lab->operands[2];
630 int v2 = lab->operands[3];
631 bool target_x86 = (cu_->instruction_set == kX86);
632 switch (lab->operands[0]) {
633 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700634 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700636 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
638 if (target_x86) {
639 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
640 } else {
641 OpRegCopy(TargetReg(kArg1), v1);
642 }
643 // Make sure the following LoadConstant doesn't mess with kArg1.
644 LockTemp(TargetReg(kArg1));
645 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700646 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 break;
648 case kThrowArrayBounds:
649 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
650 if (v2 != TargetReg(kArg0)) {
651 OpRegCopy(TargetReg(kArg0), v1);
652 if (target_x86) {
653 // x86 leaves the array pointer in v2, so load the array length that the handler expects
654 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
655 } else {
656 OpRegCopy(TargetReg(kArg1), v2);
657 }
658 } else {
659 if (v1 == TargetReg(kArg1)) {
660 // Swap v1 and v2, using kArg2 as a temp
661 OpRegCopy(TargetReg(kArg2), v1);
662 if (target_x86) {
663 // x86 leaves the array pointer in v2; load the array length that the handler expects
664 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
665 } else {
666 OpRegCopy(TargetReg(kArg1), v2);
667 }
668 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
669 } else {
670 if (target_x86) {
671 // x86 leaves the array pointer in v2; load the array length that the handler expects
672 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
673 } else {
674 OpRegCopy(TargetReg(kArg1), v2);
675 }
676 OpRegCopy(TargetReg(kArg0), v1);
677 }
678 }
Ian Rogers848871b2013-08-05 10:56:33 -0700679 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 break;
681 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700682 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 break;
684 case kThrowNoSuchMethod:
685 OpRegCopy(TargetReg(kArg0), v1);
686 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700687 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 break;
689 case kThrowStackOverflow:
Ian Rogers848871b2013-08-05 10:56:33 -0700690 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 // Restore stack alignment
692 if (target_x86) {
693 OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
694 } else {
695 OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
696 }
697 break;
698 default:
699 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
700 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000701 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 int r_tgt = CallHelperSetup(func_offset);
703 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
704 }
705}
706
707void Mir2Lir::GenIGet(uint32_t field_idx, int opt_flags, OpSize size,
708 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 int field_offset;
711 bool is_volatile;
712
Ian Rogers9b297bf2013-09-06 11:11:25 -0700713 bool fast_path = FastInstance(field_idx, false, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714
715 if (fast_path && !SLOW_FIELD_PATH) {
716 RegLocation rl_result;
717 RegisterClass reg_class = oat_reg_class_by_size(size);
718 DCHECK_GE(field_offset, 0);
719 rl_obj = LoadValue(rl_obj, kCoreReg);
720 if (is_long_or_double) {
721 DCHECK(rl_dest.wide);
722 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
723 if (cu_->instruction_set == kX86) {
724 rl_result = EvalLoc(rl_dest, reg_class, true);
725 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
726 LoadBaseDispWide(rl_obj.low_reg, field_offset, rl_result.low_reg,
727 rl_result.high_reg, rl_obj.s_reg_low);
728 if (is_volatile) {
729 GenMemBarrier(kLoadLoad);
730 }
731 } else {
732 int reg_ptr = AllocTemp();
733 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
734 rl_result = EvalLoc(rl_dest, reg_class, true);
735 LoadBaseDispWide(reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
736 if (is_volatile) {
737 GenMemBarrier(kLoadLoad);
738 }
739 FreeTemp(reg_ptr);
740 }
741 StoreValueWide(rl_dest, rl_result);
742 } else {
743 rl_result = EvalLoc(rl_dest, reg_class, true);
744 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
745 LoadBaseDisp(rl_obj.low_reg, field_offset, rl_result.low_reg,
746 kWord, rl_obj.s_reg_low);
747 if (is_volatile) {
748 GenMemBarrier(kLoadLoad);
749 }
750 StoreValue(rl_dest, rl_result);
751 }
752 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700753 ThreadOffset getterOffset =
754 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
755 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
756 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 CallRuntimeHelperImmRegLocation(getterOffset, field_idx, rl_obj, true);
758 if (is_long_or_double) {
759 RegLocation rl_result = GetReturnWide(rl_dest.fp);
760 StoreValueWide(rl_dest, rl_result);
761 } else {
762 RegLocation rl_result = GetReturn(rl_dest.fp);
763 StoreValue(rl_dest, rl_result);
764 }
765 }
766}
767
768void Mir2Lir::GenIPut(uint32_t field_idx, int opt_flags, OpSize size,
769 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700770 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 int field_offset;
772 bool is_volatile;
773
Ian Rogers9b297bf2013-09-06 11:11:25 -0700774 bool fast_path = FastInstance(field_idx, true, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 if (fast_path && !SLOW_FIELD_PATH) {
776 RegisterClass reg_class = oat_reg_class_by_size(size);
777 DCHECK_GE(field_offset, 0);
778 rl_obj = LoadValue(rl_obj, kCoreReg);
779 if (is_long_or_double) {
780 int reg_ptr;
781 rl_src = LoadValueWide(rl_src, kAnyReg);
782 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
783 reg_ptr = AllocTemp();
784 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
785 if (is_volatile) {
786 GenMemBarrier(kStoreStore);
787 }
788 StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
789 if (is_volatile) {
790 GenMemBarrier(kLoadLoad);
791 }
792 FreeTemp(reg_ptr);
793 } else {
794 rl_src = LoadValue(rl_src, reg_class);
795 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
796 if (is_volatile) {
797 GenMemBarrier(kStoreStore);
798 }
799 StoreBaseDisp(rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
800 if (is_volatile) {
801 GenMemBarrier(kLoadLoad);
802 }
803 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
804 MarkGCCard(rl_src.low_reg, rl_obj.low_reg);
805 }
806 }
807 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700808 ThreadOffset setter_offset =
809 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
810 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
811 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_idx, rl_obj, rl_src, true);
813 }
814}
815
Ian Rogersa9a82542013-10-04 11:17:26 -0700816void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
817 RegLocation rl_src) {
818 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
819 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
820 (opt_flags & MIR_IGNORE_NULL_CHECK));
821 ThreadOffset helper = needs_range_check
822 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
823 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
824 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
825 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
826}
827
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700828void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 RegLocation rl_method = LoadCurrMethod();
830 int res_reg = AllocTemp();
831 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
832 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
833 *cu_->dex_file,
834 type_idx)) {
835 // Call out to helper which resolves type and verifies access.
836 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700837 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 type_idx, rl_method.low_reg, true);
839 RegLocation rl_result = GetReturn(false);
840 StoreValue(rl_dest, rl_result);
841 } else {
842 // We're don't need access checks, load type from dex cache
843 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700844 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 LoadWordDisp(rl_method.low_reg, dex_cache_offset, res_reg);
846 int32_t offset_of_type =
847 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
848 * type_idx);
849 LoadWordDisp(res_reg, offset_of_type, rl_result.low_reg);
850 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
851 type_idx) || SLOW_TYPE_PATH) {
852 // Slow path, at runtime test if type is null and if so initialize
853 FlushAllRegs();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800854 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.low_reg, 0, NULL);
855 LIR* cont = NewLIR0(kPseudoTargetLabel);
856
857 // Object to generate the slow path for class resolution.
858 class SlowPath : public LIRSlowPath {
859 public:
860 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
861 const RegLocation& rl_method, const RegLocation& rl_result) :
862 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
863 rl_method_(rl_method), rl_result_(rl_result) {
864 }
865
866 void Compile() {
867 GenerateTargetLabel();
868
869 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
870 rl_method_.low_reg, true);
871 m2l_->OpRegCopy(rl_result_.low_reg, m2l_->TargetReg(kRet0));
872
873 m2l_->OpUnconditionalBranch(cont_);
874 }
875
876 private:
877 const int type_idx_;
878 const RegLocation rl_method_;
879 const RegLocation rl_result_;
880 };
881
882 // Add to list for future.
883 AddSlowPath(new (arena_) SlowPath(this, branch, cont,
884 type_idx, rl_method, rl_result));
885
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800887 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 // Fast path, we're done - just store result
889 StoreValue(rl_dest, rl_result);
890 }
891 }
892}
893
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700894void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 /* NOTE: Most strings should be available at compile time */
896 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
897 (sizeof(mirror::String*) * string_idx);
898 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
899 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
900 // slow path, resolve string if not in dex cache
901 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700902 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800903
904 // If the Method* is already in a register, we can save a copy.
905 RegLocation rl_method = mir_graph_->GetMethodLoc();
906 int r_method;
907 if (rl_method.location == kLocPhysReg) {
908 // A temp would conflict with register use below.
909 DCHECK(!IsTemp(rl_method.low_reg));
910 r_method = rl_method.low_reg;
911 } else {
912 r_method = TargetReg(kArg2);
913 LoadCurrMethodDirect(r_method);
914 }
915 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
916 TargetReg(kArg0));
917
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800920 if (cu_->instruction_set == kThumb2 ||
921 cu_->instruction_set == kMips) {
922 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800923 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800924 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
925 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800927
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800928 // Object to generate the slow path for string resolution.
929 class SlowPath : public LIRSlowPath {
930 public:
931 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
932 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
933 }
934
935 void Compile() {
936 GenerateTargetLabel();
937
938 int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
939
940 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
941 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
942 m2l_->MarkSafepointPC(call_inst);
943 m2l_->FreeTemp(r_tgt);
944
945 m2l_->OpUnconditionalBranch(cont_);
946 }
947
948 private:
949 int r_method_;
950 };
951
952 // Add to list for future.
953 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 } else {
955 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800956 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
957 LoadConstant(TargetReg(kArg1), string_idx);
958 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
Ian Rogers7655f292013-07-29 11:07:13 -0700959 TargetReg(kArg1), true);
Mark Mendell766e9292014-01-27 07:55:47 -0800960 LIR* target = NewLIR0(kPseudoTargetLabel);
961 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 }
963 GenBarrier();
964 StoreValue(rl_dest, GetReturn(false));
965 } else {
966 RegLocation rl_method = LoadCurrMethod();
967 int res_reg = AllocTemp();
968 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
969 LoadWordDisp(rl_method.low_reg,
Brian Carlstromea46f952013-07-30 01:26:50 -0700970 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 LoadWordDisp(res_reg, offset_of_string, rl_result.low_reg);
972 StoreValue(rl_dest, rl_result);
973 }
974}
975
976/*
977 * Let helper function take care of everything. Will
978 * call Class::NewInstanceFromCode(type_idx, method);
979 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700980void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 FlushAllRegs(); /* Everything to home location */
982 // alloc will always check for resolution, do we also need to verify
983 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700984 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800985 const DexFile* dex_file = cu_->dex_file;
986 CompilerDriver* driver = cu_->compiler_driver;
987 if (driver->CanAccessInstantiableTypeWithoutChecks(
988 cu_->method_idx, *dex_file, type_idx)) {
989 bool is_type_initialized;
990 bool use_direct_type_ptr;
991 uintptr_t direct_type_ptr;
992 if (kEmbedClassInCode &&
993 driver->CanEmbedTypeInCode(*dex_file, type_idx,
994 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
995 // The fast path.
996 if (!use_direct_type_ptr) {
997 // Use the literal pool and a PC-relative load from a data word.
998 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
999 if (data_target == nullptr) {
1000 data_target = AddWordData(&class_literal_list_, type_idx);
1001 }
1002 LIR* load_pc_rel = OpPcRelLoad(TargetReg(kArg0), data_target);
1003 AppendLIR(load_pc_rel);
1004 if (!is_type_initialized) {
1005 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1006 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1007 } else {
1008 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1009 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1010 }
1011 } else {
1012 // Use the direct pointer.
1013 if (!is_type_initialized) {
1014 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1015 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1016 } else {
1017 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1018 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1019 }
1020 }
1021 } else {
1022 // The slow path.
1023 DCHECK_EQ(func_offset.Int32Value(), -1);
1024 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1025 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1026 }
1027 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001029 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001030 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 RegLocation rl_result = GetReturn(false);
1033 StoreValue(rl_dest, rl_result);
1034}
1035
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001036void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -07001038 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039}
1040
1041// For final classes there are no sub-classes to check and so we can answer the instance-of
1042// question with simple comparisons.
1043void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1044 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001045 // X86 has its own implementation.
1046 DCHECK_NE(cu_->instruction_set, kX86);
1047
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 RegLocation object = LoadValue(rl_src, kCoreReg);
1049 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1050 int result_reg = rl_result.low_reg;
1051 if (result_reg == object.low_reg) {
1052 result_reg = AllocTypedTemp(false, kCoreReg);
1053 }
1054 LoadConstant(result_reg, 0); // assume false
1055 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL);
1056
1057 int check_class = AllocTypedTemp(false, kCoreReg);
1058 int object_class = AllocTypedTemp(false, kCoreReg);
1059
1060 LoadCurrMethodDirect(check_class);
1061 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001062 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 check_class);
1064 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
1065 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001066 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 check_class);
1068 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
1069 int32_t offset_of_type =
1070 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1071 (sizeof(mirror::Class*) * type_idx);
1072 LoadWordDisp(check_class, offset_of_type, check_class);
1073 }
1074
1075 LIR* ne_branchover = NULL;
1076 if (cu_->instruction_set == kThumb2) {
1077 OpRegReg(kOpCmp, check_class, object_class); // Same?
1078 OpIT(kCondEq, ""); // if-convert the test
1079 LoadConstant(result_reg, 1); // .eq case - load true
1080 } else {
1081 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1082 LoadConstant(result_reg, 1); // eq case - load true
1083 }
1084 LIR* target = NewLIR0(kPseudoTargetLabel);
1085 null_branchover->target = target;
1086 if (ne_branchover != NULL) {
1087 ne_branchover->target = target;
1088 }
1089 FreeTemp(object_class);
1090 FreeTemp(check_class);
1091 if (IsTemp(result_reg)) {
1092 OpRegCopy(rl_result.low_reg, result_reg);
1093 FreeTemp(result_reg);
1094 }
1095 StoreValue(rl_dest, rl_result);
1096}
1097
1098void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1099 bool type_known_abstract, bool use_declaring_class,
1100 bool can_assume_type_is_in_dex_cache,
1101 uint32_t type_idx, RegLocation rl_dest,
1102 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001103 // X86 has its own implementation.
1104 DCHECK_NE(cu_->instruction_set, kX86);
1105
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 FlushAllRegs();
1107 // May generate a call - use explicit registers
1108 LockCallTemps();
1109 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1110 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1111 if (needs_access_check) {
1112 // Check we have access to type_idx and if not throw IllegalAccessError,
1113 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001114 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 type_idx, true);
1116 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1117 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1118 } else if (use_declaring_class) {
1119 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1120 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001121 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
1123 // Load dex cache entry into class_reg (kArg2)
1124 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1125 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001126 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 int32_t offset_of_type =
1128 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1129 * type_idx);
1130 LoadWordDisp(class_reg, offset_of_type, class_reg);
1131 if (!can_assume_type_is_in_dex_cache) {
1132 // Need to test presence of type in dex cache at runtime
1133 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1134 // Not resolved
1135 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001136 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001137 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1139 // Rejoin code paths
1140 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1141 hop_branch->target = hop_target;
1142 }
1143 }
1144 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1145 RegLocation rl_result = GetReturn(false);
1146 if (cu_->instruction_set == kMips) {
1147 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
1148 LoadConstant(rl_result.low_reg, 0);
1149 }
1150 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1151
1152 /* load object->klass_ */
1153 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1154 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1155 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1156 LIR* branchover = NULL;
1157 if (type_known_final) {
1158 // rl_result == ref == null == 0.
1159 if (cu_->instruction_set == kThumb2) {
1160 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1161 OpIT(kCondEq, "E"); // if-convert the test
1162 LoadConstant(rl_result.low_reg, 1); // .eq case - load true
1163 LoadConstant(rl_result.low_reg, 0); // .ne case - load false
1164 } else {
1165 LoadConstant(rl_result.low_reg, 0); // ne case - load false
1166 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
1167 LoadConstant(rl_result.low_reg, 1); // eq case - load true
1168 }
1169 } else {
1170 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001171 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (!type_known_abstract) {
1173 /* Uses conditional nullification */
1174 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1175 OpIT(kCondEq, "EE"); // if-convert the test
1176 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1177 }
1178 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1179 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1180 FreeTemp(r_tgt);
1181 } else {
1182 if (!type_known_abstract) {
1183 /* Uses branchovers */
1184 LoadConstant(rl_result.low_reg, 1); // assume true
1185 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1186 }
Mark Mendell6607d972014-02-10 06:54:18 -08001187 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1188 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1189 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1190 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 }
1192 }
1193 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001194 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 /* branch targets here */
1196 LIR* target = NewLIR0(kPseudoTargetLabel);
1197 StoreValue(rl_dest, rl_result);
1198 branch1->target = target;
1199 if (branchover != NULL) {
1200 branchover->target = target;
1201 }
1202}
1203
1204void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1205 bool type_known_final, type_known_abstract, use_declaring_class;
1206 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1207 *cu_->dex_file,
1208 type_idx,
1209 &type_known_final,
1210 &type_known_abstract,
1211 &use_declaring_class);
1212 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1213 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1214
1215 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1216 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1217 } else {
1218 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1219 use_declaring_class, can_assume_type_is_in_dex_cache,
1220 type_idx, rl_dest, rl_src);
1221 }
1222}
1223
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001224void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 bool type_known_final, type_known_abstract, use_declaring_class;
1226 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1227 *cu_->dex_file,
1228 type_idx,
1229 &type_known_final,
1230 &type_known_abstract,
1231 &use_declaring_class);
1232 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1233 // of the exception throw path.
1234 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001235 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 // Verifier type analysis proved this check cast would never cause an exception.
1237 return;
1238 }
1239 FlushAllRegs();
1240 // May generate a call - use explicit registers
1241 LockCallTemps();
1242 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1243 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1244 if (needs_access_check) {
1245 // Check we have access to type_idx and if not throw IllegalAccessError,
1246 // returns Class* in kRet0
1247 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001248 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 type_idx, TargetReg(kArg1), true);
1250 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1251 } else if (use_declaring_class) {
1252 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001253 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 } else {
1255 // Load dex cache entry into class_reg (kArg2)
1256 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001257 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 int32_t offset_of_type =
1259 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1260 (sizeof(mirror::Class*) * type_idx);
1261 LoadWordDisp(class_reg, offset_of_type, class_reg);
1262 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1263 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001264 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1265 LIR* cont = NewLIR0(kPseudoTargetLabel);
1266
1267 // Slow path to initialize the type. Executed if the type is NULL.
1268 class SlowPath : public LIRSlowPath {
1269 public:
1270 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1271 const int class_reg) :
1272 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1273 class_reg_(class_reg) {
1274 }
1275
1276 void Compile() {
1277 GenerateTargetLabel();
1278
1279 // Call out to helper, which will return resolved type in kArg0
1280 // InitializeTypeFromCode(idx, method)
1281 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1282 m2l_->TargetReg(kArg1), true);
1283 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1284 m2l_->OpUnconditionalBranch(cont_);
1285 }
1286 public:
1287 const int type_idx_;
1288 const int class_reg_;
1289 };
1290
1291 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1292 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
1294 }
1295 // At this point, class_reg (kArg2) has class
1296 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001297
1298 // Slow path for the case where the classes are not equal. In this case we need
1299 // to call a helper function to do the check.
1300 class SlowPath : public LIRSlowPath {
1301 public:
1302 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1303 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1304 }
1305
1306 void Compile() {
1307 GenerateTargetLabel();
1308
1309 if (load_) {
1310 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1311 m2l_->TargetReg(kArg1));
1312 }
1313 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1314 m2l_->TargetReg(kArg1), true);
1315
1316 m2l_->OpUnconditionalBranch(cont_);
1317 }
1318
1319 private:
1320 bool load_;
1321 };
1322
1323 if (type_known_abstract) {
1324 // Easier case, run slow path if target is non-null (slow path will load from target)
1325 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1326 LIR* cont = NewLIR0(kPseudoTargetLabel);
1327 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1328 } else {
1329 // Harder, more common case. We need to generate a forward branch over the load
1330 // if the target is null. If it's non-null we perform the load and branch to the
1331 // slow path if the classes are not equal.
1332
1333 /* Null is OK - continue */
1334 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1335 /* load object->klass_ */
1336 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1337 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1338 TargetReg(kArg1));
1339
1340 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1341 LIR* cont = NewLIR0(kPseudoTargetLabel);
1342
1343 // Add the slow path that will not perform load since this is already done.
1344 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1345
1346 // Set the null check to branch to the continuation.
1347 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 }
1349}
1350
1351void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001352 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 RegLocation rl_result;
1354 if (cu_->instruction_set == kThumb2) {
1355 /*
1356 * NOTE: This is the one place in the code in which we might have
1357 * as many as six live temporary registers. There are 5 in the normal
1358 * set for Arm. Until we have spill capabilities, temporarily add
1359 * lr to the temp set. It is safe to do this locally, but note that
1360 * lr is used explicitly elsewhere in the code generator and cannot
1361 * normally be used as a general temp register.
1362 */
1363 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1364 FreeTemp(TargetReg(kLr)); // and make it available
1365 }
1366 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1367 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1368 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1369 // The longs may overlap - use intermediate temp if so
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001370 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 int t_reg = AllocTemp();
1372 OpRegRegReg(first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1373 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1374 OpRegCopy(rl_result.low_reg, t_reg);
1375 FreeTemp(t_reg);
1376 } else {
1377 OpRegRegReg(first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1378 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg,
1379 rl_src2.high_reg);
1380 }
1381 /*
1382 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1383 * following StoreValueWide might need to allocate a temp register.
1384 * To further work around the lack of a spill capability, explicitly
1385 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1386 * Remove when spill is functional.
1387 */
1388 FreeRegLocTemps(rl_result, rl_src1);
1389 FreeRegLocTemps(rl_result, rl_src2);
1390 StoreValueWide(rl_dest, rl_result);
1391 if (cu_->instruction_set == kThumb2) {
1392 Clobber(TargetReg(kLr));
1393 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1394 }
1395}
1396
1397
1398void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001399 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001400 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401
1402 switch (opcode) {
1403 case Instruction::SHL_LONG:
1404 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001405 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 break;
1407 case Instruction::SHR_LONG:
1408 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001409 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 break;
1411 case Instruction::USHR_LONG:
1412 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001413 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 break;
1415 default:
1416 LOG(FATAL) << "Unexpected case";
1417 }
1418 FlushAllRegs(); /* Send everything to home location */
1419 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1420 RegLocation rl_result = GetReturnWide(false);
1421 StoreValueWide(rl_dest, rl_result);
1422}
1423
1424
1425void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001426 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001427 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 OpKind op = kOpBkpt;
1429 bool is_div_rem = false;
1430 bool check_zero = false;
1431 bool unary = false;
1432 RegLocation rl_result;
1433 bool shift_op = false;
1434 switch (opcode) {
1435 case Instruction::NEG_INT:
1436 op = kOpNeg;
1437 unary = true;
1438 break;
1439 case Instruction::NOT_INT:
1440 op = kOpMvn;
1441 unary = true;
1442 break;
1443 case Instruction::ADD_INT:
1444 case Instruction::ADD_INT_2ADDR:
1445 op = kOpAdd;
1446 break;
1447 case Instruction::SUB_INT:
1448 case Instruction::SUB_INT_2ADDR:
1449 op = kOpSub;
1450 break;
1451 case Instruction::MUL_INT:
1452 case Instruction::MUL_INT_2ADDR:
1453 op = kOpMul;
1454 break;
1455 case Instruction::DIV_INT:
1456 case Instruction::DIV_INT_2ADDR:
1457 check_zero = true;
1458 op = kOpDiv;
1459 is_div_rem = true;
1460 break;
1461 /* NOTE: returns in kArg1 */
1462 case Instruction::REM_INT:
1463 case Instruction::REM_INT_2ADDR:
1464 check_zero = true;
1465 op = kOpRem;
1466 is_div_rem = true;
1467 break;
1468 case Instruction::AND_INT:
1469 case Instruction::AND_INT_2ADDR:
1470 op = kOpAnd;
1471 break;
1472 case Instruction::OR_INT:
1473 case Instruction::OR_INT_2ADDR:
1474 op = kOpOr;
1475 break;
1476 case Instruction::XOR_INT:
1477 case Instruction::XOR_INT_2ADDR:
1478 op = kOpXor;
1479 break;
1480 case Instruction::SHL_INT:
1481 case Instruction::SHL_INT_2ADDR:
1482 shift_op = true;
1483 op = kOpLsl;
1484 break;
1485 case Instruction::SHR_INT:
1486 case Instruction::SHR_INT_2ADDR:
1487 shift_op = true;
1488 op = kOpAsr;
1489 break;
1490 case Instruction::USHR_INT:
1491 case Instruction::USHR_INT_2ADDR:
1492 shift_op = true;
1493 op = kOpLsr;
1494 break;
1495 default:
1496 LOG(FATAL) << "Invalid word arith op: " << opcode;
1497 }
1498 if (!is_div_rem) {
1499 if (unary) {
1500 rl_src1 = LoadValue(rl_src1, kCoreReg);
1501 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1502 OpRegReg(op, rl_result.low_reg, rl_src1.low_reg);
1503 } else {
1504 if (shift_op) {
1505 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001506 rl_src2 = LoadValue(rl_src2, kCoreReg);
1507 t_reg = AllocTemp();
1508 OpRegRegImm(kOpAnd, t_reg, rl_src2.low_reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 rl_src1 = LoadValue(rl_src1, kCoreReg);
1510 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1511 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1512 FreeTemp(t_reg);
1513 } else {
1514 rl_src1 = LoadValue(rl_src1, kCoreReg);
1515 rl_src2 = LoadValue(rl_src2, kCoreReg);
1516 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1517 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1518 }
1519 }
1520 StoreValue(rl_dest, rl_result);
1521 } else {
Dave Allison70202782013-10-22 17:52:19 -07001522 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 -07001523 if (cu_->instruction_set == kMips) {
1524 rl_src1 = LoadValue(rl_src1, kCoreReg);
1525 rl_src2 = LoadValue(rl_src2, kCoreReg);
1526 if (check_zero) {
1527 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1528 }
1529 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001530 done = true;
1531 } else if (cu_->instruction_set == kThumb2) {
1532 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1533 // Use ARM SDIV instruction for division. For remainder we also need to
1534 // calculate using a MUL and subtract.
1535 rl_src1 = LoadValue(rl_src1, kCoreReg);
1536 rl_src2 = LoadValue(rl_src2, kCoreReg);
1537 if (check_zero) {
1538 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1539 }
1540 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
1541 done = true;
1542 }
1543 }
1544
1545 // If we haven't already generated the code use the callout function.
1546 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001547 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 FlushAllRegs(); /* Send everything to home location */
1549 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1550 int r_tgt = CallHelperSetup(func_offset);
1551 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1552 if (check_zero) {
1553 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1554 }
Dave Allison70202782013-10-22 17:52:19 -07001555 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001556 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 if (op == kOpDiv)
1558 rl_result = GetReturn(false);
1559 else
1560 rl_result = GetReturnAlt();
1561 }
1562 StoreValue(rl_dest, rl_result);
1563 }
1564}
1565
1566/*
1567 * The following are the first-level codegen routines that analyze the format
1568 * of each bytecode then either dispatch special purpose codegen routines
1569 * or produce corresponding Thumb instructions directly.
1570 */
1571
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001573static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 x &= x - 1;
1575 return (x & (x - 1)) == 0;
1576}
1577
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1579// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001580bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001581 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1583 return false;
1584 }
1585 // No divide instruction for Arm, so check for more special cases
1586 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001587 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 }
1589 int k = LowestSetBit(lit);
1590 if (k >= 30) {
1591 // Avoid special cases.
1592 return false;
1593 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 rl_src = LoadValue(rl_src, kCoreReg);
1595 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001596 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 int t_reg = AllocTemp();
1598 if (lit == 2) {
1599 // Division by 2 is by far the most common division by constant.
1600 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1601 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1602 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1603 } else {
1604 OpRegRegImm(kOpAsr, t_reg, rl_src.low_reg, 31);
1605 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
1606 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1607 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1608 }
1609 } else {
1610 int t_reg1 = AllocTemp();
1611 int t_reg2 = AllocTemp();
1612 if (lit == 2) {
1613 OpRegRegImm(kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1614 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1615 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
1616 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1617 } else {
1618 OpRegRegImm(kOpAsr, t_reg1, rl_src.low_reg, 31);
1619 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
1620 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1621 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
1622 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1623 }
1624 }
1625 StoreValue(rl_dest, rl_result);
1626 return true;
1627}
1628
1629// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1630// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001631bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 // Can we simplify this multiplication?
1633 bool power_of_two = false;
1634 bool pop_count_le2 = false;
1635 bool power_of_two_minus_one = false;
1636 if (lit < 2) {
1637 // Avoid special cases.
1638 return false;
1639 } else if (IsPowerOfTwo(lit)) {
1640 power_of_two = true;
1641 } else if (IsPopCountLE2(lit)) {
1642 pop_count_le2 = true;
1643 } else if (IsPowerOfTwo(lit + 1)) {
1644 power_of_two_minus_one = true;
1645 } else {
1646 return false;
1647 }
1648 rl_src = LoadValue(rl_src, kCoreReg);
1649 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1650 if (power_of_two) {
1651 // Shift.
1652 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
1653 } else if (pop_count_le2) {
1654 // Shift and add and shift.
1655 int first_bit = LowestSetBit(lit);
1656 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1657 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1658 } else {
1659 // Reverse subtract: (src << (shift + 1)) - src.
1660 DCHECK(power_of_two_minus_one);
1661 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1662 int t_reg = AllocTemp();
1663 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1664 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1665 }
1666 StoreValue(rl_dest, rl_result);
1667 return true;
1668}
1669
1670void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001671 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 RegLocation rl_result;
1673 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1674 int shift_op = false;
1675 bool is_div = false;
1676
1677 switch (opcode) {
1678 case Instruction::RSUB_INT_LIT8:
1679 case Instruction::RSUB_INT: {
1680 rl_src = LoadValue(rl_src, kCoreReg);
1681 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1682 if (cu_->instruction_set == kThumb2) {
1683 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1684 } else {
1685 OpRegReg(kOpNeg, rl_result.low_reg, rl_src.low_reg);
1686 OpRegImm(kOpAdd, rl_result.low_reg, lit);
1687 }
1688 StoreValue(rl_dest, rl_result);
1689 return;
1690 }
1691
1692 case Instruction::SUB_INT:
1693 case Instruction::SUB_INT_2ADDR:
1694 lit = -lit;
1695 // Intended fallthrough
1696 case Instruction::ADD_INT:
1697 case Instruction::ADD_INT_2ADDR:
1698 case Instruction::ADD_INT_LIT8:
1699 case Instruction::ADD_INT_LIT16:
1700 op = kOpAdd;
1701 break;
1702 case Instruction::MUL_INT:
1703 case Instruction::MUL_INT_2ADDR:
1704 case Instruction::MUL_INT_LIT8:
1705 case Instruction::MUL_INT_LIT16: {
1706 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1707 return;
1708 }
1709 op = kOpMul;
1710 break;
1711 }
1712 case Instruction::AND_INT:
1713 case Instruction::AND_INT_2ADDR:
1714 case Instruction::AND_INT_LIT8:
1715 case Instruction::AND_INT_LIT16:
1716 op = kOpAnd;
1717 break;
1718 case Instruction::OR_INT:
1719 case Instruction::OR_INT_2ADDR:
1720 case Instruction::OR_INT_LIT8:
1721 case Instruction::OR_INT_LIT16:
1722 op = kOpOr;
1723 break;
1724 case Instruction::XOR_INT:
1725 case Instruction::XOR_INT_2ADDR:
1726 case Instruction::XOR_INT_LIT8:
1727 case Instruction::XOR_INT_LIT16:
1728 op = kOpXor;
1729 break;
1730 case Instruction::SHL_INT_LIT8:
1731 case Instruction::SHL_INT:
1732 case Instruction::SHL_INT_2ADDR:
1733 lit &= 31;
1734 shift_op = true;
1735 op = kOpLsl;
1736 break;
1737 case Instruction::SHR_INT_LIT8:
1738 case Instruction::SHR_INT:
1739 case Instruction::SHR_INT_2ADDR:
1740 lit &= 31;
1741 shift_op = true;
1742 op = kOpAsr;
1743 break;
1744 case Instruction::USHR_INT_LIT8:
1745 case Instruction::USHR_INT:
1746 case Instruction::USHR_INT_2ADDR:
1747 lit &= 31;
1748 shift_op = true;
1749 op = kOpLsr;
1750 break;
1751
1752 case Instruction::DIV_INT:
1753 case Instruction::DIV_INT_2ADDR:
1754 case Instruction::DIV_INT_LIT8:
1755 case Instruction::DIV_INT_LIT16:
1756 case Instruction::REM_INT:
1757 case Instruction::REM_INT_2ADDR:
1758 case Instruction::REM_INT_LIT8:
1759 case Instruction::REM_INT_LIT16: {
1760 if (lit == 0) {
1761 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1762 return;
1763 }
buzbee11b63d12013-08-27 07:34:17 -07001764 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001766 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 (opcode == Instruction::DIV_INT_LIT16)) {
1768 is_div = true;
1769 } else {
1770 is_div = false;
1771 }
buzbee11b63d12013-08-27 07:34:17 -07001772 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1773 return;
1774 }
Dave Allison70202782013-10-22 17:52:19 -07001775
1776 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 if (cu_->instruction_set == kMips) {
1778 rl_src = LoadValue(rl_src, kCoreReg);
1779 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001780 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001781 } else if (cu_->instruction_set == kX86) {
1782 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1783 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001784 } else if (cu_->instruction_set == kThumb2) {
1785 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1786 // Use ARM SDIV instruction for division. For remainder we also need to
1787 // calculate using a MUL and subtract.
1788 rl_src = LoadValue(rl_src, kCoreReg);
1789 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
1790 done = true;
1791 }
1792 }
1793
1794 if (!done) {
1795 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1797 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001798 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001799 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1800 if (is_div)
1801 rl_result = GetReturn(false);
1802 else
1803 rl_result = GetReturnAlt();
1804 }
1805 StoreValue(rl_dest, rl_result);
1806 return;
1807 }
1808 default:
1809 LOG(FATAL) << "Unexpected opcode " << opcode;
1810 }
1811 rl_src = LoadValue(rl_src, kCoreReg);
1812 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001813 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 if (shift_op && (lit == 0)) {
1815 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
1816 } else {
1817 OpRegRegImm(op, rl_result.low_reg, rl_src.low_reg, lit);
1818 }
1819 StoreValue(rl_dest, rl_result);
1820}
1821
1822void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001823 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001824 RegLocation rl_result;
1825 OpKind first_op = kOpBkpt;
1826 OpKind second_op = kOpBkpt;
1827 bool call_out = false;
1828 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001829 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 int ret_reg = TargetReg(kRet0);
1831
1832 switch (opcode) {
1833 case Instruction::NOT_LONG:
1834 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1835 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1836 // Check for destructive overlap
1837 if (rl_result.low_reg == rl_src2.high_reg) {
1838 int t_reg = AllocTemp();
1839 OpRegCopy(t_reg, rl_src2.high_reg);
1840 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1841 OpRegReg(kOpMvn, rl_result.high_reg, t_reg);
1842 FreeTemp(t_reg);
1843 } else {
1844 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1845 OpRegReg(kOpMvn, rl_result.high_reg, rl_src2.high_reg);
1846 }
1847 StoreValueWide(rl_dest, rl_result);
1848 return;
1849 case Instruction::ADD_LONG:
1850 case Instruction::ADD_LONG_2ADDR:
1851 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001852 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001853 return;
1854 }
1855 first_op = kOpAdd;
1856 second_op = kOpAdc;
1857 break;
1858 case Instruction::SUB_LONG:
1859 case Instruction::SUB_LONG_2ADDR:
1860 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001861 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001862 return;
1863 }
1864 first_op = kOpSub;
1865 second_op = kOpSbc;
1866 break;
1867 case Instruction::MUL_LONG:
1868 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001869 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001870 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001871 return;
1872 } else {
1873 call_out = true;
1874 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001875 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 }
1877 break;
1878 case Instruction::DIV_LONG:
1879 case Instruction::DIV_LONG_2ADDR:
1880 call_out = true;
1881 check_zero = true;
1882 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001883 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001884 break;
1885 case Instruction::REM_LONG:
1886 case Instruction::REM_LONG_2ADDR:
1887 call_out = true;
1888 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001889 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001890 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1891 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1892 break;
1893 case Instruction::AND_LONG_2ADDR:
1894 case Instruction::AND_LONG:
1895 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001896 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 }
1898 first_op = kOpAnd;
1899 second_op = kOpAnd;
1900 break;
1901 case Instruction::OR_LONG:
1902 case Instruction::OR_LONG_2ADDR:
1903 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001904 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001905 return;
1906 }
1907 first_op = kOpOr;
1908 second_op = kOpOr;
1909 break;
1910 case Instruction::XOR_LONG:
1911 case Instruction::XOR_LONG_2ADDR:
1912 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001913 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 return;
1915 }
1916 first_op = kOpXor;
1917 second_op = kOpXor;
1918 break;
1919 case Instruction::NEG_LONG: {
1920 GenNegLong(rl_dest, rl_src2);
1921 return;
1922 }
1923 default:
1924 LOG(FATAL) << "Invalid long arith op";
1925 }
1926 if (!call_out) {
1927 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1928 } else {
1929 FlushAllRegs(); /* Send everything to home location */
1930 if (check_zero) {
1931 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1932 int r_tgt = CallHelperSetup(func_offset);
1933 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1934 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1935 // NOTE: callout here is not a safepoint
1936 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1937 } else {
1938 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1939 }
1940 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1941 if (ret_reg == TargetReg(kRet0))
1942 rl_result = GetReturnWide(false);
1943 else
1944 rl_result = GetReturnWideAlt();
1945 StoreValueWide(rl_dest, rl_result);
1946 }
1947}
1948
Ian Rogers848871b2013-08-05 10:56:33 -07001949void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001950 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 /*
1952 * Don't optimize the register usage since it calls out to support
1953 * functions
1954 */
1955 FlushAllRegs(); /* Send everything to home location */
1956 if (rl_src.wide) {
1957 LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1958 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1959 } else {
1960 LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1961 }
1962 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1963 if (rl_dest.wide) {
1964 RegLocation rl_result;
1965 rl_result = GetReturnWide(rl_dest.fp);
1966 StoreValueWide(rl_dest, rl_result);
1967 } else {
1968 RegLocation rl_result;
1969 rl_result = GetReturn(rl_dest.fp);
1970 StoreValue(rl_dest, rl_result);
1971 }
1972}
1973
1974/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001975void Mir2Lir::GenSuspendTest(int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1977 return;
1978 }
1979 FlushAllRegs();
1980 LIR* branch = OpTestSuspend(NULL);
1981 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001982 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1983 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 branch->target = target;
1985 suspend_launchpads_.Insert(target);
1986}
1987
1988/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001989void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1991 OpUnconditionalBranch(target);
1992 return;
1993 }
1994 OpTestSuspend(target);
1995 LIR* launch_pad =
buzbee0d829482013-10-11 15:24:55 -07001996 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1997 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 FlushAllRegs();
1999 OpUnconditionalBranch(launch_pad);
2000 suspend_launchpads_.Insert(launch_pad);
2001}
2002
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002003/* Call out to helper assembly routine that will null check obj and then lock it. */
2004void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2005 FlushAllRegs();
2006 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
2007}
2008
2009/* Call out to helper assembly routine that will null check obj and then unlock it. */
2010void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2011 FlushAllRegs();
2012 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2013}
2014
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002015/* Generic code for generating a wide constant into a VR. */
2016void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2017 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2018 LoadConstantWide(rl_result.low_reg, rl_result.high_reg, value);
2019 StoreValueWide(rl_dest, rl_result);
2020}
2021
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022} // namespace art