blob: 236128f0aa4707cf34cbe0254036a50dd2e3208d [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 */
Brian Carlstrom7940e442013-07-12 13:46:57 -070016#include "dex/compiler_ir.h"
17#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070018#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Andreas Gampe9c3b0892014-04-24 17:33:34 +000022#include "mirror/object_array-inl.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080023#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080025#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
Andreas Gampe9c3b0892014-04-24 17:33:34 +000029// Shortcuts to repeatedly used long types.
30typedef mirror::ObjectArray<mirror::Object> ObjArray;
31typedef mirror::ObjectArray<mirror::Class> ClassArray;
32
Brian Carlstrom7940e442013-07-12 13:46:57 -070033/*
34 * This source files contains "gen" codegen routines that should
35 * be applicable to most targets. Only mid-level support utilities
36 * and "op" calls may be used here.
37 */
38
39/*
buzbeeb48819d2013-09-14 16:15:25 -070040 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 * blocks.
42 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 LIR* barrier = NewLIR0(kPseudoBarrier);
45 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070046 DCHECK(!barrier->flags.use_def_invalid);
47 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070048}
49
Mingyao Yange643a172014-04-08 11:02:52 -070050void Mir2Lir::GenDivZeroException() {
51 LIR* branch = OpUnconditionalBranch(nullptr);
52 AddDivZeroCheckSlowPath(branch);
53}
54
55void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070056 LIR* branch = OpCondBranch(c_code, nullptr);
57 AddDivZeroCheckSlowPath(branch);
58}
59
Mingyao Yange643a172014-04-08 11:02:52 -070060void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
61 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070062 AddDivZeroCheckSlowPath(branch);
63}
64
65void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
66 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
67 public:
68 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
69 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
70 }
71
Mingyao Yange643a172014-04-08 11:02:52 -070072 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070073 m2l_->ResetRegPool();
74 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070075 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -070076 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
77 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowDivZero), true);
78 } else {
79 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
80 }
Mingyao Yang42894562014-04-07 12:42:16 -070081 }
82 };
83
84 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
85}
Dave Allisonb373e092014-02-20 16:06:36 -080086
Mingyao Yang80365d92014-04-18 12:10:58 -070087void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
88 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
89 public:
90 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
91 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
92 index_(index), length_(length) {
93 }
94
95 void Compile() OVERRIDE {
96 m2l_->ResetRegPool();
97 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070098 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -070099 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
100 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
101 index_, length_, true);
102 } else {
103 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
104 index_, length_, true);
105 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700106 }
107
108 private:
109 const RegStorage index_;
110 const RegStorage length_;
111 };
112
113 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
114 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
115}
116
117void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
118 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
119 public:
120 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
121 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
122 index_(index), length_(length) {
123 }
124
125 void Compile() OVERRIDE {
126 m2l_->ResetRegPool();
127 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700128 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700129
130 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
131 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700132 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
133 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
134 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
135 } else {
136 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
137 m2l_->TargetReg(kArg0), m2l_->TargetReg(kArg1), true);
138 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700139 }
140
141 private:
142 const int32_t index_;
143 const RegStorage length_;
144 };
145
146 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
147 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
148}
149
Mingyao Yange643a172014-04-08 11:02:52 -0700150LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
151 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
152 public:
153 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
154 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
155 }
156
157 void Compile() OVERRIDE {
158 m2l_->ResetRegPool();
159 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700160 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700161 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
162 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowNullPointer), true);
163 } else {
164 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
165 }
Mingyao Yange643a172014-04-08 11:02:52 -0700166 }
167 };
168
169 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
170 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
171 return branch;
172}
173
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800175LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800176 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700177 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 }
Dave Allisonb373e092014-02-20 16:06:36 -0800179 return nullptr;
180}
181
Dave Allisonf9439142014-03-27 15:10:22 -0700182/* Perform an explicit null-check on a register. */
183LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
184 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
185 return NULL;
186 }
Mingyao Yange643a172014-04-08 11:02:52 -0700187 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700188}
189
Dave Allisonb373e092014-02-20 16:06:36 -0800190void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
191 if (!Runtime::Current()->ExplicitNullChecks()) {
192 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
193 return;
194 }
195 MarkSafepointPC(last_lir_insn_);
196 }
197}
198
199void Mir2Lir::MarkPossibleStackOverflowException() {
200 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
201 MarkSafepointPC(last_lir_insn_);
202 }
203}
204
buzbee2700f7e2014-03-07 09:46:20 -0800205void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800206 if (!Runtime::Current()->ExplicitNullChecks()) {
207 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
208 return;
209 }
210 // Force an implicit null check by performing a memory operation (load) from the given
211 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800212 RegStorage tmp = AllocTemp();
213 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700214 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800215 FreeTemp(tmp);
216 MarkSafepointPC(load);
217 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218}
219
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
221 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700222 LIR* fall_through) {
buzbeea0cd2d72014-06-01 09:33:49 -0700223 DCHECK(!rl_src1.fp);
224 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 ConditionCode cond;
226 switch (opcode) {
227 case Instruction::IF_EQ:
228 cond = kCondEq;
229 break;
230 case Instruction::IF_NE:
231 cond = kCondNe;
232 break;
233 case Instruction::IF_LT:
234 cond = kCondLt;
235 break;
236 case Instruction::IF_GE:
237 cond = kCondGe;
238 break;
239 case Instruction::IF_GT:
240 cond = kCondGt;
241 break;
242 case Instruction::IF_LE:
243 cond = kCondLe;
244 break;
245 default:
246 cond = static_cast<ConditionCode>(0);
247 LOG(FATAL) << "Unexpected opcode " << opcode;
248 }
249
250 // Normalize such that if either operand is constant, src2 will be constant
251 if (rl_src1.is_const) {
252 RegLocation rl_temp = rl_src1;
253 rl_src1 = rl_src2;
254 rl_src2 = rl_temp;
255 cond = FlipComparisonOrder(cond);
256 }
257
buzbeea0cd2d72014-06-01 09:33:49 -0700258 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 // Is this really an immediate comparison?
260 if (rl_src2.is_const) {
261 // If it's already live in a register or not easily materialized, just keep going
262 RegLocation rl_temp = UpdateLoc(rl_src2);
263 if ((rl_temp.location == kLocDalvikFrame) &&
264 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
265 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800266 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 return;
268 }
269 }
buzbeea0cd2d72014-06-01 09:33:49 -0700270 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800271 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272}
273
274void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700275 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700277 DCHECK(!rl_src.fp);
278 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 switch (opcode) {
280 case Instruction::IF_EQZ:
281 cond = kCondEq;
282 break;
283 case Instruction::IF_NEZ:
284 cond = kCondNe;
285 break;
286 case Instruction::IF_LTZ:
287 cond = kCondLt;
288 break;
289 case Instruction::IF_GEZ:
290 cond = kCondGe;
291 break;
292 case Instruction::IF_GTZ:
293 cond = kCondGt;
294 break;
295 case Instruction::IF_LEZ:
296 cond = kCondLe;
297 break;
298 default:
299 cond = static_cast<ConditionCode>(0);
300 LOG(FATAL) << "Unexpected opcode " << opcode;
301 }
buzbee2700f7e2014-03-07 09:46:20 -0800302 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303}
304
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700305void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
307 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800308 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800310 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
buzbee2700f7e2014-03-07 09:46:20 -0800312 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 StoreValueWide(rl_dest, rl_result);
314}
315
316void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700318 rl_src = LoadValue(rl_src, kCoreReg);
319 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
320 OpKind op = kOpInvalid;
321 switch (opcode) {
322 case Instruction::INT_TO_BYTE:
323 op = kOp2Byte;
324 break;
325 case Instruction::INT_TO_SHORT:
326 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700328 case Instruction::INT_TO_CHAR:
329 op = kOp2Char;
330 break;
331 default:
332 LOG(ERROR) << "Bad int conversion type";
333 }
buzbee2700f7e2014-03-07 09:46:20 -0800334 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700335 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336}
337
Andreas Gampe2f244e92014-05-08 03:35:25 -0700338template <size_t pointer_size>
339static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
340 uint32_t type_idx, RegLocation rl_dest,
341 RegLocation rl_src) {
342 mir_to_lir->FlushAllRegs(); /* Everything to home location */
343 ThreadOffset<pointer_size> func_offset(-1);
344 const DexFile* dex_file = cu->dex_file;
345 CompilerDriver* driver = cu->compiler_driver;
346 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
347 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800348 bool is_type_initialized; // Ignored as an array does not have an initializer.
349 bool use_direct_type_ptr;
350 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700351 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800352 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700353 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
354 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800355 // The fast path.
356 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700357 mir_to_lir->LoadClassType(type_idx, kArg0);
358 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
359 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0),
360 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 } else {
362 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700363 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
364 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
365 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800366 }
367 } else {
368 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700369 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
370 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800371 }
372 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700374 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
375 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 }
buzbeea0cd2d72014-06-01 09:33:49 -0700377 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700378 mir_to_lir->StoreValue(rl_dest, rl_result);
379}
380
381/*
382 * Let helper function take care of everything. Will call
383 * Array::AllocFromCode(type_idx, method, count);
384 * Note: AllocFromCode will handle checks for errNegativeArraySize.
385 */
386void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
387 RegLocation rl_src) {
388 if (Is64BitInstructionSet(cu_->instruction_set)) {
389 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
390 } else {
391 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
392 }
393}
394
395template <size_t pointer_size>
396static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
397 ThreadOffset<pointer_size> func_offset(-1);
398 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
399 type_idx)) {
400 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
401 } else {
402 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
403 }
404 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405}
406
407/*
408 * Similar to GenNewArray, but with post-allocation initialization.
409 * Verifier guarantees we're dealing with an array class. Current
410 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
411 * Current code also throws internal unimp if not 'L', '[' or 'I'.
412 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700413void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 int elems = info->num_arg_words;
415 int type_idx = info->index;
416 FlushAllRegs(); /* Everything to home location */
Andreas Gampe2f244e92014-05-08 03:35:25 -0700417 if (Is64BitInstructionSet(cu_->instruction_set)) {
418 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700420 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 FreeTemp(TargetReg(kArg2));
423 FreeTemp(TargetReg(kArg1));
424 /*
425 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
426 * return region. Because AllocFromCode placed the new array
427 * in kRet0, we'll just lock it into place. When debugger support is
428 * added, it may be necessary to additionally copy all return
429 * values to a home location in thread-local storage
430 */
431 LockTemp(TargetReg(kRet0));
432
433 // TODO: use the correct component size, currently all supported types
434 // share array alignment with ints (see comment at head of function)
435 size_t component_size = sizeof(int32_t);
436
437 // Having a range of 0 is legal
438 if (info->is_range && (elems > 0)) {
439 /*
440 * Bit of ugliness here. We're going generate a mem copy loop
441 * on the register range, but it is possible that some regs
442 * in the range have been promoted. This is unlikely, but
443 * before generating the copy, we'll just force a flush
444 * of any regs in the source range that have been promoted to
445 * home location.
446 */
447 for (int i = 0; i < elems; i++) {
448 RegLocation loc = UpdateLoc(info->args[i]);
449 if (loc.location == kLocPhysReg) {
buzbee695d13a2014-04-19 13:32:20 -0700450 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 }
452 }
453 /*
454 * TUNING note: generated code here could be much improved, but
455 * this is an uncommon operation and isn't especially performance
456 * critical.
457 */
buzbee2700f7e2014-03-07 09:46:20 -0800458 RegStorage r_src = AllocTemp();
459 RegStorage r_dst = AllocTemp();
460 RegStorage r_idx = AllocTemp();
461 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700462 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 case kThumb2:
464 r_val = TargetReg(kLr);
465 break;
466 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700467 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 FreeTemp(TargetReg(kRet0));
469 r_val = AllocTemp();
470 break;
471 case kMips:
472 r_val = AllocTemp();
473 break;
474 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
475 }
476 // Set up source pointer
477 RegLocation rl_first = info->args[0];
478 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
479 // Set up the target pointer
480 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
481 mirror::Array::DataOffset(component_size).Int32Value());
482 // Set up the loop counter (known to be > 0)
483 LoadConstant(r_idx, elems - 1);
484 // Generate the copy loop. Going backwards for convenience
485 LIR* target = NewLIR0(kPseudoTargetLabel);
486 // Copy next element
buzbee695d13a2014-04-19 13:32:20 -0700487 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
488 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 FreeTemp(r_val);
490 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700491 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 // Restore the target pointer
493 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
494 -mirror::Array::DataOffset(component_size).Int32Value());
495 }
496 } else if (!info->is_range) {
497 // TUNING: interleave
498 for (int i = 0; i < elems; i++) {
499 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700500 Store32Disp(TargetReg(kRet0),
501 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800503 if (IsTemp(rl_arg.reg)) {
504 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 }
506 }
507 }
508 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700509 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 }
511}
512
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800513//
514// Slow path to ensure a class is initialized for sget/sput.
515//
516class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
517 public:
buzbee2700f7e2014-03-07 09:46:20 -0800518 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
519 RegStorage r_base) :
520 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
521 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800522 }
523
524 void Compile() {
525 LIR* unresolved_target = GenerateTargetLabel();
526 uninit_->target = unresolved_target;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700527 if (Is64BitInstructionSet(cu_->instruction_set)) {
528 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
529 storage_index_, true);
530 } else {
531 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
532 storage_index_, true);
533 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800534 // Copy helper's result into r_base, a no-op on all but MIPS.
535 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
536
537 m2l_->OpUnconditionalBranch(cont_);
538 }
539
540 private:
541 LIR* const uninit_;
542 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800543 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544};
545
Andreas Gampe2f244e92014-05-08 03:35:25 -0700546template <size_t pointer_size>
547static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
548 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
549 ThreadOffset<pointer_size> setter_offset =
550 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
551 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
552 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
553 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
554 true);
555}
556
Vladimir Markobe0e5462014-02-26 11:24:15 +0000557void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700558 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000559 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
560 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100561 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
562 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
563 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000564 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800565 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000566 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100568 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700569 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700570 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800571 if (IsTemp(rl_method.reg)) {
572 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
574 } else {
575 // Medium path, static storage base in a different class which requires checks that the other
576 // class is initialized.
577 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000578 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 // May do runtime call so everything to home locations.
580 FlushAllRegs();
581 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800582 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 LockTemp(r_method);
584 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800585 r_base = TargetReg(kArg0);
586 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700587 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000588 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
589 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800590 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000591 if (!field_info.IsInitialized() &&
592 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800593 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800594
595 // The slow path is invoked if the r_base is NULL or the class pointed
596 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800597 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800598 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800599 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800601 mirror::Class::StatusOffset().Int32Value(),
602 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800603 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604
buzbee2700f7e2014-03-07 09:46:20 -0800605 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000606 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607
608 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 FreeTemp(r_method);
611 }
612 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100613 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100615 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100617 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000619 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800620 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100622 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800623 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100625 } else {
626 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
628 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800629 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800631 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 } else {
633 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700634 if (Is64BitInstructionSet(cu_->instruction_set)) {
635 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
636 } else {
637 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
638 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
640}
641
Andreas Gampe2f244e92014-05-08 03:35:25 -0700642template <size_t pointer_size>
643static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
644 const MirSFieldLoweringInfo* field_info) {
645 ThreadOffset<pointer_size> getter_offset =
646 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
647 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
648 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
649 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
650}
651
Vladimir Markobe0e5462014-02-26 11:24:15 +0000652void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700653 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000654 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
655 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100656 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
657 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
658 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000659 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800660 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000661 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 // Fast path, static storage base is this method's class
663 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700664 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700665 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 } else {
667 // Medium path, static storage base in a different class which requires checks that the other
668 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000669 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 // May do runtime call so everything to home locations.
671 FlushAllRegs();
672 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800673 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 LockTemp(r_method);
675 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800676 r_base = TargetReg(kArg0);
677 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700678 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000679 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
680 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800681 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000682 if (!field_info.IsInitialized() &&
683 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800684 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800685
686 // The slow path is invoked if the r_base is NULL or the class pointed
687 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800688 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800689 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800690 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800691 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800692 mirror::Class::StatusOffset().Int32Value(),
693 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800694 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800695
buzbee2700f7e2014-03-07 09:46:20 -0800696 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000697 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800698
699 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 FreeTemp(r_method);
702 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800703 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100704 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
705 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800706
Vladimir Marko674744e2014-04-24 15:18:26 +0100707 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800708 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100709 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800710 // Without context sensitive analysis, we must issue the most conservative barriers.
711 // In this case, either a load or store may follow so we issue both barriers.
712 GenMemBarrier(kLoadLoad);
713 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100714 } else {
715 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800716 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100717 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800718
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 if (is_long_or_double) {
720 StoreValueWide(rl_dest, rl_result);
721 } else {
722 StoreValue(rl_dest, rl_result);
723 }
724 } else {
725 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700726 if (Is64BitInstructionSet(cu_->instruction_set)) {
727 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
728 } else {
729 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
730 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700732 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 StoreValueWide(rl_dest, rl_result);
734 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700735 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 StoreValue(rl_dest, rl_result);
737 }
738 }
739}
740
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800741// Generate code for all slow paths.
742void Mir2Lir::HandleSlowPaths() {
743 int n = slow_paths_.Size();
744 for (int i = 0; i < n; ++i) {
745 LIRSlowPath* slowpath = slow_paths_.Get(i);
746 slowpath->Compile();
747 }
748 slow_paths_.Reset();
749}
750
Andreas Gampe2f244e92014-05-08 03:35:25 -0700751template <size_t pointer_size>
752static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
753 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
754 ThreadOffset<pointer_size> getter_offset =
755 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
756 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
757 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
758 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
759 true);
760}
761
Vladimir Markobe0e5462014-02-26 11:24:15 +0000762void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700764 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000765 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
766 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100767 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
768 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
769 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
770 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000771 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700772 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100773 GenNullCheck(rl_obj.reg, opt_flags);
774 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
775 int field_offset = field_info.FieldOffset().Int32Value();
776 if (field_info.IsVolatile()) {
777 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
778 MarkPossibleNullPointerException(opt_flags);
779 // Without context sensitive analysis, we must issue the most conservative barriers.
780 // In this case, either a load or store may follow so we issue both barriers.
781 GenMemBarrier(kLoadLoad);
782 GenMemBarrier(kLoadStore);
783 } else {
784 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
785 MarkPossibleNullPointerException(opt_flags);
786 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 StoreValueWide(rl_dest, rl_result);
789 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 StoreValue(rl_dest, rl_result);
791 }
792 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700793 if (Is64BitInstructionSet(cu_->instruction_set)) {
794 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
795 } else {
796 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
797 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700799 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 StoreValueWide(rl_dest, rl_result);
801 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700802 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 StoreValue(rl_dest, rl_result);
804 }
805 }
806}
807
Andreas Gampe2f244e92014-05-08 03:35:25 -0700808template <size_t pointer_size>
809static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
810 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
811 RegLocation rl_src) {
812 ThreadOffset<pointer_size> setter_offset =
813 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
814 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
815 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
816 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
817 rl_obj, rl_src, true);
818}
819
Vladimir Markobe0e5462014-02-26 11:24:15 +0000820void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700822 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000823 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
824 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100825 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
826 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
827 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
828 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000829 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700830 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100832 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 } else {
834 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100835 }
836 GenNullCheck(rl_obj.reg, opt_flags);
837 int field_offset = field_info.FieldOffset().Int32Value();
838 if (field_info.IsVolatile()) {
839 // There might have been a store before this volatile one so insert StoreStore barrier.
840 GenMemBarrier(kStoreStore);
841 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800842 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100843 // A load might follow the volatile store so insert a StoreLoad barrier.
844 GenMemBarrier(kStoreLoad);
845 } else {
846 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
847 MarkPossibleNullPointerException(opt_flags);
848 }
849 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
850 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 }
852 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700853 if (Is64BitInstructionSet(cu_->instruction_set)) {
854 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
855 } else {
856 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
857 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 }
859}
860
Andreas Gampe2f244e92014-05-08 03:35:25 -0700861template <size_t pointer_size>
862static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
863 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
864 ThreadOffset<pointer_size> helper = needs_range_check
865 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
866 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
867 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
868 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
869 true);
870}
871
Ian Rogersa9a82542013-10-04 11:17:26 -0700872void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
873 RegLocation rl_src) {
874 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
875 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
876 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700877 if (Is64BitInstructionSet(cu_->instruction_set)) {
878 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
879 } else {
880 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
881 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700882}
883
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700884void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800886 RegStorage res_reg = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -0700887 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
889 *cu_->dex_file,
890 type_idx)) {
891 // Call out to helper which resolves type and verifies access.
892 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700893 if (Is64BitInstructionSet(cu_->instruction_set)) {
894 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
895 type_idx, rl_method.reg, true);
896 } else {
897 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
898 type_idx, rl_method.reg, true);
899 }
buzbeea0cd2d72014-06-01 09:33:49 -0700900 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 StoreValue(rl_dest, rl_result);
902 } else {
903 // We're don't need access checks, load type from dex cache
904 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700905 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700906 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000907 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700908 LoadRefDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
910 type_idx) || SLOW_TYPE_PATH) {
911 // Slow path, at runtime test if type is null and if so initialize
912 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800913 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800914 LIR* cont = NewLIR0(kPseudoTargetLabel);
915
916 // Object to generate the slow path for class resolution.
917 class SlowPath : public LIRSlowPath {
918 public:
919 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
920 const RegLocation& rl_method, const RegLocation& rl_result) :
921 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
922 rl_method_(rl_method), rl_result_(rl_result) {
923 }
924
925 void Compile() {
926 GenerateTargetLabel();
927
Andreas Gampe2f244e92014-05-08 03:35:25 -0700928 if (Is64BitInstructionSet(cu_->instruction_set)) {
929 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
930 rl_method_.reg, true);
931 } else {
932 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
933 rl_method_.reg, true);
934 }
buzbee2700f7e2014-03-07 09:46:20 -0800935 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800936
937 m2l_->OpUnconditionalBranch(cont_);
938 }
939
940 private:
941 const int type_idx_;
942 const RegLocation rl_method_;
943 const RegLocation rl_result_;
944 };
945
946 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800947 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800948
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800950 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951 // Fast path, we're done - just store result
952 StoreValue(rl_dest, rl_result);
953 }
954 }
955}
956
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700957void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000959 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
960 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
962 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
963 // slow path, resolve string if not in dex cache
964 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700965 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800966
967 // If the Method* is already in a register, we can save a copy.
968 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800969 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800970 if (rl_method.location == kLocPhysReg) {
971 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800972 DCHECK(!IsTemp(rl_method.reg));
973 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800974 } else {
975 r_method = TargetReg(kArg2);
976 LoadCurrMethodDirect(r_method);
977 }
buzbee695d13a2014-04-19 13:32:20 -0700978 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
979 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800980
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 // Might call out to helper, which will return resolved string in kRet0
buzbeea0cd2d72014-06-01 09:33:49 -0700982 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700983 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
984 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800985
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700986 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800987 // Object to generate the slow path for string resolution.
988 class SlowPath : public LIRSlowPath {
989 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700990 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
991 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
992 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800993 }
994
995 void Compile() {
996 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -0700997 if (Is64BitInstructionSet(cu_->instruction_set)) {
998 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
999 r_method_, string_idx_, true);
1000 } else {
1001 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1002 r_method_, string_idx_, true);
1003 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001004 m2l_->OpUnconditionalBranch(cont_);
1005 }
1006
1007 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001008 const RegStorage r_method_;
1009 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001010 };
1011
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001012 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001014
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001016 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 } else {
1018 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001019 RegStorage res_reg = AllocTempRef();
1020 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001021 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeea0cd2d72014-06-01 09:33:49 -07001022 LoadRefDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 StoreValue(rl_dest, rl_result);
1024 }
1025}
1026
Andreas Gampe2f244e92014-05-08 03:35:25 -07001027template <size_t pointer_size>
1028static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1029 RegLocation rl_dest) {
1030 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 // alloc will always check for resolution, do we also need to verify
1032 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001033 ThreadOffset<pointer_size> func_offset(-1);
1034 const DexFile* dex_file = cu->dex_file;
1035 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001036 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001037 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001038 bool is_type_initialized;
1039 bool use_direct_type_ptr;
1040 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001041 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001042 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001043 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1044 &direct_type_ptr, &is_finalizable) &&
1045 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001046 // The fast path.
1047 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001048 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001050 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1051 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001053 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1054 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001055 }
1056 } else {
1057 // Use the direct pointer.
1058 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001059 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1060 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001061 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001062 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1063 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001064 }
1065 }
1066 } else {
1067 // The slow path.
1068 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001069 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1070 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001071 }
1072 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001074 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1075 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 }
buzbeea0cd2d72014-06-01 09:33:49 -07001077 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001078 mir_to_lir->StoreValue(rl_dest, rl_result);
1079}
1080
1081/*
1082 * Let helper function take care of everything. Will
1083 * call Class::NewInstanceFromCode(type_idx, method);
1084 */
1085void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1086 if (Is64BitInstructionSet(cu_->instruction_set)) {
1087 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1088 } else {
1089 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1090 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091}
1092
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001093void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001095 if (Is64BitInstructionSet(cu_->instruction_set)) {
1096 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1097 } else {
1098 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1099 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100}
1101
1102// For final classes there are no sub-classes to check and so we can answer the instance-of
1103// question with simple comparisons.
1104void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1105 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001106 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001107 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001108
buzbeea0cd2d72014-06-01 09:33:49 -07001109 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001111 RegStorage result_reg = rl_result.reg;
1112 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 result_reg = AllocTypedTemp(false, kCoreReg);
1114 }
1115 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001116 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117
buzbeea0cd2d72014-06-01 09:33:49 -07001118 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1119 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120
1121 LoadCurrMethodDirect(check_class);
1122 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001123 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1124 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 } else {
buzbee695d13a2014-04-19 13:32:20 -07001126 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1127 check_class);
1128 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001129 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001130 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 }
1132
1133 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001134 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 if (cu_->instruction_set == kThumb2) {
1136 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001137 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001139 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 } else {
1141 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1142 LoadConstant(result_reg, 1); // eq case - load true
1143 }
1144 LIR* target = NewLIR0(kPseudoTargetLabel);
1145 null_branchover->target = target;
1146 if (ne_branchover != NULL) {
1147 ne_branchover->target = target;
1148 }
1149 FreeTemp(object_class);
1150 FreeTemp(check_class);
1151 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001152 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 FreeTemp(result_reg);
1154 }
1155 StoreValue(rl_dest, rl_result);
1156}
1157
1158void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1159 bool type_known_abstract, bool use_declaring_class,
1160 bool can_assume_type_is_in_dex_cache,
1161 uint32_t type_idx, RegLocation rl_dest,
1162 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001163 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001164 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001165
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 FlushAllRegs();
1167 // May generate a call - use explicit registers
1168 LockCallTemps();
1169 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001170 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 if (needs_access_check) {
1172 // Check we have access to type_idx and if not throw IllegalAccessError,
1173 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001174 if (Is64BitInstructionSet(cu_->instruction_set)) {
1175 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1176 type_idx, true);
1177 } else {
1178 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1179 type_idx, true);
1180 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1182 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1183 } else if (use_declaring_class) {
1184 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001185 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001186 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 } else {
1188 // Load dex cache entry into class_reg (kArg2)
1189 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001190 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1191 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001192 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001193 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 if (!can_assume_type_is_in_dex_cache) {
1195 // Need to test presence of type in dex cache at runtime
1196 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1197 // Not resolved
1198 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001199 if (Is64BitInstructionSet(cu_->instruction_set)) {
1200 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1201 } else {
1202 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1203 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001204 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1206 // Rejoin code paths
1207 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1208 hop_branch->target = hop_target;
1209 }
1210 }
1211 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001212 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 if (cu_->instruction_set == kMips) {
1214 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001215 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 }
1217 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1218
1219 /* load object->klass_ */
1220 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001221 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1223 LIR* branchover = NULL;
1224 if (type_known_final) {
1225 // rl_result == ref == null == 0.
1226 if (cu_->instruction_set == kThumb2) {
1227 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001228 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001229 LoadConstant(rl_result.reg, 1); // .eq case - load true
1230 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001231 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001233 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001234 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001235 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 }
1237 } else {
1238 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001239 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1240 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1241 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001242 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 if (!type_known_abstract) {
1244 /* Uses conditional nullification */
1245 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001246 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1248 }
1249 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1250 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001251 if (it != nullptr) {
1252 OpEndIT(it);
1253 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 FreeTemp(r_tgt);
1255 } else {
1256 if (!type_known_abstract) {
1257 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001258 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1260 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001261 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1262 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1263 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001264 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1265 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1266 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 }
1268 }
1269 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001270 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 /* branch targets here */
1272 LIR* target = NewLIR0(kPseudoTargetLabel);
1273 StoreValue(rl_dest, rl_result);
1274 branch1->target = target;
1275 if (branchover != NULL) {
1276 branchover->target = target;
1277 }
1278}
1279
1280void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1281 bool type_known_final, type_known_abstract, use_declaring_class;
1282 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1283 *cu_->dex_file,
1284 type_idx,
1285 &type_known_final,
1286 &type_known_abstract,
1287 &use_declaring_class);
1288 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1289 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1290
1291 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1292 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1293 } else {
1294 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1295 use_declaring_class, can_assume_type_is_in_dex_cache,
1296 type_idx, rl_dest, rl_src);
1297 }
1298}
1299
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001300void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 bool type_known_final, type_known_abstract, use_declaring_class;
1302 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1303 *cu_->dex_file,
1304 type_idx,
1305 &type_known_final,
1306 &type_known_abstract,
1307 &use_declaring_class);
1308 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1309 // of the exception throw path.
1310 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001311 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 // Verifier type analysis proved this check cast would never cause an exception.
1313 return;
1314 }
1315 FlushAllRegs();
1316 // May generate a call - use explicit registers
1317 LockCallTemps();
1318 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001319 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 if (needs_access_check) {
1321 // Check we have access to type_idx and if not throw IllegalAccessError,
1322 // returns Class* in kRet0
1323 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001324 if (Is64BitInstructionSet(cu_->instruction_set)) {
1325 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1326 type_idx, TargetReg(kArg1), true);
1327 } else {
1328 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1329 type_idx, TargetReg(kArg1), true);
1330 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1332 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001333 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1334 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 } else {
1336 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001337 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1338 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001339 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001340 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1342 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001343 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1344 LIR* cont = NewLIR0(kPseudoTargetLabel);
1345
1346 // Slow path to initialize the type. Executed if the type is NULL.
1347 class SlowPath : public LIRSlowPath {
1348 public:
1349 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001350 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001351 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1352 class_reg_(class_reg) {
1353 }
1354
1355 void Compile() {
1356 GenerateTargetLabel();
1357
1358 // Call out to helper, which will return resolved type in kArg0
1359 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001360 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1361 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1362 m2l_->TargetReg(kArg1), true);
1363 } else {
1364 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1365 m2l_->TargetReg(kArg1), true);
1366 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001367 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1368 m2l_->OpUnconditionalBranch(cont_);
1369 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001370
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001371 public:
1372 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001373 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001374 };
1375
buzbee2700f7e2014-03-07 09:46:20 -08001376 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 }
1378 }
1379 // At this point, class_reg (kArg2) has class
1380 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381
1382 // Slow path for the case where the classes are not equal. In this case we need
1383 // to call a helper function to do the check.
1384 class SlowPath : public LIRSlowPath {
1385 public:
1386 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1387 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1388 }
1389
1390 void Compile() {
1391 GenerateTargetLabel();
1392
1393 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001394 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1395 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001396 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001397 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1398 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1399 m2l_->TargetReg(kArg1), true);
1400 } else {
1401 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1402 m2l_->TargetReg(kArg1), true);
1403 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001404
1405 m2l_->OpUnconditionalBranch(cont_);
1406 }
1407
1408 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001409 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001410 };
1411
1412 if (type_known_abstract) {
1413 // Easier case, run slow path if target is non-null (slow path will load from target)
1414 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1415 LIR* cont = NewLIR0(kPseudoTargetLabel);
1416 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1417 } else {
1418 // Harder, more common case. We need to generate a forward branch over the load
1419 // if the target is null. If it's non-null we perform the load and branch to the
1420 // slow path if the classes are not equal.
1421
1422 /* Null is OK - continue */
1423 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1424 /* load object->klass_ */
1425 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001426 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001427
1428 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1429 LIR* cont = NewLIR0(kPseudoTargetLabel);
1430
1431 // Add the slow path that will not perform load since this is already done.
1432 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1433
1434 // Set the null check to branch to the continuation.
1435 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 }
1437}
1438
1439void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001440 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 RegLocation rl_result;
1442 if (cu_->instruction_set == kThumb2) {
1443 /*
1444 * NOTE: This is the one place in the code in which we might have
1445 * as many as six live temporary registers. There are 5 in the normal
1446 * set for Arm. Until we have spill capabilities, temporarily add
1447 * lr to the temp set. It is safe to do this locally, but note that
1448 * lr is used explicitly elsewhere in the code generator and cannot
1449 * normally be used as a general temp register.
1450 */
1451 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1452 FreeTemp(TargetReg(kLr)); // and make it available
1453 }
1454 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1455 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1456 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1457 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001458 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1459 RegStorage t_reg = AllocTemp();
1460 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1461 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1462 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 FreeTemp(t_reg);
1464 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001465 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1466 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 }
1468 /*
1469 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1470 * following StoreValueWide might need to allocate a temp register.
1471 * To further work around the lack of a spill capability, explicitly
1472 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1473 * Remove when spill is functional.
1474 */
1475 FreeRegLocTemps(rl_result, rl_src1);
1476 FreeRegLocTemps(rl_result, rl_src2);
1477 StoreValueWide(rl_dest, rl_result);
1478 if (cu_->instruction_set == kThumb2) {
1479 Clobber(TargetReg(kLr));
1480 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1481 }
1482}
1483
1484
Andreas Gampe2f244e92014-05-08 03:35:25 -07001485template <size_t pointer_size>
1486static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1487 RegLocation rl_shift) {
1488 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489
1490 switch (opcode) {
1491 case Instruction::SHL_LONG:
1492 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001493 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 break;
1495 case Instruction::SHR_LONG:
1496 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001497 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 break;
1499 case Instruction::USHR_LONG:
1500 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001501 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 break;
1503 default:
1504 LOG(FATAL) << "Unexpected case";
1505 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001506 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1507 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1508}
1509
1510void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1511 RegLocation rl_src1, RegLocation rl_shift) {
1512 if (Is64BitInstructionSet(cu_->instruction_set)) {
1513 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1514 } else {
1515 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1516 }
buzbeea0cd2d72014-06-01 09:33:49 -07001517 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 StoreValueWide(rl_dest, rl_result);
1519}
1520
1521
1522void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001523 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001524 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 OpKind op = kOpBkpt;
1526 bool is_div_rem = false;
1527 bool check_zero = false;
1528 bool unary = false;
1529 RegLocation rl_result;
1530 bool shift_op = false;
1531 switch (opcode) {
1532 case Instruction::NEG_INT:
1533 op = kOpNeg;
1534 unary = true;
1535 break;
1536 case Instruction::NOT_INT:
1537 op = kOpMvn;
1538 unary = true;
1539 break;
1540 case Instruction::ADD_INT:
1541 case Instruction::ADD_INT_2ADDR:
1542 op = kOpAdd;
1543 break;
1544 case Instruction::SUB_INT:
1545 case Instruction::SUB_INT_2ADDR:
1546 op = kOpSub;
1547 break;
1548 case Instruction::MUL_INT:
1549 case Instruction::MUL_INT_2ADDR:
1550 op = kOpMul;
1551 break;
1552 case Instruction::DIV_INT:
1553 case Instruction::DIV_INT_2ADDR:
1554 check_zero = true;
1555 op = kOpDiv;
1556 is_div_rem = true;
1557 break;
1558 /* NOTE: returns in kArg1 */
1559 case Instruction::REM_INT:
1560 case Instruction::REM_INT_2ADDR:
1561 check_zero = true;
1562 op = kOpRem;
1563 is_div_rem = true;
1564 break;
1565 case Instruction::AND_INT:
1566 case Instruction::AND_INT_2ADDR:
1567 op = kOpAnd;
1568 break;
1569 case Instruction::OR_INT:
1570 case Instruction::OR_INT_2ADDR:
1571 op = kOpOr;
1572 break;
1573 case Instruction::XOR_INT:
1574 case Instruction::XOR_INT_2ADDR:
1575 op = kOpXor;
1576 break;
1577 case Instruction::SHL_INT:
1578 case Instruction::SHL_INT_2ADDR:
1579 shift_op = true;
1580 op = kOpLsl;
1581 break;
1582 case Instruction::SHR_INT:
1583 case Instruction::SHR_INT_2ADDR:
1584 shift_op = true;
1585 op = kOpAsr;
1586 break;
1587 case Instruction::USHR_INT:
1588 case Instruction::USHR_INT_2ADDR:
1589 shift_op = true;
1590 op = kOpLsr;
1591 break;
1592 default:
1593 LOG(FATAL) << "Invalid word arith op: " << opcode;
1594 }
1595 if (!is_div_rem) {
1596 if (unary) {
1597 rl_src1 = LoadValue(rl_src1, kCoreReg);
1598 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001599 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001601 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001602 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001603 RegStorage t_reg = AllocTemp();
1604 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 rl_src1 = LoadValue(rl_src1, kCoreReg);
1606 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001607 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 FreeTemp(t_reg);
1609 } else {
1610 rl_src1 = LoadValue(rl_src1, kCoreReg);
1611 rl_src2 = LoadValue(rl_src2, kCoreReg);
1612 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001613 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 }
1615 }
1616 StoreValue(rl_dest, rl_result);
1617 } else {
Dave Allison70202782013-10-22 17:52:19 -07001618 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 +01001619 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 rl_src1 = LoadValue(rl_src1, kCoreReg);
1621 rl_src2 = LoadValue(rl_src2, kCoreReg);
1622 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001623 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 }
buzbee2700f7e2014-03-07 09:46:20 -08001625 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001626 done = true;
1627 } else if (cu_->instruction_set == kThumb2) {
1628 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1629 // Use ARM SDIV instruction for division. For remainder we also need to
1630 // calculate using a MUL and subtract.
1631 rl_src1 = LoadValue(rl_src1, kCoreReg);
1632 rl_src2 = LoadValue(rl_src2, kCoreReg);
1633 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001634 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001635 }
buzbee2700f7e2014-03-07 09:46:20 -08001636 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001637 done = true;
1638 }
1639 }
1640
1641 // If we haven't already generated the code use the callout function.
1642 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 FlushAllRegs(); /* Send everything to home location */
1644 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001645 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1646 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1647 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001648 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1649 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001650 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 }
Dave Allison70202782013-10-22 17:52:19 -07001652 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001653 if (Is64BitInstructionSet(cu_->instruction_set)) {
1654 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1655 } else {
1656 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1657 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001659 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 else
1661 rl_result = GetReturnAlt();
1662 }
1663 StoreValue(rl_dest, rl_result);
1664 }
1665}
1666
1667/*
1668 * The following are the first-level codegen routines that analyze the format
1669 * of each bytecode then either dispatch special purpose codegen routines
1670 * or produce corresponding Thumb instructions directly.
1671 */
1672
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001674static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 x &= x - 1;
1676 return (x & (x - 1)) == 0;
1677}
1678
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1680// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001681bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001682 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1684 return false;
1685 }
1686 // No divide instruction for Arm, so check for more special cases
1687 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001688 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 }
1690 int k = LowestSetBit(lit);
1691 if (k >= 30) {
1692 // Avoid special cases.
1693 return false;
1694 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 rl_src = LoadValue(rl_src, kCoreReg);
1696 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001697 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001698 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 if (lit == 2) {
1700 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001701 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1702 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1703 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001705 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001707 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1708 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 }
1710 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001711 RegStorage t_reg1 = AllocTemp();
1712 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001714 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1715 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001717 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001719 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001721 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001723 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 }
1725 }
1726 StoreValue(rl_dest, rl_result);
1727 return true;
1728}
1729
1730// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1731// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001732bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001733 if (lit < 0) {
1734 return false;
1735 }
1736 if (lit == 0) {
1737 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1738 LoadConstant(rl_result.reg, 0);
1739 StoreValue(rl_dest, rl_result);
1740 return true;
1741 }
1742 if (lit == 1) {
1743 rl_src = LoadValue(rl_src, kCoreReg);
1744 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1745 OpRegCopy(rl_result.reg, rl_src.reg);
1746 StoreValue(rl_dest, rl_result);
1747 return true;
1748 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001749 // There is RegRegRegShift on Arm, so check for more special cases
1750 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001751 return EasyMultiply(rl_src, rl_dest, lit);
1752 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 // Can we simplify this multiplication?
1754 bool power_of_two = false;
1755 bool pop_count_le2 = false;
1756 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001757 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001758 power_of_two = true;
1759 } else if (IsPopCountLE2(lit)) {
1760 pop_count_le2 = true;
1761 } else if (IsPowerOfTwo(lit + 1)) {
1762 power_of_two_minus_one = true;
1763 } else {
1764 return false;
1765 }
1766 rl_src = LoadValue(rl_src, kCoreReg);
1767 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1768 if (power_of_two) {
1769 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001770 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 } else if (pop_count_le2) {
1772 // Shift and add and shift.
1773 int first_bit = LowestSetBit(lit);
1774 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1775 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1776 } else {
1777 // Reverse subtract: (src << (shift + 1)) - src.
1778 DCHECK(power_of_two_minus_one);
1779 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001780 RegStorage t_reg = AllocTemp();
1781 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1782 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 }
1784 StoreValue(rl_dest, rl_result);
1785 return true;
1786}
1787
1788void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001789 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 RegLocation rl_result;
1791 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1792 int shift_op = false;
1793 bool is_div = false;
1794
1795 switch (opcode) {
1796 case Instruction::RSUB_INT_LIT8:
1797 case Instruction::RSUB_INT: {
1798 rl_src = LoadValue(rl_src, kCoreReg);
1799 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1800 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001801 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001803 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1804 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001805 }
1806 StoreValue(rl_dest, rl_result);
1807 return;
1808 }
1809
1810 case Instruction::SUB_INT:
1811 case Instruction::SUB_INT_2ADDR:
1812 lit = -lit;
1813 // Intended fallthrough
1814 case Instruction::ADD_INT:
1815 case Instruction::ADD_INT_2ADDR:
1816 case Instruction::ADD_INT_LIT8:
1817 case Instruction::ADD_INT_LIT16:
1818 op = kOpAdd;
1819 break;
1820 case Instruction::MUL_INT:
1821 case Instruction::MUL_INT_2ADDR:
1822 case Instruction::MUL_INT_LIT8:
1823 case Instruction::MUL_INT_LIT16: {
1824 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1825 return;
1826 }
1827 op = kOpMul;
1828 break;
1829 }
1830 case Instruction::AND_INT:
1831 case Instruction::AND_INT_2ADDR:
1832 case Instruction::AND_INT_LIT8:
1833 case Instruction::AND_INT_LIT16:
1834 op = kOpAnd;
1835 break;
1836 case Instruction::OR_INT:
1837 case Instruction::OR_INT_2ADDR:
1838 case Instruction::OR_INT_LIT8:
1839 case Instruction::OR_INT_LIT16:
1840 op = kOpOr;
1841 break;
1842 case Instruction::XOR_INT:
1843 case Instruction::XOR_INT_2ADDR:
1844 case Instruction::XOR_INT_LIT8:
1845 case Instruction::XOR_INT_LIT16:
1846 op = kOpXor;
1847 break;
1848 case Instruction::SHL_INT_LIT8:
1849 case Instruction::SHL_INT:
1850 case Instruction::SHL_INT_2ADDR:
1851 lit &= 31;
1852 shift_op = true;
1853 op = kOpLsl;
1854 break;
1855 case Instruction::SHR_INT_LIT8:
1856 case Instruction::SHR_INT:
1857 case Instruction::SHR_INT_2ADDR:
1858 lit &= 31;
1859 shift_op = true;
1860 op = kOpAsr;
1861 break;
1862 case Instruction::USHR_INT_LIT8:
1863 case Instruction::USHR_INT:
1864 case Instruction::USHR_INT_2ADDR:
1865 lit &= 31;
1866 shift_op = true;
1867 op = kOpLsr;
1868 break;
1869
1870 case Instruction::DIV_INT:
1871 case Instruction::DIV_INT_2ADDR:
1872 case Instruction::DIV_INT_LIT8:
1873 case Instruction::DIV_INT_LIT16:
1874 case Instruction::REM_INT:
1875 case Instruction::REM_INT_2ADDR:
1876 case Instruction::REM_INT_LIT8:
1877 case Instruction::REM_INT_LIT16: {
1878 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001879 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 return;
1881 }
buzbee11b63d12013-08-27 07:34:17 -07001882 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001883 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001884 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 (opcode == Instruction::DIV_INT_LIT16)) {
1886 is_div = true;
1887 } else {
1888 is_div = false;
1889 }
buzbee11b63d12013-08-27 07:34:17 -07001890 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1891 return;
1892 }
Dave Allison70202782013-10-22 17:52:19 -07001893
1894 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001895 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001896 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001897 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001898 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001899 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001900 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1901 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001902 } else if (cu_->instruction_set == kThumb2) {
1903 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1904 // Use ARM SDIV instruction for division. For remainder we also need to
1905 // calculate using a MUL and subtract.
1906 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001907 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001908 done = true;
1909 }
1910 }
1911
1912 if (!done) {
1913 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001914 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1915 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001916 if (Is64BitInstructionSet(cu_->instruction_set)) {
1917 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1918 false);
1919 } else {
1920 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1921 false);
1922 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001924 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 else
1926 rl_result = GetReturnAlt();
1927 }
1928 StoreValue(rl_dest, rl_result);
1929 return;
1930 }
1931 default:
1932 LOG(FATAL) << "Unexpected opcode " << opcode;
1933 }
1934 rl_src = LoadValue(rl_src, kCoreReg);
1935 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001936 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001938 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001940 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 }
1942 StoreValue(rl_dest, rl_result);
1943}
1944
Andreas Gampe2f244e92014-05-08 03:35:25 -07001945template <size_t pointer_size>
1946static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1947 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 RegLocation rl_result;
1949 OpKind first_op = kOpBkpt;
1950 OpKind second_op = kOpBkpt;
1951 bool call_out = false;
1952 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001953 ThreadOffset<pointer_size> func_offset(-1);
1954 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955
1956 switch (opcode) {
1957 case Instruction::NOT_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001958 if (cu->instruction_set == kArm64) {
1959 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1960 return;
1961 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001962 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1963 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001965 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001966 RegStorage t_reg = mir_to_lir->AllocTemp();
1967 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1968 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1969 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1970 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001971 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001972 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1973 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001975 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 return;
1977 case Instruction::ADD_LONG:
1978 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001979 if (cu->instruction_set != kThumb2) {
1980 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 return;
1982 }
1983 first_op = kOpAdd;
1984 second_op = kOpAdc;
1985 break;
1986 case Instruction::SUB_LONG:
1987 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001988 if (cu->instruction_set != kThumb2) {
1989 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 return;
1991 }
1992 first_op = kOpSub;
1993 second_op = kOpSbc;
1994 break;
1995 case Instruction::MUL_LONG:
1996 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001997 if (cu->instruction_set != kMips) {
1998 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 return;
2000 } else {
2001 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002002 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2003 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 }
2005 break;
2006 case Instruction::DIV_LONG:
2007 case Instruction::DIV_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002008 if (cu->instruction_set == kArm64) {
2009 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2010 return;
2011 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 call_out = true;
2013 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002014 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2015 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 break;
2017 case Instruction::REM_LONG:
2018 case Instruction::REM_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002019 if (cu->instruction_set == kArm64) {
2020 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2021 return;
2022 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023 call_out = true;
2024 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002025 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002027 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2028 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 break;
2030 case Instruction::AND_LONG_2ADDR:
2031 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002032 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2033 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002034 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035 }
2036 first_op = kOpAnd;
2037 second_op = kOpAnd;
2038 break;
2039 case Instruction::OR_LONG:
2040 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002041 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2042 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002043 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 return;
2045 }
2046 first_op = kOpOr;
2047 second_op = kOpOr;
2048 break;
2049 case Instruction::XOR_LONG:
2050 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002051 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2052 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002053 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 return;
2055 }
2056 first_op = kOpXor;
2057 second_op = kOpXor;
2058 break;
2059 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002060 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 return;
2062 }
2063 default:
2064 LOG(FATAL) << "Invalid long arith op";
2065 }
2066 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002067 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002068 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002069 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2072 mir_to_lir->TargetReg(kArg1));
2073 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2074 mir_to_lir->TargetReg(kArg3));
2075 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2076 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2077 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2078 mir_to_lir->TargetReg(kArg3)));
2079 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002081 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002082 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002083 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 }
2085 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002086 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002087 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002089 rl_result = mir_to_lir->GetReturnWideAlt();
2090 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 }
2092}
2093
Andreas Gampe2f244e92014-05-08 03:35:25 -07002094void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2095 RegLocation rl_src1, RegLocation rl_src2) {
2096 if (Is64BitInstructionSet(cu_->instruction_set)) {
2097 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2098 } else {
2099 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2100 }
2101}
2102
Mark Mendelle87f9b52014-04-30 14:13:18 -04002103void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2104 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2105 LoadConstantNoClobber(rl_result.reg, value);
2106 StoreValue(rl_dest, rl_result);
2107 if (value == 0) {
2108 Workaround7250540(rl_dest, rl_result.reg);
2109 }
2110}
2111
Andreas Gampe2f244e92014-05-08 03:35:25 -07002112template <size_t pointer_size>
2113void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002114 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002115 /*
2116 * Don't optimize the register usage since it calls out to support
2117 * functions
2118 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002119 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2120
Brian Carlstrom7940e442013-07-12 13:46:57 -07002121 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2123 if (rl_dest.wide) {
2124 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002125 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002126 StoreValueWide(rl_dest, rl_result);
2127 } else {
2128 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002129 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002130 StoreValue(rl_dest, rl_result);
2131 }
2132}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002133template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2134 RegLocation rl_dest, RegLocation rl_src);
2135template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2136 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002137
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002138class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2139 public:
2140 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2141 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2142 }
2143
2144 void Compile() OVERRIDE {
2145 m2l_->ResetRegPool();
2146 m2l_->ResetDefTracking();
2147 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002148 if (Is64BitInstructionSet(cu_->instruction_set)) {
2149 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2150 } else {
2151 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2152 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002153 if (cont_ != nullptr) {
2154 m2l_->OpUnconditionalBranch(cont_);
2155 }
2156 }
2157};
2158
Brian Carlstrom7940e442013-07-12 13:46:57 -07002159/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002160void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002161 if (Runtime::Current()->ExplicitSuspendChecks()) {
2162 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2163 return;
2164 }
2165 FlushAllRegs();
2166 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002167 LIR* cont = NewLIR0(kPseudoTargetLabel);
2168 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002169 } else {
2170 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2171 return;
2172 }
2173 FlushAllRegs(); // TODO: needed?
2174 LIR* inst = CheckSuspendUsingLoad();
2175 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002176 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002177}
2178
2179/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002180void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002181 if (Runtime::Current()->ExplicitSuspendChecks()) {
2182 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2183 OpUnconditionalBranch(target);
2184 return;
2185 }
2186 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002187 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002188 LIR* branch = OpUnconditionalBranch(nullptr);
2189 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002190 } else {
2191 // For the implicit suspend check, just perform the trigger
2192 // load and branch to the target.
2193 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2194 OpUnconditionalBranch(target);
2195 return;
2196 }
2197 FlushAllRegs();
2198 LIR* inst = CheckSuspendUsingLoad();
2199 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002200 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002201 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202}
2203
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002204/* Call out to helper assembly routine that will null check obj and then lock it. */
2205void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2206 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002207 if (Is64BitInstructionSet(cu_->instruction_set)) {
2208 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2209 } else {
2210 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2211 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002212}
2213
2214/* Call out to helper assembly routine that will null check obj and then unlock it. */
2215void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2216 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002217 if (Is64BitInstructionSet(cu_->instruction_set)) {
2218 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2219 } else {
2220 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2221 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002222}
2223
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002224/* Generic code for generating a wide constant into a VR. */
2225void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2226 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002227 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002228 StoreValueWide(rl_dest, rl_result);
2229}
2230
Brian Carlstrom7940e442013-07-12 13:46:57 -07002231} // namespace art