blob: 7af9d5775a2184142561f9e72da6ad41fda2fa8d [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom7940e442013-07-12 13:46:57 -070016#include "dex/compiler_ir.h"
17#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070018#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
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
buzbee2700f7e2014-03-07 09:46:20 -080056LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
57 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 LIR* branch;
59 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080060 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
61 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 branch = OpUnconditionalBranch(tgt);
63 } else {
buzbee2700f7e2014-03-07 09:46:20 -080064 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
66 }
67 // Remember branch target - will process later
68 throw_launchpads_.Insert(tgt);
69 return branch;
70}
71
Dave Allisonb373e092014-02-20 16:06:36 -080072
Brian Carlstrom7940e442013-07-12 13:46:57 -070073/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -080074LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -080075 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -070076 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 }
Dave Allisonb373e092014-02-20 16:06:36 -080078 return nullptr;
79}
80
Dave Allisonf9439142014-03-27 15:10:22 -070081/* Perform an explicit null-check on a register. */
82LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
83 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
84 return NULL;
85 }
86 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
87}
88
Dave Allisonb373e092014-02-20 16:06:36 -080089void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
90 if (!Runtime::Current()->ExplicitNullChecks()) {
91 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
92 return;
93 }
94 MarkSafepointPC(last_lir_insn_);
95 }
96}
97
98void Mir2Lir::MarkPossibleStackOverflowException() {
99 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
100 MarkSafepointPC(last_lir_insn_);
101 }
102}
103
buzbee2700f7e2014-03-07 09:46:20 -0800104void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800105 if (!Runtime::Current()->ExplicitNullChecks()) {
106 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
107 return;
108 }
109 // Force an implicit null check by performing a memory operation (load) from the given
110 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800111 RegStorage tmp = AllocTemp();
112 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800113 LIR* load = LoadWordDisp(reg, 0, tmp);
114 FreeTemp(tmp);
115 MarkSafepointPC(load);
116 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117}
118
119/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800120LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700121 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800122 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
123 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
125 // Remember branch target - will process later
126 throw_launchpads_.Insert(tgt);
127 return branch;
128}
129
130void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
131 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700132 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 ConditionCode cond;
134 switch (opcode) {
135 case Instruction::IF_EQ:
136 cond = kCondEq;
137 break;
138 case Instruction::IF_NE:
139 cond = kCondNe;
140 break;
141 case Instruction::IF_LT:
142 cond = kCondLt;
143 break;
144 case Instruction::IF_GE:
145 cond = kCondGe;
146 break;
147 case Instruction::IF_GT:
148 cond = kCondGt;
149 break;
150 case Instruction::IF_LE:
151 cond = kCondLe;
152 break;
153 default:
154 cond = static_cast<ConditionCode>(0);
155 LOG(FATAL) << "Unexpected opcode " << opcode;
156 }
157
158 // Normalize such that if either operand is constant, src2 will be constant
159 if (rl_src1.is_const) {
160 RegLocation rl_temp = rl_src1;
161 rl_src1 = rl_src2;
162 rl_src2 = rl_temp;
163 cond = FlipComparisonOrder(cond);
164 }
165
166 rl_src1 = LoadValue(rl_src1, kCoreReg);
167 // Is this really an immediate comparison?
168 if (rl_src2.is_const) {
169 // If it's already live in a register or not easily materialized, just keep going
170 RegLocation rl_temp = UpdateLoc(rl_src2);
171 if ((rl_temp.location == kLocDalvikFrame) &&
172 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
173 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800174 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 return;
176 }
177 }
178 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800179 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180}
181
182void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700183 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 ConditionCode cond;
185 rl_src = LoadValue(rl_src, kCoreReg);
186 switch (opcode) {
187 case Instruction::IF_EQZ:
188 cond = kCondEq;
189 break;
190 case Instruction::IF_NEZ:
191 cond = kCondNe;
192 break;
193 case Instruction::IF_LTZ:
194 cond = kCondLt;
195 break;
196 case Instruction::IF_GEZ:
197 cond = kCondGe;
198 break;
199 case Instruction::IF_GTZ:
200 cond = kCondGt;
201 break;
202 case Instruction::IF_LEZ:
203 cond = kCondLe;
204 break;
205 default:
206 cond = static_cast<ConditionCode>(0);
207 LOG(FATAL) << "Unexpected opcode " << opcode;
208 }
buzbee2700f7e2014-03-07 09:46:20 -0800209 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210}
211
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700212void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
214 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800215 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800217 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 }
buzbee2700f7e2014-03-07 09:46:20 -0800219 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 StoreValueWide(rl_dest, rl_result);
221}
222
223void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700224 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700225 rl_src = LoadValue(rl_src, kCoreReg);
226 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
227 OpKind op = kOpInvalid;
228 switch (opcode) {
229 case Instruction::INT_TO_BYTE:
230 op = kOp2Byte;
231 break;
232 case Instruction::INT_TO_SHORT:
233 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700235 case Instruction::INT_TO_CHAR:
236 op = kOp2Char;
237 break;
238 default:
239 LOG(ERROR) << "Bad int conversion type";
240 }
buzbee2700f7e2014-03-07 09:46:20 -0800241 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700242 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243}
244
245/*
246 * Let helper function take care of everything. Will call
247 * Array::AllocFromCode(type_idx, method, count);
248 * Note: AllocFromCode will handle checks for errNegativeArraySize.
249 */
250void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700251 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700253 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800254 const DexFile* dex_file = cu_->dex_file;
255 CompilerDriver* driver = cu_->compiler_driver;
256 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800258 bool is_type_initialized; // Ignored as an array does not have an initializer.
259 bool use_direct_type_ptr;
260 uintptr_t direct_type_ptr;
261 if (kEmbedClassInCode &&
262 driver->CanEmbedTypeInCode(*dex_file, type_idx,
263 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
264 // The fast path.
265 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800266 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700267 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800268 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
269 } else {
270 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700271 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800272 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
273 }
274 } else {
275 // The slow path.
276 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700277 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800278 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
279 }
280 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700282 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800283 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 RegLocation rl_result = GetReturn(false);
286 StoreValue(rl_dest, rl_result);
287}
288
289/*
290 * Similar to GenNewArray, but with post-allocation initialization.
291 * Verifier guarantees we're dealing with an array class. Current
292 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
293 * Current code also throws internal unimp if not 'L', '[' or 'I'.
294 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700295void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 int elems = info->num_arg_words;
297 int type_idx = info->index;
298 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700299 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
301 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700302 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700304 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 }
306 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
307 FreeTemp(TargetReg(kArg2));
308 FreeTemp(TargetReg(kArg1));
309 /*
310 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
311 * return region. Because AllocFromCode placed the new array
312 * in kRet0, we'll just lock it into place. When debugger support is
313 * added, it may be necessary to additionally copy all return
314 * values to a home location in thread-local storage
315 */
316 LockTemp(TargetReg(kRet0));
317
318 // TODO: use the correct component size, currently all supported types
319 // share array alignment with ints (see comment at head of function)
320 size_t component_size = sizeof(int32_t);
321
322 // Having a range of 0 is legal
323 if (info->is_range && (elems > 0)) {
324 /*
325 * Bit of ugliness here. We're going generate a mem copy loop
326 * on the register range, but it is possible that some regs
327 * in the range have been promoted. This is unlikely, but
328 * before generating the copy, we'll just force a flush
329 * of any regs in the source range that have been promoted to
330 * home location.
331 */
332 for (int i = 0; i < elems; i++) {
333 RegLocation loc = UpdateLoc(info->args[i]);
334 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800335 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 }
337 }
338 /*
339 * TUNING note: generated code here could be much improved, but
340 * this is an uncommon operation and isn't especially performance
341 * critical.
342 */
buzbee2700f7e2014-03-07 09:46:20 -0800343 RegStorage r_src = AllocTemp();
344 RegStorage r_dst = AllocTemp();
345 RegStorage r_idx = AllocTemp();
346 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700347 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 case kThumb2:
349 r_val = TargetReg(kLr);
350 break;
351 case kX86:
352 FreeTemp(TargetReg(kRet0));
353 r_val = AllocTemp();
354 break;
355 case kMips:
356 r_val = AllocTemp();
357 break;
358 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
359 }
360 // Set up source pointer
361 RegLocation rl_first = info->args[0];
362 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
363 // Set up the target pointer
364 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
365 mirror::Array::DataOffset(component_size).Int32Value());
366 // Set up the loop counter (known to be > 0)
367 LoadConstant(r_idx, elems - 1);
368 // Generate the copy loop. Going backwards for convenience
369 LIR* target = NewLIR0(kPseudoTargetLabel);
370 // Copy next element
371 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
372 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
373 FreeTemp(r_val);
374 OpDecAndBranch(kCondGe, r_idx, target);
375 if (cu_->instruction_set == kX86) {
376 // Restore the target pointer
377 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
378 -mirror::Array::DataOffset(component_size).Int32Value());
379 }
380 } else if (!info->is_range) {
381 // TUNING: interleave
382 for (int i = 0; i < elems; i++) {
383 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
384 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800385 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
386 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800388 if (IsTemp(rl_arg.reg)) {
389 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 }
391 }
392 }
393 if (info->result.location != kLocInvalid) {
394 StoreValue(info->result, GetReturn(false /* not fp */));
395 }
396}
397
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800398//
399// Slow path to ensure a class is initialized for sget/sput.
400//
401class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
402 public:
buzbee2700f7e2014-03-07 09:46:20 -0800403 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
404 RegStorage r_base) :
405 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
406 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800407 }
408
409 void Compile() {
410 LIR* unresolved_target = GenerateTargetLabel();
411 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700412 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800413 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800414 // Copy helper's result into r_base, a no-op on all but MIPS.
415 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
416
417 m2l_->OpUnconditionalBranch(cont_);
418 }
419
420 private:
421 LIR* const uninit_;
422 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800423 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800424};
425
Vladimir Markobe0e5462014-02-26 11:24:15 +0000426void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700427 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000428 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
429 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
430 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
431 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800432 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000433 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 // Fast path, static storage base is this method's class
435 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800436 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800437 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
438 if (IsTemp(rl_method.reg)) {
439 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 }
441 } else {
442 // Medium path, static storage base in a different class which requires checks that the other
443 // class is initialized.
444 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000445 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 // May do runtime call so everything to home locations.
447 FlushAllRegs();
448 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800449 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 LockTemp(r_method);
451 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800452 r_base = TargetReg(kArg0);
453 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800454 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800455 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000456 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800457 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000458 if (!field_info.IsInitialized() &&
459 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800460 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800461
462 // The slow path is invoked if the r_base is NULL or the class pointed
463 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800464 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800465 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800466 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800467 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800468 mirror::Class::StatusOffset().Int32Value(),
469 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800470 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800471
buzbee2700f7e2014-03-07 09:46:20 -0800472 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000473 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800474
475 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 FreeTemp(r_method);
478 }
479 // rBase now holds static storage base
480 if (is_long_or_double) {
481 rl_src = LoadValueWide(rl_src, kAnyReg);
482 } else {
483 rl_src = LoadValue(rl_src, kAnyReg);
484 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000485 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800486 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 GenMemBarrier(kStoreStore);
488 }
489 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800490 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800492 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000494 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800495 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 GenMemBarrier(kStoreLoad);
497 }
498 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800499 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800501 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 } else {
503 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700504 ThreadOffset<4> setter_offset =
505 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
506 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
507 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000508 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 }
510}
511
Vladimir Markobe0e5462014-02-26 11:24:15 +0000512void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700513 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000514 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
515 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
516 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
517 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800518 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000519 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 // Fast path, static storage base is this method's class
521 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800522 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800523 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 } else {
525 // Medium path, static storage base in a different class which requires checks that the other
526 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000527 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 // May do runtime call so everything to home locations.
529 FlushAllRegs();
530 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800531 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 LockTemp(r_method);
533 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800534 r_base = TargetReg(kArg0);
535 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800536 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800537 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000538 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800539 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000540 if (!field_info.IsInitialized() &&
541 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800542 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800543
544 // The slow path is invoked if the r_base is NULL or the class pointed
545 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800546 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800547 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800548 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800549 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800550 mirror::Class::StatusOffset().Int32Value(),
551 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800552 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800553
buzbee2700f7e2014-03-07 09:46:20 -0800554 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000555 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800556
557 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 FreeTemp(r_method);
560 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800563
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800565 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800567 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800570
571 if (field_info.IsVolatile()) {
572 // Without context sensitive analysis, we must issue the most conservative barriers.
573 // In this case, either a load or store may follow so we issue both barriers.
574 GenMemBarrier(kLoadLoad);
575 GenMemBarrier(kLoadStore);
576 }
577
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 if (is_long_or_double) {
579 StoreValueWide(rl_dest, rl_result);
580 } else {
581 StoreValue(rl_dest, rl_result);
582 }
583 } else {
584 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700585 ThreadOffset<4> getterOffset =
586 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
587 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
588 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000589 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 if (is_long_or_double) {
591 RegLocation rl_result = GetReturnWide(rl_dest.fp);
592 StoreValueWide(rl_dest, rl_result);
593 } else {
594 RegLocation rl_result = GetReturn(rl_dest.fp);
595 StoreValue(rl_dest, rl_result);
596 }
597 }
598}
599
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600// Generate code for all slow paths.
601void Mir2Lir::HandleSlowPaths() {
602 int n = slow_paths_.Size();
603 for (int i = 0; i < n; ++i) {
604 LIRSlowPath* slowpath = slow_paths_.Get(i);
605 slowpath->Compile();
606 }
607 slow_paths_.Reset();
608}
609
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700610void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700612 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 for (int i = 0; i < num_elems; i++) {
614 ResetRegPool();
615 ResetDefTracking();
616 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700617 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 current_dalvik_offset_ = lab->operands[1];
619 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800620 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
622 OpUnconditionalBranch(resume_lab);
623 }
624}
625
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700626void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 int num_elems = throw_launchpads_.Size();
628 for (int i = 0; i < num_elems; i++) {
629 ResetRegPool();
630 ResetDefTracking();
631 LIR* lab = throw_launchpads_.Get(i);
632 current_dalvik_offset_ = lab->operands[1];
633 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700634 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 int v1 = lab->operands[2];
636 int v2 = lab->operands[3];
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700637 const bool target_x86 = cu_->instruction_set == kX86;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 switch (lab->operands[0]) {
639 case kThrowNullPointer:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700640 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700642 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
644 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800645 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
646 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800648 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
650 // Make sure the following LoadConstant doesn't mess with kArg1.
651 LockTemp(TargetReg(kArg1));
652 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700653 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 break;
655 case kThrowArrayBounds:
656 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800657 if (v2 != TargetReg(kArg0).GetReg()) {
658 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 if (target_x86) {
660 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800661 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
662 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800664 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 }
666 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800667 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800669 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 if (target_x86) {
671 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800672 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
673 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800675 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 }
677 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
678 } else {
679 if (target_x86) {
680 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800681 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
682 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800684 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 }
buzbee2700f7e2014-03-07 09:46:20 -0800686 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 }
688 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700689 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 break;
691 case kThrowDivZero:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700692 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 break;
694 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800695 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700697 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 default:
700 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
701 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000702 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800703 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700704 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
706}
707
Vladimir Markobe0e5462014-02-26 11:24:15 +0000708void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700710 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000711 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
712 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
713 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 RegLocation rl_result;
715 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000716 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 rl_obj = LoadValue(rl_obj, kCoreReg);
718 if (is_long_or_double) {
719 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800720 GenNullCheck(rl_obj.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 if (cu_->instruction_set == kX86) {
722 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800723 // FIXME? duplicate null check?
724 GenNullCheck(rl_obj.reg, opt_flags);
725 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
726 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800727 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800729 // Without context sensitive analysis, we must issue the most conservative barriers.
730 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800732 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 }
734 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800735 RegStorage reg_ptr = AllocTemp();
736 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800738 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700739 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000740 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800741 // Without context sensitive analysis, we must issue the most conservative barriers.
742 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800744 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 }
746 FreeTemp(reg_ptr);
747 }
748 StoreValueWide(rl_dest, rl_result);
749 } else {
750 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800751 GenNullCheck(rl_obj.reg, opt_flags);
752 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
753 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800754 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000755 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800756 // Without context sensitive analysis, we must issue the most conservative barriers.
757 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800759 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 }
761 StoreValue(rl_dest, rl_result);
762 }
763 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700764 ThreadOffset<4> getterOffset =
765 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
766 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
767 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 if (is_long_or_double) {
770 RegLocation rl_result = GetReturnWide(rl_dest.fp);
771 StoreValueWide(rl_dest, rl_result);
772 } else {
773 RegLocation rl_result = GetReturn(rl_dest.fp);
774 StoreValue(rl_dest, rl_result);
775 }
776 }
777}
778
Vladimir Markobe0e5462014-02-26 11:24:15 +0000779void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700781 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000782 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
783 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
784 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000786 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 rl_obj = LoadValue(rl_obj, kCoreReg);
788 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800790 GenNullCheck(rl_obj.reg, opt_flags);
791 RegStorage reg_ptr = AllocTemp();
792 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000793 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800794 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 GenMemBarrier(kStoreStore);
796 }
buzbee2700f7e2014-03-07 09:46:20 -0800797 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800798 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000799 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800800 // A load might follow the volatile store so insert a StoreLoad barrier.
801 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 }
803 FreeTemp(reg_ptr);
804 } else {
805 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800806 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000807 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800808 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 GenMemBarrier(kStoreStore);
810 }
buzbee2700f7e2014-03-07 09:46:20 -0800811 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800812 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000813 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800814 // A load might follow the volatile store so insert a StoreLoad barrier.
815 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 }
817 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800818 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 }
820 }
821 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700822 ThreadOffset<4> setter_offset =
823 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
824 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
825 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000826 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
827 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 }
829}
830
Ian Rogersa9a82542013-10-04 11:17:26 -0700831void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
832 RegLocation rl_src) {
833 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
834 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
835 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700836 ThreadOffset<4> helper = needs_range_check
837 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
838 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
839 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700840 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
841}
842
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700843void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800845 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
847 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
848 *cu_->dex_file,
849 type_idx)) {
850 // Call out to helper which resolves type and verifies access.
851 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700852 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800853 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 RegLocation rl_result = GetReturn(false);
855 StoreValue(rl_dest, rl_result);
856 } else {
857 // We're don't need access checks, load type from dex cache
858 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700859 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800860 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 int32_t offset_of_type =
862 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
863 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800864 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
866 type_idx) || SLOW_TYPE_PATH) {
867 // Slow path, at runtime test if type is null and if so initialize
868 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800869 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800870 LIR* cont = NewLIR0(kPseudoTargetLabel);
871
872 // Object to generate the slow path for class resolution.
873 class SlowPath : public LIRSlowPath {
874 public:
875 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
876 const RegLocation& rl_method, const RegLocation& rl_result) :
877 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
878 rl_method_(rl_method), rl_result_(rl_result) {
879 }
880
881 void Compile() {
882 GenerateTargetLabel();
883
Ian Rogersdd7624d2014-03-14 17:43:00 -0700884 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800885 rl_method_.reg, true);
886 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800887
888 m2l_->OpUnconditionalBranch(cont_);
889 }
890
891 private:
892 const int type_idx_;
893 const RegLocation rl_method_;
894 const RegLocation rl_result_;
895 };
896
897 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800898 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800899
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800901 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 // Fast path, we're done - just store result
903 StoreValue(rl_dest, rl_result);
904 }
905 }
906}
907
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700908void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 /* NOTE: Most strings should be available at compile time */
910 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
911 (sizeof(mirror::String*) * string_idx);
912 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
913 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
914 // slow path, resolve string if not in dex cache
915 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700916 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800917
918 // If the Method* is already in a register, we can save a copy.
919 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800920 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800921 if (rl_method.location == kLocPhysReg) {
922 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800923 DCHECK(!IsTemp(rl_method.reg));
924 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800925 } else {
926 r_method = TargetReg(kArg2);
927 LoadCurrMethodDirect(r_method);
928 }
929 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
930 TargetReg(kArg0));
931
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800934 if (cu_->instruction_set == kThumb2 ||
935 cu_->instruction_set == kMips) {
936 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800937 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800938 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
939 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800941
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800942 // Object to generate the slow path for string resolution.
943 class SlowPath : public LIRSlowPath {
944 public:
buzbee2700f7e2014-03-07 09:46:20 -0800945 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800946 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
947 }
948
949 void Compile() {
950 GenerateTargetLabel();
951
Ian Rogersdd7624d2014-03-14 17:43:00 -0700952 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800953
954 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
955 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
956 m2l_->MarkSafepointPC(call_inst);
957 m2l_->FreeTemp(r_tgt);
958
959 m2l_->OpUnconditionalBranch(cont_);
960 }
961
962 private:
buzbee2700f7e2014-03-07 09:46:20 -0800963 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800964 };
965
966 // Add to list for future.
967 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 } else {
969 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800970 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
971 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700972 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800973 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800974 LIR* target = NewLIR0(kPseudoTargetLabel);
975 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 }
977 GenBarrier();
978 StoreValue(rl_dest, GetReturn(false));
979 } else {
980 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800981 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800983 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
984 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 StoreValue(rl_dest, rl_result);
986 }
987}
988
989/*
990 * Let helper function take care of everything. Will
991 * call Class::NewInstanceFromCode(type_idx, method);
992 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700993void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 FlushAllRegs(); /* Everything to home location */
995 // alloc will always check for resolution, do we also need to verify
996 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700997 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800998 const DexFile* dex_file = cu_->dex_file;
999 CompilerDriver* driver = cu_->compiler_driver;
1000 if (driver->CanAccessInstantiableTypeWithoutChecks(
1001 cu_->method_idx, *dex_file, type_idx)) {
1002 bool is_type_initialized;
1003 bool use_direct_type_ptr;
1004 uintptr_t direct_type_ptr;
1005 if (kEmbedClassInCode &&
1006 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1007 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1008 // The fast path.
1009 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001010 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001011 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001012 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001013 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1014 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001015 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001016 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1017 }
1018 } else {
1019 // Use the direct pointer.
1020 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001021 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001022 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1023 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001024 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001025 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1026 }
1027 }
1028 } else {
1029 // The slow path.
1030 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001031 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001032 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1033 }
1034 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001036 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001037 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 RegLocation rl_result = GetReturn(false);
1040 StoreValue(rl_dest, rl_result);
1041}
1042
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001043void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001045 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046}
1047
1048// For final classes there are no sub-classes to check and so we can answer the instance-of
1049// question with simple comparisons.
1050void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1051 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001052 // X86 has its own implementation.
1053 DCHECK_NE(cu_->instruction_set, kX86);
1054
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 RegLocation object = LoadValue(rl_src, kCoreReg);
1056 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001057 RegStorage result_reg = rl_result.reg;
1058 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 result_reg = AllocTypedTemp(false, kCoreReg);
1060 }
1061 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001062 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063
buzbee2700f7e2014-03-07 09:46:20 -08001064 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1065 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066
1067 LoadCurrMethodDirect(check_class);
1068 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001069 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1070 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001072 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001074 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 int32_t offset_of_type =
1076 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1077 (sizeof(mirror::Class*) * type_idx);
1078 LoadWordDisp(check_class, offset_of_type, check_class);
1079 }
1080
1081 LIR* ne_branchover = NULL;
1082 if (cu_->instruction_set == kThumb2) {
1083 OpRegReg(kOpCmp, check_class, object_class); // Same?
1084 OpIT(kCondEq, ""); // if-convert the test
1085 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison43a065c2014-04-01 15:14:46 -07001086 GenBarrier();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 } else {
1088 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1089 LoadConstant(result_reg, 1); // eq case - load true
1090 }
1091 LIR* target = NewLIR0(kPseudoTargetLabel);
1092 null_branchover->target = target;
1093 if (ne_branchover != NULL) {
1094 ne_branchover->target = target;
1095 }
1096 FreeTemp(object_class);
1097 FreeTemp(check_class);
1098 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001099 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 FreeTemp(result_reg);
1101 }
1102 StoreValue(rl_dest, rl_result);
1103}
1104
1105void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1106 bool type_known_abstract, bool use_declaring_class,
1107 bool can_assume_type_is_in_dex_cache,
1108 uint32_t type_idx, RegLocation rl_dest,
1109 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001110 // X86 has its own implementation.
1111 DCHECK_NE(cu_->instruction_set, kX86);
1112
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 FlushAllRegs();
1114 // May generate a call - use explicit registers
1115 LockCallTemps();
1116 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001117 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 if (needs_access_check) {
1119 // Check we have access to type_idx and if not throw IllegalAccessError,
1120 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001121 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 type_idx, true);
1123 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1124 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1125 } else if (use_declaring_class) {
1126 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001127 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1128 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 } else {
1130 // Load dex cache entry into class_reg (kArg2)
1131 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001132 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1133 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 int32_t offset_of_type =
1135 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1136 * type_idx);
1137 LoadWordDisp(class_reg, offset_of_type, class_reg);
1138 if (!can_assume_type_is_in_dex_cache) {
1139 // Need to test presence of type in dex cache at runtime
1140 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1141 // Not resolved
1142 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001143 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001144 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1146 // Rejoin code paths
1147 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1148 hop_branch->target = hop_target;
1149 }
1150 }
1151 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1152 RegLocation rl_result = GetReturn(false);
1153 if (cu_->instruction_set == kMips) {
1154 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001155 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1158
1159 /* load object->klass_ */
1160 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1161 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1162 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1163 LIR* branchover = NULL;
1164 if (type_known_final) {
1165 // rl_result == ref == null == 0.
1166 if (cu_->instruction_set == kThumb2) {
1167 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1168 OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001169 LoadConstant(rl_result.reg, 1); // .eq case - load true
1170 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison43a065c2014-04-01 15:14:46 -07001171 GenBarrier();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001173 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001175 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 }
1177 } else {
1178 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001179 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 if (!type_known_abstract) {
1181 /* Uses conditional nullification */
1182 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1183 OpIT(kCondEq, "EE"); // if-convert the test
1184 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1185 }
1186 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1187 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison43a065c2014-04-01 15:14:46 -07001188 GenBarrier();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 FreeTemp(r_tgt);
1190 } else {
1191 if (!type_known_abstract) {
1192 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001193 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1195 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001196 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001197 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1198 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1199 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 }
1201 }
1202 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001203 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 /* branch targets here */
1205 LIR* target = NewLIR0(kPseudoTargetLabel);
1206 StoreValue(rl_dest, rl_result);
1207 branch1->target = target;
1208 if (branchover != NULL) {
1209 branchover->target = target;
1210 }
1211}
1212
1213void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1214 bool type_known_final, type_known_abstract, use_declaring_class;
1215 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1216 *cu_->dex_file,
1217 type_idx,
1218 &type_known_final,
1219 &type_known_abstract,
1220 &use_declaring_class);
1221 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1222 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1223
1224 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1225 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1226 } else {
1227 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1228 use_declaring_class, can_assume_type_is_in_dex_cache,
1229 type_idx, rl_dest, rl_src);
1230 }
1231}
1232
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001233void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 bool type_known_final, type_known_abstract, use_declaring_class;
1235 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1236 *cu_->dex_file,
1237 type_idx,
1238 &type_known_final,
1239 &type_known_abstract,
1240 &use_declaring_class);
1241 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1242 // of the exception throw path.
1243 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001244 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 // Verifier type analysis proved this check cast would never cause an exception.
1246 return;
1247 }
1248 FlushAllRegs();
1249 // May generate a call - use explicit registers
1250 LockCallTemps();
1251 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001252 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 if (needs_access_check) {
1254 // Check we have access to type_idx and if not throw IllegalAccessError,
1255 // returns Class* in kRet0
1256 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001257 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 type_idx, TargetReg(kArg1), true);
1259 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1260 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001261 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1262 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 } else {
1264 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001265 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1266 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 int32_t offset_of_type =
1268 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1269 (sizeof(mirror::Class*) * type_idx);
1270 LoadWordDisp(class_reg, offset_of_type, class_reg);
1271 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1272 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1274 LIR* cont = NewLIR0(kPseudoTargetLabel);
1275
1276 // Slow path to initialize the type. Executed if the type is NULL.
1277 class SlowPath : public LIRSlowPath {
1278 public:
1279 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001280 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001281 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1282 class_reg_(class_reg) {
1283 }
1284
1285 void Compile() {
1286 GenerateTargetLabel();
1287
1288 // Call out to helper, which will return resolved type in kArg0
1289 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001290 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001291 m2l_->TargetReg(kArg1), true);
1292 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1293 m2l_->OpUnconditionalBranch(cont_);
1294 }
1295 public:
1296 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001297 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001298 };
1299
buzbee2700f7e2014-03-07 09:46:20 -08001300 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 }
1302 }
1303 // At this point, class_reg (kArg2) has class
1304 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001305
1306 // Slow path for the case where the classes are not equal. In this case we need
1307 // to call a helper function to do the check.
1308 class SlowPath : public LIRSlowPath {
1309 public:
1310 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1311 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1312 }
1313
1314 void Compile() {
1315 GenerateTargetLabel();
1316
1317 if (load_) {
1318 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1319 m2l_->TargetReg(kArg1));
1320 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001321 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001322 m2l_->TargetReg(kArg1), true);
1323
1324 m2l_->OpUnconditionalBranch(cont_);
1325 }
1326
1327 private:
1328 bool load_;
1329 };
1330
1331 if (type_known_abstract) {
1332 // Easier case, run slow path if target is non-null (slow path will load from target)
1333 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1334 LIR* cont = NewLIR0(kPseudoTargetLabel);
1335 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1336 } else {
1337 // Harder, more common case. We need to generate a forward branch over the load
1338 // if the target is null. If it's non-null we perform the load and branch to the
1339 // slow path if the classes are not equal.
1340
1341 /* Null is OK - continue */
1342 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1343 /* load object->klass_ */
1344 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001345 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001346
1347 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1348 LIR* cont = NewLIR0(kPseudoTargetLabel);
1349
1350 // Add the slow path that will not perform load since this is already done.
1351 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1352
1353 // Set the null check to branch to the continuation.
1354 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356}
1357
1358void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001359 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 RegLocation rl_result;
1361 if (cu_->instruction_set == kThumb2) {
1362 /*
1363 * NOTE: This is the one place in the code in which we might have
1364 * as many as six live temporary registers. There are 5 in the normal
1365 * set for Arm. Until we have spill capabilities, temporarily add
1366 * lr to the temp set. It is safe to do this locally, but note that
1367 * lr is used explicitly elsewhere in the code generator and cannot
1368 * normally be used as a general temp register.
1369 */
1370 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1371 FreeTemp(TargetReg(kLr)); // and make it available
1372 }
1373 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1374 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1375 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1376 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001377 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1378 RegStorage t_reg = AllocTemp();
1379 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1380 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1381 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 FreeTemp(t_reg);
1383 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001384 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1385 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 }
1387 /*
1388 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1389 * following StoreValueWide might need to allocate a temp register.
1390 * To further work around the lack of a spill capability, explicitly
1391 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1392 * Remove when spill is functional.
1393 */
1394 FreeRegLocTemps(rl_result, rl_src1);
1395 FreeRegLocTemps(rl_result, rl_src2);
1396 StoreValueWide(rl_dest, rl_result);
1397 if (cu_->instruction_set == kThumb2) {
1398 Clobber(TargetReg(kLr));
1399 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1400 }
1401}
1402
1403
1404void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001405 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001406 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407
1408 switch (opcode) {
1409 case Instruction::SHL_LONG:
1410 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001411 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 break;
1413 case Instruction::SHR_LONG:
1414 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001415 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 break;
1417 case Instruction::USHR_LONG:
1418 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001419 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 break;
1421 default:
1422 LOG(FATAL) << "Unexpected case";
1423 }
1424 FlushAllRegs(); /* Send everything to home location */
1425 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1426 RegLocation rl_result = GetReturnWide(false);
1427 StoreValueWide(rl_dest, rl_result);
1428}
1429
1430
1431void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001432 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001433 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 OpKind op = kOpBkpt;
1435 bool is_div_rem = false;
1436 bool check_zero = false;
1437 bool unary = false;
1438 RegLocation rl_result;
1439 bool shift_op = false;
1440 switch (opcode) {
1441 case Instruction::NEG_INT:
1442 op = kOpNeg;
1443 unary = true;
1444 break;
1445 case Instruction::NOT_INT:
1446 op = kOpMvn;
1447 unary = true;
1448 break;
1449 case Instruction::ADD_INT:
1450 case Instruction::ADD_INT_2ADDR:
1451 op = kOpAdd;
1452 break;
1453 case Instruction::SUB_INT:
1454 case Instruction::SUB_INT_2ADDR:
1455 op = kOpSub;
1456 break;
1457 case Instruction::MUL_INT:
1458 case Instruction::MUL_INT_2ADDR:
1459 op = kOpMul;
1460 break;
1461 case Instruction::DIV_INT:
1462 case Instruction::DIV_INT_2ADDR:
1463 check_zero = true;
1464 op = kOpDiv;
1465 is_div_rem = true;
1466 break;
1467 /* NOTE: returns in kArg1 */
1468 case Instruction::REM_INT:
1469 case Instruction::REM_INT_2ADDR:
1470 check_zero = true;
1471 op = kOpRem;
1472 is_div_rem = true;
1473 break;
1474 case Instruction::AND_INT:
1475 case Instruction::AND_INT_2ADDR:
1476 op = kOpAnd;
1477 break;
1478 case Instruction::OR_INT:
1479 case Instruction::OR_INT_2ADDR:
1480 op = kOpOr;
1481 break;
1482 case Instruction::XOR_INT:
1483 case Instruction::XOR_INT_2ADDR:
1484 op = kOpXor;
1485 break;
1486 case Instruction::SHL_INT:
1487 case Instruction::SHL_INT_2ADDR:
1488 shift_op = true;
1489 op = kOpLsl;
1490 break;
1491 case Instruction::SHR_INT:
1492 case Instruction::SHR_INT_2ADDR:
1493 shift_op = true;
1494 op = kOpAsr;
1495 break;
1496 case Instruction::USHR_INT:
1497 case Instruction::USHR_INT_2ADDR:
1498 shift_op = true;
1499 op = kOpLsr;
1500 break;
1501 default:
1502 LOG(FATAL) << "Invalid word arith op: " << opcode;
1503 }
1504 if (!is_div_rem) {
1505 if (unary) {
1506 rl_src1 = LoadValue(rl_src1, kCoreReg);
1507 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 } else {
1510 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001511 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001512 RegStorage t_reg = AllocTemp();
1513 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 rl_src1 = LoadValue(rl_src1, kCoreReg);
1515 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001516 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 FreeTemp(t_reg);
1518 } else {
1519 rl_src1 = LoadValue(rl_src1, kCoreReg);
1520 rl_src2 = LoadValue(rl_src2, kCoreReg);
1521 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001522 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 }
1524 }
1525 StoreValue(rl_dest, rl_result);
1526 } else {
Dave Allison70202782013-10-22 17:52:19 -07001527 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 -07001528 if (cu_->instruction_set == kMips) {
1529 rl_src1 = LoadValue(rl_src1, kCoreReg);
1530 rl_src2 = LoadValue(rl_src2, kCoreReg);
1531 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001532 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 }
buzbee2700f7e2014-03-07 09:46:20 -08001534 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001535 done = true;
1536 } else if (cu_->instruction_set == kThumb2) {
1537 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1538 // Use ARM SDIV instruction for division. For remainder we also need to
1539 // calculate using a MUL and subtract.
1540 rl_src1 = LoadValue(rl_src1, kCoreReg);
1541 rl_src2 = LoadValue(rl_src2, kCoreReg);
1542 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001543 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001544 }
buzbee2700f7e2014-03-07 09:46:20 -08001545 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001546 done = true;
1547 }
1548 }
1549
1550 // If we haven't already generated the code use the callout function.
1551 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001552 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 FlushAllRegs(); /* Send everything to home location */
1554 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001555 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1557 if (check_zero) {
1558 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1559 }
Dave Allison70202782013-10-22 17:52:19 -07001560 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001561 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 if (op == kOpDiv)
1563 rl_result = GetReturn(false);
1564 else
1565 rl_result = GetReturnAlt();
1566 }
1567 StoreValue(rl_dest, rl_result);
1568 }
1569}
1570
1571/*
1572 * The following are the first-level codegen routines that analyze the format
1573 * of each bytecode then either dispatch special purpose codegen routines
1574 * or produce corresponding Thumb instructions directly.
1575 */
1576
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001578static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 x &= x - 1;
1580 return (x & (x - 1)) == 0;
1581}
1582
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1584// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001585bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001586 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1588 return false;
1589 }
1590 // No divide instruction for Arm, so check for more special cases
1591 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001592 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 }
1594 int k = LowestSetBit(lit);
1595 if (k >= 30) {
1596 // Avoid special cases.
1597 return false;
1598 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 rl_src = LoadValue(rl_src, kCoreReg);
1600 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001601 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001602 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 if (lit == 2) {
1604 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001605 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1606 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1607 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001609 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001611 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1612 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 }
1614 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001615 RegStorage t_reg1 = AllocTemp();
1616 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001618 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1619 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001621 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001623 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001625 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 }
1629 }
1630 StoreValue(rl_dest, rl_result);
1631 return true;
1632}
1633
1634// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1635// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001636bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001637 if (lit < 0) {
1638 return false;
1639 }
1640 if (lit == 0) {
1641 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1642 LoadConstant(rl_result.reg, 0);
1643 StoreValue(rl_dest, rl_result);
1644 return true;
1645 }
1646 if (lit == 1) {
1647 rl_src = LoadValue(rl_src, kCoreReg);
1648 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1649 OpRegCopy(rl_result.reg, rl_src.reg);
1650 StoreValue(rl_dest, rl_result);
1651 return true;
1652 }
1653 // There is RegRegRegShift on Arm, so check for more special cases.
1654 // TODO: disabled, need to handle case of "dest == src" properly.
1655 if (false && cu_->instruction_set == kThumb2) {
1656 return EasyMultiply(rl_src, rl_dest, lit);
1657 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 // Can we simplify this multiplication?
1659 bool power_of_two = false;
1660 bool pop_count_le2 = false;
1661 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001662 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 power_of_two = true;
1664 } else if (IsPopCountLE2(lit)) {
1665 pop_count_le2 = true;
1666 } else if (IsPowerOfTwo(lit + 1)) {
1667 power_of_two_minus_one = true;
1668 } else {
1669 return false;
1670 }
1671 rl_src = LoadValue(rl_src, kCoreReg);
1672 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1673 if (power_of_two) {
1674 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 } else if (pop_count_le2) {
1677 // Shift and add and shift.
1678 int first_bit = LowestSetBit(lit);
1679 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1680 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1681 } else {
1682 // Reverse subtract: (src << (shift + 1)) - src.
1683 DCHECK(power_of_two_minus_one);
1684 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001685 RegStorage t_reg = AllocTemp();
1686 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1687 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 }
1689 StoreValue(rl_dest, rl_result);
1690 return true;
1691}
1692
1693void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001694 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 RegLocation rl_result;
1696 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1697 int shift_op = false;
1698 bool is_div = false;
1699
1700 switch (opcode) {
1701 case Instruction::RSUB_INT_LIT8:
1702 case Instruction::RSUB_INT: {
1703 rl_src = LoadValue(rl_src, kCoreReg);
1704 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1705 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001706 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001708 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1709 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 }
1711 StoreValue(rl_dest, rl_result);
1712 return;
1713 }
1714
1715 case Instruction::SUB_INT:
1716 case Instruction::SUB_INT_2ADDR:
1717 lit = -lit;
1718 // Intended fallthrough
1719 case Instruction::ADD_INT:
1720 case Instruction::ADD_INT_2ADDR:
1721 case Instruction::ADD_INT_LIT8:
1722 case Instruction::ADD_INT_LIT16:
1723 op = kOpAdd;
1724 break;
1725 case Instruction::MUL_INT:
1726 case Instruction::MUL_INT_2ADDR:
1727 case Instruction::MUL_INT_LIT8:
1728 case Instruction::MUL_INT_LIT16: {
1729 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1730 return;
1731 }
1732 op = kOpMul;
1733 break;
1734 }
1735 case Instruction::AND_INT:
1736 case Instruction::AND_INT_2ADDR:
1737 case Instruction::AND_INT_LIT8:
1738 case Instruction::AND_INT_LIT16:
1739 op = kOpAnd;
1740 break;
1741 case Instruction::OR_INT:
1742 case Instruction::OR_INT_2ADDR:
1743 case Instruction::OR_INT_LIT8:
1744 case Instruction::OR_INT_LIT16:
1745 op = kOpOr;
1746 break;
1747 case Instruction::XOR_INT:
1748 case Instruction::XOR_INT_2ADDR:
1749 case Instruction::XOR_INT_LIT8:
1750 case Instruction::XOR_INT_LIT16:
1751 op = kOpXor;
1752 break;
1753 case Instruction::SHL_INT_LIT8:
1754 case Instruction::SHL_INT:
1755 case Instruction::SHL_INT_2ADDR:
1756 lit &= 31;
1757 shift_op = true;
1758 op = kOpLsl;
1759 break;
1760 case Instruction::SHR_INT_LIT8:
1761 case Instruction::SHR_INT:
1762 case Instruction::SHR_INT_2ADDR:
1763 lit &= 31;
1764 shift_op = true;
1765 op = kOpAsr;
1766 break;
1767 case Instruction::USHR_INT_LIT8:
1768 case Instruction::USHR_INT:
1769 case Instruction::USHR_INT_2ADDR:
1770 lit &= 31;
1771 shift_op = true;
1772 op = kOpLsr;
1773 break;
1774
1775 case Instruction::DIV_INT:
1776 case Instruction::DIV_INT_2ADDR:
1777 case Instruction::DIV_INT_LIT8:
1778 case Instruction::DIV_INT_LIT16:
1779 case Instruction::REM_INT:
1780 case Instruction::REM_INT_2ADDR:
1781 case Instruction::REM_INT_LIT8:
1782 case Instruction::REM_INT_LIT16: {
1783 if (lit == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001784 GenImmedCheck(kCondAl, RegStorage::InvalidReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001785 return;
1786 }
buzbee11b63d12013-08-27 07:34:17 -07001787 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001789 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 (opcode == Instruction::DIV_INT_LIT16)) {
1791 is_div = true;
1792 } else {
1793 is_div = false;
1794 }
buzbee11b63d12013-08-27 07:34:17 -07001795 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1796 return;
1797 }
Dave Allison70202782013-10-22 17:52:19 -07001798
1799 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 if (cu_->instruction_set == kMips) {
1801 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001802 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001803 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001804 } else if (cu_->instruction_set == kX86) {
1805 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1806 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001807 } else if (cu_->instruction_set == kThumb2) {
1808 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1809 // Use ARM SDIV instruction for division. For remainder we also need to
1810 // calculate using a MUL and subtract.
1811 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001812 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001813 done = true;
1814 }
1815 }
1816
1817 if (!done) {
1818 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001819 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1820 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001821 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001822 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1823 if (is_div)
1824 rl_result = GetReturn(false);
1825 else
1826 rl_result = GetReturnAlt();
1827 }
1828 StoreValue(rl_dest, rl_result);
1829 return;
1830 }
1831 default:
1832 LOG(FATAL) << "Unexpected opcode " << opcode;
1833 }
1834 rl_src = LoadValue(rl_src, kCoreReg);
1835 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001836 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001838 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001840 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 }
1842 StoreValue(rl_dest, rl_result);
1843}
1844
1845void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001846 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 RegLocation rl_result;
1848 OpKind first_op = kOpBkpt;
1849 OpKind second_op = kOpBkpt;
1850 bool call_out = false;
1851 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001852 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001853 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001854
1855 switch (opcode) {
1856 case Instruction::NOT_LONG:
1857 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1858 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1859 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001860 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1861 RegStorage t_reg = AllocTemp();
1862 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1863 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1864 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001865 FreeTemp(t_reg);
1866 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001867 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1868 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001869 }
1870 StoreValueWide(rl_dest, rl_result);
1871 return;
1872 case Instruction::ADD_LONG:
1873 case Instruction::ADD_LONG_2ADDR:
1874 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001875 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 return;
1877 }
1878 first_op = kOpAdd;
1879 second_op = kOpAdc;
1880 break;
1881 case Instruction::SUB_LONG:
1882 case Instruction::SUB_LONG_2ADDR:
1883 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001884 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 return;
1886 }
1887 first_op = kOpSub;
1888 second_op = kOpSbc;
1889 break;
1890 case Instruction::MUL_LONG:
1891 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001892 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001893 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001894 return;
1895 } else {
1896 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001897 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001898 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 }
1900 break;
1901 case Instruction::DIV_LONG:
1902 case Instruction::DIV_LONG_2ADDR:
1903 call_out = true;
1904 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001905 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001906 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 break;
1908 case Instruction::REM_LONG:
1909 case Instruction::REM_LONG_2ADDR:
1910 call_out = true;
1911 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001912 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001913 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001914 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 break;
1916 case Instruction::AND_LONG_2ADDR:
1917 case Instruction::AND_LONG:
1918 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001919 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 }
1921 first_op = kOpAnd;
1922 second_op = kOpAnd;
1923 break;
1924 case Instruction::OR_LONG:
1925 case Instruction::OR_LONG_2ADDR:
1926 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001927 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 return;
1929 }
1930 first_op = kOpOr;
1931 second_op = kOpOr;
1932 break;
1933 case Instruction::XOR_LONG:
1934 case Instruction::XOR_LONG_2ADDR:
1935 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001936 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 return;
1938 }
1939 first_op = kOpXor;
1940 second_op = kOpXor;
1941 break;
1942 case Instruction::NEG_LONG: {
1943 GenNegLong(rl_dest, rl_src2);
1944 return;
1945 }
1946 default:
1947 LOG(FATAL) << "Invalid long arith op";
1948 }
1949 if (!call_out) {
1950 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1951 } else {
1952 FlushAllRegs(); /* Send everything to home location */
1953 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001954 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1955 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1956 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1957 RegStorage r_tgt = CallHelperSetup(func_offset);
1958 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1959 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 // NOTE: callout here is not a safepoint
1961 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1962 } else {
1963 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1964 }
1965 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001966 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 rl_result = GetReturnWide(false);
1968 else
1969 rl_result = GetReturnWideAlt();
1970 StoreValueWide(rl_dest, rl_result);
1971 }
1972}
1973
Ian Rogersdd7624d2014-03-14 17:43:00 -07001974void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001975 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 /*
1977 * Don't optimize the register usage since it calls out to support
1978 * functions
1979 */
1980 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1982 if (rl_dest.wide) {
1983 RegLocation rl_result;
1984 rl_result = GetReturnWide(rl_dest.fp);
1985 StoreValueWide(rl_dest, rl_result);
1986 } else {
1987 RegLocation rl_result;
1988 rl_result = GetReturn(rl_dest.fp);
1989 StoreValue(rl_dest, rl_result);
1990 }
1991}
1992
1993/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001994void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001995 if (Runtime::Current()->ExplicitSuspendChecks()) {
1996 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1997 return;
1998 }
1999 FlushAllRegs();
2000 LIR* branch = OpTestSuspend(NULL);
2001 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2002 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2003 current_dalvik_offset_);
2004 branch->target = target;
2005 suspend_launchpads_.Insert(target);
2006 } else {
2007 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2008 return;
2009 }
2010 FlushAllRegs(); // TODO: needed?
2011 LIR* inst = CheckSuspendUsingLoad();
2012 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014}
2015
2016/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002017void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002018 if (Runtime::Current()->ExplicitSuspendChecks()) {
2019 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2020 OpUnconditionalBranch(target);
2021 return;
2022 }
2023 OpTestSuspend(target);
2024 LIR* launch_pad =
2025 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2026 current_dalvik_offset_);
2027 FlushAllRegs();
2028 OpUnconditionalBranch(launch_pad);
2029 suspend_launchpads_.Insert(launch_pad);
2030 } else {
2031 // For the implicit suspend check, just perform the trigger
2032 // load and branch to the target.
2033 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2034 OpUnconditionalBranch(target);
2035 return;
2036 }
2037 FlushAllRegs();
2038 LIR* inst = CheckSuspendUsingLoad();
2039 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042}
2043
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002044/* Call out to helper assembly routine that will null check obj and then lock it. */
2045void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2046 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002047 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002048}
2049
2050/* Call out to helper assembly routine that will null check obj and then unlock it. */
2051void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2052 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002053 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002054}
2055
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002056/* Generic code for generating a wide constant into a VR. */
2057void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2058 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002059 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002060 StoreValueWide(rl_dest, rl_result);
2061}
2062
Brian Carlstrom7940e442013-07-12 13:46:57 -07002063} // namespace art