blob: de55a055689422f2719aa2aff60b920696608149 [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"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000022#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080023#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080025#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
Andreas Gampe9c3b0892014-04-24 17:33:34 +000029// Shortcuts to repeatedly used long types.
30typedef mirror::ObjectArray<mirror::Object> ObjArray;
31typedef mirror::ObjectArray<mirror::Class> ClassArray;
32
Brian Carlstrom7940e442013-07-12 13:46:57 -070033/*
34 * This source files contains "gen" codegen routines that should
35 * be applicable to most targets. Only mid-level support utilities
36 * and "op" calls may be used here.
37 */
38
39/*
buzbeeb48819d2013-09-14 16:15:25 -070040 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 * blocks.
42 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 LIR* barrier = NewLIR0(kPseudoBarrier);
45 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070046 DCHECK(!barrier->flags.use_def_invalid);
47 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070048}
49
Mingyao Yange643a172014-04-08 11:02:52 -070050void Mir2Lir::GenDivZeroException() {
51 LIR* branch = OpUnconditionalBranch(nullptr);
52 AddDivZeroCheckSlowPath(branch);
53}
54
55void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070056 LIR* branch = OpCondBranch(c_code, nullptr);
57 AddDivZeroCheckSlowPath(branch);
58}
59
Mingyao Yange643a172014-04-08 11:02:52 -070060void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
61 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070062 AddDivZeroCheckSlowPath(branch);
63}
64
65void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
66 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
67 public:
68 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
69 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
70 }
71
Mingyao Yange643a172014-04-08 11:02:52 -070072 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070073 m2l_->ResetRegPool();
74 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070075 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -070076 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
77 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowDivZero), true);
78 } else {
79 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
80 }
Mingyao Yang42894562014-04-07 12:42:16 -070081 }
82 };
83
84 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
85}
Dave Allisonb373e092014-02-20 16:06:36 -080086
Mingyao Yang80365d92014-04-18 12:10:58 -070087void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
88 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
89 public:
90 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
91 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
92 index_(index), length_(length) {
93 }
94
95 void Compile() OVERRIDE {
96 m2l_->ResetRegPool();
97 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070098 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -070099 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
100 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
101 index_, length_, true);
102 } else {
103 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
104 index_, length_, true);
105 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700106 }
107
108 private:
109 const RegStorage index_;
110 const RegStorage length_;
111 };
112
113 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
114 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
115}
116
117void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
118 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
119 public:
120 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
121 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
122 index_(index), length_(length) {
123 }
124
125 void Compile() OVERRIDE {
126 m2l_->ResetRegPool();
127 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700128 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700129
130 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
131 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700132 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
133 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
134 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
135 } else {
136 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
137 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
138 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700139 }
140
141 private:
142 const int32_t index_;
143 const RegStorage length_;
144 };
145
146 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
147 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
148}
149
Mingyao Yange643a172014-04-08 11:02:52 -0700150LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
151 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
152 public:
153 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
154 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
155 }
156
157 void Compile() OVERRIDE {
158 m2l_->ResetRegPool();
159 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700160 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700161 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
162 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowNullPointer), true);
163 } else {
164 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
165 }
Mingyao Yange643a172014-04-08 11:02:52 -0700166 }
167 };
168
169 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
170 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
171 return branch;
172}
173
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800175LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800176 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700177 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 }
Dave Allisonb373e092014-02-20 16:06:36 -0800179 return nullptr;
180}
181
Dave Allisonf9439142014-03-27 15:10:22 -0700182/* Perform an explicit null-check on a register. */
183LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
184 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
185 return NULL;
186 }
Mingyao Yange643a172014-04-08 11:02:52 -0700187 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700188}
189
Dave Allisonb373e092014-02-20 16:06:36 -0800190void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
191 if (!Runtime::Current()->ExplicitNullChecks()) {
192 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
193 return;
194 }
195 MarkSafepointPC(last_lir_insn_);
196 }
197}
198
199void Mir2Lir::MarkPossibleStackOverflowException() {
200 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
201 MarkSafepointPC(last_lir_insn_);
202 }
203}
204
buzbee2700f7e2014-03-07 09:46:20 -0800205void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800206 if (!Runtime::Current()->ExplicitNullChecks()) {
207 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
208 return;
209 }
210 // Force an implicit null check by performing a memory operation (load) from the given
211 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800212 RegStorage tmp = AllocTemp();
213 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700214 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800215 FreeTemp(tmp);
216 MarkSafepointPC(load);
217 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218}
219
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
221 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700222 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 ConditionCode cond;
224 switch (opcode) {
225 case Instruction::IF_EQ:
226 cond = kCondEq;
227 break;
228 case Instruction::IF_NE:
229 cond = kCondNe;
230 break;
231 case Instruction::IF_LT:
232 cond = kCondLt;
233 break;
234 case Instruction::IF_GE:
235 cond = kCondGe;
236 break;
237 case Instruction::IF_GT:
238 cond = kCondGt;
239 break;
240 case Instruction::IF_LE:
241 cond = kCondLe;
242 break;
243 default:
244 cond = static_cast<ConditionCode>(0);
245 LOG(FATAL) << "Unexpected opcode " << opcode;
246 }
247
248 // Normalize such that if either operand is constant, src2 will be constant
249 if (rl_src1.is_const) {
250 RegLocation rl_temp = rl_src1;
251 rl_src1 = rl_src2;
252 rl_src2 = rl_temp;
253 cond = FlipComparisonOrder(cond);
254 }
255
256 rl_src1 = LoadValue(rl_src1, kCoreReg);
257 // Is this really an immediate comparison?
258 if (rl_src2.is_const) {
259 // If it's already live in a register or not easily materialized, just keep going
260 RegLocation rl_temp = UpdateLoc(rl_src2);
261 if ((rl_temp.location == kLocDalvikFrame) &&
262 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
263 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800264 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 return;
266 }
267 }
268 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800269 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270}
271
272void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 ConditionCode cond;
275 rl_src = LoadValue(rl_src, kCoreReg);
276 switch (opcode) {
277 case Instruction::IF_EQZ:
278 cond = kCondEq;
279 break;
280 case Instruction::IF_NEZ:
281 cond = kCondNe;
282 break;
283 case Instruction::IF_LTZ:
284 cond = kCondLt;
285 break;
286 case Instruction::IF_GEZ:
287 cond = kCondGe;
288 break;
289 case Instruction::IF_GTZ:
290 cond = kCondGt;
291 break;
292 case Instruction::IF_LEZ:
293 cond = kCondLe;
294 break;
295 default:
296 cond = static_cast<ConditionCode>(0);
297 LOG(FATAL) << "Unexpected opcode " << opcode;
298 }
buzbee2700f7e2014-03-07 09:46:20 -0800299 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300}
301
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700302void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
304 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800305 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800307 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 }
buzbee2700f7e2014-03-07 09:46:20 -0800309 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 StoreValueWide(rl_dest, rl_result);
311}
312
313void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700314 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700315 rl_src = LoadValue(rl_src, kCoreReg);
316 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
317 OpKind op = kOpInvalid;
318 switch (opcode) {
319 case Instruction::INT_TO_BYTE:
320 op = kOp2Byte;
321 break;
322 case Instruction::INT_TO_SHORT:
323 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700325 case Instruction::INT_TO_CHAR:
326 op = kOp2Char;
327 break;
328 default:
329 LOG(ERROR) << "Bad int conversion type";
330 }
buzbee2700f7e2014-03-07 09:46:20 -0800331 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700332 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333}
334
Andreas Gampe2f244e92014-05-08 03:35:25 -0700335template <size_t pointer_size>
336static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
337 uint32_t type_idx, RegLocation rl_dest,
338 RegLocation rl_src) {
339 mir_to_lir->FlushAllRegs(); /* Everything to home location */
340 ThreadOffset<pointer_size> func_offset(-1);
341 const DexFile* dex_file = cu->dex_file;
342 CompilerDriver* driver = cu->compiler_driver;
343 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
344 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800345 bool is_type_initialized; // Ignored as an array does not have an initializer.
346 bool use_direct_type_ptr;
347 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700348 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800349 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700350 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
351 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800352 // The fast path.
353 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700354 mir_to_lir->LoadClassType(type_idx, kArg0);
355 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
356 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0),
357 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800358 } else {
359 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700360 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
361 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
362 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800363 }
364 } else {
365 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700366 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
367 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800368 }
369 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700371 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
372 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
Andreas Gampe2f244e92014-05-08 03:35:25 -0700374 RegLocation rl_result = mir_to_lir->GetReturn(false);
375 mir_to_lir->StoreValue(rl_dest, rl_result);
376}
377
378/*
379 * Let helper function take care of everything. Will call
380 * Array::AllocFromCode(type_idx, method, count);
381 * Note: AllocFromCode will handle checks for errNegativeArraySize.
382 */
383void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
384 RegLocation rl_src) {
385 if (Is64BitInstructionSet(cu_->instruction_set)) {
386 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
387 } else {
388 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
389 }
390}
391
392template <size_t pointer_size>
393static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
394 ThreadOffset<pointer_size> func_offset(-1);
395 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
396 type_idx)) {
397 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
398 } else {
399 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
400 }
401 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402}
403
404/*
405 * Similar to GenNewArray, but with post-allocation initialization.
406 * Verifier guarantees we're dealing with an array class. Current
407 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
408 * Current code also throws internal unimp if not 'L', '[' or 'I'.
409 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700410void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 int elems = info->num_arg_words;
412 int type_idx = info->index;
413 FlushAllRegs(); /* Everything to home location */
Andreas Gampe2f244e92014-05-08 03:35:25 -0700414 if (Is64BitInstructionSet(cu_->instruction_set)) {
415 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700417 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 FreeTemp(TargetReg(kArg2));
420 FreeTemp(TargetReg(kArg1));
421 /*
422 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
423 * return region. Because AllocFromCode placed the new array
424 * in kRet0, we'll just lock it into place. When debugger support is
425 * added, it may be necessary to additionally copy all return
426 * values to a home location in thread-local storage
427 */
428 LockTemp(TargetReg(kRet0));
429
430 // TODO: use the correct component size, currently all supported types
431 // share array alignment with ints (see comment at head of function)
432 size_t component_size = sizeof(int32_t);
433
434 // Having a range of 0 is legal
435 if (info->is_range && (elems > 0)) {
436 /*
437 * Bit of ugliness here. We're going generate a mem copy loop
438 * on the register range, but it is possible that some regs
439 * in the range have been promoted. This is unlikely, but
440 * before generating the copy, we'll just force a flush
441 * of any regs in the source range that have been promoted to
442 * home location.
443 */
444 for (int i = 0; i < elems; i++) {
445 RegLocation loc = UpdateLoc(info->args[i]);
446 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700447 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 }
449 }
450 /*
451 * TUNING note: generated code here could be much improved, but
452 * this is an uncommon operation and isn't especially performance
453 * critical.
454 */
buzbee2700f7e2014-03-07 09:46:20 -0800455 RegStorage r_src = AllocTemp();
456 RegStorage r_dst = AllocTemp();
457 RegStorage r_idx = AllocTemp();
458 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700459 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 case kThumb2:
461 r_val = TargetReg(kLr);
462 break;
463 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700464 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 FreeTemp(TargetReg(kRet0));
466 r_val = AllocTemp();
467 break;
468 case kMips:
469 r_val = AllocTemp();
470 break;
471 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
472 }
473 // Set up source pointer
474 RegLocation rl_first = info->args[0];
475 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
476 // Set up the target pointer
477 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
478 mirror::Array::DataOffset(component_size).Int32Value());
479 // Set up the loop counter (known to be > 0)
480 LoadConstant(r_idx, elems - 1);
481 // Generate the copy loop. Going backwards for convenience
482 LIR* target = NewLIR0(kPseudoTargetLabel);
483 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700484 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
485 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 FreeTemp(r_val);
487 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700488 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 // Restore the target pointer
490 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
491 -mirror::Array::DataOffset(component_size).Int32Value());
492 }
493 } else if (!info->is_range) {
494 // TUNING: interleave
495 for (int i = 0; i < elems; i++) {
496 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700497 Store32Disp(TargetReg(kRet0),
498 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800500 if (IsTemp(rl_arg.reg)) {
501 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 }
503 }
504 }
505 if (info->result.location != kLocInvalid) {
506 StoreValue(info->result, GetReturn(false /* not fp */));
507 }
508}
509
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800510//
511// Slow path to ensure a class is initialized for sget/sput.
512//
513class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
514 public:
buzbee2700f7e2014-03-07 09:46:20 -0800515 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
516 RegStorage r_base) :
517 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
518 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800519 }
520
521 void Compile() {
522 LIR* unresolved_target = GenerateTargetLabel();
523 uninit_->target = unresolved_target;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700524 if (Is64BitInstructionSet(cu_->instruction_set)) {
525 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
526 storage_index_, true);
527 } else {
528 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
529 storage_index_, true);
530 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800531 // Copy helper's result into r_base, a no-op on all but MIPS.
532 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
533
534 m2l_->OpUnconditionalBranch(cont_);
535 }
536
537 private:
538 LIR* const uninit_;
539 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800540 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800541};
542
Andreas Gampe2f244e92014-05-08 03:35:25 -0700543template <size_t pointer_size>
544static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
545 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
546 ThreadOffset<pointer_size> setter_offset =
547 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
548 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
549 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
550 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
551 true);
552}
553
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000556 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
557 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100558 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
559 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
560 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800562 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000563 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 // Fast path, static storage base is this method's class
565 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800566 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700567 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800568 if (IsTemp(rl_method.reg)) {
569 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 }
571 } else {
572 // Medium path, static storage base in a different class which requires checks that the other
573 // class is initialized.
574 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000575 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 // May do runtime call so everything to home locations.
577 FlushAllRegs();
578 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800579 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 LockTemp(r_method);
581 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800582 r_base = TargetReg(kArg0);
583 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700584 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000585 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
586 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800587 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000588 if (!field_info.IsInitialized() &&
589 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800591
592 // The slow path is invoked if the r_base is NULL or the class pointed
593 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800594 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800595 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800596 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800597 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800598 mirror::Class::StatusOffset().Int32Value(),
599 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601
buzbee2700f7e2014-03-07 09:46:20 -0800602 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000603 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604
605 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 FreeTemp(r_method);
608 }
609 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100610 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100612 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100614 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000616 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800617 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100619 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800620 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100622 } else {
623 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 }
625 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800626 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 } else {
630 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700631 if (Is64BitInstructionSet(cu_->instruction_set)) {
632 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
633 } else {
634 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
635 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 }
637}
638
Andreas Gampe2f244e92014-05-08 03:35:25 -0700639template <size_t pointer_size>
640static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
641 const MirSFieldLoweringInfo* field_info) {
642 ThreadOffset<pointer_size> getter_offset =
643 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
644 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
645 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
646 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
647}
648
Vladimir Markobe0e5462014-02-26 11:24:15 +0000649void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700650 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000651 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
652 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100653 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
654 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
655 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000656 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800657 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000658 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 // Fast path, static storage base is this method's class
660 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800661 r_base = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700662 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 } else {
664 // Medium path, static storage base in a different class which requires checks that the other
665 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000666 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 // May do runtime call so everything to home locations.
668 FlushAllRegs();
669 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800670 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 LockTemp(r_method);
672 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800673 r_base = TargetReg(kArg0);
674 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700675 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000676 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
677 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800678 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000679 if (!field_info.IsInitialized() &&
680 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800681 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800682
683 // The slow path is invoked if the r_base is NULL or the class pointed
684 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800685 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800686 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800687 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800688 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800689 mirror::Class::StatusOffset().Int32Value(),
690 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800691 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800692
buzbee2700f7e2014-03-07 09:46:20 -0800693 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800695
696 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 FreeTemp(r_method);
699 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800700 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100701 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
702 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800703
Vladimir Marko674744e2014-04-24 15:18:26 +0100704 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800705 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100706 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800707 // Without context sensitive analysis, we must issue the most conservative barriers.
708 // In this case, either a load or store may follow so we issue both barriers.
709 GenMemBarrier(kLoadLoad);
710 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100711 } else {
712 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800713 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100714 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800715
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 if (is_long_or_double) {
717 StoreValueWide(rl_dest, rl_result);
718 } else {
719 StoreValue(rl_dest, rl_result);
720 }
721 } else {
722 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700723 if (Is64BitInstructionSet(cu_->instruction_set)) {
724 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
725 } else {
726 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
727 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 if (is_long_or_double) {
729 RegLocation rl_result = GetReturnWide(rl_dest.fp);
730 StoreValueWide(rl_dest, rl_result);
731 } else {
732 RegLocation rl_result = GetReturn(rl_dest.fp);
733 StoreValue(rl_dest, rl_result);
734 }
735 }
736}
737
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800738// Generate code for all slow paths.
739void Mir2Lir::HandleSlowPaths() {
740 int n = slow_paths_.Size();
741 for (int i = 0; i < n; ++i) {
742 LIRSlowPath* slowpath = slow_paths_.Get(i);
743 slowpath->Compile();
744 }
745 slow_paths_.Reset();
746}
747
Andreas Gampe2f244e92014-05-08 03:35:25 -0700748template <size_t pointer_size>
749static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
750 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
751 ThreadOffset<pointer_size> getter_offset =
752 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
753 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
754 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
755 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
756 true);
757}
758
Vladimir Markobe0e5462014-02-26 11:24:15 +0000759void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700761 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000762 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
763 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100764 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
765 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
766 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
767 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 rl_obj = LoadValue(rl_obj, kCoreReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100770 GenNullCheck(rl_obj.reg, opt_flags);
771 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
772 int field_offset = field_info.FieldOffset().Int32Value();
773 if (field_info.IsVolatile()) {
774 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
775 MarkPossibleNullPointerException(opt_flags);
776 // Without context sensitive analysis, we must issue the most conservative barriers.
777 // In this case, either a load or store may follow so we issue both barriers.
778 GenMemBarrier(kLoadLoad);
779 GenMemBarrier(kLoadStore);
780 } else {
781 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
782 MarkPossibleNullPointerException(opt_flags);
783 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 StoreValueWide(rl_dest, rl_result);
786 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 StoreValue(rl_dest, rl_result);
788 }
789 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700790 if (Is64BitInstructionSet(cu_->instruction_set)) {
791 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
792 } else {
793 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 if (is_long_or_double) {
796 RegLocation rl_result = GetReturnWide(rl_dest.fp);
797 StoreValueWide(rl_dest, rl_result);
798 } else {
799 RegLocation rl_result = GetReturn(rl_dest.fp);
800 StoreValue(rl_dest, rl_result);
801 }
802 }
803}
804
Andreas Gampe2f244e92014-05-08 03:35:25 -0700805template <size_t pointer_size>
806static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
807 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
808 RegLocation rl_src) {
809 ThreadOffset<pointer_size> setter_offset =
810 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
811 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
812 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
813 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
814 rl_obj, rl_src, true);
815}
816
Vladimir Markobe0e5462014-02-26 11:24:15 +0000817void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700819 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000820 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
821 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100822 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
823 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
824 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
825 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000826 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 rl_obj = LoadValue(rl_obj, kCoreReg);
828 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100829 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 } else {
831 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100832 }
833 GenNullCheck(rl_obj.reg, opt_flags);
834 int field_offset = field_info.FieldOffset().Int32Value();
835 if (field_info.IsVolatile()) {
836 // There might have been a store before this volatile one so insert StoreStore barrier.
837 GenMemBarrier(kStoreStore);
838 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800839 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100840 // A load might follow the volatile store so insert a StoreLoad barrier.
841 GenMemBarrier(kStoreLoad);
842 } else {
843 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
844 MarkPossibleNullPointerException(opt_flags);
845 }
846 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
847 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 }
849 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700850 if (Is64BitInstructionSet(cu_->instruction_set)) {
851 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
852 } else {
853 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
854 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 }
856}
857
Andreas Gampe2f244e92014-05-08 03:35:25 -0700858template <size_t pointer_size>
859static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
860 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
861 ThreadOffset<pointer_size> helper = needs_range_check
862 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
863 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
864 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
865 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
866 true);
867}
868
Ian Rogersa9a82542013-10-04 11:17:26 -0700869void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
870 RegLocation rl_src) {
871 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
872 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
873 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700874 if (Is64BitInstructionSet(cu_->instruction_set)) {
875 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
876 } else {
877 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
878 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700879}
880
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700881void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800883 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
885 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
886 *cu_->dex_file,
887 type_idx)) {
888 // Call out to helper which resolves type and verifies access.
889 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700890 if (Is64BitInstructionSet(cu_->instruction_set)) {
891 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
892 type_idx, rl_method.reg, true);
893 } else {
894 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
895 type_idx, rl_method.reg, true);
896 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 RegLocation rl_result = GetReturn(false);
898 StoreValue(rl_dest, rl_result);
899 } else {
900 // We're don't need access checks, load type from dex cache
901 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700902 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700903 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000904 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700905 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
907 type_idx) || SLOW_TYPE_PATH) {
908 // Slow path, at runtime test if type is null and if so initialize
909 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800910 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800911 LIR* cont = NewLIR0(kPseudoTargetLabel);
912
913 // Object to generate the slow path for class resolution.
914 class SlowPath : public LIRSlowPath {
915 public:
916 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
917 const RegLocation& rl_method, const RegLocation& rl_result) :
918 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
919 rl_method_(rl_method), rl_result_(rl_result) {
920 }
921
922 void Compile() {
923 GenerateTargetLabel();
924
Andreas Gampe2f244e92014-05-08 03:35:25 -0700925 if (Is64BitInstructionSet(cu_->instruction_set)) {
926 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
927 rl_method_.reg, true);
928 } else {
929 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
930 rl_method_.reg, true);
931 }
buzbee2700f7e2014-03-07 09:46:20 -0800932 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800933
934 m2l_->OpUnconditionalBranch(cont_);
935 }
936
937 private:
938 const int type_idx_;
939 const RegLocation rl_method_;
940 const RegLocation rl_result_;
941 };
942
943 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800944 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800945
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800947 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 // Fast path, we're done - just store result
949 StoreValue(rl_dest, rl_result);
950 }
951 }
952}
953
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700954void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000956 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
957 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
959 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
960 // slow path, resolve string if not in dex cache
961 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700962 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800963
964 // If the Method* is already in a register, we can save a copy.
965 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800966 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800967 if (rl_method.location == kLocPhysReg) {
968 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800969 DCHECK(!IsTemp(rl_method.reg));
970 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800971 } else {
972 r_method = TargetReg(kArg2);
973 LoadCurrMethodDirect(r_method);
974 }
buzbee695d13a2014-04-19 13:32:20 -0700975 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
976 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800977
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700979 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700980 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
981 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800982
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700983 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800984 // Object to generate the slow path for string resolution.
985 class SlowPath : public LIRSlowPath {
986 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700987 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
988 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
989 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800990 }
991
992 void Compile() {
993 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700994 if (Is64BitInstructionSet(cu_->instruction_set)) {
995 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
996 r_method_, string_idx_, true);
997 } else {
998 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
999 r_method_, string_idx_, true);
1000 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001001 m2l_->OpUnconditionalBranch(cont_);
1002 }
1003
1004 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001005 const RegStorage r_method_;
1006 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001007 };
1008
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001009 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001011
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 GenBarrier();
1013 StoreValue(rl_dest, GetReturn(false));
1014 } else {
1015 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001016 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001018 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1019 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 StoreValue(rl_dest, rl_result);
1021 }
1022}
1023
Andreas Gampe2f244e92014-05-08 03:35:25 -07001024template <size_t pointer_size>
1025static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1026 RegLocation rl_dest) {
1027 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 // alloc will always check for resolution, do we also need to verify
1029 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001030 ThreadOffset<pointer_size> func_offset(-1);
1031 const DexFile* dex_file = cu->dex_file;
1032 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001033 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001034 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001035 bool is_type_initialized;
1036 bool use_direct_type_ptr;
1037 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001038 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001039 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001040 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1041 &direct_type_ptr, &is_finalizable) &&
1042 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001043 // The fast path.
1044 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001045 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001046 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001047 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1048 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001050 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1051 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 }
1053 } else {
1054 // Use the direct pointer.
1055 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001056 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1057 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001058 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001059 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1060 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 }
1062 }
1063 } else {
1064 // The slow path.
1065 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001066 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1067 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 }
1069 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001071 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1072 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001074 RegLocation rl_result = mir_to_lir->GetReturn(false);
1075 mir_to_lir->StoreValue(rl_dest, rl_result);
1076}
1077
1078/*
1079 * Let helper function take care of everything. Will
1080 * call Class::NewInstanceFromCode(type_idx, method);
1081 */
1082void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1083 if (Is64BitInstructionSet(cu_->instruction_set)) {
1084 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1085 } else {
1086 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1087 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088}
1089
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001090void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001092 if (Is64BitInstructionSet(cu_->instruction_set)) {
1093 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1094 } else {
1095 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1096 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097}
1098
1099// For final classes there are no sub-classes to check and so we can answer the instance-of
1100// question with simple comparisons.
1101void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1102 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001103 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001104 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001105
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 RegLocation object = LoadValue(rl_src, kCoreReg);
1107 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001108 RegStorage result_reg = rl_result.reg;
1109 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 result_reg = AllocTypedTemp(false, kCoreReg);
1111 }
1112 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001113 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114
buzbee2700f7e2014-03-07 09:46:20 -08001115 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1116 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117
1118 LoadCurrMethodDirect(check_class);
1119 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001120 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1121 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
buzbee695d13a2014-04-19 13:32:20 -07001123 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1124 check_class);
1125 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001126 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001127 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 }
1129
1130 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001131 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 if (cu_->instruction_set == kThumb2) {
1133 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001134 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001136 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 } else {
1138 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1139 LoadConstant(result_reg, 1); // eq case - load true
1140 }
1141 LIR* target = NewLIR0(kPseudoTargetLabel);
1142 null_branchover->target = target;
1143 if (ne_branchover != NULL) {
1144 ne_branchover->target = target;
1145 }
1146 FreeTemp(object_class);
1147 FreeTemp(check_class);
1148 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001149 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 FreeTemp(result_reg);
1151 }
1152 StoreValue(rl_dest, rl_result);
1153}
1154
1155void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1156 bool type_known_abstract, bool use_declaring_class,
1157 bool can_assume_type_is_in_dex_cache,
1158 uint32_t type_idx, RegLocation rl_dest,
1159 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001160 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001161 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001162
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 FlushAllRegs();
1164 // May generate a call - use explicit registers
1165 LockCallTemps();
1166 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001167 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 if (needs_access_check) {
1169 // Check we have access to type_idx and if not throw IllegalAccessError,
1170 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001171 if (Is64BitInstructionSet(cu_->instruction_set)) {
1172 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1173 type_idx, true);
1174 } else {
1175 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1176 type_idx, true);
1177 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1179 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1180 } else if (use_declaring_class) {
1181 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001182 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001183 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 } else {
1185 // Load dex cache entry into class_reg (kArg2)
1186 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001187 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1188 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001189 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001190 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 if (!can_assume_type_is_in_dex_cache) {
1192 // Need to test presence of type in dex cache at runtime
1193 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1194 // Not resolved
1195 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001196 if (Is64BitInstructionSet(cu_->instruction_set)) {
1197 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1198 } else {
1199 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1200 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001201 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1203 // Rejoin code paths
1204 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1205 hop_branch->target = hop_target;
1206 }
1207 }
1208 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1209 RegLocation rl_result = GetReturn(false);
1210 if (cu_->instruction_set == kMips) {
1211 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001212 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 }
1214 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1215
1216 /* load object->klass_ */
1217 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001218 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1220 LIR* branchover = NULL;
1221 if (type_known_final) {
1222 // rl_result == ref == null == 0.
1223 if (cu_->instruction_set == kThumb2) {
1224 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001225 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001226 LoadConstant(rl_result.reg, 1); // .eq case - load true
1227 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001228 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001230 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001232 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 }
1234 } else {
1235 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001236 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1237 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1238 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001239 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 if (!type_known_abstract) {
1241 /* Uses conditional nullification */
1242 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001243 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1245 }
1246 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1247 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001248 if (it != nullptr) {
1249 OpEndIT(it);
1250 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 FreeTemp(r_tgt);
1252 } else {
1253 if (!type_known_abstract) {
1254 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001255 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1257 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001258 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1259 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1260 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001261 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1262 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1263 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 }
1265 }
1266 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001267 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 /* branch targets here */
1269 LIR* target = NewLIR0(kPseudoTargetLabel);
1270 StoreValue(rl_dest, rl_result);
1271 branch1->target = target;
1272 if (branchover != NULL) {
1273 branchover->target = target;
1274 }
1275}
1276
1277void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1278 bool type_known_final, type_known_abstract, use_declaring_class;
1279 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1280 *cu_->dex_file,
1281 type_idx,
1282 &type_known_final,
1283 &type_known_abstract,
1284 &use_declaring_class);
1285 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1286 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1287
1288 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1289 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1290 } else {
1291 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1292 use_declaring_class, can_assume_type_is_in_dex_cache,
1293 type_idx, rl_dest, rl_src);
1294 }
1295}
1296
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001297void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 bool type_known_final, type_known_abstract, use_declaring_class;
1299 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1300 *cu_->dex_file,
1301 type_idx,
1302 &type_known_final,
1303 &type_known_abstract,
1304 &use_declaring_class);
1305 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1306 // of the exception throw path.
1307 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001308 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 // Verifier type analysis proved this check cast would never cause an exception.
1310 return;
1311 }
1312 FlushAllRegs();
1313 // May generate a call - use explicit registers
1314 LockCallTemps();
1315 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001316 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 if (needs_access_check) {
1318 // Check we have access to type_idx and if not throw IllegalAccessError,
1319 // returns Class* in kRet0
1320 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001321 if (Is64BitInstructionSet(cu_->instruction_set)) {
1322 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1323 type_idx, TargetReg(kArg1), true);
1324 } else {
1325 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1326 type_idx, TargetReg(kArg1), true);
1327 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1329 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001330 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1331 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 } else {
1333 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001334 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1335 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001336 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001337 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1339 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001340 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1341 LIR* cont = NewLIR0(kPseudoTargetLabel);
1342
1343 // Slow path to initialize the type. Executed if the type is NULL.
1344 class SlowPath : public LIRSlowPath {
1345 public:
1346 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001347 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001348 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1349 class_reg_(class_reg) {
1350 }
1351
1352 void Compile() {
1353 GenerateTargetLabel();
1354
1355 // Call out to helper, which will return resolved type in kArg0
1356 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001357 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1358 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1359 m2l_->TargetReg(kArg1), true);
1360 } else {
1361 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1362 m2l_->TargetReg(kArg1), true);
1363 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001364 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1365 m2l_->OpUnconditionalBranch(cont_);
1366 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001367
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001368 public:
1369 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001370 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001371 };
1372
buzbee2700f7e2014-03-07 09:46:20 -08001373 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 }
1375 }
1376 // At this point, class_reg (kArg2) has class
1377 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001378
1379 // Slow path for the case where the classes are not equal. In this case we need
1380 // to call a helper function to do the check.
1381 class SlowPath : public LIRSlowPath {
1382 public:
1383 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1384 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1385 }
1386
1387 void Compile() {
1388 GenerateTargetLabel();
1389
1390 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001391 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1392 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001393 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001394 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1395 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1396 m2l_->TargetReg(kArg1), true);
1397 } else {
1398 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1399 m2l_->TargetReg(kArg1), true);
1400 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001401
1402 m2l_->OpUnconditionalBranch(cont_);
1403 }
1404
1405 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001406 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001407 };
1408
1409 if (type_known_abstract) {
1410 // Easier case, run slow path if target is non-null (slow path will load from target)
1411 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1412 LIR* cont = NewLIR0(kPseudoTargetLabel);
1413 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1414 } else {
1415 // Harder, more common case. We need to generate a forward branch over the load
1416 // if the target is null. If it's non-null we perform the load and branch to the
1417 // slow path if the classes are not equal.
1418
1419 /* Null is OK - continue */
1420 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1421 /* load object->klass_ */
1422 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001423 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001424
1425 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1426 LIR* cont = NewLIR0(kPseudoTargetLabel);
1427
1428 // Add the slow path that will not perform load since this is already done.
1429 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1430
1431 // Set the null check to branch to the continuation.
1432 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 }
1434}
1435
1436void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001437 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 RegLocation rl_result;
1439 if (cu_->instruction_set == kThumb2) {
1440 /*
1441 * NOTE: This is the one place in the code in which we might have
1442 * as many as six live temporary registers. There are 5 in the normal
1443 * set for Arm. Until we have spill capabilities, temporarily add
1444 * lr to the temp set. It is safe to do this locally, but note that
1445 * lr is used explicitly elsewhere in the code generator and cannot
1446 * normally be used as a general temp register.
1447 */
1448 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1449 FreeTemp(TargetReg(kLr)); // and make it available
1450 }
1451 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1452 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1453 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1454 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001455 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1456 RegStorage t_reg = AllocTemp();
1457 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1458 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1459 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 FreeTemp(t_reg);
1461 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001462 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1463 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 }
1465 /*
1466 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1467 * following StoreValueWide might need to allocate a temp register.
1468 * To further work around the lack of a spill capability, explicitly
1469 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1470 * Remove when spill is functional.
1471 */
1472 FreeRegLocTemps(rl_result, rl_src1);
1473 FreeRegLocTemps(rl_result, rl_src2);
1474 StoreValueWide(rl_dest, rl_result);
1475 if (cu_->instruction_set == kThumb2) {
1476 Clobber(TargetReg(kLr));
1477 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1478 }
1479}
1480
1481
Andreas Gampe2f244e92014-05-08 03:35:25 -07001482template <size_t pointer_size>
1483static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1484 RegLocation rl_shift) {
1485 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486
1487 switch (opcode) {
1488 case Instruction::SHL_LONG:
1489 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001490 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 break;
1492 case Instruction::SHR_LONG:
1493 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001494 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 break;
1496 case Instruction::USHR_LONG:
1497 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001498 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 break;
1500 default:
1501 LOG(FATAL) << "Unexpected case";
1502 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001503 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1504 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1505}
1506
1507void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1508 RegLocation rl_src1, RegLocation rl_shift) {
1509 if (Is64BitInstructionSet(cu_->instruction_set)) {
1510 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1511 } else {
1512 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1513 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 RegLocation rl_result = GetReturnWide(false);
1515 StoreValueWide(rl_dest, rl_result);
1516}
1517
1518
1519void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001520 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001521 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 OpKind op = kOpBkpt;
1523 bool is_div_rem = false;
1524 bool check_zero = false;
1525 bool unary = false;
1526 RegLocation rl_result;
1527 bool shift_op = false;
1528 switch (opcode) {
1529 case Instruction::NEG_INT:
1530 op = kOpNeg;
1531 unary = true;
1532 break;
1533 case Instruction::NOT_INT:
1534 op = kOpMvn;
1535 unary = true;
1536 break;
1537 case Instruction::ADD_INT:
1538 case Instruction::ADD_INT_2ADDR:
1539 op = kOpAdd;
1540 break;
1541 case Instruction::SUB_INT:
1542 case Instruction::SUB_INT_2ADDR:
1543 op = kOpSub;
1544 break;
1545 case Instruction::MUL_INT:
1546 case Instruction::MUL_INT_2ADDR:
1547 op = kOpMul;
1548 break;
1549 case Instruction::DIV_INT:
1550 case Instruction::DIV_INT_2ADDR:
1551 check_zero = true;
1552 op = kOpDiv;
1553 is_div_rem = true;
1554 break;
1555 /* NOTE: returns in kArg1 */
1556 case Instruction::REM_INT:
1557 case Instruction::REM_INT_2ADDR:
1558 check_zero = true;
1559 op = kOpRem;
1560 is_div_rem = true;
1561 break;
1562 case Instruction::AND_INT:
1563 case Instruction::AND_INT_2ADDR:
1564 op = kOpAnd;
1565 break;
1566 case Instruction::OR_INT:
1567 case Instruction::OR_INT_2ADDR:
1568 op = kOpOr;
1569 break;
1570 case Instruction::XOR_INT:
1571 case Instruction::XOR_INT_2ADDR:
1572 op = kOpXor;
1573 break;
1574 case Instruction::SHL_INT:
1575 case Instruction::SHL_INT_2ADDR:
1576 shift_op = true;
1577 op = kOpLsl;
1578 break;
1579 case Instruction::SHR_INT:
1580 case Instruction::SHR_INT_2ADDR:
1581 shift_op = true;
1582 op = kOpAsr;
1583 break;
1584 case Instruction::USHR_INT:
1585 case Instruction::USHR_INT_2ADDR:
1586 shift_op = true;
1587 op = kOpLsr;
1588 break;
1589 default:
1590 LOG(FATAL) << "Invalid word arith op: " << opcode;
1591 }
1592 if (!is_div_rem) {
1593 if (unary) {
1594 rl_src1 = LoadValue(rl_src1, kCoreReg);
1595 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001596 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 } else {
1598 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001599 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001600 RegStorage t_reg = AllocTemp();
1601 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 rl_src1 = LoadValue(rl_src1, kCoreReg);
1603 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001604 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 FreeTemp(t_reg);
1606 } else {
1607 rl_src1 = LoadValue(rl_src1, kCoreReg);
1608 rl_src2 = LoadValue(rl_src2, kCoreReg);
1609 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
1612 }
1613 StoreValue(rl_dest, rl_result);
1614 } else {
Dave Allison70202782013-10-22 17:52:19 -07001615 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 -07001616 if (cu_->instruction_set == kMips) {
1617 rl_src1 = LoadValue(rl_src1, kCoreReg);
1618 rl_src2 = LoadValue(rl_src2, kCoreReg);
1619 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001620 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 }
buzbee2700f7e2014-03-07 09:46:20 -08001622 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001623 done = true;
1624 } else if (cu_->instruction_set == kThumb2) {
1625 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1626 // Use ARM SDIV instruction for division. For remainder we also need to
1627 // calculate using a MUL and subtract.
1628 rl_src1 = LoadValue(rl_src1, kCoreReg);
1629 rl_src2 = LoadValue(rl_src2, kCoreReg);
1630 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001631 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001632 }
buzbee2700f7e2014-03-07 09:46:20 -08001633 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001634 done = true;
1635 }
1636 }
1637
1638 // If we haven't already generated the code use the callout function.
1639 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 FlushAllRegs(); /* Send everything to home location */
1641 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001642 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1643 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1644 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1646 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001647 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 }
Dave Allison70202782013-10-22 17:52:19 -07001649 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001650 if (Is64BitInstructionSet(cu_->instruction_set)) {
1651 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1652 } else {
1653 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1654 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 if (op == kOpDiv)
1656 rl_result = GetReturn(false);
1657 else
1658 rl_result = GetReturnAlt();
1659 }
1660 StoreValue(rl_dest, rl_result);
1661 }
1662}
1663
1664/*
1665 * The following are the first-level codegen routines that analyze the format
1666 * of each bytecode then either dispatch special purpose codegen routines
1667 * or produce corresponding Thumb instructions directly.
1668 */
1669
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001671static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 x &= x - 1;
1673 return (x & (x - 1)) == 0;
1674}
1675
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1677// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001678bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001679 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1681 return false;
1682 }
1683 // No divide instruction for Arm, so check for more special cases
1684 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001685 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 }
1687 int k = LowestSetBit(lit);
1688 if (k >= 30) {
1689 // Avoid special cases.
1690 return false;
1691 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 rl_src = LoadValue(rl_src, kCoreReg);
1693 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001694 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001695 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 if (lit == 2) {
1697 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001698 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1699 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1700 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001702 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001704 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1705 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 }
1707 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001708 RegStorage t_reg1 = AllocTemp();
1709 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001711 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1712 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001714 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001716 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001720 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 }
1722 }
1723 StoreValue(rl_dest, rl_result);
1724 return true;
1725}
1726
1727// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1728// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001729bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001730 if (lit < 0) {
1731 return false;
1732 }
1733 if (lit == 0) {
1734 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1735 LoadConstant(rl_result.reg, 0);
1736 StoreValue(rl_dest, rl_result);
1737 return true;
1738 }
1739 if (lit == 1) {
1740 rl_src = LoadValue(rl_src, kCoreReg);
1741 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1742 OpRegCopy(rl_result.reg, rl_src.reg);
1743 StoreValue(rl_dest, rl_result);
1744 return true;
1745 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001746 // There is RegRegRegShift on Arm, so check for more special cases
1747 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001748 return EasyMultiply(rl_src, rl_dest, lit);
1749 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 // Can we simplify this multiplication?
1751 bool power_of_two = false;
1752 bool pop_count_le2 = false;
1753 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001754 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 power_of_two = true;
1756 } else if (IsPopCountLE2(lit)) {
1757 pop_count_le2 = true;
1758 } else if (IsPowerOfTwo(lit + 1)) {
1759 power_of_two_minus_one = true;
1760 } else {
1761 return false;
1762 }
1763 rl_src = LoadValue(rl_src, kCoreReg);
1764 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1765 if (power_of_two) {
1766 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001767 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 } else if (pop_count_le2) {
1769 // Shift and add and shift.
1770 int first_bit = LowestSetBit(lit);
1771 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1772 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1773 } else {
1774 // Reverse subtract: (src << (shift + 1)) - src.
1775 DCHECK(power_of_two_minus_one);
1776 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001777 RegStorage t_reg = AllocTemp();
1778 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1779 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001780 }
1781 StoreValue(rl_dest, rl_result);
1782 return true;
1783}
1784
1785void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001786 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 RegLocation rl_result;
1788 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1789 int shift_op = false;
1790 bool is_div = false;
1791
1792 switch (opcode) {
1793 case Instruction::RSUB_INT_LIT8:
1794 case Instruction::RSUB_INT: {
1795 rl_src = LoadValue(rl_src, kCoreReg);
1796 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1797 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001798 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001799 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001800 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1801 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 }
1803 StoreValue(rl_dest, rl_result);
1804 return;
1805 }
1806
1807 case Instruction::SUB_INT:
1808 case Instruction::SUB_INT_2ADDR:
1809 lit = -lit;
1810 // Intended fallthrough
1811 case Instruction::ADD_INT:
1812 case Instruction::ADD_INT_2ADDR:
1813 case Instruction::ADD_INT_LIT8:
1814 case Instruction::ADD_INT_LIT16:
1815 op = kOpAdd;
1816 break;
1817 case Instruction::MUL_INT:
1818 case Instruction::MUL_INT_2ADDR:
1819 case Instruction::MUL_INT_LIT8:
1820 case Instruction::MUL_INT_LIT16: {
1821 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1822 return;
1823 }
1824 op = kOpMul;
1825 break;
1826 }
1827 case Instruction::AND_INT:
1828 case Instruction::AND_INT_2ADDR:
1829 case Instruction::AND_INT_LIT8:
1830 case Instruction::AND_INT_LIT16:
1831 op = kOpAnd;
1832 break;
1833 case Instruction::OR_INT:
1834 case Instruction::OR_INT_2ADDR:
1835 case Instruction::OR_INT_LIT8:
1836 case Instruction::OR_INT_LIT16:
1837 op = kOpOr;
1838 break;
1839 case Instruction::XOR_INT:
1840 case Instruction::XOR_INT_2ADDR:
1841 case Instruction::XOR_INT_LIT8:
1842 case Instruction::XOR_INT_LIT16:
1843 op = kOpXor;
1844 break;
1845 case Instruction::SHL_INT_LIT8:
1846 case Instruction::SHL_INT:
1847 case Instruction::SHL_INT_2ADDR:
1848 lit &= 31;
1849 shift_op = true;
1850 op = kOpLsl;
1851 break;
1852 case Instruction::SHR_INT_LIT8:
1853 case Instruction::SHR_INT:
1854 case Instruction::SHR_INT_2ADDR:
1855 lit &= 31;
1856 shift_op = true;
1857 op = kOpAsr;
1858 break;
1859 case Instruction::USHR_INT_LIT8:
1860 case Instruction::USHR_INT:
1861 case Instruction::USHR_INT_2ADDR:
1862 lit &= 31;
1863 shift_op = true;
1864 op = kOpLsr;
1865 break;
1866
1867 case Instruction::DIV_INT:
1868 case Instruction::DIV_INT_2ADDR:
1869 case Instruction::DIV_INT_LIT8:
1870 case Instruction::DIV_INT_LIT16:
1871 case Instruction::REM_INT:
1872 case Instruction::REM_INT_2ADDR:
1873 case Instruction::REM_INT_LIT8:
1874 case Instruction::REM_INT_LIT16: {
1875 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001876 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001877 return;
1878 }
buzbee11b63d12013-08-27 07:34:17 -07001879 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001881 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001882 (opcode == Instruction::DIV_INT_LIT16)) {
1883 is_div = true;
1884 } else {
1885 is_div = false;
1886 }
buzbee11b63d12013-08-27 07:34:17 -07001887 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1888 return;
1889 }
Dave Allison70202782013-10-22 17:52:19 -07001890
1891 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001892 if (cu_->instruction_set == kMips) {
1893 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001894 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001895 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001896 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001897 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1898 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001899 } else if (cu_->instruction_set == kThumb2) {
1900 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1901 // Use ARM SDIV instruction for division. For remainder we also need to
1902 // calculate using a MUL and subtract.
1903 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001904 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001905 done = true;
1906 }
1907 }
1908
1909 if (!done) {
1910 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001911 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1912 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001913 if (Is64BitInstructionSet(cu_->instruction_set)) {
1914 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1915 false);
1916 } else {
1917 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1918 false);
1919 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 if (is_div)
1921 rl_result = GetReturn(false);
1922 else
1923 rl_result = GetReturnAlt();
1924 }
1925 StoreValue(rl_dest, rl_result);
1926 return;
1927 }
1928 default:
1929 LOG(FATAL) << "Unexpected opcode " << opcode;
1930 }
1931 rl_src = LoadValue(rl_src, kCoreReg);
1932 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001933 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001935 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001937 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 }
1939 StoreValue(rl_dest, rl_result);
1940}
1941
Andreas Gampe2f244e92014-05-08 03:35:25 -07001942template <size_t pointer_size>
1943static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1944 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 RegLocation rl_result;
1946 OpKind first_op = kOpBkpt;
1947 OpKind second_op = kOpBkpt;
1948 bool call_out = false;
1949 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001950 ThreadOffset<pointer_size> func_offset(-1);
1951 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952
1953 switch (opcode) {
1954 case Instruction::NOT_LONG:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001955 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1956 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001958 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001959 RegStorage t_reg = mir_to_lir->AllocTemp();
1960 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1961 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1962 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1963 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001965 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1966 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001968 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 return;
1970 case Instruction::ADD_LONG:
1971 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001972 if (cu->instruction_set != kThumb2) {
1973 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 return;
1975 }
1976 first_op = kOpAdd;
1977 second_op = kOpAdc;
1978 break;
1979 case Instruction::SUB_LONG:
1980 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001981 if (cu->instruction_set != kThumb2) {
1982 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 return;
1984 }
1985 first_op = kOpSub;
1986 second_op = kOpSbc;
1987 break;
1988 case Instruction::MUL_LONG:
1989 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001990 if (cu->instruction_set != kMips) {
1991 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 return;
1993 } else {
1994 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001995 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
1996 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 }
1998 break;
1999 case Instruction::DIV_LONG:
2000 case Instruction::DIV_LONG_2ADDR:
2001 call_out = true;
2002 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002003 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2004 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 break;
2006 case Instruction::REM_LONG:
2007 case Instruction::REM_LONG_2ADDR:
2008 call_out = true;
2009 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002012 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2013 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 break;
2015 case Instruction::AND_LONG_2ADDR:
2016 case Instruction::AND_LONG:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002017 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
2018 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 }
2020 first_op = kOpAnd;
2021 second_op = kOpAnd;
2022 break;
2023 case Instruction::OR_LONG:
2024 case Instruction::OR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002025 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
2026 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 return;
2028 }
2029 first_op = kOpOr;
2030 second_op = kOpOr;
2031 break;
2032 case Instruction::XOR_LONG:
2033 case Instruction::XOR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002034 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64) {
2035 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 return;
2037 }
2038 first_op = kOpXor;
2039 second_op = kOpXor;
2040 break;
2041 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002042 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 return;
2044 }
2045 default:
2046 LOG(FATAL) << "Invalid long arith op";
2047 }
2048 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002049 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002050 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002051 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002053 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2054 mir_to_lir->TargetReg(kArg1));
2055 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2056 mir_to_lir->TargetReg(kArg3));
2057 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2058 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2059 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2060 mir_to_lir->TargetReg(kArg3)));
2061 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002063 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002065 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066 }
2067 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002068 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
2069 rl_result = mir_to_lir->GetReturnWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 rl_result = mir_to_lir->GetReturnWideAlt();
2072 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 }
2074}
2075
Andreas Gampe2f244e92014-05-08 03:35:25 -07002076void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2077 RegLocation rl_src1, RegLocation rl_src2) {
2078 if (Is64BitInstructionSet(cu_->instruction_set)) {
2079 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2080 } else {
2081 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2082 }
2083}
2084
Mark Mendelle87f9b52014-04-30 14:13:18 -04002085void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2086 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2087 LoadConstantNoClobber(rl_result.reg, value);
2088 StoreValue(rl_dest, rl_result);
2089 if (value == 0) {
2090 Workaround7250540(rl_dest, rl_result.reg);
2091 }
2092}
2093
Andreas Gampe2f244e92014-05-08 03:35:25 -07002094template <size_t pointer_size>
2095void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002096 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 /*
2098 * Don't optimize the register usage since it calls out to support
2099 * functions
2100 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002101 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2102
Brian Carlstrom7940e442013-07-12 13:46:57 -07002103 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2105 if (rl_dest.wide) {
2106 RegLocation rl_result;
2107 rl_result = GetReturnWide(rl_dest.fp);
2108 StoreValueWide(rl_dest, rl_result);
2109 } else {
2110 RegLocation rl_result;
2111 rl_result = GetReturn(rl_dest.fp);
2112 StoreValue(rl_dest, rl_result);
2113 }
2114}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002115template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2116 RegLocation rl_dest, RegLocation rl_src);
2117template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2118 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002119
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002120class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2121 public:
2122 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2123 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2124 }
2125
2126 void Compile() OVERRIDE {
2127 m2l_->ResetRegPool();
2128 m2l_->ResetDefTracking();
2129 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002130 if (Is64BitInstructionSet(cu_->instruction_set)) {
2131 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2132 } else {
2133 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2134 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002135 if (cont_ != nullptr) {
2136 m2l_->OpUnconditionalBranch(cont_);
2137 }
2138 }
2139};
2140
Brian Carlstrom7940e442013-07-12 13:46:57 -07002141/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002142void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002143 if (Runtime::Current()->ExplicitSuspendChecks()) {
2144 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2145 return;
2146 }
2147 FlushAllRegs();
2148 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002149 LIR* cont = NewLIR0(kPseudoTargetLabel);
2150 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002151 } else {
2152 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2153 return;
2154 }
2155 FlushAllRegs(); // TODO: needed?
2156 LIR* inst = CheckSuspendUsingLoad();
2157 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002158 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002159}
2160
2161/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002162void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002163 if (Runtime::Current()->ExplicitSuspendChecks()) {
2164 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2165 OpUnconditionalBranch(target);
2166 return;
2167 }
2168 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002169 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002170 LIR* branch = OpUnconditionalBranch(nullptr);
2171 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002172 } else {
2173 // For the implicit suspend check, just perform the trigger
2174 // load and branch to the target.
2175 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2176 OpUnconditionalBranch(target);
2177 return;
2178 }
2179 FlushAllRegs();
2180 LIR* inst = CheckSuspendUsingLoad();
2181 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002182 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002183 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002184}
2185
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002186/* Call out to helper assembly routine that will null check obj and then lock it. */
2187void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2188 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002189 if (Is64BitInstructionSet(cu_->instruction_set)) {
2190 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2191 } else {
2192 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2193 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002194}
2195
2196/* Call out to helper assembly routine that will null check obj and then unlock it. */
2197void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2198 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002199 if (Is64BitInstructionSet(cu_->instruction_set)) {
2200 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2201 } else {
2202 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2203 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002204}
2205
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002206/* Generic code for generating a wide constant into a VR. */
2207void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2208 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002209 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002210 StoreValueWide(rl_dest, rl_result);
2211}
2212
Brian Carlstrom7940e442013-07-12 13:46:57 -07002213} // namespace art