blob: 0d938d91cdaa01f24966fdf6a04870a9da8fd9c5 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom7940e442013-07-12 13:46:57 -070016#include "dex/compiler_ir.h"
17#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070018#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
buzbee2700f7e2014-03-07 09:46:20 -080045LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
46 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 LIR* branch;
48 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080049 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
50 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 branch = OpUnconditionalBranch(tgt);
52 } else {
buzbee2700f7e2014-03-07 09:46:20 -080053 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
55 }
56 // Remember branch target - will process later
57 throw_launchpads_.Insert(tgt);
58 return branch;
59}
60
Mingyao Yange643a172014-04-08 11:02:52 -070061void Mir2Lir::GenDivZeroException() {
62 LIR* branch = OpUnconditionalBranch(nullptr);
63 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070067 LIR* branch = OpCondBranch(c_code, nullptr);
68 AddDivZeroCheckSlowPath(branch);
69}
70
Mingyao Yange643a172014-04-08 11:02:52 -070071void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
72 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070073 AddDivZeroCheckSlowPath(branch);
74}
75
76void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
77 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
78 public:
79 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
80 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
81 }
82
Mingyao Yange643a172014-04-08 11:02:52 -070083 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070084 m2l_->ResetRegPool();
85 m2l_->ResetDefTracking();
86 GenerateTargetLabel();
87 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
88 }
89 };
90
91 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
92}
Dave Allisonb373e092014-02-20 16:06:36 -080093
Mingyao Yange643a172014-04-08 11:02:52 -070094LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
95 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
96 public:
97 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
98 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
99 }
100
101 void Compile() OVERRIDE {
102 m2l_->ResetRegPool();
103 m2l_->ResetDefTracking();
104 GenerateTargetLabel();
105 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
106 }
107 };
108
109 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
110 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
111 return branch;
112}
113
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800115LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800116 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700117 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 }
Dave Allisonb373e092014-02-20 16:06:36 -0800119 return nullptr;
120}
121
Dave Allisonf9439142014-03-27 15:10:22 -0700122/* Perform an explicit null-check on a register. */
123LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
124 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
125 return NULL;
126 }
Mingyao Yange643a172014-04-08 11:02:52 -0700127 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700128}
129
Dave Allisonb373e092014-02-20 16:06:36 -0800130void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
131 if (!Runtime::Current()->ExplicitNullChecks()) {
132 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
133 return;
134 }
135 MarkSafepointPC(last_lir_insn_);
136 }
137}
138
139void Mir2Lir::MarkPossibleStackOverflowException() {
140 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
141 MarkSafepointPC(last_lir_insn_);
142 }
143}
144
buzbee2700f7e2014-03-07 09:46:20 -0800145void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800146 if (!Runtime::Current()->ExplicitNullChecks()) {
147 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
148 return;
149 }
150 // Force an implicit null check by performing a memory operation (load) from the given
151 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800152 RegStorage tmp = AllocTemp();
153 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800154 LIR* load = LoadWordDisp(reg, 0, tmp);
155 FreeTemp(tmp);
156 MarkSafepointPC(load);
157 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158}
159
160/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800161LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700162 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800163 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
164 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
166 // Remember branch target - will process later
167 throw_launchpads_.Insert(tgt);
168 return branch;
169}
170
171void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
172 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 ConditionCode cond;
175 switch (opcode) {
176 case Instruction::IF_EQ:
177 cond = kCondEq;
178 break;
179 case Instruction::IF_NE:
180 cond = kCondNe;
181 break;
182 case Instruction::IF_LT:
183 cond = kCondLt;
184 break;
185 case Instruction::IF_GE:
186 cond = kCondGe;
187 break;
188 case Instruction::IF_GT:
189 cond = kCondGt;
190 break;
191 case Instruction::IF_LE:
192 cond = kCondLe;
193 break;
194 default:
195 cond = static_cast<ConditionCode>(0);
196 LOG(FATAL) << "Unexpected opcode " << opcode;
197 }
198
199 // Normalize such that if either operand is constant, src2 will be constant
200 if (rl_src1.is_const) {
201 RegLocation rl_temp = rl_src1;
202 rl_src1 = rl_src2;
203 rl_src2 = rl_temp;
204 cond = FlipComparisonOrder(cond);
205 }
206
207 rl_src1 = LoadValue(rl_src1, kCoreReg);
208 // Is this really an immediate comparison?
209 if (rl_src2.is_const) {
210 // If it's already live in a register or not easily materialized, just keep going
211 RegLocation rl_temp = UpdateLoc(rl_src2);
212 if ((rl_temp.location == kLocDalvikFrame) &&
213 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
214 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800215 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 return;
217 }
218 }
219 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800220 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221}
222
223void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700224 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 ConditionCode cond;
226 rl_src = LoadValue(rl_src, kCoreReg);
227 switch (opcode) {
228 case Instruction::IF_EQZ:
229 cond = kCondEq;
230 break;
231 case Instruction::IF_NEZ:
232 cond = kCondNe;
233 break;
234 case Instruction::IF_LTZ:
235 cond = kCondLt;
236 break;
237 case Instruction::IF_GEZ:
238 cond = kCondGe;
239 break;
240 case Instruction::IF_GTZ:
241 cond = kCondGt;
242 break;
243 case Instruction::IF_LEZ:
244 cond = kCondLe;
245 break;
246 default:
247 cond = static_cast<ConditionCode>(0);
248 LOG(FATAL) << "Unexpected opcode " << opcode;
249 }
buzbee2700f7e2014-03-07 09:46:20 -0800250 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700253void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
255 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800256 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800258 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 }
buzbee2700f7e2014-03-07 09:46:20 -0800260 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 StoreValueWide(rl_dest, rl_result);
262}
263
264void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700265 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700266 rl_src = LoadValue(rl_src, kCoreReg);
267 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
268 OpKind op = kOpInvalid;
269 switch (opcode) {
270 case Instruction::INT_TO_BYTE:
271 op = kOp2Byte;
272 break;
273 case Instruction::INT_TO_SHORT:
274 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700276 case Instruction::INT_TO_CHAR:
277 op = kOp2Char;
278 break;
279 default:
280 LOG(ERROR) << "Bad int conversion type";
281 }
buzbee2700f7e2014-03-07 09:46:20 -0800282 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700283 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
286/*
287 * Let helper function take care of everything. Will call
288 * Array::AllocFromCode(type_idx, method, count);
289 * Note: AllocFromCode will handle checks for errNegativeArraySize.
290 */
291void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700294 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800295 const DexFile* dex_file = cu_->dex_file;
296 CompilerDriver* driver = cu_->compiler_driver;
297 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800299 bool is_type_initialized; // Ignored as an array does not have an initializer.
300 bool use_direct_type_ptr;
301 uintptr_t direct_type_ptr;
302 if (kEmbedClassInCode &&
303 driver->CanEmbedTypeInCode(*dex_file, type_idx,
304 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
305 // The fast path.
306 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800307 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700308 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800309 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
310 } else {
311 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700312 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800313 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
314 }
315 } else {
316 // The slow path.
317 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700318 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800319 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
320 }
321 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700323 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800324 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 RegLocation rl_result = GetReturn(false);
327 StoreValue(rl_dest, rl_result);
328}
329
330/*
331 * Similar to GenNewArray, but with post-allocation initialization.
332 * Verifier guarantees we're dealing with an array class. Current
333 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
334 * Current code also throws internal unimp if not 'L', '[' or 'I'.
335 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700336void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 int elems = info->num_arg_words;
338 int type_idx = info->index;
339 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700340 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
342 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700343 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700345 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 }
347 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
348 FreeTemp(TargetReg(kArg2));
349 FreeTemp(TargetReg(kArg1));
350 /*
351 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
352 * return region. Because AllocFromCode placed the new array
353 * in kRet0, we'll just lock it into place. When debugger support is
354 * added, it may be necessary to additionally copy all return
355 * values to a home location in thread-local storage
356 */
357 LockTemp(TargetReg(kRet0));
358
359 // TODO: use the correct component size, currently all supported types
360 // share array alignment with ints (see comment at head of function)
361 size_t component_size = sizeof(int32_t);
362
363 // Having a range of 0 is legal
364 if (info->is_range && (elems > 0)) {
365 /*
366 * Bit of ugliness here. We're going generate a mem copy loop
367 * on the register range, but it is possible that some regs
368 * in the range have been promoted. This is unlikely, but
369 * before generating the copy, we'll just force a flush
370 * of any regs in the source range that have been promoted to
371 * home location.
372 */
373 for (int i = 0; i < elems; i++) {
374 RegLocation loc = UpdateLoc(info->args[i]);
375 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
378 }
379 /*
380 * TUNING note: generated code here could be much improved, but
381 * this is an uncommon operation and isn't especially performance
382 * critical.
383 */
buzbee2700f7e2014-03-07 09:46:20 -0800384 RegStorage r_src = AllocTemp();
385 RegStorage r_dst = AllocTemp();
386 RegStorage r_idx = AllocTemp();
387 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700388 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 case kThumb2:
390 r_val = TargetReg(kLr);
391 break;
392 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700393 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 FreeTemp(TargetReg(kRet0));
395 r_val = AllocTemp();
396 break;
397 case kMips:
398 r_val = AllocTemp();
399 break;
400 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
401 }
402 // Set up source pointer
403 RegLocation rl_first = info->args[0];
404 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
405 // Set up the target pointer
406 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
407 mirror::Array::DataOffset(component_size).Int32Value());
408 // Set up the loop counter (known to be > 0)
409 LoadConstant(r_idx, elems - 1);
410 // Generate the copy loop. Going backwards for convenience
411 LIR* target = NewLIR0(kPseudoTargetLabel);
412 // Copy next element
413 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
414 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
415 FreeTemp(r_val);
416 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700417 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 // Restore the target pointer
419 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
420 -mirror::Array::DataOffset(component_size).Int32Value());
421 }
422 } else if (!info->is_range) {
423 // TUNING: interleave
424 for (int i = 0; i < elems; i++) {
425 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
426 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800427 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
428 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800430 if (IsTemp(rl_arg.reg)) {
431 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
433 }
434 }
435 if (info->result.location != kLocInvalid) {
436 StoreValue(info->result, GetReturn(false /* not fp */));
437 }
438}
439
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800440//
441// Slow path to ensure a class is initialized for sget/sput.
442//
443class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
444 public:
buzbee2700f7e2014-03-07 09:46:20 -0800445 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
446 RegStorage r_base) :
447 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
448 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800449 }
450
451 void Compile() {
452 LIR* unresolved_target = GenerateTargetLabel();
453 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700454 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800455 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800456 // Copy helper's result into r_base, a no-op on all but MIPS.
457 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
458
459 m2l_->OpUnconditionalBranch(cont_);
460 }
461
462 private:
463 LIR* const uninit_;
464 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800465 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800466};
467
Vladimir Markobe0e5462014-02-26 11:24:15 +0000468void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700469 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000470 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
471 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
472 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
473 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800474 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000475 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 // Fast path, static storage base is this method's class
477 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800478 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800479 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
480 if (IsTemp(rl_method.reg)) {
481 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 }
483 } else {
484 // Medium path, static storage base in a different class which requires checks that the other
485 // class is initialized.
486 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000487 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 // May do runtime call so everything to home locations.
489 FlushAllRegs();
490 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800491 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 LockTemp(r_method);
493 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800494 r_base = TargetReg(kArg0);
495 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800496 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800497 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000498 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800499 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000500 if (!field_info.IsInitialized() &&
501 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800502 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800503
504 // The slow path is invoked if the r_base is NULL or the class pointed
505 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800506 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800507 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800508 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800509 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800510 mirror::Class::StatusOffset().Int32Value(),
511 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800512 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800513
buzbee2700f7e2014-03-07 09:46:20 -0800514 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000515 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800516
517 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 FreeTemp(r_method);
520 }
521 // rBase now holds static storage base
522 if (is_long_or_double) {
523 rl_src = LoadValueWide(rl_src, kAnyReg);
524 } else {
525 rl_src = LoadValue(rl_src, kAnyReg);
526 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000527 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800528 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 GenMemBarrier(kStoreStore);
530 }
531 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800532 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800534 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000536 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800537 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 GenMemBarrier(kStoreLoad);
539 }
540 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800541 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800543 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 } else {
545 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700546 ThreadOffset<4> setter_offset =
547 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
548 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
549 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000550 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 }
552}
553
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000556 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
557 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
558 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
559 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800560 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 // Fast path, static storage base is this method's class
563 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800564 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800565 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 } else {
567 // Medium path, static storage base in a different class which requires checks that the other
568 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000569 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 // May do runtime call so everything to home locations.
571 FlushAllRegs();
572 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800573 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 LockTemp(r_method);
575 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800576 r_base = TargetReg(kArg0);
577 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800578 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800579 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000580 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800581 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000582 if (!field_info.IsInitialized() &&
583 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800584 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800585
586 // The slow path is invoked if the r_base is NULL or the class pointed
587 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800588 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800589 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800591 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800592 mirror::Class::StatusOffset().Int32Value(),
593 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800594 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800595
buzbee2700f7e2014-03-07 09:46:20 -0800596 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000597 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800598
599 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 FreeTemp(r_method);
602 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800605
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800607 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800609 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800611 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800612
613 if (field_info.IsVolatile()) {
614 // Without context sensitive analysis, we must issue the most conservative barriers.
615 // In this case, either a load or store may follow so we issue both barriers.
616 GenMemBarrier(kLoadLoad);
617 GenMemBarrier(kLoadStore);
618 }
619
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 if (is_long_or_double) {
621 StoreValueWide(rl_dest, rl_result);
622 } else {
623 StoreValue(rl_dest, rl_result);
624 }
625 } else {
626 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700627 ThreadOffset<4> getterOffset =
628 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
629 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
630 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000631 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 if (is_long_or_double) {
633 RegLocation rl_result = GetReturnWide(rl_dest.fp);
634 StoreValueWide(rl_dest, rl_result);
635 } else {
636 RegLocation rl_result = GetReturn(rl_dest.fp);
637 StoreValue(rl_dest, rl_result);
638 }
639 }
640}
641
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800642// Generate code for all slow paths.
643void Mir2Lir::HandleSlowPaths() {
644 int n = slow_paths_.Size();
645 for (int i = 0; i < n; ++i) {
646 LIRSlowPath* slowpath = slow_paths_.Get(i);
647 slowpath->Compile();
648 }
649 slow_paths_.Reset();
650}
651
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700652void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700654 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 for (int i = 0; i < num_elems; i++) {
656 ResetRegPool();
657 ResetDefTracking();
658 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700659 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 current_dalvik_offset_ = lab->operands[1];
661 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800662 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
664 OpUnconditionalBranch(resume_lab);
665 }
666}
667
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700668void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 int num_elems = throw_launchpads_.Size();
670 for (int i = 0; i < num_elems; i++) {
671 ResetRegPool();
672 ResetDefTracking();
673 LIR* lab = throw_launchpads_.Get(i);
674 current_dalvik_offset_ = lab->operands[1];
675 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700676 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 int v1 = lab->operands[2];
678 int v2 = lab->operands[3];
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700679 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 switch (lab->operands[0]) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700681 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
683 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800684 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
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(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 }
689 // Make sure the following LoadConstant doesn't mess with kArg1.
690 LockTemp(TargetReg(kArg1));
691 LoadConstant(TargetReg(kArg0), v2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700692 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 break;
694 case kThrowArrayBounds:
695 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800696 if (v2 != TargetReg(kArg0).GetReg()) {
697 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 if (target_x86) {
699 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800700 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
701 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800703 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 }
705 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800706 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800708 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 if (target_x86) {
710 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800711 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
712 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800714 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 }
716 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
717 } else {
718 if (target_x86) {
719 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800720 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
721 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800723 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
buzbee2700f7e2014-03-07 09:46:20 -0800725 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 }
727 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700728 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800731 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700733 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 default:
736 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
737 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000738 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800739 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700740 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 }
742}
743
Vladimir Markobe0e5462014-02-26 11:24:15 +0000744void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700746 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000747 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
748 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
749 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 RegLocation rl_result;
751 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000752 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 rl_obj = LoadValue(rl_obj, kCoreReg);
754 if (is_long_or_double) {
755 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800756 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700757 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800759 // FIXME? duplicate null check?
760 GenNullCheck(rl_obj.reg, opt_flags);
761 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
762 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800763 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000764 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800765 // Without context sensitive analysis, we must issue the most conservative barriers.
766 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800768 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 }
770 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800771 RegStorage reg_ptr = AllocTemp();
772 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800774 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700775 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000776 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800777 // Without context sensitive analysis, we must issue the most conservative barriers.
778 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800780 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 }
782 FreeTemp(reg_ptr);
783 }
784 StoreValueWide(rl_dest, rl_result);
785 } else {
786 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800787 GenNullCheck(rl_obj.reg, opt_flags);
788 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
789 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800790 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000791 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800792 // Without context sensitive analysis, we must issue the most conservative barriers.
793 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 }
797 StoreValue(rl_dest, rl_result);
798 }
799 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700800 ThreadOffset<4> getterOffset =
801 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
802 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
803 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000804 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 if (is_long_or_double) {
806 RegLocation rl_result = GetReturnWide(rl_dest.fp);
807 StoreValueWide(rl_dest, rl_result);
808 } else {
809 RegLocation rl_result = GetReturn(rl_dest.fp);
810 StoreValue(rl_dest, rl_result);
811 }
812 }
813}
814
Vladimir Markobe0e5462014-02-26 11:24:15 +0000815void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700817 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000818 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
819 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
820 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000822 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 rl_obj = LoadValue(rl_obj, kCoreReg);
824 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800826 GenNullCheck(rl_obj.reg, opt_flags);
827 RegStorage reg_ptr = AllocTemp();
828 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000829 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800830 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 GenMemBarrier(kStoreStore);
832 }
buzbee2700f7e2014-03-07 09:46:20 -0800833 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800834 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000835 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800836 // A load might follow the volatile store so insert a StoreLoad barrier.
837 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 }
839 FreeTemp(reg_ptr);
840 } else {
841 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800842 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000843 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800844 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 GenMemBarrier(kStoreStore);
846 }
buzbee2700f7e2014-03-07 09:46:20 -0800847 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800848 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000849 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800850 // A load might follow the volatile store so insert a StoreLoad barrier.
851 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 }
853 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800854 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 }
856 }
857 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700858 ThreadOffset<4> setter_offset =
859 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
860 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
861 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000862 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
863 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 }
865}
866
Ian Rogersa9a82542013-10-04 11:17:26 -0700867void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
868 RegLocation rl_src) {
869 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
870 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
871 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700872 ThreadOffset<4> helper = needs_range_check
873 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
874 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
875 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700876 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
877}
878
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700879void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800881 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
883 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
884 *cu_->dex_file,
885 type_idx)) {
886 // Call out to helper which resolves type and verifies access.
887 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700888 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800889 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 RegLocation rl_result = GetReturn(false);
891 StoreValue(rl_dest, rl_result);
892 } else {
893 // We're don't need access checks, load type from dex cache
894 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700895 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800896 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 int32_t offset_of_type =
898 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
899 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800900 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
902 type_idx) || SLOW_TYPE_PATH) {
903 // Slow path, at runtime test if type is null and if so initialize
904 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800905 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800906 LIR* cont = NewLIR0(kPseudoTargetLabel);
907
908 // Object to generate the slow path for class resolution.
909 class SlowPath : public LIRSlowPath {
910 public:
911 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
912 const RegLocation& rl_method, const RegLocation& rl_result) :
913 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
914 rl_method_(rl_method), rl_result_(rl_result) {
915 }
916
917 void Compile() {
918 GenerateTargetLabel();
919
Ian Rogersdd7624d2014-03-14 17:43:00 -0700920 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800921 rl_method_.reg, true);
922 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800923
924 m2l_->OpUnconditionalBranch(cont_);
925 }
926
927 private:
928 const int type_idx_;
929 const RegLocation rl_method_;
930 const RegLocation rl_result_;
931 };
932
933 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800934 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800935
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800937 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938 // Fast path, we're done - just store result
939 StoreValue(rl_dest, rl_result);
940 }
941 }
942}
943
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700944void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700945 /* NOTE: Most strings should be available at compile time */
946 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
947 (sizeof(mirror::String*) * string_idx);
948 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
949 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
950 // slow path, resolve string if not in dex cache
951 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700952 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800953
954 // If the Method* is already in a register, we can save a copy.
955 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800956 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800957 if (rl_method.location == kLocPhysReg) {
958 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800959 DCHECK(!IsTemp(rl_method.reg));
960 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800961 } else {
962 r_method = TargetReg(kArg2);
963 LoadCurrMethodDirect(r_method);
964 }
965 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
966 TargetReg(kArg0));
967
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800970 if (cu_->instruction_set == kThumb2 ||
971 cu_->instruction_set == kMips) {
972 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800973 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800974 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
975 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800977
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800978 // Object to generate the slow path for string resolution.
979 class SlowPath : public LIRSlowPath {
980 public:
buzbee2700f7e2014-03-07 09:46:20 -0800981 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800982 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
983 }
984
985 void Compile() {
986 GenerateTargetLabel();
987
Dave Allisond6ed6422014-04-09 23:36:15 +0000988 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800989
Dave Allisond6ed6422014-04-09 23:36:15 +0000990 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
991 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
992 m2l_->MarkSafepointPC(call_inst);
993 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800994
995 m2l_->OpUnconditionalBranch(cont_);
996 }
997
998 private:
buzbee2700f7e2014-03-07 09:46:20 -0800999 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001000 };
1001
1002 // Add to list for future.
1003 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001005 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001006 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1007 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001008 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001009 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001010 LIR* target = NewLIR0(kPseudoTargetLabel);
1011 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 }
1013 GenBarrier();
1014 StoreValue(rl_dest, GetReturn(false));
1015 } else {
1016 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001017 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001019 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1020 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 StoreValue(rl_dest, rl_result);
1022 }
1023}
1024
1025/*
1026 * Let helper function take care of everything. Will
1027 * call Class::NewInstanceFromCode(type_idx, method);
1028 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001029void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 FlushAllRegs(); /* Everything to home location */
1031 // alloc will always check for resolution, do we also need to verify
1032 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001033 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001034 const DexFile* dex_file = cu_->dex_file;
1035 CompilerDriver* driver = cu_->compiler_driver;
1036 if (driver->CanAccessInstantiableTypeWithoutChecks(
1037 cu_->method_idx, *dex_file, type_idx)) {
1038 bool is_type_initialized;
1039 bool use_direct_type_ptr;
1040 uintptr_t direct_type_ptr;
1041 if (kEmbedClassInCode &&
1042 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1043 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1044 // The fast path.
1045 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001046 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001047 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001048 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1050 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001051 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1053 }
1054 } else {
1055 // Use the direct pointer.
1056 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001057 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001058 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1059 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001060 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1062 }
1063 }
1064 } else {
1065 // The slow path.
1066 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001067 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1069 }
1070 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001072 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001073 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 RegLocation rl_result = GetReturn(false);
1076 StoreValue(rl_dest, rl_result);
1077}
1078
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001079void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001081 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082}
1083
1084// For final classes there are no sub-classes to check and so we can answer the instance-of
1085// question with simple comparisons.
1086void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1087 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001088 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001089 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001090
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 RegLocation object = LoadValue(rl_src, kCoreReg);
1092 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001093 RegStorage result_reg = rl_result.reg;
1094 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 result_reg = AllocTypedTemp(false, kCoreReg);
1096 }
1097 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001098 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099
buzbee2700f7e2014-03-07 09:46:20 -08001100 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1101 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102
1103 LoadCurrMethodDirect(check_class);
1104 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001105 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1106 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001108 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001110 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 int32_t offset_of_type =
1112 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1113 (sizeof(mirror::Class*) * type_idx);
1114 LoadWordDisp(check_class, offset_of_type, check_class);
1115 }
1116
1117 LIR* ne_branchover = NULL;
1118 if (cu_->instruction_set == kThumb2) {
1119 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001120 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001122 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 } else {
1124 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1125 LoadConstant(result_reg, 1); // eq case - load true
1126 }
1127 LIR* target = NewLIR0(kPseudoTargetLabel);
1128 null_branchover->target = target;
1129 if (ne_branchover != NULL) {
1130 ne_branchover->target = target;
1131 }
1132 FreeTemp(object_class);
1133 FreeTemp(check_class);
1134 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001135 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 FreeTemp(result_reg);
1137 }
1138 StoreValue(rl_dest, rl_result);
1139}
1140
1141void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1142 bool type_known_abstract, bool use_declaring_class,
1143 bool can_assume_type_is_in_dex_cache,
1144 uint32_t type_idx, RegLocation rl_dest,
1145 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001146 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001147 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001148
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 FlushAllRegs();
1150 // May generate a call - use explicit registers
1151 LockCallTemps();
1152 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001153 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 if (needs_access_check) {
1155 // Check we have access to type_idx and if not throw IllegalAccessError,
1156 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001157 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 type_idx, true);
1159 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1160 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1161 } else if (use_declaring_class) {
1162 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001163 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1164 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 } else {
1166 // Load dex cache entry into class_reg (kArg2)
1167 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001168 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1169 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 int32_t offset_of_type =
1171 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1172 * type_idx);
1173 LoadWordDisp(class_reg, offset_of_type, class_reg);
1174 if (!can_assume_type_is_in_dex_cache) {
1175 // Need to test presence of type in dex cache at runtime
1176 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1177 // Not resolved
1178 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001179 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001180 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1182 // Rejoin code paths
1183 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1184 hop_branch->target = hop_target;
1185 }
1186 }
1187 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1188 RegLocation rl_result = GetReturn(false);
1189 if (cu_->instruction_set == kMips) {
1190 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001191 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 }
1193 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1194
1195 /* load object->klass_ */
1196 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1197 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1198 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1199 LIR* branchover = NULL;
1200 if (type_known_final) {
1201 // rl_result == ref == null == 0.
1202 if (cu_->instruction_set == kThumb2) {
1203 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001204 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001205 LoadConstant(rl_result.reg, 1); // .eq case - load true
1206 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001207 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001209 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001211 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 }
1213 } else {
1214 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001215 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001216 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (!type_known_abstract) {
1218 /* Uses conditional nullification */
1219 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001220 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1222 }
1223 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1224 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001225 if (it != nullptr) {
1226 OpEndIT(it);
1227 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 FreeTemp(r_tgt);
1229 } else {
1230 if (!type_known_abstract) {
1231 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001232 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1234 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001235 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001236 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1237 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1238 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240 }
1241 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001242 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 /* branch targets here */
1244 LIR* target = NewLIR0(kPseudoTargetLabel);
1245 StoreValue(rl_dest, rl_result);
1246 branch1->target = target;
1247 if (branchover != NULL) {
1248 branchover->target = target;
1249 }
1250}
1251
1252void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1253 bool type_known_final, type_known_abstract, use_declaring_class;
1254 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1255 *cu_->dex_file,
1256 type_idx,
1257 &type_known_final,
1258 &type_known_abstract,
1259 &use_declaring_class);
1260 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1261 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1262
1263 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1264 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1265 } else {
1266 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1267 use_declaring_class, can_assume_type_is_in_dex_cache,
1268 type_idx, rl_dest, rl_src);
1269 }
1270}
1271
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001272void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 bool type_known_final, type_known_abstract, use_declaring_class;
1274 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1275 *cu_->dex_file,
1276 type_idx,
1277 &type_known_final,
1278 &type_known_abstract,
1279 &use_declaring_class);
1280 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1281 // of the exception throw path.
1282 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001283 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 // Verifier type analysis proved this check cast would never cause an exception.
1285 return;
1286 }
1287 FlushAllRegs();
1288 // May generate a call - use explicit registers
1289 LockCallTemps();
1290 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001291 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 if (needs_access_check) {
1293 // Check we have access to type_idx and if not throw IllegalAccessError,
1294 // returns Class* in kRet0
1295 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001296 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 type_idx, TargetReg(kArg1), true);
1298 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1299 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001300 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1301 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 } else {
1303 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001304 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1305 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 int32_t offset_of_type =
1307 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1308 (sizeof(mirror::Class*) * type_idx);
1309 LoadWordDisp(class_reg, offset_of_type, class_reg);
1310 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1311 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001312 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1313 LIR* cont = NewLIR0(kPseudoTargetLabel);
1314
1315 // Slow path to initialize the type. Executed if the type is NULL.
1316 class SlowPath : public LIRSlowPath {
1317 public:
1318 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001319 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001320 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1321 class_reg_(class_reg) {
1322 }
1323
1324 void Compile() {
1325 GenerateTargetLabel();
1326
1327 // Call out to helper, which will return resolved type in kArg0
1328 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001329 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001330 m2l_->TargetReg(kArg1), true);
1331 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1332 m2l_->OpUnconditionalBranch(cont_);
1333 }
1334 public:
1335 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001336 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001337 };
1338
buzbee2700f7e2014-03-07 09:46:20 -08001339 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 }
1341 }
1342 // At this point, class_reg (kArg2) has class
1343 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001344
1345 // Slow path for the case where the classes are not equal. In this case we need
1346 // to call a helper function to do the check.
1347 class SlowPath : public LIRSlowPath {
1348 public:
1349 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1350 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1351 }
1352
1353 void Compile() {
1354 GenerateTargetLabel();
1355
1356 if (load_) {
1357 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1358 m2l_->TargetReg(kArg1));
1359 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001360 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001361 m2l_->TargetReg(kArg1), true);
1362
1363 m2l_->OpUnconditionalBranch(cont_);
1364 }
1365
1366 private:
1367 bool load_;
1368 };
1369
1370 if (type_known_abstract) {
1371 // Easier case, run slow path if target is non-null (slow path will load from target)
1372 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1373 LIR* cont = NewLIR0(kPseudoTargetLabel);
1374 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1375 } else {
1376 // Harder, more common case. We need to generate a forward branch over the load
1377 // if the target is null. If it's non-null we perform the load and branch to the
1378 // slow path if the classes are not equal.
1379
1380 /* Null is OK - continue */
1381 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1382 /* load object->klass_ */
1383 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001384 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385
1386 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1387 LIR* cont = NewLIR0(kPseudoTargetLabel);
1388
1389 // Add the slow path that will not perform load since this is already done.
1390 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1391
1392 // Set the null check to branch to the continuation.
1393 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 }
1395}
1396
1397void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001398 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 RegLocation rl_result;
1400 if (cu_->instruction_set == kThumb2) {
1401 /*
1402 * NOTE: This is the one place in the code in which we might have
1403 * as many as six live temporary registers. There are 5 in the normal
1404 * set for Arm. Until we have spill capabilities, temporarily add
1405 * lr to the temp set. It is safe to do this locally, but note that
1406 * lr is used explicitly elsewhere in the code generator and cannot
1407 * normally be used as a general temp register.
1408 */
1409 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1410 FreeTemp(TargetReg(kLr)); // and make it available
1411 }
1412 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1413 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1414 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1415 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001416 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1417 RegStorage t_reg = AllocTemp();
1418 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1419 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1420 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 FreeTemp(t_reg);
1422 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001423 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1424 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425 }
1426 /*
1427 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1428 * following StoreValueWide might need to allocate a temp register.
1429 * To further work around the lack of a spill capability, explicitly
1430 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1431 * Remove when spill is functional.
1432 */
1433 FreeRegLocTemps(rl_result, rl_src1);
1434 FreeRegLocTemps(rl_result, rl_src2);
1435 StoreValueWide(rl_dest, rl_result);
1436 if (cu_->instruction_set == kThumb2) {
1437 Clobber(TargetReg(kLr));
1438 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1439 }
1440}
1441
1442
1443void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001444 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001445 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446
1447 switch (opcode) {
1448 case Instruction::SHL_LONG:
1449 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001450 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 break;
1452 case Instruction::SHR_LONG:
1453 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001454 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 break;
1456 case Instruction::USHR_LONG:
1457 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001458 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 break;
1460 default:
1461 LOG(FATAL) << "Unexpected case";
1462 }
1463 FlushAllRegs(); /* Send everything to home location */
1464 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1465 RegLocation rl_result = GetReturnWide(false);
1466 StoreValueWide(rl_dest, rl_result);
1467}
1468
1469
1470void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001471 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001472 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 OpKind op = kOpBkpt;
1474 bool is_div_rem = false;
1475 bool check_zero = false;
1476 bool unary = false;
1477 RegLocation rl_result;
1478 bool shift_op = false;
1479 switch (opcode) {
1480 case Instruction::NEG_INT:
1481 op = kOpNeg;
1482 unary = true;
1483 break;
1484 case Instruction::NOT_INT:
1485 op = kOpMvn;
1486 unary = true;
1487 break;
1488 case Instruction::ADD_INT:
1489 case Instruction::ADD_INT_2ADDR:
1490 op = kOpAdd;
1491 break;
1492 case Instruction::SUB_INT:
1493 case Instruction::SUB_INT_2ADDR:
1494 op = kOpSub;
1495 break;
1496 case Instruction::MUL_INT:
1497 case Instruction::MUL_INT_2ADDR:
1498 op = kOpMul;
1499 break;
1500 case Instruction::DIV_INT:
1501 case Instruction::DIV_INT_2ADDR:
1502 check_zero = true;
1503 op = kOpDiv;
1504 is_div_rem = true;
1505 break;
1506 /* NOTE: returns in kArg1 */
1507 case Instruction::REM_INT:
1508 case Instruction::REM_INT_2ADDR:
1509 check_zero = true;
1510 op = kOpRem;
1511 is_div_rem = true;
1512 break;
1513 case Instruction::AND_INT:
1514 case Instruction::AND_INT_2ADDR:
1515 op = kOpAnd;
1516 break;
1517 case Instruction::OR_INT:
1518 case Instruction::OR_INT_2ADDR:
1519 op = kOpOr;
1520 break;
1521 case Instruction::XOR_INT:
1522 case Instruction::XOR_INT_2ADDR:
1523 op = kOpXor;
1524 break;
1525 case Instruction::SHL_INT:
1526 case Instruction::SHL_INT_2ADDR:
1527 shift_op = true;
1528 op = kOpLsl;
1529 break;
1530 case Instruction::SHR_INT:
1531 case Instruction::SHR_INT_2ADDR:
1532 shift_op = true;
1533 op = kOpAsr;
1534 break;
1535 case Instruction::USHR_INT:
1536 case Instruction::USHR_INT_2ADDR:
1537 shift_op = true;
1538 op = kOpLsr;
1539 break;
1540 default:
1541 LOG(FATAL) << "Invalid word arith op: " << opcode;
1542 }
1543 if (!is_div_rem) {
1544 if (unary) {
1545 rl_src1 = LoadValue(rl_src1, kCoreReg);
1546 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001547 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 } else {
1549 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001550 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001551 RegStorage t_reg = AllocTemp();
1552 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 rl_src1 = LoadValue(rl_src1, kCoreReg);
1554 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001555 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 FreeTemp(t_reg);
1557 } else {
1558 rl_src1 = LoadValue(rl_src1, kCoreReg);
1559 rl_src2 = LoadValue(rl_src2, kCoreReg);
1560 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001561 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 }
1563 }
1564 StoreValue(rl_dest, rl_result);
1565 } else {
Dave Allison70202782013-10-22 17:52:19 -07001566 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 -07001567 if (cu_->instruction_set == kMips) {
1568 rl_src1 = LoadValue(rl_src1, kCoreReg);
1569 rl_src2 = LoadValue(rl_src2, kCoreReg);
1570 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001571 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 }
buzbee2700f7e2014-03-07 09:46:20 -08001573 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001574 done = true;
1575 } else if (cu_->instruction_set == kThumb2) {
1576 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1577 // Use ARM SDIV instruction for division. For remainder we also need to
1578 // calculate using a MUL and subtract.
1579 rl_src1 = LoadValue(rl_src1, kCoreReg);
1580 rl_src2 = LoadValue(rl_src2, kCoreReg);
1581 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001582 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001583 }
buzbee2700f7e2014-03-07 09:46:20 -08001584 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001585 done = true;
1586 }
1587 }
1588
1589 // If we haven't already generated the code use the callout function.
1590 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001591 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 FlushAllRegs(); /* Send everything to home location */
1593 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001594 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1596 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001597 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 }
Dave Allison70202782013-10-22 17:52:19 -07001599 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001600 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 if (op == kOpDiv)
1602 rl_result = GetReturn(false);
1603 else
1604 rl_result = GetReturnAlt();
1605 }
1606 StoreValue(rl_dest, rl_result);
1607 }
1608}
1609
1610/*
1611 * The following are the first-level codegen routines that analyze the format
1612 * of each bytecode then either dispatch special purpose codegen routines
1613 * or produce corresponding Thumb instructions directly.
1614 */
1615
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001617static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 x &= x - 1;
1619 return (x & (x - 1)) == 0;
1620}
1621
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1623// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001624bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001625 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1627 return false;
1628 }
1629 // No divide instruction for Arm, so check for more special cases
1630 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001631 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 }
1633 int k = LowestSetBit(lit);
1634 if (k >= 30) {
1635 // Avoid special cases.
1636 return false;
1637 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 rl_src = LoadValue(rl_src, kCoreReg);
1639 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001640 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001641 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001642 if (lit == 2) {
1643 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001644 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1645 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1646 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001648 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001650 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1651 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 }
1653 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001654 RegStorage t_reg1 = AllocTemp();
1655 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001657 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1658 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001660 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001662 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001664 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001666 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 }
1668 }
1669 StoreValue(rl_dest, rl_result);
1670 return true;
1671}
1672
1673// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1674// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001675bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001676 if (lit < 0) {
1677 return false;
1678 }
1679 if (lit == 0) {
1680 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1681 LoadConstant(rl_result.reg, 0);
1682 StoreValue(rl_dest, rl_result);
1683 return true;
1684 }
1685 if (lit == 1) {
1686 rl_src = LoadValue(rl_src, kCoreReg);
1687 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1688 OpRegCopy(rl_result.reg, rl_src.reg);
1689 StoreValue(rl_dest, rl_result);
1690 return true;
1691 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001692 // There is RegRegRegShift on Arm, so check for more special cases
1693 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001694 return EasyMultiply(rl_src, rl_dest, lit);
1695 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 // Can we simplify this multiplication?
1697 bool power_of_two = false;
1698 bool pop_count_le2 = false;
1699 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001700 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 power_of_two = true;
1702 } else if (IsPopCountLE2(lit)) {
1703 pop_count_le2 = true;
1704 } else if (IsPowerOfTwo(lit + 1)) {
1705 power_of_two_minus_one = true;
1706 } else {
1707 return false;
1708 }
1709 rl_src = LoadValue(rl_src, kCoreReg);
1710 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1711 if (power_of_two) {
1712 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001713 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 } else if (pop_count_le2) {
1715 // Shift and add and shift.
1716 int first_bit = LowestSetBit(lit);
1717 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1718 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1719 } else {
1720 // Reverse subtract: (src << (shift + 1)) - src.
1721 DCHECK(power_of_two_minus_one);
1722 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001723 RegStorage t_reg = AllocTemp();
1724 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1725 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 }
1727 StoreValue(rl_dest, rl_result);
1728 return true;
1729}
1730
1731void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001732 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 RegLocation rl_result;
1734 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1735 int shift_op = false;
1736 bool is_div = false;
1737
1738 switch (opcode) {
1739 case Instruction::RSUB_INT_LIT8:
1740 case Instruction::RSUB_INT: {
1741 rl_src = LoadValue(rl_src, kCoreReg);
1742 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1743 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001744 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001746 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1747 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 }
1749 StoreValue(rl_dest, rl_result);
1750 return;
1751 }
1752
1753 case Instruction::SUB_INT:
1754 case Instruction::SUB_INT_2ADDR:
1755 lit = -lit;
1756 // Intended fallthrough
1757 case Instruction::ADD_INT:
1758 case Instruction::ADD_INT_2ADDR:
1759 case Instruction::ADD_INT_LIT8:
1760 case Instruction::ADD_INT_LIT16:
1761 op = kOpAdd;
1762 break;
1763 case Instruction::MUL_INT:
1764 case Instruction::MUL_INT_2ADDR:
1765 case Instruction::MUL_INT_LIT8:
1766 case Instruction::MUL_INT_LIT16: {
1767 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1768 return;
1769 }
1770 op = kOpMul;
1771 break;
1772 }
1773 case Instruction::AND_INT:
1774 case Instruction::AND_INT_2ADDR:
1775 case Instruction::AND_INT_LIT8:
1776 case Instruction::AND_INT_LIT16:
1777 op = kOpAnd;
1778 break;
1779 case Instruction::OR_INT:
1780 case Instruction::OR_INT_2ADDR:
1781 case Instruction::OR_INT_LIT8:
1782 case Instruction::OR_INT_LIT16:
1783 op = kOpOr;
1784 break;
1785 case Instruction::XOR_INT:
1786 case Instruction::XOR_INT_2ADDR:
1787 case Instruction::XOR_INT_LIT8:
1788 case Instruction::XOR_INT_LIT16:
1789 op = kOpXor;
1790 break;
1791 case Instruction::SHL_INT_LIT8:
1792 case Instruction::SHL_INT:
1793 case Instruction::SHL_INT_2ADDR:
1794 lit &= 31;
1795 shift_op = true;
1796 op = kOpLsl;
1797 break;
1798 case Instruction::SHR_INT_LIT8:
1799 case Instruction::SHR_INT:
1800 case Instruction::SHR_INT_2ADDR:
1801 lit &= 31;
1802 shift_op = true;
1803 op = kOpAsr;
1804 break;
1805 case Instruction::USHR_INT_LIT8:
1806 case Instruction::USHR_INT:
1807 case Instruction::USHR_INT_2ADDR:
1808 lit &= 31;
1809 shift_op = true;
1810 op = kOpLsr;
1811 break;
1812
1813 case Instruction::DIV_INT:
1814 case Instruction::DIV_INT_2ADDR:
1815 case Instruction::DIV_INT_LIT8:
1816 case Instruction::DIV_INT_LIT16:
1817 case Instruction::REM_INT:
1818 case Instruction::REM_INT_2ADDR:
1819 case Instruction::REM_INT_LIT8:
1820 case Instruction::REM_INT_LIT16: {
1821 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001822 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001823 return;
1824 }
buzbee11b63d12013-08-27 07:34:17 -07001825 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001827 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 (opcode == Instruction::DIV_INT_LIT16)) {
1829 is_div = true;
1830 } else {
1831 is_div = false;
1832 }
buzbee11b63d12013-08-27 07:34:17 -07001833 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1834 return;
1835 }
Dave Allison70202782013-10-22 17:52:19 -07001836
1837 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 if (cu_->instruction_set == kMips) {
1839 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001840 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001841 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001842 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001843 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1844 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001845 } else if (cu_->instruction_set == kThumb2) {
1846 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1847 // Use ARM SDIV instruction for division. For remainder we also need to
1848 // calculate using a MUL and subtract.
1849 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001850 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001851 done = true;
1852 }
1853 }
1854
1855 if (!done) {
1856 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1858 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001859 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1861 if (is_div)
1862 rl_result = GetReturn(false);
1863 else
1864 rl_result = GetReturnAlt();
1865 }
1866 StoreValue(rl_dest, rl_result);
1867 return;
1868 }
1869 default:
1870 LOG(FATAL) << "Unexpected opcode " << opcode;
1871 }
1872 rl_src = LoadValue(rl_src, kCoreReg);
1873 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001874 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001876 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001878 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 }
1880 StoreValue(rl_dest, rl_result);
1881}
1882
1883void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001884 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 RegLocation rl_result;
1886 OpKind first_op = kOpBkpt;
1887 OpKind second_op = kOpBkpt;
1888 bool call_out = false;
1889 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001890 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001891 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001892
1893 switch (opcode) {
1894 case Instruction::NOT_LONG:
1895 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1896 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1897 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001898 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1899 RegStorage t_reg = AllocTemp();
1900 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1901 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1902 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001903 FreeTemp(t_reg);
1904 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001905 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1906 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 }
1908 StoreValueWide(rl_dest, rl_result);
1909 return;
1910 case Instruction::ADD_LONG:
1911 case Instruction::ADD_LONG_2ADDR:
1912 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001913 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 return;
1915 }
1916 first_op = kOpAdd;
1917 second_op = kOpAdc;
1918 break;
1919 case Instruction::SUB_LONG:
1920 case Instruction::SUB_LONG_2ADDR:
1921 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001922 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 return;
1924 }
1925 first_op = kOpSub;
1926 second_op = kOpSbc;
1927 break;
1928 case Instruction::MUL_LONG:
1929 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001930 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001931 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001932 return;
1933 } else {
1934 call_out = 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, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 }
1938 break;
1939 case Instruction::DIV_LONG:
1940 case Instruction::DIV_LONG_2ADDR:
1941 call_out = true;
1942 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001943 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001944 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 break;
1946 case Instruction::REM_LONG:
1947 case Instruction::REM_LONG_2ADDR:
1948 call_out = true;
1949 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001950 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001952 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001953 break;
1954 case Instruction::AND_LONG_2ADDR:
1955 case Instruction::AND_LONG:
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 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 }
1959 first_op = kOpAnd;
1960 second_op = kOpAnd;
1961 break;
1962 case Instruction::OR_LONG:
1963 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001964 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001965 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 return;
1967 }
1968 first_op = kOpOr;
1969 second_op = kOpOr;
1970 break;
1971 case Instruction::XOR_LONG:
1972 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001973 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001974 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 return;
1976 }
1977 first_op = kOpXor;
1978 second_op = kOpXor;
1979 break;
1980 case Instruction::NEG_LONG: {
1981 GenNegLong(rl_dest, rl_src2);
1982 return;
1983 }
1984 default:
1985 LOG(FATAL) << "Invalid long arith op";
1986 }
1987 if (!call_out) {
1988 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1989 } else {
1990 FlushAllRegs(); /* Send everything to home location */
1991 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001992 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1993 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1994 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1995 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07001996 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08001997 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 // NOTE: callout here is not a safepoint
1999 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2000 } else {
2001 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2002 }
2003 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002004 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 rl_result = GetReturnWide(false);
2006 else
2007 rl_result = GetReturnWideAlt();
2008 StoreValueWide(rl_dest, rl_result);
2009 }
2010}
2011
Ian Rogersdd7624d2014-03-14 17:43:00 -07002012void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002013 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 /*
2015 * Don't optimize the register usage since it calls out to support
2016 * functions
2017 */
2018 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2020 if (rl_dest.wide) {
2021 RegLocation rl_result;
2022 rl_result = GetReturnWide(rl_dest.fp);
2023 StoreValueWide(rl_dest, rl_result);
2024 } else {
2025 RegLocation rl_result;
2026 rl_result = GetReturn(rl_dest.fp);
2027 StoreValue(rl_dest, rl_result);
2028 }
2029}
2030
2031/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002032void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002033 if (Runtime::Current()->ExplicitSuspendChecks()) {
2034 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2035 return;
2036 }
2037 FlushAllRegs();
2038 LIR* branch = OpTestSuspend(NULL);
2039 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2040 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2041 current_dalvik_offset_);
2042 branch->target = target;
2043 suspend_launchpads_.Insert(target);
2044 } else {
2045 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2046 return;
2047 }
2048 FlushAllRegs(); // TODO: needed?
2049 LIR* inst = CheckSuspendUsingLoad();
2050 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052}
2053
2054/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002055void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002056 if (Runtime::Current()->ExplicitSuspendChecks()) {
2057 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2058 OpUnconditionalBranch(target);
2059 return;
2060 }
2061 OpTestSuspend(target);
2062 LIR* launch_pad =
2063 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2064 current_dalvik_offset_);
2065 FlushAllRegs();
2066 OpUnconditionalBranch(launch_pad);
2067 suspend_launchpads_.Insert(launch_pad);
2068 } else {
2069 // For the implicit suspend check, just perform the trigger
2070 // load and branch to the target.
2071 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2072 OpUnconditionalBranch(target);
2073 return;
2074 }
2075 FlushAllRegs();
2076 LIR* inst = CheckSuspendUsingLoad();
2077 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080}
2081
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002082/* Call out to helper assembly routine that will null check obj and then lock it. */
2083void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2084 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002085 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002086}
2087
2088/* Call out to helper assembly routine that will null check obj and then unlock it. */
2089void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2090 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002091 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002092}
2093
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002094/* Generic code for generating a wide constant into a VR. */
2095void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2096 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002097 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002098 StoreValueWide(rl_dest, rl_result);
2099}
2100
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101} // namespace art