blob: 3e03750fed6403cc607fd19bf20d1fdeef18844e [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) {
buzbeea0cd2d72014-06-01 09:33:49 -0700217 DCHECK(!rl_src1.fp);
218 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 ConditionCode cond;
220 switch (opcode) {
221 case Instruction::IF_EQ:
222 cond = kCondEq;
223 break;
224 case Instruction::IF_NE:
225 cond = kCondNe;
226 break;
227 case Instruction::IF_LT:
228 cond = kCondLt;
229 break;
230 case Instruction::IF_GE:
231 cond = kCondGe;
232 break;
233 case Instruction::IF_GT:
234 cond = kCondGt;
235 break;
236 case Instruction::IF_LE:
237 cond = kCondLe;
238 break;
239 default:
240 cond = static_cast<ConditionCode>(0);
241 LOG(FATAL) << "Unexpected opcode " << opcode;
242 }
243
244 // Normalize such that if either operand is constant, src2 will be constant
245 if (rl_src1.is_const) {
246 RegLocation rl_temp = rl_src1;
247 rl_src1 = rl_src2;
248 rl_src2 = rl_temp;
249 cond = FlipComparisonOrder(cond);
250 }
251
buzbeea0cd2d72014-06-01 09:33:49 -0700252 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 // Is this really an immediate comparison?
254 if (rl_src2.is_const) {
255 // If it's already live in a register or not easily materialized, just keep going
256 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700257 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100259 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800261 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 return;
263 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700264
265 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
266 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
267 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
268 // the 32b literal 0 for null.
269 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
270 // Use the OpCmpImmBranch and ignore the value in the register.
271 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
272 return;
273 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700275
buzbeea0cd2d72014-06-01 09:33:49 -0700276 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800277 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278}
279
280void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700281 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700283 DCHECK(!rl_src.fp);
284 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 switch (opcode) {
286 case Instruction::IF_EQZ:
287 cond = kCondEq;
288 break;
289 case Instruction::IF_NEZ:
290 cond = kCondNe;
291 break;
292 case Instruction::IF_LTZ:
293 cond = kCondLt;
294 break;
295 case Instruction::IF_GEZ:
296 cond = kCondGe;
297 break;
298 case Instruction::IF_GTZ:
299 cond = kCondGt;
300 break;
301 case Instruction::IF_LEZ:
302 cond = kCondLe;
303 break;
304 default:
305 cond = static_cast<ConditionCode>(0);
306 LOG(FATAL) << "Unexpected opcode " << opcode;
307 }
buzbee2700f7e2014-03-07 09:46:20 -0800308 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309}
310
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700311void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
313 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800314 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800316 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 }
buzbee2700f7e2014-03-07 09:46:20 -0800318 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 StoreValueWide(rl_dest, rl_result);
320}
321
322void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700323 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700324 rl_src = LoadValue(rl_src, kCoreReg);
325 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
326 OpKind op = kOpInvalid;
327 switch (opcode) {
328 case Instruction::INT_TO_BYTE:
329 op = kOp2Byte;
330 break;
331 case Instruction::INT_TO_SHORT:
332 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700334 case Instruction::INT_TO_CHAR:
335 op = kOp2Char;
336 break;
337 default:
338 LOG(ERROR) << "Bad int conversion type";
339 }
buzbee2700f7e2014-03-07 09:46:20 -0800340 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700341 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342}
343
Andreas Gampe98430592014-07-27 19:44:50 -0700344/*
345 * Let helper function take care of everything. Will call
346 * Array::AllocFromCode(type_idx, method, count);
347 * Note: AllocFromCode will handle checks for errNegativeArraySize.
348 */
349void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
350 RegLocation rl_src) {
351 FlushAllRegs(); /* Everything to home location */
352 const DexFile* dex_file = cu_->dex_file;
353 CompilerDriver* driver = cu_->compiler_driver;
354 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800355 bool is_type_initialized; // Ignored as an array does not have an initializer.
356 bool use_direct_type_ptr;
357 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700358 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800359 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700360 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
361 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800362 // The fast path.
363 if (!use_direct_type_ptr) {
Andreas Gampe98430592014-07-27 19:44:50 -0700364 LoadClassType(type_idx, kArg0);
365 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
366 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800367 } else {
368 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700369 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
370 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800371 }
372 } else {
373 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700374 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800375 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700377 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 }
Andreas Gampe98430592014-07-27 19:44:50 -0700379 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380}
381
382/*
383 * Similar to GenNewArray, but with post-allocation initialization.
384 * Verifier guarantees we're dealing with an array class. Current
385 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
386 * Current code also throws internal unimp if not 'L', '[' or 'I'.
387 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700388void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 int elems = info->num_arg_words;
390 int type_idx = info->index;
391 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700392 QuickEntrypointEnum target;
393 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
394 type_idx)) {
395 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700397 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 }
Andreas Gampe98430592014-07-27 19:44:50 -0700399 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700400 FreeTemp(TargetReg(kArg2, kNotWide));
401 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 /*
403 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
404 * return region. Because AllocFromCode placed the new array
405 * in kRet0, we'll just lock it into place. When debugger support is
406 * added, it may be necessary to additionally copy all return
407 * values to a home location in thread-local storage
408 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700409 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700410 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411
412 // TODO: use the correct component size, currently all supported types
413 // share array alignment with ints (see comment at head of function)
414 size_t component_size = sizeof(int32_t);
415
416 // Having a range of 0 is legal
417 if (info->is_range && (elems > 0)) {
418 /*
419 * Bit of ugliness here. We're going generate a mem copy loop
420 * on the register range, but it is possible that some regs
421 * in the range have been promoted. This is unlikely, but
422 * before generating the copy, we'll just force a flush
423 * of any regs in the source range that have been promoted to
424 * home location.
425 */
426 for (int i = 0; i < elems; i++) {
427 RegLocation loc = UpdateLoc(info->args[i]);
428 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100429 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700430 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 }
432 }
433 /*
434 * TUNING note: generated code here could be much improved, but
435 * this is an uncommon operation and isn't especially performance
436 * critical.
437 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700438 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700439 RegStorage r_src = AllocTempRef();
440 RegStorage r_dst = AllocTempRef();
441 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800442 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700443 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700445 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700446 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 break;
448 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700449 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700450 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 r_val = AllocTemp();
452 break;
453 case kMips:
454 r_val = AllocTemp();
455 break;
456 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
457 }
458 // Set up source pointer
459 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700460 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700462 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 mirror::Array::DataOffset(component_size).Int32Value());
464 // Set up the loop counter (known to be > 0)
465 LoadConstant(r_idx, elems - 1);
466 // Generate the copy loop. Going backwards for convenience
467 LIR* target = NewLIR0(kPseudoTargetLabel);
468 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100469 {
470 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
471 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
472 // NOTE: No dalvik register annotation, local optimizations will be stopped
473 // by the loop boundaries.
474 }
buzbee695d13a2014-04-19 13:32:20 -0700475 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 FreeTemp(r_val);
477 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700478 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700480 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 -mirror::Array::DataOffset(component_size).Int32Value());
482 }
483 } else if (!info->is_range) {
484 // TUNING: interleave
485 for (int i = 0; i < elems; i++) {
486 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700487 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000488 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800490 if (IsTemp(rl_arg.reg)) {
491 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 }
493 }
494 }
495 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700496 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 }
498}
499
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800500//
501// Slow path to ensure a class is initialized for sget/sput.
502//
503class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
504 public:
buzbee2700f7e2014-03-07 09:46:20 -0800505 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
506 RegStorage r_base) :
507 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
508 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800509 }
510
511 void Compile() {
512 LIR* unresolved_target = GenerateTargetLabel();
513 uninit_->target = unresolved_target;
Andreas Gampe98430592014-07-27 19:44:50 -0700514 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800515 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700516 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800517
518 m2l_->OpUnconditionalBranch(cont_);
519 }
520
521 private:
522 LIR* const uninit_;
523 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800524 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800525};
526
Vladimir Markobe0e5462014-02-26 11:24:15 +0000527void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700528 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000529 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
530 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100531 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700532 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000533 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800534 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000535 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100537 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700538 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000539 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
540 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800541 if (IsTemp(rl_method.reg)) {
542 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 }
544 } else {
545 // Medium path, static storage base in a different class which requires checks that the other
546 // class is initialized.
547 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000548 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 // May do runtime call so everything to home locations.
550 FlushAllRegs();
551 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700552 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 LockTemp(r_method);
554 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700555 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800556 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000557 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
558 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000559 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000560 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800561 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000562 if (!field_info.IsInitialized() &&
563 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800564 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800565
566 // The slow path is invoked if the r_base is NULL or the class pointed
567 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800568 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700569 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800570 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800571 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800572 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000573 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800574 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800575
buzbee2700f7e2014-03-07 09:46:20 -0800576 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000577 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800578
579 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700580 // Ensure load of status and store of value don't re-order.
581 // TODO: Presumably the actual value store is control-dependent on the status load,
582 // and will thus not be reordered in any case, since stores are never speculated.
583 // Does later code "know" that the class is now initialized? If so, we still
584 // need the barrier to guard later static loads.
585 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 FreeTemp(r_method);
588 }
589 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100590 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100592 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100594 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000596 if (is_object) {
597 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
598 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100599 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000600 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
601 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 }
603 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800604 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800606 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 } else {
608 FlushAllRegs(); // Everything to home locations
Andreas Gampe98430592014-07-27 19:44:50 -0700609 QuickEntrypointEnum target =
610 is_long_or_double ? kQuickSet64Static
611 : (is_object ? kQuickSetObjStatic : kQuickSet32Static);
612 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 }
614}
615
Vladimir Markobe0e5462014-02-26 11:24:15 +0000616void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700617 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000618 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
619 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100620 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700621 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000622 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800623 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000624 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 // Fast path, static storage base is this method's class
626 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700627 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000628 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
629 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 } else {
631 // Medium path, static storage base in a different class which requires checks that the other
632 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000633 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 // May do runtime call so everything to home locations.
635 FlushAllRegs();
636 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700637 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 LockTemp(r_method);
639 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700640 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800641 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000642 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
643 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000644 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000645 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800646 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000647 if (!field_info.IsInitialized() &&
648 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800649 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800650
651 // The slow path is invoked if the r_base is NULL or the class pointed
652 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800653 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700654 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800655 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800656 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800657 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000658 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800659 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800660
buzbee2700f7e2014-03-07 09:46:20 -0800661 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800663
664 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700665 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700666 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 FreeTemp(r_method);
669 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800670 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100671 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
672 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800673
Vladimir Marko674744e2014-04-24 15:18:26 +0100674 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000675 if (is_object) {
676 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
677 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100678 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000679 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
680 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800681 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100682 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800683
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 if (is_long_or_double) {
685 StoreValueWide(rl_dest, rl_result);
686 } else {
687 StoreValue(rl_dest, rl_result);
688 }
689 } else {
690 FlushAllRegs(); // Everything to home locations
Andreas Gampe98430592014-07-27 19:44:50 -0700691 QuickEntrypointEnum target =
692 is_long_or_double ? kQuickGet64Static
693 : (is_object ? kQuickGetObjStatic : kQuickGet32Static);
694 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
695
Douglas Leung2db3e262014-06-25 16:02:55 -0700696 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700698 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 StoreValueWide(rl_dest, rl_result);
700 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700701 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 StoreValue(rl_dest, rl_result);
703 }
704 }
705}
706
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800707// Generate code for all slow paths.
708void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700709 // We should check slow_paths_.Size() every time, because a new slow path
710 // may be created during slowpath->Compile().
711 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800712 LIRSlowPath* slowpath = slow_paths_.Get(i);
713 slowpath->Compile();
714 }
715 slow_paths_.Reset();
716}
717
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000721 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
722 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100723 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700724 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100725 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700726 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000727 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700728 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100729 GenNullCheck(rl_obj.reg, opt_flags);
730 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
731 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000732 LIR* load_lir;
733 if (is_object) {
734 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
735 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100736 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000737 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
738 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100739 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000740 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 StoreValueWide(rl_dest, rl_result);
743 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 StoreValue(rl_dest, rl_result);
745 }
746 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700747 QuickEntrypointEnum target =
748 is_long_or_double ? kQuickGet64Instance
749 : (is_object ? kQuickGetObjInstance : kQuickGet32Instance);
750 // Second argument of pGetXXInstance is always a reference.
751 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
752 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
753
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700754 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700756 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 StoreValueWide(rl_dest, rl_result);
758 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700759 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 StoreValue(rl_dest, rl_result);
761 }
762 }
763}
764
Vladimir Markobe0e5462014-02-26 11:24:15 +0000765void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700767 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
769 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100770 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700771 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100772 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700773 // Dex code never writes to the class field.
774 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
775 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700776 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100778 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 } else {
780 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100781 }
782 GenNullCheck(rl_obj.reg, opt_flags);
783 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000784 LIR* store;
785 if (is_object) {
786 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
787 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100788 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000789 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
790 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100791 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000792 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100793 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
794 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 }
796 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700797 QuickEntrypointEnum target =
798 is_long_or_double ? kQuickSet64Instance
799 : (is_object ? kQuickSetObjInstance : kQuickSet32Instance);
800 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
801 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 }
803}
804
Ian Rogersa9a82542013-10-04 11:17:26 -0700805void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
806 RegLocation rl_src) {
807 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
808 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
809 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700810 QuickEntrypointEnum target = needs_range_check
811 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
812 : kQuickAputObjectWithBoundCheck)
813 : kQuickAputObject;
814 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700815}
816
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700817void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700819 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700820 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700821 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700823 *cu_->dex_file,
824 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 // Call out to helper which resolves type and verifies access.
826 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700827 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700828 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 StoreValue(rl_dest, rl_result);
830 } else {
831 // We're don't need access checks, load type from dex cache
832 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700833 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000834 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000835 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000836 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
838 type_idx) || SLOW_TYPE_PATH) {
839 // Slow path, at runtime test if type is null and if so initialize
840 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800841 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800842 LIR* cont = NewLIR0(kPseudoTargetLabel);
843
844 // Object to generate the slow path for class resolution.
845 class SlowPath : public LIRSlowPath {
846 public:
847 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
848 const RegLocation& rl_method, const RegLocation& rl_result) :
849 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
850 rl_method_(rl_method), rl_result_(rl_result) {
851 }
852
853 void Compile() {
854 GenerateTargetLabel();
855
Andreas Gampe98430592014-07-27 19:44:50 -0700856 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700857 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800858 m2l_->OpUnconditionalBranch(cont_);
859 }
860
861 private:
862 const int type_idx_;
863 const RegLocation rl_method_;
864 const RegLocation rl_result_;
865 };
866
867 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800868 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800869
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800871 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 // Fast path, we're done - just store result
873 StoreValue(rl_dest, rl_result);
874 }
875 }
876}
877
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700878void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000880 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
881 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
883 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
884 // slow path, resolve string if not in dex cache
885 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700886 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800887
888 // If the Method* is already in a register, we can save a copy.
889 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800890 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800891 if (rl_method.location == kLocPhysReg) {
892 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800893 DCHECK(!IsTemp(rl_method.reg));
894 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800895 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700896 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -0800897 LoadCurrMethodDirect(r_method);
898 }
buzbee695d13a2014-04-19 13:32:20 -0700899 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -0700900 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800901
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -0700903 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
904 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700905 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800906
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700907 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800908 // Object to generate the slow path for string resolution.
909 class SlowPath : public LIRSlowPath {
910 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700911 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
912 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
913 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800914 }
915
916 void Compile() {
917 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -0700918 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800919 m2l_->OpUnconditionalBranch(cont_);
920 }
921
922 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700923 const RegStorage r_method_;
924 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800925 };
926
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700927 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700929
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -0700931 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 } else {
933 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700934 RegStorage res_reg = AllocTempRef();
935 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000936 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
937 kNotVolatile);
938 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 StoreValue(rl_dest, rl_result);
940 }
941}
942
Andreas Gampe98430592014-07-27 19:44:50 -0700943/*
944 * Let helper function take care of everything. Will
945 * call Class::NewInstanceFromCode(type_idx, method);
946 */
947void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
948 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 // alloc will always check for resolution, do we also need to verify
950 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -0700951 const DexFile* dex_file = cu_->dex_file;
952 CompilerDriver* driver = cu_->compiler_driver;
953 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800954 bool is_type_initialized;
955 bool use_direct_type_ptr;
956 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700957 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800958 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700959 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
960 &direct_type_ptr, &is_finalizable) &&
961 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800962 // The fast path.
963 if (!use_direct_type_ptr) {
Andreas Gampe98430592014-07-27 19:44:50 -0700964 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800965 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -0700966 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800967 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700968 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800969 }
970 } else {
971 // Use the direct pointer.
972 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -0700973 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800974 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700975 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800976 }
977 }
978 } else {
979 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700980 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800981 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700983 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 }
Andreas Gampe98430592014-07-27 19:44:50 -0700985 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986}
987
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700988void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -0700990 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991}
992
993// For final classes there are no sub-classes to check and so we can answer the instance-of
994// question with simple comparisons.
995void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
996 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800997 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700998 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800999
buzbeea0cd2d72014-06-01 09:33:49 -07001000 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001002 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001003 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001005 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 }
1007 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001008 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009
buzbeea0cd2d72014-06-01 09:33:49 -07001010 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1011 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012
1013 LoadCurrMethodDirect(check_class);
1014 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001015 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1016 kNotVolatile);
1017 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1018 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 } else {
buzbee695d13a2014-04-19 13:32:20 -07001020 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001021 check_class, kNotVolatile);
1022 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1023 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001024 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001025 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 }
1027
buzbee695d13a2014-04-19 13:32:20 -07001028 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 if (cu_->instruction_set == kThumb2) {
1030 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001031 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001033 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001035 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 }
1037 LIR* target = NewLIR0(kPseudoTargetLabel);
1038 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 FreeTemp(object_class);
1040 FreeTemp(check_class);
1041 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001042 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 FreeTemp(result_reg);
1044 }
1045 StoreValue(rl_dest, rl_result);
1046}
1047
1048void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1049 bool type_known_abstract, bool use_declaring_class,
1050 bool can_assume_type_is_in_dex_cache,
1051 uint32_t type_idx, RegLocation rl_dest,
1052 RegLocation rl_src) {
1053 FlushAllRegs();
1054 // May generate a call - use explicit registers
1055 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001056 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001057 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001058 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001059 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1060 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 if (needs_access_check) {
1062 // Check we have access to type_idx and if not throw IllegalAccessError,
1063 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001064 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001065 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1066 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001068 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001069 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001070 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001072 if (can_assume_type_is_in_dex_cache) {
1073 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001074 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001075 }
1076
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001078 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001079 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001080 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001081 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001083 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1084 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1085
1086 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001087 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001088
1089 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1090 public:
1091 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1092 RegLocation rl_src)
1093 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1094 rl_src_(rl_src) {
1095 }
1096
1097 void Compile() OVERRIDE {
1098 GenerateTargetLabel();
1099
Andreas Gampe98430592014-07-27 19:44:50 -07001100 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001101 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1102 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001103 m2l_->OpUnconditionalBranch(cont_);
1104 }
1105
1106 private:
1107 uint32_t type_idx_;
1108 RegLocation rl_src_;
1109 };
1110
1111 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1112 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
1114 }
1115 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001116 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001117 if (!IsSameReg(rl_result.reg, ref_reg)) {
1118 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001119 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001121 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122
1123 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001124 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001126 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1127 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1129 LIR* branchover = NULL;
1130 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001131 // rl_result == ref == class.
1132 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001133 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 } else {
1135 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001136 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001137 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 if (!type_known_abstract) {
1139 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001140 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001141 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001142 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001144 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001146 if (it != nullptr) {
1147 OpEndIT(it);
1148 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 FreeTemp(r_tgt);
1150 } else {
1151 if (!type_known_abstract) {
1152 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001153 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001154 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001156
Serguei Katkov9ee45192014-07-17 14:39:03 +07001157 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001158 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160 }
1161 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001162 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 /* branch targets here */
1164 LIR* target = NewLIR0(kPseudoTargetLabel);
1165 StoreValue(rl_dest, rl_result);
1166 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001167 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 branchover->target = target;
1169 }
1170}
1171
1172void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1173 bool type_known_final, type_known_abstract, use_declaring_class;
1174 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1175 *cu_->dex_file,
1176 type_idx,
1177 &type_known_final,
1178 &type_known_abstract,
1179 &use_declaring_class);
1180 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1181 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1182
1183 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1184 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1185 } else {
1186 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1187 use_declaring_class, can_assume_type_is_in_dex_cache,
1188 type_idx, rl_dest, rl_src);
1189 }
1190}
1191
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001192void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 bool type_known_final, type_known_abstract, use_declaring_class;
1194 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1195 *cu_->dex_file,
1196 type_idx,
1197 &type_known_final,
1198 &type_known_abstract,
1199 &use_declaring_class);
1200 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1201 // of the exception throw path.
1202 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001203 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 // Verifier type analysis proved this check cast would never cause an exception.
1205 return;
1206 }
1207 FlushAllRegs();
1208 // May generate a call - use explicit registers
1209 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001210 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001211 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001212 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 if (needs_access_check) {
1214 // Check we have access to type_idx and if not throw IllegalAccessError,
1215 // returns Class* in kRet0
1216 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001217 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001218 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001220 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001221 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 } else {
1223 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001224 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001225 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001226 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001227 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1229 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001230 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1231 LIR* cont = NewLIR0(kPseudoTargetLabel);
1232
1233 // Slow path to initialize the type. Executed if the type is NULL.
1234 class SlowPath : public LIRSlowPath {
1235 public:
1236 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001237 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001238 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1239 class_reg_(class_reg) {
1240 }
1241
1242 void Compile() {
1243 GenerateTargetLabel();
1244
1245 // Call out to helper, which will return resolved type in kArg0
1246 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001247 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1248 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001249 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001250 m2l_->OpUnconditionalBranch(cont_);
1251 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001252
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001253 public:
1254 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001255 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001256 };
1257
buzbee2700f7e2014-03-07 09:46:20 -08001258 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
1260 }
1261 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001262 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001263
1264 // Slow path for the case where the classes are not equal. In this case we need
1265 // to call a helper function to do the check.
1266 class SlowPath : public LIRSlowPath {
1267 public:
1268 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1269 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1270 }
1271
1272 void Compile() {
1273 GenerateTargetLabel();
1274
1275 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001276 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1277 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001278 }
Andreas Gampe98430592014-07-27 19:44:50 -07001279 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1280 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001281 m2l_->OpUnconditionalBranch(cont_);
1282 }
1283
1284 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001285 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001286 };
1287
1288 if (type_known_abstract) {
1289 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001290 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001291 LIR* cont = NewLIR0(kPseudoTargetLabel);
1292 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1293 } else {
1294 // Harder, more common case. We need to generate a forward branch over the load
1295 // if the target is null. If it's non-null we perform the load and branch to the
1296 // slow path if the classes are not equal.
1297
1298 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001299 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001300 /* load object->klass_ */
1301 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001302 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1303 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001304
Andreas Gampeccc60262014-07-04 18:02:38 -07001305 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001306 LIR* cont = NewLIR0(kPseudoTargetLabel);
1307
1308 // Add the slow path that will not perform load since this is already done.
1309 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1310
1311 // Set the null check to branch to the continuation.
1312 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 }
1314}
1315
1316void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001317 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 RegLocation rl_result;
1319 if (cu_->instruction_set == kThumb2) {
1320 /*
1321 * NOTE: This is the one place in the code in which we might have
1322 * as many as six live temporary registers. There are 5 in the normal
1323 * set for Arm. Until we have spill capabilities, temporarily add
1324 * lr to the temp set. It is safe to do this locally, but note that
1325 * lr is used explicitly elsewhere in the code generator and cannot
1326 * normally be used as a general temp register.
1327 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001328 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1329 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 }
1331 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1332 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1333 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1334 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001335 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1336 RegStorage t_reg = AllocTemp();
1337 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1338 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1339 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 FreeTemp(t_reg);
1341 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001342 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1343 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 }
1345 /*
1346 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1347 * following StoreValueWide might need to allocate a temp register.
1348 * To further work around the lack of a spill capability, explicitly
1349 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1350 * Remove when spill is functional.
1351 */
1352 FreeRegLocTemps(rl_result, rl_src1);
1353 FreeRegLocTemps(rl_result, rl_src2);
1354 StoreValueWide(rl_dest, rl_result);
1355 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001356 Clobber(TargetReg(kLr, kNotWide));
1357 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 }
1359}
1360
Andreas Gampe98430592014-07-27 19:44:50 -07001361void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1362 RegLocation rl_src1, RegLocation rl_shift) {
1363 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 switch (opcode) {
1365 case Instruction::SHL_LONG:
1366 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001367 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 break;
1369 case Instruction::SHR_LONG:
1370 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001371 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 break;
1373 case Instruction::USHR_LONG:
1374 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001375 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 break;
1377 default:
1378 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001379 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 }
Andreas Gampe98430592014-07-27 19:44:50 -07001381 FlushAllRegs(); /* Send everything to home location */
1382 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001383 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 StoreValueWide(rl_dest, rl_result);
1385}
1386
1387
1388void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001389 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001390 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 OpKind op = kOpBkpt;
1392 bool is_div_rem = false;
1393 bool check_zero = false;
1394 bool unary = false;
1395 RegLocation rl_result;
1396 bool shift_op = false;
1397 switch (opcode) {
1398 case Instruction::NEG_INT:
1399 op = kOpNeg;
1400 unary = true;
1401 break;
1402 case Instruction::NOT_INT:
1403 op = kOpMvn;
1404 unary = true;
1405 break;
1406 case Instruction::ADD_INT:
1407 case Instruction::ADD_INT_2ADDR:
1408 op = kOpAdd;
1409 break;
1410 case Instruction::SUB_INT:
1411 case Instruction::SUB_INT_2ADDR:
1412 op = kOpSub;
1413 break;
1414 case Instruction::MUL_INT:
1415 case Instruction::MUL_INT_2ADDR:
1416 op = kOpMul;
1417 break;
1418 case Instruction::DIV_INT:
1419 case Instruction::DIV_INT_2ADDR:
1420 check_zero = true;
1421 op = kOpDiv;
1422 is_div_rem = true;
1423 break;
1424 /* NOTE: returns in kArg1 */
1425 case Instruction::REM_INT:
1426 case Instruction::REM_INT_2ADDR:
1427 check_zero = true;
1428 op = kOpRem;
1429 is_div_rem = true;
1430 break;
1431 case Instruction::AND_INT:
1432 case Instruction::AND_INT_2ADDR:
1433 op = kOpAnd;
1434 break;
1435 case Instruction::OR_INT:
1436 case Instruction::OR_INT_2ADDR:
1437 op = kOpOr;
1438 break;
1439 case Instruction::XOR_INT:
1440 case Instruction::XOR_INT_2ADDR:
1441 op = kOpXor;
1442 break;
1443 case Instruction::SHL_INT:
1444 case Instruction::SHL_INT_2ADDR:
1445 shift_op = true;
1446 op = kOpLsl;
1447 break;
1448 case Instruction::SHR_INT:
1449 case Instruction::SHR_INT_2ADDR:
1450 shift_op = true;
1451 op = kOpAsr;
1452 break;
1453 case Instruction::USHR_INT:
1454 case Instruction::USHR_INT_2ADDR:
1455 shift_op = true;
1456 op = kOpLsr;
1457 break;
1458 default:
1459 LOG(FATAL) << "Invalid word arith op: " << opcode;
1460 }
1461 if (!is_div_rem) {
1462 if (unary) {
1463 rl_src1 = LoadValue(rl_src1, kCoreReg);
1464 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001465 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001467 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001468 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001469 RegStorage t_reg = AllocTemp();
1470 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 rl_src1 = LoadValue(rl_src1, kCoreReg);
1472 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001473 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 FreeTemp(t_reg);
1475 } else {
1476 rl_src1 = LoadValue(rl_src1, kCoreReg);
1477 rl_src2 = LoadValue(rl_src2, kCoreReg);
1478 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001479 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 }
1481 }
1482 StoreValue(rl_dest, rl_result);
1483 } else {
Dave Allison70202782013-10-22 17:52:19 -07001484 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 +01001485 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 rl_src1 = LoadValue(rl_src1, kCoreReg);
1487 rl_src2 = LoadValue(rl_src2, kCoreReg);
1488 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001489 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 }
buzbee2700f7e2014-03-07 09:46:20 -08001491 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001492 done = true;
1493 } else if (cu_->instruction_set == kThumb2) {
1494 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1495 // Use ARM SDIV instruction for division. For remainder we also need to
1496 // calculate using a MUL and subtract.
1497 rl_src1 = LoadValue(rl_src1, kCoreReg);
1498 rl_src2 = LoadValue(rl_src2, kCoreReg);
1499 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001500 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001501 }
buzbee2700f7e2014-03-07 09:46:20 -08001502 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001503 done = true;
1504 }
1505 }
1506
1507 // If we haven't already generated the code use the callout function.
1508 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001510 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001511 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001512 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001514 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
Dave Allison70202782013-10-22 17:52:19 -07001516 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001517 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001519 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 else
1521 rl_result = GetReturnAlt();
1522 }
1523 StoreValue(rl_dest, rl_result);
1524 }
1525}
1526
1527/*
1528 * The following are the first-level codegen routines that analyze the format
1529 * of each bytecode then either dispatch special purpose codegen routines
1530 * or produce corresponding Thumb instructions directly.
1531 */
1532
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001534static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 x &= x - 1;
1536 return (x & (x - 1)) == 0;
1537}
1538
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1540// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001541bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001542 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1544 return false;
1545 }
1546 // No divide instruction for Arm, so check for more special cases
1547 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001548 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
1550 int k = LowestSetBit(lit);
1551 if (k >= 30) {
1552 // Avoid special cases.
1553 return false;
1554 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 rl_src = LoadValue(rl_src, kCoreReg);
1556 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001557 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001558 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 if (lit == 2) {
1560 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001561 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1562 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1563 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001565 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001567 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1568 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 }
1570 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001571 RegStorage t_reg1 = AllocTemp();
1572 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001574 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1575 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001577 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001579 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001581 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001583 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
1585 }
1586 StoreValue(rl_dest, rl_result);
1587 return true;
1588}
1589
1590// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1591// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001592bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001593 if (lit < 0) {
1594 return false;
1595 }
1596 if (lit == 0) {
1597 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1598 LoadConstant(rl_result.reg, 0);
1599 StoreValue(rl_dest, rl_result);
1600 return true;
1601 }
1602 if (lit == 1) {
1603 rl_src = LoadValue(rl_src, kCoreReg);
1604 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1605 OpRegCopy(rl_result.reg, rl_src.reg);
1606 StoreValue(rl_dest, rl_result);
1607 return true;
1608 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001609 // There is RegRegRegShift on Arm, so check for more special cases
1610 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001611 return EasyMultiply(rl_src, rl_dest, lit);
1612 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 // Can we simplify this multiplication?
1614 bool power_of_two = false;
1615 bool pop_count_le2 = false;
1616 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001617 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 power_of_two = true;
1619 } else if (IsPopCountLE2(lit)) {
1620 pop_count_le2 = true;
1621 } else if (IsPowerOfTwo(lit + 1)) {
1622 power_of_two_minus_one = true;
1623 } else {
1624 return false;
1625 }
1626 rl_src = LoadValue(rl_src, kCoreReg);
1627 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1628 if (power_of_two) {
1629 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001630 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 } else if (pop_count_le2) {
1632 // Shift and add and shift.
1633 int first_bit = LowestSetBit(lit);
1634 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1635 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1636 } else {
1637 // Reverse subtract: (src << (shift + 1)) - src.
1638 DCHECK(power_of_two_minus_one);
1639 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001640 RegStorage t_reg = AllocTemp();
1641 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1642 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 }
1644 StoreValue(rl_dest, rl_result);
1645 return true;
1646}
1647
1648void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001649 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 RegLocation rl_result;
1651 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1652 int shift_op = false;
1653 bool is_div = false;
1654
1655 switch (opcode) {
1656 case Instruction::RSUB_INT_LIT8:
1657 case Instruction::RSUB_INT: {
1658 rl_src = LoadValue(rl_src, kCoreReg);
1659 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1660 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001663 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1664 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 }
1666 StoreValue(rl_dest, rl_result);
1667 return;
1668 }
1669
1670 case Instruction::SUB_INT:
1671 case Instruction::SUB_INT_2ADDR:
1672 lit = -lit;
1673 // Intended fallthrough
1674 case Instruction::ADD_INT:
1675 case Instruction::ADD_INT_2ADDR:
1676 case Instruction::ADD_INT_LIT8:
1677 case Instruction::ADD_INT_LIT16:
1678 op = kOpAdd;
1679 break;
1680 case Instruction::MUL_INT:
1681 case Instruction::MUL_INT_2ADDR:
1682 case Instruction::MUL_INT_LIT8:
1683 case Instruction::MUL_INT_LIT16: {
1684 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1685 return;
1686 }
1687 op = kOpMul;
1688 break;
1689 }
1690 case Instruction::AND_INT:
1691 case Instruction::AND_INT_2ADDR:
1692 case Instruction::AND_INT_LIT8:
1693 case Instruction::AND_INT_LIT16:
1694 op = kOpAnd;
1695 break;
1696 case Instruction::OR_INT:
1697 case Instruction::OR_INT_2ADDR:
1698 case Instruction::OR_INT_LIT8:
1699 case Instruction::OR_INT_LIT16:
1700 op = kOpOr;
1701 break;
1702 case Instruction::XOR_INT:
1703 case Instruction::XOR_INT_2ADDR:
1704 case Instruction::XOR_INT_LIT8:
1705 case Instruction::XOR_INT_LIT16:
1706 op = kOpXor;
1707 break;
1708 case Instruction::SHL_INT_LIT8:
1709 case Instruction::SHL_INT:
1710 case Instruction::SHL_INT_2ADDR:
1711 lit &= 31;
1712 shift_op = true;
1713 op = kOpLsl;
1714 break;
1715 case Instruction::SHR_INT_LIT8:
1716 case Instruction::SHR_INT:
1717 case Instruction::SHR_INT_2ADDR:
1718 lit &= 31;
1719 shift_op = true;
1720 op = kOpAsr;
1721 break;
1722 case Instruction::USHR_INT_LIT8:
1723 case Instruction::USHR_INT:
1724 case Instruction::USHR_INT_2ADDR:
1725 lit &= 31;
1726 shift_op = true;
1727 op = kOpLsr;
1728 break;
1729
1730 case Instruction::DIV_INT:
1731 case Instruction::DIV_INT_2ADDR:
1732 case Instruction::DIV_INT_LIT8:
1733 case Instruction::DIV_INT_LIT16:
1734 case Instruction::REM_INT:
1735 case Instruction::REM_INT_2ADDR:
1736 case Instruction::REM_INT_LIT8:
1737 case Instruction::REM_INT_LIT16: {
1738 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001739 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 return;
1741 }
buzbee11b63d12013-08-27 07:34:17 -07001742 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001744 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 (opcode == Instruction::DIV_INT_LIT16)) {
1746 is_div = true;
1747 } else {
1748 is_div = false;
1749 }
buzbee11b63d12013-08-27 07:34:17 -07001750 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1751 return;
1752 }
Dave Allison70202782013-10-22 17:52:19 -07001753
1754 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001755 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001757 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001758 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001759 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001760 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1761 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001762 } else if (cu_->instruction_set == kThumb2) {
1763 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1764 // Use ARM SDIV instruction for division. For remainder we also need to
1765 // calculate using a MUL and subtract.
1766 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001767 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001768 done = true;
1769 }
1770 }
1771
1772 if (!done) {
1773 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001774 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1775 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001776 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001778 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 else
1780 rl_result = GetReturnAlt();
1781 }
1782 StoreValue(rl_dest, rl_result);
1783 return;
1784 }
1785 default:
1786 LOG(FATAL) << "Unexpected opcode " << opcode;
1787 }
1788 rl_src = LoadValue(rl_src, kCoreReg);
1789 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001790 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001791 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001792 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001794 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 }
1796 StoreValue(rl_dest, rl_result);
1797}
1798
Andreas Gampe98430592014-07-27 19:44:50 -07001799void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
1800 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 RegLocation rl_result;
1802 OpKind first_op = kOpBkpt;
1803 OpKind second_op = kOpBkpt;
1804 bool call_out = false;
1805 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001806 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1807 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001808
1809 switch (opcode) {
1810 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001811 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
1812 GenNotLong(rl_dest, rl_src2);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001813 return;
1814 }
Andreas Gampe98430592014-07-27 19:44:50 -07001815 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1816 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001818 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001819 RegStorage t_reg = AllocTemp();
1820 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1821 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1822 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1823 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001824 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001825 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1826 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 }
Andreas Gampe98430592014-07-27 19:44:50 -07001828 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 return;
1830 case Instruction::ADD_LONG:
1831 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001832 if (cu_->instruction_set != kThumb2) {
1833 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 return;
1835 }
1836 first_op = kOpAdd;
1837 second_op = kOpAdc;
1838 break;
1839 case Instruction::SUB_LONG:
1840 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001841 if (cu_->instruction_set != kThumb2) {
1842 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 return;
1844 }
1845 first_op = kOpSub;
1846 second_op = kOpSbc;
1847 break;
1848 case Instruction::MUL_LONG:
1849 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001850 if (cu_->instruction_set != kMips) {
1851 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 return;
1853 } else {
1854 call_out = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001855 TargetReg(kRet0, kNotWide).GetReg();
1856 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001857 }
1858 break;
1859 case Instruction::DIV_LONG:
1860 case Instruction::DIV_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001861 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
1862 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001863 return;
1864 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001865 call_out = true;
1866 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001867 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1868 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001869 break;
1870 case Instruction::REM_LONG:
1871 case Instruction::REM_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001872 if (cu_->instruction_set == kArm64 || cu_->instruction_set == kX86_64) {
1873 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001874 return;
1875 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 call_out = true;
1877 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001878 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001880 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1881 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001882 break;
1883 case Instruction::AND_LONG_2ADDR:
1884 case Instruction::AND_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001885 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 ||
1886 cu_->instruction_set == kArm64) {
1887 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 }
1889 first_op = kOpAnd;
1890 second_op = kOpAnd;
1891 break;
1892 case Instruction::OR_LONG:
1893 case Instruction::OR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001894 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 ||
1895 cu_->instruction_set == kArm64) {
1896 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 return;
1898 }
1899 first_op = kOpOr;
1900 second_op = kOpOr;
1901 break;
1902 case Instruction::XOR_LONG:
1903 case Instruction::XOR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001904 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64 ||
1905 cu_->instruction_set == kArm64) {
1906 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 return;
1908 }
1909 first_op = kOpXor;
1910 second_op = kOpXor;
1911 break;
1912 case Instruction::NEG_LONG: {
Andreas Gampe98430592014-07-27 19:44:50 -07001913 GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 return;
1915 }
1916 default:
1917 LOG(FATAL) << "Invalid long arith op";
1918 }
1919 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07001920 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001922 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07001924 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
1925 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
1926 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1927 RegStorage r_tgt = CallHelperSetup(target);
1928 GenDivZeroCheckWide(r_tmp2);
1929 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001930 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001931 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001932 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001933 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 }
1935 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07001936 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
1937 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 else
Andreas Gampe98430592014-07-27 19:44:50 -07001939 rl_result = GetReturnWideAlt();
1940 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001941 }
1942}
1943
Mark Mendelle87f9b52014-04-30 14:13:18 -04001944void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
1945 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
1946 LoadConstantNoClobber(rl_result.reg, value);
1947 StoreValue(rl_dest, rl_result);
1948 if (value == 0) {
1949 Workaround7250540(rl_dest, rl_result.reg);
1950 }
1951}
1952
Andreas Gampe98430592014-07-27 19:44:50 -07001953void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
1954 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 /*
1956 * Don't optimize the register usage since it calls out to support
1957 * functions
1958 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07001959
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07001961 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001962 if (rl_dest.wide) {
1963 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07001964 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965 StoreValueWide(rl_dest, rl_result);
1966 } else {
1967 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07001968 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 StoreValue(rl_dest, rl_result);
1970 }
1971}
1972
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001973class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
1974 public:
1975 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
1976 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
1977 }
1978
1979 void Compile() OVERRIDE {
1980 m2l_->ResetRegPool();
1981 m2l_->ResetDefTracking();
1982 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07001983 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001984 if (cont_ != nullptr) {
1985 m2l_->OpUnconditionalBranch(cont_);
1986 }
1987 }
1988};
1989
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001991void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00001992 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08001993 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1994 return;
1995 }
1996 FlushAllRegs();
1997 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001998 LIR* cont = NewLIR0(kPseudoTargetLabel);
1999 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002000 } else {
2001 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2002 return;
2003 }
2004 FlushAllRegs(); // TODO: needed?
2005 LIR* inst = CheckSuspendUsingLoad();
2006 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002007 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008}
2009
2010/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002011void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002012 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002013 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2014 OpUnconditionalBranch(target);
2015 return;
2016 }
2017 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002018 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002019 LIR* branch = OpUnconditionalBranch(nullptr);
2020 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002021 } else {
2022 // For the implicit suspend check, just perform the trigger
2023 // load and branch to the target.
2024 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2025 OpUnconditionalBranch(target);
2026 return;
2027 }
2028 FlushAllRegs();
2029 LIR* inst = CheckSuspendUsingLoad();
2030 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033}
2034
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002035/* Call out to helper assembly routine that will null check obj and then lock it. */
2036void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2037 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002038 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002039}
2040
2041/* Call out to helper assembly routine that will null check obj and then unlock it. */
2042void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2043 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002044 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002045}
2046
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002047/* Generic code for generating a wide constant into a VR. */
2048void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2049 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002050 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002051 StoreValueWide(rl_dest, rl_result);
2052}
2053
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054} // namespace art