blob: 774176ebb19dcfeab2e6fc1fc5752cd89ae6a6e8 [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
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600325void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
326 rl_src = UpdateLocWide(rl_src);
327 rl_src = NarrowRegLoc(rl_src);
328 StoreValue(rl_dest, rl_src);
329}
330
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700332 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700333 rl_src = LoadValue(rl_src, kCoreReg);
334 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
335 OpKind op = kOpInvalid;
336 switch (opcode) {
337 case Instruction::INT_TO_BYTE:
338 op = kOp2Byte;
339 break;
340 case Instruction::INT_TO_SHORT:
341 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700343 case Instruction::INT_TO_CHAR:
344 op = kOp2Char;
345 break;
346 default:
347 LOG(ERROR) << "Bad int conversion type";
348 }
buzbee2700f7e2014-03-07 09:46:20 -0800349 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700350 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351}
352
Andreas Gampe98430592014-07-27 19:44:50 -0700353/*
354 * Let helper function take care of everything. Will call
355 * Array::AllocFromCode(type_idx, method, count);
356 * Note: AllocFromCode will handle checks for errNegativeArraySize.
357 */
358void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
359 RegLocation rl_src) {
360 FlushAllRegs(); /* Everything to home location */
361 const DexFile* dex_file = cu_->dex_file;
362 CompilerDriver* driver = cu_->compiler_driver;
363 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800364 bool is_type_initialized; // Ignored as an array does not have an initializer.
365 bool use_direct_type_ptr;
366 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700367 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800368 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700369 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
370 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800371 // The fast path.
372 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700373 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700374 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
375 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800376 } else {
377 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700378 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
379 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800380 }
381 } else {
382 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700383 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800384 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700386 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 }
Andreas Gampe98430592014-07-27 19:44:50 -0700388 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389}
390
391/*
392 * Similar to GenNewArray, but with post-allocation initialization.
393 * Verifier guarantees we're dealing with an array class. Current
394 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
395 * Current code also throws internal unimp if not 'L', '[' or 'I'.
396 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700397void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 int elems = info->num_arg_words;
399 int type_idx = info->index;
400 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700401 QuickEntrypointEnum target;
402 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
403 type_idx)) {
404 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700406 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 }
Andreas Gampe98430592014-07-27 19:44:50 -0700408 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700409 FreeTemp(TargetReg(kArg2, kNotWide));
410 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 /*
412 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
413 * return region. Because AllocFromCode placed the new array
414 * in kRet0, we'll just lock it into place. When debugger support is
415 * added, it may be necessary to additionally copy all return
416 * values to a home location in thread-local storage
417 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700418 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700419 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420
421 // TODO: use the correct component size, currently all supported types
422 // share array alignment with ints (see comment at head of function)
423 size_t component_size = sizeof(int32_t);
424
Vladimir Markobf535be2014-11-19 18:52:35 +0000425 if (elems > 5) {
426 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 /*
428 * Bit of ugliness here. We're going generate a mem copy loop
429 * on the register range, but it is possible that some regs
430 * in the range have been promoted. This is unlikely, but
431 * before generating the copy, we'll just force a flush
432 * of any regs in the source range that have been promoted to
433 * home location.
434 */
435 for (int i = 0; i < elems; i++) {
436 RegLocation loc = UpdateLoc(info->args[i]);
437 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100438 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600439 if (loc.ref) {
440 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
441 } else {
442 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 }
445 }
446 /*
447 * TUNING note: generated code here could be much improved, but
448 * this is an uncommon operation and isn't especially performance
449 * critical.
450 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700451 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700452 RegStorage r_src = AllocTempRef();
453 RegStorage r_dst = AllocTempRef();
454 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800455 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700456 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700458 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700459 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 break;
461 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700462 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700463 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 r_val = AllocTemp();
465 break;
466 case kMips:
467 r_val = AllocTemp();
468 break;
469 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
470 }
471 // Set up source pointer
472 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700473 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700475 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 mirror::Array::DataOffset(component_size).Int32Value());
477 // Set up the loop counter (known to be > 0)
478 LoadConstant(r_idx, elems - 1);
479 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800480 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100482 {
483 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
484 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
485 // NOTE: No dalvik register annotation, local optimizations will be stopped
486 // by the loop boundaries.
487 }
buzbee695d13a2014-04-19 13:32:20 -0700488 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800490 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700491 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700493 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 -mirror::Array::DataOffset(component_size).Int32Value());
495 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000496 FreeTemp(r_idx);
497 FreeTemp(r_dst);
498 FreeTemp(r_src);
499 } else {
500 DCHECK_LE(elems, 5); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 // TUNING: interleave
502 for (int i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600503 RegLocation rl_arg;
504 if (info->args[i].ref) {
505 rl_arg = LoadValue(info->args[i], kRefReg);
506 StoreRefDisp(ref_reg,
507 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
508 kNotVolatile);
509 } else {
510 rl_arg = LoadValue(info->args[i], kCoreReg);
511 Store32Disp(ref_reg,
512 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
513 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800515 if (IsTemp(rl_arg.reg)) {
516 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 }
518 }
519 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000520 if (elems != 0 && info->args[0].ref) {
521 // If there is at least one potentially non-null value, unconditionally mark the GC card.
522 for (int i = 0; i < elems; i++) {
523 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
524 UnconditionallyMarkGCCard(ref_reg);
525 break;
526 }
527 }
528 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700530 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 }
532}
533
Ian Rogers832336b2014-10-08 15:35:22 -0700534/*
535 * Array data table format:
536 * ushort ident = 0x0300 magic value
537 * ushort width width of each element in the table
538 * uint size number of elements in the table
539 * ubyte data[size*width] table of data values (may contain a single-byte
540 * padding at the end)
541 *
542 * Total size is 4+(width * size + 1)/2 16-bit code units.
543 */
544void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
545 if (kIsDebugBuild) {
546 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
547 const Instruction::ArrayDataPayload* payload =
548 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
549 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
550 }
551 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
552 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
553}
554
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800555//
556// Slow path to ensure a class is initialized for sget/sput.
557//
558class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
559 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100560 // There are up to two branches to the static field slow path, the "unresolved" when the type
561 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
562 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800563 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100564 RegStorage r_base)
565 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
566 second_branch_(unresolved != nullptr ? uninit : nullptr),
567 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800568 }
569
570 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100571 LIR* target = GenerateTargetLabel();
572 if (second_branch_ != nullptr) {
573 second_branch_->target = target;
574 }
Andreas Gampe98430592014-07-27 19:44:50 -0700575 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800576 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700577 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800578
579 m2l_->OpUnconditionalBranch(cont_);
580 }
581
582 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100583 // Second branch to the slow path, or null if there's only one branch.
584 LIR* const second_branch_;
585
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800586 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800587 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800588};
589
Fred Shih37f05ef2014-07-16 18:38:08 -0700590void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000591 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000592 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000593 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700594 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000595 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800596 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000597 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100599 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700600 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000601 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
602 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800603 if (IsTemp(rl_method.reg)) {
604 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 }
606 } else {
607 // Medium path, static storage base in a different class which requires checks that the other
608 // class is initialized.
609 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000610 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 // May do runtime call so everything to home locations.
612 FlushAllRegs();
613 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700614 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615 LockTemp(r_method);
616 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700617 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000619 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
620 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000621 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000622 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800623 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100624 LIR* unresolved_branch = nullptr;
625 if (!field_info.IsClassInDexCache() &&
626 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
627 // Check if r_base is NULL.
628 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
629 }
630 LIR* uninit_branch = nullptr;
631 if (!field_info.IsClassInitialized() &&
632 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
633 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700634 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100636 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800637 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000638 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100639 FreeTemp(r_tmp);
640 }
641 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
642 // The slow path is invoked if the r_base is NULL or the class pointed
643 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800644 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800645 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000646 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800647
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100648 if (uninit_branch != nullptr) {
649 // Ensure load of status and store of value don't re-order.
650 // TODO: Presumably the actual value store is control-dependent on the status load,
651 // and will thus not be reordered in any case, since stores are never speculated.
652 // Does later code "know" that the class is now initialized? If so, we still
653 // need the barrier to guard later static loads.
654 GenMemBarrier(kLoadAny);
655 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 FreeTemp(r_method);
658 }
659 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700660 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
661 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100662 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100664 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700666 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000667 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
668 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100669 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700670 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000671 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700673 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000674 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800676 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 } else {
678 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700679 QuickEntrypointEnum target;
680 switch (size) {
681 case kReference:
682 target = kQuickSetObjStatic;
683 break;
684 case k64:
685 case kDouble:
686 target = kQuickSet64Static;
687 break;
688 case k32:
689 case kSingle:
690 target = kQuickSet32Static;
691 break;
692 case kSignedHalf:
693 case kUnsignedHalf:
694 target = kQuickSet16Static;
695 break;
696 case kSignedByte:
697 case kUnsignedByte:
698 target = kQuickSet8Static;
699 break;
700 case kWord: // Intentional fallthrough.
701 default:
702 LOG(FATAL) << "Can't determine entrypoint for: " << size;
703 target = kQuickSet32Static;
704 }
Andreas Gampe98430592014-07-27 19:44:50 -0700705 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 }
707}
708
Fred Shih37f05ef2014-07-16 18:38:08 -0700709void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000710 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000711 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000712 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700713
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700714 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000715 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800716 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000717 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 // Fast path, static storage base is this method's class
719 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700720 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000721 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
722 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 } else {
724 // Medium path, static storage base in a different class which requires checks that the other
725 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000726 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 // May do runtime call so everything to home locations.
728 FlushAllRegs();
729 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700730 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 LockTemp(r_method);
732 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700733 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800734 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000735 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
736 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000737 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000738 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800739 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100740 LIR* unresolved_branch = nullptr;
741 if (!field_info.IsClassInDexCache() &&
742 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
743 // Check if r_base is NULL.
744 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
745 }
746 LIR* uninit_branch = nullptr;
747 if (!field_info.IsClassInitialized() &&
748 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
749 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700750 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800751 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100752 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800753 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000754 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100755 FreeTemp(r_tmp);
756 }
757 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
758 // The slow path is invoked if the r_base is NULL or the class pointed
759 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800760 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800761 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000762 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800763
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100764 if (uninit_branch != nullptr) {
765 // Ensure load of status and load of value don't re-order.
766 GenMemBarrier(kLoadAny);
767 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 FreeTemp(r_method);
770 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800771 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700772 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100773 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800774
Vladimir Marko674744e2014-04-24 15:18:26 +0100775 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700776 if (IsRef(size)) {
777 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000778 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
779 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100780 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700781 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000782 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800783 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100784 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800785
Fred Shih37f05ef2014-07-16 18:38:08 -0700786 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 StoreValueWide(rl_dest, rl_result);
788 } else {
789 StoreValue(rl_dest, rl_result);
790 }
791 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700792 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700794 QuickEntrypointEnum target;
795 switch (type) {
796 case Primitive::kPrimNot:
797 target = kQuickGetObjStatic;
798 break;
799 case Primitive::kPrimLong:
800 case Primitive::kPrimDouble:
801 target = kQuickGet64Static;
802 break;
803 case Primitive::kPrimInt:
804 case Primitive::kPrimFloat:
805 target = kQuickGet32Static;
806 break;
807 case Primitive::kPrimShort:
808 target = kQuickGetShortStatic;
809 break;
810 case Primitive::kPrimChar:
811 target = kQuickGetCharStatic;
812 break;
813 case Primitive::kPrimByte:
814 target = kQuickGetByteStatic;
815 break;
816 case Primitive::kPrimBoolean:
817 target = kQuickGetBooleanStatic;
818 break;
819 case Primitive::kPrimVoid: // Intentional fallthrough.
820 default:
821 LOG(FATAL) << "Can't determine entrypoint for: " << type;
822 target = kQuickGet32Static;
823 }
Andreas Gampe98430592014-07-27 19:44:50 -0700824 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
825
Douglas Leung2db3e262014-06-25 16:02:55 -0700826 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700827 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700828 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 StoreValueWide(rl_dest, rl_result);
830 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700831 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValue(rl_dest, rl_result);
833 }
834 }
835}
836
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800837// Generate code for all slow paths.
838void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700839 // We should check slow_paths_.Size() every time, because a new slow path
840 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100841 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800842 slowpath->Compile();
843 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100844 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800845}
846
Fred Shih37f05ef2014-07-16 18:38:08 -0700847void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
848 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000849 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000850 DCHECK_EQ(IGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000851 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700852 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700853 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700854 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000855 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700856 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100857 GenNullCheck(rl_obj.reg, opt_flags);
858 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
859 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000860 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700861 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000862 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
863 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100864 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700865 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000866 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100867 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000868 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700869 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 StoreValueWide(rl_dest, rl_result);
871 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 StoreValue(rl_dest, rl_result);
873 }
874 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700875 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
876 QuickEntrypointEnum target;
877 switch (type) {
878 case Primitive::kPrimNot:
879 target = kQuickGetObjInstance;
880 break;
881 case Primitive::kPrimLong:
882 case Primitive::kPrimDouble:
883 target = kQuickGet64Instance;
884 break;
885 case Primitive::kPrimFloat:
886 case Primitive::kPrimInt:
887 target = kQuickGet32Instance;
888 break;
889 case Primitive::kPrimShort:
890 target = kQuickGetShortInstance;
891 break;
892 case Primitive::kPrimChar:
893 target = kQuickGetCharInstance;
894 break;
895 case Primitive::kPrimByte:
896 target = kQuickGetByteInstance;
897 break;
898 case Primitive::kPrimBoolean:
899 target = kQuickGetBooleanInstance;
900 break;
901 case Primitive::kPrimVoid: // Intentional fallthrough.
902 default:
903 LOG(FATAL) << "Can't determine entrypoint for: " << type;
904 target = kQuickGet32Instance;
905 }
Andreas Gampe98430592014-07-27 19:44:50 -0700906 // Second argument of pGetXXInstance is always a reference.
907 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
908 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
909
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700910 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700911 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700912 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 StoreValueWide(rl_dest, rl_result);
914 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700915 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 StoreValue(rl_dest, rl_result);
917 }
918 }
919}
920
Vladimir Markobe0e5462014-02-26 11:24:15 +0000921void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700922 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000923 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000924 DCHECK_EQ(IPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000925 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700926 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700927 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700928 // Dex code never writes to the class field.
929 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
930 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700931 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700932 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100933 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 } else {
935 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100936 }
937 GenNullCheck(rl_obj.reg, opt_flags);
938 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000939 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700940 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000941 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
942 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100943 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700944 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000945 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100946 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000947 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700948 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000949 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950 }
951 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700952 QuickEntrypointEnum target;
953 switch (size) {
954 case kReference:
955 target = kQuickSetObjInstance;
956 break;
957 case k64:
958 case kDouble:
959 target = kQuickSet64Instance;
960 break;
961 case k32:
962 case kSingle:
963 target = kQuickSet32Instance;
964 break;
965 case kSignedHalf:
966 case kUnsignedHalf:
967 target = kQuickSet16Instance;
968 break;
969 case kSignedByte:
970 case kUnsignedByte:
971 target = kQuickSet8Instance;
972 break;
973 case kWord: // Intentional fallthrough.
974 default:
975 LOG(FATAL) << "Can't determine entrypoint for: " << size;
976 target = kQuickSet32Instance;
977 }
Andreas Gampe98430592014-07-27 19:44:50 -0700978 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
979 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 }
981}
982
Ian Rogersa9a82542013-10-04 11:17:26 -0700983void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
984 RegLocation rl_src) {
985 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
986 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
987 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700988 QuickEntrypointEnum target = needs_range_check
989 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
990 : kQuickAputObjectWithBoundCheck)
991 : kQuickAputObject;
992 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700993}
994
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700995void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700997 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700998 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001000 *cu_->dex_file,
1001 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 // Call out to helper which resolves type and verifies access.
1003 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -07001004 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001005 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 StoreValue(rl_dest, rl_result);
1007 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001008 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 // We're don't need access checks, load type from dex cache
1010 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001011 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001012 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001013 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001014 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
1016 type_idx) || SLOW_TYPE_PATH) {
1017 // Slow path, at runtime test if type is null and if so initialize
1018 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001019 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001020 LIR* cont = NewLIR0(kPseudoTargetLabel);
1021
1022 // Object to generate the slow path for class resolution.
1023 class SlowPath : public LIRSlowPath {
1024 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001025 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1026 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
1027 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1028 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001029 }
1030
1031 void Compile() {
1032 GenerateTargetLabel();
1033
Andreas Gampe98430592014-07-27 19:44:50 -07001034 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001035 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001036 m2l_->OpUnconditionalBranch(cont_);
1037 }
1038
1039 private:
1040 const int type_idx_;
1041 const RegLocation rl_method_;
1042 const RegLocation rl_result_;
1043 };
1044
1045 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001046 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001047
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001049 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 // Fast path, we're done - just store result
1051 StoreValue(rl_dest, rl_result);
1052 }
1053 }
1054}
1055
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001056void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001058 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1059 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1061 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1062 // slow path, resolve string if not in dex cache
1063 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001064 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001065
1066 // If the Method* is already in a register, we can save a copy.
1067 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001068 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001069 if (rl_method.location == kLocPhysReg) {
1070 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001071 DCHECK(!IsTemp(rl_method.reg));
1072 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001073 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001074 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001075 LoadCurrMethodDirect(r_method);
1076 }
Mathieu Chartiereace4582014-11-24 18:29:54 -08001077 // Method to declaring class.
1078 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1079 TargetReg(kArg0, kRef), kNotVolatile);
1080 // Declaring class to dex cache strings.
1081 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Class::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001082 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001083
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001085 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1086 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001087 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001088
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001089 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001090 // Object to generate the slow path for string resolution.
1091 class SlowPath : public LIRSlowPath {
1092 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001093 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1094 int32_t string_idx_in) :
1095 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1096 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001097 }
1098
1099 void Compile() {
1100 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001101 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001102 m2l_->OpUnconditionalBranch(cont_);
1103 }
1104
1105 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001106 const RegStorage r_method_;
1107 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001108 };
1109
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001110 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001112
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001114 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 } else {
1116 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001117 RegStorage res_reg = AllocTempRef();
1118 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001119 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1120 kNotVolatile);
1121 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001122 kNotVolatile);
1123 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 StoreValue(rl_dest, rl_result);
1125 }
1126}
1127
Andreas Gampe98430592014-07-27 19:44:50 -07001128/*
1129 * Let helper function take care of everything. Will
1130 * call Class::NewInstanceFromCode(type_idx, method);
1131 */
1132void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1133 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 // alloc will always check for resolution, do we also need to verify
1135 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001136 const DexFile* dex_file = cu_->dex_file;
1137 CompilerDriver* driver = cu_->compiler_driver;
1138 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001139 bool is_type_initialized;
1140 bool use_direct_type_ptr;
1141 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001142 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001143 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001144 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1145 &direct_type_ptr, &is_finalizable) &&
1146 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001147 // The fast path.
1148 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001149 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001150 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001151 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001152 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001153 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001154 }
1155 } else {
1156 // Use the direct pointer.
1157 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001158 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001159 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001160 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001161 }
1162 }
1163 } else {
1164 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001165 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001166 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001168 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
Andreas Gampe98430592014-07-27 19:44:50 -07001170 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171}
1172
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001173void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001175 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176}
1177
1178// For final classes there are no sub-classes to check and so we can answer the instance-of
1179// question with simple comparisons.
1180void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1181 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001182 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001183 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001184
buzbeea0cd2d72014-06-01 09:33:49 -07001185 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001187 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001188 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001190 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 }
1192 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001193 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194
buzbeea0cd2d72014-06-01 09:33:49 -07001195 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1196 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197
1198 LoadCurrMethodDirect(check_class);
1199 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001200 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1201 kNotVolatile);
1202 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1203 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 } else {
buzbee695d13a2014-04-19 13:32:20 -07001205 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001206 check_class, kNotVolatile);
1207 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1208 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001209 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001210 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 }
1212
buzbee695d13a2014-04-19 13:32:20 -07001213 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 if (cu_->instruction_set == kThumb2) {
1215 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001216 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001218 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001220 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 }
1222 LIR* target = NewLIR0(kPseudoTargetLabel);
1223 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 FreeTemp(object_class);
1225 FreeTemp(check_class);
1226 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001227 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 FreeTemp(result_reg);
1229 }
1230 StoreValue(rl_dest, rl_result);
1231}
1232
1233void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1234 bool type_known_abstract, bool use_declaring_class,
1235 bool can_assume_type_is_in_dex_cache,
1236 uint32_t type_idx, RegLocation rl_dest,
1237 RegLocation rl_src) {
1238 FlushAllRegs();
1239 // May generate a call - use explicit registers
1240 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001241 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001242 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001243 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001244 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1245 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 if (needs_access_check) {
1247 // Check we have access to type_idx and if not throw IllegalAccessError,
1248 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001249 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001250 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1251 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001253 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001254 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001255 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001257 if (can_assume_type_is_in_dex_cache) {
1258 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001259 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001260 }
1261
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001263 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001264 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001265 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001266 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001268 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1269 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1270
1271 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001272 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001273
1274 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1275 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001276 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1277 RegLocation rl_src_in)
1278 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1279 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001280 }
1281
1282 void Compile() OVERRIDE {
1283 GenerateTargetLabel();
1284
Andreas Gampe98430592014-07-27 19:44:50 -07001285 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001286 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1287 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001288 m2l_->OpUnconditionalBranch(cont_);
1289 }
1290
1291 private:
1292 uint32_t type_idx_;
1293 RegLocation rl_src_;
1294 };
1295
1296 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1297 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 }
1299 }
1300 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001301 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001302 if (!IsSameReg(rl_result.reg, ref_reg)) {
1303 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001304 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001306 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307
1308 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001309 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001311 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1312 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1314 LIR* branchover = NULL;
1315 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001316 // rl_result == ref == class.
1317 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001318 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 } else {
1320 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001321 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001322 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 if (!type_known_abstract) {
1324 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001325 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001326 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001327 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001329 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001331 if (it != nullptr) {
1332 OpEndIT(it);
1333 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 FreeTemp(r_tgt);
1335 } else {
1336 if (!type_known_abstract) {
1337 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001338 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001339 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001341
Serguei Katkov9ee45192014-07-17 14:39:03 +07001342 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001343 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 }
1345 }
1346 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001347 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 /* branch targets here */
1349 LIR* target = NewLIR0(kPseudoTargetLabel);
1350 StoreValue(rl_dest, rl_result);
1351 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001352 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 branchover->target = target;
1354 }
1355}
1356
1357void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1358 bool type_known_final, type_known_abstract, use_declaring_class;
1359 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1360 *cu_->dex_file,
1361 type_idx,
1362 &type_known_final,
1363 &type_known_abstract,
1364 &use_declaring_class);
1365 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1366 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1367
1368 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1369 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1370 } else {
1371 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1372 use_declaring_class, can_assume_type_is_in_dex_cache,
1373 type_idx, rl_dest, rl_src);
1374 }
1375}
1376
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001377void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 bool type_known_final, type_known_abstract, use_declaring_class;
1379 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1380 *cu_->dex_file,
1381 type_idx,
1382 &type_known_final,
1383 &type_known_abstract,
1384 &use_declaring_class);
1385 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1386 // of the exception throw path.
1387 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001388 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 // Verifier type analysis proved this check cast would never cause an exception.
1390 return;
1391 }
1392 FlushAllRegs();
1393 // May generate a call - use explicit registers
1394 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001395 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001396 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001397 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 if (needs_access_check) {
1399 // Check we have access to type_idx and if not throw IllegalAccessError,
1400 // returns Class* in kRet0
1401 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001402 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001403 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001405 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001406 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 } else {
1408 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001409 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001410 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001411 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001412 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1414 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1416 LIR* cont = NewLIR0(kPseudoTargetLabel);
1417
1418 // Slow path to initialize the type. Executed if the type is NULL.
1419 class SlowPath : public LIRSlowPath {
1420 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001421 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1422 const RegStorage class_reg_in) :
1423 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1424 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001425 }
1426
1427 void Compile() {
1428 GenerateTargetLabel();
1429
1430 // Call out to helper, which will return resolved type in kArg0
1431 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001432 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1433 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001434 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001435 m2l_->OpUnconditionalBranch(cont_);
1436 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001437
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001438 public:
1439 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001440 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001441 };
1442
buzbee2700f7e2014-03-07 09:46:20 -08001443 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 }
1445 }
1446 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001447 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001448
1449 // Slow path for the case where the classes are not equal. In this case we need
1450 // to call a helper function to do the check.
1451 class SlowPath : public LIRSlowPath {
1452 public:
1453 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1454 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1455 }
1456
1457 void Compile() {
1458 GenerateTargetLabel();
1459
1460 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001461 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1462 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001463 }
Andreas Gampe98430592014-07-27 19:44:50 -07001464 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1465 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001466 m2l_->OpUnconditionalBranch(cont_);
1467 }
1468
1469 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001470 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001471 };
1472
1473 if (type_known_abstract) {
1474 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001475 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001476 LIR* cont = NewLIR0(kPseudoTargetLabel);
1477 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1478 } else {
1479 // Harder, more common case. We need to generate a forward branch over the load
1480 // if the target is null. If it's non-null we perform the load and branch to the
1481 // slow path if the classes are not equal.
1482
1483 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001484 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001485 /* load object->klass_ */
1486 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001487 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1488 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001489
Andreas Gampeccc60262014-07-04 18:02:38 -07001490 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001491 LIR* cont = NewLIR0(kPseudoTargetLabel);
1492
1493 // Add the slow path that will not perform load since this is already done.
1494 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1495
1496 // Set the null check to branch to the continuation.
1497 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 }
1499}
1500
1501void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001502 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 RegLocation rl_result;
1504 if (cu_->instruction_set == kThumb2) {
1505 /*
1506 * NOTE: This is the one place in the code in which we might have
1507 * as many as six live temporary registers. There are 5 in the normal
1508 * set for Arm. Until we have spill capabilities, temporarily add
1509 * lr to the temp set. It is safe to do this locally, but note that
1510 * lr is used explicitly elsewhere in the code generator and cannot
1511 * normally be used as a general temp register.
1512 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001513 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1514 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
1516 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1517 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1518 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1519 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001520 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1521 RegStorage t_reg = AllocTemp();
1522 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1523 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1524 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 FreeTemp(t_reg);
1526 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001527 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1528 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 }
1530 /*
1531 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1532 * following StoreValueWide might need to allocate a temp register.
1533 * To further work around the lack of a spill capability, explicitly
1534 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1535 * Remove when spill is functional.
1536 */
1537 FreeRegLocTemps(rl_result, rl_src1);
1538 FreeRegLocTemps(rl_result, rl_src2);
1539 StoreValueWide(rl_dest, rl_result);
1540 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001541 Clobber(TargetReg(kLr, kNotWide));
1542 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 }
1544}
1545
Andreas Gampe98430592014-07-27 19:44:50 -07001546void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1547 RegLocation rl_src1, RegLocation rl_shift) {
1548 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 switch (opcode) {
1550 case Instruction::SHL_LONG:
1551 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001552 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 break;
1554 case Instruction::SHR_LONG:
1555 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001556 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 break;
1558 case Instruction::USHR_LONG:
1559 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001560 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 break;
1562 default:
1563 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001564 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565 }
Andreas Gampe98430592014-07-27 19:44:50 -07001566 FlushAllRegs(); /* Send everything to home location */
1567 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001568 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 StoreValueWide(rl_dest, rl_result);
1570}
1571
1572
1573void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001574 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001575 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 OpKind op = kOpBkpt;
1577 bool is_div_rem = false;
1578 bool check_zero = false;
1579 bool unary = false;
1580 RegLocation rl_result;
1581 bool shift_op = false;
1582 switch (opcode) {
1583 case Instruction::NEG_INT:
1584 op = kOpNeg;
1585 unary = true;
1586 break;
1587 case Instruction::NOT_INT:
1588 op = kOpMvn;
1589 unary = true;
1590 break;
1591 case Instruction::ADD_INT:
1592 case Instruction::ADD_INT_2ADDR:
1593 op = kOpAdd;
1594 break;
1595 case Instruction::SUB_INT:
1596 case Instruction::SUB_INT_2ADDR:
1597 op = kOpSub;
1598 break;
1599 case Instruction::MUL_INT:
1600 case Instruction::MUL_INT_2ADDR:
1601 op = kOpMul;
1602 break;
1603 case Instruction::DIV_INT:
1604 case Instruction::DIV_INT_2ADDR:
1605 check_zero = true;
1606 op = kOpDiv;
1607 is_div_rem = true;
1608 break;
1609 /* NOTE: returns in kArg1 */
1610 case Instruction::REM_INT:
1611 case Instruction::REM_INT_2ADDR:
1612 check_zero = true;
1613 op = kOpRem;
1614 is_div_rem = true;
1615 break;
1616 case Instruction::AND_INT:
1617 case Instruction::AND_INT_2ADDR:
1618 op = kOpAnd;
1619 break;
1620 case Instruction::OR_INT:
1621 case Instruction::OR_INT_2ADDR:
1622 op = kOpOr;
1623 break;
1624 case Instruction::XOR_INT:
1625 case Instruction::XOR_INT_2ADDR:
1626 op = kOpXor;
1627 break;
1628 case Instruction::SHL_INT:
1629 case Instruction::SHL_INT_2ADDR:
1630 shift_op = true;
1631 op = kOpLsl;
1632 break;
1633 case Instruction::SHR_INT:
1634 case Instruction::SHR_INT_2ADDR:
1635 shift_op = true;
1636 op = kOpAsr;
1637 break;
1638 case Instruction::USHR_INT:
1639 case Instruction::USHR_INT_2ADDR:
1640 shift_op = true;
1641 op = kOpLsr;
1642 break;
1643 default:
1644 LOG(FATAL) << "Invalid word arith op: " << opcode;
1645 }
1646 if (!is_div_rem) {
1647 if (unary) {
1648 rl_src1 = LoadValue(rl_src1, kCoreReg);
1649 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001650 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001652 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001653 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001654 RegStorage t_reg = AllocTemp();
1655 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 rl_src1 = LoadValue(rl_src1, kCoreReg);
1657 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001658 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 FreeTemp(t_reg);
1660 } else {
1661 rl_src1 = LoadValue(rl_src1, kCoreReg);
1662 rl_src2 = LoadValue(rl_src2, kCoreReg);
1663 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001664 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 }
1666 }
1667 StoreValue(rl_dest, rl_result);
1668 } else {
Dave Allison70202782013-10-22 17:52:19 -07001669 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 +01001670 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 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);
Brian Carlstrom7940e442013-07-12 13:46:57 -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 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001679 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1680 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001681 // Use ARM SDIV instruction for division. For remainder we also need to
1682 // calculate using a MUL and subtract.
1683 rl_src1 = LoadValue(rl_src1, kCoreReg);
1684 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001685 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001686 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001687 }
buzbee2700f7e2014-03-07 09:46:20 -08001688 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001689 done = true;
1690 }
1691 }
1692
1693 // If we haven't already generated the code use the callout function.
1694 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001696 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001697 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001698 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001699 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001700 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 }
Dave Allison70202782013-10-22 17:52:19 -07001702 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001703 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001705 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 else
1707 rl_result = GetReturnAlt();
1708 }
1709 StoreValue(rl_dest, rl_result);
1710 }
1711}
1712
1713/*
1714 * The following are the first-level codegen routines that analyze the format
1715 * of each bytecode then either dispatch special purpose codegen routines
1716 * or produce corresponding Thumb instructions directly.
1717 */
1718
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001720static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 x &= x - 1;
1722 return (x & (x - 1)) == 0;
1723}
1724
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1726// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001727bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001728 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1730 return false;
1731 }
1732 // No divide instruction for Arm, so check for more special cases
1733 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001734 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735 }
1736 int k = LowestSetBit(lit);
1737 if (k >= 30) {
1738 // Avoid special cases.
1739 return false;
1740 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 rl_src = LoadValue(rl_src, kCoreReg);
1742 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001743 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001744 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 if (lit == 2) {
1746 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001747 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1748 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1749 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001753 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1754 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 }
1756 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001757 RegStorage t_reg1 = AllocTemp();
1758 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001760 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1761 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001763 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001765 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001767 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001769 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 }
1771 }
1772 StoreValue(rl_dest, rl_result);
1773 return true;
1774}
1775
1776// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1777// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001778bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001779 if (lit < 0) {
1780 return false;
1781 }
1782 if (lit == 0) {
1783 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1784 LoadConstant(rl_result.reg, 0);
1785 StoreValue(rl_dest, rl_result);
1786 return true;
1787 }
1788 if (lit == 1) {
1789 rl_src = LoadValue(rl_src, kCoreReg);
1790 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1791 OpRegCopy(rl_result.reg, rl_src.reg);
1792 StoreValue(rl_dest, rl_result);
1793 return true;
1794 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001795 // There is RegRegRegShift on Arm, so check for more special cases
1796 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001797 return EasyMultiply(rl_src, rl_dest, lit);
1798 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001799 // Can we simplify this multiplication?
1800 bool power_of_two = false;
1801 bool pop_count_le2 = false;
1802 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001803 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 power_of_two = true;
1805 } else if (IsPopCountLE2(lit)) {
1806 pop_count_le2 = true;
1807 } else if (IsPowerOfTwo(lit + 1)) {
1808 power_of_two_minus_one = true;
1809 } else {
1810 return false;
1811 }
1812 rl_src = LoadValue(rl_src, kCoreReg);
1813 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1814 if (power_of_two) {
1815 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001816 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001817 } else if (pop_count_le2) {
1818 // Shift and add and shift.
1819 int first_bit = LowestSetBit(lit);
1820 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1821 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1822 } else {
1823 // Reverse subtract: (src << (shift + 1)) - src.
1824 DCHECK(power_of_two_minus_one);
1825 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001826 RegStorage t_reg = AllocTemp();
1827 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1828 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001829 }
1830 StoreValue(rl_dest, rl_result);
1831 return true;
1832}
1833
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001834// Returns true if it generates instructions.
1835bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1836 RegLocation rl_src2) {
1837 if (!rl_src2.is_const ||
1838 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1839 return false;
1840 }
1841
1842 if (!rl_src2.wide) {
1843 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1844 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1845 // Generate multiply by reciprocal instead of div.
1846 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1847 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1848 return true;
1849 }
1850 } else {
1851 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1852 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1853 // Generate multiply by reciprocal instead of div.
1854 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1855 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1856 return true;
1857 }
1858 }
1859 return false;
1860}
1861
Brian Carlstrom7940e442013-07-12 13:46:57 -07001862void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001863 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 RegLocation rl_result;
1865 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1866 int shift_op = false;
1867 bool is_div = false;
1868
1869 switch (opcode) {
1870 case Instruction::RSUB_INT_LIT8:
1871 case Instruction::RSUB_INT: {
1872 rl_src = LoadValue(rl_src, kCoreReg);
1873 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1874 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001875 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001876 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001877 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1878 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 }
1880 StoreValue(rl_dest, rl_result);
1881 return;
1882 }
1883
1884 case Instruction::SUB_INT:
1885 case Instruction::SUB_INT_2ADDR:
1886 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001887 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 case Instruction::ADD_INT:
1889 case Instruction::ADD_INT_2ADDR:
1890 case Instruction::ADD_INT_LIT8:
1891 case Instruction::ADD_INT_LIT16:
1892 op = kOpAdd;
1893 break;
1894 case Instruction::MUL_INT:
1895 case Instruction::MUL_INT_2ADDR:
1896 case Instruction::MUL_INT_LIT8:
1897 case Instruction::MUL_INT_LIT16: {
1898 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1899 return;
1900 }
1901 op = kOpMul;
1902 break;
1903 }
1904 case Instruction::AND_INT:
1905 case Instruction::AND_INT_2ADDR:
1906 case Instruction::AND_INT_LIT8:
1907 case Instruction::AND_INT_LIT16:
1908 op = kOpAnd;
1909 break;
1910 case Instruction::OR_INT:
1911 case Instruction::OR_INT_2ADDR:
1912 case Instruction::OR_INT_LIT8:
1913 case Instruction::OR_INT_LIT16:
1914 op = kOpOr;
1915 break;
1916 case Instruction::XOR_INT:
1917 case Instruction::XOR_INT_2ADDR:
1918 case Instruction::XOR_INT_LIT8:
1919 case Instruction::XOR_INT_LIT16:
1920 op = kOpXor;
1921 break;
1922 case Instruction::SHL_INT_LIT8:
1923 case Instruction::SHL_INT:
1924 case Instruction::SHL_INT_2ADDR:
1925 lit &= 31;
1926 shift_op = true;
1927 op = kOpLsl;
1928 break;
1929 case Instruction::SHR_INT_LIT8:
1930 case Instruction::SHR_INT:
1931 case Instruction::SHR_INT_2ADDR:
1932 lit &= 31;
1933 shift_op = true;
1934 op = kOpAsr;
1935 break;
1936 case Instruction::USHR_INT_LIT8:
1937 case Instruction::USHR_INT:
1938 case Instruction::USHR_INT_2ADDR:
1939 lit &= 31;
1940 shift_op = true;
1941 op = kOpLsr;
1942 break;
1943
1944 case Instruction::DIV_INT:
1945 case Instruction::DIV_INT_2ADDR:
1946 case Instruction::DIV_INT_LIT8:
1947 case Instruction::DIV_INT_LIT16:
1948 case Instruction::REM_INT:
1949 case Instruction::REM_INT_2ADDR:
1950 case Instruction::REM_INT_LIT8:
1951 case Instruction::REM_INT_LIT16: {
1952 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001953 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 return;
1955 }
buzbee11b63d12013-08-27 07:34:17 -07001956 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001958 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 (opcode == Instruction::DIV_INT_LIT16)) {
1960 is_div = true;
1961 } else {
1962 is_div = false;
1963 }
buzbee11b63d12013-08-27 07:34:17 -07001964 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1965 return;
1966 }
Dave Allison70202782013-10-22 17:52:19 -07001967
1968 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001969 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001970 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001971 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001972 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001973 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001974 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1975 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001976 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001977 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1978 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001979 // Use ARM SDIV instruction for division. For remainder we also need to
1980 // calculate using a MUL and subtract.
1981 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001982 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001983 done = true;
1984 }
1985 }
1986
1987 if (!done) {
1988 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001989 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1990 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001991 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001993 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 else
1995 rl_result = GetReturnAlt();
1996 }
1997 StoreValue(rl_dest, rl_result);
1998 return;
1999 }
2000 default:
2001 LOG(FATAL) << "Unexpected opcode " << opcode;
2002 }
2003 rl_src = LoadValue(rl_src, kCoreReg);
2004 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07002005 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08002007 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008 } else {
buzbee2700f7e2014-03-07 09:46:20 -08002009 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002010 }
2011 StoreValue(rl_dest, rl_result);
2012}
2013
Andreas Gampe98430592014-07-27 19:44:50 -07002014void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002015 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 RegLocation rl_result;
2017 OpKind first_op = kOpBkpt;
2018 OpKind second_op = kOpBkpt;
2019 bool call_out = false;
2020 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002021 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2022 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023
2024 switch (opcode) {
2025 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002026 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2027 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002029 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002030 RegStorage t_reg = AllocTemp();
2031 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2032 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2033 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2034 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002036 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2037 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 }
Andreas Gampe98430592014-07-27 19:44:50 -07002039 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 return;
2041 case Instruction::ADD_LONG:
2042 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 first_op = kOpAdd;
2044 second_op = kOpAdc;
2045 break;
2046 case Instruction::SUB_LONG:
2047 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 first_op = kOpSub;
2049 second_op = kOpSbc;
2050 break;
2051 case Instruction::MUL_LONG:
2052 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002053 call_out = true;
2054 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2055 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002056 break;
2057 case Instruction::DIV_LONG:
2058 case Instruction::DIV_LONG_2ADDR:
2059 call_out = true;
2060 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002061 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2062 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002063 break;
2064 case Instruction::REM_LONG:
2065 case Instruction::REM_LONG_2ADDR:
2066 call_out = true;
2067 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002068 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002069 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002070 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2071 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 break;
2073 case Instruction::AND_LONG_2ADDR:
2074 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002075 first_op = kOpAnd;
2076 second_op = kOpAnd;
2077 break;
2078 case Instruction::OR_LONG:
2079 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 first_op = kOpOr;
2081 second_op = kOpOr;
2082 break;
2083 case Instruction::XOR_LONG:
2084 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085 first_op = kOpXor;
2086 second_op = kOpXor;
2087 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 default:
2089 LOG(FATAL) << "Invalid long arith op";
2090 }
2091 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002092 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002094 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002096 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2097 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2098 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2099 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002100 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2101 GenDivZeroCheckWide(r_tmp2);
2102 }
Andreas Gampe98430592014-07-27 19:44:50 -07002103 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002105 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002106 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002107 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002108 }
2109 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002110 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2111 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 else
Andreas Gampe98430592014-07-27 19:44:50 -07002113 rl_result = GetReturnWideAlt();
2114 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002115 }
2116}
2117
Mark Mendelle87f9b52014-04-30 14:13:18 -04002118void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2119 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2120 LoadConstantNoClobber(rl_result.reg, value);
2121 StoreValue(rl_dest, rl_result);
2122 if (value == 0) {
2123 Workaround7250540(rl_dest, rl_result.reg);
2124 }
2125}
2126
Andreas Gampe98430592014-07-27 19:44:50 -07002127void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2128 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002129 /*
2130 * Don't optimize the register usage since it calls out to support
2131 * functions
2132 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002133
Brian Carlstrom7940e442013-07-12 13:46:57 -07002134 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002135 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 if (rl_dest.wide) {
2137 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002138 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 StoreValueWide(rl_dest, rl_result);
2140 } else {
2141 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002142 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002143 StoreValue(rl_dest, rl_result);
2144 }
2145}
2146
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002147class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2148 public:
2149 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2150 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2151 }
2152
2153 void Compile() OVERRIDE {
2154 m2l_->ResetRegPool();
2155 m2l_->ResetDefTracking();
2156 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002157 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002158 if (cont_ != nullptr) {
2159 m2l_->OpUnconditionalBranch(cont_);
2160 }
2161 }
2162};
2163
Brian Carlstrom7940e442013-07-12 13:46:57 -07002164/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002165void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002166 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002167 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2168 return;
2169 }
2170 FlushAllRegs();
2171 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002172 LIR* cont = NewLIR0(kPseudoTargetLabel);
2173 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002174 } else {
2175 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2176 return;
2177 }
2178 FlushAllRegs(); // TODO: needed?
2179 LIR* inst = CheckSuspendUsingLoad();
2180 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002181 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002182}
2183
2184/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002185void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002186 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002187 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2188 OpUnconditionalBranch(target);
2189 return;
2190 }
2191 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002192 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002193 LIR* branch = OpUnconditionalBranch(nullptr);
2194 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002195 } else {
2196 // For the implicit suspend check, just perform the trigger
2197 // load and branch to the target.
2198 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2199 OpUnconditionalBranch(target);
2200 return;
2201 }
2202 FlushAllRegs();
2203 LIR* inst = CheckSuspendUsingLoad();
2204 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002205 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002206 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002207}
2208
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002209/* Call out to helper assembly routine that will null check obj and then lock it. */
2210void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002211 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002212 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002213 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002214}
2215
2216/* Call out to helper assembly routine that will null check obj and then unlock it. */
2217void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002218 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002219 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002220 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002221}
2222
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002223/* Generic code for generating a wide constant into a VR. */
2224void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2225 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002226 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002227 StoreValueWide(rl_dest, rl_result);
2228}
2229
Andreas Gampe48971b32014-08-06 10:09:01 -07002230void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002231 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2232 DCHECK(bb != nullptr);
2233 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002234 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002235 const uint16_t entries = table[1];
2236 // Chained cmp-and-branch.
2237 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002238 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002239 rl_src = LoadValue(rl_src, kCoreReg);
2240 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002241 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002242 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002243 // Switch to using a temp and add.
2244 break;
2245 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002246 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2247 DCHECK(successor_block_info != nullptr);
2248 int case_block_id = successor_block_info->block;
2249 DCHECK_EQ(starting_key + i, successor_block_info->key);
2250 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002251 }
2252 if (i < entries) {
2253 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2254 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2255 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002256 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002257 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2258 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2259 DCHECK(successor_block_info != nullptr);
2260 int case_block_id = successor_block_info->block;
2261 DCHECK_EQ(starting_key + i, successor_block_info->key);
2262 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002263 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2264 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002265 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2266 DCHECK(successor_block_info != nullptr);
2267 int case_block_id = successor_block_info->block;
2268 DCHECK_EQ(starting_key + i, successor_block_info->key);
2269 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002270 } else {
2271 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002272 for (; i < entries; ++i, ++succ_bb_iter) {
2273 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2274 DCHECK(successor_block_info != nullptr);
2275 int case_block_id = successor_block_info->block;
2276 DCHECK_EQ(starting_key + i, successor_block_info->key);
2277 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002278 }
2279 }
2280 }
2281}
2282
2283void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002284 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002285 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002286 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002287 }
2288
2289 const uint16_t entries = table[1];
2290 if (entries <= kSmallSwitchThreshold) {
2291 GenSmallPackedSwitch(mir, table_offset, rl_src);
2292 } else {
2293 // Use the backend-specific implementation.
2294 GenLargePackedSwitch(mir, table_offset, rl_src);
2295 }
2296}
2297
2298void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002299 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2300 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002301 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002302 const uint16_t entries = table[1];
2303 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002304 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002305 int i = 0;
2306 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2307 int case_block_id = successor_block_info->block;
2308 int key = successor_block_info->key;
2309 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2310 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002311 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002312 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002313}
2314
2315void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002316 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002317 if (cu_->verbose) {
2318 DumpSparseSwitchTable(table);
2319 }
2320
2321 const uint16_t entries = table[1];
2322 if (entries <= kSmallSwitchThreshold) {
2323 GenSmallSparseSwitch(mir, table_offset, rl_src);
2324 } else {
2325 // Use the backend-specific implementation.
2326 GenLargeSparseSwitch(mir, table_offset, rl_src);
2327 }
2328}
2329
Fred Shih37f05ef2014-07-16 18:38:08 -07002330bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2331 switch (size) {
2332 case kReference:
2333 return type == Primitive::kPrimNot;
2334 case k64:
2335 case kDouble:
2336 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2337 case k32:
2338 case kSingle:
2339 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2340 case kSignedHalf:
2341 return type == Primitive::kPrimShort;
2342 case kUnsignedHalf:
2343 return type == Primitive::kPrimChar;
2344 case kSignedByte:
2345 return type == Primitive::kPrimByte;
2346 case kUnsignedByte:
2347 return type == Primitive::kPrimBoolean;
2348 case kWord: // Intentional fallthrough.
2349 default:
2350 return false; // There are no sane types with this op size.
2351 }
2352}
2353
Brian Carlstrom7940e442013-07-12 13:46:57 -07002354} // namespace art