blob: afae89d150599b79f2a2d2dcb3451839d9e4213c [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)
89 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), 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)
108 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
109 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)
132 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
133 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)
162 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
163 }
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:
485 r_val = AllocTemp();
486 break;
487 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
488 }
489 // Set up source pointer
490 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700491 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700493 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 mirror::Array::DataOffset(component_size).Int32Value());
495 // Set up the loop counter (known to be > 0)
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000496 LoadConstant(r_idx, static_cast<int>(elems - 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800498 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100500 {
501 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
502 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
503 // NOTE: No dalvik register annotation, local optimizations will be stopped
504 // by the loop boundaries.
505 }
buzbee695d13a2014-04-19 13:32:20 -0700506 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800508 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700509 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700511 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 -mirror::Array::DataOffset(component_size).Int32Value());
513 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000514 FreeTemp(r_idx);
515 FreeTemp(r_dst);
516 FreeTemp(r_src);
517 } else {
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000518 DCHECK_LE(elems, 5u); // Usually but not necessarily non-range.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 // TUNING: interleave
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000520 for (size_t i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600521 RegLocation rl_arg;
522 if (info->args[i].ref) {
523 rl_arg = LoadValue(info->args[i], kRefReg);
524 StoreRefDisp(ref_reg,
525 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
526 kNotVolatile);
527 } else {
528 rl_arg = LoadValue(info->args[i], kCoreReg);
529 Store32Disp(ref_reg,
530 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
531 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800533 if (IsTemp(rl_arg.reg)) {
534 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 }
536 }
537 }
Vladimir Markobf535be2014-11-19 18:52:35 +0000538 if (elems != 0 && info->args[0].ref) {
539 // If there is at least one potentially non-null value, unconditionally mark the GC card.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000540 for (size_t i = 0; i < elems; i++) {
Vladimir Markobf535be2014-11-19 18:52:35 +0000541 if (!mir_graph_->IsConstantNullRef(info->args[i])) {
542 UnconditionallyMarkGCCard(ref_reg);
543 break;
544 }
545 }
546 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700548 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 }
550}
551
Ian Rogers832336b2014-10-08 15:35:22 -0700552/*
553 * Array data table format:
554 * ushort ident = 0x0300 magic value
555 * ushort width width of each element in the table
556 * uint size number of elements in the table
557 * ubyte data[size*width] table of data values (may contain a single-byte
558 * padding at the end)
559 *
560 * Total size is 4+(width * size + 1)/2 16-bit code units.
561 */
562void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
563 if (kIsDebugBuild) {
564 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
565 const Instruction::ArrayDataPayload* payload =
566 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
567 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
568 }
569 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
570 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
571}
572
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800573//
574// Slow path to ensure a class is initialized for sget/sput.
575//
576class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
577 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100578 // There are up to two branches to the static field slow path, the "unresolved" when the type
579 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
580 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800581 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100582 RegStorage r_base)
583 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
584 second_branch_(unresolved != nullptr ? uninit : nullptr),
585 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800586 }
587
588 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100589 LIR* target = GenerateTargetLabel();
590 if (second_branch_ != nullptr) {
591 second_branch_->target = target;
592 }
Andreas Gampe98430592014-07-27 19:44:50 -0700593 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800594 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700595 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800596
597 m2l_->OpUnconditionalBranch(cont_);
598 }
599
600 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100601 // Second branch to the slow path, or null if there's only one branch.
602 LIR* const second_branch_;
603
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800604 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800605 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800606};
607
Fred Shih37f05ef2014-07-16 18:38:08 -0700608void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000609 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000610 DCHECK_EQ(SPutMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000611 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800612 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800614 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000615 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100617 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700618 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000619 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
620 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800621 if (IsTemp(rl_method.reg)) {
622 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
624 } else {
625 // Medium path, static storage base in a different class which requires checks that the other
626 // class is initialized.
627 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000628 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 // May do runtime call so everything to home locations.
630 FlushAllRegs();
631 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700632 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 LockTemp(r_method);
634 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700635 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800636 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000637 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
638 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000639 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000640 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800641 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100642 LIR* unresolved_branch = nullptr;
643 if (!field_info.IsClassInDexCache() &&
644 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
645 // Check if r_base is NULL.
646 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
647 }
648 LIR* uninit_branch = nullptr;
649 if (!field_info.IsClassInitialized() &&
650 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
651 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700652 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800653 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100654 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800655 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000656 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100657 FreeTemp(r_tmp);
658 }
659 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
660 // The slow path is invoked if the r_base is NULL or the class pointed
661 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800662 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800663 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000664 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800665
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100666 if (uninit_branch != nullptr) {
667 // Ensure load of status and store of value don't re-order.
668 // TODO: Presumably the actual value store is control-dependent on the status load,
669 // and will thus not be reordered in any case, since stores are never speculated.
670 // Does later code "know" that the class is now initialized? If so, we still
671 // need the barrier to guard later static loads.
672 GenMemBarrier(kLoadAny);
673 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 FreeTemp(r_method);
676 }
677 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700678 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
679 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100680 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100682 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700684 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000685 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
686 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100687 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700688 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000689 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700691 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000692 MarkGCCard(mir->optimization_flags, rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800694 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 } else {
696 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700697 QuickEntrypointEnum target;
698 switch (size) {
699 case kReference:
700 target = kQuickSetObjStatic;
701 break;
702 case k64:
703 case kDouble:
704 target = kQuickSet64Static;
705 break;
706 case k32:
707 case kSingle:
708 target = kQuickSet32Static;
709 break;
710 case kSignedHalf:
711 case kUnsignedHalf:
712 target = kQuickSet16Static;
713 break;
714 case kSignedByte:
715 case kUnsignedByte:
716 target = kQuickSet8Static;
717 break;
718 case kWord: // Intentional fallthrough.
719 default:
720 LOG(FATAL) << "Can't determine entrypoint for: " << size;
721 target = kQuickSet32Static;
722 }
Andreas Gampe98430592014-07-27 19:44:50 -0700723 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
725}
726
Fred Shih37f05ef2014-07-16 18:38:08 -0700727void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000729 DCHECK_EQ(SGetMemAccessType(mir->dalvikInsn.opcode), field_info.MemAccessType());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000730 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700731
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800732 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000733 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800734 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000735 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 // Fast path, static storage base is this method's class
737 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700738 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000739 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
740 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 } else {
742 // Medium path, static storage base in a different class which requires checks that the other
743 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000744 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 // May do runtime call so everything to home locations.
746 FlushAllRegs();
747 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700748 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 LockTemp(r_method);
750 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700751 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800752 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000753 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
754 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000755 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000756 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800757 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100758 LIR* unresolved_branch = nullptr;
759 if (!field_info.IsClassInDexCache() &&
760 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
761 // Check if r_base is NULL.
762 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
763 }
764 LIR* uninit_branch = nullptr;
765 if (!field_info.IsClassInitialized() &&
766 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
767 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700768 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800769 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100770 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800771 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000772 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100773 FreeTemp(r_tmp);
774 }
775 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
776 // The slow path is invoked if the r_base is NULL or the class pointed
777 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800778 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800779 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000780 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800781
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100782 if (uninit_branch != nullptr) {
783 // Ensure load of status and load of value don't re-order.
784 GenMemBarrier(kLoadAny);
785 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 FreeTemp(r_method);
788 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800789 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700790 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100791 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800792
Vladimir Marko674744e2014-04-24 15:18:26 +0100793 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700794 if (IsRef(size)) {
795 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000796 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
797 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100798 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700799 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000800 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800801 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100802 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800803
Fred Shih37f05ef2014-07-16 18:38:08 -0700804 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 StoreValueWide(rl_dest, rl_result);
806 } else {
807 StoreValue(rl_dest, rl_result);
808 }
809 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700810 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700812 QuickEntrypointEnum target;
813 switch (type) {
814 case Primitive::kPrimNot:
815 target = kQuickGetObjStatic;
816 break;
817 case Primitive::kPrimLong:
818 case Primitive::kPrimDouble:
819 target = kQuickGet64Static;
820 break;
821 case Primitive::kPrimInt:
822 case Primitive::kPrimFloat:
823 target = kQuickGet32Static;
824 break;
825 case Primitive::kPrimShort:
826 target = kQuickGetShortStatic;
827 break;
828 case Primitive::kPrimChar:
829 target = kQuickGetCharStatic;
830 break;
831 case Primitive::kPrimByte:
832 target = kQuickGetByteStatic;
833 break;
834 case Primitive::kPrimBoolean:
835 target = kQuickGetBooleanStatic;
836 break;
837 case Primitive::kPrimVoid: // Intentional fallthrough.
838 default:
839 LOG(FATAL) << "Can't determine entrypoint for: " << type;
840 target = kQuickGet32Static;
841 }
Andreas Gampe98430592014-07-27 19:44:50 -0700842 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
843
Douglas Leung2db3e262014-06-25 16:02:55 -0700844 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700845 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700846 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 StoreValueWide(rl_dest, rl_result);
848 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700849 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 StoreValue(rl_dest, rl_result);
851 }
852 }
853}
854
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800855// Generate code for all slow paths.
856void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700857 // We should check slow_paths_.Size() every time, because a new slow path
858 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100859 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800860 slowpath->Compile();
861 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100862 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800863}
864
Fred Shih37f05ef2014-07-16 18:38:08 -0700865void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
866 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000867 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800868 if (kIsDebugBuild) {
869 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
870 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
871 IGetMemAccessType(mir->dalvikInsn.opcode);
872 DCHECK_EQ(mem_access_type, field_info.MemAccessType()) << mir->dalvikInsn.opcode;
873 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000874 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800875 if (!ForceSlowFieldPath(cu_) && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700876 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700877 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000878 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700879 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100880 GenNullCheck(rl_obj.reg, opt_flags);
881 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
882 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000883 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700884 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000885 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
886 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100887 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700888 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000889 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100890 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000891 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700892 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 StoreValueWide(rl_dest, rl_result);
894 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 StoreValue(rl_dest, rl_result);
896 }
897 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700898 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
899 QuickEntrypointEnum target;
900 switch (type) {
901 case Primitive::kPrimNot:
902 target = kQuickGetObjInstance;
903 break;
904 case Primitive::kPrimLong:
905 case Primitive::kPrimDouble:
906 target = kQuickGet64Instance;
907 break;
908 case Primitive::kPrimFloat:
909 case Primitive::kPrimInt:
910 target = kQuickGet32Instance;
911 break;
912 case Primitive::kPrimShort:
913 target = kQuickGetShortInstance;
914 break;
915 case Primitive::kPrimChar:
916 target = kQuickGetCharInstance;
917 break;
918 case Primitive::kPrimByte:
919 target = kQuickGetByteInstance;
920 break;
921 case Primitive::kPrimBoolean:
922 target = kQuickGetBooleanInstance;
923 break;
924 case Primitive::kPrimVoid: // Intentional fallthrough.
925 default:
926 LOG(FATAL) << "Can't determine entrypoint for: " << type;
927 target = kQuickGet32Instance;
928 }
Andreas Gampe98430592014-07-27 19:44:50 -0700929 // Second argument of pGetXXInstance is always a reference.
930 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
931 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
932
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700933 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700934 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700935 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 StoreValueWide(rl_dest, rl_result);
937 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700938 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 StoreValue(rl_dest, rl_result);
940 }
941 }
942}
943
Vladimir Markobe0e5462014-02-26 11:24:15 +0000944void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700945 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000946 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800947 if (kIsDebugBuild) {
948 auto mem_access_type = IsInstructionIGetQuickOrIPutQuick(mir->dalvikInsn.opcode) ?
949 IGetQuickOrIPutQuickMemAccessType(mir->dalvikInsn.opcode) :
950 IPutMemAccessType(mir->dalvikInsn.opcode);
951 DCHECK_EQ(mem_access_type, field_info.MemAccessType());
952 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000953 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800954 if (!ForceSlowFieldPath(cu_) && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700955 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700956 // Dex code never writes to the class field.
957 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
958 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700959 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700960 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100961 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 } else {
963 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100964 }
965 GenNullCheck(rl_obj.reg, opt_flags);
966 int field_offset = field_info.FieldOffset().Int32Value();
Vladimir Markoee5e2732015-01-13 17:34:28 +0000967 LIR* null_ck_insn;
Fred Shih37f05ef2014-07-16 18:38:08 -0700968 if (IsRef(size)) {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000969 null_ck_insn = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000970 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100971 } else {
Vladimir Markoee5e2732015-01-13 17:34:28 +0000972 null_ck_insn = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
973 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100974 }
Vladimir Markoee5e2732015-01-13 17:34:28 +0000975 MarkPossibleNullPointerExceptionAfter(opt_flags, null_ck_insn);
Fred Shih37f05ef2014-07-16 18:38:08 -0700976 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko743b98c2014-11-24 19:45:41 +0000977 MarkGCCard(opt_flags, rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 }
979 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700980 QuickEntrypointEnum target;
981 switch (size) {
982 case kReference:
983 target = kQuickSetObjInstance;
984 break;
985 case k64:
986 case kDouble:
987 target = kQuickSet64Instance;
988 break;
989 case k32:
990 case kSingle:
991 target = kQuickSet32Instance;
992 break;
993 case kSignedHalf:
994 case kUnsignedHalf:
995 target = kQuickSet16Instance;
996 break;
997 case kSignedByte:
998 case kUnsignedByte:
999 target = kQuickSet8Instance;
1000 break;
1001 case kWord: // Intentional fallthrough.
1002 default:
1003 LOG(FATAL) << "Can't determine entrypoint for: " << size;
1004 target = kQuickSet32Instance;
1005 }
Andreas Gampe98430592014-07-27 19:44:50 -07001006 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
1007 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 }
1009}
1010
Ian Rogersa9a82542013-10-04 11:17:26 -07001011void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
1012 RegLocation rl_src) {
1013 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
1014 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
1015 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -07001016 QuickEntrypointEnum target = needs_range_check
1017 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
1018 : kQuickAputObjectWithBoundCheck)
1019 : kQuickAputObject;
1020 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -07001021}
1022
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001023void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001025 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -07001026 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001028 *cu_->dex_file,
1029 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 // Call out to helper which resolves type and verifies access.
1031 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -07001032 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -07001033 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 StoreValue(rl_dest, rl_result);
1035 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001036 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 // We're don't need access checks, load type from dex cache
1038 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -07001039 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001040 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001041 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001042 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001044 type_idx) || ForceSlowTypePath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 // Slow path, at runtime test if type is null and if so initialize
1046 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -08001047 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001048 LIR* cont = NewLIR0(kPseudoTargetLabel);
1049
1050 // Object to generate the slow path for class resolution.
1051 class SlowPath : public LIRSlowPath {
1052 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001053 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1054 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
1055 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1056 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001057 }
1058
1059 void Compile() {
1060 GenerateTargetLabel();
1061
Andreas Gampe98430592014-07-27 19:44:50 -07001062 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001063 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001064 m2l_->OpUnconditionalBranch(cont_);
1065 }
1066
1067 private:
1068 const int type_idx_;
1069 const RegLocation rl_method_;
1070 const RegLocation rl_result_;
1071 };
1072
1073 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001074 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001075
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001077 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 // Fast path, we're done - just store result
1079 StoreValue(rl_dest, rl_result);
1080 }
1081 }
1082}
1083
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001084void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001086 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1087 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001089 *cu_->dex_file, string_idx) || ForceSlowStringPath(cu_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 // slow path, resolve string if not in dex cache
1091 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001092 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001093
1094 // If the Method* is already in a register, we can save a copy.
1095 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001096 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001097 if (rl_method.location == kLocPhysReg) {
1098 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001099 DCHECK(!IsTemp(rl_method.reg));
1100 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001101 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001102 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001103 LoadCurrMethodDirect(r_method);
1104 }
Mathieu Chartiereace4582014-11-24 18:29:54 -08001105 // Method to declaring class.
1106 LoadRefDisp(r_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1107 TargetReg(kArg0, kRef), kNotVolatile);
1108 // Declaring class to dex cache strings.
1109 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Class::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001110 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001111
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001113 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1114 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001115 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001116
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001117 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001118 // Object to generate the slow path for string resolution.
1119 class SlowPath : public LIRSlowPath {
1120 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001121 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1122 int32_t string_idx_in) :
1123 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1124 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001125 }
1126
1127 void Compile() {
1128 GenerateTargetLabel();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001129 m2l_->CallRuntimeHelperImmReg(kQuickResolveString, string_idx_, r_method_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001130 m2l_->OpUnconditionalBranch(cont_);
1131 }
1132
1133 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001134 const RegStorage r_method_;
1135 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001136 };
1137
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001138 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001140
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001142 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 } else {
1144 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001145 RegStorage res_reg = AllocTempRef();
1146 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Mathieu Chartiereace4582014-11-24 18:29:54 -08001147 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), res_reg,
1148 kNotVolatile);
1149 LoadRefDisp(res_reg, mirror::Class::DexCacheStringsOffset().Int32Value(), res_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +00001150 kNotVolatile);
1151 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 StoreValue(rl_dest, rl_result);
1153 }
1154}
1155
Andreas Gampe98430592014-07-27 19:44:50 -07001156/*
1157 * Let helper function take care of everything. Will
1158 * call Class::NewInstanceFromCode(type_idx, method);
1159 */
1160void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1161 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 // alloc will always check for resolution, do we also need to verify
1163 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001164 const DexFile* dex_file = cu_->dex_file;
1165 CompilerDriver* driver = cu_->compiler_driver;
1166 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001167 bool is_type_initialized;
1168 bool use_direct_type_ptr;
1169 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001170 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001171 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001172 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1173 &direct_type_ptr, &is_finalizable) &&
1174 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001175 // The fast path.
1176 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001177 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001178 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001179 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001180 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001181 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001182 }
1183 } else {
1184 // Use the direct pointer.
1185 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001186 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001187 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001188 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001189 }
1190 }
1191 } else {
1192 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001193 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001194 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001196 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 }
Andreas Gampe98430592014-07-27 19:44:50 -07001198 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199}
1200
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001201void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001203 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204}
1205
1206// For final classes there are no sub-classes to check and so we can answer the instance-of
1207// question with simple comparisons.
1208void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1209 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001210 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001211 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001212
buzbeea0cd2d72014-06-01 09:33:49 -07001213 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001215 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001216 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001218 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 }
1220 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001221 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222
buzbeea0cd2d72014-06-01 09:33:49 -07001223 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1224 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225
1226 LoadCurrMethodDirect(check_class);
1227 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001228 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1229 kNotVolatile);
1230 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1231 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 } else {
buzbee695d13a2014-04-19 13:32:20 -07001233 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001234 check_class, kNotVolatile);
1235 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1236 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001237 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001238 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240
buzbee695d13a2014-04-19 13:32:20 -07001241 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001242 if (cu_->instruction_set == kThumb2) {
1243 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001244 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001246 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001248 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 }
1250 LIR* target = NewLIR0(kPseudoTargetLabel);
1251 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 FreeTemp(object_class);
1253 FreeTemp(check_class);
1254 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001255 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 FreeTemp(result_reg);
1257 }
1258 StoreValue(rl_dest, rl_result);
1259}
1260
1261void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1262 bool type_known_abstract, bool use_declaring_class,
1263 bool can_assume_type_is_in_dex_cache,
1264 uint32_t type_idx, RegLocation rl_dest,
1265 RegLocation rl_src) {
1266 FlushAllRegs();
1267 // May generate a call - use explicit registers
1268 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001269 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001270 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001271 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001272 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1273 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 if (needs_access_check) {
1275 // Check we have access to type_idx and if not throw IllegalAccessError,
1276 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001277 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001278 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1279 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001281 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001282 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001283 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001285 if (can_assume_type_is_in_dex_cache) {
1286 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001287 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001288 }
1289
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001291 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001292 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001293 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001294 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001296 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1297 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1298
1299 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001300 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001301
1302 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1303 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001304 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1305 RegLocation rl_src_in)
1306 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1307 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001308 }
1309
1310 void Compile() OVERRIDE {
1311 GenerateTargetLabel();
1312
Andreas Gampe98430592014-07-27 19:44:50 -07001313 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001314 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1315 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001316 m2l_->OpUnconditionalBranch(cont_);
1317 }
1318
1319 private:
1320 uint32_t type_idx_;
1321 RegLocation rl_src_;
1322 };
1323
1324 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1325 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 }
1327 }
1328 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001329 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001330 if (!IsSameReg(rl_result.reg, ref_reg)) {
1331 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001332 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001334 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335
1336 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001337 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001339 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1340 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1342 LIR* branchover = NULL;
1343 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001344 // rl_result == ref == class.
1345 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001346 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 } else {
1348 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001349 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001350 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 if (!type_known_abstract) {
1352 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001353 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001354 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001355 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001357 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001359 if (it != nullptr) {
1360 OpEndIT(it);
1361 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 FreeTemp(r_tgt);
1363 } else {
1364 if (!type_known_abstract) {
1365 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001366 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001367 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001369
Serguei Katkov9ee45192014-07-17 14:39:03 +07001370 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001371 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 }
1373 }
1374 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001375 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 /* branch targets here */
1377 LIR* target = NewLIR0(kPseudoTargetLabel);
1378 StoreValue(rl_dest, rl_result);
1379 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001380 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 branchover->target = target;
1382 }
1383}
1384
1385void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1386 bool type_known_final, type_known_abstract, use_declaring_class;
1387 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1388 *cu_->dex_file,
1389 type_idx,
1390 &type_known_final,
1391 &type_known_abstract,
1392 &use_declaring_class);
1393 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1394 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1395
1396 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1397 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1398 } else {
1399 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1400 use_declaring_class, can_assume_type_is_in_dex_cache,
1401 type_idx, rl_dest, rl_src);
1402 }
1403}
1404
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001405void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 bool type_known_final, type_known_abstract, use_declaring_class;
1407 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1408 *cu_->dex_file,
1409 type_idx,
1410 &type_known_final,
1411 &type_known_abstract,
1412 &use_declaring_class);
1413 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1414 // of the exception throw path.
1415 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001416 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 // Verifier type analysis proved this check cast would never cause an exception.
1418 return;
1419 }
1420 FlushAllRegs();
1421 // May generate a call - use explicit registers
1422 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001423 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001424 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001425 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 if (needs_access_check) {
1427 // Check we have access to type_idx and if not throw IllegalAccessError,
1428 // returns Class* in kRet0
1429 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001430 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001431 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001433 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001434 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001435 } else {
1436 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001437 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001438 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001439 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001440 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1442 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001443 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1444 LIR* cont = NewLIR0(kPseudoTargetLabel);
1445
1446 // Slow path to initialize the type. Executed if the type is NULL.
1447 class SlowPath : public LIRSlowPath {
1448 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001449 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1450 const RegStorage class_reg_in) :
1451 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1452 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001453 }
1454
1455 void Compile() {
1456 GenerateTargetLabel();
1457
1458 // Call out to helper, which will return resolved type in kArg0
1459 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001460 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1461 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001462 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001463 m2l_->OpUnconditionalBranch(cont_);
1464 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001465
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001466 public:
1467 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001468 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001469 };
1470
buzbee2700f7e2014-03-07 09:46:20 -08001471 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 }
1473 }
1474 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001475 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001476
1477 // Slow path for the case where the classes are not equal. In this case we need
1478 // to call a helper function to do the check.
1479 class SlowPath : public LIRSlowPath {
1480 public:
1481 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1482 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1483 }
1484
1485 void Compile() {
1486 GenerateTargetLabel();
1487
1488 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001489 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1490 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001491 }
Andreas Gampe98430592014-07-27 19:44:50 -07001492 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1493 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001494 m2l_->OpUnconditionalBranch(cont_);
1495 }
1496
1497 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001498 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001499 };
1500
1501 if (type_known_abstract) {
1502 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001503 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001504 LIR* cont = NewLIR0(kPseudoTargetLabel);
1505 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1506 } else {
1507 // Harder, more common case. We need to generate a forward branch over the load
1508 // if the target is null. If it's non-null we perform the load and branch to the
1509 // slow path if the classes are not equal.
1510
1511 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001512 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001513 /* load object->klass_ */
1514 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001515 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1516 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001517
Andreas Gampeccc60262014-07-04 18:02:38 -07001518 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001519 LIR* cont = NewLIR0(kPseudoTargetLabel);
1520
1521 // Add the slow path that will not perform load since this is already done.
1522 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1523
1524 // Set the null check to branch to the continuation.
1525 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 }
1527}
1528
1529void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001530 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 RegLocation rl_result;
1532 if (cu_->instruction_set == kThumb2) {
1533 /*
1534 * NOTE: This is the one place in the code in which we might have
1535 * as many as six live temporary registers. There are 5 in the normal
1536 * set for Arm. Until we have spill capabilities, temporarily add
1537 * lr to the temp set. It is safe to do this locally, but note that
1538 * lr is used explicitly elsewhere in the code generator and cannot
1539 * normally be used as a general temp register.
1540 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001541 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1542 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 }
1544 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1545 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1546 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1547 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001548 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1549 RegStorage t_reg = AllocTemp();
1550 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1551 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1552 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 FreeTemp(t_reg);
1554 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001555 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1556 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 }
1558 /*
1559 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1560 * following StoreValueWide might need to allocate a temp register.
1561 * To further work around the lack of a spill capability, explicitly
1562 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1563 * Remove when spill is functional.
1564 */
1565 FreeRegLocTemps(rl_result, rl_src1);
1566 FreeRegLocTemps(rl_result, rl_src2);
1567 StoreValueWide(rl_dest, rl_result);
1568 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001569 Clobber(TargetReg(kLr, kNotWide));
1570 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 }
1572}
1573
Andreas Gampe98430592014-07-27 19:44:50 -07001574void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1575 RegLocation rl_src1, RegLocation rl_shift) {
1576 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 switch (opcode) {
1578 case Instruction::SHL_LONG:
1579 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001580 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 break;
1582 case Instruction::SHR_LONG:
1583 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001584 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 break;
1586 case Instruction::USHR_LONG:
1587 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001588 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 break;
1590 default:
1591 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001592 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 }
Andreas Gampe98430592014-07-27 19:44:50 -07001594 FlushAllRegs(); /* Send everything to home location */
1595 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001596 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 StoreValueWide(rl_dest, rl_result);
1598}
1599
1600
1601void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001602 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001603 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 OpKind op = kOpBkpt;
1605 bool is_div_rem = false;
1606 bool check_zero = false;
1607 bool unary = false;
1608 RegLocation rl_result;
1609 bool shift_op = false;
1610 switch (opcode) {
1611 case Instruction::NEG_INT:
1612 op = kOpNeg;
1613 unary = true;
1614 break;
1615 case Instruction::NOT_INT:
1616 op = kOpMvn;
1617 unary = true;
1618 break;
1619 case Instruction::ADD_INT:
1620 case Instruction::ADD_INT_2ADDR:
1621 op = kOpAdd;
1622 break;
1623 case Instruction::SUB_INT:
1624 case Instruction::SUB_INT_2ADDR:
1625 op = kOpSub;
1626 break;
1627 case Instruction::MUL_INT:
1628 case Instruction::MUL_INT_2ADDR:
1629 op = kOpMul;
1630 break;
1631 case Instruction::DIV_INT:
1632 case Instruction::DIV_INT_2ADDR:
1633 check_zero = true;
1634 op = kOpDiv;
1635 is_div_rem = true;
1636 break;
1637 /* NOTE: returns in kArg1 */
1638 case Instruction::REM_INT:
1639 case Instruction::REM_INT_2ADDR:
1640 check_zero = true;
1641 op = kOpRem;
1642 is_div_rem = true;
1643 break;
1644 case Instruction::AND_INT:
1645 case Instruction::AND_INT_2ADDR:
1646 op = kOpAnd;
1647 break;
1648 case Instruction::OR_INT:
1649 case Instruction::OR_INT_2ADDR:
1650 op = kOpOr;
1651 break;
1652 case Instruction::XOR_INT:
1653 case Instruction::XOR_INT_2ADDR:
1654 op = kOpXor;
1655 break;
1656 case Instruction::SHL_INT:
1657 case Instruction::SHL_INT_2ADDR:
1658 shift_op = true;
1659 op = kOpLsl;
1660 break;
1661 case Instruction::SHR_INT:
1662 case Instruction::SHR_INT_2ADDR:
1663 shift_op = true;
1664 op = kOpAsr;
1665 break;
1666 case Instruction::USHR_INT:
1667 case Instruction::USHR_INT_2ADDR:
1668 shift_op = true;
1669 op = kOpLsr;
1670 break;
1671 default:
1672 LOG(FATAL) << "Invalid word arith op: " << opcode;
1673 }
1674 if (!is_div_rem) {
1675 if (unary) {
1676 rl_src1 = LoadValue(rl_src1, kCoreReg);
1677 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001678 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001680 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001681 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001682 RegStorage t_reg = AllocTemp();
1683 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 rl_src1 = LoadValue(rl_src1, kCoreReg);
1685 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001686 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 FreeTemp(t_reg);
1688 } else {
1689 rl_src1 = LoadValue(rl_src1, kCoreReg);
1690 rl_src2 = LoadValue(rl_src2, kCoreReg);
1691 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001692 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 }
1694 }
1695 StoreValue(rl_dest, rl_result);
1696 } else {
Dave Allison70202782013-10-22 17:52:19 -07001697 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001698 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 rl_src1 = LoadValue(rl_src1, kCoreReg);
1700 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001701 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001702 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 }
buzbee2700f7e2014-03-07 09:46:20 -08001704 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001705 done = true;
1706 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001707 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001708 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001709 // Use ARM SDIV instruction for division. For remainder we also need to
1710 // calculate using a MUL and subtract.
1711 rl_src1 = LoadValue(rl_src1, kCoreReg);
1712 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001713 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001714 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001715 }
buzbee2700f7e2014-03-07 09:46:20 -08001716 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001717 done = true;
1718 }
1719 }
1720
1721 // If we haven't already generated the code use the callout function.
1722 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001724 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001725 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001726 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001727 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001728 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 }
Dave Allison70202782013-10-22 17:52:19 -07001730 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001731 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001733 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 else
1735 rl_result = GetReturnAlt();
1736 }
1737 StoreValue(rl_dest, rl_result);
1738 }
1739}
1740
1741/*
1742 * The following are the first-level codegen routines that analyze the format
1743 * of each bytecode then either dispatch special purpose codegen routines
1744 * or produce corresponding Thumb instructions directly.
1745 */
1746
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001748static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 x &= x - 1;
1750 return (x & (x - 1)) == 0;
1751}
1752
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1754// and store the result in 'rl_dest'.
Andreas Gamped500b532015-01-16 22:09:55 -08001755bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001756 RegLocation rl_src, RegLocation rl_dest, int lit) {
Andreas Gamped500b532015-01-16 22:09:55 -08001757 if ((lit < 2) || (!IsPowerOfTwo(lit))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 return false;
1759 }
Andreas Gampe7e499922015-01-06 08:28:12 -08001760 int k = CTZ(lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 if (k >= 30) {
1762 // Avoid special cases.
1763 return false;
1764 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 rl_src = LoadValue(rl_src, kCoreReg);
1766 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001767 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001768 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 if (lit == 2) {
1770 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001771 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1772 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1773 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001775 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001777 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1778 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001779 }
1780 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001781 RegStorage t_reg1 = AllocTemp();
1782 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001784 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1785 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001787 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001789 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001791 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001792 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001793 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 }
1795 }
1796 StoreValue(rl_dest, rl_result);
1797 return true;
1798}
1799
1800// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1801// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001802bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001803 if (lit < 0) {
1804 return false;
1805 }
1806 if (lit == 0) {
1807 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1808 LoadConstant(rl_result.reg, 0);
1809 StoreValue(rl_dest, rl_result);
1810 return true;
1811 }
1812 if (lit == 1) {
1813 rl_src = LoadValue(rl_src, kCoreReg);
1814 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1815 OpRegCopy(rl_result.reg, rl_src.reg);
1816 StoreValue(rl_dest, rl_result);
1817 return true;
1818 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001819 // There is RegRegRegShift on Arm, so check for more special cases
1820 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001821 return EasyMultiply(rl_src, rl_dest, lit);
1822 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001823 // Can we simplify this multiplication?
1824 bool power_of_two = false;
1825 bool pop_count_le2 = false;
1826 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001827 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828 power_of_two = true;
1829 } else if (IsPopCountLE2(lit)) {
1830 pop_count_le2 = true;
1831 } else if (IsPowerOfTwo(lit + 1)) {
1832 power_of_two_minus_one = true;
1833 } else {
1834 return false;
1835 }
1836 rl_src = LoadValue(rl_src, kCoreReg);
1837 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1838 if (power_of_two) {
1839 // Shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001840 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, CTZ(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 } else if (pop_count_le2) {
1842 // Shift and add and shift.
Andreas Gampe7e499922015-01-06 08:28:12 -08001843 int first_bit = CTZ(lit);
1844 int second_bit = CTZ(lit ^ (1 << first_bit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001845 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1846 } else {
1847 // Reverse subtract: (src << (shift + 1)) - src.
1848 DCHECK(power_of_two_minus_one);
Andreas Gampe7e499922015-01-06 08:28:12 -08001849 // TUNING: rsb dst, src, src lsl#CTZ(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001850 RegStorage t_reg = AllocTemp();
Andreas Gampe7e499922015-01-06 08:28:12 -08001851 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, CTZ(lit + 1));
buzbee2700f7e2014-03-07 09:46:20 -08001852 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001853 }
1854 StoreValue(rl_dest, rl_result);
1855 return true;
1856}
1857
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001858// Returns true if it generates instructions.
1859bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1860 RegLocation rl_src2) {
1861 if (!rl_src2.is_const ||
1862 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1863 return false;
1864 }
1865
1866 if (!rl_src2.wide) {
1867 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1868 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1869 // Generate multiply by reciprocal instead of div.
1870 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1871 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1872 return true;
1873 }
1874 } else {
1875 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1876 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1877 // Generate multiply by reciprocal instead of div.
1878 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1879 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1880 return true;
1881 }
1882 }
1883 return false;
1884}
1885
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001887 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001888 RegLocation rl_result;
1889 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1890 int shift_op = false;
1891 bool is_div = false;
1892
1893 switch (opcode) {
1894 case Instruction::RSUB_INT_LIT8:
1895 case Instruction::RSUB_INT: {
1896 rl_src = LoadValue(rl_src, kCoreReg);
1897 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1898 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001899 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001901 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1902 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001903 }
1904 StoreValue(rl_dest, rl_result);
1905 return;
1906 }
1907
1908 case Instruction::SUB_INT:
1909 case Instruction::SUB_INT_2ADDR:
1910 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001911 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001912 case Instruction::ADD_INT:
1913 case Instruction::ADD_INT_2ADDR:
1914 case Instruction::ADD_INT_LIT8:
1915 case Instruction::ADD_INT_LIT16:
1916 op = kOpAdd;
1917 break;
1918 case Instruction::MUL_INT:
1919 case Instruction::MUL_INT_2ADDR:
1920 case Instruction::MUL_INT_LIT8:
1921 case Instruction::MUL_INT_LIT16: {
1922 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1923 return;
1924 }
1925 op = kOpMul;
1926 break;
1927 }
1928 case Instruction::AND_INT:
1929 case Instruction::AND_INT_2ADDR:
1930 case Instruction::AND_INT_LIT8:
1931 case Instruction::AND_INT_LIT16:
1932 op = kOpAnd;
1933 break;
1934 case Instruction::OR_INT:
1935 case Instruction::OR_INT_2ADDR:
1936 case Instruction::OR_INT_LIT8:
1937 case Instruction::OR_INT_LIT16:
1938 op = kOpOr;
1939 break;
1940 case Instruction::XOR_INT:
1941 case Instruction::XOR_INT_2ADDR:
1942 case Instruction::XOR_INT_LIT8:
1943 case Instruction::XOR_INT_LIT16:
1944 op = kOpXor;
1945 break;
1946 case Instruction::SHL_INT_LIT8:
1947 case Instruction::SHL_INT:
1948 case Instruction::SHL_INT_2ADDR:
1949 lit &= 31;
1950 shift_op = true;
1951 op = kOpLsl;
1952 break;
1953 case Instruction::SHR_INT_LIT8:
1954 case Instruction::SHR_INT:
1955 case Instruction::SHR_INT_2ADDR:
1956 lit &= 31;
1957 shift_op = true;
1958 op = kOpAsr;
1959 break;
1960 case Instruction::USHR_INT_LIT8:
1961 case Instruction::USHR_INT:
1962 case Instruction::USHR_INT_2ADDR:
1963 lit &= 31;
1964 shift_op = true;
1965 op = kOpLsr;
1966 break;
1967
1968 case Instruction::DIV_INT:
1969 case Instruction::DIV_INT_2ADDR:
1970 case Instruction::DIV_INT_LIT8:
1971 case Instruction::DIV_INT_LIT16:
1972 case Instruction::REM_INT:
1973 case Instruction::REM_INT_2ADDR:
1974 case Instruction::REM_INT_LIT8:
1975 case Instruction::REM_INT_LIT16: {
1976 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001977 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978 return;
1979 }
buzbee11b63d12013-08-27 07:34:17 -07001980 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001982 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 (opcode == Instruction::DIV_INT_LIT16)) {
1984 is_div = true;
1985 } else {
1986 is_div = false;
1987 }
buzbee11b63d12013-08-27 07:34:17 -07001988 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1989 return;
1990 }
Dave Allison70202782013-10-22 17:52:19 -07001991
1992 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001993 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001995 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001996 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001997 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001998 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1999 done = true;
Dave Allison70202782013-10-22 17:52:19 -07002000 } else if (cu_->instruction_set == kThumb2) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08002001 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002002 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07002003 // Use ARM SDIV instruction for division. For remainder we also need to
2004 // calculate using a MUL and subtract.
2005 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08002006 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07002007 done = true;
2008 }
2009 }
2010
2011 if (!done) {
2012 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07002013 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
2014 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07002015 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07002017 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002018 else
2019 rl_result = GetReturnAlt();
2020 }
2021 StoreValue(rl_dest, rl_result);
2022 return;
2023 }
2024 default:
2025 LOG(FATAL) << "Unexpected opcode " << opcode;
2026 }
2027 rl_src = LoadValue(rl_src, kCoreReg);
2028 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07002029 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08002031 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 } else {
buzbee2700f7e2014-03-07 09:46:20 -08002033 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 }
2035 StoreValue(rl_dest, rl_result);
2036}
2037
Andreas Gampe98430592014-07-27 19:44:50 -07002038void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002039 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 RegLocation rl_result;
2041 OpKind first_op = kOpBkpt;
2042 OpKind second_op = kOpBkpt;
2043 bool call_out = false;
2044 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07002045 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2046 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047
2048 switch (opcode) {
2049 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07002050 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
2051 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002052 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002053 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07002054 RegStorage t_reg = AllocTemp();
2055 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2056 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2057 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2058 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002060 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2061 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002062 }
Andreas Gampe98430592014-07-27 19:44:50 -07002063 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 return;
2065 case Instruction::ADD_LONG:
2066 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 first_op = kOpAdd;
2068 second_op = kOpAdc;
2069 break;
2070 case Instruction::SUB_LONG:
2071 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 first_op = kOpSub;
2073 second_op = kOpSbc;
2074 break;
2075 case Instruction::MUL_LONG:
2076 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002077 call_out = true;
2078 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2079 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 break;
2081 case Instruction::DIV_LONG:
2082 case Instruction::DIV_LONG_2ADDR:
2083 call_out = true;
2084 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002085 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2086 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002087 break;
2088 case Instruction::REM_LONG:
2089 case Instruction::REM_LONG_2ADDR:
2090 call_out = true;
2091 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002092 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002094 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2095 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 break;
2097 case Instruction::AND_LONG_2ADDR:
2098 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002099 first_op = kOpAnd;
2100 second_op = kOpAnd;
2101 break;
2102 case Instruction::OR_LONG:
2103 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 first_op = kOpOr;
2105 second_op = kOpOr;
2106 break;
2107 case Instruction::XOR_LONG:
2108 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002109 first_op = kOpXor;
2110 second_op = kOpXor;
2111 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 default:
2113 LOG(FATAL) << "Invalid long arith op";
2114 }
2115 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002116 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002118 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002119 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002120 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2121 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2122 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2123 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002124 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2125 GenDivZeroCheckWide(r_tmp2);
2126 }
Andreas Gampe98430592014-07-27 19:44:50 -07002127 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002128 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002129 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002130 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002131 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002132 }
2133 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002134 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2135 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 else
Andreas Gampe98430592014-07-27 19:44:50 -07002137 rl_result = GetReturnWideAlt();
2138 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002139 }
2140}
2141
Mark Mendelle87f9b52014-04-30 14:13:18 -04002142void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2143 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2144 LoadConstantNoClobber(rl_result.reg, value);
2145 StoreValue(rl_dest, rl_result);
2146 if (value == 0) {
2147 Workaround7250540(rl_dest, rl_result.reg);
2148 }
2149}
2150
Andreas Gampe98430592014-07-27 19:44:50 -07002151void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2152 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002153 /*
2154 * Don't optimize the register usage since it calls out to support
2155 * functions
2156 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002157
Brian Carlstrom7940e442013-07-12 13:46:57 -07002158 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002159 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002160 if (rl_dest.wide) {
2161 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002162 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002163 StoreValueWide(rl_dest, rl_result);
2164 } else {
2165 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002166 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002167 StoreValue(rl_dest, rl_result);
2168 }
2169}
2170
Vladimir Marko6ce3eba2015-02-16 13:05:59 +00002171class Mir2Lir::SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002172 public:
2173 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2174 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2175 }
2176
2177 void Compile() OVERRIDE {
2178 m2l_->ResetRegPool();
2179 m2l_->ResetDefTracking();
2180 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002181 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002182 if (cont_ != nullptr) {
2183 m2l_->OpUnconditionalBranch(cont_);
2184 }
2185 }
2186};
2187
Brian Carlstrom7940e442013-07-12 13:46:57 -07002188/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002189void Mir2Lir::GenSuspendTest(int opt_flags) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002190 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2191 return;
2192 }
Dave Allison69dfe512014-07-11 17:11:58 +00002193 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002194 FlushAllRegs();
2195 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002196 LIR* cont = NewLIR0(kPseudoTargetLabel);
2197 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002198 } else {
Dave Allisonb373e092014-02-20 16:06:36 -08002199 FlushAllRegs(); // TODO: needed?
2200 LIR* inst = CheckSuspendUsingLoad();
2201 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002203}
2204
2205/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002206void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Vladimir Marko8b858e12014-11-27 14:52:37 +00002207 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK) != 0) {
2208 OpUnconditionalBranch(target);
2209 return;
2210 }
Dave Allison69dfe512014-07-11 17:11:58 +00002211 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002212 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002213 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002214 LIR* branch = OpUnconditionalBranch(nullptr);
2215 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002216 } else {
2217 // For the implicit suspend check, just perform the trigger
2218 // load and branch to the target.
Dave Allisonb373e092014-02-20 16:06:36 -08002219 FlushAllRegs();
2220 LIR* inst = CheckSuspendUsingLoad();
2221 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002222 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002223 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002224}
2225
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002226/* Call out to helper assembly routine that will null check obj and then lock it. */
2227void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002228 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002229 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002230 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002231}
2232
2233/* Call out to helper assembly routine that will null check obj and then unlock it. */
2234void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002235 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002236 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002237 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002238}
2239
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002240/* Generic code for generating a wide constant into a VR. */
2241void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2242 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002243 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002244 StoreValueWide(rl_dest, rl_result);
2245}
2246
Andreas Gampe48971b32014-08-06 10:09:01 -07002247void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002248 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2249 DCHECK(bb != nullptr);
2250 ArenaVector<SuccessorBlockInfo*>::const_iterator succ_bb_iter = bb->successor_blocks.cbegin();
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002251 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002252 const uint16_t entries = table[1];
2253 // Chained cmp-and-branch.
2254 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002255 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002256 rl_src = LoadValue(rl_src, kCoreReg);
2257 int i = 0;
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002258 for (; i < entries; ++i, ++succ_bb_iter) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002259 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002260 // Switch to using a temp and add.
2261 break;
2262 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002263 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2264 DCHECK(successor_block_info != nullptr);
2265 int case_block_id = successor_block_info->block;
2266 DCHECK_EQ(starting_key + i, successor_block_info->key);
2267 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002268 }
2269 if (i < entries) {
2270 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2271 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2272 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002273 LoadConstantNoClobber(key_temp, starting_key + i);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002274 for (; i < entries - 1; ++i, ++succ_bb_iter) {
2275 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2276 DCHECK(successor_block_info != nullptr);
2277 int case_block_id = successor_block_info->block;
2278 DCHECK_EQ(starting_key + i, successor_block_info->key);
2279 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002280 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2281 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002282 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2283 DCHECK(successor_block_info != nullptr);
2284 int case_block_id = successor_block_info->block;
2285 DCHECK_EQ(starting_key + i, successor_block_info->key);
2286 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002287 } else {
2288 // No free temp, just finish the old loop.
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002289 for (; i < entries; ++i, ++succ_bb_iter) {
2290 SuccessorBlockInfo* successor_block_info = *succ_bb_iter;
2291 DCHECK(successor_block_info != nullptr);
2292 int case_block_id = successor_block_info->block;
2293 DCHECK_EQ(starting_key + i, successor_block_info->key);
2294 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block_id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002295 }
2296 }
2297 }
2298}
2299
2300void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002301 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002302 if (cu_->verbose) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002303 DumpPackedSwitchTable(table);
Andreas Gampe48971b32014-08-06 10:09:01 -07002304 }
2305
2306 const uint16_t entries = table[1];
2307 if (entries <= kSmallSwitchThreshold) {
2308 GenSmallPackedSwitch(mir, table_offset, rl_src);
2309 } else {
2310 // Use the backend-specific implementation.
2311 GenLargePackedSwitch(mir, table_offset, rl_src);
2312 }
2313}
2314
2315void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002316 BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb);
2317 DCHECK(bb != nullptr);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002318 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002319 const uint16_t entries = table[1];
2320 // Chained cmp-and-branch.
Andreas Gampe48971b32014-08-06 10:09:01 -07002321 rl_src = LoadValue(rl_src, kCoreReg);
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002322 int i = 0;
2323 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
2324 int case_block_id = successor_block_info->block;
2325 int key = successor_block_info->key;
2326 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block_id]);
2327 i++;
Andreas Gampe48971b32014-08-06 10:09:01 -07002328 }
Chao-ying Fuda96aed2014-10-27 14:42:00 -07002329 DCHECK_EQ(i, entries);
Andreas Gampe48971b32014-08-06 10:09:01 -07002330}
2331
2332void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002333 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002334 if (cu_->verbose) {
2335 DumpSparseSwitchTable(table);
2336 }
2337
2338 const uint16_t entries = table[1];
2339 if (entries <= kSmallSwitchThreshold) {
2340 GenSmallSparseSwitch(mir, table_offset, rl_src);
2341 } else {
2342 // Use the backend-specific implementation.
2343 GenLargeSparseSwitch(mir, table_offset, rl_src);
2344 }
2345}
2346
Fred Shih37f05ef2014-07-16 18:38:08 -07002347bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2348 switch (size) {
2349 case kReference:
2350 return type == Primitive::kPrimNot;
2351 case k64:
2352 case kDouble:
2353 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2354 case k32:
2355 case kSingle:
2356 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2357 case kSignedHalf:
2358 return type == Primitive::kPrimShort;
2359 case kUnsignedHalf:
2360 return type == Primitive::kPrimChar;
2361 case kSignedByte:
2362 return type == Primitive::kPrimByte;
2363 case kUnsignedByte:
2364 return type == Primitive::kPrimBoolean;
2365 case kWord: // Intentional fallthrough.
2366 default:
2367 return false; // There are no sane types with this op size.
2368 }
2369}
2370
Brian Carlstrom7940e442013-07-12 13:46:57 -07002371} // namespace art