blob: c5c42e8361b73be154a06e4ded184de3ffdf2bef [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
Ian Rogersdd7624d2014-03-14 17:43:00 -0700953 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954
955 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);
959
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?
1085 OpIT(kCondEq, ""); // if-convert the test
1086 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison43a065c2014-04-01 15:14:46 -07001087 GenBarrier();
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?
1169 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 Allison43a065c2014-04-01 15:14:46 -07001172 GenBarrier();
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));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 if (!type_known_abstract) {
1182 /* Uses conditional nullification */
1183 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1184 OpIT(kCondEq, "EE"); // if-convert the test
1185 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1186 }
1187 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1188 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison43a065c2014-04-01 15:14:46 -07001189 GenBarrier();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 FreeTemp(r_tgt);
1191 } else {
1192 if (!type_known_abstract) {
1193 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001194 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1196 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001197 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001198 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1199 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1200 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 }
1202 }
1203 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001204 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 /* branch targets here */
1206 LIR* target = NewLIR0(kPseudoTargetLabel);
1207 StoreValue(rl_dest, rl_result);
1208 branch1->target = target;
1209 if (branchover != NULL) {
1210 branchover->target = target;
1211 }
1212}
1213
1214void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1215 bool type_known_final, type_known_abstract, use_declaring_class;
1216 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1217 *cu_->dex_file,
1218 type_idx,
1219 &type_known_final,
1220 &type_known_abstract,
1221 &use_declaring_class);
1222 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1223 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1224
1225 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1226 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1227 } else {
1228 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1229 use_declaring_class, can_assume_type_is_in_dex_cache,
1230 type_idx, rl_dest, rl_src);
1231 }
1232}
1233
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001234void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 bool type_known_final, type_known_abstract, use_declaring_class;
1236 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1237 *cu_->dex_file,
1238 type_idx,
1239 &type_known_final,
1240 &type_known_abstract,
1241 &use_declaring_class);
1242 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1243 // of the exception throw path.
1244 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001245 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 // Verifier type analysis proved this check cast would never cause an exception.
1247 return;
1248 }
1249 FlushAllRegs();
1250 // May generate a call - use explicit registers
1251 LockCallTemps();
1252 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001253 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 if (needs_access_check) {
1255 // Check we have access to type_idx and if not throw IllegalAccessError,
1256 // returns Class* in kRet0
1257 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001258 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 type_idx, TargetReg(kArg1), true);
1260 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1261 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001262 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1263 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 } else {
1265 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001266 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1267 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 int32_t offset_of_type =
1269 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1270 (sizeof(mirror::Class*) * type_idx);
1271 LoadWordDisp(class_reg, offset_of_type, class_reg);
1272 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1273 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001274 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1275 LIR* cont = NewLIR0(kPseudoTargetLabel);
1276
1277 // Slow path to initialize the type. Executed if the type is NULL.
1278 class SlowPath : public LIRSlowPath {
1279 public:
1280 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001281 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001282 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1283 class_reg_(class_reg) {
1284 }
1285
1286 void Compile() {
1287 GenerateTargetLabel();
1288
1289 // Call out to helper, which will return resolved type in kArg0
1290 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001291 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001292 m2l_->TargetReg(kArg1), true);
1293 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1294 m2l_->OpUnconditionalBranch(cont_);
1295 }
1296 public:
1297 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001298 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001299 };
1300
buzbee2700f7e2014-03-07 09:46:20 -08001301 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 }
1303 }
1304 // At this point, class_reg (kArg2) has class
1305 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001306
1307 // Slow path for the case where the classes are not equal. In this case we need
1308 // to call a helper function to do the check.
1309 class SlowPath : public LIRSlowPath {
1310 public:
1311 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1312 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1313 }
1314
1315 void Compile() {
1316 GenerateTargetLabel();
1317
1318 if (load_) {
1319 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1320 m2l_->TargetReg(kArg1));
1321 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001322 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001323 m2l_->TargetReg(kArg1), true);
1324
1325 m2l_->OpUnconditionalBranch(cont_);
1326 }
1327
1328 private:
1329 bool load_;
1330 };
1331
1332 if (type_known_abstract) {
1333 // Easier case, run slow path if target is non-null (slow path will load from target)
1334 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1335 LIR* cont = NewLIR0(kPseudoTargetLabel);
1336 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1337 } else {
1338 // Harder, more common case. We need to generate a forward branch over the load
1339 // if the target is null. If it's non-null we perform the load and branch to the
1340 // slow path if the classes are not equal.
1341
1342 /* Null is OK - continue */
1343 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1344 /* load object->klass_ */
1345 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001346 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001347
1348 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1349 LIR* cont = NewLIR0(kPseudoTargetLabel);
1350
1351 // Add the slow path that will not perform load since this is already done.
1352 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1353
1354 // Set the null check to branch to the continuation.
1355 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 }
1357}
1358
1359void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001360 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 RegLocation rl_result;
1362 if (cu_->instruction_set == kThumb2) {
1363 /*
1364 * NOTE: This is the one place in the code in which we might have
1365 * as many as six live temporary registers. There are 5 in the normal
1366 * set for Arm. Until we have spill capabilities, temporarily add
1367 * lr to the temp set. It is safe to do this locally, but note that
1368 * lr is used explicitly elsewhere in the code generator and cannot
1369 * normally be used as a general temp register.
1370 */
1371 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1372 FreeTemp(TargetReg(kLr)); // and make it available
1373 }
1374 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1375 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1376 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1377 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001378 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1379 RegStorage t_reg = AllocTemp();
1380 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1381 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1382 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383 FreeTemp(t_reg);
1384 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001385 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1386 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 }
1388 /*
1389 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1390 * following StoreValueWide might need to allocate a temp register.
1391 * To further work around the lack of a spill capability, explicitly
1392 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1393 * Remove when spill is functional.
1394 */
1395 FreeRegLocTemps(rl_result, rl_src1);
1396 FreeRegLocTemps(rl_result, rl_src2);
1397 StoreValueWide(rl_dest, rl_result);
1398 if (cu_->instruction_set == kThumb2) {
1399 Clobber(TargetReg(kLr));
1400 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1401 }
1402}
1403
1404
1405void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001406 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001407 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408
1409 switch (opcode) {
1410 case Instruction::SHL_LONG:
1411 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001412 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 break;
1414 case Instruction::SHR_LONG:
1415 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001416 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 break;
1418 case Instruction::USHR_LONG:
1419 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001420 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 break;
1422 default:
1423 LOG(FATAL) << "Unexpected case";
1424 }
1425 FlushAllRegs(); /* Send everything to home location */
1426 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1427 RegLocation rl_result = GetReturnWide(false);
1428 StoreValueWide(rl_dest, rl_result);
1429}
1430
1431
1432void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001433 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001434 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001435 OpKind op = kOpBkpt;
1436 bool is_div_rem = false;
1437 bool check_zero = false;
1438 bool unary = false;
1439 RegLocation rl_result;
1440 bool shift_op = false;
1441 switch (opcode) {
1442 case Instruction::NEG_INT:
1443 op = kOpNeg;
1444 unary = true;
1445 break;
1446 case Instruction::NOT_INT:
1447 op = kOpMvn;
1448 unary = true;
1449 break;
1450 case Instruction::ADD_INT:
1451 case Instruction::ADD_INT_2ADDR:
1452 op = kOpAdd;
1453 break;
1454 case Instruction::SUB_INT:
1455 case Instruction::SUB_INT_2ADDR:
1456 op = kOpSub;
1457 break;
1458 case Instruction::MUL_INT:
1459 case Instruction::MUL_INT_2ADDR:
1460 op = kOpMul;
1461 break;
1462 case Instruction::DIV_INT:
1463 case Instruction::DIV_INT_2ADDR:
1464 check_zero = true;
1465 op = kOpDiv;
1466 is_div_rem = true;
1467 break;
1468 /* NOTE: returns in kArg1 */
1469 case Instruction::REM_INT:
1470 case Instruction::REM_INT_2ADDR:
1471 check_zero = true;
1472 op = kOpRem;
1473 is_div_rem = true;
1474 break;
1475 case Instruction::AND_INT:
1476 case Instruction::AND_INT_2ADDR:
1477 op = kOpAnd;
1478 break;
1479 case Instruction::OR_INT:
1480 case Instruction::OR_INT_2ADDR:
1481 op = kOpOr;
1482 break;
1483 case Instruction::XOR_INT:
1484 case Instruction::XOR_INT_2ADDR:
1485 op = kOpXor;
1486 break;
1487 case Instruction::SHL_INT:
1488 case Instruction::SHL_INT_2ADDR:
1489 shift_op = true;
1490 op = kOpLsl;
1491 break;
1492 case Instruction::SHR_INT:
1493 case Instruction::SHR_INT_2ADDR:
1494 shift_op = true;
1495 op = kOpAsr;
1496 break;
1497 case Instruction::USHR_INT:
1498 case Instruction::USHR_INT_2ADDR:
1499 shift_op = true;
1500 op = kOpLsr;
1501 break;
1502 default:
1503 LOG(FATAL) << "Invalid word arith op: " << opcode;
1504 }
1505 if (!is_div_rem) {
1506 if (unary) {
1507 rl_src1 = LoadValue(rl_src1, kCoreReg);
1508 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001509 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 } else {
1511 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001512 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001513 RegStorage t_reg = AllocTemp();
1514 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 rl_src1 = LoadValue(rl_src1, kCoreReg);
1516 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001517 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 FreeTemp(t_reg);
1519 } else {
1520 rl_src1 = LoadValue(rl_src1, kCoreReg);
1521 rl_src2 = LoadValue(rl_src2, kCoreReg);
1522 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001523 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
1525 }
1526 StoreValue(rl_dest, rl_result);
1527 } else {
Dave Allison70202782013-10-22 17:52:19 -07001528 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 -07001529 if (cu_->instruction_set == kMips) {
1530 rl_src1 = LoadValue(rl_src1, kCoreReg);
1531 rl_src2 = LoadValue(rl_src2, kCoreReg);
1532 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001533 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 }
buzbee2700f7e2014-03-07 09:46:20 -08001535 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001536 done = true;
1537 } else if (cu_->instruction_set == kThumb2) {
1538 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1539 // Use ARM SDIV instruction for division. For remainder we also need to
1540 // calculate using a MUL and subtract.
1541 rl_src1 = LoadValue(rl_src1, kCoreReg);
1542 rl_src2 = LoadValue(rl_src2, kCoreReg);
1543 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001544 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001545 }
buzbee2700f7e2014-03-07 09:46:20 -08001546 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001547 done = true;
1548 }
1549 }
1550
1551 // If we haven't already generated the code use the callout function.
1552 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001553 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 FlushAllRegs(); /* Send everything to home location */
1555 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001556 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1558 if (check_zero) {
1559 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1560 }
Dave Allison70202782013-10-22 17:52:19 -07001561 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001562 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 if (op == kOpDiv)
1564 rl_result = GetReturn(false);
1565 else
1566 rl_result = GetReturnAlt();
1567 }
1568 StoreValue(rl_dest, rl_result);
1569 }
1570}
1571
1572/*
1573 * The following are the first-level codegen routines that analyze the format
1574 * of each bytecode then either dispatch special purpose codegen routines
1575 * or produce corresponding Thumb instructions directly.
1576 */
1577
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001579static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 x &= x - 1;
1581 return (x & (x - 1)) == 0;
1582}
1583
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1585// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001586bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001587 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1589 return false;
1590 }
1591 // No divide instruction for Arm, so check for more special cases
1592 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001593 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 }
1595 int k = LowestSetBit(lit);
1596 if (k >= 30) {
1597 // Avoid special cases.
1598 return false;
1599 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 rl_src = LoadValue(rl_src, kCoreReg);
1601 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001602 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001603 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 if (lit == 2) {
1605 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001606 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1607 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1608 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001612 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1613 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 }
1615 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001616 RegStorage t_reg1 = AllocTemp();
1617 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001619 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1620 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001622 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001624 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001626 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001628 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 }
1630 }
1631 StoreValue(rl_dest, rl_result);
1632 return true;
1633}
1634
1635// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1636// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001637bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001638 if (lit < 0) {
1639 return false;
1640 }
1641 if (lit == 0) {
1642 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1643 LoadConstant(rl_result.reg, 0);
1644 StoreValue(rl_dest, rl_result);
1645 return true;
1646 }
1647 if (lit == 1) {
1648 rl_src = LoadValue(rl_src, kCoreReg);
1649 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1650 OpRegCopy(rl_result.reg, rl_src.reg);
1651 StoreValue(rl_dest, rl_result);
1652 return true;
1653 }
1654 // There is RegRegRegShift on Arm, so check for more special cases.
1655 // TODO: disabled, need to handle case of "dest == src" properly.
1656 if (false && cu_->instruction_set == kThumb2) {
1657 return EasyMultiply(rl_src, rl_dest, lit);
1658 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 // Can we simplify this multiplication?
1660 bool power_of_two = false;
1661 bool pop_count_le2 = false;
1662 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001663 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 power_of_two = true;
1665 } else if (IsPopCountLE2(lit)) {
1666 pop_count_le2 = true;
1667 } else if (IsPowerOfTwo(lit + 1)) {
1668 power_of_two_minus_one = true;
1669 } else {
1670 return false;
1671 }
1672 rl_src = LoadValue(rl_src, kCoreReg);
1673 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1674 if (power_of_two) {
1675 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001676 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677 } else if (pop_count_le2) {
1678 // Shift and add and shift.
1679 int first_bit = LowestSetBit(lit);
1680 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1681 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1682 } else {
1683 // Reverse subtract: (src << (shift + 1)) - src.
1684 DCHECK(power_of_two_minus_one);
1685 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001686 RegStorage t_reg = AllocTemp();
1687 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1688 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 }
1690 StoreValue(rl_dest, rl_result);
1691 return true;
1692}
1693
1694void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001695 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 RegLocation rl_result;
1697 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1698 int shift_op = false;
1699 bool is_div = false;
1700
1701 switch (opcode) {
1702 case Instruction::RSUB_INT_LIT8:
1703 case Instruction::RSUB_INT: {
1704 rl_src = LoadValue(rl_src, kCoreReg);
1705 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1706 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001707 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001709 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1710 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 }
1712 StoreValue(rl_dest, rl_result);
1713 return;
1714 }
1715
1716 case Instruction::SUB_INT:
1717 case Instruction::SUB_INT_2ADDR:
1718 lit = -lit;
1719 // Intended fallthrough
1720 case Instruction::ADD_INT:
1721 case Instruction::ADD_INT_2ADDR:
1722 case Instruction::ADD_INT_LIT8:
1723 case Instruction::ADD_INT_LIT16:
1724 op = kOpAdd;
1725 break;
1726 case Instruction::MUL_INT:
1727 case Instruction::MUL_INT_2ADDR:
1728 case Instruction::MUL_INT_LIT8:
1729 case Instruction::MUL_INT_LIT16: {
1730 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1731 return;
1732 }
1733 op = kOpMul;
1734 break;
1735 }
1736 case Instruction::AND_INT:
1737 case Instruction::AND_INT_2ADDR:
1738 case Instruction::AND_INT_LIT8:
1739 case Instruction::AND_INT_LIT16:
1740 op = kOpAnd;
1741 break;
1742 case Instruction::OR_INT:
1743 case Instruction::OR_INT_2ADDR:
1744 case Instruction::OR_INT_LIT8:
1745 case Instruction::OR_INT_LIT16:
1746 op = kOpOr;
1747 break;
1748 case Instruction::XOR_INT:
1749 case Instruction::XOR_INT_2ADDR:
1750 case Instruction::XOR_INT_LIT8:
1751 case Instruction::XOR_INT_LIT16:
1752 op = kOpXor;
1753 break;
1754 case Instruction::SHL_INT_LIT8:
1755 case Instruction::SHL_INT:
1756 case Instruction::SHL_INT_2ADDR:
1757 lit &= 31;
1758 shift_op = true;
1759 op = kOpLsl;
1760 break;
1761 case Instruction::SHR_INT_LIT8:
1762 case Instruction::SHR_INT:
1763 case Instruction::SHR_INT_2ADDR:
1764 lit &= 31;
1765 shift_op = true;
1766 op = kOpAsr;
1767 break;
1768 case Instruction::USHR_INT_LIT8:
1769 case Instruction::USHR_INT:
1770 case Instruction::USHR_INT_2ADDR:
1771 lit &= 31;
1772 shift_op = true;
1773 op = kOpLsr;
1774 break;
1775
1776 case Instruction::DIV_INT:
1777 case Instruction::DIV_INT_2ADDR:
1778 case Instruction::DIV_INT_LIT8:
1779 case Instruction::DIV_INT_LIT16:
1780 case Instruction::REM_INT:
1781 case Instruction::REM_INT_2ADDR:
1782 case Instruction::REM_INT_LIT8:
1783 case Instruction::REM_INT_LIT16: {
1784 if (lit == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001785 GenImmedCheck(kCondAl, RegStorage::InvalidReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 return;
1787 }
buzbee11b63d12013-08-27 07:34:17 -07001788 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001789 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001790 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001791 (opcode == Instruction::DIV_INT_LIT16)) {
1792 is_div = true;
1793 } else {
1794 is_div = false;
1795 }
buzbee11b63d12013-08-27 07:34:17 -07001796 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1797 return;
1798 }
Dave Allison70202782013-10-22 17:52:19 -07001799
1800 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 if (cu_->instruction_set == kMips) {
1802 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001803 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001804 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001805 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001806 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1807 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001808 } else if (cu_->instruction_set == kThumb2) {
1809 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1810 // Use ARM SDIV instruction for division. For remainder we also need to
1811 // calculate using a MUL and subtract.
1812 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001813 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001814 done = true;
1815 }
1816 }
1817
1818 if (!done) {
1819 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1821 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001822 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001823 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1824 if (is_div)
1825 rl_result = GetReturn(false);
1826 else
1827 rl_result = GetReturnAlt();
1828 }
1829 StoreValue(rl_dest, rl_result);
1830 return;
1831 }
1832 default:
1833 LOG(FATAL) << "Unexpected opcode " << opcode;
1834 }
1835 rl_src = LoadValue(rl_src, kCoreReg);
1836 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001837 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001839 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001841 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001842 }
1843 StoreValue(rl_dest, rl_result);
1844}
1845
1846void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001847 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 RegLocation rl_result;
1849 OpKind first_op = kOpBkpt;
1850 OpKind second_op = kOpBkpt;
1851 bool call_out = false;
1852 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001853 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001854 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001855
1856 switch (opcode) {
1857 case Instruction::NOT_LONG:
1858 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1859 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1860 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001861 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1862 RegStorage t_reg = AllocTemp();
1863 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1864 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1865 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001866 FreeTemp(t_reg);
1867 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001868 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1869 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001870 }
1871 StoreValueWide(rl_dest, rl_result);
1872 return;
1873 case Instruction::ADD_LONG:
1874 case Instruction::ADD_LONG_2ADDR:
1875 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001876 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 return;
1878 }
1879 first_op = kOpAdd;
1880 second_op = kOpAdc;
1881 break;
1882 case Instruction::SUB_LONG:
1883 case Instruction::SUB_LONG_2ADDR:
1884 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001885 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 return;
1887 }
1888 first_op = kOpSub;
1889 second_op = kOpSbc;
1890 break;
1891 case Instruction::MUL_LONG:
1892 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001893 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001894 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 return;
1896 } else {
1897 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001898 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001899 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 }
1901 break;
1902 case Instruction::DIV_LONG:
1903 case Instruction::DIV_LONG_2ADDR:
1904 call_out = true;
1905 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001906 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001907 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 break;
1909 case Instruction::REM_LONG:
1910 case Instruction::REM_LONG_2ADDR:
1911 call_out = true;
1912 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001913 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001915 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001916 break;
1917 case Instruction::AND_LONG_2ADDR:
1918 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001919 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001920 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 }
1922 first_op = kOpAnd;
1923 second_op = kOpAnd;
1924 break;
1925 case Instruction::OR_LONG:
1926 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001927 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001928 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 return;
1930 }
1931 first_op = kOpOr;
1932 second_op = kOpOr;
1933 break;
1934 case Instruction::XOR_LONG:
1935 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001936 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001937 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 return;
1939 }
1940 first_op = kOpXor;
1941 second_op = kOpXor;
1942 break;
1943 case Instruction::NEG_LONG: {
1944 GenNegLong(rl_dest, rl_src2);
1945 return;
1946 }
1947 default:
1948 LOG(FATAL) << "Invalid long arith op";
1949 }
1950 if (!call_out) {
1951 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1952 } else {
1953 FlushAllRegs(); /* Send everything to home location */
1954 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001955 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1956 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1957 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1958 RegStorage r_tgt = CallHelperSetup(func_offset);
1959 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1960 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001961 // NOTE: callout here is not a safepoint
1962 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1963 } else {
1964 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1965 }
1966 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001967 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 rl_result = GetReturnWide(false);
1969 else
1970 rl_result = GetReturnWideAlt();
1971 StoreValueWide(rl_dest, rl_result);
1972 }
1973}
1974
Ian Rogersdd7624d2014-03-14 17:43:00 -07001975void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001976 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977 /*
1978 * Don't optimize the register usage since it calls out to support
1979 * functions
1980 */
1981 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1983 if (rl_dest.wide) {
1984 RegLocation rl_result;
1985 rl_result = GetReturnWide(rl_dest.fp);
1986 StoreValueWide(rl_dest, rl_result);
1987 } else {
1988 RegLocation rl_result;
1989 rl_result = GetReturn(rl_dest.fp);
1990 StoreValue(rl_dest, rl_result);
1991 }
1992}
1993
1994/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001995void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001996 if (Runtime::Current()->ExplicitSuspendChecks()) {
1997 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1998 return;
1999 }
2000 FlushAllRegs();
2001 LIR* branch = OpTestSuspend(NULL);
2002 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2003 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2004 current_dalvik_offset_);
2005 branch->target = target;
2006 suspend_launchpads_.Insert(target);
2007 } else {
2008 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2009 return;
2010 }
2011 FlushAllRegs(); // TODO: needed?
2012 LIR* inst = CheckSuspendUsingLoad();
2013 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015}
2016
2017/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002018void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002019 if (Runtime::Current()->ExplicitSuspendChecks()) {
2020 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2021 OpUnconditionalBranch(target);
2022 return;
2023 }
2024 OpTestSuspend(target);
2025 LIR* launch_pad =
2026 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2027 current_dalvik_offset_);
2028 FlushAllRegs();
2029 OpUnconditionalBranch(launch_pad);
2030 suspend_launchpads_.Insert(launch_pad);
2031 } else {
2032 // For the implicit suspend check, just perform the trigger
2033 // load and branch to the target.
2034 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2035 OpUnconditionalBranch(target);
2036 return;
2037 }
2038 FlushAllRegs();
2039 LIR* inst = CheckSuspendUsingLoad();
2040 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043}
2044
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002045/* Call out to helper assembly routine that will null check obj and then lock it. */
2046void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2047 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002048 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002049}
2050
2051/* Call out to helper assembly routine that will null check obj and then unlock it. */
2052void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2053 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002054 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002055}
2056
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002057/* Generic code for generating a wide constant into a VR. */
2058void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2059 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002060 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002061 StoreValueWide(rl_dest, rl_result);
2062}
2063
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064} // namespace art