blob: 69ca7154e44ec2910c0dd6dfaef64efd5e051ded [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) {
buzbeea0cd2d72014-06-01 09:33:49 -0700223 DCHECK(!rl_src1.fp);
224 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 ConditionCode cond;
226 switch (opcode) {
227 case Instruction::IF_EQ:
228 cond = kCondEq;
229 break;
230 case Instruction::IF_NE:
231 cond = kCondNe;
232 break;
233 case Instruction::IF_LT:
234 cond = kCondLt;
235 break;
236 case Instruction::IF_GE:
237 cond = kCondGe;
238 break;
239 case Instruction::IF_GT:
240 cond = kCondGt;
241 break;
242 case Instruction::IF_LE:
243 cond = kCondLe;
244 break;
245 default:
246 cond = static_cast<ConditionCode>(0);
247 LOG(FATAL) << "Unexpected opcode " << opcode;
248 }
249
250 // Normalize such that if either operand is constant, src2 will be constant
251 if (rl_src1.is_const) {
252 RegLocation rl_temp = rl_src1;
253 rl_src1 = rl_src2;
254 rl_src2 = rl_temp;
255 cond = FlipComparisonOrder(cond);
256 }
257
buzbeea0cd2d72014-06-01 09:33:49 -0700258 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 // Is this really an immediate comparison?
260 if (rl_src2.is_const) {
261 // If it's already live in a register or not easily materialized, just keep going
262 RegLocation rl_temp = UpdateLoc(rl_src2);
263 if ((rl_temp.location == kLocDalvikFrame) &&
264 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
265 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800266 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 return;
268 }
269 }
buzbeea0cd2d72014-06-01 09:33:49 -0700270 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800271 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272}
273
274void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700275 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700277 DCHECK(!rl_src.fp);
278 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 switch (opcode) {
280 case Instruction::IF_EQZ:
281 cond = kCondEq;
282 break;
283 case Instruction::IF_NEZ:
284 cond = kCondNe;
285 break;
286 case Instruction::IF_LTZ:
287 cond = kCondLt;
288 break;
289 case Instruction::IF_GEZ:
290 cond = kCondGe;
291 break;
292 case Instruction::IF_GTZ:
293 cond = kCondGt;
294 break;
295 case Instruction::IF_LEZ:
296 cond = kCondLe;
297 break;
298 default:
299 cond = static_cast<ConditionCode>(0);
300 LOG(FATAL) << "Unexpected opcode " << opcode;
301 }
buzbee2700f7e2014-03-07 09:46:20 -0800302 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303}
304
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700305void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
307 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800308 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800310 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
buzbee2700f7e2014-03-07 09:46:20 -0800312 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 StoreValueWide(rl_dest, rl_result);
314}
315
316void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700318 rl_src = LoadValue(rl_src, kCoreReg);
319 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
320 OpKind op = kOpInvalid;
321 switch (opcode) {
322 case Instruction::INT_TO_BYTE:
323 op = kOp2Byte;
324 break;
325 case Instruction::INT_TO_SHORT:
326 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700328 case Instruction::INT_TO_CHAR:
329 op = kOp2Char;
330 break;
331 default:
332 LOG(ERROR) << "Bad int conversion type";
333 }
buzbee2700f7e2014-03-07 09:46:20 -0800334 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700335 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336}
337
Andreas Gampe2f244e92014-05-08 03:35:25 -0700338template <size_t pointer_size>
339static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
340 uint32_t type_idx, RegLocation rl_dest,
341 RegLocation rl_src) {
342 mir_to_lir->FlushAllRegs(); /* Everything to home location */
343 ThreadOffset<pointer_size> func_offset(-1);
344 const DexFile* dex_file = cu->dex_file;
345 CompilerDriver* driver = cu->compiler_driver;
346 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
347 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800348 bool is_type_initialized; // Ignored as an array does not have an initializer.
349 bool use_direct_type_ptr;
350 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700351 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800352 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700353 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
354 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800355 // The fast path.
356 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700357 mir_to_lir->LoadClassType(type_idx, kArg0);
358 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
359 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0),
360 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 } else {
362 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700363 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
364 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
365 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800366 }
367 } else {
368 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700369 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
370 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800371 }
372 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700374 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
375 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 }
buzbeea0cd2d72014-06-01 09:33:49 -0700377 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700378 mir_to_lir->StoreValue(rl_dest, rl_result);
379}
380
381/*
382 * Let helper function take care of everything. Will call
383 * Array::AllocFromCode(type_idx, method, count);
384 * Note: AllocFromCode will handle checks for errNegativeArraySize.
385 */
386void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
387 RegLocation rl_src) {
388 if (Is64BitInstructionSet(cu_->instruction_set)) {
389 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
390 } else {
391 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
392 }
393}
394
395template <size_t pointer_size>
396static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
397 ThreadOffset<pointer_size> func_offset(-1);
398 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
399 type_idx)) {
400 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
401 } else {
402 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
403 }
404 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405}
406
407/*
408 * Similar to GenNewArray, but with post-allocation initialization.
409 * Verifier guarantees we're dealing with an array class. Current
410 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
411 * Current code also throws internal unimp if not 'L', '[' or 'I'.
412 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700413void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 int elems = info->num_arg_words;
415 int type_idx = info->index;
416 FlushAllRegs(); /* Everything to home location */
Andreas Gampe2f244e92014-05-08 03:35:25 -0700417 if (Is64BitInstructionSet(cu_->instruction_set)) {
418 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700420 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 FreeTemp(TargetReg(kArg2));
423 FreeTemp(TargetReg(kArg1));
424 /*
425 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
426 * return region. Because AllocFromCode placed the new array
427 * in kRet0, we'll just lock it into place. When debugger support is
428 * added, it may be necessary to additionally copy all return
429 * values to a home location in thread-local storage
430 */
431 LockTemp(TargetReg(kRet0));
432
433 // TODO: use the correct component size, currently all supported types
434 // share array alignment with ints (see comment at head of function)
435 size_t component_size = sizeof(int32_t);
436
437 // Having a range of 0 is legal
438 if (info->is_range && (elems > 0)) {
439 /*
440 * Bit of ugliness here. We're going generate a mem copy loop
441 * on the register range, but it is possible that some regs
442 * in the range have been promoted. This is unlikely, but
443 * before generating the copy, we'll just force a flush
444 * of any regs in the source range that have been promoted to
445 * home location.
446 */
447 for (int i = 0; i < elems; i++) {
448 RegLocation loc = UpdateLoc(info->args[i]);
449 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700450 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 }
452 }
453 /*
454 * TUNING note: generated code here could be much improved, but
455 * this is an uncommon operation and isn't especially performance
456 * critical.
457 */
buzbee2700f7e2014-03-07 09:46:20 -0800458 RegStorage r_src = AllocTemp();
459 RegStorage r_dst = AllocTemp();
460 RegStorage r_idx = AllocTemp();
461 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700462 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 case kThumb2:
464 r_val = TargetReg(kLr);
465 break;
466 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700467 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 FreeTemp(TargetReg(kRet0));
469 r_val = AllocTemp();
470 break;
471 case kMips:
472 r_val = AllocTemp();
473 break;
474 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
475 }
476 // Set up source pointer
477 RegLocation rl_first = info->args[0];
478 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
479 // Set up the target pointer
480 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
481 mirror::Array::DataOffset(component_size).Int32Value());
482 // Set up the loop counter (known to be > 0)
483 LoadConstant(r_idx, elems - 1);
484 // Generate the copy loop. Going backwards for convenience
485 LIR* target = NewLIR0(kPseudoTargetLabel);
486 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700487 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
488 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 FreeTemp(r_val);
490 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700491 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 // Restore the target pointer
493 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
494 -mirror::Array::DataOffset(component_size).Int32Value());
495 }
496 } else if (!info->is_range) {
497 // TUNING: interleave
498 for (int i = 0; i < elems; i++) {
499 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700500 Store32Disp(TargetReg(kRet0),
501 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800503 if (IsTemp(rl_arg.reg)) {
504 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 }
506 }
507 }
508 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700509 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 }
511}
512
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800513//
514// Slow path to ensure a class is initialized for sget/sput.
515//
516class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
517 public:
buzbee2700f7e2014-03-07 09:46:20 -0800518 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
519 RegStorage r_base) :
520 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
521 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800522 }
523
524 void Compile() {
525 LIR* unresolved_target = GenerateTargetLabel();
526 uninit_->target = unresolved_target;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700527 if (Is64BitInstructionSet(cu_->instruction_set)) {
528 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
529 storage_index_, true);
530 } else {
531 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
532 storage_index_, true);
533 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534 // Copy helper's result into r_base, a no-op on all but MIPS.
535 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
536
537 m2l_->OpUnconditionalBranch(cont_);
538 }
539
540 private:
541 LIR* const uninit_;
542 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800543 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544};
545
Andreas Gampe2f244e92014-05-08 03:35:25 -0700546template <size_t pointer_size>
547static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
548 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
549 ThreadOffset<pointer_size> setter_offset =
550 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
551 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
552 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
553 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
554 true);
555}
556
Vladimir Markobe0e5462014-02-26 11:24:15 +0000557void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700558 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000559 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
560 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100561 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
562 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
563 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000564 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800565 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000566 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100568 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700569 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700570 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800571 if (IsTemp(rl_method.reg)) {
572 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
574 } else {
575 // Medium path, static storage base in a different class which requires checks that the other
576 // class is initialized.
577 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000578 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 // May do runtime call so everything to home locations.
580 FlushAllRegs();
581 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800582 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 LockTemp(r_method);
584 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800585 r_base = TargetReg(kArg0);
586 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700587 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000588 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
589 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000591 if (!field_info.IsInitialized() &&
592 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800593 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800594
595 // The slow path is invoked if the r_base is NULL or the class pointed
596 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800597 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800598 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800599 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800601 mirror::Class::StatusOffset().Int32Value(),
602 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800603 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604
buzbee2700f7e2014-03-07 09:46:20 -0800605 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000606 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607
608 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700609 // Ensure load of status and load of value don't re-order.
610 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 FreeTemp(r_method);
613 }
614 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100615 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100617 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100619 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000621 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800622 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100624 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800625 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100627 } else {
628 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 }
630 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800631 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800633 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 } else {
635 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700636 if (Is64BitInstructionSet(cu_->instruction_set)) {
637 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
638 } else {
639 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
640 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 }
642}
643
Andreas Gampe2f244e92014-05-08 03:35:25 -0700644template <size_t pointer_size>
645static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
646 const MirSFieldLoweringInfo* field_info) {
647 ThreadOffset<pointer_size> getter_offset =
648 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
649 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
650 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
651 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
652}
653
Vladimir Markobe0e5462014-02-26 11:24:15 +0000654void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700655 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000656 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
657 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100658 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
659 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
660 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000661 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800662 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000663 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 // Fast path, static storage base is this method's class
665 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700666 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700667 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 } else {
669 // Medium path, static storage base in a different class which requires checks that the other
670 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000671 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 // May do runtime call so everything to home locations.
673 FlushAllRegs();
674 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800675 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 LockTemp(r_method);
677 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800678 r_base = TargetReg(kArg0);
679 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700680 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000681 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
682 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800683 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000684 if (!field_info.IsInitialized() &&
685 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800686 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800687
688 // The slow path is invoked if the r_base is NULL or the class pointed
689 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800690 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800691 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800692 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800693 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800694 mirror::Class::StatusOffset().Int32Value(),
695 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800696 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800697
buzbee2700f7e2014-03-07 09:46:20 -0800698 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000699 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800700
701 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700702 // Ensure load of status and load of value don't re-order.
703 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 FreeTemp(r_method);
706 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800707 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100708 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
709 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800710
Vladimir Marko674744e2014-04-24 15:18:26 +0100711 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800712 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100713 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800714 // Without context sensitive analysis, we must issue the most conservative barriers.
715 // In this case, either a load or store may follow so we issue both barriers.
716 GenMemBarrier(kLoadLoad);
717 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100718 } else {
719 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800720 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100721 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800722
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 if (is_long_or_double) {
724 StoreValueWide(rl_dest, rl_result);
725 } else {
726 StoreValue(rl_dest, rl_result);
727 }
728 } else {
729 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700730 if (Is64BitInstructionSet(cu_->instruction_set)) {
731 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
732 } else {
733 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
734 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700736 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 StoreValueWide(rl_dest, rl_result);
738 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700739 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 StoreValue(rl_dest, rl_result);
741 }
742 }
743}
744
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800745// Generate code for all slow paths.
746void Mir2Lir::HandleSlowPaths() {
747 int n = slow_paths_.Size();
748 for (int i = 0; i < n; ++i) {
749 LIRSlowPath* slowpath = slow_paths_.Get(i);
750 slowpath->Compile();
751 }
752 slow_paths_.Reset();
753}
754
Andreas Gampe2f244e92014-05-08 03:35:25 -0700755template <size_t pointer_size>
756static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
757 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
758 ThreadOffset<pointer_size> getter_offset =
759 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
760 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
761 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
762 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
763 true);
764}
765
Vladimir Markobe0e5462014-02-26 11:24:15 +0000766void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700768 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000769 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
770 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100771 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
772 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
773 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
774 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000775 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700776 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100777 GenNullCheck(rl_obj.reg, opt_flags);
778 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
779 int field_offset = field_info.FieldOffset().Int32Value();
780 if (field_info.IsVolatile()) {
781 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
782 MarkPossibleNullPointerException(opt_flags);
783 // Without context sensitive analysis, we must issue the most conservative barriers.
784 // In this case, either a load or store may follow so we issue both barriers.
785 GenMemBarrier(kLoadLoad);
786 GenMemBarrier(kLoadStore);
787 } else {
788 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
789 MarkPossibleNullPointerException(opt_flags);
790 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 StoreValueWide(rl_dest, rl_result);
793 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 StoreValue(rl_dest, rl_result);
795 }
796 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700797 if (Is64BitInstructionSet(cu_->instruction_set)) {
798 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
799 } else {
800 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
801 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700803 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 StoreValueWide(rl_dest, rl_result);
805 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700806 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 StoreValue(rl_dest, rl_result);
808 }
809 }
810}
811
Andreas Gampe2f244e92014-05-08 03:35:25 -0700812template <size_t pointer_size>
813static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
814 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
815 RegLocation rl_src) {
816 ThreadOffset<pointer_size> setter_offset =
817 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
818 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
819 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
820 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
821 rl_obj, rl_src, true);
822}
823
Vladimir Markobe0e5462014-02-26 11:24:15 +0000824void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700826 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000827 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
828 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100829 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
830 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
831 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
832 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700834 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100836 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 } else {
838 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100839 }
840 GenNullCheck(rl_obj.reg, opt_flags);
841 int field_offset = field_info.FieldOffset().Int32Value();
842 if (field_info.IsVolatile()) {
843 // There might have been a store before this volatile one so insert StoreStore barrier.
844 GenMemBarrier(kStoreStore);
845 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800846 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100847 // A load might follow the volatile store so insert a StoreLoad barrier.
848 GenMemBarrier(kStoreLoad);
849 } else {
850 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
851 MarkPossibleNullPointerException(opt_flags);
852 }
853 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
854 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 }
856 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700857 if (Is64BitInstructionSet(cu_->instruction_set)) {
858 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
859 } else {
860 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
861 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 }
863}
864
Andreas Gampe2f244e92014-05-08 03:35:25 -0700865template <size_t pointer_size>
866static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
867 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
868 ThreadOffset<pointer_size> helper = needs_range_check
869 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
870 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
871 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
872 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
873 true);
874}
875
Ian Rogersa9a82542013-10-04 11:17:26 -0700876void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
877 RegLocation rl_src) {
878 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
879 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
880 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700881 if (Is64BitInstructionSet(cu_->instruction_set)) {
882 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
883 } else {
884 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
885 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700886}
887
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700888void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800890 RegStorage res_reg = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -0700891 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
893 *cu_->dex_file,
894 type_idx)) {
895 // Call out to helper which resolves type and verifies access.
896 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700897 if (Is64BitInstructionSet(cu_->instruction_set)) {
898 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
899 type_idx, rl_method.reg, true);
900 } else {
901 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
902 type_idx, rl_method.reg, true);
903 }
buzbeea0cd2d72014-06-01 09:33:49 -0700904 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 StoreValue(rl_dest, rl_result);
906 } else {
907 // We're don't need access checks, load type from dex cache
908 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700909 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700910 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000911 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700912 LoadRefDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
914 type_idx) || SLOW_TYPE_PATH) {
915 // Slow path, at runtime test if type is null and if so initialize
916 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800917 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800918 LIR* cont = NewLIR0(kPseudoTargetLabel);
919
920 // Object to generate the slow path for class resolution.
921 class SlowPath : public LIRSlowPath {
922 public:
923 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
924 const RegLocation& rl_method, const RegLocation& rl_result) :
925 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
926 rl_method_(rl_method), rl_result_(rl_result) {
927 }
928
929 void Compile() {
930 GenerateTargetLabel();
931
Andreas Gampe2f244e92014-05-08 03:35:25 -0700932 if (Is64BitInstructionSet(cu_->instruction_set)) {
933 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
934 rl_method_.reg, true);
935 } else {
936 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
937 rl_method_.reg, true);
938 }
buzbee2700f7e2014-03-07 09:46:20 -0800939 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800940
941 m2l_->OpUnconditionalBranch(cont_);
942 }
943
944 private:
945 const int type_idx_;
946 const RegLocation rl_method_;
947 const RegLocation rl_result_;
948 };
949
950 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800951 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800952
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 // Fast path, we're done - just store result
956 StoreValue(rl_dest, rl_result);
957 }
958 }
959}
960
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700961void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000963 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
964 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
966 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
967 // slow path, resolve string if not in dex cache
968 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700969 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800970
971 // If the Method* is already in a register, we can save a copy.
972 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800973 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800974 if (rl_method.location == kLocPhysReg) {
975 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800976 DCHECK(!IsTemp(rl_method.reg));
977 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800978 } else {
979 r_method = TargetReg(kArg2);
980 LoadCurrMethodDirect(r_method);
981 }
buzbee695d13a2014-04-19 13:32:20 -0700982 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
983 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800984
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 // Might call out to helper, which will return resolved string in kRet0
buzbeea0cd2d72014-06-01 09:33:49 -0700986 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700987 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
988 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800989
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700990 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800991 // Object to generate the slow path for string resolution.
992 class SlowPath : public LIRSlowPath {
993 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700994 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
995 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
996 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800997 }
998
999 void Compile() {
1000 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001001 if (Is64BitInstructionSet(cu_->instruction_set)) {
1002 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1003 r_method_, string_idx_, true);
1004 } else {
1005 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1006 r_method_, string_idx_, true);
1007 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001008 m2l_->OpUnconditionalBranch(cont_);
1009 }
1010
1011 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001012 const RegStorage r_method_;
1013 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001014 };
1015
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001016 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001018
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001020 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 } else {
1022 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001023 RegStorage res_reg = AllocTempRef();
1024 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001025 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeea0cd2d72014-06-01 09:33:49 -07001026 LoadRefDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 StoreValue(rl_dest, rl_result);
1028 }
1029}
1030
Andreas Gampe2f244e92014-05-08 03:35:25 -07001031template <size_t pointer_size>
1032static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1033 RegLocation rl_dest) {
1034 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 // alloc will always check for resolution, do we also need to verify
1036 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001037 ThreadOffset<pointer_size> func_offset(-1);
1038 const DexFile* dex_file = cu->dex_file;
1039 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001040 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001041 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001042 bool is_type_initialized;
1043 bool use_direct_type_ptr;
1044 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001045 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001046 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001047 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1048 &direct_type_ptr, &is_finalizable) &&
1049 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001050 // The fast path.
1051 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001052 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001054 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1055 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001057 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1058 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 }
1060 } else {
1061 // Use the direct pointer.
1062 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001063 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1064 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001066 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1067 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 }
1069 }
1070 } else {
1071 // The slow path.
1072 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001073 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1074 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 }
1076 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001078 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1079 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
buzbeea0cd2d72014-06-01 09:33:49 -07001081 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 mir_to_lir->StoreValue(rl_dest, rl_result);
1083}
1084
1085/*
1086 * Let helper function take care of everything. Will
1087 * call Class::NewInstanceFromCode(type_idx, method);
1088 */
1089void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1090 if (Is64BitInstructionSet(cu_->instruction_set)) {
1091 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1092 } else {
1093 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095}
1096
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001097void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001099 if (Is64BitInstructionSet(cu_->instruction_set)) {
1100 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1101 } else {
1102 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1103 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104}
1105
1106// For final classes there are no sub-classes to check and so we can answer the instance-of
1107// question with simple comparisons.
1108void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1109 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001110 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001111 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001112
buzbeea0cd2d72014-06-01 09:33:49 -07001113 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001115 RegStorage result_reg = rl_result.reg;
1116 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 result_reg = AllocTypedTemp(false, kCoreReg);
1118 }
1119 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001120 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121
buzbeea0cd2d72014-06-01 09:33:49 -07001122 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1123 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124
1125 LoadCurrMethodDirect(check_class);
1126 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001127 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1128 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 } else {
buzbee695d13a2014-04-19 13:32:20 -07001130 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1131 check_class);
1132 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001133 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001134 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 }
1136
1137 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001138 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 if (cu_->instruction_set == kThumb2) {
1140 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001141 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001143 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 } else {
1145 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1146 LoadConstant(result_reg, 1); // eq case - load true
1147 }
1148 LIR* target = NewLIR0(kPseudoTargetLabel);
1149 null_branchover->target = target;
1150 if (ne_branchover != NULL) {
1151 ne_branchover->target = target;
1152 }
1153 FreeTemp(object_class);
1154 FreeTemp(check_class);
1155 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001156 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 FreeTemp(result_reg);
1158 }
1159 StoreValue(rl_dest, rl_result);
1160}
1161
1162void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1163 bool type_known_abstract, bool use_declaring_class,
1164 bool can_assume_type_is_in_dex_cache,
1165 uint32_t type_idx, RegLocation rl_dest,
1166 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001167 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001168 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001169
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 FlushAllRegs();
1171 // May generate a call - use explicit registers
1172 LockCallTemps();
1173 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001174 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 if (needs_access_check) {
1176 // Check we have access to type_idx and if not throw IllegalAccessError,
1177 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001178 if (Is64BitInstructionSet(cu_->instruction_set)) {
1179 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1180 type_idx, true);
1181 } else {
1182 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1183 type_idx, true);
1184 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1186 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1187 } else if (use_declaring_class) {
1188 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001189 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001190 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 } else {
1192 // Load dex cache entry into class_reg (kArg2)
1193 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001194 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1195 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001196 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001197 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (!can_assume_type_is_in_dex_cache) {
1199 // Need to test presence of type in dex cache at runtime
1200 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1201 // Not resolved
1202 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001203 if (Is64BitInstructionSet(cu_->instruction_set)) {
1204 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1205 } else {
1206 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1207 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001208 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1210 // Rejoin code paths
1211 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1212 hop_branch->target = hop_target;
1213 }
1214 }
1215 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001216 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (cu_->instruction_set == kMips) {
1218 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001219 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 }
1221 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1222
1223 /* load object->klass_ */
1224 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001225 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1227 LIR* branchover = NULL;
1228 if (type_known_final) {
1229 // rl_result == ref == null == 0.
1230 if (cu_->instruction_set == kThumb2) {
1231 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001232 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001233 LoadConstant(rl_result.reg, 1); // .eq case - load true
1234 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001235 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001237 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001239 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 }
1241 } else {
1242 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001243 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1244 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1245 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001246 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 if (!type_known_abstract) {
1248 /* Uses conditional nullification */
1249 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001250 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1252 }
1253 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1254 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001255 if (it != nullptr) {
1256 OpEndIT(it);
1257 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 FreeTemp(r_tgt);
1259 } else {
1260 if (!type_known_abstract) {
1261 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001262 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1264 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001265 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1266 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1267 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001268 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1269 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1270 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 }
1272 }
1273 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001274 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 /* branch targets here */
1276 LIR* target = NewLIR0(kPseudoTargetLabel);
1277 StoreValue(rl_dest, rl_result);
1278 branch1->target = target;
1279 if (branchover != NULL) {
1280 branchover->target = target;
1281 }
1282}
1283
1284void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1285 bool type_known_final, type_known_abstract, use_declaring_class;
1286 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1287 *cu_->dex_file,
1288 type_idx,
1289 &type_known_final,
1290 &type_known_abstract,
1291 &use_declaring_class);
1292 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1293 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1294
1295 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1296 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1297 } else {
1298 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1299 use_declaring_class, can_assume_type_is_in_dex_cache,
1300 type_idx, rl_dest, rl_src);
1301 }
1302}
1303
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001304void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 bool type_known_final, type_known_abstract, use_declaring_class;
1306 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1307 *cu_->dex_file,
1308 type_idx,
1309 &type_known_final,
1310 &type_known_abstract,
1311 &use_declaring_class);
1312 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1313 // of the exception throw path.
1314 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001315 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 // Verifier type analysis proved this check cast would never cause an exception.
1317 return;
1318 }
1319 FlushAllRegs();
1320 // May generate a call - use explicit registers
1321 LockCallTemps();
1322 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001323 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 if (needs_access_check) {
1325 // Check we have access to type_idx and if not throw IllegalAccessError,
1326 // returns Class* in kRet0
1327 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001328 if (Is64BitInstructionSet(cu_->instruction_set)) {
1329 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1330 type_idx, TargetReg(kArg1), true);
1331 } else {
1332 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1333 type_idx, TargetReg(kArg1), true);
1334 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1336 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001337 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1338 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 } else {
1340 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001341 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1342 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001343 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001344 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1346 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001347 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1348 LIR* cont = NewLIR0(kPseudoTargetLabel);
1349
1350 // Slow path to initialize the type. Executed if the type is NULL.
1351 class SlowPath : public LIRSlowPath {
1352 public:
1353 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001354 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001355 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1356 class_reg_(class_reg) {
1357 }
1358
1359 void Compile() {
1360 GenerateTargetLabel();
1361
1362 // Call out to helper, which will return resolved type in kArg0
1363 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001364 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1365 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1366 m2l_->TargetReg(kArg1), true);
1367 } else {
1368 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1369 m2l_->TargetReg(kArg1), true);
1370 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001371 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1372 m2l_->OpUnconditionalBranch(cont_);
1373 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001374
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 public:
1376 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001377 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001378 };
1379
buzbee2700f7e2014-03-07 09:46:20 -08001380 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 }
1382 }
1383 // At this point, class_reg (kArg2) has class
1384 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385
1386 // Slow path for the case where the classes are not equal. In this case we need
1387 // to call a helper function to do the check.
1388 class SlowPath : public LIRSlowPath {
1389 public:
1390 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1391 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1392 }
1393
1394 void Compile() {
1395 GenerateTargetLabel();
1396
1397 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001398 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1399 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001401 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1402 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1403 m2l_->TargetReg(kArg1), true);
1404 } else {
1405 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1406 m2l_->TargetReg(kArg1), true);
1407 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001408
1409 m2l_->OpUnconditionalBranch(cont_);
1410 }
1411
1412 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001413 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001414 };
1415
1416 if (type_known_abstract) {
1417 // Easier case, run slow path if target is non-null (slow path will load from target)
1418 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1419 LIR* cont = NewLIR0(kPseudoTargetLabel);
1420 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1421 } else {
1422 // Harder, more common case. We need to generate a forward branch over the load
1423 // if the target is null. If it's non-null we perform the load and branch to the
1424 // slow path if the classes are not equal.
1425
1426 /* Null is OK - continue */
1427 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1428 /* load object->klass_ */
1429 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001430 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001431
1432 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1433 LIR* cont = NewLIR0(kPseudoTargetLabel);
1434
1435 // Add the slow path that will not perform load since this is already done.
1436 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1437
1438 // Set the null check to branch to the continuation.
1439 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 }
1441}
1442
1443void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001444 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 RegLocation rl_result;
1446 if (cu_->instruction_set == kThumb2) {
1447 /*
1448 * NOTE: This is the one place in the code in which we might have
1449 * as many as six live temporary registers. There are 5 in the normal
1450 * set for Arm. Until we have spill capabilities, temporarily add
1451 * lr to the temp set. It is safe to do this locally, but note that
1452 * lr is used explicitly elsewhere in the code generator and cannot
1453 * normally be used as a general temp register.
1454 */
1455 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1456 FreeTemp(TargetReg(kLr)); // and make it available
1457 }
1458 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1459 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1460 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1461 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001462 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1463 RegStorage t_reg = AllocTemp();
1464 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1465 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1466 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 FreeTemp(t_reg);
1468 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001469 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1470 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 }
1472 /*
1473 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1474 * following StoreValueWide might need to allocate a temp register.
1475 * To further work around the lack of a spill capability, explicitly
1476 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1477 * Remove when spill is functional.
1478 */
1479 FreeRegLocTemps(rl_result, rl_src1);
1480 FreeRegLocTemps(rl_result, rl_src2);
1481 StoreValueWide(rl_dest, rl_result);
1482 if (cu_->instruction_set == kThumb2) {
1483 Clobber(TargetReg(kLr));
1484 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1485 }
1486}
1487
1488
Andreas Gampe2f244e92014-05-08 03:35:25 -07001489template <size_t pointer_size>
1490static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1491 RegLocation rl_shift) {
1492 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493
1494 switch (opcode) {
1495 case Instruction::SHL_LONG:
1496 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001497 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 break;
1499 case Instruction::SHR_LONG:
1500 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001501 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 break;
1503 case Instruction::USHR_LONG:
1504 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001505 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 break;
1507 default:
1508 LOG(FATAL) << "Unexpected case";
1509 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001510 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1511 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1512}
1513
1514void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1515 RegLocation rl_src1, RegLocation rl_shift) {
1516 if (Is64BitInstructionSet(cu_->instruction_set)) {
1517 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1518 } else {
1519 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1520 }
buzbeea0cd2d72014-06-01 09:33:49 -07001521 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 StoreValueWide(rl_dest, rl_result);
1523}
1524
1525
1526void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001527 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001528 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 OpKind op = kOpBkpt;
1530 bool is_div_rem = false;
1531 bool check_zero = false;
1532 bool unary = false;
1533 RegLocation rl_result;
1534 bool shift_op = false;
1535 switch (opcode) {
1536 case Instruction::NEG_INT:
1537 op = kOpNeg;
1538 unary = true;
1539 break;
1540 case Instruction::NOT_INT:
1541 op = kOpMvn;
1542 unary = true;
1543 break;
1544 case Instruction::ADD_INT:
1545 case Instruction::ADD_INT_2ADDR:
1546 op = kOpAdd;
1547 break;
1548 case Instruction::SUB_INT:
1549 case Instruction::SUB_INT_2ADDR:
1550 op = kOpSub;
1551 break;
1552 case Instruction::MUL_INT:
1553 case Instruction::MUL_INT_2ADDR:
1554 op = kOpMul;
1555 break;
1556 case Instruction::DIV_INT:
1557 case Instruction::DIV_INT_2ADDR:
1558 check_zero = true;
1559 op = kOpDiv;
1560 is_div_rem = true;
1561 break;
1562 /* NOTE: returns in kArg1 */
1563 case Instruction::REM_INT:
1564 case Instruction::REM_INT_2ADDR:
1565 check_zero = true;
1566 op = kOpRem;
1567 is_div_rem = true;
1568 break;
1569 case Instruction::AND_INT:
1570 case Instruction::AND_INT_2ADDR:
1571 op = kOpAnd;
1572 break;
1573 case Instruction::OR_INT:
1574 case Instruction::OR_INT_2ADDR:
1575 op = kOpOr;
1576 break;
1577 case Instruction::XOR_INT:
1578 case Instruction::XOR_INT_2ADDR:
1579 op = kOpXor;
1580 break;
1581 case Instruction::SHL_INT:
1582 case Instruction::SHL_INT_2ADDR:
1583 shift_op = true;
1584 op = kOpLsl;
1585 break;
1586 case Instruction::SHR_INT:
1587 case Instruction::SHR_INT_2ADDR:
1588 shift_op = true;
1589 op = kOpAsr;
1590 break;
1591 case Instruction::USHR_INT:
1592 case Instruction::USHR_INT_2ADDR:
1593 shift_op = true;
1594 op = kOpLsr;
1595 break;
1596 default:
1597 LOG(FATAL) << "Invalid word arith op: " << opcode;
1598 }
1599 if (!is_div_rem) {
1600 if (unary) {
1601 rl_src1 = LoadValue(rl_src1, kCoreReg);
1602 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001603 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001605 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001606 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001607 RegStorage t_reg = AllocTemp();
1608 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 rl_src1 = LoadValue(rl_src1, kCoreReg);
1610 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001611 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 FreeTemp(t_reg);
1613 } else {
1614 rl_src1 = LoadValue(rl_src1, kCoreReg);
1615 rl_src2 = LoadValue(rl_src2, kCoreReg);
1616 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001617 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 }
1619 }
1620 StoreValue(rl_dest, rl_result);
1621 } else {
Dave Allison70202782013-10-22 17:52:19 -07001622 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 +01001623 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 rl_src1 = LoadValue(rl_src1, kCoreReg);
1625 rl_src2 = LoadValue(rl_src2, kCoreReg);
1626 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001627 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 }
buzbee2700f7e2014-03-07 09:46:20 -08001629 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001630 done = true;
1631 } else if (cu_->instruction_set == kThumb2) {
1632 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1633 // Use ARM SDIV instruction for division. For remainder we also need to
1634 // calculate using a MUL and subtract.
1635 rl_src1 = LoadValue(rl_src1, kCoreReg);
1636 rl_src2 = LoadValue(rl_src2, kCoreReg);
1637 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001638 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001639 }
buzbee2700f7e2014-03-07 09:46:20 -08001640 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001641 done = true;
1642 }
1643 }
1644
1645 // If we haven't already generated the code use the callout function.
1646 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 FlushAllRegs(); /* Send everything to home location */
1648 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001649 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1650 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1651 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1653 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001654 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 }
Dave Allison70202782013-10-22 17:52:19 -07001656 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001657 if (Is64BitInstructionSet(cu_->instruction_set)) {
1658 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1659 } else {
1660 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001663 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 else
1665 rl_result = GetReturnAlt();
1666 }
1667 StoreValue(rl_dest, rl_result);
1668 }
1669}
1670
1671/*
1672 * The following are the first-level codegen routines that analyze the format
1673 * of each bytecode then either dispatch special purpose codegen routines
1674 * or produce corresponding Thumb instructions directly.
1675 */
1676
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001678static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 x &= x - 1;
1680 return (x & (x - 1)) == 0;
1681}
1682
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1684// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001685bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001686 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1688 return false;
1689 }
1690 // No divide instruction for Arm, so check for more special cases
1691 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001692 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 }
1694 int k = LowestSetBit(lit);
1695 if (k >= 30) {
1696 // Avoid special cases.
1697 return false;
1698 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 rl_src = LoadValue(rl_src, kCoreReg);
1700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001701 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001702 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 if (lit == 2) {
1704 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1706 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1707 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001709 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001711 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1712 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 }
1714 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001715 RegStorage t_reg1 = AllocTemp();
1716 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1719 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001721 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001723 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001727 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 }
1729 }
1730 StoreValue(rl_dest, rl_result);
1731 return true;
1732}
1733
1734// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1735// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001736bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001737 if (lit < 0) {
1738 return false;
1739 }
1740 if (lit == 0) {
1741 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1742 LoadConstant(rl_result.reg, 0);
1743 StoreValue(rl_dest, rl_result);
1744 return true;
1745 }
1746 if (lit == 1) {
1747 rl_src = LoadValue(rl_src, kCoreReg);
1748 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1749 OpRegCopy(rl_result.reg, rl_src.reg);
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001753 // There is RegRegRegShift on Arm, so check for more special cases
1754 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001755 return EasyMultiply(rl_src, rl_dest, lit);
1756 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 // Can we simplify this multiplication?
1758 bool power_of_two = false;
1759 bool pop_count_le2 = false;
1760 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001761 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 power_of_two = true;
1763 } else if (IsPopCountLE2(lit)) {
1764 pop_count_le2 = true;
1765 } else if (IsPowerOfTwo(lit + 1)) {
1766 power_of_two_minus_one = true;
1767 } else {
1768 return false;
1769 }
1770 rl_src = LoadValue(rl_src, kCoreReg);
1771 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1772 if (power_of_two) {
1773 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001774 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001775 } else if (pop_count_le2) {
1776 // Shift and add and shift.
1777 int first_bit = LowestSetBit(lit);
1778 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1779 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1780 } else {
1781 // Reverse subtract: (src << (shift + 1)) - src.
1782 DCHECK(power_of_two_minus_one);
1783 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001784 RegStorage t_reg = AllocTemp();
1785 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1786 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 }
1788 StoreValue(rl_dest, rl_result);
1789 return true;
1790}
1791
1792void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001793 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 RegLocation rl_result;
1795 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1796 int shift_op = false;
1797 bool is_div = false;
1798
1799 switch (opcode) {
1800 case Instruction::RSUB_INT_LIT8:
1801 case Instruction::RSUB_INT: {
1802 rl_src = LoadValue(rl_src, kCoreReg);
1803 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1804 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001805 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001807 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1808 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001809 }
1810 StoreValue(rl_dest, rl_result);
1811 return;
1812 }
1813
1814 case Instruction::SUB_INT:
1815 case Instruction::SUB_INT_2ADDR:
1816 lit = -lit;
1817 // Intended fallthrough
1818 case Instruction::ADD_INT:
1819 case Instruction::ADD_INT_2ADDR:
1820 case Instruction::ADD_INT_LIT8:
1821 case Instruction::ADD_INT_LIT16:
1822 op = kOpAdd;
1823 break;
1824 case Instruction::MUL_INT:
1825 case Instruction::MUL_INT_2ADDR:
1826 case Instruction::MUL_INT_LIT8:
1827 case Instruction::MUL_INT_LIT16: {
1828 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1829 return;
1830 }
1831 op = kOpMul;
1832 break;
1833 }
1834 case Instruction::AND_INT:
1835 case Instruction::AND_INT_2ADDR:
1836 case Instruction::AND_INT_LIT8:
1837 case Instruction::AND_INT_LIT16:
1838 op = kOpAnd;
1839 break;
1840 case Instruction::OR_INT:
1841 case Instruction::OR_INT_2ADDR:
1842 case Instruction::OR_INT_LIT8:
1843 case Instruction::OR_INT_LIT16:
1844 op = kOpOr;
1845 break;
1846 case Instruction::XOR_INT:
1847 case Instruction::XOR_INT_2ADDR:
1848 case Instruction::XOR_INT_LIT8:
1849 case Instruction::XOR_INT_LIT16:
1850 op = kOpXor;
1851 break;
1852 case Instruction::SHL_INT_LIT8:
1853 case Instruction::SHL_INT:
1854 case Instruction::SHL_INT_2ADDR:
1855 lit &= 31;
1856 shift_op = true;
1857 op = kOpLsl;
1858 break;
1859 case Instruction::SHR_INT_LIT8:
1860 case Instruction::SHR_INT:
1861 case Instruction::SHR_INT_2ADDR:
1862 lit &= 31;
1863 shift_op = true;
1864 op = kOpAsr;
1865 break;
1866 case Instruction::USHR_INT_LIT8:
1867 case Instruction::USHR_INT:
1868 case Instruction::USHR_INT_2ADDR:
1869 lit &= 31;
1870 shift_op = true;
1871 op = kOpLsr;
1872 break;
1873
1874 case Instruction::DIV_INT:
1875 case Instruction::DIV_INT_2ADDR:
1876 case Instruction::DIV_INT_LIT8:
1877 case Instruction::DIV_INT_LIT16:
1878 case Instruction::REM_INT:
1879 case Instruction::REM_INT_2ADDR:
1880 case Instruction::REM_INT_LIT8:
1881 case Instruction::REM_INT_LIT16: {
1882 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001883 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001884 return;
1885 }
buzbee11b63d12013-08-27 07:34:17 -07001886 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001888 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001889 (opcode == Instruction::DIV_INT_LIT16)) {
1890 is_div = true;
1891 } else {
1892 is_div = false;
1893 }
buzbee11b63d12013-08-27 07:34:17 -07001894 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1895 return;
1896 }
Dave Allison70202782013-10-22 17:52:19 -07001897
1898 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001899 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001901 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001902 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001903 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001904 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1905 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001906 } else if (cu_->instruction_set == kThumb2) {
1907 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1908 // Use ARM SDIV instruction for division. For remainder we also need to
1909 // calculate using a MUL and subtract.
1910 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001911 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001912 done = true;
1913 }
1914 }
1915
1916 if (!done) {
1917 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001918 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1919 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001920 if (Is64BitInstructionSet(cu_->instruction_set)) {
1921 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1922 false);
1923 } else {
1924 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1925 false);
1926 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001928 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 else
1930 rl_result = GetReturnAlt();
1931 }
1932 StoreValue(rl_dest, rl_result);
1933 return;
1934 }
1935 default:
1936 LOG(FATAL) << "Unexpected opcode " << opcode;
1937 }
1938 rl_src = LoadValue(rl_src, kCoreReg);
1939 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001940 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001942 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001943 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001944 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 }
1946 StoreValue(rl_dest, rl_result);
1947}
1948
Andreas Gampe2f244e92014-05-08 03:35:25 -07001949template <size_t pointer_size>
1950static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1951 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 RegLocation rl_result;
1953 OpKind first_op = kOpBkpt;
1954 OpKind second_op = kOpBkpt;
1955 bool call_out = false;
1956 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001957 ThreadOffset<pointer_size> func_offset(-1);
1958 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959
1960 switch (opcode) {
1961 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001962 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001963 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1964 return;
1965 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001966 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1967 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001969 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001970 RegStorage t_reg = mir_to_lir->AllocTemp();
1971 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1972 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1973 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1974 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001976 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1977 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001979 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 return;
1981 case Instruction::ADD_LONG:
1982 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001983 if (cu->instruction_set != kThumb2) {
1984 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 return;
1986 }
1987 first_op = kOpAdd;
1988 second_op = kOpAdc;
1989 break;
1990 case Instruction::SUB_LONG:
1991 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001992 if (cu->instruction_set != kThumb2) {
1993 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 return;
1995 }
1996 first_op = kOpSub;
1997 second_op = kOpSbc;
1998 break;
1999 case Instruction::MUL_LONG:
2000 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002001 if (cu->instruction_set != kMips) {
2002 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002003 return;
2004 } else {
2005 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002006 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2007 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008 }
2009 break;
2010 case Instruction::DIV_LONG:
2011 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002012 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002013 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2014 return;
2015 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 call_out = true;
2017 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002018 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2019 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 break;
2021 case Instruction::REM_LONG:
2022 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002023 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002024 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2025 return;
2026 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 call_out = true;
2028 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002029 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002031 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2032 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 break;
2034 case Instruction::AND_LONG_2ADDR:
2035 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002036 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2037 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002038 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 }
2040 first_op = kOpAnd;
2041 second_op = kOpAnd;
2042 break;
2043 case Instruction::OR_LONG:
2044 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002045 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2046 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002047 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 return;
2049 }
2050 first_op = kOpOr;
2051 second_op = kOpOr;
2052 break;
2053 case Instruction::XOR_LONG:
2054 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002055 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2056 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002057 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 return;
2059 }
2060 first_op = kOpXor;
2061 second_op = kOpXor;
2062 break;
2063 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002064 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002065 return;
2066 }
2067 default:
2068 LOG(FATAL) << "Invalid long arith op";
2069 }
2070 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002073 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002075 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2076 mir_to_lir->TargetReg(kArg1));
2077 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2078 mir_to_lir->TargetReg(kArg3));
2079 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2080 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2081 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2082 mir_to_lir->TargetReg(kArg3)));
2083 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002085 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002086 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002087 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 }
2089 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002090 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002091 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002093 rl_result = mir_to_lir->GetReturnWideAlt();
2094 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 }
2096}
2097
Andreas Gampe2f244e92014-05-08 03:35:25 -07002098void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2099 RegLocation rl_src1, RegLocation rl_src2) {
2100 if (Is64BitInstructionSet(cu_->instruction_set)) {
2101 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2102 } else {
2103 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2104 }
2105}
2106
Mark Mendelle87f9b52014-04-30 14:13:18 -04002107void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2108 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2109 LoadConstantNoClobber(rl_result.reg, value);
2110 StoreValue(rl_dest, rl_result);
2111 if (value == 0) {
2112 Workaround7250540(rl_dest, rl_result.reg);
2113 }
2114}
2115
Andreas Gampe2f244e92014-05-08 03:35:25 -07002116template <size_t pointer_size>
2117void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002118 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002119 /*
2120 * Don't optimize the register usage since it calls out to support
2121 * functions
2122 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002123 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2124
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002126 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2127 if (rl_dest.wide) {
2128 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002129 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002130 StoreValueWide(rl_dest, rl_result);
2131 } else {
2132 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002133 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002134 StoreValue(rl_dest, rl_result);
2135 }
2136}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002137template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2138 RegLocation rl_dest, RegLocation rl_src);
2139template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2140 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002141
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002142class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2143 public:
2144 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2145 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2146 }
2147
2148 void Compile() OVERRIDE {
2149 m2l_->ResetRegPool();
2150 m2l_->ResetDefTracking();
2151 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002152 if (Is64BitInstructionSet(cu_->instruction_set)) {
2153 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2154 } else {
2155 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2156 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002157 if (cont_ != nullptr) {
2158 m2l_->OpUnconditionalBranch(cont_);
2159 }
2160 }
2161};
2162
Brian Carlstrom7940e442013-07-12 13:46:57 -07002163/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002164void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002165 if (Runtime::Current()->ExplicitSuspendChecks()) {
2166 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2167 return;
2168 }
2169 FlushAllRegs();
2170 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002171 LIR* cont = NewLIR0(kPseudoTargetLabel);
2172 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002173 } else {
2174 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2175 return;
2176 }
2177 FlushAllRegs(); // TODO: needed?
2178 LIR* inst = CheckSuspendUsingLoad();
2179 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002180 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002181}
2182
2183/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002184void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002185 if (Runtime::Current()->ExplicitSuspendChecks()) {
2186 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2187 OpUnconditionalBranch(target);
2188 return;
2189 }
2190 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002191 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002192 LIR* branch = OpUnconditionalBranch(nullptr);
2193 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002194 } else {
2195 // For the implicit suspend check, just perform the trigger
2196 // load and branch to the target.
2197 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2198 OpUnconditionalBranch(target);
2199 return;
2200 }
2201 FlushAllRegs();
2202 LIR* inst = CheckSuspendUsingLoad();
2203 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002204 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002205 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002206}
2207
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002208/* Call out to helper assembly routine that will null check obj and then lock it. */
2209void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2210 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002211 if (Is64BitInstructionSet(cu_->instruction_set)) {
2212 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2213 } else {
2214 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2215 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002216}
2217
2218/* Call out to helper assembly routine that will null check obj and then unlock it. */
2219void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2220 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002221 if (Is64BitInstructionSet(cu_->instruction_set)) {
2222 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2223 } else {
2224 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2225 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002226}
2227
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002228/* Generic code for generating a wide constant into a VR. */
2229void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2230 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002231 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002232 StoreValueWide(rl_dest, rl_result);
2233}
2234
Brian Carlstrom7940e442013-07-12 13:46:57 -07002235} // namespace art