blob: 9f7a8813c006e129b9a5a60a65464d4e9af0571e [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().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100760 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800761 slowpath->Compile();
762 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100763 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800764}
765
Fred Shih37f05ef2014-07-16 18:38:08 -0700766void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
767 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
769 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700770 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700771 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700772 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000773 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700774 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100775 GenNullCheck(rl_obj.reg, opt_flags);
776 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
777 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000778 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700779 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000780 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
781 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100782 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700783 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000784 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100785 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000786 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700787 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 StoreValueWide(rl_dest, rl_result);
789 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 StoreValue(rl_dest, rl_result);
791 }
792 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700793 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
794 QuickEntrypointEnum target;
795 switch (type) {
796 case Primitive::kPrimNot:
797 target = kQuickGetObjInstance;
798 break;
799 case Primitive::kPrimLong:
800 case Primitive::kPrimDouble:
801 target = kQuickGet64Instance;
802 break;
803 case Primitive::kPrimFloat:
804 case Primitive::kPrimInt:
805 target = kQuickGet32Instance;
806 break;
807 case Primitive::kPrimShort:
808 target = kQuickGetShortInstance;
809 break;
810 case Primitive::kPrimChar:
811 target = kQuickGetCharInstance;
812 break;
813 case Primitive::kPrimByte:
814 target = kQuickGetByteInstance;
815 break;
816 case Primitive::kPrimBoolean:
817 target = kQuickGetBooleanInstance;
818 break;
819 case Primitive::kPrimVoid: // Intentional fallthrough.
820 default:
821 LOG(FATAL) << "Can't determine entrypoint for: " << type;
822 target = kQuickGet32Instance;
823 }
Andreas Gampe98430592014-07-27 19:44:50 -0700824 // Second argument of pGetXXInstance is always a reference.
825 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
826 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
827
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700828 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700829 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700830 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 StoreValueWide(rl_dest, rl_result);
832 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700833 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 StoreValue(rl_dest, rl_result);
835 }
836 }
837}
838
Vladimir Markobe0e5462014-02-26 11:24:15 +0000839void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700840 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000841 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
842 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700843 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700844 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700845 // Dex code never writes to the class field.
846 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
847 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700848 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700849 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100850 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 } else {
852 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100853 }
854 GenNullCheck(rl_obj.reg, opt_flags);
855 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000856 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700857 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000858 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
859 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100860 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700861 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000862 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100863 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000864 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700865 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100866 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 }
868 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700869 QuickEntrypointEnum target;
870 switch (size) {
871 case kReference:
872 target = kQuickSetObjInstance;
873 break;
874 case k64:
875 case kDouble:
876 target = kQuickSet64Instance;
877 break;
878 case k32:
879 case kSingle:
880 target = kQuickSet32Instance;
881 break;
882 case kSignedHalf:
883 case kUnsignedHalf:
884 target = kQuickSet16Instance;
885 break;
886 case kSignedByte:
887 case kUnsignedByte:
888 target = kQuickSet8Instance;
889 break;
890 case kWord: // Intentional fallthrough.
891 default:
892 LOG(FATAL) << "Can't determine entrypoint for: " << size;
893 target = kQuickSet32Instance;
894 }
Andreas Gampe98430592014-07-27 19:44:50 -0700895 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
896 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 }
898}
899
Ian Rogersa9a82542013-10-04 11:17:26 -0700900void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
901 RegLocation rl_src) {
902 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
903 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
904 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700905 QuickEntrypointEnum target = needs_range_check
906 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
907 : kQuickAputObjectWithBoundCheck)
908 : kQuickAputObject;
909 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700910}
911
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700912void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700914 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700915 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700916 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700918 *cu_->dex_file,
919 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 // Call out to helper which resolves type and verifies access.
921 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700922 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700923 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 StoreValue(rl_dest, rl_result);
925 } else {
926 // We're don't need access checks, load type from dex cache
927 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700928 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000929 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000930 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000931 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
933 type_idx) || SLOW_TYPE_PATH) {
934 // Slow path, at runtime test if type is null and if so initialize
935 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800936 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800937 LIR* cont = NewLIR0(kPseudoTargetLabel);
938
939 // Object to generate the slow path for class resolution.
940 class SlowPath : public LIRSlowPath {
941 public:
942 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
943 const RegLocation& rl_method, const RegLocation& rl_result) :
944 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
945 rl_method_(rl_method), rl_result_(rl_result) {
946 }
947
948 void Compile() {
949 GenerateTargetLabel();
950
Andreas Gampe98430592014-07-27 19:44:50 -0700951 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700952 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800953 m2l_->OpUnconditionalBranch(cont_);
954 }
955
956 private:
957 const int type_idx_;
958 const RegLocation rl_method_;
959 const RegLocation rl_result_;
960 };
961
962 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800963 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800964
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800966 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 // Fast path, we're done - just store result
968 StoreValue(rl_dest, rl_result);
969 }
970 }
971}
972
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700973void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000975 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
976 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
978 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
979 // slow path, resolve string if not in dex cache
980 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700981 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800982
983 // If the Method* is already in a register, we can save a copy.
984 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800985 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800986 if (rl_method.location == kLocPhysReg) {
987 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800988 DCHECK(!IsTemp(rl_method.reg));
989 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800990 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -0700991 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -0800992 LoadCurrMethodDirect(r_method);
993 }
buzbee695d13a2014-04-19 13:32:20 -0700994 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -0700995 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -0800996
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -0700998 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
999 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001000 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001001
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001002 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001003 // Object to generate the slow path for string resolution.
1004 class SlowPath : public LIRSlowPath {
1005 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001006 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1007 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1008 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001009 }
1010
1011 void Compile() {
1012 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001013 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001014 m2l_->OpUnconditionalBranch(cont_);
1015 }
1016
1017 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001018 const RegStorage r_method_;
1019 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001020 };
1021
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001022 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001024
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001026 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 } else {
1028 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001029 RegStorage res_reg = AllocTempRef();
1030 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001031 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1032 kNotVolatile);
1033 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 StoreValue(rl_dest, rl_result);
1035 }
1036}
1037
Andreas Gampe98430592014-07-27 19:44:50 -07001038/*
1039 * Let helper function take care of everything. Will
1040 * call Class::NewInstanceFromCode(type_idx, method);
1041 */
1042void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1043 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 // alloc will always check for resolution, do we also need to verify
1045 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001046 const DexFile* dex_file = cu_->dex_file;
1047 CompilerDriver* driver = cu_->compiler_driver;
1048 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 bool is_type_initialized;
1050 bool use_direct_type_ptr;
1051 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001052 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001054 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1055 &direct_type_ptr, &is_finalizable) &&
1056 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001057 // The fast path.
1058 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001059 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001060 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001061 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001063 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001064 }
1065 } else {
1066 // Use the direct pointer.
1067 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001068 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001070 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001071 }
1072 }
1073 } else {
1074 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001075 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001076 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001078 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
Andreas Gampe98430592014-07-27 19:44:50 -07001080 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081}
1082
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001083void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001085 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086}
1087
1088// For final classes there are no sub-classes to check and so we can answer the instance-of
1089// question with simple comparisons.
1090void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1091 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001092 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001093 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001094
buzbeea0cd2d72014-06-01 09:33:49 -07001095 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001097 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001098 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001100 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 }
1102 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001103 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104
buzbeea0cd2d72014-06-01 09:33:49 -07001105 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1106 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107
1108 LoadCurrMethodDirect(check_class);
1109 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001110 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1111 kNotVolatile);
1112 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1113 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 } else {
buzbee695d13a2014-04-19 13:32:20 -07001115 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001116 check_class, kNotVolatile);
1117 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1118 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001119 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001120 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 }
1122
buzbee695d13a2014-04-19 13:32:20 -07001123 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 if (cu_->instruction_set == kThumb2) {
1125 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001126 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001128 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001130 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 }
1132 LIR* target = NewLIR0(kPseudoTargetLabel);
1133 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 FreeTemp(object_class);
1135 FreeTemp(check_class);
1136 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001137 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 FreeTemp(result_reg);
1139 }
1140 StoreValue(rl_dest, rl_result);
1141}
1142
1143void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1144 bool type_known_abstract, bool use_declaring_class,
1145 bool can_assume_type_is_in_dex_cache,
1146 uint32_t type_idx, RegLocation rl_dest,
1147 RegLocation rl_src) {
1148 FlushAllRegs();
1149 // May generate a call - use explicit registers
1150 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001151 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001152 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001153 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001154 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1155 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 if (needs_access_check) {
1157 // Check we have access to type_idx and if not throw IllegalAccessError,
1158 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001159 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001160 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1161 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001163 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001164 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001165 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001167 if (can_assume_type_is_in_dex_cache) {
1168 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001169 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001170 }
1171
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001173 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001174 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001175 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001176 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001178 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1179 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1180
1181 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001182 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001183
1184 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1185 public:
1186 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1187 RegLocation rl_src)
1188 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1189 rl_src_(rl_src) {
1190 }
1191
1192 void Compile() OVERRIDE {
1193 GenerateTargetLabel();
1194
Andreas Gampe98430592014-07-27 19:44:50 -07001195 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001196 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1197 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001198 m2l_->OpUnconditionalBranch(cont_);
1199 }
1200
1201 private:
1202 uint32_t type_idx_;
1203 RegLocation rl_src_;
1204 };
1205
1206 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1207 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 }
1209 }
1210 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001211 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001212 if (!IsSameReg(rl_result.reg, ref_reg)) {
1213 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001214 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001216 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217
1218 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001219 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001221 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1222 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1224 LIR* branchover = NULL;
1225 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001226 // rl_result == ref == class.
1227 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001228 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 } else {
1230 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001231 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001232 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 if (!type_known_abstract) {
1234 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001235 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001236 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001237 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001239 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001241 if (it != nullptr) {
1242 OpEndIT(it);
1243 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 FreeTemp(r_tgt);
1245 } else {
1246 if (!type_known_abstract) {
1247 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001248 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001249 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001251
Serguei Katkov9ee45192014-07-17 14:39:03 +07001252 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001253 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
1255 }
1256 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001257 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 /* branch targets here */
1259 LIR* target = NewLIR0(kPseudoTargetLabel);
1260 StoreValue(rl_dest, rl_result);
1261 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001262 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263 branchover->target = target;
1264 }
1265}
1266
1267void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1268 bool type_known_final, type_known_abstract, use_declaring_class;
1269 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1270 *cu_->dex_file,
1271 type_idx,
1272 &type_known_final,
1273 &type_known_abstract,
1274 &use_declaring_class);
1275 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1276 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1277
1278 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1279 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1280 } else {
1281 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1282 use_declaring_class, can_assume_type_is_in_dex_cache,
1283 type_idx, rl_dest, rl_src);
1284 }
1285}
1286
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001287void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 bool type_known_final, type_known_abstract, use_declaring_class;
1289 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1290 *cu_->dex_file,
1291 type_idx,
1292 &type_known_final,
1293 &type_known_abstract,
1294 &use_declaring_class);
1295 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1296 // of the exception throw path.
1297 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001298 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 // Verifier type analysis proved this check cast would never cause an exception.
1300 return;
1301 }
1302 FlushAllRegs();
1303 // May generate a call - use explicit registers
1304 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001305 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001306 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001307 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 if (needs_access_check) {
1309 // Check we have access to type_idx and if not throw IllegalAccessError,
1310 // returns Class* in kRet0
1311 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001312 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001313 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001315 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001316 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 } else {
1318 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001319 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001320 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001321 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001322 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1324 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001325 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1326 LIR* cont = NewLIR0(kPseudoTargetLabel);
1327
1328 // Slow path to initialize the type. Executed if the type is NULL.
1329 class SlowPath : public LIRSlowPath {
1330 public:
1331 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001332 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001333 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1334 class_reg_(class_reg) {
1335 }
1336
1337 void Compile() {
1338 GenerateTargetLabel();
1339
1340 // Call out to helper, which will return resolved type in kArg0
1341 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001342 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1343 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001344 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001345 m2l_->OpUnconditionalBranch(cont_);
1346 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001347
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001348 public:
1349 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001350 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001351 };
1352
buzbee2700f7e2014-03-07 09:46:20 -08001353 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 }
1355 }
1356 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001357 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001358
1359 // Slow path for the case where the classes are not equal. In this case we need
1360 // to call a helper function to do the check.
1361 class SlowPath : public LIRSlowPath {
1362 public:
1363 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1364 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1365 }
1366
1367 void Compile() {
1368 GenerateTargetLabel();
1369
1370 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001371 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1372 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001373 }
Andreas Gampe98430592014-07-27 19:44:50 -07001374 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1375 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001376 m2l_->OpUnconditionalBranch(cont_);
1377 }
1378
1379 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001380 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381 };
1382
1383 if (type_known_abstract) {
1384 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001385 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001386 LIR* cont = NewLIR0(kPseudoTargetLabel);
1387 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1388 } else {
1389 // Harder, more common case. We need to generate a forward branch over the load
1390 // if the target is null. If it's non-null we perform the load and branch to the
1391 // slow path if the classes are not equal.
1392
1393 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001394 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001395 /* load object->klass_ */
1396 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001397 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1398 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001399
Andreas Gampeccc60262014-07-04 18:02:38 -07001400 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001401 LIR* cont = NewLIR0(kPseudoTargetLabel);
1402
1403 // Add the slow path that will not perform load since this is already done.
1404 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1405
1406 // Set the null check to branch to the continuation.
1407 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 }
1409}
1410
1411void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001412 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 RegLocation rl_result;
1414 if (cu_->instruction_set == kThumb2) {
1415 /*
1416 * NOTE: This is the one place in the code in which we might have
1417 * as many as six live temporary registers. There are 5 in the normal
1418 * set for Arm. Until we have spill capabilities, temporarily add
1419 * lr to the temp set. It is safe to do this locally, but note that
1420 * lr is used explicitly elsewhere in the code generator and cannot
1421 * normally be used as a general temp register.
1422 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001423 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1424 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425 }
1426 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1427 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1428 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1429 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001430 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1431 RegStorage t_reg = AllocTemp();
1432 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1433 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1434 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001435 FreeTemp(t_reg);
1436 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001437 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1438 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 }
1440 /*
1441 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1442 * following StoreValueWide might need to allocate a temp register.
1443 * To further work around the lack of a spill capability, explicitly
1444 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1445 * Remove when spill is functional.
1446 */
1447 FreeRegLocTemps(rl_result, rl_src1);
1448 FreeRegLocTemps(rl_result, rl_src2);
1449 StoreValueWide(rl_dest, rl_result);
1450 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001451 Clobber(TargetReg(kLr, kNotWide));
1452 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001453 }
1454}
1455
Andreas Gampe98430592014-07-27 19:44:50 -07001456void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1457 RegLocation rl_src1, RegLocation rl_shift) {
1458 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 switch (opcode) {
1460 case Instruction::SHL_LONG:
1461 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001462 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 break;
1464 case Instruction::SHR_LONG:
1465 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001466 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 break;
1468 case Instruction::USHR_LONG:
1469 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001470 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 break;
1472 default:
1473 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001474 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 }
Andreas Gampe98430592014-07-27 19:44:50 -07001476 FlushAllRegs(); /* Send everything to home location */
1477 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001478 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 StoreValueWide(rl_dest, rl_result);
1480}
1481
1482
1483void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001484 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001485 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 OpKind op = kOpBkpt;
1487 bool is_div_rem = false;
1488 bool check_zero = false;
1489 bool unary = false;
1490 RegLocation rl_result;
1491 bool shift_op = false;
1492 switch (opcode) {
1493 case Instruction::NEG_INT:
1494 op = kOpNeg;
1495 unary = true;
1496 break;
1497 case Instruction::NOT_INT:
1498 op = kOpMvn;
1499 unary = true;
1500 break;
1501 case Instruction::ADD_INT:
1502 case Instruction::ADD_INT_2ADDR:
1503 op = kOpAdd;
1504 break;
1505 case Instruction::SUB_INT:
1506 case Instruction::SUB_INT_2ADDR:
1507 op = kOpSub;
1508 break;
1509 case Instruction::MUL_INT:
1510 case Instruction::MUL_INT_2ADDR:
1511 op = kOpMul;
1512 break;
1513 case Instruction::DIV_INT:
1514 case Instruction::DIV_INT_2ADDR:
1515 check_zero = true;
1516 op = kOpDiv;
1517 is_div_rem = true;
1518 break;
1519 /* NOTE: returns in kArg1 */
1520 case Instruction::REM_INT:
1521 case Instruction::REM_INT_2ADDR:
1522 check_zero = true;
1523 op = kOpRem;
1524 is_div_rem = true;
1525 break;
1526 case Instruction::AND_INT:
1527 case Instruction::AND_INT_2ADDR:
1528 op = kOpAnd;
1529 break;
1530 case Instruction::OR_INT:
1531 case Instruction::OR_INT_2ADDR:
1532 op = kOpOr;
1533 break;
1534 case Instruction::XOR_INT:
1535 case Instruction::XOR_INT_2ADDR:
1536 op = kOpXor;
1537 break;
1538 case Instruction::SHL_INT:
1539 case Instruction::SHL_INT_2ADDR:
1540 shift_op = true;
1541 op = kOpLsl;
1542 break;
1543 case Instruction::SHR_INT:
1544 case Instruction::SHR_INT_2ADDR:
1545 shift_op = true;
1546 op = kOpAsr;
1547 break;
1548 case Instruction::USHR_INT:
1549 case Instruction::USHR_INT_2ADDR:
1550 shift_op = true;
1551 op = kOpLsr;
1552 break;
1553 default:
1554 LOG(FATAL) << "Invalid word arith op: " << opcode;
1555 }
1556 if (!is_div_rem) {
1557 if (unary) {
1558 rl_src1 = LoadValue(rl_src1, kCoreReg);
1559 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001560 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001562 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001563 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001564 RegStorage t_reg = AllocTemp();
1565 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 rl_src1 = LoadValue(rl_src1, kCoreReg);
1567 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001568 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 FreeTemp(t_reg);
1570 } else {
1571 rl_src1 = LoadValue(rl_src1, kCoreReg);
1572 rl_src2 = LoadValue(rl_src2, kCoreReg);
1573 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001574 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 }
1576 }
1577 StoreValue(rl_dest, rl_result);
1578 } else {
Dave Allison70202782013-10-22 17:52:19 -07001579 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 +01001580 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 rl_src1 = LoadValue(rl_src1, kCoreReg);
1582 rl_src2 = LoadValue(rl_src2, kCoreReg);
1583 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001584 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
buzbee2700f7e2014-03-07 09:46:20 -08001586 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001587 done = true;
1588 } else if (cu_->instruction_set == kThumb2) {
1589 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1590 // Use ARM SDIV instruction for division. For remainder we also need to
1591 // calculate using a MUL and subtract.
1592 rl_src1 = LoadValue(rl_src1, kCoreReg);
1593 rl_src2 = LoadValue(rl_src2, kCoreReg);
1594 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001595 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001596 }
buzbee2700f7e2014-03-07 09:46:20 -08001597 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001598 done = true;
1599 }
1600 }
1601
1602 // If we haven't already generated the code use the callout function.
1603 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001605 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001606 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001607 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001609 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 }
Dave Allison70202782013-10-22 17:52:19 -07001611 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001612 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001614 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 else
1616 rl_result = GetReturnAlt();
1617 }
1618 StoreValue(rl_dest, rl_result);
1619 }
1620}
1621
1622/*
1623 * The following are the first-level codegen routines that analyze the format
1624 * of each bytecode then either dispatch special purpose codegen routines
1625 * or produce corresponding Thumb instructions directly.
1626 */
1627
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001629static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 x &= x - 1;
1631 return (x & (x - 1)) == 0;
1632}
1633
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1635// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001636bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001637 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001638 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1639 return false;
1640 }
1641 // No divide instruction for Arm, so check for more special cases
1642 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001643 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 }
1645 int k = LowestSetBit(lit);
1646 if (k >= 30) {
1647 // Avoid special cases.
1648 return false;
1649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 rl_src = LoadValue(rl_src, kCoreReg);
1651 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001652 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001653 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 if (lit == 2) {
1655 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001656 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1657 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1658 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001660 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001662 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1663 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 }
1665 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001666 RegStorage t_reg1 = AllocTemp();
1667 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001669 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1670 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001672 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001674 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001676 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001677 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001678 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 }
1680 }
1681 StoreValue(rl_dest, rl_result);
1682 return true;
1683}
1684
1685// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1686// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001687bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001688 if (lit < 0) {
1689 return false;
1690 }
1691 if (lit == 0) {
1692 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1693 LoadConstant(rl_result.reg, 0);
1694 StoreValue(rl_dest, rl_result);
1695 return true;
1696 }
1697 if (lit == 1) {
1698 rl_src = LoadValue(rl_src, kCoreReg);
1699 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1700 OpRegCopy(rl_result.reg, rl_src.reg);
1701 StoreValue(rl_dest, rl_result);
1702 return true;
1703 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001704 // There is RegRegRegShift on Arm, so check for more special cases
1705 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001706 return EasyMultiply(rl_src, rl_dest, lit);
1707 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 // Can we simplify this multiplication?
1709 bool power_of_two = false;
1710 bool pop_count_le2 = false;
1711 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001712 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 power_of_two = true;
1714 } else if (IsPopCountLE2(lit)) {
1715 pop_count_le2 = true;
1716 } else if (IsPowerOfTwo(lit + 1)) {
1717 power_of_two_minus_one = true;
1718 } else {
1719 return false;
1720 }
1721 rl_src = LoadValue(rl_src, kCoreReg);
1722 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1723 if (power_of_two) {
1724 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 } else if (pop_count_le2) {
1727 // Shift and add and shift.
1728 int first_bit = LowestSetBit(lit);
1729 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1730 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1731 } else {
1732 // Reverse subtract: (src << (shift + 1)) - src.
1733 DCHECK(power_of_two_minus_one);
1734 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001735 RegStorage t_reg = AllocTemp();
1736 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1737 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 }
1739 StoreValue(rl_dest, rl_result);
1740 return true;
1741}
1742
1743void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001744 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 RegLocation rl_result;
1746 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1747 int shift_op = false;
1748 bool is_div = false;
1749
1750 switch (opcode) {
1751 case Instruction::RSUB_INT_LIT8:
1752 case Instruction::RSUB_INT: {
1753 rl_src = LoadValue(rl_src, kCoreReg);
1754 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1755 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001756 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001758 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1759 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760 }
1761 StoreValue(rl_dest, rl_result);
1762 return;
1763 }
1764
1765 case Instruction::SUB_INT:
1766 case Instruction::SUB_INT_2ADDR:
1767 lit = -lit;
1768 // Intended fallthrough
1769 case Instruction::ADD_INT:
1770 case Instruction::ADD_INT_2ADDR:
1771 case Instruction::ADD_INT_LIT8:
1772 case Instruction::ADD_INT_LIT16:
1773 op = kOpAdd;
1774 break;
1775 case Instruction::MUL_INT:
1776 case Instruction::MUL_INT_2ADDR:
1777 case Instruction::MUL_INT_LIT8:
1778 case Instruction::MUL_INT_LIT16: {
1779 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1780 return;
1781 }
1782 op = kOpMul;
1783 break;
1784 }
1785 case Instruction::AND_INT:
1786 case Instruction::AND_INT_2ADDR:
1787 case Instruction::AND_INT_LIT8:
1788 case Instruction::AND_INT_LIT16:
1789 op = kOpAnd;
1790 break;
1791 case Instruction::OR_INT:
1792 case Instruction::OR_INT_2ADDR:
1793 case Instruction::OR_INT_LIT8:
1794 case Instruction::OR_INT_LIT16:
1795 op = kOpOr;
1796 break;
1797 case Instruction::XOR_INT:
1798 case Instruction::XOR_INT_2ADDR:
1799 case Instruction::XOR_INT_LIT8:
1800 case Instruction::XOR_INT_LIT16:
1801 op = kOpXor;
1802 break;
1803 case Instruction::SHL_INT_LIT8:
1804 case Instruction::SHL_INT:
1805 case Instruction::SHL_INT_2ADDR:
1806 lit &= 31;
1807 shift_op = true;
1808 op = kOpLsl;
1809 break;
1810 case Instruction::SHR_INT_LIT8:
1811 case Instruction::SHR_INT:
1812 case Instruction::SHR_INT_2ADDR:
1813 lit &= 31;
1814 shift_op = true;
1815 op = kOpAsr;
1816 break;
1817 case Instruction::USHR_INT_LIT8:
1818 case Instruction::USHR_INT:
1819 case Instruction::USHR_INT_2ADDR:
1820 lit &= 31;
1821 shift_op = true;
1822 op = kOpLsr;
1823 break;
1824
1825 case Instruction::DIV_INT:
1826 case Instruction::DIV_INT_2ADDR:
1827 case Instruction::DIV_INT_LIT8:
1828 case Instruction::DIV_INT_LIT16:
1829 case Instruction::REM_INT:
1830 case Instruction::REM_INT_2ADDR:
1831 case Instruction::REM_INT_LIT8:
1832 case Instruction::REM_INT_LIT16: {
1833 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001834 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001835 return;
1836 }
buzbee11b63d12013-08-27 07:34:17 -07001837 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001839 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 (opcode == Instruction::DIV_INT_LIT16)) {
1841 is_div = true;
1842 } else {
1843 is_div = false;
1844 }
buzbee11b63d12013-08-27 07:34:17 -07001845 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1846 return;
1847 }
Dave Allison70202782013-10-22 17:52:19 -07001848
1849 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001850 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001851 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001852 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001853 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001854 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001855 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1856 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001857 } else if (cu_->instruction_set == kThumb2) {
1858 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1859 // Use ARM SDIV instruction for division. For remainder we also need to
1860 // calculate using a MUL and subtract.
1861 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001862 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001863 done = true;
1864 }
1865 }
1866
1867 if (!done) {
1868 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001869 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1870 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001871 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001873 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001874 else
1875 rl_result = GetReturnAlt();
1876 }
1877 StoreValue(rl_dest, rl_result);
1878 return;
1879 }
1880 default:
1881 LOG(FATAL) << "Unexpected opcode " << opcode;
1882 }
1883 rl_src = LoadValue(rl_src, kCoreReg);
1884 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001885 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001887 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001889 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001890 }
1891 StoreValue(rl_dest, rl_result);
1892}
1893
Andreas Gampe98430592014-07-27 19:44:50 -07001894void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
1895 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001896 RegLocation rl_result;
1897 OpKind first_op = kOpBkpt;
1898 OpKind second_op = kOpBkpt;
1899 bool call_out = false;
1900 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001901 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1902 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001903
1904 switch (opcode) {
1905 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001906 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1907 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001909 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001910 RegStorage t_reg = AllocTemp();
1911 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1912 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1913 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1914 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001916 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1917 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001918 }
Andreas Gampe98430592014-07-27 19:44:50 -07001919 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 return;
1921 case Instruction::ADD_LONG:
1922 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 first_op = kOpAdd;
1924 second_op = kOpAdc;
1925 break;
1926 case Instruction::SUB_LONG:
1927 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001928 first_op = kOpSub;
1929 second_op = kOpSbc;
1930 break;
1931 case Instruction::MUL_LONG:
1932 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07001933 call_out = true;
1934 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1935 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 break;
1937 case Instruction::DIV_LONG:
1938 case Instruction::DIV_LONG_2ADDR:
1939 call_out = true;
1940 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001941 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1942 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001943 break;
1944 case Instruction::REM_LONG:
1945 case Instruction::REM_LONG_2ADDR:
1946 call_out = true;
1947 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07001948 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07001950 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
1951 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 break;
1953 case Instruction::AND_LONG_2ADDR:
1954 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 first_op = kOpAnd;
1956 second_op = kOpAnd;
1957 break;
1958 case Instruction::OR_LONG:
1959 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 first_op = kOpOr;
1961 second_op = kOpOr;
1962 break;
1963 case Instruction::XOR_LONG:
1964 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965 first_op = kOpXor;
1966 second_op = kOpXor;
1967 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 default:
1969 LOG(FATAL) << "Invalid long arith op";
1970 }
1971 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07001972 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001974 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07001976 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
1977 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
1978 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1979 RegStorage r_tgt = CallHelperSetup(target);
1980 GenDivZeroCheckWide(r_tmp2);
1981 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07001983 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001985 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 }
1987 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07001988 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
1989 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 else
Andreas Gampe98430592014-07-27 19:44:50 -07001991 rl_result = GetReturnWideAlt();
1992 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001993 }
1994}
1995
Mark Mendelle87f9b52014-04-30 14:13:18 -04001996void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
1997 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
1998 LoadConstantNoClobber(rl_result.reg, value);
1999 StoreValue(rl_dest, rl_result);
2000 if (value == 0) {
2001 Workaround7250540(rl_dest, rl_result.reg);
2002 }
2003}
2004
Andreas Gampe98430592014-07-27 19:44:50 -07002005void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2006 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002007 /*
2008 * Don't optimize the register usage since it calls out to support
2009 * functions
2010 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002011
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002013 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 if (rl_dest.wide) {
2015 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002016 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 StoreValueWide(rl_dest, rl_result);
2018 } else {
2019 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002020 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002021 StoreValue(rl_dest, rl_result);
2022 }
2023}
2024
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002025class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2026 public:
2027 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2028 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2029 }
2030
2031 void Compile() OVERRIDE {
2032 m2l_->ResetRegPool();
2033 m2l_->ResetDefTracking();
2034 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002035 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002036 if (cont_ != nullptr) {
2037 m2l_->OpUnconditionalBranch(cont_);
2038 }
2039 }
2040};
2041
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002043void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002044 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002045 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2046 return;
2047 }
2048 FlushAllRegs();
2049 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002050 LIR* cont = NewLIR0(kPseudoTargetLabel);
2051 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002052 } else {
2053 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2054 return;
2055 }
2056 FlushAllRegs(); // TODO: needed?
2057 LIR* inst = CheckSuspendUsingLoad();
2058 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060}
2061
2062/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002063void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002064 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002065 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2066 OpUnconditionalBranch(target);
2067 return;
2068 }
2069 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002070 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002071 LIR* branch = OpUnconditionalBranch(nullptr);
2072 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002073 } else {
2074 // For the implicit suspend check, just perform the trigger
2075 // load and branch to the target.
2076 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2077 OpUnconditionalBranch(target);
2078 return;
2079 }
2080 FlushAllRegs();
2081 LIR* inst = CheckSuspendUsingLoad();
2082 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085}
2086
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002087/* Call out to helper assembly routine that will null check obj and then lock it. */
2088void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2089 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002090 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002091}
2092
2093/* Call out to helper assembly routine that will null check obj and then unlock it. */
2094void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2095 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002096 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002097}
2098
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002099/* Generic code for generating a wide constant into a VR. */
2100void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2101 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002102 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002103 StoreValueWide(rl_dest, rl_result);
2104}
2105
Andreas Gampe48971b32014-08-06 10:09:01 -07002106void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002107 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002108 const uint16_t entries = table[1];
2109 // Chained cmp-and-branch.
2110 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
2111 int32_t current_key = as_int32[0];
2112 const int32_t* targets = &as_int32[1];
2113 rl_src = LoadValue(rl_src, kCoreReg);
2114 int i = 0;
2115 for (; i < entries; i++, current_key++) {
2116 if (!InexpensiveConstantInt(current_key, Instruction::Code::IF_EQ)) {
2117 // Switch to using a temp and add.
2118 break;
2119 }
2120 BasicBlock* case_block =
2121 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2122 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2123 }
2124 if (i < entries) {
2125 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2126 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2127 if (key_temp.Valid()) {
2128 LoadConstantNoClobber(key_temp, current_key);
2129 for (; i < entries - 1; i++, current_key++) {
2130 BasicBlock* case_block =
2131 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2132 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2133 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2134 }
2135 BasicBlock* case_block =
2136 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2137 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2138 } else {
2139 // No free temp, just finish the old loop.
2140 for (; i < entries; i++, current_key++) {
2141 BasicBlock* case_block =
2142 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2143 OpCmpImmBranch(kCondEq, rl_src.reg, current_key, &block_label_list_[case_block->id]);
2144 }
2145 }
2146 }
2147}
2148
2149void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002150 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002151 if (cu_->verbose) {
2152 DumpSparseSwitchTable(table);
2153 }
2154
2155 const uint16_t entries = table[1];
2156 if (entries <= kSmallSwitchThreshold) {
2157 GenSmallPackedSwitch(mir, table_offset, rl_src);
2158 } else {
2159 // Use the backend-specific implementation.
2160 GenLargePackedSwitch(mir, table_offset, rl_src);
2161 }
2162}
2163
2164void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002165 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002166 const uint16_t entries = table[1];
2167 // Chained cmp-and-branch.
2168 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2169 const int32_t* targets = &keys[entries];
2170 rl_src = LoadValue(rl_src, kCoreReg);
2171 for (int i = 0; i < entries; i++) {
2172 int key = keys[i];
2173 BasicBlock* case_block =
2174 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2175 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2176 }
2177}
2178
2179void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002180 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002181 if (cu_->verbose) {
2182 DumpSparseSwitchTable(table);
2183 }
2184
2185 const uint16_t entries = table[1];
2186 if (entries <= kSmallSwitchThreshold) {
2187 GenSmallSparseSwitch(mir, table_offset, rl_src);
2188 } else {
2189 // Use the backend-specific implementation.
2190 GenLargeSparseSwitch(mir, table_offset, rl_src);
2191 }
2192}
2193
Fred Shih37f05ef2014-07-16 18:38:08 -07002194bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2195 switch (size) {
2196 case kReference:
2197 return type == Primitive::kPrimNot;
2198 case k64:
2199 case kDouble:
2200 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2201 case k32:
2202 case kSingle:
2203 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2204 case kSignedHalf:
2205 return type == Primitive::kPrimShort;
2206 case kUnsignedHalf:
2207 return type == Primitive::kPrimChar;
2208 case kSignedByte:
2209 return type == Primitive::kPrimByte;
2210 case kUnsignedByte:
2211 return type == Primitive::kPrimBoolean;
2212 case kWord: // Intentional fallthrough.
2213 default:
2214 return false; // There are no sane types with this op size.
2215 }
2216}
2217
Brian Carlstrom7940e442013-07-12 13:46:57 -07002218} // namespace art