blob: 2c59055243fecddd6da428c9aa4e373e017d6601 [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 Gampe4b537a82014-06-30 22:24:53 -0700130 RegStorage arg1_32 = m2l_->TargetReg(kArg1, false);
131 RegStorage arg0_32 = m2l_->TargetReg(kArg0, false);
132
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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700179 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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700194 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) {
203 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
204 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() {
Andreas Gampe5655e842014-06-17 16:36:07 -0700212 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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700218 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);
371 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0),
372 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800373 } else {
374 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700375 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
376 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
377 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800378 }
379 } else {
380 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700381 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
382 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800383 }
384 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700386 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
387 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 }
buzbeea0cd2d72014-06-01 09:33:49 -0700389 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700390 mir_to_lir->StoreValue(rl_dest, rl_result);
391}
392
393/*
394 * Let helper function take care of everything. Will call
395 * Array::AllocFromCode(type_idx, method, count);
396 * Note: AllocFromCode will handle checks for errNegativeArraySize.
397 */
398void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
399 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700400 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700401 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
402 } else {
403 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
404 }
405}
406
407template <size_t pointer_size>
408static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
409 ThreadOffset<pointer_size> func_offset(-1);
410 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
411 type_idx)) {
412 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
413 } else {
414 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
415 }
416 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417}
418
419/*
420 * Similar to GenNewArray, but with post-allocation initialization.
421 * Verifier guarantees we're dealing with an array class. Current
422 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
423 * Current code also throws internal unimp if not 'L', '[' or 'I'.
424 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700425void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 int elems = info->num_arg_words;
427 int type_idx = info->index;
428 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700429 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700430 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700432 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 FreeTemp(TargetReg(kArg2));
435 FreeTemp(TargetReg(kArg1));
436 /*
437 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
438 * return region. Because AllocFromCode placed the new array
439 * in kRet0, we'll just lock it into place. When debugger support is
440 * added, it may be necessary to additionally copy all return
441 * values to a home location in thread-local storage
442 */
443 LockTemp(TargetReg(kRet0));
444
445 // TODO: use the correct component size, currently all supported types
446 // share array alignment with ints (see comment at head of function)
447 size_t component_size = sizeof(int32_t);
448
449 // Having a range of 0 is legal
450 if (info->is_range && (elems > 0)) {
451 /*
452 * Bit of ugliness here. We're going generate a mem copy loop
453 * on the register range, but it is possible that some regs
454 * in the range have been promoted. This is unlikely, but
455 * before generating the copy, we'll just force a flush
456 * of any regs in the source range that have been promoted to
457 * home location.
458 */
459 for (int i = 0; i < elems; i++) {
460 RegLocation loc = UpdateLoc(info->args[i]);
461 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100462 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee695d13a2014-04-19 13:32:20 -0700463 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
465 }
466 /*
467 * TUNING note: generated code here could be much improved, but
468 * this is an uncommon operation and isn't especially performance
469 * critical.
470 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700471 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700472 RegStorage r_src = AllocTempRef();
473 RegStorage r_dst = AllocTempRef();
474 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800475 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700476 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700478 case kArm64:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700479 r_val = TargetReg(kLr, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 break;
481 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700482 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 FreeTemp(TargetReg(kRet0));
484 r_val = AllocTemp();
485 break;
486 case kMips:
487 r_val = AllocTemp();
488 break;
489 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
490 }
491 // Set up source pointer
492 RegLocation rl_first = info->args[0];
493 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
494 // Set up the target pointer
495 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
496 mirror::Array::DataOffset(component_size).Int32Value());
497 // Set up the loop counter (known to be > 0)
498 LoadConstant(r_idx, elems - 1);
499 // Generate the copy loop. Going backwards for convenience
500 LIR* target = NewLIR0(kPseudoTargetLabel);
501 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100502 {
503 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
504 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
505 // NOTE: No dalvik register annotation, local optimizations will be stopped
506 // by the loop boundaries.
507 }
buzbee695d13a2014-04-19 13:32:20 -0700508 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 FreeTemp(r_val);
510 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700511 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 // Restore the target pointer
513 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
514 -mirror::Array::DataOffset(component_size).Int32Value());
515 }
516 } else if (!info->is_range) {
517 // TUNING: interleave
518 for (int i = 0; i < elems; i++) {
519 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700520 Store32Disp(TargetReg(kRet0),
Andreas Gampe3c12c512014-06-24 18:46:29 +0000521 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800523 if (IsTemp(rl_arg.reg)) {
524 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 }
526 }
527 }
528 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700529 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 }
531}
532
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800533//
534// Slow path to ensure a class is initialized for sget/sput.
535//
536class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
537 public:
buzbee2700f7e2014-03-07 09:46:20 -0800538 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
539 RegStorage r_base) :
540 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
541 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800542 }
543
544 void Compile() {
545 LIR* unresolved_target = GenerateTargetLabel();
546 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700547 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700548 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
549 storage_index_, true);
550 } else {
551 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
552 storage_index_, true);
553 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800554 // Copy helper's result into r_base, a no-op on all but MIPS.
555 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
556
557 m2l_->OpUnconditionalBranch(cont_);
558 }
559
560 private:
561 LIR* const uninit_;
562 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800563 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800564};
565
Andreas Gampe2f244e92014-05-08 03:35:25 -0700566template <size_t pointer_size>
567static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
568 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
569 ThreadOffset<pointer_size> setter_offset =
570 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
571 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
572 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
573 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
574 true);
575}
576
Vladimir Markobe0e5462014-02-26 11:24:15 +0000577void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700578 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000579 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
580 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100581 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
582 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
583 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000584 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800585 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100588 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700589 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000590 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
591 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800592 if (IsTemp(rl_method.reg)) {
593 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 }
595 } else {
596 // Medium path, static storage base in a different class which requires checks that the other
597 // class is initialized.
598 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000599 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 // May do runtime call so everything to home locations.
601 FlushAllRegs();
602 // Using fixed register to sync with possible call to runtime support.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700603 RegStorage r_method = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 LockTemp(r_method);
605 LoadCurrMethodDirect(r_method);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700606 r_base = TargetRefReg(kArg0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000608 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
609 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000610 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000611 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800612 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000613 if (!field_info.IsInitialized() &&
614 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800615 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800616
617 // The slow path is invoked if the r_base is NULL or the class pointed
618 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800619 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800620 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800622 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800623 mirror::Class::StatusOffset().Int32Value(),
624 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800625 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626
buzbee2700f7e2014-03-07 09:46:20 -0800627 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000628 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800629
630 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700631 // Ensure load of status and load of value don't re-order.
632 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 FreeTemp(r_method);
635 }
636 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100637 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100639 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100641 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000643 if (is_object) {
644 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
645 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100646 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000647 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
648 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
650 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800651 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800653 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 } else {
655 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700656 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700657 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
658 } else {
659 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
660 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
662}
663
Andreas Gampe2f244e92014-05-08 03:35:25 -0700664template <size_t pointer_size>
665static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
666 const MirSFieldLoweringInfo* field_info) {
667 ThreadOffset<pointer_size> getter_offset =
668 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
669 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
670 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
671 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
672}
673
Vladimir Markobe0e5462014-02-26 11:24:15 +0000674void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700675 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000676 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
677 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100678 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
679 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
680 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000681 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800682 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000683 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 // Fast path, static storage base is this method's class
685 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700686 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000687 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
688 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 } else {
690 // Medium path, static storage base in a different class which requires checks that the other
691 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 // May do runtime call so everything to home locations.
694 FlushAllRegs();
695 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800696 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 LockTemp(r_method);
698 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800699 r_base = TargetReg(kArg0);
700 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000701 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
702 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000703 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000704 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800705 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000706 if (!field_info.IsInitialized() &&
707 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800708 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800709
710 // The slow path is invoked if the r_base is NULL or the class pointed
711 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800712 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800713 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800714 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800715 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800716 mirror::Class::StatusOffset().Int32Value(),
717 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800718 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800719
buzbee2700f7e2014-03-07 09:46:20 -0800720 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000721 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800722
723 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700724 // Ensure load of status and load of value don't re-order.
725 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 FreeTemp(r_method);
728 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800729 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100730 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
731 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800732
Vladimir Marko674744e2014-04-24 15:18:26 +0100733 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000734 if (is_object) {
735 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
736 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100737 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000738 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
739 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800740 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100741 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800742
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 if (is_long_or_double) {
744 StoreValueWide(rl_dest, rl_result);
745 } else {
746 StoreValue(rl_dest, rl_result);
747 }
748 } else {
749 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700750 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700751 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
752 } else {
753 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
754 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700755 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700757 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 StoreValueWide(rl_dest, rl_result);
759 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700760 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 StoreValue(rl_dest, rl_result);
762 }
763 }
764}
765
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800766// Generate code for all slow paths.
767void Mir2Lir::HandleSlowPaths() {
768 int n = slow_paths_.Size();
769 for (int i = 0; i < n; ++i) {
770 LIRSlowPath* slowpath = slow_paths_.Get(i);
771 slowpath->Compile();
772 }
773 slow_paths_.Reset();
774}
775
Andreas Gampe2f244e92014-05-08 03:35:25 -0700776template <size_t pointer_size>
777static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
778 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
779 ThreadOffset<pointer_size> getter_offset =
780 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
781 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
782 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
783 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
784 true);
785}
786
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700789 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000790 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
791 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100792 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
793 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
794 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
795 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000796 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700797 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100798 GenNullCheck(rl_obj.reg, opt_flags);
799 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
800 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000801 LIR* load_lir;
802 if (is_object) {
803 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
804 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100805 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000806 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
807 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100808 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000809 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 StoreValueWide(rl_dest, rl_result);
812 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 StoreValue(rl_dest, rl_result);
814 }
815 } else {
buzbee33ae5582014-06-12 14:56:32 -0700816 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700817 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
818 } else {
819 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
820 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700822 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 StoreValueWide(rl_dest, rl_result);
824 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700825 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 StoreValue(rl_dest, rl_result);
827 }
828 }
829}
830
Andreas Gampe2f244e92014-05-08 03:35:25 -0700831template <size_t pointer_size>
832static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
833 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
834 RegLocation rl_src) {
835 ThreadOffset<pointer_size> setter_offset =
836 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
837 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
838 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
839 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
840 rl_obj, rl_src, true);
841}
842
Vladimir Markobe0e5462014-02-26 11:24:15 +0000843void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700845 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000846 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
847 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100848 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
849 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
850 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
851 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000852 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700853 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100855 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 } else {
857 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100858 }
859 GenNullCheck(rl_obj.reg, opt_flags);
860 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000861 LIR* store;
862 if (is_object) {
863 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
864 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100865 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000866 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
867 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100868 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000869 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100870 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
871 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 }
873 } else {
buzbee33ae5582014-06-12 14:56:32 -0700874 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700875 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
876 } else {
877 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
878 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 }
880}
881
Andreas Gampe2f244e92014-05-08 03:35:25 -0700882template <size_t pointer_size>
883static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
884 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
885 ThreadOffset<pointer_size> helper = needs_range_check
886 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
887 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
888 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
889 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
890 true);
891}
892
Ian Rogersa9a82542013-10-04 11:17:26 -0700893void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
894 RegLocation rl_src) {
895 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
896 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
897 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700898 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700899 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
900 } else {
901 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
902 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700903}
904
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700905void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700907 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700908 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700909 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700911 *cu_->dex_file,
912 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 // Call out to helper which resolves type and verifies access.
914 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700915 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700916 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
917 type_idx, rl_method.reg, true);
918 } else {
919 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
920 type_idx, rl_method.reg, true);
921 }
buzbeea0cd2d72014-06-01 09:33:49 -0700922 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 StoreValue(rl_dest, rl_result);
924 } else {
925 // We're don't need access checks, load type from dex cache
926 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700927 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000928 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000929 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000930 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
932 type_idx) || SLOW_TYPE_PATH) {
933 // Slow path, at runtime test if type is null and if so initialize
934 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800935 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936 LIR* cont = NewLIR0(kPseudoTargetLabel);
937
938 // Object to generate the slow path for class resolution.
939 class SlowPath : public LIRSlowPath {
940 public:
941 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
942 const RegLocation& rl_method, const RegLocation& rl_result) :
943 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
944 rl_method_(rl_method), rl_result_(rl_result) {
945 }
946
947 void Compile() {
948 GenerateTargetLabel();
949
buzbee33ae5582014-06-12 14:56:32 -0700950 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700951 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
952 rl_method_.reg, true);
953 } else {
954 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
955 rl_method_.reg, true);
956 }
buzbee2700f7e2014-03-07 09:46:20 -0800957 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958
959 m2l_->OpUnconditionalBranch(cont_);
960 }
961
962 private:
963 const int type_idx_;
964 const RegLocation rl_method_;
965 const RegLocation rl_result_;
966 };
967
968 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800969 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800970
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800972 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 // Fast path, we're done - just store result
974 StoreValue(rl_dest, rl_result);
975 }
976 }
977}
978
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700979void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000981 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
982 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
984 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
985 // slow path, resolve string if not in dex cache
986 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700987 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800988
989 // If the Method* is already in a register, we can save a copy.
990 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800991 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800992 if (rl_method.location == kLocPhysReg) {
993 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800994 DCHECK(!IsTemp(rl_method.reg));
995 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800996 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700997 r_method = TargetRefReg(kArg2);
Mark Mendell766e9292014-01-27 07:55:47 -0800998 LoadCurrMethodDirect(r_method);
999 }
buzbee695d13a2014-04-19 13:32:20 -07001000 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -07001001 TargetRefReg(kArg0), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001002
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampe4b537a82014-06-30 22:24:53 -07001004 LoadRefDisp(TargetRefReg(kArg0), offset_of_string, TargetRefReg(kRet0), kNotVolatile);
1005 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetRefReg(kRet0), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001006 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001007
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001008 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001009 // Object to generate the slow path for string resolution.
1010 class SlowPath : public LIRSlowPath {
1011 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001012 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1013 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1014 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001015 }
1016
1017 void Compile() {
1018 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001019 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001020 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1021 r_method_, string_idx_, true);
1022 } else {
1023 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1024 r_method_, string_idx_, true);
1025 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001026 m2l_->OpUnconditionalBranch(cont_);
1027 }
1028
1029 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001030 const RegStorage r_method_;
1031 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001032 };
1033
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001034 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001036
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001038 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 } else {
1040 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001041 RegStorage res_reg = AllocTempRef();
1042 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001043 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1044 kNotVolatile);
1045 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 StoreValue(rl_dest, rl_result);
1047 }
1048}
1049
Andreas Gampe2f244e92014-05-08 03:35:25 -07001050template <size_t pointer_size>
1051static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1052 RegLocation rl_dest) {
1053 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 // alloc will always check for resolution, do we also need to verify
1055 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001056 ThreadOffset<pointer_size> func_offset(-1);
1057 const DexFile* dex_file = cu->dex_file;
1058 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001060 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 bool is_type_initialized;
1062 bool use_direct_type_ptr;
1063 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001064 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001066 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1067 &direct_type_ptr, &is_finalizable) &&
1068 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 // The fast path.
1070 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001071 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001073 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1074 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001076 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1077 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001078 }
1079 } else {
1080 // Use the direct pointer.
1081 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1083 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, 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);
1086 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001087 }
1088 }
1089 } else {
1090 // The slow path.
1091 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001092 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1093 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001094 }
1095 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001097 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1098 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 }
buzbeea0cd2d72014-06-01 09:33:49 -07001100 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001101 mir_to_lir->StoreValue(rl_dest, rl_result);
1102}
1103
1104/*
1105 * Let helper function take care of everything. Will
1106 * call Class::NewInstanceFromCode(type_idx, method);
1107 */
1108void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001109 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001110 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1111 } else {
1112 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1113 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114}
1115
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001116void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001118 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001119 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1120 } else {
1121 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123}
1124
1125// For final classes there are no sub-classes to check and so we can answer the instance-of
1126// question with simple comparisons.
1127void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1128 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001129 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001130 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001131
buzbeea0cd2d72014-06-01 09:33:49 -07001132 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001134 RegStorage result_reg = rl_result.reg;
1135 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 result_reg = AllocTypedTemp(false, kCoreReg);
1137 }
1138 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001139 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140
buzbeea0cd2d72014-06-01 09:33:49 -07001141 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1142 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143
1144 LoadCurrMethodDirect(check_class);
1145 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001146 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1147 kNotVolatile);
1148 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1149 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 } else {
buzbee695d13a2014-04-19 13:32:20 -07001151 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001152 check_class, kNotVolatile);
1153 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1154 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001155 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001156 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 }
1158
1159 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001160 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 if (cu_->instruction_set == kThumb2) {
1162 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001163 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001165 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 } else {
1167 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1168 LoadConstant(result_reg, 1); // eq case - load true
1169 }
1170 LIR* target = NewLIR0(kPseudoTargetLabel);
1171 null_branchover->target = target;
1172 if (ne_branchover != NULL) {
1173 ne_branchover->target = target;
1174 }
1175 FreeTemp(object_class);
1176 FreeTemp(check_class);
1177 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001178 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 FreeTemp(result_reg);
1180 }
1181 StoreValue(rl_dest, rl_result);
1182}
1183
1184void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1185 bool type_known_abstract, bool use_declaring_class,
1186 bool can_assume_type_is_in_dex_cache,
1187 uint32_t type_idx, RegLocation rl_dest,
1188 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001189 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001190 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001191
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 FlushAllRegs();
1193 // May generate a call - use explicit registers
1194 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001195 RegStorage method_reg = TargetRefReg(kArg1);
1196 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1197 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (needs_access_check) {
1199 // Check we have access to type_idx and if not throw IllegalAccessError,
1200 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001201 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001202 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1203 type_idx, true);
1204 } else {
1205 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1206 type_idx, true);
1207 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1209 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1210 } else if (use_declaring_class) {
1211 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001212 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001213 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 } else {
1215 // Load dex cache entry into class_reg (kArg2)
1216 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001217 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001218 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001219 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001220 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 if (!can_assume_type_is_in_dex_cache) {
1222 // Need to test presence of type in dex cache at runtime
1223 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1224 // Not resolved
1225 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001226 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001227 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1228 } else {
1229 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1230 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001231 OpRegCopy(TargetRefReg(kArg2), TargetRefReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1233 // Rejoin code paths
1234 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1235 hop_branch->target = hop_target;
1236 }
1237 }
1238 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001239 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 if (cu_->instruction_set == kMips) {
1241 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001242 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 }
1244 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1245
1246 /* load object->klass_ */
1247 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001248 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetRefReg(kArg1),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001249 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1251 LIR* branchover = NULL;
1252 if (type_known_final) {
1253 // rl_result == ref == null == 0.
1254 if (cu_->instruction_set == kThumb2) {
1255 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001256 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001257 LoadConstant(rl_result.reg, 1); // .eq case - load true
1258 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001259 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001261 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001263 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 }
1265 } else {
1266 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001267 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001268 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1269 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001270 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 if (!type_known_abstract) {
1272 /* Uses conditional nullification */
1273 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001274 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1276 }
1277 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1278 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001279 if (it != nullptr) {
1280 OpEndIT(it);
1281 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 FreeTemp(r_tgt);
1283 } else {
1284 if (!type_known_abstract) {
1285 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001286 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1288 }
buzbee33ae5582014-06-12 14:56:32 -07001289 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001290 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1291 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001292 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1293 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1294 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 }
1296 }
1297 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001298 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 /* branch targets here */
1300 LIR* target = NewLIR0(kPseudoTargetLabel);
1301 StoreValue(rl_dest, rl_result);
1302 branch1->target = target;
1303 if (branchover != NULL) {
1304 branchover->target = target;
1305 }
1306}
1307
1308void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1309 bool type_known_final, type_known_abstract, use_declaring_class;
1310 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1311 *cu_->dex_file,
1312 type_idx,
1313 &type_known_final,
1314 &type_known_abstract,
1315 &use_declaring_class);
1316 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1317 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1318
1319 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1320 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1321 } else {
1322 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1323 use_declaring_class, can_assume_type_is_in_dex_cache,
1324 type_idx, rl_dest, rl_src);
1325 }
1326}
1327
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001328void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 bool type_known_final, type_known_abstract, use_declaring_class;
1330 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1331 *cu_->dex_file,
1332 type_idx,
1333 &type_known_final,
1334 &type_known_abstract,
1335 &use_declaring_class);
1336 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1337 // of the exception throw path.
1338 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001339 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 // Verifier type analysis proved this check cast would never cause an exception.
1341 return;
1342 }
1343 FlushAllRegs();
1344 // May generate a call - use explicit registers
1345 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001346 RegStorage method_reg = TargetRefReg(kArg1);
1347 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1348 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 if (needs_access_check) {
1350 // Check we have access to type_idx and if not throw IllegalAccessError,
1351 // returns Class* in kRet0
1352 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001353 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001354 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1355 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001356 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001357 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1358 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001359 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001360 OpRegCopy(class_reg, TargetRefReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001362 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001363 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 } else {
1365 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001366 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001367 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001368 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001369 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1371 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001372 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1373 LIR* cont = NewLIR0(kPseudoTargetLabel);
1374
1375 // Slow path to initialize the type. Executed if the type is NULL.
1376 class SlowPath : public LIRSlowPath {
1377 public:
1378 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001379 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001380 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1381 class_reg_(class_reg) {
1382 }
1383
1384 void Compile() {
1385 GenerateTargetLabel();
1386
1387 // Call out to helper, which will return resolved type in kArg0
1388 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001389 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001390 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001391 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001392 } else {
1393 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001394 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001395 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001396 m2l_->OpRegCopy(class_reg_, m2l_->TargetRefReg(kRet0)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001397 m2l_->OpUnconditionalBranch(cont_);
1398 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001399
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400 public:
1401 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001402 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001403 };
1404
buzbee2700f7e2014-03-07 09:46:20 -08001405 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 }
1407 }
1408 // At this point, class_reg (kArg2) has class
Andreas Gampe4b537a82014-06-30 22:24:53 -07001409 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001410
1411 // Slow path for the case where the classes are not equal. In this case we need
1412 // to call a helper function to do the check.
1413 class SlowPath : public LIRSlowPath {
1414 public:
1415 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1416 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1417 }
1418
1419 void Compile() {
1420 GenerateTargetLabel();
1421
1422 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001423 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001424 m2l_->TargetReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001425 }
buzbee33ae5582014-06-12 14:56:32 -07001426 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001427 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1428 m2l_->TargetReg(kArg1), true);
1429 } else {
1430 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1431 m2l_->TargetReg(kArg1), true);
1432 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001433
1434 m2l_->OpUnconditionalBranch(cont_);
1435 }
1436
1437 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001438 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001439 };
1440
1441 if (type_known_abstract) {
1442 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001443 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001444 LIR* cont = NewLIR0(kPseudoTargetLabel);
1445 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1446 } else {
1447 // Harder, more common case. We need to generate a forward branch over the load
1448 // if the target is null. If it's non-null we perform the load and branch to the
1449 // slow path if the classes are not equal.
1450
1451 /* Null is OK - continue */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001452 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001453 /* load object->klass_ */
1454 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001455 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1456 TargetRefReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001457
Andreas Gampe4b537a82014-06-30 22:24:53 -07001458 LIR* branch2 = OpCmpBranch(kCondNe, TargetRefReg(kArg1), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459 LIR* cont = NewLIR0(kPseudoTargetLabel);
1460
1461 // Add the slow path that will not perform load since this is already done.
1462 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1463
1464 // Set the null check to branch to the continuation.
1465 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 }
1467}
1468
1469void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001470 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 RegLocation rl_result;
1472 if (cu_->instruction_set == kThumb2) {
1473 /*
1474 * NOTE: This is the one place in the code in which we might have
1475 * as many as six live temporary registers. There are 5 in the normal
1476 * set for Arm. Until we have spill capabilities, temporarily add
1477 * lr to the temp set. It is safe to do this locally, but note that
1478 * lr is used explicitly elsewhere in the code generator and cannot
1479 * normally be used as a general temp register.
1480 */
1481 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1482 FreeTemp(TargetReg(kLr)); // and make it available
1483 }
1484 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1485 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1486 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1487 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001488 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1489 RegStorage t_reg = AllocTemp();
1490 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1491 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1492 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 FreeTemp(t_reg);
1494 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001495 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1496 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 }
1498 /*
1499 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1500 * following StoreValueWide might need to allocate a temp register.
1501 * To further work around the lack of a spill capability, explicitly
1502 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1503 * Remove when spill is functional.
1504 */
1505 FreeRegLocTemps(rl_result, rl_src1);
1506 FreeRegLocTemps(rl_result, rl_src2);
1507 StoreValueWide(rl_dest, rl_result);
1508 if (cu_->instruction_set == kThumb2) {
1509 Clobber(TargetReg(kLr));
1510 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1511 }
1512}
1513
1514
Andreas Gampe2f244e92014-05-08 03:35:25 -07001515template <size_t pointer_size>
1516static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1517 RegLocation rl_shift) {
1518 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519
1520 switch (opcode) {
1521 case Instruction::SHL_LONG:
1522 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001523 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 break;
1525 case Instruction::SHR_LONG:
1526 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001527 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 break;
1529 case Instruction::USHR_LONG:
1530 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001531 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 break;
1533 default:
1534 LOG(FATAL) << "Unexpected case";
1535 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001536 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1537 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1538}
1539
1540void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1541 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001542 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001543 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1544 } else {
1545 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1546 }
buzbeea0cd2d72014-06-01 09:33:49 -07001547 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 StoreValueWide(rl_dest, rl_result);
1549}
1550
1551
1552void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001553 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001554 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 OpKind op = kOpBkpt;
1556 bool is_div_rem = false;
1557 bool check_zero = false;
1558 bool unary = false;
1559 RegLocation rl_result;
1560 bool shift_op = false;
1561 switch (opcode) {
1562 case Instruction::NEG_INT:
1563 op = kOpNeg;
1564 unary = true;
1565 break;
1566 case Instruction::NOT_INT:
1567 op = kOpMvn;
1568 unary = true;
1569 break;
1570 case Instruction::ADD_INT:
1571 case Instruction::ADD_INT_2ADDR:
1572 op = kOpAdd;
1573 break;
1574 case Instruction::SUB_INT:
1575 case Instruction::SUB_INT_2ADDR:
1576 op = kOpSub;
1577 break;
1578 case Instruction::MUL_INT:
1579 case Instruction::MUL_INT_2ADDR:
1580 op = kOpMul;
1581 break;
1582 case Instruction::DIV_INT:
1583 case Instruction::DIV_INT_2ADDR:
1584 check_zero = true;
1585 op = kOpDiv;
1586 is_div_rem = true;
1587 break;
1588 /* NOTE: returns in kArg1 */
1589 case Instruction::REM_INT:
1590 case Instruction::REM_INT_2ADDR:
1591 check_zero = true;
1592 op = kOpRem;
1593 is_div_rem = true;
1594 break;
1595 case Instruction::AND_INT:
1596 case Instruction::AND_INT_2ADDR:
1597 op = kOpAnd;
1598 break;
1599 case Instruction::OR_INT:
1600 case Instruction::OR_INT_2ADDR:
1601 op = kOpOr;
1602 break;
1603 case Instruction::XOR_INT:
1604 case Instruction::XOR_INT_2ADDR:
1605 op = kOpXor;
1606 break;
1607 case Instruction::SHL_INT:
1608 case Instruction::SHL_INT_2ADDR:
1609 shift_op = true;
1610 op = kOpLsl;
1611 break;
1612 case Instruction::SHR_INT:
1613 case Instruction::SHR_INT_2ADDR:
1614 shift_op = true;
1615 op = kOpAsr;
1616 break;
1617 case Instruction::USHR_INT:
1618 case Instruction::USHR_INT_2ADDR:
1619 shift_op = true;
1620 op = kOpLsr;
1621 break;
1622 default:
1623 LOG(FATAL) << "Invalid word arith op: " << opcode;
1624 }
1625 if (!is_div_rem) {
1626 if (unary) {
1627 rl_src1 = LoadValue(rl_src1, kCoreReg);
1628 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001629 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001631 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001632 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001633 RegStorage t_reg = AllocTemp();
1634 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 rl_src1 = LoadValue(rl_src1, kCoreReg);
1636 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001637 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 FreeTemp(t_reg);
1639 } else {
1640 rl_src1 = LoadValue(rl_src1, kCoreReg);
1641 rl_src2 = LoadValue(rl_src2, kCoreReg);
1642 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001643 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 }
1645 }
1646 StoreValue(rl_dest, rl_result);
1647 } else {
Dave Allison70202782013-10-22 17:52:19 -07001648 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 +01001649 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 rl_src1 = LoadValue(rl_src1, kCoreReg);
1651 rl_src2 = LoadValue(rl_src2, kCoreReg);
1652 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001653 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 }
buzbee2700f7e2014-03-07 09:46:20 -08001655 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001656 done = true;
1657 } else if (cu_->instruction_set == kThumb2) {
1658 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1659 // Use ARM SDIV instruction for division. For remainder we also need to
1660 // calculate using a MUL and subtract.
1661 rl_src1 = LoadValue(rl_src1, kCoreReg);
1662 rl_src2 = LoadValue(rl_src2, kCoreReg);
1663 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001664 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001665 }
buzbee2700f7e2014-03-07 09:46:20 -08001666 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001667 done = true;
1668 }
1669 }
1670
1671 // If we haven't already generated the code use the callout function.
1672 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 FlushAllRegs(); /* Send everything to home location */
1674 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee33ae5582014-06-12 14:56:32 -07001675 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001676 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1677 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1679 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001680 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 }
Dave Allison70202782013-10-22 17:52:19 -07001682 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001683 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001684 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1685 } else {
1686 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1687 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001689 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 else
1691 rl_result = GetReturnAlt();
1692 }
1693 StoreValue(rl_dest, rl_result);
1694 }
1695}
1696
1697/*
1698 * The following are the first-level codegen routines that analyze the format
1699 * of each bytecode then either dispatch special purpose codegen routines
1700 * or produce corresponding Thumb instructions directly.
1701 */
1702
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001704static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 x &= x - 1;
1706 return (x & (x - 1)) == 0;
1707}
1708
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1710// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001711bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001712 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1714 return false;
1715 }
1716 // No divide instruction for Arm, so check for more special cases
1717 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001718 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 }
1720 int k = LowestSetBit(lit);
1721 if (k >= 30) {
1722 // Avoid special cases.
1723 return false;
1724 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 rl_src = LoadValue(rl_src, kCoreReg);
1726 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001727 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001728 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 if (lit == 2) {
1730 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1732 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1733 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001735 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001737 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1738 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 }
1740 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001741 RegStorage t_reg1 = AllocTemp();
1742 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001744 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1745 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001747 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001749 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001753 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754 }
1755 }
1756 StoreValue(rl_dest, rl_result);
1757 return true;
1758}
1759
1760// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1761// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001762bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001763 if (lit < 0) {
1764 return false;
1765 }
1766 if (lit == 0) {
1767 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1768 LoadConstant(rl_result.reg, 0);
1769 StoreValue(rl_dest, rl_result);
1770 return true;
1771 }
1772 if (lit == 1) {
1773 rl_src = LoadValue(rl_src, kCoreReg);
1774 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1775 OpRegCopy(rl_result.reg, rl_src.reg);
1776 StoreValue(rl_dest, rl_result);
1777 return true;
1778 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001779 // There is RegRegRegShift on Arm, so check for more special cases
1780 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001781 return EasyMultiply(rl_src, rl_dest, lit);
1782 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 // Can we simplify this multiplication?
1784 bool power_of_two = false;
1785 bool pop_count_le2 = false;
1786 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001787 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 power_of_two = true;
1789 } else if (IsPopCountLE2(lit)) {
1790 pop_count_le2 = true;
1791 } else if (IsPowerOfTwo(lit + 1)) {
1792 power_of_two_minus_one = true;
1793 } else {
1794 return false;
1795 }
1796 rl_src = LoadValue(rl_src, kCoreReg);
1797 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1798 if (power_of_two) {
1799 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001800 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 } else if (pop_count_le2) {
1802 // Shift and add and shift.
1803 int first_bit = LowestSetBit(lit);
1804 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1805 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1806 } else {
1807 // Reverse subtract: (src << (shift + 1)) - src.
1808 DCHECK(power_of_two_minus_one);
1809 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001810 RegStorage t_reg = AllocTemp();
1811 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1812 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 }
1814 StoreValue(rl_dest, rl_result);
1815 return true;
1816}
1817
1818void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001819 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 RegLocation rl_result;
1821 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1822 int shift_op = false;
1823 bool is_div = false;
1824
1825 switch (opcode) {
1826 case Instruction::RSUB_INT_LIT8:
1827 case Instruction::RSUB_INT: {
1828 rl_src = LoadValue(rl_src, kCoreReg);
1829 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1830 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001831 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001833 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1834 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001835 }
1836 StoreValue(rl_dest, rl_result);
1837 return;
1838 }
1839
1840 case Instruction::SUB_INT:
1841 case Instruction::SUB_INT_2ADDR:
1842 lit = -lit;
1843 // Intended fallthrough
1844 case Instruction::ADD_INT:
1845 case Instruction::ADD_INT_2ADDR:
1846 case Instruction::ADD_INT_LIT8:
1847 case Instruction::ADD_INT_LIT16:
1848 op = kOpAdd;
1849 break;
1850 case Instruction::MUL_INT:
1851 case Instruction::MUL_INT_2ADDR:
1852 case Instruction::MUL_INT_LIT8:
1853 case Instruction::MUL_INT_LIT16: {
1854 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1855 return;
1856 }
1857 op = kOpMul;
1858 break;
1859 }
1860 case Instruction::AND_INT:
1861 case Instruction::AND_INT_2ADDR:
1862 case Instruction::AND_INT_LIT8:
1863 case Instruction::AND_INT_LIT16:
1864 op = kOpAnd;
1865 break;
1866 case Instruction::OR_INT:
1867 case Instruction::OR_INT_2ADDR:
1868 case Instruction::OR_INT_LIT8:
1869 case Instruction::OR_INT_LIT16:
1870 op = kOpOr;
1871 break;
1872 case Instruction::XOR_INT:
1873 case Instruction::XOR_INT_2ADDR:
1874 case Instruction::XOR_INT_LIT8:
1875 case Instruction::XOR_INT_LIT16:
1876 op = kOpXor;
1877 break;
1878 case Instruction::SHL_INT_LIT8:
1879 case Instruction::SHL_INT:
1880 case Instruction::SHL_INT_2ADDR:
1881 lit &= 31;
1882 shift_op = true;
1883 op = kOpLsl;
1884 break;
1885 case Instruction::SHR_INT_LIT8:
1886 case Instruction::SHR_INT:
1887 case Instruction::SHR_INT_2ADDR:
1888 lit &= 31;
1889 shift_op = true;
1890 op = kOpAsr;
1891 break;
1892 case Instruction::USHR_INT_LIT8:
1893 case Instruction::USHR_INT:
1894 case Instruction::USHR_INT_2ADDR:
1895 lit &= 31;
1896 shift_op = true;
1897 op = kOpLsr;
1898 break;
1899
1900 case Instruction::DIV_INT:
1901 case Instruction::DIV_INT_2ADDR:
1902 case Instruction::DIV_INT_LIT8:
1903 case Instruction::DIV_INT_LIT16:
1904 case Instruction::REM_INT:
1905 case Instruction::REM_INT_2ADDR:
1906 case Instruction::REM_INT_LIT8:
1907 case Instruction::REM_INT_LIT16: {
1908 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001909 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910 return;
1911 }
buzbee11b63d12013-08-27 07:34:17 -07001912 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001913 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001914 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 (opcode == Instruction::DIV_INT_LIT16)) {
1916 is_div = true;
1917 } else {
1918 is_div = false;
1919 }
buzbee11b63d12013-08-27 07:34:17 -07001920 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1921 return;
1922 }
Dave Allison70202782013-10-22 17:52:19 -07001923
1924 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001925 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001926 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001927 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001928 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001929 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001930 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1931 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001932 } else if (cu_->instruction_set == kThumb2) {
1933 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1934 // Use ARM SDIV instruction for division. For remainder we also need to
1935 // calculate using a MUL and subtract.
1936 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001937 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001938 done = true;
1939 }
1940 }
1941
1942 if (!done) {
1943 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001944 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1945 Clobber(TargetReg(kArg0));
buzbee33ae5582014-06-12 14:56:32 -07001946 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001947 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1948 false);
1949 } else {
1950 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1951 false);
1952 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001953 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001954 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 else
1956 rl_result = GetReturnAlt();
1957 }
1958 StoreValue(rl_dest, rl_result);
1959 return;
1960 }
1961 default:
1962 LOG(FATAL) << "Unexpected opcode " << opcode;
1963 }
1964 rl_src = LoadValue(rl_src, kCoreReg);
1965 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001966 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001968 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001970 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001971 }
1972 StoreValue(rl_dest, rl_result);
1973}
1974
Andreas Gampe2f244e92014-05-08 03:35:25 -07001975template <size_t pointer_size>
1976static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1977 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 RegLocation rl_result;
1979 OpKind first_op = kOpBkpt;
1980 OpKind second_op = kOpBkpt;
1981 bool call_out = false;
1982 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001983 ThreadOffset<pointer_size> func_offset(-1);
1984 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985
1986 switch (opcode) {
1987 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001988 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001989 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1990 return;
1991 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001992 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1993 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001995 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001996 RegStorage t_reg = mir_to_lir->AllocTemp();
1997 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1998 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1999 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2000 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002002 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2003 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002005 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 return;
2007 case Instruction::ADD_LONG:
2008 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002009 if (cu->instruction_set != kThumb2) {
2010 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 return;
2012 }
2013 first_op = kOpAdd;
2014 second_op = kOpAdc;
2015 break;
2016 case Instruction::SUB_LONG:
2017 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002018 if (cu->instruction_set != kThumb2) {
2019 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 return;
2021 }
2022 first_op = kOpSub;
2023 second_op = kOpSbc;
2024 break;
2025 case Instruction::MUL_LONG:
2026 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002027 if (cu->instruction_set != kMips) {
2028 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 return;
2030 } else {
2031 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002032 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2033 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 }
2035 break;
2036 case Instruction::DIV_LONG:
2037 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002038 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002039 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2040 return;
2041 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 call_out = true;
2043 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002044 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2045 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 break;
2047 case Instruction::REM_LONG:
2048 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002049 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002050 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2051 return;
2052 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 call_out = true;
2054 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002055 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002056 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002057 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2058 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 break;
2060 case Instruction::AND_LONG_2ADDR:
2061 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002062 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2063 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002064 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002065 }
2066 first_op = kOpAnd;
2067 second_op = kOpAnd;
2068 break;
2069 case Instruction::OR_LONG:
2070 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002071 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2072 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002073 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 return;
2075 }
2076 first_op = kOpOr;
2077 second_op = kOpOr;
2078 break;
2079 case Instruction::XOR_LONG:
2080 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002081 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2082 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002083 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 return;
2085 }
2086 first_op = kOpXor;
2087 second_op = kOpXor;
2088 break;
2089 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002090 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 return;
2092 }
2093 default:
2094 LOG(FATAL) << "Invalid long arith op";
2095 }
2096 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002097 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002101 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2102 mir_to_lir->TargetReg(kArg1));
2103 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2104 mir_to_lir->TargetReg(kArg3));
2105 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2106 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2107 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2108 mir_to_lir->TargetReg(kArg3)));
2109 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002110 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002111 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002113 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002114 }
2115 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002116 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002117 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002118 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002119 rl_result = mir_to_lir->GetReturnWideAlt();
2120 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002121 }
2122}
2123
Andreas Gampe2f244e92014-05-08 03:35:25 -07002124void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2125 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002126 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002127 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2128 } else {
2129 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2130 }
2131}
2132
Mark Mendelle87f9b52014-04-30 14:13:18 -04002133void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2134 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2135 LoadConstantNoClobber(rl_result.reg, value);
2136 StoreValue(rl_dest, rl_result);
2137 if (value == 0) {
2138 Workaround7250540(rl_dest, rl_result.reg);
2139 }
2140}
2141
Andreas Gampe2f244e92014-05-08 03:35:25 -07002142template <size_t pointer_size>
2143void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002144 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002145 /*
2146 * Don't optimize the register usage since it calls out to support
2147 * functions
2148 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002149 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2150
Brian Carlstrom7940e442013-07-12 13:46:57 -07002151 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002152 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2153 if (rl_dest.wide) {
2154 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002155 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002156 StoreValueWide(rl_dest, rl_result);
2157 } else {
2158 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002159 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002160 StoreValue(rl_dest, rl_result);
2161 }
2162}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002163template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2164 RegLocation rl_dest, RegLocation rl_src);
2165template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2166 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002167
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002168class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2169 public:
2170 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2171 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2172 }
2173
2174 void Compile() OVERRIDE {
2175 m2l_->ResetRegPool();
2176 m2l_->ResetDefTracking();
2177 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002178 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002179 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2180 } else {
2181 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2182 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002183 if (cont_ != nullptr) {
2184 m2l_->OpUnconditionalBranch(cont_);
2185 }
2186 }
2187};
2188
Brian Carlstrom7940e442013-07-12 13:46:57 -07002189/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002190void Mir2Lir::GenSuspendTest(int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002191 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002192 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2193 return;
2194 }
2195 FlushAllRegs();
2196 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002197 LIR* cont = NewLIR0(kPseudoTargetLabel);
2198 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002199 } else {
2200 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2201 return;
2202 }
2203 FlushAllRegs(); // TODO: needed?
2204 LIR* inst = CheckSuspendUsingLoad();
2205 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002206 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002207}
2208
2209/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002210void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002211 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002212 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2213 OpUnconditionalBranch(target);
2214 return;
2215 }
2216 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002217 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002218 LIR* branch = OpUnconditionalBranch(nullptr);
2219 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002220 } else {
2221 // For the implicit suspend check, just perform the trigger
2222 // load and branch to the target.
2223 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2224 OpUnconditionalBranch(target);
2225 return;
2226 }
2227 FlushAllRegs();
2228 LIR* inst = CheckSuspendUsingLoad();
2229 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002230 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002231 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002232}
2233
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002234/* Call out to helper assembly routine that will null check obj and then lock it. */
2235void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2236 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002237 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002238 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2239 } else {
2240 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2241 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002242}
2243
2244/* Call out to helper assembly routine that will null check obj and then unlock it. */
2245void Mir2Lir::GenMonitorExit(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, pUnlockObject), rl_src, true);
2249 } else {
2250 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2251 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002252}
2253
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002254/* Generic code for generating a wide constant into a VR. */
2255void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2256 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002257 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002258 StoreValueWide(rl_dest, rl_result);
2259}
2260
Brian Carlstrom7940e442013-07-12 13:46:57 -07002261} // namespace art