blob: 9410f7e83b4398979b2d94b244669a96399c3fb2 [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:
69 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
70 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
71 }
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:
87 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
88 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
89 index_(index), length_(length) {
90 }
91
92 void Compile() OVERRIDE {
93 m2l_->ResetRegPool();
94 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070095 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070096 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -070097 }
98
99 private:
100 const RegStorage index_;
101 const RegStorage length_;
102 };
103
104 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
105 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
106}
107
108void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
109 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
110 public:
111 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
112 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
113 index_(index), length_(length) {
114 }
115
116 void Compile() OVERRIDE {
117 m2l_->ResetRegPool();
118 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700119 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700120
Andreas Gampeccc60262014-07-04 18:02:38 -0700121 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
122 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700123
124 m2l_->OpRegCopy(arg1_32, length_);
125 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700126 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700127 }
128
129 private:
130 const int32_t index_;
131 const RegStorage length_;
132 };
133
134 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
135 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
136}
137
Mingyao Yange643a172014-04-08 11:02:52 -0700138LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
139 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
140 public:
141 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
142 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
143 }
144
145 void Compile() OVERRIDE {
146 m2l_->ResetRegPool();
147 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700148 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700149 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700150 }
151 };
152
153 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
154 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
155 return branch;
156}
157
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800159LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000160 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700161 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 }
Dave Allisonb373e092014-02-20 16:06:36 -0800163 return nullptr;
164}
165
Dave Allisonf9439142014-03-27 15:10:22 -0700166/* Perform an explicit null-check on a register. */
167LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
168 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
169 return NULL;
170 }
Mingyao Yange643a172014-04-08 11:02:52 -0700171 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700172}
173
Dave Allisonb373e092014-02-20 16:06:36 -0800174void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000175 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800176 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
177 return;
178 }
Dave Allison69dfe512014-07-11 17:11:58 +0000179 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800180 MarkSafepointPC(last_lir_insn_);
181 }
182}
183
Andreas Gampe3c12c512014-06-24 18:46:29 +0000184void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000185 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000186 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
187 return;
188 }
189 MarkSafepointPCAfter(after);
190 }
191}
192
Dave Allisonb373e092014-02-20 16:06:36 -0800193void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000194 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800195 MarkSafepointPC(last_lir_insn_);
196 }
197}
198
buzbee2700f7e2014-03-07 09:46:20 -0800199void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000200 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800201 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
202 return;
203 }
204 // Force an implicit null check by performing a memory operation (load) from the given
205 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800206 RegStorage tmp = AllocTemp();
207 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700208 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800209 FreeTemp(tmp);
210 MarkSafepointPC(load);
211 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212}
213
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700215 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700217 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 switch (opcode) {
219 case Instruction::IF_EQ:
220 cond = kCondEq;
221 break;
222 case Instruction::IF_NE:
223 cond = kCondNe;
224 break;
225 case Instruction::IF_LT:
226 cond = kCondLt;
227 break;
228 case Instruction::IF_GE:
229 cond = kCondGe;
230 break;
231 case Instruction::IF_GT:
232 cond = kCondGt;
233 break;
234 case Instruction::IF_LE:
235 cond = kCondLe;
236 break;
237 default:
238 cond = static_cast<ConditionCode>(0);
239 LOG(FATAL) << "Unexpected opcode " << opcode;
240 }
241
242 // Normalize such that if either operand is constant, src2 will be constant
243 if (rl_src1.is_const) {
244 RegLocation rl_temp = rl_src1;
245 rl_src1 = rl_src2;
246 rl_src2 = rl_temp;
247 cond = FlipComparisonOrder(cond);
248 }
249
buzbee7c02e912014-10-03 13:14:17 -0700250 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 // Is this really an immediate comparison?
252 if (rl_src2.is_const) {
253 // If it's already live in a register or not easily materialized, just keep going
254 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700255 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100257 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800259 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 return;
261 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700262
263 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
264 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
265 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
266 // the 32b literal 0 for null.
267 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
268 // Use the OpCmpImmBranch and ignore the value in the register.
269 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
270 return;
271 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700273
buzbee7c02e912014-10-03 13:14:17 -0700274 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800275 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276}
277
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700278void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700280 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
281 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 switch (opcode) {
283 case Instruction::IF_EQZ:
284 cond = kCondEq;
285 break;
286 case Instruction::IF_NEZ:
287 cond = kCondNe;
288 break;
289 case Instruction::IF_LTZ:
290 cond = kCondLt;
291 break;
292 case Instruction::IF_GEZ:
293 cond = kCondGe;
294 break;
295 case Instruction::IF_GTZ:
296 cond = kCondGt;
297 break;
298 case Instruction::IF_LEZ:
299 cond = kCondLe;
300 break;
301 default:
302 cond = static_cast<ConditionCode>(0);
303 LOG(FATAL) << "Unexpected opcode " << opcode;
304 }
buzbee2700f7e2014-03-07 09:46:20 -0800305 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306}
307
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700308void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
310 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800311 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800313 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 }
buzbee2700f7e2014-03-07 09:46:20 -0800315 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 StoreValueWide(rl_dest, rl_result);
317}
318
319void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700320 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700321 rl_src = LoadValue(rl_src, kCoreReg);
322 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
323 OpKind op = kOpInvalid;
324 switch (opcode) {
325 case Instruction::INT_TO_BYTE:
326 op = kOp2Byte;
327 break;
328 case Instruction::INT_TO_SHORT:
329 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700331 case Instruction::INT_TO_CHAR:
332 op = kOp2Char;
333 break;
334 default:
335 LOG(ERROR) << "Bad int conversion type";
336 }
buzbee2700f7e2014-03-07 09:46:20 -0800337 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700338 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339}
340
Andreas Gampe98430592014-07-27 19:44:50 -0700341/*
342 * Let helper function take care of everything. Will call
343 * Array::AllocFromCode(type_idx, method, count);
344 * Note: AllocFromCode will handle checks for errNegativeArraySize.
345 */
346void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
347 RegLocation rl_src) {
348 FlushAllRegs(); /* Everything to home location */
349 const DexFile* dex_file = cu_->dex_file;
350 CompilerDriver* driver = cu_->compiler_driver;
351 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800352 bool is_type_initialized; // Ignored as an array does not have an initializer.
353 bool use_direct_type_ptr;
354 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700355 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800356 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700357 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
358 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800359 // The fast path.
360 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700361 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700362 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
363 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800364 } else {
365 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700366 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
367 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800368 }
369 } else {
370 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700371 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800372 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700374 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 }
Andreas Gampe98430592014-07-27 19:44:50 -0700376 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377}
378
379/*
380 * Similar to GenNewArray, but with post-allocation initialization.
381 * Verifier guarantees we're dealing with an array class. Current
382 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
383 * Current code also throws internal unimp if not 'L', '[' or 'I'.
384 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700385void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 int elems = info->num_arg_words;
387 int type_idx = info->index;
388 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700389 QuickEntrypointEnum target;
390 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
391 type_idx)) {
392 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700394 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 }
Andreas Gampe98430592014-07-27 19:44:50 -0700396 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700397 FreeTemp(TargetReg(kArg2, kNotWide));
398 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 /*
400 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
401 * return region. Because AllocFromCode placed the new array
402 * in kRet0, we'll just lock it into place. When debugger support is
403 * added, it may be necessary to additionally copy all return
404 * values to a home location in thread-local storage
405 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700406 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700407 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408
409 // TODO: use the correct component size, currently all supported types
410 // share array alignment with ints (see comment at head of function)
411 size_t component_size = sizeof(int32_t);
412
413 // Having a range of 0 is legal
414 if (info->is_range && (elems > 0)) {
415 /*
416 * Bit of ugliness here. We're going generate a mem copy loop
417 * on the register range, but it is possible that some regs
418 * in the range have been promoted. This is unlikely, but
419 * before generating the copy, we'll just force a flush
420 * of any regs in the source range that have been promoted to
421 * home location.
422 */
423 for (int i = 0; i < elems; i++) {
424 RegLocation loc = UpdateLoc(info->args[i]);
425 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100426 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700427 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 }
429 }
430 /*
431 * TUNING note: generated code here could be much improved, but
432 * this is an uncommon operation and isn't especially performance
433 * critical.
434 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700435 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700436 RegStorage r_src = AllocTempRef();
437 RegStorage r_dst = AllocTempRef();
438 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800439 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700440 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700442 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700443 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 break;
445 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700446 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700447 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 r_val = AllocTemp();
449 break;
450 case kMips:
451 r_val = AllocTemp();
452 break;
453 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
454 }
455 // Set up source pointer
456 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700457 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700459 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 mirror::Array::DataOffset(component_size).Int32Value());
461 // Set up the loop counter (known to be > 0)
462 LoadConstant(r_idx, elems - 1);
463 // Generate the copy loop. Going backwards for convenience
464 LIR* target = NewLIR0(kPseudoTargetLabel);
465 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100466 {
467 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
468 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
469 // NOTE: No dalvik register annotation, local optimizations will be stopped
470 // by the loop boundaries.
471 }
buzbee695d13a2014-04-19 13:32:20 -0700472 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 FreeTemp(r_val);
474 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700475 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700477 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 -mirror::Array::DataOffset(component_size).Int32Value());
479 }
480 } else if (!info->is_range) {
481 // TUNING: interleave
482 for (int i = 0; i < elems; i++) {
483 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700484 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000485 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800487 if (IsTemp(rl_arg.reg)) {
488 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 }
490 }
491 }
492 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700493 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
495}
496
Ian Rogers832336b2014-10-08 15:35:22 -0700497/*
498 * Array data table format:
499 * ushort ident = 0x0300 magic value
500 * ushort width width of each element in the table
501 * uint size number of elements in the table
502 * ubyte data[size*width] table of data values (may contain a single-byte
503 * padding at the end)
504 *
505 * Total size is 4+(width * size + 1)/2 16-bit code units.
506 */
507void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
508 if (kIsDebugBuild) {
509 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
510 const Instruction::ArrayDataPayload* payload =
511 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
512 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
513 }
514 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
515 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
516}
517
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800518//
519// Slow path to ensure a class is initialized for sget/sput.
520//
521class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
522 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100523 // There are up to two branches to the static field slow path, the "unresolved" when the type
524 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
525 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800526 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100527 RegStorage r_base)
528 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
529 second_branch_(unresolved != nullptr ? uninit : nullptr),
530 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800531 }
532
533 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100534 LIR* target = GenerateTargetLabel();
535 if (second_branch_ != nullptr) {
536 second_branch_->target = target;
537 }
Andreas Gampe98430592014-07-27 19:44:50 -0700538 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800539 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700540 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800541
542 m2l_->OpUnconditionalBranch(cont_);
543 }
544
545 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100546 // Second branch to the slow path, or null if there's only one branch.
547 LIR* const second_branch_;
548
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800549 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800550 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800551};
552
Fred Shih37f05ef2014-07-16 18:38:08 -0700553void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
555 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700556 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000557 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800558 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000559 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100561 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700562 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000563 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
564 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800565 if (IsTemp(rl_method.reg)) {
566 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 }
568 } else {
569 // Medium path, static storage base in a different class which requires checks that the other
570 // class is initialized.
571 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000572 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 // May do runtime call so everything to home locations.
574 FlushAllRegs();
575 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700576 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 LockTemp(r_method);
578 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700579 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800580 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000581 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
582 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000583 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000584 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800585 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100586 LIR* unresolved_branch = nullptr;
587 if (!field_info.IsClassInDexCache() &&
588 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
589 // Check if r_base is NULL.
590 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
591 }
592 LIR* uninit_branch = nullptr;
593 if (!field_info.IsClassInitialized() &&
594 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
595 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700596 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800597 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100598 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800599 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000600 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100601 FreeTemp(r_tmp);
602 }
603 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
604 // The slow path is invoked if the r_base is NULL or the class pointed
605 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800606 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800607 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000608 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100610 if (uninit_branch != nullptr) {
611 // Ensure load of status and store of value don't re-order.
612 // TODO: Presumably the actual value store is control-dependent on the status load,
613 // and will thus not be reordered in any case, since stores are never speculated.
614 // Does later code "know" that the class is now initialized? If so, we still
615 // need the barrier to guard later static loads.
616 GenMemBarrier(kLoadAny);
617 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 FreeTemp(r_method);
620 }
621 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700622 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
623 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100624 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100626 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700628 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000629 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
630 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100631 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700632 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000633 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700635 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800636 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800638 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 } else {
640 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700641 QuickEntrypointEnum target;
642 switch (size) {
643 case kReference:
644 target = kQuickSetObjStatic;
645 break;
646 case k64:
647 case kDouble:
648 target = kQuickSet64Static;
649 break;
650 case k32:
651 case kSingle:
652 target = kQuickSet32Static;
653 break;
654 case kSignedHalf:
655 case kUnsignedHalf:
656 target = kQuickSet16Static;
657 break;
658 case kSignedByte:
659 case kUnsignedByte:
660 target = kQuickSet8Static;
661 break;
662 case kWord: // Intentional fallthrough.
663 default:
664 LOG(FATAL) << "Can't determine entrypoint for: " << size;
665 target = kQuickSet32Static;
666 }
Andreas Gampe98430592014-07-27 19:44:50 -0700667 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
669}
670
Fred Shih37f05ef2014-07-16 18:38:08 -0700671void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000672 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
673 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700674
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700675 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000676 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800677 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000678 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 // Fast path, static storage base is this method's class
680 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700681 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000682 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
683 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 } else {
685 // Medium path, static storage base in a different class which requires checks that the other
686 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000687 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 // May do runtime call so everything to home locations.
689 FlushAllRegs();
690 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700691 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 LockTemp(r_method);
693 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700694 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800695 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000696 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
697 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000698 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000699 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800700 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100701 LIR* unresolved_branch = nullptr;
702 if (!field_info.IsClassInDexCache() &&
703 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
704 // Check if r_base is NULL.
705 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
706 }
707 LIR* uninit_branch = nullptr;
708 if (!field_info.IsClassInitialized() &&
709 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
710 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700711 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800712 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100713 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800714 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000715 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100716 FreeTemp(r_tmp);
717 }
718 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
719 // The slow path is invoked if the r_base is NULL or the class pointed
720 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800721 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800722 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000723 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800724
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100725 if (uninit_branch != nullptr) {
726 // Ensure load of status and load of value don't re-order.
727 GenMemBarrier(kLoadAny);
728 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 FreeTemp(r_method);
731 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800732 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700733 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100734 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800735
Vladimir Marko674744e2014-04-24 15:18:26 +0100736 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700737 if (IsRef(size)) {
738 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000739 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
740 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100741 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700742 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000743 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800744 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100745 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800746
Fred Shih37f05ef2014-07-16 18:38:08 -0700747 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 StoreValueWide(rl_dest, rl_result);
749 } else {
750 StoreValue(rl_dest, rl_result);
751 }
752 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700753 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700755 QuickEntrypointEnum target;
756 switch (type) {
757 case Primitive::kPrimNot:
758 target = kQuickGetObjStatic;
759 break;
760 case Primitive::kPrimLong:
761 case Primitive::kPrimDouble:
762 target = kQuickGet64Static;
763 break;
764 case Primitive::kPrimInt:
765 case Primitive::kPrimFloat:
766 target = kQuickGet32Static;
767 break;
768 case Primitive::kPrimShort:
769 target = kQuickGetShortStatic;
770 break;
771 case Primitive::kPrimChar:
772 target = kQuickGetCharStatic;
773 break;
774 case Primitive::kPrimByte:
775 target = kQuickGetByteStatic;
776 break;
777 case Primitive::kPrimBoolean:
778 target = kQuickGetBooleanStatic;
779 break;
780 case Primitive::kPrimVoid: // Intentional fallthrough.
781 default:
782 LOG(FATAL) << "Can't determine entrypoint for: " << type;
783 target = kQuickGet32Static;
784 }
Andreas Gampe98430592014-07-27 19:44:50 -0700785 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
786
Douglas Leung2db3e262014-06-25 16:02:55 -0700787 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700788 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700789 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 StoreValueWide(rl_dest, rl_result);
791 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700792 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 StoreValue(rl_dest, rl_result);
794 }
795 }
796}
797
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800798// Generate code for all slow paths.
799void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700800 // We should check slow_paths_.Size() every time, because a new slow path
801 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100802 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800803 slowpath->Compile();
804 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100805 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800806}
807
Fred Shih37f05ef2014-07-16 18:38:08 -0700808void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
809 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000810 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
811 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700812 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700813 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700814 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000815 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700816 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100817 GenNullCheck(rl_obj.reg, opt_flags);
818 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
819 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000820 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700821 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000822 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
823 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100824 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700825 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000826 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100827 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000828 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700829 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 StoreValueWide(rl_dest, rl_result);
831 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValue(rl_dest, rl_result);
833 }
834 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700835 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
836 QuickEntrypointEnum target;
837 switch (type) {
838 case Primitive::kPrimNot:
839 target = kQuickGetObjInstance;
840 break;
841 case Primitive::kPrimLong:
842 case Primitive::kPrimDouble:
843 target = kQuickGet64Instance;
844 break;
845 case Primitive::kPrimFloat:
846 case Primitive::kPrimInt:
847 target = kQuickGet32Instance;
848 break;
849 case Primitive::kPrimShort:
850 target = kQuickGetShortInstance;
851 break;
852 case Primitive::kPrimChar:
853 target = kQuickGetCharInstance;
854 break;
855 case Primitive::kPrimByte:
856 target = kQuickGetByteInstance;
857 break;
858 case Primitive::kPrimBoolean:
859 target = kQuickGetBooleanInstance;
860 break;
861 case Primitive::kPrimVoid: // Intentional fallthrough.
862 default:
863 LOG(FATAL) << "Can't determine entrypoint for: " << type;
864 target = kQuickGet32Instance;
865 }
Andreas Gampe98430592014-07-27 19:44:50 -0700866 // Second argument of pGetXXInstance is always a reference.
867 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
868 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
869
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700870 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700871 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700872 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 StoreValueWide(rl_dest, rl_result);
874 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700875 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 StoreValue(rl_dest, rl_result);
877 }
878 }
879}
880
Vladimir Markobe0e5462014-02-26 11:24:15 +0000881void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700882 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000883 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
884 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700885 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700886 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700887 // Dex code never writes to the class field.
888 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
889 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700890 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700891 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100892 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 } else {
894 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100895 }
896 GenNullCheck(rl_obj.reg, opt_flags);
897 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000898 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700899 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000900 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
901 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100902 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700903 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000904 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100905 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000906 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700907 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100908 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 }
910 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700911 QuickEntrypointEnum target;
912 switch (size) {
913 case kReference:
914 target = kQuickSetObjInstance;
915 break;
916 case k64:
917 case kDouble:
918 target = kQuickSet64Instance;
919 break;
920 case k32:
921 case kSingle:
922 target = kQuickSet32Instance;
923 break;
924 case kSignedHalf:
925 case kUnsignedHalf:
926 target = kQuickSet16Instance;
927 break;
928 case kSignedByte:
929 case kUnsignedByte:
930 target = kQuickSet8Instance;
931 break;
932 case kWord: // Intentional fallthrough.
933 default:
934 LOG(FATAL) << "Can't determine entrypoint for: " << size;
935 target = kQuickSet32Instance;
936 }
Andreas Gampe98430592014-07-27 19:44:50 -0700937 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
938 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 }
940}
941
Ian Rogersa9a82542013-10-04 11:17:26 -0700942void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
943 RegLocation rl_src) {
944 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
945 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
946 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700947 QuickEntrypointEnum target = needs_range_check
948 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
949 : kQuickAputObjectWithBoundCheck)
950 : kQuickAputObject;
951 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700952}
953
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700954void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700956 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700957 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700958 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700960 *cu_->dex_file,
961 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 // Call out to helper which resolves type and verifies access.
963 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700964 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700965 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 StoreValue(rl_dest, rl_result);
967 } else {
968 // We're don't need access checks, load type from dex cache
969 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700970 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000971 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000972 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000973 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
975 type_idx) || SLOW_TYPE_PATH) {
976 // Slow path, at runtime test if type is null and if so initialize
977 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800978 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800979 LIR* cont = NewLIR0(kPseudoTargetLabel);
980
981 // Object to generate the slow path for class resolution.
982 class SlowPath : public LIRSlowPath {
983 public:
984 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
985 const RegLocation& rl_method, const RegLocation& rl_result) :
986 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
987 rl_method_(rl_method), rl_result_(rl_result) {
988 }
989
990 void Compile() {
991 GenerateTargetLabel();
992
Andreas Gampe98430592014-07-27 19:44:50 -0700993 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700994 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800995 m2l_->OpUnconditionalBranch(cont_);
996 }
997
998 private:
999 const int type_idx_;
1000 const RegLocation rl_method_;
1001 const RegLocation rl_result_;
1002 };
1003
1004 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001005 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001006
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001008 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 // Fast path, we're done - just store result
1010 StoreValue(rl_dest, rl_result);
1011 }
1012 }
1013}
1014
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001015void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001017 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1018 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1020 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1021 // slow path, resolve string if not in dex cache
1022 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001023 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001024
1025 // If the Method* is already in a register, we can save a copy.
1026 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001027 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001028 if (rl_method.location == kLocPhysReg) {
1029 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001030 DCHECK(!IsTemp(rl_method.reg));
1031 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001032 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001033 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001034 LoadCurrMethodDirect(r_method);
1035 }
buzbee695d13a2014-04-19 13:32:20 -07001036 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001037 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001038
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001040 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1041 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001042 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001043
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001044 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001045 // Object to generate the slow path for string resolution.
1046 class SlowPath : public LIRSlowPath {
1047 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001048 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1049 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1050 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001051 }
1052
1053 void Compile() {
1054 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001055 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001056 m2l_->OpUnconditionalBranch(cont_);
1057 }
1058
1059 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001060 const RegStorage r_method_;
1061 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001062 };
1063
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001064 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001066
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001068 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069 } else {
1070 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001071 RegStorage res_reg = AllocTempRef();
1072 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001073 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1074 kNotVolatile);
1075 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 StoreValue(rl_dest, rl_result);
1077 }
1078}
1079
Andreas Gampe98430592014-07-27 19:44:50 -07001080/*
1081 * Let helper function take care of everything. Will
1082 * call Class::NewInstanceFromCode(type_idx, method);
1083 */
1084void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1085 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 // alloc will always check for resolution, do we also need to verify
1087 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001088 const DexFile* dex_file = cu_->dex_file;
1089 CompilerDriver* driver = cu_->compiler_driver;
1090 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 bool is_type_initialized;
1092 bool use_direct_type_ptr;
1093 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001094 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001095 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001096 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1097 &direct_type_ptr, &is_finalizable) &&
1098 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001099 // The fast path.
1100 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001101 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001102 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001103 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001104 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001105 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001106 }
1107 } else {
1108 // Use the direct pointer.
1109 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001110 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001111 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001112 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001113 }
1114 }
1115 } else {
1116 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001117 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001118 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001120 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 }
Andreas Gampe98430592014-07-27 19:44:50 -07001122 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123}
1124
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001125void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001127 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128}
1129
1130// For final classes there are no sub-classes to check and so we can answer the instance-of
1131// question with simple comparisons.
1132void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1133 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001134 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001135 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001136
buzbeea0cd2d72014-06-01 09:33:49 -07001137 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001139 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001140 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001142 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 }
1144 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001145 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146
buzbeea0cd2d72014-06-01 09:33:49 -07001147 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1148 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149
1150 LoadCurrMethodDirect(check_class);
1151 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001152 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1153 kNotVolatile);
1154 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1155 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 } else {
buzbee695d13a2014-04-19 13:32:20 -07001157 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001158 check_class, kNotVolatile);
1159 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1160 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001161 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001162 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 }
1164
buzbee695d13a2014-04-19 13:32:20 -07001165 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 if (cu_->instruction_set == kThumb2) {
1167 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001168 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001170 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001172 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 }
1174 LIR* target = NewLIR0(kPseudoTargetLabel);
1175 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 FreeTemp(object_class);
1177 FreeTemp(check_class);
1178 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001179 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 FreeTemp(result_reg);
1181 }
1182 StoreValue(rl_dest, rl_result);
1183}
1184
1185void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1186 bool type_known_abstract, bool use_declaring_class,
1187 bool can_assume_type_is_in_dex_cache,
1188 uint32_t type_idx, RegLocation rl_dest,
1189 RegLocation rl_src) {
1190 FlushAllRegs();
1191 // May generate a call - use explicit registers
1192 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001193 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001194 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001195 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001196 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1197 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (needs_access_check) {
1199 // Check we have access to type_idx and if not throw IllegalAccessError,
1200 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001201 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001202 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1203 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001205 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001206 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001207 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001209 if (can_assume_type_is_in_dex_cache) {
1210 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001211 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001212 }
1213
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001215 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001216 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001217 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001218 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001220 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1221 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1222
1223 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001224 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001225
1226 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1227 public:
1228 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1229 RegLocation rl_src)
1230 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1231 rl_src_(rl_src) {
1232 }
1233
1234 void Compile() OVERRIDE {
1235 GenerateTargetLabel();
1236
Andreas Gampe98430592014-07-27 19:44:50 -07001237 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001238 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1239 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001240 m2l_->OpUnconditionalBranch(cont_);
1241 }
1242
1243 private:
1244 uint32_t type_idx_;
1245 RegLocation rl_src_;
1246 };
1247
1248 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1249 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 }
1251 }
1252 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001253 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001254 if (!IsSameReg(rl_result.reg, ref_reg)) {
1255 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001256 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001258 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259
1260 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001261 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001263 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1264 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1266 LIR* branchover = NULL;
1267 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001268 // rl_result == ref == class.
1269 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001270 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 } else {
1272 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001273 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001274 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 if (!type_known_abstract) {
1276 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001277 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001278 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001279 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001281 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001283 if (it != nullptr) {
1284 OpEndIT(it);
1285 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 FreeTemp(r_tgt);
1287 } else {
1288 if (!type_known_abstract) {
1289 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001290 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001291 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001293
Serguei Katkov9ee45192014-07-17 14:39:03 +07001294 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001295 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 }
1297 }
1298 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001299 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 /* branch targets here */
1301 LIR* target = NewLIR0(kPseudoTargetLabel);
1302 StoreValue(rl_dest, rl_result);
1303 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001304 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 branchover->target = target;
1306 }
1307}
1308
1309void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1310 bool type_known_final, type_known_abstract, use_declaring_class;
1311 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1312 *cu_->dex_file,
1313 type_idx,
1314 &type_known_final,
1315 &type_known_abstract,
1316 &use_declaring_class);
1317 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1318 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1319
1320 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1321 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1322 } else {
1323 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1324 use_declaring_class, can_assume_type_is_in_dex_cache,
1325 type_idx, rl_dest, rl_src);
1326 }
1327}
1328
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001329void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 bool type_known_final, type_known_abstract, use_declaring_class;
1331 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1332 *cu_->dex_file,
1333 type_idx,
1334 &type_known_final,
1335 &type_known_abstract,
1336 &use_declaring_class);
1337 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1338 // of the exception throw path.
1339 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001340 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 // Verifier type analysis proved this check cast would never cause an exception.
1342 return;
1343 }
1344 FlushAllRegs();
1345 // May generate a call - use explicit registers
1346 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001347 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001348 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001349 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 if (needs_access_check) {
1351 // Check we have access to type_idx and if not throw IllegalAccessError,
1352 // returns Class* in kRet0
1353 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001354 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001355 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001357 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001358 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 } else {
1360 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001361 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001362 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001363 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001364 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1366 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001367 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1368 LIR* cont = NewLIR0(kPseudoTargetLabel);
1369
1370 // Slow path to initialize the type. Executed if the type is NULL.
1371 class SlowPath : public LIRSlowPath {
1372 public:
1373 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001374 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1376 class_reg_(class_reg) {
1377 }
1378
1379 void Compile() {
1380 GenerateTargetLabel();
1381
1382 // Call out to helper, which will return resolved type in kArg0
1383 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001384 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1385 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001386 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001387 m2l_->OpUnconditionalBranch(cont_);
1388 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001389
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001390 public:
1391 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001392 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001393 };
1394
buzbee2700f7e2014-03-07 09:46:20 -08001395 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 }
1397 }
1398 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001399 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400
1401 // Slow path for the case where the classes are not equal. In this case we need
1402 // to call a helper function to do the check.
1403 class SlowPath : public LIRSlowPath {
1404 public:
1405 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1406 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1407 }
1408
1409 void Compile() {
1410 GenerateTargetLabel();
1411
1412 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001413 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1414 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415 }
Andreas Gampe98430592014-07-27 19:44:50 -07001416 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1417 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001418 m2l_->OpUnconditionalBranch(cont_);
1419 }
1420
1421 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001422 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001423 };
1424
1425 if (type_known_abstract) {
1426 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001427 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428 LIR* cont = NewLIR0(kPseudoTargetLabel);
1429 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1430 } else {
1431 // Harder, more common case. We need to generate a forward branch over the load
1432 // if the target is null. If it's non-null we perform the load and branch to the
1433 // slow path if the classes are not equal.
1434
1435 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001436 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001437 /* load object->klass_ */
1438 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001439 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1440 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001441
Andreas Gampeccc60262014-07-04 18:02:38 -07001442 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001443 LIR* cont = NewLIR0(kPseudoTargetLabel);
1444
1445 // Add the slow path that will not perform load since this is already done.
1446 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1447
1448 // Set the null check to branch to the continuation.
1449 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001450 }
1451}
1452
1453void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001454 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 RegLocation rl_result;
1456 if (cu_->instruction_set == kThumb2) {
1457 /*
1458 * NOTE: This is the one place in the code in which we might have
1459 * as many as six live temporary registers. There are 5 in the normal
1460 * set for Arm. Until we have spill capabilities, temporarily add
1461 * lr to the temp set. It is safe to do this locally, but note that
1462 * lr is used explicitly elsewhere in the code generator and cannot
1463 * normally be used as a general temp register.
1464 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001465 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1466 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 }
1468 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1469 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1470 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1471 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001472 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1473 RegStorage t_reg = AllocTemp();
1474 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1475 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1476 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 FreeTemp(t_reg);
1478 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001479 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1480 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 }
1482 /*
1483 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1484 * following StoreValueWide might need to allocate a temp register.
1485 * To further work around the lack of a spill capability, explicitly
1486 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1487 * Remove when spill is functional.
1488 */
1489 FreeRegLocTemps(rl_result, rl_src1);
1490 FreeRegLocTemps(rl_result, rl_src2);
1491 StoreValueWide(rl_dest, rl_result);
1492 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001493 Clobber(TargetReg(kLr, kNotWide));
1494 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 }
1496}
1497
Andreas Gampe98430592014-07-27 19:44:50 -07001498void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1499 RegLocation rl_src1, RegLocation rl_shift) {
1500 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 switch (opcode) {
1502 case Instruction::SHL_LONG:
1503 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001504 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 break;
1506 case Instruction::SHR_LONG:
1507 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001508 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 break;
1510 case Instruction::USHR_LONG:
1511 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001512 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 break;
1514 default:
1515 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001516 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 }
Andreas Gampe98430592014-07-27 19:44:50 -07001518 FlushAllRegs(); /* Send everything to home location */
1519 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001520 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 StoreValueWide(rl_dest, rl_result);
1522}
1523
1524
1525void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001526 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001527 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 OpKind op = kOpBkpt;
1529 bool is_div_rem = false;
1530 bool check_zero = false;
1531 bool unary = false;
1532 RegLocation rl_result;
1533 bool shift_op = false;
1534 switch (opcode) {
1535 case Instruction::NEG_INT:
1536 op = kOpNeg;
1537 unary = true;
1538 break;
1539 case Instruction::NOT_INT:
1540 op = kOpMvn;
1541 unary = true;
1542 break;
1543 case Instruction::ADD_INT:
1544 case Instruction::ADD_INT_2ADDR:
1545 op = kOpAdd;
1546 break;
1547 case Instruction::SUB_INT:
1548 case Instruction::SUB_INT_2ADDR:
1549 op = kOpSub;
1550 break;
1551 case Instruction::MUL_INT:
1552 case Instruction::MUL_INT_2ADDR:
1553 op = kOpMul;
1554 break;
1555 case Instruction::DIV_INT:
1556 case Instruction::DIV_INT_2ADDR:
1557 check_zero = true;
1558 op = kOpDiv;
1559 is_div_rem = true;
1560 break;
1561 /* NOTE: returns in kArg1 */
1562 case Instruction::REM_INT:
1563 case Instruction::REM_INT_2ADDR:
1564 check_zero = true;
1565 op = kOpRem;
1566 is_div_rem = true;
1567 break;
1568 case Instruction::AND_INT:
1569 case Instruction::AND_INT_2ADDR:
1570 op = kOpAnd;
1571 break;
1572 case Instruction::OR_INT:
1573 case Instruction::OR_INT_2ADDR:
1574 op = kOpOr;
1575 break;
1576 case Instruction::XOR_INT:
1577 case Instruction::XOR_INT_2ADDR:
1578 op = kOpXor;
1579 break;
1580 case Instruction::SHL_INT:
1581 case Instruction::SHL_INT_2ADDR:
1582 shift_op = true;
1583 op = kOpLsl;
1584 break;
1585 case Instruction::SHR_INT:
1586 case Instruction::SHR_INT_2ADDR:
1587 shift_op = true;
1588 op = kOpAsr;
1589 break;
1590 case Instruction::USHR_INT:
1591 case Instruction::USHR_INT_2ADDR:
1592 shift_op = true;
1593 op = kOpLsr;
1594 break;
1595 default:
1596 LOG(FATAL) << "Invalid word arith op: " << opcode;
1597 }
1598 if (!is_div_rem) {
1599 if (unary) {
1600 rl_src1 = LoadValue(rl_src1, kCoreReg);
1601 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001602 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001604 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001605 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001606 RegStorage t_reg = AllocTemp();
1607 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 rl_src1 = LoadValue(rl_src1, kCoreReg);
1609 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 FreeTemp(t_reg);
1612 } else {
1613 rl_src1 = LoadValue(rl_src1, kCoreReg);
1614 rl_src2 = LoadValue(rl_src2, kCoreReg);
1615 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001616 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 }
1618 }
1619 StoreValue(rl_dest, rl_result);
1620 } else {
Dave Allison70202782013-10-22 17:52:19 -07001621 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 +01001622 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 rl_src1 = LoadValue(rl_src1, kCoreReg);
1624 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001625 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001626 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 }
buzbee2700f7e2014-03-07 09:46:20 -08001628 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001629 done = true;
1630 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001631 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1632 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001633 // Use ARM SDIV instruction for division. For remainder we also need to
1634 // calculate using a MUL and subtract.
1635 rl_src1 = LoadValue(rl_src1, kCoreReg);
1636 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001637 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001638 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001639 }
buzbee2700f7e2014-03-07 09:46:20 -08001640 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001641 done = true;
1642 }
1643 }
1644
1645 // If we haven't already generated the code use the callout function.
1646 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001648 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001649 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001650 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001651 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001652 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
Dave Allison70202782013-10-22 17:52:19 -07001654 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001655 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001657 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 else
1659 rl_result = GetReturnAlt();
1660 }
1661 StoreValue(rl_dest, rl_result);
1662 }
1663}
1664
1665/*
1666 * The following are the first-level codegen routines that analyze the format
1667 * of each bytecode then either dispatch special purpose codegen routines
1668 * or produce corresponding Thumb instructions directly.
1669 */
1670
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001672static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 x &= x - 1;
1674 return (x & (x - 1)) == 0;
1675}
1676
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1678// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001679bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001680 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1682 return false;
1683 }
1684 // No divide instruction for Arm, so check for more special cases
1685 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001686 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 }
1688 int k = LowestSetBit(lit);
1689 if (k >= 30) {
1690 // Avoid special cases.
1691 return false;
1692 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 rl_src = LoadValue(rl_src, kCoreReg);
1694 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001695 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001696 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697 if (lit == 2) {
1698 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001699 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1700 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1701 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001703 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1706 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 }
1708 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001709 RegStorage t_reg1 = AllocTemp();
1710 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001712 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1713 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001715 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001717 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001719 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 }
1723 }
1724 StoreValue(rl_dest, rl_result);
1725 return true;
1726}
1727
1728// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1729// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001730bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001731 if (lit < 0) {
1732 return false;
1733 }
1734 if (lit == 0) {
1735 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1736 LoadConstant(rl_result.reg, 0);
1737 StoreValue(rl_dest, rl_result);
1738 return true;
1739 }
1740 if (lit == 1) {
1741 rl_src = LoadValue(rl_src, kCoreReg);
1742 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1743 OpRegCopy(rl_result.reg, rl_src.reg);
1744 StoreValue(rl_dest, rl_result);
1745 return true;
1746 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001747 // There is RegRegRegShift on Arm, so check for more special cases
1748 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001749 return EasyMultiply(rl_src, rl_dest, lit);
1750 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 // Can we simplify this multiplication?
1752 bool power_of_two = false;
1753 bool pop_count_le2 = false;
1754 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001755 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 power_of_two = true;
1757 } else if (IsPopCountLE2(lit)) {
1758 pop_count_le2 = true;
1759 } else if (IsPowerOfTwo(lit + 1)) {
1760 power_of_two_minus_one = true;
1761 } else {
1762 return false;
1763 }
1764 rl_src = LoadValue(rl_src, kCoreReg);
1765 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1766 if (power_of_two) {
1767 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001768 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 } else if (pop_count_le2) {
1770 // Shift and add and shift.
1771 int first_bit = LowestSetBit(lit);
1772 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1773 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1774 } else {
1775 // Reverse subtract: (src << (shift + 1)) - src.
1776 DCHECK(power_of_two_minus_one);
1777 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001778 RegStorage t_reg = AllocTemp();
1779 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1780 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 }
1782 StoreValue(rl_dest, rl_result);
1783 return true;
1784}
1785
1786void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001787 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 RegLocation rl_result;
1789 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1790 int shift_op = false;
1791 bool is_div = false;
1792
1793 switch (opcode) {
1794 case Instruction::RSUB_INT_LIT8:
1795 case Instruction::RSUB_INT: {
1796 rl_src = LoadValue(rl_src, kCoreReg);
1797 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1798 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001799 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001801 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1802 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803 }
1804 StoreValue(rl_dest, rl_result);
1805 return;
1806 }
1807
1808 case Instruction::SUB_INT:
1809 case Instruction::SUB_INT_2ADDR:
1810 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001811 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 case Instruction::ADD_INT:
1813 case Instruction::ADD_INT_2ADDR:
1814 case Instruction::ADD_INT_LIT8:
1815 case Instruction::ADD_INT_LIT16:
1816 op = kOpAdd;
1817 break;
1818 case Instruction::MUL_INT:
1819 case Instruction::MUL_INT_2ADDR:
1820 case Instruction::MUL_INT_LIT8:
1821 case Instruction::MUL_INT_LIT16: {
1822 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1823 return;
1824 }
1825 op = kOpMul;
1826 break;
1827 }
1828 case Instruction::AND_INT:
1829 case Instruction::AND_INT_2ADDR:
1830 case Instruction::AND_INT_LIT8:
1831 case Instruction::AND_INT_LIT16:
1832 op = kOpAnd;
1833 break;
1834 case Instruction::OR_INT:
1835 case Instruction::OR_INT_2ADDR:
1836 case Instruction::OR_INT_LIT8:
1837 case Instruction::OR_INT_LIT16:
1838 op = kOpOr;
1839 break;
1840 case Instruction::XOR_INT:
1841 case Instruction::XOR_INT_2ADDR:
1842 case Instruction::XOR_INT_LIT8:
1843 case Instruction::XOR_INT_LIT16:
1844 op = kOpXor;
1845 break;
1846 case Instruction::SHL_INT_LIT8:
1847 case Instruction::SHL_INT:
1848 case Instruction::SHL_INT_2ADDR:
1849 lit &= 31;
1850 shift_op = true;
1851 op = kOpLsl;
1852 break;
1853 case Instruction::SHR_INT_LIT8:
1854 case Instruction::SHR_INT:
1855 case Instruction::SHR_INT_2ADDR:
1856 lit &= 31;
1857 shift_op = true;
1858 op = kOpAsr;
1859 break;
1860 case Instruction::USHR_INT_LIT8:
1861 case Instruction::USHR_INT:
1862 case Instruction::USHR_INT_2ADDR:
1863 lit &= 31;
1864 shift_op = true;
1865 op = kOpLsr;
1866 break;
1867
1868 case Instruction::DIV_INT:
1869 case Instruction::DIV_INT_2ADDR:
1870 case Instruction::DIV_INT_LIT8:
1871 case Instruction::DIV_INT_LIT16:
1872 case Instruction::REM_INT:
1873 case Instruction::REM_INT_2ADDR:
1874 case Instruction::REM_INT_LIT8:
1875 case Instruction::REM_INT_LIT16: {
1876 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001877 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878 return;
1879 }
buzbee11b63d12013-08-27 07:34:17 -07001880 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001881 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001882 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001883 (opcode == Instruction::DIV_INT_LIT16)) {
1884 is_div = true;
1885 } else {
1886 is_div = false;
1887 }
buzbee11b63d12013-08-27 07:34:17 -07001888 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1889 return;
1890 }
Dave Allison70202782013-10-22 17:52:19 -07001891
1892 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001893 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001894 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001895 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001896 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001897 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001898 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1899 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001900 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001901 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1902 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001903 // Use ARM SDIV instruction for division. For remainder we also need to
1904 // calculate using a MUL and subtract.
1905 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001906 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001907 done = true;
1908 }
1909 }
1910
1911 if (!done) {
1912 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001913 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1914 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001915 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001916 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001917 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001918 else
1919 rl_result = GetReturnAlt();
1920 }
1921 StoreValue(rl_dest, rl_result);
1922 return;
1923 }
1924 default:
1925 LOG(FATAL) << "Unexpected opcode " << opcode;
1926 }
1927 rl_src = LoadValue(rl_src, kCoreReg);
1928 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001929 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001930 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001931 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001932 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001933 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 }
1935 StoreValue(rl_dest, rl_result);
1936}
1937
Andreas Gampe98430592014-07-27 19:44:50 -07001938void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001939 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 RegLocation rl_result;
1941 OpKind first_op = kOpBkpt;
1942 OpKind second_op = kOpBkpt;
1943 bool call_out = false;
1944 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001945 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1946 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947
1948 switch (opcode) {
1949 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001950 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1951 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001953 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001954 RegStorage t_reg = AllocTemp();
1955 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1956 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1957 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1958 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001960 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1961 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001962 }
Andreas Gampe98430592014-07-27 19:44:50 -07001963 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 return;
1965 case Instruction::ADD_LONG:
1966 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 first_op = kOpAdd;
1968 second_op = kOpAdc;
1969 break;
1970 case Instruction::SUB_LONG:
1971 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 first_op = kOpSub;
1973 second_op = kOpSbc;
1974 break;
1975 case Instruction::MUL_LONG:
1976 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001977 call_out = true;
1978 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1979 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 break;
1981 case Instruction::DIV_LONG:
1982 case Instruction::DIV_LONG_2ADDR:
1983 call_out = true;
1984 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001985 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1986 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 break;
1988 case Instruction::REM_LONG:
1989 case Instruction::REM_LONG_2ADDR:
1990 call_out = true;
1991 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001992 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001994 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1995 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 break;
1997 case Instruction::AND_LONG_2ADDR:
1998 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 first_op = kOpAnd;
2000 second_op = kOpAnd;
2001 break;
2002 case Instruction::OR_LONG:
2003 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 first_op = kOpOr;
2005 second_op = kOpOr;
2006 break;
2007 case Instruction::XOR_LONG:
2008 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 first_op = kOpXor;
2010 second_op = kOpXor;
2011 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 default:
2013 LOG(FATAL) << "Invalid long arith op";
2014 }
2015 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002016 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002018 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002020 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2021 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2022 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2023 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002024 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2025 GenDivZeroCheckWide(r_tmp2);
2026 }
Andreas Gampe98430592014-07-27 19:44:50 -07002027 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002029 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002031 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 }
2033 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002034 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2035 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 else
Andreas Gampe98430592014-07-27 19:44:50 -07002037 rl_result = GetReturnWideAlt();
2038 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002039 }
2040}
2041
Mark Mendelle87f9b52014-04-30 14:13:18 -04002042void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2043 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2044 LoadConstantNoClobber(rl_result.reg, value);
2045 StoreValue(rl_dest, rl_result);
2046 if (value == 0) {
2047 Workaround7250540(rl_dest, rl_result.reg);
2048 }
2049}
2050
Andreas Gampe98430592014-07-27 19:44:50 -07002051void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2052 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 /*
2054 * Don't optimize the register usage since it calls out to support
2055 * functions
2056 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002057
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002059 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 if (rl_dest.wide) {
2061 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002062 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002063 StoreValueWide(rl_dest, rl_result);
2064 } else {
2065 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002066 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 StoreValue(rl_dest, rl_result);
2068 }
2069}
2070
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002071class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2072 public:
2073 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2074 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2075 }
2076
2077 void Compile() OVERRIDE {
2078 m2l_->ResetRegPool();
2079 m2l_->ResetDefTracking();
2080 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002081 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002082 if (cont_ != nullptr) {
2083 m2l_->OpUnconditionalBranch(cont_);
2084 }
2085 }
2086};
2087
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002089void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002090 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002091 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2092 return;
2093 }
2094 FlushAllRegs();
2095 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002096 LIR* cont = NewLIR0(kPseudoTargetLabel);
2097 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002098 } else {
2099 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2100 return;
2101 }
2102 FlushAllRegs(); // TODO: needed?
2103 LIR* inst = CheckSuspendUsingLoad();
2104 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002106}
2107
2108/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002109void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002110 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002111 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2112 OpUnconditionalBranch(target);
2113 return;
2114 }
2115 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002116 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002117 LIR* branch = OpUnconditionalBranch(nullptr);
2118 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002119 } else {
2120 // For the implicit suspend check, just perform the trigger
2121 // load and branch to the target.
2122 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2123 OpUnconditionalBranch(target);
2124 return;
2125 }
2126 FlushAllRegs();
2127 LIR* inst = CheckSuspendUsingLoad();
2128 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002129 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002130 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002131}
2132
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002133/* Call out to helper assembly routine that will null check obj and then lock it. */
2134void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002135 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002136 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002137 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002138}
2139
2140/* Call out to helper assembly routine that will null check obj and then unlock it. */
2141void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002142 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002143 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002144 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002145}
2146
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002147/* Generic code for generating a wide constant into a VR. */
2148void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2149 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002150 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002151 StoreValueWide(rl_dest, rl_result);
2152}
2153
Andreas Gampe48971b32014-08-06 10:09:01 -07002154void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002155 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002156 const uint16_t entries = table[1];
2157 // Chained cmp-and-branch.
2158 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002159 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002160 const int32_t* targets = &as_int32[1];
2161 rl_src = LoadValue(rl_src, kCoreReg);
2162 int i = 0;
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002163 for (; i < entries; i++) {
2164 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002165 // Switch to using a temp and add.
2166 break;
2167 }
2168 BasicBlock* case_block =
2169 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002170 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002171 }
2172 if (i < entries) {
2173 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2174 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2175 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002176 LoadConstantNoClobber(key_temp, starting_key + i);
2177 for (; i < entries - 1; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002178 BasicBlock* case_block =
2179 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2180 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2181 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2182 }
2183 BasicBlock* case_block =
2184 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2185 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2186 } else {
2187 // No free temp, just finish the old loop.
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002188 for (; i < entries; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002189 BasicBlock* case_block =
2190 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002191 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002192 }
2193 }
2194 }
2195}
2196
2197void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002198 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002199 if (cu_->verbose) {
2200 DumpSparseSwitchTable(table);
2201 }
2202
2203 const uint16_t entries = table[1];
2204 if (entries <= kSmallSwitchThreshold) {
2205 GenSmallPackedSwitch(mir, table_offset, rl_src);
2206 } else {
2207 // Use the backend-specific implementation.
2208 GenLargePackedSwitch(mir, table_offset, rl_src);
2209 }
2210}
2211
2212void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002213 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002214 const uint16_t entries = table[1];
2215 // Chained cmp-and-branch.
2216 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2217 const int32_t* targets = &keys[entries];
2218 rl_src = LoadValue(rl_src, kCoreReg);
2219 for (int i = 0; i < entries; i++) {
2220 int key = keys[i];
2221 BasicBlock* case_block =
2222 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2223 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2224 }
2225}
2226
2227void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002228 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002229 if (cu_->verbose) {
2230 DumpSparseSwitchTable(table);
2231 }
2232
2233 const uint16_t entries = table[1];
2234 if (entries <= kSmallSwitchThreshold) {
2235 GenSmallSparseSwitch(mir, table_offset, rl_src);
2236 } else {
2237 // Use the backend-specific implementation.
2238 GenLargeSparseSwitch(mir, table_offset, rl_src);
2239 }
2240}
2241
Fred Shih37f05ef2014-07-16 18:38:08 -07002242bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2243 switch (size) {
2244 case kReference:
2245 return type == Primitive::kPrimNot;
2246 case k64:
2247 case kDouble:
2248 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2249 case k32:
2250 case kSingle:
2251 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2252 case kSignedHalf:
2253 return type == Primitive::kPrimShort;
2254 case kUnsignedHalf:
2255 return type == Primitive::kPrimChar;
2256 case kSignedByte:
2257 return type == Primitive::kPrimByte;
2258 case kUnsignedByte:
2259 return type == Primitive::kPrimBoolean;
2260 case kWord: // Intentional fallthrough.
2261 default:
2262 return false; // There are no sane types with this op size.
2263 }
2264}
2265
Brian Carlstrom7940e442013-07-12 13:46:57 -07002266} // namespace art