blob: dafefea0a3e2844f962b07b4ea1cdb0a63217780 [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);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700371 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0, false),
Andreas Gampe2f244e92014-05-08 03:35:25 -0700372 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 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700434 FreeTemp(TargetReg(kArg2, false));
435 FreeTemp(TargetReg(kArg1, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 /*
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 */
Chao-ying Fua77ee512014-07-01 17:43:41 -0700443 RegStorage ref_reg = TargetRefReg(kRet0);
444 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445
446 // TODO: use the correct component size, currently all supported types
447 // share array alignment with ints (see comment at head of function)
448 size_t component_size = sizeof(int32_t);
449
450 // Having a range of 0 is legal
451 if (info->is_range && (elems > 0)) {
452 /*
453 * Bit of ugliness here. We're going generate a mem copy loop
454 * on the register range, but it is possible that some regs
455 * in the range have been promoted. This is unlikely, but
456 * before generating the copy, we'll just force a flush
457 * of any regs in the source range that have been promoted to
458 * home location.
459 */
460 for (int i = 0; i < elems; i++) {
461 RegLocation loc = UpdateLoc(info->args[i]);
462 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100463 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700464 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
466 }
467 /*
468 * TUNING note: generated code here could be much improved, but
469 * this is an uncommon operation and isn't especially performance
470 * critical.
471 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700472 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700473 RegStorage r_src = AllocTempRef();
474 RegStorage r_dst = AllocTempRef();
475 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800476 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700477 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700479 case kArm64:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700480 r_val = TargetReg(kLr, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 break;
482 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700483 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700484 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 r_val = AllocTemp();
486 break;
487 case kMips:
488 r_val = AllocTemp();
489 break;
490 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
491 }
492 // Set up source pointer
493 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700494 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700496 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 mirror::Array::DataOffset(component_size).Int32Value());
498 // Set up the loop counter (known to be > 0)
499 LoadConstant(r_idx, elems - 1);
500 // Generate the copy loop. Going backwards for convenience
501 LIR* target = NewLIR0(kPseudoTargetLabel);
502 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100503 {
504 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
505 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
506 // NOTE: No dalvik register annotation, local optimizations will be stopped
507 // by the loop boundaries.
508 }
buzbee695d13a2014-04-19 13:32:20 -0700509 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 FreeTemp(r_val);
511 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700512 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700514 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 -mirror::Array::DataOffset(component_size).Int32Value());
516 }
517 } else if (!info->is_range) {
518 // TUNING: interleave
519 for (int i = 0; i < elems; i++) {
520 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700521 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000522 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800524 if (IsTemp(rl_arg.reg)) {
525 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 }
527 }
528 }
529 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700530 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 }
532}
533
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534//
535// Slow path to ensure a class is initialized for sget/sput.
536//
537class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
538 public:
buzbee2700f7e2014-03-07 09:46:20 -0800539 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
540 RegStorage r_base) :
541 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
542 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800543 }
544
545 void Compile() {
546 LIR* unresolved_target = GenerateTargetLabel();
547 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700548 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700549 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
550 storage_index_, true);
551 } else {
552 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
553 storage_index_, true);
554 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800555 // Copy helper's result into r_base, a no-op on all but MIPS.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700556 m2l_->OpRegCopy(r_base_, m2l_->TargetRefReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800557
558 m2l_->OpUnconditionalBranch(cont_);
559 }
560
561 private:
562 LIR* const uninit_;
563 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800564 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800565};
566
Andreas Gampe2f244e92014-05-08 03:35:25 -0700567template <size_t pointer_size>
568static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
569 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
570 ThreadOffset<pointer_size> setter_offset =
571 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
572 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
573 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
574 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
575 true);
576}
577
Vladimir Markobe0e5462014-02-26 11:24:15 +0000578void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700579 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000580 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
581 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100582 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
583 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
584 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800586 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000587 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100589 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700590 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000591 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
592 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800593 if (IsTemp(rl_method.reg)) {
594 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 }
596 } else {
597 // Medium path, static storage base in a different class which requires checks that the other
598 // class is initialized.
599 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000600 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 // May do runtime call so everything to home locations.
602 FlushAllRegs();
603 // Using fixed register to sync with possible call to runtime support.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700604 RegStorage r_method = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 LockTemp(r_method);
606 LoadCurrMethodDirect(r_method);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700607 r_base = TargetRefReg(kArg0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800608 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000609 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
610 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000611 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000612 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800613 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000614 if (!field_info.IsInitialized() &&
615 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800617
618 // The slow path is invoked if the r_base is NULL or the class pointed
619 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800620 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700621 RegStorage r_tmp = TargetReg(kArg2, false);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800623 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800624 mirror::Class::StatusOffset().Int32Value(),
625 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800626 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800627
buzbee2700f7e2014-03-07 09:46:20 -0800628 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000629 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800630
631 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700632 // Ensure load of status and load of value don't re-order.
633 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 FreeTemp(r_method);
636 }
637 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100638 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100640 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100642 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000644 if (is_object) {
645 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
646 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100647 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000648 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
649 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
651 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800652 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800654 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 } else {
656 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700657 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700658 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
659 } else {
660 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 }
663}
664
Andreas Gampe2f244e92014-05-08 03:35:25 -0700665template <size_t pointer_size>
666static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
667 const MirSFieldLoweringInfo* field_info) {
668 ThreadOffset<pointer_size> getter_offset =
669 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
670 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
671 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
672 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
673}
674
Vladimir Markobe0e5462014-02-26 11:24:15 +0000675void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700676 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000677 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
678 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100679 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
680 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
681 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000682 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800683 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000684 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 // Fast path, static storage base is this method's class
686 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700687 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000688 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
689 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 } else {
691 // Medium path, static storage base in a different class which requires checks that the other
692 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000693 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 // May do runtime call so everything to home locations.
695 FlushAllRegs();
696 // Using fixed register to sync with possible call to runtime support.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700697 RegStorage r_method = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 LockTemp(r_method);
699 LoadCurrMethodDirect(r_method);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700700 r_base = TargetRefReg(kArg0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800701 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000702 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
703 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000704 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000705 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800706 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000707 if (!field_info.IsInitialized() &&
708 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800709 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800710
711 // The slow path is invoked if the r_base is NULL or the class pointed
712 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800713 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700714 RegStorage r_tmp = TargetReg(kArg2, false);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800715 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800716 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800717 mirror::Class::StatusOffset().Int32Value(),
718 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800719 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800720
buzbee2700f7e2014-03-07 09:46:20 -0800721 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000722 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800723
724 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700725 // Ensure load of status and load of value don't re-order.
726 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 FreeTemp(r_method);
729 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800730 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100731 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
732 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800733
Vladimir Marko674744e2014-04-24 15:18:26 +0100734 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000735 if (is_object) {
736 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
737 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100738 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000739 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
740 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800741 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100742 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800743
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 if (is_long_or_double) {
745 StoreValueWide(rl_dest, rl_result);
746 } else {
747 StoreValue(rl_dest, rl_result);
748 }
749 } else {
750 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700751 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700752 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
753 } else {
754 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
755 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700756 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700758 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 StoreValueWide(rl_dest, rl_result);
760 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700761 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 StoreValue(rl_dest, rl_result);
763 }
764 }
765}
766
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800767// Generate code for all slow paths.
768void Mir2Lir::HandleSlowPaths() {
769 int n = slow_paths_.Size();
770 for (int i = 0; i < n; ++i) {
771 LIRSlowPath* slowpath = slow_paths_.Get(i);
772 slowpath->Compile();
773 }
774 slow_paths_.Reset();
775}
776
Andreas Gampe2f244e92014-05-08 03:35:25 -0700777template <size_t pointer_size>
778static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
779 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
780 ThreadOffset<pointer_size> getter_offset =
781 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
782 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
783 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
784 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
785 true);
786}
787
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700790 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000791 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
792 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100793 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
794 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
795 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
796 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000797 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700798 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100799 GenNullCheck(rl_obj.reg, opt_flags);
800 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
801 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000802 LIR* load_lir;
803 if (is_object) {
804 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
805 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100806 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000807 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
808 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100809 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000810 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 StoreValueWide(rl_dest, rl_result);
813 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 StoreValue(rl_dest, rl_result);
815 }
816 } else {
buzbee33ae5582014-06-12 14:56:32 -0700817 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700818 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
819 } else {
820 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
821 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700823 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 StoreValueWide(rl_dest, rl_result);
825 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700826 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 StoreValue(rl_dest, rl_result);
828 }
829 }
830}
831
Andreas Gampe2f244e92014-05-08 03:35:25 -0700832template <size_t pointer_size>
833static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
834 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
835 RegLocation rl_src) {
836 ThreadOffset<pointer_size> setter_offset =
837 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
838 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
839 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
840 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
841 rl_obj, rl_src, true);
842}
843
Vladimir Markobe0e5462014-02-26 11:24:15 +0000844void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700846 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000847 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
848 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100849 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
850 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
851 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
852 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000853 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700854 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100856 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857 } else {
858 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100859 }
860 GenNullCheck(rl_obj.reg, opt_flags);
861 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000862 LIR* store;
863 if (is_object) {
864 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
865 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100866 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000867 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
868 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100869 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000870 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100871 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
872 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 }
874 } else {
buzbee33ae5582014-06-12 14:56:32 -0700875 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700876 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
877 } else {
878 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
879 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 }
881}
882
Andreas Gampe2f244e92014-05-08 03:35:25 -0700883template <size_t pointer_size>
884static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
885 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
886 ThreadOffset<pointer_size> helper = needs_range_check
887 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
888 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
889 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
890 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
891 true);
892}
893
Ian Rogersa9a82542013-10-04 11:17:26 -0700894void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
895 RegLocation rl_src) {
896 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
897 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
898 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700899 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700900 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
901 } else {
902 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
903 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700904}
905
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700906void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700908 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700909 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700910 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700912 *cu_->dex_file,
913 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 // Call out to helper which resolves type and verifies access.
915 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700916 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700917 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
918 type_idx, rl_method.reg, true);
919 } else {
920 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
921 type_idx, rl_method.reg, true);
922 }
buzbeea0cd2d72014-06-01 09:33:49 -0700923 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 StoreValue(rl_dest, rl_result);
925 } else {
926 // We're don't need access checks, load type from dex cache
927 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700928 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000929 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000930 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000931 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
933 type_idx) || SLOW_TYPE_PATH) {
934 // Slow path, at runtime test if type is null and if so initialize
935 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800936 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800937 LIR* cont = NewLIR0(kPseudoTargetLabel);
938
939 // Object to generate the slow path for class resolution.
940 class SlowPath : public LIRSlowPath {
941 public:
942 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
943 const RegLocation& rl_method, const RegLocation& rl_result) :
944 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
945 rl_method_(rl_method), rl_result_(rl_result) {
946 }
947
948 void Compile() {
949 GenerateTargetLabel();
950
buzbee33ae5582014-06-12 14:56:32 -0700951 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700952 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
953 rl_method_.reg, true);
954 } else {
955 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
956 rl_method_.reg, true);
957 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700958 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetRefReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800959
960 m2l_->OpUnconditionalBranch(cont_);
961 }
962
963 private:
964 const int type_idx_;
965 const RegLocation rl_method_;
966 const RegLocation rl_result_;
967 };
968
969 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800970 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800971
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800973 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 // Fast path, we're done - just store result
975 StoreValue(rl_dest, rl_result);
976 }
977 }
978}
979
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700980void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000982 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
983 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
985 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
986 // slow path, resolve string if not in dex cache
987 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700988 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800989
990 // If the Method* is already in a register, we can save a copy.
991 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800992 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800993 if (rl_method.location == kLocPhysReg) {
994 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800995 DCHECK(!IsTemp(rl_method.reg));
996 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800997 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700998 r_method = TargetRefReg(kArg2);
Mark Mendell766e9292014-01-27 07:55:47 -0800999 LoadCurrMethodDirect(r_method);
1000 }
buzbee695d13a2014-04-19 13:32:20 -07001001 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -07001002 TargetRefReg(kArg0), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001003
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampe4b537a82014-06-30 22:24:53 -07001005 LoadRefDisp(TargetRefReg(kArg0), offset_of_string, TargetRefReg(kRet0), kNotVolatile);
1006 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetRefReg(kRet0), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001007 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001008
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001009 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001010 // Object to generate the slow path for string resolution.
1011 class SlowPath : public LIRSlowPath {
1012 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001013 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1014 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1015 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001016 }
1017
1018 void Compile() {
1019 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001020 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001021 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1022 r_method_, string_idx_, true);
1023 } else {
1024 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1025 r_method_, string_idx_, true);
1026 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001027 m2l_->OpUnconditionalBranch(cont_);
1028 }
1029
1030 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001031 const RegStorage r_method_;
1032 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001033 };
1034
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001035 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001037
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001039 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 } else {
1041 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001042 RegStorage res_reg = AllocTempRef();
1043 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001044 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1045 kNotVolatile);
1046 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 StoreValue(rl_dest, rl_result);
1048 }
1049}
1050
Andreas Gampe2f244e92014-05-08 03:35:25 -07001051template <size_t pointer_size>
1052static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1053 RegLocation rl_dest) {
1054 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 // alloc will always check for resolution, do we also need to verify
1056 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001057 ThreadOffset<pointer_size> func_offset(-1);
1058 const DexFile* dex_file = cu->dex_file;
1059 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001060 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001061 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 bool is_type_initialized;
1063 bool use_direct_type_ptr;
1064 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001065 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001066 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001067 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1068 &direct_type_ptr, &is_finalizable) &&
1069 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 // The fast path.
1071 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001072 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001073 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001074 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001075 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetRefReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001076 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001077 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001078 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetRefReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001079 }
1080 } else {
1081 // Use the direct pointer.
1082 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001083 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1084 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001085 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001086 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1087 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001088 }
1089 }
1090 } else {
1091 // The slow path.
1092 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001093 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1094 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001095 }
1096 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001098 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1099 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
buzbeea0cd2d72014-06-01 09:33:49 -07001101 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001102 mir_to_lir->StoreValue(rl_dest, rl_result);
1103}
1104
1105/*
1106 * Let helper function take care of everything. Will
1107 * call Class::NewInstanceFromCode(type_idx, method);
1108 */
1109void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001110 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001111 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1112 } else {
1113 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1114 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115}
1116
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001117void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001119 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001120 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1121 } else {
1122 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1123 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124}
1125
1126// For final classes there are no sub-classes to check and so we can answer the instance-of
1127// question with simple comparisons.
1128void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1129 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001130 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001131 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001132
buzbeea0cd2d72014-06-01 09:33:49 -07001133 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001135 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001136 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001138 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 }
1140 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001141 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142
buzbeea0cd2d72014-06-01 09:33:49 -07001143 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1144 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145
1146 LoadCurrMethodDirect(check_class);
1147 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001148 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1149 kNotVolatile);
1150 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1151 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 } else {
buzbee695d13a2014-04-19 13:32:20 -07001153 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001154 check_class, kNotVolatile);
1155 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1156 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001157 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001158 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160
1161 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001162 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 if (cu_->instruction_set == kThumb2) {
1164 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001165 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001167 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 } else {
1169 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1170 LoadConstant(result_reg, 1); // eq case - load true
1171 }
1172 LIR* target = NewLIR0(kPseudoTargetLabel);
1173 null_branchover->target = target;
1174 if (ne_branchover != NULL) {
1175 ne_branchover->target = target;
1176 }
1177 FreeTemp(object_class);
1178 FreeTemp(check_class);
1179 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001180 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 FreeTemp(result_reg);
1182 }
1183 StoreValue(rl_dest, rl_result);
1184}
1185
1186void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1187 bool type_known_abstract, bool use_declaring_class,
1188 bool can_assume_type_is_in_dex_cache,
1189 uint32_t type_idx, RegLocation rl_dest,
1190 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001191 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001192 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001193
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 FlushAllRegs();
1195 // May generate a call - use explicit registers
1196 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001197 RegStorage method_reg = TargetRefReg(kArg1);
1198 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1199 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 if (needs_access_check) {
1201 // Check we have access to type_idx and if not throw IllegalAccessError,
1202 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001203 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001204 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1205 type_idx, true);
1206 } else {
1207 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1208 type_idx, true);
1209 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001210 OpRegCopy(class_reg, TargetRefReg(kRet0)); // Align usage with fast path
1211 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 } else if (use_declaring_class) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001213 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001214 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001215 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 } else {
1217 // Load dex cache entry into class_reg (kArg2)
Chao-ying Fua77ee512014-07-01 17:43:41 -07001218 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001219 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001220 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001221 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001222 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 if (!can_assume_type_is_in_dex_cache) {
1224 // Need to test presence of type in dex cache at runtime
1225 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1226 // Not resolved
1227 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001228 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001229 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1230 } else {
1231 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1232 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001233 OpRegCopy(TargetRefReg(kArg2), TargetRefReg(kRet0)); // Align usage with fast path
Chao-ying Fua77ee512014-07-01 17:43:41 -07001234 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); /* reload Ref */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 // Rejoin code paths
1236 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1237 hop_branch->target = hop_target;
1238 }
1239 }
1240 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001241 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001242 if (cu_->instruction_set == kMips) {
1243 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001244 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001246 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetRefReg(kArg0), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247
1248 /* load object->klass_ */
1249 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001250 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetRefReg(kArg1),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001251 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1253 LIR* branchover = NULL;
1254 if (type_known_final) {
1255 // rl_result == ref == null == 0.
1256 if (cu_->instruction_set == kThumb2) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001257 OpRegReg(kOpCmp, TargetRefReg(kArg1), TargetRefReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001258 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001259 LoadConstant(rl_result.reg, 1); // .eq case - load true
1260 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001261 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001263 LoadConstant(rl_result.reg, 0); // ne case - load false
Chao-ying Fua77ee512014-07-01 17:43:41 -07001264 branchover = OpCmpBranch(kCondNe, TargetRefReg(kArg1), TargetRefReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001265 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 }
1267 } else {
1268 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001269 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001270 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1271 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001272 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 if (!type_known_abstract) {
1274 /* Uses conditional nullification */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001275 OpRegReg(kOpCmp, TargetRefReg(kArg1), TargetRefReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001276 it = OpIT(kCondEq, "EE"); // if-convert the test
Chao-ying Fua77ee512014-07-01 17:43:41 -07001277 LoadConstant(TargetReg(kArg0, false), 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001279 OpRegCopy(TargetRefReg(kArg0), TargetRefReg(kArg2)); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001281 if (it != nullptr) {
1282 OpEndIT(it);
1283 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 FreeTemp(r_tgt);
1285 } else {
1286 if (!type_known_abstract) {
1287 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001288 LoadConstant(rl_result.reg, 1); // assume true
Chao-ying Fua77ee512014-07-01 17:43:41 -07001289 branchover = OpCmpBranch(kCondEq, TargetRefReg(kArg1), TargetRefReg(kArg2), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 }
buzbee33ae5582014-06-12 14:56:32 -07001291 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001292 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1293 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Chao-ying Fua77ee512014-07-01 17:43:41 -07001294 OpRegCopy(TargetRefReg(kArg0), TargetRefReg(kArg2)); // .ne case - arg0 <= class
Mark Mendell6607d972014-02-10 06:54:18 -08001295 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1296 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 }
1298 }
1299 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001300 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 /* branch targets here */
1302 LIR* target = NewLIR0(kPseudoTargetLabel);
1303 StoreValue(rl_dest, rl_result);
1304 branch1->target = target;
1305 if (branchover != NULL) {
1306 branchover->target = target;
1307 }
1308}
1309
1310void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1311 bool type_known_final, type_known_abstract, use_declaring_class;
1312 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1313 *cu_->dex_file,
1314 type_idx,
1315 &type_known_final,
1316 &type_known_abstract,
1317 &use_declaring_class);
1318 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1319 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1320
1321 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1322 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1323 } else {
1324 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1325 use_declaring_class, can_assume_type_is_in_dex_cache,
1326 type_idx, rl_dest, rl_src);
1327 }
1328}
1329
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001330void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 bool type_known_final, type_known_abstract, use_declaring_class;
1332 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1333 *cu_->dex_file,
1334 type_idx,
1335 &type_known_final,
1336 &type_known_abstract,
1337 &use_declaring_class);
1338 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1339 // of the exception throw path.
1340 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001341 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 // Verifier type analysis proved this check cast would never cause an exception.
1343 return;
1344 }
1345 FlushAllRegs();
1346 // May generate a call - use explicit registers
1347 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001348 RegStorage method_reg = TargetRefReg(kArg1);
1349 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1350 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 if (needs_access_check) {
1352 // Check we have access to type_idx and if not throw IllegalAccessError,
1353 // returns Class* in kRet0
1354 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001355 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001356 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1357 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001358 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001359 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1360 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001361 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001362 OpRegCopy(class_reg, TargetRefReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001364 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001365 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 } else {
1367 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001368 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001369 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001370 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001371 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1373 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001374 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1375 LIR* cont = NewLIR0(kPseudoTargetLabel);
1376
1377 // Slow path to initialize the type. Executed if the type is NULL.
1378 class SlowPath : public LIRSlowPath {
1379 public:
1380 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001381 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001382 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1383 class_reg_(class_reg) {
1384 }
1385
1386 void Compile() {
1387 GenerateTargetLabel();
1388
1389 // Call out to helper, which will return resolved type in kArg0
1390 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001391 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001392 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001393 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001394 } else {
1395 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001396 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001397 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001398 m2l_->OpRegCopy(class_reg_, m2l_->TargetRefReg(kRet0)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001399 m2l_->OpUnconditionalBranch(cont_);
1400 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001401
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001402 public:
1403 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001404 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405 };
1406
buzbee2700f7e2014-03-07 09:46:20 -08001407 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 }
1409 }
1410 // At this point, class_reg (kArg2) has class
Andreas Gampe4b537a82014-06-30 22:24:53 -07001411 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001412
1413 // Slow path for the case where the classes are not equal. In this case we need
1414 // to call a helper function to do the check.
1415 class SlowPath : public LIRSlowPath {
1416 public:
1417 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1418 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1419 }
1420
1421 void Compile() {
1422 GenerateTargetLabel();
1423
1424 if (load_) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001425 m2l_->LoadRefDisp(m2l_->TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1426 m2l_->TargetRefReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001427 }
buzbee33ae5582014-06-12 14:56:32 -07001428 if (m2l_->cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001429 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetRefReg(kArg2),
1430 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001431 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001432 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetRefReg(kArg2),
1433 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001434 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001435
1436 m2l_->OpUnconditionalBranch(cont_);
1437 }
1438
1439 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001440 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001441 };
1442
1443 if (type_known_abstract) {
1444 // Easier case, run slow path if target is non-null (slow path will load from target)
Chao-ying Fua77ee512014-07-01 17:43:41 -07001445 LIR* branch = OpCmpImmBranch(kCondNe, TargetRefReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001446 LIR* cont = NewLIR0(kPseudoTargetLabel);
1447 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1448 } else {
1449 // Harder, more common case. We need to generate a forward branch over the load
1450 // if the target is null. If it's non-null we perform the load and branch to the
1451 // slow path if the classes are not equal.
1452
1453 /* Null is OK - continue */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001454 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetRefReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001455 /* load object->klass_ */
1456 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001457 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1458 TargetRefReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459
Andreas Gampe4b537a82014-06-30 22:24:53 -07001460 LIR* branch2 = OpCmpBranch(kCondNe, TargetRefReg(kArg1), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001461 LIR* cont = NewLIR0(kPseudoTargetLabel);
1462
1463 // Add the slow path that will not perform load since this is already done.
1464 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1465
1466 // Set the null check to branch to the continuation.
1467 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 }
1469}
1470
1471void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001472 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 RegLocation rl_result;
1474 if (cu_->instruction_set == kThumb2) {
1475 /*
1476 * NOTE: This is the one place in the code in which we might have
1477 * as many as six live temporary registers. There are 5 in the normal
1478 * set for Arm. Until we have spill capabilities, temporarily add
1479 * lr to the temp set. It is safe to do this locally, but note that
1480 * lr is used explicitly elsewhere in the code generator and cannot
1481 * normally be used as a general temp register.
1482 */
1483 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1484 FreeTemp(TargetReg(kLr)); // and make it available
1485 }
1486 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1487 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1488 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1489 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001490 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1491 RegStorage t_reg = AllocTemp();
1492 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1493 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1494 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 FreeTemp(t_reg);
1496 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001497 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1498 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 }
1500 /*
1501 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1502 * following StoreValueWide might need to allocate a temp register.
1503 * To further work around the lack of a spill capability, explicitly
1504 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1505 * Remove when spill is functional.
1506 */
1507 FreeRegLocTemps(rl_result, rl_src1);
1508 FreeRegLocTemps(rl_result, rl_src2);
1509 StoreValueWide(rl_dest, rl_result);
1510 if (cu_->instruction_set == kThumb2) {
1511 Clobber(TargetReg(kLr));
1512 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1513 }
1514}
1515
1516
Andreas Gampe2f244e92014-05-08 03:35:25 -07001517template <size_t pointer_size>
1518static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1519 RegLocation rl_shift) {
1520 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521
1522 switch (opcode) {
1523 case Instruction::SHL_LONG:
1524 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001525 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 break;
1527 case Instruction::SHR_LONG:
1528 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001529 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 break;
1531 case Instruction::USHR_LONG:
1532 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001533 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 break;
1535 default:
1536 LOG(FATAL) << "Unexpected case";
1537 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001538 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1539 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1540}
1541
1542void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1543 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001544 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001545 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1546 } else {
1547 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1548 }
buzbeea0cd2d72014-06-01 09:33:49 -07001549 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 StoreValueWide(rl_dest, rl_result);
1551}
1552
1553
1554void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001555 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001556 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 OpKind op = kOpBkpt;
1558 bool is_div_rem = false;
1559 bool check_zero = false;
1560 bool unary = false;
1561 RegLocation rl_result;
1562 bool shift_op = false;
1563 switch (opcode) {
1564 case Instruction::NEG_INT:
1565 op = kOpNeg;
1566 unary = true;
1567 break;
1568 case Instruction::NOT_INT:
1569 op = kOpMvn;
1570 unary = true;
1571 break;
1572 case Instruction::ADD_INT:
1573 case Instruction::ADD_INT_2ADDR:
1574 op = kOpAdd;
1575 break;
1576 case Instruction::SUB_INT:
1577 case Instruction::SUB_INT_2ADDR:
1578 op = kOpSub;
1579 break;
1580 case Instruction::MUL_INT:
1581 case Instruction::MUL_INT_2ADDR:
1582 op = kOpMul;
1583 break;
1584 case Instruction::DIV_INT:
1585 case Instruction::DIV_INT_2ADDR:
1586 check_zero = true;
1587 op = kOpDiv;
1588 is_div_rem = true;
1589 break;
1590 /* NOTE: returns in kArg1 */
1591 case Instruction::REM_INT:
1592 case Instruction::REM_INT_2ADDR:
1593 check_zero = true;
1594 op = kOpRem;
1595 is_div_rem = true;
1596 break;
1597 case Instruction::AND_INT:
1598 case Instruction::AND_INT_2ADDR:
1599 op = kOpAnd;
1600 break;
1601 case Instruction::OR_INT:
1602 case Instruction::OR_INT_2ADDR:
1603 op = kOpOr;
1604 break;
1605 case Instruction::XOR_INT:
1606 case Instruction::XOR_INT_2ADDR:
1607 op = kOpXor;
1608 break;
1609 case Instruction::SHL_INT:
1610 case Instruction::SHL_INT_2ADDR:
1611 shift_op = true;
1612 op = kOpLsl;
1613 break;
1614 case Instruction::SHR_INT:
1615 case Instruction::SHR_INT_2ADDR:
1616 shift_op = true;
1617 op = kOpAsr;
1618 break;
1619 case Instruction::USHR_INT:
1620 case Instruction::USHR_INT_2ADDR:
1621 shift_op = true;
1622 op = kOpLsr;
1623 break;
1624 default:
1625 LOG(FATAL) << "Invalid word arith op: " << opcode;
1626 }
1627 if (!is_div_rem) {
1628 if (unary) {
1629 rl_src1 = LoadValue(rl_src1, kCoreReg);
1630 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001633 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001634 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001635 RegStorage t_reg = AllocTemp();
1636 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 rl_src1 = LoadValue(rl_src1, kCoreReg);
1638 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001639 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 FreeTemp(t_reg);
1641 } else {
1642 rl_src1 = LoadValue(rl_src1, kCoreReg);
1643 rl_src2 = LoadValue(rl_src2, kCoreReg);
1644 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001645 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001646 }
1647 }
1648 StoreValue(rl_dest, rl_result);
1649 } else {
Dave Allison70202782013-10-22 17:52:19 -07001650 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 +01001651 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 rl_src1 = LoadValue(rl_src1, kCoreReg);
1653 rl_src2 = LoadValue(rl_src2, kCoreReg);
1654 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001655 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 }
buzbee2700f7e2014-03-07 09:46:20 -08001657 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001658 done = true;
1659 } else if (cu_->instruction_set == kThumb2) {
1660 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1661 // Use ARM SDIV instruction for division. For remainder we also need to
1662 // calculate using a MUL and subtract.
1663 rl_src1 = LoadValue(rl_src1, kCoreReg);
1664 rl_src2 = LoadValue(rl_src2, kCoreReg);
1665 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001666 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001667 }
buzbee2700f7e2014-03-07 09:46:20 -08001668 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001669 done = true;
1670 }
1671 }
1672
1673 // If we haven't already generated the code use the callout function.
1674 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 FlushAllRegs(); /* Send everything to home location */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001676 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, false));
buzbee33ae5582014-06-12 14:56:32 -07001677 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001678 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1679 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Chao-ying Fua77ee512014-07-01 17:43:41 -07001680 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 if (check_zero) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001682 GenDivZeroCheck(TargetReg(kArg1, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 }
Dave Allison70202782013-10-22 17:52:19 -07001684 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001685 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001686 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1687 } else {
1688 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1689 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001691 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 else
1693 rl_result = GetReturnAlt();
1694 }
1695 StoreValue(rl_dest, rl_result);
1696 }
1697}
1698
1699/*
1700 * The following are the first-level codegen routines that analyze the format
1701 * of each bytecode then either dispatch special purpose codegen routines
1702 * or produce corresponding Thumb instructions directly.
1703 */
1704
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001706static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 x &= x - 1;
1708 return (x & (x - 1)) == 0;
1709}
1710
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1712// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001713bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001714 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1716 return false;
1717 }
1718 // No divide instruction for Arm, so check for more special cases
1719 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001720 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 }
1722 int k = LowestSetBit(lit);
1723 if (k >= 30) {
1724 // Avoid special cases.
1725 return false;
1726 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 rl_src = LoadValue(rl_src, kCoreReg);
1728 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001729 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001730 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 if (lit == 2) {
1732 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001733 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1734 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1735 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001737 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001739 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1740 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 }
1742 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001743 RegStorage t_reg1 = AllocTemp();
1744 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001746 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1747 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001749 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001753 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001755 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 }
1757 }
1758 StoreValue(rl_dest, rl_result);
1759 return true;
1760}
1761
1762// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1763// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001764bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001765 if (lit < 0) {
1766 return false;
1767 }
1768 if (lit == 0) {
1769 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1770 LoadConstant(rl_result.reg, 0);
1771 StoreValue(rl_dest, rl_result);
1772 return true;
1773 }
1774 if (lit == 1) {
1775 rl_src = LoadValue(rl_src, kCoreReg);
1776 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1777 OpRegCopy(rl_result.reg, rl_src.reg);
1778 StoreValue(rl_dest, rl_result);
1779 return true;
1780 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001781 // There is RegRegRegShift on Arm, so check for more special cases
1782 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001783 return EasyMultiply(rl_src, rl_dest, lit);
1784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001785 // Can we simplify this multiplication?
1786 bool power_of_two = false;
1787 bool pop_count_le2 = false;
1788 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001789 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 power_of_two = true;
1791 } else if (IsPopCountLE2(lit)) {
1792 pop_count_le2 = true;
1793 } else if (IsPowerOfTwo(lit + 1)) {
1794 power_of_two_minus_one = true;
1795 } else {
1796 return false;
1797 }
1798 rl_src = LoadValue(rl_src, kCoreReg);
1799 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1800 if (power_of_two) {
1801 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001802 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803 } else if (pop_count_le2) {
1804 // Shift and add and shift.
1805 int first_bit = LowestSetBit(lit);
1806 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1807 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1808 } else {
1809 // Reverse subtract: (src << (shift + 1)) - src.
1810 DCHECK(power_of_two_minus_one);
1811 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001812 RegStorage t_reg = AllocTemp();
1813 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1814 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 }
1816 StoreValue(rl_dest, rl_result);
1817 return true;
1818}
1819
1820void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001821 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001822 RegLocation rl_result;
1823 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1824 int shift_op = false;
1825 bool is_div = false;
1826
1827 switch (opcode) {
1828 case Instruction::RSUB_INT_LIT8:
1829 case Instruction::RSUB_INT: {
1830 rl_src = LoadValue(rl_src, kCoreReg);
1831 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1832 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001833 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001835 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1836 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 }
1838 StoreValue(rl_dest, rl_result);
1839 return;
1840 }
1841
1842 case Instruction::SUB_INT:
1843 case Instruction::SUB_INT_2ADDR:
1844 lit = -lit;
1845 // Intended fallthrough
1846 case Instruction::ADD_INT:
1847 case Instruction::ADD_INT_2ADDR:
1848 case Instruction::ADD_INT_LIT8:
1849 case Instruction::ADD_INT_LIT16:
1850 op = kOpAdd;
1851 break;
1852 case Instruction::MUL_INT:
1853 case Instruction::MUL_INT_2ADDR:
1854 case Instruction::MUL_INT_LIT8:
1855 case Instruction::MUL_INT_LIT16: {
1856 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1857 return;
1858 }
1859 op = kOpMul;
1860 break;
1861 }
1862 case Instruction::AND_INT:
1863 case Instruction::AND_INT_2ADDR:
1864 case Instruction::AND_INT_LIT8:
1865 case Instruction::AND_INT_LIT16:
1866 op = kOpAnd;
1867 break;
1868 case Instruction::OR_INT:
1869 case Instruction::OR_INT_2ADDR:
1870 case Instruction::OR_INT_LIT8:
1871 case Instruction::OR_INT_LIT16:
1872 op = kOpOr;
1873 break;
1874 case Instruction::XOR_INT:
1875 case Instruction::XOR_INT_2ADDR:
1876 case Instruction::XOR_INT_LIT8:
1877 case Instruction::XOR_INT_LIT16:
1878 op = kOpXor;
1879 break;
1880 case Instruction::SHL_INT_LIT8:
1881 case Instruction::SHL_INT:
1882 case Instruction::SHL_INT_2ADDR:
1883 lit &= 31;
1884 shift_op = true;
1885 op = kOpLsl;
1886 break;
1887 case Instruction::SHR_INT_LIT8:
1888 case Instruction::SHR_INT:
1889 case Instruction::SHR_INT_2ADDR:
1890 lit &= 31;
1891 shift_op = true;
1892 op = kOpAsr;
1893 break;
1894 case Instruction::USHR_INT_LIT8:
1895 case Instruction::USHR_INT:
1896 case Instruction::USHR_INT_2ADDR:
1897 lit &= 31;
1898 shift_op = true;
1899 op = kOpLsr;
1900 break;
1901
1902 case Instruction::DIV_INT:
1903 case Instruction::DIV_INT_2ADDR:
1904 case Instruction::DIV_INT_LIT8:
1905 case Instruction::DIV_INT_LIT16:
1906 case Instruction::REM_INT:
1907 case Instruction::REM_INT_2ADDR:
1908 case Instruction::REM_INT_LIT8:
1909 case Instruction::REM_INT_LIT16: {
1910 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001911 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912 return;
1913 }
buzbee11b63d12013-08-27 07:34:17 -07001914 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001916 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 (opcode == Instruction::DIV_INT_LIT16)) {
1918 is_div = true;
1919 } else {
1920 is_div = false;
1921 }
buzbee11b63d12013-08-27 07:34:17 -07001922 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1923 return;
1924 }
Dave Allison70202782013-10-22 17:52:19 -07001925
1926 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001927 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001929 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001930 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001931 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001932 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1933 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001934 } else if (cu_->instruction_set == kThumb2) {
1935 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1936 // Use ARM SDIV instruction for division. For remainder we also need to
1937 // calculate using a MUL and subtract.
1938 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001939 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001940 done = true;
1941 }
1942 }
1943
1944 if (!done) {
1945 FlushAllRegs(); /* Everything to home location. */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001946 LoadValueDirectFixed(rl_src, TargetReg(kArg0, false));
1947 Clobber(TargetReg(kArg0, false));
buzbee33ae5582014-06-12 14:56:32 -07001948 if (cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001949 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, false), lit,
Andreas Gampe2f244e92014-05-08 03:35:25 -07001950 false);
1951 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001952 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, false), lit,
Andreas Gampe2f244e92014-05-08 03:35:25 -07001953 false);
1954 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001956 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 else
1958 rl_result = GetReturnAlt();
1959 }
1960 StoreValue(rl_dest, rl_result);
1961 return;
1962 }
1963 default:
1964 LOG(FATAL) << "Unexpected opcode " << opcode;
1965 }
1966 rl_src = LoadValue(rl_src, kCoreReg);
1967 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001968 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001970 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001971 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001972 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 }
1974 StoreValue(rl_dest, rl_result);
1975}
1976
Andreas Gampe2f244e92014-05-08 03:35:25 -07001977template <size_t pointer_size>
1978static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1979 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 RegLocation rl_result;
1981 OpKind first_op = kOpBkpt;
1982 OpKind second_op = kOpBkpt;
1983 bool call_out = false;
1984 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001985 ThreadOffset<pointer_size> func_offset(-1);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001986 int ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987
1988 switch (opcode) {
1989 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001990 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001991 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1992 return;
1993 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001994 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1995 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001997 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001998 RegStorage t_reg = mir_to_lir->AllocTemp();
1999 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2000 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2001 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2002 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002003 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002004 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2005 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002007 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008 return;
2009 case Instruction::ADD_LONG:
2010 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002011 if (cu->instruction_set != kThumb2) {
2012 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 return;
2014 }
2015 first_op = kOpAdd;
2016 second_op = kOpAdc;
2017 break;
2018 case Instruction::SUB_LONG:
2019 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002020 if (cu->instruction_set != kThumb2) {
2021 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 return;
2023 }
2024 first_op = kOpSub;
2025 second_op = kOpSbc;
2026 break;
2027 case Instruction::MUL_LONG:
2028 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002029 if (cu->instruction_set != kMips) {
2030 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031 return;
2032 } else {
2033 call_out = true;
Chao-ying Fua77ee512014-07-01 17:43:41 -07002034 ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002035 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 }
2037 break;
2038 case Instruction::DIV_LONG:
2039 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002040 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002041 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2042 return;
2043 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 call_out = true;
2045 check_zero = true;
Chao-ying Fua77ee512014-07-01 17:43:41 -07002046 ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002047 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 break;
2049 case Instruction::REM_LONG:
2050 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002051 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002052 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2053 return;
2054 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055 call_out = true;
2056 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002057 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Chao-ying Fua77ee512014-07-01 17:43:41 -07002059 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, false).GetReg() :
2060 mir_to_lir->TargetReg(kRet0, false).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 break;
2062 case Instruction::AND_LONG_2ADDR:
2063 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002064 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2065 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002066 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 }
2068 first_op = kOpAnd;
2069 second_op = kOpAnd;
2070 break;
2071 case Instruction::OR_LONG:
2072 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002073 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2074 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002075 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002076 return;
2077 }
2078 first_op = kOpOr;
2079 second_op = kOpOr;
2080 break;
2081 case Instruction::XOR_LONG:
2082 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002083 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2084 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002085 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002086 return;
2087 }
2088 first_op = kOpXor;
2089 second_op = kOpXor;
2090 break;
2091 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002092 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 return;
2094 }
2095 default:
2096 LOG(FATAL) << "Invalid long arith op";
2097 }
2098 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002101 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102 if (check_zero) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002103 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kArg1);
2104 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kArg3);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002105 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2106 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002107 mir_to_lir->GenDivZeroCheckWide(mir_to_lir->TargetReg(kArg2, kArg3));
Andreas Gampe2f244e92014-05-08 03:35:25 -07002108 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002109 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002110 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002111 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002112 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002113 }
2114 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Chao-ying Fua77ee512014-07-01 17:43:41 -07002115 if (ret_reg == mir_to_lir->TargetReg(kRet0, false).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002116 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002118 rl_result = mir_to_lir->GetReturnWideAlt();
2119 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002120 }
2121}
2122
Andreas Gampe2f244e92014-05-08 03:35:25 -07002123void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2124 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002125 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002126 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2127 } else {
2128 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2129 }
2130}
2131
Mark Mendelle87f9b52014-04-30 14:13:18 -04002132void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2133 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2134 LoadConstantNoClobber(rl_result.reg, value);
2135 StoreValue(rl_dest, rl_result);
2136 if (value == 0) {
2137 Workaround7250540(rl_dest, rl_result.reg);
2138 }
2139}
2140
Andreas Gampe2f244e92014-05-08 03:35:25 -07002141template <size_t pointer_size>
2142void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002143 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002144 /*
2145 * Don't optimize the register usage since it calls out to support
2146 * functions
2147 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002148 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2149
Brian Carlstrom7940e442013-07-12 13:46:57 -07002150 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002151 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2152 if (rl_dest.wide) {
2153 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002154 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002155 StoreValueWide(rl_dest, rl_result);
2156 } else {
2157 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002158 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002159 StoreValue(rl_dest, rl_result);
2160 }
2161}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002162template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2163 RegLocation rl_dest, RegLocation rl_src);
2164template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2165 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002166
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002167class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2168 public:
2169 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2170 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2171 }
2172
2173 void Compile() OVERRIDE {
2174 m2l_->ResetRegPool();
2175 m2l_->ResetDefTracking();
2176 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002177 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002178 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2179 } else {
2180 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2181 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002182 if (cont_ != nullptr) {
2183 m2l_->OpUnconditionalBranch(cont_);
2184 }
2185 }
2186};
2187
Brian Carlstrom7940e442013-07-12 13:46:57 -07002188/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002189void Mir2Lir::GenSuspendTest(int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002190 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002191 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2192 return;
2193 }
2194 FlushAllRegs();
2195 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002196 LIR* cont = NewLIR0(kPseudoTargetLabel);
2197 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002198 } else {
2199 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2200 return;
2201 }
2202 FlushAllRegs(); // TODO: needed?
2203 LIR* inst = CheckSuspendUsingLoad();
2204 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002205 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002206}
2207
2208/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002209void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002210 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002211 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2212 OpUnconditionalBranch(target);
2213 return;
2214 }
2215 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002216 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002217 LIR* branch = OpUnconditionalBranch(nullptr);
2218 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002219 } else {
2220 // For the implicit suspend check, just perform the trigger
2221 // load and branch to the target.
2222 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2223 OpUnconditionalBranch(target);
2224 return;
2225 }
2226 FlushAllRegs();
2227 LIR* inst = CheckSuspendUsingLoad();
2228 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002229 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002230 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002231}
2232
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002233/* Call out to helper assembly routine that will null check obj and then lock it. */
2234void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2235 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002236 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002237 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2238 } else {
2239 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2240 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002241}
2242
2243/* Call out to helper assembly routine that will null check obj and then unlock it. */
2244void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2245 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002246 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002247 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2248 } else {
2249 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2250 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002251}
2252
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002253/* Generic code for generating a wide constant into a VR. */
2254void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2255 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002256 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002257 StoreValueWide(rl_dest, rl_result);
2258}
2259
Brian Carlstrom7940e442013-07-12 13:46:57 -07002260} // namespace art