blob: a3fb4201db17449db0623fc37fa27a113b39a86c [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:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700352 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 FreeTemp(TargetReg(kRet0));
354 r_val = AllocTemp();
355 break;
356 case kMips:
357 r_val = AllocTemp();
358 break;
359 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
360 }
361 // Set up source pointer
362 RegLocation rl_first = info->args[0];
363 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
364 // Set up the target pointer
365 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
366 mirror::Array::DataOffset(component_size).Int32Value());
367 // Set up the loop counter (known to be > 0)
368 LoadConstant(r_idx, elems - 1);
369 // Generate the copy loop. Going backwards for convenience
370 LIR* target = NewLIR0(kPseudoTargetLabel);
371 // Copy next element
372 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
373 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
374 FreeTemp(r_val);
375 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700376 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 // Restore the target pointer
378 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
379 -mirror::Array::DataOffset(component_size).Int32Value());
380 }
381 } else if (!info->is_range) {
382 // TUNING: interleave
383 for (int i = 0; i < elems; i++) {
384 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
385 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800386 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
387 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800389 if (IsTemp(rl_arg.reg)) {
390 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 }
392 }
393 }
394 if (info->result.location != kLocInvalid) {
395 StoreValue(info->result, GetReturn(false /* not fp */));
396 }
397}
398
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800399//
400// Slow path to ensure a class is initialized for sget/sput.
401//
402class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
403 public:
buzbee2700f7e2014-03-07 09:46:20 -0800404 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
405 RegStorage r_base) :
406 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
407 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800408 }
409
410 void Compile() {
411 LIR* unresolved_target = GenerateTargetLabel();
412 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700413 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800414 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800415 // Copy helper's result into r_base, a no-op on all but MIPS.
416 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
417
418 m2l_->OpUnconditionalBranch(cont_);
419 }
420
421 private:
422 LIR* const uninit_;
423 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800424 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800425};
426
Vladimir Markobe0e5462014-02-26 11:24:15 +0000427void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700428 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000429 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
430 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
431 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
432 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800433 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000434 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 // Fast path, static storage base is this method's class
436 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800437 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800438 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
439 if (IsTemp(rl_method.reg)) {
440 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 }
442 } else {
443 // Medium path, static storage base in a different class which requires checks that the other
444 // class is initialized.
445 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000446 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 // May do runtime call so everything to home locations.
448 FlushAllRegs();
449 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800450 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 LockTemp(r_method);
452 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800453 r_base = TargetReg(kArg0);
454 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800455 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800456 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000457 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800458 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000459 if (!field_info.IsInitialized() &&
460 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800461 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800462
463 // The slow path is invoked if the r_base is NULL or the class pointed
464 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800465 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800466 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800467 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800468 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800469 mirror::Class::StatusOffset().Int32Value(),
470 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800471 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800472
buzbee2700f7e2014-03-07 09:46:20 -0800473 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000474 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800475
476 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 FreeTemp(r_method);
479 }
480 // rBase now holds static storage base
481 if (is_long_or_double) {
482 rl_src = LoadValueWide(rl_src, kAnyReg);
483 } else {
484 rl_src = LoadValue(rl_src, kAnyReg);
485 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000486 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800487 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 GenMemBarrier(kStoreStore);
489 }
490 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800491 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800493 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000495 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800496 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 GenMemBarrier(kStoreLoad);
498 }
499 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800500 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800502 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 } else {
504 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700505 ThreadOffset<4> setter_offset =
506 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
507 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
508 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000509 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 }
511}
512
Vladimir Markobe0e5462014-02-26 11:24:15 +0000513void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700514 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000515 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
516 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
517 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
518 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800519 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000520 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 // Fast path, static storage base is this method's class
522 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800523 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800524 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 } else {
526 // Medium path, static storage base in a different class which requires checks that the other
527 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000528 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 // May do runtime call so everything to home locations.
530 FlushAllRegs();
531 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800532 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 LockTemp(r_method);
534 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800535 r_base = TargetReg(kArg0);
536 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800537 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800538 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000539 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800540 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000541 if (!field_info.IsInitialized() &&
542 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800543 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544
545 // The slow path is invoked if the r_base is NULL or the class pointed
546 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800547 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800548 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800549 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800550 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800551 mirror::Class::StatusOffset().Int32Value(),
552 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800553 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800554
buzbee2700f7e2014-03-07 09:46:20 -0800555 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000556 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800557
558 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 FreeTemp(r_method);
561 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800562 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800564
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800566 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800568 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800570 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800571
572 if (field_info.IsVolatile()) {
573 // Without context sensitive analysis, we must issue the most conservative barriers.
574 // In this case, either a load or store may follow so we issue both barriers.
575 GenMemBarrier(kLoadLoad);
576 GenMemBarrier(kLoadStore);
577 }
578
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 if (is_long_or_double) {
580 StoreValueWide(rl_dest, rl_result);
581 } else {
582 StoreValue(rl_dest, rl_result);
583 }
584 } else {
585 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700586 ThreadOffset<4> getterOffset =
587 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
588 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
589 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000590 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 if (is_long_or_double) {
592 RegLocation rl_result = GetReturnWide(rl_dest.fp);
593 StoreValueWide(rl_dest, rl_result);
594 } else {
595 RegLocation rl_result = GetReturn(rl_dest.fp);
596 StoreValue(rl_dest, rl_result);
597 }
598 }
599}
600
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800601// Generate code for all slow paths.
602void Mir2Lir::HandleSlowPaths() {
603 int n = slow_paths_.Size();
604 for (int i = 0; i < n; ++i) {
605 LIRSlowPath* slowpath = slow_paths_.Get(i);
606 slowpath->Compile();
607 }
608 slow_paths_.Reset();
609}
610
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700611void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700613 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 for (int i = 0; i < num_elems; i++) {
615 ResetRegPool();
616 ResetDefTracking();
617 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700618 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 current_dalvik_offset_ = lab->operands[1];
620 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800621 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
623 OpUnconditionalBranch(resume_lab);
624 }
625}
626
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700627void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 int num_elems = throw_launchpads_.Size();
629 for (int i = 0; i < num_elems; i++) {
630 ResetRegPool();
631 ResetDefTracking();
632 LIR* lab = throw_launchpads_.Get(i);
633 current_dalvik_offset_ = lab->operands[1];
634 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700635 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 int v1 = lab->operands[2];
637 int v2 = lab->operands[3];
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700638 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 switch (lab->operands[0]) {
640 case kThrowNullPointer:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700641 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700643 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
645 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800646 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
647 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800649 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
651 // Make sure the following LoadConstant doesn't mess with kArg1.
652 LockTemp(TargetReg(kArg1));
653 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700654 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 break;
656 case kThrowArrayBounds:
657 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800658 if (v2 != TargetReg(kArg0).GetReg()) {
659 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 if (target_x86) {
661 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800662 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
663 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800665 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 }
667 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800668 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800670 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 if (target_x86) {
672 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800673 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
674 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800676 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 }
678 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
679 } else {
680 if (target_x86) {
681 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800682 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
683 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800685 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 }
buzbee2700f7e2014-03-07 09:46:20 -0800687 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 }
689 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700690 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 break;
692 case kThrowDivZero:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700693 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 break;
695 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800696 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700698 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 default:
701 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
702 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000703 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800704 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700705 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 }
707}
708
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700711 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000712 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
713 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
714 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 RegLocation rl_result;
716 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000717 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 rl_obj = LoadValue(rl_obj, kCoreReg);
719 if (is_long_or_double) {
720 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800721 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700722 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800724 // FIXME? duplicate null check?
725 GenNullCheck(rl_obj.reg, opt_flags);
726 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
727 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800728 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000729 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800730 // Without context sensitive analysis, we must issue the most conservative barriers.
731 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800733 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
735 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800736 RegStorage reg_ptr = AllocTemp();
737 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800739 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700740 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000741 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800742 // Without context sensitive analysis, we must issue the most conservative barriers.
743 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800745 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 }
747 FreeTemp(reg_ptr);
748 }
749 StoreValueWide(rl_dest, rl_result);
750 } else {
751 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800752 GenNullCheck(rl_obj.reg, opt_flags);
753 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
754 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800755 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000756 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800757 // Without context sensitive analysis, we must issue the most conservative barriers.
758 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800760 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 }
762 StoreValue(rl_dest, rl_result);
763 }
764 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700765 ThreadOffset<4> getterOffset =
766 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
767 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
768 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000769 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 if (is_long_or_double) {
771 RegLocation rl_result = GetReturnWide(rl_dest.fp);
772 StoreValueWide(rl_dest, rl_result);
773 } else {
774 RegLocation rl_result = GetReturn(rl_dest.fp);
775 StoreValue(rl_dest, rl_result);
776 }
777 }
778}
779
Vladimir Markobe0e5462014-02-26 11:24:15 +0000780void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700782 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000783 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
784 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
785 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 rl_obj = LoadValue(rl_obj, kCoreReg);
789 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800791 GenNullCheck(rl_obj.reg, opt_flags);
792 RegStorage reg_ptr = AllocTemp();
793 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000794 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 GenMemBarrier(kStoreStore);
797 }
buzbee2700f7e2014-03-07 09:46:20 -0800798 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800799 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000800 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800801 // A load might follow the volatile store so insert a StoreLoad barrier.
802 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 }
804 FreeTemp(reg_ptr);
805 } else {
806 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800807 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000808 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800809 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 GenMemBarrier(kStoreStore);
811 }
buzbee2700f7e2014-03-07 09:46:20 -0800812 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800813 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000814 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800815 // A load might follow the volatile store so insert a StoreLoad barrier.
816 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 }
818 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800819 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 }
821 }
822 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700823 ThreadOffset<4> setter_offset =
824 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
825 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
826 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000827 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
828 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 }
830}
831
Ian Rogersa9a82542013-10-04 11:17:26 -0700832void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
833 RegLocation rl_src) {
834 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
835 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
836 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700837 ThreadOffset<4> helper = needs_range_check
838 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
839 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
840 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700841 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
842}
843
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700844void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800846 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
848 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
849 *cu_->dex_file,
850 type_idx)) {
851 // Call out to helper which resolves type and verifies access.
852 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700853 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800854 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 RegLocation rl_result = GetReturn(false);
856 StoreValue(rl_dest, rl_result);
857 } else {
858 // We're don't need access checks, load type from dex cache
859 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700860 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800861 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 int32_t offset_of_type =
863 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
864 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800865 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
867 type_idx) || SLOW_TYPE_PATH) {
868 // Slow path, at runtime test if type is null and if so initialize
869 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800870 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800871 LIR* cont = NewLIR0(kPseudoTargetLabel);
872
873 // Object to generate the slow path for class resolution.
874 class SlowPath : public LIRSlowPath {
875 public:
876 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
877 const RegLocation& rl_method, const RegLocation& rl_result) :
878 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
879 rl_method_(rl_method), rl_result_(rl_result) {
880 }
881
882 void Compile() {
883 GenerateTargetLabel();
884
Ian Rogersdd7624d2014-03-14 17:43:00 -0700885 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800886 rl_method_.reg, true);
887 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800888
889 m2l_->OpUnconditionalBranch(cont_);
890 }
891
892 private:
893 const int type_idx_;
894 const RegLocation rl_method_;
895 const RegLocation rl_result_;
896 };
897
898 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800899 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800900
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800902 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 // Fast path, we're done - just store result
904 StoreValue(rl_dest, rl_result);
905 }
906 }
907}
908
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 /* NOTE: Most strings should be available at compile time */
911 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
912 (sizeof(mirror::String*) * string_idx);
913 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
914 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
915 // slow path, resolve string if not in dex cache
916 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700917 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800918
919 // If the Method* is already in a register, we can save a copy.
920 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800921 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800922 if (rl_method.location == kLocPhysReg) {
923 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800924 DCHECK(!IsTemp(rl_method.reg));
925 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800926 } else {
927 r_method = TargetReg(kArg2);
928 LoadCurrMethodDirect(r_method);
929 }
930 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
931 TargetReg(kArg0));
932
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800935 if (cu_->instruction_set == kThumb2 ||
936 cu_->instruction_set == kMips) {
937 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800938 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800939 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
940 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800942
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800943 // Object to generate the slow path for string resolution.
944 class SlowPath : public LIRSlowPath {
945 public:
buzbee2700f7e2014-03-07 09:46:20 -0800946 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800947 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
948 }
949
950 void Compile() {
951 GenerateTargetLabel();
952
Dave Allison081f73e2014-04-07 18:58:07 +0000953 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954
Dave Allison081f73e2014-04-07 18:58:07 +0000955 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
956 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
957 m2l_->MarkSafepointPC(call_inst);
958 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800959
960 m2l_->OpUnconditionalBranch(cont_);
961 }
962
963 private:
buzbee2700f7e2014-03-07 09:46:20 -0800964 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965 };
966
967 // Add to list for future.
968 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700970 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800971 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
972 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700973 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800974 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800975 LIR* target = NewLIR0(kPseudoTargetLabel);
976 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 }
978 GenBarrier();
979 StoreValue(rl_dest, GetReturn(false));
980 } else {
981 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800982 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800984 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
985 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 StoreValue(rl_dest, rl_result);
987 }
988}
989
990/*
991 * Let helper function take care of everything. Will
992 * call Class::NewInstanceFromCode(type_idx, method);
993 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700994void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 FlushAllRegs(); /* Everything to home location */
996 // alloc will always check for resolution, do we also need to verify
997 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -0700998 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800999 const DexFile* dex_file = cu_->dex_file;
1000 CompilerDriver* driver = cu_->compiler_driver;
1001 if (driver->CanAccessInstantiableTypeWithoutChecks(
1002 cu_->method_idx, *dex_file, type_idx)) {
1003 bool is_type_initialized;
1004 bool use_direct_type_ptr;
1005 uintptr_t direct_type_ptr;
1006 if (kEmbedClassInCode &&
1007 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1008 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1009 // The fast path.
1010 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001011 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001012 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001013 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001014 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1015 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001016 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001017 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1018 }
1019 } else {
1020 // Use the direct pointer.
1021 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001022 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001023 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1024 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001025 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001026 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1027 }
1028 }
1029 } else {
1030 // The slow path.
1031 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001032 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001033 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1034 }
1035 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001037 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001038 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 RegLocation rl_result = GetReturn(false);
1041 StoreValue(rl_dest, rl_result);
1042}
1043
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001044void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001046 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047}
1048
1049// For final classes there are no sub-classes to check and so we can answer the instance-of
1050// question with simple comparisons.
1051void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1052 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001053 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001054 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001055
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 RegLocation object = LoadValue(rl_src, kCoreReg);
1057 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001058 RegStorage result_reg = rl_result.reg;
1059 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 result_reg = AllocTypedTemp(false, kCoreReg);
1061 }
1062 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001063 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064
buzbee2700f7e2014-03-07 09:46:20 -08001065 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1066 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067
1068 LoadCurrMethodDirect(check_class);
1069 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001070 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1071 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001073 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001075 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 int32_t offset_of_type =
1077 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1078 (sizeof(mirror::Class*) * type_idx);
1079 LoadWordDisp(check_class, offset_of_type, check_class);
1080 }
1081
1082 LIR* ne_branchover = NULL;
1083 if (cu_->instruction_set == kThumb2) {
1084 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001085 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001087 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 } else {
1089 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1090 LoadConstant(result_reg, 1); // eq case - load true
1091 }
1092 LIR* target = NewLIR0(kPseudoTargetLabel);
1093 null_branchover->target = target;
1094 if (ne_branchover != NULL) {
1095 ne_branchover->target = target;
1096 }
1097 FreeTemp(object_class);
1098 FreeTemp(check_class);
1099 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001100 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 FreeTemp(result_reg);
1102 }
1103 StoreValue(rl_dest, rl_result);
1104}
1105
1106void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1107 bool type_known_abstract, bool use_declaring_class,
1108 bool can_assume_type_is_in_dex_cache,
1109 uint32_t type_idx, RegLocation rl_dest,
1110 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001111 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001112 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001113
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 FlushAllRegs();
1115 // May generate a call - use explicit registers
1116 LockCallTemps();
1117 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001118 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 if (needs_access_check) {
1120 // Check we have access to type_idx and if not throw IllegalAccessError,
1121 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001122 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 type_idx, true);
1124 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1125 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1126 } else if (use_declaring_class) {
1127 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001128 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1129 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 } else {
1131 // Load dex cache entry into class_reg (kArg2)
1132 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001133 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1134 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 int32_t offset_of_type =
1136 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1137 * type_idx);
1138 LoadWordDisp(class_reg, offset_of_type, class_reg);
1139 if (!can_assume_type_is_in_dex_cache) {
1140 // Need to test presence of type in dex cache at runtime
1141 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1142 // Not resolved
1143 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001144 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001145 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1147 // Rejoin code paths
1148 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1149 hop_branch->target = hop_target;
1150 }
1151 }
1152 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1153 RegLocation rl_result = GetReturn(false);
1154 if (cu_->instruction_set == kMips) {
1155 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001156 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 }
1158 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1159
1160 /* load object->klass_ */
1161 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1162 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1163 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1164 LIR* branchover = NULL;
1165 if (type_known_final) {
1166 // rl_result == ref == null == 0.
1167 if (cu_->instruction_set == kThumb2) {
1168 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001169 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001170 LoadConstant(rl_result.reg, 1); // .eq case - load true
1171 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001172 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001174 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001176 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178 } else {
1179 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001180 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001181 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 if (!type_known_abstract) {
1183 /* Uses conditional nullification */
1184 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001185 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1187 }
1188 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1189 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001190 if (it != nullptr) {
1191 OpEndIT(it);
1192 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 FreeTemp(r_tgt);
1194 } else {
1195 if (!type_known_abstract) {
1196 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001197 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1199 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001200 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001201 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1202 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1203 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 }
1205 }
1206 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001207 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 /* branch targets here */
1209 LIR* target = NewLIR0(kPseudoTargetLabel);
1210 StoreValue(rl_dest, rl_result);
1211 branch1->target = target;
1212 if (branchover != NULL) {
1213 branchover->target = target;
1214 }
1215}
1216
1217void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1218 bool type_known_final, type_known_abstract, use_declaring_class;
1219 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1220 *cu_->dex_file,
1221 type_idx,
1222 &type_known_final,
1223 &type_known_abstract,
1224 &use_declaring_class);
1225 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1226 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1227
1228 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1229 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1230 } else {
1231 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1232 use_declaring_class, can_assume_type_is_in_dex_cache,
1233 type_idx, rl_dest, rl_src);
1234 }
1235}
1236
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001237void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 bool type_known_final, type_known_abstract, use_declaring_class;
1239 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1240 *cu_->dex_file,
1241 type_idx,
1242 &type_known_final,
1243 &type_known_abstract,
1244 &use_declaring_class);
1245 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1246 // of the exception throw path.
1247 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001248 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 // Verifier type analysis proved this check cast would never cause an exception.
1250 return;
1251 }
1252 FlushAllRegs();
1253 // May generate a call - use explicit registers
1254 LockCallTemps();
1255 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001256 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 if (needs_access_check) {
1258 // Check we have access to type_idx and if not throw IllegalAccessError,
1259 // returns Class* in kRet0
1260 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001261 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 type_idx, TargetReg(kArg1), true);
1263 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1264 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001265 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1266 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 } else {
1268 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001269 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1270 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 int32_t offset_of_type =
1272 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1273 (sizeof(mirror::Class*) * type_idx);
1274 LoadWordDisp(class_reg, offset_of_type, class_reg);
1275 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1276 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001277 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1278 LIR* cont = NewLIR0(kPseudoTargetLabel);
1279
1280 // Slow path to initialize the type. Executed if the type is NULL.
1281 class SlowPath : public LIRSlowPath {
1282 public:
1283 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001284 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001285 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1286 class_reg_(class_reg) {
1287 }
1288
1289 void Compile() {
1290 GenerateTargetLabel();
1291
1292 // Call out to helper, which will return resolved type in kArg0
1293 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001294 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001295 m2l_->TargetReg(kArg1), true);
1296 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1297 m2l_->OpUnconditionalBranch(cont_);
1298 }
1299 public:
1300 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001301 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001302 };
1303
buzbee2700f7e2014-03-07 09:46:20 -08001304 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 }
1306 }
1307 // At this point, class_reg (kArg2) has class
1308 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001309
1310 // Slow path for the case where the classes are not equal. In this case we need
1311 // to call a helper function to do the check.
1312 class SlowPath : public LIRSlowPath {
1313 public:
1314 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1315 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1316 }
1317
1318 void Compile() {
1319 GenerateTargetLabel();
1320
1321 if (load_) {
1322 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1323 m2l_->TargetReg(kArg1));
1324 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001325 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001326 m2l_->TargetReg(kArg1), true);
1327
1328 m2l_->OpUnconditionalBranch(cont_);
1329 }
1330
1331 private:
1332 bool load_;
1333 };
1334
1335 if (type_known_abstract) {
1336 // Easier case, run slow path if target is non-null (slow path will load from target)
1337 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1338 LIR* cont = NewLIR0(kPseudoTargetLabel);
1339 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1340 } else {
1341 // Harder, more common case. We need to generate a forward branch over the load
1342 // if the target is null. If it's non-null we perform the load and branch to the
1343 // slow path if the classes are not equal.
1344
1345 /* Null is OK - continue */
1346 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1347 /* load object->klass_ */
1348 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001349 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001350
1351 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1352 LIR* cont = NewLIR0(kPseudoTargetLabel);
1353
1354 // Add the slow path that will not perform load since this is already done.
1355 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1356
1357 // Set the null check to branch to the continuation.
1358 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 }
1360}
1361
1362void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001363 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 RegLocation rl_result;
1365 if (cu_->instruction_set == kThumb2) {
1366 /*
1367 * NOTE: This is the one place in the code in which we might have
1368 * as many as six live temporary registers. There are 5 in the normal
1369 * set for Arm. Until we have spill capabilities, temporarily add
1370 * lr to the temp set. It is safe to do this locally, but note that
1371 * lr is used explicitly elsewhere in the code generator and cannot
1372 * normally be used as a general temp register.
1373 */
1374 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1375 FreeTemp(TargetReg(kLr)); // and make it available
1376 }
1377 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1378 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1379 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1380 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001381 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1382 RegStorage t_reg = AllocTemp();
1383 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1384 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1385 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 FreeTemp(t_reg);
1387 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001388 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1389 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
1391 /*
1392 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1393 * following StoreValueWide might need to allocate a temp register.
1394 * To further work around the lack of a spill capability, explicitly
1395 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1396 * Remove when spill is functional.
1397 */
1398 FreeRegLocTemps(rl_result, rl_src1);
1399 FreeRegLocTemps(rl_result, rl_src2);
1400 StoreValueWide(rl_dest, rl_result);
1401 if (cu_->instruction_set == kThumb2) {
1402 Clobber(TargetReg(kLr));
1403 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1404 }
1405}
1406
1407
1408void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001409 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001410 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411
1412 switch (opcode) {
1413 case Instruction::SHL_LONG:
1414 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001415 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 break;
1417 case Instruction::SHR_LONG:
1418 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001419 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001420 break;
1421 case Instruction::USHR_LONG:
1422 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001423 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 break;
1425 default:
1426 LOG(FATAL) << "Unexpected case";
1427 }
1428 FlushAllRegs(); /* Send everything to home location */
1429 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1430 RegLocation rl_result = GetReturnWide(false);
1431 StoreValueWide(rl_dest, rl_result);
1432}
1433
1434
1435void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001436 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001437 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 OpKind op = kOpBkpt;
1439 bool is_div_rem = false;
1440 bool check_zero = false;
1441 bool unary = false;
1442 RegLocation rl_result;
1443 bool shift_op = false;
1444 switch (opcode) {
1445 case Instruction::NEG_INT:
1446 op = kOpNeg;
1447 unary = true;
1448 break;
1449 case Instruction::NOT_INT:
1450 op = kOpMvn;
1451 unary = true;
1452 break;
1453 case Instruction::ADD_INT:
1454 case Instruction::ADD_INT_2ADDR:
1455 op = kOpAdd;
1456 break;
1457 case Instruction::SUB_INT:
1458 case Instruction::SUB_INT_2ADDR:
1459 op = kOpSub;
1460 break;
1461 case Instruction::MUL_INT:
1462 case Instruction::MUL_INT_2ADDR:
1463 op = kOpMul;
1464 break;
1465 case Instruction::DIV_INT:
1466 case Instruction::DIV_INT_2ADDR:
1467 check_zero = true;
1468 op = kOpDiv;
1469 is_div_rem = true;
1470 break;
1471 /* NOTE: returns in kArg1 */
1472 case Instruction::REM_INT:
1473 case Instruction::REM_INT_2ADDR:
1474 check_zero = true;
1475 op = kOpRem;
1476 is_div_rem = true;
1477 break;
1478 case Instruction::AND_INT:
1479 case Instruction::AND_INT_2ADDR:
1480 op = kOpAnd;
1481 break;
1482 case Instruction::OR_INT:
1483 case Instruction::OR_INT_2ADDR:
1484 op = kOpOr;
1485 break;
1486 case Instruction::XOR_INT:
1487 case Instruction::XOR_INT_2ADDR:
1488 op = kOpXor;
1489 break;
1490 case Instruction::SHL_INT:
1491 case Instruction::SHL_INT_2ADDR:
1492 shift_op = true;
1493 op = kOpLsl;
1494 break;
1495 case Instruction::SHR_INT:
1496 case Instruction::SHR_INT_2ADDR:
1497 shift_op = true;
1498 op = kOpAsr;
1499 break;
1500 case Instruction::USHR_INT:
1501 case Instruction::USHR_INT_2ADDR:
1502 shift_op = true;
1503 op = kOpLsr;
1504 break;
1505 default:
1506 LOG(FATAL) << "Invalid word arith op: " << opcode;
1507 }
1508 if (!is_div_rem) {
1509 if (unary) {
1510 rl_src1 = LoadValue(rl_src1, kCoreReg);
1511 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001512 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 } else {
1514 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001515 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001516 RegStorage t_reg = AllocTemp();
1517 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 rl_src1 = LoadValue(rl_src1, kCoreReg);
1519 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001520 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 FreeTemp(t_reg);
1522 } else {
1523 rl_src1 = LoadValue(rl_src1, kCoreReg);
1524 rl_src2 = LoadValue(rl_src2, kCoreReg);
1525 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001526 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 }
1528 }
1529 StoreValue(rl_dest, rl_result);
1530 } else {
Dave Allison70202782013-10-22 17:52:19 -07001531 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 -07001532 if (cu_->instruction_set == kMips) {
1533 rl_src1 = LoadValue(rl_src1, kCoreReg);
1534 rl_src2 = LoadValue(rl_src2, kCoreReg);
1535 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001536 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 }
buzbee2700f7e2014-03-07 09:46:20 -08001538 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001539 done = true;
1540 } else if (cu_->instruction_set == kThumb2) {
1541 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1542 // Use ARM SDIV instruction for division. For remainder we also need to
1543 // calculate using a MUL and subtract.
1544 rl_src1 = LoadValue(rl_src1, kCoreReg);
1545 rl_src2 = LoadValue(rl_src2, kCoreReg);
1546 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001547 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001548 }
buzbee2700f7e2014-03-07 09:46:20 -08001549 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001550 done = true;
1551 }
1552 }
1553
1554 // If we haven't already generated the code use the callout function.
1555 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001556 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 FlushAllRegs(); /* Send everything to home location */
1558 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001559 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1561 if (check_zero) {
1562 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1563 }
Dave Allison70202782013-10-22 17:52:19 -07001564 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001565 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 if (op == kOpDiv)
1567 rl_result = GetReturn(false);
1568 else
1569 rl_result = GetReturnAlt();
1570 }
1571 StoreValue(rl_dest, rl_result);
1572 }
1573}
1574
1575/*
1576 * The following are the first-level codegen routines that analyze the format
1577 * of each bytecode then either dispatch special purpose codegen routines
1578 * or produce corresponding Thumb instructions directly.
1579 */
1580
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001582static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 x &= x - 1;
1584 return (x & (x - 1)) == 0;
1585}
1586
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1588// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001589bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001590 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1592 return false;
1593 }
1594 // No divide instruction for Arm, so check for more special cases
1595 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001596 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 }
1598 int k = LowestSetBit(lit);
1599 if (k >= 30) {
1600 // Avoid special cases.
1601 return false;
1602 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 rl_src = LoadValue(rl_src, kCoreReg);
1604 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001605 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001606 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 if (lit == 2) {
1608 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001609 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1610 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1611 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001613 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001615 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1616 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 }
1618 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001619 RegStorage t_reg1 = AllocTemp();
1620 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001622 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1623 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001625 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001629 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 }
1633 }
1634 StoreValue(rl_dest, rl_result);
1635 return true;
1636}
1637
1638// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1639// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001640bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001641 if (lit < 0) {
1642 return false;
1643 }
1644 if (lit == 0) {
1645 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1646 LoadConstant(rl_result.reg, 0);
1647 StoreValue(rl_dest, rl_result);
1648 return true;
1649 }
1650 if (lit == 1) {
1651 rl_src = LoadValue(rl_src, kCoreReg);
1652 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1653 OpRegCopy(rl_result.reg, rl_src.reg);
1654 StoreValue(rl_dest, rl_result);
1655 return true;
1656 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001657 // There is RegRegRegShift on Arm, so check for more special cases
1658 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001659 return EasyMultiply(rl_src, rl_dest, lit);
1660 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 // Can we simplify this multiplication?
1662 bool power_of_two = false;
1663 bool pop_count_le2 = false;
1664 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001665 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 power_of_two = true;
1667 } else if (IsPopCountLE2(lit)) {
1668 pop_count_le2 = true;
1669 } else if (IsPowerOfTwo(lit + 1)) {
1670 power_of_two_minus_one = true;
1671 } else {
1672 return false;
1673 }
1674 rl_src = LoadValue(rl_src, kCoreReg);
1675 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1676 if (power_of_two) {
1677 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001678 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 } else if (pop_count_le2) {
1680 // Shift and add and shift.
1681 int first_bit = LowestSetBit(lit);
1682 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1683 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1684 } else {
1685 // Reverse subtract: (src << (shift + 1)) - src.
1686 DCHECK(power_of_two_minus_one);
1687 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001688 RegStorage t_reg = AllocTemp();
1689 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1690 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001691 }
1692 StoreValue(rl_dest, rl_result);
1693 return true;
1694}
1695
1696void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001697 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698 RegLocation rl_result;
1699 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1700 int shift_op = false;
1701 bool is_div = false;
1702
1703 switch (opcode) {
1704 case Instruction::RSUB_INT_LIT8:
1705 case Instruction::RSUB_INT: {
1706 rl_src = LoadValue(rl_src, kCoreReg);
1707 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1708 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001709 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001711 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1712 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 }
1714 StoreValue(rl_dest, rl_result);
1715 return;
1716 }
1717
1718 case Instruction::SUB_INT:
1719 case Instruction::SUB_INT_2ADDR:
1720 lit = -lit;
1721 // Intended fallthrough
1722 case Instruction::ADD_INT:
1723 case Instruction::ADD_INT_2ADDR:
1724 case Instruction::ADD_INT_LIT8:
1725 case Instruction::ADD_INT_LIT16:
1726 op = kOpAdd;
1727 break;
1728 case Instruction::MUL_INT:
1729 case Instruction::MUL_INT_2ADDR:
1730 case Instruction::MUL_INT_LIT8:
1731 case Instruction::MUL_INT_LIT16: {
1732 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1733 return;
1734 }
1735 op = kOpMul;
1736 break;
1737 }
1738 case Instruction::AND_INT:
1739 case Instruction::AND_INT_2ADDR:
1740 case Instruction::AND_INT_LIT8:
1741 case Instruction::AND_INT_LIT16:
1742 op = kOpAnd;
1743 break;
1744 case Instruction::OR_INT:
1745 case Instruction::OR_INT_2ADDR:
1746 case Instruction::OR_INT_LIT8:
1747 case Instruction::OR_INT_LIT16:
1748 op = kOpOr;
1749 break;
1750 case Instruction::XOR_INT:
1751 case Instruction::XOR_INT_2ADDR:
1752 case Instruction::XOR_INT_LIT8:
1753 case Instruction::XOR_INT_LIT16:
1754 op = kOpXor;
1755 break;
1756 case Instruction::SHL_INT_LIT8:
1757 case Instruction::SHL_INT:
1758 case Instruction::SHL_INT_2ADDR:
1759 lit &= 31;
1760 shift_op = true;
1761 op = kOpLsl;
1762 break;
1763 case Instruction::SHR_INT_LIT8:
1764 case Instruction::SHR_INT:
1765 case Instruction::SHR_INT_2ADDR:
1766 lit &= 31;
1767 shift_op = true;
1768 op = kOpAsr;
1769 break;
1770 case Instruction::USHR_INT_LIT8:
1771 case Instruction::USHR_INT:
1772 case Instruction::USHR_INT_2ADDR:
1773 lit &= 31;
1774 shift_op = true;
1775 op = kOpLsr;
1776 break;
1777
1778 case Instruction::DIV_INT:
1779 case Instruction::DIV_INT_2ADDR:
1780 case Instruction::DIV_INT_LIT8:
1781 case Instruction::DIV_INT_LIT16:
1782 case Instruction::REM_INT:
1783 case Instruction::REM_INT_2ADDR:
1784 case Instruction::REM_INT_LIT8:
1785 case Instruction::REM_INT_LIT16: {
1786 if (lit == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001787 GenImmedCheck(kCondAl, RegStorage::InvalidReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 return;
1789 }
buzbee11b63d12013-08-27 07:34:17 -07001790 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001791 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001792 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 (opcode == Instruction::DIV_INT_LIT16)) {
1794 is_div = true;
1795 } else {
1796 is_div = false;
1797 }
buzbee11b63d12013-08-27 07:34:17 -07001798 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1799 return;
1800 }
Dave Allison70202782013-10-22 17:52:19 -07001801
1802 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803 if (cu_->instruction_set == kMips) {
1804 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001805 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001806 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001807 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001808 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1809 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001810 } else if (cu_->instruction_set == kThumb2) {
1811 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1812 // Use ARM SDIV instruction for division. For remainder we also need to
1813 // calculate using a MUL and subtract.
1814 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001815 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001816 done = true;
1817 }
1818 }
1819
1820 if (!done) {
1821 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001822 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1823 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001824 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001825 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1826 if (is_div)
1827 rl_result = GetReturn(false);
1828 else
1829 rl_result = GetReturnAlt();
1830 }
1831 StoreValue(rl_dest, rl_result);
1832 return;
1833 }
1834 default:
1835 LOG(FATAL) << "Unexpected opcode " << opcode;
1836 }
1837 rl_src = LoadValue(rl_src, kCoreReg);
1838 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001839 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001841 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001842 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001843 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001844 }
1845 StoreValue(rl_dest, rl_result);
1846}
1847
1848void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001849 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 RegLocation rl_result;
1851 OpKind first_op = kOpBkpt;
1852 OpKind second_op = kOpBkpt;
1853 bool call_out = false;
1854 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001855 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001856 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857
1858 switch (opcode) {
1859 case Instruction::NOT_LONG:
1860 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1861 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1862 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001863 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1864 RegStorage t_reg = AllocTemp();
1865 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1866 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1867 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001868 FreeTemp(t_reg);
1869 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001870 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1871 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 }
1873 StoreValueWide(rl_dest, rl_result);
1874 return;
1875 case Instruction::ADD_LONG:
1876 case Instruction::ADD_LONG_2ADDR:
1877 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001878 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 return;
1880 }
1881 first_op = kOpAdd;
1882 second_op = kOpAdc;
1883 break;
1884 case Instruction::SUB_LONG:
1885 case Instruction::SUB_LONG_2ADDR:
1886 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001887 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 return;
1889 }
1890 first_op = kOpSub;
1891 second_op = kOpSbc;
1892 break;
1893 case Instruction::MUL_LONG:
1894 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001895 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001896 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 return;
1898 } else {
1899 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001900 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001901 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 }
1903 break;
1904 case Instruction::DIV_LONG:
1905 case Instruction::DIV_LONG_2ADDR:
1906 call_out = true;
1907 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001908 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001909 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910 break;
1911 case Instruction::REM_LONG:
1912 case Instruction::REM_LONG_2ADDR:
1913 call_out = true;
1914 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001915 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001916 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001917 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001918 break;
1919 case Instruction::AND_LONG_2ADDR:
1920 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001921 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001922 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 }
1924 first_op = kOpAnd;
1925 second_op = kOpAnd;
1926 break;
1927 case Instruction::OR_LONG:
1928 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001929 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001930 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001931 return;
1932 }
1933 first_op = kOpOr;
1934 second_op = kOpOr;
1935 break;
1936 case Instruction::XOR_LONG:
1937 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001938 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001939 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 return;
1941 }
1942 first_op = kOpXor;
1943 second_op = kOpXor;
1944 break;
1945 case Instruction::NEG_LONG: {
1946 GenNegLong(rl_dest, rl_src2);
1947 return;
1948 }
1949 default:
1950 LOG(FATAL) << "Invalid long arith op";
1951 }
1952 if (!call_out) {
1953 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1954 } else {
1955 FlushAllRegs(); /* Send everything to home location */
1956 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001957 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1958 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1959 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1960 RegStorage r_tgt = CallHelperSetup(func_offset);
1961 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1962 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001963 // NOTE: callout here is not a safepoint
1964 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1965 } else {
1966 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1967 }
1968 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001969 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970 rl_result = GetReturnWide(false);
1971 else
1972 rl_result = GetReturnWideAlt();
1973 StoreValueWide(rl_dest, rl_result);
1974 }
1975}
1976
Ian Rogersdd7624d2014-03-14 17:43:00 -07001977void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001978 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979 /*
1980 * Don't optimize the register usage since it calls out to support
1981 * functions
1982 */
1983 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1985 if (rl_dest.wide) {
1986 RegLocation rl_result;
1987 rl_result = GetReturnWide(rl_dest.fp);
1988 StoreValueWide(rl_dest, rl_result);
1989 } else {
1990 RegLocation rl_result;
1991 rl_result = GetReturn(rl_dest.fp);
1992 StoreValue(rl_dest, rl_result);
1993 }
1994}
1995
1996/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001997void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001998 if (Runtime::Current()->ExplicitSuspendChecks()) {
1999 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2000 return;
2001 }
2002 FlushAllRegs();
2003 LIR* branch = OpTestSuspend(NULL);
2004 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2005 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2006 current_dalvik_offset_);
2007 branch->target = target;
2008 suspend_launchpads_.Insert(target);
2009 } else {
2010 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2011 return;
2012 }
2013 FlushAllRegs(); // TODO: needed?
2014 LIR* inst = CheckSuspendUsingLoad();
2015 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017}
2018
2019/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002020void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002021 if (Runtime::Current()->ExplicitSuspendChecks()) {
2022 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2023 OpUnconditionalBranch(target);
2024 return;
2025 }
2026 OpTestSuspend(target);
2027 LIR* launch_pad =
2028 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2029 current_dalvik_offset_);
2030 FlushAllRegs();
2031 OpUnconditionalBranch(launch_pad);
2032 suspend_launchpads_.Insert(launch_pad);
2033 } else {
2034 // For the implicit suspend check, just perform the trigger
2035 // load and branch to the target.
2036 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2037 OpUnconditionalBranch(target);
2038 return;
2039 }
2040 FlushAllRegs();
2041 LIR* inst = CheckSuspendUsingLoad();
2042 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002045}
2046
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002047/* Call out to helper assembly routine that will null check obj and then lock it. */
2048void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2049 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002050 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002051}
2052
2053/* Call out to helper assembly routine that will null check obj and then unlock it. */
2054void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2055 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002056 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002057}
2058
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002059/* Generic code for generating a wide constant into a VR. */
2060void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2061 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002062 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002063 StoreValueWide(rl_dest, rl_result);
2064}
2065
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066} // namespace art