blob: bea1eb880bf8faa3bf011358bedfd66a949fe68a [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
Andreas Gampeccc60262014-07-04 18:02:38 -0700130 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
131 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700132
133 m2l_->OpRegCopy(arg1_32, length_);
134 m2l_->LoadConstant(arg0_32, index_);
buzbee33ae5582014-06-12 14:56:32 -0700135 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700136 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700137 arg0_32, arg1_32, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700138 } else {
139 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700140 arg0_32, arg1_32, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700141 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700142 }
143
144 private:
145 const int32_t index_;
146 const RegStorage length_;
147 };
148
149 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
150 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
151}
152
Mingyao Yange643a172014-04-08 11:02:52 -0700153LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
154 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
155 public:
156 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
157 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
158 }
159
160 void Compile() OVERRIDE {
161 m2l_->ResetRegPool();
162 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700163 GenerateTargetLabel(kPseudoThrowTarget);
buzbee33ae5582014-06-12 14:56:32 -0700164 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700165 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowNullPointer), true);
166 } else {
167 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
168 }
Mingyao Yange643a172014-04-08 11:02:52 -0700169 }
170 };
171
172 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
173 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
174 return branch;
175}
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800178LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000179 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700180 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
Dave Allisonb373e092014-02-20 16:06:36 -0800182 return nullptr;
183}
184
Dave Allisonf9439142014-03-27 15:10:22 -0700185/* Perform an explicit null-check on a register. */
186LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
187 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
188 return NULL;
189 }
Mingyao Yange643a172014-04-08 11:02:52 -0700190 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700191}
192
Dave Allisonb373e092014-02-20 16:06:36 -0800193void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000194 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800195 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
196 return;
197 }
198 MarkSafepointPC(last_lir_insn_);
199 }
200}
201
Andreas Gampe3c12c512014-06-24 18:46:29 +0000202void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000203 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000204 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
205 return;
206 }
207 MarkSafepointPCAfter(after);
208 }
209}
210
Dave Allisonb373e092014-02-20 16:06:36 -0800211void Mir2Lir::MarkPossibleStackOverflowException() {
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000212 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800213 MarkSafepointPC(last_lir_insn_);
214 }
215}
216
buzbee2700f7e2014-03-07 09:46:20 -0800217void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000218 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800219 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
220 return;
221 }
222 // Force an implicit null check by performing a memory operation (load) from the given
223 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800224 RegStorage tmp = AllocTemp();
225 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700226 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800227 FreeTemp(tmp);
228 MarkSafepointPC(load);
229 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230}
231
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
233 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700234 LIR* fall_through) {
buzbeea0cd2d72014-06-01 09:33:49 -0700235 DCHECK(!rl_src1.fp);
236 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 ConditionCode cond;
238 switch (opcode) {
239 case Instruction::IF_EQ:
240 cond = kCondEq;
241 break;
242 case Instruction::IF_NE:
243 cond = kCondNe;
244 break;
245 case Instruction::IF_LT:
246 cond = kCondLt;
247 break;
248 case Instruction::IF_GE:
249 cond = kCondGe;
250 break;
251 case Instruction::IF_GT:
252 cond = kCondGt;
253 break;
254 case Instruction::IF_LE:
255 cond = kCondLe;
256 break;
257 default:
258 cond = static_cast<ConditionCode>(0);
259 LOG(FATAL) << "Unexpected opcode " << opcode;
260 }
261
262 // Normalize such that if either operand is constant, src2 will be constant
263 if (rl_src1.is_const) {
264 RegLocation rl_temp = rl_src1;
265 rl_src1 = rl_src2;
266 rl_src2 = rl_temp;
267 cond = FlipComparisonOrder(cond);
268 }
269
buzbeea0cd2d72014-06-01 09:33:49 -0700270 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 // Is this really an immediate comparison?
272 if (rl_src2.is_const) {
273 // If it's already live in a register or not easily materialized, just keep going
274 RegLocation rl_temp = UpdateLoc(rl_src2);
275 if ((rl_temp.location == kLocDalvikFrame) &&
276 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
277 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800278 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 return;
280 }
281 }
buzbeea0cd2d72014-06-01 09:33:49 -0700282 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800283 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
286void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700287 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700289 DCHECK(!rl_src.fp);
290 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 switch (opcode) {
292 case Instruction::IF_EQZ:
293 cond = kCondEq;
294 break;
295 case Instruction::IF_NEZ:
296 cond = kCondNe;
297 break;
298 case Instruction::IF_LTZ:
299 cond = kCondLt;
300 break;
301 case Instruction::IF_GEZ:
302 cond = kCondGe;
303 break;
304 case Instruction::IF_GTZ:
305 cond = kCondGt;
306 break;
307 case Instruction::IF_LEZ:
308 cond = kCondLe;
309 break;
310 default:
311 cond = static_cast<ConditionCode>(0);
312 LOG(FATAL) << "Unexpected opcode " << opcode;
313 }
buzbee2700f7e2014-03-07 09:46:20 -0800314 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315}
316
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
319 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800320 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800322 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 }
buzbee2700f7e2014-03-07 09:46:20 -0800324 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 StoreValueWide(rl_dest, rl_result);
326}
327
328void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700329 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700330 rl_src = LoadValue(rl_src, kCoreReg);
331 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
332 OpKind op = kOpInvalid;
333 switch (opcode) {
334 case Instruction::INT_TO_BYTE:
335 op = kOp2Byte;
336 break;
337 case Instruction::INT_TO_SHORT:
338 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700340 case Instruction::INT_TO_CHAR:
341 op = kOp2Char;
342 break;
343 default:
344 LOG(ERROR) << "Bad int conversion type";
345 }
buzbee2700f7e2014-03-07 09:46:20 -0800346 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700347 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348}
349
Andreas Gampe2f244e92014-05-08 03:35:25 -0700350template <size_t pointer_size>
351static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
352 uint32_t type_idx, RegLocation rl_dest,
353 RegLocation rl_src) {
354 mir_to_lir->FlushAllRegs(); /* Everything to home location */
355 ThreadOffset<pointer_size> func_offset(-1);
356 const DexFile* dex_file = cu->dex_file;
357 CompilerDriver* driver = cu->compiler_driver;
358 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
359 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800360 bool is_type_initialized; // Ignored as an array does not have an initializer.
361 bool use_direct_type_ptr;
362 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700363 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800364 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700365 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
366 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800367 // The fast path.
368 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700369 mir_to_lir->LoadClassType(type_idx, kArg0);
370 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -0700371 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset,
372 mir_to_lir->TargetReg(kArg0, kNotWide),
Andreas Gampe2f244e92014-05-08 03:35:25 -0700373 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800374 } else {
375 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700376 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
377 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
378 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800379 }
380 } else {
381 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700382 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
383 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800384 }
385 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700387 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
388 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 }
buzbeea0cd2d72014-06-01 09:33:49 -0700390 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700391 mir_to_lir->StoreValue(rl_dest, rl_result);
392}
393
394/*
395 * Let helper function take care of everything. Will call
396 * Array::AllocFromCode(type_idx, method, count);
397 * Note: AllocFromCode will handle checks for errNegativeArraySize.
398 */
399void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
400 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700401 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700402 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
403 } else {
404 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
405 }
406}
407
408template <size_t pointer_size>
409static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
410 ThreadOffset<pointer_size> func_offset(-1);
411 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
412 type_idx)) {
413 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
414 } else {
415 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
416 }
417 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418}
419
420/*
421 * Similar to GenNewArray, but with post-allocation initialization.
422 * Verifier guarantees we're dealing with an array class. Current
423 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
424 * Current code also throws internal unimp if not 'L', '[' or 'I'.
425 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 int elems = info->num_arg_words;
428 int type_idx = info->index;
429 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700430 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700431 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700433 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700435 FreeTemp(TargetReg(kArg2, kNotWide));
436 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 /*
438 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
439 * return region. Because AllocFromCode placed the new array
440 * in kRet0, we'll just lock it into place. When debugger support is
441 * added, it may be necessary to additionally copy all return
442 * values to a home location in thread-local storage
443 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700444 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700445 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446
447 // TODO: use the correct component size, currently all supported types
448 // share array alignment with ints (see comment at head of function)
449 size_t component_size = sizeof(int32_t);
450
451 // Having a range of 0 is legal
452 if (info->is_range && (elems > 0)) {
453 /*
454 * Bit of ugliness here. We're going generate a mem copy loop
455 * on the register range, but it is possible that some regs
456 * in the range have been promoted. This is unlikely, but
457 * before generating the copy, we'll just force a flush
458 * of any regs in the source range that have been promoted to
459 * home location.
460 */
461 for (int i = 0; i < elems; i++) {
462 RegLocation loc = UpdateLoc(info->args[i]);
463 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100464 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700465 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 }
468 /*
469 * TUNING note: generated code here could be much improved, but
470 * this is an uncommon operation and isn't especially performance
471 * critical.
472 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700473 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700474 RegStorage r_src = AllocTempRef();
475 RegStorage r_dst = AllocTempRef();
476 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800477 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700478 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700480 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700481 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 break;
483 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700484 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700485 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 r_val = AllocTemp();
487 break;
488 case kMips:
489 r_val = AllocTemp();
490 break;
491 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
492 }
493 // Set up source pointer
494 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700495 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700497 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 mirror::Array::DataOffset(component_size).Int32Value());
499 // Set up the loop counter (known to be > 0)
500 LoadConstant(r_idx, elems - 1);
501 // Generate the copy loop. Going backwards for convenience
502 LIR* target = NewLIR0(kPseudoTargetLabel);
503 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100504 {
505 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
506 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
507 // NOTE: No dalvik register annotation, local optimizations will be stopped
508 // by the loop boundaries.
509 }
buzbee695d13a2014-04-19 13:32:20 -0700510 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 FreeTemp(r_val);
512 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700513 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700515 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 -mirror::Array::DataOffset(component_size).Int32Value());
517 }
518 } else if (!info->is_range) {
519 // TUNING: interleave
520 for (int i = 0; i < elems; i++) {
521 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700522 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000523 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800525 if (IsTemp(rl_arg.reg)) {
526 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 }
528 }
529 }
530 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700531 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
533}
534
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800535//
536// Slow path to ensure a class is initialized for sget/sput.
537//
538class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
539 public:
buzbee2700f7e2014-03-07 09:46:20 -0800540 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
541 RegStorage r_base) :
542 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
543 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544 }
545
546 void Compile() {
547 LIR* unresolved_target = GenerateTargetLabel();
548 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700549 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700550 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
551 storage_index_, true);
552 } else {
553 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
554 storage_index_, true);
555 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800556 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700557 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800558
559 m2l_->OpUnconditionalBranch(cont_);
560 }
561
562 private:
563 LIR* const uninit_;
564 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800565 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800566};
567
Andreas Gampe2f244e92014-05-08 03:35:25 -0700568template <size_t pointer_size>
569static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
570 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
571 ThreadOffset<pointer_size> setter_offset =
572 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
573 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
574 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
575 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
576 true);
577}
578
Vladimir Markobe0e5462014-02-26 11:24:15 +0000579void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700580 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000581 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
582 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100583 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
584 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
585 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800587 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100590 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700591 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000592 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
593 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800594 if (IsTemp(rl_method.reg)) {
595 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 } else {
598 // Medium path, static storage base in a different class which requires checks that the other
599 // class is initialized.
600 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000601 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 // May do runtime call so everything to home locations.
603 FlushAllRegs();
604 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700605 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LockTemp(r_method);
607 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700608 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000610 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
611 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000612 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000613 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000615 if (!field_info.IsInitialized() &&
616 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800617 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800618
619 // The slow path is invoked if the r_base is NULL or the class pointed
620 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700622 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800623 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800624 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800625 mirror::Class::StatusOffset().Int32Value(),
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000626 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800627 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628
buzbee2700f7e2014-03-07 09:46:20 -0800629 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000630 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800631
632 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700633 // Ensure load of status and store of value don't re-order.
634 // TODO: Presumably the actual value store is control-dependent on the status load,
635 // and will thus not be reordered in any case, since stores are never speculated.
636 // Does later code "know" that the class is now initialized? If so, we still
637 // need the barrier to guard later static loads.
638 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 FreeTemp(r_method);
641 }
642 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100643 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100645 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100647 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000649 if (is_object) {
650 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
651 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100652 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000653 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
654 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
656 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800657 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800659 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 } else {
661 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700662 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700663 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
664 } else {
665 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
666 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668}
669
Andreas Gampe2f244e92014-05-08 03:35:25 -0700670template <size_t pointer_size>
671static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
672 const MirSFieldLoweringInfo* field_info) {
673 ThreadOffset<pointer_size> getter_offset =
674 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
675 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
676 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
677 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
678}
679
Vladimir Markobe0e5462014-02-26 11:24:15 +0000680void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700681 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000682 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
683 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100684 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
685 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
686 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000687 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800688 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 // Fast path, static storage base is this method's class
691 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700692 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000693 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
694 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 } else {
696 // Medium path, static storage base in a different class which requires checks that the other
697 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 // May do runtime call so everything to home locations.
700 FlushAllRegs();
701 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700702 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 LockTemp(r_method);
704 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700705 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800706 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000707 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
708 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000709 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000710 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800711 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000712 if (!field_info.IsInitialized() &&
713 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800714 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800715
716 // The slow path is invoked if the r_base is NULL or the class pointed
717 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800718 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700719 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800720 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800721 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800722 mirror::Class::StatusOffset().Int32Value(),
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000723 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800724 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800725
buzbee2700f7e2014-03-07 09:46:20 -0800726 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000727 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800728
729 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700730 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700731 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 FreeTemp(r_method);
734 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800735 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100736 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
737 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800738
Vladimir Marko674744e2014-04-24 15:18:26 +0100739 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000740 if (is_object) {
741 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
742 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100743 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000744 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
745 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800746 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100747 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800748
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 if (is_long_or_double) {
750 StoreValueWide(rl_dest, rl_result);
751 } else {
752 StoreValue(rl_dest, rl_result);
753 }
754 } else {
755 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700756 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700757 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
758 } else {
759 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
760 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700761 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700763 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 StoreValueWide(rl_dest, rl_result);
765 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700766 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 StoreValue(rl_dest, rl_result);
768 }
769 }
770}
771
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800772// Generate code for all slow paths.
773void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700774 // We should check slow_paths_.Size() every time, because a new slow path
775 // may be created during slowpath->Compile().
776 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800777 LIRSlowPath* slowpath = slow_paths_.Get(i);
778 slowpath->Compile();
779 }
780 slow_paths_.Reset();
781}
782
Andreas Gampe2f244e92014-05-08 03:35:25 -0700783template <size_t pointer_size>
784static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
785 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
786 ThreadOffset<pointer_size> getter_offset =
787 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
788 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
789 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
790 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
791 true);
792}
793
Vladimir Markobe0e5462014-02-26 11:24:15 +0000794void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700796 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000797 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
798 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100799 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
800 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
801 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
802 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000803 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700804 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100805 GenNullCheck(rl_obj.reg, opt_flags);
806 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
807 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000808 LIR* load_lir;
809 if (is_object) {
810 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
811 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100812 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000813 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
814 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100815 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000816 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 StoreValueWide(rl_dest, rl_result);
819 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 StoreValue(rl_dest, rl_result);
821 }
822 } else {
buzbee33ae5582014-06-12 14:56:32 -0700823 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700824 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
825 } else {
826 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
827 }
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700828 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700830 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 StoreValueWide(rl_dest, rl_result);
832 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700833 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 StoreValue(rl_dest, rl_result);
835 }
836 }
837}
838
Andreas Gampe2f244e92014-05-08 03:35:25 -0700839template <size_t pointer_size>
840static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
841 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
842 RegLocation rl_src) {
843 ThreadOffset<pointer_size> setter_offset =
844 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
845 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
846 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
847 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
848 rl_obj, rl_src, true);
849}
850
Vladimir Markobe0e5462014-02-26 11:24:15 +0000851void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700853 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000854 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
855 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100856 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
857 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
858 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
859 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000860 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700861 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100863 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 } else {
865 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100866 }
867 GenNullCheck(rl_obj.reg, opt_flags);
868 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000869 LIR* store;
870 if (is_object) {
871 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
872 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100873 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000874 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
875 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100876 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000877 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100878 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
879 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 }
881 } else {
buzbee33ae5582014-06-12 14:56:32 -0700882 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700883 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
884 } else {
885 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
886 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 }
888}
889
Andreas Gampe2f244e92014-05-08 03:35:25 -0700890template <size_t pointer_size>
891static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
892 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
893 ThreadOffset<pointer_size> helper = needs_range_check
894 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
895 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
896 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
897 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
898 true);
899}
900
Ian Rogersa9a82542013-10-04 11:17:26 -0700901void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
902 RegLocation rl_src) {
903 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
904 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
905 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700906 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700907 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
908 } else {
909 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
910 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700911}
912
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700913void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700915 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700916 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700917 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700919 *cu_->dex_file,
920 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 // Call out to helper which resolves type and verifies access.
922 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700923 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700924 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
925 type_idx, rl_method.reg, true);
926 } else {
927 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
928 type_idx, rl_method.reg, true);
929 }
buzbeea0cd2d72014-06-01 09:33:49 -0700930 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 StoreValue(rl_dest, rl_result);
932 } else {
933 // We're don't need access checks, load type from dex cache
934 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700935 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000936 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000937 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000938 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
940 type_idx) || SLOW_TYPE_PATH) {
941 // Slow path, at runtime test if type is null and if so initialize
942 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800943 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800944 LIR* cont = NewLIR0(kPseudoTargetLabel);
945
946 // Object to generate the slow path for class resolution.
947 class SlowPath : public LIRSlowPath {
948 public:
949 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
950 const RegLocation& rl_method, const RegLocation& rl_result) :
951 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
952 rl_method_(rl_method), rl_result_(rl_result) {
953 }
954
955 void Compile() {
956 GenerateTargetLabel();
957
buzbee33ae5582014-06-12 14:56:32 -0700958 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700959 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
960 rl_method_.reg, true);
961 } else {
962 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
963 rl_method_.reg, true);
964 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700965 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800966
967 m2l_->OpUnconditionalBranch(cont_);
968 }
969
970 private:
971 const int type_idx_;
972 const RegLocation rl_method_;
973 const RegLocation rl_result_;
974 };
975
976 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800977 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800978
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800980 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 // Fast path, we're done - just store result
982 StoreValue(rl_dest, rl_result);
983 }
984 }
985}
986
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700987void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000989 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
990 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
992 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
993 // slow path, resolve string if not in dex cache
994 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700995 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800996
997 // If the Method* is already in a register, we can save a copy.
998 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800999 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001000 if (rl_method.location == kLocPhysReg) {
1001 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001002 DCHECK(!IsTemp(rl_method.reg));
1003 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001004 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001005 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001006 LoadCurrMethodDirect(r_method);
1007 }
buzbee695d13a2014-04-19 13:32:20 -07001008 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001009 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001010
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001012 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1013 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001014 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001015
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001016 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001017 // Object to generate the slow path for string resolution.
1018 class SlowPath : public LIRSlowPath {
1019 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001020 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1021 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1022 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 }
1024
1025 void Compile() {
1026 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001027 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001028 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1029 r_method_, string_idx_, true);
1030 } else {
1031 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1032 r_method_, string_idx_, true);
1033 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001034 m2l_->OpUnconditionalBranch(cont_);
1035 }
1036
1037 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001038 const RegStorage r_method_;
1039 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001040 };
1041
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001042 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001044
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001046 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 } else {
1048 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001049 RegStorage res_reg = AllocTempRef();
1050 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001051 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1052 kNotVolatile);
1053 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 StoreValue(rl_dest, rl_result);
1055 }
1056}
1057
Andreas Gampe2f244e92014-05-08 03:35:25 -07001058template <size_t pointer_size>
1059static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1060 RegLocation rl_dest) {
1061 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 // alloc will always check for resolution, do we also need to verify
1063 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001064 ThreadOffset<pointer_size> func_offset(-1);
1065 const DexFile* dex_file = cu->dex_file;
1066 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001067 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001068 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 bool is_type_initialized;
1070 bool use_direct_type_ptr;
1071 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001072 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001073 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001074 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1075 &direct_type_ptr, &is_finalizable) &&
1076 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001077 // The fast path.
1078 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001079 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001080 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001081 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -07001082 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1083 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001084 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001085 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
Andreas Gampeccc60262014-07-04 18:02:38 -07001086 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1087 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001088 }
1089 } else {
1090 // Use the direct pointer.
1091 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001092 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1093 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001094 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001095 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1096 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001097 }
1098 }
1099 } else {
1100 // The slow path.
1101 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001102 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1103 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001104 }
1105 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001107 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1108 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 }
buzbeea0cd2d72014-06-01 09:33:49 -07001110 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001111 mir_to_lir->StoreValue(rl_dest, rl_result);
1112}
1113
1114/*
1115 * Let helper function take care of everything. Will
1116 * call Class::NewInstanceFromCode(type_idx, method);
1117 */
1118void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001119 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001120 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1121 } else {
1122 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1123 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124}
1125
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001126void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001128 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001129 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1130 } else {
1131 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1132 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133}
1134
1135// For final classes there are no sub-classes to check and so we can answer the instance-of
1136// question with simple comparisons.
1137void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1138 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001139 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001140 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001141
buzbeea0cd2d72014-06-01 09:33:49 -07001142 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001144 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001145 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001147 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 }
1149 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001150 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151
buzbeea0cd2d72014-06-01 09:33:49 -07001152 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1153 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154
1155 LoadCurrMethodDirect(check_class);
1156 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001157 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1158 kNotVolatile);
1159 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1160 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 } else {
buzbee695d13a2014-04-19 13:32:20 -07001162 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001163 check_class, kNotVolatile);
1164 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1165 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001166 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001167 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 }
1169
buzbee695d13a2014-04-19 13:32:20 -07001170 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 if (cu_->instruction_set == kThumb2) {
1172 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001173 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001175 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001177 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 }
1179 LIR* target = NewLIR0(kPseudoTargetLabel);
1180 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 FreeTemp(object_class);
1182 FreeTemp(check_class);
1183 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001184 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 FreeTemp(result_reg);
1186 }
1187 StoreValue(rl_dest, rl_result);
1188}
1189
1190void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1191 bool type_known_abstract, bool use_declaring_class,
1192 bool can_assume_type_is_in_dex_cache,
1193 uint32_t type_idx, RegLocation rl_dest,
1194 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001195 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001196 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001197
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 FlushAllRegs();
1199 // May generate a call - use explicit registers
1200 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001201 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001202 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001203 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 if (needs_access_check) {
1205 // Check we have access to type_idx and if not throw IllegalAccessError,
1206 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001207 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001208 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1209 type_idx, true);
1210 } else {
1211 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1212 type_idx, true);
1213 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001214 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
1215 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 } else if (use_declaring_class) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001217 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001218 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001219 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001221 if (can_assume_type_is_in_dex_cache) {
1222 // Conditionally, as in the other case we will also load it.
1223 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
1224 }
1225
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001227 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001228 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001229 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001230 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001232 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1233 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1234
1235 // Should load value here.
1236 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
1237
1238 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1239 public:
1240 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1241 RegLocation rl_src)
1242 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1243 rl_src_(rl_src) {
1244 }
1245
1246 void Compile() OVERRIDE {
1247 GenerateTargetLabel();
1248
1249 if (cu_->target64) {
1250 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1251 true);
1252 } else {
1253 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1254 true);
1255 }
1256 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1257 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
1258
1259 m2l_->OpUnconditionalBranch(cont_);
1260 }
1261
1262 private:
1263 uint32_t type_idx_;
1264 RegLocation rl_src_;
1265 };
1266
1267 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1268 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 }
1270 }
1271 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001272 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 if (cu_->instruction_set == kMips) {
1274 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001275 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001277 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278
1279 /* load object->klass_ */
1280 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001281 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1282 TargetReg(kArg1, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1284 LIR* branchover = NULL;
1285 if (type_known_final) {
1286 // rl_result == ref == null == 0.
Andreas Gampe90969af2014-07-15 23:02:11 -07001287 GenSelectConst32(TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), kCondEq, 1, 0, rl_result.reg,
1288 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 } else {
1290 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001291 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001292 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1293 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001294 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 if (!type_known_abstract) {
1296 /* Uses conditional nullification */
Andreas Gampeccc60262014-07-04 18:02:38 -07001297 OpRegReg(kOpCmp, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001298 it = OpIT(kCondEq, "EE"); // if-convert the test
Andreas Gampeccc60262014-07-04 18:02:38 -07001299 LoadConstant(TargetReg(kArg0, kNotWide), 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001301 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001303 if (it != nullptr) {
1304 OpEndIT(it);
1305 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 FreeTemp(r_tgt);
1307 } else {
1308 if (!type_known_abstract) {
1309 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001310 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001311 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001313
Andreas Gampeccc60262014-07-04 18:02:38 -07001314 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Andreas Gampe90969af2014-07-15 23:02:11 -07001315 if (cu_->target64) {
1316 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial), false);
1317 } else {
1318 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial), false);
1319 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 }
1321 }
1322 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001323 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 /* branch targets here */
1325 LIR* target = NewLIR0(kPseudoTargetLabel);
1326 StoreValue(rl_dest, rl_result);
1327 branch1->target = target;
1328 if (branchover != NULL) {
1329 branchover->target = target;
1330 }
1331}
1332
1333void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1334 bool type_known_final, type_known_abstract, use_declaring_class;
1335 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1336 *cu_->dex_file,
1337 type_idx,
1338 &type_known_final,
1339 &type_known_abstract,
1340 &use_declaring_class);
1341 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1342 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1343
1344 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1345 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1346 } else {
1347 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1348 use_declaring_class, can_assume_type_is_in_dex_cache,
1349 type_idx, rl_dest, rl_src);
1350 }
1351}
1352
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001353void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 bool type_known_final, type_known_abstract, use_declaring_class;
1355 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1356 *cu_->dex_file,
1357 type_idx,
1358 &type_known_final,
1359 &type_known_abstract,
1360 &use_declaring_class);
1361 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1362 // of the exception throw path.
1363 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001364 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 // Verifier type analysis proved this check cast would never cause an exception.
1366 return;
1367 }
1368 FlushAllRegs();
1369 // May generate a call - use explicit registers
1370 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001371 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001372 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001373 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 if (needs_access_check) {
1375 // Check we have access to type_idx and if not throw IllegalAccessError,
1376 // returns Class* in kRet0
1377 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001378 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001379 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1380 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001381 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001382 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1383 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001384 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001385 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001387 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001388 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 } else {
1390 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001391 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001392 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001393 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001394 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1396 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001397 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1398 LIR* cont = NewLIR0(kPseudoTargetLabel);
1399
1400 // Slow path to initialize the type. Executed if the type is NULL.
1401 class SlowPath : public LIRSlowPath {
1402 public:
1403 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001404 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1406 class_reg_(class_reg) {
1407 }
1408
1409 void Compile() {
1410 GenerateTargetLabel();
1411
1412 // Call out to helper, which will return resolved type in kArg0
1413 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001414 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001415 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001416 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001417 } else {
1418 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001419 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001420 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001421 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001422 m2l_->OpUnconditionalBranch(cont_);
1423 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001424
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001425 public:
1426 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001427 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428 };
1429
buzbee2700f7e2014-03-07 09:46:20 -08001430 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 }
1432 }
1433 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001434 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001435
1436 // Slow path for the case where the classes are not equal. In this case we need
1437 // to call a helper function to do the check.
1438 class SlowPath : public LIRSlowPath {
1439 public:
1440 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1441 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1442 }
1443
1444 void Compile() {
1445 GenerateTargetLabel();
1446
1447 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001448 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1449 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001450 }
buzbee33ae5582014-06-12 14:56:32 -07001451 if (m2l_->cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001452 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast),
1453 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1454 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001455 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001456 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast),
1457 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1458 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001459 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001460
1461 m2l_->OpUnconditionalBranch(cont_);
1462 }
1463
1464 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001465 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001466 };
1467
1468 if (type_known_abstract) {
1469 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001470 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001471 LIR* cont = NewLIR0(kPseudoTargetLabel);
1472 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1473 } else {
1474 // Harder, more common case. We need to generate a forward branch over the load
1475 // if the target is null. If it's non-null we perform the load and branch to the
1476 // slow path if the classes are not equal.
1477
1478 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001479 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001480 /* load object->klass_ */
1481 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001482 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1483 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001484
Andreas Gampeccc60262014-07-04 18:02:38 -07001485 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001486 LIR* cont = NewLIR0(kPseudoTargetLabel);
1487
1488 // Add the slow path that will not perform load since this is already done.
1489 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1490
1491 // Set the null check to branch to the continuation.
1492 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 }
1494}
1495
1496void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001497 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 RegLocation rl_result;
1499 if (cu_->instruction_set == kThumb2) {
1500 /*
1501 * NOTE: This is the one place in the code in which we might have
1502 * as many as six live temporary registers. There are 5 in the normal
1503 * set for Arm. Until we have spill capabilities, temporarily add
1504 * lr to the temp set. It is safe to do this locally, but note that
1505 * lr is used explicitly elsewhere in the code generator and cannot
1506 * normally be used as a general temp register.
1507 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001508 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1509 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 }
1511 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1512 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1513 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1514 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001515 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1516 RegStorage t_reg = AllocTemp();
1517 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1518 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1519 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 FreeTemp(t_reg);
1521 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001522 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1523 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
1525 /*
1526 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1527 * following StoreValueWide might need to allocate a temp register.
1528 * To further work around the lack of a spill capability, explicitly
1529 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1530 * Remove when spill is functional.
1531 */
1532 FreeRegLocTemps(rl_result, rl_src1);
1533 FreeRegLocTemps(rl_result, rl_src2);
1534 StoreValueWide(rl_dest, rl_result);
1535 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001536 Clobber(TargetReg(kLr, kNotWide));
1537 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 }
1539}
1540
1541
Andreas Gampe2f244e92014-05-08 03:35:25 -07001542template <size_t pointer_size>
1543static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1544 RegLocation rl_shift) {
1545 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546
1547 switch (opcode) {
1548 case Instruction::SHL_LONG:
1549 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001550 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001551 break;
1552 case Instruction::SHR_LONG:
1553 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001554 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 break;
1556 case Instruction::USHR_LONG:
1557 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001558 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 break;
1560 default:
1561 LOG(FATAL) << "Unexpected case";
1562 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001563 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1564 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1565}
1566
1567void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1568 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001569 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001570 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1571 } else {
1572 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1573 }
buzbeea0cd2d72014-06-01 09:33:49 -07001574 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 StoreValueWide(rl_dest, rl_result);
1576}
1577
1578
1579void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001580 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001581 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 OpKind op = kOpBkpt;
1583 bool is_div_rem = false;
1584 bool check_zero = false;
1585 bool unary = false;
1586 RegLocation rl_result;
1587 bool shift_op = false;
1588 switch (opcode) {
1589 case Instruction::NEG_INT:
1590 op = kOpNeg;
1591 unary = true;
1592 break;
1593 case Instruction::NOT_INT:
1594 op = kOpMvn;
1595 unary = true;
1596 break;
1597 case Instruction::ADD_INT:
1598 case Instruction::ADD_INT_2ADDR:
1599 op = kOpAdd;
1600 break;
1601 case Instruction::SUB_INT:
1602 case Instruction::SUB_INT_2ADDR:
1603 op = kOpSub;
1604 break;
1605 case Instruction::MUL_INT:
1606 case Instruction::MUL_INT_2ADDR:
1607 op = kOpMul;
1608 break;
1609 case Instruction::DIV_INT:
1610 case Instruction::DIV_INT_2ADDR:
1611 check_zero = true;
1612 op = kOpDiv;
1613 is_div_rem = true;
1614 break;
1615 /* NOTE: returns in kArg1 */
1616 case Instruction::REM_INT:
1617 case Instruction::REM_INT_2ADDR:
1618 check_zero = true;
1619 op = kOpRem;
1620 is_div_rem = true;
1621 break;
1622 case Instruction::AND_INT:
1623 case Instruction::AND_INT_2ADDR:
1624 op = kOpAnd;
1625 break;
1626 case Instruction::OR_INT:
1627 case Instruction::OR_INT_2ADDR:
1628 op = kOpOr;
1629 break;
1630 case Instruction::XOR_INT:
1631 case Instruction::XOR_INT_2ADDR:
1632 op = kOpXor;
1633 break;
1634 case Instruction::SHL_INT:
1635 case Instruction::SHL_INT_2ADDR:
1636 shift_op = true;
1637 op = kOpLsl;
1638 break;
1639 case Instruction::SHR_INT:
1640 case Instruction::SHR_INT_2ADDR:
1641 shift_op = true;
1642 op = kOpAsr;
1643 break;
1644 case Instruction::USHR_INT:
1645 case Instruction::USHR_INT_2ADDR:
1646 shift_op = true;
1647 op = kOpLsr;
1648 break;
1649 default:
1650 LOG(FATAL) << "Invalid word arith op: " << opcode;
1651 }
1652 if (!is_div_rem) {
1653 if (unary) {
1654 rl_src1 = LoadValue(rl_src1, kCoreReg);
1655 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001656 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001657 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001658 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001659 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001660 RegStorage t_reg = AllocTemp();
1661 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 rl_src1 = LoadValue(rl_src1, kCoreReg);
1663 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001664 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 FreeTemp(t_reg);
1666 } else {
1667 rl_src1 = LoadValue(rl_src1, kCoreReg);
1668 rl_src2 = LoadValue(rl_src2, kCoreReg);
1669 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001670 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 }
1672 }
1673 StoreValue(rl_dest, rl_result);
1674 } else {
Dave Allison70202782013-10-22 17:52:19 -07001675 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 +01001676 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677 rl_src1 = LoadValue(rl_src1, kCoreReg);
1678 rl_src2 = LoadValue(rl_src2, kCoreReg);
1679 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001680 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 }
buzbee2700f7e2014-03-07 09:46:20 -08001682 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001683 done = true;
1684 } else if (cu_->instruction_set == kThumb2) {
1685 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1686 // Use ARM SDIV instruction for division. For remainder we also need to
1687 // calculate using a MUL and subtract.
1688 rl_src1 = LoadValue(rl_src1, kCoreReg);
1689 rl_src2 = LoadValue(rl_src2, kCoreReg);
1690 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001691 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001692 }
buzbee2700f7e2014-03-07 09:46:20 -08001693 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001694 done = true;
1695 }
1696 }
1697
1698 // If we haven't already generated the code use the callout function.
1699 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001701 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001702 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001703 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1704 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Andreas Gampeccc60262014-07-04 18:02:38 -07001705 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001707 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 }
Dave Allison70202782013-10-22 17:52:19 -07001709 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001710 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001711 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1712 } else {
1713 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1714 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001716 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 else
1718 rl_result = GetReturnAlt();
1719 }
1720 StoreValue(rl_dest, rl_result);
1721 }
1722}
1723
1724/*
1725 * The following are the first-level codegen routines that analyze the format
1726 * of each bytecode then either dispatch special purpose codegen routines
1727 * or produce corresponding Thumb instructions directly.
1728 */
1729
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001731static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 x &= x - 1;
1733 return (x & (x - 1)) == 0;
1734}
1735
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1737// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001738bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001739 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1741 return false;
1742 }
1743 // No divide instruction for Arm, so check for more special cases
1744 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001745 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 }
1747 int k = LowestSetBit(lit);
1748 if (k >= 30) {
1749 // Avoid special cases.
1750 return false;
1751 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 rl_src = LoadValue(rl_src, kCoreReg);
1753 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001754 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001755 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 if (lit == 2) {
1757 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001758 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1759 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1760 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001762 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001764 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1765 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 }
1767 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001768 RegStorage t_reg1 = AllocTemp();
1769 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001771 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1772 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001773 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001774 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001775 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001776 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001778 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001780 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 }
1782 }
1783 StoreValue(rl_dest, rl_result);
1784 return true;
1785}
1786
1787// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1788// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001789bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001790 if (lit < 0) {
1791 return false;
1792 }
1793 if (lit == 0) {
1794 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1795 LoadConstant(rl_result.reg, 0);
1796 StoreValue(rl_dest, rl_result);
1797 return true;
1798 }
1799 if (lit == 1) {
1800 rl_src = LoadValue(rl_src, kCoreReg);
1801 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1802 OpRegCopy(rl_result.reg, rl_src.reg);
1803 StoreValue(rl_dest, rl_result);
1804 return true;
1805 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001806 // There is RegRegRegShift on Arm, so check for more special cases
1807 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001808 return EasyMultiply(rl_src, rl_dest, lit);
1809 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001810 // Can we simplify this multiplication?
1811 bool power_of_two = false;
1812 bool pop_count_le2 = false;
1813 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001814 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 power_of_two = true;
1816 } else if (IsPopCountLE2(lit)) {
1817 pop_count_le2 = true;
1818 } else if (IsPowerOfTwo(lit + 1)) {
1819 power_of_two_minus_one = true;
1820 } else {
1821 return false;
1822 }
1823 rl_src = LoadValue(rl_src, kCoreReg);
1824 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1825 if (power_of_two) {
1826 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001827 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 } else if (pop_count_le2) {
1829 // Shift and add and shift.
1830 int first_bit = LowestSetBit(lit);
1831 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1832 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1833 } else {
1834 // Reverse subtract: (src << (shift + 1)) - src.
1835 DCHECK(power_of_two_minus_one);
1836 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001837 RegStorage t_reg = AllocTemp();
1838 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1839 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 }
1841 StoreValue(rl_dest, rl_result);
1842 return true;
1843}
1844
1845void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001846 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001847 RegLocation rl_result;
1848 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1849 int shift_op = false;
1850 bool is_div = false;
1851
1852 switch (opcode) {
1853 case Instruction::RSUB_INT_LIT8:
1854 case Instruction::RSUB_INT: {
1855 rl_src = LoadValue(rl_src, kCoreReg);
1856 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1857 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001858 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001859 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001860 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1861 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001862 }
1863 StoreValue(rl_dest, rl_result);
1864 return;
1865 }
1866
1867 case Instruction::SUB_INT:
1868 case Instruction::SUB_INT_2ADDR:
1869 lit = -lit;
1870 // Intended fallthrough
1871 case Instruction::ADD_INT:
1872 case Instruction::ADD_INT_2ADDR:
1873 case Instruction::ADD_INT_LIT8:
1874 case Instruction::ADD_INT_LIT16:
1875 op = kOpAdd;
1876 break;
1877 case Instruction::MUL_INT:
1878 case Instruction::MUL_INT_2ADDR:
1879 case Instruction::MUL_INT_LIT8:
1880 case Instruction::MUL_INT_LIT16: {
1881 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1882 return;
1883 }
1884 op = kOpMul;
1885 break;
1886 }
1887 case Instruction::AND_INT:
1888 case Instruction::AND_INT_2ADDR:
1889 case Instruction::AND_INT_LIT8:
1890 case Instruction::AND_INT_LIT16:
1891 op = kOpAnd;
1892 break;
1893 case Instruction::OR_INT:
1894 case Instruction::OR_INT_2ADDR:
1895 case Instruction::OR_INT_LIT8:
1896 case Instruction::OR_INT_LIT16:
1897 op = kOpOr;
1898 break;
1899 case Instruction::XOR_INT:
1900 case Instruction::XOR_INT_2ADDR:
1901 case Instruction::XOR_INT_LIT8:
1902 case Instruction::XOR_INT_LIT16:
1903 op = kOpXor;
1904 break;
1905 case Instruction::SHL_INT_LIT8:
1906 case Instruction::SHL_INT:
1907 case Instruction::SHL_INT_2ADDR:
1908 lit &= 31;
1909 shift_op = true;
1910 op = kOpLsl;
1911 break;
1912 case Instruction::SHR_INT_LIT8:
1913 case Instruction::SHR_INT:
1914 case Instruction::SHR_INT_2ADDR:
1915 lit &= 31;
1916 shift_op = true;
1917 op = kOpAsr;
1918 break;
1919 case Instruction::USHR_INT_LIT8:
1920 case Instruction::USHR_INT:
1921 case Instruction::USHR_INT_2ADDR:
1922 lit &= 31;
1923 shift_op = true;
1924 op = kOpLsr;
1925 break;
1926
1927 case Instruction::DIV_INT:
1928 case Instruction::DIV_INT_2ADDR:
1929 case Instruction::DIV_INT_LIT8:
1930 case Instruction::DIV_INT_LIT16:
1931 case Instruction::REM_INT:
1932 case Instruction::REM_INT_2ADDR:
1933 case Instruction::REM_INT_LIT8:
1934 case Instruction::REM_INT_LIT16: {
1935 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001936 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 return;
1938 }
buzbee11b63d12013-08-27 07:34:17 -07001939 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001941 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001942 (opcode == Instruction::DIV_INT_LIT16)) {
1943 is_div = true;
1944 } else {
1945 is_div = false;
1946 }
buzbee11b63d12013-08-27 07:34:17 -07001947 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1948 return;
1949 }
Dave Allison70202782013-10-22 17:52:19 -07001950
1951 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001952 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001953 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001954 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001955 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001956 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001957 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1958 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001959 } else if (cu_->instruction_set == kThumb2) {
1960 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1961 // Use ARM SDIV instruction for division. For remainder we also need to
1962 // calculate using a MUL and subtract.
1963 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001964 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001965 done = true;
1966 }
1967 }
1968
1969 if (!done) {
1970 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001971 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1972 Clobber(TargetReg(kArg0, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001973 if (cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001974 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, kNotWide),
1975 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001976 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001977 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, kNotWide),
1978 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001979 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001981 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 else
1983 rl_result = GetReturnAlt();
1984 }
1985 StoreValue(rl_dest, rl_result);
1986 return;
1987 }
1988 default:
1989 LOG(FATAL) << "Unexpected opcode " << opcode;
1990 }
1991 rl_src = LoadValue(rl_src, kCoreReg);
1992 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001993 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001995 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001997 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 }
1999 StoreValue(rl_dest, rl_result);
2000}
2001
Andreas Gampe2f244e92014-05-08 03:35:25 -07002002template <size_t pointer_size>
2003static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
2004 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 RegLocation rl_result;
2006 OpKind first_op = kOpBkpt;
2007 OpKind second_op = kOpBkpt;
2008 bool call_out = false;
2009 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010 ThreadOffset<pointer_size> func_offset(-1);
Andreas Gampeccc60262014-07-04 18:02:38 -07002011 int ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012
2013 switch (opcode) {
2014 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07002015 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002016 mir_to_lir->GenNotLong(rl_dest, rl_src2);
2017 return;
2018 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002019 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
2020 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002021 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002022 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002023 RegStorage t_reg = mir_to_lir->AllocTemp();
2024 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2025 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2026 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2027 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002029 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2030 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002032 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 return;
2034 case Instruction::ADD_LONG:
2035 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002036 if (cu->instruction_set != kThumb2) {
2037 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 return;
2039 }
2040 first_op = kOpAdd;
2041 second_op = kOpAdc;
2042 break;
2043 case Instruction::SUB_LONG:
2044 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002045 if (cu->instruction_set != kThumb2) {
2046 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 return;
2048 }
2049 first_op = kOpSub;
2050 second_op = kOpSbc;
2051 break;
2052 case Instruction::MUL_LONG:
2053 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002054 if (cu->instruction_set != kMips) {
2055 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002056 return;
2057 } else {
2058 call_out = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002059 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002060 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 }
2062 break;
2063 case Instruction::DIV_LONG:
2064 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002065 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002066 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2067 return;
2068 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 call_out = true;
2070 check_zero = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002071 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002072 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 break;
2074 case Instruction::REM_LONG:
2075 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002076 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002077 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2078 return;
2079 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 call_out = true;
2081 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002082 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampeccc60262014-07-04 18:02:38 -07002084 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, kNotWide).GetReg() :
2085 mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002086 break;
2087 case Instruction::AND_LONG_2ADDR:
2088 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002089 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2090 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002091 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 }
2093 first_op = kOpAnd;
2094 second_op = kOpAnd;
2095 break;
2096 case Instruction::OR_LONG:
2097 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002098 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2099 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002100 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 return;
2102 }
2103 first_op = kOpOr;
2104 second_op = kOpOr;
2105 break;
2106 case Instruction::XOR_LONG:
2107 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002108 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2109 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002110 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002111 return;
2112 }
2113 first_op = kOpXor;
2114 second_op = kOpXor;
2115 break;
2116 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002117 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002118 return;
2119 }
2120 default:
2121 LOG(FATAL) << "Invalid long arith op";
2122 }
2123 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002124 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002126 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002127 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002128 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kWide);
2129 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kWide);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002130 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2131 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Andreas Gampeccc60262014-07-04 18:02:38 -07002132 mir_to_lir->GenDivZeroCheckWide(r_tmp2);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002133 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002134 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002135 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002137 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002138 }
2139 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampeccc60262014-07-04 18:02:38 -07002140 if (ret_reg == mir_to_lir->TargetReg(kRet0, kNotWide).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002141 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002142 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002143 rl_result = mir_to_lir->GetReturnWideAlt();
2144 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002145 }
2146}
2147
Andreas Gampe2f244e92014-05-08 03:35:25 -07002148void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2149 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002150 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002151 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2152 } else {
2153 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2154 }
2155}
2156
Mark Mendelle87f9b52014-04-30 14:13:18 -04002157void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2158 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2159 LoadConstantNoClobber(rl_result.reg, value);
2160 StoreValue(rl_dest, rl_result);
2161 if (value == 0) {
2162 Workaround7250540(rl_dest, rl_result.reg);
2163 }
2164}
2165
Andreas Gampe2f244e92014-05-08 03:35:25 -07002166template <size_t pointer_size>
2167void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002168 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002169 /*
2170 * Don't optimize the register usage since it calls out to support
2171 * functions
2172 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002173 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2174
Brian Carlstrom7940e442013-07-12 13:46:57 -07002175 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002176 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2177 if (rl_dest.wide) {
2178 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002179 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002180 StoreValueWide(rl_dest, rl_result);
2181 } else {
2182 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002183 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002184 StoreValue(rl_dest, rl_result);
2185 }
2186}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002187template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2188 RegLocation rl_dest, RegLocation rl_src);
2189template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2190 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002191
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002192class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2193 public:
2194 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2195 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2196 }
2197
2198 void Compile() OVERRIDE {
2199 m2l_->ResetRegPool();
2200 m2l_->ResetDefTracking();
2201 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002202 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002203 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2204 } else {
2205 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2206 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002207 if (cont_ != nullptr) {
2208 m2l_->OpUnconditionalBranch(cont_);
2209 }
2210 }
2211};
2212
Brian Carlstrom7940e442013-07-12 13:46:57 -07002213/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002214void Mir2Lir::GenSuspendTest(int opt_flags) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +00002215 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002216 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2217 return;
2218 }
2219 FlushAllRegs();
2220 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002221 LIR* cont = NewLIR0(kPseudoTargetLabel);
2222 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002223 } else {
2224 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2225 return;
2226 }
2227 FlushAllRegs(); // TODO: needed?
2228 LIR* inst = CheckSuspendUsingLoad();
2229 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002230 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002231}
2232
2233/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002234void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +00002235 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002236 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2237 OpUnconditionalBranch(target);
2238 return;
2239 }
2240 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002241 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002242 LIR* branch = OpUnconditionalBranch(nullptr);
2243 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002244 } else {
2245 // For the implicit suspend check, just perform the trigger
2246 // load and branch to the target.
2247 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2248 OpUnconditionalBranch(target);
2249 return;
2250 }
2251 FlushAllRegs();
2252 LIR* inst = CheckSuspendUsingLoad();
2253 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002254 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002255 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002256}
2257
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002258/* Call out to helper assembly routine that will null check obj and then lock it. */
2259void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2260 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002261 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002262 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2263 } else {
2264 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2265 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002266}
2267
2268/* Call out to helper assembly routine that will null check obj and then unlock it. */
2269void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2270 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002271 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002272 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2273 } else {
2274 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2275 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002276}
2277
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002278/* Generic code for generating a wide constant into a VR. */
2279void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2280 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002281 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002282 StoreValueWide(rl_dest, rl_result);
2283}
2284
Brian Carlstrom7940e442013-07-12 13:46:57 -07002285} // namespace art