blob: d1b8b034de08d3dcf46d95958b5712753d92a200 [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);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000586 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000587 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700588 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000589 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800590 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000591 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100593 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700594 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000595 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
596 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800597 if (IsTemp(rl_method.reg)) {
598 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 }
600 } else {
601 // Medium path, static storage base in a different class which requires checks that the other
602 // class is initialized.
603 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000604 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 // May do runtime call so everything to home locations.
606 FlushAllRegs();
607 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700608 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 LockTemp(r_method);
610 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700611 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800612 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000613 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
614 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000615 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000616 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800617 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100618 LIR* unresolved_branch = nullptr;
619 if (!field_info.IsClassInDexCache() &&
620 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
621 // Check if r_base is NULL.
622 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
623 }
624 LIR* uninit_branch = nullptr;
625 if (!field_info.IsClassInitialized() &&
626 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
627 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700628 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800629 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100630 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800631 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000632 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100633 FreeTemp(r_tmp);
634 }
635 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
636 // The slow path is invoked if the r_base is NULL or the class pointed
637 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800638 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800639 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000640 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800641
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100642 if (uninit_branch != nullptr) {
643 // Ensure load of status and store of value don't re-order.
644 // TODO: Presumably the actual value store is control-dependent on the status load,
645 // and will thus not be reordered in any case, since stores are never speculated.
646 // Does later code "know" that the class is now initialized? If so, we still
647 // need the barrier to guard later static loads.
648 GenMemBarrier(kLoadAny);
649 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 FreeTemp(r_method);
652 }
653 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700654 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
655 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100656 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100658 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700660 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000661 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
662 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100663 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700664 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000665 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700667 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000668 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800670 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 } else {
672 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700673 QuickEntrypointEnum target;
674 switch (size) {
675 case kReference:
676 target = kQuickSetObjStatic;
677 break;
678 case k64:
679 case kDouble:
680 target = kQuickSet64Static;
681 break;
682 case k32:
683 case kSingle:
684 target = kQuickSet32Static;
685 break;
686 case kSignedHalf:
687 case kUnsignedHalf:
688 target = kQuickSet16Static;
689 break;
690 case kSignedByte:
691 case kUnsignedByte:
692 target = kQuickSet8Static;
693 break;
694 case kWord: // Intentional fallthrough.
695 default:
696 LOG(FATAL) << "Can't determine entrypoint for: " << size;
697 target = kQuickSet32Static;
698 }
Andreas Gampe98430592014-07-27 19:44:50 -0700699 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 }
701}
702
Fred Shih37f05ef2014-07-16 18:38:08 -0700703void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000704 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000705 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700707
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700708 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800710 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000711 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 // Fast path, static storage base is this method's class
713 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700714 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000715 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
716 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 } else {
718 // Medium path, static storage base in a different class which requires checks that the other
719 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000720 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 // May do runtime call so everything to home locations.
722 FlushAllRegs();
723 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700724 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 LockTemp(r_method);
726 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700727 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800728 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000729 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
730 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000731 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000732 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800733 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100734 LIR* unresolved_branch = nullptr;
735 if (!field_info.IsClassInDexCache() &&
736 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
737 // Check if r_base is NULL.
738 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
739 }
740 LIR* uninit_branch = nullptr;
741 if (!field_info.IsClassInitialized() &&
742 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
743 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700744 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800745 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100746 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800747 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000748 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100749 FreeTemp(r_tmp);
750 }
751 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
752 // The slow path is invoked if the r_base is NULL or the class pointed
753 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800754 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800755 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000756 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800757
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100758 if (uninit_branch != nullptr) {
759 // Ensure load of status and load of value don't re-order.
760 GenMemBarrier(kLoadAny);
761 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 FreeTemp(r_method);
764 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800765 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700766 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100767 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800768
Vladimir Marko674744e2014-04-24 15:18:26 +0100769 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700770 if (IsRef(size)) {
771 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000772 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
773 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100774 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700775 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000776 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800777 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100778 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800779
Fred Shih37f05ef2014-07-16 18:38:08 -0700780 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 StoreValueWide(rl_dest, rl_result);
782 } else {
783 StoreValue(rl_dest, rl_result);
784 }
785 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700786 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700788 QuickEntrypointEnum target;
789 switch (type) {
790 case Primitive::kPrimNot:
791 target = kQuickGetObjStatic;
792 break;
793 case Primitive::kPrimLong:
794 case Primitive::kPrimDouble:
795 target = kQuickGet64Static;
796 break;
797 case Primitive::kPrimInt:
798 case Primitive::kPrimFloat:
799 target = kQuickGet32Static;
800 break;
801 case Primitive::kPrimShort:
802 target = kQuickGetShortStatic;
803 break;
804 case Primitive::kPrimChar:
805 target = kQuickGetCharStatic;
806 break;
807 case Primitive::kPrimByte:
808 target = kQuickGetByteStatic;
809 break;
810 case Primitive::kPrimBoolean:
811 target = kQuickGetBooleanStatic;
812 break;
813 case Primitive::kPrimVoid: // Intentional fallthrough.
814 default:
815 LOG(FATAL) << "Can't determine entrypoint for: " << type;
816 target = kQuickGet32Static;
817 }
Andreas Gampe98430592014-07-27 19:44:50 -0700818 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
819
Douglas Leung2db3e262014-06-25 16:02:55 -0700820 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700821 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700822 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 StoreValueWide(rl_dest, rl_result);
824 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700825 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 StoreValue(rl_dest, rl_result);
827 }
828 }
829}
830
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800831// Generate code for all slow paths.
832void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700833 // We should check slow_paths_.Size() every time, because a new slow path
834 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100835 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800836 slowpath->Compile();
837 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100838 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800839}
840
Fred Shih37f05ef2014-07-16 18:38:08 -0700841void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
842 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000843 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000844 DCHECK_EQ(IGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000845 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700846 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700847 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700848 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000849 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700850 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100851 GenNullCheck(rl_obj.reg, opt_flags);
852 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
853 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000854 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700855 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000856 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
857 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100858 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700859 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000860 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100861 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000862 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700863 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 StoreValueWide(rl_dest, rl_result);
865 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 StoreValue(rl_dest, rl_result);
867 }
868 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700869 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
870 QuickEntrypointEnum target;
871 switch (type) {
872 case Primitive::kPrimNot:
873 target = kQuickGetObjInstance;
874 break;
875 case Primitive::kPrimLong:
876 case Primitive::kPrimDouble:
877 target = kQuickGet64Instance;
878 break;
879 case Primitive::kPrimFloat:
880 case Primitive::kPrimInt:
881 target = kQuickGet32Instance;
882 break;
883 case Primitive::kPrimShort:
884 target = kQuickGetShortInstance;
885 break;
886 case Primitive::kPrimChar:
887 target = kQuickGetCharInstance;
888 break;
889 case Primitive::kPrimByte:
890 target = kQuickGetByteInstance;
891 break;
892 case Primitive::kPrimBoolean:
893 target = kQuickGetBooleanInstance;
894 break;
895 case Primitive::kPrimVoid: // Intentional fallthrough.
896 default:
897 LOG(FATAL) << "Can't determine entrypoint for: " << type;
898 target = kQuickGet32Instance;
899 }
Andreas Gampe98430592014-07-27 19:44:50 -0700900 // Second argument of pGetXXInstance is always a reference.
901 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
902 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
903
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700904 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700905 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700906 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 StoreValueWide(rl_dest, rl_result);
908 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700909 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 StoreValue(rl_dest, rl_result);
911 }
912 }
913}
914
Vladimir Markobe0e5462014-02-26 11:24:15 +0000915void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700916 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000917 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000918 DCHECK_EQ(IPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000919 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700920 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700921 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700922 // Dex code never writes to the class field.
923 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
924 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700925 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700926 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100927 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 } else {
929 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100930 }
931 GenNullCheck(rl_obj.reg, opt_flags);
932 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000933 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700934 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000935 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
936 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100937 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700938 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000939 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100940 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000941 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700942 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000943 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 }
945 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700946 QuickEntrypointEnum target;
947 switch (size) {
948 case kReference:
949 target = kQuickSetObjInstance;
950 break;
951 case k64:
952 case kDouble:
953 target = kQuickSet64Instance;
954 break;
955 case k32:
956 case kSingle:
957 target = kQuickSet32Instance;
958 break;
959 case kSignedHalf:
960 case kUnsignedHalf:
961 target = kQuickSet16Instance;
962 break;
963 case kSignedByte:
964 case kUnsignedByte:
965 target = kQuickSet8Instance;
966 break;
967 case kWord: // Intentional fallthrough.
968 default:
969 LOG(FATAL) << "Can't determine entrypoint for: " << size;
970 target = kQuickSet32Instance;
971 }
Andreas Gampe98430592014-07-27 19:44:50 -0700972 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
973 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 }
975}
976
Ian Rogersa9a82542013-10-04 11:17:26 -0700977void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
978 RegLocation rl_src) {
979 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
980 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
981 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700982 QuickEntrypointEnum target = needs_range_check
983 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
984 : kQuickAputObjectWithBoundCheck)
985 : kQuickAputObject;
986 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700987}
988
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700989void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700991 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700992 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700994 *cu_->dex_file,
995 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 // Call out to helper which resolves type and verifies access.
997 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700998 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700999 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 StoreValue(rl_dest, rl_result);
1001 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001002 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 // We're don't need access checks, load type from dex cache
1004 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001005 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001006 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001007 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001008 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
1010 type_idx) || SLOW_TYPE_PATH) {
1011 // Slow path, at runtime test if type is null and if so initialize
1012 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001013 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001014 LIR* cont = NewLIR0(kPseudoTargetLabel);
1015
1016 // Object to generate the slow path for class resolution.
1017 class SlowPath : public LIRSlowPath {
1018 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001019 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1020 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
1021 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1022 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 }
1024
1025 void Compile() {
1026 GenerateTargetLabel();
1027
Andreas Gampe98430592014-07-27 19:44:50 -07001028 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001029 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001030 m2l_->OpUnconditionalBranch(cont_);
1031 }
1032
1033 private:
1034 const int type_idx_;
1035 const RegLocation rl_method_;
1036 const RegLocation rl_result_;
1037 };
1038
1039 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001040 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001041
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001043 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 // Fast path, we're done - just store result
1045 StoreValue(rl_dest, rl_result);
1046 }
1047 }
1048}
1049
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001050void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001052 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1053 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1055 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1056 // slow path, resolve string if not in dex cache
1057 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001058 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001059
1060 // If the Method* is already in a register, we can save a copy.
1061 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001062 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001063 if (rl_method.location == kLocPhysReg) {
1064 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001065 DCHECK(!IsTemp(rl_method.reg));
1066 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001067 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001068 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001069 LoadCurrMethodDirect(r_method);
1070 }
buzbee695d13a2014-04-19 13:32:20 -07001071 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001072 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001073
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001075 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1076 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001077 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001078
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001079 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001080 // Object to generate the slow path for string resolution.
1081 class SlowPath : public LIRSlowPath {
1082 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001083 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1084 int32_t string_idx_in) :
1085 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1086 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001087 }
1088
1089 void Compile() {
1090 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001091 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001092 m2l_->OpUnconditionalBranch(cont_);
1093 }
1094
1095 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001096 const RegStorage r_method_;
1097 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001098 };
1099
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001100 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001102
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001104 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 } else {
1106 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001107 RegStorage res_reg = AllocTempRef();
1108 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001109 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1110 kNotVolatile);
1111 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 StoreValue(rl_dest, rl_result);
1113 }
1114}
1115
Andreas Gampe98430592014-07-27 19:44:50 -07001116/*
1117 * Let helper function take care of everything. Will
1118 * call Class::NewInstanceFromCode(type_idx, method);
1119 */
1120void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1121 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 // alloc will always check for resolution, do we also need to verify
1123 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001124 const DexFile* dex_file = cu_->dex_file;
1125 CompilerDriver* driver = cu_->compiler_driver;
1126 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001127 bool is_type_initialized;
1128 bool use_direct_type_ptr;
1129 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001130 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001131 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001132 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1133 &direct_type_ptr, &is_finalizable) &&
1134 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001135 // The fast path.
1136 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001137 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001138 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001139 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001140 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001141 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001142 }
1143 } else {
1144 // Use the direct pointer.
1145 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001146 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001147 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001148 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001149 }
1150 }
1151 } else {
1152 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001153 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001154 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001156 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 }
Andreas Gampe98430592014-07-27 19:44:50 -07001158 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159}
1160
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001161void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001163 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164}
1165
1166// For final classes there are no sub-classes to check and so we can answer the instance-of
1167// question with simple comparisons.
1168void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1169 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001170 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001171 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001172
buzbeea0cd2d72014-06-01 09:33:49 -07001173 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001175 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001176 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001178 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 }
1180 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001181 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182
buzbeea0cd2d72014-06-01 09:33:49 -07001183 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1184 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185
1186 LoadCurrMethodDirect(check_class);
1187 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001188 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1189 kNotVolatile);
1190 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1191 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 } else {
buzbee695d13a2014-04-19 13:32:20 -07001193 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001194 check_class, kNotVolatile);
1195 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1196 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001197 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001198 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 }
1200
buzbee695d13a2014-04-19 13:32:20 -07001201 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 if (cu_->instruction_set == kThumb2) {
1203 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001204 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001206 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001208 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 }
1210 LIR* target = NewLIR0(kPseudoTargetLabel);
1211 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 FreeTemp(object_class);
1213 FreeTemp(check_class);
1214 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001215 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 FreeTemp(result_reg);
1217 }
1218 StoreValue(rl_dest, rl_result);
1219}
1220
1221void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1222 bool type_known_abstract, bool use_declaring_class,
1223 bool can_assume_type_is_in_dex_cache,
1224 uint32_t type_idx, RegLocation rl_dest,
1225 RegLocation rl_src) {
1226 FlushAllRegs();
1227 // May generate a call - use explicit registers
1228 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001229 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001230 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001231 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001232 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1233 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 if (needs_access_check) {
1235 // Check we have access to type_idx and if not throw IllegalAccessError,
1236 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001237 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001238 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1239 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001241 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001242 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001243 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001245 if (can_assume_type_is_in_dex_cache) {
1246 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001247 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001248 }
1249
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001251 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001252 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001253 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001254 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001256 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1257 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1258
1259 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001260 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001261
1262 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1263 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001264 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1265 RegLocation rl_src_in)
1266 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1267 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001268 }
1269
1270 void Compile() OVERRIDE {
1271 GenerateTargetLabel();
1272
Andreas Gampe98430592014-07-27 19:44:50 -07001273 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001274 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1275 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001276 m2l_->OpUnconditionalBranch(cont_);
1277 }
1278
1279 private:
1280 uint32_t type_idx_;
1281 RegLocation rl_src_;
1282 };
1283
1284 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1285 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 }
1287 }
1288 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001289 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001290 if (!IsSameReg(rl_result.reg, ref_reg)) {
1291 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001292 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001294 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295
1296 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001297 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001299 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1300 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1302 LIR* branchover = NULL;
1303 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001304 // rl_result == ref == class.
1305 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001306 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 } else {
1308 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001309 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001310 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 if (!type_known_abstract) {
1312 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001313 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001314 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001315 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001317 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001319 if (it != nullptr) {
1320 OpEndIT(it);
1321 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 FreeTemp(r_tgt);
1323 } else {
1324 if (!type_known_abstract) {
1325 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001326 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001327 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001329
Serguei Katkov9ee45192014-07-17 14:39:03 +07001330 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001331 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
1333 }
1334 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001335 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 /* branch targets here */
1337 LIR* target = NewLIR0(kPseudoTargetLabel);
1338 StoreValue(rl_dest, rl_result);
1339 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001340 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 branchover->target = target;
1342 }
1343}
1344
1345void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1346 bool type_known_final, type_known_abstract, use_declaring_class;
1347 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1348 *cu_->dex_file,
1349 type_idx,
1350 &type_known_final,
1351 &type_known_abstract,
1352 &use_declaring_class);
1353 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1354 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1355
1356 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1357 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1358 } else {
1359 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1360 use_declaring_class, can_assume_type_is_in_dex_cache,
1361 type_idx, rl_dest, rl_src);
1362 }
1363}
1364
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001365void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 bool type_known_final, type_known_abstract, use_declaring_class;
1367 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1368 *cu_->dex_file,
1369 type_idx,
1370 &type_known_final,
1371 &type_known_abstract,
1372 &use_declaring_class);
1373 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1374 // of the exception throw path.
1375 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001376 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 // Verifier type analysis proved this check cast would never cause an exception.
1378 return;
1379 }
1380 FlushAllRegs();
1381 // May generate a call - use explicit registers
1382 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001383 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001384 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001385 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 if (needs_access_check) {
1387 // Check we have access to type_idx and if not throw IllegalAccessError,
1388 // returns Class* in kRet0
1389 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001390 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001391 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001393 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001394 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 } else {
1396 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001397 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001398 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001399 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001400 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1402 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001403 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1404 LIR* cont = NewLIR0(kPseudoTargetLabel);
1405
1406 // Slow path to initialize the type. Executed if the type is NULL.
1407 class SlowPath : public LIRSlowPath {
1408 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001409 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1410 const RegStorage class_reg_in) :
1411 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1412 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001413 }
1414
1415 void Compile() {
1416 GenerateTargetLabel();
1417
1418 // Call out to helper, which will return resolved type in kArg0
1419 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001420 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1421 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001422 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001423 m2l_->OpUnconditionalBranch(cont_);
1424 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001425
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001426 public:
1427 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001428 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001429 };
1430
buzbee2700f7e2014-03-07 09:46:20 -08001431 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 }
1433 }
1434 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001435 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001436
1437 // Slow path for the case where the classes are not equal. In this case we need
1438 // to call a helper function to do the check.
1439 class SlowPath : public LIRSlowPath {
1440 public:
1441 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1442 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1443 }
1444
1445 void Compile() {
1446 GenerateTargetLabel();
1447
1448 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001449 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1450 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001451 }
Andreas Gampe98430592014-07-27 19:44:50 -07001452 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1453 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001454 m2l_->OpUnconditionalBranch(cont_);
1455 }
1456
1457 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001458 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459 };
1460
1461 if (type_known_abstract) {
1462 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001463 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001464 LIR* cont = NewLIR0(kPseudoTargetLabel);
1465 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1466 } else {
1467 // Harder, more common case. We need to generate a forward branch over the load
1468 // if the target is null. If it's non-null we perform the load and branch to the
1469 // slow path if the classes are not equal.
1470
1471 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001472 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001473 /* load object->klass_ */
1474 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001475 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1476 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001477
Andreas Gampeccc60262014-07-04 18:02:38 -07001478 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001479 LIR* cont = NewLIR0(kPseudoTargetLabel);
1480
1481 // Add the slow path that will not perform load since this is already done.
1482 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1483
1484 // Set the null check to branch to the continuation.
1485 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 }
1487}
1488
1489void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001490 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 RegLocation rl_result;
1492 if (cu_->instruction_set == kThumb2) {
1493 /*
1494 * NOTE: This is the one place in the code in which we might have
1495 * as many as six live temporary registers. There are 5 in the normal
1496 * set for Arm. Until we have spill capabilities, temporarily add
1497 * lr to the temp set. It is safe to do this locally, but note that
1498 * lr is used explicitly elsewhere in the code generator and cannot
1499 * normally be used as a general temp register.
1500 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001501 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1502 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 }
1504 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1505 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1506 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1507 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001508 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1509 RegStorage t_reg = AllocTemp();
1510 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1511 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1512 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 FreeTemp(t_reg);
1514 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001515 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1516 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 }
1518 /*
1519 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1520 * following StoreValueWide might need to allocate a temp register.
1521 * To further work around the lack of a spill capability, explicitly
1522 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1523 * Remove when spill is functional.
1524 */
1525 FreeRegLocTemps(rl_result, rl_src1);
1526 FreeRegLocTemps(rl_result, rl_src2);
1527 StoreValueWide(rl_dest, rl_result);
1528 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001529 Clobber(TargetReg(kLr, kNotWide));
1530 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 }
1532}
1533
Andreas Gampe98430592014-07-27 19:44:50 -07001534void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1535 RegLocation rl_src1, RegLocation rl_shift) {
1536 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 switch (opcode) {
1538 case Instruction::SHL_LONG:
1539 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001540 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 break;
1542 case Instruction::SHR_LONG:
1543 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001544 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 break;
1546 case Instruction::USHR_LONG:
1547 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001548 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 break;
1550 default:
1551 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001552 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 }
Andreas Gampe98430592014-07-27 19:44:50 -07001554 FlushAllRegs(); /* Send everything to home location */
1555 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001556 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 StoreValueWide(rl_dest, rl_result);
1558}
1559
1560
1561void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001562 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001563 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 OpKind op = kOpBkpt;
1565 bool is_div_rem = false;
1566 bool check_zero = false;
1567 bool unary = false;
1568 RegLocation rl_result;
1569 bool shift_op = false;
1570 switch (opcode) {
1571 case Instruction::NEG_INT:
1572 op = kOpNeg;
1573 unary = true;
1574 break;
1575 case Instruction::NOT_INT:
1576 op = kOpMvn;
1577 unary = true;
1578 break;
1579 case Instruction::ADD_INT:
1580 case Instruction::ADD_INT_2ADDR:
1581 op = kOpAdd;
1582 break;
1583 case Instruction::SUB_INT:
1584 case Instruction::SUB_INT_2ADDR:
1585 op = kOpSub;
1586 break;
1587 case Instruction::MUL_INT:
1588 case Instruction::MUL_INT_2ADDR:
1589 op = kOpMul;
1590 break;
1591 case Instruction::DIV_INT:
1592 case Instruction::DIV_INT_2ADDR:
1593 check_zero = true;
1594 op = kOpDiv;
1595 is_div_rem = true;
1596 break;
1597 /* NOTE: returns in kArg1 */
1598 case Instruction::REM_INT:
1599 case Instruction::REM_INT_2ADDR:
1600 check_zero = true;
1601 op = kOpRem;
1602 is_div_rem = true;
1603 break;
1604 case Instruction::AND_INT:
1605 case Instruction::AND_INT_2ADDR:
1606 op = kOpAnd;
1607 break;
1608 case Instruction::OR_INT:
1609 case Instruction::OR_INT_2ADDR:
1610 op = kOpOr;
1611 break;
1612 case Instruction::XOR_INT:
1613 case Instruction::XOR_INT_2ADDR:
1614 op = kOpXor;
1615 break;
1616 case Instruction::SHL_INT:
1617 case Instruction::SHL_INT_2ADDR:
1618 shift_op = true;
1619 op = kOpLsl;
1620 break;
1621 case Instruction::SHR_INT:
1622 case Instruction::SHR_INT_2ADDR:
1623 shift_op = true;
1624 op = kOpAsr;
1625 break;
1626 case Instruction::USHR_INT:
1627 case Instruction::USHR_INT_2ADDR:
1628 shift_op = true;
1629 op = kOpLsr;
1630 break;
1631 default:
1632 LOG(FATAL) << "Invalid word arith op: " << opcode;
1633 }
1634 if (!is_div_rem) {
1635 if (unary) {
1636 rl_src1 = LoadValue(rl_src1, kCoreReg);
1637 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001638 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001640 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001641 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001642 RegStorage t_reg = AllocTemp();
1643 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 rl_src1 = LoadValue(rl_src1, kCoreReg);
1645 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001646 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 FreeTemp(t_reg);
1648 } else {
1649 rl_src1 = LoadValue(rl_src1, kCoreReg);
1650 rl_src2 = LoadValue(rl_src2, kCoreReg);
1651 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001652 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
1654 }
1655 StoreValue(rl_dest, rl_result);
1656 } else {
Dave Allison70202782013-10-22 17:52:19 -07001657 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 +01001658 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 rl_src1 = LoadValue(rl_src1, kCoreReg);
1660 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001661 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001662 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 }
buzbee2700f7e2014-03-07 09:46:20 -08001664 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001665 done = true;
1666 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001667 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1668 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001669 // Use ARM SDIV instruction for division. For remainder we also need to
1670 // calculate using a MUL and subtract.
1671 rl_src1 = LoadValue(rl_src1, kCoreReg);
1672 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001673 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001674 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001675 }
buzbee2700f7e2014-03-07 09:46:20 -08001676 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001677 done = true;
1678 }
1679 }
1680
1681 // If we haven't already generated the code use the callout function.
1682 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001684 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001685 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001686 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001687 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001688 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 }
Dave Allison70202782013-10-22 17:52:19 -07001690 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001691 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001693 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 else
1695 rl_result = GetReturnAlt();
1696 }
1697 StoreValue(rl_dest, rl_result);
1698 }
1699}
1700
1701/*
1702 * The following are the first-level codegen routines that analyze the format
1703 * of each bytecode then either dispatch special purpose codegen routines
1704 * or produce corresponding Thumb instructions directly.
1705 */
1706
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001708static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 x &= x - 1;
1710 return (x & (x - 1)) == 0;
1711}
1712
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1714// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001715bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001716 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1718 return false;
1719 }
1720 // No divide instruction for Arm, so check for more special cases
1721 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001722 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 }
1724 int k = LowestSetBit(lit);
1725 if (k >= 30) {
1726 // Avoid special cases.
1727 return false;
1728 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 rl_src = LoadValue(rl_src, kCoreReg);
1730 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001731 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001732 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 if (lit == 2) {
1734 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001735 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1736 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1737 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001739 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001741 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1742 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 }
1744 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001745 RegStorage t_reg1 = AllocTemp();
1746 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001748 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1749 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001753 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001755 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001757 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 }
1759 }
1760 StoreValue(rl_dest, rl_result);
1761 return true;
1762}
1763
1764// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1765// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001766bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001767 if (lit < 0) {
1768 return false;
1769 }
1770 if (lit == 0) {
1771 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1772 LoadConstant(rl_result.reg, 0);
1773 StoreValue(rl_dest, rl_result);
1774 return true;
1775 }
1776 if (lit == 1) {
1777 rl_src = LoadValue(rl_src, kCoreReg);
1778 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1779 OpRegCopy(rl_result.reg, rl_src.reg);
1780 StoreValue(rl_dest, rl_result);
1781 return true;
1782 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001783 // There is RegRegRegShift on Arm, so check for more special cases
1784 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001785 return EasyMultiply(rl_src, rl_dest, lit);
1786 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 // Can we simplify this multiplication?
1788 bool power_of_two = false;
1789 bool pop_count_le2 = false;
1790 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001791 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001792 power_of_two = true;
1793 } else if (IsPopCountLE2(lit)) {
1794 pop_count_le2 = true;
1795 } else if (IsPowerOfTwo(lit + 1)) {
1796 power_of_two_minus_one = true;
1797 } else {
1798 return false;
1799 }
1800 rl_src = LoadValue(rl_src, kCoreReg);
1801 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1802 if (power_of_two) {
1803 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001804 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001805 } else if (pop_count_le2) {
1806 // Shift and add and shift.
1807 int first_bit = LowestSetBit(lit);
1808 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1809 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1810 } else {
1811 // Reverse subtract: (src << (shift + 1)) - src.
1812 DCHECK(power_of_two_minus_one);
1813 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001814 RegStorage t_reg = AllocTemp();
1815 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1816 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 }
1818 StoreValue(rl_dest, rl_result);
1819 return true;
1820}
1821
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001822// Returns true if it generates instructions.
1823bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1824 RegLocation rl_src2) {
1825 if (!rl_src2.is_const ||
1826 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1827 return false;
1828 }
1829
1830 if (!rl_src2.wide) {
1831 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1832 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1833 // Generate multiply by reciprocal instead of div.
1834 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1835 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1836 return true;
1837 }
1838 } else {
1839 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1840 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1841 // Generate multiply by reciprocal instead of div.
1842 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1843 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1844 return true;
1845 }
1846 }
1847 return false;
1848}
1849
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001851 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 RegLocation rl_result;
1853 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1854 int shift_op = false;
1855 bool is_div = false;
1856
1857 switch (opcode) {
1858 case Instruction::RSUB_INT_LIT8:
1859 case Instruction::RSUB_INT: {
1860 rl_src = LoadValue(rl_src, kCoreReg);
1861 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1862 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001863 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001865 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1866 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 }
1868 StoreValue(rl_dest, rl_result);
1869 return;
1870 }
1871
1872 case Instruction::SUB_INT:
1873 case Instruction::SUB_INT_2ADDR:
1874 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001875 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 case Instruction::ADD_INT:
1877 case Instruction::ADD_INT_2ADDR:
1878 case Instruction::ADD_INT_LIT8:
1879 case Instruction::ADD_INT_LIT16:
1880 op = kOpAdd;
1881 break;
1882 case Instruction::MUL_INT:
1883 case Instruction::MUL_INT_2ADDR:
1884 case Instruction::MUL_INT_LIT8:
1885 case Instruction::MUL_INT_LIT16: {
1886 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1887 return;
1888 }
1889 op = kOpMul;
1890 break;
1891 }
1892 case Instruction::AND_INT:
1893 case Instruction::AND_INT_2ADDR:
1894 case Instruction::AND_INT_LIT8:
1895 case Instruction::AND_INT_LIT16:
1896 op = kOpAnd;
1897 break;
1898 case Instruction::OR_INT:
1899 case Instruction::OR_INT_2ADDR:
1900 case Instruction::OR_INT_LIT8:
1901 case Instruction::OR_INT_LIT16:
1902 op = kOpOr;
1903 break;
1904 case Instruction::XOR_INT:
1905 case Instruction::XOR_INT_2ADDR:
1906 case Instruction::XOR_INT_LIT8:
1907 case Instruction::XOR_INT_LIT16:
1908 op = kOpXor;
1909 break;
1910 case Instruction::SHL_INT_LIT8:
1911 case Instruction::SHL_INT:
1912 case Instruction::SHL_INT_2ADDR:
1913 lit &= 31;
1914 shift_op = true;
1915 op = kOpLsl;
1916 break;
1917 case Instruction::SHR_INT_LIT8:
1918 case Instruction::SHR_INT:
1919 case Instruction::SHR_INT_2ADDR:
1920 lit &= 31;
1921 shift_op = true;
1922 op = kOpAsr;
1923 break;
1924 case Instruction::USHR_INT_LIT8:
1925 case Instruction::USHR_INT:
1926 case Instruction::USHR_INT_2ADDR:
1927 lit &= 31;
1928 shift_op = true;
1929 op = kOpLsr;
1930 break;
1931
1932 case Instruction::DIV_INT:
1933 case Instruction::DIV_INT_2ADDR:
1934 case Instruction::DIV_INT_LIT8:
1935 case Instruction::DIV_INT_LIT16:
1936 case Instruction::REM_INT:
1937 case Instruction::REM_INT_2ADDR:
1938 case Instruction::REM_INT_LIT8:
1939 case Instruction::REM_INT_LIT16: {
1940 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001941 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001942 return;
1943 }
buzbee11b63d12013-08-27 07:34:17 -07001944 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001945 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001946 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 (opcode == Instruction::DIV_INT_LIT16)) {
1948 is_div = true;
1949 } else {
1950 is_div = false;
1951 }
buzbee11b63d12013-08-27 07:34:17 -07001952 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1953 return;
1954 }
Dave Allison70202782013-10-22 17:52:19 -07001955
1956 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001957 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001959 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001960 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001961 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001962 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1963 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001964 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001965 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1966 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001967 // Use ARM SDIV instruction for division. For remainder we also need to
1968 // calculate using a MUL and subtract.
1969 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001970 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001971 done = true;
1972 }
1973 }
1974
1975 if (!done) {
1976 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001977 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1978 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001979 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001981 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 else
1983 rl_result = GetReturnAlt();
1984 }
1985 StoreValue(rl_dest, rl_result);
1986 return;
1987 }
1988 default:
1989 LOG(FATAL) << "Unexpected opcode " << opcode;
1990 }
1991 rl_src = LoadValue(rl_src, kCoreReg);
1992 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001993 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001995 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001997 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 }
1999 StoreValue(rl_dest, rl_result);
2000}
2001
Andreas Gampe98430592014-07-27 19:44:50 -07002002void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002003 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 RegLocation rl_result;
2005 OpKind first_op = kOpBkpt;
2006 OpKind second_op = kOpBkpt;
2007 bool call_out = false;
2008 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002009 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2010 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011
2012 switch (opcode) {
2013 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002014 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2015 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002017 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002018 RegStorage t_reg = AllocTemp();
2019 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2020 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2021 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2022 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002024 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2025 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026 }
Andreas Gampe98430592014-07-27 19:44:50 -07002027 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 return;
2029 case Instruction::ADD_LONG:
2030 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031 first_op = kOpAdd;
2032 second_op = kOpAdc;
2033 break;
2034 case Instruction::SUB_LONG:
2035 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 first_op = kOpSub;
2037 second_op = kOpSbc;
2038 break;
2039 case Instruction::MUL_LONG:
2040 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002041 call_out = true;
2042 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2043 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 break;
2045 case Instruction::DIV_LONG:
2046 case Instruction::DIV_LONG_2ADDR:
2047 call_out = true;
2048 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002049 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2050 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 break;
2052 case Instruction::REM_LONG:
2053 case Instruction::REM_LONG_2ADDR:
2054 call_out = true;
2055 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002056 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002057 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002058 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2059 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 break;
2061 case Instruction::AND_LONG_2ADDR:
2062 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002063 first_op = kOpAnd;
2064 second_op = kOpAnd;
2065 break;
2066 case Instruction::OR_LONG:
2067 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002068 first_op = kOpOr;
2069 second_op = kOpOr;
2070 break;
2071 case Instruction::XOR_LONG:
2072 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 first_op = kOpXor;
2074 second_op = kOpXor;
2075 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002076 default:
2077 LOG(FATAL) << "Invalid long arith op";
2078 }
2079 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002080 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002082 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002084 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2085 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2086 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2087 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002088 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2089 GenDivZeroCheckWide(r_tmp2);
2090 }
Andreas Gampe98430592014-07-27 19:44:50 -07002091 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002093 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002094 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002095 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 }
2097 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002098 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2099 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 else
Andreas Gampe98430592014-07-27 19:44:50 -07002101 rl_result = GetReturnWideAlt();
2102 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002103 }
2104}
2105
Mark Mendelle87f9b52014-04-30 14:13:18 -04002106void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2107 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2108 LoadConstantNoClobber(rl_result.reg, value);
2109 StoreValue(rl_dest, rl_result);
2110 if (value == 0) {
2111 Workaround7250540(rl_dest, rl_result.reg);
2112 }
2113}
2114
Andreas Gampe98430592014-07-27 19:44:50 -07002115void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2116 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117 /*
2118 * Don't optimize the register usage since it calls out to support
2119 * functions
2120 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002121
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002123 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002124 if (rl_dest.wide) {
2125 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002126 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002127 StoreValueWide(rl_dest, rl_result);
2128 } else {
2129 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002130 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002131 StoreValue(rl_dest, rl_result);
2132 }
2133}
2134
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002135class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2136 public:
2137 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2138 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2139 }
2140
2141 void Compile() OVERRIDE {
2142 m2l_->ResetRegPool();
2143 m2l_->ResetDefTracking();
2144 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002145 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002146 if (cont_ != nullptr) {
2147 m2l_->OpUnconditionalBranch(cont_);
2148 }
2149 }
2150};
2151
Brian Carlstrom7940e442013-07-12 13:46:57 -07002152/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002153void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002154 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002155 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2156 return;
2157 }
2158 FlushAllRegs();
2159 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002160 LIR* cont = NewLIR0(kPseudoTargetLabel);
2161 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002162 } else {
2163 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2164 return;
2165 }
2166 FlushAllRegs(); // TODO: needed?
2167 LIR* inst = CheckSuspendUsingLoad();
2168 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002169 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002170}
2171
2172/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002173void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002174 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002175 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2176 OpUnconditionalBranch(target);
2177 return;
2178 }
2179 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002180 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002181 LIR* branch = OpUnconditionalBranch(nullptr);
2182 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002183 } else {
2184 // For the implicit suspend check, just perform the trigger
2185 // load and branch to the target.
2186 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2187 OpUnconditionalBranch(target);
2188 return;
2189 }
2190 FlushAllRegs();
2191 LIR* inst = CheckSuspendUsingLoad();
2192 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002193 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002194 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002195}
2196
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002197/* Call out to helper assembly routine that will null check obj and then lock it. */
2198void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002199 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002200 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002201 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002202}
2203
2204/* Call out to helper assembly routine that will null check obj and then unlock it. */
2205void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002206 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002207 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002208 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002209}
2210
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002211/* Generic code for generating a wide constant into a VR. */
2212void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2213 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002214 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002215 StoreValueWide(rl_dest, rl_result);
2216}
2217
Andreas Gampe48971b32014-08-06 10:09:01 -07002218void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002219 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002220 const uint16_t entries = table[1];
2221 // Chained cmp-and-branch.
2222 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002223 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002224 const int32_t* targets = &as_int32[1];
2225 rl_src = LoadValue(rl_src, kCoreReg);
2226 int i = 0;
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002227 for (; i < entries; i++) {
2228 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002229 // Switch to using a temp and add.
2230 break;
2231 }
2232 BasicBlock* case_block =
2233 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002234 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);
2241 for (; i < entries - 1; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002242 BasicBlock* case_block =
2243 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2244 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2245 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2246 }
2247 BasicBlock* case_block =
2248 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2249 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2250 } else {
2251 // No free temp, just finish the old loop.
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002252 for (; i < entries; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002253 BasicBlock* case_block =
2254 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002255 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002256 }
2257 }
2258 }
2259}
2260
2261void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002262 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002263 if (cu_->verbose) {
2264 DumpSparseSwitchTable(table);
2265 }
2266
2267 const uint16_t entries = table[1];
2268 if (entries <= kSmallSwitchThreshold) {
2269 GenSmallPackedSwitch(mir, table_offset, rl_src);
2270 } else {
2271 // Use the backend-specific implementation.
2272 GenLargePackedSwitch(mir, table_offset, rl_src);
2273 }
2274}
2275
2276void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002277 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002278 const uint16_t entries = table[1];
2279 // Chained cmp-and-branch.
2280 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2281 const int32_t* targets = &keys[entries];
2282 rl_src = LoadValue(rl_src, kCoreReg);
2283 for (int i = 0; i < entries; i++) {
2284 int key = keys[i];
2285 BasicBlock* case_block =
2286 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2287 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2288 }
2289}
2290
2291void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002292 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002293 if (cu_->verbose) {
2294 DumpSparseSwitchTable(table);
2295 }
2296
2297 const uint16_t entries = table[1];
2298 if (entries <= kSmallSwitchThreshold) {
2299 GenSmallSparseSwitch(mir, table_offset, rl_src);
2300 } else {
2301 // Use the backend-specific implementation.
2302 GenLargeSparseSwitch(mir, table_offset, rl_src);
2303 }
2304}
2305
Fred Shih37f05ef2014-07-16 18:38:08 -07002306bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2307 switch (size) {
2308 case kReference:
2309 return type == Primitive::kPrimNot;
2310 case k64:
2311 case kDouble:
2312 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2313 case k32:
2314 case kSingle:
2315 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2316 case kSignedHalf:
2317 return type == Primitive::kPrimShort;
2318 case kUnsignedHalf:
2319 return type == Primitive::kPrimChar;
2320 case kSignedByte:
2321 return type == Primitive::kPrimByte;
2322 case kUnsignedByte:
2323 return type == Primitive::kPrimBoolean;
2324 case kWord: // Intentional fallthrough.
2325 default:
2326 return false; // There are no sane types with this op size.
2327 }
2328}
2329
Brian Carlstrom7940e442013-07-12 13:46:57 -07002330} // namespace art