blob: d2b32b585baebedfff7a9ab4f5156a3806d27d6a [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 */
Andreas Gampe7e499922015-01-06 08:28:12 -080016
17#include <functional>
18
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/arm/instruction_set_features_arm.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "dex/compiler_ir.h"
21#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070022#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070024#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000026#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080027#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070028#include "mirror/object_reference.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080029#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "verifier/method_verifier.h"
31
32namespace art {
33
Andreas Gampe9c3b0892014-04-24 17:33:34 +000034// Shortcuts to repeatedly used long types.
35typedef mirror::ObjectArray<mirror::Object> ObjArray;
36typedef mirror::ObjectArray<mirror::Class> ClassArray;
37
Brian Carlstrom7940e442013-07-12 13:46:57 -070038/*
39 * This source files contains "gen" codegen routines that should
40 * be applicable to most targets. Only mid-level support utilities
41 * and "op" calls may be used here.
42 */
43
44/*
buzbeeb48819d2013-09-14 16:15:25 -070045 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 * blocks.
47 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070048void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 LIR* barrier = NewLIR0(kPseudoBarrier);
50 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070051 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010052 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070053}
54
Mingyao Yange643a172014-04-08 11:02:52 -070055void Mir2Lir::GenDivZeroException() {
56 LIR* branch = OpUnconditionalBranch(nullptr);
57 AddDivZeroCheckSlowPath(branch);
58}
59
60void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070061 LIR* branch = OpCondBranch(c_code, nullptr);
62 AddDivZeroCheckSlowPath(branch);
63}
64
Mingyao Yange643a172014-04-08 11:02:52 -070065void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
66 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070067 AddDivZeroCheckSlowPath(branch);
68}
69
70void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
71 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
72 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080073 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
74 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -070075 }
76
Mingyao Yange643a172014-04-08 11:02:52 -070077 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070078 m2l_->ResetRegPool();
79 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070080 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070081 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -070082 }
83 };
84
85 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
86}
Dave Allisonb373e092014-02-20 16:06:36 -080087
Mingyao Yang80365d92014-04-18 12:10:58 -070088void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
89 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
90 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080091 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
92 RegStorage length_in)
93 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
94 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -070095 }
96
97 void Compile() OVERRIDE {
98 m2l_->ResetRegPool();
99 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700100 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700101 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700102 }
103
104 private:
105 const RegStorage index_;
106 const RegStorage length_;
107 };
108
109 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
110 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
111}
112
113void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
114 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
115 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800116 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
117 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
118 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700119 }
120
121 void Compile() OVERRIDE {
122 m2l_->ResetRegPool();
123 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700124 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700125
Andreas Gampeccc60262014-07-04 18:02:38 -0700126 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
127 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700128
129 m2l_->OpRegCopy(arg1_32, length_);
130 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700131 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700132 }
133
134 private:
135 const int32_t index_;
136 const RegStorage length_;
137 };
138
139 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
140 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
141}
142
Mingyao Yange643a172014-04-08 11:02:52 -0700143LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
144 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
145 public:
146 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
147 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
148 }
149
150 void Compile() OVERRIDE {
151 m2l_->ResetRegPool();
152 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700153 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700154 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700155 }
156 };
157
158 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
159 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
160 return branch;
161}
162
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800164LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000165 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700166 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600168 // If null check has not been eliminated, reset redundant store tracking.
169 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
170 ResetDefTracking();
171 }
Dave Allisonb373e092014-02-20 16:06:36 -0800172 return nullptr;
173}
174
Dave Allisonf9439142014-03-27 15:10:22 -0700175/* Perform an explicit null-check on a register. */
176LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
177 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
178 return NULL;
179 }
Mingyao Yange643a172014-04-08 11:02:52 -0700180 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700181}
182
Dave Allisonb373e092014-02-20 16:06:36 -0800183void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000184 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800185 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
186 return;
187 }
Dave Allison69dfe512014-07-11 17:11:58 +0000188 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800189 MarkSafepointPC(last_lir_insn_);
190 }
191}
192
Andreas Gampe3c12c512014-06-24 18:46:29 +0000193void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000194 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000195 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
196 return;
197 }
198 MarkSafepointPCAfter(after);
199 }
200}
201
Dave Allisonb373e092014-02-20 16:06:36 -0800202void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000203 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800204 MarkSafepointPC(last_lir_insn_);
205 }
206}
207
buzbee2700f7e2014-03-07 09:46:20 -0800208void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000209 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800210 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
211 return;
212 }
213 // Force an implicit null check by performing a memory operation (load) from the given
214 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800215 RegStorage tmp = AllocTemp();
216 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700217 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800218 FreeTemp(tmp);
219 MarkSafepointPC(load);
220 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221}
222
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700224 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700226 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 switch (opcode) {
228 case Instruction::IF_EQ:
229 cond = kCondEq;
230 break;
231 case Instruction::IF_NE:
232 cond = kCondNe;
233 break;
234 case Instruction::IF_LT:
235 cond = kCondLt;
236 break;
237 case Instruction::IF_GE:
238 cond = kCondGe;
239 break;
240 case Instruction::IF_GT:
241 cond = kCondGt;
242 break;
243 case Instruction::IF_LE:
244 cond = kCondLe;
245 break;
246 default:
247 cond = static_cast<ConditionCode>(0);
248 LOG(FATAL) << "Unexpected opcode " << opcode;
249 }
250
251 // Normalize such that if either operand is constant, src2 will be constant
252 if (rl_src1.is_const) {
253 RegLocation rl_temp = rl_src1;
254 rl_src1 = rl_src2;
255 rl_src2 = rl_temp;
256 cond = FlipComparisonOrder(cond);
257 }
258
buzbee7c02e912014-10-03 13:14:17 -0700259 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 // Is this really an immediate comparison?
261 if (rl_src2.is_const) {
262 // If it's already live in a register or not easily materialized, just keep going
263 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700264 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100266 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800268 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 return;
270 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700271
272 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
273 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
274 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
275 // the 32b literal 0 for null.
276 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
277 // Use the OpCmpImmBranch and ignore the value in the register.
278 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
279 return;
280 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700282
buzbee7c02e912014-10-03 13:14:17 -0700283 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800284 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285}
286
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700287void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700289 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
290 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 switch (opcode) {
292 case Instruction::IF_EQZ:
293 cond = kCondEq;
294 break;
295 case Instruction::IF_NEZ:
296 cond = kCondNe;
297 break;
298 case Instruction::IF_LTZ:
299 cond = kCondLt;
300 break;
301 case Instruction::IF_GEZ:
302 cond = kCondGe;
303 break;
304 case Instruction::IF_GTZ:
305 cond = kCondGt;
306 break;
307 case Instruction::IF_LEZ:
308 cond = kCondLe;
309 break;
310 default:
311 cond = static_cast<ConditionCode>(0);
312 LOG(FATAL) << "Unexpected opcode " << opcode;
313 }
buzbee2700f7e2014-03-07 09:46:20 -0800314 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315}
316
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
319 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800320 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800322 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 }
buzbee2700f7e2014-03-07 09:46:20 -0800324 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 StoreValueWide(rl_dest, rl_result);
326}
327
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600328void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
329 rl_src = UpdateLocWide(rl_src);
330 rl_src = NarrowRegLoc(rl_src);
331 StoreValue(rl_dest, rl_src);
332}
333
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700335 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700336 rl_src = LoadValue(rl_src, kCoreReg);
337 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
338 OpKind op = kOpInvalid;
339 switch (opcode) {
340 case Instruction::INT_TO_BYTE:
341 op = kOp2Byte;
342 break;
343 case Instruction::INT_TO_SHORT:
344 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700346 case Instruction::INT_TO_CHAR:
347 op = kOp2Char;
348 break;
349 default:
350 LOG(ERROR) << "Bad int conversion type";
351 }
buzbee2700f7e2014-03-07 09:46:20 -0800352 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700353 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354}
355
Andreas Gampe98430592014-07-27 19:44:50 -0700356/*
357 * Let helper function take care of everything. Will call
358 * Array::AllocFromCode(type_idx, method, count);
359 * Note: AllocFromCode will handle checks for errNegativeArraySize.
360 */
361void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
362 RegLocation rl_src) {
363 FlushAllRegs(); /* Everything to home location */
364 const DexFile* dex_file = cu_->dex_file;
365 CompilerDriver* driver = cu_->compiler_driver;
366 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800367 bool is_type_initialized; // Ignored as an array does not have an initializer.
368 bool use_direct_type_ptr;
369 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700370 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800371 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700372 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
373 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800374 // The fast path.
375 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700376 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800377 CallRuntimeHelperRegRegLocationMethod(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
Andreas Gampe98430592014-07-27 19:44:50 -0700378 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800379 } else {
380 // Use the direct pointer.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800381 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
Andreas Gampe98430592014-07-27 19:44:50 -0700382 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800383 }
384 } else {
385 // The slow path.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800386 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800387 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 } else {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800389 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 }
Andreas Gampe98430592014-07-27 19:44:50 -0700391 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392}
393
394/*
395 * Similar to GenNewArray, but with post-allocation initialization.
396 * Verifier guarantees we're dealing with an array class. Current
397 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
398 * Current code also throws internal unimp if not 'L', '[' or 'I'.
399 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700400void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 int elems = info->num_arg_words;
402 int type_idx = info->index;
403 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700404 QuickEntrypointEnum target;
405 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
406 type_idx)) {
407 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700409 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800411 CallRuntimeHelperImmImmMethod(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700412 FreeTemp(TargetReg(kArg2, kNotWide));
413 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 /*
415 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
416 * return region. Because AllocFromCode placed the new array
417 * in kRet0, we'll just lock it into place. When debugger support is
418 * added, it may be necessary to additionally copy all return
419 * values to a home location in thread-local storage
420 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700421 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700422 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423
424 // TODO: use the correct component size, currently all supported types
425 // share array alignment with ints (see comment at head of function)
426 size_t component_size = sizeof(int32_t);
427
Vladimir Markobf535be2014-11-19 18:52:35 +0000428 if (elems > 5) {
429 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 /*
431 * Bit of ugliness here. We're going generate a mem copy loop
432 * on the register range, but it is possible that some regs
433 * in the range have been promoted. This is unlikely, but
434 * before generating the copy, we'll just force a flush
435 * of any regs in the source range that have been promoted to
436 * home location.
437 */
438 for (int i = 0; i < elems; i++) {
439 RegLocation loc = UpdateLoc(info->args[i]);
440 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100441 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600442 if (loc.ref) {
443 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
444 } else {
445 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
446 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 }
448 }
449 /*
450 * TUNING note: generated code here could be much improved, but
451 * this is an uncommon operation and isn't especially performance
452 * critical.
453 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700454 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700455 RegStorage r_src = AllocTempRef();
456 RegStorage r_dst = AllocTempRef();
457 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800458 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700459 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700461 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700462 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 break;
464 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700465 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700466 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 r_val = AllocTemp();
468 break;
469 case kMips:
470 r_val = AllocTemp();
471 break;
472 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
473 }
474 // Set up source pointer
475 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700476 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700478 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 mirror::Array::DataOffset(component_size).Int32Value());
480 // Set up the loop counter (known to be > 0)
481 LoadConstant(r_idx, elems - 1);
482 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800483 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100485 {
486 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
487 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
488 // NOTE: No dalvik register annotation, local optimizations will be stopped
489 // by the loop boundaries.
490 }
buzbee695d13a2014-04-19 13:32:20 -0700491 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800493 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700494 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700496 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 -mirror::Array::DataOffset(component_size).Int32Value());
498 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000499 FreeTemp(r_idx);
500 FreeTemp(r_dst);
501 FreeTemp(r_src);
502 } else {
503 DCHECK_LE(elems, 5); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 // TUNING: interleave
505 for (int i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600506 RegLocation rl_arg;
507 if (info->args[i].ref) {
508 rl_arg = LoadValue(info->args[i], kRefReg);
509 StoreRefDisp(ref_reg,
510 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
511 kNotVolatile);
512 } else {
513 rl_arg = LoadValue(info->args[i], kCoreReg);
514 Store32Disp(ref_reg,
515 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
516 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800518 if (IsTemp(rl_arg.reg)) {
519 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 }
521 }
522 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000523 if (elems != 0 && info->args[0].ref) {
524 // If there is at least one potentially non-null value, unconditionally mark the GC card.
525 for (int i = 0; i < elems; i++) {
526 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
527 UnconditionallyMarkGCCard(ref_reg);
528 break;
529 }
530 }
531 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700533 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 }
535}
536
Ian Rogers832336b2014-10-08 15:35:22 -0700537/*
538 * Array data table format:
539 * ushort ident = 0x0300 magic value
540 * ushort width width of each element in the table
541 * uint size number of elements in the table
542 * ubyte data[size*width] table of data values (may contain a single-byte
543 * padding at the end)
544 *
545 * Total size is 4+(width * size + 1)/2 16-bit code units.
546 */
547void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
548 if (kIsDebugBuild) {
549 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
550 const Instruction::ArrayDataPayload* payload =
551 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
552 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
553 }
554 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
555 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
556}
557
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800558//
559// Slow path to ensure a class is initialized for sget/sput.
560//
561class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
562 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100563 // There are up to two branches to the static field slow path, the "unresolved" when the type
564 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
565 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800566 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100567 RegStorage r_base)
568 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
569 second_branch_(unresolved != nullptr ? uninit : nullptr),
570 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800571 }
572
573 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100574 LIR* target = GenerateTargetLabel();
575 if (second_branch_ != nullptr) {
576 second_branch_->target = target;
577 }
Andreas Gampe98430592014-07-27 19:44:50 -0700578 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800579 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700580 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800581
582 m2l_->OpUnconditionalBranch(cont_);
583 }
584
585 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100586 // Second branch to the slow path, or null if there's only one branch.
587 LIR* const second_branch_;
588
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800589 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800590 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800591};
592
Fred Shih37f05ef2014-07-16 18:38:08 -0700593void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000594 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000595 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000596 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700597 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000598 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800599 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000600 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100602 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700603 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000604 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
605 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800606 if (IsTemp(rl_method.reg)) {
607 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
609 } else {
610 // Medium path, static storage base in a different class which requires checks that the other
611 // class is initialized.
612 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 // May do runtime call so everything to home locations.
615 FlushAllRegs();
616 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700617 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 LockTemp(r_method);
619 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700620 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000622 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
623 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000624 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000625 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100627 LIR* unresolved_branch = nullptr;
628 if (!field_info.IsClassInDexCache() &&
629 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
630 // Check if r_base is NULL.
631 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
632 }
633 LIR* uninit_branch = nullptr;
634 if (!field_info.IsClassInitialized() &&
635 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
636 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700637 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800638 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100639 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800640 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000641 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100642 FreeTemp(r_tmp);
643 }
644 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
645 // The slow path is invoked if the r_base is NULL or the class pointed
646 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800647 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800648 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000649 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800650
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100651 if (uninit_branch != nullptr) {
652 // Ensure load of status and store of value don't re-order.
653 // TODO: Presumably the actual value store is control-dependent on the status load,
654 // and will thus not be reordered in any case, since stores are never speculated.
655 // Does later code "know" that the class is now initialized? If so, we still
656 // need the barrier to guard later static loads.
657 GenMemBarrier(kLoadAny);
658 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 FreeTemp(r_method);
661 }
662 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700663 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
664 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100665 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100667 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700669 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000670 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
671 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100672 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700673 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000674 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700676 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000677 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800679 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 } else {
681 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700682 QuickEntrypointEnum target;
683 switch (size) {
684 case kReference:
685 target = kQuickSetObjStatic;
686 break;
687 case k64:
688 case kDouble:
689 target = kQuickSet64Static;
690 break;
691 case k32:
692 case kSingle:
693 target = kQuickSet32Static;
694 break;
695 case kSignedHalf:
696 case kUnsignedHalf:
697 target = kQuickSet16Static;
698 break;
699 case kSignedByte:
700 case kUnsignedByte:
701 target = kQuickSet8Static;
702 break;
703 case kWord: // Intentional fallthrough.
704 default:
705 LOG(FATAL) << "Can't determine entrypoint for: " << size;
706 target = kQuickSet32Static;
707 }
Andreas Gampe98430592014-07-27 19:44:50 -0700708 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 }
710}
711
Fred Shih37f05ef2014-07-16 18:38:08 -0700712void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000713 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000714 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000715 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700716
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700717 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800719 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000720 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 // Fast path, static storage base is this method's class
722 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700723 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000724 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
725 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 } else {
727 // Medium path, static storage base in a different class which requires checks that the other
728 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000729 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 // May do runtime call so everything to home locations.
731 FlushAllRegs();
732 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700733 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 LockTemp(r_method);
735 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700736 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800737 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000738 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
739 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000740 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000741 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800742 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100743 LIR* unresolved_branch = nullptr;
744 if (!field_info.IsClassInDexCache() &&
745 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
746 // Check if r_base is NULL.
747 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
748 }
749 LIR* uninit_branch = nullptr;
750 if (!field_info.IsClassInitialized() &&
751 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
752 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700753 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800754 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100755 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800756 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000757 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100758 FreeTemp(r_tmp);
759 }
760 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
761 // The slow path is invoked if the r_base is NULL or the class pointed
762 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800763 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800764 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000765 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800766
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100767 if (uninit_branch != nullptr) {
768 // Ensure load of status and load of value don't re-order.
769 GenMemBarrier(kLoadAny);
770 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 FreeTemp(r_method);
773 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800774 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700775 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100776 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800777
Vladimir Marko674744e2014-04-24 15:18:26 +0100778 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700779 if (IsRef(size)) {
780 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000781 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
782 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100783 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700784 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000785 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800786 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100787 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800788
Fred Shih37f05ef2014-07-16 18:38:08 -0700789 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 StoreValueWide(rl_dest, rl_result);
791 } else {
792 StoreValue(rl_dest, rl_result);
793 }
794 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700795 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700797 QuickEntrypointEnum target;
798 switch (type) {
799 case Primitive::kPrimNot:
800 target = kQuickGetObjStatic;
801 break;
802 case Primitive::kPrimLong:
803 case Primitive::kPrimDouble:
804 target = kQuickGet64Static;
805 break;
806 case Primitive::kPrimInt:
807 case Primitive::kPrimFloat:
808 target = kQuickGet32Static;
809 break;
810 case Primitive::kPrimShort:
811 target = kQuickGetShortStatic;
812 break;
813 case Primitive::kPrimChar:
814 target = kQuickGetCharStatic;
815 break;
816 case Primitive::kPrimByte:
817 target = kQuickGetByteStatic;
818 break;
819 case Primitive::kPrimBoolean:
820 target = kQuickGetBooleanStatic;
821 break;
822 case Primitive::kPrimVoid: // Intentional fallthrough.
823 default:
824 LOG(FATAL) << "Can't determine entrypoint for: " << type;
825 target = kQuickGet32Static;
826 }
Andreas Gampe98430592014-07-27 19:44:50 -0700827 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
828
Douglas Leung2db3e262014-06-25 16:02:55 -0700829 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700830 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700831 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValueWide(rl_dest, rl_result);
833 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700834 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 StoreValue(rl_dest, rl_result);
836 }
837 }
838}
839
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800840// Generate code for all slow paths.
841void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700842 // We should check slow_paths_.Size() every time, because a new slow path
843 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100844 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800845 slowpath->Compile();
846 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100847 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800848}
849
Fred Shih37f05ef2014-07-16 18:38:08 -0700850void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
851 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000852 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000853 DCHECK_EQ(IGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000854 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700855 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700856 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700857 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000858 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700859 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100860 GenNullCheck(rl_obj.reg, opt_flags);
861 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
862 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000863 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700864 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000865 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
866 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100867 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700868 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000869 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100870 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000871 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700872 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 StoreValueWide(rl_dest, rl_result);
874 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 StoreValue(rl_dest, rl_result);
876 }
877 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700878 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
879 QuickEntrypointEnum target;
880 switch (type) {
881 case Primitive::kPrimNot:
882 target = kQuickGetObjInstance;
883 break;
884 case Primitive::kPrimLong:
885 case Primitive::kPrimDouble:
886 target = kQuickGet64Instance;
887 break;
888 case Primitive::kPrimFloat:
889 case Primitive::kPrimInt:
890 target = kQuickGet32Instance;
891 break;
892 case Primitive::kPrimShort:
893 target = kQuickGetShortInstance;
894 break;
895 case Primitive::kPrimChar:
896 target = kQuickGetCharInstance;
897 break;
898 case Primitive::kPrimByte:
899 target = kQuickGetByteInstance;
900 break;
901 case Primitive::kPrimBoolean:
902 target = kQuickGetBooleanInstance;
903 break;
904 case Primitive::kPrimVoid: // Intentional fallthrough.
905 default:
906 LOG(FATAL) << "Can't determine entrypoint for: " << type;
907 target = kQuickGet32Instance;
908 }
Andreas Gampe98430592014-07-27 19:44:50 -0700909 // Second argument of pGetXXInstance is always a reference.
910 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
911 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
912
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700913 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700914 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700915 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 StoreValueWide(rl_dest, rl_result);
917 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700918 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 StoreValue(rl_dest, rl_result);
920 }
921 }
922}
923
Vladimir Markobe0e5462014-02-26 11:24:15 +0000924void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700925 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000926 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000927 DCHECK_EQ(IPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000928 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700929 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700930 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700931 // Dex code never writes to the class field.
932 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
933 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700934 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700935 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100936 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 } else {
938 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100939 }
940 GenNullCheck(rl_obj.reg, opt_flags);
941 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000942 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700943 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000944 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000945 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100946 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000947 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
948 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100949 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000950 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700951 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000952 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 }
954 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700955 QuickEntrypointEnum target;
956 switch (size) {
957 case kReference:
958 target = kQuickSetObjInstance;
959 break;
960 case k64:
961 case kDouble:
962 target = kQuickSet64Instance;
963 break;
964 case k32:
965 case kSingle:
966 target = kQuickSet32Instance;
967 break;
968 case kSignedHalf:
969 case kUnsignedHalf:
970 target = kQuickSet16Instance;
971 break;
972 case kSignedByte:
973 case kUnsignedByte:
974 target = kQuickSet8Instance;
975 break;
976 case kWord: // Intentional fallthrough.
977 default:
978 LOG(FATAL) << "Can't determine entrypoint for: " << size;
979 target = kQuickSet32Instance;
980 }
Andreas Gampe98430592014-07-27 19:44:50 -0700981 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
982 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 }
984}
985
Ian Rogersa9a82542013-10-04 11:17:26 -0700986void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
987 RegLocation rl_src) {
988 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
989 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
990 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700991 QuickEntrypointEnum target = needs_range_check
992 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
993 : kQuickAputObjectWithBoundCheck)
994 : kQuickAputObject;
995 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700996}
997
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700998void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001000 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -07001001 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001003 *cu_->dex_file,
1004 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 // Call out to helper which resolves type and verifies access.
1006 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -07001007 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001008 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 StoreValue(rl_dest, rl_result);
1010 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001011 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 // We're don't need access checks, load type from dex cache
1013 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001014 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001015 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001016 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001017 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
1019 type_idx) || SLOW_TYPE_PATH) {
1020 // Slow path, at runtime test if type is null and if so initialize
1021 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001022 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 LIR* cont = NewLIR0(kPseudoTargetLabel);
1024
1025 // Object to generate the slow path for class resolution.
1026 class SlowPath : public LIRSlowPath {
1027 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001028 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1029 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
1030 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1031 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001032 }
1033
1034 void Compile() {
1035 GenerateTargetLabel();
1036
Andreas Gampe98430592014-07-27 19:44:50 -07001037 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001038 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001039 m2l_->OpUnconditionalBranch(cont_);
1040 }
1041
1042 private:
1043 const int type_idx_;
1044 const RegLocation rl_method_;
1045 const RegLocation rl_result_;
1046 };
1047
1048 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001049 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001050
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001052 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 // Fast path, we're done - just store result
1054 StoreValue(rl_dest, rl_result);
1055 }
1056 }
1057}
1058
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001059void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001061 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1062 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1064 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1065 // slow path, resolve string if not in dex cache
1066 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001067 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001068
1069 // If the Method* is already in a register, we can save a copy.
1070 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001071 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001072 if (rl_method.location == kLocPhysReg) {
1073 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001074 DCHECK(!IsTemp(rl_method.reg));
1075 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001076 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001077 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001078 LoadCurrMethodDirect(r_method);
1079 }
Mathieu Chartiereace4582014-11-24 18:29:54 -08001080 // Method to declaring class.
1081 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1082 TargetReg(kArg0, kRef), kNotVolatile);
1083 // Declaring class to dex cache strings.
1084 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Class::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001085 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001086
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001088 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1089 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001090 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001091
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001092 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001093 // Object to generate the slow path for string resolution.
1094 class SlowPath : public LIRSlowPath {
1095 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001096 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1097 int32_t string_idx_in) :
1098 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1099 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001100 }
1101
1102 void Compile() {
1103 GenerateTargetLabel();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001104 m2l_->CallRuntimeHelperImmReg(kQuickResolveString, string_idx_, r_method_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001105 m2l_->OpUnconditionalBranch(cont_);
1106 }
1107
1108 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001109 const RegStorage r_method_;
1110 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001111 };
1112
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001113 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001115
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001117 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 } else {
1119 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001120 RegStorage res_reg = AllocTempRef();
1121 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001122 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1123 kNotVolatile);
1124 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001125 kNotVolatile);
1126 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 StoreValue(rl_dest, rl_result);
1128 }
1129}
1130
Andreas Gampe98430592014-07-27 19:44:50 -07001131/*
1132 * Let helper function take care of everything. Will
1133 * call Class::NewInstanceFromCode(type_idx, method);
1134 */
1135void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1136 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 // alloc will always check for resolution, do we also need to verify
1138 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001139 const DexFile* dex_file = cu_->dex_file;
1140 CompilerDriver* driver = cu_->compiler_driver;
1141 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001142 bool is_type_initialized;
1143 bool use_direct_type_ptr;
1144 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001145 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001146 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001147 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1148 &direct_type_ptr, &is_finalizable) &&
1149 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001150 // The fast path.
1151 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001152 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001153 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001154 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001155 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001156 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001157 }
1158 } else {
1159 // Use the direct pointer.
1160 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001161 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001162 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001163 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001164 }
1165 }
1166 } else {
1167 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001168 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001169 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001171 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 }
Andreas Gampe98430592014-07-27 19:44:50 -07001173 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174}
1175
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001176void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001178 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179}
1180
1181// For final classes there are no sub-classes to check and so we can answer the instance-of
1182// question with simple comparisons.
1183void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1184 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001185 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001186 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001187
buzbeea0cd2d72014-06-01 09:33:49 -07001188 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001190 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001191 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001193 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 }
1195 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001196 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197
buzbeea0cd2d72014-06-01 09:33:49 -07001198 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1199 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200
1201 LoadCurrMethodDirect(check_class);
1202 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001203 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1204 kNotVolatile);
1205 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1206 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 } else {
buzbee695d13a2014-04-19 13:32:20 -07001208 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001209 check_class, kNotVolatile);
1210 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1211 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001212 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001213 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 }
1215
buzbee695d13a2014-04-19 13:32:20 -07001216 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 if (cu_->instruction_set == kThumb2) {
1218 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001219 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001221 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001223 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 }
1225 LIR* target = NewLIR0(kPseudoTargetLabel);
1226 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 FreeTemp(object_class);
1228 FreeTemp(check_class);
1229 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001230 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 FreeTemp(result_reg);
1232 }
1233 StoreValue(rl_dest, rl_result);
1234}
1235
1236void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1237 bool type_known_abstract, bool use_declaring_class,
1238 bool can_assume_type_is_in_dex_cache,
1239 uint32_t type_idx, RegLocation rl_dest,
1240 RegLocation rl_src) {
1241 FlushAllRegs();
1242 // May generate a call - use explicit registers
1243 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001244 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001245 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001246 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001247 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1248 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 if (needs_access_check) {
1250 // Check we have access to type_idx and if not throw IllegalAccessError,
1251 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001252 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001253 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1254 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001256 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001257 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001258 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001260 if (can_assume_type_is_in_dex_cache) {
1261 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001262 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001263 }
1264
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001266 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001267 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001268 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001269 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001271 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1272 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1273
1274 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001275 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001276
1277 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1278 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001279 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1280 RegLocation rl_src_in)
1281 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1282 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001283 }
1284
1285 void Compile() OVERRIDE {
1286 GenerateTargetLabel();
1287
Andreas Gampe98430592014-07-27 19:44:50 -07001288 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001289 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1290 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001291 m2l_->OpUnconditionalBranch(cont_);
1292 }
1293
1294 private:
1295 uint32_t type_idx_;
1296 RegLocation rl_src_;
1297 };
1298
1299 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1300 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 }
1302 }
1303 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001304 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001305 if (!IsSameReg(rl_result.reg, ref_reg)) {
1306 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001307 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001309 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310
1311 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001312 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001314 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1315 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1317 LIR* branchover = NULL;
1318 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001319 // rl_result == ref == class.
1320 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001321 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 } else {
1323 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001324 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001325 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 if (!type_known_abstract) {
1327 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001328 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001329 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001330 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001332 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001334 if (it != nullptr) {
1335 OpEndIT(it);
1336 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 FreeTemp(r_tgt);
1338 } else {
1339 if (!type_known_abstract) {
1340 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001341 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001342 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001344
Serguei Katkov9ee45192014-07-17 14:39:03 +07001345 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001346 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 }
1348 }
1349 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001350 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 /* branch targets here */
1352 LIR* target = NewLIR0(kPseudoTargetLabel);
1353 StoreValue(rl_dest, rl_result);
1354 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001355 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 branchover->target = target;
1357 }
1358}
1359
1360void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1361 bool type_known_final, type_known_abstract, use_declaring_class;
1362 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1363 *cu_->dex_file,
1364 type_idx,
1365 &type_known_final,
1366 &type_known_abstract,
1367 &use_declaring_class);
1368 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1369 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1370
1371 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1372 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1373 } else {
1374 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1375 use_declaring_class, can_assume_type_is_in_dex_cache,
1376 type_idx, rl_dest, rl_src);
1377 }
1378}
1379
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001380void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 bool type_known_final, type_known_abstract, use_declaring_class;
1382 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1383 *cu_->dex_file,
1384 type_idx,
1385 &type_known_final,
1386 &type_known_abstract,
1387 &use_declaring_class);
1388 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1389 // of the exception throw path.
1390 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001391 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 // Verifier type analysis proved this check cast would never cause an exception.
1393 return;
1394 }
1395 FlushAllRegs();
1396 // May generate a call - use explicit registers
1397 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001398 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001399 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001400 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 if (needs_access_check) {
1402 // Check we have access to type_idx and if not throw IllegalAccessError,
1403 // returns Class* in kRet0
1404 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001405 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001406 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001408 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001409 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 } else {
1411 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001412 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001413 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001414 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001415 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1417 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001418 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1419 LIR* cont = NewLIR0(kPseudoTargetLabel);
1420
1421 // Slow path to initialize the type. Executed if the type is NULL.
1422 class SlowPath : public LIRSlowPath {
1423 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001424 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1425 const RegStorage class_reg_in) :
1426 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1427 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001428 }
1429
1430 void Compile() {
1431 GenerateTargetLabel();
1432
1433 // Call out to helper, which will return resolved type in kArg0
1434 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001435 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1436 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001437 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001438 m2l_->OpUnconditionalBranch(cont_);
1439 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001440
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001441 public:
1442 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001443 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001444 };
1445
buzbee2700f7e2014-03-07 09:46:20 -08001446 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 }
1448 }
1449 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001450 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001451
1452 // Slow path for the case where the classes are not equal. In this case we need
1453 // to call a helper function to do the check.
1454 class SlowPath : public LIRSlowPath {
1455 public:
1456 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1457 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1458 }
1459
1460 void Compile() {
1461 GenerateTargetLabel();
1462
1463 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001464 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1465 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001466 }
Andreas Gampe98430592014-07-27 19:44:50 -07001467 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1468 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001469 m2l_->OpUnconditionalBranch(cont_);
1470 }
1471
1472 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001473 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001474 };
1475
1476 if (type_known_abstract) {
1477 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001478 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001479 LIR* cont = NewLIR0(kPseudoTargetLabel);
1480 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1481 } else {
1482 // Harder, more common case. We need to generate a forward branch over the load
1483 // if the target is null. If it's non-null we perform the load and branch to the
1484 // slow path if the classes are not equal.
1485
1486 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001487 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001488 /* load object->klass_ */
1489 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001490 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1491 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001492
Andreas Gampeccc60262014-07-04 18:02:38 -07001493 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001494 LIR* cont = NewLIR0(kPseudoTargetLabel);
1495
1496 // Add the slow path that will not perform load since this is already done.
1497 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1498
1499 // Set the null check to branch to the continuation.
1500 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 }
1502}
1503
1504void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001505 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 RegLocation rl_result;
1507 if (cu_->instruction_set == kThumb2) {
1508 /*
1509 * NOTE: This is the one place in the code in which we might have
1510 * as many as six live temporary registers. There are 5 in the normal
1511 * set for Arm. Until we have spill capabilities, temporarily add
1512 * lr to the temp set. It is safe to do this locally, but note that
1513 * lr is used explicitly elsewhere in the code generator and cannot
1514 * normally be used as a general temp register.
1515 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001516 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1517 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 }
1519 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1520 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1521 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1522 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001523 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1524 RegStorage t_reg = AllocTemp();
1525 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1526 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1527 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 FreeTemp(t_reg);
1529 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001530 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1531 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 }
1533 /*
1534 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1535 * following StoreValueWide might need to allocate a temp register.
1536 * To further work around the lack of a spill capability, explicitly
1537 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1538 * Remove when spill is functional.
1539 */
1540 FreeRegLocTemps(rl_result, rl_src1);
1541 FreeRegLocTemps(rl_result, rl_src2);
1542 StoreValueWide(rl_dest, rl_result);
1543 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001544 Clobber(TargetReg(kLr, kNotWide));
1545 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 }
1547}
1548
Andreas Gampe98430592014-07-27 19:44:50 -07001549void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1550 RegLocation rl_src1, RegLocation rl_shift) {
1551 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 switch (opcode) {
1553 case Instruction::SHL_LONG:
1554 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001555 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 break;
1557 case Instruction::SHR_LONG:
1558 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001559 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 break;
1561 case Instruction::USHR_LONG:
1562 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001563 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 break;
1565 default:
1566 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001567 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 }
Andreas Gampe98430592014-07-27 19:44:50 -07001569 FlushAllRegs(); /* Send everything to home location */
1570 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001571 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 StoreValueWide(rl_dest, rl_result);
1573}
1574
1575
1576void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001577 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001578 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 OpKind op = kOpBkpt;
1580 bool is_div_rem = false;
1581 bool check_zero = false;
1582 bool unary = false;
1583 RegLocation rl_result;
1584 bool shift_op = false;
1585 switch (opcode) {
1586 case Instruction::NEG_INT:
1587 op = kOpNeg;
1588 unary = true;
1589 break;
1590 case Instruction::NOT_INT:
1591 op = kOpMvn;
1592 unary = true;
1593 break;
1594 case Instruction::ADD_INT:
1595 case Instruction::ADD_INT_2ADDR:
1596 op = kOpAdd;
1597 break;
1598 case Instruction::SUB_INT:
1599 case Instruction::SUB_INT_2ADDR:
1600 op = kOpSub;
1601 break;
1602 case Instruction::MUL_INT:
1603 case Instruction::MUL_INT_2ADDR:
1604 op = kOpMul;
1605 break;
1606 case Instruction::DIV_INT:
1607 case Instruction::DIV_INT_2ADDR:
1608 check_zero = true;
1609 op = kOpDiv;
1610 is_div_rem = true;
1611 break;
1612 /* NOTE: returns in kArg1 */
1613 case Instruction::REM_INT:
1614 case Instruction::REM_INT_2ADDR:
1615 check_zero = true;
1616 op = kOpRem;
1617 is_div_rem = true;
1618 break;
1619 case Instruction::AND_INT:
1620 case Instruction::AND_INT_2ADDR:
1621 op = kOpAnd;
1622 break;
1623 case Instruction::OR_INT:
1624 case Instruction::OR_INT_2ADDR:
1625 op = kOpOr;
1626 break;
1627 case Instruction::XOR_INT:
1628 case Instruction::XOR_INT_2ADDR:
1629 op = kOpXor;
1630 break;
1631 case Instruction::SHL_INT:
1632 case Instruction::SHL_INT_2ADDR:
1633 shift_op = true;
1634 op = kOpLsl;
1635 break;
1636 case Instruction::SHR_INT:
1637 case Instruction::SHR_INT_2ADDR:
1638 shift_op = true;
1639 op = kOpAsr;
1640 break;
1641 case Instruction::USHR_INT:
1642 case Instruction::USHR_INT_2ADDR:
1643 shift_op = true;
1644 op = kOpLsr;
1645 break;
1646 default:
1647 LOG(FATAL) << "Invalid word arith op: " << opcode;
1648 }
1649 if (!is_div_rem) {
1650 if (unary) {
1651 rl_src1 = LoadValue(rl_src1, kCoreReg);
1652 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001653 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001655 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001656 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001657 RegStorage t_reg = AllocTemp();
1658 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 rl_src1 = LoadValue(rl_src1, kCoreReg);
1660 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001661 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 FreeTemp(t_reg);
1663 } else {
1664 rl_src1 = LoadValue(rl_src1, kCoreReg);
1665 rl_src2 = LoadValue(rl_src2, kCoreReg);
1666 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001667 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 }
1669 }
1670 StoreValue(rl_dest, rl_result);
1671 } else {
Dave Allison70202782013-10-22 17:52:19 -07001672 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 +01001673 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 rl_src1 = LoadValue(rl_src1, kCoreReg);
1675 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001676 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001677 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 }
buzbee2700f7e2014-03-07 09:46:20 -08001679 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001680 done = true;
1681 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001682 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1683 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001684 // Use ARM SDIV instruction for division. For remainder we also need to
1685 // calculate using a MUL and subtract.
1686 rl_src1 = LoadValue(rl_src1, kCoreReg);
1687 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001688 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001689 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001690 }
buzbee2700f7e2014-03-07 09:46:20 -08001691 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001692 done = true;
1693 }
1694 }
1695
1696 // If we haven't already generated the code use the callout function.
1697 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001699 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001700 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001701 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001702 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001703 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 }
Dave Allison70202782013-10-22 17:52:19 -07001705 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001706 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001708 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 else
1710 rl_result = GetReturnAlt();
1711 }
1712 StoreValue(rl_dest, rl_result);
1713 }
1714}
1715
1716/*
1717 * The following are the first-level codegen routines that analyze the format
1718 * of each bytecode then either dispatch special purpose codegen routines
1719 * or produce corresponding Thumb instructions directly.
1720 */
1721
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001723static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 x &= x - 1;
1725 return (x & (x - 1)) == 0;
1726}
1727
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1729// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001730bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001731 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1733 return false;
1734 }
1735 // No divide instruction for Arm, so check for more special cases
1736 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001737 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001738 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001739 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001740 if (k >= 30) {
1741 // Avoid special cases.
1742 return false;
1743 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 rl_src = LoadValue(rl_src, kCoreReg);
1745 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001746 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001747 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 if (lit == 2) {
1749 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001750 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1751 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1752 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001754 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001756 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1757 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 }
1759 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001760 RegStorage t_reg1 = AllocTemp();
1761 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001763 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1764 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001766 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001768 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001770 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001772 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001773 }
1774 }
1775 StoreValue(rl_dest, rl_result);
1776 return true;
1777}
1778
1779// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1780// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001781bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001782 if (lit < 0) {
1783 return false;
1784 }
1785 if (lit == 0) {
1786 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1787 LoadConstant(rl_result.reg, 0);
1788 StoreValue(rl_dest, rl_result);
1789 return true;
1790 }
1791 if (lit == 1) {
1792 rl_src = LoadValue(rl_src, kCoreReg);
1793 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1794 OpRegCopy(rl_result.reg, rl_src.reg);
1795 StoreValue(rl_dest, rl_result);
1796 return true;
1797 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001798 // There is RegRegRegShift on Arm, so check for more special cases
1799 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001800 return EasyMultiply(rl_src, rl_dest, lit);
1801 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 // Can we simplify this multiplication?
1803 bool power_of_two = false;
1804 bool pop_count_le2 = false;
1805 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001806 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001807 power_of_two = true;
1808 } else if (IsPopCountLE2(lit)) {
1809 pop_count_le2 = true;
1810 } else if (IsPowerOfTwo(lit + 1)) {
1811 power_of_two_minus_one = true;
1812 } else {
1813 return false;
1814 }
1815 rl_src = LoadValue(rl_src, kCoreReg);
1816 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1817 if (power_of_two) {
1818 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001819 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 } else if (pop_count_le2) {
1821 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001822 int first_bit = CTZ(lit);
1823 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001824 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1825 } else {
1826 // Reverse subtract: (src << (shift + 1)) - src.
1827 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001828 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001829 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001830 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001831 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 }
1833 StoreValue(rl_dest, rl_result);
1834 return true;
1835}
1836
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001837// Returns true if it generates instructions.
1838bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1839 RegLocation rl_src2) {
1840 if (!rl_src2.is_const ||
1841 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1842 return false;
1843 }
1844
1845 if (!rl_src2.wide) {
1846 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1847 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1848 // Generate multiply by reciprocal instead of div.
1849 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1850 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1851 return true;
1852 }
1853 } else {
1854 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1855 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1856 // Generate multiply by reciprocal instead of div.
1857 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1858 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1859 return true;
1860 }
1861 }
1862 return false;
1863}
1864
Brian Carlstrom7940e442013-07-12 13:46:57 -07001865void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001866 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 RegLocation rl_result;
1868 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1869 int shift_op = false;
1870 bool is_div = false;
1871
1872 switch (opcode) {
1873 case Instruction::RSUB_INT_LIT8:
1874 case Instruction::RSUB_INT: {
1875 rl_src = LoadValue(rl_src, kCoreReg);
1876 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1877 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001878 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001879 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001880 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1881 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001882 }
1883 StoreValue(rl_dest, rl_result);
1884 return;
1885 }
1886
1887 case Instruction::SUB_INT:
1888 case Instruction::SUB_INT_2ADDR:
1889 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001890 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001891 case Instruction::ADD_INT:
1892 case Instruction::ADD_INT_2ADDR:
1893 case Instruction::ADD_INT_LIT8:
1894 case Instruction::ADD_INT_LIT16:
1895 op = kOpAdd;
1896 break;
1897 case Instruction::MUL_INT:
1898 case Instruction::MUL_INT_2ADDR:
1899 case Instruction::MUL_INT_LIT8:
1900 case Instruction::MUL_INT_LIT16: {
1901 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1902 return;
1903 }
1904 op = kOpMul;
1905 break;
1906 }
1907 case Instruction::AND_INT:
1908 case Instruction::AND_INT_2ADDR:
1909 case Instruction::AND_INT_LIT8:
1910 case Instruction::AND_INT_LIT16:
1911 op = kOpAnd;
1912 break;
1913 case Instruction::OR_INT:
1914 case Instruction::OR_INT_2ADDR:
1915 case Instruction::OR_INT_LIT8:
1916 case Instruction::OR_INT_LIT16:
1917 op = kOpOr;
1918 break;
1919 case Instruction::XOR_INT:
1920 case Instruction::XOR_INT_2ADDR:
1921 case Instruction::XOR_INT_LIT8:
1922 case Instruction::XOR_INT_LIT16:
1923 op = kOpXor;
1924 break;
1925 case Instruction::SHL_INT_LIT8:
1926 case Instruction::SHL_INT:
1927 case Instruction::SHL_INT_2ADDR:
1928 lit &= 31;
1929 shift_op = true;
1930 op = kOpLsl;
1931 break;
1932 case Instruction::SHR_INT_LIT8:
1933 case Instruction::SHR_INT:
1934 case Instruction::SHR_INT_2ADDR:
1935 lit &= 31;
1936 shift_op = true;
1937 op = kOpAsr;
1938 break;
1939 case Instruction::USHR_INT_LIT8:
1940 case Instruction::USHR_INT:
1941 case Instruction::USHR_INT_2ADDR:
1942 lit &= 31;
1943 shift_op = true;
1944 op = kOpLsr;
1945 break;
1946
1947 case Instruction::DIV_INT:
1948 case Instruction::DIV_INT_2ADDR:
1949 case Instruction::DIV_INT_LIT8:
1950 case Instruction::DIV_INT_LIT16:
1951 case Instruction::REM_INT:
1952 case Instruction::REM_INT_2ADDR:
1953 case Instruction::REM_INT_LIT8:
1954 case Instruction::REM_INT_LIT16: {
1955 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001956 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001957 return;
1958 }
buzbee11b63d12013-08-27 07:34:17 -07001959 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001961 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001962 (opcode == Instruction::DIV_INT_LIT16)) {
1963 is_div = true;
1964 } else {
1965 is_div = false;
1966 }
buzbee11b63d12013-08-27 07:34:17 -07001967 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1968 return;
1969 }
Dave Allison70202782013-10-22 17:52:19 -07001970
1971 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001972 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001973 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001974 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001975 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001976 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001977 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1978 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001979 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001980 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1981 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001982 // Use ARM SDIV instruction for division. For remainder we also need to
1983 // calculate using a MUL and subtract.
1984 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001985 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001986 done = true;
1987 }
1988 }
1989
1990 if (!done) {
1991 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001992 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1993 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001994 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001996 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 else
1998 rl_result = GetReturnAlt();
1999 }
2000 StoreValue(rl_dest, rl_result);
2001 return;
2002 }
2003 default:
2004 LOG(FATAL) << "Unexpected opcode " << opcode;
2005 }
2006 rl_src = LoadValue(rl_src, kCoreReg);
2007 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07002008 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08002010 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 } else {
buzbee2700f7e2014-03-07 09:46:20 -08002012 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013 }
2014 StoreValue(rl_dest, rl_result);
2015}
2016
Andreas Gampe98430592014-07-27 19:44:50 -07002017void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002018 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002019 RegLocation rl_result;
2020 OpKind first_op = kOpBkpt;
2021 OpKind second_op = kOpBkpt;
2022 bool call_out = false;
2023 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002024 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2025 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026
2027 switch (opcode) {
2028 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002029 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2030 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002031 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002032 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002033 RegStorage t_reg = AllocTemp();
2034 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2035 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2036 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2037 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002039 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2040 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 }
Andreas Gampe98430592014-07-27 19:44:50 -07002042 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 return;
2044 case Instruction::ADD_LONG:
2045 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 first_op = kOpAdd;
2047 second_op = kOpAdc;
2048 break;
2049 case Instruction::SUB_LONG:
2050 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 first_op = kOpSub;
2052 second_op = kOpSbc;
2053 break;
2054 case Instruction::MUL_LONG:
2055 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002056 call_out = true;
2057 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2058 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 break;
2060 case Instruction::DIV_LONG:
2061 case Instruction::DIV_LONG_2ADDR:
2062 call_out = true;
2063 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002064 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2065 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002066 break;
2067 case Instruction::REM_LONG:
2068 case Instruction::REM_LONG_2ADDR:
2069 call_out = true;
2070 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002071 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002073 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2074 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002075 break;
2076 case Instruction::AND_LONG_2ADDR:
2077 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 first_op = kOpAnd;
2079 second_op = kOpAnd;
2080 break;
2081 case Instruction::OR_LONG:
2082 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 first_op = kOpOr;
2084 second_op = kOpOr;
2085 break;
2086 case Instruction::XOR_LONG:
2087 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 first_op = kOpXor;
2089 second_op = kOpXor;
2090 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 default:
2092 LOG(FATAL) << "Invalid long arith op";
2093 }
2094 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002095 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002097 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002099 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2100 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2101 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2102 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002103 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2104 GenDivZeroCheckWide(r_tmp2);
2105 }
Andreas Gampe98430592014-07-27 19:44:50 -07002106 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002107 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002108 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002109 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002110 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002111 }
2112 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002113 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2114 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115 else
Andreas Gampe98430592014-07-27 19:44:50 -07002116 rl_result = GetReturnWideAlt();
2117 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002118 }
2119}
2120
Mark Mendelle87f9b52014-04-30 14:13:18 -04002121void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2122 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2123 LoadConstantNoClobber(rl_result.reg, value);
2124 StoreValue(rl_dest, rl_result);
2125 if (value == 0) {
2126 Workaround7250540(rl_dest, rl_result.reg);
2127 }
2128}
2129
Andreas Gampe98430592014-07-27 19:44:50 -07002130void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2131 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002132 /*
2133 * Don't optimize the register usage since it calls out to support
2134 * functions
2135 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002136
Brian Carlstrom7940e442013-07-12 13:46:57 -07002137 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002138 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 if (rl_dest.wide) {
2140 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002141 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002142 StoreValueWide(rl_dest, rl_result);
2143 } else {
2144 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002145 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002146 StoreValue(rl_dest, rl_result);
2147 }
2148}
2149
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002150class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2151 public:
2152 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2153 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2154 }
2155
2156 void Compile() OVERRIDE {
2157 m2l_->ResetRegPool();
2158 m2l_->ResetDefTracking();
2159 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002160 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002161 if (cont_ != nullptr) {
2162 m2l_->OpUnconditionalBranch(cont_);
2163 }
2164 }
2165};
2166
Brian Carlstrom7940e442013-07-12 13:46:57 -07002167/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002168void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002169 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2170 return;
2171 }
Dave Allison69dfe512014-07-11 17:11:58 +00002172 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002173 FlushAllRegs();
2174 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002175 LIR* cont = NewLIR0(kPseudoTargetLabel);
2176 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002177 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002178 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) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002186 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2187 OpUnconditionalBranch(target);
2188 return;
2189 }
Dave Allison69dfe512014-07-11 17:11:58 +00002190 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002191 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.
Dave Allisonb373e092014-02-20 16:06:36 -08002198 FlushAllRegs();
2199 LIR* inst = CheckSuspendUsingLoad();
2200 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002201 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002203}
2204
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002205/* Call out to helper assembly routine that will null check obj and then lock it. */
2206void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002207 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002208 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002209 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002210}
2211
2212/* Call out to helper assembly routine that will null check obj and then unlock it. */
2213void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002214 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002215 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002216 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002217}
2218
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002219/* Generic code for generating a wide constant into a VR. */
2220void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2221 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002222 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002223 StoreValueWide(rl_dest, rl_result);
2224}
2225
Andreas Gampe48971b32014-08-06 10:09:01 -07002226void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002227 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2228 DCHECK(bb != nullptr);
2229 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002230 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002231 const uint16_t entries = table[1];
2232 // Chained cmp-and-branch.
2233 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002234 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002235 rl_src = LoadValue(rl_src, kCoreReg);
2236 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002237 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002238 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002239 // Switch to using a temp and add.
2240 break;
2241 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002242 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2243 DCHECK(successor_block_info != nullptr);
2244 int case_block_id = successor_block_info->block;
2245 DCHECK_EQ(starting_key + i, successor_block_info->key);
2246 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002247 }
2248 if (i < entries) {
2249 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2250 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2251 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002252 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002253 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2254 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2255 DCHECK(successor_block_info != nullptr);
2256 int case_block_id = successor_block_info->block;
2257 DCHECK_EQ(starting_key + i, successor_block_info->key);
2258 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002259 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2260 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002261 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2262 DCHECK(successor_block_info != nullptr);
2263 int case_block_id = successor_block_info->block;
2264 DCHECK_EQ(starting_key + i, successor_block_info->key);
2265 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002266 } else {
2267 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002268 for (; i < entries; ++i, ++succ_bb_iter) {
2269 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2270 DCHECK(successor_block_info != nullptr);
2271 int case_block_id = successor_block_info->block;
2272 DCHECK_EQ(starting_key + i, successor_block_info->key);
2273 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002274 }
2275 }
2276 }
2277}
2278
2279void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002280 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002281 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002282 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002283 }
2284
2285 const uint16_t entries = table[1];
2286 if (entries <= kSmallSwitchThreshold) {
2287 GenSmallPackedSwitch(mir, table_offset, rl_src);
2288 } else {
2289 // Use the backend-specific implementation.
2290 GenLargePackedSwitch(mir, table_offset, rl_src);
2291 }
2292}
2293
2294void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002295 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2296 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002297 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002298 const uint16_t entries = table[1];
2299 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002300 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002301 int i = 0;
2302 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2303 int case_block_id = successor_block_info->block;
2304 int key = successor_block_info->key;
2305 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2306 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002307 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002308 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002309}
2310
2311void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002312 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002313 if (cu_->verbose) {
2314 DumpSparseSwitchTable(table);
2315 }
2316
2317 const uint16_t entries = table[1];
2318 if (entries <= kSmallSwitchThreshold) {
2319 GenSmallSparseSwitch(mir, table_offset, rl_src);
2320 } else {
2321 // Use the backend-specific implementation.
2322 GenLargeSparseSwitch(mir, table_offset, rl_src);
2323 }
2324}
2325
Fred Shih37f05ef2014-07-16 18:38:08 -07002326bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2327 switch (size) {
2328 case kReference:
2329 return type == Primitive::kPrimNot;
2330 case k64:
2331 case kDouble:
2332 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2333 case k32:
2334 case kSingle:
2335 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2336 case kSignedHalf:
2337 return type == Primitive::kPrimShort;
2338 case kUnsignedHalf:
2339 return type == Primitive::kPrimChar;
2340 case kSignedByte:
2341 return type == Primitive::kPrimByte;
2342 case kUnsignedByte:
2343 return type == Primitive::kPrimBoolean;
2344 case kWord: // Intentional fallthrough.
2345 default:
2346 return false; // There are no sane types with this op size.
2347 }
2348}
2349
Brian Carlstrom7940e442013-07-12 13:46:57 -07002350} // namespace art