blob: 12a21d0238fd78c866d1a7087ebb0feb80de6bd2 [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"
Andreas Gampeaa910d52014-07-30 18:59:05 -070024#include "mirror/object_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080026#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29
Andreas Gampe9c3b0892014-04-24 17:33:34 +000030// Shortcuts to repeatedly used long types.
31typedef mirror::ObjectArray<mirror::Object> ObjArray;
32typedef mirror::ObjectArray<mirror::Class> ClassArray;
33
Brian Carlstrom7940e442013-07-12 13:46:57 -070034/*
35 * This source files contains "gen" codegen routines that should
36 * be applicable to most targets. Only mid-level support utilities
37 * and "op" calls may be used here.
38 */
39
40/*
buzbeeb48819d2013-09-14 16:15:25 -070041 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 * blocks.
43 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070044void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 LIR* barrier = NewLIR0(kPseudoBarrier);
46 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070047 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010048 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070049}
50
Mingyao Yange643a172014-04-08 11:02:52 -070051void Mir2Lir::GenDivZeroException() {
52 LIR* branch = OpUnconditionalBranch(nullptr);
53 AddDivZeroCheckSlowPath(branch);
54}
55
56void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070057 LIR* branch = OpCondBranch(c_code, nullptr);
58 AddDivZeroCheckSlowPath(branch);
59}
60
Mingyao Yange643a172014-04-08 11:02:52 -070061void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
62 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070063 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
67 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
68 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080069 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
70 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -070071 }
72
Mingyao Yange643a172014-04-08 11:02:52 -070073 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070074 m2l_->ResetRegPool();
75 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070076 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070077 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -070078 }
79 };
80
81 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
82}
Dave Allisonb373e092014-02-20 16:06:36 -080083
Mingyao Yang80365d92014-04-18 12:10:58 -070084void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
85 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
86 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080087 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
88 RegStorage length_in)
89 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
90 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -070091 }
92
93 void Compile() OVERRIDE {
94 m2l_->ResetRegPool();
95 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070096 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070097 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -070098 }
99
100 private:
101 const RegStorage index_;
102 const RegStorage length_;
103 };
104
105 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
106 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
107}
108
109void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
110 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
111 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800112 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
113 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
114 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700115 }
116
117 void Compile() OVERRIDE {
118 m2l_->ResetRegPool();
119 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700120 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700121
Andreas Gampeccc60262014-07-04 18:02:38 -0700122 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
123 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700124
125 m2l_->OpRegCopy(arg1_32, length_);
126 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700127 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700128 }
129
130 private:
131 const int32_t index_;
132 const RegStorage length_;
133 };
134
135 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
136 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
137}
138
Mingyao Yange643a172014-04-08 11:02:52 -0700139LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
140 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
141 public:
142 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
143 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
144 }
145
146 void Compile() OVERRIDE {
147 m2l_->ResetRegPool();
148 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700149 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700150 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700151 }
152 };
153
154 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
155 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
156 return branch;
157}
158
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800160LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000161 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700162 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600164 // If null check has not been eliminated, reset redundant store tracking.
165 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
166 ResetDefTracking();
167 }
Dave Allisonb373e092014-02-20 16:06:36 -0800168 return nullptr;
169}
170
Dave Allisonf9439142014-03-27 15:10:22 -0700171/* Perform an explicit null-check on a register. */
172LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
173 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
174 return NULL;
175 }
Mingyao Yange643a172014-04-08 11:02:52 -0700176 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700177}
178
Dave Allisonb373e092014-02-20 16:06:36 -0800179void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000180 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800181 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
182 return;
183 }
Dave Allison69dfe512014-07-11 17:11:58 +0000184 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800185 MarkSafepointPC(last_lir_insn_);
186 }
187}
188
Andreas Gampe3c12c512014-06-24 18:46:29 +0000189void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000190 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000191 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
192 return;
193 }
194 MarkSafepointPCAfter(after);
195 }
196}
197
Dave Allisonb373e092014-02-20 16:06:36 -0800198void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000199 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800200 MarkSafepointPC(last_lir_insn_);
201 }
202}
203
buzbee2700f7e2014-03-07 09:46:20 -0800204void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000205 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800206 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
207 return;
208 }
209 // Force an implicit null check by performing a memory operation (load) from the given
210 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800211 RegStorage tmp = AllocTemp();
212 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700213 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800214 FreeTemp(tmp);
215 MarkSafepointPC(load);
216 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217}
218
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700220 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700222 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 switch (opcode) {
224 case Instruction::IF_EQ:
225 cond = kCondEq;
226 break;
227 case Instruction::IF_NE:
228 cond = kCondNe;
229 break;
230 case Instruction::IF_LT:
231 cond = kCondLt;
232 break;
233 case Instruction::IF_GE:
234 cond = kCondGe;
235 break;
236 case Instruction::IF_GT:
237 cond = kCondGt;
238 break;
239 case Instruction::IF_LE:
240 cond = kCondLe;
241 break;
242 default:
243 cond = static_cast<ConditionCode>(0);
244 LOG(FATAL) << "Unexpected opcode " << opcode;
245 }
246
247 // Normalize such that if either operand is constant, src2 will be constant
248 if (rl_src1.is_const) {
249 RegLocation rl_temp = rl_src1;
250 rl_src1 = rl_src2;
251 rl_src2 = rl_temp;
252 cond = FlipComparisonOrder(cond);
253 }
254
buzbee7c02e912014-10-03 13:14:17 -0700255 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 // Is this really an immediate comparison?
257 if (rl_src2.is_const) {
258 // If it's already live in a register or not easily materialized, just keep going
259 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700260 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100262 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800264 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 return;
266 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700267
268 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
269 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
270 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
271 // the 32b literal 0 for null.
272 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
273 // Use the OpCmpImmBranch and ignore the value in the register.
274 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
275 return;
276 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700278
buzbee7c02e912014-10-03 13:14:17 -0700279 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800280 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281}
282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700285 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
286 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 switch (opcode) {
288 case Instruction::IF_EQZ:
289 cond = kCondEq;
290 break;
291 case Instruction::IF_NEZ:
292 cond = kCondNe;
293 break;
294 case Instruction::IF_LTZ:
295 cond = kCondLt;
296 break;
297 case Instruction::IF_GEZ:
298 cond = kCondGe;
299 break;
300 case Instruction::IF_GTZ:
301 cond = kCondGt;
302 break;
303 case Instruction::IF_LEZ:
304 cond = kCondLe;
305 break;
306 default:
307 cond = static_cast<ConditionCode>(0);
308 LOG(FATAL) << "Unexpected opcode " << opcode;
309 }
buzbee2700f7e2014-03-07 09:46:20 -0800310 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311}
312
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
315 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800316 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800318 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 }
buzbee2700f7e2014-03-07 09:46:20 -0800320 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 StoreValueWide(rl_dest, rl_result);
322}
323
324void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700325 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700326 rl_src = LoadValue(rl_src, kCoreReg);
327 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
328 OpKind op = kOpInvalid;
329 switch (opcode) {
330 case Instruction::INT_TO_BYTE:
331 op = kOp2Byte;
332 break;
333 case Instruction::INT_TO_SHORT:
334 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700336 case Instruction::INT_TO_CHAR:
337 op = kOp2Char;
338 break;
339 default:
340 LOG(ERROR) << "Bad int conversion type";
341 }
buzbee2700f7e2014-03-07 09:46:20 -0800342 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700343 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344}
345
Andreas Gampe98430592014-07-27 19:44:50 -0700346/*
347 * Let helper function take care of everything. Will call
348 * Array::AllocFromCode(type_idx, method, count);
349 * Note: AllocFromCode will handle checks for errNegativeArraySize.
350 */
351void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
352 RegLocation rl_src) {
353 FlushAllRegs(); /* Everything to home location */
354 const DexFile* dex_file = cu_->dex_file;
355 CompilerDriver* driver = cu_->compiler_driver;
356 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800357 bool is_type_initialized; // Ignored as an array does not have an initializer.
358 bool use_direct_type_ptr;
359 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700360 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700362 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
363 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800364 // The fast path.
365 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700366 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700367 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
368 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800369 } else {
370 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700371 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
372 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800373 }
374 } else {
375 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700376 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800377 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700379 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 }
Andreas Gampe98430592014-07-27 19:44:50 -0700381 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382}
383
384/*
385 * Similar to GenNewArray, but with post-allocation initialization.
386 * Verifier guarantees we're dealing with an array class. Current
387 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
388 * Current code also throws internal unimp if not 'L', '[' or 'I'.
389 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700390void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 int elems = info->num_arg_words;
392 int type_idx = info->index;
393 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700394 QuickEntrypointEnum target;
395 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
396 type_idx)) {
397 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700399 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
Andreas Gampe98430592014-07-27 19:44:50 -0700401 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700402 FreeTemp(TargetReg(kArg2, kNotWide));
403 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 /*
405 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
406 * return region. Because AllocFromCode placed the new array
407 * in kRet0, we'll just lock it into place. When debugger support is
408 * added, it may be necessary to additionally copy all return
409 * values to a home location in thread-local storage
410 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700411 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700412 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413
414 // TODO: use the correct component size, currently all supported types
415 // share array alignment with ints (see comment at head of function)
416 size_t component_size = sizeof(int32_t);
417
418 // Having a range of 0 is legal
419 if (info->is_range && (elems > 0)) {
420 /*
421 * Bit of ugliness here. We're going generate a mem copy loop
422 * on the register range, but it is possible that some regs
423 * in the range have been promoted. This is unlikely, but
424 * before generating the copy, we'll just force a flush
425 * of any regs in the source range that have been promoted to
426 * home location.
427 */
428 for (int i = 0; i < elems; i++) {
429 RegLocation loc = UpdateLoc(info->args[i]);
430 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100431 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700432 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 }
435 /*
436 * TUNING note: generated code here could be much improved, but
437 * this is an uncommon operation and isn't especially performance
438 * critical.
439 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700440 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700441 RegStorage r_src = AllocTempRef();
442 RegStorage r_dst = AllocTempRef();
443 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800444 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700445 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700447 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700448 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 break;
450 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700451 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700452 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 r_val = AllocTemp();
454 break;
455 case kMips:
456 r_val = AllocTemp();
457 break;
458 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
459 }
460 // Set up source pointer
461 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700462 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700464 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 mirror::Array::DataOffset(component_size).Int32Value());
466 // Set up the loop counter (known to be > 0)
467 LoadConstant(r_idx, elems - 1);
468 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800469 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100471 {
472 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
473 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
474 // NOTE: No dalvik register annotation, local optimizations will be stopped
475 // by the loop boundaries.
476 }
buzbee695d13a2014-04-19 13:32:20 -0700477 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800479 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700480 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700482 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 -mirror::Array::DataOffset(component_size).Int32Value());
484 }
485 } else if (!info->is_range) {
486 // TUNING: interleave
487 for (int i = 0; i < elems; i++) {
488 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700489 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000490 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800492 if (IsTemp(rl_arg.reg)) {
493 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
495 }
496 }
497 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700498 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 }
500}
501
Ian Rogers832336b2014-10-08 15:35:22 -0700502/*
503 * Array data table format:
504 * ushort ident = 0x0300 magic value
505 * ushort width width of each element in the table
506 * uint size number of elements in the table
507 * ubyte data[size*width] table of data values (may contain a single-byte
508 * padding at the end)
509 *
510 * Total size is 4+(width * size + 1)/2 16-bit code units.
511 */
512void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
513 if (kIsDebugBuild) {
514 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
515 const Instruction::ArrayDataPayload* payload =
516 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
517 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
518 }
519 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
520 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
521}
522
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800523//
524// Slow path to ensure a class is initialized for sget/sput.
525//
526class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
527 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100528 // There are up to two branches to the static field slow path, the "unresolved" when the type
529 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
530 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800531 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100532 RegStorage r_base)
533 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
534 second_branch_(unresolved != nullptr ? uninit : nullptr),
535 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800536 }
537
538 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100539 LIR* target = GenerateTargetLabel();
540 if (second_branch_ != nullptr) {
541 second_branch_->target = target;
542 }
Andreas Gampe98430592014-07-27 19:44:50 -0700543 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700545 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800546
547 m2l_->OpUnconditionalBranch(cont_);
548 }
549
550 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100551 // Second branch to the slow path, or null if there's only one branch.
552 LIR* const second_branch_;
553
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800554 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800555 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800556};
557
Fred Shih37f05ef2014-07-16 18:38:08 -0700558void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000559 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
560 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700561 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000562 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800563 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000564 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100566 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700567 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000568 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
569 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800570 if (IsTemp(rl_method.reg)) {
571 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
573 } else {
574 // Medium path, static storage base in a different class which requires checks that the other
575 // class is initialized.
576 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000577 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 // May do runtime call so everything to home locations.
579 FlushAllRegs();
580 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700581 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 LockTemp(r_method);
583 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700584 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800585 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000586 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
587 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000588 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000589 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100591 LIR* unresolved_branch = nullptr;
592 if (!field_info.IsClassInDexCache() &&
593 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
594 // Check if r_base is NULL.
595 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
596 }
597 LIR* uninit_branch = nullptr;
598 if (!field_info.IsClassInitialized() &&
599 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
600 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700601 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800602 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100603 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800604 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000605 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100606 FreeTemp(r_tmp);
607 }
608 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
609 // The slow path is invoked if the r_base is NULL or the class pointed
610 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800611 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800612 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100615 if (uninit_branch != nullptr) {
616 // Ensure load of status and store of value don't re-order.
617 // TODO: Presumably the actual value store is control-dependent on the status load,
618 // and will thus not be reordered in any case, since stores are never speculated.
619 // Does later code "know" that the class is now initialized? If so, we still
620 // need the barrier to guard later static loads.
621 GenMemBarrier(kLoadAny);
622 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 FreeTemp(r_method);
625 }
626 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700627 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
628 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100629 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100631 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700633 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000634 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
635 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100636 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700637 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000638 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700640 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800641 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800643 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 } else {
645 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700646 QuickEntrypointEnum target;
647 switch (size) {
648 case kReference:
649 target = kQuickSetObjStatic;
650 break;
651 case k64:
652 case kDouble:
653 target = kQuickSet64Static;
654 break;
655 case k32:
656 case kSingle:
657 target = kQuickSet32Static;
658 break;
659 case kSignedHalf:
660 case kUnsignedHalf:
661 target = kQuickSet16Static;
662 break;
663 case kSignedByte:
664 case kUnsignedByte:
665 target = kQuickSet8Static;
666 break;
667 case kWord: // Intentional fallthrough.
668 default:
669 LOG(FATAL) << "Can't determine entrypoint for: " << size;
670 target = kQuickSet32Static;
671 }
Andreas Gampe98430592014-07-27 19:44:50 -0700672 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
674}
675
Fred Shih37f05ef2014-07-16 18:38:08 -0700676void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
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());
Fred Shih37f05ef2014-07-16 18:38:08 -0700679
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700680 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000681 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800682 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000683 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 // Fast path, static storage base is this method's class
685 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700686 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000687 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
688 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 } else {
690 // Medium path, static storage base in a different class which requires checks that the other
691 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 // May do runtime call so everything to home locations.
694 FlushAllRegs();
695 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700696 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 LockTemp(r_method);
698 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700699 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800700 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000701 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
702 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000703 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000704 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800705 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100706 LIR* unresolved_branch = nullptr;
707 if (!field_info.IsClassInDexCache() &&
708 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
709 // Check if r_base is NULL.
710 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
711 }
712 LIR* uninit_branch = nullptr;
713 if (!field_info.IsClassInitialized() &&
714 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
715 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700716 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800717 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100718 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800719 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000720 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100721 FreeTemp(r_tmp);
722 }
723 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
724 // The slow path is invoked if the r_base is NULL or the class pointed
725 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800726 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800727 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800729
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100730 if (uninit_branch != nullptr) {
731 // Ensure load of status and load of value don't re-order.
732 GenMemBarrier(kLoadAny);
733 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 FreeTemp(r_method);
736 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800737 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700738 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100739 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800740
Vladimir Marko674744e2014-04-24 15:18:26 +0100741 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700742 if (IsRef(size)) {
743 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000744 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
745 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100746 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700747 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000748 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800749 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100750 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800751
Fred Shih37f05ef2014-07-16 18:38:08 -0700752 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 StoreValueWide(rl_dest, rl_result);
754 } else {
755 StoreValue(rl_dest, rl_result);
756 }
757 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700758 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700760 QuickEntrypointEnum target;
761 switch (type) {
762 case Primitive::kPrimNot:
763 target = kQuickGetObjStatic;
764 break;
765 case Primitive::kPrimLong:
766 case Primitive::kPrimDouble:
767 target = kQuickGet64Static;
768 break;
769 case Primitive::kPrimInt:
770 case Primitive::kPrimFloat:
771 target = kQuickGet32Static;
772 break;
773 case Primitive::kPrimShort:
774 target = kQuickGetShortStatic;
775 break;
776 case Primitive::kPrimChar:
777 target = kQuickGetCharStatic;
778 break;
779 case Primitive::kPrimByte:
780 target = kQuickGetByteStatic;
781 break;
782 case Primitive::kPrimBoolean:
783 target = kQuickGetBooleanStatic;
784 break;
785 case Primitive::kPrimVoid: // Intentional fallthrough.
786 default:
787 LOG(FATAL) << "Can't determine entrypoint for: " << type;
788 target = kQuickGet32Static;
789 }
Andreas Gampe98430592014-07-27 19:44:50 -0700790 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
791
Douglas Leung2db3e262014-06-25 16:02:55 -0700792 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700793 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700794 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 StoreValueWide(rl_dest, rl_result);
796 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700797 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 StoreValue(rl_dest, rl_result);
799 }
800 }
801}
802
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800803// Generate code for all slow paths.
804void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700805 // We should check slow_paths_.Size() every time, because a new slow path
806 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100807 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800808 slowpath->Compile();
809 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100810 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800811}
812
Fred Shih37f05ef2014-07-16 18:38:08 -0700813void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
814 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000815 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
816 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700817 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700818 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700819 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000820 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700821 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100822 GenNullCheck(rl_obj.reg, opt_flags);
823 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
824 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000825 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700826 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000827 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
828 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100829 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700830 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000831 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100832 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000833 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700834 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 StoreValueWide(rl_dest, rl_result);
836 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 StoreValue(rl_dest, rl_result);
838 }
839 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700840 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
841 QuickEntrypointEnum target;
842 switch (type) {
843 case Primitive::kPrimNot:
844 target = kQuickGetObjInstance;
845 break;
846 case Primitive::kPrimLong:
847 case Primitive::kPrimDouble:
848 target = kQuickGet64Instance;
849 break;
850 case Primitive::kPrimFloat:
851 case Primitive::kPrimInt:
852 target = kQuickGet32Instance;
853 break;
854 case Primitive::kPrimShort:
855 target = kQuickGetShortInstance;
856 break;
857 case Primitive::kPrimChar:
858 target = kQuickGetCharInstance;
859 break;
860 case Primitive::kPrimByte:
861 target = kQuickGetByteInstance;
862 break;
863 case Primitive::kPrimBoolean:
864 target = kQuickGetBooleanInstance;
865 break;
866 case Primitive::kPrimVoid: // Intentional fallthrough.
867 default:
868 LOG(FATAL) << "Can't determine entrypoint for: " << type;
869 target = kQuickGet32Instance;
870 }
Andreas Gampe98430592014-07-27 19:44:50 -0700871 // Second argument of pGetXXInstance is always a reference.
872 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
873 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
874
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700875 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700876 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700877 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 StoreValueWide(rl_dest, rl_result);
879 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700880 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 StoreValue(rl_dest, rl_result);
882 }
883 }
884}
885
Vladimir Markobe0e5462014-02-26 11:24:15 +0000886void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700887 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000888 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
889 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700890 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700891 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700892 // Dex code never writes to the class field.
893 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
894 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700895 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700896 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100897 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 } else {
899 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100900 }
901 GenNullCheck(rl_obj.reg, opt_flags);
902 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000903 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700904 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000905 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
906 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100907 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700908 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000909 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100910 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000911 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700912 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100913 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 }
915 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700916 QuickEntrypointEnum target;
917 switch (size) {
918 case kReference:
919 target = kQuickSetObjInstance;
920 break;
921 case k64:
922 case kDouble:
923 target = kQuickSet64Instance;
924 break;
925 case k32:
926 case kSingle:
927 target = kQuickSet32Instance;
928 break;
929 case kSignedHalf:
930 case kUnsignedHalf:
931 target = kQuickSet16Instance;
932 break;
933 case kSignedByte:
934 case kUnsignedByte:
935 target = kQuickSet8Instance;
936 break;
937 case kWord: // Intentional fallthrough.
938 default:
939 LOG(FATAL) << "Can't determine entrypoint for: " << size;
940 target = kQuickSet32Instance;
941 }
Andreas Gampe98430592014-07-27 19:44:50 -0700942 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
943 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 }
945}
946
Ian Rogersa9a82542013-10-04 11:17:26 -0700947void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
948 RegLocation rl_src) {
949 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
950 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
951 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700952 QuickEntrypointEnum target = needs_range_check
953 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
954 : kQuickAputObjectWithBoundCheck)
955 : kQuickAputObject;
956 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700957}
958
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700959void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700961 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700962 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700964 *cu_->dex_file,
965 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 // Call out to helper which resolves type and verifies access.
967 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700968 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700969 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 StoreValue(rl_dest, rl_result);
971 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800972 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 // We're don't need access checks, load type from dex cache
974 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700975 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000976 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000977 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000978 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
980 type_idx) || SLOW_TYPE_PATH) {
981 // Slow path, at runtime test if type is null and if so initialize
982 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800983 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800984 LIR* cont = NewLIR0(kPseudoTargetLabel);
985
986 // Object to generate the slow path for class resolution.
987 class SlowPath : public LIRSlowPath {
988 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800989 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
990 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
991 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
992 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800993 }
994
995 void Compile() {
996 GenerateTargetLabel();
997
Andreas Gampe98430592014-07-27 19:44:50 -0700998 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700999 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001000 m2l_->OpUnconditionalBranch(cont_);
1001 }
1002
1003 private:
1004 const int type_idx_;
1005 const RegLocation rl_method_;
1006 const RegLocation rl_result_;
1007 };
1008
1009 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001010 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001011
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001013 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 // Fast path, we're done - just store result
1015 StoreValue(rl_dest, rl_result);
1016 }
1017 }
1018}
1019
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001020void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001022 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1023 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1025 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1026 // slow path, resolve string if not in dex cache
1027 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001028 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001029
1030 // If the Method* is already in a register, we can save a copy.
1031 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001032 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001033 if (rl_method.location == kLocPhysReg) {
1034 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001035 DCHECK(!IsTemp(rl_method.reg));
1036 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001037 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001038 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001039 LoadCurrMethodDirect(r_method);
1040 }
buzbee695d13a2014-04-19 13:32:20 -07001041 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001042 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001043
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001045 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1046 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001047 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001048
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001049 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001050 // Object to generate the slow path for string resolution.
1051 class SlowPath : public LIRSlowPath {
1052 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001053 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1054 int32_t string_idx_in) :
1055 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1056 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001057 }
1058
1059 void Compile() {
1060 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001061 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001062 m2l_->OpUnconditionalBranch(cont_);
1063 }
1064
1065 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001066 const RegStorage r_method_;
1067 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001068 };
1069
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001070 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001072
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001074 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 } else {
1076 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001077 RegStorage res_reg = AllocTempRef();
1078 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001079 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1080 kNotVolatile);
1081 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 StoreValue(rl_dest, rl_result);
1083 }
1084}
1085
Andreas Gampe98430592014-07-27 19:44:50 -07001086/*
1087 * Let helper function take care of everything. Will
1088 * call Class::NewInstanceFromCode(type_idx, method);
1089 */
1090void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1091 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 // alloc will always check for resolution, do we also need to verify
1093 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001094 const DexFile* dex_file = cu_->dex_file;
1095 CompilerDriver* driver = cu_->compiler_driver;
1096 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001097 bool is_type_initialized;
1098 bool use_direct_type_ptr;
1099 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001100 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001101 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001102 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1103 &direct_type_ptr, &is_finalizable) &&
1104 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001105 // The fast path.
1106 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001107 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001108 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001109 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001110 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001111 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001112 }
1113 } else {
1114 // Use the direct pointer.
1115 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001116 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001117 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001118 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001119 }
1120 }
1121 } else {
1122 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001123 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001124 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001126 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 }
Andreas Gampe98430592014-07-27 19:44:50 -07001128 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129}
1130
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001131void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001133 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134}
1135
1136// For final classes there are no sub-classes to check and so we can answer the instance-of
1137// question with simple comparisons.
1138void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1139 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001140 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001141 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001142
buzbeea0cd2d72014-06-01 09:33:49 -07001143 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001145 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001146 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001148 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
1150 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001151 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152
buzbeea0cd2d72014-06-01 09:33:49 -07001153 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1154 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155
1156 LoadCurrMethodDirect(check_class);
1157 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001158 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1159 kNotVolatile);
1160 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1161 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 } else {
buzbee695d13a2014-04-19 13:32:20 -07001163 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001164 check_class, kNotVolatile);
1165 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1166 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001167 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001168 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
1170
buzbee695d13a2014-04-19 13:32:20 -07001171 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 if (cu_->instruction_set == kThumb2) {
1173 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001174 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001176 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001178 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 }
1180 LIR* target = NewLIR0(kPseudoTargetLabel);
1181 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 FreeTemp(object_class);
1183 FreeTemp(check_class);
1184 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001185 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 FreeTemp(result_reg);
1187 }
1188 StoreValue(rl_dest, rl_result);
1189}
1190
1191void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1192 bool type_known_abstract, bool use_declaring_class,
1193 bool can_assume_type_is_in_dex_cache,
1194 uint32_t type_idx, RegLocation rl_dest,
1195 RegLocation rl_src) {
1196 FlushAllRegs();
1197 // May generate a call - use explicit registers
1198 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001199 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001200 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001201 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001202 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1203 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 if (needs_access_check) {
1205 // Check we have access to type_idx and if not throw IllegalAccessError,
1206 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001207 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001208 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1209 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001211 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001212 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001213 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001215 if (can_assume_type_is_in_dex_cache) {
1216 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001217 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001218 }
1219
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001221 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001222 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001223 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001224 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001226 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1227 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1228
1229 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001230 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001231
1232 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1233 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001234 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1235 RegLocation rl_src_in)
1236 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1237 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001238 }
1239
1240 void Compile() OVERRIDE {
1241 GenerateTargetLabel();
1242
Andreas Gampe98430592014-07-27 19:44:50 -07001243 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001244 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1245 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001246 m2l_->OpUnconditionalBranch(cont_);
1247 }
1248
1249 private:
1250 uint32_t type_idx_;
1251 RegLocation rl_src_;
1252 };
1253
1254 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1255 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 }
1257 }
1258 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001259 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001260 if (!IsSameReg(rl_result.reg, ref_reg)) {
1261 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001262 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001264 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265
1266 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001267 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001269 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1270 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1272 LIR* branchover = NULL;
1273 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001274 // rl_result == ref == class.
1275 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001276 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 } else {
1278 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001279 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001280 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 if (!type_known_abstract) {
1282 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001283 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001284 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001285 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001287 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001289 if (it != nullptr) {
1290 OpEndIT(it);
1291 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 FreeTemp(r_tgt);
1293 } else {
1294 if (!type_known_abstract) {
1295 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001296 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001297 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001299
Serguei Katkov9ee45192014-07-17 14:39:03 +07001300 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001301 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 }
1303 }
1304 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001305 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 /* branch targets here */
1307 LIR* target = NewLIR0(kPseudoTargetLabel);
1308 StoreValue(rl_dest, rl_result);
1309 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001310 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 branchover->target = target;
1312 }
1313}
1314
1315void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1316 bool type_known_final, type_known_abstract, use_declaring_class;
1317 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1318 *cu_->dex_file,
1319 type_idx,
1320 &type_known_final,
1321 &type_known_abstract,
1322 &use_declaring_class);
1323 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1324 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1325
1326 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1327 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1328 } else {
1329 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1330 use_declaring_class, can_assume_type_is_in_dex_cache,
1331 type_idx, rl_dest, rl_src);
1332 }
1333}
1334
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001335void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 bool type_known_final, type_known_abstract, use_declaring_class;
1337 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1338 *cu_->dex_file,
1339 type_idx,
1340 &type_known_final,
1341 &type_known_abstract,
1342 &use_declaring_class);
1343 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1344 // of the exception throw path.
1345 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001346 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 // Verifier type analysis proved this check cast would never cause an exception.
1348 return;
1349 }
1350 FlushAllRegs();
1351 // May generate a call - use explicit registers
1352 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001353 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001354 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001355 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 if (needs_access_check) {
1357 // Check we have access to type_idx and if not throw IllegalAccessError,
1358 // returns Class* in kRet0
1359 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001360 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001361 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001363 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001364 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 } else {
1366 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001367 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001368 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001369 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001370 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1372 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001373 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1374 LIR* cont = NewLIR0(kPseudoTargetLabel);
1375
1376 // Slow path to initialize the type. Executed if the type is NULL.
1377 class SlowPath : public LIRSlowPath {
1378 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001379 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1380 const RegStorage class_reg_in) :
1381 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1382 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001383 }
1384
1385 void Compile() {
1386 GenerateTargetLabel();
1387
1388 // Call out to helper, which will return resolved type in kArg0
1389 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001390 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1391 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001392 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001393 m2l_->OpUnconditionalBranch(cont_);
1394 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001395
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001396 public:
1397 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001398 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001399 };
1400
buzbee2700f7e2014-03-07 09:46:20 -08001401 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 }
1403 }
1404 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001405 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001406
1407 // Slow path for the case where the classes are not equal. In this case we need
1408 // to call a helper function to do the check.
1409 class SlowPath : public LIRSlowPath {
1410 public:
1411 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1412 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1413 }
1414
1415 void Compile() {
1416 GenerateTargetLabel();
1417
1418 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001419 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1420 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001421 }
Andreas Gampe98430592014-07-27 19:44:50 -07001422 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1423 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001424 m2l_->OpUnconditionalBranch(cont_);
1425 }
1426
1427 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001428 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001429 };
1430
1431 if (type_known_abstract) {
1432 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001433 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001434 LIR* cont = NewLIR0(kPseudoTargetLabel);
1435 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1436 } else {
1437 // Harder, more common case. We need to generate a forward branch over the load
1438 // if the target is null. If it's non-null we perform the load and branch to the
1439 // slow path if the classes are not equal.
1440
1441 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001442 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001443 /* load object->klass_ */
1444 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001445 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1446 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001447
Andreas Gampeccc60262014-07-04 18:02:38 -07001448 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001449 LIR* cont = NewLIR0(kPseudoTargetLabel);
1450
1451 // Add the slow path that will not perform load since this is already done.
1452 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1453
1454 // Set the null check to branch to the continuation.
1455 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 }
1457}
1458
1459void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001460 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 RegLocation rl_result;
1462 if (cu_->instruction_set == kThumb2) {
1463 /*
1464 * NOTE: This is the one place in the code in which we might have
1465 * as many as six live temporary registers. There are 5 in the normal
1466 * set for Arm. Until we have spill capabilities, temporarily add
1467 * lr to the temp set. It is safe to do this locally, but note that
1468 * lr is used explicitly elsewhere in the code generator and cannot
1469 * normally be used as a general temp register.
1470 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001471 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1472 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 }
1474 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1475 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1476 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1477 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001478 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1479 RegStorage t_reg = AllocTemp();
1480 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1481 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1482 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 FreeTemp(t_reg);
1484 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001485 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1486 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 }
1488 /*
1489 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1490 * following StoreValueWide might need to allocate a temp register.
1491 * To further work around the lack of a spill capability, explicitly
1492 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1493 * Remove when spill is functional.
1494 */
1495 FreeRegLocTemps(rl_result, rl_src1);
1496 FreeRegLocTemps(rl_result, rl_src2);
1497 StoreValueWide(rl_dest, rl_result);
1498 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001499 Clobber(TargetReg(kLr, kNotWide));
1500 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 }
1502}
1503
Andreas Gampe98430592014-07-27 19:44:50 -07001504void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1505 RegLocation rl_src1, RegLocation rl_shift) {
1506 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 switch (opcode) {
1508 case Instruction::SHL_LONG:
1509 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001510 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 break;
1512 case Instruction::SHR_LONG:
1513 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001514 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 break;
1516 case Instruction::USHR_LONG:
1517 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001518 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 break;
1520 default:
1521 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001522 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 }
Andreas Gampe98430592014-07-27 19:44:50 -07001524 FlushAllRegs(); /* Send everything to home location */
1525 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001526 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 StoreValueWide(rl_dest, rl_result);
1528}
1529
1530
1531void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001532 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001533 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 OpKind op = kOpBkpt;
1535 bool is_div_rem = false;
1536 bool check_zero = false;
1537 bool unary = false;
1538 RegLocation rl_result;
1539 bool shift_op = false;
1540 switch (opcode) {
1541 case Instruction::NEG_INT:
1542 op = kOpNeg;
1543 unary = true;
1544 break;
1545 case Instruction::NOT_INT:
1546 op = kOpMvn;
1547 unary = true;
1548 break;
1549 case Instruction::ADD_INT:
1550 case Instruction::ADD_INT_2ADDR:
1551 op = kOpAdd;
1552 break;
1553 case Instruction::SUB_INT:
1554 case Instruction::SUB_INT_2ADDR:
1555 op = kOpSub;
1556 break;
1557 case Instruction::MUL_INT:
1558 case Instruction::MUL_INT_2ADDR:
1559 op = kOpMul;
1560 break;
1561 case Instruction::DIV_INT:
1562 case Instruction::DIV_INT_2ADDR:
1563 check_zero = true;
1564 op = kOpDiv;
1565 is_div_rem = true;
1566 break;
1567 /* NOTE: returns in kArg1 */
1568 case Instruction::REM_INT:
1569 case Instruction::REM_INT_2ADDR:
1570 check_zero = true;
1571 op = kOpRem;
1572 is_div_rem = true;
1573 break;
1574 case Instruction::AND_INT:
1575 case Instruction::AND_INT_2ADDR:
1576 op = kOpAnd;
1577 break;
1578 case Instruction::OR_INT:
1579 case Instruction::OR_INT_2ADDR:
1580 op = kOpOr;
1581 break;
1582 case Instruction::XOR_INT:
1583 case Instruction::XOR_INT_2ADDR:
1584 op = kOpXor;
1585 break;
1586 case Instruction::SHL_INT:
1587 case Instruction::SHL_INT_2ADDR:
1588 shift_op = true;
1589 op = kOpLsl;
1590 break;
1591 case Instruction::SHR_INT:
1592 case Instruction::SHR_INT_2ADDR:
1593 shift_op = true;
1594 op = kOpAsr;
1595 break;
1596 case Instruction::USHR_INT:
1597 case Instruction::USHR_INT_2ADDR:
1598 shift_op = true;
1599 op = kOpLsr;
1600 break;
1601 default:
1602 LOG(FATAL) << "Invalid word arith op: " << opcode;
1603 }
1604 if (!is_div_rem) {
1605 if (unary) {
1606 rl_src1 = LoadValue(rl_src1, kCoreReg);
1607 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001608 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001610 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001611 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001612 RegStorage t_reg = AllocTemp();
1613 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 rl_src1 = LoadValue(rl_src1, kCoreReg);
1615 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001616 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 FreeTemp(t_reg);
1618 } else {
1619 rl_src1 = LoadValue(rl_src1, kCoreReg);
1620 rl_src2 = LoadValue(rl_src2, kCoreReg);
1621 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001622 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 }
1624 }
1625 StoreValue(rl_dest, rl_result);
1626 } else {
Dave Allison70202782013-10-22 17:52:19 -07001627 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 +01001628 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 rl_src1 = LoadValue(rl_src1, kCoreReg);
1630 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001631 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001632 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 }
buzbee2700f7e2014-03-07 09:46:20 -08001634 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001635 done = true;
1636 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001637 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1638 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001639 // Use ARM SDIV instruction for division. For remainder we also need to
1640 // calculate using a MUL and subtract.
1641 rl_src1 = LoadValue(rl_src1, kCoreReg);
1642 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001643 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001644 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001645 }
buzbee2700f7e2014-03-07 09:46:20 -08001646 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001647 done = true;
1648 }
1649 }
1650
1651 // If we haven't already generated the code use the callout function.
1652 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001654 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001655 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001656 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001657 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001658 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 }
Dave Allison70202782013-10-22 17:52:19 -07001660 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001661 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001663 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 else
1665 rl_result = GetReturnAlt();
1666 }
1667 StoreValue(rl_dest, rl_result);
1668 }
1669}
1670
1671/*
1672 * The following are the first-level codegen routines that analyze the format
1673 * of each bytecode then either dispatch special purpose codegen routines
1674 * or produce corresponding Thumb instructions directly.
1675 */
1676
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001678static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 x &= x - 1;
1680 return (x & (x - 1)) == 0;
1681}
1682
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1684// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001685bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001686 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1688 return false;
1689 }
1690 // No divide instruction for Arm, so check for more special cases
1691 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001692 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 }
1694 int k = LowestSetBit(lit);
1695 if (k >= 30) {
1696 // Avoid special cases.
1697 return false;
1698 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 rl_src = LoadValue(rl_src, kCoreReg);
1700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001701 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001702 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 if (lit == 2) {
1704 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1706 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1707 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001709 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001711 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1712 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 }
1714 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001715 RegStorage t_reg1 = AllocTemp();
1716 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1719 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001721 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001723 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001727 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 }
1729 }
1730 StoreValue(rl_dest, rl_result);
1731 return true;
1732}
1733
1734// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1735// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001736bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001737 if (lit < 0) {
1738 return false;
1739 }
1740 if (lit == 0) {
1741 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1742 LoadConstant(rl_result.reg, 0);
1743 StoreValue(rl_dest, rl_result);
1744 return true;
1745 }
1746 if (lit == 1) {
1747 rl_src = LoadValue(rl_src, kCoreReg);
1748 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1749 OpRegCopy(rl_result.reg, rl_src.reg);
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001753 // There is RegRegRegShift on Arm, so check for more special cases
1754 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001755 return EasyMultiply(rl_src, rl_dest, lit);
1756 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 // Can we simplify this multiplication?
1758 bool power_of_two = false;
1759 bool pop_count_le2 = false;
1760 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001761 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 power_of_two = true;
1763 } else if (IsPopCountLE2(lit)) {
1764 pop_count_le2 = true;
1765 } else if (IsPowerOfTwo(lit + 1)) {
1766 power_of_two_minus_one = true;
1767 } else {
1768 return false;
1769 }
1770 rl_src = LoadValue(rl_src, kCoreReg);
1771 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1772 if (power_of_two) {
1773 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001774 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001775 } else if (pop_count_le2) {
1776 // Shift and add and shift.
1777 int first_bit = LowestSetBit(lit);
1778 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1779 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1780 } else {
1781 // Reverse subtract: (src << (shift + 1)) - src.
1782 DCHECK(power_of_two_minus_one);
1783 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001784 RegStorage t_reg = AllocTemp();
1785 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1786 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 }
1788 StoreValue(rl_dest, rl_result);
1789 return true;
1790}
1791
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001792// Returns true if it generates instructions.
1793bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1794 RegLocation rl_src2) {
1795 if (!rl_src2.is_const ||
1796 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1797 return false;
1798 }
1799
1800 if (!rl_src2.wide) {
1801 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1802 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1803 // Generate multiply by reciprocal instead of div.
1804 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1805 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1806 return true;
1807 }
1808 } else {
1809 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1810 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1811 // Generate multiply by reciprocal instead of div.
1812 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1813 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1814 return true;
1815 }
1816 }
1817 return false;
1818}
1819
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820void 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;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001845 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 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) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001935 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1936 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001937 // Use ARM SDIV instruction for division. For remainder we also need to
1938 // calculate using a MUL and subtract.
1939 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001940 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001941 done = true;
1942 }
1943 }
1944
1945 if (!done) {
1946 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001947 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1948 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001949 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001951 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 else
1953 rl_result = GetReturnAlt();
1954 }
1955 StoreValue(rl_dest, rl_result);
1956 return;
1957 }
1958 default:
1959 LOG(FATAL) << "Unexpected opcode " << opcode;
1960 }
1961 rl_src = LoadValue(rl_src, kCoreReg);
1962 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001963 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001965 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001967 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 }
1969 StoreValue(rl_dest, rl_result);
1970}
1971
Andreas Gampe98430592014-07-27 19:44:50 -07001972void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001973 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 RegLocation rl_result;
1975 OpKind first_op = kOpBkpt;
1976 OpKind second_op = kOpBkpt;
1977 bool call_out = false;
1978 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001979 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1980 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981
1982 switch (opcode) {
1983 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001984 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1985 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001987 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001988 RegStorage t_reg = AllocTemp();
1989 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1990 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1991 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1992 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001994 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1995 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 }
Andreas Gampe98430592014-07-27 19:44:50 -07001997 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 return;
1999 case Instruction::ADD_LONG:
2000 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 first_op = kOpAdd;
2002 second_op = kOpAdc;
2003 break;
2004 case Instruction::SUB_LONG:
2005 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 first_op = kOpSub;
2007 second_op = kOpSbc;
2008 break;
2009 case Instruction::MUL_LONG:
2010 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002011 call_out = true;
2012 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2013 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 break;
2015 case Instruction::DIV_LONG:
2016 case Instruction::DIV_LONG_2ADDR:
2017 call_out = true;
2018 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002019 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2020 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002021 break;
2022 case Instruction::REM_LONG:
2023 case Instruction::REM_LONG_2ADDR:
2024 call_out = true;
2025 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002026 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002028 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2029 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 break;
2031 case Instruction::AND_LONG_2ADDR:
2032 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 first_op = kOpAnd;
2034 second_op = kOpAnd;
2035 break;
2036 case Instruction::OR_LONG:
2037 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 first_op = kOpOr;
2039 second_op = kOpOr;
2040 break;
2041 case Instruction::XOR_LONG:
2042 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 first_op = kOpXor;
2044 second_op = kOpXor;
2045 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 default:
2047 LOG(FATAL) << "Invalid long arith op";
2048 }
2049 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002050 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002052 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002054 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2055 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2056 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2057 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002058 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2059 GenDivZeroCheckWide(r_tmp2);
2060 }
Andreas Gampe98430592014-07-27 19:44:50 -07002061 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002063 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002065 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066 }
2067 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002068 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2069 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 else
Andreas Gampe98430592014-07-27 19:44:50 -07002071 rl_result = GetReturnWideAlt();
2072 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002073 }
2074}
2075
Mark Mendelle87f9b52014-04-30 14:13:18 -04002076void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2077 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2078 LoadConstantNoClobber(rl_result.reg, value);
2079 StoreValue(rl_dest, rl_result);
2080 if (value == 0) {
2081 Workaround7250540(rl_dest, rl_result.reg);
2082 }
2083}
2084
Andreas Gampe98430592014-07-27 19:44:50 -07002085void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2086 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002087 /*
2088 * Don't optimize the register usage since it calls out to support
2089 * functions
2090 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002091
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002093 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002094 if (rl_dest.wide) {
2095 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002096 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 StoreValueWide(rl_dest, rl_result);
2098 } else {
2099 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002100 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 StoreValue(rl_dest, rl_result);
2102 }
2103}
2104
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002105class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2106 public:
2107 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2108 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2109 }
2110
2111 void Compile() OVERRIDE {
2112 m2l_->ResetRegPool();
2113 m2l_->ResetDefTracking();
2114 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002115 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002116 if (cont_ != nullptr) {
2117 m2l_->OpUnconditionalBranch(cont_);
2118 }
2119 }
2120};
2121
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002123void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002124 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002125 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2126 return;
2127 }
2128 FlushAllRegs();
2129 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002130 LIR* cont = NewLIR0(kPseudoTargetLabel);
2131 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002132 } else {
2133 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2134 return;
2135 }
2136 FlushAllRegs(); // TODO: needed?
2137 LIR* inst = CheckSuspendUsingLoad();
2138 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002140}
2141
2142/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002143void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002144 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002145 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2146 OpUnconditionalBranch(target);
2147 return;
2148 }
2149 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002150 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002151 LIR* branch = OpUnconditionalBranch(nullptr);
2152 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002153 } else {
2154 // For the implicit suspend check, just perform the trigger
2155 // load and branch to the target.
2156 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2157 OpUnconditionalBranch(target);
2158 return;
2159 }
2160 FlushAllRegs();
2161 LIR* inst = CheckSuspendUsingLoad();
2162 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002163 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002164 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002165}
2166
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002167/* Call out to helper assembly routine that will null check obj and then lock it. */
2168void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002169 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002170 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002171 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002172}
2173
2174/* Call out to helper assembly routine that will null check obj and then unlock it. */
2175void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002176 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002177 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002178 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002179}
2180
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002181/* Generic code for generating a wide constant into a VR. */
2182void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2183 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002184 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002185 StoreValueWide(rl_dest, rl_result);
2186}
2187
Andreas Gampe48971b32014-08-06 10:09:01 -07002188void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002189 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002190 const uint16_t entries = table[1];
2191 // Chained cmp-and-branch.
2192 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002193 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002194 const int32_t* targets = &as_int32[1];
2195 rl_src = LoadValue(rl_src, kCoreReg);
2196 int i = 0;
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002197 for (; i < entries; i++) {
2198 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002199 // Switch to using a temp and add.
2200 break;
2201 }
2202 BasicBlock* case_block =
2203 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002204 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002205 }
2206 if (i < entries) {
2207 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2208 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2209 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002210 LoadConstantNoClobber(key_temp, starting_key + i);
2211 for (; i < entries - 1; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002212 BasicBlock* case_block =
2213 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2214 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2215 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2216 }
2217 BasicBlock* case_block =
2218 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2219 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2220 } else {
2221 // No free temp, just finish the old loop.
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002222 for (; i < entries; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002223 BasicBlock* case_block =
2224 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002225 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002226 }
2227 }
2228 }
2229}
2230
2231void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002232 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002233 if (cu_->verbose) {
2234 DumpSparseSwitchTable(table);
2235 }
2236
2237 const uint16_t entries = table[1];
2238 if (entries <= kSmallSwitchThreshold) {
2239 GenSmallPackedSwitch(mir, table_offset, rl_src);
2240 } else {
2241 // Use the backend-specific implementation.
2242 GenLargePackedSwitch(mir, table_offset, rl_src);
2243 }
2244}
2245
2246void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002247 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002248 const uint16_t entries = table[1];
2249 // Chained cmp-and-branch.
2250 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2251 const int32_t* targets = &keys[entries];
2252 rl_src = LoadValue(rl_src, kCoreReg);
2253 for (int i = 0; i < entries; i++) {
2254 int key = keys[i];
2255 BasicBlock* case_block =
2256 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2257 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2258 }
2259}
2260
2261void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002262 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002263 if (cu_->verbose) {
2264 DumpSparseSwitchTable(table);
2265 }
2266
2267 const uint16_t entries = table[1];
2268 if (entries <= kSmallSwitchThreshold) {
2269 GenSmallSparseSwitch(mir, table_offset, rl_src);
2270 } else {
2271 // Use the backend-specific implementation.
2272 GenLargeSparseSwitch(mir, table_offset, rl_src);
2273 }
2274}
2275
Fred Shih37f05ef2014-07-16 18:38:08 -07002276bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2277 switch (size) {
2278 case kReference:
2279 return type == Primitive::kPrimNot;
2280 case k64:
2281 case kDouble:
2282 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2283 case k32:
2284 case kSingle:
2285 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2286 case kSignedHalf:
2287 return type == Primitive::kPrimShort;
2288 case kUnsignedHalf:
2289 return type == Primitive::kPrimChar;
2290 case kSignedByte:
2291 return type == Primitive::kPrimByte;
2292 case kUnsignedByte:
2293 return type == Primitive::kPrimBoolean;
2294 case kWord: // Intentional fallthrough.
2295 default:
2296 return false; // There are no sane types with this op size.
2297 }
2298}
2299
Brian Carlstrom7940e442013-07-12 13:46:57 -07002300} // namespace art