blob: 8f6d716ecbed3a8d06584bed09c9f0fdd94c0a56 [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 */
buzbee2700f7e2014-03-07 09:46:20 -0800459 RegStorage r_src = AllocTemp();
460 RegStorage r_dst = AllocTemp();
461 RegStorage r_idx = AllocTemp();
462 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700463 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 case kThumb2:
465 r_val = TargetReg(kLr);
466 break;
467 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700468 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 FreeTemp(TargetReg(kRet0));
470 r_val = AllocTemp();
471 break;
472 case kMips:
473 r_val = AllocTemp();
474 break;
475 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
476 }
477 // Set up source pointer
478 RegLocation rl_first = info->args[0];
479 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
480 // Set up the target pointer
481 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
482 mirror::Array::DataOffset(component_size).Int32Value());
483 // Set up the loop counter (known to be > 0)
484 LoadConstant(r_idx, elems - 1);
485 // Generate the copy loop. Going backwards for convenience
486 LIR* target = NewLIR0(kPseudoTargetLabel);
487 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100488 {
489 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
490 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
491 // NOTE: No dalvik register annotation, local optimizations will be stopped
492 // by the loop boundaries.
493 }
buzbee695d13a2014-04-19 13:32:20 -0700494 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 FreeTemp(r_val);
496 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700497 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 // Restore the target pointer
499 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
500 -mirror::Array::DataOffset(component_size).Int32Value());
501 }
502 } else if (!info->is_range) {
503 // TUNING: interleave
504 for (int i = 0; i < elems; i++) {
505 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700506 Store32Disp(TargetReg(kRet0),
507 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800509 if (IsTemp(rl_arg.reg)) {
510 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 }
512 }
513 }
514 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700515 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 }
517}
518
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800519//
520// Slow path to ensure a class is initialized for sget/sput.
521//
522class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
523 public:
buzbee2700f7e2014-03-07 09:46:20 -0800524 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
525 RegStorage r_base) :
526 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
527 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800528 }
529
530 void Compile() {
531 LIR* unresolved_target = GenerateTargetLabel();
532 uninit_->target = unresolved_target;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700533 if (Is64BitInstructionSet(cu_->instruction_set)) {
534 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
535 storage_index_, true);
536 } else {
537 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
538 storage_index_, true);
539 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800540 // Copy helper's result into r_base, a no-op on all but MIPS.
541 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
542
543 m2l_->OpUnconditionalBranch(cont_);
544 }
545
546 private:
547 LIR* const uninit_;
548 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800549 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800550};
551
Andreas Gampe2f244e92014-05-08 03:35:25 -0700552template <size_t pointer_size>
553static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
554 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
555 ThreadOffset<pointer_size> setter_offset =
556 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
557 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
558 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
559 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
560 true);
561}
562
Vladimir Markobe0e5462014-02-26 11:24:15 +0000563void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700564 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000565 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
566 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100567 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
568 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
569 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000570 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800571 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000572 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100574 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700575 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700576 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800577 if (IsTemp(rl_method.reg)) {
578 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 }
580 } else {
581 // Medium path, static storage base in a different class which requires checks that the other
582 // class is initialized.
583 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000584 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 // May do runtime call so everything to home locations.
586 FlushAllRegs();
587 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800588 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 LockTemp(r_method);
590 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800591 r_base = TargetReg(kArg0);
592 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700593 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000594 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
595 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800596 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000597 if (!field_info.IsInitialized() &&
598 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800599 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800600
601 // The slow path is invoked if the r_base is NULL or the class pointed
602 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800604 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800605 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800606 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800607 mirror::Class::StatusOffset().Int32Value(),
608 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800609 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800610
buzbee2700f7e2014-03-07 09:46:20 -0800611 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000612 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800613
614 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700615 // Ensure load of status and load of value don't re-order.
616 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 FreeTemp(r_method);
619 }
620 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100621 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100623 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100625 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000627 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800628 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100630 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800631 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100633 } else {
634 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 }
636 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800637 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800639 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 } else {
641 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700642 if (Is64BitInstructionSet(cu_->instruction_set)) {
643 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
644 } else {
645 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
646 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
648}
649
Andreas Gampe2f244e92014-05-08 03:35:25 -0700650template <size_t pointer_size>
651static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
652 const MirSFieldLoweringInfo* field_info) {
653 ThreadOffset<pointer_size> getter_offset =
654 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
655 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
656 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
657 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
658}
659
Vladimir Markobe0e5462014-02-26 11:24:15 +0000660void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700661 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
663 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100664 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
665 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
666 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000667 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800668 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000669 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 // Fast path, static storage base is this method's class
671 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700672 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700673 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 } else {
675 // Medium path, static storage base in a different class which requires checks that the other
676 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000677 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 // May do runtime call so everything to home locations.
679 FlushAllRegs();
680 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800681 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 LockTemp(r_method);
683 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800684 r_base = TargetReg(kArg0);
685 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700686 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000687 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
688 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800689 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000690 if (!field_info.IsInitialized() &&
691 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800692 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800693
694 // The slow path is invoked if the r_base is NULL or the class pointed
695 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800696 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800697 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800698 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800699 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800700 mirror::Class::StatusOffset().Int32Value(),
701 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800702 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800703
buzbee2700f7e2014-03-07 09:46:20 -0800704 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000705 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800706
707 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700708 // Ensure load of status and load of value don't re-order.
709 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 FreeTemp(r_method);
712 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800713 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100714 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
715 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800716
Vladimir Marko674744e2014-04-24 15:18:26 +0100717 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800718 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100719 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800720 // Without context sensitive analysis, we must issue the most conservative barriers.
721 // In this case, either a load or store may follow so we issue both barriers.
722 GenMemBarrier(kLoadLoad);
723 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100724 } else {
725 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800726 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100727 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800728
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 if (is_long_or_double) {
730 StoreValueWide(rl_dest, rl_result);
731 } else {
732 StoreValue(rl_dest, rl_result);
733 }
734 } else {
735 FlushAllRegs(); // Everything to home locations
Andreas Gampe2f244e92014-05-08 03:35:25 -0700736 if (Is64BitInstructionSet(cu_->instruction_set)) {
737 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
738 } else {
739 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
740 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700742 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 StoreValueWide(rl_dest, rl_result);
744 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700745 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 StoreValue(rl_dest, rl_result);
747 }
748 }
749}
750
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800751// Generate code for all slow paths.
752void Mir2Lir::HandleSlowPaths() {
753 int n = slow_paths_.Size();
754 for (int i = 0; i < n; ++i) {
755 LIRSlowPath* slowpath = slow_paths_.Get(i);
756 slowpath->Compile();
757 }
758 slow_paths_.Reset();
759}
760
Andreas Gampe2f244e92014-05-08 03:35:25 -0700761template <size_t pointer_size>
762static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
763 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
764 ThreadOffset<pointer_size> getter_offset =
765 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
766 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
767 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
768 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
769 true);
770}
771
Vladimir Markobe0e5462014-02-26 11:24:15 +0000772void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700774 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000775 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
776 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100777 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
778 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
779 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
780 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000781 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700782 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100783 GenNullCheck(rl_obj.reg, opt_flags);
784 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
785 int field_offset = field_info.FieldOffset().Int32Value();
786 if (field_info.IsVolatile()) {
787 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
788 MarkPossibleNullPointerException(opt_flags);
789 // Without context sensitive analysis, we must issue the most conservative barriers.
790 // In this case, either a load or store may follow so we issue both barriers.
791 GenMemBarrier(kLoadLoad);
792 GenMemBarrier(kLoadStore);
793 } else {
794 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
795 MarkPossibleNullPointerException(opt_flags);
796 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 StoreValueWide(rl_dest, rl_result);
799 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 StoreValue(rl_dest, rl_result);
801 }
802 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700803 if (Is64BitInstructionSet(cu_->instruction_set)) {
804 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
805 } else {
806 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
807 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700809 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 StoreValueWide(rl_dest, rl_result);
811 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700812 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 StoreValue(rl_dest, rl_result);
814 }
815 }
816}
817
Andreas Gampe2f244e92014-05-08 03:35:25 -0700818template <size_t pointer_size>
819static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
820 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
821 RegLocation rl_src) {
822 ThreadOffset<pointer_size> setter_offset =
823 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
824 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
825 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
826 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
827 rl_obj, rl_src, true);
828}
829
Vladimir Markobe0e5462014-02-26 11:24:15 +0000830void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700832 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
834 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100835 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
836 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
837 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
838 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000839 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700840 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100842 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 } else {
844 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100845 }
846 GenNullCheck(rl_obj.reg, opt_flags);
847 int field_offset = field_info.FieldOffset().Int32Value();
848 if (field_info.IsVolatile()) {
849 // There might have been a store before this volatile one so insert StoreStore barrier.
850 GenMemBarrier(kStoreStore);
851 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800852 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100853 // A load might follow the volatile store so insert a StoreLoad barrier.
854 GenMemBarrier(kStoreLoad);
855 } else {
856 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
857 MarkPossibleNullPointerException(opt_flags);
858 }
859 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
860 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 }
862 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700863 if (Is64BitInstructionSet(cu_->instruction_set)) {
864 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
865 } else {
866 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
867 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 }
869}
870
Andreas Gampe2f244e92014-05-08 03:35:25 -0700871template <size_t pointer_size>
872static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
873 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
874 ThreadOffset<pointer_size> helper = needs_range_check
875 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
876 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
877 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
878 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
879 true);
880}
881
Ian Rogersa9a82542013-10-04 11:17:26 -0700882void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
883 RegLocation rl_src) {
884 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
885 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
886 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe2f244e92014-05-08 03:35:25 -0700887 if (Is64BitInstructionSet(cu_->instruction_set)) {
888 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
889 } else {
890 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
891 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700892}
893
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700894void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800896 RegStorage res_reg = AllocTemp();
buzbeea0cd2d72014-06-01 09:33:49 -0700897 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
899 *cu_->dex_file,
900 type_idx)) {
901 // Call out to helper which resolves type and verifies access.
902 // Resolved type returned in kRet0.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700903 if (Is64BitInstructionSet(cu_->instruction_set)) {
904 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
905 type_idx, rl_method.reg, true);
906 } else {
907 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
908 type_idx, rl_method.reg, true);
909 }
buzbeea0cd2d72014-06-01 09:33:49 -0700910 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 StoreValue(rl_dest, rl_result);
912 } else {
913 // We're don't need access checks, load type from dex cache
914 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700915 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700916 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000917 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700918 LoadRefDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
920 type_idx) || SLOW_TYPE_PATH) {
921 // Slow path, at runtime test if type is null and if so initialize
922 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800923 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800924 LIR* cont = NewLIR0(kPseudoTargetLabel);
925
926 // Object to generate the slow path for class resolution.
927 class SlowPath : public LIRSlowPath {
928 public:
929 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
930 const RegLocation& rl_method, const RegLocation& rl_result) :
931 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
932 rl_method_(rl_method), rl_result_(rl_result) {
933 }
934
935 void Compile() {
936 GenerateTargetLabel();
937
Andreas Gampe2f244e92014-05-08 03:35:25 -0700938 if (Is64BitInstructionSet(cu_->instruction_set)) {
939 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
940 rl_method_.reg, true);
941 } else {
942 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
943 rl_method_.reg, true);
944 }
buzbee2700f7e2014-03-07 09:46:20 -0800945 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800946
947 m2l_->OpUnconditionalBranch(cont_);
948 }
949
950 private:
951 const int type_idx_;
952 const RegLocation rl_method_;
953 const RegLocation rl_result_;
954 };
955
956 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800957 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800960 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 // Fast path, we're done - just store result
962 StoreValue(rl_dest, rl_result);
963 }
964 }
965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000969 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
970 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
972 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
973 // slow path, resolve string if not in dex cache
974 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700975 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800976
977 // If the Method* is already in a register, we can save a copy.
978 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800979 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800980 if (rl_method.location == kLocPhysReg) {
981 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800982 DCHECK(!IsTemp(rl_method.reg));
983 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800984 } else {
985 r_method = TargetReg(kArg2);
986 LoadCurrMethodDirect(r_method);
987 }
buzbee695d13a2014-04-19 13:32:20 -0700988 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
989 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800990
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 // Might call out to helper, which will return resolved string in kRet0
buzbeea0cd2d72014-06-01 09:33:49 -0700992 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700993 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
994 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800995
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700996 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800997 // Object to generate the slow path for string resolution.
998 class SlowPath : public LIRSlowPath {
999 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001000 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1001 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1002 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001003 }
1004
1005 void Compile() {
1006 GenerateTargetLabel();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001007 if (Is64BitInstructionSet(cu_->instruction_set)) {
1008 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1009 r_method_, string_idx_, true);
1010 } else {
1011 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1012 r_method_, string_idx_, true);
1013 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001014 m2l_->OpUnconditionalBranch(cont_);
1015 }
1016
1017 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001018 const RegStorage r_method_;
1019 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001020 };
1021
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001022 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001024
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001026 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 } else {
1028 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001029 RegStorage res_reg = AllocTempRef();
1030 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001031 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeea0cd2d72014-06-01 09:33:49 -07001032 LoadRefDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 StoreValue(rl_dest, rl_result);
1034 }
1035}
1036
Andreas Gampe2f244e92014-05-08 03:35:25 -07001037template <size_t pointer_size>
1038static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1039 RegLocation rl_dest) {
1040 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 // alloc will always check for resolution, do we also need to verify
1042 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001043 ThreadOffset<pointer_size> func_offset(-1);
1044 const DexFile* dex_file = cu->dex_file;
1045 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001046 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001047 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001048 bool is_type_initialized;
1049 bool use_direct_type_ptr;
1050 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001051 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001052 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001053 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1054 &direct_type_ptr, &is_finalizable) &&
1055 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001056 // The fast path.
1057 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001058 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001060 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1061 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001063 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1064 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 }
1066 } else {
1067 // Use the direct pointer.
1068 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001069 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1070 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001071 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001072 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1073 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001074 }
1075 }
1076 } else {
1077 // The slow path.
1078 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001079 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1080 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001081 }
1082 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001084 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1085 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
buzbeea0cd2d72014-06-01 09:33:49 -07001087 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001088 mir_to_lir->StoreValue(rl_dest, rl_result);
1089}
1090
1091/*
1092 * Let helper function take care of everything. Will
1093 * call Class::NewInstanceFromCode(type_idx, method);
1094 */
1095void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1096 if (Is64BitInstructionSet(cu_->instruction_set)) {
1097 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1098 } else {
1099 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1100 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101}
1102
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001103void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07001105 if (Is64BitInstructionSet(cu_->instruction_set)) {
1106 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1107 } else {
1108 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1109 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110}
1111
1112// For final classes there are no sub-classes to check and so we can answer the instance-of
1113// question with simple comparisons.
1114void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1115 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001116 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001117 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001118
buzbeea0cd2d72014-06-01 09:33:49 -07001119 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001121 RegStorage result_reg = rl_result.reg;
1122 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 result_reg = AllocTypedTemp(false, kCoreReg);
1124 }
1125 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001126 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127
buzbeea0cd2d72014-06-01 09:33:49 -07001128 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1129 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130
1131 LoadCurrMethodDirect(check_class);
1132 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001133 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1134 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 } else {
buzbee695d13a2014-04-19 13:32:20 -07001136 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1137 check_class);
1138 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001139 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001140 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 }
1142
1143 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001144 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 if (cu_->instruction_set == kThumb2) {
1146 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001147 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001149 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 } else {
1151 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1152 LoadConstant(result_reg, 1); // eq case - load true
1153 }
1154 LIR* target = NewLIR0(kPseudoTargetLabel);
1155 null_branchover->target = target;
1156 if (ne_branchover != NULL) {
1157 ne_branchover->target = target;
1158 }
1159 FreeTemp(object_class);
1160 FreeTemp(check_class);
1161 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001162 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 FreeTemp(result_reg);
1164 }
1165 StoreValue(rl_dest, rl_result);
1166}
1167
1168void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1169 bool type_known_abstract, bool use_declaring_class,
1170 bool can_assume_type_is_in_dex_cache,
1171 uint32_t type_idx, RegLocation rl_dest,
1172 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001173 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001174 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001175
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 FlushAllRegs();
1177 // May generate a call - use explicit registers
1178 LockCallTemps();
1179 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001180 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 if (needs_access_check) {
1182 // Check we have access to type_idx and if not throw IllegalAccessError,
1183 // returns Class* in kArg0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001184 if (Is64BitInstructionSet(cu_->instruction_set)) {
1185 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1186 type_idx, true);
1187 } else {
1188 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1189 type_idx, true);
1190 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1192 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1193 } else if (use_declaring_class) {
1194 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001195 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001196 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 } else {
1198 // Load dex cache entry into class_reg (kArg2)
1199 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001200 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1201 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001202 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001203 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 if (!can_assume_type_is_in_dex_cache) {
1205 // Need to test presence of type in dex cache at runtime
1206 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1207 // Not resolved
1208 // Call out to helper, which will return resolved type in kRet0
Andreas Gampe2f244e92014-05-08 03:35:25 -07001209 if (Is64BitInstructionSet(cu_->instruction_set)) {
1210 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1211 } else {
1212 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1213 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001214 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1216 // Rejoin code paths
1217 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1218 hop_branch->target = hop_target;
1219 }
1220 }
1221 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001222 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 if (cu_->instruction_set == kMips) {
1224 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001225 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 }
1227 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1228
1229 /* load object->klass_ */
1230 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001231 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1233 LIR* branchover = NULL;
1234 if (type_known_final) {
1235 // rl_result == ref == null == 0.
1236 if (cu_->instruction_set == kThumb2) {
1237 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001238 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001239 LoadConstant(rl_result.reg, 1); // .eq case - load true
1240 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001241 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001242 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001243 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001245 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 }
1247 } else {
1248 if (cu_->instruction_set == kThumb2) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001249 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1250 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1251 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001252 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 if (!type_known_abstract) {
1254 /* Uses conditional nullification */
1255 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001256 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1258 }
1259 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1260 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001261 if (it != nullptr) {
1262 OpEndIT(it);
1263 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 FreeTemp(r_tgt);
1265 } else {
1266 if (!type_known_abstract) {
1267 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001268 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1270 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001271 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1272 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1273 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001274 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1275 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1276 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 }
1278 }
1279 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001280 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 /* branch targets here */
1282 LIR* target = NewLIR0(kPseudoTargetLabel);
1283 StoreValue(rl_dest, rl_result);
1284 branch1->target = target;
1285 if (branchover != NULL) {
1286 branchover->target = target;
1287 }
1288}
1289
1290void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1291 bool type_known_final, type_known_abstract, use_declaring_class;
1292 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1293 *cu_->dex_file,
1294 type_idx,
1295 &type_known_final,
1296 &type_known_abstract,
1297 &use_declaring_class);
1298 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1299 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1300
1301 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1302 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1303 } else {
1304 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1305 use_declaring_class, can_assume_type_is_in_dex_cache,
1306 type_idx, rl_dest, rl_src);
1307 }
1308}
1309
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001310void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 bool type_known_final, type_known_abstract, use_declaring_class;
1312 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1313 *cu_->dex_file,
1314 type_idx,
1315 &type_known_final,
1316 &type_known_abstract,
1317 &use_declaring_class);
1318 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1319 // of the exception throw path.
1320 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001321 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 // Verifier type analysis proved this check cast would never cause an exception.
1323 return;
1324 }
1325 FlushAllRegs();
1326 // May generate a call - use explicit registers
1327 LockCallTemps();
1328 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001329 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 if (needs_access_check) {
1331 // Check we have access to type_idx and if not throw IllegalAccessError,
1332 // returns Class* in kRet0
1333 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001334 if (Is64BitInstructionSet(cu_->instruction_set)) {
1335 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1336 type_idx, TargetReg(kArg1), true);
1337 } else {
1338 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1339 type_idx, TargetReg(kArg1), true);
1340 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1342 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001343 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1344 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 } else {
1346 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001347 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1348 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001349 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001350 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1352 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001353 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1354 LIR* cont = NewLIR0(kPseudoTargetLabel);
1355
1356 // Slow path to initialize the type. Executed if the type is NULL.
1357 class SlowPath : public LIRSlowPath {
1358 public:
1359 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001360 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001361 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1362 class_reg_(class_reg) {
1363 }
1364
1365 void Compile() {
1366 GenerateTargetLabel();
1367
1368 // Call out to helper, which will return resolved type in kArg0
1369 // InitializeTypeFromCode(idx, method)
Andreas Gampe2f244e92014-05-08 03:35:25 -07001370 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1371 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1372 m2l_->TargetReg(kArg1), true);
1373 } else {
1374 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1375 m2l_->TargetReg(kArg1), true);
1376 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001377 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1378 m2l_->OpUnconditionalBranch(cont_);
1379 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001380
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381 public:
1382 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001383 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001384 };
1385
buzbee2700f7e2014-03-07 09:46:20 -08001386 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 }
1388 }
1389 // At this point, class_reg (kArg2) has class
1390 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001391
1392 // Slow path for the case where the classes are not equal. In this case we need
1393 // to call a helper function to do the check.
1394 class SlowPath : public LIRSlowPath {
1395 public:
1396 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1397 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1398 }
1399
1400 void Compile() {
1401 GenerateTargetLabel();
1402
1403 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001404 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1405 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001406 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001407 if (Is64BitInstructionSet(m2l_->cu_->instruction_set)) {
1408 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1409 m2l_->TargetReg(kArg1), true);
1410 } else {
1411 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1412 m2l_->TargetReg(kArg1), true);
1413 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001414
1415 m2l_->OpUnconditionalBranch(cont_);
1416 }
1417
1418 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001419 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001420 };
1421
1422 if (type_known_abstract) {
1423 // Easier case, run slow path if target is non-null (slow path will load from target)
1424 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1425 LIR* cont = NewLIR0(kPseudoTargetLabel);
1426 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1427 } else {
1428 // Harder, more common case. We need to generate a forward branch over the load
1429 // if the target is null. If it's non-null we perform the load and branch to the
1430 // slow path if the classes are not equal.
1431
1432 /* Null is OK - continue */
1433 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1434 /* load object->klass_ */
1435 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001436 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001437
1438 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1439 LIR* cont = NewLIR0(kPseudoTargetLabel);
1440
1441 // Add the slow path that will not perform load since this is already done.
1442 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1443
1444 // Set the null check to branch to the continuation.
1445 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 }
1447}
1448
1449void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001450 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 RegLocation rl_result;
1452 if (cu_->instruction_set == kThumb2) {
1453 /*
1454 * NOTE: This is the one place in the code in which we might have
1455 * as many as six live temporary registers. There are 5 in the normal
1456 * set for Arm. Until we have spill capabilities, temporarily add
1457 * lr to the temp set. It is safe to do this locally, but note that
1458 * lr is used explicitly elsewhere in the code generator and cannot
1459 * normally be used as a general temp register.
1460 */
1461 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1462 FreeTemp(TargetReg(kLr)); // and make it available
1463 }
1464 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1465 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1466 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1467 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001468 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1469 RegStorage t_reg = AllocTemp();
1470 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1471 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1472 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 FreeTemp(t_reg);
1474 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001475 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1476 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 }
1478 /*
1479 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1480 * following StoreValueWide might need to allocate a temp register.
1481 * To further work around the lack of a spill capability, explicitly
1482 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1483 * Remove when spill is functional.
1484 */
1485 FreeRegLocTemps(rl_result, rl_src1);
1486 FreeRegLocTemps(rl_result, rl_src2);
1487 StoreValueWide(rl_dest, rl_result);
1488 if (cu_->instruction_set == kThumb2) {
1489 Clobber(TargetReg(kLr));
1490 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1491 }
1492}
1493
1494
Andreas Gampe2f244e92014-05-08 03:35:25 -07001495template <size_t pointer_size>
1496static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1497 RegLocation rl_shift) {
1498 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499
1500 switch (opcode) {
1501 case Instruction::SHL_LONG:
1502 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001503 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 break;
1505 case Instruction::SHR_LONG:
1506 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001507 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 break;
1509 case Instruction::USHR_LONG:
1510 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001511 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 break;
1513 default:
1514 LOG(FATAL) << "Unexpected case";
1515 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001516 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1517 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1518}
1519
1520void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1521 RegLocation rl_src1, RegLocation rl_shift) {
1522 if (Is64BitInstructionSet(cu_->instruction_set)) {
1523 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1524 } else {
1525 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1526 }
buzbeea0cd2d72014-06-01 09:33:49 -07001527 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 StoreValueWide(rl_dest, rl_result);
1529}
1530
1531
1532void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001533 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001534 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 OpKind op = kOpBkpt;
1536 bool is_div_rem = false;
1537 bool check_zero = false;
1538 bool unary = false;
1539 RegLocation rl_result;
1540 bool shift_op = false;
1541 switch (opcode) {
1542 case Instruction::NEG_INT:
1543 op = kOpNeg;
1544 unary = true;
1545 break;
1546 case Instruction::NOT_INT:
1547 op = kOpMvn;
1548 unary = true;
1549 break;
1550 case Instruction::ADD_INT:
1551 case Instruction::ADD_INT_2ADDR:
1552 op = kOpAdd;
1553 break;
1554 case Instruction::SUB_INT:
1555 case Instruction::SUB_INT_2ADDR:
1556 op = kOpSub;
1557 break;
1558 case Instruction::MUL_INT:
1559 case Instruction::MUL_INT_2ADDR:
1560 op = kOpMul;
1561 break;
1562 case Instruction::DIV_INT:
1563 case Instruction::DIV_INT_2ADDR:
1564 check_zero = true;
1565 op = kOpDiv;
1566 is_div_rem = true;
1567 break;
1568 /* NOTE: returns in kArg1 */
1569 case Instruction::REM_INT:
1570 case Instruction::REM_INT_2ADDR:
1571 check_zero = true;
1572 op = kOpRem;
1573 is_div_rem = true;
1574 break;
1575 case Instruction::AND_INT:
1576 case Instruction::AND_INT_2ADDR:
1577 op = kOpAnd;
1578 break;
1579 case Instruction::OR_INT:
1580 case Instruction::OR_INT_2ADDR:
1581 op = kOpOr;
1582 break;
1583 case Instruction::XOR_INT:
1584 case Instruction::XOR_INT_2ADDR:
1585 op = kOpXor;
1586 break;
1587 case Instruction::SHL_INT:
1588 case Instruction::SHL_INT_2ADDR:
1589 shift_op = true;
1590 op = kOpLsl;
1591 break;
1592 case Instruction::SHR_INT:
1593 case Instruction::SHR_INT_2ADDR:
1594 shift_op = true;
1595 op = kOpAsr;
1596 break;
1597 case Instruction::USHR_INT:
1598 case Instruction::USHR_INT_2ADDR:
1599 shift_op = true;
1600 op = kOpLsr;
1601 break;
1602 default:
1603 LOG(FATAL) << "Invalid word arith op: " << opcode;
1604 }
1605 if (!is_div_rem) {
1606 if (unary) {
1607 rl_src1 = LoadValue(rl_src1, kCoreReg);
1608 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001609 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001610 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001611 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001612 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001613 RegStorage t_reg = AllocTemp();
1614 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 rl_src1 = LoadValue(rl_src1, kCoreReg);
1616 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001617 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 FreeTemp(t_reg);
1619 } else {
1620 rl_src1 = LoadValue(rl_src1, kCoreReg);
1621 rl_src2 = LoadValue(rl_src2, kCoreReg);
1622 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001623 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 }
1625 }
1626 StoreValue(rl_dest, rl_result);
1627 } else {
Dave Allison70202782013-10-22 17:52:19 -07001628 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 +01001629 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001630 rl_src1 = LoadValue(rl_src1, kCoreReg);
1631 rl_src2 = LoadValue(rl_src2, kCoreReg);
1632 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001633 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634 }
buzbee2700f7e2014-03-07 09:46:20 -08001635 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001636 done = true;
1637 } else if (cu_->instruction_set == kThumb2) {
1638 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1639 // Use ARM SDIV instruction for division. For remainder we also need to
1640 // calculate using a MUL and subtract.
1641 rl_src1 = LoadValue(rl_src1, kCoreReg);
1642 rl_src2 = LoadValue(rl_src2, kCoreReg);
1643 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001644 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001645 }
buzbee2700f7e2014-03-07 09:46:20 -08001646 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001647 done = true;
1648 }
1649 }
1650
1651 // If we haven't already generated the code use the callout function.
1652 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 FlushAllRegs(); /* Send everything to home location */
1654 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001655 RegStorage r_tgt = Is64BitInstructionSet(cu_->instruction_set) ?
1656 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1657 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1659 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001660 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 }
Dave Allison70202782013-10-22 17:52:19 -07001662 // NOTE: callout here is not a safepoint.
Andreas Gampe2f244e92014-05-08 03:35:25 -07001663 if (Is64BitInstructionSet(cu_->instruction_set)) {
1664 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1665 } else {
1666 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1667 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001669 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 else
1671 rl_result = GetReturnAlt();
1672 }
1673 StoreValue(rl_dest, rl_result);
1674 }
1675}
1676
1677/*
1678 * The following are the first-level codegen routines that analyze the format
1679 * of each bytecode then either dispatch special purpose codegen routines
1680 * or produce corresponding Thumb instructions directly.
1681 */
1682
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001684static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685 x &= x - 1;
1686 return (x & (x - 1)) == 0;
1687}
1688
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1690// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001691bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001692 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1694 return false;
1695 }
1696 // No divide instruction for Arm, so check for more special cases
1697 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001698 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 }
1700 int k = LowestSetBit(lit);
1701 if (k >= 30) {
1702 // Avoid special cases.
1703 return false;
1704 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 rl_src = LoadValue(rl_src, kCoreReg);
1706 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001707 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001708 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001709 if (lit == 2) {
1710 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001711 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1712 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1713 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001715 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001717 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1718 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 }
1720 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001721 RegStorage t_reg1 = AllocTemp();
1722 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001724 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1725 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001727 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001729 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001733 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 }
1735 }
1736 StoreValue(rl_dest, rl_result);
1737 return true;
1738}
1739
1740// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1741// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001742bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001743 if (lit < 0) {
1744 return false;
1745 }
1746 if (lit == 0) {
1747 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1748 LoadConstant(rl_result.reg, 0);
1749 StoreValue(rl_dest, rl_result);
1750 return true;
1751 }
1752 if (lit == 1) {
1753 rl_src = LoadValue(rl_src, kCoreReg);
1754 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1755 OpRegCopy(rl_result.reg, rl_src.reg);
1756 StoreValue(rl_dest, rl_result);
1757 return true;
1758 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001759 // There is RegRegRegShift on Arm, so check for more special cases
1760 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001761 return EasyMultiply(rl_src, rl_dest, lit);
1762 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 // Can we simplify this multiplication?
1764 bool power_of_two = false;
1765 bool pop_count_le2 = false;
1766 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001767 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 power_of_two = true;
1769 } else if (IsPopCountLE2(lit)) {
1770 pop_count_le2 = true;
1771 } else if (IsPowerOfTwo(lit + 1)) {
1772 power_of_two_minus_one = true;
1773 } else {
1774 return false;
1775 }
1776 rl_src = LoadValue(rl_src, kCoreReg);
1777 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1778 if (power_of_two) {
1779 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001780 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 } else if (pop_count_le2) {
1782 // Shift and add and shift.
1783 int first_bit = LowestSetBit(lit);
1784 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1785 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1786 } else {
1787 // Reverse subtract: (src << (shift + 1)) - src.
1788 DCHECK(power_of_two_minus_one);
1789 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001790 RegStorage t_reg = AllocTemp();
1791 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1792 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 }
1794 StoreValue(rl_dest, rl_result);
1795 return true;
1796}
1797
1798void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001799 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001800 RegLocation rl_result;
1801 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1802 int shift_op = false;
1803 bool is_div = false;
1804
1805 switch (opcode) {
1806 case Instruction::RSUB_INT_LIT8:
1807 case Instruction::RSUB_INT: {
1808 rl_src = LoadValue(rl_src, kCoreReg);
1809 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1810 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001811 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001813 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1814 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 }
1816 StoreValue(rl_dest, rl_result);
1817 return;
1818 }
1819
1820 case Instruction::SUB_INT:
1821 case Instruction::SUB_INT_2ADDR:
1822 lit = -lit;
1823 // Intended fallthrough
1824 case Instruction::ADD_INT:
1825 case Instruction::ADD_INT_2ADDR:
1826 case Instruction::ADD_INT_LIT8:
1827 case Instruction::ADD_INT_LIT16:
1828 op = kOpAdd;
1829 break;
1830 case Instruction::MUL_INT:
1831 case Instruction::MUL_INT_2ADDR:
1832 case Instruction::MUL_INT_LIT8:
1833 case Instruction::MUL_INT_LIT16: {
1834 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1835 return;
1836 }
1837 op = kOpMul;
1838 break;
1839 }
1840 case Instruction::AND_INT:
1841 case Instruction::AND_INT_2ADDR:
1842 case Instruction::AND_INT_LIT8:
1843 case Instruction::AND_INT_LIT16:
1844 op = kOpAnd;
1845 break;
1846 case Instruction::OR_INT:
1847 case Instruction::OR_INT_2ADDR:
1848 case Instruction::OR_INT_LIT8:
1849 case Instruction::OR_INT_LIT16:
1850 op = kOpOr;
1851 break;
1852 case Instruction::XOR_INT:
1853 case Instruction::XOR_INT_2ADDR:
1854 case Instruction::XOR_INT_LIT8:
1855 case Instruction::XOR_INT_LIT16:
1856 op = kOpXor;
1857 break;
1858 case Instruction::SHL_INT_LIT8:
1859 case Instruction::SHL_INT:
1860 case Instruction::SHL_INT_2ADDR:
1861 lit &= 31;
1862 shift_op = true;
1863 op = kOpLsl;
1864 break;
1865 case Instruction::SHR_INT_LIT8:
1866 case Instruction::SHR_INT:
1867 case Instruction::SHR_INT_2ADDR:
1868 lit &= 31;
1869 shift_op = true;
1870 op = kOpAsr;
1871 break;
1872 case Instruction::USHR_INT_LIT8:
1873 case Instruction::USHR_INT:
1874 case Instruction::USHR_INT_2ADDR:
1875 lit &= 31;
1876 shift_op = true;
1877 op = kOpLsr;
1878 break;
1879
1880 case Instruction::DIV_INT:
1881 case Instruction::DIV_INT_2ADDR:
1882 case Instruction::DIV_INT_LIT8:
1883 case Instruction::DIV_INT_LIT16:
1884 case Instruction::REM_INT:
1885 case Instruction::REM_INT_2ADDR:
1886 case Instruction::REM_INT_LIT8:
1887 case Instruction::REM_INT_LIT16: {
1888 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001889 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001890 return;
1891 }
buzbee11b63d12013-08-27 07:34:17 -07001892 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001894 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895 (opcode == Instruction::DIV_INT_LIT16)) {
1896 is_div = true;
1897 } else {
1898 is_div = false;
1899 }
buzbee11b63d12013-08-27 07:34:17 -07001900 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1901 return;
1902 }
Dave Allison70202782013-10-22 17:52:19 -07001903
1904 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001905 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001906 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001907 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001908 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001909 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001910 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1911 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001912 } else if (cu_->instruction_set == kThumb2) {
1913 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1914 // Use ARM SDIV instruction for division. For remainder we also need to
1915 // calculate using a MUL and subtract.
1916 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001917 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001918 done = true;
1919 }
1920 }
1921
1922 if (!done) {
1923 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1925 Clobber(TargetReg(kArg0));
Andreas Gampe2f244e92014-05-08 03:35:25 -07001926 if (Is64BitInstructionSet(cu_->instruction_set)) {
1927 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1928 false);
1929 } else {
1930 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1931 false);
1932 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001933 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001934 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001935 else
1936 rl_result = GetReturnAlt();
1937 }
1938 StoreValue(rl_dest, rl_result);
1939 return;
1940 }
1941 default:
1942 LOG(FATAL) << "Unexpected opcode " << opcode;
1943 }
1944 rl_src = LoadValue(rl_src, kCoreReg);
1945 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001946 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001948 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001949 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001950 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 }
1952 StoreValue(rl_dest, rl_result);
1953}
1954
Andreas Gampe2f244e92014-05-08 03:35:25 -07001955template <size_t pointer_size>
1956static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1957 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 RegLocation rl_result;
1959 OpKind first_op = kOpBkpt;
1960 OpKind second_op = kOpBkpt;
1961 bool call_out = false;
1962 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001963 ThreadOffset<pointer_size> func_offset(-1);
1964 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001965
1966 switch (opcode) {
1967 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001968 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001969 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1970 return;
1971 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001972 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1973 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001975 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001976 RegStorage t_reg = mir_to_lir->AllocTemp();
1977 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1978 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1979 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1980 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001982 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1983 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001985 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001986 return;
1987 case Instruction::ADD_LONG:
1988 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001989 if (cu->instruction_set != kThumb2) {
1990 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 return;
1992 }
1993 first_op = kOpAdd;
1994 second_op = kOpAdc;
1995 break;
1996 case Instruction::SUB_LONG:
1997 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001998 if (cu->instruction_set != kThumb2) {
1999 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002000 return;
2001 }
2002 first_op = kOpSub;
2003 second_op = kOpSbc;
2004 break;
2005 case Instruction::MUL_LONG:
2006 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002007 if (cu->instruction_set != kMips) {
2008 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 return;
2010 } else {
2011 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002012 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2013 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 }
2015 break;
2016 case Instruction::DIV_LONG:
2017 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002018 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002019 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2020 return;
2021 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 call_out = true;
2023 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002024 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2025 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026 break;
2027 case Instruction::REM_LONG:
2028 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002029 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002030 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2031 return;
2032 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002033 call_out = true;
2034 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002035 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002037 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2038 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 break;
2040 case Instruction::AND_LONG_2ADDR:
2041 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002042 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2043 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002044 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002045 }
2046 first_op = kOpAnd;
2047 second_op = kOpAnd;
2048 break;
2049 case Instruction::OR_LONG:
2050 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002051 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2052 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002053 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 return;
2055 }
2056 first_op = kOpOr;
2057 second_op = kOpOr;
2058 break;
2059 case Instruction::XOR_LONG:
2060 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002061 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2062 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002063 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 return;
2065 }
2066 first_op = kOpXor;
2067 second_op = kOpXor;
2068 break;
2069 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002070 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002071 return;
2072 }
2073 default:
2074 LOG(FATAL) << "Invalid long arith op";
2075 }
2076 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002077 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002079 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002081 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2082 mir_to_lir->TargetReg(kArg1));
2083 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2084 mir_to_lir->TargetReg(kArg3));
2085 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2086 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2087 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2088 mir_to_lir->TargetReg(kArg3)));
2089 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002090 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002091 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002093 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002094 }
2095 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002096 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002097 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 rl_result = mir_to_lir->GetReturnWideAlt();
2100 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 }
2102}
2103
Andreas Gampe2f244e92014-05-08 03:35:25 -07002104void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2105 RegLocation rl_src1, RegLocation rl_src2) {
2106 if (Is64BitInstructionSet(cu_->instruction_set)) {
2107 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2108 } else {
2109 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2110 }
2111}
2112
Mark Mendelle87f9b52014-04-30 14:13:18 -04002113void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2114 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2115 LoadConstantNoClobber(rl_result.reg, value);
2116 StoreValue(rl_dest, rl_result);
2117 if (value == 0) {
2118 Workaround7250540(rl_dest, rl_result.reg);
2119 }
2120}
2121
Andreas Gampe2f244e92014-05-08 03:35:25 -07002122template <size_t pointer_size>
2123void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002124 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125 /*
2126 * Don't optimize the register usage since it calls out to support
2127 * functions
2128 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002129 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2130
Brian Carlstrom7940e442013-07-12 13:46:57 -07002131 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002132 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2133 if (rl_dest.wide) {
2134 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002135 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 StoreValueWide(rl_dest, rl_result);
2137 } else {
2138 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002139 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002140 StoreValue(rl_dest, rl_result);
2141 }
2142}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002143template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2144 RegLocation rl_dest, RegLocation rl_src);
2145template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2146 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002147
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002148class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2149 public:
2150 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2151 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2152 }
2153
2154 void Compile() OVERRIDE {
2155 m2l_->ResetRegPool();
2156 m2l_->ResetDefTracking();
2157 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002158 if (Is64BitInstructionSet(cu_->instruction_set)) {
2159 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2160 } else {
2161 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2162 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002163 if (cont_ != nullptr) {
2164 m2l_->OpUnconditionalBranch(cont_);
2165 }
2166 }
2167};
2168
Brian Carlstrom7940e442013-07-12 13:46:57 -07002169/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002170void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002171 if (Runtime::Current()->ExplicitSuspendChecks()) {
2172 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2173 return;
2174 }
2175 FlushAllRegs();
2176 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002177 LIR* cont = NewLIR0(kPseudoTargetLabel);
2178 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002179 } else {
2180 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2181 return;
2182 }
2183 FlushAllRegs(); // TODO: needed?
2184 LIR* inst = CheckSuspendUsingLoad();
2185 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002186 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002187}
2188
2189/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002190void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002191 if (Runtime::Current()->ExplicitSuspendChecks()) {
2192 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2193 OpUnconditionalBranch(target);
2194 return;
2195 }
2196 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002197 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002198 LIR* branch = OpUnconditionalBranch(nullptr);
2199 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002200 } else {
2201 // For the implicit suspend check, just perform the trigger
2202 // load and branch to the target.
2203 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2204 OpUnconditionalBranch(target);
2205 return;
2206 }
2207 FlushAllRegs();
2208 LIR* inst = CheckSuspendUsingLoad();
2209 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002210 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002211 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002212}
2213
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002214/* Call out to helper assembly routine that will null check obj and then lock it. */
2215void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2216 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002217 if (Is64BitInstructionSet(cu_->instruction_set)) {
2218 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2219 } else {
2220 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2221 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002222}
2223
2224/* Call out to helper assembly routine that will null check obj and then unlock it. */
2225void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2226 FlushAllRegs();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002227 if (Is64BitInstructionSet(cu_->instruction_set)) {
2228 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2229 } else {
2230 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2231 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002232}
2233
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002234/* Generic code for generating a wide constant into a VR. */
2235void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2236 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002237 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002238 StoreValueWide(rl_dest, rl_result);
2239}
2240
Brian Carlstrom7940e442013-07-12 13:46:57 -07002241} // namespace art