blob: 4522379c0d61eefeb6b9c5a8a598757a0ebcc233 [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) {
503 rl_src = LoadValueWide(rl_src, kAnyReg);
504 } else {
505 rl_src = LoadValue(rl_src, kAnyReg);
506 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000507 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800508 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 GenMemBarrier(kStoreStore);
510 }
511 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800512 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800514 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000516 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800517 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 GenMemBarrier(kStoreLoad);
519 }
520 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800521 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800523 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 } else {
525 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700526 ThreadOffset<4> setter_offset =
527 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
528 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
529 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000530 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 }
532}
533
Vladimir Markobe0e5462014-02-26 11:24:15 +0000534void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700535 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000536 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
537 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
538 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
539 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800540 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000541 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 // Fast path, static storage base is this method's class
543 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800544 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800545 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 } else {
547 // Medium path, static storage base in a different class which requires checks that the other
548 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000549 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 // May do runtime call so everything to home locations.
551 FlushAllRegs();
552 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800553 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 LockTemp(r_method);
555 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800556 r_base = TargetReg(kArg0);
557 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800558 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800559 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000560 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000562 if (!field_info.IsInitialized() &&
563 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800564 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800565
566 // The slow path is invoked if the r_base is NULL or the class pointed
567 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800568 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800569 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800570 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800571 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800572 mirror::Class::StatusOffset().Int32Value(),
573 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800574 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800575
buzbee2700f7e2014-03-07 09:46:20 -0800576 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000577 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800578
579 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 FreeTemp(r_method);
582 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800583 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800585
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800587 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800589 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800591 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800592
593 if (field_info.IsVolatile()) {
594 // Without context sensitive analysis, we must issue the most conservative barriers.
595 // In this case, either a load or store may follow so we issue both barriers.
596 GenMemBarrier(kLoadLoad);
597 GenMemBarrier(kLoadStore);
598 }
599
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 if (is_long_or_double) {
601 StoreValueWide(rl_dest, rl_result);
602 } else {
603 StoreValue(rl_dest, rl_result);
604 }
605 } else {
606 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700607 ThreadOffset<4> getterOffset =
608 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
609 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
610 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000611 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 if (is_long_or_double) {
613 RegLocation rl_result = GetReturnWide(rl_dest.fp);
614 StoreValueWide(rl_dest, rl_result);
615 } else {
616 RegLocation rl_result = GetReturn(rl_dest.fp);
617 StoreValue(rl_dest, rl_result);
618 }
619 }
620}
621
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800622// Generate code for all slow paths.
623void Mir2Lir::HandleSlowPaths() {
624 int n = slow_paths_.Size();
625 for (int i = 0; i < n; ++i) {
626 LIRSlowPath* slowpath = slow_paths_.Get(i);
627 slowpath->Compile();
628 }
629 slow_paths_.Reset();
630}
631
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700632void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700634 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 for (int i = 0; i < num_elems; i++) {
636 ResetRegPool();
637 ResetDefTracking();
638 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700639 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 current_dalvik_offset_ = lab->operands[1];
641 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800642 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
644 OpUnconditionalBranch(resume_lab);
645 }
646}
647
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700648void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 int num_elems = throw_launchpads_.Size();
650 for (int i = 0; i < num_elems; i++) {
651 ResetRegPool();
652 ResetDefTracking();
653 LIR* lab = throw_launchpads_.Get(i);
654 current_dalvik_offset_ = lab->operands[1];
655 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700656 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 int v1 = lab->operands[2];
658 int v2 = lab->operands[3];
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700659 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 switch (lab->operands[0]) {
661 case kThrowNullPointer:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700662 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700664 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
666 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800667 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
668 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800670 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 }
672 // Make sure the following LoadConstant doesn't mess with kArg1.
673 LockTemp(TargetReg(kArg1));
674 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700675 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 break;
677 case kThrowArrayBounds:
678 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800679 if (v2 != TargetReg(kArg0).GetReg()) {
680 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 if (target_x86) {
682 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800683 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
684 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800686 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 }
688 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800689 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800691 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 if (target_x86) {
693 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800694 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
695 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800697 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 }
699 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
700 } else {
701 if (target_x86) {
702 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800703 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
704 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800706 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 }
buzbee2700f7e2014-03-07 09:46:20 -0800708 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 }
710 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700711 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800714 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700716 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 default:
719 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
720 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000721 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800722 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700723 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
725}
726
Vladimir Markobe0e5462014-02-26 11:24:15 +0000727void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700729 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000730 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
731 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
732 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 RegLocation rl_result;
734 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000735 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 rl_obj = LoadValue(rl_obj, kCoreReg);
737 if (is_long_or_double) {
738 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800739 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700740 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800742 // FIXME? duplicate null check?
743 GenNullCheck(rl_obj.reg, opt_flags);
744 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
745 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800746 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000747 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800748 // Without context sensitive analysis, we must issue the most conservative barriers.
749 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800751 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 }
753 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800754 RegStorage reg_ptr = AllocTemp();
755 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800757 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700758 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000759 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800760 // Without context sensitive analysis, we must issue the most conservative barriers.
761 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800763 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 }
765 FreeTemp(reg_ptr);
766 }
767 StoreValueWide(rl_dest, rl_result);
768 } else {
769 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800770 GenNullCheck(rl_obj.reg, opt_flags);
771 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
772 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800773 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800775 // Without context sensitive analysis, we must issue the most conservative barriers.
776 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800778 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 }
780 StoreValue(rl_dest, rl_result);
781 }
782 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700783 ThreadOffset<4> getterOffset =
784 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
785 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
786 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 if (is_long_or_double) {
789 RegLocation rl_result = GetReturnWide(rl_dest.fp);
790 StoreValueWide(rl_dest, rl_result);
791 } else {
792 RegLocation rl_result = GetReturn(rl_dest.fp);
793 StoreValue(rl_dest, rl_result);
794 }
795 }
796}
797
Vladimir Markobe0e5462014-02-26 11:24:15 +0000798void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700800 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000801 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
802 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
803 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000805 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 rl_obj = LoadValue(rl_obj, kCoreReg);
807 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800809 GenNullCheck(rl_obj.reg, opt_flags);
810 RegStorage reg_ptr = AllocTemp();
811 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000812 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800813 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 GenMemBarrier(kStoreStore);
815 }
buzbee2700f7e2014-03-07 09:46:20 -0800816 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800817 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000818 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800819 // A load might follow the volatile store so insert a StoreLoad barrier.
820 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 }
822 FreeTemp(reg_ptr);
823 } else {
824 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800825 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000826 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800827 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 GenMemBarrier(kStoreStore);
829 }
buzbee2700f7e2014-03-07 09:46:20 -0800830 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800831 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000832 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800833 // A load might follow the volatile store so insert a StoreLoad barrier.
834 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 }
836 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800837 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 }
839 }
840 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700841 ThreadOffset<4> setter_offset =
842 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
843 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
844 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000845 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
846 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 }
848}
849
Ian Rogersa9a82542013-10-04 11:17:26 -0700850void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
851 RegLocation rl_src) {
852 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
853 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
854 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700855 ThreadOffset<4> helper = needs_range_check
856 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
857 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
858 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700859 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
860}
861
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700862void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800864 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
866 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
867 *cu_->dex_file,
868 type_idx)) {
869 // Call out to helper which resolves type and verifies access.
870 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700871 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800872 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 RegLocation rl_result = GetReturn(false);
874 StoreValue(rl_dest, rl_result);
875 } else {
876 // We're don't need access checks, load type from dex cache
877 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700878 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800879 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 int32_t offset_of_type =
881 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
882 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800883 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
885 type_idx) || SLOW_TYPE_PATH) {
886 // Slow path, at runtime test if type is null and if so initialize
887 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800888 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800889 LIR* cont = NewLIR0(kPseudoTargetLabel);
890
891 // Object to generate the slow path for class resolution.
892 class SlowPath : public LIRSlowPath {
893 public:
894 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
895 const RegLocation& rl_method, const RegLocation& rl_result) :
896 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
897 rl_method_(rl_method), rl_result_(rl_result) {
898 }
899
900 void Compile() {
901 GenerateTargetLabel();
902
Ian Rogersdd7624d2014-03-14 17:43:00 -0700903 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800904 rl_method_.reg, true);
905 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800906
907 m2l_->OpUnconditionalBranch(cont_);
908 }
909
910 private:
911 const int type_idx_;
912 const RegLocation rl_method_;
913 const RegLocation rl_result_;
914 };
915
916 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800917 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800918
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800920 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 // Fast path, we're done - just store result
922 StoreValue(rl_dest, rl_result);
923 }
924 }
925}
926
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700927void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 /* NOTE: Most strings should be available at compile time */
929 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
930 (sizeof(mirror::String*) * string_idx);
931 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
932 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
933 // slow path, resolve string if not in dex cache
934 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700935 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800936
937 // If the Method* is already in a register, we can save a copy.
938 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800939 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800940 if (rl_method.location == kLocPhysReg) {
941 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800942 DCHECK(!IsTemp(rl_method.reg));
943 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800944 } else {
945 r_method = TargetReg(kArg2);
946 LoadCurrMethodDirect(r_method);
947 }
948 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
949 TargetReg(kArg0));
950
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800953 if (cu_->instruction_set == kThumb2 ||
954 cu_->instruction_set == kMips) {
955 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800956 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800957 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
958 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800960
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800961 // Object to generate the slow path for string resolution.
962 class SlowPath : public LIRSlowPath {
963 public:
buzbee2700f7e2014-03-07 09:46:20 -0800964 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
966 }
967
968 void Compile() {
969 GenerateTargetLabel();
970
Dave Allisond6ed6422014-04-09 23:36:15 +0000971 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800972
Dave Allisond6ed6422014-04-09 23:36:15 +0000973 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
974 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
975 m2l_->MarkSafepointPC(call_inst);
976 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800977
978 m2l_->OpUnconditionalBranch(cont_);
979 }
980
981 private:
buzbee2700f7e2014-03-07 09:46:20 -0800982 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800983 };
984
985 // Add to list for future.
986 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700988 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800989 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
990 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700991 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -0800992 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800993 LIR* target = NewLIR0(kPseudoTargetLabel);
994 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 }
996 GenBarrier();
997 StoreValue(rl_dest, GetReturn(false));
998 } else {
999 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001000 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001002 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1003 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 StoreValue(rl_dest, rl_result);
1005 }
1006}
1007
1008/*
1009 * Let helper function take care of everything. Will
1010 * call Class::NewInstanceFromCode(type_idx, method);
1011 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001012void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 FlushAllRegs(); /* Everything to home location */
1014 // alloc will always check for resolution, do we also need to verify
1015 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001016 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001017 const DexFile* dex_file = cu_->dex_file;
1018 CompilerDriver* driver = cu_->compiler_driver;
1019 if (driver->CanAccessInstantiableTypeWithoutChecks(
1020 cu_->method_idx, *dex_file, type_idx)) {
1021 bool is_type_initialized;
1022 bool use_direct_type_ptr;
1023 uintptr_t direct_type_ptr;
1024 if (kEmbedClassInCode &&
1025 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1026 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1027 // The fast path.
1028 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001029 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001030 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001031 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001032 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1033 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001034 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001035 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1036 }
1037 } else {
1038 // Use the direct pointer.
1039 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001040 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001041 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1042 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001043 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001044 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1045 }
1046 }
1047 } else {
1048 // The slow path.
1049 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001050 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001051 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1052 }
1053 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001055 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 RegLocation rl_result = GetReturn(false);
1059 StoreValue(rl_dest, rl_result);
1060}
1061
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001062void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001064 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065}
1066
1067// For final classes there are no sub-classes to check and so we can answer the instance-of
1068// question with simple comparisons.
1069void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1070 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001071 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001072 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001073
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 RegLocation object = LoadValue(rl_src, kCoreReg);
1075 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001076 RegStorage result_reg = rl_result.reg;
1077 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 result_reg = AllocTypedTemp(false, kCoreReg);
1079 }
1080 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001081 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082
buzbee2700f7e2014-03-07 09:46:20 -08001083 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1084 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085
1086 LoadCurrMethodDirect(check_class);
1087 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001088 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1089 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001091 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001093 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 int32_t offset_of_type =
1095 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1096 (sizeof(mirror::Class*) * type_idx);
1097 LoadWordDisp(check_class, offset_of_type, check_class);
1098 }
1099
1100 LIR* ne_branchover = NULL;
1101 if (cu_->instruction_set == kThumb2) {
1102 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001103 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001105 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 } else {
1107 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1108 LoadConstant(result_reg, 1); // eq case - load true
1109 }
1110 LIR* target = NewLIR0(kPseudoTargetLabel);
1111 null_branchover->target = target;
1112 if (ne_branchover != NULL) {
1113 ne_branchover->target = target;
1114 }
1115 FreeTemp(object_class);
1116 FreeTemp(check_class);
1117 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001118 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 FreeTemp(result_reg);
1120 }
1121 StoreValue(rl_dest, rl_result);
1122}
1123
1124void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1125 bool type_known_abstract, bool use_declaring_class,
1126 bool can_assume_type_is_in_dex_cache,
1127 uint32_t type_idx, RegLocation rl_dest,
1128 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001129 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001130 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001131
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 FlushAllRegs();
1133 // May generate a call - use explicit registers
1134 LockCallTemps();
1135 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001136 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 if (needs_access_check) {
1138 // Check we have access to type_idx and if not throw IllegalAccessError,
1139 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001140 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 type_idx, true);
1142 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1143 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1144 } else if (use_declaring_class) {
1145 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001146 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1147 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 } else {
1149 // Load dex cache entry into class_reg (kArg2)
1150 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001151 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1152 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 int32_t offset_of_type =
1154 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1155 * type_idx);
1156 LoadWordDisp(class_reg, offset_of_type, class_reg);
1157 if (!can_assume_type_is_in_dex_cache) {
1158 // Need to test presence of type in dex cache at runtime
1159 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1160 // Not resolved
1161 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001162 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001163 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1165 // Rejoin code paths
1166 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1167 hop_branch->target = hop_target;
1168 }
1169 }
1170 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1171 RegLocation rl_result = GetReturn(false);
1172 if (cu_->instruction_set == kMips) {
1173 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001174 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 }
1176 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1177
1178 /* load object->klass_ */
1179 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1180 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1181 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1182 LIR* branchover = NULL;
1183 if (type_known_final) {
1184 // rl_result == ref == null == 0.
1185 if (cu_->instruction_set == kThumb2) {
1186 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001187 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001188 LoadConstant(rl_result.reg, 1); // .eq case - load true
1189 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001190 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001192 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001194 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 }
1196 } else {
1197 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001198 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001199 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 if (!type_known_abstract) {
1201 /* Uses conditional nullification */
1202 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001203 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1205 }
1206 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1207 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001208 if (it != nullptr) {
1209 OpEndIT(it);
1210 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 FreeTemp(r_tgt);
1212 } else {
1213 if (!type_known_abstract) {
1214 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001215 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1217 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001218 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001219 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1220 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1221 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 }
1223 }
1224 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001225 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 /* branch targets here */
1227 LIR* target = NewLIR0(kPseudoTargetLabel);
1228 StoreValue(rl_dest, rl_result);
1229 branch1->target = target;
1230 if (branchover != NULL) {
1231 branchover->target = target;
1232 }
1233}
1234
1235void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1236 bool type_known_final, type_known_abstract, use_declaring_class;
1237 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1238 *cu_->dex_file,
1239 type_idx,
1240 &type_known_final,
1241 &type_known_abstract,
1242 &use_declaring_class);
1243 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1244 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1245
1246 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1247 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1248 } else {
1249 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1250 use_declaring_class, can_assume_type_is_in_dex_cache,
1251 type_idx, rl_dest, rl_src);
1252 }
1253}
1254
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001255void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 bool type_known_final, type_known_abstract, use_declaring_class;
1257 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1258 *cu_->dex_file,
1259 type_idx,
1260 &type_known_final,
1261 &type_known_abstract,
1262 &use_declaring_class);
1263 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1264 // of the exception throw path.
1265 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001266 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 // Verifier type analysis proved this check cast would never cause an exception.
1268 return;
1269 }
1270 FlushAllRegs();
1271 // May generate a call - use explicit registers
1272 LockCallTemps();
1273 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001274 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 if (needs_access_check) {
1276 // Check we have access to type_idx and if not throw IllegalAccessError,
1277 // returns Class* in kRet0
1278 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001279 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 type_idx, TargetReg(kArg1), true);
1281 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1282 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001283 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1284 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 } else {
1286 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001287 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1288 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 int32_t offset_of_type =
1290 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1291 (sizeof(mirror::Class*) * type_idx);
1292 LoadWordDisp(class_reg, offset_of_type, class_reg);
1293 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1294 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001295 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1296 LIR* cont = NewLIR0(kPseudoTargetLabel);
1297
1298 // Slow path to initialize the type. Executed if the type is NULL.
1299 class SlowPath : public LIRSlowPath {
1300 public:
1301 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001302 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001303 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1304 class_reg_(class_reg) {
1305 }
1306
1307 void Compile() {
1308 GenerateTargetLabel();
1309
1310 // Call out to helper, which will return resolved type in kArg0
1311 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001312 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001313 m2l_->TargetReg(kArg1), true);
1314 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1315 m2l_->OpUnconditionalBranch(cont_);
1316 }
1317 public:
1318 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001319 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001320 };
1321
buzbee2700f7e2014-03-07 09:46:20 -08001322 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 }
1324 }
1325 // At this point, class_reg (kArg2) has class
1326 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001327
1328 // Slow path for the case where the classes are not equal. In this case we need
1329 // to call a helper function to do the check.
1330 class SlowPath : public LIRSlowPath {
1331 public:
1332 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1333 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1334 }
1335
1336 void Compile() {
1337 GenerateTargetLabel();
1338
1339 if (load_) {
1340 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1341 m2l_->TargetReg(kArg1));
1342 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001343 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001344 m2l_->TargetReg(kArg1), true);
1345
1346 m2l_->OpUnconditionalBranch(cont_);
1347 }
1348
1349 private:
1350 bool load_;
1351 };
1352
1353 if (type_known_abstract) {
1354 // Easier case, run slow path if target is non-null (slow path will load from target)
1355 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1356 LIR* cont = NewLIR0(kPseudoTargetLabel);
1357 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1358 } else {
1359 // Harder, more common case. We need to generate a forward branch over the load
1360 // if the target is null. If it's non-null we perform the load and branch to the
1361 // slow path if the classes are not equal.
1362
1363 /* Null is OK - continue */
1364 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1365 /* load object->klass_ */
1366 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001367 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001368
1369 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1370 LIR* cont = NewLIR0(kPseudoTargetLabel);
1371
1372 // Add the slow path that will not perform load since this is already done.
1373 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1374
1375 // Set the null check to branch to the continuation.
1376 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 }
1378}
1379
1380void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001381 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 RegLocation rl_result;
1383 if (cu_->instruction_set == kThumb2) {
1384 /*
1385 * NOTE: This is the one place in the code in which we might have
1386 * as many as six live temporary registers. There are 5 in the normal
1387 * set for Arm. Until we have spill capabilities, temporarily add
1388 * lr to the temp set. It is safe to do this locally, but note that
1389 * lr is used explicitly elsewhere in the code generator and cannot
1390 * normally be used as a general temp register.
1391 */
1392 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1393 FreeTemp(TargetReg(kLr)); // and make it available
1394 }
1395 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1396 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1397 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1398 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001399 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1400 RegStorage t_reg = AllocTemp();
1401 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1402 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1403 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 FreeTemp(t_reg);
1405 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001406 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1407 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 }
1409 /*
1410 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1411 * following StoreValueWide might need to allocate a temp register.
1412 * To further work around the lack of a spill capability, explicitly
1413 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1414 * Remove when spill is functional.
1415 */
1416 FreeRegLocTemps(rl_result, rl_src1);
1417 FreeRegLocTemps(rl_result, rl_src2);
1418 StoreValueWide(rl_dest, rl_result);
1419 if (cu_->instruction_set == kThumb2) {
1420 Clobber(TargetReg(kLr));
1421 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1422 }
1423}
1424
1425
1426void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001427 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001428 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429
1430 switch (opcode) {
1431 case Instruction::SHL_LONG:
1432 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001433 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 break;
1435 case Instruction::SHR_LONG:
1436 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001437 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 break;
1439 case Instruction::USHR_LONG:
1440 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001441 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 break;
1443 default:
1444 LOG(FATAL) << "Unexpected case";
1445 }
1446 FlushAllRegs(); /* Send everything to home location */
1447 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1448 RegLocation rl_result = GetReturnWide(false);
1449 StoreValueWide(rl_dest, rl_result);
1450}
1451
1452
1453void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001454 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001455 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 OpKind op = kOpBkpt;
1457 bool is_div_rem = false;
1458 bool check_zero = false;
1459 bool unary = false;
1460 RegLocation rl_result;
1461 bool shift_op = false;
1462 switch (opcode) {
1463 case Instruction::NEG_INT:
1464 op = kOpNeg;
1465 unary = true;
1466 break;
1467 case Instruction::NOT_INT:
1468 op = kOpMvn;
1469 unary = true;
1470 break;
1471 case Instruction::ADD_INT:
1472 case Instruction::ADD_INT_2ADDR:
1473 op = kOpAdd;
1474 break;
1475 case Instruction::SUB_INT:
1476 case Instruction::SUB_INT_2ADDR:
1477 op = kOpSub;
1478 break;
1479 case Instruction::MUL_INT:
1480 case Instruction::MUL_INT_2ADDR:
1481 op = kOpMul;
1482 break;
1483 case Instruction::DIV_INT:
1484 case Instruction::DIV_INT_2ADDR:
1485 check_zero = true;
1486 op = kOpDiv;
1487 is_div_rem = true;
1488 break;
1489 /* NOTE: returns in kArg1 */
1490 case Instruction::REM_INT:
1491 case Instruction::REM_INT_2ADDR:
1492 check_zero = true;
1493 op = kOpRem;
1494 is_div_rem = true;
1495 break;
1496 case Instruction::AND_INT:
1497 case Instruction::AND_INT_2ADDR:
1498 op = kOpAnd;
1499 break;
1500 case Instruction::OR_INT:
1501 case Instruction::OR_INT_2ADDR:
1502 op = kOpOr;
1503 break;
1504 case Instruction::XOR_INT:
1505 case Instruction::XOR_INT_2ADDR:
1506 op = kOpXor;
1507 break;
1508 case Instruction::SHL_INT:
1509 case Instruction::SHL_INT_2ADDR:
1510 shift_op = true;
1511 op = kOpLsl;
1512 break;
1513 case Instruction::SHR_INT:
1514 case Instruction::SHR_INT_2ADDR:
1515 shift_op = true;
1516 op = kOpAsr;
1517 break;
1518 case Instruction::USHR_INT:
1519 case Instruction::USHR_INT_2ADDR:
1520 shift_op = true;
1521 op = kOpLsr;
1522 break;
1523 default:
1524 LOG(FATAL) << "Invalid word arith op: " << opcode;
1525 }
1526 if (!is_div_rem) {
1527 if (unary) {
1528 rl_src1 = LoadValue(rl_src1, kCoreReg);
1529 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001530 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 } else {
1532 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001533 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001534 RegStorage t_reg = AllocTemp();
1535 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 rl_src1 = LoadValue(rl_src1, kCoreReg);
1537 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001538 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 FreeTemp(t_reg);
1540 } else {
1541 rl_src1 = LoadValue(rl_src1, kCoreReg);
1542 rl_src2 = LoadValue(rl_src2, kCoreReg);
1543 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001544 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 }
1546 }
1547 StoreValue(rl_dest, rl_result);
1548 } else {
Dave Allison70202782013-10-22 17:52:19 -07001549 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 -07001550 if (cu_->instruction_set == kMips) {
1551 rl_src1 = LoadValue(rl_src1, kCoreReg);
1552 rl_src2 = LoadValue(rl_src2, kCoreReg);
1553 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001554 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 }
buzbee2700f7e2014-03-07 09:46:20 -08001556 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001557 done = true;
1558 } else if (cu_->instruction_set == kThumb2) {
1559 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1560 // Use ARM SDIV instruction for division. For remainder we also need to
1561 // calculate using a MUL and subtract.
1562 rl_src1 = LoadValue(rl_src1, kCoreReg);
1563 rl_src2 = LoadValue(rl_src2, kCoreReg);
1564 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001565 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Dave Allison70202782013-10-22 17:52:19 -07001566 }
buzbee2700f7e2014-03-07 09:46:20 -08001567 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001568 done = true;
1569 }
1570 }
1571
1572 // If we haven't already generated the code use the callout function.
1573 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001574 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 FlushAllRegs(); /* Send everything to home location */
1576 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001577 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1579 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001580 AddDivZeroSlowPath(kCondEq, TargetReg(kArg1), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 }
Dave Allison70202782013-10-22 17:52:19 -07001582 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001583 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 if (op == kOpDiv)
1585 rl_result = GetReturn(false);
1586 else
1587 rl_result = GetReturnAlt();
1588 }
1589 StoreValue(rl_dest, rl_result);
1590 }
1591}
1592
1593/*
1594 * The following are the first-level codegen routines that analyze the format
1595 * of each bytecode then either dispatch special purpose codegen routines
1596 * or produce corresponding Thumb instructions directly.
1597 */
1598
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001600static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 x &= x - 1;
1602 return (x & (x - 1)) == 0;
1603}
1604
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1606// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001607bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001608 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1610 return false;
1611 }
1612 // No divide instruction for Arm, so check for more special cases
1613 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001614 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 }
1616 int k = LowestSetBit(lit);
1617 if (k >= 30) {
1618 // Avoid special cases.
1619 return false;
1620 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 rl_src = LoadValue(rl_src, kCoreReg);
1622 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001623 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001624 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 if (lit == 2) {
1626 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1628 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1629 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001633 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1634 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 }
1636 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001637 RegStorage t_reg1 = AllocTemp();
1638 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001640 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1641 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001642 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001643 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001645 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001647 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001649 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 }
1651 }
1652 StoreValue(rl_dest, rl_result);
1653 return true;
1654}
1655
1656// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1657// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001658bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001659 if (lit < 0) {
1660 return false;
1661 }
1662 if (lit == 0) {
1663 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1664 LoadConstant(rl_result.reg, 0);
1665 StoreValue(rl_dest, rl_result);
1666 return true;
1667 }
1668 if (lit == 1) {
1669 rl_src = LoadValue(rl_src, kCoreReg);
1670 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1671 OpRegCopy(rl_result.reg, rl_src.reg);
1672 StoreValue(rl_dest, rl_result);
1673 return true;
1674 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001675 // There is RegRegRegShift on Arm, so check for more special cases
1676 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001677 return EasyMultiply(rl_src, rl_dest, lit);
1678 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 // Can we simplify this multiplication?
1680 bool power_of_two = false;
1681 bool pop_count_le2 = false;
1682 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001683 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 power_of_two = true;
1685 } else if (IsPopCountLE2(lit)) {
1686 pop_count_le2 = true;
1687 } else if (IsPowerOfTwo(lit + 1)) {
1688 power_of_two_minus_one = true;
1689 } else {
1690 return false;
1691 }
1692 rl_src = LoadValue(rl_src, kCoreReg);
1693 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1694 if (power_of_two) {
1695 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001696 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697 } else if (pop_count_le2) {
1698 // Shift and add and shift.
1699 int first_bit = LowestSetBit(lit);
1700 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1701 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1702 } else {
1703 // Reverse subtract: (src << (shift + 1)) - src.
1704 DCHECK(power_of_two_minus_one);
1705 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001706 RegStorage t_reg = AllocTemp();
1707 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1708 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 }
1710 StoreValue(rl_dest, rl_result);
1711 return true;
1712}
1713
1714void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001715 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 RegLocation rl_result;
1717 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1718 int shift_op = false;
1719 bool is_div = false;
1720
1721 switch (opcode) {
1722 case Instruction::RSUB_INT_LIT8:
1723 case Instruction::RSUB_INT: {
1724 rl_src = LoadValue(rl_src, kCoreReg);
1725 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1726 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001727 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001729 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1730 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 }
1732 StoreValue(rl_dest, rl_result);
1733 return;
1734 }
1735
1736 case Instruction::SUB_INT:
1737 case Instruction::SUB_INT_2ADDR:
1738 lit = -lit;
1739 // Intended fallthrough
1740 case Instruction::ADD_INT:
1741 case Instruction::ADD_INT_2ADDR:
1742 case Instruction::ADD_INT_LIT8:
1743 case Instruction::ADD_INT_LIT16:
1744 op = kOpAdd;
1745 break;
1746 case Instruction::MUL_INT:
1747 case Instruction::MUL_INT_2ADDR:
1748 case Instruction::MUL_INT_LIT8:
1749 case Instruction::MUL_INT_LIT16: {
1750 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1751 return;
1752 }
1753 op = kOpMul;
1754 break;
1755 }
1756 case Instruction::AND_INT:
1757 case Instruction::AND_INT_2ADDR:
1758 case Instruction::AND_INT_LIT8:
1759 case Instruction::AND_INT_LIT16:
1760 op = kOpAnd;
1761 break;
1762 case Instruction::OR_INT:
1763 case Instruction::OR_INT_2ADDR:
1764 case Instruction::OR_INT_LIT8:
1765 case Instruction::OR_INT_LIT16:
1766 op = kOpOr;
1767 break;
1768 case Instruction::XOR_INT:
1769 case Instruction::XOR_INT_2ADDR:
1770 case Instruction::XOR_INT_LIT8:
1771 case Instruction::XOR_INT_LIT16:
1772 op = kOpXor;
1773 break;
1774 case Instruction::SHL_INT_LIT8:
1775 case Instruction::SHL_INT:
1776 case Instruction::SHL_INT_2ADDR:
1777 lit &= 31;
1778 shift_op = true;
1779 op = kOpLsl;
1780 break;
1781 case Instruction::SHR_INT_LIT8:
1782 case Instruction::SHR_INT:
1783 case Instruction::SHR_INT_2ADDR:
1784 lit &= 31;
1785 shift_op = true;
1786 op = kOpAsr;
1787 break;
1788 case Instruction::USHR_INT_LIT8:
1789 case Instruction::USHR_INT:
1790 case Instruction::USHR_INT_2ADDR:
1791 lit &= 31;
1792 shift_op = true;
1793 op = kOpLsr;
1794 break;
1795
1796 case Instruction::DIV_INT:
1797 case Instruction::DIV_INT_2ADDR:
1798 case Instruction::DIV_INT_LIT8:
1799 case Instruction::DIV_INT_LIT16:
1800 case Instruction::REM_INT:
1801 case Instruction::REM_INT_2ADDR:
1802 case Instruction::REM_INT_LIT8:
1803 case Instruction::REM_INT_LIT16: {
1804 if (lit == 0) {
Mingyao Yang42894562014-04-07 12:42:16 -07001805 AddDivZeroSlowPath(kCondAl, RegStorage::InvalidReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 return;
1807 }
buzbee11b63d12013-08-27 07:34:17 -07001808 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001809 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001810 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001811 (opcode == Instruction::DIV_INT_LIT16)) {
1812 is_div = true;
1813 } else {
1814 is_div = false;
1815 }
buzbee11b63d12013-08-27 07:34:17 -07001816 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1817 return;
1818 }
Dave Allison70202782013-10-22 17:52:19 -07001819
1820 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001821 if (cu_->instruction_set == kMips) {
1822 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001823 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001824 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001825 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001826 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1827 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001828 } else if (cu_->instruction_set == kThumb2) {
1829 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1830 // Use ARM SDIV instruction for division. For remainder we also need to
1831 // calculate using a MUL and subtract.
1832 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001833 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001834 done = true;
1835 }
1836 }
1837
1838 if (!done) {
1839 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1841 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001842 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1844 if (is_div)
1845 rl_result = GetReturn(false);
1846 else
1847 rl_result = GetReturnAlt();
1848 }
1849 StoreValue(rl_dest, rl_result);
1850 return;
1851 }
1852 default:
1853 LOG(FATAL) << "Unexpected opcode " << opcode;
1854 }
1855 rl_src = LoadValue(rl_src, kCoreReg);
1856 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001857 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001859 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001861 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001862 }
1863 StoreValue(rl_dest, rl_result);
1864}
1865
1866void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001867 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001868 RegLocation rl_result;
1869 OpKind first_op = kOpBkpt;
1870 OpKind second_op = kOpBkpt;
1871 bool call_out = false;
1872 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001873 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001874 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875
1876 switch (opcode) {
1877 case Instruction::NOT_LONG:
1878 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1879 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1880 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001881 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1882 RegStorage t_reg = AllocTemp();
1883 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1884 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1885 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 FreeTemp(t_reg);
1887 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001888 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1889 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001890 }
1891 StoreValueWide(rl_dest, rl_result);
1892 return;
1893 case Instruction::ADD_LONG:
1894 case Instruction::ADD_LONG_2ADDR:
1895 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001896 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 return;
1898 }
1899 first_op = kOpAdd;
1900 second_op = kOpAdc;
1901 break;
1902 case Instruction::SUB_LONG:
1903 case Instruction::SUB_LONG_2ADDR:
1904 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001905 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 return;
1907 }
1908 first_op = kOpSub;
1909 second_op = kOpSbc;
1910 break;
1911 case Instruction::MUL_LONG:
1912 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001913 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001914 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 return;
1916 } else {
1917 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001918 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001919 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 }
1921 break;
1922 case Instruction::DIV_LONG:
1923 case Instruction::DIV_LONG_2ADDR:
1924 call_out = true;
1925 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001926 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001927 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 break;
1929 case Instruction::REM_LONG:
1930 case Instruction::REM_LONG_2ADDR:
1931 call_out = true;
1932 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001933 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001935 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 break;
1937 case Instruction::AND_LONG_2ADDR:
1938 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001939 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001940 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 }
1942 first_op = kOpAnd;
1943 second_op = kOpAnd;
1944 break;
1945 case Instruction::OR_LONG:
1946 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001947 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001948 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 return;
1950 }
1951 first_op = kOpOr;
1952 second_op = kOpOr;
1953 break;
1954 case Instruction::XOR_LONG:
1955 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001956 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001957 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 return;
1959 }
1960 first_op = kOpXor;
1961 second_op = kOpXor;
1962 break;
1963 case Instruction::NEG_LONG: {
1964 GenNegLong(rl_dest, rl_src2);
1965 return;
1966 }
1967 default:
1968 LOG(FATAL) << "Invalid long arith op";
1969 }
1970 if (!call_out) {
1971 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1972 } else {
1973 FlushAllRegs(); /* Send everything to home location */
1974 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001975 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1976 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1977 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1978 RegStorage r_tgt = CallHelperSetup(func_offset);
1979 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1980 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 // NOTE: callout here is not a safepoint
1982 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1983 } else {
1984 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1985 }
1986 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001987 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988 rl_result = GetReturnWide(false);
1989 else
1990 rl_result = GetReturnWideAlt();
1991 StoreValueWide(rl_dest, rl_result);
1992 }
1993}
1994
Ian Rogersdd7624d2014-03-14 17:43:00 -07001995void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001996 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 /*
1998 * Don't optimize the register usage since it calls out to support
1999 * functions
2000 */
2001 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002002 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2003 if (rl_dest.wide) {
2004 RegLocation rl_result;
2005 rl_result = GetReturnWide(rl_dest.fp);
2006 StoreValueWide(rl_dest, rl_result);
2007 } else {
2008 RegLocation rl_result;
2009 rl_result = GetReturn(rl_dest.fp);
2010 StoreValue(rl_dest, rl_result);
2011 }
2012}
2013
2014/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002015void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002016 if (Runtime::Current()->ExplicitSuspendChecks()) {
2017 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2018 return;
2019 }
2020 FlushAllRegs();
2021 LIR* branch = OpTestSuspend(NULL);
2022 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2023 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2024 current_dalvik_offset_);
2025 branch->target = target;
2026 suspend_launchpads_.Insert(target);
2027 } else {
2028 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2029 return;
2030 }
2031 FlushAllRegs(); // TODO: needed?
2032 LIR* inst = CheckSuspendUsingLoad();
2033 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035}
2036
2037/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002038void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002039 if (Runtime::Current()->ExplicitSuspendChecks()) {
2040 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2041 OpUnconditionalBranch(target);
2042 return;
2043 }
2044 OpTestSuspend(target);
2045 LIR* launch_pad =
2046 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2047 current_dalvik_offset_);
2048 FlushAllRegs();
2049 OpUnconditionalBranch(launch_pad);
2050 suspend_launchpads_.Insert(launch_pad);
2051 } else {
2052 // For the implicit suspend check, just perform the trigger
2053 // load and branch to the target.
2054 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2055 OpUnconditionalBranch(target);
2056 return;
2057 }
2058 FlushAllRegs();
2059 LIR* inst = CheckSuspendUsingLoad();
2060 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002063}
2064
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002065/* Call out to helper assembly routine that will null check obj and then lock it. */
2066void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2067 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002068 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002069}
2070
2071/* Call out to helper assembly routine that will null check obj and then unlock it. */
2072void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2073 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002074 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002075}
2076
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002077/* Generic code for generating a wide constant into a VR. */
2078void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2079 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002080 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002081 StoreValueWide(rl_dest, rl_result);
2082}
2083
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084} // namespace art