blob: 6781a9bed4c79435dac9b4f5d1c5e1a2472451f1 [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 Yang80365d92014-04-18 12:10:58 -070094void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
95 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
96 public:
97 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
98 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
99 index_(index), length_(length) {
100 }
101
102 void Compile() OVERRIDE {
103 m2l_->ResetRegPool();
104 m2l_->ResetDefTracking();
105 GenerateTargetLabel();
106 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
107 index_, length_, true);
108 }
109
110 private:
111 const RegStorage index_;
112 const RegStorage length_;
113 };
114
115 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
116 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
117}
118
119void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
120 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
121 public:
122 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
123 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
124 index_(index), length_(length) {
125 }
126
127 void Compile() OVERRIDE {
128 m2l_->ResetRegPool();
129 m2l_->ResetDefTracking();
130 GenerateTargetLabel();
131
132 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
133 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
134 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
135 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
136 }
137
138 private:
139 const int32_t index_;
140 const RegStorage length_;
141 };
142
143 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
144 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
145}
146
Mingyao Yange643a172014-04-08 11:02:52 -0700147LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
148 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
149 public:
150 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
151 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
152 }
153
154 void Compile() OVERRIDE {
155 m2l_->ResetRegPool();
156 m2l_->ResetDefTracking();
157 GenerateTargetLabel();
158 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
159 }
160 };
161
162 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
163 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
164 return branch;
165}
166
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800168LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800169 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700170 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 }
Dave Allisonb373e092014-02-20 16:06:36 -0800172 return nullptr;
173}
174
Dave Allisonf9439142014-03-27 15:10:22 -0700175/* Perform an explicit null-check on a register. */
176LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
177 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
178 return NULL;
179 }
Mingyao Yange643a172014-04-08 11:02:52 -0700180 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700181}
182
Dave Allisonb373e092014-02-20 16:06:36 -0800183void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
184 if (!Runtime::Current()->ExplicitNullChecks()) {
185 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
186 return;
187 }
188 MarkSafepointPC(last_lir_insn_);
189 }
190}
191
192void Mir2Lir::MarkPossibleStackOverflowException() {
193 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
194 MarkSafepointPC(last_lir_insn_);
195 }
196}
197
buzbee2700f7e2014-03-07 09:46:20 -0800198void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800199 if (!Runtime::Current()->ExplicitNullChecks()) {
200 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
201 return;
202 }
203 // Force an implicit null check by performing a memory operation (load) from the given
204 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800205 RegStorage tmp = AllocTemp();
206 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800207 LIR* load = LoadWordDisp(reg, 0, tmp);
208 FreeTemp(tmp);
209 MarkSafepointPC(load);
210 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211}
212
213/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800214LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800216 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
217 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
219 // Remember branch target - will process later
220 throw_launchpads_.Insert(tgt);
221 return branch;
222}
223
224void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
225 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700226 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 ConditionCode cond;
228 switch (opcode) {
229 case Instruction::IF_EQ:
230 cond = kCondEq;
231 break;
232 case Instruction::IF_NE:
233 cond = kCondNe;
234 break;
235 case Instruction::IF_LT:
236 cond = kCondLt;
237 break;
238 case Instruction::IF_GE:
239 cond = kCondGe;
240 break;
241 case Instruction::IF_GT:
242 cond = kCondGt;
243 break;
244 case Instruction::IF_LE:
245 cond = kCondLe;
246 break;
247 default:
248 cond = static_cast<ConditionCode>(0);
249 LOG(FATAL) << "Unexpected opcode " << opcode;
250 }
251
252 // Normalize such that if either operand is constant, src2 will be constant
253 if (rl_src1.is_const) {
254 RegLocation rl_temp = rl_src1;
255 rl_src1 = rl_src2;
256 rl_src2 = rl_temp;
257 cond = FlipComparisonOrder(cond);
258 }
259
260 rl_src1 = LoadValue(rl_src1, kCoreReg);
261 // Is this really an immediate comparison?
262 if (rl_src2.is_const) {
263 // If it's already live in a register or not easily materialized, just keep going
264 RegLocation rl_temp = UpdateLoc(rl_src2);
265 if ((rl_temp.location == kLocDalvikFrame) &&
266 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
267 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800268 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 return;
270 }
271 }
272 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800273 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274}
275
276void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700277 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 ConditionCode cond;
279 rl_src = LoadValue(rl_src, kCoreReg);
280 switch (opcode) {
281 case Instruction::IF_EQZ:
282 cond = kCondEq;
283 break;
284 case Instruction::IF_NEZ:
285 cond = kCondNe;
286 break;
287 case Instruction::IF_LTZ:
288 cond = kCondLt;
289 break;
290 case Instruction::IF_GEZ:
291 cond = kCondGe;
292 break;
293 case Instruction::IF_GTZ:
294 cond = kCondGt;
295 break;
296 case Instruction::IF_LEZ:
297 cond = kCondLe;
298 break;
299 default:
300 cond = static_cast<ConditionCode>(0);
301 LOG(FATAL) << "Unexpected opcode " << opcode;
302 }
buzbee2700f7e2014-03-07 09:46:20 -0800303 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304}
305
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700306void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
308 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800309 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800311 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 }
buzbee2700f7e2014-03-07 09:46:20 -0800313 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 StoreValueWide(rl_dest, rl_result);
315}
316
317void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700318 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700319 rl_src = LoadValue(rl_src, kCoreReg);
320 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
321 OpKind op = kOpInvalid;
322 switch (opcode) {
323 case Instruction::INT_TO_BYTE:
324 op = kOp2Byte;
325 break;
326 case Instruction::INT_TO_SHORT:
327 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700329 case Instruction::INT_TO_CHAR:
330 op = kOp2Char;
331 break;
332 default:
333 LOG(ERROR) << "Bad int conversion type";
334 }
buzbee2700f7e2014-03-07 09:46:20 -0800335 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700336 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337}
338
339/*
340 * Let helper function take care of everything. Will call
341 * Array::AllocFromCode(type_idx, method, count);
342 * Note: AllocFromCode will handle checks for errNegativeArraySize.
343 */
344void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700345 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700347 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800348 const DexFile* dex_file = cu_->dex_file;
349 CompilerDriver* driver = cu_->compiler_driver;
350 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800352 bool is_type_initialized; // Ignored as an array does not have an initializer.
353 bool use_direct_type_ptr;
354 uintptr_t direct_type_ptr;
355 if (kEmbedClassInCode &&
356 driver->CanEmbedTypeInCode(*dex_file, type_idx,
357 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
358 // The fast path.
359 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800360 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700361 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800362 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
363 } else {
364 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700365 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800366 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
367 }
368 } else {
369 // The slow path.
370 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700371 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800372 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
373 }
374 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700376 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800377 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 RegLocation rl_result = GetReturn(false);
380 StoreValue(rl_dest, rl_result);
381}
382
383/*
384 * Similar to GenNewArray, but with post-allocation initialization.
385 * Verifier guarantees we're dealing with an array class. Current
386 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
387 * Current code also throws internal unimp if not 'L', '[' or 'I'.
388 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700389void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 int elems = info->num_arg_words;
391 int type_idx = info->index;
392 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700393 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
395 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700396 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700398 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
400 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
401 FreeTemp(TargetReg(kArg2));
402 FreeTemp(TargetReg(kArg1));
403 /*
404 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
405 * return region. Because AllocFromCode placed the new array
406 * in kRet0, we'll just lock it into place. When debugger support is
407 * added, it may be necessary to additionally copy all return
408 * values to a home location in thread-local storage
409 */
410 LockTemp(TargetReg(kRet0));
411
412 // TODO: use the correct component size, currently all supported types
413 // share array alignment with ints (see comment at head of function)
414 size_t component_size = sizeof(int32_t);
415
416 // Having a range of 0 is legal
417 if (info->is_range && (elems > 0)) {
418 /*
419 * Bit of ugliness here. We're going generate a mem copy loop
420 * on the register range, but it is possible that some regs
421 * in the range have been promoted. This is unlikely, but
422 * before generating the copy, we'll just force a flush
423 * of any regs in the source range that have been promoted to
424 * home location.
425 */
426 for (int i = 0; i < elems; i++) {
427 RegLocation loc = UpdateLoc(info->args[i]);
428 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800429 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
431 }
432 /*
433 * TUNING note: generated code here could be much improved, but
434 * this is an uncommon operation and isn't especially performance
435 * critical.
436 */
buzbee2700f7e2014-03-07 09:46:20 -0800437 RegStorage r_src = AllocTemp();
438 RegStorage r_dst = AllocTemp();
439 RegStorage r_idx = AllocTemp();
440 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700441 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 case kThumb2:
443 r_val = TargetReg(kLr);
444 break;
445 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700446 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 FreeTemp(TargetReg(kRet0));
448 r_val = AllocTemp();
449 break;
450 case kMips:
451 r_val = AllocTemp();
452 break;
453 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
454 }
455 // Set up source pointer
456 RegLocation rl_first = info->args[0];
457 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
458 // Set up the target pointer
459 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
460 mirror::Array::DataOffset(component_size).Int32Value());
461 // Set up the loop counter (known to be > 0)
462 LoadConstant(r_idx, elems - 1);
463 // Generate the copy loop. Going backwards for convenience
464 LIR* target = NewLIR0(kPseudoTargetLabel);
465 // Copy next element
466 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
467 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
468 FreeTemp(r_val);
469 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700470 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 // Restore the target pointer
472 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
473 -mirror::Array::DataOffset(component_size).Int32Value());
474 }
475 } else if (!info->is_range) {
476 // TUNING: interleave
477 for (int i = 0; i < elems; i++) {
478 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
479 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800480 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
481 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800483 if (IsTemp(rl_arg.reg)) {
484 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 }
486 }
487 }
488 if (info->result.location != kLocInvalid) {
489 StoreValue(info->result, GetReturn(false /* not fp */));
490 }
491}
492
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800493//
494// Slow path to ensure a class is initialized for sget/sput.
495//
496class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
497 public:
buzbee2700f7e2014-03-07 09:46:20 -0800498 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
499 RegStorage r_base) :
500 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
501 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800502 }
503
504 void Compile() {
505 LIR* unresolved_target = GenerateTargetLabel();
506 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700507 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800508 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800509 // Copy helper's result into r_base, a no-op on all but MIPS.
510 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
511
512 m2l_->OpUnconditionalBranch(cont_);
513 }
514
515 private:
516 LIR* const uninit_;
517 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800518 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800519};
520
Vladimir Markobe0e5462014-02-26 11:24:15 +0000521void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700522 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000523 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
524 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
525 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
526 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800527 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000528 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 // Fast path, static storage base is this method's class
530 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800531 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800532 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
533 if (IsTemp(rl_method.reg)) {
534 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 }
536 } else {
537 // Medium path, static storage base in a different class which requires checks that the other
538 // class is initialized.
539 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000540 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 // May do runtime call so everything to home locations.
542 FlushAllRegs();
543 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800544 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 LockTemp(r_method);
546 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800547 r_base = TargetReg(kArg0);
548 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800549 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800550 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000551 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800552 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000553 if (!field_info.IsInitialized() &&
554 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800555 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800556
557 // The slow path is invoked if the r_base is NULL or the class pointed
558 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800559 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800560 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800562 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800563 mirror::Class::StatusOffset().Int32Value(),
564 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800565 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800566
buzbee2700f7e2014-03-07 09:46:20 -0800567 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000568 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569
570 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 FreeTemp(r_method);
573 }
574 // rBase now holds static storage base
575 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700576 RegisterClass register_kind = kAnyReg;
577 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
578 // Force long/double volatile stores into SSE registers to avoid tearing.
579 register_kind = kFPReg;
580 }
581 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 } else {
583 rl_src = LoadValue(rl_src, kAnyReg);
584 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800586 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 GenMemBarrier(kStoreStore);
588 }
589 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800590 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800592 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000594 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800595 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 GenMemBarrier(kStoreLoad);
597 }
598 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800599 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 } else {
603 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700604 ThreadOffset<4> setter_offset =
605 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
606 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
607 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000608 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 }
610}
611
Vladimir Markobe0e5462014-02-26 11:24:15 +0000612void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700613 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000614 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
615 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
616 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
617 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800618 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000619 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 // Fast path, static storage base is this method's class
621 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800623 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 } else {
625 // Medium path, static storage base in a different class which requires checks that the other
626 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000627 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 // May do runtime call so everything to home locations.
629 FlushAllRegs();
630 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800631 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 LockTemp(r_method);
633 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800634 r_base = TargetReg(kArg0);
635 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800636 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800637 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000638 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800639 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000640 if (!field_info.IsInitialized() &&
641 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800642 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800643
644 // The slow path is invoked if the r_base is NULL or the class pointed
645 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800646 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800647 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800648 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800649 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800650 mirror::Class::StatusOffset().Int32Value(),
651 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800652 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800653
buzbee2700f7e2014-03-07 09:46:20 -0800654 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000655 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800656
657 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 FreeTemp(r_method);
660 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800661 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700662 RegisterClass result_reg_kind = kAnyReg;
663 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
664 // Force long/double volatile loads into SSE registers to avoid tearing.
665 result_reg_kind = kFPReg;
666 }
667 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800668
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800670 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800672 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800674 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800675
676 if (field_info.IsVolatile()) {
677 // Without context sensitive analysis, we must issue the most conservative barriers.
678 // In this case, either a load or store may follow so we issue both barriers.
679 GenMemBarrier(kLoadLoad);
680 GenMemBarrier(kLoadStore);
681 }
682
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 if (is_long_or_double) {
684 StoreValueWide(rl_dest, rl_result);
685 } else {
686 StoreValue(rl_dest, rl_result);
687 }
688 } else {
689 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700690 ThreadOffset<4> getterOffset =
691 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
692 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
693 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 if (is_long_or_double) {
696 RegLocation rl_result = GetReturnWide(rl_dest.fp);
697 StoreValueWide(rl_dest, rl_result);
698 } else {
699 RegLocation rl_result = GetReturn(rl_dest.fp);
700 StoreValue(rl_dest, rl_result);
701 }
702 }
703}
704
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800705// Generate code for all slow paths.
706void Mir2Lir::HandleSlowPaths() {
707 int n = slow_paths_.Size();
708 for (int i = 0; i < n; ++i) {
709 LIRSlowPath* slowpath = slow_paths_.Get(i);
710 slowpath->Compile();
711 }
712 slow_paths_.Reset();
713}
714
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700715void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700717 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 for (int i = 0; i < num_elems; i++) {
719 ResetRegPool();
720 ResetDefTracking();
721 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700722 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 current_dalvik_offset_ = lab->operands[1];
724 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800725 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
727 OpUnconditionalBranch(resume_lab);
728 }
729}
730
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700731void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 int num_elems = throw_launchpads_.Size();
733 for (int i = 0; i < num_elems; i++) {
734 ResetRegPool();
735 ResetDefTracking();
736 LIR* lab = throw_launchpads_.Get(i);
737 current_dalvik_offset_ = lab->operands[1];
738 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700739 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 int v1 = lab->operands[2];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 switch (lab->operands[0]) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800743 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700745 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 default:
748 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
749 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000750 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800751 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700752 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 }
754}
755
Vladimir Markobe0e5462014-02-26 11:24:15 +0000756void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700758 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000759 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
760 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
761 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 RegLocation rl_result;
763 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000764 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 rl_obj = LoadValue(rl_obj, kCoreReg);
766 if (is_long_or_double) {
767 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800768 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700769 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700770 RegisterClass result_reg_kind = kAnyReg;
771 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
772 // Force long/double volatile loads into SSE registers to avoid tearing.
773 result_reg_kind = kFPReg;
774 }
775 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800776 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
777 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800778 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000779 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800780 // Without context sensitive analysis, we must issue the most conservative barriers.
781 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800783 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 }
785 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800786 RegStorage reg_ptr = AllocTemp();
787 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800789 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700790 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000791 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800792 // Without context sensitive analysis, we must issue the most conservative barriers.
793 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 }
797 FreeTemp(reg_ptr);
798 }
799 StoreValueWide(rl_dest, rl_result);
800 } else {
801 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800802 GenNullCheck(rl_obj.reg, opt_flags);
803 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
804 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800805 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000806 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800807 // Without context sensitive analysis, we must issue the most conservative barriers.
808 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800810 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 }
812 StoreValue(rl_dest, rl_result);
813 }
814 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700815 ThreadOffset<4> getterOffset =
816 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
817 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
818 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000819 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 if (is_long_or_double) {
821 RegLocation rl_result = GetReturnWide(rl_dest.fp);
822 StoreValueWide(rl_dest, rl_result);
823 } else {
824 RegLocation rl_result = GetReturn(rl_dest.fp);
825 StoreValue(rl_dest, rl_result);
826 }
827 }
828}
829
Vladimir Markobe0e5462014-02-26 11:24:15 +0000830void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700832 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
834 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
835 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000837 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 rl_obj = LoadValue(rl_obj, kCoreReg);
839 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700840 RegisterClass src_reg_kind = kAnyReg;
841 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
842 // Force long/double volatile stores into SSE registers to avoid tearing.
843 src_reg_kind = kFPReg;
844 }
845 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800846 GenNullCheck(rl_obj.reg, opt_flags);
847 RegStorage reg_ptr = AllocTemp();
848 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000849 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800850 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 GenMemBarrier(kStoreStore);
852 }
buzbee2700f7e2014-03-07 09:46:20 -0800853 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800854 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000855 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800856 // A load might follow the volatile store so insert a StoreLoad barrier.
857 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 }
859 FreeTemp(reg_ptr);
860 } else {
861 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800862 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000863 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800864 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 GenMemBarrier(kStoreStore);
866 }
buzbee2700f7e2014-03-07 09:46:20 -0800867 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800868 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000869 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800870 // A load might follow the volatile store so insert a StoreLoad barrier.
871 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 }
873 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800874 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 }
876 }
877 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700878 ThreadOffset<4> setter_offset =
879 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
880 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
881 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000882 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
883 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 }
885}
886
Ian Rogersa9a82542013-10-04 11:17:26 -0700887void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
888 RegLocation rl_src) {
889 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
890 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
891 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700892 ThreadOffset<4> helper = needs_range_check
893 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
894 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
895 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700896 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
897}
898
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700899void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800901 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
903 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
904 *cu_->dex_file,
905 type_idx)) {
906 // Call out to helper which resolves type and verifies access.
907 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700908 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800909 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 RegLocation rl_result = GetReturn(false);
911 StoreValue(rl_dest, rl_result);
912 } else {
913 // We're don't need access checks, load type from dex cache
914 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700915 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800916 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 int32_t offset_of_type =
918 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
919 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800920 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
922 type_idx) || SLOW_TYPE_PATH) {
923 // Slow path, at runtime test if type is null and if so initialize
924 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800925 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800926 LIR* cont = NewLIR0(kPseudoTargetLabel);
927
928 // Object to generate the slow path for class resolution.
929 class SlowPath : public LIRSlowPath {
930 public:
931 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
932 const RegLocation& rl_method, const RegLocation& rl_result) :
933 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
934 rl_method_(rl_method), rl_result_(rl_result) {
935 }
936
937 void Compile() {
938 GenerateTargetLabel();
939
Ian Rogersdd7624d2014-03-14 17:43:00 -0700940 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800941 rl_method_.reg, true);
942 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800943
944 m2l_->OpUnconditionalBranch(cont_);
945 }
946
947 private:
948 const int type_idx_;
949 const RegLocation rl_method_;
950 const RegLocation rl_result_;
951 };
952
953 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800954 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800955
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800957 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 // Fast path, we're done - just store result
959 StoreValue(rl_dest, rl_result);
960 }
961 }
962}
963
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700964void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 /* NOTE: Most strings should be available at compile time */
966 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
967 (sizeof(mirror::String*) * string_idx);
968 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
969 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
970 // slow path, resolve string if not in dex cache
971 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700972 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800973
974 // If the Method* is already in a register, we can save a copy.
975 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800976 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800977 if (rl_method.location == kLocPhysReg) {
978 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800979 DCHECK(!IsTemp(rl_method.reg));
980 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800981 } else {
982 r_method = TargetReg(kArg2);
983 LoadCurrMethodDirect(r_method);
984 }
985 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
986 TargetReg(kArg0));
987
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800990 if (cu_->instruction_set == kThumb2 ||
991 cu_->instruction_set == kMips) {
992 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800993 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800994 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
995 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800997
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800998 // Object to generate the slow path for string resolution.
999 class SlowPath : public LIRSlowPath {
1000 public:
buzbee2700f7e2014-03-07 09:46:20 -08001001 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001002 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
1003 }
1004
1005 void Compile() {
1006 GenerateTargetLabel();
1007
Dave Allisond6ed6422014-04-09 23:36:15 +00001008 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001009
Dave Allisond6ed6422014-04-09 23:36:15 +00001010 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
1011 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
1012 m2l_->MarkSafepointPC(call_inst);
1013 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001014
1015 m2l_->OpUnconditionalBranch(cont_);
1016 }
1017
1018 private:
buzbee2700f7e2014-03-07 09:46:20 -08001019 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001020 };
1021
1022 // Add to list for future.
1023 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001025 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001026 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1027 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001028 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001029 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001030 LIR* target = NewLIR0(kPseudoTargetLabel);
1031 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 }
1033 GenBarrier();
1034 StoreValue(rl_dest, GetReturn(false));
1035 } else {
1036 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001037 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001039 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1040 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 StoreValue(rl_dest, rl_result);
1042 }
1043}
1044
1045/*
1046 * Let helper function take care of everything. Will
1047 * call Class::NewInstanceFromCode(type_idx, method);
1048 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001049void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 FlushAllRegs(); /* Everything to home location */
1051 // alloc will always check for resolution, do we also need to verify
1052 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001053 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001054 const DexFile* dex_file = cu_->dex_file;
1055 CompilerDriver* driver = cu_->compiler_driver;
1056 if (driver->CanAccessInstantiableTypeWithoutChecks(
1057 cu_->method_idx, *dex_file, type_idx)) {
1058 bool is_type_initialized;
1059 bool use_direct_type_ptr;
1060 uintptr_t direct_type_ptr;
1061 if (kEmbedClassInCode &&
1062 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1063 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1064 // The fast path.
1065 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001066 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001067 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001068 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1070 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001071 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1073 }
1074 } else {
1075 // Use the direct pointer.
1076 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001077 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001078 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1079 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001080 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001081 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1082 }
1083 }
1084 } else {
1085 // The slow path.
1086 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001087 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001088 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1089 }
1090 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001092 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001093 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 RegLocation rl_result = GetReturn(false);
1096 StoreValue(rl_dest, rl_result);
1097}
1098
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001101 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102}
1103
1104// For final classes there are no sub-classes to check and so we can answer the instance-of
1105// question with simple comparisons.
1106void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1107 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001108 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001109 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001110
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 RegLocation object = LoadValue(rl_src, kCoreReg);
1112 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001113 RegStorage result_reg = rl_result.reg;
1114 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 result_reg = AllocTypedTemp(false, kCoreReg);
1116 }
1117 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001118 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119
buzbee2700f7e2014-03-07 09:46:20 -08001120 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1121 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122
1123 LoadCurrMethodDirect(check_class);
1124 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001125 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1126 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001128 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001130 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 int32_t offset_of_type =
1132 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1133 (sizeof(mirror::Class*) * type_idx);
1134 LoadWordDisp(check_class, offset_of_type, check_class);
1135 }
1136
1137 LIR* ne_branchover = NULL;
1138 if (cu_->instruction_set == kThumb2) {
1139 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001140 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001142 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 } else {
1144 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1145 LoadConstant(result_reg, 1); // eq case - load true
1146 }
1147 LIR* target = NewLIR0(kPseudoTargetLabel);
1148 null_branchover->target = target;
1149 if (ne_branchover != NULL) {
1150 ne_branchover->target = target;
1151 }
1152 FreeTemp(object_class);
1153 FreeTemp(check_class);
1154 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001155 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 FreeTemp(result_reg);
1157 }
1158 StoreValue(rl_dest, rl_result);
1159}
1160
1161void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1162 bool type_known_abstract, bool use_declaring_class,
1163 bool can_assume_type_is_in_dex_cache,
1164 uint32_t type_idx, RegLocation rl_dest,
1165 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001166 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001167 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001168
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 FlushAllRegs();
1170 // May generate a call - use explicit registers
1171 LockCallTemps();
1172 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001173 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 if (needs_access_check) {
1175 // Check we have access to type_idx and if not throw IllegalAccessError,
1176 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001177 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 type_idx, true);
1179 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1180 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1181 } else if (use_declaring_class) {
1182 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001183 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1184 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 } else {
1186 // Load dex cache entry into class_reg (kArg2)
1187 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001188 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1189 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 int32_t offset_of_type =
1191 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1192 * type_idx);
1193 LoadWordDisp(class_reg, offset_of_type, class_reg);
1194 if (!can_assume_type_is_in_dex_cache) {
1195 // Need to test presence of type in dex cache at runtime
1196 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1197 // Not resolved
1198 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001199 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001200 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1202 // Rejoin code paths
1203 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1204 hop_branch->target = hop_target;
1205 }
1206 }
1207 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1208 RegLocation rl_result = GetReturn(false);
1209 if (cu_->instruction_set == kMips) {
1210 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001211 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 }
1213 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1214
1215 /* load object->klass_ */
1216 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1217 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1218 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1219 LIR* branchover = NULL;
1220 if (type_known_final) {
1221 // rl_result == ref == null == 0.
1222 if (cu_->instruction_set == kThumb2) {
1223 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001224 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001225 LoadConstant(rl_result.reg, 1); // .eq case - load true
1226 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001227 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001229 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001231 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 }
1233 } else {
1234 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001235 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001236 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 if (!type_known_abstract) {
1238 /* Uses conditional nullification */
1239 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001240 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1242 }
1243 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1244 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001245 if (it != nullptr) {
1246 OpEndIT(it);
1247 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 FreeTemp(r_tgt);
1249 } else {
1250 if (!type_known_abstract) {
1251 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001252 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1254 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001255 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001256 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1257 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1258 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
1260 }
1261 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001262 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 /* branch targets here */
1264 LIR* target = NewLIR0(kPseudoTargetLabel);
1265 StoreValue(rl_dest, rl_result);
1266 branch1->target = target;
1267 if (branchover != NULL) {
1268 branchover->target = target;
1269 }
1270}
1271
1272void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1273 bool type_known_final, type_known_abstract, use_declaring_class;
1274 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1275 *cu_->dex_file,
1276 type_idx,
1277 &type_known_final,
1278 &type_known_abstract,
1279 &use_declaring_class);
1280 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1281 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1282
1283 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1284 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1285 } else {
1286 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1287 use_declaring_class, can_assume_type_is_in_dex_cache,
1288 type_idx, rl_dest, rl_src);
1289 }
1290}
1291
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001292void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 bool type_known_final, type_known_abstract, use_declaring_class;
1294 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1295 *cu_->dex_file,
1296 type_idx,
1297 &type_known_final,
1298 &type_known_abstract,
1299 &use_declaring_class);
1300 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1301 // of the exception throw path.
1302 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001303 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 // Verifier type analysis proved this check cast would never cause an exception.
1305 return;
1306 }
1307 FlushAllRegs();
1308 // May generate a call - use explicit registers
1309 LockCallTemps();
1310 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001311 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 if (needs_access_check) {
1313 // Check we have access to type_idx and if not throw IllegalAccessError,
1314 // returns Class* in kRet0
1315 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001316 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 type_idx, TargetReg(kArg1), true);
1318 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1319 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001320 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1321 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 } else {
1323 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001324 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1325 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 int32_t offset_of_type =
1327 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1328 (sizeof(mirror::Class*) * type_idx);
1329 LoadWordDisp(class_reg, offset_of_type, class_reg);
1330 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1331 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001332 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1333 LIR* cont = NewLIR0(kPseudoTargetLabel);
1334
1335 // Slow path to initialize the type. Executed if the type is NULL.
1336 class SlowPath : public LIRSlowPath {
1337 public:
1338 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001339 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001340 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1341 class_reg_(class_reg) {
1342 }
1343
1344 void Compile() {
1345 GenerateTargetLabel();
1346
1347 // Call out to helper, which will return resolved type in kArg0
1348 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001349 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001350 m2l_->TargetReg(kArg1), true);
1351 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1352 m2l_->OpUnconditionalBranch(cont_);
1353 }
1354 public:
1355 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001356 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001357 };
1358
buzbee2700f7e2014-03-07 09:46:20 -08001359 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 }
1361 }
1362 // At this point, class_reg (kArg2) has class
1363 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001364
1365 // Slow path for the case where the classes are not equal. In this case we need
1366 // to call a helper function to do the check.
1367 class SlowPath : public LIRSlowPath {
1368 public:
1369 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1370 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1371 }
1372
1373 void Compile() {
1374 GenerateTargetLabel();
1375
1376 if (load_) {
1377 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1378 m2l_->TargetReg(kArg1));
1379 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001380 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381 m2l_->TargetReg(kArg1), true);
1382
1383 m2l_->OpUnconditionalBranch(cont_);
1384 }
1385
1386 private:
1387 bool load_;
1388 };
1389
1390 if (type_known_abstract) {
1391 // Easier case, run slow path if target is non-null (slow path will load from target)
1392 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1393 LIR* cont = NewLIR0(kPseudoTargetLabel);
1394 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1395 } else {
1396 // Harder, more common case. We need to generate a forward branch over the load
1397 // if the target is null. If it's non-null we perform the load and branch to the
1398 // slow path if the classes are not equal.
1399
1400 /* Null is OK - continue */
1401 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1402 /* load object->klass_ */
1403 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001404 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405
1406 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1407 LIR* cont = NewLIR0(kPseudoTargetLabel);
1408
1409 // Add the slow path that will not perform load since this is already done.
1410 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1411
1412 // Set the null check to branch to the continuation.
1413 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 }
1415}
1416
1417void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001418 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 RegLocation rl_result;
1420 if (cu_->instruction_set == kThumb2) {
1421 /*
1422 * NOTE: This is the one place in the code in which we might have
1423 * as many as six live temporary registers. There are 5 in the normal
1424 * set for Arm. Until we have spill capabilities, temporarily add
1425 * lr to the temp set. It is safe to do this locally, but note that
1426 * lr is used explicitly elsewhere in the code generator and cannot
1427 * normally be used as a general temp register.
1428 */
1429 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1430 FreeTemp(TargetReg(kLr)); // and make it available
1431 }
1432 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1433 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1434 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1435 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001436 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1437 RegStorage t_reg = AllocTemp();
1438 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1439 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1440 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 FreeTemp(t_reg);
1442 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001443 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1444 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 }
1446 /*
1447 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1448 * following StoreValueWide might need to allocate a temp register.
1449 * To further work around the lack of a spill capability, explicitly
1450 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1451 * Remove when spill is functional.
1452 */
1453 FreeRegLocTemps(rl_result, rl_src1);
1454 FreeRegLocTemps(rl_result, rl_src2);
1455 StoreValueWide(rl_dest, rl_result);
1456 if (cu_->instruction_set == kThumb2) {
1457 Clobber(TargetReg(kLr));
1458 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1459 }
1460}
1461
1462
1463void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001464 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001465 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466
1467 switch (opcode) {
1468 case Instruction::SHL_LONG:
1469 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001470 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 break;
1472 case Instruction::SHR_LONG:
1473 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001474 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 break;
1476 case Instruction::USHR_LONG:
1477 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001478 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 break;
1480 default:
1481 LOG(FATAL) << "Unexpected case";
1482 }
1483 FlushAllRegs(); /* Send everything to home location */
1484 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1485 RegLocation rl_result = GetReturnWide(false);
1486 StoreValueWide(rl_dest, rl_result);
1487}
1488
1489
1490void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001491 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001492 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 OpKind op = kOpBkpt;
1494 bool is_div_rem = false;
1495 bool check_zero = false;
1496 bool unary = false;
1497 RegLocation rl_result;
1498 bool shift_op = false;
1499 switch (opcode) {
1500 case Instruction::NEG_INT:
1501 op = kOpNeg;
1502 unary = true;
1503 break;
1504 case Instruction::NOT_INT:
1505 op = kOpMvn;
1506 unary = true;
1507 break;
1508 case Instruction::ADD_INT:
1509 case Instruction::ADD_INT_2ADDR:
1510 op = kOpAdd;
1511 break;
1512 case Instruction::SUB_INT:
1513 case Instruction::SUB_INT_2ADDR:
1514 op = kOpSub;
1515 break;
1516 case Instruction::MUL_INT:
1517 case Instruction::MUL_INT_2ADDR:
1518 op = kOpMul;
1519 break;
1520 case Instruction::DIV_INT:
1521 case Instruction::DIV_INT_2ADDR:
1522 check_zero = true;
1523 op = kOpDiv;
1524 is_div_rem = true;
1525 break;
1526 /* NOTE: returns in kArg1 */
1527 case Instruction::REM_INT:
1528 case Instruction::REM_INT_2ADDR:
1529 check_zero = true;
1530 op = kOpRem;
1531 is_div_rem = true;
1532 break;
1533 case Instruction::AND_INT:
1534 case Instruction::AND_INT_2ADDR:
1535 op = kOpAnd;
1536 break;
1537 case Instruction::OR_INT:
1538 case Instruction::OR_INT_2ADDR:
1539 op = kOpOr;
1540 break;
1541 case Instruction::XOR_INT:
1542 case Instruction::XOR_INT_2ADDR:
1543 op = kOpXor;
1544 break;
1545 case Instruction::SHL_INT:
1546 case Instruction::SHL_INT_2ADDR:
1547 shift_op = true;
1548 op = kOpLsl;
1549 break;
1550 case Instruction::SHR_INT:
1551 case Instruction::SHR_INT_2ADDR:
1552 shift_op = true;
1553 op = kOpAsr;
1554 break;
1555 case Instruction::USHR_INT:
1556 case Instruction::USHR_INT_2ADDR:
1557 shift_op = true;
1558 op = kOpLsr;
1559 break;
1560 default:
1561 LOG(FATAL) << "Invalid word arith op: " << opcode;
1562 }
1563 if (!is_div_rem) {
1564 if (unary) {
1565 rl_src1 = LoadValue(rl_src1, kCoreReg);
1566 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001567 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 } else {
1569 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001570 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001571 RegStorage t_reg = AllocTemp();
1572 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 rl_src1 = LoadValue(rl_src1, kCoreReg);
1574 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001575 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 FreeTemp(t_reg);
1577 } else {
1578 rl_src1 = LoadValue(rl_src1, kCoreReg);
1579 rl_src2 = LoadValue(rl_src2, kCoreReg);
1580 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001581 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 }
1583 }
1584 StoreValue(rl_dest, rl_result);
1585 } else {
Dave Allison70202782013-10-22 17:52:19 -07001586 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 -07001587 if (cu_->instruction_set == kMips) {
1588 rl_src1 = LoadValue(rl_src1, kCoreReg);
1589 rl_src2 = LoadValue(rl_src2, kCoreReg);
1590 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001591 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 }
buzbee2700f7e2014-03-07 09:46:20 -08001593 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001594 done = true;
1595 } else if (cu_->instruction_set == kThumb2) {
1596 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1597 // Use ARM SDIV instruction for division. For remainder we also need to
1598 // calculate using a MUL and subtract.
1599 rl_src1 = LoadValue(rl_src1, kCoreReg);
1600 rl_src2 = LoadValue(rl_src2, kCoreReg);
1601 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001602 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001603 }
buzbee2700f7e2014-03-07 09:46:20 -08001604 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001605 done = true;
1606 }
1607 }
1608
1609 // If we haven't already generated the code use the callout function.
1610 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001611 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 FlushAllRegs(); /* Send everything to home location */
1613 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001614 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1616 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001617 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 }
Dave Allison70202782013-10-22 17:52:19 -07001619 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001620 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 if (op == kOpDiv)
1622 rl_result = GetReturn(false);
1623 else
1624 rl_result = GetReturnAlt();
1625 }
1626 StoreValue(rl_dest, rl_result);
1627 }
1628}
1629
1630/*
1631 * The following are the first-level codegen routines that analyze the format
1632 * of each bytecode then either dispatch special purpose codegen routines
1633 * or produce corresponding Thumb instructions directly.
1634 */
1635
Brian Carlstrom7940e442013-07-12 13:46:57 -07001636// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001637static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 x &= x - 1;
1639 return (x & (x - 1)) == 0;
1640}
1641
Brian Carlstrom7940e442013-07-12 13:46:57 -07001642// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1643// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001644bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001645 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1647 return false;
1648 }
1649 // No divide instruction for Arm, so check for more special cases
1650 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001651 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 }
1653 int k = LowestSetBit(lit);
1654 if (k >= 30) {
1655 // Avoid special cases.
1656 return false;
1657 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 rl_src = LoadValue(rl_src, kCoreReg);
1659 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001660 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001661 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 if (lit == 2) {
1663 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001664 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1665 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1666 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001668 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001670 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1671 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 }
1673 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001674 RegStorage t_reg1 = AllocTemp();
1675 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001677 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1678 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001680 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001682 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001684 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001686 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 }
1688 }
1689 StoreValue(rl_dest, rl_result);
1690 return true;
1691}
1692
1693// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1694// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001695bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001696 if (lit < 0) {
1697 return false;
1698 }
1699 if (lit == 0) {
1700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1701 LoadConstant(rl_result.reg, 0);
1702 StoreValue(rl_dest, rl_result);
1703 return true;
1704 }
1705 if (lit == 1) {
1706 rl_src = LoadValue(rl_src, kCoreReg);
1707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1708 OpRegCopy(rl_result.reg, rl_src.reg);
1709 StoreValue(rl_dest, rl_result);
1710 return true;
1711 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001712 // There is RegRegRegShift on Arm, so check for more special cases
1713 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001714 return EasyMultiply(rl_src, rl_dest, lit);
1715 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 // Can we simplify this multiplication?
1717 bool power_of_two = false;
1718 bool pop_count_le2 = false;
1719 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001720 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 power_of_two = true;
1722 } else if (IsPopCountLE2(lit)) {
1723 pop_count_le2 = true;
1724 } else if (IsPowerOfTwo(lit + 1)) {
1725 power_of_two_minus_one = true;
1726 } else {
1727 return false;
1728 }
1729 rl_src = LoadValue(rl_src, kCoreReg);
1730 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1731 if (power_of_two) {
1732 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001733 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 } else if (pop_count_le2) {
1735 // Shift and add and shift.
1736 int first_bit = LowestSetBit(lit);
1737 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1738 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1739 } else {
1740 // Reverse subtract: (src << (shift + 1)) - src.
1741 DCHECK(power_of_two_minus_one);
1742 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001743 RegStorage t_reg = AllocTemp();
1744 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1745 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 }
1747 StoreValue(rl_dest, rl_result);
1748 return true;
1749}
1750
1751void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001752 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 RegLocation rl_result;
1754 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1755 int shift_op = false;
1756 bool is_div = false;
1757
1758 switch (opcode) {
1759 case Instruction::RSUB_INT_LIT8:
1760 case Instruction::RSUB_INT: {
1761 rl_src = LoadValue(rl_src, kCoreReg);
1762 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1763 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001764 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001766 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1767 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 }
1769 StoreValue(rl_dest, rl_result);
1770 return;
1771 }
1772
1773 case Instruction::SUB_INT:
1774 case Instruction::SUB_INT_2ADDR:
1775 lit = -lit;
1776 // Intended fallthrough
1777 case Instruction::ADD_INT:
1778 case Instruction::ADD_INT_2ADDR:
1779 case Instruction::ADD_INT_LIT8:
1780 case Instruction::ADD_INT_LIT16:
1781 op = kOpAdd;
1782 break;
1783 case Instruction::MUL_INT:
1784 case Instruction::MUL_INT_2ADDR:
1785 case Instruction::MUL_INT_LIT8:
1786 case Instruction::MUL_INT_LIT16: {
1787 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1788 return;
1789 }
1790 op = kOpMul;
1791 break;
1792 }
1793 case Instruction::AND_INT:
1794 case Instruction::AND_INT_2ADDR:
1795 case Instruction::AND_INT_LIT8:
1796 case Instruction::AND_INT_LIT16:
1797 op = kOpAnd;
1798 break;
1799 case Instruction::OR_INT:
1800 case Instruction::OR_INT_2ADDR:
1801 case Instruction::OR_INT_LIT8:
1802 case Instruction::OR_INT_LIT16:
1803 op = kOpOr;
1804 break;
1805 case Instruction::XOR_INT:
1806 case Instruction::XOR_INT_2ADDR:
1807 case Instruction::XOR_INT_LIT8:
1808 case Instruction::XOR_INT_LIT16:
1809 op = kOpXor;
1810 break;
1811 case Instruction::SHL_INT_LIT8:
1812 case Instruction::SHL_INT:
1813 case Instruction::SHL_INT_2ADDR:
1814 lit &= 31;
1815 shift_op = true;
1816 op = kOpLsl;
1817 break;
1818 case Instruction::SHR_INT_LIT8:
1819 case Instruction::SHR_INT:
1820 case Instruction::SHR_INT_2ADDR:
1821 lit &= 31;
1822 shift_op = true;
1823 op = kOpAsr;
1824 break;
1825 case Instruction::USHR_INT_LIT8:
1826 case Instruction::USHR_INT:
1827 case Instruction::USHR_INT_2ADDR:
1828 lit &= 31;
1829 shift_op = true;
1830 op = kOpLsr;
1831 break;
1832
1833 case Instruction::DIV_INT:
1834 case Instruction::DIV_INT_2ADDR:
1835 case Instruction::DIV_INT_LIT8:
1836 case Instruction::DIV_INT_LIT16:
1837 case Instruction::REM_INT:
1838 case Instruction::REM_INT_2ADDR:
1839 case Instruction::REM_INT_LIT8:
1840 case Instruction::REM_INT_LIT16: {
1841 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001842 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 return;
1844 }
buzbee11b63d12013-08-27 07:34:17 -07001845 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001847 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 (opcode == Instruction::DIV_INT_LIT16)) {
1849 is_div = true;
1850 } else {
1851 is_div = false;
1852 }
buzbee11b63d12013-08-27 07:34:17 -07001853 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1854 return;
1855 }
Dave Allison70202782013-10-22 17:52:19 -07001856
1857 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 if (cu_->instruction_set == kMips) {
1859 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001860 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001861 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001862 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001863 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1864 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001865 } else if (cu_->instruction_set == kThumb2) {
1866 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1867 // Use ARM SDIV instruction for division. For remainder we also need to
1868 // calculate using a MUL and subtract.
1869 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001870 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001871 done = true;
1872 }
1873 }
1874
1875 if (!done) {
1876 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1878 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001879 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1881 if (is_div)
1882 rl_result = GetReturn(false);
1883 else
1884 rl_result = GetReturnAlt();
1885 }
1886 StoreValue(rl_dest, rl_result);
1887 return;
1888 }
1889 default:
1890 LOG(FATAL) << "Unexpected opcode " << opcode;
1891 }
1892 rl_src = LoadValue(rl_src, kCoreReg);
1893 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001894 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001896 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001898 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001899 }
1900 StoreValue(rl_dest, rl_result);
1901}
1902
1903void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001904 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001905 RegLocation rl_result;
1906 OpKind first_op = kOpBkpt;
1907 OpKind second_op = kOpBkpt;
1908 bool call_out = false;
1909 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001910 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001911 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912
1913 switch (opcode) {
1914 case Instruction::NOT_LONG:
1915 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1916 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1917 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001918 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1919 RegStorage t_reg = AllocTemp();
1920 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1921 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1922 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 FreeTemp(t_reg);
1924 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001925 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1926 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 }
1928 StoreValueWide(rl_dest, rl_result);
1929 return;
1930 case Instruction::ADD_LONG:
1931 case Instruction::ADD_LONG_2ADDR:
1932 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001933 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 return;
1935 }
1936 first_op = kOpAdd;
1937 second_op = kOpAdc;
1938 break;
1939 case Instruction::SUB_LONG:
1940 case Instruction::SUB_LONG_2ADDR:
1941 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001942 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001943 return;
1944 }
1945 first_op = kOpSub;
1946 second_op = kOpSbc;
1947 break;
1948 case Instruction::MUL_LONG:
1949 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001950 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001951 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 return;
1953 } else {
1954 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001955 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001956 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 }
1958 break;
1959 case Instruction::DIV_LONG:
1960 case Instruction::DIV_LONG_2ADDR:
1961 call_out = true;
1962 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001963 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001964 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965 break;
1966 case Instruction::REM_LONG:
1967 case Instruction::REM_LONG_2ADDR:
1968 call_out = true;
1969 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001970 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001971 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001972 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 break;
1974 case Instruction::AND_LONG_2ADDR:
1975 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001976 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001977 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 }
1979 first_op = kOpAnd;
1980 second_op = kOpAnd;
1981 break;
1982 case Instruction::OR_LONG:
1983 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001984 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001985 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 return;
1987 }
1988 first_op = kOpOr;
1989 second_op = kOpOr;
1990 break;
1991 case Instruction::XOR_LONG:
1992 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001993 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001994 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995 return;
1996 }
1997 first_op = kOpXor;
1998 second_op = kOpXor;
1999 break;
2000 case Instruction::NEG_LONG: {
2001 GenNegLong(rl_dest, rl_src2);
2002 return;
2003 }
2004 default:
2005 LOG(FATAL) << "Invalid long arith op";
2006 }
2007 if (!call_out) {
2008 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
2009 } else {
2010 FlushAllRegs(); /* Send everything to home location */
2011 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08002012 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
2013 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
2014 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2015 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07002016 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08002017 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002018 // NOTE: callout here is not a safepoint
2019 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2020 } else {
2021 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2022 }
2023 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002024 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002025 rl_result = GetReturnWide(false);
2026 else
2027 rl_result = GetReturnWideAlt();
2028 StoreValueWide(rl_dest, rl_result);
2029 }
2030}
2031
Ian Rogersdd7624d2014-03-14 17:43:00 -07002032void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002033 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 /*
2035 * Don't optimize the register usage since it calls out to support
2036 * functions
2037 */
2038 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2040 if (rl_dest.wide) {
2041 RegLocation rl_result;
2042 rl_result = GetReturnWide(rl_dest.fp);
2043 StoreValueWide(rl_dest, rl_result);
2044 } else {
2045 RegLocation rl_result;
2046 rl_result = GetReturn(rl_dest.fp);
2047 StoreValue(rl_dest, rl_result);
2048 }
2049}
2050
2051/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002052void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002053 if (Runtime::Current()->ExplicitSuspendChecks()) {
2054 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2055 return;
2056 }
2057 FlushAllRegs();
2058 LIR* branch = OpTestSuspend(NULL);
2059 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2060 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2061 current_dalvik_offset_);
2062 branch->target = target;
2063 suspend_launchpads_.Insert(target);
2064 } else {
2065 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2066 return;
2067 }
2068 FlushAllRegs(); // TODO: needed?
2069 LIR* inst = CheckSuspendUsingLoad();
2070 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002071 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072}
2073
2074/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002075void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002076 if (Runtime::Current()->ExplicitSuspendChecks()) {
2077 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2078 OpUnconditionalBranch(target);
2079 return;
2080 }
2081 OpTestSuspend(target);
2082 LIR* launch_pad =
2083 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2084 current_dalvik_offset_);
2085 FlushAllRegs();
2086 OpUnconditionalBranch(launch_pad);
2087 suspend_launchpads_.Insert(launch_pad);
2088 } else {
2089 // For the implicit suspend check, just perform the trigger
2090 // load and branch to the target.
2091 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2092 OpUnconditionalBranch(target);
2093 return;
2094 }
2095 FlushAllRegs();
2096 LIR* inst = CheckSuspendUsingLoad();
2097 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002099 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100}
2101
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002102/* Call out to helper assembly routine that will null check obj and then lock it. */
2103void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2104 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002105 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002106}
2107
2108/* Call out to helper assembly routine that will null check obj and then unlock it. */
2109void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2110 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002111 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002112}
2113
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002114/* Generic code for generating a wide constant into a VR. */
2115void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2116 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002117 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002118 StoreValueWide(rl_dest, rl_result);
2119}
2120
Brian Carlstrom7940e442013-07-12 13:46:57 -07002121} // namespace art