blob: 73a123e575103b154c3c4f261beff4da8dda1ef5 [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
buzbee2700f7e2014-03-07 09:46:20 -080045LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
46 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 LIR* branch;
48 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080049 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
50 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 branch = OpUnconditionalBranch(tgt);
52 } else {
buzbee2700f7e2014-03-07 09:46:20 -080053 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
55 }
56 // Remember branch target - will process later
57 throw_launchpads_.Insert(tgt);
58 return branch;
59}
60
Mingyao Yang42894562014-04-07 12:42:16 -070061void Mir2Lir::AddDivZeroSlowPath(ConditionCode c_code) {
62 LIR* branch = OpCondBranch(c_code, nullptr);
63 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::AddDivZeroSlowPath(ConditionCode c_code, RegStorage reg, int imm_val) {
67 LIR* branch;
68 if (c_code == kCondAl) {
69 branch = OpUnconditionalBranch(nullptr);
70 } else {
71 branch = OpCmpImmBranch(c_code, reg, imm_val, nullptr);
72 }
73 AddDivZeroCheckSlowPath(branch);
74}
75
76void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
77 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
78 public:
79 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
80 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
81 }
82
83 void Compile() {
84 m2l_->ResetRegPool();
85 m2l_->ResetDefTracking();
86 GenerateTargetLabel();
87 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
88 }
89 };
90
91 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
92}
Dave Allisonb373e092014-02-20 16:06:36 -080093
Brian Carlstrom7940e442013-07-12 13:46:57 -070094/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -080095LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -080096 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -070097 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 }
Dave Allisonb373e092014-02-20 16:06:36 -080099 return nullptr;
100}
101
Dave Allisonf9439142014-03-27 15:10:22 -0700102/* Perform an explicit null-check on a register. */
103LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
104 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
105 return NULL;
106 }
107 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
108}
109
Dave Allisonb373e092014-02-20 16:06:36 -0800110void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
111 if (!Runtime::Current()->ExplicitNullChecks()) {
112 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
113 return;
114 }
115 MarkSafepointPC(last_lir_insn_);
116 }
117}
118
119void Mir2Lir::MarkPossibleStackOverflowException() {
120 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
121 MarkSafepointPC(last_lir_insn_);
122 }
123}
124
buzbee2700f7e2014-03-07 09:46:20 -0800125void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800126 if (!Runtime::Current()->ExplicitNullChecks()) {
127 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
128 return;
129 }
130 // Force an implicit null check by performing a memory operation (load) from the given
131 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800132 RegStorage tmp = AllocTemp();
133 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800134 LIR* load = LoadWordDisp(reg, 0, tmp);
135 FreeTemp(tmp);
136 MarkSafepointPC(load);
137 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138}
139
140/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800141LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700142 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800143 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
144 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
146 // Remember branch target - will process later
147 throw_launchpads_.Insert(tgt);
148 return branch;
149}
150
151void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
152 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700153 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 ConditionCode cond;
155 switch (opcode) {
156 case Instruction::IF_EQ:
157 cond = kCondEq;
158 break;
159 case Instruction::IF_NE:
160 cond = kCondNe;
161 break;
162 case Instruction::IF_LT:
163 cond = kCondLt;
164 break;
165 case Instruction::IF_GE:
166 cond = kCondGe;
167 break;
168 case Instruction::IF_GT:
169 cond = kCondGt;
170 break;
171 case Instruction::IF_LE:
172 cond = kCondLe;
173 break;
174 default:
175 cond = static_cast<ConditionCode>(0);
176 LOG(FATAL) << "Unexpected opcode " << opcode;
177 }
178
179 // Normalize such that if either operand is constant, src2 will be constant
180 if (rl_src1.is_const) {
181 RegLocation rl_temp = rl_src1;
182 rl_src1 = rl_src2;
183 rl_src2 = rl_temp;
184 cond = FlipComparisonOrder(cond);
185 }
186
187 rl_src1 = LoadValue(rl_src1, kCoreReg);
188 // Is this really an immediate comparison?
189 if (rl_src2.is_const) {
190 // If it's already live in a register or not easily materialized, just keep going
191 RegLocation rl_temp = UpdateLoc(rl_src2);
192 if ((rl_temp.location == kLocDalvikFrame) &&
193 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
194 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800195 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 return;
197 }
198 }
199 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800200 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201}
202
203void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700204 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 ConditionCode cond;
206 rl_src = LoadValue(rl_src, kCoreReg);
207 switch (opcode) {
208 case Instruction::IF_EQZ:
209 cond = kCondEq;
210 break;
211 case Instruction::IF_NEZ:
212 cond = kCondNe;
213 break;
214 case Instruction::IF_LTZ:
215 cond = kCondLt;
216 break;
217 case Instruction::IF_GEZ:
218 cond = kCondGe;
219 break;
220 case Instruction::IF_GTZ:
221 cond = kCondGt;
222 break;
223 case Instruction::IF_LEZ:
224 cond = kCondLe;
225 break;
226 default:
227 cond = static_cast<ConditionCode>(0);
228 LOG(FATAL) << "Unexpected opcode " << opcode;
229 }
buzbee2700f7e2014-03-07 09:46:20 -0800230 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231}
232
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700233void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
235 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800236 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800238 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 }
buzbee2700f7e2014-03-07 09:46:20 -0800240 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 StoreValueWide(rl_dest, rl_result);
242}
243
244void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700245 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700246 rl_src = LoadValue(rl_src, kCoreReg);
247 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
248 OpKind op = kOpInvalid;
249 switch (opcode) {
250 case Instruction::INT_TO_BYTE:
251 op = kOp2Byte;
252 break;
253 case Instruction::INT_TO_SHORT:
254 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700256 case Instruction::INT_TO_CHAR:
257 op = kOp2Char;
258 break;
259 default:
260 LOG(ERROR) << "Bad int conversion type";
261 }
buzbee2700f7e2014-03-07 09:46:20 -0800262 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700263 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264}
265
266/*
267 * Let helper function take care of everything. Will call
268 * Array::AllocFromCode(type_idx, method, count);
269 * Note: AllocFromCode will handle checks for errNegativeArraySize.
270 */
271void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700272 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700274 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800275 const DexFile* dex_file = cu_->dex_file;
276 CompilerDriver* driver = cu_->compiler_driver;
277 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800279 bool is_type_initialized; // Ignored as an array does not have an initializer.
280 bool use_direct_type_ptr;
281 uintptr_t direct_type_ptr;
282 if (kEmbedClassInCode &&
283 driver->CanEmbedTypeInCode(*dex_file, type_idx,
284 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
285 // The fast path.
286 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800287 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700288 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800289 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
290 } else {
291 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700292 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800293 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
294 }
295 } else {
296 // The slow path.
297 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700298 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800299 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
300 }
301 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700303 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800304 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 RegLocation rl_result = GetReturn(false);
307 StoreValue(rl_dest, rl_result);
308}
309
310/*
311 * Similar to GenNewArray, but with post-allocation initialization.
312 * Verifier guarantees we're dealing with an array class. Current
313 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
314 * Current code also throws internal unimp if not 'L', '[' or 'I'.
315 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700316void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 int elems = info->num_arg_words;
318 int type_idx = info->index;
319 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700320 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
322 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700323 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700325 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
327 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
328 FreeTemp(TargetReg(kArg2));
329 FreeTemp(TargetReg(kArg1));
330 /*
331 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
332 * return region. Because AllocFromCode placed the new array
333 * in kRet0, we'll just lock it into place. When debugger support is
334 * added, it may be necessary to additionally copy all return
335 * values to a home location in thread-local storage
336 */
337 LockTemp(TargetReg(kRet0));
338
339 // TODO: use the correct component size, currently all supported types
340 // share array alignment with ints (see comment at head of function)
341 size_t component_size = sizeof(int32_t);
342
343 // Having a range of 0 is legal
344 if (info->is_range && (elems > 0)) {
345 /*
346 * Bit of ugliness here. We're going generate a mem copy loop
347 * on the register range, but it is possible that some regs
348 * in the range have been promoted. This is unlikely, but
349 * before generating the copy, we'll just force a flush
350 * of any regs in the source range that have been promoted to
351 * home location.
352 */
353 for (int i = 0; i < elems; i++) {
354 RegLocation loc = UpdateLoc(info->args[i]);
355 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800356 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 }
358 }
359 /*
360 * TUNING note: generated code here could be much improved, but
361 * this is an uncommon operation and isn't especially performance
362 * critical.
363 */
buzbee2700f7e2014-03-07 09:46:20 -0800364 RegStorage r_src = AllocTemp();
365 RegStorage r_dst = AllocTemp();
366 RegStorage r_idx = AllocTemp();
367 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700368 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 case kThumb2:
370 r_val = TargetReg(kLr);
371 break;
372 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700373 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 FreeTemp(TargetReg(kRet0));
375 r_val = AllocTemp();
376 break;
377 case kMips:
378 r_val = AllocTemp();
379 break;
380 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
381 }
382 // Set up source pointer
383 RegLocation rl_first = info->args[0];
384 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
385 // Set up the target pointer
386 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
387 mirror::Array::DataOffset(component_size).Int32Value());
388 // Set up the loop counter (known to be > 0)
389 LoadConstant(r_idx, elems - 1);
390 // Generate the copy loop. Going backwards for convenience
391 LIR* target = NewLIR0(kPseudoTargetLabel);
392 // Copy next element
393 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
394 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
395 FreeTemp(r_val);
396 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700397 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 // Restore the target pointer
399 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
400 -mirror::Array::DataOffset(component_size).Int32Value());
401 }
402 } else if (!info->is_range) {
403 // TUNING: interleave
404 for (int i = 0; i < elems; i++) {
405 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
406 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800407 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
408 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800410 if (IsTemp(rl_arg.reg)) {
411 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 }
413 }
414 }
415 if (info->result.location != kLocInvalid) {
416 StoreValue(info->result, GetReturn(false /* not fp */));
417 }
418}
419
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800420//
421// Slow path to ensure a class is initialized for sget/sput.
422//
423class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
424 public:
buzbee2700f7e2014-03-07 09:46:20 -0800425 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
426 RegStorage r_base) :
427 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
428 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800429 }
430
431 void Compile() {
432 LIR* unresolved_target = GenerateTargetLabel();
433 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700434 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800435 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800436 // Copy helper's result into r_base, a no-op on all but MIPS.
437 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
438
439 m2l_->OpUnconditionalBranch(cont_);
440 }
441
442 private:
443 LIR* const uninit_;
444 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800445 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800446};
447
Vladimir Markobe0e5462014-02-26 11:24:15 +0000448void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700449 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000450 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
451 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
452 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
453 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800454 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000455 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 // Fast path, static storage base is this method's class
457 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800458 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800459 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
460 if (IsTemp(rl_method.reg)) {
461 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
463 } else {
464 // Medium path, static storage base in a different class which requires checks that the other
465 // class is initialized.
466 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000467 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 // May do runtime call so everything to home locations.
469 FlushAllRegs();
470 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800471 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 LockTemp(r_method);
473 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800474 r_base = TargetReg(kArg0);
475 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800476 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800477 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000478 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800479 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000480 if (!field_info.IsInitialized() &&
481 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800482 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800483
484 // The slow path is invoked if the r_base is NULL or the class pointed
485 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800486 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800487 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800488 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800489 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800490 mirror::Class::StatusOffset().Int32Value(),
491 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800492 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800493
buzbee2700f7e2014-03-07 09:46:20 -0800494 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000495 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800496
497 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 FreeTemp(r_method);
500 }
501 // rBase now holds static storage base
502 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700503 RegisterClass register_kind = kAnyReg;
504 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
505 // Force long/double volatile stores into SSE registers to avoid tearing.
506 register_kind = kFPReg;
507 }
508 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 } else {
510 rl_src = LoadValue(rl_src, kAnyReg);
511 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000512 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800513 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 GenMemBarrier(kStoreStore);
515 }
516 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800517 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800519 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000521 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800522 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 GenMemBarrier(kStoreLoad);
524 }
525 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800526 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800528 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 } else {
530 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700531 ThreadOffset<4> setter_offset =
532 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
533 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
534 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000535 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 }
537}
538
Vladimir Markobe0e5462014-02-26 11:24:15 +0000539void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700540 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000541 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
542 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
543 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
544 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800545 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000546 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 // Fast path, static storage base is this method's class
548 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800549 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800550 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 } else {
552 // Medium path, static storage base in a different class which requires checks that the other
553 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 // May do runtime call so everything to home locations.
556 FlushAllRegs();
557 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800558 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 LockTemp(r_method);
560 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 r_base = TargetReg(kArg0);
562 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800563 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800564 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000565 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800566 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000567 if (!field_info.IsInitialized() &&
568 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800570
571 // The slow path is invoked if the r_base is NULL or the class pointed
572 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800573 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800574 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800575 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800576 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800577 mirror::Class::StatusOffset().Int32Value(),
578 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800579 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800580
buzbee2700f7e2014-03-07 09:46:20 -0800581 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000582 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800583
584 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 FreeTemp(r_method);
587 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800588 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700589 RegisterClass result_reg_kind = kAnyReg;
590 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
591 // Force long/double volatile loads into SSE registers to avoid tearing.
592 result_reg_kind = kFPReg;
593 }
594 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800595
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800597 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800599 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800602
603 if (field_info.IsVolatile()) {
604 // Without context sensitive analysis, we must issue the most conservative barriers.
605 // In this case, either a load or store may follow so we issue both barriers.
606 GenMemBarrier(kLoadLoad);
607 GenMemBarrier(kLoadStore);
608 }
609
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 if (is_long_or_double) {
611 StoreValueWide(rl_dest, rl_result);
612 } else {
613 StoreValue(rl_dest, rl_result);
614 }
615 } else {
616 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700617 ThreadOffset<4> getterOffset =
618 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
619 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
620 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000621 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 if (is_long_or_double) {
623 RegLocation rl_result = GetReturnWide(rl_dest.fp);
624 StoreValueWide(rl_dest, rl_result);
625 } else {
626 RegLocation rl_result = GetReturn(rl_dest.fp);
627 StoreValue(rl_dest, rl_result);
628 }
629 }
630}
631
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800632// Generate code for all slow paths.
633void Mir2Lir::HandleSlowPaths() {
634 int n = slow_paths_.Size();
635 for (int i = 0; i < n; ++i) {
636 LIRSlowPath* slowpath = slow_paths_.Get(i);
637 slowpath->Compile();
638 }
639 slow_paths_.Reset();
640}
641
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700642void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700644 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 for (int i = 0; i < num_elems; i++) {
646 ResetRegPool();
647 ResetDefTracking();
648 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700649 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 current_dalvik_offset_ = lab->operands[1];
651 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800652 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
654 OpUnconditionalBranch(resume_lab);
655 }
656}
657
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700658void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 int num_elems = throw_launchpads_.Size();
660 for (int i = 0; i < num_elems; i++) {
661 ResetRegPool();
662 ResetDefTracking();
663 LIR* lab = throw_launchpads_.Get(i);
664 current_dalvik_offset_ = lab->operands[1];
665 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700666 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 int v1 = lab->operands[2];
668 int v2 = lab->operands[3];
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700669 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 switch (lab->operands[0]) {
671 case kThrowNullPointer:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700672 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700674 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
676 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800677 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
678 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800680 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 }
682 // Make sure the following LoadConstant doesn't mess with kArg1.
683 LockTemp(TargetReg(kArg1));
684 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700685 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 break;
687 case kThrowArrayBounds:
688 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800689 if (v2 != TargetReg(kArg0).GetReg()) {
690 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 if (target_x86) {
692 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800693 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
694 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800696 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
698 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800699 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800701 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 if (target_x86) {
703 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800704 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
705 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800707 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
709 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
710 } else {
711 if (target_x86) {
712 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800713 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
714 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800716 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 }
buzbee2700f7e2014-03-07 09:46:20 -0800718 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 }
720 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700721 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800724 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700726 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 default:
729 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
730 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000731 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800732 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700733 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
735}
736
Vladimir Markobe0e5462014-02-26 11:24:15 +0000737void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700739 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000740 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
741 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
742 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 RegLocation rl_result;
744 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000745 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 rl_obj = LoadValue(rl_obj, kCoreReg);
747 if (is_long_or_double) {
748 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800749 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700750 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700751 RegisterClass result_reg_kind = kAnyReg;
752 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
753 // Force long/double volatile loads into SSE registers to avoid tearing.
754 result_reg_kind = kFPReg;
755 }
756 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800757 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
758 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800759 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000760 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800761 // Without context sensitive analysis, we must issue the most conservative barriers.
762 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800764 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 }
766 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800767 RegStorage reg_ptr = AllocTemp();
768 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800770 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700771 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000772 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800773 // Without context sensitive analysis, we must issue the most conservative barriers.
774 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800776 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 }
778 FreeTemp(reg_ptr);
779 }
780 StoreValueWide(rl_dest, rl_result);
781 } else {
782 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800783 GenNullCheck(rl_obj.reg, opt_flags);
784 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
785 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800786 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800788 // Without context sensitive analysis, we must issue the most conservative barriers.
789 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800791 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 }
793 StoreValue(rl_dest, rl_result);
794 }
795 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700796 ThreadOffset<4> getterOffset =
797 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
798 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
799 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000800 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 if (is_long_or_double) {
802 RegLocation rl_result = GetReturnWide(rl_dest.fp);
803 StoreValueWide(rl_dest, rl_result);
804 } else {
805 RegLocation rl_result = GetReturn(rl_dest.fp);
806 StoreValue(rl_dest, rl_result);
807 }
808 }
809}
810
Vladimir Markobe0e5462014-02-26 11:24:15 +0000811void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700813 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000814 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
815 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
816 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000818 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 rl_obj = LoadValue(rl_obj, kCoreReg);
820 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700821 RegisterClass src_reg_kind = kAnyReg;
822 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
823 // Force long/double volatile stores into SSE registers to avoid tearing.
824 src_reg_kind = kFPReg;
825 }
826 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800827 GenNullCheck(rl_obj.reg, opt_flags);
828 RegStorage reg_ptr = AllocTemp();
829 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000830 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800831 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 GenMemBarrier(kStoreStore);
833 }
buzbee2700f7e2014-03-07 09:46:20 -0800834 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800835 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000836 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800837 // A load might follow the volatile store so insert a StoreLoad barrier.
838 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 }
840 FreeTemp(reg_ptr);
841 } else {
842 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800843 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000844 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800845 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 GenMemBarrier(kStoreStore);
847 }
buzbee2700f7e2014-03-07 09:46:20 -0800848 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800849 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000850 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800851 // A load might follow the volatile store so insert a StoreLoad barrier.
852 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 }
854 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800855 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 }
857 }
858 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700859 ThreadOffset<4> setter_offset =
860 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
861 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
862 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000863 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
864 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 }
866}
867
Ian Rogersa9a82542013-10-04 11:17:26 -0700868void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
869 RegLocation rl_src) {
870 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
871 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
872 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700873 ThreadOffset<4> helper = needs_range_check
874 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
875 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
876 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700877 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
878}
879
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700880void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800882 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
884 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
885 *cu_->dex_file,
886 type_idx)) {
887 // Call out to helper which resolves type and verifies access.
888 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700889 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800890 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 RegLocation rl_result = GetReturn(false);
892 StoreValue(rl_dest, rl_result);
893 } else {
894 // We're don't need access checks, load type from dex cache
895 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700896 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800897 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 int32_t offset_of_type =
899 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
900 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800901 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
903 type_idx) || SLOW_TYPE_PATH) {
904 // Slow path, at runtime test if type is null and if so initialize
905 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800906 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800907 LIR* cont = NewLIR0(kPseudoTargetLabel);
908
909 // Object to generate the slow path for class resolution.
910 class SlowPath : public LIRSlowPath {
911 public:
912 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
913 const RegLocation& rl_method, const RegLocation& rl_result) :
914 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
915 rl_method_(rl_method), rl_result_(rl_result) {
916 }
917
918 void Compile() {
919 GenerateTargetLabel();
920
Ian Rogersdd7624d2014-03-14 17:43:00 -0700921 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800922 rl_method_.reg, true);
923 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800924
925 m2l_->OpUnconditionalBranch(cont_);
926 }
927
928 private:
929 const int type_idx_;
930 const RegLocation rl_method_;
931 const RegLocation rl_result_;
932 };
933
934 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800935 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800938 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 // Fast path, we're done - just store result
940 StoreValue(rl_dest, rl_result);
941 }
942 }
943}
944
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700945void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 /* NOTE: Most strings should be available at compile time */
947 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
948 (sizeof(mirror::String*) * string_idx);
949 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
950 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
951 // slow path, resolve string if not in dex cache
952 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700953 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800954
955 // If the Method* is already in a register, we can save a copy.
956 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800957 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800958 if (rl_method.location == kLocPhysReg) {
959 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800960 DCHECK(!IsTemp(rl_method.reg));
961 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800962 } else {
963 r_method = TargetReg(kArg2);
964 LoadCurrMethodDirect(r_method);
965 }
966 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
967 TargetReg(kArg0));
968
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800971 if (cu_->instruction_set == kThumb2 ||
972 cu_->instruction_set == kMips) {
973 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800974 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800975 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
976 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800978
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800979 // Object to generate the slow path for string resolution.
980 class SlowPath : public LIRSlowPath {
981 public:
buzbee2700f7e2014-03-07 09:46:20 -0800982 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800983 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
984 }
985
986 void Compile() {
987 GenerateTargetLabel();
988
Dave Allisond6ed6422014-04-09 23:36:15 +0000989 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800990
Dave Allisond6ed6422014-04-09 23:36:15 +0000991 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
992 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
993 m2l_->MarkSafepointPC(call_inst);
994 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800995
996 m2l_->OpUnconditionalBranch(cont_);
997 }
998
999 private:
buzbee2700f7e2014-03-07 09:46:20 -08001000 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001001 };
1002
1003 // Add to list for future.
1004 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001006 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001007 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1008 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001009 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001010 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001011 LIR* target = NewLIR0(kPseudoTargetLabel);
1012 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
1014 GenBarrier();
1015 StoreValue(rl_dest, GetReturn(false));
1016 } else {
1017 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001018 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001020 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1021 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 StoreValue(rl_dest, rl_result);
1023 }
1024}
1025
1026/*
1027 * Let helper function take care of everything. Will
1028 * call Class::NewInstanceFromCode(type_idx, method);
1029 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001030void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 FlushAllRegs(); /* Everything to home location */
1032 // alloc will always check for resolution, do we also need to verify
1033 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001034 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001035 const DexFile* dex_file = cu_->dex_file;
1036 CompilerDriver* driver = cu_->compiler_driver;
1037 if (driver->CanAccessInstantiableTypeWithoutChecks(
1038 cu_->method_idx, *dex_file, type_idx)) {
1039 bool is_type_initialized;
1040 bool use_direct_type_ptr;
1041 uintptr_t direct_type_ptr;
1042 if (kEmbedClassInCode &&
1043 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1044 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1045 // The fast path.
1046 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001047 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001048 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001049 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001050 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1051 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001052 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1054 }
1055 } else {
1056 // Use the direct pointer.
1057 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001058 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1060 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001061 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1063 }
1064 }
1065 } else {
1066 // The slow path.
1067 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001068 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1070 }
1071 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001073 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001074 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 RegLocation rl_result = GetReturn(false);
1077 StoreValue(rl_dest, rl_result);
1078}
1079
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001080void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001082 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083}
1084
1085// For final classes there are no sub-classes to check and so we can answer the instance-of
1086// question with simple comparisons.
1087void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1088 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001089 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001090 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001091
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 RegLocation object = LoadValue(rl_src, kCoreReg);
1093 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001094 RegStorage result_reg = rl_result.reg;
1095 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 result_reg = AllocTypedTemp(false, kCoreReg);
1097 }
1098 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001099 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100
buzbee2700f7e2014-03-07 09:46:20 -08001101 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1102 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103
1104 LoadCurrMethodDirect(check_class);
1105 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001106 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1107 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001109 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001111 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 int32_t offset_of_type =
1113 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1114 (sizeof(mirror::Class*) * type_idx);
1115 LoadWordDisp(check_class, offset_of_type, check_class);
1116 }
1117
1118 LIR* ne_branchover = NULL;
1119 if (cu_->instruction_set == kThumb2) {
1120 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001121 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001123 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 } else {
1125 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1126 LoadConstant(result_reg, 1); // eq case - load true
1127 }
1128 LIR* target = NewLIR0(kPseudoTargetLabel);
1129 null_branchover->target = target;
1130 if (ne_branchover != NULL) {
1131 ne_branchover->target = target;
1132 }
1133 FreeTemp(object_class);
1134 FreeTemp(check_class);
1135 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001136 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 FreeTemp(result_reg);
1138 }
1139 StoreValue(rl_dest, rl_result);
1140}
1141
1142void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1143 bool type_known_abstract, bool use_declaring_class,
1144 bool can_assume_type_is_in_dex_cache,
1145 uint32_t type_idx, RegLocation rl_dest,
1146 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001147 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001148 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001149
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 FlushAllRegs();
1151 // May generate a call - use explicit registers
1152 LockCallTemps();
1153 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001154 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 if (needs_access_check) {
1156 // Check we have access to type_idx and if not throw IllegalAccessError,
1157 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001158 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 type_idx, true);
1160 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1161 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1162 } else if (use_declaring_class) {
1163 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001164 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1165 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 } else {
1167 // Load dex cache entry into class_reg (kArg2)
1168 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001169 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1170 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 int32_t offset_of_type =
1172 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1173 * type_idx);
1174 LoadWordDisp(class_reg, offset_of_type, class_reg);
1175 if (!can_assume_type_is_in_dex_cache) {
1176 // Need to test presence of type in dex cache at runtime
1177 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1178 // Not resolved
1179 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001180 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001181 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1183 // Rejoin code paths
1184 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1185 hop_branch->target = hop_target;
1186 }
1187 }
1188 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1189 RegLocation rl_result = GetReturn(false);
1190 if (cu_->instruction_set == kMips) {
1191 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001192 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 }
1194 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1195
1196 /* load object->klass_ */
1197 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1198 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1199 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1200 LIR* branchover = NULL;
1201 if (type_known_final) {
1202 // rl_result == ref == null == 0.
1203 if (cu_->instruction_set == kThumb2) {
1204 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001205 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001206 LoadConstant(rl_result.reg, 1); // .eq case - load true
1207 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001208 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001210 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001212 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 }
1214 } else {
1215 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001216 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001217 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 if (!type_known_abstract) {
1219 /* Uses conditional nullification */
1220 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001221 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1223 }
1224 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1225 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001226 if (it != nullptr) {
1227 OpEndIT(it);
1228 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 FreeTemp(r_tgt);
1230 } else {
1231 if (!type_known_abstract) {
1232 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001233 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1235 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001236 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001237 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1238 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1239 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 }
1241 }
1242 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001243 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 /* branch targets here */
1245 LIR* target = NewLIR0(kPseudoTargetLabel);
1246 StoreValue(rl_dest, rl_result);
1247 branch1->target = target;
1248 if (branchover != NULL) {
1249 branchover->target = target;
1250 }
1251}
1252
1253void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1254 bool type_known_final, type_known_abstract, use_declaring_class;
1255 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1256 *cu_->dex_file,
1257 type_idx,
1258 &type_known_final,
1259 &type_known_abstract,
1260 &use_declaring_class);
1261 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1262 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1263
1264 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1265 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1266 } else {
1267 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1268 use_declaring_class, can_assume_type_is_in_dex_cache,
1269 type_idx, rl_dest, rl_src);
1270 }
1271}
1272
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001273void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 bool type_known_final, type_known_abstract, use_declaring_class;
1275 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1276 *cu_->dex_file,
1277 type_idx,
1278 &type_known_final,
1279 &type_known_abstract,
1280 &use_declaring_class);
1281 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1282 // of the exception throw path.
1283 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001284 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 // Verifier type analysis proved this check cast would never cause an exception.
1286 return;
1287 }
1288 FlushAllRegs();
1289 // May generate a call - use explicit registers
1290 LockCallTemps();
1291 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001292 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 if (needs_access_check) {
1294 // Check we have access to type_idx and if not throw IllegalAccessError,
1295 // returns Class* in kRet0
1296 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001297 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 type_idx, TargetReg(kArg1), true);
1299 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1300 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001301 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1302 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 } else {
1304 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001305 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1306 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 int32_t offset_of_type =
1308 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1309 (sizeof(mirror::Class*) * type_idx);
1310 LoadWordDisp(class_reg, offset_of_type, class_reg);
1311 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1312 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001313 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1314 LIR* cont = NewLIR0(kPseudoTargetLabel);
1315
1316 // Slow path to initialize the type. Executed if the type is NULL.
1317 class SlowPath : public LIRSlowPath {
1318 public:
1319 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001320 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001321 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1322 class_reg_(class_reg) {
1323 }
1324
1325 void Compile() {
1326 GenerateTargetLabel();
1327
1328 // Call out to helper, which will return resolved type in kArg0
1329 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001330 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001331 m2l_->TargetReg(kArg1), true);
1332 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1333 m2l_->OpUnconditionalBranch(cont_);
1334 }
1335 public:
1336 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001337 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001338 };
1339
buzbee2700f7e2014-03-07 09:46:20 -08001340 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 }
1342 }
1343 // At this point, class_reg (kArg2) has class
1344 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001345
1346 // Slow path for the case where the classes are not equal. In this case we need
1347 // to call a helper function to do the check.
1348 class SlowPath : public LIRSlowPath {
1349 public:
1350 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1351 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1352 }
1353
1354 void Compile() {
1355 GenerateTargetLabel();
1356
1357 if (load_) {
1358 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1359 m2l_->TargetReg(kArg1));
1360 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001361 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001362 m2l_->TargetReg(kArg1), true);
1363
1364 m2l_->OpUnconditionalBranch(cont_);
1365 }
1366
1367 private:
1368 bool load_;
1369 };
1370
1371 if (type_known_abstract) {
1372 // Easier case, run slow path if target is non-null (slow path will load from target)
1373 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1374 LIR* cont = NewLIR0(kPseudoTargetLabel);
1375 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1376 } else {
1377 // Harder, more common case. We need to generate a forward branch over the load
1378 // if the target is null. If it's non-null we perform the load and branch to the
1379 // slow path if the classes are not equal.
1380
1381 /* Null is OK - continue */
1382 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1383 /* load object->klass_ */
1384 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001385 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001386
1387 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1388 LIR* cont = NewLIR0(kPseudoTargetLabel);
1389
1390 // Add the slow path that will not perform load since this is already done.
1391 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1392
1393 // Set the null check to branch to the continuation.
1394 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 }
1396}
1397
1398void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001399 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 RegLocation rl_result;
1401 if (cu_->instruction_set == kThumb2) {
1402 /*
1403 * NOTE: This is the one place in the code in which we might have
1404 * as many as six live temporary registers. There are 5 in the normal
1405 * set for Arm. Until we have spill capabilities, temporarily add
1406 * lr to the temp set. It is safe to do this locally, but note that
1407 * lr is used explicitly elsewhere in the code generator and cannot
1408 * normally be used as a general temp register.
1409 */
1410 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1411 FreeTemp(TargetReg(kLr)); // and make it available
1412 }
1413 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1414 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1415 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1416 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001417 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1418 RegStorage t_reg = AllocTemp();
1419 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1420 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1421 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 FreeTemp(t_reg);
1423 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001424 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1425 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 }
1427 /*
1428 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1429 * following StoreValueWide might need to allocate a temp register.
1430 * To further work around the lack of a spill capability, explicitly
1431 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1432 * Remove when spill is functional.
1433 */
1434 FreeRegLocTemps(rl_result, rl_src1);
1435 FreeRegLocTemps(rl_result, rl_src2);
1436 StoreValueWide(rl_dest, rl_result);
1437 if (cu_->instruction_set == kThumb2) {
1438 Clobber(TargetReg(kLr));
1439 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1440 }
1441}
1442
1443
1444void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001445 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001446 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447
1448 switch (opcode) {
1449 case Instruction::SHL_LONG:
1450 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001451 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 break;
1453 case Instruction::SHR_LONG:
1454 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001455 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 break;
1457 case Instruction::USHR_LONG:
1458 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001459 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 break;
1461 default:
1462 LOG(FATAL) << "Unexpected case";
1463 }
1464 FlushAllRegs(); /* Send everything to home location */
1465 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1466 RegLocation rl_result = GetReturnWide(false);
1467 StoreValueWide(rl_dest, rl_result);
1468}
1469
1470
1471void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001472 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001473 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 OpKind op = kOpBkpt;
1475 bool is_div_rem = false;
1476 bool check_zero = false;
1477 bool unary = false;
1478 RegLocation rl_result;
1479 bool shift_op = false;
1480 switch (opcode) {
1481 case Instruction::NEG_INT:
1482 op = kOpNeg;
1483 unary = true;
1484 break;
1485 case Instruction::NOT_INT:
1486 op = kOpMvn;
1487 unary = true;
1488 break;
1489 case Instruction::ADD_INT:
1490 case Instruction::ADD_INT_2ADDR:
1491 op = kOpAdd;
1492 break;
1493 case Instruction::SUB_INT:
1494 case Instruction::SUB_INT_2ADDR:
1495 op = kOpSub;
1496 break;
1497 case Instruction::MUL_INT:
1498 case Instruction::MUL_INT_2ADDR:
1499 op = kOpMul;
1500 break;
1501 case Instruction::DIV_INT:
1502 case Instruction::DIV_INT_2ADDR:
1503 check_zero = true;
1504 op = kOpDiv;
1505 is_div_rem = true;
1506 break;
1507 /* NOTE: returns in kArg1 */
1508 case Instruction::REM_INT:
1509 case Instruction::REM_INT_2ADDR:
1510 check_zero = true;
1511 op = kOpRem;
1512 is_div_rem = true;
1513 break;
1514 case Instruction::AND_INT:
1515 case Instruction::AND_INT_2ADDR:
1516 op = kOpAnd;
1517 break;
1518 case Instruction::OR_INT:
1519 case Instruction::OR_INT_2ADDR:
1520 op = kOpOr;
1521 break;
1522 case Instruction::XOR_INT:
1523 case Instruction::XOR_INT_2ADDR:
1524 op = kOpXor;
1525 break;
1526 case Instruction::SHL_INT:
1527 case Instruction::SHL_INT_2ADDR:
1528 shift_op = true;
1529 op = kOpLsl;
1530 break;
1531 case Instruction::SHR_INT:
1532 case Instruction::SHR_INT_2ADDR:
1533 shift_op = true;
1534 op = kOpAsr;
1535 break;
1536 case Instruction::USHR_INT:
1537 case Instruction::USHR_INT_2ADDR:
1538 shift_op = true;
1539 op = kOpLsr;
1540 break;
1541 default:
1542 LOG(FATAL) << "Invalid word arith op: " << opcode;
1543 }
1544 if (!is_div_rem) {
1545 if (unary) {
1546 rl_src1 = LoadValue(rl_src1, kCoreReg);
1547 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001548 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 } else {
1550 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001551 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001552 RegStorage t_reg = AllocTemp();
1553 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 rl_src1 = LoadValue(rl_src1, kCoreReg);
1555 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001556 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 FreeTemp(t_reg);
1558 } else {
1559 rl_src1 = LoadValue(rl_src1, kCoreReg);
1560 rl_src2 = LoadValue(rl_src2, kCoreReg);
1561 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001562 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 }
1564 }
1565 StoreValue(rl_dest, rl_result);
1566 } else {
Dave Allison70202782013-10-22 17:52:19 -07001567 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 -07001568 if (cu_->instruction_set == kMips) {
1569 rl_src1 = LoadValue(rl_src1, kCoreReg);
1570 rl_src2 = LoadValue(rl_src2, kCoreReg);
1571 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001572 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 }
buzbee2700f7e2014-03-07 09:46:20 -08001574 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001575 done = true;
1576 } else if (cu_->instruction_set == kThumb2) {
1577 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1578 // Use ARM SDIV instruction for division. For remainder we also need to
1579 // calculate using a MUL and subtract.
1580 rl_src1 = LoadValue(rl_src1, kCoreReg);
1581 rl_src2 = LoadValue(rl_src2, kCoreReg);
1582 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001583 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Dave Allison70202782013-10-22 17:52:19 -07001584 }
buzbee2700f7e2014-03-07 09:46:20 -08001585 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001586 done = true;
1587 }
1588 }
1589
1590 // If we haven't already generated the code use the callout function.
1591 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001592 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 FlushAllRegs(); /* Send everything to home location */
1594 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001595 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1597 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001598 AddDivZeroSlowPath(kCondEq, TargetReg(kArg1), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 }
Dave Allison70202782013-10-22 17:52:19 -07001600 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001601 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 if (op == kOpDiv)
1603 rl_result = GetReturn(false);
1604 else
1605 rl_result = GetReturnAlt();
1606 }
1607 StoreValue(rl_dest, rl_result);
1608 }
1609}
1610
1611/*
1612 * The following are the first-level codegen routines that analyze the format
1613 * of each bytecode then either dispatch special purpose codegen routines
1614 * or produce corresponding Thumb instructions directly.
1615 */
1616
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001618static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 x &= x - 1;
1620 return (x & (x - 1)) == 0;
1621}
1622
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1624// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001625bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001626 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1628 return false;
1629 }
1630 // No divide instruction for Arm, so check for more special cases
1631 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001632 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 }
1634 int k = LowestSetBit(lit);
1635 if (k >= 30) {
1636 // Avoid special cases.
1637 return false;
1638 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 rl_src = LoadValue(rl_src, kCoreReg);
1640 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001641 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001642 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 if (lit == 2) {
1644 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001645 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1646 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1647 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001649 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001651 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1652 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
1654 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001655 RegStorage t_reg1 = AllocTemp();
1656 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001657 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001658 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1659 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001663 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001665 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001667 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 }
1669 }
1670 StoreValue(rl_dest, rl_result);
1671 return true;
1672}
1673
1674// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1675// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001676bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001677 if (lit < 0) {
1678 return false;
1679 }
1680 if (lit == 0) {
1681 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1682 LoadConstant(rl_result.reg, 0);
1683 StoreValue(rl_dest, rl_result);
1684 return true;
1685 }
1686 if (lit == 1) {
1687 rl_src = LoadValue(rl_src, kCoreReg);
1688 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1689 OpRegCopy(rl_result.reg, rl_src.reg);
1690 StoreValue(rl_dest, rl_result);
1691 return true;
1692 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001693 // There is RegRegRegShift on Arm, so check for more special cases
1694 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001695 return EasyMultiply(rl_src, rl_dest, lit);
1696 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697 // Can we simplify this multiplication?
1698 bool power_of_two = false;
1699 bool pop_count_le2 = false;
1700 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001701 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 power_of_two = true;
1703 } else if (IsPopCountLE2(lit)) {
1704 pop_count_le2 = true;
1705 } else if (IsPowerOfTwo(lit + 1)) {
1706 power_of_two_minus_one = true;
1707 } else {
1708 return false;
1709 }
1710 rl_src = LoadValue(rl_src, kCoreReg);
1711 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1712 if (power_of_two) {
1713 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001714 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 } else if (pop_count_le2) {
1716 // Shift and add and shift.
1717 int first_bit = LowestSetBit(lit);
1718 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1719 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1720 } else {
1721 // Reverse subtract: (src << (shift + 1)) - src.
1722 DCHECK(power_of_two_minus_one);
1723 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001724 RegStorage t_reg = AllocTemp();
1725 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1726 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 }
1728 StoreValue(rl_dest, rl_result);
1729 return true;
1730}
1731
1732void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001733 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 RegLocation rl_result;
1735 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1736 int shift_op = false;
1737 bool is_div = false;
1738
1739 switch (opcode) {
1740 case Instruction::RSUB_INT_LIT8:
1741 case Instruction::RSUB_INT: {
1742 rl_src = LoadValue(rl_src, kCoreReg);
1743 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1744 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001745 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001747 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1748 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 }
1750 StoreValue(rl_dest, rl_result);
1751 return;
1752 }
1753
1754 case Instruction::SUB_INT:
1755 case Instruction::SUB_INT_2ADDR:
1756 lit = -lit;
1757 // Intended fallthrough
1758 case Instruction::ADD_INT:
1759 case Instruction::ADD_INT_2ADDR:
1760 case Instruction::ADD_INT_LIT8:
1761 case Instruction::ADD_INT_LIT16:
1762 op = kOpAdd;
1763 break;
1764 case Instruction::MUL_INT:
1765 case Instruction::MUL_INT_2ADDR:
1766 case Instruction::MUL_INT_LIT8:
1767 case Instruction::MUL_INT_LIT16: {
1768 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1769 return;
1770 }
1771 op = kOpMul;
1772 break;
1773 }
1774 case Instruction::AND_INT:
1775 case Instruction::AND_INT_2ADDR:
1776 case Instruction::AND_INT_LIT8:
1777 case Instruction::AND_INT_LIT16:
1778 op = kOpAnd;
1779 break;
1780 case Instruction::OR_INT:
1781 case Instruction::OR_INT_2ADDR:
1782 case Instruction::OR_INT_LIT8:
1783 case Instruction::OR_INT_LIT16:
1784 op = kOpOr;
1785 break;
1786 case Instruction::XOR_INT:
1787 case Instruction::XOR_INT_2ADDR:
1788 case Instruction::XOR_INT_LIT8:
1789 case Instruction::XOR_INT_LIT16:
1790 op = kOpXor;
1791 break;
1792 case Instruction::SHL_INT_LIT8:
1793 case Instruction::SHL_INT:
1794 case Instruction::SHL_INT_2ADDR:
1795 lit &= 31;
1796 shift_op = true;
1797 op = kOpLsl;
1798 break;
1799 case Instruction::SHR_INT_LIT8:
1800 case Instruction::SHR_INT:
1801 case Instruction::SHR_INT_2ADDR:
1802 lit &= 31;
1803 shift_op = true;
1804 op = kOpAsr;
1805 break;
1806 case Instruction::USHR_INT_LIT8:
1807 case Instruction::USHR_INT:
1808 case Instruction::USHR_INT_2ADDR:
1809 lit &= 31;
1810 shift_op = true;
1811 op = kOpLsr;
1812 break;
1813
1814 case Instruction::DIV_INT:
1815 case Instruction::DIV_INT_2ADDR:
1816 case Instruction::DIV_INT_LIT8:
1817 case Instruction::DIV_INT_LIT16:
1818 case Instruction::REM_INT:
1819 case Instruction::REM_INT_2ADDR:
1820 case Instruction::REM_INT_LIT8:
1821 case Instruction::REM_INT_LIT16: {
1822 if (lit == 0) {
Mingyao Yang42894562014-04-07 12:42:16 -07001823 AddDivZeroSlowPath(kCondAl, RegStorage::InvalidReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001824 return;
1825 }
buzbee11b63d12013-08-27 07:34:17 -07001826 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001828 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 (opcode == Instruction::DIV_INT_LIT16)) {
1830 is_div = true;
1831 } else {
1832 is_div = false;
1833 }
buzbee11b63d12013-08-27 07:34:17 -07001834 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1835 return;
1836 }
Dave Allison70202782013-10-22 17:52:19 -07001837
1838 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 if (cu_->instruction_set == kMips) {
1840 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001841 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001842 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001843 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001844 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1845 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001846 } else if (cu_->instruction_set == kThumb2) {
1847 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1848 // Use ARM SDIV instruction for division. For remainder we also need to
1849 // calculate using a MUL and subtract.
1850 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001851 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001852 done = true;
1853 }
1854 }
1855
1856 if (!done) {
1857 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1859 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001860 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1862 if (is_div)
1863 rl_result = GetReturn(false);
1864 else
1865 rl_result = GetReturnAlt();
1866 }
1867 StoreValue(rl_dest, rl_result);
1868 return;
1869 }
1870 default:
1871 LOG(FATAL) << "Unexpected opcode " << opcode;
1872 }
1873 rl_src = LoadValue(rl_src, kCoreReg);
1874 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001875 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001877 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001879 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 }
1881 StoreValue(rl_dest, rl_result);
1882}
1883
1884void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001885 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 RegLocation rl_result;
1887 OpKind first_op = kOpBkpt;
1888 OpKind second_op = kOpBkpt;
1889 bool call_out = false;
1890 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001891 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001892 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893
1894 switch (opcode) {
1895 case Instruction::NOT_LONG:
1896 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1897 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1898 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001899 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1900 RegStorage t_reg = AllocTemp();
1901 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1902 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1903 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904 FreeTemp(t_reg);
1905 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001906 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1907 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 }
1909 StoreValueWide(rl_dest, rl_result);
1910 return;
1911 case Instruction::ADD_LONG:
1912 case Instruction::ADD_LONG_2ADDR:
1913 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001914 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 return;
1916 }
1917 first_op = kOpAdd;
1918 second_op = kOpAdc;
1919 break;
1920 case Instruction::SUB_LONG:
1921 case Instruction::SUB_LONG_2ADDR:
1922 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001923 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 return;
1925 }
1926 first_op = kOpSub;
1927 second_op = kOpSbc;
1928 break;
1929 case Instruction::MUL_LONG:
1930 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001931 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001932 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001933 return;
1934 } else {
1935 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001936 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001937 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 }
1939 break;
1940 case Instruction::DIV_LONG:
1941 case Instruction::DIV_LONG_2ADDR:
1942 call_out = true;
1943 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001944 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001945 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001946 break;
1947 case Instruction::REM_LONG:
1948 case Instruction::REM_LONG_2ADDR:
1949 call_out = true;
1950 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001951 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001953 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 break;
1955 case Instruction::AND_LONG_2ADDR:
1956 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001957 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001958 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 }
1960 first_op = kOpAnd;
1961 second_op = kOpAnd;
1962 break;
1963 case Instruction::OR_LONG:
1964 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001965 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001966 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 return;
1968 }
1969 first_op = kOpOr;
1970 second_op = kOpOr;
1971 break;
1972 case Instruction::XOR_LONG:
1973 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001974 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001975 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 return;
1977 }
1978 first_op = kOpXor;
1979 second_op = kOpXor;
1980 break;
1981 case Instruction::NEG_LONG: {
1982 GenNegLong(rl_dest, rl_src2);
1983 return;
1984 }
1985 default:
1986 LOG(FATAL) << "Invalid long arith op";
1987 }
1988 if (!call_out) {
1989 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1990 } else {
1991 FlushAllRegs(); /* Send everything to home location */
1992 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001993 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1994 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1995 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1996 RegStorage r_tgt = CallHelperSetup(func_offset);
1997 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1998 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 // NOTE: callout here is not a safepoint
2000 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2001 } else {
2002 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2003 }
2004 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002005 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 rl_result = GetReturnWide(false);
2007 else
2008 rl_result = GetReturnWideAlt();
2009 StoreValueWide(rl_dest, rl_result);
2010 }
2011}
2012
Ian Rogersdd7624d2014-03-14 17:43:00 -07002013void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002014 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 /*
2016 * Don't optimize the register usage since it calls out to support
2017 * functions
2018 */
2019 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2021 if (rl_dest.wide) {
2022 RegLocation rl_result;
2023 rl_result = GetReturnWide(rl_dest.fp);
2024 StoreValueWide(rl_dest, rl_result);
2025 } else {
2026 RegLocation rl_result;
2027 rl_result = GetReturn(rl_dest.fp);
2028 StoreValue(rl_dest, rl_result);
2029 }
2030}
2031
2032/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002033void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002034 if (Runtime::Current()->ExplicitSuspendChecks()) {
2035 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2036 return;
2037 }
2038 FlushAllRegs();
2039 LIR* branch = OpTestSuspend(NULL);
2040 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2041 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2042 current_dalvik_offset_);
2043 branch->target = target;
2044 suspend_launchpads_.Insert(target);
2045 } else {
2046 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2047 return;
2048 }
2049 FlushAllRegs(); // TODO: needed?
2050 LIR* inst = CheckSuspendUsingLoad();
2051 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053}
2054
2055/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002056void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002057 if (Runtime::Current()->ExplicitSuspendChecks()) {
2058 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2059 OpUnconditionalBranch(target);
2060 return;
2061 }
2062 OpTestSuspend(target);
2063 LIR* launch_pad =
2064 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2065 current_dalvik_offset_);
2066 FlushAllRegs();
2067 OpUnconditionalBranch(launch_pad);
2068 suspend_launchpads_.Insert(launch_pad);
2069 } else {
2070 // For the implicit suspend check, just perform the trigger
2071 // load and branch to the target.
2072 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2073 OpUnconditionalBranch(target);
2074 return;
2075 }
2076 FlushAllRegs();
2077 LIR* inst = CheckSuspendUsingLoad();
2078 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081}
2082
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002083/* Call out to helper assembly routine that will null check obj and then lock it. */
2084void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2085 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002086 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002087}
2088
2089/* Call out to helper assembly routine that will null check obj and then unlock it. */
2090void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2091 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002092 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002093}
2094
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002095/* Generic code for generating a wide constant into a VR. */
2096void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2097 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002098 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002099 StoreValueWide(rl_dest, rl_result);
2100}
2101
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102} // namespace art