blob: 2afc43cc26a7962ceb95c8f572d6b5e85ebbd2d1 [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"
Dave Allisonf9487c02014-04-08 23:08:12 +000020#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070021#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080023#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080025#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
29/*
30 * This source files contains "gen" codegen routines that should
31 * be applicable to most targets. Only mid-level support utilities
32 * and "op" calls may be used here.
33 */
34
35/*
buzbeeb48819d2013-09-14 16:15:25 -070036 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070037 * blocks.
38 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070039void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 LIR* barrier = NewLIR0(kPseudoBarrier);
41 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070042 DCHECK(!barrier->flags.use_def_invalid);
43 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070044}
45
buzbee2700f7e2014-03-07 09:46:20 -080046LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
47 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 LIR* branch;
49 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080050 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
51 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 branch = OpUnconditionalBranch(tgt);
53 } else {
buzbee2700f7e2014-03-07 09:46:20 -080054 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
56 }
57 // Remember branch target - will process later
58 throw_launchpads_.Insert(tgt);
59 return branch;
60}
61
Mingyao Yang42894562014-04-07 12:42:16 -070062void Mir2Lir::AddDivZeroSlowPath(ConditionCode c_code) {
63 LIR* branch = OpCondBranch(c_code, nullptr);
64 AddDivZeroCheckSlowPath(branch);
65}
66
67void Mir2Lir::AddDivZeroSlowPath(ConditionCode c_code, RegStorage reg, int imm_val) {
68 LIR* branch;
69 if (c_code == kCondAl) {
70 branch = OpUnconditionalBranch(nullptr);
71 } else {
72 branch = OpCmpImmBranch(c_code, reg, imm_val, nullptr);
73 }
74 AddDivZeroCheckSlowPath(branch);
75}
76
77void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
78 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
79 public:
80 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
81 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
82 }
83
84 void Compile() {
85 m2l_->ResetRegPool();
86 m2l_->ResetDefTracking();
87 GenerateTargetLabel();
88 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
89 }
90 };
91
92 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
93}
Dave Allisonb373e092014-02-20 16:06:36 -080094
Brian Carlstrom7940e442013-07-12 13:46:57 -070095/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -080096LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -080097 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -070098 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 }
Dave Allisonb373e092014-02-20 16:06:36 -0800100 return nullptr;
101}
102
Dave Allisonf9439142014-03-27 15:10:22 -0700103/* Perform an explicit null-check on a register. */
104LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
105 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
106 return NULL;
107 }
108 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
109}
110
Dave Allisonb373e092014-02-20 16:06:36 -0800111void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
112 if (!Runtime::Current()->ExplicitNullChecks()) {
113 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
114 return;
115 }
116 MarkSafepointPC(last_lir_insn_);
117 }
118}
119
120void Mir2Lir::MarkPossibleStackOverflowException() {
121 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
122 MarkSafepointPC(last_lir_insn_);
123 }
124}
125
buzbee2700f7e2014-03-07 09:46:20 -0800126void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800127 if (!Runtime::Current()->ExplicitNullChecks()) {
128 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
129 return;
130 }
131 // Force an implicit null check by performing a memory operation (load) from the given
132 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800133 RegStorage tmp = AllocTemp();
134 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800135 LIR* load = LoadWordDisp(reg, 0, tmp);
136 FreeTemp(tmp);
137 MarkSafepointPC(load);
138 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139}
140
141/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800142LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700143 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800144 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
145 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
147 // Remember branch target - will process later
148 throw_launchpads_.Insert(tgt);
149 return branch;
150}
151
152void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
153 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700154 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 ConditionCode cond;
156 switch (opcode) {
157 case Instruction::IF_EQ:
158 cond = kCondEq;
159 break;
160 case Instruction::IF_NE:
161 cond = kCondNe;
162 break;
163 case Instruction::IF_LT:
164 cond = kCondLt;
165 break;
166 case Instruction::IF_GE:
167 cond = kCondGe;
168 break;
169 case Instruction::IF_GT:
170 cond = kCondGt;
171 break;
172 case Instruction::IF_LE:
173 cond = kCondLe;
174 break;
175 default:
176 cond = static_cast<ConditionCode>(0);
177 LOG(FATAL) << "Unexpected opcode " << opcode;
178 }
179
180 // Normalize such that if either operand is constant, src2 will be constant
181 if (rl_src1.is_const) {
182 RegLocation rl_temp = rl_src1;
183 rl_src1 = rl_src2;
184 rl_src2 = rl_temp;
185 cond = FlipComparisonOrder(cond);
186 }
187
188 rl_src1 = LoadValue(rl_src1, kCoreReg);
189 // Is this really an immediate comparison?
190 if (rl_src2.is_const) {
191 // If it's already live in a register or not easily materialized, just keep going
192 RegLocation rl_temp = UpdateLoc(rl_src2);
193 if ((rl_temp.location == kLocDalvikFrame) &&
194 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
195 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800196 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197 return;
198 }
199 }
200 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800201 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202}
203
204void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700205 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 ConditionCode cond;
207 rl_src = LoadValue(rl_src, kCoreReg);
208 switch (opcode) {
209 case Instruction::IF_EQZ:
210 cond = kCondEq;
211 break;
212 case Instruction::IF_NEZ:
213 cond = kCondNe;
214 break;
215 case Instruction::IF_LTZ:
216 cond = kCondLt;
217 break;
218 case Instruction::IF_GEZ:
219 cond = kCondGe;
220 break;
221 case Instruction::IF_GTZ:
222 cond = kCondGt;
223 break;
224 case Instruction::IF_LEZ:
225 cond = kCondLe;
226 break;
227 default:
228 cond = static_cast<ConditionCode>(0);
229 LOG(FATAL) << "Unexpected opcode " << opcode;
230 }
buzbee2700f7e2014-03-07 09:46:20 -0800231 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232}
233
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700234void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
236 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800237 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800239 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 }
buzbee2700f7e2014-03-07 09:46:20 -0800241 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 StoreValueWide(rl_dest, rl_result);
243}
244
245void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700246 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700247 rl_src = LoadValue(rl_src, kCoreReg);
248 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
249 OpKind op = kOpInvalid;
250 switch (opcode) {
251 case Instruction::INT_TO_BYTE:
252 op = kOp2Byte;
253 break;
254 case Instruction::INT_TO_SHORT:
255 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700257 case Instruction::INT_TO_CHAR:
258 op = kOp2Char;
259 break;
260 default:
261 LOG(ERROR) << "Bad int conversion type";
262 }
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700264 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265}
266
267/*
268 * Let helper function take care of everything. Will call
269 * Array::AllocFromCode(type_idx, method, count);
270 * Note: AllocFromCode will handle checks for errNegativeArraySize.
271 */
272void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700275 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800276 const DexFile* dex_file = cu_->dex_file;
277 CompilerDriver* driver = cu_->compiler_driver;
278 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800280 bool is_type_initialized; // Ignored as an array does not have an initializer.
281 bool use_direct_type_ptr;
282 uintptr_t direct_type_ptr;
283 if (kEmbedClassInCode &&
284 driver->CanEmbedTypeInCode(*dex_file, type_idx,
285 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
286 // The fast path.
287 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800288 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700289 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800290 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
291 } else {
292 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700293 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800294 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
295 }
296 } else {
297 // The slow path.
298 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700299 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800300 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
301 }
302 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700304 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800305 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 RegLocation rl_result = GetReturn(false);
308 StoreValue(rl_dest, rl_result);
309}
310
311/*
312 * Similar to GenNewArray, but with post-allocation initialization.
313 * Verifier guarantees we're dealing with an array class. Current
314 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
315 * Current code also throws internal unimp if not 'L', '[' or 'I'.
316 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 int elems = info->num_arg_words;
319 int type_idx = info->index;
320 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700321 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
323 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700324 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700326 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 }
328 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
329 FreeTemp(TargetReg(kArg2));
330 FreeTemp(TargetReg(kArg1));
331 /*
332 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
333 * return region. Because AllocFromCode placed the new array
334 * in kRet0, we'll just lock it into place. When debugger support is
335 * added, it may be necessary to additionally copy all return
336 * values to a home location in thread-local storage
337 */
338 LockTemp(TargetReg(kRet0));
339
340 // TODO: use the correct component size, currently all supported types
341 // share array alignment with ints (see comment at head of function)
342 size_t component_size = sizeof(int32_t);
343
344 // Having a range of 0 is legal
345 if (info->is_range && (elems > 0)) {
346 /*
347 * Bit of ugliness here. We're going generate a mem copy loop
348 * on the register range, but it is possible that some regs
349 * in the range have been promoted. This is unlikely, but
350 * before generating the copy, we'll just force a flush
351 * of any regs in the source range that have been promoted to
352 * home location.
353 */
354 for (int i = 0; i < elems; i++) {
355 RegLocation loc = UpdateLoc(info->args[i]);
356 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800357 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 }
359 }
360 /*
361 * TUNING note: generated code here could be much improved, but
362 * this is an uncommon operation and isn't especially performance
363 * critical.
364 */
buzbee2700f7e2014-03-07 09:46:20 -0800365 RegStorage r_src = AllocTemp();
366 RegStorage r_dst = AllocTemp();
367 RegStorage r_idx = AllocTemp();
368 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700369 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 case kThumb2:
371 r_val = TargetReg(kLr);
372 break;
373 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700374 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 FreeTemp(TargetReg(kRet0));
376 r_val = AllocTemp();
377 break;
378 case kMips:
379 r_val = AllocTemp();
380 break;
381 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
382 }
383 // Set up source pointer
384 RegLocation rl_first = info->args[0];
385 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
386 // Set up the target pointer
387 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
388 mirror::Array::DataOffset(component_size).Int32Value());
389 // Set up the loop counter (known to be > 0)
390 LoadConstant(r_idx, elems - 1);
391 // Generate the copy loop. Going backwards for convenience
392 LIR* target = NewLIR0(kPseudoTargetLabel);
393 // Copy next element
394 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
395 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
396 FreeTemp(r_val);
397 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700398 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 // Restore the target pointer
400 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
401 -mirror::Array::DataOffset(component_size).Int32Value());
402 }
403 } else if (!info->is_range) {
404 // TUNING: interleave
405 for (int i = 0; i < elems; i++) {
406 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
407 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800408 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
409 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800411 if (IsTemp(rl_arg.reg)) {
412 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
414 }
415 }
416 if (info->result.location != kLocInvalid) {
417 StoreValue(info->result, GetReturn(false /* not fp */));
418 }
419}
420
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800421//
422// Slow path to ensure a class is initialized for sget/sput.
423//
424class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
425 public:
buzbee2700f7e2014-03-07 09:46:20 -0800426 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
427 RegStorage r_base) :
428 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
429 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800430 }
431
432 void Compile() {
433 LIR* unresolved_target = GenerateTargetLabel();
434 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700435 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800436 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800437 // Copy helper's result into r_base, a no-op on all but MIPS.
438 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
439
440 m2l_->OpUnconditionalBranch(cont_);
441 }
442
443 private:
444 LIR* const uninit_;
445 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800446 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800447};
448
Vladimir Markobe0e5462014-02-26 11:24:15 +0000449void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700450 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000451 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
452 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
453 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
454 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800455 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000456 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 // Fast path, static storage base is this method's class
458 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800459 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800460 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
461 if (IsTemp(rl_method.reg)) {
462 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 }
464 } else {
465 // Medium path, static storage base in a different class which requires checks that the other
466 // class is initialized.
467 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000468 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 // May do runtime call so everything to home locations.
470 FlushAllRegs();
471 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800472 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 LockTemp(r_method);
474 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800475 r_base = TargetReg(kArg0);
476 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800477 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800478 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000479 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800480 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000481 if (!field_info.IsInitialized() &&
482 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800483 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800484
485 // The slow path is invoked if the r_base is NULL or the class pointed
486 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800487 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800488 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800489 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800490 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800491 mirror::Class::StatusOffset().Int32Value(),
492 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800493 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800494
buzbee2700f7e2014-03-07 09:46:20 -0800495 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000496 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800497
498 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 FreeTemp(r_method);
501 }
502 // rBase now holds static storage base
503 if (is_long_or_double) {
504 rl_src = LoadValueWide(rl_src, kAnyReg);
505 } else {
506 rl_src = LoadValue(rl_src, kAnyReg);
507 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000508 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800509 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 GenMemBarrier(kStoreStore);
511 }
512 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800513 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800515 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000517 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800518 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 GenMemBarrier(kStoreLoad);
520 }
521 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800522 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800524 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 } else {
526 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700527 ThreadOffset<4> setter_offset =
528 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
529 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
530 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000531 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
533}
534
Vladimir Markobe0e5462014-02-26 11:24:15 +0000535void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700536 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000537 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
538 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
539 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
540 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800541 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000542 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 // Fast path, static storage base is this method's class
544 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800545 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800546 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 } else {
548 // Medium path, static storage base in a different class which requires checks that the other
549 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000550 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 // May do runtime call so everything to home locations.
552 FlushAllRegs();
553 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800554 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 LockTemp(r_method);
556 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800557 r_base = TargetReg(kArg0);
558 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800559 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800560 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800562 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000563 if (!field_info.IsInitialized() &&
564 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800565 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800566
567 // The slow path is invoked if the r_base is NULL or the class pointed
568 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800570 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800571 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800572 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800573 mirror::Class::StatusOffset().Int32Value(),
574 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800575 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800576
buzbee2700f7e2014-03-07 09:46:20 -0800577 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000578 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800579
580 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 FreeTemp(r_method);
583 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800584 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800586
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800588 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800590 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800592 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800593
594 if (field_info.IsVolatile()) {
595 // Without context sensitive analysis, we must issue the most conservative barriers.
596 // In this case, either a load or store may follow so we issue both barriers.
597 GenMemBarrier(kLoadLoad);
598 GenMemBarrier(kLoadStore);
599 }
600
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 if (is_long_or_double) {
602 StoreValueWide(rl_dest, rl_result);
603 } else {
604 StoreValue(rl_dest, rl_result);
605 }
606 } else {
607 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700608 ThreadOffset<4> getterOffset =
609 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
610 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
611 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000612 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 if (is_long_or_double) {
614 RegLocation rl_result = GetReturnWide(rl_dest.fp);
615 StoreValueWide(rl_dest, rl_result);
616 } else {
617 RegLocation rl_result = GetReturn(rl_dest.fp);
618 StoreValue(rl_dest, rl_result);
619 }
620 }
621}
622
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800623// Generate code for all slow paths.
624void Mir2Lir::HandleSlowPaths() {
625 int n = slow_paths_.Size();
626 for (int i = 0; i < n; ++i) {
627 LIRSlowPath* slowpath = slow_paths_.Get(i);
628 slowpath->Compile();
629 }
630 slow_paths_.Reset();
631}
632
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700633void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700635 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 for (int i = 0; i < num_elems; i++) {
637 ResetRegPool();
638 ResetDefTracking();
639 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700640 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 current_dalvik_offset_ = lab->operands[1];
642 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800643 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
645 OpUnconditionalBranch(resume_lab);
646 }
647}
648
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700649void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 int num_elems = throw_launchpads_.Size();
651 for (int i = 0; i < num_elems; i++) {
652 ResetRegPool();
653 ResetDefTracking();
654 LIR* lab = throw_launchpads_.Get(i);
655 current_dalvik_offset_ = lab->operands[1];
656 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700657 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 int v1 = lab->operands[2];
659 int v2 = lab->operands[3];
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700660 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 switch (lab->operands[0]) {
662 case kThrowNullPointer:
Ian Rogersdd7624d2014-03-14 17:43:00 -0700663 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700665 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
667 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800668 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
669 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800671 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 }
673 // Make sure the following LoadConstant doesn't mess with kArg1.
674 LockTemp(TargetReg(kArg1));
675 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700676 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 break;
678 case kThrowArrayBounds:
679 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800680 if (v2 != TargetReg(kArg0).GetReg()) {
681 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 if (target_x86) {
683 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800684 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
685 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800687 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 }
689 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800690 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800692 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 if (target_x86) {
694 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800695 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
696 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800698 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 }
700 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
701 } else {
702 if (target_x86) {
703 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800704 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
705 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800707 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
buzbee2700f7e2014-03-07 09:46:20 -0800709 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 }
711 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700712 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800715 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700717 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 default:
720 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
721 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000722 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800723 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700724 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
726}
727
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700730 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000731 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
732 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
733 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 RegLocation rl_result;
735 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000736 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 rl_obj = LoadValue(rl_obj, kCoreReg);
738 if (is_long_or_double) {
739 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800740 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700741 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800743 // FIXME? duplicate null check?
744 GenNullCheck(rl_obj.reg, opt_flags);
745 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
746 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800747 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000748 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800749 // Without context sensitive analysis, we must issue the most conservative barriers.
750 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800752 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 }
754 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800755 RegStorage reg_ptr = AllocTemp();
756 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800758 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700759 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000760 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800761 // Without context sensitive analysis, we must issue the most conservative barriers.
762 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800764 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 }
766 FreeTemp(reg_ptr);
767 }
768 StoreValueWide(rl_dest, rl_result);
769 } else {
770 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800771 GenNullCheck(rl_obj.reg, opt_flags);
772 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
773 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800774 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000775 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800776 // Without context sensitive analysis, we must issue the most conservative barriers.
777 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800779 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 }
781 StoreValue(rl_dest, rl_result);
782 }
783 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700784 ThreadOffset<4> getterOffset =
785 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
786 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
787 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 if (is_long_or_double) {
790 RegLocation rl_result = GetReturnWide(rl_dest.fp);
791 StoreValueWide(rl_dest, rl_result);
792 } else {
793 RegLocation rl_result = GetReturn(rl_dest.fp);
794 StoreValue(rl_dest, rl_result);
795 }
796 }
797}
798
Vladimir Markobe0e5462014-02-26 11:24:15 +0000799void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700801 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000802 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
803 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
804 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000806 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 rl_obj = LoadValue(rl_obj, kCoreReg);
808 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800810 GenNullCheck(rl_obj.reg, opt_flags);
811 RegStorage reg_ptr = AllocTemp();
812 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000813 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800814 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 GenMemBarrier(kStoreStore);
816 }
buzbee2700f7e2014-03-07 09:46:20 -0800817 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800818 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000819 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800820 // A load might follow the volatile store so insert a StoreLoad barrier.
821 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 }
823 FreeTemp(reg_ptr);
824 } else {
825 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800826 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000827 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800828 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 GenMemBarrier(kStoreStore);
830 }
buzbee2700f7e2014-03-07 09:46:20 -0800831 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800832 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800834 // A load might follow the volatile store so insert a StoreLoad barrier.
835 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 }
837 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800838 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 }
840 }
841 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700842 ThreadOffset<4> setter_offset =
843 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
844 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
845 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000846 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
847 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 }
849}
850
Ian Rogersa9a82542013-10-04 11:17:26 -0700851void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
852 RegLocation rl_src) {
853 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
854 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
855 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700856 ThreadOffset<4> helper = needs_range_check
857 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
858 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
859 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700860 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
861}
862
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700863void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800865 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
867 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
868 *cu_->dex_file,
869 type_idx)) {
870 // Call out to helper which resolves type and verifies access.
871 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700872 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800873 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 RegLocation rl_result = GetReturn(false);
875 StoreValue(rl_dest, rl_result);
876 } else {
877 // We're don't need access checks, load type from dex cache
878 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700879 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800880 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 int32_t offset_of_type =
882 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
883 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800884 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
886 type_idx) || SLOW_TYPE_PATH) {
887 // Slow path, at runtime test if type is null and if so initialize
888 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800889 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800890 LIR* cont = NewLIR0(kPseudoTargetLabel);
891
892 // Object to generate the slow path for class resolution.
893 class SlowPath : public LIRSlowPath {
894 public:
895 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
896 const RegLocation& rl_method, const RegLocation& rl_result) :
897 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
898 rl_method_(rl_method), rl_result_(rl_result) {
899 }
900
901 void Compile() {
902 GenerateTargetLabel();
903
Ian Rogersdd7624d2014-03-14 17:43:00 -0700904 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800905 rl_method_.reg, true);
906 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800907
908 m2l_->OpUnconditionalBranch(cont_);
909 }
910
911 private:
912 const int type_idx_;
913 const RegLocation rl_method_;
914 const RegLocation rl_result_;
915 };
916
917 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800918 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800919
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800921 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 // Fast path, we're done - just store result
923 StoreValue(rl_dest, rl_result);
924 }
925 }
926}
927
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700928void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700929 /* NOTE: Most strings should be available at compile time */
930 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
931 (sizeof(mirror::String*) * string_idx);
932 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
933 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
934 // slow path, resolve string if not in dex cache
935 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700936 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800937
938 // If the Method* is already in a register, we can save a copy.
939 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800940 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800941 if (rl_method.location == kLocPhysReg) {
942 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800943 DCHECK(!IsTemp(rl_method.reg));
944 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800945 } else {
946 r_method = TargetReg(kArg2);
947 LoadCurrMethodDirect(r_method);
948 }
949 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
950 TargetReg(kArg0));
951
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954 if (cu_->instruction_set == kThumb2 ||
955 cu_->instruction_set == kMips) {
956 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800957 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
959 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800961
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800962 // Object to generate the slow path for string resolution.
963 class SlowPath : public LIRSlowPath {
964 public:
buzbee2700f7e2014-03-07 09:46:20 -0800965 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800966 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
967 }
968
969 void Compile() {
970 GenerateTargetLabel();
971
Dave Allisonf9487c02014-04-08 23:08:12 +0000972 const CompilerOptions& compiler_options =
973 m2l_->cu_->compiler_driver->GetCompilerOptions();
974 if (compiler_options.GenerateHelperTrampolines()) {
975 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_);
976 m2l_->CallHelper(RegStorage::InvalidReg(), QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
977 true);
978 } else {
979 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800980
Dave Allisonf9487c02014-04-08 23:08:12 +0000981 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_);
982 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
983 m2l_->MarkSafepointPC(call_inst);
984 m2l_->FreeTemp(r_tgt);
985 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800986
987 m2l_->OpUnconditionalBranch(cont_);
988 }
989
990 private:
buzbee2700f7e2014-03-07 09:46:20 -0800991 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800992 };
993
994 // Add to list for future.
995 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700997 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -0800998 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
999 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001000 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001001 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001002 LIR* target = NewLIR0(kPseudoTargetLabel);
1003 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 }
1005 GenBarrier();
1006 StoreValue(rl_dest, GetReturn(false));
1007 } else {
1008 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001009 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001011 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1012 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 StoreValue(rl_dest, rl_result);
1014 }
1015}
1016
1017/*
1018 * Let helper function take care of everything. Will
1019 * call Class::NewInstanceFromCode(type_idx, method);
1020 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001021void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 FlushAllRegs(); /* Everything to home location */
1023 // alloc will always check for resolution, do we also need to verify
1024 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001025 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001026 const DexFile* dex_file = cu_->dex_file;
1027 CompilerDriver* driver = cu_->compiler_driver;
1028 if (driver->CanAccessInstantiableTypeWithoutChecks(
1029 cu_->method_idx, *dex_file, type_idx)) {
1030 bool is_type_initialized;
1031 bool use_direct_type_ptr;
1032 uintptr_t direct_type_ptr;
1033 if (kEmbedClassInCode &&
1034 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1035 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1036 // The fast path.
1037 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001038 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001039 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 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), 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 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1045 }
1046 } else {
1047 // Use the direct pointer.
1048 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001049 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001050 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1051 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001052 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1054 }
1055 }
1056 } else {
1057 // The slow path.
1058 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001059 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001060 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1061 }
1062 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001064 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 RegLocation rl_result = GetReturn(false);
1068 StoreValue(rl_dest, rl_result);
1069}
1070
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001071void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001073 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074}
1075
1076// For final classes there are no sub-classes to check and so we can answer the instance-of
1077// question with simple comparisons.
1078void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1079 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001080 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001081 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001082
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 RegLocation object = LoadValue(rl_src, kCoreReg);
1084 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001085 RegStorage result_reg = rl_result.reg;
1086 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 result_reg = AllocTypedTemp(false, kCoreReg);
1088 }
1089 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001090 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091
buzbee2700f7e2014-03-07 09:46:20 -08001092 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1093 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094
1095 LoadCurrMethodDirect(check_class);
1096 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001097 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1098 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001100 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001102 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 int32_t offset_of_type =
1104 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1105 (sizeof(mirror::Class*) * type_idx);
1106 LoadWordDisp(check_class, offset_of_type, check_class);
1107 }
1108
1109 LIR* ne_branchover = NULL;
1110 if (cu_->instruction_set == kThumb2) {
1111 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001112 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001114 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 } else {
1116 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1117 LoadConstant(result_reg, 1); // eq case - load true
1118 }
1119 LIR* target = NewLIR0(kPseudoTargetLabel);
1120 null_branchover->target = target;
1121 if (ne_branchover != NULL) {
1122 ne_branchover->target = target;
1123 }
1124 FreeTemp(object_class);
1125 FreeTemp(check_class);
1126 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001127 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 FreeTemp(result_reg);
1129 }
1130 StoreValue(rl_dest, rl_result);
1131}
1132
1133void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1134 bool type_known_abstract, bool use_declaring_class,
1135 bool can_assume_type_is_in_dex_cache,
1136 uint32_t type_idx, RegLocation rl_dest,
1137 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001138 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001139 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001140
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 FlushAllRegs();
1142 // May generate a call - use explicit registers
1143 LockCallTemps();
1144 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001145 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 if (needs_access_check) {
1147 // Check we have access to type_idx and if not throw IllegalAccessError,
1148 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001149 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 type_idx, true);
1151 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1152 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1153 } else if (use_declaring_class) {
1154 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001155 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1156 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 } else {
1158 // Load dex cache entry into class_reg (kArg2)
1159 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001160 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1161 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 int32_t offset_of_type =
1163 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1164 * type_idx);
1165 LoadWordDisp(class_reg, offset_of_type, class_reg);
1166 if (!can_assume_type_is_in_dex_cache) {
1167 // Need to test presence of type in dex cache at runtime
1168 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1169 // Not resolved
1170 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001171 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001172 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1174 // Rejoin code paths
1175 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1176 hop_branch->target = hop_target;
1177 }
1178 }
1179 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1180 RegLocation rl_result = GetReturn(false);
1181 if (cu_->instruction_set == kMips) {
1182 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001183 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 }
1185 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1186
1187 /* load object->klass_ */
1188 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1189 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1190 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1191 LIR* branchover = NULL;
1192 if (type_known_final) {
1193 // rl_result == ref == null == 0.
1194 if (cu_->instruction_set == kThumb2) {
1195 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001196 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001197 LoadConstant(rl_result.reg, 1); // .eq case - load true
1198 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001199 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001201 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001203 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 }
1205 } else {
1206 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001207 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001208 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 if (!type_known_abstract) {
1210 /* Uses conditional nullification */
1211 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001212 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1214 }
1215 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1216 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001217 if (it != nullptr) {
1218 OpEndIT(it);
1219 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 FreeTemp(r_tgt);
1221 } else {
1222 if (!type_known_abstract) {
1223 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001224 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1226 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001227 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001228 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1229 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1230 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 }
1232 }
1233 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001234 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 /* branch targets here */
1236 LIR* target = NewLIR0(kPseudoTargetLabel);
1237 StoreValue(rl_dest, rl_result);
1238 branch1->target = target;
1239 if (branchover != NULL) {
1240 branchover->target = target;
1241 }
1242}
1243
1244void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1245 bool type_known_final, type_known_abstract, use_declaring_class;
1246 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1247 *cu_->dex_file,
1248 type_idx,
1249 &type_known_final,
1250 &type_known_abstract,
1251 &use_declaring_class);
1252 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1253 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1254
1255 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1256 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1257 } else {
1258 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1259 use_declaring_class, can_assume_type_is_in_dex_cache,
1260 type_idx, rl_dest, rl_src);
1261 }
1262}
1263
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001264void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 bool type_known_final, type_known_abstract, use_declaring_class;
1266 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1267 *cu_->dex_file,
1268 type_idx,
1269 &type_known_final,
1270 &type_known_abstract,
1271 &use_declaring_class);
1272 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1273 // of the exception throw path.
1274 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001275 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 // Verifier type analysis proved this check cast would never cause an exception.
1277 return;
1278 }
1279 FlushAllRegs();
1280 // May generate a call - use explicit registers
1281 LockCallTemps();
1282 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001283 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 if (needs_access_check) {
1285 // Check we have access to type_idx and if not throw IllegalAccessError,
1286 // returns Class* in kRet0
1287 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001288 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 type_idx, TargetReg(kArg1), true);
1290 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1291 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001292 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1293 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 } else {
1295 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001296 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1297 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 int32_t offset_of_type =
1299 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1300 (sizeof(mirror::Class*) * type_idx);
1301 LoadWordDisp(class_reg, offset_of_type, class_reg);
1302 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1303 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001304 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1305 LIR* cont = NewLIR0(kPseudoTargetLabel);
1306
1307 // Slow path to initialize the type. Executed if the type is NULL.
1308 class SlowPath : public LIRSlowPath {
1309 public:
1310 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001311 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001312 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1313 class_reg_(class_reg) {
1314 }
1315
1316 void Compile() {
1317 GenerateTargetLabel();
1318
1319 // Call out to helper, which will return resolved type in kArg0
1320 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001321 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001322 m2l_->TargetReg(kArg1), true);
1323 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1324 m2l_->OpUnconditionalBranch(cont_);
1325 }
1326 public:
1327 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001328 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001329 };
1330
buzbee2700f7e2014-03-07 09:46:20 -08001331 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
1333 }
1334 // At this point, class_reg (kArg2) has class
1335 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001336
1337 // Slow path for the case where the classes are not equal. In this case we need
1338 // to call a helper function to do the check.
1339 class SlowPath : public LIRSlowPath {
1340 public:
1341 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1342 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1343 }
1344
1345 void Compile() {
1346 GenerateTargetLabel();
1347
1348 if (load_) {
1349 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1350 m2l_->TargetReg(kArg1));
1351 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001352 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001353 m2l_->TargetReg(kArg1), true);
1354
1355 m2l_->OpUnconditionalBranch(cont_);
1356 }
1357
1358 private:
1359 bool load_;
1360 };
1361
1362 if (type_known_abstract) {
1363 // Easier case, run slow path if target is non-null (slow path will load from target)
1364 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1365 LIR* cont = NewLIR0(kPseudoTargetLabel);
1366 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1367 } else {
1368 // Harder, more common case. We need to generate a forward branch over the load
1369 // if the target is null. If it's non-null we perform the load and branch to the
1370 // slow path if the classes are not equal.
1371
1372 /* Null is OK - continue */
1373 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1374 /* load object->klass_ */
1375 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001376 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001377
1378 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1379 LIR* cont = NewLIR0(kPseudoTargetLabel);
1380
1381 // Add the slow path that will not perform load since this is already done.
1382 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1383
1384 // Set the null check to branch to the continuation.
1385 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 }
1387}
1388
1389void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001390 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 RegLocation rl_result;
1392 if (cu_->instruction_set == kThumb2) {
1393 /*
1394 * NOTE: This is the one place in the code in which we might have
1395 * as many as six live temporary registers. There are 5 in the normal
1396 * set for Arm. Until we have spill capabilities, temporarily add
1397 * lr to the temp set. It is safe to do this locally, but note that
1398 * lr is used explicitly elsewhere in the code generator and cannot
1399 * normally be used as a general temp register.
1400 */
1401 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1402 FreeTemp(TargetReg(kLr)); // and make it available
1403 }
1404 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1405 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1406 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1407 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001408 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1409 RegStorage t_reg = AllocTemp();
1410 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1411 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1412 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 FreeTemp(t_reg);
1414 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001415 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1416 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 }
1418 /*
1419 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1420 * following StoreValueWide might need to allocate a temp register.
1421 * To further work around the lack of a spill capability, explicitly
1422 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1423 * Remove when spill is functional.
1424 */
1425 FreeRegLocTemps(rl_result, rl_src1);
1426 FreeRegLocTemps(rl_result, rl_src2);
1427 StoreValueWide(rl_dest, rl_result);
1428 if (cu_->instruction_set == kThumb2) {
1429 Clobber(TargetReg(kLr));
1430 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1431 }
1432}
1433
1434
1435void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001436 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001437 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438
1439 switch (opcode) {
1440 case Instruction::SHL_LONG:
1441 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001442 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 break;
1444 case Instruction::SHR_LONG:
1445 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001446 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 break;
1448 case Instruction::USHR_LONG:
1449 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001450 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 break;
1452 default:
1453 LOG(FATAL) << "Unexpected case";
1454 }
1455 FlushAllRegs(); /* Send everything to home location */
1456 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1457 RegLocation rl_result = GetReturnWide(false);
1458 StoreValueWide(rl_dest, rl_result);
1459}
1460
1461
1462void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001463 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001464 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 OpKind op = kOpBkpt;
1466 bool is_div_rem = false;
1467 bool check_zero = false;
1468 bool unary = false;
1469 RegLocation rl_result;
1470 bool shift_op = false;
1471 switch (opcode) {
1472 case Instruction::NEG_INT:
1473 op = kOpNeg;
1474 unary = true;
1475 break;
1476 case Instruction::NOT_INT:
1477 op = kOpMvn;
1478 unary = true;
1479 break;
1480 case Instruction::ADD_INT:
1481 case Instruction::ADD_INT_2ADDR:
1482 op = kOpAdd;
1483 break;
1484 case Instruction::SUB_INT:
1485 case Instruction::SUB_INT_2ADDR:
1486 op = kOpSub;
1487 break;
1488 case Instruction::MUL_INT:
1489 case Instruction::MUL_INT_2ADDR:
1490 op = kOpMul;
1491 break;
1492 case Instruction::DIV_INT:
1493 case Instruction::DIV_INT_2ADDR:
1494 check_zero = true;
1495 op = kOpDiv;
1496 is_div_rem = true;
1497 break;
1498 /* NOTE: returns in kArg1 */
1499 case Instruction::REM_INT:
1500 case Instruction::REM_INT_2ADDR:
1501 check_zero = true;
1502 op = kOpRem;
1503 is_div_rem = true;
1504 break;
1505 case Instruction::AND_INT:
1506 case Instruction::AND_INT_2ADDR:
1507 op = kOpAnd;
1508 break;
1509 case Instruction::OR_INT:
1510 case Instruction::OR_INT_2ADDR:
1511 op = kOpOr;
1512 break;
1513 case Instruction::XOR_INT:
1514 case Instruction::XOR_INT_2ADDR:
1515 op = kOpXor;
1516 break;
1517 case Instruction::SHL_INT:
1518 case Instruction::SHL_INT_2ADDR:
1519 shift_op = true;
1520 op = kOpLsl;
1521 break;
1522 case Instruction::SHR_INT:
1523 case Instruction::SHR_INT_2ADDR:
1524 shift_op = true;
1525 op = kOpAsr;
1526 break;
1527 case Instruction::USHR_INT:
1528 case Instruction::USHR_INT_2ADDR:
1529 shift_op = true;
1530 op = kOpLsr;
1531 break;
1532 default:
1533 LOG(FATAL) << "Invalid word arith op: " << opcode;
1534 }
1535 if (!is_div_rem) {
1536 if (unary) {
1537 rl_src1 = LoadValue(rl_src1, kCoreReg);
1538 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001539 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001540 } else {
1541 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001542 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001543 RegStorage t_reg = AllocTemp();
1544 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 rl_src1 = LoadValue(rl_src1, kCoreReg);
1546 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001547 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 FreeTemp(t_reg);
1549 } else {
1550 rl_src1 = LoadValue(rl_src1, kCoreReg);
1551 rl_src2 = LoadValue(rl_src2, kCoreReg);
1552 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001553 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 }
1555 }
1556 StoreValue(rl_dest, rl_result);
1557 } else {
Dave Allison70202782013-10-22 17:52:19 -07001558 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 -07001559 if (cu_->instruction_set == kMips) {
1560 rl_src1 = LoadValue(rl_src1, kCoreReg);
1561 rl_src2 = LoadValue(rl_src2, kCoreReg);
1562 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001563 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 }
buzbee2700f7e2014-03-07 09:46:20 -08001565 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001566 done = true;
1567 } else if (cu_->instruction_set == kThumb2) {
1568 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1569 // Use ARM SDIV instruction for division. For remainder we also need to
1570 // calculate using a MUL and subtract.
1571 rl_src1 = LoadValue(rl_src1, kCoreReg);
1572 rl_src2 = LoadValue(rl_src2, kCoreReg);
1573 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001574 AddDivZeroSlowPath(kCondEq, rl_src2.reg, 0);
Dave Allison70202782013-10-22 17:52:19 -07001575 }
buzbee2700f7e2014-03-07 09:46:20 -08001576 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001577 done = true;
1578 }
1579 }
1580
1581 // If we haven't already generated the code use the callout function.
1582 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001583 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 FlushAllRegs(); /* Send everything to home location */
1585 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001586 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1588 if (check_zero) {
Mingyao Yang42894562014-04-07 12:42:16 -07001589 AddDivZeroSlowPath(kCondEq, TargetReg(kArg1), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 }
Dave Allison70202782013-10-22 17:52:19 -07001591 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001592 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 if (op == kOpDiv)
1594 rl_result = GetReturn(false);
1595 else
1596 rl_result = GetReturnAlt();
1597 }
1598 StoreValue(rl_dest, rl_result);
1599 }
1600}
1601
1602/*
1603 * The following are the first-level codegen routines that analyze the format
1604 * of each bytecode then either dispatch special purpose codegen routines
1605 * or produce corresponding Thumb instructions directly.
1606 */
1607
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001609static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 x &= x - 1;
1611 return (x & (x - 1)) == 0;
1612}
1613
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1615// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001616bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001617 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1619 return false;
1620 }
1621 // No divide instruction for Arm, so check for more special cases
1622 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001623 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 }
1625 int k = LowestSetBit(lit);
1626 if (k >= 30) {
1627 // Avoid special cases.
1628 return false;
1629 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 rl_src = LoadValue(rl_src, kCoreReg);
1631 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001632 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001633 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634 if (lit == 2) {
1635 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001636 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1637 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1638 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001640 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001642 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1643 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 }
1645 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001646 RegStorage t_reg1 = AllocTemp();
1647 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001649 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1650 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001652 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001654 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001656 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001657 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001658 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 }
1660 }
1661 StoreValue(rl_dest, rl_result);
1662 return true;
1663}
1664
1665// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1666// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001667bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001668 if (lit < 0) {
1669 return false;
1670 }
1671 if (lit == 0) {
1672 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1673 LoadConstant(rl_result.reg, 0);
1674 StoreValue(rl_dest, rl_result);
1675 return true;
1676 }
1677 if (lit == 1) {
1678 rl_src = LoadValue(rl_src, kCoreReg);
1679 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1680 OpRegCopy(rl_result.reg, rl_src.reg);
1681 StoreValue(rl_dest, rl_result);
1682 return true;
1683 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001684 // There is RegRegRegShift on Arm, so check for more special cases
1685 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001686 return EasyMultiply(rl_src, rl_dest, lit);
1687 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 // Can we simplify this multiplication?
1689 bool power_of_two = false;
1690 bool pop_count_le2 = false;
1691 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001692 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 power_of_two = true;
1694 } else if (IsPopCountLE2(lit)) {
1695 pop_count_le2 = true;
1696 } else if (IsPowerOfTwo(lit + 1)) {
1697 power_of_two_minus_one = true;
1698 } else {
1699 return false;
1700 }
1701 rl_src = LoadValue(rl_src, kCoreReg);
1702 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1703 if (power_of_two) {
1704 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 } else if (pop_count_le2) {
1707 // Shift and add and shift.
1708 int first_bit = LowestSetBit(lit);
1709 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1710 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1711 } else {
1712 // Reverse subtract: (src << (shift + 1)) - src.
1713 DCHECK(power_of_two_minus_one);
1714 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001715 RegStorage t_reg = AllocTemp();
1716 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1717 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 }
1719 StoreValue(rl_dest, rl_result);
1720 return true;
1721}
1722
1723void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001724 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 RegLocation rl_result;
1726 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1727 int shift_op = false;
1728 bool is_div = false;
1729
1730 switch (opcode) {
1731 case Instruction::RSUB_INT_LIT8:
1732 case Instruction::RSUB_INT: {
1733 rl_src = LoadValue(rl_src, kCoreReg);
1734 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1735 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001736 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001737 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001738 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1739 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 }
1741 StoreValue(rl_dest, rl_result);
1742 return;
1743 }
1744
1745 case Instruction::SUB_INT:
1746 case Instruction::SUB_INT_2ADDR:
1747 lit = -lit;
1748 // Intended fallthrough
1749 case Instruction::ADD_INT:
1750 case Instruction::ADD_INT_2ADDR:
1751 case Instruction::ADD_INT_LIT8:
1752 case Instruction::ADD_INT_LIT16:
1753 op = kOpAdd;
1754 break;
1755 case Instruction::MUL_INT:
1756 case Instruction::MUL_INT_2ADDR:
1757 case Instruction::MUL_INT_LIT8:
1758 case Instruction::MUL_INT_LIT16: {
1759 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1760 return;
1761 }
1762 op = kOpMul;
1763 break;
1764 }
1765 case Instruction::AND_INT:
1766 case Instruction::AND_INT_2ADDR:
1767 case Instruction::AND_INT_LIT8:
1768 case Instruction::AND_INT_LIT16:
1769 op = kOpAnd;
1770 break;
1771 case Instruction::OR_INT:
1772 case Instruction::OR_INT_2ADDR:
1773 case Instruction::OR_INT_LIT8:
1774 case Instruction::OR_INT_LIT16:
1775 op = kOpOr;
1776 break;
1777 case Instruction::XOR_INT:
1778 case Instruction::XOR_INT_2ADDR:
1779 case Instruction::XOR_INT_LIT8:
1780 case Instruction::XOR_INT_LIT16:
1781 op = kOpXor;
1782 break;
1783 case Instruction::SHL_INT_LIT8:
1784 case Instruction::SHL_INT:
1785 case Instruction::SHL_INT_2ADDR:
1786 lit &= 31;
1787 shift_op = true;
1788 op = kOpLsl;
1789 break;
1790 case Instruction::SHR_INT_LIT8:
1791 case Instruction::SHR_INT:
1792 case Instruction::SHR_INT_2ADDR:
1793 lit &= 31;
1794 shift_op = true;
1795 op = kOpAsr;
1796 break;
1797 case Instruction::USHR_INT_LIT8:
1798 case Instruction::USHR_INT:
1799 case Instruction::USHR_INT_2ADDR:
1800 lit &= 31;
1801 shift_op = true;
1802 op = kOpLsr;
1803 break;
1804
1805 case Instruction::DIV_INT:
1806 case Instruction::DIV_INT_2ADDR:
1807 case Instruction::DIV_INT_LIT8:
1808 case Instruction::DIV_INT_LIT16:
1809 case Instruction::REM_INT:
1810 case Instruction::REM_INT_2ADDR:
1811 case Instruction::REM_INT_LIT8:
1812 case Instruction::REM_INT_LIT16: {
1813 if (lit == 0) {
Mingyao Yang42894562014-04-07 12:42:16 -07001814 AddDivZeroSlowPath(kCondAl, RegStorage::InvalidReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 return;
1816 }
buzbee11b63d12013-08-27 07:34:17 -07001817 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001819 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 (opcode == Instruction::DIV_INT_LIT16)) {
1821 is_div = true;
1822 } else {
1823 is_div = false;
1824 }
buzbee11b63d12013-08-27 07:34:17 -07001825 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1826 return;
1827 }
Dave Allison70202782013-10-22 17:52:19 -07001828
1829 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 if (cu_->instruction_set == kMips) {
1831 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001832 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001833 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001834 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001835 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1836 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001837 } else if (cu_->instruction_set == kThumb2) {
1838 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1839 // Use ARM SDIV instruction for division. For remainder we also need to
1840 // calculate using a MUL and subtract.
1841 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001842 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001843 done = true;
1844 }
1845 }
1846
1847 if (!done) {
1848 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1850 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001851 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1853 if (is_div)
1854 rl_result = GetReturn(false);
1855 else
1856 rl_result = GetReturnAlt();
1857 }
1858 StoreValue(rl_dest, rl_result);
1859 return;
1860 }
1861 default:
1862 LOG(FATAL) << "Unexpected opcode " << opcode;
1863 }
1864 rl_src = LoadValue(rl_src, kCoreReg);
1865 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001866 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001868 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001869 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001870 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001871 }
1872 StoreValue(rl_dest, rl_result);
1873}
1874
1875void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001876 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 RegLocation rl_result;
1878 OpKind first_op = kOpBkpt;
1879 OpKind second_op = kOpBkpt;
1880 bool call_out = false;
1881 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001882 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001883 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001884
1885 switch (opcode) {
1886 case Instruction::NOT_LONG:
1887 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1888 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1889 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001890 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1891 RegStorage t_reg = AllocTemp();
1892 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1893 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1894 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 FreeTemp(t_reg);
1896 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001897 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1898 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 }
1900 StoreValueWide(rl_dest, rl_result);
1901 return;
1902 case Instruction::ADD_LONG:
1903 case Instruction::ADD_LONG_2ADDR:
1904 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001905 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 return;
1907 }
1908 first_op = kOpAdd;
1909 second_op = kOpAdc;
1910 break;
1911 case Instruction::SUB_LONG:
1912 case Instruction::SUB_LONG_2ADDR:
1913 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001914 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 return;
1916 }
1917 first_op = kOpSub;
1918 second_op = kOpSbc;
1919 break;
1920 case Instruction::MUL_LONG:
1921 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001922 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001923 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 return;
1925 } else {
1926 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001927 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001928 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 }
1930 break;
1931 case Instruction::DIV_LONG:
1932 case Instruction::DIV_LONG_2ADDR:
1933 call_out = true;
1934 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001935 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001936 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 break;
1938 case Instruction::REM_LONG:
1939 case Instruction::REM_LONG_2ADDR:
1940 call_out = true;
1941 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001942 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001943 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001944 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 break;
1946 case Instruction::AND_LONG_2ADDR:
1947 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001948 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001949 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 }
1951 first_op = kOpAnd;
1952 second_op = kOpAnd;
1953 break;
1954 case Instruction::OR_LONG:
1955 case Instruction::OR_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 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 return;
1959 }
1960 first_op = kOpOr;
1961 second_op = kOpOr;
1962 break;
1963 case Instruction::XOR_LONG:
1964 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001965 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001966 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 return;
1968 }
1969 first_op = kOpXor;
1970 second_op = kOpXor;
1971 break;
1972 case Instruction::NEG_LONG: {
1973 GenNegLong(rl_dest, rl_src2);
1974 return;
1975 }
1976 default:
1977 LOG(FATAL) << "Invalid long arith op";
1978 }
1979 if (!call_out) {
1980 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1981 } else {
1982 FlushAllRegs(); /* Send everything to home location */
1983 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001984 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1985 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1986 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1987 RegStorage r_tgt = CallHelperSetup(func_offset);
1988 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1989 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 // NOTE: callout here is not a safepoint
1991 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1992 } else {
1993 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1994 }
1995 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001996 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 rl_result = GetReturnWide(false);
1998 else
1999 rl_result = GetReturnWideAlt();
2000 StoreValueWide(rl_dest, rl_result);
2001 }
2002}
2003
Ian Rogersdd7624d2014-03-14 17:43:00 -07002004void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002005 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 /*
2007 * Don't optimize the register usage since it calls out to support
2008 * functions
2009 */
2010 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2012 if (rl_dest.wide) {
2013 RegLocation rl_result;
2014 rl_result = GetReturnWide(rl_dest.fp);
2015 StoreValueWide(rl_dest, rl_result);
2016 } else {
2017 RegLocation rl_result;
2018 rl_result = GetReturn(rl_dest.fp);
2019 StoreValue(rl_dest, rl_result);
2020 }
2021}
2022
2023/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002024void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002025 if (Runtime::Current()->ExplicitSuspendChecks()) {
2026 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2027 return;
2028 }
2029 FlushAllRegs();
2030 LIR* branch = OpTestSuspend(NULL);
2031 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2032 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2033 current_dalvik_offset_);
2034 branch->target = target;
2035 suspend_launchpads_.Insert(target);
2036 } else {
2037 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2038 return;
2039 }
2040 FlushAllRegs(); // TODO: needed?
2041 LIR* inst = CheckSuspendUsingLoad();
2042 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044}
2045
2046/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002047void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002048 if (Runtime::Current()->ExplicitSuspendChecks()) {
2049 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2050 OpUnconditionalBranch(target);
2051 return;
2052 }
2053 OpTestSuspend(target);
2054 LIR* launch_pad =
2055 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2056 current_dalvik_offset_);
2057 FlushAllRegs();
2058 OpUnconditionalBranch(launch_pad);
2059 suspend_launchpads_.Insert(launch_pad);
2060 } else {
2061 // For the implicit suspend check, just perform the trigger
2062 // load and branch to the target.
2063 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2064 OpUnconditionalBranch(target);
2065 return;
2066 }
2067 FlushAllRegs();
2068 LIR* inst = CheckSuspendUsingLoad();
2069 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002071 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072}
2073
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002074/* Call out to helper assembly routine that will null check obj and then lock it. */
2075void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2076 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002077 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002078}
2079
2080/* Call out to helper assembly routine that will null check obj and then unlock it. */
2081void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2082 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002083 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002084}
2085
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002086/* Generic code for generating a wide constant into a VR. */
2087void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2088 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002089 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002090 StoreValueWide(rl_dest, rl_result);
2091}
2092
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093} // namespace art