blob: b2af29833b0175b0fe9ff3786f333d9d8b565bb5 [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) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700364 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700365 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
Fred Shih37f05ef2014-07-16 18:38:08 -0700527void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000528 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
529 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700530 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000531 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800532 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000533 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100535 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700536 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000537 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
538 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800539 if (IsTemp(rl_method.reg)) {
540 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 }
542 } else {
543 // Medium path, static storage base in a different class which requires checks that the other
544 // class is initialized.
545 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000546 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 // May do runtime call so everything to home locations.
548 FlushAllRegs();
549 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700550 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 LockTemp(r_method);
552 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700553 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800554 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000555 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
556 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000557 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000558 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800559 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000560 if (!field_info.IsInitialized() &&
561 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800562 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800563
564 // The slow path is invoked if the r_base is NULL or the class pointed
565 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800566 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700567 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800568 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800569 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800570 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000571 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800572 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800573
buzbee2700f7e2014-03-07 09:46:20 -0800574 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000575 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800576
577 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700578 // Ensure load of status and store of value don't re-order.
579 // TODO: Presumably the actual value store is control-dependent on the status load,
580 // and will thus not be reordered in any case, since stores are never speculated.
581 // Does later code "know" that the class is now initialized? If so, we still
582 // need the barrier to guard later static loads.
583 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 FreeTemp(r_method);
586 }
587 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700588 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
589 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100590 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100592 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700594 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000595 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
596 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100597 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700598 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000599 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700601 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800602 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 } else {
606 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700607 QuickEntrypointEnum target;
608 switch (size) {
609 case kReference:
610 target = kQuickSetObjStatic;
611 break;
612 case k64:
613 case kDouble:
614 target = kQuickSet64Static;
615 break;
616 case k32:
617 case kSingle:
618 target = kQuickSet32Static;
619 break;
620 case kSignedHalf:
621 case kUnsignedHalf:
622 target = kQuickSet16Static;
623 break;
624 case kSignedByte:
625 case kUnsignedByte:
626 target = kQuickSet8Static;
627 break;
628 case kWord: // Intentional fallthrough.
629 default:
630 LOG(FATAL) << "Can't determine entrypoint for: " << size;
631 target = kQuickSet32Static;
632 }
Andreas Gampe98430592014-07-27 19:44:50 -0700633 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 }
635}
636
Fred Shih37f05ef2014-07-16 18:38:08 -0700637void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000638 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
639 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700640
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700641 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000642 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800643 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000644 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 // Fast path, static storage base is this method's class
646 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700647 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000648 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
649 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 } else {
651 // Medium path, static storage base in a different class which requires checks that the other
652 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000653 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 // May do runtime call so everything to home locations.
655 FlushAllRegs();
656 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700657 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 LockTemp(r_method);
659 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700660 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800661 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000662 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
663 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000664 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000665 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800666 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000667 if (!field_info.IsInitialized() &&
668 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800669 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800670
671 // The slow path is invoked if the r_base is NULL or the class pointed
672 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800673 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700674 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800675 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800676 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800677 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000678 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800679 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800680
buzbee2700f7e2014-03-07 09:46:20 -0800681 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000682 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800683
684 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700685 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700686 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 FreeTemp(r_method);
689 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800690 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700691 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100692 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800693
Vladimir Marko674744e2014-04-24 15:18:26 +0100694 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700695 if (IsRef(size)) {
696 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000697 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
698 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100699 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700700 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000701 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800702 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100703 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800704
Fred Shih37f05ef2014-07-16 18:38:08 -0700705 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 StoreValueWide(rl_dest, rl_result);
707 } else {
708 StoreValue(rl_dest, rl_result);
709 }
710 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700711 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700713 QuickEntrypointEnum target;
714 switch (type) {
715 case Primitive::kPrimNot:
716 target = kQuickGetObjStatic;
717 break;
718 case Primitive::kPrimLong:
719 case Primitive::kPrimDouble:
720 target = kQuickGet64Static;
721 break;
722 case Primitive::kPrimInt:
723 case Primitive::kPrimFloat:
724 target = kQuickGet32Static;
725 break;
726 case Primitive::kPrimShort:
727 target = kQuickGetShortStatic;
728 break;
729 case Primitive::kPrimChar:
730 target = kQuickGetCharStatic;
731 break;
732 case Primitive::kPrimByte:
733 target = kQuickGetByteStatic;
734 break;
735 case Primitive::kPrimBoolean:
736 target = kQuickGetBooleanStatic;
737 break;
738 case Primitive::kPrimVoid: // Intentional fallthrough.
739 default:
740 LOG(FATAL) << "Can't determine entrypoint for: " << type;
741 target = kQuickGet32Static;
742 }
Andreas Gampe98430592014-07-27 19:44:50 -0700743 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
744
Douglas Leung2db3e262014-06-25 16:02:55 -0700745 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700746 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700747 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 StoreValueWide(rl_dest, rl_result);
749 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700750 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 StoreValue(rl_dest, rl_result);
752 }
753 }
754}
755
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800756// Generate code for all slow paths.
757void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700758 // We should check slow_paths_.Size() every time, because a new slow path
759 // may be created during slowpath->Compile().
760 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800761 LIRSlowPath* slowpath = slow_paths_.Get(i);
762 slowpath->Compile();
763 }
764 slow_paths_.Reset();
765}
766
Fred Shih37f05ef2014-07-16 18:38:08 -0700767void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
768 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000769 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
770 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700771 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700772 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700773 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700775 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100776 GenNullCheck(rl_obj.reg, opt_flags);
777 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
778 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000779 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700780 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000781 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
782 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100783 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700784 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000785 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100786 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000787 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700788 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 StoreValueWide(rl_dest, rl_result);
790 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 StoreValue(rl_dest, rl_result);
792 }
793 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700794 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
795 QuickEntrypointEnum target;
796 switch (type) {
797 case Primitive::kPrimNot:
798 target = kQuickGetObjInstance;
799 break;
800 case Primitive::kPrimLong:
801 case Primitive::kPrimDouble:
802 target = kQuickGet64Instance;
803 break;
804 case Primitive::kPrimFloat:
805 case Primitive::kPrimInt:
806 target = kQuickGet32Instance;
807 break;
808 case Primitive::kPrimShort:
809 target = kQuickGetShortInstance;
810 break;
811 case Primitive::kPrimChar:
812 target = kQuickGetCharInstance;
813 break;
814 case Primitive::kPrimByte:
815 target = kQuickGetByteInstance;
816 break;
817 case Primitive::kPrimBoolean:
818 target = kQuickGetBooleanInstance;
819 break;
820 case Primitive::kPrimVoid: // Intentional fallthrough.
821 default:
822 LOG(FATAL) << "Can't determine entrypoint for: " << type;
823 target = kQuickGet32Instance;
824 }
Andreas Gampe98430592014-07-27 19:44:50 -0700825 // Second argument of pGetXXInstance is always a reference.
826 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
827 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
828
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700829 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700830 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700831 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValueWide(rl_dest, rl_result);
833 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700834 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 StoreValue(rl_dest, rl_result);
836 }
837 }
838}
839
Vladimir Markobe0e5462014-02-26 11:24:15 +0000840void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700841 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000842 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
843 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700844 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700845 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700846 // Dex code never writes to the class field.
847 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
848 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700849 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700850 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100851 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 } else {
853 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100854 }
855 GenNullCheck(rl_obj.reg, opt_flags);
856 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000857 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700858 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000859 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
860 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100861 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700862 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000863 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100864 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000865 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700866 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100867 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
869 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700870 QuickEntrypointEnum target;
871 switch (size) {
872 case kReference:
873 target = kQuickSetObjInstance;
874 break;
875 case k64:
876 case kDouble:
877 target = kQuickSet64Instance;
878 break;
879 case k32:
880 case kSingle:
881 target = kQuickSet32Instance;
882 break;
883 case kSignedHalf:
884 case kUnsignedHalf:
885 target = kQuickSet16Instance;
886 break;
887 case kSignedByte:
888 case kUnsignedByte:
889 target = kQuickSet8Instance;
890 break;
891 case kWord: // Intentional fallthrough.
892 default:
893 LOG(FATAL) << "Can't determine entrypoint for: " << size;
894 target = kQuickSet32Instance;
895 }
Andreas Gampe98430592014-07-27 19:44:50 -0700896 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
897 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 }
899}
900
Ian Rogersa9a82542013-10-04 11:17:26 -0700901void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
902 RegLocation rl_src) {
903 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
904 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
905 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700906 QuickEntrypointEnum target = needs_range_check
907 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
908 : kQuickAputObjectWithBoundCheck)
909 : kQuickAputObject;
910 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700911}
912
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700913void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700915 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700916 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700917 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700919 *cu_->dex_file,
920 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 // Call out to helper which resolves type and verifies access.
922 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700923 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700924 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925 StoreValue(rl_dest, rl_result);
926 } else {
927 // We're don't need access checks, load type from dex cache
928 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700929 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000930 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000931 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000932 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
934 type_idx) || SLOW_TYPE_PATH) {
935 // Slow path, at runtime test if type is null and if so initialize
936 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800937 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800938 LIR* cont = NewLIR0(kPseudoTargetLabel);
939
940 // Object to generate the slow path for class resolution.
941 class SlowPath : public LIRSlowPath {
942 public:
943 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
944 const RegLocation& rl_method, const RegLocation& rl_result) :
945 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
946 rl_method_(rl_method), rl_result_(rl_result) {
947 }
948
949 void Compile() {
950 GenerateTargetLabel();
951
Andreas Gampe98430592014-07-27 19:44:50 -0700952 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700953 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800954 m2l_->OpUnconditionalBranch(cont_);
955 }
956
957 private:
958 const int type_idx_;
959 const RegLocation rl_method_;
960 const RegLocation rl_result_;
961 };
962
963 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800964 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800967 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 // Fast path, we're done - just store result
969 StoreValue(rl_dest, rl_result);
970 }
971 }
972}
973
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700974void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000976 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
977 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
979 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
980 // slow path, resolve string if not in dex cache
981 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700982 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800983
984 // If the Method* is already in a register, we can save a copy.
985 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800986 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800987 if (rl_method.location == kLocPhysReg) {
988 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800989 DCHECK(!IsTemp(rl_method.reg));
990 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800991 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700992 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -0800993 LoadCurrMethodDirect(r_method);
994 }
buzbee695d13a2014-04-19 13:32:20 -0700995 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -0700996 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800997
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -0700999 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1000 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001001 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001002
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001003 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001004 // Object to generate the slow path for string resolution.
1005 class SlowPath : public LIRSlowPath {
1006 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001007 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1008 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1009 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001010 }
1011
1012 void Compile() {
1013 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001014 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001015 m2l_->OpUnconditionalBranch(cont_);
1016 }
1017
1018 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001019 const RegStorage r_method_;
1020 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001021 };
1022
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001023 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001025
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001027 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 } else {
1029 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001030 RegStorage res_reg = AllocTempRef();
1031 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001032 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1033 kNotVolatile);
1034 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 StoreValue(rl_dest, rl_result);
1036 }
1037}
1038
Andreas Gampe98430592014-07-27 19:44:50 -07001039/*
1040 * Let helper function take care of everything. Will
1041 * call Class::NewInstanceFromCode(type_idx, method);
1042 */
1043void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1044 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 // alloc will always check for resolution, do we also need to verify
1046 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001047 const DexFile* dex_file = cu_->dex_file;
1048 CompilerDriver* driver = cu_->compiler_driver;
1049 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001050 bool is_type_initialized;
1051 bool use_direct_type_ptr;
1052 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001053 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001054 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001055 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1056 &direct_type_ptr, &is_finalizable) &&
1057 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001058 // The fast path.
1059 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001060 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001062 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001063 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001064 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 }
1066 } else {
1067 // Use the direct pointer.
1068 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001069 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001071 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 }
1073 }
1074 } else {
1075 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001076 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001077 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001079 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
Andreas Gampe98430592014-07-27 19:44:50 -07001081 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082}
1083
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001084void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001086 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087}
1088
1089// For final classes there are no sub-classes to check and so we can answer the instance-of
1090// question with simple comparisons.
1091void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1092 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001093 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001094 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001095
buzbeea0cd2d72014-06-01 09:33:49 -07001096 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001098 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001099 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001101 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 }
1103 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001104 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105
buzbeea0cd2d72014-06-01 09:33:49 -07001106 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1107 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108
1109 LoadCurrMethodDirect(check_class);
1110 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001111 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1112 kNotVolatile);
1113 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1114 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 } else {
buzbee695d13a2014-04-19 13:32:20 -07001116 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001117 check_class, kNotVolatile);
1118 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1119 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001120 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001121 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 }
1123
buzbee695d13a2014-04-19 13:32:20 -07001124 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 if (cu_->instruction_set == kThumb2) {
1126 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001127 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001129 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001131 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 }
1133 LIR* target = NewLIR0(kPseudoTargetLabel);
1134 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 FreeTemp(object_class);
1136 FreeTemp(check_class);
1137 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001138 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 FreeTemp(result_reg);
1140 }
1141 StoreValue(rl_dest, rl_result);
1142}
1143
1144void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1145 bool type_known_abstract, bool use_declaring_class,
1146 bool can_assume_type_is_in_dex_cache,
1147 uint32_t type_idx, RegLocation rl_dest,
1148 RegLocation rl_src) {
1149 FlushAllRegs();
1150 // May generate a call - use explicit registers
1151 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001152 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001153 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001154 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001155 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1156 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 if (needs_access_check) {
1158 // Check we have access to type_idx and if not throw IllegalAccessError,
1159 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001160 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001161 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1162 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001164 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001165 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001166 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001168 if (can_assume_type_is_in_dex_cache) {
1169 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001170 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001171 }
1172
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001174 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001175 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001176 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001177 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001179 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1180 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1181
1182 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001183 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001184
1185 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1186 public:
1187 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1188 RegLocation rl_src)
1189 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1190 rl_src_(rl_src) {
1191 }
1192
1193 void Compile() OVERRIDE {
1194 GenerateTargetLabel();
1195
Andreas Gampe98430592014-07-27 19:44:50 -07001196 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001197 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1198 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001199 m2l_->OpUnconditionalBranch(cont_);
1200 }
1201
1202 private:
1203 uint32_t type_idx_;
1204 RegLocation rl_src_;
1205 };
1206
1207 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1208 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 }
1210 }
1211 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001212 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001213 if (!IsSameReg(rl_result.reg, ref_reg)) {
1214 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001215 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001217 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218
1219 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001220 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001222 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1223 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1225 LIR* branchover = NULL;
1226 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001227 // rl_result == ref == class.
1228 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001229 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 } else {
1231 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001232 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001233 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 if (!type_known_abstract) {
1235 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001236 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001237 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001238 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001240 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001242 if (it != nullptr) {
1243 OpEndIT(it);
1244 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 FreeTemp(r_tgt);
1246 } else {
1247 if (!type_known_abstract) {
1248 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001249 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001250 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001252
Serguei Katkov9ee45192014-07-17 14:39:03 +07001253 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001254 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
1256 }
1257 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001258 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 /* branch targets here */
1260 LIR* target = NewLIR0(kPseudoTargetLabel);
1261 StoreValue(rl_dest, rl_result);
1262 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001263 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 branchover->target = target;
1265 }
1266}
1267
1268void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1269 bool type_known_final, type_known_abstract, use_declaring_class;
1270 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1271 *cu_->dex_file,
1272 type_idx,
1273 &type_known_final,
1274 &type_known_abstract,
1275 &use_declaring_class);
1276 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1277 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1278
1279 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1280 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1281 } else {
1282 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1283 use_declaring_class, can_assume_type_is_in_dex_cache,
1284 type_idx, rl_dest, rl_src);
1285 }
1286}
1287
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001288void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 bool type_known_final, type_known_abstract, use_declaring_class;
1290 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1291 *cu_->dex_file,
1292 type_idx,
1293 &type_known_final,
1294 &type_known_abstract,
1295 &use_declaring_class);
1296 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1297 // of the exception throw path.
1298 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001299 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 // Verifier type analysis proved this check cast would never cause an exception.
1301 return;
1302 }
1303 FlushAllRegs();
1304 // May generate a call - use explicit registers
1305 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001306 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001307 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001308 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 if (needs_access_check) {
1310 // Check we have access to type_idx and if not throw IllegalAccessError,
1311 // returns Class* in kRet0
1312 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001313 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001314 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001315 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001316 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001317 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 } else {
1319 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001320 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001321 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001322 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001323 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1325 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001326 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1327 LIR* cont = NewLIR0(kPseudoTargetLabel);
1328
1329 // Slow path to initialize the type. Executed if the type is NULL.
1330 class SlowPath : public LIRSlowPath {
1331 public:
1332 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001333 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001334 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1335 class_reg_(class_reg) {
1336 }
1337
1338 void Compile() {
1339 GenerateTargetLabel();
1340
1341 // Call out to helper, which will return resolved type in kArg0
1342 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001343 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1344 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001345 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001346 m2l_->OpUnconditionalBranch(cont_);
1347 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001348
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001349 public:
1350 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001351 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001352 };
1353
buzbee2700f7e2014-03-07 09:46:20 -08001354 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356 }
1357 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001358 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001359
1360 // Slow path for the case where the classes are not equal. In this case we need
1361 // to call a helper function to do the check.
1362 class SlowPath : public LIRSlowPath {
1363 public:
1364 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1365 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1366 }
1367
1368 void Compile() {
1369 GenerateTargetLabel();
1370
1371 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001372 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1373 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001374 }
Andreas Gampe98430592014-07-27 19:44:50 -07001375 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1376 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001377 m2l_->OpUnconditionalBranch(cont_);
1378 }
1379
1380 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001381 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001382 };
1383
1384 if (type_known_abstract) {
1385 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001386 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001387 LIR* cont = NewLIR0(kPseudoTargetLabel);
1388 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1389 } else {
1390 // Harder, more common case. We need to generate a forward branch over the load
1391 // if the target is null. If it's non-null we perform the load and branch to the
1392 // slow path if the classes are not equal.
1393
1394 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001395 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001396 /* load object->klass_ */
1397 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001398 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1399 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001400
Andreas Gampeccc60262014-07-04 18:02:38 -07001401 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001402 LIR* cont = NewLIR0(kPseudoTargetLabel);
1403
1404 // Add the slow path that will not perform load since this is already done.
1405 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1406
1407 // Set the null check to branch to the continuation.
1408 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 }
1410}
1411
1412void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001413 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 RegLocation rl_result;
1415 if (cu_->instruction_set == kThumb2) {
1416 /*
1417 * NOTE: This is the one place in the code in which we might have
1418 * as many as six live temporary registers. There are 5 in the normal
1419 * set for Arm. Until we have spill capabilities, temporarily add
1420 * lr to the temp set. It is safe to do this locally, but note that
1421 * lr is used explicitly elsewhere in the code generator and cannot
1422 * normally be used as a general temp register.
1423 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001424 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1425 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 }
1427 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1428 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1429 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1430 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001431 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1432 RegStorage t_reg = AllocTemp();
1433 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1434 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1435 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 FreeTemp(t_reg);
1437 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001438 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1439 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 }
1441 /*
1442 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1443 * following StoreValueWide might need to allocate a temp register.
1444 * To further work around the lack of a spill capability, explicitly
1445 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1446 * Remove when spill is functional.
1447 */
1448 FreeRegLocTemps(rl_result, rl_src1);
1449 FreeRegLocTemps(rl_result, rl_src2);
1450 StoreValueWide(rl_dest, rl_result);
1451 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001452 Clobber(TargetReg(kLr, kNotWide));
1453 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 }
1455}
1456
Andreas Gampe98430592014-07-27 19:44:50 -07001457void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1458 RegLocation rl_src1, RegLocation rl_shift) {
1459 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 switch (opcode) {
1461 case Instruction::SHL_LONG:
1462 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001463 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 break;
1465 case Instruction::SHR_LONG:
1466 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001467 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 break;
1469 case Instruction::USHR_LONG:
1470 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001471 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 break;
1473 default:
1474 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001475 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 }
Andreas Gampe98430592014-07-27 19:44:50 -07001477 FlushAllRegs(); /* Send everything to home location */
1478 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001479 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 StoreValueWide(rl_dest, rl_result);
1481}
1482
1483
1484void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001485 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001486 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 OpKind op = kOpBkpt;
1488 bool is_div_rem = false;
1489 bool check_zero = false;
1490 bool unary = false;
1491 RegLocation rl_result;
1492 bool shift_op = false;
1493 switch (opcode) {
1494 case Instruction::NEG_INT:
1495 op = kOpNeg;
1496 unary = true;
1497 break;
1498 case Instruction::NOT_INT:
1499 op = kOpMvn;
1500 unary = true;
1501 break;
1502 case Instruction::ADD_INT:
1503 case Instruction::ADD_INT_2ADDR:
1504 op = kOpAdd;
1505 break;
1506 case Instruction::SUB_INT:
1507 case Instruction::SUB_INT_2ADDR:
1508 op = kOpSub;
1509 break;
1510 case Instruction::MUL_INT:
1511 case Instruction::MUL_INT_2ADDR:
1512 op = kOpMul;
1513 break;
1514 case Instruction::DIV_INT:
1515 case Instruction::DIV_INT_2ADDR:
1516 check_zero = true;
1517 op = kOpDiv;
1518 is_div_rem = true;
1519 break;
1520 /* NOTE: returns in kArg1 */
1521 case Instruction::REM_INT:
1522 case Instruction::REM_INT_2ADDR:
1523 check_zero = true;
1524 op = kOpRem;
1525 is_div_rem = true;
1526 break;
1527 case Instruction::AND_INT:
1528 case Instruction::AND_INT_2ADDR:
1529 op = kOpAnd;
1530 break;
1531 case Instruction::OR_INT:
1532 case Instruction::OR_INT_2ADDR:
1533 op = kOpOr;
1534 break;
1535 case Instruction::XOR_INT:
1536 case Instruction::XOR_INT_2ADDR:
1537 op = kOpXor;
1538 break;
1539 case Instruction::SHL_INT:
1540 case Instruction::SHL_INT_2ADDR:
1541 shift_op = true;
1542 op = kOpLsl;
1543 break;
1544 case Instruction::SHR_INT:
1545 case Instruction::SHR_INT_2ADDR:
1546 shift_op = true;
1547 op = kOpAsr;
1548 break;
1549 case Instruction::USHR_INT:
1550 case Instruction::USHR_INT_2ADDR:
1551 shift_op = true;
1552 op = kOpLsr;
1553 break;
1554 default:
1555 LOG(FATAL) << "Invalid word arith op: " << opcode;
1556 }
1557 if (!is_div_rem) {
1558 if (unary) {
1559 rl_src1 = LoadValue(rl_src1, kCoreReg);
1560 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001561 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001563 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001564 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001565 RegStorage t_reg = AllocTemp();
1566 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001567 rl_src1 = LoadValue(rl_src1, kCoreReg);
1568 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001569 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 FreeTemp(t_reg);
1571 } else {
1572 rl_src1 = LoadValue(rl_src1, kCoreReg);
1573 rl_src2 = LoadValue(rl_src2, kCoreReg);
1574 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001575 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 }
1577 }
1578 StoreValue(rl_dest, rl_result);
1579 } else {
Dave Allison70202782013-10-22 17:52:19 -07001580 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 +01001581 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 rl_src1 = LoadValue(rl_src1, kCoreReg);
1583 rl_src2 = LoadValue(rl_src2, kCoreReg);
1584 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001585 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586 }
buzbee2700f7e2014-03-07 09:46:20 -08001587 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001588 done = true;
1589 } else if (cu_->instruction_set == kThumb2) {
1590 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1591 // Use ARM SDIV instruction for division. For remainder we also need to
1592 // calculate using a MUL and subtract.
1593 rl_src1 = LoadValue(rl_src1, kCoreReg);
1594 rl_src2 = LoadValue(rl_src2, kCoreReg);
1595 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001596 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001597 }
buzbee2700f7e2014-03-07 09:46:20 -08001598 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001599 done = true;
1600 }
1601 }
1602
1603 // If we haven't already generated the code use the callout function.
1604 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001606 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001607 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001608 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001610 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
Dave Allison70202782013-10-22 17:52:19 -07001612 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001613 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001615 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 else
1617 rl_result = GetReturnAlt();
1618 }
1619 StoreValue(rl_dest, rl_result);
1620 }
1621}
1622
1623/*
1624 * The following are the first-level codegen routines that analyze the format
1625 * of each bytecode then either dispatch special purpose codegen routines
1626 * or produce corresponding Thumb instructions directly.
1627 */
1628
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001630static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 x &= x - 1;
1632 return (x & (x - 1)) == 0;
1633}
1634
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1636// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001637bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001638 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1640 return false;
1641 }
1642 // No divide instruction for Arm, so check for more special cases
1643 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001644 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 }
1646 int k = LowestSetBit(lit);
1647 if (k >= 30) {
1648 // Avoid special cases.
1649 return false;
1650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 rl_src = LoadValue(rl_src, kCoreReg);
1652 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001653 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001654 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 if (lit == 2) {
1656 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001657 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1658 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1659 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001663 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1664 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 }
1666 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001667 RegStorage t_reg1 = AllocTemp();
1668 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001670 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1671 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001673 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001677 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001679 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 }
1681 }
1682 StoreValue(rl_dest, rl_result);
1683 return true;
1684}
1685
1686// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1687// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001688bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001689 if (lit < 0) {
1690 return false;
1691 }
1692 if (lit == 0) {
1693 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1694 LoadConstant(rl_result.reg, 0);
1695 StoreValue(rl_dest, rl_result);
1696 return true;
1697 }
1698 if (lit == 1) {
1699 rl_src = LoadValue(rl_src, kCoreReg);
1700 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1701 OpRegCopy(rl_result.reg, rl_src.reg);
1702 StoreValue(rl_dest, rl_result);
1703 return true;
1704 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001705 // There is RegRegRegShift on Arm, so check for more special cases
1706 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001707 return EasyMultiply(rl_src, rl_dest, lit);
1708 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 // Can we simplify this multiplication?
1710 bool power_of_two = false;
1711 bool pop_count_le2 = false;
1712 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001713 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 power_of_two = true;
1715 } else if (IsPopCountLE2(lit)) {
1716 pop_count_le2 = true;
1717 } else if (IsPowerOfTwo(lit + 1)) {
1718 power_of_two_minus_one = true;
1719 } else {
1720 return false;
1721 }
1722 rl_src = LoadValue(rl_src, kCoreReg);
1723 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1724 if (power_of_two) {
1725 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001726 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 } else if (pop_count_le2) {
1728 // Shift and add and shift.
1729 int first_bit = LowestSetBit(lit);
1730 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1731 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1732 } else {
1733 // Reverse subtract: (src << (shift + 1)) - src.
1734 DCHECK(power_of_two_minus_one);
1735 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001736 RegStorage t_reg = AllocTemp();
1737 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1738 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 }
1740 StoreValue(rl_dest, rl_result);
1741 return true;
1742}
1743
1744void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001745 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 RegLocation rl_result;
1747 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1748 int shift_op = false;
1749 bool is_div = false;
1750
1751 switch (opcode) {
1752 case Instruction::RSUB_INT_LIT8:
1753 case Instruction::RSUB_INT: {
1754 rl_src = LoadValue(rl_src, kCoreReg);
1755 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1756 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001757 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001759 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1760 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 }
1762 StoreValue(rl_dest, rl_result);
1763 return;
1764 }
1765
1766 case Instruction::SUB_INT:
1767 case Instruction::SUB_INT_2ADDR:
1768 lit = -lit;
1769 // Intended fallthrough
1770 case Instruction::ADD_INT:
1771 case Instruction::ADD_INT_2ADDR:
1772 case Instruction::ADD_INT_LIT8:
1773 case Instruction::ADD_INT_LIT16:
1774 op = kOpAdd;
1775 break;
1776 case Instruction::MUL_INT:
1777 case Instruction::MUL_INT_2ADDR:
1778 case Instruction::MUL_INT_LIT8:
1779 case Instruction::MUL_INT_LIT16: {
1780 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1781 return;
1782 }
1783 op = kOpMul;
1784 break;
1785 }
1786 case Instruction::AND_INT:
1787 case Instruction::AND_INT_2ADDR:
1788 case Instruction::AND_INT_LIT8:
1789 case Instruction::AND_INT_LIT16:
1790 op = kOpAnd;
1791 break;
1792 case Instruction::OR_INT:
1793 case Instruction::OR_INT_2ADDR:
1794 case Instruction::OR_INT_LIT8:
1795 case Instruction::OR_INT_LIT16:
1796 op = kOpOr;
1797 break;
1798 case Instruction::XOR_INT:
1799 case Instruction::XOR_INT_2ADDR:
1800 case Instruction::XOR_INT_LIT8:
1801 case Instruction::XOR_INT_LIT16:
1802 op = kOpXor;
1803 break;
1804 case Instruction::SHL_INT_LIT8:
1805 case Instruction::SHL_INT:
1806 case Instruction::SHL_INT_2ADDR:
1807 lit &= 31;
1808 shift_op = true;
1809 op = kOpLsl;
1810 break;
1811 case Instruction::SHR_INT_LIT8:
1812 case Instruction::SHR_INT:
1813 case Instruction::SHR_INT_2ADDR:
1814 lit &= 31;
1815 shift_op = true;
1816 op = kOpAsr;
1817 break;
1818 case Instruction::USHR_INT_LIT8:
1819 case Instruction::USHR_INT:
1820 case Instruction::USHR_INT_2ADDR:
1821 lit &= 31;
1822 shift_op = true;
1823 op = kOpLsr;
1824 break;
1825
1826 case Instruction::DIV_INT:
1827 case Instruction::DIV_INT_2ADDR:
1828 case Instruction::DIV_INT_LIT8:
1829 case Instruction::DIV_INT_LIT16:
1830 case Instruction::REM_INT:
1831 case Instruction::REM_INT_2ADDR:
1832 case Instruction::REM_INT_LIT8:
1833 case Instruction::REM_INT_LIT16: {
1834 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001835 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836 return;
1837 }
buzbee11b63d12013-08-27 07:34:17 -07001838 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001840 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 (opcode == Instruction::DIV_INT_LIT16)) {
1842 is_div = true;
1843 } else {
1844 is_div = false;
1845 }
buzbee11b63d12013-08-27 07:34:17 -07001846 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1847 return;
1848 }
Dave Allison70202782013-10-22 17:52:19 -07001849
1850 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001851 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001853 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001854 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001855 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001856 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1857 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001858 } else if (cu_->instruction_set == kThumb2) {
1859 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1860 // Use ARM SDIV instruction for division. For remainder we also need to
1861 // calculate using a MUL and subtract.
1862 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001863 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001864 done = true;
1865 }
1866 }
1867
1868 if (!done) {
1869 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001870 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1871 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001872 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001873 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001874 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001875 else
1876 rl_result = GetReturnAlt();
1877 }
1878 StoreValue(rl_dest, rl_result);
1879 return;
1880 }
1881 default:
1882 LOG(FATAL) << "Unexpected opcode " << opcode;
1883 }
1884 rl_src = LoadValue(rl_src, kCoreReg);
1885 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001886 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001888 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001889 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001890 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001891 }
1892 StoreValue(rl_dest, rl_result);
1893}
1894
Andreas Gampe98430592014-07-27 19:44:50 -07001895void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
1896 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001897 RegLocation rl_result;
1898 OpKind first_op = kOpBkpt;
1899 OpKind second_op = kOpBkpt;
1900 bool call_out = false;
1901 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001902 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1903 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001904
1905 switch (opcode) {
1906 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001907 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1908 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001909 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001910 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001911 RegStorage t_reg = AllocTemp();
1912 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1913 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1914 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1915 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001916 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001917 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1918 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 }
Andreas Gampe98430592014-07-27 19:44:50 -07001920 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001921 return;
1922 case Instruction::ADD_LONG:
1923 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 first_op = kOpAdd;
1925 second_op = kOpAdc;
1926 break;
1927 case Instruction::SUB_LONG:
1928 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 first_op = kOpSub;
1930 second_op = kOpSbc;
1931 break;
1932 case Instruction::MUL_LONG:
1933 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001934 call_out = true;
1935 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1936 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 break;
1938 case Instruction::DIV_LONG:
1939 case Instruction::DIV_LONG_2ADDR:
1940 call_out = true;
1941 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001942 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1943 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001944 break;
1945 case Instruction::REM_LONG:
1946 case Instruction::REM_LONG_2ADDR:
1947 call_out = true;
1948 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001949 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001951 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1952 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001953 break;
1954 case Instruction::AND_LONG_2ADDR:
1955 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001956 first_op = kOpAnd;
1957 second_op = kOpAnd;
1958 break;
1959 case Instruction::OR_LONG:
1960 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001961 first_op = kOpOr;
1962 second_op = kOpOr;
1963 break;
1964 case Instruction::XOR_LONG:
1965 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966 first_op = kOpXor;
1967 second_op = kOpXor;
1968 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 default:
1970 LOG(FATAL) << "Invalid long arith op";
1971 }
1972 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07001973 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001975 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07001977 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
1978 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
1979 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1980 RegStorage r_tgt = CallHelperSetup(target);
1981 GenDivZeroCheckWide(r_tmp2);
1982 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001984 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001986 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 }
1988 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07001989 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
1990 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 else
Andreas Gampe98430592014-07-27 19:44:50 -07001992 rl_result = GetReturnWideAlt();
1993 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001994 }
1995}
1996
Mark Mendelle87f9b52014-04-30 14:13:18 -04001997void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
1998 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
1999 LoadConstantNoClobber(rl_result.reg, value);
2000 StoreValue(rl_dest, rl_result);
2001 if (value == 0) {
2002 Workaround7250540(rl_dest, rl_result.reg);
2003 }
2004}
2005
Andreas Gampe98430592014-07-27 19:44:50 -07002006void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2007 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008 /*
2009 * Don't optimize the register usage since it calls out to support
2010 * functions
2011 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002012
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002014 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 if (rl_dest.wide) {
2016 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002017 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002018 StoreValueWide(rl_dest, rl_result);
2019 } else {
2020 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002021 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 StoreValue(rl_dest, rl_result);
2023 }
2024}
2025
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002026class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2027 public:
2028 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2029 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2030 }
2031
2032 void Compile() OVERRIDE {
2033 m2l_->ResetRegPool();
2034 m2l_->ResetDefTracking();
2035 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002036 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002037 if (cont_ != nullptr) {
2038 m2l_->OpUnconditionalBranch(cont_);
2039 }
2040 }
2041};
2042
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002044void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002045 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002046 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2047 return;
2048 }
2049 FlushAllRegs();
2050 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002051 LIR* cont = NewLIR0(kPseudoTargetLabel);
2052 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002053 } else {
2054 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2055 return;
2056 }
2057 FlushAllRegs(); // TODO: needed?
2058 LIR* inst = CheckSuspendUsingLoad();
2059 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061}
2062
2063/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002064void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002065 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002066 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2067 OpUnconditionalBranch(target);
2068 return;
2069 }
2070 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002071 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002072 LIR* branch = OpUnconditionalBranch(nullptr);
2073 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002074 } else {
2075 // For the implicit suspend check, just perform the trigger
2076 // load and branch to the target.
2077 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2078 OpUnconditionalBranch(target);
2079 return;
2080 }
2081 FlushAllRegs();
2082 LIR* inst = CheckSuspendUsingLoad();
2083 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002086}
2087
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002088/* Call out to helper assembly routine that will null check obj and then lock it. */
2089void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2090 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002091 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002092}
2093
2094/* Call out to helper assembly routine that will null check obj and then unlock it. */
2095void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2096 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002097 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002098}
2099
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002100/* Generic code for generating a wide constant into a VR. */
2101void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2102 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002103 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002104 StoreValueWide(rl_dest, rl_result);
2105}
2106
Andreas Gampe48971b32014-08-06 10:09:01 -07002107void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
2108 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
2109 const uint16_t entries = table[1];
2110 // Chained cmp-and-branch.
2111 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
2112 int32_t current_key = as_int32[0];
2113 const int32_t* targets = &as_int32[1];
2114 rl_src = LoadValue(rl_src, kCoreReg);
2115 int i = 0;
2116 for (; i < entries; i++, current_key++) {
2117 if (!InexpensiveConstantInt(current_key, Instruction::Code::IF_EQ)) {
2118 // Switch to using a temp and add.
2119 break;
2120 }
2121 BasicBlock* case_block =
2122 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2123 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2124 }
2125 if (i < entries) {
2126 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2127 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2128 if (key_temp.Valid()) {
2129 LoadConstantNoClobber(key_temp, current_key);
2130 for (; i < entries - 1; i++, current_key++) {
2131 BasicBlock* case_block =
2132 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2133 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2134 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2135 }
2136 BasicBlock* case_block =
2137 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2138 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2139 } else {
2140 // No free temp, just finish the old loop.
2141 for (; i < entries; i++, current_key++) {
2142 BasicBlock* case_block =
2143 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2144 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2145 }
2146 }
2147 }
2148}
2149
2150void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
2151 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
2152 if (cu_->verbose) {
2153 DumpSparseSwitchTable(table);
2154 }
2155
2156 const uint16_t entries = table[1];
2157 if (entries <= kSmallSwitchThreshold) {
2158 GenSmallPackedSwitch(mir, table_offset, rl_src);
2159 } else {
2160 // Use the backend-specific implementation.
2161 GenLargePackedSwitch(mir, table_offset, rl_src);
2162 }
2163}
2164
2165void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
2166 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
2167 const uint16_t entries = table[1];
2168 // Chained cmp-and-branch.
2169 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2170 const int32_t* targets = &keys[entries];
2171 rl_src = LoadValue(rl_src, kCoreReg);
2172 for (int i = 0; i < entries; i++) {
2173 int key = keys[i];
2174 BasicBlock* case_block =
2175 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2176 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2177 }
2178}
2179
2180void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
2181 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
2182 if (cu_->verbose) {
2183 DumpSparseSwitchTable(table);
2184 }
2185
2186 const uint16_t entries = table[1];
2187 if (entries <= kSmallSwitchThreshold) {
2188 GenSmallSparseSwitch(mir, table_offset, rl_src);
2189 } else {
2190 // Use the backend-specific implementation.
2191 GenLargeSparseSwitch(mir, table_offset, rl_src);
2192 }
2193}
2194
Fred Shih37f05ef2014-07-16 18:38:08 -07002195bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2196 switch (size) {
2197 case kReference:
2198 return type == Primitive::kPrimNot;
2199 case k64:
2200 case kDouble:
2201 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2202 case k32:
2203 case kSingle:
2204 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2205 case kSignedHalf:
2206 return type == Primitive::kPrimShort;
2207 case kUnsignedHalf:
2208 return type == Primitive::kPrimChar;
2209 case kSignedByte:
2210 return type == Primitive::kPrimByte;
2211 case kUnsignedByte:
2212 return type == Primitive::kPrimBoolean;
2213 case kWord: // Intentional fallthrough.
2214 default:
2215 return false; // There are no sane types with this op size.
2216 }
2217}
2218
Brian Carlstrom7940e442013-07-12 13:46:57 -07002219} // namespace art