blob: 04a23cf133baa66896d36abe45c03f709bb32d29 [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 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700752 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700754 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 StoreValueWide(rl_dest, rl_result);
756 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700757 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 StoreValue(rl_dest, rl_result);
759 }
760 }
761}
762
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800763// Generate code for all slow paths.
764void Mir2Lir::HandleSlowPaths() {
765 int n = slow_paths_.Size();
766 for (int i = 0; i < n; ++i) {
767 LIRSlowPath* slowpath = slow_paths_.Get(i);
768 slowpath->Compile();
769 }
770 slow_paths_.Reset();
771}
772
Andreas Gampe2f244e92014-05-08 03:35:25 -0700773template <size_t pointer_size>
774static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
775 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
776 ThreadOffset<pointer_size> getter_offset =
777 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
778 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
779 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
780 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
781 true);
782}
783
Vladimir Markobe0e5462014-02-26 11:24:15 +0000784void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700786 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
788 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100789 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
790 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
791 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
792 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000793 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700794 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100795 GenNullCheck(rl_obj.reg, opt_flags);
796 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
797 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000798 LIR* load_lir;
799 if (is_object) {
800 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
801 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100802 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000803 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
804 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100805 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000806 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 StoreValueWide(rl_dest, rl_result);
809 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 StoreValue(rl_dest, rl_result);
811 }
812 } else {
buzbee33ae5582014-06-12 14:56:32 -0700813 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700814 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
815 } else {
816 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
817 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700819 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 StoreValueWide(rl_dest, rl_result);
821 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700822 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 StoreValue(rl_dest, rl_result);
824 }
825 }
826}
827
Andreas Gampe2f244e92014-05-08 03:35:25 -0700828template <size_t pointer_size>
829static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
830 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
831 RegLocation rl_src) {
832 ThreadOffset<pointer_size> setter_offset =
833 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
834 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
835 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
836 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
837 rl_obj, rl_src, true);
838}
839
Vladimir Markobe0e5462014-02-26 11:24:15 +0000840void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700842 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000843 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
844 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100845 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
846 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
847 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
848 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000849 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700850 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100852 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 } else {
854 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100855 }
856 GenNullCheck(rl_obj.reg, opt_flags);
857 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000858 LIR* store;
859 if (is_object) {
860 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
861 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100862 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000863 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
864 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100865 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000866 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100867 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
868 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 }
870 } else {
buzbee33ae5582014-06-12 14:56:32 -0700871 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700872 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
873 } else {
874 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
875 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 }
877}
878
Andreas Gampe2f244e92014-05-08 03:35:25 -0700879template <size_t pointer_size>
880static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
881 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
882 ThreadOffset<pointer_size> helper = needs_range_check
883 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
884 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
885 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
886 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
887 true);
888}
889
Ian Rogersa9a82542013-10-04 11:17:26 -0700890void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
891 RegLocation rl_src) {
892 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
893 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
894 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700895 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700896 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
897 } else {
898 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
899 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 RegLocation rl_method = LoadCurrMethod();
buzbee33ae5582014-06-12 14:56:32 -0700904 DCHECK(!cu_->target64 || rl_method.reg.Is64Bit());
905 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700906 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
908 *cu_->dex_file,
909 type_idx)) {
910 // Call out to helper which resolves type and verifies access.
911 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700912 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700913 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
914 type_idx, rl_method.reg, true);
915 } else {
916 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
917 type_idx, rl_method.reg, true);
918 }
buzbeea0cd2d72014-06-01 09:33:49 -0700919 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 StoreValue(rl_dest, rl_result);
921 } else {
922 // We're don't need access checks, load type from dex cache
923 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700924 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000925 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000926 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000927 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
929 type_idx) || SLOW_TYPE_PATH) {
930 // Slow path, at runtime test if type is null and if so initialize
931 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800932 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800933 LIR* cont = NewLIR0(kPseudoTargetLabel);
934
935 // Object to generate the slow path for class resolution.
936 class SlowPath : public LIRSlowPath {
937 public:
938 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
939 const RegLocation& rl_method, const RegLocation& rl_result) :
940 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
941 rl_method_(rl_method), rl_result_(rl_result) {
942 }
943
944 void Compile() {
945 GenerateTargetLabel();
946
buzbee33ae5582014-06-12 14:56:32 -0700947 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700948 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
949 rl_method_.reg, true);
950 } else {
951 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
952 rl_method_.reg, true);
953 }
buzbee2700f7e2014-03-07 09:46:20 -0800954 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800955
956 m2l_->OpUnconditionalBranch(cont_);
957 }
958
959 private:
960 const int type_idx_;
961 const RegLocation rl_method_;
962 const RegLocation rl_result_;
963 };
964
965 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800966 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800967
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800969 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 // Fast path, we're done - just store result
971 StoreValue(rl_dest, rl_result);
972 }
973 }
974}
975
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700976void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000978 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
979 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
981 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
982 // slow path, resolve string if not in dex cache
983 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700984 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800985
986 // If the Method* is already in a register, we can save a copy.
987 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800988 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800989 if (rl_method.location == kLocPhysReg) {
990 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800991 DCHECK(!IsTemp(rl_method.reg));
992 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800993 } else {
994 r_method = TargetReg(kArg2);
995 LoadCurrMethodDirect(r_method);
996 }
buzbee695d13a2014-04-19 13:32:20 -0700997 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000998 TargetReg(kArg0), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800999
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampe3c12c512014-06-24 18:46:29 +00001001 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0), kNotVolatile);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001002 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
1003 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001004
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001005 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001006 // Object to generate the slow path for string resolution.
1007 class SlowPath : public LIRSlowPath {
1008 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001009 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1010 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1011 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001012 }
1013
1014 void Compile() {
1015 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001016 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001017 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1018 r_method_, string_idx_, true);
1019 } else {
1020 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1021 r_method_, string_idx_, true);
1022 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 m2l_->OpUnconditionalBranch(cont_);
1024 }
1025
1026 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001027 const RegStorage r_method_;
1028 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001029 };
1030
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001031 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001033
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001035 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 } else {
1037 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001038 RegStorage res_reg = AllocTempRef();
1039 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001040 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1041 kNotVolatile);
1042 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 StoreValue(rl_dest, rl_result);
1044 }
1045}
1046
Andreas Gampe2f244e92014-05-08 03:35:25 -07001047template <size_t pointer_size>
1048static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1049 RegLocation rl_dest) {
1050 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 // alloc will always check for resolution, do we also need to verify
1052 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001053 ThreadOffset<pointer_size> func_offset(-1);
1054 const DexFile* dex_file = cu->dex_file;
1055 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001057 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001058 bool is_type_initialized;
1059 bool use_direct_type_ptr;
1060 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001061 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001063 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1064 &direct_type_ptr, &is_finalizable) &&
1065 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001066 // The fast path.
1067 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001068 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001070 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1071 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001073 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1074 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 }
1076 } else {
1077 // Use the direct pointer.
1078 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001079 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1080 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001081 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1083 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001084 }
1085 }
1086 } else {
1087 // The slow path.
1088 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001089 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1090 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 }
1092 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001094 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1095 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 }
buzbeea0cd2d72014-06-01 09:33:49 -07001097 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001098 mir_to_lir->StoreValue(rl_dest, rl_result);
1099}
1100
1101/*
1102 * Let helper function take care of everything. Will
1103 * call Class::NewInstanceFromCode(type_idx, method);
1104 */
1105void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001106 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001107 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1108 } else {
1109 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1110 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111}
1112
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001113void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001115 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001116 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1117 } else {
1118 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1119 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120}
1121
1122// For final classes there are no sub-classes to check and so we can answer the instance-of
1123// question with simple comparisons.
1124void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1125 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001126 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001127 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001128
buzbeea0cd2d72014-06-01 09:33:49 -07001129 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001131 RegStorage result_reg = rl_result.reg;
1132 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 result_reg = AllocTypedTemp(false, kCoreReg);
1134 }
1135 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001136 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137
buzbeea0cd2d72014-06-01 09:33:49 -07001138 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1139 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140
1141 LoadCurrMethodDirect(check_class);
1142 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001143 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1144 kNotVolatile);
1145 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1146 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 } else {
buzbee695d13a2014-04-19 13:32:20 -07001148 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001149 check_class, kNotVolatile);
1150 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1151 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001152 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001153 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 }
1155
1156 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001157 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 if (cu_->instruction_set == kThumb2) {
1159 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001160 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001162 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 } else {
1164 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1165 LoadConstant(result_reg, 1); // eq case - load true
1166 }
1167 LIR* target = NewLIR0(kPseudoTargetLabel);
1168 null_branchover->target = target;
1169 if (ne_branchover != NULL) {
1170 ne_branchover->target = target;
1171 }
1172 FreeTemp(object_class);
1173 FreeTemp(check_class);
1174 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001175 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 FreeTemp(result_reg);
1177 }
1178 StoreValue(rl_dest, rl_result);
1179}
1180
1181void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1182 bool type_known_abstract, bool use_declaring_class,
1183 bool can_assume_type_is_in_dex_cache,
1184 uint32_t type_idx, RegLocation rl_dest,
1185 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001186 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001187 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001188
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 FlushAllRegs();
1190 // May generate a call - use explicit registers
1191 LockCallTemps();
1192 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001193 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 if (needs_access_check) {
1195 // Check we have access to type_idx and if not throw IllegalAccessError,
1196 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001197 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001198 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1199 type_idx, true);
1200 } else {
1201 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1202 type_idx, true);
1203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1205 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1206 } else if (use_declaring_class) {
1207 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001208 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001209 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 } else {
1211 // Load dex cache entry into class_reg (kArg2)
1212 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001213 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001214 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001215 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001216 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (!can_assume_type_is_in_dex_cache) {
1218 // Need to test presence of type in dex cache at runtime
1219 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1220 // Not resolved
1221 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001222 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001223 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1224 } else {
1225 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1226 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001227 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1229 // Rejoin code paths
1230 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1231 hop_branch->target = hop_target;
1232 }
1233 }
1234 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001235 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 if (cu_->instruction_set == kMips) {
1237 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001238 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1241
1242 /* load object->klass_ */
1243 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001244 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1),
1245 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1247 LIR* branchover = NULL;
1248 if (type_known_final) {
1249 // rl_result == ref == null == 0.
1250 if (cu_->instruction_set == kThumb2) {
1251 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001252 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001253 LoadConstant(rl_result.reg, 1); // .eq case - load true
1254 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001255 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001257 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001259 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 }
1261 } else {
1262 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001263 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001264 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1265 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001266 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 if (!type_known_abstract) {
1268 /* Uses conditional nullification */
1269 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001270 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1272 }
1273 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1274 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001275 if (it != nullptr) {
1276 OpEndIT(it);
1277 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 FreeTemp(r_tgt);
1279 } else {
1280 if (!type_known_abstract) {
1281 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001282 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1284 }
buzbee33ae5582014-06-12 14:56:32 -07001285 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001286 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1287 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001288 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1289 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1290 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 }
1292 }
1293 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001294 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 /* branch targets here */
1296 LIR* target = NewLIR0(kPseudoTargetLabel);
1297 StoreValue(rl_dest, rl_result);
1298 branch1->target = target;
1299 if (branchover != NULL) {
1300 branchover->target = target;
1301 }
1302}
1303
1304void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1305 bool type_known_final, type_known_abstract, use_declaring_class;
1306 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1307 *cu_->dex_file,
1308 type_idx,
1309 &type_known_final,
1310 &type_known_abstract,
1311 &use_declaring_class);
1312 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1313 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1314
1315 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1316 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1317 } else {
1318 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1319 use_declaring_class, can_assume_type_is_in_dex_cache,
1320 type_idx, rl_dest, rl_src);
1321 }
1322}
1323
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001324void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 bool type_known_final, type_known_abstract, use_declaring_class;
1326 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1327 *cu_->dex_file,
1328 type_idx,
1329 &type_known_final,
1330 &type_known_abstract,
1331 &use_declaring_class);
1332 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1333 // of the exception throw path.
1334 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001335 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 // Verifier type analysis proved this check cast would never cause an exception.
1337 return;
1338 }
1339 FlushAllRegs();
1340 // May generate a call - use explicit registers
1341 LockCallTemps();
1342 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001343 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 if (needs_access_check) {
1345 // Check we have access to type_idx and if not throw IllegalAccessError,
1346 // returns Class* in kRet0
1347 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001348 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001349 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1350 type_idx, TargetReg(kArg1), true);
1351 } else {
1352 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1353 type_idx, TargetReg(kArg1), true);
1354 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1356 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001357 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001358 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 } else {
1360 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001361 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001362 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001363 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001364 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1366 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001367 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1368 LIR* cont = NewLIR0(kPseudoTargetLabel);
1369
1370 // Slow path to initialize the type. Executed if the type is NULL.
1371 class SlowPath : public LIRSlowPath {
1372 public:
1373 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001374 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1376 class_reg_(class_reg) {
1377 }
1378
1379 void Compile() {
1380 GenerateTargetLabel();
1381
1382 // Call out to helper, which will return resolved type in kArg0
1383 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001384 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001385 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1386 m2l_->TargetReg(kArg1), true);
1387 } else {
1388 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1389 m2l_->TargetReg(kArg1), true);
1390 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001391 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1392 m2l_->OpUnconditionalBranch(cont_);
1393 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001394
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001395 public:
1396 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001397 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001398 };
1399
buzbee2700f7e2014-03-07 09:46:20 -08001400 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 }
1402 }
1403 // At this point, class_reg (kArg2) has class
1404 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405
1406 // Slow path for the case where the classes are not equal. In this case we need
1407 // to call a helper function to do the check.
1408 class SlowPath : public LIRSlowPath {
1409 public:
1410 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1411 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1412 }
1413
1414 void Compile() {
1415 GenerateTargetLabel();
1416
1417 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001418 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001419 m2l_->TargetReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001420 }
buzbee33ae5582014-06-12 14:56:32 -07001421 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001422 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1423 m2l_->TargetReg(kArg1), true);
1424 } else {
1425 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1426 m2l_->TargetReg(kArg1), true);
1427 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428
1429 m2l_->OpUnconditionalBranch(cont_);
1430 }
1431
1432 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001433 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001434 };
1435
1436 if (type_known_abstract) {
1437 // Easier case, run slow path if target is non-null (slow path will load from target)
1438 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1439 LIR* cont = NewLIR0(kPseudoTargetLabel);
1440 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1441 } else {
1442 // Harder, more common case. We need to generate a forward branch over the load
1443 // if the target is null. If it's non-null we perform the load and branch to the
1444 // slow path if the classes are not equal.
1445
1446 /* Null is OK - continue */
1447 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1448 /* load object->klass_ */
1449 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001450 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1),
1451 kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001452
1453 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1454 LIR* cont = NewLIR0(kPseudoTargetLabel);
1455
1456 // Add the slow path that will not perform load since this is already done.
1457 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1458
1459 // Set the null check to branch to the continuation.
1460 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 }
1462}
1463
1464void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001465 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 RegLocation rl_result;
1467 if (cu_->instruction_set == kThumb2) {
1468 /*
1469 * NOTE: This is the one place in the code in which we might have
1470 * as many as six live temporary registers. There are 5 in the normal
1471 * set for Arm. Until we have spill capabilities, temporarily add
1472 * lr to the temp set. It is safe to do this locally, but note that
1473 * lr is used explicitly elsewhere in the code generator and cannot
1474 * normally be used as a general temp register.
1475 */
1476 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1477 FreeTemp(TargetReg(kLr)); // and make it available
1478 }
1479 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1480 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1481 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1482 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001483 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1484 RegStorage t_reg = AllocTemp();
1485 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1486 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1487 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001488 FreeTemp(t_reg);
1489 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001490 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1491 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 }
1493 /*
1494 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1495 * following StoreValueWide might need to allocate a temp register.
1496 * To further work around the lack of a spill capability, explicitly
1497 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1498 * Remove when spill is functional.
1499 */
1500 FreeRegLocTemps(rl_result, rl_src1);
1501 FreeRegLocTemps(rl_result, rl_src2);
1502 StoreValueWide(rl_dest, rl_result);
1503 if (cu_->instruction_set == kThumb2) {
1504 Clobber(TargetReg(kLr));
1505 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1506 }
1507}
1508
1509
Andreas Gampe2f244e92014-05-08 03:35:25 -07001510template <size_t pointer_size>
1511static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1512 RegLocation rl_shift) {
1513 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514
1515 switch (opcode) {
1516 case Instruction::SHL_LONG:
1517 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001518 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 break;
1520 case Instruction::SHR_LONG:
1521 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001522 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 break;
1524 case Instruction::USHR_LONG:
1525 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001526 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 break;
1528 default:
1529 LOG(FATAL) << "Unexpected case";
1530 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001531 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1532 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1533}
1534
1535void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1536 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001537 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001538 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1539 } else {
1540 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1541 }
buzbeea0cd2d72014-06-01 09:33:49 -07001542 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 StoreValueWide(rl_dest, rl_result);
1544}
1545
1546
1547void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001548 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001549 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 OpKind op = kOpBkpt;
1551 bool is_div_rem = false;
1552 bool check_zero = false;
1553 bool unary = false;
1554 RegLocation rl_result;
1555 bool shift_op = false;
1556 switch (opcode) {
1557 case Instruction::NEG_INT:
1558 op = kOpNeg;
1559 unary = true;
1560 break;
1561 case Instruction::NOT_INT:
1562 op = kOpMvn;
1563 unary = true;
1564 break;
1565 case Instruction::ADD_INT:
1566 case Instruction::ADD_INT_2ADDR:
1567 op = kOpAdd;
1568 break;
1569 case Instruction::SUB_INT:
1570 case Instruction::SUB_INT_2ADDR:
1571 op = kOpSub;
1572 break;
1573 case Instruction::MUL_INT:
1574 case Instruction::MUL_INT_2ADDR:
1575 op = kOpMul;
1576 break;
1577 case Instruction::DIV_INT:
1578 case Instruction::DIV_INT_2ADDR:
1579 check_zero = true;
1580 op = kOpDiv;
1581 is_div_rem = true;
1582 break;
1583 /* NOTE: returns in kArg1 */
1584 case Instruction::REM_INT:
1585 case Instruction::REM_INT_2ADDR:
1586 check_zero = true;
1587 op = kOpRem;
1588 is_div_rem = true;
1589 break;
1590 case Instruction::AND_INT:
1591 case Instruction::AND_INT_2ADDR:
1592 op = kOpAnd;
1593 break;
1594 case Instruction::OR_INT:
1595 case Instruction::OR_INT_2ADDR:
1596 op = kOpOr;
1597 break;
1598 case Instruction::XOR_INT:
1599 case Instruction::XOR_INT_2ADDR:
1600 op = kOpXor;
1601 break;
1602 case Instruction::SHL_INT:
1603 case Instruction::SHL_INT_2ADDR:
1604 shift_op = true;
1605 op = kOpLsl;
1606 break;
1607 case Instruction::SHR_INT:
1608 case Instruction::SHR_INT_2ADDR:
1609 shift_op = true;
1610 op = kOpAsr;
1611 break;
1612 case Instruction::USHR_INT:
1613 case Instruction::USHR_INT_2ADDR:
1614 shift_op = true;
1615 op = kOpLsr;
1616 break;
1617 default:
1618 LOG(FATAL) << "Invalid word arith op: " << opcode;
1619 }
1620 if (!is_div_rem) {
1621 if (unary) {
1622 rl_src1 = LoadValue(rl_src1, kCoreReg);
1623 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001624 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001626 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001627 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001628 RegStorage t_reg = AllocTemp();
1629 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 rl_src1 = LoadValue(rl_src1, kCoreReg);
1631 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001632 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 FreeTemp(t_reg);
1634 } else {
1635 rl_src1 = LoadValue(rl_src1, kCoreReg);
1636 rl_src2 = LoadValue(rl_src2, kCoreReg);
1637 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001638 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 }
1640 }
1641 StoreValue(rl_dest, rl_result);
1642 } else {
Dave Allison70202782013-10-22 17:52:19 -07001643 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 +01001644 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 rl_src1 = LoadValue(rl_src1, kCoreReg);
1646 rl_src2 = LoadValue(rl_src2, kCoreReg);
1647 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001648 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 }
buzbee2700f7e2014-03-07 09:46:20 -08001650 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001651 done = true;
1652 } else if (cu_->instruction_set == kThumb2) {
1653 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1654 // Use ARM SDIV instruction for division. For remainder we also need to
1655 // calculate using a MUL and subtract.
1656 rl_src1 = LoadValue(rl_src1, kCoreReg);
1657 rl_src2 = LoadValue(rl_src2, kCoreReg);
1658 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001659 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001660 }
buzbee2700f7e2014-03-07 09:46:20 -08001661 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001662 done = true;
1663 }
1664 }
1665
1666 // If we haven't already generated the code use the callout function.
1667 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 FlushAllRegs(); /* Send everything to home location */
1669 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee33ae5582014-06-12 14:56:32 -07001670 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001671 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1672 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1674 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001675 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 }
Dave Allison70202782013-10-22 17:52:19 -07001677 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001678 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001679 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1680 } else {
1681 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1682 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001684 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 else
1686 rl_result = GetReturnAlt();
1687 }
1688 StoreValue(rl_dest, rl_result);
1689 }
1690}
1691
1692/*
1693 * The following are the first-level codegen routines that analyze the format
1694 * of each bytecode then either dispatch special purpose codegen routines
1695 * or produce corresponding Thumb instructions directly.
1696 */
1697
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001699static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 x &= x - 1;
1701 return (x & (x - 1)) == 0;
1702}
1703
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1705// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001706bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001707 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1709 return false;
1710 }
1711 // No divide instruction for Arm, so check for more special cases
1712 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001713 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 }
1715 int k = LowestSetBit(lit);
1716 if (k >= 30) {
1717 // Avoid special cases.
1718 return false;
1719 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 rl_src = LoadValue(rl_src, kCoreReg);
1721 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001722 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001723 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 if (lit == 2) {
1725 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001726 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1727 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1728 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001730 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001732 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1733 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 }
1735 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001736 RegStorage t_reg1 = AllocTemp();
1737 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001739 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1740 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001742 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001744 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001746 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001748 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 }
1750 }
1751 StoreValue(rl_dest, rl_result);
1752 return true;
1753}
1754
1755// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1756// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001757bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001758 if (lit < 0) {
1759 return false;
1760 }
1761 if (lit == 0) {
1762 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1763 LoadConstant(rl_result.reg, 0);
1764 StoreValue(rl_dest, rl_result);
1765 return true;
1766 }
1767 if (lit == 1) {
1768 rl_src = LoadValue(rl_src, kCoreReg);
1769 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1770 OpRegCopy(rl_result.reg, rl_src.reg);
1771 StoreValue(rl_dest, rl_result);
1772 return true;
1773 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001774 // There is RegRegRegShift on Arm, so check for more special cases
1775 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001776 return EasyMultiply(rl_src, rl_dest, lit);
1777 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 // Can we simplify this multiplication?
1779 bool power_of_two = false;
1780 bool pop_count_le2 = false;
1781 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001782 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 power_of_two = true;
1784 } else if (IsPopCountLE2(lit)) {
1785 pop_count_le2 = true;
1786 } else if (IsPowerOfTwo(lit + 1)) {
1787 power_of_two_minus_one = true;
1788 } else {
1789 return false;
1790 }
1791 rl_src = LoadValue(rl_src, kCoreReg);
1792 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1793 if (power_of_two) {
1794 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001795 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 } else if (pop_count_le2) {
1797 // Shift and add and shift.
1798 int first_bit = LowestSetBit(lit);
1799 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1800 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1801 } else {
1802 // Reverse subtract: (src << (shift + 1)) - src.
1803 DCHECK(power_of_two_minus_one);
1804 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001805 RegStorage t_reg = AllocTemp();
1806 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1807 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001808 }
1809 StoreValue(rl_dest, rl_result);
1810 return true;
1811}
1812
1813void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001814 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 RegLocation rl_result;
1816 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1817 int shift_op = false;
1818 bool is_div = false;
1819
1820 switch (opcode) {
1821 case Instruction::RSUB_INT_LIT8:
1822 case Instruction::RSUB_INT: {
1823 rl_src = LoadValue(rl_src, kCoreReg);
1824 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1825 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001826 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001828 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1829 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 }
1831 StoreValue(rl_dest, rl_result);
1832 return;
1833 }
1834
1835 case Instruction::SUB_INT:
1836 case Instruction::SUB_INT_2ADDR:
1837 lit = -lit;
1838 // Intended fallthrough
1839 case Instruction::ADD_INT:
1840 case Instruction::ADD_INT_2ADDR:
1841 case Instruction::ADD_INT_LIT8:
1842 case Instruction::ADD_INT_LIT16:
1843 op = kOpAdd;
1844 break;
1845 case Instruction::MUL_INT:
1846 case Instruction::MUL_INT_2ADDR:
1847 case Instruction::MUL_INT_LIT8:
1848 case Instruction::MUL_INT_LIT16: {
1849 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1850 return;
1851 }
1852 op = kOpMul;
1853 break;
1854 }
1855 case Instruction::AND_INT:
1856 case Instruction::AND_INT_2ADDR:
1857 case Instruction::AND_INT_LIT8:
1858 case Instruction::AND_INT_LIT16:
1859 op = kOpAnd;
1860 break;
1861 case Instruction::OR_INT:
1862 case Instruction::OR_INT_2ADDR:
1863 case Instruction::OR_INT_LIT8:
1864 case Instruction::OR_INT_LIT16:
1865 op = kOpOr;
1866 break;
1867 case Instruction::XOR_INT:
1868 case Instruction::XOR_INT_2ADDR:
1869 case Instruction::XOR_INT_LIT8:
1870 case Instruction::XOR_INT_LIT16:
1871 op = kOpXor;
1872 break;
1873 case Instruction::SHL_INT_LIT8:
1874 case Instruction::SHL_INT:
1875 case Instruction::SHL_INT_2ADDR:
1876 lit &= 31;
1877 shift_op = true;
1878 op = kOpLsl;
1879 break;
1880 case Instruction::SHR_INT_LIT8:
1881 case Instruction::SHR_INT:
1882 case Instruction::SHR_INT_2ADDR:
1883 lit &= 31;
1884 shift_op = true;
1885 op = kOpAsr;
1886 break;
1887 case Instruction::USHR_INT_LIT8:
1888 case Instruction::USHR_INT:
1889 case Instruction::USHR_INT_2ADDR:
1890 lit &= 31;
1891 shift_op = true;
1892 op = kOpLsr;
1893 break;
1894
1895 case Instruction::DIV_INT:
1896 case Instruction::DIV_INT_2ADDR:
1897 case Instruction::DIV_INT_LIT8:
1898 case Instruction::DIV_INT_LIT16:
1899 case Instruction::REM_INT:
1900 case Instruction::REM_INT_2ADDR:
1901 case Instruction::REM_INT_LIT8:
1902 case Instruction::REM_INT_LIT16: {
1903 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001904 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001905 return;
1906 }
buzbee11b63d12013-08-27 07:34:17 -07001907 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001909 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910 (opcode == Instruction::DIV_INT_LIT16)) {
1911 is_div = true;
1912 } else {
1913 is_div = false;
1914 }
buzbee11b63d12013-08-27 07:34:17 -07001915 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1916 return;
1917 }
Dave Allison70202782013-10-22 17:52:19 -07001918
1919 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001920 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001922 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001923 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001924 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001925 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1926 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001927 } else if (cu_->instruction_set == kThumb2) {
1928 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1929 // Use ARM SDIV instruction for division. For remainder we also need to
1930 // calculate using a MUL and subtract.
1931 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001932 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001933 done = true;
1934 }
1935 }
1936
1937 if (!done) {
1938 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1940 Clobber(TargetReg(kArg0));
buzbee33ae5582014-06-12 14:56:32 -07001941 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001942 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1943 false);
1944 } else {
1945 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1946 false);
1947 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001949 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 else
1951 rl_result = GetReturnAlt();
1952 }
1953 StoreValue(rl_dest, rl_result);
1954 return;
1955 }
1956 default:
1957 LOG(FATAL) << "Unexpected opcode " << opcode;
1958 }
1959 rl_src = LoadValue(rl_src, kCoreReg);
1960 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001961 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001962 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001963 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001965 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 }
1967 StoreValue(rl_dest, rl_result);
1968}
1969
Andreas Gampe2f244e92014-05-08 03:35:25 -07001970template <size_t pointer_size>
1971static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1972 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 RegLocation rl_result;
1974 OpKind first_op = kOpBkpt;
1975 OpKind second_op = kOpBkpt;
1976 bool call_out = false;
1977 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001978 ThreadOffset<pointer_size> func_offset(-1);
1979 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980
1981 switch (opcode) {
1982 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001983 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001984 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1985 return;
1986 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001987 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1988 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001990 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001991 RegStorage t_reg = mir_to_lir->AllocTemp();
1992 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1993 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1994 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1995 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001997 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1998 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002000 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 return;
2002 case Instruction::ADD_LONG:
2003 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002004 if (cu->instruction_set != kThumb2) {
2005 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 return;
2007 }
2008 first_op = kOpAdd;
2009 second_op = kOpAdc;
2010 break;
2011 case Instruction::SUB_LONG:
2012 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002013 if (cu->instruction_set != kThumb2) {
2014 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 return;
2016 }
2017 first_op = kOpSub;
2018 second_op = kOpSbc;
2019 break;
2020 case Instruction::MUL_LONG:
2021 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002022 if (cu->instruction_set != kMips) {
2023 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024 return;
2025 } else {
2026 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002027 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2028 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 }
2030 break;
2031 case Instruction::DIV_LONG:
2032 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002033 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002034 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2035 return;
2036 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 call_out = true;
2038 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002039 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2040 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 break;
2042 case Instruction::REM_LONG:
2043 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002044 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002045 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2046 return;
2047 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 call_out = true;
2049 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002050 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002052 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2053 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 break;
2055 case Instruction::AND_LONG_2ADDR:
2056 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002057 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2058 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002059 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 }
2061 first_op = kOpAnd;
2062 second_op = kOpAnd;
2063 break;
2064 case Instruction::OR_LONG:
2065 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002066 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2067 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002068 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 return;
2070 }
2071 first_op = kOpOr;
2072 second_op = kOpOr;
2073 break;
2074 case Instruction::XOR_LONG:
2075 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002076 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2077 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002078 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 return;
2080 }
2081 first_op = kOpXor;
2082 second_op = kOpXor;
2083 break;
2084 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002085 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002086 return;
2087 }
2088 default:
2089 LOG(FATAL) << "Invalid long arith op";
2090 }
2091 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002092 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002094 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002096 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2097 mir_to_lir->TargetReg(kArg1));
2098 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2099 mir_to_lir->TargetReg(kArg3));
2100 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2101 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2102 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2103 mir_to_lir->TargetReg(kArg3)));
2104 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002106 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002107 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002108 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002109 }
2110 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002111 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002112 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002113 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002114 rl_result = mir_to_lir->GetReturnWideAlt();
2115 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002116 }
2117}
2118
Andreas Gampe2f244e92014-05-08 03:35:25 -07002119void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2120 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002121 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002122 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2123 } else {
2124 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2125 }
2126}
2127
Mark Mendelle87f9b52014-04-30 14:13:18 -04002128void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2129 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2130 LoadConstantNoClobber(rl_result.reg, value);
2131 StoreValue(rl_dest, rl_result);
2132 if (value == 0) {
2133 Workaround7250540(rl_dest, rl_result.reg);
2134 }
2135}
2136
Andreas Gampe2f244e92014-05-08 03:35:25 -07002137template <size_t pointer_size>
2138void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002139 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002140 /*
2141 * Don't optimize the register usage since it calls out to support
2142 * functions
2143 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002144 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2145
Brian Carlstrom7940e442013-07-12 13:46:57 -07002146 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002147 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2148 if (rl_dest.wide) {
2149 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002150 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002151 StoreValueWide(rl_dest, rl_result);
2152 } else {
2153 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002154 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002155 StoreValue(rl_dest, rl_result);
2156 }
2157}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002158template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2159 RegLocation rl_dest, RegLocation rl_src);
2160template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2161 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002162
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002163class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2164 public:
2165 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2166 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2167 }
2168
2169 void Compile() OVERRIDE {
2170 m2l_->ResetRegPool();
2171 m2l_->ResetDefTracking();
2172 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002173 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002174 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2175 } else {
2176 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2177 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002178 if (cont_ != nullptr) {
2179 m2l_->OpUnconditionalBranch(cont_);
2180 }
2181 }
2182};
2183
Brian Carlstrom7940e442013-07-12 13:46:57 -07002184/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002185void Mir2Lir::GenSuspendTest(int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002186 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002187 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2188 return;
2189 }
2190 FlushAllRegs();
2191 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002192 LIR* cont = NewLIR0(kPseudoTargetLabel);
2193 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002194 } else {
2195 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2196 return;
2197 }
2198 FlushAllRegs(); // TODO: needed?
2199 LIR* inst = CheckSuspendUsingLoad();
2200 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002201 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202}
2203
2204/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002205void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002206 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002207 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2208 OpUnconditionalBranch(target);
2209 return;
2210 }
2211 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002212 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002213 LIR* branch = OpUnconditionalBranch(nullptr);
2214 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002215 } else {
2216 // For the implicit suspend check, just perform the trigger
2217 // load and branch to the target.
2218 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2219 OpUnconditionalBranch(target);
2220 return;
2221 }
2222 FlushAllRegs();
2223 LIR* inst = CheckSuspendUsingLoad();
2224 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002225 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002226 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002227}
2228
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002229/* Call out to helper assembly routine that will null check obj and then lock it. */
2230void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2231 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002232 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002233 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2234 } else {
2235 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2236 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002237}
2238
2239/* Call out to helper assembly routine that will null check obj and then unlock it. */
2240void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2241 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002242 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002243 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2244 } else {
2245 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2246 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002247}
2248
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002249/* Generic code for generating a wide constant into a VR. */
2250void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2251 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002252 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002253 StoreValueWide(rl_dest, rl_result);
2254}
2255
Brian Carlstrom7940e442013-07-12 13:46:57 -07002256} // namespace art