blob: b80fd749fdd5fb5a5ae3a256dc988fc74c341f80 [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
Andreas Gampe0b9203e2015-01-22 20:39:27 -080017#include "mir_to_lir-inl.h"
18
Andreas Gampe7e499922015-01-06 08:28:12 -080019#include <functional>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/arm/instruction_set_features_arm.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/compiler_ir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080024#include "dex/mir_graph.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070025#include "dex/quick/arm/arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080026#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070027#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000029#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080030#include "mirror/object-inl.h"
Andreas Gampeaa910d52014-07-30 18:59:05 -070031#include "mirror/object_reference.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080032#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "verifier/method_verifier.h"
34
35namespace art {
36
Andreas Gampe9c3b0892014-04-24 17:33:34 +000037// Shortcuts to repeatedly used long types.
38typedef mirror::ObjectArray<mirror::Object> ObjArray;
39typedef mirror::ObjectArray<mirror::Class> ClassArray;
40
Brian Carlstrom7940e442013-07-12 13:46:57 -070041/*
42 * This source files contains "gen" codegen routines that should
43 * be applicable to most targets. Only mid-level support utilities
44 * and "op" calls may be used here.
45 */
46
Andreas Gampe0b9203e2015-01-22 20:39:27 -080047ALWAYS_INLINE static inline bool ForceSlowFieldPath(CompilationUnit* cu) {
48 return (cu->enable_debug & (1 << kDebugSlowFieldPath)) != 0;
49}
50
51ALWAYS_INLINE static inline bool ForceSlowStringPath(CompilationUnit* cu) {
52 return (cu->enable_debug & (1 << kDebugSlowStringPath)) != 0;
53}
54
55ALWAYS_INLINE static inline bool ForceSlowTypePath(CompilationUnit* cu) {
56 return (cu->enable_debug & (1 << kDebugSlowTypePath)) != 0;
57}
58
Brian Carlstrom7940e442013-07-12 13:46:57 -070059/*
buzbeeb48819d2013-09-14 16:15:25 -070060 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 * blocks.
62 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070063void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 LIR* barrier = NewLIR0(kPseudoBarrier);
65 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070066 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010067 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070068}
69
Mingyao Yange643a172014-04-08 11:02:52 -070070void Mir2Lir::GenDivZeroException() {
71 LIR* branch = OpUnconditionalBranch(nullptr);
72 AddDivZeroCheckSlowPath(branch);
73}
74
75void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070076 LIR* branch = OpCondBranch(c_code, nullptr);
77 AddDivZeroCheckSlowPath(branch);
78}
79
Mingyao Yange643a172014-04-08 11:02:52 -070080void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
81 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070082 AddDivZeroCheckSlowPath(branch);
83}
84
85void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
86 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
87 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080088 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +000089 : LIRSlowPath(m2l, branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -070090 }
91
Mingyao Yange643a172014-04-08 11:02:52 -070092 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070093 m2l_->ResetRegPool();
94 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070095 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070096 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -070097 }
98 };
99
100 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
101}
Dave Allisonb373e092014-02-20 16:06:36 -0800102
Mingyao Yang80365d92014-04-18 12:10:58 -0700103void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
104 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
105 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800106 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
107 RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000108 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800109 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700110 }
111
112 void Compile() OVERRIDE {
113 m2l_->ResetRegPool();
114 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700115 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700116 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700117 }
118
119 private:
120 const RegStorage index_;
121 const RegStorage length_;
122 };
123
124 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
125 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
126}
127
128void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
129 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
130 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800131 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000132 : LIRSlowPath(m2l, branch_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800133 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700134 }
135
136 void Compile() OVERRIDE {
137 m2l_->ResetRegPool();
138 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700139 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700140
Andreas Gampeccc60262014-07-04 18:02:38 -0700141 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
142 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700143
144 m2l_->OpRegCopy(arg1_32, length_);
145 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700146 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700147 }
148
149 private:
150 const int32_t index_;
151 const RegStorage length_;
152 };
153
154 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
155 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
156}
157
Mingyao Yange643a172014-04-08 11:02:52 -0700158LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
159 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
160 public:
161 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000162 : LIRSlowPath(m2l, branch) {
Mingyao Yange643a172014-04-08 11:02:52 -0700163 }
164
165 void Compile() OVERRIDE {
166 m2l_->ResetRegPool();
167 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700168 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700169 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700170 }
171 };
172
173 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
174 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
175 return branch;
176}
177
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800179LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000180 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700181 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 }
Pavel Vyssotski9c3617a2014-11-13 18:25:23 +0600183 // If null check has not been eliminated, reset redundant store tracking.
184 if ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0) {
185 ResetDefTracking();
186 }
Dave Allisonb373e092014-02-20 16:06:36 -0800187 return nullptr;
188}
189
Dave Allisonf9439142014-03-27 15:10:22 -0700190/* Perform an explicit null-check on a register. */
191LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
192 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
193 return NULL;
194 }
Mingyao Yange643a172014-04-08 11:02:52 -0700195 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700196}
197
Dave Allisonb373e092014-02-20 16:06:36 -0800198void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000199 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800200 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
201 return;
202 }
Dave Allison69dfe512014-07-11 17:11:58 +0000203 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800204 MarkSafepointPC(last_lir_insn_);
205 }
206}
207
Andreas Gampe3c12c512014-06-24 18:46:29 +0000208void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000209 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000210 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
211 return;
212 }
213 MarkSafepointPCAfter(after);
214 }
215}
216
Dave Allisonb373e092014-02-20 16:06:36 -0800217void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000218 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800219 MarkSafepointPC(last_lir_insn_);
220 }
221}
222
buzbee2700f7e2014-03-07 09:46:20 -0800223void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000224 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800225 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
226 return;
227 }
228 // Force an implicit null check by performing a memory operation (load) from the given
229 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800230 RegStorage tmp = AllocTemp();
231 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700232 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800233 FreeTemp(tmp);
234 MarkSafepointPC(load);
235 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236}
237
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700239 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700241 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 switch (opcode) {
243 case Instruction::IF_EQ:
244 cond = kCondEq;
245 break;
246 case Instruction::IF_NE:
247 cond = kCondNe;
248 break;
249 case Instruction::IF_LT:
250 cond = kCondLt;
251 break;
252 case Instruction::IF_GE:
253 cond = kCondGe;
254 break;
255 case Instruction::IF_GT:
256 cond = kCondGt;
257 break;
258 case Instruction::IF_LE:
259 cond = kCondLe;
260 break;
261 default:
262 cond = static_cast<ConditionCode>(0);
263 LOG(FATAL) << "Unexpected opcode " << opcode;
264 }
265
266 // Normalize such that if either operand is constant, src2 will be constant
267 if (rl_src1.is_const) {
268 RegLocation rl_temp = rl_src1;
269 rl_src1 = rl_src2;
270 rl_src2 = rl_temp;
271 cond = FlipComparisonOrder(cond);
272 }
273
buzbee7c02e912014-10-03 13:14:17 -0700274 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 // Is this really an immediate comparison?
276 if (rl_src2.is_const) {
277 // If it's already live in a register or not easily materialized, just keep going
278 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700279 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100281 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800283 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 return;
285 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700286
287 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
288 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
289 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
290 // the 32b literal 0 for null.
291 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
292 // Use the OpCmpImmBranch and ignore the value in the register.
293 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
294 return;
295 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700297
buzbee7c02e912014-10-03 13:14:17 -0700298 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800299 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300}
301
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700302void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700304 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
305 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 switch (opcode) {
307 case Instruction::IF_EQZ:
308 cond = kCondEq;
309 break;
310 case Instruction::IF_NEZ:
311 cond = kCondNe;
312 break;
313 case Instruction::IF_LTZ:
314 cond = kCondLt;
315 break;
316 case Instruction::IF_GEZ:
317 cond = kCondGe;
318 break;
319 case Instruction::IF_GTZ:
320 cond = kCondGt;
321 break;
322 case Instruction::IF_LEZ:
323 cond = kCondLe;
324 break;
325 default:
326 cond = static_cast<ConditionCode>(0);
327 LOG(FATAL) << "Unexpected opcode " << opcode;
328 }
buzbee2700f7e2014-03-07 09:46:20 -0800329 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330}
331
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700332void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
334 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800335 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800337 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 }
buzbee2700f7e2014-03-07 09:46:20 -0800339 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 StoreValueWide(rl_dest, rl_result);
341}
342
Yevgeny Rouban6af82062014-11-26 18:11:54 +0600343void Mir2Lir::GenLongToInt(RegLocation rl_dest, RegLocation rl_src) {
344 rl_src = UpdateLocWide(rl_src);
345 rl_src = NarrowRegLoc(rl_src);
346 StoreValue(rl_dest, rl_src);
347}
348
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700350 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700351 rl_src = LoadValue(rl_src, kCoreReg);
352 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
353 OpKind op = kOpInvalid;
354 switch (opcode) {
355 case Instruction::INT_TO_BYTE:
356 op = kOp2Byte;
357 break;
358 case Instruction::INT_TO_SHORT:
359 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700361 case Instruction::INT_TO_CHAR:
362 op = kOp2Char;
363 break;
364 default:
365 LOG(ERROR) << "Bad int conversion type";
366 }
buzbee2700f7e2014-03-07 09:46:20 -0800367 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700368 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369}
370
Andreas Gampe98430592014-07-27 19:44:50 -0700371/*
372 * Let helper function take care of everything. Will call
373 * Array::AllocFromCode(type_idx, method, count);
374 * Note: AllocFromCode will handle checks for errNegativeArraySize.
375 */
376void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
377 RegLocation rl_src) {
378 FlushAllRegs(); /* Everything to home location */
379 const DexFile* dex_file = cu_->dex_file;
380 CompilerDriver* driver = cu_->compiler_driver;
381 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800382 bool is_type_initialized; // Ignored as an array does not have an initializer.
383 bool use_direct_type_ptr;
384 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700385 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800386 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700387 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
388 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800389 // The fast path.
390 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700391 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800392 CallRuntimeHelperRegRegLocationMethod(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
Andreas Gampe98430592014-07-27 19:44:50 -0700393 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800394 } else {
395 // Use the direct pointer.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800396 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
Andreas Gampe98430592014-07-27 19:44:50 -0700397 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800398 }
399 } else {
400 // The slow path.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800401 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800402 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 } else {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800404 CallRuntimeHelperImmRegLocationMethod(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 }
Andreas Gampe98430592014-07-27 19:44:50 -0700406 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407}
408
409/*
410 * Similar to GenNewArray, but with post-allocation initialization.
411 * Verifier guarantees we're dealing with an array class. Current
412 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
413 * Current code also throws internal unimp if not 'L', '[' or 'I'.
414 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700415void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000416 size_t elems = info->num_arg_words;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 int type_idx = info->index;
418 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700419 QuickEntrypointEnum target;
420 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
421 type_idx)) {
422 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700424 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 }
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800426 CallRuntimeHelperImmImmMethod(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700427 FreeTemp(TargetReg(kArg2, kNotWide));
428 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 /*
430 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
431 * return region. Because AllocFromCode placed the new array
432 * in kRet0, we'll just lock it into place. When debugger support is
433 * added, it may be necessary to additionally copy all return
434 * values to a home location in thread-local storage
435 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700436 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700437 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438
439 // TODO: use the correct component size, currently all supported types
440 // share array alignment with ints (see comment at head of function)
441 size_t component_size = sizeof(int32_t);
442
Vladimir Markobf535be2014-11-19 18:52:35 +0000443 if (elems > 5) {
444 DCHECK(info->is_range); // Non-range insn can't encode more than 5 elems.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 /*
446 * Bit of ugliness here. We're going generate a mem copy loop
447 * on the register range, but it is possible that some regs
448 * in the range have been promoted. This is unlikely, but
449 * before generating the copy, we'll just force a flush
450 * of any regs in the source range that have been promoted to
451 * home location.
452 */
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000453 for (size_t i = 0; i < elems; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 RegLocation loc = UpdateLoc(info->args[i]);
455 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100456 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600457 if (loc.ref) {
458 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
459 } else {
460 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
461 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
463 }
464 /*
465 * TUNING note: generated code here could be much improved, but
466 * this is an uncommon operation and isn't especially performance
467 * critical.
468 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700469 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700470 RegStorage r_src = AllocTempRef();
471 RegStorage r_dst = AllocTempRef();
472 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800473 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700474 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700476 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700477 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 break;
479 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700480 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700481 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 r_val = AllocTemp();
483 break;
484 case kMips:
Maja Gagic6ea651f2015-02-24 16:55:04 +0100485 case kMips64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 r_val = AllocTemp();
487 break;
488 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
489 }
490 // Set up source pointer
491 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700492 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700494 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 mirror::Array::DataOffset(component_size).Int32Value());
496 // Set up the loop counter (known to be > 0)
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000497 LoadConstant(r_idx, static_cast<int>(elems - 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800499 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100501 {
502 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
503 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
504 // NOTE: No dalvik register annotation, local optimizations will be stopped
505 // by the loop boundaries.
506 }
buzbee695d13a2014-04-19 13:32:20 -0700507 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800509 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700510 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700512 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 -mirror::Array::DataOffset(component_size).Int32Value());
514 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000515 FreeTemp(r_idx);
516 FreeTemp(r_dst);
517 FreeTemp(r_src);
518 } else {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000519 DCHECK_LE(elems, 5u); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 // TUNING: interleave
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000521 for (size_t i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600522 RegLocation rl_arg;
523 if (info->args[i].ref) {
524 rl_arg = LoadValue(info->args[i], kRefReg);
525 StoreRefDisp(ref_reg,
526 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
527 kNotVolatile);
528 } else {
529 rl_arg = LoadValue(info->args[i], kCoreReg);
530 Store32Disp(ref_reg,
531 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
532 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800534 if (IsTemp(rl_arg.reg)) {
535 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 }
537 }
538 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000539 if (elems != 0 && info->args[0].ref) {
540 // If there is at least one potentially non-null value, unconditionally mark the GC card.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000541 for (size_t i = 0; i < elems; i++) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000542 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
543 UnconditionallyMarkGCCard(ref_reg);
544 break;
545 }
546 }
547 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700549 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 }
551}
552
Ian Rogers832336b2014-10-08 15:35:22 -0700553/*
554 * Array data table format:
555 * ushort ident = 0x0300 magic value
556 * ushort width width of each element in the table
557 * uint size number of elements in the table
558 * ubyte data[size*width] table of data values (may contain a single-byte
559 * padding at the end)
560 *
561 * Total size is 4+(width * size + 1)/2 16-bit code units.
562 */
563void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
564 if (kIsDebugBuild) {
565 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
566 const Instruction::ArrayDataPayload* payload =
567 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
568 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
569 }
570 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
571 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
572}
573
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800574//
575// Slow path to ensure a class is initialized for sget/sput.
576//
577class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
578 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100579 // There are up to two branches to the static field slow path, the "unresolved" when the type
580 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
581 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800582 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100583 RegStorage r_base)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000584 : LIRSlowPath(m2l, unresolved != nullptr ? unresolved : uninit, cont),
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100585 second_branch_(unresolved != nullptr ? uninit : nullptr),
586 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800587 }
588
589 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100590 LIR* target = GenerateTargetLabel();
591 if (second_branch_ != nullptr) {
592 second_branch_->target = target;
593 }
Andreas Gampe98430592014-07-27 19:44:50 -0700594 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800595 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700596 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800597
598 m2l_->OpUnconditionalBranch(cont_);
599 }
600
601 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100602 // Second branch to the slow path, or null if there's only one branch.
603 LIR* const second_branch_;
604
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800605 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800606 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800607};
608
Fred Shih37f05ef2014-07-16 18:38:08 -0700609void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000610 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000611 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000612 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800613 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000614 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800615 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000616 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100618 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700619 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000620 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
621 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800622 if (IsTemp(rl_method.reg)) {
623 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 }
625 } else {
626 // Medium path, static storage base in a different class which requires checks that the other
627 // class is initialized.
628 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000629 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 // May do runtime call so everything to home locations.
631 FlushAllRegs();
632 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700633 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 LockTemp(r_method);
635 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700636 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800637 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000638 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
639 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000640 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000641 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800642 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100643 LIR* unresolved_branch = nullptr;
644 if (!field_info.IsClassInDexCache() &&
645 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
646 // Check if r_base is NULL.
647 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
648 }
649 LIR* uninit_branch = nullptr;
650 if (!field_info.IsClassInitialized() &&
651 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
652 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700653 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800654 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100655 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800656 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000657 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100658 FreeTemp(r_tmp);
659 }
660 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
661 // The slow path is invoked if the r_base is NULL or the class pointed
662 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800663 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800664 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000665 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800666
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100667 if (uninit_branch != nullptr) {
668 // Ensure load of status and store of value don't re-order.
669 // TODO: Presumably the actual value store is control-dependent on the status load,
670 // and will thus not be reordered in any case, since stores are never speculated.
671 // Does later code "know" that the class is now initialized? If so, we still
672 // need the barrier to guard later static loads.
673 GenMemBarrier(kLoadAny);
674 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 FreeTemp(r_method);
677 }
678 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700679 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
680 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100681 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100683 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700685 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000686 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
687 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100688 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700689 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000690 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700692 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000693 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800695 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 } else {
697 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700698 QuickEntrypointEnum target;
699 switch (size) {
700 case kReference:
701 target = kQuickSetObjStatic;
702 break;
703 case k64:
704 case kDouble:
705 target = kQuickSet64Static;
706 break;
707 case k32:
708 case kSingle:
709 target = kQuickSet32Static;
710 break;
711 case kSignedHalf:
712 case kUnsignedHalf:
713 target = kQuickSet16Static;
714 break;
715 case kSignedByte:
716 case kUnsignedByte:
717 target = kQuickSet8Static;
718 break;
719 case kWord: // Intentional fallthrough.
720 default:
721 LOG(FATAL) << "Can't determine entrypoint for: " << size;
722 target = kQuickSet32Static;
723 }
Andreas Gampe98430592014-07-27 19:44:50 -0700724 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
726}
727
Fred Shih37f05ef2014-07-16 18:38:08 -0700728void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000729 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000730 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000731 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700732
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800733 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000734 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800735 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000736 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 // Fast path, static storage base is this method's class
738 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700739 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000740 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
741 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 } else {
743 // Medium path, static storage base in a different class which requires checks that the other
744 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000745 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 // May do runtime call so everything to home locations.
747 FlushAllRegs();
748 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700749 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 LockTemp(r_method);
751 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700752 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800753 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000754 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
755 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000756 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000757 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800758 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100759 LIR* unresolved_branch = nullptr;
760 if (!field_info.IsClassInDexCache() &&
761 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
762 // Check if r_base is NULL.
763 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
764 }
765 LIR* uninit_branch = nullptr;
766 if (!field_info.IsClassInitialized() &&
767 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
768 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700769 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800770 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100771 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800772 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000773 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100774 FreeTemp(r_tmp);
775 }
776 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
777 // The slow path is invoked if the r_base is NULL or the class pointed
778 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800779 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800780 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000781 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800782
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100783 if (uninit_branch != nullptr) {
784 // Ensure load of status and load of value don't re-order.
785 GenMemBarrier(kLoadAny);
786 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 FreeTemp(r_method);
789 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800790 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700791 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100792 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800793
Vladimir Marko674744e2014-04-24 15:18:26 +0100794 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700795 if (IsRef(size)) {
796 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000797 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
798 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100799 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700800 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000801 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800802 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100803 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800804
Fred Shih37f05ef2014-07-16 18:38:08 -0700805 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 StoreValueWide(rl_dest, rl_result);
807 } else {
808 StoreValue(rl_dest, rl_result);
809 }
810 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700811 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700813 QuickEntrypointEnum target;
814 switch (type) {
815 case Primitive::kPrimNot:
816 target = kQuickGetObjStatic;
817 break;
818 case Primitive::kPrimLong:
819 case Primitive::kPrimDouble:
820 target = kQuickGet64Static;
821 break;
822 case Primitive::kPrimInt:
823 case Primitive::kPrimFloat:
824 target = kQuickGet32Static;
825 break;
826 case Primitive::kPrimShort:
827 target = kQuickGetShortStatic;
828 break;
829 case Primitive::kPrimChar:
830 target = kQuickGetCharStatic;
831 break;
832 case Primitive::kPrimByte:
833 target = kQuickGetByteStatic;
834 break;
835 case Primitive::kPrimBoolean:
836 target = kQuickGetBooleanStatic;
837 break;
838 case Primitive::kPrimVoid: // Intentional fallthrough.
839 default:
840 LOG(FATAL) << "Can't determine entrypoint for: " << type;
841 target = kQuickGet32Static;
842 }
Andreas Gampe98430592014-07-27 19:44:50 -0700843 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
844
Douglas Leung2db3e262014-06-25 16:02:55 -0700845 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700846 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700847 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 StoreValueWide(rl_dest, rl_result);
849 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700850 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 StoreValue(rl_dest, rl_result);
852 }
853 }
854}
855
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800856// Generate code for all slow paths.
857void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700858 // We should check slow_paths_.Size() every time, because a new slow path
859 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100860 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800861 slowpath->Compile();
862 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100863 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800864}
865
Fred Shih37f05ef2014-07-16 18:38:08 -0700866void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
867 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000868 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800869 if (kIsDebugBuild) {
870 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
871 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
872 IGetMemAccessType(mir->dalvikInsn.opcode);
873 DCHECK_EQ(mem_access_type, field_info.MemAccessType()) << mir->dalvikInsn.opcode;
874 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000875 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800876 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700877 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700878 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000879 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700880 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100881 GenNullCheck(rl_obj.reg, opt_flags);
882 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
883 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000884 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700885 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000886 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
887 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100888 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700889 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000890 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100891 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000892 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700893 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 StoreValueWide(rl_dest, rl_result);
895 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 StoreValue(rl_dest, rl_result);
897 }
898 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700899 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
900 QuickEntrypointEnum target;
901 switch (type) {
902 case Primitive::kPrimNot:
903 target = kQuickGetObjInstance;
904 break;
905 case Primitive::kPrimLong:
906 case Primitive::kPrimDouble:
907 target = kQuickGet64Instance;
908 break;
909 case Primitive::kPrimFloat:
910 case Primitive::kPrimInt:
911 target = kQuickGet32Instance;
912 break;
913 case Primitive::kPrimShort:
914 target = kQuickGetShortInstance;
915 break;
916 case Primitive::kPrimChar:
917 target = kQuickGetCharInstance;
918 break;
919 case Primitive::kPrimByte:
920 target = kQuickGetByteInstance;
921 break;
922 case Primitive::kPrimBoolean:
923 target = kQuickGetBooleanInstance;
924 break;
925 case Primitive::kPrimVoid: // Intentional fallthrough.
926 default:
927 LOG(FATAL) << "Can't determine entrypoint for: " << type;
928 target = kQuickGet32Instance;
929 }
Andreas Gampe98430592014-07-27 19:44:50 -0700930 // Second argument of pGetXXInstance is always a reference.
931 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
932 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
933
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700934 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700935 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700936 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 StoreValueWide(rl_dest, rl_result);
938 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700939 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 StoreValue(rl_dest, rl_result);
941 }
942 }
943}
944
Vladimir Markobe0e5462014-02-26 11:24:15 +0000945void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700946 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000947 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800948 if (kIsDebugBuild) {
949 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
950 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
951 IPutMemAccessType(mir->dalvikInsn.opcode);
952 DCHECK_EQ(mem_access_type, field_info.MemAccessType());
953 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000954 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800955 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700956 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700957 // Dex code never writes to the class field.
958 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
959 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700960 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700961 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100962 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 } else {
964 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100965 }
966 GenNullCheck(rl_obj.reg, opt_flags);
967 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000968 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700969 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000970 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000971 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100972 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000973 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
974 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100975 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000976 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700977 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000978 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 }
980 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700981 QuickEntrypointEnum target;
982 switch (size) {
983 case kReference:
984 target = kQuickSetObjInstance;
985 break;
986 case k64:
987 case kDouble:
988 target = kQuickSet64Instance;
989 break;
990 case k32:
991 case kSingle:
992 target = kQuickSet32Instance;
993 break;
994 case kSignedHalf:
995 case kUnsignedHalf:
996 target = kQuickSet16Instance;
997 break;
998 case kSignedByte:
999 case kUnsignedByte:
1000 target = kQuickSet8Instance;
1001 break;
1002 case kWord: // Intentional fallthrough.
1003 default:
1004 LOG(FATAL) << "Can't determine entrypoint for: " << size;
1005 target = kQuickSet32Instance;
1006 }
Andreas Gampe98430592014-07-27 19:44:50 -07001007 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
1008 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 }
1010}
1011
Ian Rogersa9a82542013-10-04 11:17:26 -07001012void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
1013 RegLocation rl_src) {
1014 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
1015 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
1016 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -07001017 QuickEntrypointEnum target = needs_range_check
1018 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
1019 : kQuickAputObjectWithBoundCheck)
1020 : kQuickAputObject;
1021 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -07001022}
1023
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001024void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001026 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -07001027 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 *cu_->dex_file,
1030 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 // Call out to helper which resolves type and verifies access.
1032 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -07001033 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001034 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 StoreValue(rl_dest, rl_result);
1036 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001037 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 // We're don't need access checks, load type from dex cache
1039 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001040 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001041 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001042 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001043 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001045 type_idx) || ForceSlowTypePath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 // Slow path, at runtime test if type is null and if so initialize
1047 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001048 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001049 LIR* cont = NewLIR0(kPseudoTargetLabel);
1050
1051 // Object to generate the slow path for class resolution.
1052 class SlowPath : public LIRSlowPath {
1053 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001054 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001055 const RegLocation& rl_method_in, const RegLocation& rl_result_in)
1056 : LIRSlowPath(m2l, fromfast, cont_in),
1057 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001058 }
1059
1060 void Compile() {
1061 GenerateTargetLabel();
1062
Andreas Gampe98430592014-07-27 19:44:50 -07001063 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001064 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001065 m2l_->OpUnconditionalBranch(cont_);
1066 }
1067
1068 private:
1069 const int type_idx_;
1070 const RegLocation rl_method_;
1071 const RegLocation rl_result_;
1072 };
1073
1074 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001075 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001076
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001078 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 // Fast path, we're done - just store result
1080 StoreValue(rl_dest, rl_result);
1081 }
1082 }
1083}
1084
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001085void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001087 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1088 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001090 *cu_->dex_file, string_idx) || ForceSlowStringPath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 // slow path, resolve string if not in dex cache
1092 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001093 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001094
1095 // If the Method* is already in a register, we can save a copy.
1096 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001097 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001098 if (rl_method.location == kLocPhysReg) {
1099 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001100 DCHECK(!IsTemp(rl_method.reg));
1101 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001102 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001103 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001104 LoadCurrMethodDirect(r_method);
1105 }
Mathieu Chartiereace4582014-11-24 18:29:54 -08001106 // Method to declaring class.
1107 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1108 TargetReg(kArg0, kRef), kNotVolatile);
1109 // Declaring class to dex cache strings.
1110 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Class::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001111 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001112
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001114 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1115 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001116 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001117
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001118 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001119 // Object to generate the slow path for string resolution.
1120 class SlowPath : public LIRSlowPath {
1121 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001122 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001123 int32_t string_idx_in)
1124 : LIRSlowPath(m2l, fromfast_in, cont_in),
1125 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001126 }
1127
1128 void Compile() {
1129 GenerateTargetLabel();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001130 m2l_->CallRuntimeHelperImmReg(kQuickResolveString, string_idx_, r_method_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001131 m2l_->OpUnconditionalBranch(cont_);
1132 }
1133
1134 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001135 const RegStorage r_method_;
1136 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001137 };
1138
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001139 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001141
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001143 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 } else {
1145 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001146 RegStorage res_reg = AllocTempRef();
1147 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001148 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1149 kNotVolatile);
1150 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001151 kNotVolatile);
1152 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 StoreValue(rl_dest, rl_result);
1154 }
1155}
1156
Andreas Gampe98430592014-07-27 19:44:50 -07001157/*
1158 * Let helper function take care of everything. Will
1159 * call Class::NewInstanceFromCode(type_idx, method);
1160 */
1161void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1162 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 // alloc will always check for resolution, do we also need to verify
1164 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001165 const DexFile* dex_file = cu_->dex_file;
1166 CompilerDriver* driver = cu_->compiler_driver;
1167 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001168 bool is_type_initialized;
1169 bool use_direct_type_ptr;
1170 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001171 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001172 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001173 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1174 &direct_type_ptr, &is_finalizable) &&
1175 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001176 // The fast path.
1177 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001178 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001179 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001180 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001181 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001182 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001183 }
1184 } else {
1185 // Use the direct pointer.
1186 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001187 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001188 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001189 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001190 }
1191 }
1192 } else {
1193 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001194 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001195 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001197 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 }
Andreas Gampe98430592014-07-27 19:44:50 -07001199 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200}
1201
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001202void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001204 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205}
1206
1207// For final classes there are no sub-classes to check and so we can answer the instance-of
1208// question with simple comparisons.
1209void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1210 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001211 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001212 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001213
buzbeea0cd2d72014-06-01 09:33:49 -07001214 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001216 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001217 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001219 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 }
1221 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001222 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223
buzbeea0cd2d72014-06-01 09:33:49 -07001224 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1225 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226
1227 LoadCurrMethodDirect(check_class);
1228 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001229 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1230 kNotVolatile);
1231 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1232 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 } else {
buzbee695d13a2014-04-19 13:32:20 -07001234 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001235 check_class, kNotVolatile);
1236 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1237 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001238 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001239 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 }
1241
buzbee695d13a2014-04-19 13:32:20 -07001242 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 if (cu_->instruction_set == kThumb2) {
1244 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001245 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001247 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001249 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 }
1251 LIR* target = NewLIR0(kPseudoTargetLabel);
1252 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 FreeTemp(object_class);
1254 FreeTemp(check_class);
1255 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001256 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 FreeTemp(result_reg);
1258 }
1259 StoreValue(rl_dest, rl_result);
1260}
1261
1262void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1263 bool type_known_abstract, bool use_declaring_class,
1264 bool can_assume_type_is_in_dex_cache,
1265 uint32_t type_idx, RegLocation rl_dest,
1266 RegLocation rl_src) {
1267 FlushAllRegs();
1268 // May generate a call - use explicit registers
1269 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001270 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001271 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001272 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001273 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1274 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 if (needs_access_check) {
1276 // Check we have access to type_idx and if not throw IllegalAccessError,
1277 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001278 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001279 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1280 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001282 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001283 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001284 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001286 if (can_assume_type_is_in_dex_cache) {
1287 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001288 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001289 }
1290
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001292 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001293 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001294 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001295 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001297 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1298 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1299
1300 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001301 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001302
1303 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1304 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001305 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1306 RegLocation rl_src_in)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001307 : LIRSlowPath(m2l, branch, cont), type_idx_(type_idx_in),
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001308 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001309 }
1310
1311 void Compile() OVERRIDE {
1312 GenerateTargetLabel();
1313
Andreas Gampe98430592014-07-27 19:44:50 -07001314 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001315 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1316 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001317 m2l_->OpUnconditionalBranch(cont_);
1318 }
1319
1320 private:
1321 uint32_t type_idx_;
1322 RegLocation rl_src_;
1323 };
1324
1325 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1326 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 }
1328 }
1329 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001330 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001331 if (!IsSameReg(rl_result.reg, ref_reg)) {
1332 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001333 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001335 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336
1337 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001338 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001340 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1341 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1343 LIR* branchover = NULL;
1344 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001345 // rl_result == ref == class.
1346 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001347 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 } else {
1349 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001350 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001351 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (!type_known_abstract) {
1353 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001354 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001355 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001356 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001358 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001360 if (it != nullptr) {
1361 OpEndIT(it);
1362 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 FreeTemp(r_tgt);
1364 } else {
1365 if (!type_known_abstract) {
1366 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001367 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001368 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001369 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001370
Serguei Katkov9ee45192014-07-17 14:39:03 +07001371 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001372 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 }
1374 }
1375 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001376 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 /* branch targets here */
1378 LIR* target = NewLIR0(kPseudoTargetLabel);
1379 StoreValue(rl_dest, rl_result);
1380 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001381 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 branchover->target = target;
1383 }
1384}
1385
1386void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1387 bool type_known_final, type_known_abstract, use_declaring_class;
1388 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1389 *cu_->dex_file,
1390 type_idx,
1391 &type_known_final,
1392 &type_known_abstract,
1393 &use_declaring_class);
1394 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1395 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1396
1397 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1398 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1399 } else {
1400 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1401 use_declaring_class, can_assume_type_is_in_dex_cache,
1402 type_idx, rl_dest, rl_src);
1403 }
1404}
1405
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001406void Mir2Lir::GenCheckCast(int opt_flags, uint32_t insn_idx, uint32_t type_idx,
1407 RegLocation rl_src) {
1408 if ((opt_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1409 // Compiler analysis proved that this check-cast would never cause an exception.
1410 return;
1411 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 bool type_known_final, type_known_abstract, use_declaring_class;
1413 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1414 *cu_->dex_file,
1415 type_idx,
1416 &type_known_final,
1417 &type_known_abstract,
1418 &use_declaring_class);
1419 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1420 // of the exception throw path.
1421 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001422 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001423 // Verifier type analysis proved this check cast would never cause an exception.
1424 return;
1425 }
1426 FlushAllRegs();
1427 // May generate a call - use explicit registers
1428 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001429 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001430 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001431 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 if (needs_access_check) {
1433 // Check we have access to type_idx and if not throw IllegalAccessError,
1434 // returns Class* in kRet0
1435 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001436 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001437 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001439 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001440 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 } else {
1442 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001443 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001444 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001445 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001446 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1448 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001449 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1450 LIR* cont = NewLIR0(kPseudoTargetLabel);
1451
1452 // Slow path to initialize the type. Executed if the type is NULL.
1453 class SlowPath : public LIRSlowPath {
1454 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001455 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001456 const RegStorage class_reg_in)
1457 : LIRSlowPath(m2l, fromfast, cont_in),
1458 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459 }
1460
1461 void Compile() {
1462 GenerateTargetLabel();
1463
1464 // Call out to helper, which will return resolved type in kArg0
1465 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001466 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1467 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001468 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001469 m2l_->OpUnconditionalBranch(cont_);
1470 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001471
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001472 public:
1473 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001474 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001475 };
1476
buzbee2700f7e2014-03-07 09:46:20 -08001477 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 }
1479 }
1480 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001481 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001482
1483 // Slow path for the case where the classes are not equal. In this case we need
1484 // to call a helper function to do the check.
1485 class SlowPath : public LIRSlowPath {
1486 public:
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00001487 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load)
1488 : LIRSlowPath(m2l, fromfast, cont), load_(load) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001489 }
1490
1491 void Compile() {
1492 GenerateTargetLabel();
1493
1494 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001495 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1496 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001497 }
Andreas Gampe98430592014-07-27 19:44:50 -07001498 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1499 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001500 m2l_->OpUnconditionalBranch(cont_);
1501 }
1502
1503 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001504 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001505 };
1506
1507 if (type_known_abstract) {
1508 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001509 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001510 LIR* cont = NewLIR0(kPseudoTargetLabel);
1511 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1512 } else {
1513 // Harder, more common case. We need to generate a forward branch over the load
1514 // if the target is null. If it's non-null we perform the load and branch to the
1515 // slow path if the classes are not equal.
1516
1517 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001518 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001519 /* load object->klass_ */
1520 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001521 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1522 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001523
Andreas Gampeccc60262014-07-04 18:02:38 -07001524 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001525 LIR* cont = NewLIR0(kPseudoTargetLabel);
1526
1527 // Add the slow path that will not perform load since this is already done.
1528 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1529
1530 // Set the null check to branch to the continuation.
1531 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 }
1533}
1534
1535void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001536 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 RegLocation rl_result;
1538 if (cu_->instruction_set == kThumb2) {
1539 /*
1540 * NOTE: This is the one place in the code in which we might have
1541 * as many as six live temporary registers. There are 5 in the normal
1542 * set for Arm. Until we have spill capabilities, temporarily add
1543 * lr to the temp set. It is safe to do this locally, but note that
1544 * lr is used explicitly elsewhere in the code generator and cannot
1545 * normally be used as a general temp register.
1546 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001547 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1548 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
1550 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1551 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1552 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1553 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001554 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1555 RegStorage t_reg = AllocTemp();
1556 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1557 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1558 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 FreeTemp(t_reg);
1560 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001561 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1562 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 }
1564 /*
1565 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1566 * following StoreValueWide might need to allocate a temp register.
1567 * To further work around the lack of a spill capability, explicitly
1568 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1569 * Remove when spill is functional.
1570 */
1571 FreeRegLocTemps(rl_result, rl_src1);
1572 FreeRegLocTemps(rl_result, rl_src2);
1573 StoreValueWide(rl_dest, rl_result);
1574 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001575 Clobber(TargetReg(kLr, kNotWide));
1576 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 }
1578}
1579
Andreas Gampe98430592014-07-27 19:44:50 -07001580void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1581 RegLocation rl_src1, RegLocation rl_shift) {
1582 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 switch (opcode) {
1584 case Instruction::SHL_LONG:
1585 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001586 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 break;
1588 case Instruction::SHR_LONG:
1589 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001590 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 break;
1592 case Instruction::USHR_LONG:
1593 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001594 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 break;
1596 default:
1597 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001598 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 }
Andreas Gampe98430592014-07-27 19:44:50 -07001600 FlushAllRegs(); /* Send everything to home location */
1601 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001602 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 StoreValueWide(rl_dest, rl_result);
1604}
1605
1606
1607void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001608 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001609 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 OpKind op = kOpBkpt;
1611 bool is_div_rem = false;
1612 bool check_zero = false;
1613 bool unary = false;
1614 RegLocation rl_result;
1615 bool shift_op = false;
1616 switch (opcode) {
1617 case Instruction::NEG_INT:
1618 op = kOpNeg;
1619 unary = true;
1620 break;
1621 case Instruction::NOT_INT:
1622 op = kOpMvn;
1623 unary = true;
1624 break;
1625 case Instruction::ADD_INT:
1626 case Instruction::ADD_INT_2ADDR:
1627 op = kOpAdd;
1628 break;
1629 case Instruction::SUB_INT:
1630 case Instruction::SUB_INT_2ADDR:
1631 op = kOpSub;
1632 break;
1633 case Instruction::MUL_INT:
1634 case Instruction::MUL_INT_2ADDR:
1635 op = kOpMul;
1636 break;
1637 case Instruction::DIV_INT:
1638 case Instruction::DIV_INT_2ADDR:
1639 check_zero = true;
1640 op = kOpDiv;
1641 is_div_rem = true;
1642 break;
1643 /* NOTE: returns in kArg1 */
1644 case Instruction::REM_INT:
1645 case Instruction::REM_INT_2ADDR:
1646 check_zero = true;
1647 op = kOpRem;
1648 is_div_rem = true;
1649 break;
1650 case Instruction::AND_INT:
1651 case Instruction::AND_INT_2ADDR:
1652 op = kOpAnd;
1653 break;
1654 case Instruction::OR_INT:
1655 case Instruction::OR_INT_2ADDR:
1656 op = kOpOr;
1657 break;
1658 case Instruction::XOR_INT:
1659 case Instruction::XOR_INT_2ADDR:
1660 op = kOpXor;
1661 break;
1662 case Instruction::SHL_INT:
1663 case Instruction::SHL_INT_2ADDR:
1664 shift_op = true;
1665 op = kOpLsl;
1666 break;
1667 case Instruction::SHR_INT:
1668 case Instruction::SHR_INT_2ADDR:
1669 shift_op = true;
1670 op = kOpAsr;
1671 break;
1672 case Instruction::USHR_INT:
1673 case Instruction::USHR_INT_2ADDR:
1674 shift_op = true;
1675 op = kOpLsr;
1676 break;
1677 default:
1678 LOG(FATAL) << "Invalid word arith op: " << opcode;
1679 }
1680 if (!is_div_rem) {
1681 if (unary) {
1682 rl_src1 = LoadValue(rl_src1, kCoreReg);
1683 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001684 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001686 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001687 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001688 RegStorage t_reg = AllocTemp();
1689 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 rl_src1 = LoadValue(rl_src1, kCoreReg);
1691 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001692 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 FreeTemp(t_reg);
1694 } else {
1695 rl_src1 = LoadValue(rl_src1, kCoreReg);
1696 rl_src2 = LoadValue(rl_src2, kCoreReg);
1697 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001698 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 }
1700 }
1701 StoreValue(rl_dest, rl_result);
1702 } else {
Dave Allison70202782013-10-22 17:52:19 -07001703 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Maja Gagic6ea651f2015-02-24 16:55:04 +01001704 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
1705 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 rl_src1 = LoadValue(rl_src1, kCoreReg);
1707 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001708 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001709 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 }
buzbee2700f7e2014-03-07 09:46:20 -08001711 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001712 done = true;
1713 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001714 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001715 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001716 // Use ARM SDIV instruction for division. For remainder we also need to
1717 // calculate using a MUL and subtract.
1718 rl_src1 = LoadValue(rl_src1, kCoreReg);
1719 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001720 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001721 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001722 }
buzbee2700f7e2014-03-07 09:46:20 -08001723 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001724 done = true;
1725 }
1726 }
1727
1728 // If we haven't already generated the code use the callout function.
1729 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001731 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001732 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001733 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001734 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001735 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 }
Dave Allison70202782013-10-22 17:52:19 -07001737 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001738 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001740 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 else
1742 rl_result = GetReturnAlt();
1743 }
1744 StoreValue(rl_dest, rl_result);
1745 }
1746}
1747
1748/*
1749 * The following are the first-level codegen routines that analyze the format
1750 * of each bytecode then either dispatch special purpose codegen routines
1751 * or produce corresponding Thumb instructions directly.
1752 */
1753
Brian Carlstrom7940e442013-07-12 13:46:57 -07001754// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001755static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 x &= x - 1;
1757 return (x & (x - 1)) == 0;
1758}
1759
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1761// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001762bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001763 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001764 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 return false;
1766 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001767 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 if (k >= 30) {
1769 // Avoid special cases.
1770 return false;
1771 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772 rl_src = LoadValue(rl_src, kCoreReg);
1773 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001774 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001775 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 if (lit == 2) {
1777 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001778 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1779 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1780 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001782 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001784 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1785 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 }
1787 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001788 RegStorage t_reg1 = AllocTemp();
1789 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001791 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1792 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001794 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001796 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001797 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001798 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001799 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001800 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 }
1802 }
1803 StoreValue(rl_dest, rl_result);
1804 return true;
1805}
1806
1807// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1808// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001809bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001810 if (lit < 0) {
1811 return false;
1812 }
1813 if (lit == 0) {
1814 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1815 LoadConstant(rl_result.reg, 0);
1816 StoreValue(rl_dest, rl_result);
1817 return true;
1818 }
1819 if (lit == 1) {
1820 rl_src = LoadValue(rl_src, kCoreReg);
1821 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1822 OpRegCopy(rl_result.reg, rl_src.reg);
1823 StoreValue(rl_dest, rl_result);
1824 return true;
1825 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001826 // There is RegRegRegShift on Arm, so check for more special cases
1827 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001828 return EasyMultiply(rl_src, rl_dest, lit);
1829 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 // Can we simplify this multiplication?
1831 bool power_of_two = false;
1832 bool pop_count_le2 = false;
1833 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001834 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001835 power_of_two = true;
1836 } else if (IsPopCountLE2(lit)) {
1837 pop_count_le2 = true;
1838 } else if (IsPowerOfTwo(lit + 1)) {
1839 power_of_two_minus_one = true;
1840 } else {
1841 return false;
1842 }
1843 rl_src = LoadValue(rl_src, kCoreReg);
1844 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1845 if (power_of_two) {
1846 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001847 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001848 } else if (pop_count_le2) {
1849 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001850 int first_bit = CTZ(lit);
1851 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001852 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1853 } else {
1854 // Reverse subtract: (src << (shift + 1)) - src.
1855 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001856 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001857 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001858 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001859 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001860 }
1861 StoreValue(rl_dest, rl_result);
1862 return true;
1863}
1864
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001865// Returns true if it generates instructions.
1866bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1867 RegLocation rl_src2) {
1868 if (!rl_src2.is_const ||
1869 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1870 return false;
1871 }
1872
1873 if (!rl_src2.wide) {
1874 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1875 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1876 // Generate multiply by reciprocal instead of div.
1877 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1878 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1879 return true;
1880 }
1881 } else {
1882 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1883 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1884 // Generate multiply by reciprocal instead of div.
1885 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1886 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1887 return true;
1888 }
1889 }
1890 return false;
1891}
1892
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001894 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 RegLocation rl_result;
1896 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1897 int shift_op = false;
1898 bool is_div = false;
1899
1900 switch (opcode) {
1901 case Instruction::RSUB_INT_LIT8:
1902 case Instruction::RSUB_INT: {
1903 rl_src = LoadValue(rl_src, kCoreReg);
1904 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1905 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001906 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001908 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1909 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910 }
1911 StoreValue(rl_dest, rl_result);
1912 return;
1913 }
1914
1915 case Instruction::SUB_INT:
1916 case Instruction::SUB_INT_2ADDR:
1917 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001918 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001919 case Instruction::ADD_INT:
1920 case Instruction::ADD_INT_2ADDR:
1921 case Instruction::ADD_INT_LIT8:
1922 case Instruction::ADD_INT_LIT16:
1923 op = kOpAdd;
1924 break;
1925 case Instruction::MUL_INT:
1926 case Instruction::MUL_INT_2ADDR:
1927 case Instruction::MUL_INT_LIT8:
1928 case Instruction::MUL_INT_LIT16: {
1929 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1930 return;
1931 }
1932 op = kOpMul;
1933 break;
1934 }
1935 case Instruction::AND_INT:
1936 case Instruction::AND_INT_2ADDR:
1937 case Instruction::AND_INT_LIT8:
1938 case Instruction::AND_INT_LIT16:
1939 op = kOpAnd;
1940 break;
1941 case Instruction::OR_INT:
1942 case Instruction::OR_INT_2ADDR:
1943 case Instruction::OR_INT_LIT8:
1944 case Instruction::OR_INT_LIT16:
1945 op = kOpOr;
1946 break;
1947 case Instruction::XOR_INT:
1948 case Instruction::XOR_INT_2ADDR:
1949 case Instruction::XOR_INT_LIT8:
1950 case Instruction::XOR_INT_LIT16:
1951 op = kOpXor;
1952 break;
1953 case Instruction::SHL_INT_LIT8:
1954 case Instruction::SHL_INT:
1955 case Instruction::SHL_INT_2ADDR:
1956 lit &= 31;
1957 shift_op = true;
1958 op = kOpLsl;
1959 break;
1960 case Instruction::SHR_INT_LIT8:
1961 case Instruction::SHR_INT:
1962 case Instruction::SHR_INT_2ADDR:
1963 lit &= 31;
1964 shift_op = true;
1965 op = kOpAsr;
1966 break;
1967 case Instruction::USHR_INT_LIT8:
1968 case Instruction::USHR_INT:
1969 case Instruction::USHR_INT_2ADDR:
1970 lit &= 31;
1971 shift_op = true;
1972 op = kOpLsr;
1973 break;
1974
1975 case Instruction::DIV_INT:
1976 case Instruction::DIV_INT_2ADDR:
1977 case Instruction::DIV_INT_LIT8:
1978 case Instruction::DIV_INT_LIT16:
1979 case Instruction::REM_INT:
1980 case Instruction::REM_INT_2ADDR:
1981 case Instruction::REM_INT_LIT8:
1982 case Instruction::REM_INT_LIT16: {
1983 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001984 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 return;
1986 }
buzbee11b63d12013-08-27 07:34:17 -07001987 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001989 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 (opcode == Instruction::DIV_INT_LIT16)) {
1991 is_div = true;
1992 } else {
1993 is_div = false;
1994 }
buzbee11b63d12013-08-27 07:34:17 -07001995 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1996 return;
1997 }
Dave Allison70202782013-10-22 17:52:19 -07001998
1999 bool done = false;
Maja Gagic6ea651f2015-02-24 16:55:04 +01002000 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64 ||
2001 cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002002 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08002003 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07002004 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07002005 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08002006 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
2007 done = true;
Dave Allison70202782013-10-22 17:52:19 -07002008 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08002009 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002010 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07002011 // Use ARM SDIV instruction for division. For remainder we also need to
2012 // calculate using a MUL and subtract.
2013 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08002014 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07002015 done = true;
2016 }
2017 }
2018
2019 if (!done) {
2020 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07002021 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
2022 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07002023 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07002025 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026 else
2027 rl_result = GetReturnAlt();
2028 }
2029 StoreValue(rl_dest, rl_result);
2030 return;
2031 }
2032 default:
2033 LOG(FATAL) << "Unexpected opcode " << opcode;
2034 }
2035 rl_src = LoadValue(rl_src, kCoreReg);
2036 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07002037 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08002039 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 } else {
buzbee2700f7e2014-03-07 09:46:20 -08002041 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 }
2043 StoreValue(rl_dest, rl_result);
2044}
2045
Andreas Gampe98430592014-07-27 19:44:50 -07002046void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002047 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 RegLocation rl_result;
2049 OpKind first_op = kOpBkpt;
2050 OpKind second_op = kOpBkpt;
2051 bool call_out = false;
2052 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002053 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2054 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055
2056 switch (opcode) {
2057 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002058 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2059 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002061 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002062 RegStorage t_reg = AllocTemp();
2063 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2064 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2065 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2066 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002068 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2069 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 }
Andreas Gampe98430592014-07-27 19:44:50 -07002071 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 return;
2073 case Instruction::ADD_LONG:
2074 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002075 first_op = kOpAdd;
2076 second_op = kOpAdc;
2077 break;
2078 case Instruction::SUB_LONG:
2079 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 first_op = kOpSub;
2081 second_op = kOpSbc;
2082 break;
2083 case Instruction::MUL_LONG:
2084 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002085 call_out = true;
2086 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2087 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 break;
2089 case Instruction::DIV_LONG:
2090 case Instruction::DIV_LONG_2ADDR:
2091 call_out = true;
2092 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002093 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2094 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 break;
2096 case Instruction::REM_LONG:
2097 case Instruction::REM_LONG_2ADDR:
2098 call_out = true;
2099 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002100 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002102 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2103 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 break;
2105 case Instruction::AND_LONG_2ADDR:
2106 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002107 first_op = kOpAnd;
2108 second_op = kOpAnd;
2109 break;
2110 case Instruction::OR_LONG:
2111 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 first_op = kOpOr;
2113 second_op = kOpOr;
2114 break;
2115 case Instruction::XOR_LONG:
2116 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117 first_op = kOpXor;
2118 second_op = kOpXor;
2119 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002120 default:
2121 LOG(FATAL) << "Invalid long arith op";
2122 }
2123 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002124 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002126 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002127 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002128 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2129 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2130 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2131 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002132 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2133 GenDivZeroCheckWide(r_tmp2);
2134 }
Andreas Gampe98430592014-07-27 19:44:50 -07002135 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002137 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002138 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002139 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002140 }
2141 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002142 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2143 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002144 else
Andreas Gampe98430592014-07-27 19:44:50 -07002145 rl_result = GetReturnWideAlt();
2146 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002147 }
2148}
2149
Mark Mendelle87f9b52014-04-30 14:13:18 -04002150void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2151 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2152 LoadConstantNoClobber(rl_result.reg, value);
2153 StoreValue(rl_dest, rl_result);
Mark Mendelle87f9b52014-04-30 14:13:18 -04002154}
2155
Andreas Gampe98430592014-07-27 19:44:50 -07002156void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2157 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002158 /*
2159 * Don't optimize the register usage since it calls out to support
2160 * functions
2161 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002162
Brian Carlstrom7940e442013-07-12 13:46:57 -07002163 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002164 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002165 if (rl_dest.wide) {
2166 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002167 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002168 StoreValueWide(rl_dest, rl_result);
2169 } else {
2170 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002171 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002172 StoreValue(rl_dest, rl_result);
2173 }
2174}
2175
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002176class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002177 public:
2178 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +00002179 : LIRSlowPath(m2l, branch, cont) {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002180 }
2181
2182 void Compile() OVERRIDE {
2183 m2l_->ResetRegPool();
2184 m2l_->ResetDefTracking();
2185 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002186 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002187 if (cont_ != nullptr) {
2188 m2l_->OpUnconditionalBranch(cont_);
2189 }
2190 }
2191};
2192
Brian Carlstrom7940e442013-07-12 13:46:57 -07002193/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002194void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002195 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2196 return;
2197 }
Dave Allison69dfe512014-07-11 17:11:58 +00002198 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002199 FlushAllRegs();
2200 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002201 LIR* cont = NewLIR0(kPseudoTargetLabel);
2202 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002203 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002204 FlushAllRegs(); // TODO: needed?
2205 LIR* inst = CheckSuspendUsingLoad();
2206 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002207 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002208}
2209
2210/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002211void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002212 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2213 OpUnconditionalBranch(target);
2214 return;
2215 }
Dave Allison69dfe512014-07-11 17:11:58 +00002216 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002217 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002218 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002219 LIR* branch = OpUnconditionalBranch(nullptr);
2220 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002221 } else {
2222 // For the implicit suspend check, just perform the trigger
2223 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002224 FlushAllRegs();
2225 LIR* inst = CheckSuspendUsingLoad();
2226 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002227 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002228 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002229}
2230
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002231/* Call out to helper assembly routine that will null check obj and then lock it. */
2232void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002233 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002234 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002235 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002236}
2237
2238/* Call out to helper assembly routine that will null check obj and then unlock it. */
2239void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002240 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002241 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002242 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002243}
2244
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002245/* Generic code for generating a wide constant into a VR. */
2246void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2247 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002248 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002249 StoreValueWide(rl_dest, rl_result);
2250}
2251
Andreas Gampe48971b32014-08-06 10:09:01 -07002252void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002253 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2254 DCHECK(bb != nullptr);
2255 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002256 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002257 const uint16_t entries = table[1];
2258 // Chained cmp-and-branch.
2259 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002260 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002261 rl_src = LoadValue(rl_src, kCoreReg);
2262 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002263 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002264 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002265 // Switch to using a temp and add.
2266 break;
2267 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002268 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2269 DCHECK(successor_block_info != nullptr);
2270 int case_block_id = successor_block_info->block;
2271 DCHECK_EQ(starting_key + i, successor_block_info->key);
2272 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002273 }
2274 if (i < entries) {
2275 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2276 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2277 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002278 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002279 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2280 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2281 DCHECK(successor_block_info != nullptr);
2282 int case_block_id = successor_block_info->block;
2283 DCHECK_EQ(starting_key + i, successor_block_info->key);
2284 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002285 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2286 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002287 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2288 DCHECK(successor_block_info != nullptr);
2289 int case_block_id = successor_block_info->block;
2290 DCHECK_EQ(starting_key + i, successor_block_info->key);
2291 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002292 } else {
2293 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002294 for (; i < entries; ++i, ++succ_bb_iter) {
2295 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2296 DCHECK(successor_block_info != nullptr);
2297 int case_block_id = successor_block_info->block;
2298 DCHECK_EQ(starting_key + i, successor_block_info->key);
2299 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002300 }
2301 }
2302 }
2303}
2304
2305void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002306 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002307 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002308 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002309 }
2310
2311 const uint16_t entries = table[1];
2312 if (entries <= kSmallSwitchThreshold) {
2313 GenSmallPackedSwitch(mir, table_offset, rl_src);
2314 } else {
2315 // Use the backend-specific implementation.
2316 GenLargePackedSwitch(mir, table_offset, rl_src);
2317 }
2318}
2319
2320void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002321 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2322 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002323 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002324 const uint16_t entries = table[1];
2325 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002326 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002327 int i = 0;
2328 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2329 int case_block_id = successor_block_info->block;
2330 int key = successor_block_info->key;
2331 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2332 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002333 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002334 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002335}
2336
2337void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002338 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002339 if (cu_->verbose) {
2340 DumpSparseSwitchTable(table);
2341 }
2342
2343 const uint16_t entries = table[1];
2344 if (entries <= kSmallSwitchThreshold) {
2345 GenSmallSparseSwitch(mir, table_offset, rl_src);
2346 } else {
2347 // Use the backend-specific implementation.
2348 GenLargeSparseSwitch(mir, table_offset, rl_src);
2349 }
2350}
2351
Fred Shih37f05ef2014-07-16 18:38:08 -07002352bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2353 switch (size) {
2354 case kReference:
2355 return type == Primitive::kPrimNot;
2356 case k64:
2357 case kDouble:
2358 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2359 case k32:
2360 case kSingle:
2361 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2362 case kSignedHalf:
2363 return type == Primitive::kPrimShort;
2364 case kUnsignedHalf:
2365 return type == Primitive::kPrimChar;
2366 case kSignedByte:
2367 return type == Primitive::kPrimByte;
2368 case kUnsignedByte:
2369 return type == Primitive::kPrimBoolean;
2370 case kWord: // Intentional fallthrough.
2371 default:
2372 return false; // There are no sane types with this op size.
2373 }
2374}
2375
Brian Carlstrom7940e442013-07-12 13:46:57 -07002376} // namespace art