blob: b00cbeb61d0d17e37d26e464cb899fa2fb8e918d [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);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010047 barrier->u.m.def_mask = &kEncodeAll;
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);
buzbee33ae5582014-06-12 14:56:32 -070076 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070077 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);
buzbee33ae5582014-06-12 14:56:32 -070099 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700100 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_);
buzbee33ae5582014-06-12 14:56:32 -0700132 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700133 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);
buzbee33ae5582014-06-12 14:56:32 -0700161 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700162 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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700176 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700191 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800192 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
193 return;
194 }
195 MarkSafepointPC(last_lir_insn_);
196 }
197}
198
Andreas Gampe3c12c512014-06-24 18:46:29 +0000199void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
200 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
201 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
202 return;
203 }
204 MarkSafepointPCAfter(after);
205 }
206}
207
Dave Allisonb373e092014-02-20 16:06:36 -0800208void Mir2Lir::MarkPossibleStackOverflowException() {
Andreas Gampe5655e842014-06-17 16:36:07 -0700209 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800210 MarkSafepointPC(last_lir_insn_);
211 }
212}
213
buzbee2700f7e2014-03-07 09:46:20 -0800214void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700215 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800216 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
217 return;
218 }
219 // Force an implicit null check by performing a memory operation (load) from the given
220 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800221 RegStorage tmp = AllocTemp();
222 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700223 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800224 FreeTemp(tmp);
225 MarkSafepointPC(load);
226 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227}
228
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
230 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700231 LIR* fall_through) {
buzbeea0cd2d72014-06-01 09:33:49 -0700232 DCHECK(!rl_src1.fp);
233 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 ConditionCode cond;
235 switch (opcode) {
236 case Instruction::IF_EQ:
237 cond = kCondEq;
238 break;
239 case Instruction::IF_NE:
240 cond = kCondNe;
241 break;
242 case Instruction::IF_LT:
243 cond = kCondLt;
244 break;
245 case Instruction::IF_GE:
246 cond = kCondGe;
247 break;
248 case Instruction::IF_GT:
249 cond = kCondGt;
250 break;
251 case Instruction::IF_LE:
252 cond = kCondLe;
253 break;
254 default:
255 cond = static_cast<ConditionCode>(0);
256 LOG(FATAL) << "Unexpected opcode " << opcode;
257 }
258
259 // Normalize such that if either operand is constant, src2 will be constant
260 if (rl_src1.is_const) {
261 RegLocation rl_temp = rl_src1;
262 rl_src1 = rl_src2;
263 rl_src2 = rl_temp;
264 cond = FlipComparisonOrder(cond);
265 }
266
buzbeea0cd2d72014-06-01 09:33:49 -0700267 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 // Is this really an immediate comparison?
269 if (rl_src2.is_const) {
270 // If it's already live in a register or not easily materialized, just keep going
271 RegLocation rl_temp = UpdateLoc(rl_src2);
272 if ((rl_temp.location == kLocDalvikFrame) &&
273 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
274 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800275 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 return;
277 }
278 }
buzbeea0cd2d72014-06-01 09:33:49 -0700279 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800280 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281}
282
283void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700284 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700286 DCHECK(!rl_src.fp);
287 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 switch (opcode) {
289 case Instruction::IF_EQZ:
290 cond = kCondEq;
291 break;
292 case Instruction::IF_NEZ:
293 cond = kCondNe;
294 break;
295 case Instruction::IF_LTZ:
296 cond = kCondLt;
297 break;
298 case Instruction::IF_GEZ:
299 cond = kCondGe;
300 break;
301 case Instruction::IF_GTZ:
302 cond = kCondGt;
303 break;
304 case Instruction::IF_LEZ:
305 cond = kCondLe;
306 break;
307 default:
308 cond = static_cast<ConditionCode>(0);
309 LOG(FATAL) << "Unexpected opcode " << opcode;
310 }
buzbee2700f7e2014-03-07 09:46:20 -0800311 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312}
313
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700314void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
316 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800317 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800319 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 }
buzbee2700f7e2014-03-07 09:46:20 -0800321 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 StoreValueWide(rl_dest, rl_result);
323}
324
325void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700326 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700327 rl_src = LoadValue(rl_src, kCoreReg);
328 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
329 OpKind op = kOpInvalid;
330 switch (opcode) {
331 case Instruction::INT_TO_BYTE:
332 op = kOp2Byte;
333 break;
334 case Instruction::INT_TO_SHORT:
335 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700337 case Instruction::INT_TO_CHAR:
338 op = kOp2Char;
339 break;
340 default:
341 LOG(ERROR) << "Bad int conversion type";
342 }
buzbee2700f7e2014-03-07 09:46:20 -0800343 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700344 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345}
346
Andreas Gampe2f244e92014-05-08 03:35:25 -0700347template <size_t pointer_size>
348static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
349 uint32_t type_idx, RegLocation rl_dest,
350 RegLocation rl_src) {
351 mir_to_lir->FlushAllRegs(); /* Everything to home location */
352 ThreadOffset<pointer_size> func_offset(-1);
353 const DexFile* dex_file = cu->dex_file;
354 CompilerDriver* driver = cu->compiler_driver;
355 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
356 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800357 bool is_type_initialized; // Ignored as an array does not have an initializer.
358 bool use_direct_type_ptr;
359 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700360 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700362 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
363 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800364 // The fast path.
365 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700366 mir_to_lir->LoadClassType(type_idx, kArg0);
367 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
368 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0),
369 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800370 } else {
371 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700372 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
373 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
374 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800375 }
376 } else {
377 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700378 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
379 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800380 }
381 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700383 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
384 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 }
buzbeea0cd2d72014-06-01 09:33:49 -0700386 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700387 mir_to_lir->StoreValue(rl_dest, rl_result);
388}
389
390/*
391 * Let helper function take care of everything. Will call
392 * Array::AllocFromCode(type_idx, method, count);
393 * Note: AllocFromCode will handle checks for errNegativeArraySize.
394 */
395void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
396 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700397 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700398 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
399 } else {
400 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
401 }
402}
403
404template <size_t pointer_size>
405static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
406 ThreadOffset<pointer_size> func_offset(-1);
407 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
408 type_idx)) {
409 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
410 } else {
411 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
412 }
413 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414}
415
416/*
417 * Similar to GenNewArray, but with post-allocation initialization.
418 * Verifier guarantees we're dealing with an array class. Current
419 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
420 * Current code also throws internal unimp if not 'L', '[' or 'I'.
421 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700422void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 int elems = info->num_arg_words;
424 int type_idx = info->index;
425 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700426 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700427 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700429 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 FreeTemp(TargetReg(kArg2));
432 FreeTemp(TargetReg(kArg1));
433 /*
434 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
435 * return region. Because AllocFromCode placed the new array
436 * in kRet0, we'll just lock it into place. When debugger support is
437 * added, it may be necessary to additionally copy all return
438 * values to a home location in thread-local storage
439 */
440 LockTemp(TargetReg(kRet0));
441
442 // TODO: use the correct component size, currently all supported types
443 // share array alignment with ints (see comment at head of function)
444 size_t component_size = sizeof(int32_t);
445
446 // Having a range of 0 is legal
447 if (info->is_range && (elems > 0)) {
448 /*
449 * Bit of ugliness here. We're going generate a mem copy loop
450 * on the register range, but it is possible that some regs
451 * in the range have been promoted. This is unlikely, but
452 * before generating the copy, we'll just force a flush
453 * of any regs in the source range that have been promoted to
454 * home location.
455 */
456 for (int i = 0; i < elems; i++) {
457 RegLocation loc = UpdateLoc(info->args[i]);
458 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100459 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee695d13a2014-04-19 13:32:20 -0700460 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 }
462 }
463 /*
464 * TUNING note: generated code here could be much improved, but
465 * this is an uncommon operation and isn't especially performance
466 * critical.
467 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700468 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700469 RegStorage r_src = AllocTempRef();
470 RegStorage r_dst = AllocTempRef();
471 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800472 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700473 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700475 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 r_val = TargetReg(kLr);
477 break;
478 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700479 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 FreeTemp(TargetReg(kRet0));
481 r_val = AllocTemp();
482 break;
483 case kMips:
484 r_val = AllocTemp();
485 break;
486 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
487 }
488 // Set up source pointer
489 RegLocation rl_first = info->args[0];
490 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
491 // Set up the target pointer
492 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
493 mirror::Array::DataOffset(component_size).Int32Value());
494 // Set up the loop counter (known to be > 0)
495 LoadConstant(r_idx, elems - 1);
496 // Generate the copy loop. Going backwards for convenience
497 LIR* target = NewLIR0(kPseudoTargetLabel);
498 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100499 {
500 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
501 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
502 // NOTE: No dalvik register annotation, local optimizations will be stopped
503 // by the loop boundaries.
504 }
buzbee695d13a2014-04-19 13:32:20 -0700505 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 FreeTemp(r_val);
507 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700508 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 // Restore the target pointer
510 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
511 -mirror::Array::DataOffset(component_size).Int32Value());
512 }
513 } else if (!info->is_range) {
514 // TUNING: interleave
515 for (int i = 0; i < elems; i++) {
516 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700517 Store32Disp(TargetReg(kRet0),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000518 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800520 if (IsTemp(rl_arg.reg)) {
521 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 }
523 }
524 }
525 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700526 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 }
528}
529
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800530//
531// Slow path to ensure a class is initialized for sget/sput.
532//
533class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
534 public:
buzbee2700f7e2014-03-07 09:46:20 -0800535 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
536 RegStorage r_base) :
537 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
538 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800539 }
540
541 void Compile() {
542 LIR* unresolved_target = GenerateTargetLabel();
543 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700544 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700545 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
546 storage_index_, true);
547 } else {
548 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
549 storage_index_, true);
550 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800551 // Copy helper's result into r_base, a no-op on all but MIPS.
552 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
553
554 m2l_->OpUnconditionalBranch(cont_);
555 }
556
557 private:
558 LIR* const uninit_;
559 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800560 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800561};
562
Andreas Gampe2f244e92014-05-08 03:35:25 -0700563template <size_t pointer_size>
564static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
565 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
566 ThreadOffset<pointer_size> setter_offset =
567 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
568 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
569 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
570 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
571 true);
572}
573
Vladimir Markobe0e5462014-02-26 11:24:15 +0000574void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700575 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000576 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
577 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100578 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
579 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
580 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000581 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800582 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000583 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100585 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700586 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000587 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
588 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800589 if (IsTemp(rl_method.reg)) {
590 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 }
592 } else {
593 // Medium path, static storage base in a different class which requires checks that the other
594 // class is initialized.
595 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000596 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 // May do runtime call so everything to home locations.
598 FlushAllRegs();
599 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800600 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 LockTemp(r_method);
602 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603 r_base = TargetReg(kArg0);
604 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000605 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
606 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000607 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000608 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000610 if (!field_info.IsInitialized() &&
611 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800612 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800613
614 // The slow path is invoked if the r_base is NULL or the class pointed
615 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800617 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800619 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800620 mirror::Class::StatusOffset().Int32Value(),
621 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800622 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800623
buzbee2700f7e2014-03-07 09:46:20 -0800624 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000625 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626
627 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700628 // Ensure load of status and load of value don't re-order.
629 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 FreeTemp(r_method);
632 }
633 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100634 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100636 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100638 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000640 if (is_object) {
641 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
642 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100643 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000644 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
645 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 }
647 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800648 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800650 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 } else {
652 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700653 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700654 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
655 } else {
656 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
657 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
659}
660
Andreas Gampe2f244e92014-05-08 03:35:25 -0700661template <size_t pointer_size>
662static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
663 const MirSFieldLoweringInfo* field_info) {
664 ThreadOffset<pointer_size> getter_offset =
665 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
666 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
667 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
668 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
669}
670
Vladimir Markobe0e5462014-02-26 11:24:15 +0000671void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700672 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000673 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
674 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100675 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
676 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
677 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000678 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800679 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000680 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 // Fast path, static storage base is this method's class
682 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700683 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000684 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
685 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 } else {
687 // Medium path, static storage base in a different class which requires checks that the other
688 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 // May do runtime call so everything to home locations.
691 FlushAllRegs();
692 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800693 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 LockTemp(r_method);
695 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800696 r_base = TargetReg(kArg0);
697 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000698 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
699 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000700 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000701 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800702 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000703 if (!field_info.IsInitialized() &&
704 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800705 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800706
707 // The slow path is invoked if the r_base is NULL or the class pointed
708 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800709 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800710 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800711 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800712 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800713 mirror::Class::StatusOffset().Int32Value(),
714 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800715 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800716
buzbee2700f7e2014-03-07 09:46:20 -0800717 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800719
720 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700721 // Ensure load of status and load of value don't re-order.
722 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 FreeTemp(r_method);
725 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800726 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100727 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
728 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800729
Vladimir Marko674744e2014-04-24 15:18:26 +0100730 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000731 if (is_object) {
732 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
733 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100734 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000735 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
736 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800737 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100738 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800739
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 if (is_long_or_double) {
741 StoreValueWide(rl_dest, rl_result);
742 } else {
743 StoreValue(rl_dest, rl_result);
744 }
745 } else {
746 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700747 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700748 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
749 } else {
750 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
751 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700753 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 StoreValueWide(rl_dest, rl_result);
755 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700756 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 StoreValue(rl_dest, rl_result);
758 }
759 }
760}
761
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800762// Generate code for all slow paths.
763void Mir2Lir::HandleSlowPaths() {
764 int n = slow_paths_.Size();
765 for (int i = 0; i < n; ++i) {
766 LIRSlowPath* slowpath = slow_paths_.Get(i);
767 slowpath->Compile();
768 }
769 slow_paths_.Reset();
770}
771
Andreas Gampe2f244e92014-05-08 03:35:25 -0700772template <size_t pointer_size>
773static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
774 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
775 ThreadOffset<pointer_size> getter_offset =
776 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
777 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
778 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
779 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
780 true);
781}
782
Vladimir Markobe0e5462014-02-26 11:24:15 +0000783void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700785 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000786 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
787 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100788 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
789 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
790 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
791 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000792 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700793 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100794 GenNullCheck(rl_obj.reg, opt_flags);
795 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
796 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000797 LIR* load_lir;
798 if (is_object) {
799 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
800 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100801 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000802 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
803 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100804 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000805 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 StoreValueWide(rl_dest, rl_result);
808 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 StoreValue(rl_dest, rl_result);
810 }
811 } else {
buzbee33ae5582014-06-12 14:56:32 -0700812 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700813 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
814 } else {
815 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
816 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700818 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 StoreValueWide(rl_dest, rl_result);
820 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700821 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 StoreValue(rl_dest, rl_result);
823 }
824 }
825}
826
Andreas Gampe2f244e92014-05-08 03:35:25 -0700827template <size_t pointer_size>
828static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
829 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
830 RegLocation rl_src) {
831 ThreadOffset<pointer_size> setter_offset =
832 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
833 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
834 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
835 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
836 rl_obj, rl_src, true);
837}
838
Vladimir Markobe0e5462014-02-26 11:24:15 +0000839void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700841 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000842 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
843 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100844 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
845 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
846 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
847 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000848 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700849 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100851 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 } else {
853 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100854 }
855 GenNullCheck(rl_obj.reg, opt_flags);
856 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000857 LIR* store;
858 if (is_object) {
859 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
860 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100861 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000862 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
863 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100864 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000865 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100866 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
867 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
869 } else {
buzbee33ae5582014-06-12 14:56:32 -0700870 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700871 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
872 } else {
873 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
874 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 }
876}
877
Andreas Gampe2f244e92014-05-08 03:35:25 -0700878template <size_t pointer_size>
879static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
880 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
881 ThreadOffset<pointer_size> helper = needs_range_check
882 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
883 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
884 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
885 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
886 true);
887}
888
Ian Rogersa9a82542013-10-04 11:17:26 -0700889void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
890 RegLocation rl_src) {
891 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
892 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
893 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700894 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700895 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
896 } else {
897 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
898 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700899}
900
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700901void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 RegLocation rl_method = LoadCurrMethod();
buzbee33ae5582014-06-12 14:56:32 -0700903 DCHECK(!cu_->target64 || rl_method.reg.Is64Bit());
904 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700905 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
907 *cu_->dex_file,
908 type_idx)) {
909 // Call out to helper which resolves type and verifies access.
910 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700911 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700912 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
913 type_idx, rl_method.reg, true);
914 } else {
915 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
916 type_idx, rl_method.reg, true);
917 }
buzbeea0cd2d72014-06-01 09:33:49 -0700918 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 StoreValue(rl_dest, rl_result);
920 } else {
921 // We're don't need access checks, load type from dex cache
922 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700923 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000924 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000925 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000926 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
928 type_idx) || SLOW_TYPE_PATH) {
929 // Slow path, at runtime test if type is null and if so initialize
930 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800931 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800932 LIR* cont = NewLIR0(kPseudoTargetLabel);
933
934 // Object to generate the slow path for class resolution.
935 class SlowPath : public LIRSlowPath {
936 public:
937 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
938 const RegLocation& rl_method, const RegLocation& rl_result) :
939 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
940 rl_method_(rl_method), rl_result_(rl_result) {
941 }
942
943 void Compile() {
944 GenerateTargetLabel();
945
buzbee33ae5582014-06-12 14:56:32 -0700946 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700947 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
948 rl_method_.reg, true);
949 } else {
950 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
951 rl_method_.reg, true);
952 }
buzbee2700f7e2014-03-07 09:46:20 -0800953 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954
955 m2l_->OpUnconditionalBranch(cont_);
956 }
957
958 private:
959 const int type_idx_;
960 const RegLocation rl_method_;
961 const RegLocation rl_result_;
962 };
963
964 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800965 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800966
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800968 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 // Fast path, we're done - just store result
970 StoreValue(rl_dest, rl_result);
971 }
972 }
973}
974
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700975void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000977 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
978 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
980 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
981 // slow path, resolve string if not in dex cache
982 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700983 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800984
985 // If the Method* is already in a register, we can save a copy.
986 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800987 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800988 if (rl_method.location == kLocPhysReg) {
989 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800990 DCHECK(!IsTemp(rl_method.reg));
991 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800992 } else {
993 r_method = TargetReg(kArg2);
994 LoadCurrMethodDirect(r_method);
995 }
buzbee695d13a2014-04-19 13:32:20 -0700996 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000997 TargetReg(kArg0), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800998
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampe3c12c512014-06-24 18:46:29 +00001000 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0), kNotVolatile);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001001 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
1002 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001003
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001004 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001005 // Object to generate the slow path for string resolution.
1006 class SlowPath : public LIRSlowPath {
1007 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001008 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1009 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1010 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001011 }
1012
1013 void Compile() {
1014 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001015 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001016 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1017 r_method_, string_idx_, true);
1018 } else {
1019 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1020 r_method_, string_idx_, true);
1021 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001022 m2l_->OpUnconditionalBranch(cont_);
1023 }
1024
1025 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001026 const RegStorage r_method_;
1027 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001028 };
1029
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001030 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001032
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001034 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 } else {
1036 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001037 RegStorage res_reg = AllocTempRef();
1038 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001039 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1040 kNotVolatile);
1041 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 StoreValue(rl_dest, rl_result);
1043 }
1044}
1045
Andreas Gampe2f244e92014-05-08 03:35:25 -07001046template <size_t pointer_size>
1047static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1048 RegLocation rl_dest) {
1049 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 // alloc will always check for resolution, do we also need to verify
1051 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001052 ThreadOffset<pointer_size> func_offset(-1);
1053 const DexFile* dex_file = cu->dex_file;
1054 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001055 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001056 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001057 bool is_type_initialized;
1058 bool use_direct_type_ptr;
1059 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001060 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001062 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1063 &direct_type_ptr, &is_finalizable) &&
1064 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 // The fast path.
1066 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001067 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001069 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1070 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001071 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001072 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1073 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001074 }
1075 } else {
1076 // Use the direct pointer.
1077 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001078 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1079 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001080 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001081 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1082 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001083 }
1084 }
1085 } else {
1086 // The slow path.
1087 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001088 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1089 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001090 }
1091 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001093 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1094 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 }
buzbeea0cd2d72014-06-01 09:33:49 -07001096 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001097 mir_to_lir->StoreValue(rl_dest, rl_result);
1098}
1099
1100/*
1101 * Let helper function take care of everything. Will
1102 * call Class::NewInstanceFromCode(type_idx, method);
1103 */
1104void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001105 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001106 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1107 } else {
1108 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1109 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110}
1111
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001112void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001114 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001115 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1116 } else {
1117 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119}
1120
1121// For final classes there are no sub-classes to check and so we can answer the instance-of
1122// question with simple comparisons.
1123void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1124 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001125 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001126 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001127
buzbeea0cd2d72014-06-01 09:33:49 -07001128 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001130 RegStorage result_reg = rl_result.reg;
1131 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 result_reg = AllocTypedTemp(false, kCoreReg);
1133 }
1134 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001135 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136
buzbeea0cd2d72014-06-01 09:33:49 -07001137 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1138 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139
1140 LoadCurrMethodDirect(check_class);
1141 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001142 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1143 kNotVolatile);
1144 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1145 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 } else {
buzbee695d13a2014-04-19 13:32:20 -07001147 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001148 check_class, kNotVolatile);
1149 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1150 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001151 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001152 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 }
1154
1155 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001156 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 if (cu_->instruction_set == kThumb2) {
1158 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001159 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001161 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 } else {
1163 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1164 LoadConstant(result_reg, 1); // eq case - load true
1165 }
1166 LIR* target = NewLIR0(kPseudoTargetLabel);
1167 null_branchover->target = target;
1168 if (ne_branchover != NULL) {
1169 ne_branchover->target = target;
1170 }
1171 FreeTemp(object_class);
1172 FreeTemp(check_class);
1173 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001174 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 FreeTemp(result_reg);
1176 }
1177 StoreValue(rl_dest, rl_result);
1178}
1179
1180void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1181 bool type_known_abstract, bool use_declaring_class,
1182 bool can_assume_type_is_in_dex_cache,
1183 uint32_t type_idx, RegLocation rl_dest,
1184 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001185 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001186 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001187
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 FlushAllRegs();
1189 // May generate a call - use explicit registers
1190 LockCallTemps();
1191 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001192 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 if (needs_access_check) {
1194 // Check we have access to type_idx and if not throw IllegalAccessError,
1195 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001196 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001197 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1198 type_idx, true);
1199 } else {
1200 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1201 type_idx, true);
1202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1204 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1205 } else if (use_declaring_class) {
1206 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001207 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001208 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 } else {
1210 // Load dex cache entry into class_reg (kArg2)
1211 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001212 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001213 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001214 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001215 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 if (!can_assume_type_is_in_dex_cache) {
1217 // Need to test presence of type in dex cache at runtime
1218 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1219 // Not resolved
1220 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001221 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001222 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1223 } else {
1224 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1225 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001226 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1228 // Rejoin code paths
1229 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1230 hop_branch->target = hop_target;
1231 }
1232 }
1233 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001234 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 if (cu_->instruction_set == kMips) {
1236 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001237 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
1239 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1240
1241 /* load object->klass_ */
1242 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001243 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1),
1244 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1246 LIR* branchover = NULL;
1247 if (type_known_final) {
1248 // rl_result == ref == null == 0.
1249 if (cu_->instruction_set == kThumb2) {
1250 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001251 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001252 LoadConstant(rl_result.reg, 1); // .eq case - load true
1253 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001254 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001256 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001258 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
1260 } else {
1261 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001262 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001263 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1264 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001265 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 if (!type_known_abstract) {
1267 /* Uses conditional nullification */
1268 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001269 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1271 }
1272 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1273 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001274 if (it != nullptr) {
1275 OpEndIT(it);
1276 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 FreeTemp(r_tgt);
1278 } else {
1279 if (!type_known_abstract) {
1280 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001281 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1283 }
buzbee33ae5582014-06-12 14:56:32 -07001284 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001285 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1286 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001287 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1288 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1289 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 }
1291 }
1292 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001293 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 /* branch targets here */
1295 LIR* target = NewLIR0(kPseudoTargetLabel);
1296 StoreValue(rl_dest, rl_result);
1297 branch1->target = target;
1298 if (branchover != NULL) {
1299 branchover->target = target;
1300 }
1301}
1302
1303void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1304 bool type_known_final, type_known_abstract, use_declaring_class;
1305 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1306 *cu_->dex_file,
1307 type_idx,
1308 &type_known_final,
1309 &type_known_abstract,
1310 &use_declaring_class);
1311 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1312 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1313
1314 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1315 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1316 } else {
1317 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1318 use_declaring_class, can_assume_type_is_in_dex_cache,
1319 type_idx, rl_dest, rl_src);
1320 }
1321}
1322
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001323void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 bool type_known_final, type_known_abstract, use_declaring_class;
1325 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1326 *cu_->dex_file,
1327 type_idx,
1328 &type_known_final,
1329 &type_known_abstract,
1330 &use_declaring_class);
1331 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1332 // of the exception throw path.
1333 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001334 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 // Verifier type analysis proved this check cast would never cause an exception.
1336 return;
1337 }
1338 FlushAllRegs();
1339 // May generate a call - use explicit registers
1340 LockCallTemps();
1341 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001342 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 if (needs_access_check) {
1344 // Check we have access to type_idx and if not throw IllegalAccessError,
1345 // returns Class* in kRet0
1346 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001347 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001348 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1349 type_idx, TargetReg(kArg1), true);
1350 } else {
1351 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1352 type_idx, TargetReg(kArg1), true);
1353 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1355 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001356 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001357 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 } else {
1359 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001360 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001361 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001362 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001363 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1365 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001366 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1367 LIR* cont = NewLIR0(kPseudoTargetLabel);
1368
1369 // Slow path to initialize the type. Executed if the type is NULL.
1370 class SlowPath : public LIRSlowPath {
1371 public:
1372 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001373 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001374 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1375 class_reg_(class_reg) {
1376 }
1377
1378 void Compile() {
1379 GenerateTargetLabel();
1380
1381 // Call out to helper, which will return resolved type in kArg0
1382 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001383 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001384 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1385 m2l_->TargetReg(kArg1), true);
1386 } else {
1387 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1388 m2l_->TargetReg(kArg1), true);
1389 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001390 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1391 m2l_->OpUnconditionalBranch(cont_);
1392 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001393
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001394 public:
1395 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001396 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001397 };
1398
buzbee2700f7e2014-03-07 09:46:20 -08001399 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 }
1401 }
1402 // At this point, class_reg (kArg2) has class
1403 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001404
1405 // Slow path for the case where the classes are not equal. In this case we need
1406 // to call a helper function to do the check.
1407 class SlowPath : public LIRSlowPath {
1408 public:
1409 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1410 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1411 }
1412
1413 void Compile() {
1414 GenerateTargetLabel();
1415
1416 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001417 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001418 m2l_->TargetReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001419 }
buzbee33ae5582014-06-12 14:56:32 -07001420 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001421 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1422 m2l_->TargetReg(kArg1), true);
1423 } else {
1424 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1425 m2l_->TargetReg(kArg1), true);
1426 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001427
1428 m2l_->OpUnconditionalBranch(cont_);
1429 }
1430
1431 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001432 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001433 };
1434
1435 if (type_known_abstract) {
1436 // Easier case, run slow path if target is non-null (slow path will load from target)
1437 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1438 LIR* cont = NewLIR0(kPseudoTargetLabel);
1439 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1440 } else {
1441 // Harder, more common case. We need to generate a forward branch over the load
1442 // if the target is null. If it's non-null we perform the load and branch to the
1443 // slow path if the classes are not equal.
1444
1445 /* Null is OK - continue */
1446 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1447 /* load object->klass_ */
1448 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001449 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1),
1450 kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001451
1452 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1453 LIR* cont = NewLIR0(kPseudoTargetLabel);
1454
1455 // Add the slow path that will not perform load since this is already done.
1456 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1457
1458 // Set the null check to branch to the continuation.
1459 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 }
1461}
1462
1463void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001464 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 RegLocation rl_result;
1466 if (cu_->instruction_set == kThumb2) {
1467 /*
1468 * NOTE: This is the one place in the code in which we might have
1469 * as many as six live temporary registers. There are 5 in the normal
1470 * set for Arm. Until we have spill capabilities, temporarily add
1471 * lr to the temp set. It is safe to do this locally, but note that
1472 * lr is used explicitly elsewhere in the code generator and cannot
1473 * normally be used as a general temp register.
1474 */
1475 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1476 FreeTemp(TargetReg(kLr)); // and make it available
1477 }
1478 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1479 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1480 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1481 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001482 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1483 RegStorage t_reg = AllocTemp();
1484 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1485 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1486 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 FreeTemp(t_reg);
1488 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001489 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1490 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 }
1492 /*
1493 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1494 * following StoreValueWide might need to allocate a temp register.
1495 * To further work around the lack of a spill capability, explicitly
1496 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1497 * Remove when spill is functional.
1498 */
1499 FreeRegLocTemps(rl_result, rl_src1);
1500 FreeRegLocTemps(rl_result, rl_src2);
1501 StoreValueWide(rl_dest, rl_result);
1502 if (cu_->instruction_set == kThumb2) {
1503 Clobber(TargetReg(kLr));
1504 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1505 }
1506}
1507
1508
Andreas Gampe2f244e92014-05-08 03:35:25 -07001509template <size_t pointer_size>
1510static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1511 RegLocation rl_shift) {
1512 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513
1514 switch (opcode) {
1515 case Instruction::SHL_LONG:
1516 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001517 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 break;
1519 case Instruction::SHR_LONG:
1520 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001521 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 break;
1523 case Instruction::USHR_LONG:
1524 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001525 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 break;
1527 default:
1528 LOG(FATAL) << "Unexpected case";
1529 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001530 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1531 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1532}
1533
1534void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1535 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001536 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001537 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1538 } else {
1539 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1540 }
buzbeea0cd2d72014-06-01 09:33:49 -07001541 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 StoreValueWide(rl_dest, rl_result);
1543}
1544
1545
1546void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001547 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001548 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 OpKind op = kOpBkpt;
1550 bool is_div_rem = false;
1551 bool check_zero = false;
1552 bool unary = false;
1553 RegLocation rl_result;
1554 bool shift_op = false;
1555 switch (opcode) {
1556 case Instruction::NEG_INT:
1557 op = kOpNeg;
1558 unary = true;
1559 break;
1560 case Instruction::NOT_INT:
1561 op = kOpMvn;
1562 unary = true;
1563 break;
1564 case Instruction::ADD_INT:
1565 case Instruction::ADD_INT_2ADDR:
1566 op = kOpAdd;
1567 break;
1568 case Instruction::SUB_INT:
1569 case Instruction::SUB_INT_2ADDR:
1570 op = kOpSub;
1571 break;
1572 case Instruction::MUL_INT:
1573 case Instruction::MUL_INT_2ADDR:
1574 op = kOpMul;
1575 break;
1576 case Instruction::DIV_INT:
1577 case Instruction::DIV_INT_2ADDR:
1578 check_zero = true;
1579 op = kOpDiv;
1580 is_div_rem = true;
1581 break;
1582 /* NOTE: returns in kArg1 */
1583 case Instruction::REM_INT:
1584 case Instruction::REM_INT_2ADDR:
1585 check_zero = true;
1586 op = kOpRem;
1587 is_div_rem = true;
1588 break;
1589 case Instruction::AND_INT:
1590 case Instruction::AND_INT_2ADDR:
1591 op = kOpAnd;
1592 break;
1593 case Instruction::OR_INT:
1594 case Instruction::OR_INT_2ADDR:
1595 op = kOpOr;
1596 break;
1597 case Instruction::XOR_INT:
1598 case Instruction::XOR_INT_2ADDR:
1599 op = kOpXor;
1600 break;
1601 case Instruction::SHL_INT:
1602 case Instruction::SHL_INT_2ADDR:
1603 shift_op = true;
1604 op = kOpLsl;
1605 break;
1606 case Instruction::SHR_INT:
1607 case Instruction::SHR_INT_2ADDR:
1608 shift_op = true;
1609 op = kOpAsr;
1610 break;
1611 case Instruction::USHR_INT:
1612 case Instruction::USHR_INT_2ADDR:
1613 shift_op = true;
1614 op = kOpLsr;
1615 break;
1616 default:
1617 LOG(FATAL) << "Invalid word arith op: " << opcode;
1618 }
1619 if (!is_div_rem) {
1620 if (unary) {
1621 rl_src1 = LoadValue(rl_src1, kCoreReg);
1622 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001623 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001625 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001626 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001627 RegStorage t_reg = AllocTemp();
1628 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 rl_src1 = LoadValue(rl_src1, kCoreReg);
1630 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 FreeTemp(t_reg);
1633 } else {
1634 rl_src1 = LoadValue(rl_src1, kCoreReg);
1635 rl_src2 = LoadValue(rl_src2, kCoreReg);
1636 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001637 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 }
1639 }
1640 StoreValue(rl_dest, rl_result);
1641 } else {
Dave Allison70202782013-10-22 17:52:19 -07001642 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 +01001643 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 rl_src1 = LoadValue(rl_src1, kCoreReg);
1645 rl_src2 = LoadValue(rl_src2, kCoreReg);
1646 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001647 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 }
buzbee2700f7e2014-03-07 09:46:20 -08001649 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001650 done = true;
1651 } else if (cu_->instruction_set == kThumb2) {
1652 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1653 // Use ARM SDIV instruction for division. For remainder we also need to
1654 // calculate using a MUL and subtract.
1655 rl_src1 = LoadValue(rl_src1, kCoreReg);
1656 rl_src2 = LoadValue(rl_src2, kCoreReg);
1657 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001658 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001659 }
buzbee2700f7e2014-03-07 09:46:20 -08001660 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001661 done = true;
1662 }
1663 }
1664
1665 // If we haven't already generated the code use the callout function.
1666 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 FlushAllRegs(); /* Send everything to home location */
1668 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee33ae5582014-06-12 14:56:32 -07001669 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001670 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1671 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1673 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001674 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 }
Dave Allison70202782013-10-22 17:52:19 -07001676 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001677 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001678 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1679 } else {
1680 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1681 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001683 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 else
1685 rl_result = GetReturnAlt();
1686 }
1687 StoreValue(rl_dest, rl_result);
1688 }
1689}
1690
1691/*
1692 * The following are the first-level codegen routines that analyze the format
1693 * of each bytecode then either dispatch special purpose codegen routines
1694 * or produce corresponding Thumb instructions directly.
1695 */
1696
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001698static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 x &= x - 1;
1700 return (x & (x - 1)) == 0;
1701}
1702
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1704// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001705bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001706 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1708 return false;
1709 }
1710 // No divide instruction for Arm, so check for more special cases
1711 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001712 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 }
1714 int k = LowestSetBit(lit);
1715 if (k >= 30) {
1716 // Avoid special cases.
1717 return false;
1718 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 rl_src = LoadValue(rl_src, kCoreReg);
1720 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001721 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001722 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 if (lit == 2) {
1724 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1726 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1727 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001729 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1732 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 }
1734 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001735 RegStorage t_reg1 = AllocTemp();
1736 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001737 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001738 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1739 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001741 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001742 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001743 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001745 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001747 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 }
1749 }
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752}
1753
1754// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1755// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001756bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001757 if (lit < 0) {
1758 return false;
1759 }
1760 if (lit == 0) {
1761 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1762 LoadConstant(rl_result.reg, 0);
1763 StoreValue(rl_dest, rl_result);
1764 return true;
1765 }
1766 if (lit == 1) {
1767 rl_src = LoadValue(rl_src, kCoreReg);
1768 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1769 OpRegCopy(rl_result.reg, rl_src.reg);
1770 StoreValue(rl_dest, rl_result);
1771 return true;
1772 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001773 // There is RegRegRegShift on Arm, so check for more special cases
1774 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001775 return EasyMultiply(rl_src, rl_dest, lit);
1776 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 // Can we simplify this multiplication?
1778 bool power_of_two = false;
1779 bool pop_count_le2 = false;
1780 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001781 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 power_of_two = true;
1783 } else if (IsPopCountLE2(lit)) {
1784 pop_count_le2 = true;
1785 } else if (IsPowerOfTwo(lit + 1)) {
1786 power_of_two_minus_one = true;
1787 } else {
1788 return false;
1789 }
1790 rl_src = LoadValue(rl_src, kCoreReg);
1791 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1792 if (power_of_two) {
1793 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001794 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 } else if (pop_count_le2) {
1796 // Shift and add and shift.
1797 int first_bit = LowestSetBit(lit);
1798 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1799 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1800 } else {
1801 // Reverse subtract: (src << (shift + 1)) - src.
1802 DCHECK(power_of_two_minus_one);
1803 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001804 RegStorage t_reg = AllocTemp();
1805 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1806 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001807 }
1808 StoreValue(rl_dest, rl_result);
1809 return true;
1810}
1811
1812void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001813 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 RegLocation rl_result;
1815 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1816 int shift_op = false;
1817 bool is_div = false;
1818
1819 switch (opcode) {
1820 case Instruction::RSUB_INT_LIT8:
1821 case Instruction::RSUB_INT: {
1822 rl_src = LoadValue(rl_src, kCoreReg);
1823 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1824 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001825 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001827 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1828 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 }
1830 StoreValue(rl_dest, rl_result);
1831 return;
1832 }
1833
1834 case Instruction::SUB_INT:
1835 case Instruction::SUB_INT_2ADDR:
1836 lit = -lit;
1837 // Intended fallthrough
1838 case Instruction::ADD_INT:
1839 case Instruction::ADD_INT_2ADDR:
1840 case Instruction::ADD_INT_LIT8:
1841 case Instruction::ADD_INT_LIT16:
1842 op = kOpAdd;
1843 break;
1844 case Instruction::MUL_INT:
1845 case Instruction::MUL_INT_2ADDR:
1846 case Instruction::MUL_INT_LIT8:
1847 case Instruction::MUL_INT_LIT16: {
1848 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1849 return;
1850 }
1851 op = kOpMul;
1852 break;
1853 }
1854 case Instruction::AND_INT:
1855 case Instruction::AND_INT_2ADDR:
1856 case Instruction::AND_INT_LIT8:
1857 case Instruction::AND_INT_LIT16:
1858 op = kOpAnd;
1859 break;
1860 case Instruction::OR_INT:
1861 case Instruction::OR_INT_2ADDR:
1862 case Instruction::OR_INT_LIT8:
1863 case Instruction::OR_INT_LIT16:
1864 op = kOpOr;
1865 break;
1866 case Instruction::XOR_INT:
1867 case Instruction::XOR_INT_2ADDR:
1868 case Instruction::XOR_INT_LIT8:
1869 case Instruction::XOR_INT_LIT16:
1870 op = kOpXor;
1871 break;
1872 case Instruction::SHL_INT_LIT8:
1873 case Instruction::SHL_INT:
1874 case Instruction::SHL_INT_2ADDR:
1875 lit &= 31;
1876 shift_op = true;
1877 op = kOpLsl;
1878 break;
1879 case Instruction::SHR_INT_LIT8:
1880 case Instruction::SHR_INT:
1881 case Instruction::SHR_INT_2ADDR:
1882 lit &= 31;
1883 shift_op = true;
1884 op = kOpAsr;
1885 break;
1886 case Instruction::USHR_INT_LIT8:
1887 case Instruction::USHR_INT:
1888 case Instruction::USHR_INT_2ADDR:
1889 lit &= 31;
1890 shift_op = true;
1891 op = kOpLsr;
1892 break;
1893
1894 case Instruction::DIV_INT:
1895 case Instruction::DIV_INT_2ADDR:
1896 case Instruction::DIV_INT_LIT8:
1897 case Instruction::DIV_INT_LIT16:
1898 case Instruction::REM_INT:
1899 case Instruction::REM_INT_2ADDR:
1900 case Instruction::REM_INT_LIT8:
1901 case Instruction::REM_INT_LIT16: {
1902 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001903 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904 return;
1905 }
buzbee11b63d12013-08-27 07:34:17 -07001906 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001908 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001909 (opcode == Instruction::DIV_INT_LIT16)) {
1910 is_div = true;
1911 } else {
1912 is_div = false;
1913 }
buzbee11b63d12013-08-27 07:34:17 -07001914 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1915 return;
1916 }
Dave Allison70202782013-10-22 17:52:19 -07001917
1918 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001919 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001921 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001922 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001923 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001924 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1925 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001926 } else if (cu_->instruction_set == kThumb2) {
1927 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1928 // Use ARM SDIV instruction for division. For remainder we also need to
1929 // calculate using a MUL and subtract.
1930 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001931 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001932 done = true;
1933 }
1934 }
1935
1936 if (!done) {
1937 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1939 Clobber(TargetReg(kArg0));
buzbee33ae5582014-06-12 14:56:32 -07001940 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001941 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1942 false);
1943 } else {
1944 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1945 false);
1946 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001948 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 else
1950 rl_result = GetReturnAlt();
1951 }
1952 StoreValue(rl_dest, rl_result);
1953 return;
1954 }
1955 default:
1956 LOG(FATAL) << "Unexpected opcode " << opcode;
1957 }
1958 rl_src = LoadValue(rl_src, kCoreReg);
1959 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001960 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001961 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001962 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001963 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001964 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965 }
1966 StoreValue(rl_dest, rl_result);
1967}
1968
Andreas Gampe2f244e92014-05-08 03:35:25 -07001969template <size_t pointer_size>
1970static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1971 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 RegLocation rl_result;
1973 OpKind first_op = kOpBkpt;
1974 OpKind second_op = kOpBkpt;
1975 bool call_out = false;
1976 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001977 ThreadOffset<pointer_size> func_offset(-1);
1978 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979
1980 switch (opcode) {
1981 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001982 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001983 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1984 return;
1985 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001986 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1987 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001989 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001990 RegStorage t_reg = mir_to_lir->AllocTemp();
1991 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1992 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1993 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1994 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001996 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1997 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001999 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002000 return;
2001 case Instruction::ADD_LONG:
2002 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002003 if (cu->instruction_set != kThumb2) {
2004 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 return;
2006 }
2007 first_op = kOpAdd;
2008 second_op = kOpAdc;
2009 break;
2010 case Instruction::SUB_LONG:
2011 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002012 if (cu->instruction_set != kThumb2) {
2013 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 return;
2015 }
2016 first_op = kOpSub;
2017 second_op = kOpSbc;
2018 break;
2019 case Instruction::MUL_LONG:
2020 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002021 if (cu->instruction_set != kMips) {
2022 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 return;
2024 } else {
2025 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002026 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2027 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 }
2029 break;
2030 case Instruction::DIV_LONG:
2031 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002032 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002033 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2034 return;
2035 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 call_out = true;
2037 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002038 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2039 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 break;
2041 case Instruction::REM_LONG:
2042 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002043 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002044 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2045 return;
2046 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 call_out = true;
2048 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002049 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002050 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002051 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2052 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 break;
2054 case Instruction::AND_LONG_2ADDR:
2055 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002056 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2057 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002058 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 }
2060 first_op = kOpAnd;
2061 second_op = kOpAnd;
2062 break;
2063 case Instruction::OR_LONG:
2064 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002065 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2066 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002067 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002068 return;
2069 }
2070 first_op = kOpOr;
2071 second_op = kOpOr;
2072 break;
2073 case Instruction::XOR_LONG:
2074 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002075 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2076 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002077 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 return;
2079 }
2080 first_op = kOpXor;
2081 second_op = kOpXor;
2082 break;
2083 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002084 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085 return;
2086 }
2087 default:
2088 LOG(FATAL) << "Invalid long arith op";
2089 }
2090 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002091 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002093 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002094 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002095 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2096 mir_to_lir->TargetReg(kArg1));
2097 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2098 mir_to_lir->TargetReg(kArg3));
2099 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2100 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2101 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2102 mir_to_lir->TargetReg(kArg3)));
2103 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002105 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002106 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002107 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002108 }
2109 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002110 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002111 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002113 rl_result = mir_to_lir->GetReturnWideAlt();
2114 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115 }
2116}
2117
Andreas Gampe2f244e92014-05-08 03:35:25 -07002118void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2119 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002120 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002121 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2122 } else {
2123 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2124 }
2125}
2126
Mark Mendelle87f9b52014-04-30 14:13:18 -04002127void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2128 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2129 LoadConstantNoClobber(rl_result.reg, value);
2130 StoreValue(rl_dest, rl_result);
2131 if (value == 0) {
2132 Workaround7250540(rl_dest, rl_result.reg);
2133 }
2134}
2135
Andreas Gampe2f244e92014-05-08 03:35:25 -07002136template <size_t pointer_size>
2137void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002138 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 /*
2140 * Don't optimize the register usage since it calls out to support
2141 * functions
2142 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002143 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2144
Brian Carlstrom7940e442013-07-12 13:46:57 -07002145 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002146 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2147 if (rl_dest.wide) {
2148 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002149 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002150 StoreValueWide(rl_dest, rl_result);
2151 } else {
2152 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002153 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002154 StoreValue(rl_dest, rl_result);
2155 }
2156}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002157template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2158 RegLocation rl_dest, RegLocation rl_src);
2159template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2160 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002161
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002162class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2163 public:
2164 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2165 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2166 }
2167
2168 void Compile() OVERRIDE {
2169 m2l_->ResetRegPool();
2170 m2l_->ResetDefTracking();
2171 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002172 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002173 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2174 } else {
2175 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2176 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002177 if (cont_ != nullptr) {
2178 m2l_->OpUnconditionalBranch(cont_);
2179 }
2180 }
2181};
2182
Brian Carlstrom7940e442013-07-12 13:46:57 -07002183/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002184void Mir2Lir::GenSuspendTest(int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002185 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002186 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2187 return;
2188 }
2189 FlushAllRegs();
2190 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002191 LIR* cont = NewLIR0(kPseudoTargetLabel);
2192 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002193 } else {
2194 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2195 return;
2196 }
2197 FlushAllRegs(); // TODO: needed?
2198 LIR* inst = CheckSuspendUsingLoad();
2199 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002200 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002201}
2202
2203/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002204void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002205 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002206 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2207 OpUnconditionalBranch(target);
2208 return;
2209 }
2210 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002211 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002212 LIR* branch = OpUnconditionalBranch(nullptr);
2213 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002214 } else {
2215 // For the implicit suspend check, just perform the trigger
2216 // load and branch to the target.
2217 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2218 OpUnconditionalBranch(target);
2219 return;
2220 }
2221 FlushAllRegs();
2222 LIR* inst = CheckSuspendUsingLoad();
2223 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002224 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002225 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002226}
2227
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002228/* Call out to helper assembly routine that will null check obj and then lock it. */
2229void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2230 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002231 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002232 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2233 } else {
2234 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2235 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002236}
2237
2238/* Call out to helper assembly routine that will null check obj and then unlock it. */
2239void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2240 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002241 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002242 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2243 } else {
2244 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2245 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002246}
2247
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002248/* Generic code for generating a wide constant into a VR. */
2249void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2250 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002251 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002252 StoreValueWide(rl_dest, rl_result);
2253}
2254
Brian Carlstrom7940e442013-07-12 13:46:57 -07002255} // namespace art