blob: 3f7ecfe03b7dbae332231e30686533f06716ec14 [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,
215 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700216 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700218 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 switch (opcode) {
220 case Instruction::IF_EQ:
221 cond = kCondEq;
222 break;
223 case Instruction::IF_NE:
224 cond = kCondNe;
225 break;
226 case Instruction::IF_LT:
227 cond = kCondLt;
228 break;
229 case Instruction::IF_GE:
230 cond = kCondGe;
231 break;
232 case Instruction::IF_GT:
233 cond = kCondGt;
234 break;
235 case Instruction::IF_LE:
236 cond = kCondLe;
237 break;
238 default:
239 cond = static_cast<ConditionCode>(0);
240 LOG(FATAL) << "Unexpected opcode " << opcode;
241 }
242
243 // Normalize such that if either operand is constant, src2 will be constant
244 if (rl_src1.is_const) {
245 RegLocation rl_temp = rl_src1;
246 rl_src1 = rl_src2;
247 rl_src2 = rl_temp;
248 cond = FlipComparisonOrder(cond);
249 }
250
buzbee7c02e912014-10-03 13:14:17 -0700251 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 // Is this really an immediate comparison?
253 if (rl_src2.is_const) {
254 // If it's already live in a register or not easily materialized, just keep going
255 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700256 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100258 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800260 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 return;
262 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700263
264 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
265 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
266 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
267 // the 32b literal 0 for null.
268 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
269 // Use the OpCmpImmBranch and ignore the value in the register.
270 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
271 return;
272 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700274
buzbee7c02e912014-10-03 13:14:17 -0700275 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800276 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277}
278
279void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700280 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700282 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
283 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 switch (opcode) {
285 case Instruction::IF_EQZ:
286 cond = kCondEq;
287 break;
288 case Instruction::IF_NEZ:
289 cond = kCondNe;
290 break;
291 case Instruction::IF_LTZ:
292 cond = kCondLt;
293 break;
294 case Instruction::IF_GEZ:
295 cond = kCondGe;
296 break;
297 case Instruction::IF_GTZ:
298 cond = kCondGt;
299 break;
300 case Instruction::IF_LEZ:
301 cond = kCondLe;
302 break;
303 default:
304 cond = static_cast<ConditionCode>(0);
305 LOG(FATAL) << "Unexpected opcode " << opcode;
306 }
buzbee2700f7e2014-03-07 09:46:20 -0800307 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308}
309
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700310void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
312 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800313 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800315 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 }
buzbee2700f7e2014-03-07 09:46:20 -0800317 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 StoreValueWide(rl_dest, rl_result);
319}
320
321void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700322 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700323 rl_src = LoadValue(rl_src, kCoreReg);
324 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
325 OpKind op = kOpInvalid;
326 switch (opcode) {
327 case Instruction::INT_TO_BYTE:
328 op = kOp2Byte;
329 break;
330 case Instruction::INT_TO_SHORT:
331 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700333 case Instruction::INT_TO_CHAR:
334 op = kOp2Char;
335 break;
336 default:
337 LOG(ERROR) << "Bad int conversion type";
338 }
buzbee2700f7e2014-03-07 09:46:20 -0800339 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700340 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341}
342
Andreas Gampe98430592014-07-27 19:44:50 -0700343/*
344 * Let helper function take care of everything. Will call
345 * Array::AllocFromCode(type_idx, method, count);
346 * Note: AllocFromCode will handle checks for errNegativeArraySize.
347 */
348void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
349 RegLocation rl_src) {
350 FlushAllRegs(); /* Everything to home location */
351 const DexFile* dex_file = cu_->dex_file;
352 CompilerDriver* driver = cu_->compiler_driver;
353 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800354 bool is_type_initialized; // Ignored as an array does not have an initializer.
355 bool use_direct_type_ptr;
356 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700357 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800358 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700359 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
360 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 // The fast path.
362 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700363 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700364 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
365 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800366 } else {
367 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700368 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
369 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800370 }
371 } else {
372 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700373 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800374 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700376 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
Andreas Gampe98430592014-07-27 19:44:50 -0700378 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379}
380
381/*
382 * Similar to GenNewArray, but with post-allocation initialization.
383 * Verifier guarantees we're dealing with an array class. Current
384 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
385 * Current code also throws internal unimp if not 'L', '[' or 'I'.
386 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700387void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 int elems = info->num_arg_words;
389 int type_idx = info->index;
390 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700391 QuickEntrypointEnum target;
392 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
393 type_idx)) {
394 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700396 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 }
Andreas Gampe98430592014-07-27 19:44:50 -0700398 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700399 FreeTemp(TargetReg(kArg2, kNotWide));
400 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 /*
402 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
403 * return region. Because AllocFromCode placed the new array
404 * in kRet0, we'll just lock it into place. When debugger support is
405 * added, it may be necessary to additionally copy all return
406 * values to a home location in thread-local storage
407 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700408 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700409 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410
411 // TODO: use the correct component size, currently all supported types
412 // share array alignment with ints (see comment at head of function)
413 size_t component_size = sizeof(int32_t);
414
415 // Having a range of 0 is legal
416 if (info->is_range && (elems > 0)) {
417 /*
418 * Bit of ugliness here. We're going generate a mem copy loop
419 * on the register range, but it is possible that some regs
420 * in the range have been promoted. This is unlikely, but
421 * before generating the copy, we'll just force a flush
422 * of any regs in the source range that have been promoted to
423 * home location.
424 */
425 for (int i = 0; i < elems; i++) {
426 RegLocation loc = UpdateLoc(info->args[i]);
427 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100428 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700429 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 }
431 }
432 /*
433 * TUNING note: generated code here could be much improved, but
434 * this is an uncommon operation and isn't especially performance
435 * critical.
436 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700437 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700438 RegStorage r_src = AllocTempRef();
439 RegStorage r_dst = AllocTempRef();
440 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800441 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700442 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700444 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700445 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 break;
447 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700448 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700449 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 r_val = AllocTemp();
451 break;
452 case kMips:
453 r_val = AllocTemp();
454 break;
455 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
456 }
457 // Set up source pointer
458 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700459 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700461 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 mirror::Array::DataOffset(component_size).Int32Value());
463 // Set up the loop counter (known to be > 0)
464 LoadConstant(r_idx, elems - 1);
465 // Generate the copy loop. Going backwards for convenience
466 LIR* target = NewLIR0(kPseudoTargetLabel);
467 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100468 {
469 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
470 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
471 // NOTE: No dalvik register annotation, local optimizations will be stopped
472 // by the loop boundaries.
473 }
buzbee695d13a2014-04-19 13:32:20 -0700474 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 FreeTemp(r_val);
476 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700477 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700479 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 -mirror::Array::DataOffset(component_size).Int32Value());
481 }
482 } else if (!info->is_range) {
483 // TUNING: interleave
484 for (int i = 0; i < elems; i++) {
485 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700486 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000487 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800489 if (IsTemp(rl_arg.reg)) {
490 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 }
492 }
493 }
494 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700495 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 }
497}
498
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800499//
500// Slow path to ensure a class is initialized for sget/sput.
501//
502class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
503 public:
buzbee2700f7e2014-03-07 09:46:20 -0800504 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
505 RegStorage r_base) :
506 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
507 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800508 }
509
510 void Compile() {
511 LIR* unresolved_target = GenerateTargetLabel();
512 uninit_->target = unresolved_target;
Andreas Gampe98430592014-07-27 19:44:50 -0700513 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800514 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700515 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800516
517 m2l_->OpUnconditionalBranch(cont_);
518 }
519
520 private:
521 LIR* const uninit_;
522 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800523 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800524};
525
Fred Shih37f05ef2014-07-16 18:38:08 -0700526void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000527 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
528 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700529 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000530 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800531 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000532 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100534 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700535 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000536 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
537 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800538 if (IsTemp(rl_method.reg)) {
539 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
541 } else {
542 // Medium path, static storage base in a different class which requires checks that the other
543 // class is initialized.
544 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000545 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 // May do runtime call so everything to home locations.
547 FlushAllRegs();
548 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700549 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 LockTemp(r_method);
551 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700552 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800553 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000554 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
555 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000556 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000557 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800558 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000559 if (!field_info.IsInitialized() &&
560 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800562
563 // The slow path is invoked if the r_base is NULL or the class pointed
564 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800565 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700566 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800567 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800568 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800569 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000570 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800571 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800572
buzbee2700f7e2014-03-07 09:46:20 -0800573 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000574 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800575
576 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700577 // Ensure load of status and store of value don't re-order.
578 // TODO: Presumably the actual value store is control-dependent on the status load,
579 // and will thus not be reordered in any case, since stores are never speculated.
580 // Does later code "know" that the class is now initialized? If so, we still
581 // need the barrier to guard later static loads.
582 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 FreeTemp(r_method);
585 }
586 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700587 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
588 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100589 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100591 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700593 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000594 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
595 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100596 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700597 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000598 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700600 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800601 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 } else {
605 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700606 QuickEntrypointEnum target;
607 switch (size) {
608 case kReference:
609 target = kQuickSetObjStatic;
610 break;
611 case k64:
612 case kDouble:
613 target = kQuickSet64Static;
614 break;
615 case k32:
616 case kSingle:
617 target = kQuickSet32Static;
618 break;
619 case kSignedHalf:
620 case kUnsignedHalf:
621 target = kQuickSet16Static;
622 break;
623 case kSignedByte:
624 case kUnsignedByte:
625 target = kQuickSet8Static;
626 break;
627 case kWord: // Intentional fallthrough.
628 default:
629 LOG(FATAL) << "Can't determine entrypoint for: " << size;
630 target = kQuickSet32Static;
631 }
Andreas Gampe98430592014-07-27 19:44:50 -0700632 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 }
634}
635
Fred Shih37f05ef2014-07-16 18:38:08 -0700636void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000637 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
638 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700639
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700640 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000641 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800642 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000643 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 // Fast path, static storage base is this method's class
645 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700646 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000647 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
648 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 } else {
650 // Medium path, static storage base in a different class which requires checks that the other
651 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000652 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 // May do runtime call so everything to home locations.
654 FlushAllRegs();
655 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700656 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 LockTemp(r_method);
658 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700659 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800660 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000661 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
662 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000663 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000664 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800665 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000666 if (!field_info.IsInitialized() &&
667 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800668 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800669
670 // The slow path is invoked if the r_base is NULL or the class pointed
671 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800672 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700673 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800674 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800675 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800676 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000677 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800678 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800679
buzbee2700f7e2014-03-07 09:46:20 -0800680 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000681 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800682
683 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700684 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700685 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 FreeTemp(r_method);
688 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800689 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700690 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100691 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800692
Vladimir Marko674744e2014-04-24 15:18:26 +0100693 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700694 if (IsRef(size)) {
695 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000696 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
697 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100698 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700699 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000700 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800701 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100702 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800703
Fred Shih37f05ef2014-07-16 18:38:08 -0700704 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 StoreValueWide(rl_dest, rl_result);
706 } else {
707 StoreValue(rl_dest, rl_result);
708 }
709 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700710 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700712 QuickEntrypointEnum target;
713 switch (type) {
714 case Primitive::kPrimNot:
715 target = kQuickGetObjStatic;
716 break;
717 case Primitive::kPrimLong:
718 case Primitive::kPrimDouble:
719 target = kQuickGet64Static;
720 break;
721 case Primitive::kPrimInt:
722 case Primitive::kPrimFloat:
723 target = kQuickGet32Static;
724 break;
725 case Primitive::kPrimShort:
726 target = kQuickGetShortStatic;
727 break;
728 case Primitive::kPrimChar:
729 target = kQuickGetCharStatic;
730 break;
731 case Primitive::kPrimByte:
732 target = kQuickGetByteStatic;
733 break;
734 case Primitive::kPrimBoolean:
735 target = kQuickGetBooleanStatic;
736 break;
737 case Primitive::kPrimVoid: // Intentional fallthrough.
738 default:
739 LOG(FATAL) << "Can't determine entrypoint for: " << type;
740 target = kQuickGet32Static;
741 }
Andreas Gampe98430592014-07-27 19:44:50 -0700742 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
743
Douglas Leung2db3e262014-06-25 16:02:55 -0700744 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700745 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700746 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 StoreValueWide(rl_dest, rl_result);
748 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700749 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 StoreValue(rl_dest, rl_result);
751 }
752 }
753}
754
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800755// Generate code for all slow paths.
756void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700757 // We should check slow_paths_.Size() every time, because a new slow path
758 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100759 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800760 slowpath->Compile();
761 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100762 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800763}
764
Fred Shih37f05ef2014-07-16 18:38:08 -0700765void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
766 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000767 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
768 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700769 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700770 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700771 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000772 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700773 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100774 GenNullCheck(rl_obj.reg, opt_flags);
775 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
776 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000777 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700778 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000779 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
780 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100781 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700782 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000783 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100784 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000785 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700786 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 StoreValueWide(rl_dest, rl_result);
788 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 StoreValue(rl_dest, rl_result);
790 }
791 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700792 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
793 QuickEntrypointEnum target;
794 switch (type) {
795 case Primitive::kPrimNot:
796 target = kQuickGetObjInstance;
797 break;
798 case Primitive::kPrimLong:
799 case Primitive::kPrimDouble:
800 target = kQuickGet64Instance;
801 break;
802 case Primitive::kPrimFloat:
803 case Primitive::kPrimInt:
804 target = kQuickGet32Instance;
805 break;
806 case Primitive::kPrimShort:
807 target = kQuickGetShortInstance;
808 break;
809 case Primitive::kPrimChar:
810 target = kQuickGetCharInstance;
811 break;
812 case Primitive::kPrimByte:
813 target = kQuickGetByteInstance;
814 break;
815 case Primitive::kPrimBoolean:
816 target = kQuickGetBooleanInstance;
817 break;
818 case Primitive::kPrimVoid: // Intentional fallthrough.
819 default:
820 LOG(FATAL) << "Can't determine entrypoint for: " << type;
821 target = kQuickGet32Instance;
822 }
Andreas Gampe98430592014-07-27 19:44:50 -0700823 // Second argument of pGetXXInstance is always a reference.
824 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
825 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
826
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700827 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700828 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700829 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 StoreValueWide(rl_dest, rl_result);
831 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700832 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 StoreValue(rl_dest, rl_result);
834 }
835 }
836}
837
Vladimir Markobe0e5462014-02-26 11:24:15 +0000838void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700839 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000840 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
841 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700842 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700843 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700844 // Dex code never writes to the class field.
845 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
846 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700847 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700848 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100849 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 } else {
851 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100852 }
853 GenNullCheck(rl_obj.reg, opt_flags);
854 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000855 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700856 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000857 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
858 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100859 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700860 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000861 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100862 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000863 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700864 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100865 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 }
867 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700868 QuickEntrypointEnum target;
869 switch (size) {
870 case kReference:
871 target = kQuickSetObjInstance;
872 break;
873 case k64:
874 case kDouble:
875 target = kQuickSet64Instance;
876 break;
877 case k32:
878 case kSingle:
879 target = kQuickSet32Instance;
880 break;
881 case kSignedHalf:
882 case kUnsignedHalf:
883 target = kQuickSet16Instance;
884 break;
885 case kSignedByte:
886 case kUnsignedByte:
887 target = kQuickSet8Instance;
888 break;
889 case kWord: // Intentional fallthrough.
890 default:
891 LOG(FATAL) << "Can't determine entrypoint for: " << size;
892 target = kQuickSet32Instance;
893 }
Andreas Gampe98430592014-07-27 19:44:50 -0700894 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
895 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 }
897}
898
Ian Rogersa9a82542013-10-04 11:17:26 -0700899void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
900 RegLocation rl_src) {
901 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
902 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
903 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700904 QuickEntrypointEnum target = needs_range_check
905 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
906 : kQuickAputObjectWithBoundCheck)
907 : kQuickAputObject;
908 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700909}
910
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700911void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700913 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700914 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700915 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700917 *cu_->dex_file,
918 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 // Call out to helper which resolves type and verifies access.
920 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700921 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700922 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 StoreValue(rl_dest, rl_result);
924 } else {
925 // We're don't need access checks, load type from dex cache
926 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700927 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000928 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000929 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000930 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
932 type_idx) || SLOW_TYPE_PATH) {
933 // Slow path, at runtime test if type is null and if so initialize
934 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800935 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936 LIR* cont = NewLIR0(kPseudoTargetLabel);
937
938 // Object to generate the slow path for class resolution.
939 class SlowPath : public LIRSlowPath {
940 public:
941 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
942 const RegLocation& rl_method, const RegLocation& rl_result) :
943 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
944 rl_method_(rl_method), rl_result_(rl_result) {
945 }
946
947 void Compile() {
948 GenerateTargetLabel();
949
Andreas Gampe98430592014-07-27 19:44:50 -0700950 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700951 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800952 m2l_->OpUnconditionalBranch(cont_);
953 }
954
955 private:
956 const int type_idx_;
957 const RegLocation rl_method_;
958 const RegLocation rl_result_;
959 };
960
961 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800962 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800963
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 // Fast path, we're done - just store result
967 StoreValue(rl_dest, rl_result);
968 }
969 }
970}
971
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700972void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000974 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
975 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
977 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
978 // slow path, resolve string if not in dex cache
979 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700980 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800981
982 // If the Method* is already in a register, we can save a copy.
983 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800984 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800985 if (rl_method.location == kLocPhysReg) {
986 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800987 DCHECK(!IsTemp(rl_method.reg));
988 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800989 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700990 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -0800991 LoadCurrMethodDirect(r_method);
992 }
buzbee695d13a2014-04-19 13:32:20 -0700993 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -0700994 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800995
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -0700997 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
998 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700999 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001000
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001001 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001002 // Object to generate the slow path for string resolution.
1003 class SlowPath : public LIRSlowPath {
1004 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001005 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1006 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1007 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001008 }
1009
1010 void Compile() {
1011 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001012 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001013 m2l_->OpUnconditionalBranch(cont_);
1014 }
1015
1016 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001017 const RegStorage r_method_;
1018 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001019 };
1020
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001021 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001023
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001025 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 } else {
1027 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001028 RegStorage res_reg = AllocTempRef();
1029 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001030 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1031 kNotVolatile);
1032 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 StoreValue(rl_dest, rl_result);
1034 }
1035}
1036
Andreas Gampe98430592014-07-27 19:44:50 -07001037/*
1038 * Let helper function take care of everything. Will
1039 * call Class::NewInstanceFromCode(type_idx, method);
1040 */
1041void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1042 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 // alloc will always check for resolution, do we also need to verify
1044 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001045 const DexFile* dex_file = cu_->dex_file;
1046 CompilerDriver* driver = cu_->compiler_driver;
1047 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001048 bool is_type_initialized;
1049 bool use_direct_type_ptr;
1050 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001051 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001053 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1054 &direct_type_ptr, &is_finalizable) &&
1055 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 // The fast path.
1057 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001058 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001060 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001062 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001063 }
1064 } else {
1065 // Use the direct pointer.
1066 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001067 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001069 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 }
1071 }
1072 } else {
1073 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001074 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001077 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 }
Andreas Gampe98430592014-07-27 19:44:50 -07001079 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080}
1081
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001084 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085}
1086
1087// For final classes there are no sub-classes to check and so we can answer the instance-of
1088// question with simple comparisons.
1089void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1090 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001091 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001092 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001093
buzbeea0cd2d72014-06-01 09:33:49 -07001094 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001096 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001097 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001099 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
1101 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001102 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103
buzbeea0cd2d72014-06-01 09:33:49 -07001104 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1105 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106
1107 LoadCurrMethodDirect(check_class);
1108 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001109 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1110 kNotVolatile);
1111 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1112 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 } else {
buzbee695d13a2014-04-19 13:32:20 -07001114 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001115 check_class, kNotVolatile);
1116 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1117 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001118 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001119 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 }
1121
buzbee695d13a2014-04-19 13:32:20 -07001122 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 if (cu_->instruction_set == kThumb2) {
1124 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001125 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001127 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001129 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 }
1131 LIR* target = NewLIR0(kPseudoTargetLabel);
1132 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 FreeTemp(object_class);
1134 FreeTemp(check_class);
1135 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001136 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 FreeTemp(result_reg);
1138 }
1139 StoreValue(rl_dest, rl_result);
1140}
1141
1142void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1143 bool type_known_abstract, bool use_declaring_class,
1144 bool can_assume_type_is_in_dex_cache,
1145 uint32_t type_idx, RegLocation rl_dest,
1146 RegLocation rl_src) {
1147 FlushAllRegs();
1148 // May generate a call - use explicit registers
1149 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001150 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001151 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001152 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001153 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1154 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 if (needs_access_check) {
1156 // Check we have access to type_idx and if not throw IllegalAccessError,
1157 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001158 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001159 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1160 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001162 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001163 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001164 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001166 if (can_assume_type_is_in_dex_cache) {
1167 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001168 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001169 }
1170
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001172 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001173 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001174 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001175 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001177 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1178 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1179
1180 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001181 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001182
1183 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1184 public:
1185 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1186 RegLocation rl_src)
1187 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1188 rl_src_(rl_src) {
1189 }
1190
1191 void Compile() OVERRIDE {
1192 GenerateTargetLabel();
1193
Andreas Gampe98430592014-07-27 19:44:50 -07001194 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001195 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1196 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001197 m2l_->OpUnconditionalBranch(cont_);
1198 }
1199
1200 private:
1201 uint32_t type_idx_;
1202 RegLocation rl_src_;
1203 };
1204
1205 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1206 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 }
1208 }
1209 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001210 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001211 if (!IsSameReg(rl_result.reg, ref_reg)) {
1212 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001213 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001215 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216
1217 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001218 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001220 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1221 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1223 LIR* branchover = NULL;
1224 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001225 // rl_result == ref == class.
1226 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001227 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 } else {
1229 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001230 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001231 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 if (!type_known_abstract) {
1233 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001234 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001235 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001236 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001238 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001240 if (it != nullptr) {
1241 OpEndIT(it);
1242 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 FreeTemp(r_tgt);
1244 } else {
1245 if (!type_known_abstract) {
1246 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001247 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001248 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001250
Serguei Katkov9ee45192014-07-17 14:39:03 +07001251 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001252 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 }
1254 }
1255 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001256 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 /* branch targets here */
1258 LIR* target = NewLIR0(kPseudoTargetLabel);
1259 StoreValue(rl_dest, rl_result);
1260 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001261 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 branchover->target = target;
1263 }
1264}
1265
1266void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1267 bool type_known_final, type_known_abstract, use_declaring_class;
1268 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1269 *cu_->dex_file,
1270 type_idx,
1271 &type_known_final,
1272 &type_known_abstract,
1273 &use_declaring_class);
1274 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1275 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1276
1277 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1278 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1279 } else {
1280 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1281 use_declaring_class, can_assume_type_is_in_dex_cache,
1282 type_idx, rl_dest, rl_src);
1283 }
1284}
1285
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001286void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 bool type_known_final, type_known_abstract, use_declaring_class;
1288 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1289 *cu_->dex_file,
1290 type_idx,
1291 &type_known_final,
1292 &type_known_abstract,
1293 &use_declaring_class);
1294 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1295 // of the exception throw path.
1296 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001297 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 // Verifier type analysis proved this check cast would never cause an exception.
1299 return;
1300 }
1301 FlushAllRegs();
1302 // May generate a call - use explicit registers
1303 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001304 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001305 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001306 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 if (needs_access_check) {
1308 // Check we have access to type_idx and if not throw IllegalAccessError,
1309 // returns Class* in kRet0
1310 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001311 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001312 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001314 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001315 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 } else {
1317 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001318 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001319 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001320 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001321 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1323 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001324 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1325 LIR* cont = NewLIR0(kPseudoTargetLabel);
1326
1327 // Slow path to initialize the type. Executed if the type is NULL.
1328 class SlowPath : public LIRSlowPath {
1329 public:
1330 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001331 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001332 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1333 class_reg_(class_reg) {
1334 }
1335
1336 void Compile() {
1337 GenerateTargetLabel();
1338
1339 // Call out to helper, which will return resolved type in kArg0
1340 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001341 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1342 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001343 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001344 m2l_->OpUnconditionalBranch(cont_);
1345 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001346
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001347 public:
1348 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001349 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001350 };
1351
buzbee2700f7e2014-03-07 09:46:20 -08001352 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 }
1354 }
1355 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001356 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001357
1358 // Slow path for the case where the classes are not equal. In this case we need
1359 // to call a helper function to do the check.
1360 class SlowPath : public LIRSlowPath {
1361 public:
1362 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1363 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1364 }
1365
1366 void Compile() {
1367 GenerateTargetLabel();
1368
1369 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001370 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1371 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001372 }
Andreas Gampe98430592014-07-27 19:44:50 -07001373 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1374 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001375 m2l_->OpUnconditionalBranch(cont_);
1376 }
1377
1378 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001379 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001380 };
1381
1382 if (type_known_abstract) {
1383 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001384 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385 LIR* cont = NewLIR0(kPseudoTargetLabel);
1386 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1387 } else {
1388 // Harder, more common case. We need to generate a forward branch over the load
1389 // if the target is null. If it's non-null we perform the load and branch to the
1390 // slow path if the classes are not equal.
1391
1392 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001393 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001394 /* load object->klass_ */
1395 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001396 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1397 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001398
Andreas Gampeccc60262014-07-04 18:02:38 -07001399 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400 LIR* cont = NewLIR0(kPseudoTargetLabel);
1401
1402 // Add the slow path that will not perform load since this is already done.
1403 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1404
1405 // Set the null check to branch to the continuation.
1406 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 }
1408}
1409
1410void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001411 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 RegLocation rl_result;
1413 if (cu_->instruction_set == kThumb2) {
1414 /*
1415 * NOTE: This is the one place in the code in which we might have
1416 * as many as six live temporary registers. There are 5 in the normal
1417 * set for Arm. Until we have spill capabilities, temporarily add
1418 * lr to the temp set. It is safe to do this locally, but note that
1419 * lr is used explicitly elsewhere in the code generator and cannot
1420 * normally be used as a general temp register.
1421 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001422 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1423 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
1425 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1426 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1427 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1428 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001429 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1430 RegStorage t_reg = AllocTemp();
1431 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1432 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1433 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 FreeTemp(t_reg);
1435 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001436 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1437 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 }
1439 /*
1440 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1441 * following StoreValueWide might need to allocate a temp register.
1442 * To further work around the lack of a spill capability, explicitly
1443 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1444 * Remove when spill is functional.
1445 */
1446 FreeRegLocTemps(rl_result, rl_src1);
1447 FreeRegLocTemps(rl_result, rl_src2);
1448 StoreValueWide(rl_dest, rl_result);
1449 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001450 Clobber(TargetReg(kLr, kNotWide));
1451 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 }
1453}
1454
Andreas Gampe98430592014-07-27 19:44:50 -07001455void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1456 RegLocation rl_src1, RegLocation rl_shift) {
1457 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 switch (opcode) {
1459 case Instruction::SHL_LONG:
1460 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001461 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 break;
1463 case Instruction::SHR_LONG:
1464 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001465 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 break;
1467 case Instruction::USHR_LONG:
1468 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001469 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001470 break;
1471 default:
1472 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001473 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 }
Andreas Gampe98430592014-07-27 19:44:50 -07001475 FlushAllRegs(); /* Send everything to home location */
1476 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001477 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 StoreValueWide(rl_dest, rl_result);
1479}
1480
1481
1482void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001483 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001484 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 OpKind op = kOpBkpt;
1486 bool is_div_rem = false;
1487 bool check_zero = false;
1488 bool unary = false;
1489 RegLocation rl_result;
1490 bool shift_op = false;
1491 switch (opcode) {
1492 case Instruction::NEG_INT:
1493 op = kOpNeg;
1494 unary = true;
1495 break;
1496 case Instruction::NOT_INT:
1497 op = kOpMvn;
1498 unary = true;
1499 break;
1500 case Instruction::ADD_INT:
1501 case Instruction::ADD_INT_2ADDR:
1502 op = kOpAdd;
1503 break;
1504 case Instruction::SUB_INT:
1505 case Instruction::SUB_INT_2ADDR:
1506 op = kOpSub;
1507 break;
1508 case Instruction::MUL_INT:
1509 case Instruction::MUL_INT_2ADDR:
1510 op = kOpMul;
1511 break;
1512 case Instruction::DIV_INT:
1513 case Instruction::DIV_INT_2ADDR:
1514 check_zero = true;
1515 op = kOpDiv;
1516 is_div_rem = true;
1517 break;
1518 /* NOTE: returns in kArg1 */
1519 case Instruction::REM_INT:
1520 case Instruction::REM_INT_2ADDR:
1521 check_zero = true;
1522 op = kOpRem;
1523 is_div_rem = true;
1524 break;
1525 case Instruction::AND_INT:
1526 case Instruction::AND_INT_2ADDR:
1527 op = kOpAnd;
1528 break;
1529 case Instruction::OR_INT:
1530 case Instruction::OR_INT_2ADDR:
1531 op = kOpOr;
1532 break;
1533 case Instruction::XOR_INT:
1534 case Instruction::XOR_INT_2ADDR:
1535 op = kOpXor;
1536 break;
1537 case Instruction::SHL_INT:
1538 case Instruction::SHL_INT_2ADDR:
1539 shift_op = true;
1540 op = kOpLsl;
1541 break;
1542 case Instruction::SHR_INT:
1543 case Instruction::SHR_INT_2ADDR:
1544 shift_op = true;
1545 op = kOpAsr;
1546 break;
1547 case Instruction::USHR_INT:
1548 case Instruction::USHR_INT_2ADDR:
1549 shift_op = true;
1550 op = kOpLsr;
1551 break;
1552 default:
1553 LOG(FATAL) << "Invalid word arith op: " << opcode;
1554 }
1555 if (!is_div_rem) {
1556 if (unary) {
1557 rl_src1 = LoadValue(rl_src1, kCoreReg);
1558 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001559 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001561 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001562 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001563 RegStorage t_reg = AllocTemp();
1564 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565 rl_src1 = LoadValue(rl_src1, kCoreReg);
1566 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001567 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 FreeTemp(t_reg);
1569 } else {
1570 rl_src1 = LoadValue(rl_src1, kCoreReg);
1571 rl_src2 = LoadValue(rl_src2, kCoreReg);
1572 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001573 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 }
1575 }
1576 StoreValue(rl_dest, rl_result);
1577 } else {
Dave Allison70202782013-10-22 17:52:19 -07001578 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 +01001579 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 rl_src1 = LoadValue(rl_src1, kCoreReg);
1581 rl_src2 = LoadValue(rl_src2, kCoreReg);
1582 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001583 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
buzbee2700f7e2014-03-07 09:46:20 -08001585 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001586 done = true;
1587 } else if (cu_->instruction_set == kThumb2) {
1588 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1589 // Use ARM SDIV instruction for division. For remainder we also need to
1590 // calculate using a MUL and subtract.
1591 rl_src1 = LoadValue(rl_src1, kCoreReg);
1592 rl_src2 = LoadValue(rl_src2, kCoreReg);
1593 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001594 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001595 }
buzbee2700f7e2014-03-07 09:46:20 -08001596 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001597 done = true;
1598 }
1599 }
1600
1601 // If we haven't already generated the code use the callout function.
1602 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001604 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001605 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001606 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001608 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 }
Dave Allison70202782013-10-22 17:52:19 -07001610 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001611 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001613 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 else
1615 rl_result = GetReturnAlt();
1616 }
1617 StoreValue(rl_dest, rl_result);
1618 }
1619}
1620
1621/*
1622 * The following are the first-level codegen routines that analyze the format
1623 * of each bytecode then either dispatch special purpose codegen routines
1624 * or produce corresponding Thumb instructions directly.
1625 */
1626
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001628static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 x &= x - 1;
1630 return (x & (x - 1)) == 0;
1631}
1632
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1634// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001635bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001636 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1638 return false;
1639 }
1640 // No divide instruction for Arm, so check for more special cases
1641 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001642 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 }
1644 int k = LowestSetBit(lit);
1645 if (k >= 30) {
1646 // Avoid special cases.
1647 return false;
1648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 rl_src = LoadValue(rl_src, kCoreReg);
1650 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001651 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001652 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 if (lit == 2) {
1654 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001655 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1656 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1657 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001659 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1662 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 }
1664 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001665 RegStorage t_reg1 = AllocTemp();
1666 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001668 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1669 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001671 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001673 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001677 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 }
1679 }
1680 StoreValue(rl_dest, rl_result);
1681 return true;
1682}
1683
1684// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1685// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001686bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001687 if (lit < 0) {
1688 return false;
1689 }
1690 if (lit == 0) {
1691 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1692 LoadConstant(rl_result.reg, 0);
1693 StoreValue(rl_dest, rl_result);
1694 return true;
1695 }
1696 if (lit == 1) {
1697 rl_src = LoadValue(rl_src, kCoreReg);
1698 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1699 OpRegCopy(rl_result.reg, rl_src.reg);
1700 StoreValue(rl_dest, rl_result);
1701 return true;
1702 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001703 // There is RegRegRegShift on Arm, so check for more special cases
1704 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001705 return EasyMultiply(rl_src, rl_dest, lit);
1706 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 // Can we simplify this multiplication?
1708 bool power_of_two = false;
1709 bool pop_count_le2 = false;
1710 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001711 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001712 power_of_two = true;
1713 } else if (IsPopCountLE2(lit)) {
1714 pop_count_le2 = true;
1715 } else if (IsPowerOfTwo(lit + 1)) {
1716 power_of_two_minus_one = true;
1717 } else {
1718 return false;
1719 }
1720 rl_src = LoadValue(rl_src, kCoreReg);
1721 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1722 if (power_of_two) {
1723 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001724 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 } else if (pop_count_le2) {
1726 // Shift and add and shift.
1727 int first_bit = LowestSetBit(lit);
1728 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1729 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1730 } else {
1731 // Reverse subtract: (src << (shift + 1)) - src.
1732 DCHECK(power_of_two_minus_one);
1733 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001734 RegStorage t_reg = AllocTemp();
1735 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1736 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001737 }
1738 StoreValue(rl_dest, rl_result);
1739 return true;
1740}
1741
1742void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001743 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 RegLocation rl_result;
1745 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1746 int shift_op = false;
1747 bool is_div = false;
1748
1749 switch (opcode) {
1750 case Instruction::RSUB_INT_LIT8:
1751 case Instruction::RSUB_INT: {
1752 rl_src = LoadValue(rl_src, kCoreReg);
1753 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1754 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001755 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001757 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1758 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 }
1760 StoreValue(rl_dest, rl_result);
1761 return;
1762 }
1763
1764 case Instruction::SUB_INT:
1765 case Instruction::SUB_INT_2ADDR:
1766 lit = -lit;
1767 // Intended fallthrough
1768 case Instruction::ADD_INT:
1769 case Instruction::ADD_INT_2ADDR:
1770 case Instruction::ADD_INT_LIT8:
1771 case Instruction::ADD_INT_LIT16:
1772 op = kOpAdd;
1773 break;
1774 case Instruction::MUL_INT:
1775 case Instruction::MUL_INT_2ADDR:
1776 case Instruction::MUL_INT_LIT8:
1777 case Instruction::MUL_INT_LIT16: {
1778 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1779 return;
1780 }
1781 op = kOpMul;
1782 break;
1783 }
1784 case Instruction::AND_INT:
1785 case Instruction::AND_INT_2ADDR:
1786 case Instruction::AND_INT_LIT8:
1787 case Instruction::AND_INT_LIT16:
1788 op = kOpAnd;
1789 break;
1790 case Instruction::OR_INT:
1791 case Instruction::OR_INT_2ADDR:
1792 case Instruction::OR_INT_LIT8:
1793 case Instruction::OR_INT_LIT16:
1794 op = kOpOr;
1795 break;
1796 case Instruction::XOR_INT:
1797 case Instruction::XOR_INT_2ADDR:
1798 case Instruction::XOR_INT_LIT8:
1799 case Instruction::XOR_INT_LIT16:
1800 op = kOpXor;
1801 break;
1802 case Instruction::SHL_INT_LIT8:
1803 case Instruction::SHL_INT:
1804 case Instruction::SHL_INT_2ADDR:
1805 lit &= 31;
1806 shift_op = true;
1807 op = kOpLsl;
1808 break;
1809 case Instruction::SHR_INT_LIT8:
1810 case Instruction::SHR_INT:
1811 case Instruction::SHR_INT_2ADDR:
1812 lit &= 31;
1813 shift_op = true;
1814 op = kOpAsr;
1815 break;
1816 case Instruction::USHR_INT_LIT8:
1817 case Instruction::USHR_INT:
1818 case Instruction::USHR_INT_2ADDR:
1819 lit &= 31;
1820 shift_op = true;
1821 op = kOpLsr;
1822 break;
1823
1824 case Instruction::DIV_INT:
1825 case Instruction::DIV_INT_2ADDR:
1826 case Instruction::DIV_INT_LIT8:
1827 case Instruction::DIV_INT_LIT16:
1828 case Instruction::REM_INT:
1829 case Instruction::REM_INT_2ADDR:
1830 case Instruction::REM_INT_LIT8:
1831 case Instruction::REM_INT_LIT16: {
1832 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001833 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 return;
1835 }
buzbee11b63d12013-08-27 07:34:17 -07001836 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001838 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 (opcode == Instruction::DIV_INT_LIT16)) {
1840 is_div = true;
1841 } else {
1842 is_div = false;
1843 }
buzbee11b63d12013-08-27 07:34:17 -07001844 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1845 return;
1846 }
Dave Allison70202782013-10-22 17:52:19 -07001847
1848 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001849 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001851 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001852 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001853 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001854 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1855 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001856 } else if (cu_->instruction_set == kThumb2) {
1857 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1858 // Use ARM SDIV instruction for division. For remainder we also need to
1859 // calculate using a MUL and subtract.
1860 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001861 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001862 done = true;
1863 }
1864 }
1865
1866 if (!done) {
1867 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001868 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1869 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001870 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001871 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001872 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001873 else
1874 rl_result = GetReturnAlt();
1875 }
1876 StoreValue(rl_dest, rl_result);
1877 return;
1878 }
1879 default:
1880 LOG(FATAL) << "Unexpected opcode " << opcode;
1881 }
1882 rl_src = LoadValue(rl_src, kCoreReg);
1883 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001884 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001886 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001888 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001889 }
1890 StoreValue(rl_dest, rl_result);
1891}
1892
Andreas Gampe98430592014-07-27 19:44:50 -07001893void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
1894 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 RegLocation rl_result;
1896 OpKind first_op = kOpBkpt;
1897 OpKind second_op = kOpBkpt;
1898 bool call_out = false;
1899 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001900 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1901 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902
1903 switch (opcode) {
1904 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001905 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1906 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001908 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001909 RegStorage t_reg = AllocTemp();
1910 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1911 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1912 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1913 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001915 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1916 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001917 }
Andreas Gampe98430592014-07-27 19:44:50 -07001918 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 return;
1920 case Instruction::ADD_LONG:
1921 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001922 first_op = kOpAdd;
1923 second_op = kOpAdc;
1924 break;
1925 case Instruction::SUB_LONG:
1926 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 first_op = kOpSub;
1928 second_op = kOpSbc;
1929 break;
1930 case Instruction::MUL_LONG:
1931 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001932 call_out = true;
1933 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1934 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001935 break;
1936 case Instruction::DIV_LONG:
1937 case Instruction::DIV_LONG_2ADDR:
1938 call_out = true;
1939 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001940 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1941 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001942 break;
1943 case Instruction::REM_LONG:
1944 case Instruction::REM_LONG_2ADDR:
1945 call_out = true;
1946 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001947 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001949 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1950 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 break;
1952 case Instruction::AND_LONG_2ADDR:
1953 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 first_op = kOpAnd;
1955 second_op = kOpAnd;
1956 break;
1957 case Instruction::OR_LONG:
1958 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 first_op = kOpOr;
1960 second_op = kOpOr;
1961 break;
1962 case Instruction::XOR_LONG:
1963 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 first_op = kOpXor;
1965 second_op = kOpXor;
1966 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 default:
1968 LOG(FATAL) << "Invalid long arith op";
1969 }
1970 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07001971 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001973 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07001975 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
1976 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
1977 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1978 RegStorage r_tgt = CallHelperSetup(target);
1979 GenDivZeroCheckWide(r_tmp2);
1980 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001982 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001984 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 }
1986 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07001987 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
1988 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 else
Andreas Gampe98430592014-07-27 19:44:50 -07001990 rl_result = GetReturnWideAlt();
1991 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001992 }
1993}
1994
Mark Mendelle87f9b52014-04-30 14:13:18 -04001995void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
1996 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
1997 LoadConstantNoClobber(rl_result.reg, value);
1998 StoreValue(rl_dest, rl_result);
1999 if (value == 0) {
2000 Workaround7250540(rl_dest, rl_result.reg);
2001 }
2002}
2003
Andreas Gampe98430592014-07-27 19:44:50 -07002004void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2005 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 /*
2007 * Don't optimize the register usage since it calls out to support
2008 * functions
2009 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002012 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 if (rl_dest.wide) {
2014 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002015 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 StoreValueWide(rl_dest, rl_result);
2017 } else {
2018 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002019 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 StoreValue(rl_dest, rl_result);
2021 }
2022}
2023
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002024class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2025 public:
2026 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2027 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2028 }
2029
2030 void Compile() OVERRIDE {
2031 m2l_->ResetRegPool();
2032 m2l_->ResetDefTracking();
2033 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002034 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002035 if (cont_ != nullptr) {
2036 m2l_->OpUnconditionalBranch(cont_);
2037 }
2038 }
2039};
2040
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002042void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002043 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002044 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2045 return;
2046 }
2047 FlushAllRegs();
2048 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002049 LIR* cont = NewLIR0(kPseudoTargetLabel);
2050 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002051 } else {
2052 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2053 return;
2054 }
2055 FlushAllRegs(); // TODO: needed?
2056 LIR* inst = CheckSuspendUsingLoad();
2057 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059}
2060
2061/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002062void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002063 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002064 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2065 OpUnconditionalBranch(target);
2066 return;
2067 }
2068 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002069 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002070 LIR* branch = OpUnconditionalBranch(nullptr);
2071 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002072 } else {
2073 // For the implicit suspend check, just perform the trigger
2074 // load and branch to the target.
2075 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2076 OpUnconditionalBranch(target);
2077 return;
2078 }
2079 FlushAllRegs();
2080 LIR* inst = CheckSuspendUsingLoad();
2081 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002082 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084}
2085
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002086/* Call out to helper assembly routine that will null check obj and then lock it. */
2087void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2088 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002089 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002090}
2091
2092/* Call out to helper assembly routine that will null check obj and then unlock it. */
2093void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2094 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002095 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002096}
2097
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002098/* Generic code for generating a wide constant into a VR. */
2099void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2100 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002101 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002102 StoreValueWide(rl_dest, rl_result);
2103}
2104
Andreas Gampe48971b32014-08-06 10:09:01 -07002105void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002106 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002107 const uint16_t entries = table[1];
2108 // Chained cmp-and-branch.
2109 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
2110 int32_t current_key = as_int32[0];
2111 const int32_t* targets = &as_int32[1];
2112 rl_src = LoadValue(rl_src, kCoreReg);
2113 int i = 0;
2114 for (; i < entries; i++, current_key++) {
2115 if (!InexpensiveConstantInt(current_key, Instruction::Code::IF_EQ)) {
2116 // Switch to using a temp and add.
2117 break;
2118 }
2119 BasicBlock* case_block =
2120 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2121 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2122 }
2123 if (i < entries) {
2124 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2125 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2126 if (key_temp.Valid()) {
2127 LoadConstantNoClobber(key_temp, current_key);
2128 for (; i < entries - 1; i++, current_key++) {
2129 BasicBlock* case_block =
2130 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2131 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2132 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2133 }
2134 BasicBlock* case_block =
2135 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2136 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2137 } else {
2138 // No free temp, just finish the old loop.
2139 for (; i < entries; i++, current_key++) {
2140 BasicBlock* case_block =
2141 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2142 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2143 }
2144 }
2145 }
2146}
2147
2148void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002149 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002150 if (cu_->verbose) {
2151 DumpSparseSwitchTable(table);
2152 }
2153
2154 const uint16_t entries = table[1];
2155 if (entries <= kSmallSwitchThreshold) {
2156 GenSmallPackedSwitch(mir, table_offset, rl_src);
2157 } else {
2158 // Use the backend-specific implementation.
2159 GenLargePackedSwitch(mir, table_offset, rl_src);
2160 }
2161}
2162
2163void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002164 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002165 const uint16_t entries = table[1];
2166 // Chained cmp-and-branch.
2167 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2168 const int32_t* targets = &keys[entries];
2169 rl_src = LoadValue(rl_src, kCoreReg);
2170 for (int i = 0; i < entries; i++) {
2171 int key = keys[i];
2172 BasicBlock* case_block =
2173 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2174 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2175 }
2176}
2177
2178void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002179 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002180 if (cu_->verbose) {
2181 DumpSparseSwitchTable(table);
2182 }
2183
2184 const uint16_t entries = table[1];
2185 if (entries <= kSmallSwitchThreshold) {
2186 GenSmallSparseSwitch(mir, table_offset, rl_src);
2187 } else {
2188 // Use the backend-specific implementation.
2189 GenLargeSparseSwitch(mir, table_offset, rl_src);
2190 }
2191}
2192
Fred Shih37f05ef2014-07-16 18:38:08 -07002193bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2194 switch (size) {
2195 case kReference:
2196 return type == Primitive::kPrimNot;
2197 case k64:
2198 case kDouble:
2199 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2200 case k32:
2201 case kSingle:
2202 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2203 case kSignedHalf:
2204 return type == Primitive::kPrimShort;
2205 case kUnsignedHalf:
2206 return type == Primitive::kPrimChar;
2207 case kSignedByte:
2208 return type == Primitive::kPrimByte;
2209 case kUnsignedByte:
2210 return type == Primitive::kPrimBoolean;
2211 case kWord: // Intentional fallthrough.
2212 default:
2213 return false; // There are no sane types with this op size.
2214 }
2215}
2216
Brian Carlstrom7940e442013-07-12 13:46:57 -07002217} // namespace art