blob: e36b592c748cc9e307e98c61140200a5e7d7b9ed [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);
buzbee33ae5582014-06-12 14:56:32 -070076 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -070077 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);
buzbee33ae5582014-06-12 14:56:32 -070099 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700100 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_);
buzbee33ae5582014-06-12 14:56:32 -0700132 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700133 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);
buzbee33ae5582014-06-12 14:56:32 -0700161 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700162 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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700176 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
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) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700191 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800192 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() {
Andreas Gampe5655e842014-06-17 16:36:07 -0700200 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800201 MarkSafepointPC(last_lir_insn_);
202 }
203}
204
buzbee2700f7e2014-03-07 09:46:20 -0800205void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -0700206 if (!cu_->compiler_driver->GetCompilerOptions().GetExplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800207 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) {
buzbee33ae5582014-06-12 14:56:32 -0700388 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700389 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 */
buzbee33ae5582014-06-12 14:56:32 -0700417 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700418 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700420 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 FreeTemp(TargetReg(kArg2));
423 FreeTemp(TargetReg(kArg1));
424 /*
425 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
426 * return region. Because AllocFromCode placed the new array
427 * in kRet0, we'll just lock it into place. When debugger support is
428 * added, it may be necessary to additionally copy all return
429 * values to a home location in thread-local storage
430 */
431 LockTemp(TargetReg(kRet0));
432
433 // TODO: use the correct component size, currently all supported types
434 // share array alignment with ints (see comment at head of function)
435 size_t component_size = sizeof(int32_t);
436
437 // Having a range of 0 is legal
438 if (info->is_range && (elems > 0)) {
439 /*
440 * Bit of ugliness here. We're going generate a mem copy loop
441 * on the register range, but it is possible that some regs
442 * in the range have been promoted. This is unlikely, but
443 * before generating the copy, we'll just force a flush
444 * of any regs in the source range that have been promoted to
445 * home location.
446 */
447 for (int i = 0; i < elems; i++) {
448 RegLocation loc = UpdateLoc(info->args[i]);
449 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100450 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee695d13a2014-04-19 13:32:20 -0700451 Store32Disp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 }
453 }
454 /*
455 * TUNING note: generated code here could be much improved, but
456 * this is an uncommon operation and isn't especially performance
457 * critical.
458 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700459 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700460 RegStorage r_src = AllocTempRef();
461 RegStorage r_dst = AllocTempRef();
462 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800463 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700464 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700466 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 r_val = TargetReg(kLr);
468 break;
469 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700470 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 FreeTemp(TargetReg(kRet0));
472 r_val = AllocTemp();
473 break;
474 case kMips:
475 r_val = AllocTemp();
476 break;
477 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
478 }
479 // Set up source pointer
480 RegLocation rl_first = info->args[0];
481 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
482 // Set up the target pointer
483 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
484 mirror::Array::DataOffset(component_size).Int32Value());
485 // Set up the loop counter (known to be > 0)
486 LoadConstant(r_idx, elems - 1);
487 // Generate the copy loop. Going backwards for convenience
488 LIR* target = NewLIR0(kPseudoTargetLabel);
489 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100490 {
491 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
492 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
493 // NOTE: No dalvik register annotation, local optimizations will be stopped
494 // by the loop boundaries.
495 }
buzbee695d13a2014-04-19 13:32:20 -0700496 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 FreeTemp(r_val);
498 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700499 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 // Restore the target pointer
501 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
502 -mirror::Array::DataOffset(component_size).Int32Value());
503 }
504 } else if (!info->is_range) {
505 // TUNING: interleave
506 for (int i = 0; i < elems; i++) {
507 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700508 Store32Disp(TargetReg(kRet0),
509 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800511 if (IsTemp(rl_arg.reg)) {
512 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 }
514 }
515 }
516 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700517 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 }
519}
520
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800521//
522// Slow path to ensure a class is initialized for sget/sput.
523//
524class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
525 public:
buzbee2700f7e2014-03-07 09:46:20 -0800526 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
527 RegStorage r_base) :
528 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
529 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800530 }
531
532 void Compile() {
533 LIR* unresolved_target = GenerateTargetLabel();
534 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700535 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700536 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
537 storage_index_, true);
538 } else {
539 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
540 storage_index_, true);
541 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800542 // Copy helper's result into r_base, a no-op on all but MIPS.
543 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
544
545 m2l_->OpUnconditionalBranch(cont_);
546 }
547
548 private:
549 LIR* const uninit_;
550 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800551 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800552};
553
Andreas Gampe2f244e92014-05-08 03:35:25 -0700554template <size_t pointer_size>
555static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
556 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
557 ThreadOffset<pointer_size> setter_offset =
558 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
559 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
560 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
561 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
562 true);
563}
564
Vladimir Markobe0e5462014-02-26 11:24:15 +0000565void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700566 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000567 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
568 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100569 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
570 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
571 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000572 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800573 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000574 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100576 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700577 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700578 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800579 if (IsTemp(rl_method.reg)) {
580 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 }
582 } else {
583 // Medium path, static storage base in a different class which requires checks that the other
584 // class is initialized.
585 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 // May do runtime call so everything to home locations.
588 FlushAllRegs();
589 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800590 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 LockTemp(r_method);
592 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800593 r_base = TargetReg(kArg0);
594 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700595 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000596 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
597 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800598 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000599 if (!field_info.IsInitialized() &&
600 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800601 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800602
603 // The slow path is invoked if the r_base is NULL or the class pointed
604 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800605 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800606 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800608 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800609 mirror::Class::StatusOffset().Int32Value(),
610 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800611 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800612
buzbee2700f7e2014-03-07 09:46:20 -0800613 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000614 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800615
616 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700617 // Ensure load of status and load of value don't re-order.
618 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 FreeTemp(r_method);
621 }
622 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100623 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100625 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100627 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000629 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800630 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100632 StoreBaseDispVolatile(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800633 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100635 } else {
636 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
638 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800639 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800641 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 } else {
643 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700644 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700645 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
646 } else {
647 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
650}
651
Andreas Gampe2f244e92014-05-08 03:35:25 -0700652template <size_t pointer_size>
653static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
654 const MirSFieldLoweringInfo* field_info) {
655 ThreadOffset<pointer_size> getter_offset =
656 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
657 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
658 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
659 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
660}
661
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700663 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000664 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
665 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100666 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
667 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
668 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000669 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800670 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000671 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 // Fast path, static storage base is this method's class
673 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700674 r_base = AllocTempRef();
buzbee695d13a2014-04-19 13:32:20 -0700675 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 } else {
677 // Medium path, static storage base in a different class which requires checks that the other
678 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000679 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 // May do runtime call so everything to home locations.
681 FlushAllRegs();
682 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800683 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 LockTemp(r_method);
685 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800686 r_base = TargetReg(kArg0);
687 LockTemp(r_base);
buzbee695d13a2014-04-19 13:32:20 -0700688 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000689 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
690 LoadRefDisp(r_base, offset_of_field, r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800691 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000692 if (!field_info.IsInitialized() &&
693 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800694 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800695
696 // The slow path is invoked if the r_base is NULL or the class pointed
697 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800698 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800699 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800700 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800701 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800702 mirror::Class::StatusOffset().Int32Value(),
703 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800704 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800705
buzbee2700f7e2014-03-07 09:46:20 -0800706 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000707 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800708
709 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700710 // Ensure load of status and load of value don't re-order.
711 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 FreeTemp(r_method);
714 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800715 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100716 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
717 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800718
Vladimir Marko674744e2014-04-24 15:18:26 +0100719 int field_offset = field_info.FieldOffset().Int32Value();
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800720 if (field_info.IsVolatile()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100721 LoadBaseDispVolatile(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800722 // Without context sensitive analysis, we must issue the most conservative barriers.
723 // In this case, either a load or store may follow so we issue both barriers.
724 GenMemBarrier(kLoadLoad);
725 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100726 } else {
727 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800728 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100729 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800730
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 if (is_long_or_double) {
732 StoreValueWide(rl_dest, rl_result);
733 } else {
734 StoreValue(rl_dest, rl_result);
735 }
736 } else {
737 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700738 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700739 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
740 } else {
741 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
742 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700744 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 StoreValueWide(rl_dest, rl_result);
746 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700747 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 StoreValue(rl_dest, rl_result);
749 }
750 }
751}
752
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800753// Generate code for all slow paths.
754void Mir2Lir::HandleSlowPaths() {
755 int n = slow_paths_.Size();
756 for (int i = 0; i < n; ++i) {
757 LIRSlowPath* slowpath = slow_paths_.Get(i);
758 slowpath->Compile();
759 }
760 slow_paths_.Reset();
761}
762
Andreas Gampe2f244e92014-05-08 03:35:25 -0700763template <size_t pointer_size>
764static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
765 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
766 ThreadOffset<pointer_size> getter_offset =
767 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
768 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
769 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
770 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
771 true);
772}
773
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700776 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000777 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
778 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100779 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
780 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
781 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
782 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000783 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700784 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100785 GenNullCheck(rl_obj.reg, opt_flags);
786 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
787 int field_offset = field_info.FieldOffset().Int32Value();
788 if (field_info.IsVolatile()) {
789 LoadBaseDispVolatile(rl_obj.reg, field_offset, rl_result.reg, load_size);
790 MarkPossibleNullPointerException(opt_flags);
791 // Without context sensitive analysis, we must issue the most conservative barriers.
792 // In this case, either a load or store may follow so we issue both barriers.
793 GenMemBarrier(kLoadLoad);
794 GenMemBarrier(kLoadStore);
795 } else {
796 LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size);
797 MarkPossibleNullPointerException(opt_flags);
798 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 StoreValueWide(rl_dest, rl_result);
801 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 StoreValue(rl_dest, rl_result);
803 }
804 } else {
buzbee33ae5582014-06-12 14:56:32 -0700805 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700806 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
807 } else {
808 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
809 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 if (is_long_or_double) {
buzbeea0cd2d72014-06-01 09:33:49 -0700811 RegLocation rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 StoreValueWide(rl_dest, rl_result);
813 } else {
buzbeea0cd2d72014-06-01 09:33:49 -0700814 RegLocation rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 StoreValue(rl_dest, rl_result);
816 }
817 }
818}
819
Andreas Gampe2f244e92014-05-08 03:35:25 -0700820template <size_t pointer_size>
821static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
822 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
823 RegLocation rl_src) {
824 ThreadOffset<pointer_size> setter_offset =
825 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
826 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
827 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
828 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
829 rl_obj, rl_src, true);
830}
831
Vladimir Markobe0e5462014-02-26 11:24:15 +0000832void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700834 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000835 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
836 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100837 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
838 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
839 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
840 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000841 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700842 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100844 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 } else {
846 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100847 }
848 GenNullCheck(rl_obj.reg, opt_flags);
849 int field_offset = field_info.FieldOffset().Int32Value();
850 if (field_info.IsVolatile()) {
851 // There might have been a store before this volatile one so insert StoreStore barrier.
852 GenMemBarrier(kStoreStore);
853 StoreBaseDispVolatile(rl_obj.reg, field_offset, rl_src.reg, store_size);
Dave Allisonb373e092014-02-20 16:06:36 -0800854 MarkPossibleNullPointerException(opt_flags);
Vladimir Marko674744e2014-04-24 15:18:26 +0100855 // A load might follow the volatile store so insert a StoreLoad barrier.
856 GenMemBarrier(kStoreLoad);
857 } else {
858 StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size);
859 MarkPossibleNullPointerException(opt_flags);
860 }
861 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
862 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 }
864 } else {
buzbee33ae5582014-06-12 14:56:32 -0700865 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700866 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
867 } else {
868 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
869 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 }
871}
872
Andreas Gampe2f244e92014-05-08 03:35:25 -0700873template <size_t pointer_size>
874static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
875 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
876 ThreadOffset<pointer_size> helper = needs_range_check
877 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
878 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
879 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
880 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
881 true);
882}
883
Ian Rogersa9a82542013-10-04 11:17:26 -0700884void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
885 RegLocation rl_src) {
886 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
887 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
888 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700889 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700890 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
891 } else {
892 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
893 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700894}
895
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 RegLocation rl_method = LoadCurrMethod();
buzbee33ae5582014-06-12 14:56:32 -0700898 DCHECK(!cu_->target64 || rl_method.reg.Is64Bit());
899 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700900 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
902 *cu_->dex_file,
903 type_idx)) {
904 // Call out to helper which resolves type and verifies access.
905 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700906 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700907 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
908 type_idx, rl_method.reg, true);
909 } else {
910 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
911 type_idx, rl_method.reg, true);
912 }
buzbeea0cd2d72014-06-01 09:33:49 -0700913 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 StoreValue(rl_dest, rl_result);
915 } else {
916 // We're don't need access checks, load type from dex cache
917 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700918 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700919 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000920 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700921 LoadRefDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
923 type_idx) || SLOW_TYPE_PATH) {
924 // Slow path, at runtime test if type is null and if so initialize
925 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800926 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800927 LIR* cont = NewLIR0(kPseudoTargetLabel);
928
929 // Object to generate the slow path for class resolution.
930 class SlowPath : public LIRSlowPath {
931 public:
932 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
933 const RegLocation& rl_method, const RegLocation& rl_result) :
934 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
935 rl_method_(rl_method), rl_result_(rl_result) {
936 }
937
938 void Compile() {
939 GenerateTargetLabel();
940
buzbee33ae5582014-06-12 14:56:32 -0700941 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700942 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
943 rl_method_.reg, true);
944 } else {
945 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
946 rl_method_.reg, true);
947 }
buzbee2700f7e2014-03-07 09:46:20 -0800948 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800949
950 m2l_->OpUnconditionalBranch(cont_);
951 }
952
953 private:
954 const int type_idx_;
955 const RegLocation rl_method_;
956 const RegLocation rl_result_;
957 };
958
959 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800960 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800961
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800963 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 // Fast path, we're done - just store result
965 StoreValue(rl_dest, rl_result);
966 }
967 }
968}
969
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700970void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000972 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
973 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
975 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
976 // slow path, resolve string if not in dex cache
977 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700978 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800979
980 // If the Method* is already in a register, we can save a copy.
981 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800982 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800983 if (rl_method.location == kLocPhysReg) {
984 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800985 DCHECK(!IsTemp(rl_method.reg));
986 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800987 } else {
988 r_method = TargetReg(kArg2);
989 LoadCurrMethodDirect(r_method);
990 }
buzbee695d13a2014-04-19 13:32:20 -0700991 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
992 TargetReg(kArg0));
Mark Mendell766e9292014-01-27 07:55:47 -0800993
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 // Might call out to helper, which will return resolved string in kRet0
buzbeea0cd2d72014-06-01 09:33:49 -0700995 LoadRefDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700996 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
997 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -0800998
Mingyao Yang3b004ba2014-04-29 15:55:37 -0700999 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001000 // Object to generate the slow path for string resolution.
1001 class SlowPath : public LIRSlowPath {
1002 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001003 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1004 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1005 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001006 }
1007
1008 void Compile() {
1009 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001010 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001011 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1012 r_method_, string_idx_, true);
1013 } else {
1014 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1015 r_method_, string_idx_, true);
1016 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001017 m2l_->OpUnconditionalBranch(cont_);
1018 }
1019
1020 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001021 const RegStorage r_method_;
1022 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 };
1024
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001025 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001027
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001029 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 } else {
1031 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001032 RegStorage res_reg = AllocTempRef();
1033 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
buzbee695d13a2014-04-19 13:32:20 -07001034 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeea0cd2d72014-06-01 09:33:49 -07001035 LoadRefDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 StoreValue(rl_dest, rl_result);
1037 }
1038}
1039
Andreas Gampe2f244e92014-05-08 03:35:25 -07001040template <size_t pointer_size>
1041static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1042 RegLocation rl_dest) {
1043 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 // alloc will always check for resolution, do we also need to verify
1045 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001046 ThreadOffset<pointer_size> func_offset(-1);
1047 const DexFile* dex_file = cu->dex_file;
1048 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001049 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001050 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001051 bool is_type_initialized;
1052 bool use_direct_type_ptr;
1053 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001054 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001055 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001056 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1057 &direct_type_ptr, &is_finalizable) &&
1058 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001059 // The fast path.
1060 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001061 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001062 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001063 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1064 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001066 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1067 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 }
1069 } else {
1070 // Use the direct pointer.
1071 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001072 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1073 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001074 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001075 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1076 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001077 }
1078 }
1079 } else {
1080 // The slow path.
1081 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1083 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001084 }
1085 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001087 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1088 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 }
buzbeea0cd2d72014-06-01 09:33:49 -07001090 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001091 mir_to_lir->StoreValue(rl_dest, rl_result);
1092}
1093
1094/*
1095 * Let helper function take care of everything. Will
1096 * call Class::NewInstanceFromCode(type_idx, method);
1097 */
1098void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001099 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001100 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1101 } else {
1102 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1103 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104}
1105
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001106void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001108 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001109 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1110 } else {
1111 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1112 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113}
1114
1115// For final classes there are no sub-classes to check and so we can answer the instance-of
1116// question with simple comparisons.
1117void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1118 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001119 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001120 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001121
buzbeea0cd2d72014-06-01 09:33:49 -07001122 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001124 RegStorage result_reg = rl_result.reg;
1125 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 result_reg = AllocTypedTemp(false, kCoreReg);
1127 }
1128 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001129 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130
buzbeea0cd2d72014-06-01 09:33:49 -07001131 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1132 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133
1134 LoadCurrMethodDirect(check_class);
1135 if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001136 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1137 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 } else {
buzbee695d13a2014-04-19 13:32:20 -07001139 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1140 check_class);
1141 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001142 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001143 LoadRefDisp(check_class, offset_of_type, check_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 }
1145
1146 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001147 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 if (cu_->instruction_set == kThumb2) {
1149 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001150 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001152 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 } else {
1154 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1155 LoadConstant(result_reg, 1); // eq case - load true
1156 }
1157 LIR* target = NewLIR0(kPseudoTargetLabel);
1158 null_branchover->target = target;
1159 if (ne_branchover != NULL) {
1160 ne_branchover->target = target;
1161 }
1162 FreeTemp(object_class);
1163 FreeTemp(check_class);
1164 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001165 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 FreeTemp(result_reg);
1167 }
1168 StoreValue(rl_dest, rl_result);
1169}
1170
1171void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1172 bool type_known_abstract, bool use_declaring_class,
1173 bool can_assume_type_is_in_dex_cache,
1174 uint32_t type_idx, RegLocation rl_dest,
1175 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001176 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001177 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001178
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 FlushAllRegs();
1180 // May generate a call - use explicit registers
1181 LockCallTemps();
1182 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001183 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 if (needs_access_check) {
1185 // Check we have access to type_idx and if not throw IllegalAccessError,
1186 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001187 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001188 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1189 type_idx, true);
1190 } else {
1191 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1192 type_idx, true);
1193 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1195 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1196 } else if (use_declaring_class) {
1197 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001198 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -08001199 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 } else {
1201 // Load dex cache entry into class_reg (kArg2)
1202 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee695d13a2014-04-19 13:32:20 -07001203 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1204 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001205 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001206 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 if (!can_assume_type_is_in_dex_cache) {
1208 // Need to test presence of type in dex cache at runtime
1209 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1210 // Not resolved
1211 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001212 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001213 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1214 } else {
1215 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1216 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001217 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1219 // Rejoin code paths
1220 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1221 hop_branch->target = hop_target;
1222 }
1223 }
1224 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeea0cd2d72014-06-01 09:33:49 -07001225 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 if (cu_->instruction_set == kMips) {
1227 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001228 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 }
1230 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1231
1232 /* load object->klass_ */
1233 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001234 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1236 LIR* branchover = NULL;
1237 if (type_known_final) {
1238 // rl_result == ref == null == 0.
1239 if (cu_->instruction_set == kThumb2) {
1240 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001241 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001242 LoadConstant(rl_result.reg, 1); // .eq case - load true
1243 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001244 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001246 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001248 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 }
1250 } else {
1251 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001252 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001253 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1254 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001255 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 if (!type_known_abstract) {
1257 /* Uses conditional nullification */
1258 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001259 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1261 }
1262 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1263 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001264 if (it != nullptr) {
1265 OpEndIT(it);
1266 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 FreeTemp(r_tgt);
1268 } else {
1269 if (!type_known_abstract) {
1270 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001271 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1273 }
buzbee33ae5582014-06-12 14:56:32 -07001274 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001275 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1276 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001277 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1278 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1279 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 }
1281 }
1282 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001283 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 /* branch targets here */
1285 LIR* target = NewLIR0(kPseudoTargetLabel);
1286 StoreValue(rl_dest, rl_result);
1287 branch1->target = target;
1288 if (branchover != NULL) {
1289 branchover->target = target;
1290 }
1291}
1292
1293void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1294 bool type_known_final, type_known_abstract, use_declaring_class;
1295 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1296 *cu_->dex_file,
1297 type_idx,
1298 &type_known_final,
1299 &type_known_abstract,
1300 &use_declaring_class);
1301 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1302 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1303
1304 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1305 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1306 } else {
1307 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1308 use_declaring_class, can_assume_type_is_in_dex_cache,
1309 type_idx, rl_dest, rl_src);
1310 }
1311}
1312
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001313void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 bool type_known_final, type_known_abstract, use_declaring_class;
1315 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1316 *cu_->dex_file,
1317 type_idx,
1318 &type_known_final,
1319 &type_known_abstract,
1320 &use_declaring_class);
1321 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1322 // of the exception throw path.
1323 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001324 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 // Verifier type analysis proved this check cast would never cause an exception.
1326 return;
1327 }
1328 FlushAllRegs();
1329 // May generate a call - use explicit registers
1330 LockCallTemps();
1331 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001332 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 if (needs_access_check) {
1334 // Check we have access to type_idx and if not throw IllegalAccessError,
1335 // returns Class* in kRet0
1336 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001337 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001338 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1339 type_idx, TargetReg(kArg1), true);
1340 } else {
1341 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1342 type_idx, TargetReg(kArg1), true);
1343 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1345 } else if (use_declaring_class) {
buzbee695d13a2014-04-19 13:32:20 -07001346 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1347 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 } else {
1349 // Load dex cache entry into class_reg (kArg2)
buzbee695d13a2014-04-19 13:32:20 -07001350 LoadRefDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1351 class_reg);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001352 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
buzbee695d13a2014-04-19 13:32:20 -07001353 LoadRefDisp(class_reg, offset_of_type, class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1355 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001356 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1357 LIR* cont = NewLIR0(kPseudoTargetLabel);
1358
1359 // Slow path to initialize the type. Executed if the type is NULL.
1360 class SlowPath : public LIRSlowPath {
1361 public:
1362 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001363 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001364 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1365 class_reg_(class_reg) {
1366 }
1367
1368 void Compile() {
1369 GenerateTargetLabel();
1370
1371 // Call out to helper, which will return resolved type in kArg0
1372 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001373 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001374 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1375 m2l_->TargetReg(kArg1), true);
1376 } else {
1377 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1378 m2l_->TargetReg(kArg1), true);
1379 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001380 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1381 m2l_->OpUnconditionalBranch(cont_);
1382 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001383
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001384 public:
1385 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001386 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001387 };
1388
buzbee2700f7e2014-03-07 09:46:20 -08001389 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
1391 }
1392 // At this point, class_reg (kArg2) has class
1393 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001394
1395 // Slow path for the case where the classes are not equal. In this case we need
1396 // to call a helper function to do the check.
1397 class SlowPath : public LIRSlowPath {
1398 public:
1399 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1400 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1401 }
1402
1403 void Compile() {
1404 GenerateTargetLabel();
1405
1406 if (load_) {
buzbee695d13a2014-04-19 13:32:20 -07001407 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1408 m2l_->TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001409 }
buzbee33ae5582014-06-12 14:56:32 -07001410 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001411 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetReg(kArg2),
1412 m2l_->TargetReg(kArg1), true);
1413 } else {
1414 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
1415 m2l_->TargetReg(kArg1), true);
1416 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001417
1418 m2l_->OpUnconditionalBranch(cont_);
1419 }
1420
1421 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001422 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001423 };
1424
1425 if (type_known_abstract) {
1426 // Easier case, run slow path if target is non-null (slow path will load from target)
1427 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1428 LIR* cont = NewLIR0(kPseudoTargetLabel);
1429 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1430 } else {
1431 // Harder, more common case. We need to generate a forward branch over the load
1432 // if the target is null. If it's non-null we perform the load and branch to the
1433 // slow path if the classes are not equal.
1434
1435 /* Null is OK - continue */
1436 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1437 /* load object->klass_ */
1438 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee695d13a2014-04-19 13:32:20 -07001439 LoadRefDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001440
1441 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1442 LIR* cont = NewLIR0(kPseudoTargetLabel);
1443
1444 // Add the slow path that will not perform load since this is already done.
1445 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1446
1447 // Set the null check to branch to the continuation.
1448 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 }
1450}
1451
1452void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001453 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 RegLocation rl_result;
1455 if (cu_->instruction_set == kThumb2) {
1456 /*
1457 * NOTE: This is the one place in the code in which we might have
1458 * as many as six live temporary registers. There are 5 in the normal
1459 * set for Arm. Until we have spill capabilities, temporarily add
1460 * lr to the temp set. It is safe to do this locally, but note that
1461 * lr is used explicitly elsewhere in the code generator and cannot
1462 * normally be used as a general temp register.
1463 */
1464 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1465 FreeTemp(TargetReg(kLr)); // and make it available
1466 }
1467 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1468 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1469 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1470 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001471 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1472 RegStorage t_reg = AllocTemp();
1473 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1474 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1475 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 FreeTemp(t_reg);
1477 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001478 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1479 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 }
1481 /*
1482 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1483 * following StoreValueWide might need to allocate a temp register.
1484 * To further work around the lack of a spill capability, explicitly
1485 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1486 * Remove when spill is functional.
1487 */
1488 FreeRegLocTemps(rl_result, rl_src1);
1489 FreeRegLocTemps(rl_result, rl_src2);
1490 StoreValueWide(rl_dest, rl_result);
1491 if (cu_->instruction_set == kThumb2) {
1492 Clobber(TargetReg(kLr));
1493 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1494 }
1495}
1496
1497
Andreas Gampe2f244e92014-05-08 03:35:25 -07001498template <size_t pointer_size>
1499static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1500 RegLocation rl_shift) {
1501 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502
1503 switch (opcode) {
1504 case Instruction::SHL_LONG:
1505 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001506 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 break;
1508 case Instruction::SHR_LONG:
1509 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001510 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 break;
1512 case Instruction::USHR_LONG:
1513 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001514 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 break;
1516 default:
1517 LOG(FATAL) << "Unexpected case";
1518 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001519 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1520 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1521}
1522
1523void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1524 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001525 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001526 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1527 } else {
1528 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1529 }
buzbeea0cd2d72014-06-01 09:33:49 -07001530 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 StoreValueWide(rl_dest, rl_result);
1532}
1533
1534
1535void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001536 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001537 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 OpKind op = kOpBkpt;
1539 bool is_div_rem = false;
1540 bool check_zero = false;
1541 bool unary = false;
1542 RegLocation rl_result;
1543 bool shift_op = false;
1544 switch (opcode) {
1545 case Instruction::NEG_INT:
1546 op = kOpNeg;
1547 unary = true;
1548 break;
1549 case Instruction::NOT_INT:
1550 op = kOpMvn;
1551 unary = true;
1552 break;
1553 case Instruction::ADD_INT:
1554 case Instruction::ADD_INT_2ADDR:
1555 op = kOpAdd;
1556 break;
1557 case Instruction::SUB_INT:
1558 case Instruction::SUB_INT_2ADDR:
1559 op = kOpSub;
1560 break;
1561 case Instruction::MUL_INT:
1562 case Instruction::MUL_INT_2ADDR:
1563 op = kOpMul;
1564 break;
1565 case Instruction::DIV_INT:
1566 case Instruction::DIV_INT_2ADDR:
1567 check_zero = true;
1568 op = kOpDiv;
1569 is_div_rem = true;
1570 break;
1571 /* NOTE: returns in kArg1 */
1572 case Instruction::REM_INT:
1573 case Instruction::REM_INT_2ADDR:
1574 check_zero = true;
1575 op = kOpRem;
1576 is_div_rem = true;
1577 break;
1578 case Instruction::AND_INT:
1579 case Instruction::AND_INT_2ADDR:
1580 op = kOpAnd;
1581 break;
1582 case Instruction::OR_INT:
1583 case Instruction::OR_INT_2ADDR:
1584 op = kOpOr;
1585 break;
1586 case Instruction::XOR_INT:
1587 case Instruction::XOR_INT_2ADDR:
1588 op = kOpXor;
1589 break;
1590 case Instruction::SHL_INT:
1591 case Instruction::SHL_INT_2ADDR:
1592 shift_op = true;
1593 op = kOpLsl;
1594 break;
1595 case Instruction::SHR_INT:
1596 case Instruction::SHR_INT_2ADDR:
1597 shift_op = true;
1598 op = kOpAsr;
1599 break;
1600 case Instruction::USHR_INT:
1601 case Instruction::USHR_INT_2ADDR:
1602 shift_op = true;
1603 op = kOpLsr;
1604 break;
1605 default:
1606 LOG(FATAL) << "Invalid word arith op: " << opcode;
1607 }
1608 if (!is_div_rem) {
1609 if (unary) {
1610 rl_src1 = LoadValue(rl_src1, kCoreReg);
1611 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001612 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001614 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001615 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001616 RegStorage t_reg = AllocTemp();
1617 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 rl_src1 = LoadValue(rl_src1, kCoreReg);
1619 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001620 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 FreeTemp(t_reg);
1622 } else {
1623 rl_src1 = LoadValue(rl_src1, kCoreReg);
1624 rl_src2 = LoadValue(rl_src2, kCoreReg);
1625 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001626 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 }
1628 }
1629 StoreValue(rl_dest, rl_result);
1630 } else {
Dave Allison70202782013-10-22 17:52:19 -07001631 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 +01001632 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 rl_src1 = LoadValue(rl_src1, kCoreReg);
1634 rl_src2 = LoadValue(rl_src2, kCoreReg);
1635 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001636 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 }
buzbee2700f7e2014-03-07 09:46:20 -08001638 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001639 done = true;
1640 } else if (cu_->instruction_set == kThumb2) {
1641 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1642 // Use ARM SDIV instruction for division. For remainder we also need to
1643 // calculate using a MUL and subtract.
1644 rl_src1 = LoadValue(rl_src1, kCoreReg);
1645 rl_src2 = LoadValue(rl_src2, kCoreReg);
1646 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001647 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001648 }
buzbee2700f7e2014-03-07 09:46:20 -08001649 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001650 done = true;
1651 }
1652 }
1653
1654 // If we haven't already generated the code use the callout function.
1655 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 FlushAllRegs(); /* Send everything to home location */
1657 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee33ae5582014-06-12 14:56:32 -07001658 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001659 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1660 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1662 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001663 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 }
Dave Allison70202782013-10-22 17:52:19 -07001665 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001666 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001667 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1668 } else {
1669 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1670 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001671 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001672 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 else
1674 rl_result = GetReturnAlt();
1675 }
1676 StoreValue(rl_dest, rl_result);
1677 }
1678}
1679
1680/*
1681 * The following are the first-level codegen routines that analyze the format
1682 * of each bytecode then either dispatch special purpose codegen routines
1683 * or produce corresponding Thumb instructions directly.
1684 */
1685
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001687static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 x &= x - 1;
1689 return (x & (x - 1)) == 0;
1690}
1691
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1693// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001694bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001695 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001696 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1697 return false;
1698 }
1699 // No divide instruction for Arm, so check for more special cases
1700 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001701 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 }
1703 int k = LowestSetBit(lit);
1704 if (k >= 30) {
1705 // Avoid special cases.
1706 return false;
1707 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 rl_src = LoadValue(rl_src, kCoreReg);
1709 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001710 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001711 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001712 if (lit == 2) {
1713 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001714 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1715 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1716 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001718 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001720 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1721 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 }
1723 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001724 RegStorage t_reg1 = AllocTemp();
1725 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001727 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1728 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001730 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001732 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001734 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001736 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001737 }
1738 }
1739 StoreValue(rl_dest, rl_result);
1740 return true;
1741}
1742
1743// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1744// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001745bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001746 if (lit < 0) {
1747 return false;
1748 }
1749 if (lit == 0) {
1750 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1751 LoadConstant(rl_result.reg, 0);
1752 StoreValue(rl_dest, rl_result);
1753 return true;
1754 }
1755 if (lit == 1) {
1756 rl_src = LoadValue(rl_src, kCoreReg);
1757 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1758 OpRegCopy(rl_result.reg, rl_src.reg);
1759 StoreValue(rl_dest, rl_result);
1760 return true;
1761 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001762 // There is RegRegRegShift on Arm, so check for more special cases
1763 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001764 return EasyMultiply(rl_src, rl_dest, lit);
1765 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 // Can we simplify this multiplication?
1767 bool power_of_two = false;
1768 bool pop_count_le2 = false;
1769 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001770 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 power_of_two = true;
1772 } else if (IsPopCountLE2(lit)) {
1773 pop_count_le2 = true;
1774 } else if (IsPowerOfTwo(lit + 1)) {
1775 power_of_two_minus_one = true;
1776 } else {
1777 return false;
1778 }
1779 rl_src = LoadValue(rl_src, kCoreReg);
1780 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1781 if (power_of_two) {
1782 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001783 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 } else if (pop_count_le2) {
1785 // Shift and add and shift.
1786 int first_bit = LowestSetBit(lit);
1787 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1788 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1789 } else {
1790 // Reverse subtract: (src << (shift + 1)) - src.
1791 DCHECK(power_of_two_minus_one);
1792 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001793 RegStorage t_reg = AllocTemp();
1794 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1795 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 }
1797 StoreValue(rl_dest, rl_result);
1798 return true;
1799}
1800
1801void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001802 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001803 RegLocation rl_result;
1804 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1805 int shift_op = false;
1806 bool is_div = false;
1807
1808 switch (opcode) {
1809 case Instruction::RSUB_INT_LIT8:
1810 case Instruction::RSUB_INT: {
1811 rl_src = LoadValue(rl_src, kCoreReg);
1812 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1813 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001814 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001816 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1817 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 }
1819 StoreValue(rl_dest, rl_result);
1820 return;
1821 }
1822
1823 case Instruction::SUB_INT:
1824 case Instruction::SUB_INT_2ADDR:
1825 lit = -lit;
1826 // Intended fallthrough
1827 case Instruction::ADD_INT:
1828 case Instruction::ADD_INT_2ADDR:
1829 case Instruction::ADD_INT_LIT8:
1830 case Instruction::ADD_INT_LIT16:
1831 op = kOpAdd;
1832 break;
1833 case Instruction::MUL_INT:
1834 case Instruction::MUL_INT_2ADDR:
1835 case Instruction::MUL_INT_LIT8:
1836 case Instruction::MUL_INT_LIT16: {
1837 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1838 return;
1839 }
1840 op = kOpMul;
1841 break;
1842 }
1843 case Instruction::AND_INT:
1844 case Instruction::AND_INT_2ADDR:
1845 case Instruction::AND_INT_LIT8:
1846 case Instruction::AND_INT_LIT16:
1847 op = kOpAnd;
1848 break;
1849 case Instruction::OR_INT:
1850 case Instruction::OR_INT_2ADDR:
1851 case Instruction::OR_INT_LIT8:
1852 case Instruction::OR_INT_LIT16:
1853 op = kOpOr;
1854 break;
1855 case Instruction::XOR_INT:
1856 case Instruction::XOR_INT_2ADDR:
1857 case Instruction::XOR_INT_LIT8:
1858 case Instruction::XOR_INT_LIT16:
1859 op = kOpXor;
1860 break;
1861 case Instruction::SHL_INT_LIT8:
1862 case Instruction::SHL_INT:
1863 case Instruction::SHL_INT_2ADDR:
1864 lit &= 31;
1865 shift_op = true;
1866 op = kOpLsl;
1867 break;
1868 case Instruction::SHR_INT_LIT8:
1869 case Instruction::SHR_INT:
1870 case Instruction::SHR_INT_2ADDR:
1871 lit &= 31;
1872 shift_op = true;
1873 op = kOpAsr;
1874 break;
1875 case Instruction::USHR_INT_LIT8:
1876 case Instruction::USHR_INT:
1877 case Instruction::USHR_INT_2ADDR:
1878 lit &= 31;
1879 shift_op = true;
1880 op = kOpLsr;
1881 break;
1882
1883 case Instruction::DIV_INT:
1884 case Instruction::DIV_INT_2ADDR:
1885 case Instruction::DIV_INT_LIT8:
1886 case Instruction::DIV_INT_LIT16:
1887 case Instruction::REM_INT:
1888 case Instruction::REM_INT_2ADDR:
1889 case Instruction::REM_INT_LIT8:
1890 case Instruction::REM_INT_LIT16: {
1891 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001892 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 return;
1894 }
buzbee11b63d12013-08-27 07:34:17 -07001895 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001896 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001897 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 (opcode == Instruction::DIV_INT_LIT16)) {
1899 is_div = true;
1900 } else {
1901 is_div = false;
1902 }
buzbee11b63d12013-08-27 07:34:17 -07001903 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1904 return;
1905 }
Dave Allison70202782013-10-22 17:52:19 -07001906
1907 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001908 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001909 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001910 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001911 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001912 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001913 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1914 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001915 } else if (cu_->instruction_set == kThumb2) {
1916 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1917 // Use ARM SDIV instruction for division. For remainder we also need to
1918 // calculate using a MUL and subtract.
1919 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001920 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001921 done = true;
1922 }
1923 }
1924
1925 if (!done) {
1926 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1928 Clobber(TargetReg(kArg0));
buzbee33ae5582014-06-12 14:56:32 -07001929 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001930 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0), lit,
1931 false);
1932 } else {
1933 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0), lit,
1934 false);
1935 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001937 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001938 else
1939 rl_result = GetReturnAlt();
1940 }
1941 StoreValue(rl_dest, rl_result);
1942 return;
1943 }
1944 default:
1945 LOG(FATAL) << "Unexpected opcode " << opcode;
1946 }
1947 rl_src = LoadValue(rl_src, kCoreReg);
1948 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001949 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001951 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001953 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 }
1955 StoreValue(rl_dest, rl_result);
1956}
1957
Andreas Gampe2f244e92014-05-08 03:35:25 -07001958template <size_t pointer_size>
1959static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1960 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001961 RegLocation rl_result;
1962 OpKind first_op = kOpBkpt;
1963 OpKind second_op = kOpBkpt;
1964 bool call_out = false;
1965 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001966 ThreadOffset<pointer_size> func_offset(-1);
1967 int ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968
1969 switch (opcode) {
1970 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001971 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001972 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1973 return;
1974 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001975 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1976 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001978 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001979 RegStorage t_reg = mir_to_lir->AllocTemp();
1980 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1981 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1982 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
1983 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001985 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1986 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001988 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 return;
1990 case Instruction::ADD_LONG:
1991 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001992 if (cu->instruction_set != kThumb2) {
1993 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 return;
1995 }
1996 first_op = kOpAdd;
1997 second_op = kOpAdc;
1998 break;
1999 case Instruction::SUB_LONG:
2000 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002001 if (cu->instruction_set != kThumb2) {
2002 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002003 return;
2004 }
2005 first_op = kOpSub;
2006 second_op = kOpSbc;
2007 break;
2008 case Instruction::MUL_LONG:
2009 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010 if (cu->instruction_set != kMips) {
2011 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 return;
2013 } else {
2014 call_out = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002015 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2016 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002017 }
2018 break;
2019 case Instruction::DIV_LONG:
2020 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002021 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002022 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2023 return;
2024 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002025 call_out = true;
2026 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002027 ret_reg = mir_to_lir->TargetReg(kRet0).GetReg();
2028 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 break;
2030 case Instruction::REM_LONG:
2031 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002032 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002033 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2034 return;
2035 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002036 call_out = true;
2037 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002038 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002040 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2).GetReg() :
2041 mir_to_lir->TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 break;
2043 case Instruction::AND_LONG_2ADDR:
2044 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002045 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2046 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002047 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 }
2049 first_op = kOpAnd;
2050 second_op = kOpAnd;
2051 break;
2052 case Instruction::OR_LONG:
2053 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002054 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2055 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002056 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002057 return;
2058 }
2059 first_op = kOpOr;
2060 second_op = kOpOr;
2061 break;
2062 case Instruction::XOR_LONG:
2063 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002064 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2065 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002066 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 return;
2068 }
2069 first_op = kOpXor;
2070 second_op = kOpXor;
2071 break;
2072 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002073 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 return;
2075 }
2076 default:
2077 LOG(FATAL) << "Invalid long arith op";
2078 }
2079 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002080 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002081 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002082 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002083 if (check_zero) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002084 RegStorage r_tmp1 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg0),
2085 mir_to_lir->TargetReg(kArg1));
2086 RegStorage r_tmp2 = RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2087 mir_to_lir->TargetReg(kArg3));
2088 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2089 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
2090 mir_to_lir->GenDivZeroCheckWide(RegStorage::MakeRegPair(mir_to_lir->TargetReg(kArg2),
2091 mir_to_lir->TargetReg(kArg3)));
2092 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002093 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002094 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002096 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 }
2098 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 if (ret_reg == mir_to_lir->TargetReg(kRet0).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002100 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002102 rl_result = mir_to_lir->GetReturnWideAlt();
2103 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002104 }
2105}
2106
Andreas Gampe2f244e92014-05-08 03:35:25 -07002107void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2108 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002109 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002110 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2111 } else {
2112 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2113 }
2114}
2115
Mark Mendelle87f9b52014-04-30 14:13:18 -04002116void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2117 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2118 LoadConstantNoClobber(rl_result.reg, value);
2119 StoreValue(rl_dest, rl_result);
2120 if (value == 0) {
2121 Workaround7250540(rl_dest, rl_result.reg);
2122 }
2123}
2124
Andreas Gampe2f244e92014-05-08 03:35:25 -07002125template <size_t pointer_size>
2126void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002127 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002128 /*
2129 * Don't optimize the register usage since it calls out to support
2130 * functions
2131 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002132 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2133
Brian Carlstrom7940e442013-07-12 13:46:57 -07002134 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002135 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2136 if (rl_dest.wide) {
2137 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002138 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002139 StoreValueWide(rl_dest, rl_result);
2140 } else {
2141 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002142 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002143 StoreValue(rl_dest, rl_result);
2144 }
2145}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002146template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2147 RegLocation rl_dest, RegLocation rl_src);
2148template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2149 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002150
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002151class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2152 public:
2153 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2154 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2155 }
2156
2157 void Compile() OVERRIDE {
2158 m2l_->ResetRegPool();
2159 m2l_->ResetDefTracking();
2160 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002161 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002162 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2163 } else {
2164 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2165 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002166 if (cont_ != nullptr) {
2167 m2l_->OpUnconditionalBranch(cont_);
2168 }
2169 }
2170};
2171
Brian Carlstrom7940e442013-07-12 13:46:57 -07002172/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002173void Mir2Lir::GenSuspendTest(int opt_flags) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002174 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002175 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2176 return;
2177 }
2178 FlushAllRegs();
2179 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002180 LIR* cont = NewLIR0(kPseudoTargetLabel);
2181 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002182 } else {
2183 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2184 return;
2185 }
2186 FlushAllRegs(); // TODO: needed?
2187 LIR* inst = CheckSuspendUsingLoad();
2188 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002189 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002190}
2191
2192/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002193void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Andreas Gampe5655e842014-06-17 16:36:07 -07002194 if (cu_->compiler_driver->GetCompilerOptions().GetExplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002195 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2196 OpUnconditionalBranch(target);
2197 return;
2198 }
2199 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002200 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002201 LIR* branch = OpUnconditionalBranch(nullptr);
2202 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002203 } else {
2204 // For the implicit suspend check, just perform the trigger
2205 // load and branch to the target.
2206 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2207 OpUnconditionalBranch(target);
2208 return;
2209 }
2210 FlushAllRegs();
2211 LIR* inst = CheckSuspendUsingLoad();
2212 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002213 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002214 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002215}
2216
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002217/* Call out to helper assembly routine that will null check obj and then lock it. */
2218void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2219 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002220 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002221 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2222 } else {
2223 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2224 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002225}
2226
2227/* Call out to helper assembly routine that will null check obj and then unlock it. */
2228void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2229 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002230 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002231 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2232 } else {
2233 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2234 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002235}
2236
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002237/* Generic code for generating a wide constant into a VR. */
2238void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2239 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002240 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002241 StoreValueWide(rl_dest, rl_result);
2242}
2243
Brian Carlstrom7940e442013-07-12 13:46:57 -07002244} // namespace art