blob: c1c0d329631c80a3e2ef32cd0fa6d6338dfc6e37 [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 */
Ian Rogersd582fa42014-11-05 23:46:43 -080016#include "arch/arm/instruction_set_features_arm.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070017#include "dex/compiler_ir.h"
18#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070019#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070021#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000023#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080024#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070025#include "mirror/object_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080027#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
29namespace art {
30
Andreas Gampe9c3b0892014-04-24 17:33:34 +000031// Shortcuts to repeatedly used long types.
32typedef mirror::ObjectArray<mirror::Object> ObjArray;
33typedef mirror::ObjectArray<mirror::Class> ClassArray;
34
Brian Carlstrom7940e442013-07-12 13:46:57 -070035/*
36 * This source files contains "gen" codegen routines that should
37 * be applicable to most targets. Only mid-level support utilities
38 * and "op" calls may be used here.
39 */
40
41/*
buzbeeb48819d2013-09-14 16:15:25 -070042 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 * blocks.
44 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070045void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 LIR* barrier = NewLIR0(kPseudoBarrier);
47 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070048 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010049 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070050}
51
Mingyao Yange643a172014-04-08 11:02:52 -070052void Mir2Lir::GenDivZeroException() {
53 LIR* branch = OpUnconditionalBranch(nullptr);
54 AddDivZeroCheckSlowPath(branch);
55}
56
57void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070058 LIR* branch = OpCondBranch(c_code, nullptr);
59 AddDivZeroCheckSlowPath(branch);
60}
61
Mingyao Yange643a172014-04-08 11:02:52 -070062void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
63 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070064 AddDivZeroCheckSlowPath(branch);
65}
66
67void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
68 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
69 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080070 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
71 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -070072 }
73
Mingyao Yange643a172014-04-08 11:02:52 -070074 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070075 m2l_->ResetRegPool();
76 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070077 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070078 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -070079 }
80 };
81
82 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
83}
Dave Allisonb373e092014-02-20 16:06:36 -080084
Mingyao Yang80365d92014-04-18 12:10:58 -070085void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
86 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
87 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080088 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
89 RegStorage length_in)
90 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
91 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -070092 }
93
94 void Compile() OVERRIDE {
95 m2l_->ResetRegPool();
96 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070097 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070098 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -070099 }
100
101 private:
102 const RegStorage index_;
103 const RegStorage length_;
104 };
105
106 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
107 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
108}
109
110void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
111 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
112 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800113 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
114 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
115 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700116 }
117
118 void Compile() OVERRIDE {
119 m2l_->ResetRegPool();
120 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700121 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700122
Andreas Gampeccc60262014-07-04 18:02:38 -0700123 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
124 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700125
126 m2l_->OpRegCopy(arg1_32, length_);
127 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700128 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700129 }
130
131 private:
132 const int32_t index_;
133 const RegStorage length_;
134 };
135
136 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
137 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
138}
139
Mingyao Yange643a172014-04-08 11:02:52 -0700140LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
141 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
142 public:
143 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
144 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
145 }
146
147 void Compile() OVERRIDE {
148 m2l_->ResetRegPool();
149 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700150 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700151 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700152 }
153 };
154
155 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
156 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
157 return branch;
158}
159
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800161LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000162 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700163 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600165 // If null check has not been eliminated, reset redundant store tracking.
166 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
167 ResetDefTracking();
168 }
Dave Allisonb373e092014-02-20 16:06:36 -0800169 return nullptr;
170}
171
Dave Allisonf9439142014-03-27 15:10:22 -0700172/* Perform an explicit null-check on a register. */
173LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
174 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
175 return NULL;
176 }
Mingyao Yange643a172014-04-08 11:02:52 -0700177 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700178}
179
Dave Allisonb373e092014-02-20 16:06:36 -0800180void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000181 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800182 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
183 return;
184 }
Dave Allison69dfe512014-07-11 17:11:58 +0000185 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800186 MarkSafepointPC(last_lir_insn_);
187 }
188}
189
Andreas Gampe3c12c512014-06-24 18:46:29 +0000190void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000191 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000192 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
193 return;
194 }
195 MarkSafepointPCAfter(after);
196 }
197}
198
Dave Allisonb373e092014-02-20 16:06:36 -0800199void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000200 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800201 MarkSafepointPC(last_lir_insn_);
202 }
203}
204
buzbee2700f7e2014-03-07 09:46:20 -0800205void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000206 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800207 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
208 return;
209 }
210 // Force an implicit null check by performing a memory operation (load) from the given
211 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800212 RegStorage tmp = AllocTemp();
213 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700214 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800215 FreeTemp(tmp);
216 MarkSafepointPC(load);
217 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218}
219
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700221 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700223 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 switch (opcode) {
225 case Instruction::IF_EQ:
226 cond = kCondEq;
227 break;
228 case Instruction::IF_NE:
229 cond = kCondNe;
230 break;
231 case Instruction::IF_LT:
232 cond = kCondLt;
233 break;
234 case Instruction::IF_GE:
235 cond = kCondGe;
236 break;
237 case Instruction::IF_GT:
238 cond = kCondGt;
239 break;
240 case Instruction::IF_LE:
241 cond = kCondLe;
242 break;
243 default:
244 cond = static_cast<ConditionCode>(0);
245 LOG(FATAL) << "Unexpected opcode " << opcode;
246 }
247
248 // Normalize such that if either operand is constant, src2 will be constant
249 if (rl_src1.is_const) {
250 RegLocation rl_temp = rl_src1;
251 rl_src1 = rl_src2;
252 rl_src2 = rl_temp;
253 cond = FlipComparisonOrder(cond);
254 }
255
buzbee7c02e912014-10-03 13:14:17 -0700256 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 // Is this really an immediate comparison?
258 if (rl_src2.is_const) {
259 // If it's already live in a register or not easily materialized, just keep going
260 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700261 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100263 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800265 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 return;
267 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700268
269 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
270 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
271 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
272 // the 32b literal 0 for null.
273 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
274 // Use the OpCmpImmBranch and ignore the value in the register.
275 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
276 return;
277 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700279
buzbee7c02e912014-10-03 13:14:17 -0700280 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800281 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282}
283
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700286 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
287 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 switch (opcode) {
289 case Instruction::IF_EQZ:
290 cond = kCondEq;
291 break;
292 case Instruction::IF_NEZ:
293 cond = kCondNe;
294 break;
295 case Instruction::IF_LTZ:
296 cond = kCondLt;
297 break;
298 case Instruction::IF_GEZ:
299 cond = kCondGe;
300 break;
301 case Instruction::IF_GTZ:
302 cond = kCondGt;
303 break;
304 case Instruction::IF_LEZ:
305 cond = kCondLe;
306 break;
307 default:
308 cond = static_cast<ConditionCode>(0);
309 LOG(FATAL) << "Unexpected opcode " << opcode;
310 }
buzbee2700f7e2014-03-07 09:46:20 -0800311 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312}
313
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700314void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
316 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800317 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800319 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 }
buzbee2700f7e2014-03-07 09:46:20 -0800321 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 StoreValueWide(rl_dest, rl_result);
323}
324
325void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700326 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700327 rl_src = LoadValue(rl_src, kCoreReg);
328 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
329 OpKind op = kOpInvalid;
330 switch (opcode) {
331 case Instruction::INT_TO_BYTE:
332 op = kOp2Byte;
333 break;
334 case Instruction::INT_TO_SHORT:
335 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700337 case Instruction::INT_TO_CHAR:
338 op = kOp2Char;
339 break;
340 default:
341 LOG(ERROR) << "Bad int conversion type";
342 }
buzbee2700f7e2014-03-07 09:46:20 -0800343 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700344 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345}
346
Andreas Gampe98430592014-07-27 19:44:50 -0700347/*
348 * Let helper function take care of everything. Will call
349 * Array::AllocFromCode(type_idx, method, count);
350 * Note: AllocFromCode will handle checks for errNegativeArraySize.
351 */
352void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
353 RegLocation rl_src) {
354 FlushAllRegs(); /* Everything to home location */
355 const DexFile* dex_file = cu_->dex_file;
356 CompilerDriver* driver = cu_->compiler_driver;
357 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800358 bool is_type_initialized; // Ignored as an array does not have an initializer.
359 bool use_direct_type_ptr;
360 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700361 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800362 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700363 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
364 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800365 // The fast path.
366 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700367 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700368 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
369 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800370 } else {
371 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700372 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
373 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800374 }
375 } else {
376 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700377 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800378 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700380 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
Andreas Gampe98430592014-07-27 19:44:50 -0700382 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383}
384
385/*
386 * Similar to GenNewArray, but with post-allocation initialization.
387 * Verifier guarantees we're dealing with an array class. Current
388 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
389 * Current code also throws internal unimp if not 'L', '[' or 'I'.
390 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700391void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 int elems = info->num_arg_words;
393 int type_idx = info->index;
394 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700395 QuickEntrypointEnum target;
396 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
397 type_idx)) {
398 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700400 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 }
Andreas Gampe98430592014-07-27 19:44:50 -0700402 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700403 FreeTemp(TargetReg(kArg2, kNotWide));
404 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 /*
406 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
407 * return region. Because AllocFromCode placed the new array
408 * in kRet0, we'll just lock it into place. When debugger support is
409 * added, it may be necessary to additionally copy all return
410 * values to a home location in thread-local storage
411 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700412 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700413 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414
415 // TODO: use the correct component size, currently all supported types
416 // share array alignment with ints (see comment at head of function)
417 size_t component_size = sizeof(int32_t);
418
Vladimir Markobf535be2014-11-19 18:52:35 +0000419 if (elems > 5) {
420 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 /*
422 * Bit of ugliness here. We're going generate a mem copy loop
423 * on the register range, but it is possible that some regs
424 * in the range have been promoted. This is unlikely, but
425 * before generating the copy, we'll just force a flush
426 * of any regs in the source range that have been promoted to
427 * home location.
428 */
429 for (int i = 0; i < elems; i++) {
430 RegLocation loc = UpdateLoc(info->args[i]);
431 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100432 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600433 if (loc.ref) {
434 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
435 } else {
436 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
437 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439 }
440 /*
441 * TUNING note: generated code here could be much improved, but
442 * this is an uncommon operation and isn't especially performance
443 * critical.
444 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700445 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700446 RegStorage r_src = AllocTempRef();
447 RegStorage r_dst = AllocTempRef();
448 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800449 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700450 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700452 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700453 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 break;
455 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700456 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700457 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 r_val = AllocTemp();
459 break;
460 case kMips:
461 r_val = AllocTemp();
462 break;
463 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
464 }
465 // Set up source pointer
466 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700467 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700469 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 mirror::Array::DataOffset(component_size).Int32Value());
471 // Set up the loop counter (known to be > 0)
472 LoadConstant(r_idx, elems - 1);
473 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800474 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100476 {
477 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
478 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
479 // NOTE: No dalvik register annotation, local optimizations will be stopped
480 // by the loop boundaries.
481 }
buzbee695d13a2014-04-19 13:32:20 -0700482 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800484 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700485 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700487 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 -mirror::Array::DataOffset(component_size).Int32Value());
489 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000490 FreeTemp(r_idx);
491 FreeTemp(r_dst);
492 FreeTemp(r_src);
493 } else {
494 DCHECK_LE(elems, 5); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 // TUNING: interleave
496 for (int i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600497 RegLocation rl_arg;
498 if (info->args[i].ref) {
499 rl_arg = LoadValue(info->args[i], kRefReg);
500 StoreRefDisp(ref_reg,
501 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
502 kNotVolatile);
503 } else {
504 rl_arg = LoadValue(info->args[i], kCoreReg);
505 Store32Disp(ref_reg,
506 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
507 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800509 if (IsTemp(rl_arg.reg)) {
510 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 }
512 }
513 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000514 if (elems != 0 && info->args[0].ref) {
515 // If there is at least one potentially non-null value, unconditionally mark the GC card.
516 for (int i = 0; i < elems; i++) {
517 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
518 UnconditionallyMarkGCCard(ref_reg);
519 break;
520 }
521 }
522 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700524 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 }
526}
527
Ian Rogers832336b2014-10-08 15:35:22 -0700528/*
529 * Array data table format:
530 * ushort ident = 0x0300 magic value
531 * ushort width width of each element in the table
532 * uint size number of elements in the table
533 * ubyte data[size*width] table of data values (may contain a single-byte
534 * padding at the end)
535 *
536 * Total size is 4+(width * size + 1)/2 16-bit code units.
537 */
538void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
539 if (kIsDebugBuild) {
540 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
541 const Instruction::ArrayDataPayload* payload =
542 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
543 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
544 }
545 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
546 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
547}
548
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800549//
550// Slow path to ensure a class is initialized for sget/sput.
551//
552class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
553 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100554 // There are up to two branches to the static field slow path, the "unresolved" when the type
555 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
556 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800557 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100558 RegStorage r_base)
559 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
560 second_branch_(unresolved != nullptr ? uninit : nullptr),
561 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800562 }
563
564 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100565 LIR* target = GenerateTargetLabel();
566 if (second_branch_ != nullptr) {
567 second_branch_->target = target;
568 }
Andreas Gampe98430592014-07-27 19:44:50 -0700569 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800570 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700571 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800572
573 m2l_->OpUnconditionalBranch(cont_);
574 }
575
576 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100577 // Second branch to the slow path, or null if there's only one branch.
578 LIR* const second_branch_;
579
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800580 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800581 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800582};
583
Fred Shih37f05ef2014-07-16 18:38:08 -0700584void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
586 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700587 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800589 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000590 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100592 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700593 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000594 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
595 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800596 if (IsTemp(rl_method.reg)) {
597 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 }
599 } else {
600 // Medium path, static storage base in a different class which requires checks that the other
601 // class is initialized.
602 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000603 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 // May do runtime call so everything to home locations.
605 FlushAllRegs();
606 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700607 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 LockTemp(r_method);
609 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700610 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800611 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000612 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
613 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000614 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000615 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100617 LIR* unresolved_branch = nullptr;
618 if (!field_info.IsClassInDexCache() &&
619 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
620 // Check if r_base is NULL.
621 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
622 }
623 LIR* uninit_branch = nullptr;
624 if (!field_info.IsClassInitialized() &&
625 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
626 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700627 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100629 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800630 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000631 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100632 FreeTemp(r_tmp);
633 }
634 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
635 // The slow path is invoked if the r_base is NULL or the class pointed
636 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800637 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800638 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000639 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800640
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100641 if (uninit_branch != nullptr) {
642 // Ensure load of status and store of value don't re-order.
643 // TODO: Presumably the actual value store is control-dependent on the status load,
644 // and will thus not be reordered in any case, since stores are never speculated.
645 // Does later code "know" that the class is now initialized? If so, we still
646 // need the barrier to guard later static loads.
647 GenMemBarrier(kLoadAny);
648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 FreeTemp(r_method);
651 }
652 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700653 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
654 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100655 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100657 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700659 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000660 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
661 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100662 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700663 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000664 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700666 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800667 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800669 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 } else {
671 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700672 QuickEntrypointEnum target;
673 switch (size) {
674 case kReference:
675 target = kQuickSetObjStatic;
676 break;
677 case k64:
678 case kDouble:
679 target = kQuickSet64Static;
680 break;
681 case k32:
682 case kSingle:
683 target = kQuickSet32Static;
684 break;
685 case kSignedHalf:
686 case kUnsignedHalf:
687 target = kQuickSet16Static;
688 break;
689 case kSignedByte:
690 case kUnsignedByte:
691 target = kQuickSet8Static;
692 break;
693 case kWord: // Intentional fallthrough.
694 default:
695 LOG(FATAL) << "Can't determine entrypoint for: " << size;
696 target = kQuickSet32Static;
697 }
Andreas Gampe98430592014-07-27 19:44:50 -0700698 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 }
700}
701
Fred Shih37f05ef2014-07-16 18:38:08 -0700702void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000703 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
704 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700705
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700706 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000707 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800708 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 // Fast path, static storage base is this method's class
711 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700712 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000713 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
714 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 } else {
716 // Medium path, static storage base in a different class which requires checks that the other
717 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 // May do runtime call so everything to home locations.
720 FlushAllRegs();
721 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700722 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 LockTemp(r_method);
724 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700725 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800726 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000727 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
728 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000729 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000730 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800731 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100732 LIR* unresolved_branch = nullptr;
733 if (!field_info.IsClassInDexCache() &&
734 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
735 // Check if r_base is NULL.
736 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
737 }
738 LIR* uninit_branch = nullptr;
739 if (!field_info.IsClassInitialized() &&
740 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
741 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700742 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800743 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100744 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800745 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000746 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100747 FreeTemp(r_tmp);
748 }
749 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
750 // The slow path is invoked if the r_base is NULL or the class pointed
751 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800752 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800753 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000754 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800755
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100756 if (uninit_branch != nullptr) {
757 // Ensure load of status and load of value don't re-order.
758 GenMemBarrier(kLoadAny);
759 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 FreeTemp(r_method);
762 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800763 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700764 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100765 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800766
Vladimir Marko674744e2014-04-24 15:18:26 +0100767 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700768 if (IsRef(size)) {
769 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000770 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
771 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100772 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700773 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000774 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800775 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100776 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800777
Fred Shih37f05ef2014-07-16 18:38:08 -0700778 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 StoreValueWide(rl_dest, rl_result);
780 } else {
781 StoreValue(rl_dest, rl_result);
782 }
783 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700784 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700786 QuickEntrypointEnum target;
787 switch (type) {
788 case Primitive::kPrimNot:
789 target = kQuickGetObjStatic;
790 break;
791 case Primitive::kPrimLong:
792 case Primitive::kPrimDouble:
793 target = kQuickGet64Static;
794 break;
795 case Primitive::kPrimInt:
796 case Primitive::kPrimFloat:
797 target = kQuickGet32Static;
798 break;
799 case Primitive::kPrimShort:
800 target = kQuickGetShortStatic;
801 break;
802 case Primitive::kPrimChar:
803 target = kQuickGetCharStatic;
804 break;
805 case Primitive::kPrimByte:
806 target = kQuickGetByteStatic;
807 break;
808 case Primitive::kPrimBoolean:
809 target = kQuickGetBooleanStatic;
810 break;
811 case Primitive::kPrimVoid: // Intentional fallthrough.
812 default:
813 LOG(FATAL) << "Can't determine entrypoint for: " << type;
814 target = kQuickGet32Static;
815 }
Andreas Gampe98430592014-07-27 19:44:50 -0700816 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
817
Douglas Leung2db3e262014-06-25 16:02:55 -0700818 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700819 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700820 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 StoreValueWide(rl_dest, rl_result);
822 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700823 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824 StoreValue(rl_dest, rl_result);
825 }
826 }
827}
828
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800829// Generate code for all slow paths.
830void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700831 // We should check slow_paths_.Size() every time, because a new slow path
832 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100833 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800834 slowpath->Compile();
835 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100836 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800837}
838
Fred Shih37f05ef2014-07-16 18:38:08 -0700839void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
840 RegLocation rl_dest, 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.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700843 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700844 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700845 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000846 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700847 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100848 GenNullCheck(rl_obj.reg, opt_flags);
849 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
850 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000851 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700852 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000853 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
854 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100855 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700856 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000857 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100858 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000859 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700860 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 StoreValueWide(rl_dest, rl_result);
862 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 StoreValue(rl_dest, rl_result);
864 }
865 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700866 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
867 QuickEntrypointEnum target;
868 switch (type) {
869 case Primitive::kPrimNot:
870 target = kQuickGetObjInstance;
871 break;
872 case Primitive::kPrimLong:
873 case Primitive::kPrimDouble:
874 target = kQuickGet64Instance;
875 break;
876 case Primitive::kPrimFloat:
877 case Primitive::kPrimInt:
878 target = kQuickGet32Instance;
879 break;
880 case Primitive::kPrimShort:
881 target = kQuickGetShortInstance;
882 break;
883 case Primitive::kPrimChar:
884 target = kQuickGetCharInstance;
885 break;
886 case Primitive::kPrimByte:
887 target = kQuickGetByteInstance;
888 break;
889 case Primitive::kPrimBoolean:
890 target = kQuickGetBooleanInstance;
891 break;
892 case Primitive::kPrimVoid: // Intentional fallthrough.
893 default:
894 LOG(FATAL) << "Can't determine entrypoint for: " << type;
895 target = kQuickGet32Instance;
896 }
Andreas Gampe98430592014-07-27 19:44:50 -0700897 // Second argument of pGetXXInstance is always a reference.
898 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
899 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
900
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700901 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700902 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700903 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 StoreValueWide(rl_dest, rl_result);
905 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700906 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 StoreValue(rl_dest, rl_result);
908 }
909 }
910}
911
Vladimir Markobe0e5462014-02-26 11:24:15 +0000912void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700913 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000914 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
915 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700916 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700917 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700918 // Dex code never writes to the class field.
919 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
920 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700921 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700922 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100923 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 } else {
925 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100926 }
927 GenNullCheck(rl_obj.reg, opt_flags);
928 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000929 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700930 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000931 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
932 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100933 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700934 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000935 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100936 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000937 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700938 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100939 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 }
941 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700942 QuickEntrypointEnum target;
943 switch (size) {
944 case kReference:
945 target = kQuickSetObjInstance;
946 break;
947 case k64:
948 case kDouble:
949 target = kQuickSet64Instance;
950 break;
951 case k32:
952 case kSingle:
953 target = kQuickSet32Instance;
954 break;
955 case kSignedHalf:
956 case kUnsignedHalf:
957 target = kQuickSet16Instance;
958 break;
959 case kSignedByte:
960 case kUnsignedByte:
961 target = kQuickSet8Instance;
962 break;
963 case kWord: // Intentional fallthrough.
964 default:
965 LOG(FATAL) << "Can't determine entrypoint for: " << size;
966 target = kQuickSet32Instance;
967 }
Andreas Gampe98430592014-07-27 19:44:50 -0700968 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
969 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 }
971}
972
Ian Rogersa9a82542013-10-04 11:17:26 -0700973void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
974 RegLocation rl_src) {
975 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
976 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
977 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700978 QuickEntrypointEnum target = needs_range_check
979 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
980 : kQuickAputObjectWithBoundCheck)
981 : kQuickAputObject;
982 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700983}
984
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700985void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700987 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700988 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700990 *cu_->dex_file,
991 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 // Call out to helper which resolves type and verifies access.
993 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700994 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700995 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 StoreValue(rl_dest, rl_result);
997 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800998 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 // We're don't need access checks, load type from dex cache
1000 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001001 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001002 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001003 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001004 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
1006 type_idx) || SLOW_TYPE_PATH) {
1007 // Slow path, at runtime test if type is null and if so initialize
1008 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001009 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001010 LIR* cont = NewLIR0(kPseudoTargetLabel);
1011
1012 // Object to generate the slow path for class resolution.
1013 class SlowPath : public LIRSlowPath {
1014 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001015 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1016 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
1017 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1018 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001019 }
1020
1021 void Compile() {
1022 GenerateTargetLabel();
1023
Andreas Gampe98430592014-07-27 19:44:50 -07001024 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001025 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001026 m2l_->OpUnconditionalBranch(cont_);
1027 }
1028
1029 private:
1030 const int type_idx_;
1031 const RegLocation rl_method_;
1032 const RegLocation rl_result_;
1033 };
1034
1035 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001036 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001037
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001039 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 // Fast path, we're done - just store result
1041 StoreValue(rl_dest, rl_result);
1042 }
1043 }
1044}
1045
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001046void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001048 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1049 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1051 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1052 // slow path, resolve string if not in dex cache
1053 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001054 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001055
1056 // If the Method* is already in a register, we can save a copy.
1057 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001058 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001059 if (rl_method.location == kLocPhysReg) {
1060 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001061 DCHECK(!IsTemp(rl_method.reg));
1062 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001063 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001064 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001065 LoadCurrMethodDirect(r_method);
1066 }
buzbee695d13a2014-04-19 13:32:20 -07001067 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001068 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001069
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001071 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1072 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001073 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001074
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001075 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001076 // Object to generate the slow path for string resolution.
1077 class SlowPath : public LIRSlowPath {
1078 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001079 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1080 int32_t string_idx_in) :
1081 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1082 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001083 }
1084
1085 void Compile() {
1086 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001087 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001088 m2l_->OpUnconditionalBranch(cont_);
1089 }
1090
1091 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001092 const RegStorage r_method_;
1093 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001094 };
1095
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001096 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001098
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001100 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 } else {
1102 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001103 RegStorage res_reg = AllocTempRef();
1104 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001105 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1106 kNotVolatile);
1107 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 StoreValue(rl_dest, rl_result);
1109 }
1110}
1111
Andreas Gampe98430592014-07-27 19:44:50 -07001112/*
1113 * Let helper function take care of everything. Will
1114 * call Class::NewInstanceFromCode(type_idx, method);
1115 */
1116void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1117 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 // alloc will always check for resolution, do we also need to verify
1119 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001120 const DexFile* dex_file = cu_->dex_file;
1121 CompilerDriver* driver = cu_->compiler_driver;
1122 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001123 bool is_type_initialized;
1124 bool use_direct_type_ptr;
1125 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001126 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001127 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001128 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1129 &direct_type_ptr, &is_finalizable) &&
1130 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001131 // The fast path.
1132 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001133 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001134 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001135 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001136 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001137 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001138 }
1139 } else {
1140 // Use the direct pointer.
1141 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001142 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001143 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001144 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001145 }
1146 }
1147 } else {
1148 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001149 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001150 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001152 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 }
Andreas Gampe98430592014-07-27 19:44:50 -07001154 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155}
1156
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001159 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160}
1161
1162// For final classes there are no sub-classes to check and so we can answer the instance-of
1163// question with simple comparisons.
1164void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1165 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001166 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001167 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001168
buzbeea0cd2d72014-06-01 09:33:49 -07001169 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001171 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001172 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001174 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 }
1176 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001177 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178
buzbeea0cd2d72014-06-01 09:33:49 -07001179 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1180 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181
1182 LoadCurrMethodDirect(check_class);
1183 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001184 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1185 kNotVolatile);
1186 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1187 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 } else {
buzbee695d13a2014-04-19 13:32:20 -07001189 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001190 check_class, kNotVolatile);
1191 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1192 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001193 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001194 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 }
1196
buzbee695d13a2014-04-19 13:32:20 -07001197 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 if (cu_->instruction_set == kThumb2) {
1199 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001200 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001202 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001204 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 }
1206 LIR* target = NewLIR0(kPseudoTargetLabel);
1207 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 FreeTemp(object_class);
1209 FreeTemp(check_class);
1210 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001211 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 FreeTemp(result_reg);
1213 }
1214 StoreValue(rl_dest, rl_result);
1215}
1216
1217void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1218 bool type_known_abstract, bool use_declaring_class,
1219 bool can_assume_type_is_in_dex_cache,
1220 uint32_t type_idx, RegLocation rl_dest,
1221 RegLocation rl_src) {
1222 FlushAllRegs();
1223 // May generate a call - use explicit registers
1224 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001225 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001226 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001227 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001228 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1229 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 if (needs_access_check) {
1231 // Check we have access to type_idx and if not throw IllegalAccessError,
1232 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001233 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001234 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1235 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001237 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001238 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001239 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001241 if (can_assume_type_is_in_dex_cache) {
1242 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001243 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001244 }
1245
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001247 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001248 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001249 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001250 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001252 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1253 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1254
1255 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001256 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001257
1258 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1259 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001260 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1261 RegLocation rl_src_in)
1262 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1263 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001264 }
1265
1266 void Compile() OVERRIDE {
1267 GenerateTargetLabel();
1268
Andreas Gampe98430592014-07-27 19:44:50 -07001269 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001270 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1271 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001272 m2l_->OpUnconditionalBranch(cont_);
1273 }
1274
1275 private:
1276 uint32_t type_idx_;
1277 RegLocation rl_src_;
1278 };
1279
1280 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1281 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 }
1283 }
1284 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001285 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001286 if (!IsSameReg(rl_result.reg, ref_reg)) {
1287 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001288 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001290 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291
1292 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001293 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001295 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1296 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1298 LIR* branchover = NULL;
1299 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001300 // rl_result == ref == class.
1301 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001302 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 } else {
1304 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001305 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001306 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 if (!type_known_abstract) {
1308 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001309 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001310 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001311 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001313 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001315 if (it != nullptr) {
1316 OpEndIT(it);
1317 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 FreeTemp(r_tgt);
1319 } else {
1320 if (!type_known_abstract) {
1321 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001322 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001323 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001325
Serguei Katkov9ee45192014-07-17 14:39:03 +07001326 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001327 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 }
1329 }
1330 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001331 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 /* branch targets here */
1333 LIR* target = NewLIR0(kPseudoTargetLabel);
1334 StoreValue(rl_dest, rl_result);
1335 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001336 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 branchover->target = target;
1338 }
1339}
1340
1341void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1342 bool type_known_final, type_known_abstract, use_declaring_class;
1343 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1344 *cu_->dex_file,
1345 type_idx,
1346 &type_known_final,
1347 &type_known_abstract,
1348 &use_declaring_class);
1349 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1350 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1351
1352 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1353 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1354 } else {
1355 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1356 use_declaring_class, can_assume_type_is_in_dex_cache,
1357 type_idx, rl_dest, rl_src);
1358 }
1359}
1360
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001361void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 bool type_known_final, type_known_abstract, use_declaring_class;
1363 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1364 *cu_->dex_file,
1365 type_idx,
1366 &type_known_final,
1367 &type_known_abstract,
1368 &use_declaring_class);
1369 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1370 // of the exception throw path.
1371 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001372 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 // Verifier type analysis proved this check cast would never cause an exception.
1374 return;
1375 }
1376 FlushAllRegs();
1377 // May generate a call - use explicit registers
1378 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001379 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001380 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001381 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 if (needs_access_check) {
1383 // Check we have access to type_idx and if not throw IllegalAccessError,
1384 // returns Class* in kRet0
1385 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001386 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001387 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001389 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001390 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 } else {
1392 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001393 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001394 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001395 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001396 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1398 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001399 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1400 LIR* cont = NewLIR0(kPseudoTargetLabel);
1401
1402 // Slow path to initialize the type. Executed if the type is NULL.
1403 class SlowPath : public LIRSlowPath {
1404 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001405 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1406 const RegStorage class_reg_in) :
1407 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1408 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001409 }
1410
1411 void Compile() {
1412 GenerateTargetLabel();
1413
1414 // Call out to helper, which will return resolved type in kArg0
1415 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001416 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1417 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001418 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001419 m2l_->OpUnconditionalBranch(cont_);
1420 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001421
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001422 public:
1423 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001424 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001425 };
1426
buzbee2700f7e2014-03-07 09:46:20 -08001427 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 }
1429 }
1430 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001431 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001432
1433 // Slow path for the case where the classes are not equal. In this case we need
1434 // to call a helper function to do the check.
1435 class SlowPath : public LIRSlowPath {
1436 public:
1437 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1438 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1439 }
1440
1441 void Compile() {
1442 GenerateTargetLabel();
1443
1444 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001445 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1446 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001447 }
Andreas Gampe98430592014-07-27 19:44:50 -07001448 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1449 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001450 m2l_->OpUnconditionalBranch(cont_);
1451 }
1452
1453 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001454 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001455 };
1456
1457 if (type_known_abstract) {
1458 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001459 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001460 LIR* cont = NewLIR0(kPseudoTargetLabel);
1461 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1462 } else {
1463 // Harder, more common case. We need to generate a forward branch over the load
1464 // if the target is null. If it's non-null we perform the load and branch to the
1465 // slow path if the classes are not equal.
1466
1467 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001468 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001469 /* load object->klass_ */
1470 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001471 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1472 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001473
Andreas Gampeccc60262014-07-04 18:02:38 -07001474 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001475 LIR* cont = NewLIR0(kPseudoTargetLabel);
1476
1477 // Add the slow path that will not perform load since this is already done.
1478 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1479
1480 // Set the null check to branch to the continuation.
1481 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 }
1483}
1484
1485void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001486 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 RegLocation rl_result;
1488 if (cu_->instruction_set == kThumb2) {
1489 /*
1490 * NOTE: This is the one place in the code in which we might have
1491 * as many as six live temporary registers. There are 5 in the normal
1492 * set for Arm. Until we have spill capabilities, temporarily add
1493 * lr to the temp set. It is safe to do this locally, but note that
1494 * lr is used explicitly elsewhere in the code generator and cannot
1495 * normally be used as a general temp register.
1496 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001497 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1498 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 }
1500 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1501 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1502 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1503 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001504 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1505 RegStorage t_reg = AllocTemp();
1506 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1507 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1508 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 FreeTemp(t_reg);
1510 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001511 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1512 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 }
1514 /*
1515 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1516 * following StoreValueWide might need to allocate a temp register.
1517 * To further work around the lack of a spill capability, explicitly
1518 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1519 * Remove when spill is functional.
1520 */
1521 FreeRegLocTemps(rl_result, rl_src1);
1522 FreeRegLocTemps(rl_result, rl_src2);
1523 StoreValueWide(rl_dest, rl_result);
1524 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001525 Clobber(TargetReg(kLr, kNotWide));
1526 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 }
1528}
1529
Andreas Gampe98430592014-07-27 19:44:50 -07001530void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1531 RegLocation rl_src1, RegLocation rl_shift) {
1532 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 switch (opcode) {
1534 case Instruction::SHL_LONG:
1535 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001536 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 break;
1538 case Instruction::SHR_LONG:
1539 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001540 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 break;
1542 case Instruction::USHR_LONG:
1543 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001544 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 break;
1546 default:
1547 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001548 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
Andreas Gampe98430592014-07-27 19:44:50 -07001550 FlushAllRegs(); /* Send everything to home location */
1551 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001552 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 StoreValueWide(rl_dest, rl_result);
1554}
1555
1556
1557void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001558 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001559 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 OpKind op = kOpBkpt;
1561 bool is_div_rem = false;
1562 bool check_zero = false;
1563 bool unary = false;
1564 RegLocation rl_result;
1565 bool shift_op = false;
1566 switch (opcode) {
1567 case Instruction::NEG_INT:
1568 op = kOpNeg;
1569 unary = true;
1570 break;
1571 case Instruction::NOT_INT:
1572 op = kOpMvn;
1573 unary = true;
1574 break;
1575 case Instruction::ADD_INT:
1576 case Instruction::ADD_INT_2ADDR:
1577 op = kOpAdd;
1578 break;
1579 case Instruction::SUB_INT:
1580 case Instruction::SUB_INT_2ADDR:
1581 op = kOpSub;
1582 break;
1583 case Instruction::MUL_INT:
1584 case Instruction::MUL_INT_2ADDR:
1585 op = kOpMul;
1586 break;
1587 case Instruction::DIV_INT:
1588 case Instruction::DIV_INT_2ADDR:
1589 check_zero = true;
1590 op = kOpDiv;
1591 is_div_rem = true;
1592 break;
1593 /* NOTE: returns in kArg1 */
1594 case Instruction::REM_INT:
1595 case Instruction::REM_INT_2ADDR:
1596 check_zero = true;
1597 op = kOpRem;
1598 is_div_rem = true;
1599 break;
1600 case Instruction::AND_INT:
1601 case Instruction::AND_INT_2ADDR:
1602 op = kOpAnd;
1603 break;
1604 case Instruction::OR_INT:
1605 case Instruction::OR_INT_2ADDR:
1606 op = kOpOr;
1607 break;
1608 case Instruction::XOR_INT:
1609 case Instruction::XOR_INT_2ADDR:
1610 op = kOpXor;
1611 break;
1612 case Instruction::SHL_INT:
1613 case Instruction::SHL_INT_2ADDR:
1614 shift_op = true;
1615 op = kOpLsl;
1616 break;
1617 case Instruction::SHR_INT:
1618 case Instruction::SHR_INT_2ADDR:
1619 shift_op = true;
1620 op = kOpAsr;
1621 break;
1622 case Instruction::USHR_INT:
1623 case Instruction::USHR_INT_2ADDR:
1624 shift_op = true;
1625 op = kOpLsr;
1626 break;
1627 default:
1628 LOG(FATAL) << "Invalid word arith op: " << opcode;
1629 }
1630 if (!is_div_rem) {
1631 if (unary) {
1632 rl_src1 = LoadValue(rl_src1, kCoreReg);
1633 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001634 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001636 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001637 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001638 RegStorage t_reg = AllocTemp();
1639 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 rl_src1 = LoadValue(rl_src1, kCoreReg);
1641 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001642 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 FreeTemp(t_reg);
1644 } else {
1645 rl_src1 = LoadValue(rl_src1, kCoreReg);
1646 rl_src2 = LoadValue(rl_src2, kCoreReg);
1647 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001648 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 }
1650 }
1651 StoreValue(rl_dest, rl_result);
1652 } else {
Dave Allison70202782013-10-22 17:52:19 -07001653 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 +01001654 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 rl_src1 = LoadValue(rl_src1, kCoreReg);
1656 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001657 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001658 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 }
buzbee2700f7e2014-03-07 09:46:20 -08001660 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001661 done = true;
1662 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001663 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1664 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001665 // Use ARM SDIV instruction for division. For remainder we also need to
1666 // calculate using a MUL and subtract.
1667 rl_src1 = LoadValue(rl_src1, kCoreReg);
1668 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001669 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001670 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001671 }
buzbee2700f7e2014-03-07 09:46:20 -08001672 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001673 done = true;
1674 }
1675 }
1676
1677 // If we haven't already generated the code use the callout function.
1678 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001680 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001681 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001682 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001683 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001684 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 }
Dave Allison70202782013-10-22 17:52:19 -07001686 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001687 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001689 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 else
1691 rl_result = GetReturnAlt();
1692 }
1693 StoreValue(rl_dest, rl_result);
1694 }
1695}
1696
1697/*
1698 * The following are the first-level codegen routines that analyze the format
1699 * of each bytecode then either dispatch special purpose codegen routines
1700 * or produce corresponding Thumb instructions directly.
1701 */
1702
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001704static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 x &= x - 1;
1706 return (x & (x - 1)) == 0;
1707}
1708
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1710// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001711bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001712 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1714 return false;
1715 }
1716 // No divide instruction for Arm, so check for more special cases
1717 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001718 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 }
1720 int k = LowestSetBit(lit);
1721 if (k >= 30) {
1722 // Avoid special cases.
1723 return false;
1724 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 rl_src = LoadValue(rl_src, kCoreReg);
1726 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001727 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001728 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 if (lit == 2) {
1730 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1732 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1733 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001735 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001737 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1738 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 }
1740 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001741 RegStorage t_reg1 = AllocTemp();
1742 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001744 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1745 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001746 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001747 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001749 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001753 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754 }
1755 }
1756 StoreValue(rl_dest, rl_result);
1757 return true;
1758}
1759
1760// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1761// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001762bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001763 if (lit < 0) {
1764 return false;
1765 }
1766 if (lit == 0) {
1767 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1768 LoadConstant(rl_result.reg, 0);
1769 StoreValue(rl_dest, rl_result);
1770 return true;
1771 }
1772 if (lit == 1) {
1773 rl_src = LoadValue(rl_src, kCoreReg);
1774 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1775 OpRegCopy(rl_result.reg, rl_src.reg);
1776 StoreValue(rl_dest, rl_result);
1777 return true;
1778 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001779 // There is RegRegRegShift on Arm, so check for more special cases
1780 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001781 return EasyMultiply(rl_src, rl_dest, lit);
1782 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 // Can we simplify this multiplication?
1784 bool power_of_two = false;
1785 bool pop_count_le2 = false;
1786 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001787 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 power_of_two = true;
1789 } else if (IsPopCountLE2(lit)) {
1790 pop_count_le2 = true;
1791 } else if (IsPowerOfTwo(lit + 1)) {
1792 power_of_two_minus_one = true;
1793 } else {
1794 return false;
1795 }
1796 rl_src = LoadValue(rl_src, kCoreReg);
1797 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1798 if (power_of_two) {
1799 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001800 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 } else if (pop_count_le2) {
1802 // Shift and add and shift.
1803 int first_bit = LowestSetBit(lit);
1804 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1805 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1806 } else {
1807 // Reverse subtract: (src << (shift + 1)) - src.
1808 DCHECK(power_of_two_minus_one);
1809 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001810 RegStorage t_reg = AllocTemp();
1811 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1812 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 }
1814 StoreValue(rl_dest, rl_result);
1815 return true;
1816}
1817
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001818// Returns true if it generates instructions.
1819bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1820 RegLocation rl_src2) {
1821 if (!rl_src2.is_const ||
1822 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1823 return false;
1824 }
1825
1826 if (!rl_src2.wide) {
1827 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1828 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1829 // Generate multiply by reciprocal instead of div.
1830 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1831 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1832 return true;
1833 }
1834 } else {
1835 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1836 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1837 // Generate multiply by reciprocal instead of div.
1838 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1839 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1840 return true;
1841 }
1842 }
1843 return false;
1844}
1845
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001847 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 RegLocation rl_result;
1849 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1850 int shift_op = false;
1851 bool is_div = false;
1852
1853 switch (opcode) {
1854 case Instruction::RSUB_INT_LIT8:
1855 case Instruction::RSUB_INT: {
1856 rl_src = LoadValue(rl_src, kCoreReg);
1857 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1858 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001859 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001861 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1862 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001863 }
1864 StoreValue(rl_dest, rl_result);
1865 return;
1866 }
1867
1868 case Instruction::SUB_INT:
1869 case Instruction::SUB_INT_2ADDR:
1870 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001871 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 case Instruction::ADD_INT:
1873 case Instruction::ADD_INT_2ADDR:
1874 case Instruction::ADD_INT_LIT8:
1875 case Instruction::ADD_INT_LIT16:
1876 op = kOpAdd;
1877 break;
1878 case Instruction::MUL_INT:
1879 case Instruction::MUL_INT_2ADDR:
1880 case Instruction::MUL_INT_LIT8:
1881 case Instruction::MUL_INT_LIT16: {
1882 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1883 return;
1884 }
1885 op = kOpMul;
1886 break;
1887 }
1888 case Instruction::AND_INT:
1889 case Instruction::AND_INT_2ADDR:
1890 case Instruction::AND_INT_LIT8:
1891 case Instruction::AND_INT_LIT16:
1892 op = kOpAnd;
1893 break;
1894 case Instruction::OR_INT:
1895 case Instruction::OR_INT_2ADDR:
1896 case Instruction::OR_INT_LIT8:
1897 case Instruction::OR_INT_LIT16:
1898 op = kOpOr;
1899 break;
1900 case Instruction::XOR_INT:
1901 case Instruction::XOR_INT_2ADDR:
1902 case Instruction::XOR_INT_LIT8:
1903 case Instruction::XOR_INT_LIT16:
1904 op = kOpXor;
1905 break;
1906 case Instruction::SHL_INT_LIT8:
1907 case Instruction::SHL_INT:
1908 case Instruction::SHL_INT_2ADDR:
1909 lit &= 31;
1910 shift_op = true;
1911 op = kOpLsl;
1912 break;
1913 case Instruction::SHR_INT_LIT8:
1914 case Instruction::SHR_INT:
1915 case Instruction::SHR_INT_2ADDR:
1916 lit &= 31;
1917 shift_op = true;
1918 op = kOpAsr;
1919 break;
1920 case Instruction::USHR_INT_LIT8:
1921 case Instruction::USHR_INT:
1922 case Instruction::USHR_INT_2ADDR:
1923 lit &= 31;
1924 shift_op = true;
1925 op = kOpLsr;
1926 break;
1927
1928 case Instruction::DIV_INT:
1929 case Instruction::DIV_INT_2ADDR:
1930 case Instruction::DIV_INT_LIT8:
1931 case Instruction::DIV_INT_LIT16:
1932 case Instruction::REM_INT:
1933 case Instruction::REM_INT_2ADDR:
1934 case Instruction::REM_INT_LIT8:
1935 case Instruction::REM_INT_LIT16: {
1936 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001937 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 return;
1939 }
buzbee11b63d12013-08-27 07:34:17 -07001940 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001942 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001943 (opcode == Instruction::DIV_INT_LIT16)) {
1944 is_div = true;
1945 } else {
1946 is_div = false;
1947 }
buzbee11b63d12013-08-27 07:34:17 -07001948 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1949 return;
1950 }
Dave Allison70202782013-10-22 17:52:19 -07001951
1952 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001953 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001955 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001956 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001957 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001958 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1959 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001960 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001961 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1962 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001963 // Use ARM SDIV instruction for division. For remainder we also need to
1964 // calculate using a MUL and subtract.
1965 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001966 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001967 done = true;
1968 }
1969 }
1970
1971 if (!done) {
1972 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001973 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1974 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001975 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001977 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 else
1979 rl_result = GetReturnAlt();
1980 }
1981 StoreValue(rl_dest, rl_result);
1982 return;
1983 }
1984 default:
1985 LOG(FATAL) << "Unexpected opcode " << opcode;
1986 }
1987 rl_src = LoadValue(rl_src, kCoreReg);
1988 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001989 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001991 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001993 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 }
1995 StoreValue(rl_dest, rl_result);
1996}
1997
Andreas Gampe98430592014-07-27 19:44:50 -07001998void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001999 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002000 RegLocation rl_result;
2001 OpKind first_op = kOpBkpt;
2002 OpKind second_op = kOpBkpt;
2003 bool call_out = false;
2004 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002005 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2006 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002007
2008 switch (opcode) {
2009 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002010 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2011 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002013 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002014 RegStorage t_reg = AllocTemp();
2015 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2016 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2017 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2018 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002020 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2021 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 }
Andreas Gampe98430592014-07-27 19:44:50 -07002023 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024 return;
2025 case Instruction::ADD_LONG:
2026 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 first_op = kOpAdd;
2028 second_op = kOpAdc;
2029 break;
2030 case Instruction::SUB_LONG:
2031 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 first_op = kOpSub;
2033 second_op = kOpSbc;
2034 break;
2035 case Instruction::MUL_LONG:
2036 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002037 call_out = true;
2038 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2039 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 break;
2041 case Instruction::DIV_LONG:
2042 case Instruction::DIV_LONG_2ADDR:
2043 call_out = true;
2044 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002045 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2046 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 break;
2048 case Instruction::REM_LONG:
2049 case Instruction::REM_LONG_2ADDR:
2050 call_out = true;
2051 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002052 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002053 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002054 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2055 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002056 break;
2057 case Instruction::AND_LONG_2ADDR:
2058 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 first_op = kOpAnd;
2060 second_op = kOpAnd;
2061 break;
2062 case Instruction::OR_LONG:
2063 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 first_op = kOpOr;
2065 second_op = kOpOr;
2066 break;
2067 case Instruction::XOR_LONG:
2068 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 first_op = kOpXor;
2070 second_op = kOpXor;
2071 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 default:
2073 LOG(FATAL) << "Invalid long arith op";
2074 }
2075 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002076 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002077 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002078 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002080 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2081 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2082 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2083 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002084 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2085 GenDivZeroCheckWide(r_tmp2);
2086 }
Andreas Gampe98430592014-07-27 19:44:50 -07002087 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002089 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002090 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002091 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 }
2093 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002094 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2095 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 else
Andreas Gampe98430592014-07-27 19:44:50 -07002097 rl_result = GetReturnWideAlt();
2098 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 }
2100}
2101
Mark Mendelle87f9b52014-04-30 14:13:18 -04002102void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2103 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2104 LoadConstantNoClobber(rl_result.reg, value);
2105 StoreValue(rl_dest, rl_result);
2106 if (value == 0) {
2107 Workaround7250540(rl_dest, rl_result.reg);
2108 }
2109}
2110
Andreas Gampe98430592014-07-27 19:44:50 -07002111void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2112 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002113 /*
2114 * Don't optimize the register usage since it calls out to support
2115 * functions
2116 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002117
Brian Carlstrom7940e442013-07-12 13:46:57 -07002118 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002119 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002120 if (rl_dest.wide) {
2121 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002122 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002123 StoreValueWide(rl_dest, rl_result);
2124 } else {
2125 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002126 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002127 StoreValue(rl_dest, rl_result);
2128 }
2129}
2130
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002131class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2132 public:
2133 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2134 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2135 }
2136
2137 void Compile() OVERRIDE {
2138 m2l_->ResetRegPool();
2139 m2l_->ResetDefTracking();
2140 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002141 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002142 if (cont_ != nullptr) {
2143 m2l_->OpUnconditionalBranch(cont_);
2144 }
2145 }
2146};
2147
Brian Carlstrom7940e442013-07-12 13:46:57 -07002148/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002149void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002150 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002151 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2152 return;
2153 }
2154 FlushAllRegs();
2155 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002156 LIR* cont = NewLIR0(kPseudoTargetLabel);
2157 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002158 } else {
2159 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2160 return;
2161 }
2162 FlushAllRegs(); // TODO: needed?
2163 LIR* inst = CheckSuspendUsingLoad();
2164 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002165 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002166}
2167
2168/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002169void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002170 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002171 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2172 OpUnconditionalBranch(target);
2173 return;
2174 }
2175 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002176 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002177 LIR* branch = OpUnconditionalBranch(nullptr);
2178 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002179 } else {
2180 // For the implicit suspend check, just perform the trigger
2181 // load and branch to the target.
2182 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2183 OpUnconditionalBranch(target);
2184 return;
2185 }
2186 FlushAllRegs();
2187 LIR* inst = CheckSuspendUsingLoad();
2188 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002189 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002190 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002191}
2192
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002193/* Call out to helper assembly routine that will null check obj and then lock it. */
2194void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002195 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002196 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002197 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002198}
2199
2200/* Call out to helper assembly routine that will null check obj and then unlock it. */
2201void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002202 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002203 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002204 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002205}
2206
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002207/* Generic code for generating a wide constant into a VR. */
2208void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2209 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002210 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002211 StoreValueWide(rl_dest, rl_result);
2212}
2213
Andreas Gampe48971b32014-08-06 10:09:01 -07002214void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002215 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2216 DCHECK(bb != nullptr);
2217 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002218 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002219 const uint16_t entries = table[1];
2220 // Chained cmp-and-branch.
2221 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002222 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002223 rl_src = LoadValue(rl_src, kCoreReg);
2224 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002225 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002226 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002227 // Switch to using a temp and add.
2228 break;
2229 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002230 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2231 DCHECK(successor_block_info != nullptr);
2232 int case_block_id = successor_block_info->block;
2233 DCHECK_EQ(starting_key + i, successor_block_info->key);
2234 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002235 }
2236 if (i < entries) {
2237 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2238 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2239 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002240 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002241 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2242 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2243 DCHECK(successor_block_info != nullptr);
2244 int case_block_id = successor_block_info->block;
2245 DCHECK_EQ(starting_key + i, successor_block_info->key);
2246 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002247 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2248 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002249 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2250 DCHECK(successor_block_info != nullptr);
2251 int case_block_id = successor_block_info->block;
2252 DCHECK_EQ(starting_key + i, successor_block_info->key);
2253 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002254 } else {
2255 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002256 for (; i < entries; ++i, ++succ_bb_iter) {
2257 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2258 DCHECK(successor_block_info != nullptr);
2259 int case_block_id = successor_block_info->block;
2260 DCHECK_EQ(starting_key + i, successor_block_info->key);
2261 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002262 }
2263 }
2264 }
2265}
2266
2267void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002268 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002269 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002270 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002271 }
2272
2273 const uint16_t entries = table[1];
2274 if (entries <= kSmallSwitchThreshold) {
2275 GenSmallPackedSwitch(mir, table_offset, rl_src);
2276 } else {
2277 // Use the backend-specific implementation.
2278 GenLargePackedSwitch(mir, table_offset, rl_src);
2279 }
2280}
2281
2282void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002283 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2284 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002285 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002286 const uint16_t entries = table[1];
2287 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002288 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002289 int i = 0;
2290 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2291 int case_block_id = successor_block_info->block;
2292 int key = successor_block_info->key;
2293 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2294 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002295 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002296 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002297}
2298
2299void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002300 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002301 if (cu_->verbose) {
2302 DumpSparseSwitchTable(table);
2303 }
2304
2305 const uint16_t entries = table[1];
2306 if (entries <= kSmallSwitchThreshold) {
2307 GenSmallSparseSwitch(mir, table_offset, rl_src);
2308 } else {
2309 // Use the backend-specific implementation.
2310 GenLargeSparseSwitch(mir, table_offset, rl_src);
2311 }
2312}
2313
Fred Shih37f05ef2014-07-16 18:38:08 -07002314bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2315 switch (size) {
2316 case kReference:
2317 return type == Primitive::kPrimNot;
2318 case k64:
2319 case kDouble:
2320 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2321 case k32:
2322 case kSingle:
2323 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2324 case kSignedHalf:
2325 return type == Primitive::kPrimShort;
2326 case kUnsignedHalf:
2327 return type == Primitive::kPrimChar;
2328 case kSignedByte:
2329 return type == Primitive::kPrimByte;
2330 case kUnsignedByte:
2331 return type == Primitive::kPrimBoolean;
2332 case kWord: // Intentional fallthrough.
2333 default:
2334 return false; // There are no sane types with this op size.
2335 }
2336}
2337
Brian Carlstrom7940e442013-07-12 13:46:57 -07002338} // namespace art