blob: 1fc0cff678f811905a4599633fcac713c9eff6ed [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
1170 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001171 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (cu_->instruction_set == kThumb2) {
1173 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001174 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001176 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 } else {
1178 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1179 LoadConstant(result_reg, 1); // eq case - load true
1180 }
1181 LIR* target = NewLIR0(kPseudoTargetLabel);
1182 null_branchover->target = target;
1183 if (ne_branchover != NULL) {
1184 ne_branchover->target = target;
1185 }
1186 FreeTemp(object_class);
1187 FreeTemp(check_class);
1188 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001189 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 FreeTemp(result_reg);
1191 }
1192 StoreValue(rl_dest, rl_result);
1193}
1194
1195void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1196 bool type_known_abstract, bool use_declaring_class,
1197 bool can_assume_type_is_in_dex_cache,
1198 uint32_t type_idx, RegLocation rl_dest,
1199 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001200 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001201 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001202
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 FlushAllRegs();
1204 // May generate a call - use explicit registers
1205 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001206 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001207 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001208 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 if (needs_access_check) {
1210 // Check we have access to type_idx and if not throw IllegalAccessError,
1211 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001212 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001213 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1214 type_idx, true);
1215 } else {
1216 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1217 type_idx, true);
1218 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001219 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
1220 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 } else if (use_declaring_class) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001222 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001223 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001224 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 } else {
1226 // Load dex cache entry into class_reg (kArg2)
Andreas Gampeccc60262014-07-04 18:02:38 -07001227 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001228 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001229 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001230 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001231 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 if (!can_assume_type_is_in_dex_cache) {
1233 // Need to test presence of type in dex cache at runtime
1234 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1235 // Not resolved
1236 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001237 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001238 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1239 } else {
1240 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1241 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001242 OpRegCopy(TargetReg(kArg2, kRef), TargetReg(kRet0, kRef)); // Align usage with fast path
1243 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); /* reload Ref */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 // Rejoin code paths
1245 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1246 hop_branch->target = hop_target;
1247 }
1248 }
1249 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001250 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (cu_->instruction_set == kMips) {
1252 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001253 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001255 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256
1257 /* load object->klass_ */
1258 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001259 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1260 TargetReg(kArg1, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1262 LIR* branchover = NULL;
1263 if (type_known_final) {
1264 // rl_result == ref == null == 0.
1265 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001266 OpRegReg(kOpCmp, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001267 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001268 LoadConstant(rl_result.reg, 1); // .eq case - load true
1269 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001270 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001272 LoadConstant(rl_result.reg, 0); // ne case - load false
Andreas Gampeccc60262014-07-04 18:02:38 -07001273 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001274 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 }
1276 } else {
1277 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001278 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001279 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1280 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001281 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 if (!type_known_abstract) {
1283 /* Uses conditional nullification */
Andreas Gampeccc60262014-07-04 18:02:38 -07001284 OpRegReg(kOpCmp, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001285 it = OpIT(kCondEq, "EE"); // if-convert the test
Andreas Gampeccc60262014-07-04 18:02:38 -07001286 LoadConstant(TargetReg(kArg0, kNotWide), 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001288 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001290 if (it != nullptr) {
1291 OpEndIT(it);
1292 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 FreeTemp(r_tgt);
1294 } else {
1295 if (!type_known_abstract) {
1296 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001297 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001298 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 }
buzbee33ae5582014-06-12 14:56:32 -07001300 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001301 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1302 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Andreas Gampeccc60262014-07-04 18:02:38 -07001303 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Mark Mendell6607d972014-02-10 06:54:18 -08001304 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1305 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 }
1307 }
1308 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001309 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 /* branch targets here */
1311 LIR* target = NewLIR0(kPseudoTargetLabel);
1312 StoreValue(rl_dest, rl_result);
1313 branch1->target = target;
1314 if (branchover != NULL) {
1315 branchover->target = target;
1316 }
1317}
1318
1319void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1320 bool type_known_final, type_known_abstract, use_declaring_class;
1321 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1322 *cu_->dex_file,
1323 type_idx,
1324 &type_known_final,
1325 &type_known_abstract,
1326 &use_declaring_class);
1327 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1328 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1329
1330 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1331 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1332 } else {
1333 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1334 use_declaring_class, can_assume_type_is_in_dex_cache,
1335 type_idx, rl_dest, rl_src);
1336 }
1337}
1338
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001339void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 bool type_known_final, type_known_abstract, use_declaring_class;
1341 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1342 *cu_->dex_file,
1343 type_idx,
1344 &type_known_final,
1345 &type_known_abstract,
1346 &use_declaring_class);
1347 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1348 // of the exception throw path.
1349 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001350 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 // Verifier type analysis proved this check cast would never cause an exception.
1352 return;
1353 }
1354 FlushAllRegs();
1355 // May generate a call - use explicit registers
1356 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001357 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001358 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001359 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 if (needs_access_check) {
1361 // Check we have access to type_idx and if not throw IllegalAccessError,
1362 // returns Class* in kRet0
1363 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001364 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001365 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1366 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001367 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001368 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1369 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001370 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001371 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001373 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001374 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 } else {
1376 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001377 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001378 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001379 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001380 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1382 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001383 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1384 LIR* cont = NewLIR0(kPseudoTargetLabel);
1385
1386 // Slow path to initialize the type. Executed if the type is NULL.
1387 class SlowPath : public LIRSlowPath {
1388 public:
1389 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001390 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001391 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1392 class_reg_(class_reg) {
1393 }
1394
1395 void Compile() {
1396 GenerateTargetLabel();
1397
1398 // Call out to helper, which will return resolved type in kArg0
1399 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001400 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001401 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001402 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001403 } else {
1404 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001405 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001406 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001407 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001408 m2l_->OpUnconditionalBranch(cont_);
1409 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001410
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001411 public:
1412 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001413 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001414 };
1415
buzbee2700f7e2014-03-07 09:46:20 -08001416 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 }
1418 }
1419 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001420 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001421
1422 // Slow path for the case where the classes are not equal. In this case we need
1423 // to call a helper function to do the check.
1424 class SlowPath : public LIRSlowPath {
1425 public:
1426 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1427 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1428 }
1429
1430 void Compile() {
1431 GenerateTargetLabel();
1432
1433 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001434 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1435 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001436 }
buzbee33ae5582014-06-12 14:56:32 -07001437 if (m2l_->cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001438 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast),
1439 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1440 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001441 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001442 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast),
1443 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1444 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001445 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001446
1447 m2l_->OpUnconditionalBranch(cont_);
1448 }
1449
1450 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001451 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001452 };
1453
1454 if (type_known_abstract) {
1455 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001456 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001457 LIR* cont = NewLIR0(kPseudoTargetLabel);
1458 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1459 } else {
1460 // Harder, more common case. We need to generate a forward branch over the load
1461 // if the target is null. If it's non-null we perform the load and branch to the
1462 // slow path if the classes are not equal.
1463
1464 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001465 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001466 /* load object->klass_ */
1467 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001468 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1469 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001470
Andreas Gampeccc60262014-07-04 18:02:38 -07001471 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001472 LIR* cont = NewLIR0(kPseudoTargetLabel);
1473
1474 // Add the slow path that will not perform load since this is already done.
1475 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1476
1477 // Set the null check to branch to the continuation.
1478 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 }
1480}
1481
1482void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001483 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 RegLocation rl_result;
1485 if (cu_->instruction_set == kThumb2) {
1486 /*
1487 * NOTE: This is the one place in the code in which we might have
1488 * as many as six live temporary registers. There are 5 in the normal
1489 * set for Arm. Until we have spill capabilities, temporarily add
1490 * lr to the temp set. It is safe to do this locally, but note that
1491 * lr is used explicitly elsewhere in the code generator and cannot
1492 * normally be used as a general temp register.
1493 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001494 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1495 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 }
1497 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1498 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1499 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1500 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001501 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1502 RegStorage t_reg = AllocTemp();
1503 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1504 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1505 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 FreeTemp(t_reg);
1507 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1509 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 }
1511 /*
1512 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1513 * following StoreValueWide might need to allocate a temp register.
1514 * To further work around the lack of a spill capability, explicitly
1515 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1516 * Remove when spill is functional.
1517 */
1518 FreeRegLocTemps(rl_result, rl_src1);
1519 FreeRegLocTemps(rl_result, rl_src2);
1520 StoreValueWide(rl_dest, rl_result);
1521 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001522 Clobber(TargetReg(kLr, kNotWide));
1523 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
1525}
1526
1527
Andreas Gampe2f244e92014-05-08 03:35:25 -07001528template <size_t pointer_size>
1529static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1530 RegLocation rl_shift) {
1531 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532
1533 switch (opcode) {
1534 case Instruction::SHL_LONG:
1535 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001536 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 break;
1538 case Instruction::SHR_LONG:
1539 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001540 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 break;
1542 case Instruction::USHR_LONG:
1543 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001544 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 break;
1546 default:
1547 LOG(FATAL) << "Unexpected case";
1548 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001549 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1550 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1551}
1552
1553void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1554 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001555 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001556 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1557 } else {
1558 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1559 }
buzbeea0cd2d72014-06-01 09:33:49 -07001560 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 StoreValueWide(rl_dest, rl_result);
1562}
1563
1564
1565void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001566 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001567 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 OpKind op = kOpBkpt;
1569 bool is_div_rem = false;
1570 bool check_zero = false;
1571 bool unary = false;
1572 RegLocation rl_result;
1573 bool shift_op = false;
1574 switch (opcode) {
1575 case Instruction::NEG_INT:
1576 op = kOpNeg;
1577 unary = true;
1578 break;
1579 case Instruction::NOT_INT:
1580 op = kOpMvn;
1581 unary = true;
1582 break;
1583 case Instruction::ADD_INT:
1584 case Instruction::ADD_INT_2ADDR:
1585 op = kOpAdd;
1586 break;
1587 case Instruction::SUB_INT:
1588 case Instruction::SUB_INT_2ADDR:
1589 op = kOpSub;
1590 break;
1591 case Instruction::MUL_INT:
1592 case Instruction::MUL_INT_2ADDR:
1593 op = kOpMul;
1594 break;
1595 case Instruction::DIV_INT:
1596 case Instruction::DIV_INT_2ADDR:
1597 check_zero = true;
1598 op = kOpDiv;
1599 is_div_rem = true;
1600 break;
1601 /* NOTE: returns in kArg1 */
1602 case Instruction::REM_INT:
1603 case Instruction::REM_INT_2ADDR:
1604 check_zero = true;
1605 op = kOpRem;
1606 is_div_rem = true;
1607 break;
1608 case Instruction::AND_INT:
1609 case Instruction::AND_INT_2ADDR:
1610 op = kOpAnd;
1611 break;
1612 case Instruction::OR_INT:
1613 case Instruction::OR_INT_2ADDR:
1614 op = kOpOr;
1615 break;
1616 case Instruction::XOR_INT:
1617 case Instruction::XOR_INT_2ADDR:
1618 op = kOpXor;
1619 break;
1620 case Instruction::SHL_INT:
1621 case Instruction::SHL_INT_2ADDR:
1622 shift_op = true;
1623 op = kOpLsl;
1624 break;
1625 case Instruction::SHR_INT:
1626 case Instruction::SHR_INT_2ADDR:
1627 shift_op = true;
1628 op = kOpAsr;
1629 break;
1630 case Instruction::USHR_INT:
1631 case Instruction::USHR_INT_2ADDR:
1632 shift_op = true;
1633 op = kOpLsr;
1634 break;
1635 default:
1636 LOG(FATAL) << "Invalid word arith op: " << opcode;
1637 }
1638 if (!is_div_rem) {
1639 if (unary) {
1640 rl_src1 = LoadValue(rl_src1, kCoreReg);
1641 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001642 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001644 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001645 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001646 RegStorage t_reg = AllocTemp();
1647 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 rl_src1 = LoadValue(rl_src1, kCoreReg);
1649 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001650 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 FreeTemp(t_reg);
1652 } else {
1653 rl_src1 = LoadValue(rl_src1, kCoreReg);
1654 rl_src2 = LoadValue(rl_src2, kCoreReg);
1655 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001656 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001657 }
1658 }
1659 StoreValue(rl_dest, rl_result);
1660 } else {
Dave Allison70202782013-10-22 17:52:19 -07001661 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 +01001662 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 rl_src1 = LoadValue(rl_src1, kCoreReg);
1664 rl_src2 = LoadValue(rl_src2, kCoreReg);
1665 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001666 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 }
buzbee2700f7e2014-03-07 09:46:20 -08001668 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001669 done = true;
1670 } else if (cu_->instruction_set == kThumb2) {
1671 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1672 // Use ARM SDIV instruction for division. For remainder we also need to
1673 // calculate using a MUL and subtract.
1674 rl_src1 = LoadValue(rl_src1, kCoreReg);
1675 rl_src2 = LoadValue(rl_src2, kCoreReg);
1676 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001677 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001678 }
buzbee2700f7e2014-03-07 09:46:20 -08001679 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001680 done = true;
1681 }
1682 }
1683
1684 // If we haven't already generated the code use the callout function.
1685 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001687 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001688 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001689 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1690 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Andreas Gampeccc60262014-07-04 18:02:38 -07001691 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001693 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 }
Dave Allison70202782013-10-22 17:52:19 -07001695 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001696 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001697 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1698 } else {
1699 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1700 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001702 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 else
1704 rl_result = GetReturnAlt();
1705 }
1706 StoreValue(rl_dest, rl_result);
1707 }
1708}
1709
1710/*
1711 * The following are the first-level codegen routines that analyze the format
1712 * of each bytecode then either dispatch special purpose codegen routines
1713 * or produce corresponding Thumb instructions directly.
1714 */
1715
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001717static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 x &= x - 1;
1719 return (x & (x - 1)) == 0;
1720}
1721
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1723// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001724bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001725 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1727 return false;
1728 }
1729 // No divide instruction for Arm, so check for more special cases
1730 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001731 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 }
1733 int k = LowestSetBit(lit);
1734 if (k >= 30) {
1735 // Avoid special cases.
1736 return false;
1737 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 rl_src = LoadValue(rl_src, kCoreReg);
1739 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001740 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001741 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001742 if (lit == 2) {
1743 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001744 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1745 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1746 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001748 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001750 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1751 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 }
1753 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001754 RegStorage t_reg1 = AllocTemp();
1755 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001757 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1758 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001760 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001762 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001764 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001766 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 }
1768 }
1769 StoreValue(rl_dest, rl_result);
1770 return true;
1771}
1772
1773// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1774// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001775bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001776 if (lit < 0) {
1777 return false;
1778 }
1779 if (lit == 0) {
1780 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1781 LoadConstant(rl_result.reg, 0);
1782 StoreValue(rl_dest, rl_result);
1783 return true;
1784 }
1785 if (lit == 1) {
1786 rl_src = LoadValue(rl_src, kCoreReg);
1787 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1788 OpRegCopy(rl_result.reg, rl_src.reg);
1789 StoreValue(rl_dest, rl_result);
1790 return true;
1791 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001792 // There is RegRegRegShift on Arm, so check for more special cases
1793 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001794 return EasyMultiply(rl_src, rl_dest, lit);
1795 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 // Can we simplify this multiplication?
1797 bool power_of_two = false;
1798 bool pop_count_le2 = false;
1799 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001800 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 power_of_two = true;
1802 } else if (IsPopCountLE2(lit)) {
1803 pop_count_le2 = true;
1804 } else if (IsPowerOfTwo(lit + 1)) {
1805 power_of_two_minus_one = true;
1806 } else {
1807 return false;
1808 }
1809 rl_src = LoadValue(rl_src, kCoreReg);
1810 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1811 if (power_of_two) {
1812 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001813 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 } else if (pop_count_le2) {
1815 // Shift and add and shift.
1816 int first_bit = LowestSetBit(lit);
1817 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1818 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1819 } else {
1820 // Reverse subtract: (src << (shift + 1)) - src.
1821 DCHECK(power_of_two_minus_one);
1822 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001823 RegStorage t_reg = AllocTemp();
1824 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1825 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 }
1827 StoreValue(rl_dest, rl_result);
1828 return true;
1829}
1830
1831void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001832 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001833 RegLocation rl_result;
1834 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1835 int shift_op = false;
1836 bool is_div = false;
1837
1838 switch (opcode) {
1839 case Instruction::RSUB_INT_LIT8:
1840 case Instruction::RSUB_INT: {
1841 rl_src = LoadValue(rl_src, kCoreReg);
1842 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1843 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001844 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001845 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001846 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1847 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 }
1849 StoreValue(rl_dest, rl_result);
1850 return;
1851 }
1852
1853 case Instruction::SUB_INT:
1854 case Instruction::SUB_INT_2ADDR:
1855 lit = -lit;
1856 // Intended fallthrough
1857 case Instruction::ADD_INT:
1858 case Instruction::ADD_INT_2ADDR:
1859 case Instruction::ADD_INT_LIT8:
1860 case Instruction::ADD_INT_LIT16:
1861 op = kOpAdd;
1862 break;
1863 case Instruction::MUL_INT:
1864 case Instruction::MUL_INT_2ADDR:
1865 case Instruction::MUL_INT_LIT8:
1866 case Instruction::MUL_INT_LIT16: {
1867 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1868 return;
1869 }
1870 op = kOpMul;
1871 break;
1872 }
1873 case Instruction::AND_INT:
1874 case Instruction::AND_INT_2ADDR:
1875 case Instruction::AND_INT_LIT8:
1876 case Instruction::AND_INT_LIT16:
1877 op = kOpAnd;
1878 break;
1879 case Instruction::OR_INT:
1880 case Instruction::OR_INT_2ADDR:
1881 case Instruction::OR_INT_LIT8:
1882 case Instruction::OR_INT_LIT16:
1883 op = kOpOr;
1884 break;
1885 case Instruction::XOR_INT:
1886 case Instruction::XOR_INT_2ADDR:
1887 case Instruction::XOR_INT_LIT8:
1888 case Instruction::XOR_INT_LIT16:
1889 op = kOpXor;
1890 break;
1891 case Instruction::SHL_INT_LIT8:
1892 case Instruction::SHL_INT:
1893 case Instruction::SHL_INT_2ADDR:
1894 lit &= 31;
1895 shift_op = true;
1896 op = kOpLsl;
1897 break;
1898 case Instruction::SHR_INT_LIT8:
1899 case Instruction::SHR_INT:
1900 case Instruction::SHR_INT_2ADDR:
1901 lit &= 31;
1902 shift_op = true;
1903 op = kOpAsr;
1904 break;
1905 case Instruction::USHR_INT_LIT8:
1906 case Instruction::USHR_INT:
1907 case Instruction::USHR_INT_2ADDR:
1908 lit &= 31;
1909 shift_op = true;
1910 op = kOpLsr;
1911 break;
1912
1913 case Instruction::DIV_INT:
1914 case Instruction::DIV_INT_2ADDR:
1915 case Instruction::DIV_INT_LIT8:
1916 case Instruction::DIV_INT_LIT16:
1917 case Instruction::REM_INT:
1918 case Instruction::REM_INT_2ADDR:
1919 case Instruction::REM_INT_LIT8:
1920 case Instruction::REM_INT_LIT16: {
1921 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001922 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 return;
1924 }
buzbee11b63d12013-08-27 07:34:17 -07001925 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001926 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001927 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 (opcode == Instruction::DIV_INT_LIT16)) {
1929 is_div = true;
1930 } else {
1931 is_div = false;
1932 }
buzbee11b63d12013-08-27 07:34:17 -07001933 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1934 return;
1935 }
Dave Allison70202782013-10-22 17:52:19 -07001936
1937 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001938 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001940 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001941 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001942 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001943 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1944 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001945 } else if (cu_->instruction_set == kThumb2) {
1946 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1947 // Use ARM SDIV instruction for division. For remainder we also need to
1948 // calculate using a MUL and subtract.
1949 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001950 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001951 done = true;
1952 }
1953 }
1954
1955 if (!done) {
1956 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001957 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1958 Clobber(TargetReg(kArg0, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001959 if (cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001960 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, kNotWide),
1961 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001962 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001963 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, kNotWide),
1964 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001965 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001967 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 else
1969 rl_result = GetReturnAlt();
1970 }
1971 StoreValue(rl_dest, rl_result);
1972 return;
1973 }
1974 default:
1975 LOG(FATAL) << "Unexpected opcode " << opcode;
1976 }
1977 rl_src = LoadValue(rl_src, kCoreReg);
1978 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001979 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001981 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001983 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 }
1985 StoreValue(rl_dest, rl_result);
1986}
1987
Andreas Gampe2f244e92014-05-08 03:35:25 -07001988template <size_t pointer_size>
1989static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1990 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 RegLocation rl_result;
1992 OpKind first_op = kOpBkpt;
1993 OpKind second_op = kOpBkpt;
1994 bool call_out = false;
1995 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001996 ThreadOffset<pointer_size> func_offset(-1);
Andreas Gampeccc60262014-07-04 18:02:38 -07001997 int ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998
1999 switch (opcode) {
2000 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07002001 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002002 mir_to_lir->GenNotLong(rl_dest, rl_src2);
2003 return;
2004 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002005 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
2006 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002007 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002008 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002009 RegStorage t_reg = mir_to_lir->AllocTemp();
2010 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2011 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2012 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2013 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002015 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2016 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002018 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 return;
2020 case Instruction::ADD_LONG:
2021 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002022 if (cu->instruction_set != kThumb2) {
2023 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024 return;
2025 }
2026 first_op = kOpAdd;
2027 second_op = kOpAdc;
2028 break;
2029 case Instruction::SUB_LONG:
2030 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002031 if (cu->instruction_set != kThumb2) {
2032 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 return;
2034 }
2035 first_op = kOpSub;
2036 second_op = kOpSbc;
2037 break;
2038 case Instruction::MUL_LONG:
2039 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002040 if (cu->instruction_set != kMips) {
2041 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 return;
2043 } else {
2044 call_out = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002045 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002046 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 }
2048 break;
2049 case Instruction::DIV_LONG:
2050 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002051 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002052 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2053 return;
2054 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055 call_out = true;
2056 check_zero = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002057 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002058 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 break;
2060 case Instruction::REM_LONG:
2061 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002062 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002063 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2064 return;
2065 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066 call_out = true;
2067 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002068 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampeccc60262014-07-04 18:02:38 -07002070 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, kNotWide).GetReg() :
2071 mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 break;
2073 case Instruction::AND_LONG_2ADDR:
2074 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002075 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2076 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002077 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 }
2079 first_op = kOpAnd;
2080 second_op = kOpAnd;
2081 break;
2082 case Instruction::OR_LONG:
2083 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002084 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2085 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002086 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002087 return;
2088 }
2089 first_op = kOpOr;
2090 second_op = kOpOr;
2091 break;
2092 case Instruction::XOR_LONG:
2093 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002094 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2095 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002096 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 return;
2098 }
2099 first_op = kOpXor;
2100 second_op = kOpXor;
2101 break;
2102 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002103 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 return;
2105 }
2106 default:
2107 LOG(FATAL) << "Invalid long arith op";
2108 }
2109 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002110 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002111 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002112 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002113 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002114 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kWide);
2115 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kWide);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002116 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2117 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Andreas Gampeccc60262014-07-04 18:02:38 -07002118 mir_to_lir->GenDivZeroCheckWide(r_tmp2);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002119 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002120 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002121 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002123 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002124 }
2125 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampeccc60262014-07-04 18:02:38 -07002126 if (ret_reg == mir_to_lir->TargetReg(kRet0, kNotWide).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002127 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002128 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002129 rl_result = mir_to_lir->GetReturnWideAlt();
2130 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002131 }
2132}
2133
Andreas Gampe2f244e92014-05-08 03:35:25 -07002134void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2135 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002136 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002137 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2138 } else {
2139 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2140 }
2141}
2142
Mark Mendelle87f9b52014-04-30 14:13:18 -04002143void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2144 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2145 LoadConstantNoClobber(rl_result.reg, value);
2146 StoreValue(rl_dest, rl_result);
2147 if (value == 0) {
2148 Workaround7250540(rl_dest, rl_result.reg);
2149 }
2150}
2151
Andreas Gampe2f244e92014-05-08 03:35:25 -07002152template <size_t pointer_size>
2153void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002154 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002155 /*
2156 * Don't optimize the register usage since it calls out to support
2157 * functions
2158 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002159 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2160
Brian Carlstrom7940e442013-07-12 13:46:57 -07002161 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002162 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2163 if (rl_dest.wide) {
2164 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002165 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002166 StoreValueWide(rl_dest, rl_result);
2167 } else {
2168 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002169 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002170 StoreValue(rl_dest, rl_result);
2171 }
2172}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002173template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2174 RegLocation rl_dest, RegLocation rl_src);
2175template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2176 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002177
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002178class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2179 public:
2180 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2181 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2182 }
2183
2184 void Compile() OVERRIDE {
2185 m2l_->ResetRegPool();
2186 m2l_->ResetDefTracking();
2187 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002188 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002189 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2190 } else {
2191 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2192 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002193 if (cont_ != nullptr) {
2194 m2l_->OpUnconditionalBranch(cont_);
2195 }
2196 }
2197};
2198
Brian Carlstrom7940e442013-07-12 13:46:57 -07002199/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002200void Mir2Lir::GenSuspendTest(int opt_flags) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +00002201 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002202 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2203 return;
2204 }
2205 FlushAllRegs();
2206 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002207 LIR* cont = NewLIR0(kPseudoTargetLabel);
2208 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002209 } else {
2210 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2211 return;
2212 }
2213 FlushAllRegs(); // TODO: needed?
2214 LIR* inst = CheckSuspendUsingLoad();
2215 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002216 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002217}
2218
2219/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002220void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Nicolas Geoffray0025a862014-07-11 08:26:40 +00002221 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002222 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2223 OpUnconditionalBranch(target);
2224 return;
2225 }
2226 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002227 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002228 LIR* branch = OpUnconditionalBranch(nullptr);
2229 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002230 } else {
2231 // For the implicit suspend check, just perform the trigger
2232 // load and branch to the target.
2233 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2234 OpUnconditionalBranch(target);
2235 return;
2236 }
2237 FlushAllRegs();
2238 LIR* inst = CheckSuspendUsingLoad();
2239 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002240 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002242}
2243
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002244/* Call out to helper assembly routine that will null check obj and then lock it. */
2245void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2246 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002247 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002248 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2249 } else {
2250 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2251 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002252}
2253
2254/* Call out to helper assembly routine that will null check obj and then unlock it. */
2255void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2256 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002257 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002258 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2259 } else {
2260 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2261 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002262}
2263
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002264/* Generic code for generating a wide constant into a VR. */
2265void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2266 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002267 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002268 StoreValueWide(rl_dest, rl_result);
2269}
2270
Brian Carlstrom7940e442013-07-12 13:46:57 -07002271} // namespace art