blob: 1847921ccb93d0a9293fb35a7544778cbeea34e3 [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) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700523 RegisterClass register_kind = kAnyReg;
524 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
525 // Force long/double volatile stores into SSE registers to avoid tearing.
526 register_kind = kFPReg;
527 }
528 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 } else {
530 rl_src = LoadValue(rl_src, kAnyReg);
531 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000532 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800533 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 GenMemBarrier(kStoreStore);
535 }
536 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800537 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800539 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000541 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800542 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 GenMemBarrier(kStoreLoad);
544 }
545 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800546 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800548 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 } else {
550 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700551 ThreadOffset<4> setter_offset =
552 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
553 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
554 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000555 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 }
557}
558
Vladimir Markobe0e5462014-02-26 11:24:15 +0000559void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700560 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
562 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
563 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
564 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800565 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000566 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 // Fast path, static storage base is this method's class
568 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800570 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 } else {
572 // Medium path, static storage base in a different class which requires checks that the other
573 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000574 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 // May do runtime call so everything to home locations.
576 FlushAllRegs();
577 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800578 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 LockTemp(r_method);
580 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800581 r_base = TargetReg(kArg0);
582 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800583 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800584 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800586 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000587 if (!field_info.IsInitialized() &&
588 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800589 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800590
591 // The slow path is invoked if the r_base is NULL or the class pointed
592 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800593 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800594 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800595 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800596 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800597 mirror::Class::StatusOffset().Int32Value(),
598 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800599 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800600
buzbee2700f7e2014-03-07 09:46:20 -0800601 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000602 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603
604 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 FreeTemp(r_method);
607 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800608 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700609 RegisterClass result_reg_kind = kAnyReg;
610 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
611 // Force long/double volatile loads into SSE registers to avoid tearing.
612 result_reg_kind = kFPReg;
613 }
614 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800615
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800617 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800619 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800622
623 if (field_info.IsVolatile()) {
624 // Without context sensitive analysis, we must issue the most conservative barriers.
625 // In this case, either a load or store may follow so we issue both barriers.
626 GenMemBarrier(kLoadLoad);
627 GenMemBarrier(kLoadStore);
628 }
629
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 if (is_long_or_double) {
631 StoreValueWide(rl_dest, rl_result);
632 } else {
633 StoreValue(rl_dest, rl_result);
634 }
635 } else {
636 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700637 ThreadOffset<4> getterOffset =
638 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
639 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
640 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000641 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 if (is_long_or_double) {
643 RegLocation rl_result = GetReturnWide(rl_dest.fp);
644 StoreValueWide(rl_dest, rl_result);
645 } else {
646 RegLocation rl_result = GetReturn(rl_dest.fp);
647 StoreValue(rl_dest, rl_result);
648 }
649 }
650}
651
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800652// Generate code for all slow paths.
653void Mir2Lir::HandleSlowPaths() {
654 int n = slow_paths_.Size();
655 for (int i = 0; i < n; ++i) {
656 LIRSlowPath* slowpath = slow_paths_.Get(i);
657 slowpath->Compile();
658 }
659 slow_paths_.Reset();
660}
661
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700662void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700664 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 for (int i = 0; i < num_elems; i++) {
666 ResetRegPool();
667 ResetDefTracking();
668 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700669 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 current_dalvik_offset_ = lab->operands[1];
671 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800672 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
674 OpUnconditionalBranch(resume_lab);
675 }
676}
677
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700678void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 int num_elems = throw_launchpads_.Size();
680 for (int i = 0; i < num_elems; i++) {
681 ResetRegPool();
682 ResetDefTracking();
683 LIR* lab = throw_launchpads_.Get(i);
684 current_dalvik_offset_ = lab->operands[1];
685 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700686 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 int v1 = lab->operands[2];
Brian Carlstrom7fff5442014-04-17 23:11:17 -0700688 int v2 = lab->operands[3];
689 const bool target_x86 = cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 switch (lab->operands[0]) {
Brian Carlstrom7fff5442014-04-17 23:11:17 -0700691 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
692 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
693 if (target_x86) {
694 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
695 mirror::Array::LengthOffset().Int32Value());
696 } else {
697 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
698 }
699 // Make sure the following LoadConstant doesn't mess with kArg1.
700 LockTemp(TargetReg(kArg1));
701 LoadConstant(TargetReg(kArg0), v2);
702 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
703 break;
704 case kThrowArrayBounds:
705 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
706 if (v2 != TargetReg(kArg0).GetReg()) {
707 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
708 if (target_x86) {
709 // x86 leaves the array pointer in v2, so load the array length that the handler expects
710 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
711 mirror::Array::LengthOffset().Int32Value());
712 } else {
713 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
714 }
715 } else {
716 if (v1 == TargetReg(kArg1).GetReg()) {
717 // Swap v1 and v2, using kArg2 as a temp
718 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
719 if (target_x86) {
720 // x86 leaves the array pointer in v2; load the array length that the handler expects
721 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
722 mirror::Array::LengthOffset().Int32Value());
723 } else {
724 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
725 }
726 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
727 } else {
728 if (target_x86) {
729 // x86 leaves the array pointer in v2; load the array length that the handler expects
730 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
731 mirror::Array::LengthOffset().Int32Value());
732 } else {
733 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
734 }
735 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
736 }
737 }
738 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds);
739 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800741 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700743 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 default:
746 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
747 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000748 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800749 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700750 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 }
752}
753
Vladimir Markobe0e5462014-02-26 11:24:15 +0000754void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700756 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000757 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
758 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
759 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 RegLocation rl_result;
761 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000762 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 rl_obj = LoadValue(rl_obj, kCoreReg);
764 if (is_long_or_double) {
765 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800766 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700767 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700768 RegisterClass result_reg_kind = kAnyReg;
769 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
770 // Force long/double volatile loads into SSE registers to avoid tearing.
771 result_reg_kind = kFPReg;
772 }
773 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800774 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
775 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800776 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000777 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800778 // Without context sensitive analysis, we must issue the most conservative barriers.
779 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800781 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 }
783 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800784 RegStorage reg_ptr = AllocTemp();
785 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800787 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700788 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000789 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800790 // Without context sensitive analysis, we must issue the most conservative barriers.
791 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800793 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 }
795 FreeTemp(reg_ptr);
796 }
797 StoreValueWide(rl_dest, rl_result);
798 } else {
799 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800800 GenNullCheck(rl_obj.reg, opt_flags);
801 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
802 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800803 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000804 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800805 // Without context sensitive analysis, we must issue the most conservative barriers.
806 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800808 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 }
810 StoreValue(rl_dest, rl_result);
811 }
812 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700813 ThreadOffset<4> getterOffset =
814 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
815 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
816 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000817 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 if (is_long_or_double) {
819 RegLocation rl_result = GetReturnWide(rl_dest.fp);
820 StoreValueWide(rl_dest, rl_result);
821 } else {
822 RegLocation rl_result = GetReturn(rl_dest.fp);
823 StoreValue(rl_dest, rl_result);
824 }
825 }
826}
827
Vladimir Markobe0e5462014-02-26 11:24:15 +0000828void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700830 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000831 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
832 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
833 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000835 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 rl_obj = LoadValue(rl_obj, kCoreReg);
837 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700838 RegisterClass src_reg_kind = kAnyReg;
839 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
840 // Force long/double volatile stores into SSE registers to avoid tearing.
841 src_reg_kind = kFPReg;
842 }
843 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800844 GenNullCheck(rl_obj.reg, opt_flags);
845 RegStorage reg_ptr = AllocTemp();
846 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000847 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800848 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 GenMemBarrier(kStoreStore);
850 }
buzbee2700f7e2014-03-07 09:46:20 -0800851 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800852 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000853 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800854 // A load might follow the volatile store so insert a StoreLoad barrier.
855 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 }
857 FreeTemp(reg_ptr);
858 } else {
859 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800860 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000861 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800862 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 GenMemBarrier(kStoreStore);
864 }
buzbee2700f7e2014-03-07 09:46:20 -0800865 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800866 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000867 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800868 // A load might follow the volatile store so insert a StoreLoad barrier.
869 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 }
871 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800872 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 }
874 }
875 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700876 ThreadOffset<4> setter_offset =
877 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
878 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
879 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000880 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
881 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 }
883}
884
Ian Rogersa9a82542013-10-04 11:17:26 -0700885void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
886 RegLocation rl_src) {
887 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
888 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
889 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700890 ThreadOffset<4> helper = needs_range_check
891 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
892 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
893 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700894 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
895}
896
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800899 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
901 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
902 *cu_->dex_file,
903 type_idx)) {
904 // Call out to helper which resolves type and verifies access.
905 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700906 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800907 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 RegLocation rl_result = GetReturn(false);
909 StoreValue(rl_dest, rl_result);
910 } else {
911 // We're don't need access checks, load type from dex cache
912 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700913 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800914 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 int32_t offset_of_type =
916 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
917 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800918 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
920 type_idx) || SLOW_TYPE_PATH) {
921 // Slow path, at runtime test if type is null and if so initialize
922 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800923 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800924 LIR* cont = NewLIR0(kPseudoTargetLabel);
925
926 // Object to generate the slow path for class resolution.
927 class SlowPath : public LIRSlowPath {
928 public:
929 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
930 const RegLocation& rl_method, const RegLocation& rl_result) :
931 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
932 rl_method_(rl_method), rl_result_(rl_result) {
933 }
934
935 void Compile() {
936 GenerateTargetLabel();
937
Ian Rogersdd7624d2014-03-14 17:43:00 -0700938 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800939 rl_method_.reg, true);
940 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800941
942 m2l_->OpUnconditionalBranch(cont_);
943 }
944
945 private:
946 const int type_idx_;
947 const RegLocation rl_method_;
948 const RegLocation rl_result_;
949 };
950
951 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800952 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800953
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800955 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 // Fast path, we're done - just store result
957 StoreValue(rl_dest, rl_result);
958 }
959 }
960}
961
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700962void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 /* NOTE: Most strings should be available at compile time */
964 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
965 (sizeof(mirror::String*) * string_idx);
966 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
967 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
968 // slow path, resolve string if not in dex cache
969 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700970 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800971
972 // If the Method* is already in a register, we can save a copy.
973 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800974 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800975 if (rl_method.location == kLocPhysReg) {
976 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800977 DCHECK(!IsTemp(rl_method.reg));
978 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800979 } else {
980 r_method = TargetReg(kArg2);
981 LoadCurrMethodDirect(r_method);
982 }
983 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
984 TargetReg(kArg0));
985
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800988 if (cu_->instruction_set == kThumb2 ||
989 cu_->instruction_set == kMips) {
990 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800991 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800992 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
993 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800995
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800996 // Object to generate the slow path for string resolution.
997 class SlowPath : public LIRSlowPath {
998 public:
buzbee2700f7e2014-03-07 09:46:20 -0800999 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001000 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
1001 }
1002
1003 void Compile() {
1004 GenerateTargetLabel();
1005
Dave Allisond6ed6422014-04-09 23:36:15 +00001006 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001007
Dave Allisond6ed6422014-04-09 23:36:15 +00001008 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
1009 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
1010 m2l_->MarkSafepointPC(call_inst);
1011 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001012
1013 m2l_->OpUnconditionalBranch(cont_);
1014 }
1015
1016 private:
buzbee2700f7e2014-03-07 09:46:20 -08001017 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001018 };
1019
1020 // Add to list for future.
1021 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001023 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001024 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1025 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001026 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001027 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001028 LIR* target = NewLIR0(kPseudoTargetLabel);
1029 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 }
1031 GenBarrier();
1032 StoreValue(rl_dest, GetReturn(false));
1033 } else {
1034 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001035 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001037 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1038 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 StoreValue(rl_dest, rl_result);
1040 }
1041}
1042
1043/*
1044 * Let helper function take care of everything. Will
1045 * call Class::NewInstanceFromCode(type_idx, method);
1046 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001047void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 FlushAllRegs(); /* Everything to home location */
1049 // alloc will always check for resolution, do we also need to verify
1050 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001051 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 const DexFile* dex_file = cu_->dex_file;
1053 CompilerDriver* driver = cu_->compiler_driver;
1054 if (driver->CanAccessInstantiableTypeWithoutChecks(
1055 cu_->method_idx, *dex_file, type_idx)) {
1056 bool is_type_initialized;
1057 bool use_direct_type_ptr;
1058 uintptr_t direct_type_ptr;
1059 if (kEmbedClassInCode &&
1060 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1061 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1062 // The fast path.
1063 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001064 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001066 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001067 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1068 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001069 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1071 }
1072 } else {
1073 // Use the direct pointer.
1074 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001075 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001076 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1077 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001078 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001079 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1080 }
1081 }
1082 } else {
1083 // The slow path.
1084 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001085 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001086 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1087 }
1088 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001090 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 RegLocation rl_result = GetReturn(false);
1094 StoreValue(rl_dest, rl_result);
1095}
1096
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001097void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001099 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100}
1101
1102// For final classes there are no sub-classes to check and so we can answer the instance-of
1103// question with simple comparisons.
1104void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1105 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001106 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001107 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001108
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 RegLocation object = LoadValue(rl_src, kCoreReg);
1110 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001111 RegStorage result_reg = rl_result.reg;
1112 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 result_reg = AllocTypedTemp(false, kCoreReg);
1114 }
1115 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001116 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117
buzbee2700f7e2014-03-07 09:46:20 -08001118 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1119 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120
1121 LoadCurrMethodDirect(check_class);
1122 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001123 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1124 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001126 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001128 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 int32_t offset_of_type =
1130 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1131 (sizeof(mirror::Class*) * type_idx);
1132 LoadWordDisp(check_class, offset_of_type, check_class);
1133 }
1134
1135 LIR* ne_branchover = NULL;
1136 if (cu_->instruction_set == kThumb2) {
1137 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001138 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001140 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 } else {
1142 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1143 LoadConstant(result_reg, 1); // eq case - load true
1144 }
1145 LIR* target = NewLIR0(kPseudoTargetLabel);
1146 null_branchover->target = target;
1147 if (ne_branchover != NULL) {
1148 ne_branchover->target = target;
1149 }
1150 FreeTemp(object_class);
1151 FreeTemp(check_class);
1152 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001153 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 FreeTemp(result_reg);
1155 }
1156 StoreValue(rl_dest, rl_result);
1157}
1158
1159void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1160 bool type_known_abstract, bool use_declaring_class,
1161 bool can_assume_type_is_in_dex_cache,
1162 uint32_t type_idx, RegLocation rl_dest,
1163 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001164 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001165 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001166
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 FlushAllRegs();
1168 // May generate a call - use explicit registers
1169 LockCallTemps();
1170 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001171 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (needs_access_check) {
1173 // Check we have access to type_idx and if not throw IllegalAccessError,
1174 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001175 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 type_idx, true);
1177 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1178 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1179 } else if (use_declaring_class) {
1180 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001181 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1182 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 } else {
1184 // Load dex cache entry into class_reg (kArg2)
1185 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001186 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1187 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 int32_t offset_of_type =
1189 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1190 * type_idx);
1191 LoadWordDisp(class_reg, offset_of_type, class_reg);
1192 if (!can_assume_type_is_in_dex_cache) {
1193 // Need to test presence of type in dex cache at runtime
1194 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1195 // Not resolved
1196 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001197 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001198 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1200 // Rejoin code paths
1201 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1202 hop_branch->target = hop_target;
1203 }
1204 }
1205 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1206 RegLocation rl_result = GetReturn(false);
1207 if (cu_->instruction_set == kMips) {
1208 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001209 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 }
1211 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1212
1213 /* load object->klass_ */
1214 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1215 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1216 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1217 LIR* branchover = NULL;
1218 if (type_known_final) {
1219 // rl_result == ref == null == 0.
1220 if (cu_->instruction_set == kThumb2) {
1221 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001222 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001223 LoadConstant(rl_result.reg, 1); // .eq case - load true
1224 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001225 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001227 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001229 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 }
1231 } else {
1232 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001233 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001234 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 if (!type_known_abstract) {
1236 /* Uses conditional nullification */
1237 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001238 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1240 }
1241 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1242 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001243 if (it != nullptr) {
1244 OpEndIT(it);
1245 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 FreeTemp(r_tgt);
1247 } else {
1248 if (!type_known_abstract) {
1249 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001250 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1252 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001253 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001254 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1255 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1256 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 }
1258 }
1259 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001260 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 /* branch targets here */
1262 LIR* target = NewLIR0(kPseudoTargetLabel);
1263 StoreValue(rl_dest, rl_result);
1264 branch1->target = target;
1265 if (branchover != NULL) {
1266 branchover->target = target;
1267 }
1268}
1269
1270void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1271 bool type_known_final, type_known_abstract, use_declaring_class;
1272 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1273 *cu_->dex_file,
1274 type_idx,
1275 &type_known_final,
1276 &type_known_abstract,
1277 &use_declaring_class);
1278 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1279 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1280
1281 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1282 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1283 } else {
1284 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1285 use_declaring_class, can_assume_type_is_in_dex_cache,
1286 type_idx, rl_dest, rl_src);
1287 }
1288}
1289
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001290void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 bool type_known_final, type_known_abstract, use_declaring_class;
1292 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1293 *cu_->dex_file,
1294 type_idx,
1295 &type_known_final,
1296 &type_known_abstract,
1297 &use_declaring_class);
1298 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1299 // of the exception throw path.
1300 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001301 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 // Verifier type analysis proved this check cast would never cause an exception.
1303 return;
1304 }
1305 FlushAllRegs();
1306 // May generate a call - use explicit registers
1307 LockCallTemps();
1308 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001309 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 if (needs_access_check) {
1311 // Check we have access to type_idx and if not throw IllegalAccessError,
1312 // returns Class* in kRet0
1313 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001314 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001315 type_idx, TargetReg(kArg1), true);
1316 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1317 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001318 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1319 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 } else {
1321 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001322 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1323 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 int32_t offset_of_type =
1325 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1326 (sizeof(mirror::Class*) * type_idx);
1327 LoadWordDisp(class_reg, offset_of_type, class_reg);
1328 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1329 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001330 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1331 LIR* cont = NewLIR0(kPseudoTargetLabel);
1332
1333 // Slow path to initialize the type. Executed if the type is NULL.
1334 class SlowPath : public LIRSlowPath {
1335 public:
1336 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001337 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001338 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1339 class_reg_(class_reg) {
1340 }
1341
1342 void Compile() {
1343 GenerateTargetLabel();
1344
1345 // Call out to helper, which will return resolved type in kArg0
1346 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001347 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001348 m2l_->TargetReg(kArg1), true);
1349 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1350 m2l_->OpUnconditionalBranch(cont_);
1351 }
1352 public:
1353 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001354 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001355 };
1356
buzbee2700f7e2014-03-07 09:46:20 -08001357 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 }
1359 }
1360 // At this point, class_reg (kArg2) has class
1361 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001362
1363 // Slow path for the case where the classes are not equal. In this case we need
1364 // to call a helper function to do the check.
1365 class SlowPath : public LIRSlowPath {
1366 public:
1367 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1368 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1369 }
1370
1371 void Compile() {
1372 GenerateTargetLabel();
1373
1374 if (load_) {
1375 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1376 m2l_->TargetReg(kArg1));
1377 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001378 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001379 m2l_->TargetReg(kArg1), true);
1380
1381 m2l_->OpUnconditionalBranch(cont_);
1382 }
1383
1384 private:
1385 bool load_;
1386 };
1387
1388 if (type_known_abstract) {
1389 // Easier case, run slow path if target is non-null (slow path will load from target)
1390 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1391 LIR* cont = NewLIR0(kPseudoTargetLabel);
1392 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1393 } else {
1394 // Harder, more common case. We need to generate a forward branch over the load
1395 // if the target is null. If it's non-null we perform the load and branch to the
1396 // slow path if the classes are not equal.
1397
1398 /* Null is OK - continue */
1399 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1400 /* load object->klass_ */
1401 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001402 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001403
1404 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1405 LIR* cont = NewLIR0(kPseudoTargetLabel);
1406
1407 // Add the slow path that will not perform load since this is already done.
1408 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1409
1410 // Set the null check to branch to the continuation.
1411 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 }
1413}
1414
1415void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001416 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 RegLocation rl_result;
1418 if (cu_->instruction_set == kThumb2) {
1419 /*
1420 * NOTE: This is the one place in the code in which we might have
1421 * as many as six live temporary registers. There are 5 in the normal
1422 * set for Arm. Until we have spill capabilities, temporarily add
1423 * lr to the temp set. It is safe to do this locally, but note that
1424 * lr is used explicitly elsewhere in the code generator and cannot
1425 * normally be used as a general temp register.
1426 */
1427 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1428 FreeTemp(TargetReg(kLr)); // and make it available
1429 }
1430 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1431 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1432 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1433 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001434 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1435 RegStorage t_reg = AllocTemp();
1436 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1437 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1438 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 FreeTemp(t_reg);
1440 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001441 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1442 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 }
1444 /*
1445 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1446 * following StoreValueWide might need to allocate a temp register.
1447 * To further work around the lack of a spill capability, explicitly
1448 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1449 * Remove when spill is functional.
1450 */
1451 FreeRegLocTemps(rl_result, rl_src1);
1452 FreeRegLocTemps(rl_result, rl_src2);
1453 StoreValueWide(rl_dest, rl_result);
1454 if (cu_->instruction_set == kThumb2) {
1455 Clobber(TargetReg(kLr));
1456 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1457 }
1458}
1459
1460
1461void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001462 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001463 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464
1465 switch (opcode) {
1466 case Instruction::SHL_LONG:
1467 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001468 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 break;
1470 case Instruction::SHR_LONG:
1471 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001472 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 break;
1474 case Instruction::USHR_LONG:
1475 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001476 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 break;
1478 default:
1479 LOG(FATAL) << "Unexpected case";
1480 }
1481 FlushAllRegs(); /* Send everything to home location */
1482 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1483 RegLocation rl_result = GetReturnWide(false);
1484 StoreValueWide(rl_dest, rl_result);
1485}
1486
1487
1488void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001489 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001490 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 OpKind op = kOpBkpt;
1492 bool is_div_rem = false;
1493 bool check_zero = false;
1494 bool unary = false;
1495 RegLocation rl_result;
1496 bool shift_op = false;
1497 switch (opcode) {
1498 case Instruction::NEG_INT:
1499 op = kOpNeg;
1500 unary = true;
1501 break;
1502 case Instruction::NOT_INT:
1503 op = kOpMvn;
1504 unary = true;
1505 break;
1506 case Instruction::ADD_INT:
1507 case Instruction::ADD_INT_2ADDR:
1508 op = kOpAdd;
1509 break;
1510 case Instruction::SUB_INT:
1511 case Instruction::SUB_INT_2ADDR:
1512 op = kOpSub;
1513 break;
1514 case Instruction::MUL_INT:
1515 case Instruction::MUL_INT_2ADDR:
1516 op = kOpMul;
1517 break;
1518 case Instruction::DIV_INT:
1519 case Instruction::DIV_INT_2ADDR:
1520 check_zero = true;
1521 op = kOpDiv;
1522 is_div_rem = true;
1523 break;
1524 /* NOTE: returns in kArg1 */
1525 case Instruction::REM_INT:
1526 case Instruction::REM_INT_2ADDR:
1527 check_zero = true;
1528 op = kOpRem;
1529 is_div_rem = true;
1530 break;
1531 case Instruction::AND_INT:
1532 case Instruction::AND_INT_2ADDR:
1533 op = kOpAnd;
1534 break;
1535 case Instruction::OR_INT:
1536 case Instruction::OR_INT_2ADDR:
1537 op = kOpOr;
1538 break;
1539 case Instruction::XOR_INT:
1540 case Instruction::XOR_INT_2ADDR:
1541 op = kOpXor;
1542 break;
1543 case Instruction::SHL_INT:
1544 case Instruction::SHL_INT_2ADDR:
1545 shift_op = true;
1546 op = kOpLsl;
1547 break;
1548 case Instruction::SHR_INT:
1549 case Instruction::SHR_INT_2ADDR:
1550 shift_op = true;
1551 op = kOpAsr;
1552 break;
1553 case Instruction::USHR_INT:
1554 case Instruction::USHR_INT_2ADDR:
1555 shift_op = true;
1556 op = kOpLsr;
1557 break;
1558 default:
1559 LOG(FATAL) << "Invalid word arith op: " << opcode;
1560 }
1561 if (!is_div_rem) {
1562 if (unary) {
1563 rl_src1 = LoadValue(rl_src1, kCoreReg);
1564 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001565 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 } else {
1567 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001568 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001569 RegStorage t_reg = AllocTemp();
1570 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 rl_src1 = LoadValue(rl_src1, kCoreReg);
1572 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001573 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 FreeTemp(t_reg);
1575 } else {
1576 rl_src1 = LoadValue(rl_src1, kCoreReg);
1577 rl_src2 = LoadValue(rl_src2, kCoreReg);
1578 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001579 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 }
1581 }
1582 StoreValue(rl_dest, rl_result);
1583 } else {
Dave Allison70202782013-10-22 17:52:19 -07001584 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 -07001585 if (cu_->instruction_set == kMips) {
1586 rl_src1 = LoadValue(rl_src1, kCoreReg);
1587 rl_src2 = LoadValue(rl_src2, kCoreReg);
1588 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001589 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 }
buzbee2700f7e2014-03-07 09:46:20 -08001591 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001592 done = true;
1593 } else if (cu_->instruction_set == kThumb2) {
1594 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1595 // Use ARM SDIV instruction for division. For remainder we also need to
1596 // calculate using a MUL and subtract.
1597 rl_src1 = LoadValue(rl_src1, kCoreReg);
1598 rl_src2 = LoadValue(rl_src2, kCoreReg);
1599 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001600 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001601 }
buzbee2700f7e2014-03-07 09:46:20 -08001602 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001603 done = true;
1604 }
1605 }
1606
1607 // If we haven't already generated the code use the callout function.
1608 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001609 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 FlushAllRegs(); /* Send everything to home location */
1611 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001612 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1614 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001615 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 }
Dave Allison70202782013-10-22 17:52:19 -07001617 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001618 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 if (op == kOpDiv)
1620 rl_result = GetReturn(false);
1621 else
1622 rl_result = GetReturnAlt();
1623 }
1624 StoreValue(rl_dest, rl_result);
1625 }
1626}
1627
1628/*
1629 * The following are the first-level codegen routines that analyze the format
1630 * of each bytecode then either dispatch special purpose codegen routines
1631 * or produce corresponding Thumb instructions directly.
1632 */
1633
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001635static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001636 x &= x - 1;
1637 return (x & (x - 1)) == 0;
1638}
1639
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1641// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001642bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001643 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1645 return false;
1646 }
1647 // No divide instruction for Arm, so check for more special cases
1648 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001649 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 }
1651 int k = LowestSetBit(lit);
1652 if (k >= 30) {
1653 // Avoid special cases.
1654 return false;
1655 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 rl_src = LoadValue(rl_src, kCoreReg);
1657 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001658 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001659 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 if (lit == 2) {
1661 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001662 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1663 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1664 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001666 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001668 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1669 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 }
1671 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001672 RegStorage t_reg1 = AllocTemp();
1673 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1676 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001678 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001680 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001682 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001684 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 }
1686 }
1687 StoreValue(rl_dest, rl_result);
1688 return true;
1689}
1690
1691// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1692// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001693bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001694 if (lit < 0) {
1695 return false;
1696 }
1697 if (lit == 0) {
1698 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1699 LoadConstant(rl_result.reg, 0);
1700 StoreValue(rl_dest, rl_result);
1701 return true;
1702 }
1703 if (lit == 1) {
1704 rl_src = LoadValue(rl_src, kCoreReg);
1705 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1706 OpRegCopy(rl_result.reg, rl_src.reg);
1707 StoreValue(rl_dest, rl_result);
1708 return true;
1709 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001710 // There is RegRegRegShift on Arm, so check for more special cases
1711 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001712 return EasyMultiply(rl_src, rl_dest, lit);
1713 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 // Can we simplify this multiplication?
1715 bool power_of_two = false;
1716 bool pop_count_le2 = false;
1717 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001718 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 power_of_two = true;
1720 } else if (IsPopCountLE2(lit)) {
1721 pop_count_le2 = true;
1722 } else if (IsPowerOfTwo(lit + 1)) {
1723 power_of_two_minus_one = true;
1724 } else {
1725 return false;
1726 }
1727 rl_src = LoadValue(rl_src, kCoreReg);
1728 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1729 if (power_of_two) {
1730 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 } else if (pop_count_le2) {
1733 // Shift and add and shift.
1734 int first_bit = LowestSetBit(lit);
1735 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1736 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1737 } else {
1738 // Reverse subtract: (src << (shift + 1)) - src.
1739 DCHECK(power_of_two_minus_one);
1740 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001741 RegStorage t_reg = AllocTemp();
1742 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1743 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 }
1745 StoreValue(rl_dest, rl_result);
1746 return true;
1747}
1748
1749void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001750 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 RegLocation rl_result;
1752 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1753 int shift_op = false;
1754 bool is_div = false;
1755
1756 switch (opcode) {
1757 case Instruction::RSUB_INT_LIT8:
1758 case Instruction::RSUB_INT: {
1759 rl_src = LoadValue(rl_src, kCoreReg);
1760 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1761 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001762 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001764 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1765 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 }
1767 StoreValue(rl_dest, rl_result);
1768 return;
1769 }
1770
1771 case Instruction::SUB_INT:
1772 case Instruction::SUB_INT_2ADDR:
1773 lit = -lit;
1774 // Intended fallthrough
1775 case Instruction::ADD_INT:
1776 case Instruction::ADD_INT_2ADDR:
1777 case Instruction::ADD_INT_LIT8:
1778 case Instruction::ADD_INT_LIT16:
1779 op = kOpAdd;
1780 break;
1781 case Instruction::MUL_INT:
1782 case Instruction::MUL_INT_2ADDR:
1783 case Instruction::MUL_INT_LIT8:
1784 case Instruction::MUL_INT_LIT16: {
1785 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1786 return;
1787 }
1788 op = kOpMul;
1789 break;
1790 }
1791 case Instruction::AND_INT:
1792 case Instruction::AND_INT_2ADDR:
1793 case Instruction::AND_INT_LIT8:
1794 case Instruction::AND_INT_LIT16:
1795 op = kOpAnd;
1796 break;
1797 case Instruction::OR_INT:
1798 case Instruction::OR_INT_2ADDR:
1799 case Instruction::OR_INT_LIT8:
1800 case Instruction::OR_INT_LIT16:
1801 op = kOpOr;
1802 break;
1803 case Instruction::XOR_INT:
1804 case Instruction::XOR_INT_2ADDR:
1805 case Instruction::XOR_INT_LIT8:
1806 case Instruction::XOR_INT_LIT16:
1807 op = kOpXor;
1808 break;
1809 case Instruction::SHL_INT_LIT8:
1810 case Instruction::SHL_INT:
1811 case Instruction::SHL_INT_2ADDR:
1812 lit &= 31;
1813 shift_op = true;
1814 op = kOpLsl;
1815 break;
1816 case Instruction::SHR_INT_LIT8:
1817 case Instruction::SHR_INT:
1818 case Instruction::SHR_INT_2ADDR:
1819 lit &= 31;
1820 shift_op = true;
1821 op = kOpAsr;
1822 break;
1823 case Instruction::USHR_INT_LIT8:
1824 case Instruction::USHR_INT:
1825 case Instruction::USHR_INT_2ADDR:
1826 lit &= 31;
1827 shift_op = true;
1828 op = kOpLsr;
1829 break;
1830
1831 case Instruction::DIV_INT:
1832 case Instruction::DIV_INT_2ADDR:
1833 case Instruction::DIV_INT_LIT8:
1834 case Instruction::DIV_INT_LIT16:
1835 case Instruction::REM_INT:
1836 case Instruction::REM_INT_2ADDR:
1837 case Instruction::REM_INT_LIT8:
1838 case Instruction::REM_INT_LIT16: {
1839 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001840 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 return;
1842 }
buzbee11b63d12013-08-27 07:34:17 -07001843 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001844 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001845 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 (opcode == Instruction::DIV_INT_LIT16)) {
1847 is_div = true;
1848 } else {
1849 is_div = false;
1850 }
buzbee11b63d12013-08-27 07:34:17 -07001851 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1852 return;
1853 }
Dave Allison70202782013-10-22 17:52:19 -07001854
1855 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001856 if (cu_->instruction_set == kMips) {
1857 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001858 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001859 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001860 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001861 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1862 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001863 } else if (cu_->instruction_set == kThumb2) {
1864 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1865 // Use ARM SDIV instruction for division. For remainder we also need to
1866 // calculate using a MUL and subtract.
1867 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001868 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001869 done = true;
1870 }
1871 }
1872
1873 if (!done) {
1874 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1876 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001877 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1879 if (is_div)
1880 rl_result = GetReturn(false);
1881 else
1882 rl_result = GetReturnAlt();
1883 }
1884 StoreValue(rl_dest, rl_result);
1885 return;
1886 }
1887 default:
1888 LOG(FATAL) << "Unexpected opcode " << opcode;
1889 }
1890 rl_src = LoadValue(rl_src, kCoreReg);
1891 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001892 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001894 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001896 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 }
1898 StoreValue(rl_dest, rl_result);
1899}
1900
1901void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001902 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001903 RegLocation rl_result;
1904 OpKind first_op = kOpBkpt;
1905 OpKind second_op = kOpBkpt;
1906 bool call_out = false;
1907 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001908 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001909 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910
1911 switch (opcode) {
1912 case Instruction::NOT_LONG:
1913 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1914 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1915 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001916 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1917 RegStorage t_reg = AllocTemp();
1918 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1919 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1920 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 FreeTemp(t_reg);
1922 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001923 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1924 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 }
1926 StoreValueWide(rl_dest, rl_result);
1927 return;
1928 case Instruction::ADD_LONG:
1929 case Instruction::ADD_LONG_2ADDR:
1930 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001931 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001932 return;
1933 }
1934 first_op = kOpAdd;
1935 second_op = kOpAdc;
1936 break;
1937 case Instruction::SUB_LONG:
1938 case Instruction::SUB_LONG_2ADDR:
1939 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001940 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 return;
1942 }
1943 first_op = kOpSub;
1944 second_op = kOpSbc;
1945 break;
1946 case Instruction::MUL_LONG:
1947 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001948 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001949 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 return;
1951 } else {
1952 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001953 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001954 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 }
1956 break;
1957 case Instruction::DIV_LONG:
1958 case Instruction::DIV_LONG_2ADDR:
1959 call_out = true;
1960 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001961 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001962 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001963 break;
1964 case Instruction::REM_LONG:
1965 case Instruction::REM_LONG_2ADDR:
1966 call_out = true;
1967 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001968 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001970 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001971 break;
1972 case Instruction::AND_LONG_2ADDR:
1973 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001974 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001975 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 }
1977 first_op = kOpAnd;
1978 second_op = kOpAnd;
1979 break;
1980 case Instruction::OR_LONG:
1981 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001982 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001983 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 return;
1985 }
1986 first_op = kOpOr;
1987 second_op = kOpOr;
1988 break;
1989 case Instruction::XOR_LONG:
1990 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001991 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001992 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 return;
1994 }
1995 first_op = kOpXor;
1996 second_op = kOpXor;
1997 break;
1998 case Instruction::NEG_LONG: {
1999 GenNegLong(rl_dest, rl_src2);
2000 return;
2001 }
2002 default:
2003 LOG(FATAL) << "Invalid long arith op";
2004 }
2005 if (!call_out) {
2006 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
2007 } else {
2008 FlushAllRegs(); /* Send everything to home location */
2009 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08002010 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
2011 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
2012 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2013 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07002014 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08002015 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 // NOTE: callout here is not a safepoint
2017 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2018 } else {
2019 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2020 }
2021 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002022 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 rl_result = GetReturnWide(false);
2024 else
2025 rl_result = GetReturnWideAlt();
2026 StoreValueWide(rl_dest, rl_result);
2027 }
2028}
2029
Ian Rogersdd7624d2014-03-14 17:43:00 -07002030void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002031 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 /*
2033 * Don't optimize the register usage since it calls out to support
2034 * functions
2035 */
2036 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2038 if (rl_dest.wide) {
2039 RegLocation rl_result;
2040 rl_result = GetReturnWide(rl_dest.fp);
2041 StoreValueWide(rl_dest, rl_result);
2042 } else {
2043 RegLocation rl_result;
2044 rl_result = GetReturn(rl_dest.fp);
2045 StoreValue(rl_dest, rl_result);
2046 }
2047}
2048
2049/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002050void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002051 if (Runtime::Current()->ExplicitSuspendChecks()) {
2052 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2053 return;
2054 }
2055 FlushAllRegs();
2056 LIR* branch = OpTestSuspend(NULL);
2057 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2058 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2059 current_dalvik_offset_);
2060 branch->target = target;
2061 suspend_launchpads_.Insert(target);
2062 } else {
2063 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2064 return;
2065 }
2066 FlushAllRegs(); // TODO: needed?
2067 LIR* inst = CheckSuspendUsingLoad();
2068 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070}
2071
2072/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002073void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002074 if (Runtime::Current()->ExplicitSuspendChecks()) {
2075 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2076 OpUnconditionalBranch(target);
2077 return;
2078 }
2079 OpTestSuspend(target);
2080 LIR* launch_pad =
2081 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2082 current_dalvik_offset_);
2083 FlushAllRegs();
2084 OpUnconditionalBranch(launch_pad);
2085 suspend_launchpads_.Insert(launch_pad);
2086 } else {
2087 // For the implicit suspend check, just perform the trigger
2088 // load and branch to the target.
2089 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2090 OpUnconditionalBranch(target);
2091 return;
2092 }
2093 FlushAllRegs();
2094 LIR* inst = CheckSuspendUsingLoad();
2095 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098}
2099
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002100/* Call out to helper assembly routine that will null check obj and then lock it. */
2101void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2102 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002103 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002104}
2105
2106/* Call out to helper assembly routine that will null check obj and then unlock it. */
2107void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2108 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002109 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002110}
2111
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002112/* Generic code for generating a wide constant into a VR. */
2113void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2114 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002115 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002116 StoreValueWide(rl_dest, rl_result);
2117}
2118
Brian Carlstrom7940e442013-07-12 13:46:57 -07002119} // namespace art