blob: 03edf2ac397cb95d058bc4ed5e7b414b507c61a0 [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
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100565 RegLocation rl_method = LoadCurrMethod();
566 r_base = AllocTempWord();
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);
Ian Rogers03dbc042014-06-02 14:24:56 -0700606 // Ensure load of status and load of value don't re-order.
607 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 FreeTemp(r_method);
610 }
611 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100612 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100614 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100616 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000618 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800619 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100621 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800622 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100624 } else {
625 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 }
627 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800628 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800630 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 } else {
632 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700633 if (Is64BitInstructionSet(cu_->instruction_set)) {
634 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
635 } else {
636 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
637 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 }
639}
640
Andreas Gampe2f244e92014-05-08 03:35:25 -0700641template <size_t pointer_size>
642static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
643 const MirSFieldLoweringInfo* field_info) {
644 ThreadOffset<pointer_size> getter_offset =
645 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
646 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
647 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
648 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
649}
650
Vladimir Markobe0e5462014-02-26 11:24:15 +0000651void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700652 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000653 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
654 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100655 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
656 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
657 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000658 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800659 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000660 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 // Fast path, static storage base is this method's class
662 RegLocation rl_method = LoadCurrMethod();
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100663 r_base = AllocTempWord();
buzbee695d13a2014-04-19 13:32:20 -0700664 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 } else {
666 // Medium path, static storage base in a different class which requires checks that the other
667 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000668 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 // May do runtime call so everything to home locations.
670 FlushAllRegs();
671 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800672 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 LockTemp(r_method);
674 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800675 r_base = TargetReg(kArg0);
676 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700677 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000678 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
679 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800680 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000681 if (!field_info.IsInitialized() &&
682 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800683 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800684
685 // The slow path is invoked if the r_base is NULL or the class pointed
686 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800687 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800688 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800689 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800690 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800691 mirror::Class::StatusOffset().Int32Value(),
692 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800693 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800694
buzbee2700f7e2014-03-07 09:46:20 -0800695 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000696 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800697
698 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700699 // Ensure load of status and load of value don't re-order.
700 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 FreeTemp(r_method);
703 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800704 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100705 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
706 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800707
Vladimir Marko674744e2014-04-24 15:18:26 +0100708 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800709 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100710 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800711 // Without context sensitive analysis, we must issue the most conservative barriers.
712 // In this case, either a load or store may follow so we issue both barriers.
713 GenMemBarrier(kLoadLoad);
714 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100715 } else {
716 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800717 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100718 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800719
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 if (is_long_or_double) {
721 StoreValueWide(rl_dest, rl_result);
722 } else {
723 StoreValue(rl_dest, rl_result);
724 }
725 } else {
726 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700727 if (Is64BitInstructionSet(cu_->instruction_set)) {
728 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
729 } else {
730 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
731 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 if (is_long_or_double) {
733 RegLocation rl_result = GetReturnWide(rl_dest.fp);
734 StoreValueWide(rl_dest, rl_result);
735 } else {
736 RegLocation rl_result = GetReturn(rl_dest.fp);
737 StoreValue(rl_dest, rl_result);
738 }
739 }
740}
741
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800742// Generate code for all slow paths.
743void Mir2Lir::HandleSlowPaths() {
744 int n = slow_paths_.Size();
745 for (int i = 0; i < n; ++i) {
746 LIRSlowPath* slowpath = slow_paths_.Get(i);
747 slowpath->Compile();
748 }
749 slow_paths_.Reset();
750}
751
Andreas Gampe2f244e92014-05-08 03:35:25 -0700752template <size_t pointer_size>
753static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
754 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
755 ThreadOffset<pointer_size> getter_offset =
756 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
757 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
758 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
759 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
760 true);
761}
762
Vladimir Markobe0e5462014-02-26 11:24:15 +0000763void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700765 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000766 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
767 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100768 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
769 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
770 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
771 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000772 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 rl_obj = LoadValue(rl_obj, kCoreReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100774 GenNullCheck(rl_obj.reg, opt_flags);
775 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
776 int field_offset = field_info.FieldOffset().Int32Value();
777 if (field_info.IsVolatile()) {
778 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
779 MarkPossibleNullPointerException(opt_flags);
780 // Without context sensitive analysis, we must issue the most conservative barriers.
781 // In this case, either a load or store may follow so we issue both barriers.
782 GenMemBarrier(kLoadLoad);
783 GenMemBarrier(kLoadStore);
784 } else {
785 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
786 MarkPossibleNullPointerException(opt_flags);
787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 StoreValueWide(rl_dest, rl_result);
790 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 StoreValue(rl_dest, rl_result);
792 }
793 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700794 if (Is64BitInstructionSet(cu_->instruction_set)) {
795 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
796 } else {
797 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
798 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 if (is_long_or_double) {
800 RegLocation rl_result = GetReturnWide(rl_dest.fp);
801 StoreValueWide(rl_dest, rl_result);
802 } else {
803 RegLocation rl_result = GetReturn(rl_dest.fp);
804 StoreValue(rl_dest, rl_result);
805 }
806 }
807}
808
Andreas Gampe2f244e92014-05-08 03:35:25 -0700809template <size_t pointer_size>
810static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
811 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
812 RegLocation rl_src) {
813 ThreadOffset<pointer_size> setter_offset =
814 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
815 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
816 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
817 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
818 rl_obj, rl_src, true);
819}
820
Vladimir Markobe0e5462014-02-26 11:24:15 +0000821void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700823 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000824 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
825 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100826 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
827 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
828 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
829 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000830 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 rl_obj = LoadValue(rl_obj, kCoreReg);
832 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100833 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 } else {
835 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100836 }
837 GenNullCheck(rl_obj.reg, opt_flags);
838 int field_offset = field_info.FieldOffset().Int32Value();
839 if (field_info.IsVolatile()) {
840 // There might have been a store before this volatile one so insert StoreStore barrier.
841 GenMemBarrier(kStoreStore);
842 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800843 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100844 // A load might follow the volatile store so insert a StoreLoad barrier.
845 GenMemBarrier(kStoreLoad);
846 } else {
847 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
848 MarkPossibleNullPointerException(opt_flags);
849 }
850 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
851 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 }
853 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700854 if (Is64BitInstructionSet(cu_->instruction_set)) {
855 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
856 } else {
857 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
858 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 }
860}
861
Andreas Gampe2f244e92014-05-08 03:35:25 -0700862template <size_t pointer_size>
863static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
864 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
865 ThreadOffset<pointer_size> helper = needs_range_check
866 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
867 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
868 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
869 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
870 true);
871}
872
Ian Rogersa9a82542013-10-04 11:17:26 -0700873void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
874 RegLocation rl_src) {
875 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
876 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
877 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700878 if (Is64BitInstructionSet(cu_->instruction_set)) {
879 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
880 } else {
881 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
882 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700883}
884
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700885void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800887 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
889 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
890 *cu_->dex_file,
891 type_idx)) {
892 // Call out to helper which resolves type and verifies access.
893 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700894 if (Is64BitInstructionSet(cu_->instruction_set)) {
895 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
896 type_idx, rl_method.reg, true);
897 } else {
898 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
899 type_idx, rl_method.reg, true);
900 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 RegLocation rl_result = GetReturn(false);
902 StoreValue(rl_dest, rl_result);
903 } else {
904 // We're don't need access checks, load type from dex cache
905 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700906 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700907 Load32Disp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000908 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -0700909 Load32Disp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
911 type_idx) || SLOW_TYPE_PATH) {
912 // Slow path, at runtime test if type is null and if so initialize
913 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800914 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800915 LIR* cont = NewLIR0(kPseudoTargetLabel);
916
917 // Object to generate the slow path for class resolution.
918 class SlowPath : public LIRSlowPath {
919 public:
920 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
921 const RegLocation& rl_method, const RegLocation& rl_result) :
922 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
923 rl_method_(rl_method), rl_result_(rl_result) {
924 }
925
926 void Compile() {
927 GenerateTargetLabel();
928
Andreas Gampe2f244e92014-05-08 03:35:25 -0700929 if (Is64BitInstructionSet(cu_->instruction_set)) {
930 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
931 rl_method_.reg, true);
932 } else {
933 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
934 rl_method_.reg, true);
935 }
buzbee2700f7e2014-03-07 09:46:20 -0800936 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800937
938 m2l_->OpUnconditionalBranch(cont_);
939 }
940
941 private:
942 const int type_idx_;
943 const RegLocation rl_method_;
944 const RegLocation rl_result_;
945 };
946
947 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800948 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800949
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800951 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 // Fast path, we're done - just store result
953 StoreValue(rl_dest, rl_result);
954 }
955 }
956}
957
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700958void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000960 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
961 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
963 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
964 // slow path, resolve string if not in dex cache
965 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700966 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800967
968 // If the Method* is already in a register, we can save a copy.
969 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800970 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800971 if (rl_method.location == kLocPhysReg) {
972 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800973 DCHECK(!IsTemp(rl_method.reg));
974 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800975 } else {
976 r_method = TargetReg(kArg2);
977 LoadCurrMethodDirect(r_method);
978 }
buzbee695d13a2014-04-19 13:32:20 -0700979 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
980 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800981
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 // Might call out to helper, which will return resolved string in kRet0
buzbee695d13a2014-04-19 13:32:20 -0700983 Load32Disp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700984 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
985 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800986
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700987 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800988 // Object to generate the slow path for string resolution.
989 class SlowPath : public LIRSlowPath {
990 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700991 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
992 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
993 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800994 }
995
996 void Compile() {
997 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700998 if (Is64BitInstructionSet(cu_->instruction_set)) {
999 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1000 r_method_, string_idx_, true);
1001 } else {
1002 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1003 r_method_, string_idx_, true);
1004 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001005 m2l_->OpUnconditionalBranch(cont_);
1006 }
1007
1008 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001009 const RegStorage r_method_;
1010 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001011 };
1012
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001013 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001015
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 GenBarrier();
1017 StoreValue(rl_dest, GetReturn(false));
1018 } else {
1019 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001020 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001022 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1023 Load32Disp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 StoreValue(rl_dest, rl_result);
1025 }
1026}
1027
Andreas Gampe2f244e92014-05-08 03:35:25 -07001028template <size_t pointer_size>
1029static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1030 RegLocation rl_dest) {
1031 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 // alloc will always check for resolution, do we also need to verify
1033 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001034 ThreadOffset<pointer_size> func_offset(-1);
1035 const DexFile* dex_file = cu->dex_file;
1036 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001037 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001038 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001039 bool is_type_initialized;
1040 bool use_direct_type_ptr;
1041 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001042 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001043 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001044 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1045 &direct_type_ptr, &is_finalizable) &&
1046 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001047 // The fast path.
1048 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001049 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001050 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001051 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1052 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001054 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1055 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 }
1057 } else {
1058 // Use the direct pointer.
1059 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001060 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1061 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001063 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1064 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 }
1066 }
1067 } else {
1068 // The slow path.
1069 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001070 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1071 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 }
1073 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001075 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1076 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001078 RegLocation rl_result = mir_to_lir->GetReturn(false);
1079 mir_to_lir->StoreValue(rl_dest, rl_result);
1080}
1081
1082/*
1083 * Let helper function take care of everything. Will
1084 * call Class::NewInstanceFromCode(type_idx, method);
1085 */
1086void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1087 if (Is64BitInstructionSet(cu_->instruction_set)) {
1088 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1089 } else {
1090 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1091 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092}
1093
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001096 if (Is64BitInstructionSet(cu_->instruction_set)) {
1097 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1098 } else {
1099 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1100 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101}
1102
1103// For final classes there are no sub-classes to check and so we can answer the instance-of
1104// question with simple comparisons.
1105void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1106 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001107 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001108 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001109
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 RegLocation object = LoadValue(rl_src, kCoreReg);
1111 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001112 RegStorage result_reg = rl_result.reg;
1113 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 result_reg = AllocTypedTemp(false, kCoreReg);
1115 }
1116 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001117 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118
buzbee2700f7e2014-03-07 09:46:20 -08001119 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1120 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121
1122 LoadCurrMethodDirect(check_class);
1123 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001124 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1125 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 } else {
buzbee695d13a2014-04-19 13:32:20 -07001127 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1128 check_class);
1129 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001130 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001131 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 }
1133
1134 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001135 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 if (cu_->instruction_set == kThumb2) {
1137 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001138 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001140 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 } else {
1142 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1143 LoadConstant(result_reg, 1); // eq case - load true
1144 }
1145 LIR* target = NewLIR0(kPseudoTargetLabel);
1146 null_branchover->target = target;
1147 if (ne_branchover != NULL) {
1148 ne_branchover->target = target;
1149 }
1150 FreeTemp(object_class);
1151 FreeTemp(check_class);
1152 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001153 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 FreeTemp(result_reg);
1155 }
1156 StoreValue(rl_dest, rl_result);
1157}
1158
1159void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1160 bool type_known_abstract, bool use_declaring_class,
1161 bool can_assume_type_is_in_dex_cache,
1162 uint32_t type_idx, RegLocation rl_dest,
1163 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001164 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001165 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001166
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 FlushAllRegs();
1168 // May generate a call - use explicit registers
1169 LockCallTemps();
1170 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001171 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (needs_access_check) {
1173 // Check we have access to type_idx and if not throw IllegalAccessError,
1174 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001175 if (Is64BitInstructionSet(cu_->instruction_set)) {
1176 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1177 type_idx, true);
1178 } else {
1179 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1180 type_idx, true);
1181 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1183 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1184 } else if (use_declaring_class) {
1185 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001186 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001187 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 } else {
1189 // Load dex cache entry into class_reg (kArg2)
1190 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001191 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1192 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001193 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001194 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 if (!can_assume_type_is_in_dex_cache) {
1196 // Need to test presence of type in dex cache at runtime
1197 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1198 // Not resolved
1199 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001200 if (Is64BitInstructionSet(cu_->instruction_set)) {
1201 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1202 } else {
1203 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1204 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001205 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1207 // Rejoin code paths
1208 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1209 hop_branch->target = hop_target;
1210 }
1211 }
1212 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1213 RegLocation rl_result = GetReturn(false);
1214 if (cu_->instruction_set == kMips) {
1215 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001216 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 }
1218 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1219
1220 /* load object->klass_ */
1221 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001222 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1224 LIR* branchover = NULL;
1225 if (type_known_final) {
1226 // rl_result == ref == null == 0.
1227 if (cu_->instruction_set == kThumb2) {
1228 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001229 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001230 LoadConstant(rl_result.reg, 1); // .eq case - load true
1231 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001232 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001234 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001236 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 }
1238 } else {
1239 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001240 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1241 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1242 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001243 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 if (!type_known_abstract) {
1245 /* Uses conditional nullification */
1246 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001247 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1249 }
1250 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1251 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001252 if (it != nullptr) {
1253 OpEndIT(it);
1254 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 FreeTemp(r_tgt);
1256 } else {
1257 if (!type_known_abstract) {
1258 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001259 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1261 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001262 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1263 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1264 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001265 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1266 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1267 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 }
1269 }
1270 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001271 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 /* branch targets here */
1273 LIR* target = NewLIR0(kPseudoTargetLabel);
1274 StoreValue(rl_dest, rl_result);
1275 branch1->target = target;
1276 if (branchover != NULL) {
1277 branchover->target = target;
1278 }
1279}
1280
1281void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1282 bool type_known_final, type_known_abstract, use_declaring_class;
1283 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1284 *cu_->dex_file,
1285 type_idx,
1286 &type_known_final,
1287 &type_known_abstract,
1288 &use_declaring_class);
1289 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1290 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1291
1292 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1293 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1294 } else {
1295 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1296 use_declaring_class, can_assume_type_is_in_dex_cache,
1297 type_idx, rl_dest, rl_src);
1298 }
1299}
1300
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001301void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 bool type_known_final, type_known_abstract, use_declaring_class;
1303 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1304 *cu_->dex_file,
1305 type_idx,
1306 &type_known_final,
1307 &type_known_abstract,
1308 &use_declaring_class);
1309 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1310 // of the exception throw path.
1311 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001312 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 // Verifier type analysis proved this check cast would never cause an exception.
1314 return;
1315 }
1316 FlushAllRegs();
1317 // May generate a call - use explicit registers
1318 LockCallTemps();
1319 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001320 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 if (needs_access_check) {
1322 // Check we have access to type_idx and if not throw IllegalAccessError,
1323 // returns Class* in kRet0
1324 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001325 if (Is64BitInstructionSet(cu_->instruction_set)) {
1326 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1327 type_idx, TargetReg(kArg1), true);
1328 } else {
1329 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1330 type_idx, TargetReg(kArg1), true);
1331 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1333 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001334 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1335 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 } else {
1337 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001338 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1339 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001340 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001341 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1343 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001344 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1345 LIR* cont = NewLIR0(kPseudoTargetLabel);
1346
1347 // Slow path to initialize the type. Executed if the type is NULL.
1348 class SlowPath : public LIRSlowPath {
1349 public:
1350 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001351 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001352 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1353 class_reg_(class_reg) {
1354 }
1355
1356 void Compile() {
1357 GenerateTargetLabel();
1358
1359 // Call out to helper, which will return resolved type in kArg0
1360 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001361 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1362 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1363 m2l_->TargetReg(kArg1), true);
1364 } else {
1365 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1366 m2l_->TargetReg(kArg1), true);
1367 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001368 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1369 m2l_->OpUnconditionalBranch(cont_);
1370 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001371
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001372 public:
1373 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001374 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 };
1376
buzbee2700f7e2014-03-07 09:46:20 -08001377 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 }
1379 }
1380 // At this point, class_reg (kArg2) has class
1381 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001382
1383 // Slow path for the case where the classes are not equal. In this case we need
1384 // to call a helper function to do the check.
1385 class SlowPath : public LIRSlowPath {
1386 public:
1387 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1388 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1389 }
1390
1391 void Compile() {
1392 GenerateTargetLabel();
1393
1394 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001395 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1396 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001397 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001398 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1399 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1400 m2l_->TargetReg(kArg1), true);
1401 } else {
1402 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1403 m2l_->TargetReg(kArg1), true);
1404 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405
1406 m2l_->OpUnconditionalBranch(cont_);
1407 }
1408
1409 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001410 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001411 };
1412
1413 if (type_known_abstract) {
1414 // Easier case, run slow path if target is non-null (slow path will load from target)
1415 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1416 LIR* cont = NewLIR0(kPseudoTargetLabel);
1417 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1418 } else {
1419 // Harder, more common case. We need to generate a forward branch over the load
1420 // if the target is null. If it's non-null we perform the load and branch to the
1421 // slow path if the classes are not equal.
1422
1423 /* Null is OK - continue */
1424 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1425 /* load object->klass_ */
1426 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001427 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428
1429 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1430 LIR* cont = NewLIR0(kPseudoTargetLabel);
1431
1432 // Add the slow path that will not perform load since this is already done.
1433 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1434
1435 // Set the null check to branch to the continuation.
1436 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001437 }
1438}
1439
1440void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001441 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 RegLocation rl_result;
1443 if (cu_->instruction_set == kThumb2) {
1444 /*
1445 * NOTE: This is the one place in the code in which we might have
1446 * as many as six live temporary registers. There are 5 in the normal
1447 * set for Arm. Until we have spill capabilities, temporarily add
1448 * lr to the temp set. It is safe to do this locally, but note that
1449 * lr is used explicitly elsewhere in the code generator and cannot
1450 * normally be used as a general temp register.
1451 */
1452 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1453 FreeTemp(TargetReg(kLr)); // and make it available
1454 }
1455 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1456 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1457 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1458 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001459 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1460 RegStorage t_reg = AllocTemp();
1461 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1462 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1463 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 FreeTemp(t_reg);
1465 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001466 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1467 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 }
1469 /*
1470 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1471 * following StoreValueWide might need to allocate a temp register.
1472 * To further work around the lack of a spill capability, explicitly
1473 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1474 * Remove when spill is functional.
1475 */
1476 FreeRegLocTemps(rl_result, rl_src1);
1477 FreeRegLocTemps(rl_result, rl_src2);
1478 StoreValueWide(rl_dest, rl_result);
1479 if (cu_->instruction_set == kThumb2) {
1480 Clobber(TargetReg(kLr));
1481 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1482 }
1483}
1484
1485
Andreas Gampe2f244e92014-05-08 03:35:25 -07001486template <size_t pointer_size>
1487static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1488 RegLocation rl_shift) {
1489 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490
1491 switch (opcode) {
1492 case Instruction::SHL_LONG:
1493 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001494 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 break;
1496 case Instruction::SHR_LONG:
1497 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001498 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 break;
1500 case Instruction::USHR_LONG:
1501 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001502 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 break;
1504 default:
1505 LOG(FATAL) << "Unexpected case";
1506 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001507 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1508 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1509}
1510
1511void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1512 RegLocation rl_src1, RegLocation rl_shift) {
1513 if (Is64BitInstructionSet(cu_->instruction_set)) {
1514 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1515 } else {
1516 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1517 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 RegLocation rl_result = GetReturnWide(false);
1519 StoreValueWide(rl_dest, rl_result);
1520}
1521
1522
1523void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001524 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001525 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 OpKind op = kOpBkpt;
1527 bool is_div_rem = false;
1528 bool check_zero = false;
1529 bool unary = false;
1530 RegLocation rl_result;
1531 bool shift_op = false;
1532 switch (opcode) {
1533 case Instruction::NEG_INT:
1534 op = kOpNeg;
1535 unary = true;
1536 break;
1537 case Instruction::NOT_INT:
1538 op = kOpMvn;
1539 unary = true;
1540 break;
1541 case Instruction::ADD_INT:
1542 case Instruction::ADD_INT_2ADDR:
1543 op = kOpAdd;
1544 break;
1545 case Instruction::SUB_INT:
1546 case Instruction::SUB_INT_2ADDR:
1547 op = kOpSub;
1548 break;
1549 case Instruction::MUL_INT:
1550 case Instruction::MUL_INT_2ADDR:
1551 op = kOpMul;
1552 break;
1553 case Instruction::DIV_INT:
1554 case Instruction::DIV_INT_2ADDR:
1555 check_zero = true;
1556 op = kOpDiv;
1557 is_div_rem = true;
1558 break;
1559 /* NOTE: returns in kArg1 */
1560 case Instruction::REM_INT:
1561 case Instruction::REM_INT_2ADDR:
1562 check_zero = true;
1563 op = kOpRem;
1564 is_div_rem = true;
1565 break;
1566 case Instruction::AND_INT:
1567 case Instruction::AND_INT_2ADDR:
1568 op = kOpAnd;
1569 break;
1570 case Instruction::OR_INT:
1571 case Instruction::OR_INT_2ADDR:
1572 op = kOpOr;
1573 break;
1574 case Instruction::XOR_INT:
1575 case Instruction::XOR_INT_2ADDR:
1576 op = kOpXor;
1577 break;
1578 case Instruction::SHL_INT:
1579 case Instruction::SHL_INT_2ADDR:
1580 shift_op = true;
1581 op = kOpLsl;
1582 break;
1583 case Instruction::SHR_INT:
1584 case Instruction::SHR_INT_2ADDR:
1585 shift_op = true;
1586 op = kOpAsr;
1587 break;
1588 case Instruction::USHR_INT:
1589 case Instruction::USHR_INT_2ADDR:
1590 shift_op = true;
1591 op = kOpLsr;
1592 break;
1593 default:
1594 LOG(FATAL) << "Invalid word arith op: " << opcode;
1595 }
1596 if (!is_div_rem) {
1597 if (unary) {
1598 rl_src1 = LoadValue(rl_src1, kCoreReg);
1599 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001600 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001602 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001603 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001604 RegStorage t_reg = AllocTemp();
1605 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001606 rl_src1 = LoadValue(rl_src1, kCoreReg);
1607 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001608 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 FreeTemp(t_reg);
1610 } else {
1611 rl_src1 = LoadValue(rl_src1, kCoreReg);
1612 rl_src2 = LoadValue(rl_src2, kCoreReg);
1613 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001614 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 }
1616 }
1617 StoreValue(rl_dest, rl_result);
1618 } else {
Dave Allison70202782013-10-22 17:52:19 -07001619 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001620 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 rl_src1 = LoadValue(rl_src1, kCoreReg);
1622 rl_src2 = LoadValue(rl_src2, kCoreReg);
1623 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001624 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 }
buzbee2700f7e2014-03-07 09:46:20 -08001626 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001627 done = true;
1628 } else if (cu_->instruction_set == kThumb2) {
1629 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1630 // Use ARM SDIV instruction for division. For remainder we also need to
1631 // calculate using a MUL and subtract.
1632 rl_src1 = LoadValue(rl_src1, kCoreReg);
1633 rl_src2 = LoadValue(rl_src2, kCoreReg);
1634 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001635 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001636 }
buzbee2700f7e2014-03-07 09:46:20 -08001637 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001638 done = true;
1639 }
1640 }
1641
1642 // If we haven't already generated the code use the callout function.
1643 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 FlushAllRegs(); /* Send everything to home location */
1645 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001646 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1647 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1648 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1650 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001651 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 }
Dave Allison70202782013-10-22 17:52:19 -07001653 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001654 if (Is64BitInstructionSet(cu_->instruction_set)) {
1655 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1656 } else {
1657 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1658 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 if (op == kOpDiv)
1660 rl_result = GetReturn(false);
1661 else
1662 rl_result = GetReturnAlt();
1663 }
1664 StoreValue(rl_dest, rl_result);
1665 }
1666}
1667
1668/*
1669 * The following are the first-level codegen routines that analyze the format
1670 * of each bytecode then either dispatch special purpose codegen routines
1671 * or produce corresponding Thumb instructions directly.
1672 */
1673
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001675static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 x &= x - 1;
1677 return (x & (x - 1)) == 0;
1678}
1679
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1681// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001682bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001683 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1685 return false;
1686 }
1687 // No divide instruction for Arm, so check for more special cases
1688 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001689 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 }
1691 int k = LowestSetBit(lit);
1692 if (k >= 30) {
1693 // Avoid special cases.
1694 return false;
1695 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 rl_src = LoadValue(rl_src, kCoreReg);
1697 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001698 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001699 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 if (lit == 2) {
1701 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001702 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1703 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1704 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001706 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001708 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1709 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 }
1711 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001712 RegStorage t_reg1 = AllocTemp();
1713 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001715 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1716 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001720 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001722 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001724 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 }
1726 }
1727 StoreValue(rl_dest, rl_result);
1728 return true;
1729}
1730
1731// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1732// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001733bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001734 if (lit < 0) {
1735 return false;
1736 }
1737 if (lit == 0) {
1738 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1739 LoadConstant(rl_result.reg, 0);
1740 StoreValue(rl_dest, rl_result);
1741 return true;
1742 }
1743 if (lit == 1) {
1744 rl_src = LoadValue(rl_src, kCoreReg);
1745 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1746 OpRegCopy(rl_result.reg, rl_src.reg);
1747 StoreValue(rl_dest, rl_result);
1748 return true;
1749 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001750 // There is RegRegRegShift on Arm, so check for more special cases
1751 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001752 return EasyMultiply(rl_src, rl_dest, lit);
1753 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754 // Can we simplify this multiplication?
1755 bool power_of_two = false;
1756 bool pop_count_le2 = false;
1757 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001758 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 power_of_two = true;
1760 } else if (IsPopCountLE2(lit)) {
1761 pop_count_le2 = true;
1762 } else if (IsPowerOfTwo(lit + 1)) {
1763 power_of_two_minus_one = true;
1764 } else {
1765 return false;
1766 }
1767 rl_src = LoadValue(rl_src, kCoreReg);
1768 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1769 if (power_of_two) {
1770 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001771 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772 } else if (pop_count_le2) {
1773 // Shift and add and shift.
1774 int first_bit = LowestSetBit(lit);
1775 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1776 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1777 } else {
1778 // Reverse subtract: (src << (shift + 1)) - src.
1779 DCHECK(power_of_two_minus_one);
1780 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001781 RegStorage t_reg = AllocTemp();
1782 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1783 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 }
1785 StoreValue(rl_dest, rl_result);
1786 return true;
1787}
1788
1789void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001790 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001791 RegLocation rl_result;
1792 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1793 int shift_op = false;
1794 bool is_div = false;
1795
1796 switch (opcode) {
1797 case Instruction::RSUB_INT_LIT8:
1798 case Instruction::RSUB_INT: {
1799 rl_src = LoadValue(rl_src, kCoreReg);
1800 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1801 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001802 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001804 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1805 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 }
1807 StoreValue(rl_dest, rl_result);
1808 return;
1809 }
1810
1811 case Instruction::SUB_INT:
1812 case Instruction::SUB_INT_2ADDR:
1813 lit = -lit;
1814 // Intended fallthrough
1815 case Instruction::ADD_INT:
1816 case Instruction::ADD_INT_2ADDR:
1817 case Instruction::ADD_INT_LIT8:
1818 case Instruction::ADD_INT_LIT16:
1819 op = kOpAdd;
1820 break;
1821 case Instruction::MUL_INT:
1822 case Instruction::MUL_INT_2ADDR:
1823 case Instruction::MUL_INT_LIT8:
1824 case Instruction::MUL_INT_LIT16: {
1825 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1826 return;
1827 }
1828 op = kOpMul;
1829 break;
1830 }
1831 case Instruction::AND_INT:
1832 case Instruction::AND_INT_2ADDR:
1833 case Instruction::AND_INT_LIT8:
1834 case Instruction::AND_INT_LIT16:
1835 op = kOpAnd;
1836 break;
1837 case Instruction::OR_INT:
1838 case Instruction::OR_INT_2ADDR:
1839 case Instruction::OR_INT_LIT8:
1840 case Instruction::OR_INT_LIT16:
1841 op = kOpOr;
1842 break;
1843 case Instruction::XOR_INT:
1844 case Instruction::XOR_INT_2ADDR:
1845 case Instruction::XOR_INT_LIT8:
1846 case Instruction::XOR_INT_LIT16:
1847 op = kOpXor;
1848 break;
1849 case Instruction::SHL_INT_LIT8:
1850 case Instruction::SHL_INT:
1851 case Instruction::SHL_INT_2ADDR:
1852 lit &= 31;
1853 shift_op = true;
1854 op = kOpLsl;
1855 break;
1856 case Instruction::SHR_INT_LIT8:
1857 case Instruction::SHR_INT:
1858 case Instruction::SHR_INT_2ADDR:
1859 lit &= 31;
1860 shift_op = true;
1861 op = kOpAsr;
1862 break;
1863 case Instruction::USHR_INT_LIT8:
1864 case Instruction::USHR_INT:
1865 case Instruction::USHR_INT_2ADDR:
1866 lit &= 31;
1867 shift_op = true;
1868 op = kOpLsr;
1869 break;
1870
1871 case Instruction::DIV_INT:
1872 case Instruction::DIV_INT_2ADDR:
1873 case Instruction::DIV_INT_LIT8:
1874 case Instruction::DIV_INT_LIT16:
1875 case Instruction::REM_INT:
1876 case Instruction::REM_INT_2ADDR:
1877 case Instruction::REM_INT_LIT8:
1878 case Instruction::REM_INT_LIT16: {
1879 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001880 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001881 return;
1882 }
buzbee11b63d12013-08-27 07:34:17 -07001883 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001884 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001885 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 (opcode == Instruction::DIV_INT_LIT16)) {
1887 is_div = true;
1888 } else {
1889 is_div = false;
1890 }
buzbee11b63d12013-08-27 07:34:17 -07001891 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1892 return;
1893 }
Dave Allison70202782013-10-22 17:52:19 -07001894
1895 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001896 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001898 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001899 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001900 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001901 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1902 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001903 } else if (cu_->instruction_set == kThumb2) {
1904 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1905 // Use ARM SDIV instruction for division. For remainder we also need to
1906 // calculate using a MUL and subtract.
1907 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001908 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001909 done = true;
1910 }
1911 }
1912
1913 if (!done) {
1914 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1916 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001917 if (Is64BitInstructionSet(cu_->instruction_set)) {
1918 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1919 false);
1920 } else {
1921 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1922 false);
1923 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 if (is_div)
1925 rl_result = GetReturn(false);
1926 else
1927 rl_result = GetReturnAlt();
1928 }
1929 StoreValue(rl_dest, rl_result);
1930 return;
1931 }
1932 default:
1933 LOG(FATAL) << "Unexpected opcode " << opcode;
1934 }
1935 rl_src = LoadValue(rl_src, kCoreReg);
1936 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001937 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001939 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001941 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001942 }
1943 StoreValue(rl_dest, rl_result);
1944}
1945
Andreas Gampe2f244e92014-05-08 03:35:25 -07001946template <size_t pointer_size>
1947static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1948 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 RegLocation rl_result;
1950 OpKind first_op = kOpBkpt;
1951 OpKind second_op = kOpBkpt;
1952 bool call_out = false;
1953 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001954 ThreadOffset<pointer_size> func_offset(-1);
1955 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001956
1957 switch (opcode) {
1958 case Instruction::NOT_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001959 if (cu->instruction_set == kArm64) {
1960 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1961 return;
1962 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001963 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1964 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001966 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001967 RegStorage t_reg = mir_to_lir->AllocTemp();
1968 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1969 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1970 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1971 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001973 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1974 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001976 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977 return;
1978 case Instruction::ADD_LONG:
1979 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001980 if (cu->instruction_set != kThumb2) {
1981 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 return;
1983 }
1984 first_op = kOpAdd;
1985 second_op = kOpAdc;
1986 break;
1987 case Instruction::SUB_LONG:
1988 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001989 if (cu->instruction_set != kThumb2) {
1990 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 return;
1992 }
1993 first_op = kOpSub;
1994 second_op = kOpSbc;
1995 break;
1996 case Instruction::MUL_LONG:
1997 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001998 if (cu->instruction_set != kMips) {
1999 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002000 return;
2001 } else {
2002 call_out = 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, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 }
2006 break;
2007 case Instruction::DIV_LONG:
2008 case Instruction::DIV_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002009 if (cu->instruction_set == kArm64) {
2010 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2011 return;
2012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 call_out = true;
2014 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002015 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2016 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 break;
2018 case Instruction::REM_LONG:
2019 case Instruction::REM_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002020 if (cu->instruction_set == kArm64) {
2021 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2022 return;
2023 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024 call_out = true;
2025 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002026 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002028 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2029 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 break;
2031 case Instruction::AND_LONG_2ADDR:
2032 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002033 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2034 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002035 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 }
2037 first_op = kOpAnd;
2038 second_op = kOpAnd;
2039 break;
2040 case Instruction::OR_LONG:
2041 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002042 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2043 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002044 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002045 return;
2046 }
2047 first_op = kOpOr;
2048 second_op = kOpOr;
2049 break;
2050 case Instruction::XOR_LONG:
2051 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002052 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2053 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002054 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055 return;
2056 }
2057 first_op = kOpXor;
2058 second_op = kOpXor;
2059 break;
2060 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002061 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062 return;
2063 }
2064 default:
2065 LOG(FATAL) << "Invalid long arith op";
2066 }
2067 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002068 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002070 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002071 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002072 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2073 mir_to_lir->TargetReg(kArg1));
2074 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2075 mir_to_lir->TargetReg(kArg3));
2076 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2077 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2078 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2079 mir_to_lir->TargetReg(kArg3)));
2080 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002082 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002084 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085 }
2086 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002087 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
2088 rl_result = mir_to_lir->GetReturnWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002089 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002090 rl_result = mir_to_lir->GetReturnWideAlt();
2091 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 }
2093}
2094
Andreas Gampe2f244e92014-05-08 03:35:25 -07002095void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2096 RegLocation rl_src1, RegLocation rl_src2) {
2097 if (Is64BitInstructionSet(cu_->instruction_set)) {
2098 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2099 } else {
2100 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2101 }
2102}
2103
Mark Mendelle87f9b52014-04-30 14:13:18 -04002104void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2105 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2106 LoadConstantNoClobber(rl_result.reg, value);
2107 StoreValue(rl_dest, rl_result);
2108 if (value == 0) {
2109 Workaround7250540(rl_dest, rl_result.reg);
2110 }
2111}
2112
Andreas Gampe2f244e92014-05-08 03:35:25 -07002113template <size_t pointer_size>
2114void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002115 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002116 /*
2117 * Don't optimize the register usage since it calls out to support
2118 * functions
2119 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002120 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2121
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002123 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2124 if (rl_dest.wide) {
2125 RegLocation rl_result;
2126 rl_result = GetReturnWide(rl_dest.fp);
2127 StoreValueWide(rl_dest, rl_result);
2128 } else {
2129 RegLocation rl_result;
2130 rl_result = GetReturn(rl_dest.fp);
2131 StoreValue(rl_dest, rl_result);
2132 }
2133}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002134template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2135 RegLocation rl_dest, RegLocation rl_src);
2136template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2137 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002138
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002139class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2140 public:
2141 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2142 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2143 }
2144
2145 void Compile() OVERRIDE {
2146 m2l_->ResetRegPool();
2147 m2l_->ResetDefTracking();
2148 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002149 if (Is64BitInstructionSet(cu_->instruction_set)) {
2150 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2151 } else {
2152 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2153 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002154 if (cont_ != nullptr) {
2155 m2l_->OpUnconditionalBranch(cont_);
2156 }
2157 }
2158};
2159
Brian Carlstrom7940e442013-07-12 13:46:57 -07002160/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002161void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002162 if (Runtime::Current()->ExplicitSuspendChecks()) {
2163 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2164 return;
2165 }
2166 FlushAllRegs();
2167 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002168 LIR* cont = NewLIR0(kPseudoTargetLabel);
2169 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002170 } else {
2171 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2172 return;
2173 }
2174 FlushAllRegs(); // TODO: needed?
2175 LIR* inst = CheckSuspendUsingLoad();
2176 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002177 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002178}
2179
2180/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002181void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002182 if (Runtime::Current()->ExplicitSuspendChecks()) {
2183 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2184 OpUnconditionalBranch(target);
2185 return;
2186 }
2187 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002188 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002189 LIR* branch = OpUnconditionalBranch(nullptr);
2190 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002191 } else {
2192 // For the implicit suspend check, just perform the trigger
2193 // load and branch to the target.
2194 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2195 OpUnconditionalBranch(target);
2196 return;
2197 }
2198 FlushAllRegs();
2199 LIR* inst = CheckSuspendUsingLoad();
2200 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002201 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002203}
2204
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002205/* Call out to helper assembly routine that will null check obj and then lock it. */
2206void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2207 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002208 if (Is64BitInstructionSet(cu_->instruction_set)) {
2209 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2210 } else {
2211 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2212 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002213}
2214
2215/* Call out to helper assembly routine that will null check obj and then unlock it. */
2216void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2217 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002218 if (Is64BitInstructionSet(cu_->instruction_set)) {
2219 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2220 } else {
2221 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2222 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002223}
2224
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002225/* Generic code for generating a wide constant into a VR. */
2226void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2227 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002228 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002229 StoreValueWide(rl_dest, rl_result);
2230}
2231
Brian Carlstrom7940e442013-07-12 13:46:57 -07002232} // namespace art