blob: f9081cea082d7cd39e6fc86bea1e5fe300821802 [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);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010047 barrier->u.m.def_mask = &kEncodeAll;
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) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100450 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee695d13a2014-04-19 13:32:20 -0700451 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 }
453 }
454 /*
455 * TUNING note: generated code here could be much improved, but
456 * this is an uncommon operation and isn't especially performance
457 * critical.
458 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700459 // This is addressing the stack, which may be out of the 4G area.
460 RegStorage r_src = cu_->target64 ? AllocTempWide() : AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800461 RegStorage r_dst = AllocTemp();
462 RegStorage r_idx = AllocTemp();
463 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700464 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 case kThumb2:
466 r_val = TargetReg(kLr);
467 break;
468 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700469 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 FreeTemp(TargetReg(kRet0));
471 r_val = AllocTemp();
472 break;
473 case kMips:
474 r_val = AllocTemp();
475 break;
476 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
477 }
478 // Set up source pointer
479 RegLocation rl_first = info->args[0];
480 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
481 // Set up the target pointer
482 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
483 mirror::Array::DataOffset(component_size).Int32Value());
484 // Set up the loop counter (known to be > 0)
485 LoadConstant(r_idx, elems - 1);
486 // Generate the copy loop. Going backwards for convenience
487 LIR* target = NewLIR0(kPseudoTargetLabel);
488 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100489 {
490 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
491 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
492 // NOTE: No dalvik register annotation, local optimizations will be stopped
493 // by the loop boundaries.
494 }
buzbee695d13a2014-04-19 13:32:20 -0700495 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 FreeTemp(r_val);
497 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700498 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 // Restore the target pointer
500 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
501 -mirror::Array::DataOffset(component_size).Int32Value());
502 }
503 } else if (!info->is_range) {
504 // TUNING: interleave
505 for (int i = 0; i < elems; i++) {
506 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700507 Store32Disp(TargetReg(kRet0),
508 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800510 if (IsTemp(rl_arg.reg)) {
511 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 }
513 }
514 }
515 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700516 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 }
518}
519
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800520//
521// Slow path to ensure a class is initialized for sget/sput.
522//
523class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
524 public:
buzbee2700f7e2014-03-07 09:46:20 -0800525 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
526 RegStorage r_base) :
527 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
528 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800529 }
530
531 void Compile() {
532 LIR* unresolved_target = GenerateTargetLabel();
533 uninit_->target = unresolved_target;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700534 if (Is64BitInstructionSet(cu_->instruction_set)) {
535 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
536 storage_index_, true);
537 } else {
538 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
539 storage_index_, true);
540 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800541 // Copy helper's result into r_base, a no-op on all but MIPS.
542 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
543
544 m2l_->OpUnconditionalBranch(cont_);
545 }
546
547 private:
548 LIR* const uninit_;
549 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800550 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800551};
552
Andreas Gampe2f244e92014-05-08 03:35:25 -0700553template <size_t pointer_size>
554static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
555 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
556 ThreadOffset<pointer_size> setter_offset =
557 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
558 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
559 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
560 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
561 true);
562}
563
Vladimir Markobe0e5462014-02-26 11:24:15 +0000564void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700565 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000566 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
567 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100568 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
569 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
570 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000571 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800572 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000573 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100575 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700576 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700577 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800578 if (IsTemp(rl_method.reg)) {
579 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 }
581 } else {
582 // Medium path, static storage base in a different class which requires checks that the other
583 // class is initialized.
584 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 // May do runtime call so everything to home locations.
587 FlushAllRegs();
588 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800589 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 LockTemp(r_method);
591 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800592 r_base = TargetReg(kArg0);
593 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700594 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000595 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
596 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800597 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000598 if (!field_info.IsInitialized() &&
599 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800600 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800601
602 // The slow path is invoked if the r_base is NULL or the class pointed
603 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800605 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800606 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800607 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800608 mirror::Class::StatusOffset().Int32Value(),
609 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800610 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800611
buzbee2700f7e2014-03-07 09:46:20 -0800612 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614
615 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700616 // Ensure load of status and load of value don't re-order.
617 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 FreeTemp(r_method);
620 }
621 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100622 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100624 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100626 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000628 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800629 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100631 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800632 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100634 } else {
635 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 }
637 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800638 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800640 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 } else {
642 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700643 if (Is64BitInstructionSet(cu_->instruction_set)) {
644 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
645 } else {
646 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
647 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
649}
650
Andreas Gampe2f244e92014-05-08 03:35:25 -0700651template <size_t pointer_size>
652static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
653 const MirSFieldLoweringInfo* field_info) {
654 ThreadOffset<pointer_size> getter_offset =
655 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
656 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
657 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
658 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
659}
660
Vladimir Markobe0e5462014-02-26 11:24:15 +0000661void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700662 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000663 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
664 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100665 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
666 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
667 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000668 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800669 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000670 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 // Fast path, static storage base is this method's class
672 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700673 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700674 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 } else {
676 // Medium path, static storage base in a different class which requires checks that the other
677 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000678 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 // May do runtime call so everything to home locations.
680 FlushAllRegs();
681 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800682 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 LockTemp(r_method);
684 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800685 r_base = TargetReg(kArg0);
686 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700687 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000688 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
689 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800690 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000691 if (!field_info.IsInitialized() &&
692 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800693 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800694
695 // The slow path is invoked if the r_base is NULL or the class pointed
696 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800697 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800698 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800699 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800700 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800701 mirror::Class::StatusOffset().Int32Value(),
702 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800703 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800704
buzbee2700f7e2014-03-07 09:46:20 -0800705 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800707
708 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700709 // Ensure load of status and load of value don't re-order.
710 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 FreeTemp(r_method);
713 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800714 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100715 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
716 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800717
Vladimir Marko674744e2014-04-24 15:18:26 +0100718 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800719 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100720 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800721 // Without context sensitive analysis, we must issue the most conservative barriers.
722 // In this case, either a load or store may follow so we issue both barriers.
723 GenMemBarrier(kLoadLoad);
724 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100725 } else {
726 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800727 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100728 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800729
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 if (is_long_or_double) {
731 StoreValueWide(rl_dest, rl_result);
732 } else {
733 StoreValue(rl_dest, rl_result);
734 }
735 } else {
736 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700737 if (Is64BitInstructionSet(cu_->instruction_set)) {
738 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
739 } else {
740 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
741 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700743 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 StoreValueWide(rl_dest, rl_result);
745 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700746 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 StoreValue(rl_dest, rl_result);
748 }
749 }
750}
751
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800752// Generate code for all slow paths.
753void Mir2Lir::HandleSlowPaths() {
754 int n = slow_paths_.Size();
755 for (int i = 0; i < n; ++i) {
756 LIRSlowPath* slowpath = slow_paths_.Get(i);
757 slowpath->Compile();
758 }
759 slow_paths_.Reset();
760}
761
Andreas Gampe2f244e92014-05-08 03:35:25 -0700762template <size_t pointer_size>
763static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
764 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
765 ThreadOffset<pointer_size> getter_offset =
766 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
767 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
768 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
769 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
770 true);
771}
772
Vladimir Markobe0e5462014-02-26 11:24:15 +0000773void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700775 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000776 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
777 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100778 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
779 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
780 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
781 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000782 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700783 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100784 GenNullCheck(rl_obj.reg, opt_flags);
785 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
786 int field_offset = field_info.FieldOffset().Int32Value();
787 if (field_info.IsVolatile()) {
788 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
789 MarkPossibleNullPointerException(opt_flags);
790 // Without context sensitive analysis, we must issue the most conservative barriers.
791 // In this case, either a load or store may follow so we issue both barriers.
792 GenMemBarrier(kLoadLoad);
793 GenMemBarrier(kLoadStore);
794 } else {
795 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
796 MarkPossibleNullPointerException(opt_flags);
797 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 StoreValueWide(rl_dest, rl_result);
800 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 StoreValue(rl_dest, rl_result);
802 }
803 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700804 if (Is64BitInstructionSet(cu_->instruction_set)) {
805 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
806 } else {
807 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
808 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700810 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 StoreValueWide(rl_dest, rl_result);
812 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700813 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 StoreValue(rl_dest, rl_result);
815 }
816 }
817}
818
Andreas Gampe2f244e92014-05-08 03:35:25 -0700819template <size_t pointer_size>
820static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
821 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
822 RegLocation rl_src) {
823 ThreadOffset<pointer_size> setter_offset =
824 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
825 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
826 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
827 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
828 rl_obj, rl_src, true);
829}
830
Vladimir Markobe0e5462014-02-26 11:24:15 +0000831void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700833 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000834 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
835 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100836 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
837 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
838 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
839 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000840 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700841 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100843 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 } else {
845 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100846 }
847 GenNullCheck(rl_obj.reg, opt_flags);
848 int field_offset = field_info.FieldOffset().Int32Value();
849 if (field_info.IsVolatile()) {
850 // There might have been a store before this volatile one so insert StoreStore barrier.
851 GenMemBarrier(kStoreStore);
852 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800853 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100854 // A load might follow the volatile store so insert a StoreLoad barrier.
855 GenMemBarrier(kStoreLoad);
856 } else {
857 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
858 MarkPossibleNullPointerException(opt_flags);
859 }
860 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
861 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 }
863 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700864 if (Is64BitInstructionSet(cu_->instruction_set)) {
865 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
866 } else {
867 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
868 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 }
870}
871
Andreas Gampe2f244e92014-05-08 03:35:25 -0700872template <size_t pointer_size>
873static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
874 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
875 ThreadOffset<pointer_size> helper = needs_range_check
876 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
877 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
878 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
879 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
880 true);
881}
882
Ian Rogersa9a82542013-10-04 11:17:26 -0700883void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
884 RegLocation rl_src) {
885 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
886 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
887 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700888 if (Is64BitInstructionSet(cu_->instruction_set)) {
889 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
890 } else {
891 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
892 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700893}
894
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700895void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800897 RegStorage res_reg = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -0700898 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
900 *cu_->dex_file,
901 type_idx)) {
902 // Call out to helper which resolves type and verifies access.
903 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700904 if (Is64BitInstructionSet(cu_->instruction_set)) {
905 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
906 type_idx, rl_method.reg, true);
907 } else {
908 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
909 type_idx, rl_method.reg, true);
910 }
buzbeea0cd2d72014-06-01 09:33:49 -0700911 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 StoreValue(rl_dest, rl_result);
913 } else {
914 // We're don't need access checks, load type from dex cache
915 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700916 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700917 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000918 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700919 LoadRefDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
921 type_idx) || SLOW_TYPE_PATH) {
922 // Slow path, at runtime test if type is null and if so initialize
923 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800924 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800925 LIR* cont = NewLIR0(kPseudoTargetLabel);
926
927 // Object to generate the slow path for class resolution.
928 class SlowPath : public LIRSlowPath {
929 public:
930 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
931 const RegLocation& rl_method, const RegLocation& rl_result) :
932 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
933 rl_method_(rl_method), rl_result_(rl_result) {
934 }
935
936 void Compile() {
937 GenerateTargetLabel();
938
Andreas Gampe2f244e92014-05-08 03:35:25 -0700939 if (Is64BitInstructionSet(cu_->instruction_set)) {
940 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
941 rl_method_.reg, true);
942 } else {
943 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
944 rl_method_.reg, true);
945 }
buzbee2700f7e2014-03-07 09:46:20 -0800946 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800947
948 m2l_->OpUnconditionalBranch(cont_);
949 }
950
951 private:
952 const int type_idx_;
953 const RegLocation rl_method_;
954 const RegLocation rl_result_;
955 };
956
957 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800958 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800959
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800961 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 // Fast path, we're done - just store result
963 StoreValue(rl_dest, rl_result);
964 }
965 }
966}
967
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700968void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000970 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
971 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
973 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
974 // slow path, resolve string if not in dex cache
975 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700976 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800977
978 // If the Method* is already in a register, we can save a copy.
979 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800980 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800981 if (rl_method.location == kLocPhysReg) {
982 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800983 DCHECK(!IsTemp(rl_method.reg));
984 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800985 } else {
986 r_method = TargetReg(kArg2);
987 LoadCurrMethodDirect(r_method);
988 }
buzbee695d13a2014-04-19 13:32:20 -0700989 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
990 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800991
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 // Might call out to helper, which will return resolved string in kRet0
buzbeea0cd2d72014-06-01 09:33:49 -0700993 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700994 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
995 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800996
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700997 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800998 // Object to generate the slow path for string resolution.
999 class SlowPath : public LIRSlowPath {
1000 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001001 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1002 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1003 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001004 }
1005
1006 void Compile() {
1007 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001008 if (Is64BitInstructionSet(cu_->instruction_set)) {
1009 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1010 r_method_, string_idx_, true);
1011 } else {
1012 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1013 r_method_, string_idx_, true);
1014 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001015 m2l_->OpUnconditionalBranch(cont_);
1016 }
1017
1018 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001019 const RegStorage r_method_;
1020 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001021 };
1022
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001023 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001025
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001027 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 } else {
1029 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001030 RegStorage res_reg = AllocTempRef();
1031 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001032 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeea0cd2d72014-06-01 09:33:49 -07001033 LoadRefDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 StoreValue(rl_dest, rl_result);
1035 }
1036}
1037
Andreas Gampe2f244e92014-05-08 03:35:25 -07001038template <size_t pointer_size>
1039static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1040 RegLocation rl_dest) {
1041 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 // alloc will always check for resolution, do we also need to verify
1043 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001044 ThreadOffset<pointer_size> func_offset(-1);
1045 const DexFile* dex_file = cu->dex_file;
1046 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001047 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001048 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 bool is_type_initialized;
1050 bool use_direct_type_ptr;
1051 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001052 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001053 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001054 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1055 &direct_type_ptr, &is_finalizable) &&
1056 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001057 // The fast path.
1058 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001059 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001060 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001061 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1062 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001063 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001064 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1065 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001066 }
1067 } else {
1068 // Use the direct pointer.
1069 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001070 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1071 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001073 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1074 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 }
1076 }
1077 } else {
1078 // The slow path.
1079 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001080 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1081 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001082 }
1083 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001085 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1086 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 }
buzbeea0cd2d72014-06-01 09:33:49 -07001088 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001089 mir_to_lir->StoreValue(rl_dest, rl_result);
1090}
1091
1092/*
1093 * Let helper function take care of everything. Will
1094 * call Class::NewInstanceFromCode(type_idx, method);
1095 */
1096void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1097 if (Is64BitInstructionSet(cu_->instruction_set)) {
1098 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1099 } else {
1100 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102}
1103
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001104void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001106 if (Is64BitInstructionSet(cu_->instruction_set)) {
1107 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1108 } else {
1109 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1110 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111}
1112
1113// For final classes there are no sub-classes to check and so we can answer the instance-of
1114// question with simple comparisons.
1115void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1116 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001117 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001118 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001119
buzbeea0cd2d72014-06-01 09:33:49 -07001120 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001122 RegStorage result_reg = rl_result.reg;
1123 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 result_reg = AllocTypedTemp(false, kCoreReg);
1125 }
1126 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001127 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128
buzbeea0cd2d72014-06-01 09:33:49 -07001129 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1130 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131
1132 LoadCurrMethodDirect(check_class);
1133 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001134 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1135 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 } else {
buzbee695d13a2014-04-19 13:32:20 -07001137 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1138 check_class);
1139 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001140 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001141 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 }
1143
1144 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001145 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 if (cu_->instruction_set == kThumb2) {
1147 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001148 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001150 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 } else {
1152 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1153 LoadConstant(result_reg, 1); // eq case - load true
1154 }
1155 LIR* target = NewLIR0(kPseudoTargetLabel);
1156 null_branchover->target = target;
1157 if (ne_branchover != NULL) {
1158 ne_branchover->target = target;
1159 }
1160 FreeTemp(object_class);
1161 FreeTemp(check_class);
1162 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001163 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 FreeTemp(result_reg);
1165 }
1166 StoreValue(rl_dest, rl_result);
1167}
1168
1169void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1170 bool type_known_abstract, bool use_declaring_class,
1171 bool can_assume_type_is_in_dex_cache,
1172 uint32_t type_idx, RegLocation rl_dest,
1173 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001174 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001175 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001176
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 FlushAllRegs();
1178 // May generate a call - use explicit registers
1179 LockCallTemps();
1180 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001181 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 if (needs_access_check) {
1183 // Check we have access to type_idx and if not throw IllegalAccessError,
1184 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001185 if (Is64BitInstructionSet(cu_->instruction_set)) {
1186 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1187 type_idx, true);
1188 } else {
1189 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1190 type_idx, true);
1191 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1193 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1194 } else if (use_declaring_class) {
1195 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001196 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001197 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 } else {
1199 // Load dex cache entry into class_reg (kArg2)
1200 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001201 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1202 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001203 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001204 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 if (!can_assume_type_is_in_dex_cache) {
1206 // Need to test presence of type in dex cache at runtime
1207 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1208 // Not resolved
1209 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001210 if (Is64BitInstructionSet(cu_->instruction_set)) {
1211 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1212 } else {
1213 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1214 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001215 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1217 // Rejoin code paths
1218 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1219 hop_branch->target = hop_target;
1220 }
1221 }
1222 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001223 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 if (cu_->instruction_set == kMips) {
1225 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001226 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 }
1228 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1229
1230 /* load object->klass_ */
1231 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001232 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1234 LIR* branchover = NULL;
1235 if (type_known_final) {
1236 // rl_result == ref == null == 0.
1237 if (cu_->instruction_set == kThumb2) {
1238 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001239 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001240 LoadConstant(rl_result.reg, 1); // .eq case - load true
1241 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001242 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001244 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001246 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 }
1248 } else {
1249 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001250 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1251 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1252 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001253 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 if (!type_known_abstract) {
1255 /* Uses conditional nullification */
1256 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001257 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1259 }
1260 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1261 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001262 if (it != nullptr) {
1263 OpEndIT(it);
1264 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 FreeTemp(r_tgt);
1266 } else {
1267 if (!type_known_abstract) {
1268 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001269 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001270 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1271 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001272 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1273 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1274 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001275 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1276 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1277 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 }
1279 }
1280 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001281 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 /* branch targets here */
1283 LIR* target = NewLIR0(kPseudoTargetLabel);
1284 StoreValue(rl_dest, rl_result);
1285 branch1->target = target;
1286 if (branchover != NULL) {
1287 branchover->target = target;
1288 }
1289}
1290
1291void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1292 bool type_known_final, type_known_abstract, use_declaring_class;
1293 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1294 *cu_->dex_file,
1295 type_idx,
1296 &type_known_final,
1297 &type_known_abstract,
1298 &use_declaring_class);
1299 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1300 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1301
1302 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1303 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1304 } else {
1305 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1306 use_declaring_class, can_assume_type_is_in_dex_cache,
1307 type_idx, rl_dest, rl_src);
1308 }
1309}
1310
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001311void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312 bool type_known_final, type_known_abstract, use_declaring_class;
1313 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1314 *cu_->dex_file,
1315 type_idx,
1316 &type_known_final,
1317 &type_known_abstract,
1318 &use_declaring_class);
1319 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1320 // of the exception throw path.
1321 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001322 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 // Verifier type analysis proved this check cast would never cause an exception.
1324 return;
1325 }
1326 FlushAllRegs();
1327 // May generate a call - use explicit registers
1328 LockCallTemps();
1329 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001330 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 if (needs_access_check) {
1332 // Check we have access to type_idx and if not throw IllegalAccessError,
1333 // returns Class* in kRet0
1334 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001335 if (Is64BitInstructionSet(cu_->instruction_set)) {
1336 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1337 type_idx, TargetReg(kArg1), true);
1338 } else {
1339 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1340 type_idx, TargetReg(kArg1), true);
1341 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1343 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001344 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1345 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 } else {
1347 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001348 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1349 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001350 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001351 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1353 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001354 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1355 LIR* cont = NewLIR0(kPseudoTargetLabel);
1356
1357 // Slow path to initialize the type. Executed if the type is NULL.
1358 class SlowPath : public LIRSlowPath {
1359 public:
1360 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001361 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001362 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1363 class_reg_(class_reg) {
1364 }
1365
1366 void Compile() {
1367 GenerateTargetLabel();
1368
1369 // Call out to helper, which will return resolved type in kArg0
1370 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001371 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1372 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1373 m2l_->TargetReg(kArg1), true);
1374 } else {
1375 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1376 m2l_->TargetReg(kArg1), true);
1377 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001378 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1379 m2l_->OpUnconditionalBranch(cont_);
1380 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001381
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001382 public:
1383 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001384 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385 };
1386
buzbee2700f7e2014-03-07 09:46:20 -08001387 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 }
1389 }
1390 // At this point, class_reg (kArg2) has class
1391 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001392
1393 // Slow path for the case where the classes are not equal. In this case we need
1394 // to call a helper function to do the check.
1395 class SlowPath : public LIRSlowPath {
1396 public:
1397 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1398 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1399 }
1400
1401 void Compile() {
1402 GenerateTargetLabel();
1403
1404 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001405 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1406 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001407 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001408 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1409 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1410 m2l_->TargetReg(kArg1), true);
1411 } else {
1412 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1413 m2l_->TargetReg(kArg1), true);
1414 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415
1416 m2l_->OpUnconditionalBranch(cont_);
1417 }
1418
1419 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001420 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001421 };
1422
1423 if (type_known_abstract) {
1424 // Easier case, run slow path if target is non-null (slow path will load from target)
1425 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1426 LIR* cont = NewLIR0(kPseudoTargetLabel);
1427 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1428 } else {
1429 // Harder, more common case. We need to generate a forward branch over the load
1430 // if the target is null. If it's non-null we perform the load and branch to the
1431 // slow path if the classes are not equal.
1432
1433 /* Null is OK - continue */
1434 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1435 /* load object->klass_ */
1436 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001437 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001438
1439 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1440 LIR* cont = NewLIR0(kPseudoTargetLabel);
1441
1442 // Add the slow path that will not perform load since this is already done.
1443 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1444
1445 // Set the null check to branch to the continuation.
1446 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 }
1448}
1449
1450void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001451 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 RegLocation rl_result;
1453 if (cu_->instruction_set == kThumb2) {
1454 /*
1455 * NOTE: This is the one place in the code in which we might have
1456 * as many as six live temporary registers. There are 5 in the normal
1457 * set for Arm. Until we have spill capabilities, temporarily add
1458 * lr to the temp set. It is safe to do this locally, but note that
1459 * lr is used explicitly elsewhere in the code generator and cannot
1460 * normally be used as a general temp register.
1461 */
1462 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1463 FreeTemp(TargetReg(kLr)); // and make it available
1464 }
1465 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1466 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1467 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1468 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001469 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1470 RegStorage t_reg = AllocTemp();
1471 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1472 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1473 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 FreeTemp(t_reg);
1475 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001476 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1477 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 }
1479 /*
1480 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1481 * following StoreValueWide might need to allocate a temp register.
1482 * To further work around the lack of a spill capability, explicitly
1483 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1484 * Remove when spill is functional.
1485 */
1486 FreeRegLocTemps(rl_result, rl_src1);
1487 FreeRegLocTemps(rl_result, rl_src2);
1488 StoreValueWide(rl_dest, rl_result);
1489 if (cu_->instruction_set == kThumb2) {
1490 Clobber(TargetReg(kLr));
1491 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1492 }
1493}
1494
1495
Andreas Gampe2f244e92014-05-08 03:35:25 -07001496template <size_t pointer_size>
1497static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1498 RegLocation rl_shift) {
1499 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500
1501 switch (opcode) {
1502 case Instruction::SHL_LONG:
1503 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001504 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 break;
1506 case Instruction::SHR_LONG:
1507 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001508 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 break;
1510 case Instruction::USHR_LONG:
1511 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001512 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 break;
1514 default:
1515 LOG(FATAL) << "Unexpected case";
1516 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001517 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1518 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1519}
1520
1521void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1522 RegLocation rl_src1, RegLocation rl_shift) {
1523 if (Is64BitInstructionSet(cu_->instruction_set)) {
1524 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1525 } else {
1526 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1527 }
buzbeea0cd2d72014-06-01 09:33:49 -07001528 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 StoreValueWide(rl_dest, rl_result);
1530}
1531
1532
1533void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001534 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001535 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 OpKind op = kOpBkpt;
1537 bool is_div_rem = false;
1538 bool check_zero = false;
1539 bool unary = false;
1540 RegLocation rl_result;
1541 bool shift_op = false;
1542 switch (opcode) {
1543 case Instruction::NEG_INT:
1544 op = kOpNeg;
1545 unary = true;
1546 break;
1547 case Instruction::NOT_INT:
1548 op = kOpMvn;
1549 unary = true;
1550 break;
1551 case Instruction::ADD_INT:
1552 case Instruction::ADD_INT_2ADDR:
1553 op = kOpAdd;
1554 break;
1555 case Instruction::SUB_INT:
1556 case Instruction::SUB_INT_2ADDR:
1557 op = kOpSub;
1558 break;
1559 case Instruction::MUL_INT:
1560 case Instruction::MUL_INT_2ADDR:
1561 op = kOpMul;
1562 break;
1563 case Instruction::DIV_INT:
1564 case Instruction::DIV_INT_2ADDR:
1565 check_zero = true;
1566 op = kOpDiv;
1567 is_div_rem = true;
1568 break;
1569 /* NOTE: returns in kArg1 */
1570 case Instruction::REM_INT:
1571 case Instruction::REM_INT_2ADDR:
1572 check_zero = true;
1573 op = kOpRem;
1574 is_div_rem = true;
1575 break;
1576 case Instruction::AND_INT:
1577 case Instruction::AND_INT_2ADDR:
1578 op = kOpAnd;
1579 break;
1580 case Instruction::OR_INT:
1581 case Instruction::OR_INT_2ADDR:
1582 op = kOpOr;
1583 break;
1584 case Instruction::XOR_INT:
1585 case Instruction::XOR_INT_2ADDR:
1586 op = kOpXor;
1587 break;
1588 case Instruction::SHL_INT:
1589 case Instruction::SHL_INT_2ADDR:
1590 shift_op = true;
1591 op = kOpLsl;
1592 break;
1593 case Instruction::SHR_INT:
1594 case Instruction::SHR_INT_2ADDR:
1595 shift_op = true;
1596 op = kOpAsr;
1597 break;
1598 case Instruction::USHR_INT:
1599 case Instruction::USHR_INT_2ADDR:
1600 shift_op = true;
1601 op = kOpLsr;
1602 break;
1603 default:
1604 LOG(FATAL) << "Invalid word arith op: " << opcode;
1605 }
1606 if (!is_div_rem) {
1607 if (unary) {
1608 rl_src1 = LoadValue(rl_src1, kCoreReg);
1609 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001612 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001613 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001614 RegStorage t_reg = AllocTemp();
1615 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 rl_src1 = LoadValue(rl_src1, kCoreReg);
1617 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001618 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 FreeTemp(t_reg);
1620 } else {
1621 rl_src1 = LoadValue(rl_src1, kCoreReg);
1622 rl_src2 = LoadValue(rl_src2, kCoreReg);
1623 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001624 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 }
1626 }
1627 StoreValue(rl_dest, rl_result);
1628 } else {
Dave Allison70202782013-10-22 17:52:19 -07001629 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 +01001630 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 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);
Brian Carlstrom7940e442013-07-12 13:46:57 -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 } else if (cu_->instruction_set == kThumb2) {
1639 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1640 // Use ARM SDIV instruction for division. For remainder we also need to
1641 // calculate using a MUL and subtract.
1642 rl_src1 = LoadValue(rl_src1, kCoreReg);
1643 rl_src2 = LoadValue(rl_src2, kCoreReg);
1644 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001645 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001646 }
buzbee2700f7e2014-03-07 09:46:20 -08001647 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001648 done = true;
1649 }
1650 }
1651
1652 // If we haven't already generated the code use the callout function.
1653 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 FlushAllRegs(); /* Send everything to home location */
1655 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001656 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1657 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1658 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1660 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001661 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 }
Dave Allison70202782013-10-22 17:52:19 -07001663 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001664 if (Is64BitInstructionSet(cu_->instruction_set)) {
1665 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1666 } else {
1667 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1668 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001670 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 else
1672 rl_result = GetReturnAlt();
1673 }
1674 StoreValue(rl_dest, rl_result);
1675 }
1676}
1677
1678/*
1679 * The following are the first-level codegen routines that analyze the format
1680 * of each bytecode then either dispatch special purpose codegen routines
1681 * or produce corresponding Thumb instructions directly.
1682 */
1683
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001685static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 x &= x - 1;
1687 return (x & (x - 1)) == 0;
1688}
1689
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1691// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001692bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001693 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1695 return false;
1696 }
1697 // No divide instruction for Arm, so check for more special cases
1698 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001699 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 }
1701 int k = LowestSetBit(lit);
1702 if (k >= 30) {
1703 // Avoid special cases.
1704 return false;
1705 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001706 rl_src = LoadValue(rl_src, kCoreReg);
1707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001708 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001709 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 if (lit == 2) {
1711 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001712 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1713 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1714 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001715 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001716 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1719 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001720 }
1721 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001722 RegStorage t_reg1 = AllocTemp();
1723 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001725 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1726 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001728 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001730 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001732 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001734 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735 }
1736 }
1737 StoreValue(rl_dest, rl_result);
1738 return true;
1739}
1740
1741// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1742// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001743bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001744 if (lit < 0) {
1745 return false;
1746 }
1747 if (lit == 0) {
1748 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1749 LoadConstant(rl_result.reg, 0);
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752 }
1753 if (lit == 1) {
1754 rl_src = LoadValue(rl_src, kCoreReg);
1755 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1756 OpRegCopy(rl_result.reg, rl_src.reg);
1757 StoreValue(rl_dest, rl_result);
1758 return true;
1759 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001760 // There is RegRegRegShift on Arm, so check for more special cases
1761 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001762 return EasyMultiply(rl_src, rl_dest, lit);
1763 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 // Can we simplify this multiplication?
1765 bool power_of_two = false;
1766 bool pop_count_le2 = false;
1767 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001768 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 power_of_two = true;
1770 } else if (IsPopCountLE2(lit)) {
1771 pop_count_le2 = true;
1772 } else if (IsPowerOfTwo(lit + 1)) {
1773 power_of_two_minus_one = true;
1774 } else {
1775 return false;
1776 }
1777 rl_src = LoadValue(rl_src, kCoreReg);
1778 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1779 if (power_of_two) {
1780 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001781 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001782 } else if (pop_count_le2) {
1783 // Shift and add and shift.
1784 int first_bit = LowestSetBit(lit);
1785 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1786 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1787 } else {
1788 // Reverse subtract: (src << (shift + 1)) - src.
1789 DCHECK(power_of_two_minus_one);
1790 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001791 RegStorage t_reg = AllocTemp();
1792 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1793 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 }
1795 StoreValue(rl_dest, rl_result);
1796 return true;
1797}
1798
1799void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001800 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001801 RegLocation rl_result;
1802 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1803 int shift_op = false;
1804 bool is_div = false;
1805
1806 switch (opcode) {
1807 case Instruction::RSUB_INT_LIT8:
1808 case Instruction::RSUB_INT: {
1809 rl_src = LoadValue(rl_src, kCoreReg);
1810 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1811 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001812 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001814 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1815 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001816 }
1817 StoreValue(rl_dest, rl_result);
1818 return;
1819 }
1820
1821 case Instruction::SUB_INT:
1822 case Instruction::SUB_INT_2ADDR:
1823 lit = -lit;
1824 // Intended fallthrough
1825 case Instruction::ADD_INT:
1826 case Instruction::ADD_INT_2ADDR:
1827 case Instruction::ADD_INT_LIT8:
1828 case Instruction::ADD_INT_LIT16:
1829 op = kOpAdd;
1830 break;
1831 case Instruction::MUL_INT:
1832 case Instruction::MUL_INT_2ADDR:
1833 case Instruction::MUL_INT_LIT8:
1834 case Instruction::MUL_INT_LIT16: {
1835 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1836 return;
1837 }
1838 op = kOpMul;
1839 break;
1840 }
1841 case Instruction::AND_INT:
1842 case Instruction::AND_INT_2ADDR:
1843 case Instruction::AND_INT_LIT8:
1844 case Instruction::AND_INT_LIT16:
1845 op = kOpAnd;
1846 break;
1847 case Instruction::OR_INT:
1848 case Instruction::OR_INT_2ADDR:
1849 case Instruction::OR_INT_LIT8:
1850 case Instruction::OR_INT_LIT16:
1851 op = kOpOr;
1852 break;
1853 case Instruction::XOR_INT:
1854 case Instruction::XOR_INT_2ADDR:
1855 case Instruction::XOR_INT_LIT8:
1856 case Instruction::XOR_INT_LIT16:
1857 op = kOpXor;
1858 break;
1859 case Instruction::SHL_INT_LIT8:
1860 case Instruction::SHL_INT:
1861 case Instruction::SHL_INT_2ADDR:
1862 lit &= 31;
1863 shift_op = true;
1864 op = kOpLsl;
1865 break;
1866 case Instruction::SHR_INT_LIT8:
1867 case Instruction::SHR_INT:
1868 case Instruction::SHR_INT_2ADDR:
1869 lit &= 31;
1870 shift_op = true;
1871 op = kOpAsr;
1872 break;
1873 case Instruction::USHR_INT_LIT8:
1874 case Instruction::USHR_INT:
1875 case Instruction::USHR_INT_2ADDR:
1876 lit &= 31;
1877 shift_op = true;
1878 op = kOpLsr;
1879 break;
1880
1881 case Instruction::DIV_INT:
1882 case Instruction::DIV_INT_2ADDR:
1883 case Instruction::DIV_INT_LIT8:
1884 case Instruction::DIV_INT_LIT16:
1885 case Instruction::REM_INT:
1886 case Instruction::REM_INT_2ADDR:
1887 case Instruction::REM_INT_LIT8:
1888 case Instruction::REM_INT_LIT16: {
1889 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001890 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001891 return;
1892 }
buzbee11b63d12013-08-27 07:34:17 -07001893 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001894 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001895 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001896 (opcode == Instruction::DIV_INT_LIT16)) {
1897 is_div = true;
1898 } else {
1899 is_div = false;
1900 }
buzbee11b63d12013-08-27 07:34:17 -07001901 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1902 return;
1903 }
Dave Allison70202782013-10-22 17:52:19 -07001904
1905 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001906 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001908 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001909 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001910 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001911 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1912 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001913 } else if (cu_->instruction_set == kThumb2) {
1914 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1915 // Use ARM SDIV instruction for division. For remainder we also need to
1916 // calculate using a MUL and subtract.
1917 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001918 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001919 done = true;
1920 }
1921 }
1922
1923 if (!done) {
1924 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1926 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001927 if (Is64BitInstructionSet(cu_->instruction_set)) {
1928 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1929 false);
1930 } else {
1931 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1932 false);
1933 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001935 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 else
1937 rl_result = GetReturnAlt();
1938 }
1939 StoreValue(rl_dest, rl_result);
1940 return;
1941 }
1942 default:
1943 LOG(FATAL) << "Unexpected opcode " << opcode;
1944 }
1945 rl_src = LoadValue(rl_src, kCoreReg);
1946 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001947 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001949 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001951 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 }
1953 StoreValue(rl_dest, rl_result);
1954}
1955
Andreas Gampe2f244e92014-05-08 03:35:25 -07001956template <size_t pointer_size>
1957static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1958 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001959 RegLocation rl_result;
1960 OpKind first_op = kOpBkpt;
1961 OpKind second_op = kOpBkpt;
1962 bool call_out = false;
1963 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001964 ThreadOffset<pointer_size> func_offset(-1);
1965 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001966
1967 switch (opcode) {
1968 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001969 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001970 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1971 return;
1972 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001973 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1974 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001975 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001976 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001977 RegStorage t_reg = mir_to_lir->AllocTemp();
1978 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1979 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1980 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1981 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001983 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1984 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001986 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 return;
1988 case Instruction::ADD_LONG:
1989 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001990 if (cu->instruction_set != kThumb2) {
1991 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 return;
1993 }
1994 first_op = kOpAdd;
1995 second_op = kOpAdc;
1996 break;
1997 case Instruction::SUB_LONG:
1998 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001999 if (cu->instruction_set != kThumb2) {
2000 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 return;
2002 }
2003 first_op = kOpSub;
2004 second_op = kOpSbc;
2005 break;
2006 case Instruction::MUL_LONG:
2007 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002008 if (cu->instruction_set != kMips) {
2009 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002010 return;
2011 } else {
2012 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002013 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2014 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 }
2016 break;
2017 case Instruction::DIV_LONG:
2018 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002019 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002020 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
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 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2026 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 break;
2028 case Instruction::REM_LONG:
2029 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002030 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002031 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2032 return;
2033 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 call_out = true;
2035 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002036 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002038 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2039 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002040 break;
2041 case Instruction::AND_LONG_2ADDR:
2042 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002043 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2044 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002045 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 }
2047 first_op = kOpAnd;
2048 second_op = kOpAnd;
2049 break;
2050 case Instruction::OR_LONG:
2051 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002052 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2053 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002054 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055 return;
2056 }
2057 first_op = kOpOr;
2058 second_op = kOpOr;
2059 break;
2060 case Instruction::XOR_LONG:
2061 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002062 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2063 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002064 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002065 return;
2066 }
2067 first_op = kOpXor;
2068 second_op = kOpXor;
2069 break;
2070 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 return;
2073 }
2074 default:
2075 LOG(FATAL) << "Invalid long arith op";
2076 }
2077 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002078 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002080 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002082 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2083 mir_to_lir->TargetReg(kArg1));
2084 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2085 mir_to_lir->TargetReg(kArg3));
2086 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2087 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2088 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2089 mir_to_lir->TargetReg(kArg3)));
2090 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002092 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002094 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 }
2096 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002097 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002098 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002099 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002100 rl_result = mir_to_lir->GetReturnWideAlt();
2101 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102 }
2103}
2104
Andreas Gampe2f244e92014-05-08 03:35:25 -07002105void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2106 RegLocation rl_src1, RegLocation rl_src2) {
2107 if (Is64BitInstructionSet(cu_->instruction_set)) {
2108 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2109 } else {
2110 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2111 }
2112}
2113
Mark Mendelle87f9b52014-04-30 14:13:18 -04002114void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2115 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2116 LoadConstantNoClobber(rl_result.reg, value);
2117 StoreValue(rl_dest, rl_result);
2118 if (value == 0) {
2119 Workaround7250540(rl_dest, rl_result.reg);
2120 }
2121}
2122
Andreas Gampe2f244e92014-05-08 03:35:25 -07002123template <size_t pointer_size>
2124void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002125 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002126 /*
2127 * Don't optimize the register usage since it calls out to support
2128 * functions
2129 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002130 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2131
Brian Carlstrom7940e442013-07-12 13:46:57 -07002132 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002133 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2134 if (rl_dest.wide) {
2135 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002136 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002137 StoreValueWide(rl_dest, rl_result);
2138 } else {
2139 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002140 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002141 StoreValue(rl_dest, rl_result);
2142 }
2143}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002144template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2145 RegLocation rl_dest, RegLocation rl_src);
2146template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2147 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002148
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002149class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2150 public:
2151 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2152 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2153 }
2154
2155 void Compile() OVERRIDE {
2156 m2l_->ResetRegPool();
2157 m2l_->ResetDefTracking();
2158 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002159 if (Is64BitInstructionSet(cu_->instruction_set)) {
2160 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2161 } else {
2162 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2163 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002164 if (cont_ != nullptr) {
2165 m2l_->OpUnconditionalBranch(cont_);
2166 }
2167 }
2168};
2169
Brian Carlstrom7940e442013-07-12 13:46:57 -07002170/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002171void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002172 if (Runtime::Current()->ExplicitSuspendChecks()) {
2173 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2174 return;
2175 }
2176 FlushAllRegs();
2177 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002178 LIR* cont = NewLIR0(kPseudoTargetLabel);
2179 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002180 } else {
2181 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2182 return;
2183 }
2184 FlushAllRegs(); // TODO: needed?
2185 LIR* inst = CheckSuspendUsingLoad();
2186 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002187 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002188}
2189
2190/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002191void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002192 if (Runtime::Current()->ExplicitSuspendChecks()) {
2193 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2194 OpUnconditionalBranch(target);
2195 return;
2196 }
2197 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002198 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002199 LIR* branch = OpUnconditionalBranch(nullptr);
2200 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002201 } else {
2202 // For the implicit suspend check, just perform the trigger
2203 // load and branch to the target.
2204 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2205 OpUnconditionalBranch(target);
2206 return;
2207 }
2208 FlushAllRegs();
2209 LIR* inst = CheckSuspendUsingLoad();
2210 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002211 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002212 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002213}
2214
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002215/* Call out to helper assembly routine that will null check obj and then lock it. */
2216void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2217 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002218 if (Is64BitInstructionSet(cu_->instruction_set)) {
2219 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2220 } else {
2221 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2222 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002223}
2224
2225/* Call out to helper assembly routine that will null check obj and then unlock it. */
2226void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2227 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002228 if (Is64BitInstructionSet(cu_->instruction_set)) {
2229 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2230 } else {
2231 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2232 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002233}
2234
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002235/* Generic code for generating a wide constant into a VR. */
2236void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2237 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002238 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002239 StoreValueWide(rl_dest, rl_result);
2240}
2241
Brian Carlstrom7940e442013-07-12 13:46:57 -07002242} // namespace art