blob: adc228cf454760f6c64d675a88aa5ebfb1564846 [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
Andreas Gampeccc60262014-07-04 18:02:38 -0700130 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
131 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700132
133 m2l_->OpRegCopy(arg1_32, length_);
134 m2l_->LoadConstant(arg0_32, index_);
buzbee33ae5582014-06-12 14:56:32 -0700135 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700136 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pThrowArrayBounds),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700137 arg0_32, arg1_32, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700138 } else {
139 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
Andreas Gampe4b537a82014-06-30 22:24:53 -0700140 arg0_32, arg1_32, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700141 }
Mingyao Yang80365d92014-04-18 12:10:58 -0700142 }
143
144 private:
145 const int32_t index_;
146 const RegStorage length_;
147 };
148
149 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
150 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
151}
152
Mingyao Yange643a172014-04-08 11:02:52 -0700153LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
154 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
155 public:
156 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
157 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
158 }
159
160 void Compile() OVERRIDE {
161 m2l_->ResetRegPool();
162 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700163 GenerateTargetLabel(kPseudoThrowTarget);
buzbee33ae5582014-06-12 14:56:32 -0700164 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700165 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pThrowNullPointer), true);
166 } else {
167 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
168 }
Mingyao Yange643a172014-04-08 11:02:52 -0700169 }
170 };
171
172 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
173 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
174 return branch;
175}
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800178LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000179 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700180 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
Dave Allisonb373e092014-02-20 16:06:36 -0800182 return nullptr;
183}
184
Dave Allisonf9439142014-03-27 15:10:22 -0700185/* Perform an explicit null-check on a register. */
186LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
187 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
188 return NULL;
189 }
Mingyao Yange643a172014-04-08 11:02:52 -0700190 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700191}
192
Dave Allisonb373e092014-02-20 16:06:36 -0800193void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000194 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800195 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
196 return;
197 }
Dave Allison69dfe512014-07-11 17:11:58 +0000198 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800199 MarkSafepointPC(last_lir_insn_);
200 }
201}
202
Andreas Gampe3c12c512014-06-24 18:46:29 +0000203void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000204 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000205 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
206 return;
207 }
208 MarkSafepointPCAfter(after);
209 }
210}
211
Dave Allisonb373e092014-02-20 16:06:36 -0800212void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000213 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800214 MarkSafepointPC(last_lir_insn_);
215 }
216}
217
buzbee2700f7e2014-03-07 09:46:20 -0800218void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000219 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800220 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
221 return;
222 }
223 // Force an implicit null check by performing a memory operation (load) from the given
224 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800225 RegStorage tmp = AllocTemp();
226 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700227 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800228 FreeTemp(tmp);
229 MarkSafepointPC(load);
230 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231}
232
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
234 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700235 LIR* fall_through) {
buzbeea0cd2d72014-06-01 09:33:49 -0700236 DCHECK(!rl_src1.fp);
237 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 ConditionCode cond;
239 switch (opcode) {
240 case Instruction::IF_EQ:
241 cond = kCondEq;
242 break;
243 case Instruction::IF_NE:
244 cond = kCondNe;
245 break;
246 case Instruction::IF_LT:
247 cond = kCondLt;
248 break;
249 case Instruction::IF_GE:
250 cond = kCondGe;
251 break;
252 case Instruction::IF_GT:
253 cond = kCondGt;
254 break;
255 case Instruction::IF_LE:
256 cond = kCondLe;
257 break;
258 default:
259 cond = static_cast<ConditionCode>(0);
260 LOG(FATAL) << "Unexpected opcode " << opcode;
261 }
262
263 // Normalize such that if either operand is constant, src2 will be constant
264 if (rl_src1.is_const) {
265 RegLocation rl_temp = rl_src1;
266 rl_src1 = rl_src2;
267 rl_src2 = rl_temp;
268 cond = FlipComparisonOrder(cond);
269 }
270
buzbeea0cd2d72014-06-01 09:33:49 -0700271 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 // Is this really an immediate comparison?
273 if (rl_src2.is_const) {
274 // If it's already live in a register or not easily materialized, just keep going
275 RegLocation rl_temp = UpdateLoc(rl_src2);
276 if ((rl_temp.location == kLocDalvikFrame) &&
277 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
278 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800279 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 return;
281 }
282 }
buzbeea0cd2d72014-06-01 09:33:49 -0700283 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800284 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285}
286
287void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700288 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700290 DCHECK(!rl_src.fp);
291 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 switch (opcode) {
293 case Instruction::IF_EQZ:
294 cond = kCondEq;
295 break;
296 case Instruction::IF_NEZ:
297 cond = kCondNe;
298 break;
299 case Instruction::IF_LTZ:
300 cond = kCondLt;
301 break;
302 case Instruction::IF_GEZ:
303 cond = kCondGe;
304 break;
305 case Instruction::IF_GTZ:
306 cond = kCondGt;
307 break;
308 case Instruction::IF_LEZ:
309 cond = kCondLe;
310 break;
311 default:
312 cond = static_cast<ConditionCode>(0);
313 LOG(FATAL) << "Unexpected opcode " << opcode;
314 }
buzbee2700f7e2014-03-07 09:46:20 -0800315 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316}
317
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700318void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
320 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800321 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800323 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 }
buzbee2700f7e2014-03-07 09:46:20 -0800325 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 StoreValueWide(rl_dest, rl_result);
327}
328
329void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700331 rl_src = LoadValue(rl_src, kCoreReg);
332 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
333 OpKind op = kOpInvalid;
334 switch (opcode) {
335 case Instruction::INT_TO_BYTE:
336 op = kOp2Byte;
337 break;
338 case Instruction::INT_TO_SHORT:
339 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700341 case Instruction::INT_TO_CHAR:
342 op = kOp2Char;
343 break;
344 default:
345 LOG(ERROR) << "Bad int conversion type";
346 }
buzbee2700f7e2014-03-07 09:46:20 -0800347 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700348 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349}
350
Andreas Gampe2f244e92014-05-08 03:35:25 -0700351template <size_t pointer_size>
352static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
353 uint32_t type_idx, RegLocation rl_dest,
354 RegLocation rl_src) {
355 mir_to_lir->FlushAllRegs(); /* Everything to home location */
356 ThreadOffset<pointer_size> func_offset(-1);
357 const DexFile* dex_file = cu->dex_file;
358 CompilerDriver* driver = cu->compiler_driver;
359 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
360 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 bool is_type_initialized; // Ignored as an array does not have an initializer.
362 bool use_direct_type_ptr;
363 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700364 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800365 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700366 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
367 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800368 // The fast path.
369 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700370 mir_to_lir->LoadClassType(type_idx, kArg0);
371 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -0700372 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset,
373 mir_to_lir->TargetReg(kArg0, kNotWide),
Andreas Gampe2f244e92014-05-08 03:35:25 -0700374 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800375 } else {
376 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700377 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
378 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
379 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800380 }
381 } else {
382 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700383 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
384 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800385 }
386 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700388 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
389 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 }
buzbeea0cd2d72014-06-01 09:33:49 -0700391 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700392 mir_to_lir->StoreValue(rl_dest, rl_result);
393}
394
395/*
396 * Let helper function take care of everything. Will call
397 * Array::AllocFromCode(type_idx, method, count);
398 * Note: AllocFromCode will handle checks for errNegativeArraySize.
399 */
400void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
401 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700402 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700403 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
404 } else {
405 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
406 }
407}
408
409template <size_t pointer_size>
410static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
411 ThreadOffset<pointer_size> func_offset(-1);
412 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
413 type_idx)) {
414 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
415 } else {
416 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
417 }
418 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419}
420
421/*
422 * Similar to GenNewArray, but with post-allocation initialization.
423 * Verifier guarantees we're dealing with an array class. Current
424 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
425 * Current code also throws internal unimp if not 'L', '[' or 'I'.
426 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700427void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 int elems = info->num_arg_words;
429 int type_idx = info->index;
430 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700431 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700432 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700434 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700436 FreeTemp(TargetReg(kArg2, kNotWide));
437 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 /*
439 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
440 * return region. Because AllocFromCode placed the new array
441 * in kRet0, we'll just lock it into place. When debugger support is
442 * added, it may be necessary to additionally copy all return
443 * values to a home location in thread-local storage
444 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700445 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700446 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447
448 // TODO: use the correct component size, currently all supported types
449 // share array alignment with ints (see comment at head of function)
450 size_t component_size = sizeof(int32_t);
451
452 // Having a range of 0 is legal
453 if (info->is_range && (elems > 0)) {
454 /*
455 * Bit of ugliness here. We're going generate a mem copy loop
456 * on the register range, but it is possible that some regs
457 * in the range have been promoted. This is unlikely, but
458 * before generating the copy, we'll just force a flush
459 * of any regs in the source range that have been promoted to
460 * home location.
461 */
462 for (int i = 0; i < elems; i++) {
463 RegLocation loc = UpdateLoc(info->args[i]);
464 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100465 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700466 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 }
468 }
469 /*
470 * TUNING note: generated code here could be much improved, but
471 * this is an uncommon operation and isn't especially performance
472 * critical.
473 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700474 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700475 RegStorage r_src = AllocTempRef();
476 RegStorage r_dst = AllocTempRef();
477 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800478 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700479 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700481 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700482 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 break;
484 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700485 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700486 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 r_val = AllocTemp();
488 break;
489 case kMips:
490 r_val = AllocTemp();
491 break;
492 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
493 }
494 // Set up source pointer
495 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700496 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700498 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 mirror::Array::DataOffset(component_size).Int32Value());
500 // Set up the loop counter (known to be > 0)
501 LoadConstant(r_idx, elems - 1);
502 // Generate the copy loop. Going backwards for convenience
503 LIR* target = NewLIR0(kPseudoTargetLabel);
504 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100505 {
506 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
507 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
508 // NOTE: No dalvik register annotation, local optimizations will be stopped
509 // by the loop boundaries.
510 }
buzbee695d13a2014-04-19 13:32:20 -0700511 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 FreeTemp(r_val);
513 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700514 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700516 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 -mirror::Array::DataOffset(component_size).Int32Value());
518 }
519 } else if (!info->is_range) {
520 // TUNING: interleave
521 for (int i = 0; i < elems; i++) {
522 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700523 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000524 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800526 if (IsTemp(rl_arg.reg)) {
527 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 }
529 }
530 }
531 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700532 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 }
534}
535
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800536//
537// Slow path to ensure a class is initialized for sget/sput.
538//
539class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
540 public:
buzbee2700f7e2014-03-07 09:46:20 -0800541 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
542 RegStorage r_base) :
543 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
544 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800545 }
546
547 void Compile() {
548 LIR* unresolved_target = GenerateTargetLabel();
549 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700550 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700551 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
552 storage_index_, true);
553 } else {
554 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
555 storage_index_, true);
556 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800557 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700558 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800559
560 m2l_->OpUnconditionalBranch(cont_);
561 }
562
563 private:
564 LIR* const uninit_;
565 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800566 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800567};
568
Andreas Gampe2f244e92014-05-08 03:35:25 -0700569template <size_t pointer_size>
570static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
571 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
572 ThreadOffset<pointer_size> setter_offset =
573 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
574 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
575 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
576 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
577 true);
578}
579
Vladimir Markobe0e5462014-02-26 11:24:15 +0000580void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700581 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000582 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
583 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100584 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700585 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800587 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100590 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700591 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000592 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
593 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800594 if (IsTemp(rl_method.reg)) {
595 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 } else {
598 // Medium path, static storage base in a different class which requires checks that the other
599 // class is initialized.
600 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000601 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 // May do runtime call so everything to home locations.
603 FlushAllRegs();
604 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700605 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LockTemp(r_method);
607 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700608 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000610 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
611 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000612 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000613 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000615 if (!field_info.IsInitialized() &&
616 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800617 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800618
619 // The slow path is invoked if the r_base is NULL or the class pointed
620 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700622 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800623 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800624 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800625 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000626 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800627 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628
buzbee2700f7e2014-03-07 09:46:20 -0800629 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000630 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800631
632 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700633 // Ensure load of status and store of value don't re-order.
634 // TODO: Presumably the actual value store is control-dependent on the status load,
635 // and will thus not be reordered in any case, since stores are never speculated.
636 // Does later code "know" that the class is now initialized? If so, we still
637 // need the barrier to guard later static loads.
638 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 FreeTemp(r_method);
641 }
642 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100643 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100645 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100647 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000649 if (is_object) {
650 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
651 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100652 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000653 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
654 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
656 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800657 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800659 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 } else {
661 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700662 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700663 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
664 } else {
665 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
666 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668}
669
Andreas Gampe2f244e92014-05-08 03:35:25 -0700670template <size_t pointer_size>
671static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
672 const MirSFieldLoweringInfo* field_info) {
673 ThreadOffset<pointer_size> getter_offset =
674 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
675 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
676 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
677 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
678}
679
Vladimir Markobe0e5462014-02-26 11:24:15 +0000680void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700681 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000682 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
683 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100684 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700685 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000686 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800687 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000688 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 // Fast path, static storage base is this method's class
690 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700691 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000692 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
693 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 } else {
695 // Medium path, static storage base in a different class which requires checks that the other
696 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000697 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 // May do runtime call so everything to home locations.
699 FlushAllRegs();
700 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700701 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 LockTemp(r_method);
703 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700704 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800705 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000706 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
707 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000708 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000709 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800710 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000711 if (!field_info.IsInitialized() &&
712 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800713 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800714
715 // The slow path is invoked if the r_base is NULL or the class pointed
716 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800717 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700718 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800719 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800720 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800721 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000722 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800723 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800724
buzbee2700f7e2014-03-07 09:46:20 -0800725 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000726 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800727
728 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700729 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700730 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 FreeTemp(r_method);
733 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800734 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100735 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
736 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800737
Vladimir Marko674744e2014-04-24 15:18:26 +0100738 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000739 if (is_object) {
740 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
741 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100742 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000743 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
744 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800745 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100746 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800747
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 if (is_long_or_double) {
749 StoreValueWide(rl_dest, rl_result);
750 } else {
751 StoreValue(rl_dest, rl_result);
752 }
753 } else {
754 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700755 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700756 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
757 } else {
758 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
759 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700760 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700762 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 StoreValueWide(rl_dest, rl_result);
764 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700765 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 StoreValue(rl_dest, rl_result);
767 }
768 }
769}
770
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800771// Generate code for all slow paths.
772void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700773 // We should check slow_paths_.Size() every time, because a new slow path
774 // may be created during slowpath->Compile().
775 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800776 LIRSlowPath* slowpath = slow_paths_.Get(i);
777 slowpath->Compile();
778 }
779 slow_paths_.Reset();
780}
781
Andreas Gampe2f244e92014-05-08 03:35:25 -0700782template <size_t pointer_size>
783static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
784 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
785 ThreadOffset<pointer_size> getter_offset =
786 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
787 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
788 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700789 // Second argument of pGetXXInstance is always a reference.
790 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700791 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
792 true);
793}
794
Vladimir Markobe0e5462014-02-26 11:24:15 +0000795void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700797 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000798 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
799 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100800 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700801 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100802 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000803 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700804 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100805 GenNullCheck(rl_obj.reg, opt_flags);
806 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
807 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000808 LIR* load_lir;
809 if (is_object) {
810 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
811 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100812 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000813 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
814 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100815 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000816 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 StoreValueWide(rl_dest, rl_result);
819 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 StoreValue(rl_dest, rl_result);
821 }
822 } else {
buzbee33ae5582014-06-12 14:56:32 -0700823 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700824 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
825 } else {
826 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
827 }
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700828 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700830 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 StoreValueWide(rl_dest, rl_result);
832 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700833 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 StoreValue(rl_dest, rl_result);
835 }
836 }
837}
838
Andreas Gampe2f244e92014-05-08 03:35:25 -0700839template <size_t pointer_size>
840static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
841 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
842 RegLocation rl_src) {
843 ThreadOffset<pointer_size> setter_offset =
844 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
845 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
846 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
847 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
848 rl_obj, rl_src, true);
849}
850
Vladimir Markobe0e5462014-02-26 11:24:15 +0000851void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700853 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000854 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
855 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100856 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700857 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100858 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000859 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700860 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100862 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 } else {
864 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100865 }
866 GenNullCheck(rl_obj.reg, opt_flags);
867 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000868 LIR* store;
869 if (is_object) {
870 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
871 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100872 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000873 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
874 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100875 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000876 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100877 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
878 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 }
880 } else {
buzbee33ae5582014-06-12 14:56:32 -0700881 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700882 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
883 } else {
884 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
885 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 }
887}
888
Andreas Gampe2f244e92014-05-08 03:35:25 -0700889template <size_t pointer_size>
890static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
891 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
892 ThreadOffset<pointer_size> helper = needs_range_check
893 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
894 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
895 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
896 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
897 true);
898}
899
Ian Rogersa9a82542013-10-04 11:17:26 -0700900void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
901 RegLocation rl_src) {
902 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
903 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
904 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700905 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700906 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
907 } else {
908 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
909 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700910}
911
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700912void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700914 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700915 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700916 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700918 *cu_->dex_file,
919 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 // Call out to helper which resolves type and verifies access.
921 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700922 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700923 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
924 type_idx, rl_method.reg, true);
925 } else {
926 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
927 type_idx, rl_method.reg, true);
928 }
buzbeea0cd2d72014-06-01 09:33:49 -0700929 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 StoreValue(rl_dest, rl_result);
931 } else {
932 // We're don't need access checks, load type from dex cache
933 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700934 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000935 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000936 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000937 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
939 type_idx) || SLOW_TYPE_PATH) {
940 // Slow path, at runtime test if type is null and if so initialize
941 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800942 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800943 LIR* cont = NewLIR0(kPseudoTargetLabel);
944
945 // Object to generate the slow path for class resolution.
946 class SlowPath : public LIRSlowPath {
947 public:
948 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
949 const RegLocation& rl_method, const RegLocation& rl_result) :
950 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
951 rl_method_(rl_method), rl_result_(rl_result) {
952 }
953
954 void Compile() {
955 GenerateTargetLabel();
956
buzbee33ae5582014-06-12 14:56:32 -0700957 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700958 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
959 rl_method_.reg, true);
960 } else {
961 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
962 rl_method_.reg, true);
963 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700964 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800965
966 m2l_->OpUnconditionalBranch(cont_);
967 }
968
969 private:
970 const int type_idx_;
971 const RegLocation rl_method_;
972 const RegLocation rl_result_;
973 };
974
975 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800976 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800977
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800979 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 // Fast path, we're done - just store result
981 StoreValue(rl_dest, rl_result);
982 }
983 }
984}
985
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700986void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000988 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
989 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
991 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
992 // slow path, resolve string if not in dex cache
993 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700994 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800995
996 // If the Method* is already in a register, we can save a copy.
997 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800998 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800999 if (rl_method.location == kLocPhysReg) {
1000 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001001 DCHECK(!IsTemp(rl_method.reg));
1002 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001003 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001004 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001005 LoadCurrMethodDirect(r_method);
1006 }
buzbee695d13a2014-04-19 13:32:20 -07001007 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001008 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001009
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001011 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1012 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001013 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001014
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001015 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001016 // Object to generate the slow path for string resolution.
1017 class SlowPath : public LIRSlowPath {
1018 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001019 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1020 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1021 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001022 }
1023
1024 void Compile() {
1025 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001026 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001027 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1028 r_method_, string_idx_, true);
1029 } else {
1030 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1031 r_method_, string_idx_, true);
1032 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001033 m2l_->OpUnconditionalBranch(cont_);
1034 }
1035
1036 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001037 const RegStorage r_method_;
1038 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001039 };
1040
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001041 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001043
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001045 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 } else {
1047 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001048 RegStorage res_reg = AllocTempRef();
1049 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001050 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1051 kNotVolatile);
1052 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 StoreValue(rl_dest, rl_result);
1054 }
1055}
1056
Andreas Gampe2f244e92014-05-08 03:35:25 -07001057template <size_t pointer_size>
1058static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1059 RegLocation rl_dest) {
1060 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 // alloc will always check for resolution, do we also need to verify
1062 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001063 ThreadOffset<pointer_size> func_offset(-1);
1064 const DexFile* dex_file = cu->dex_file;
1065 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001066 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001067 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 bool is_type_initialized;
1069 bool use_direct_type_ptr;
1070 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001071 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001073 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1074 &direct_type_ptr, &is_finalizable) &&
1075 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001076 // The fast path.
1077 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001078 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001079 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001080 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -07001081 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1082 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001083 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001084 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
Andreas Gampeccc60262014-07-04 18:02:38 -07001085 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1086 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001087 }
1088 } else {
1089 // Use the direct pointer.
1090 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001091 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1092 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001093 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001094 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1095 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001096 }
1097 }
1098 } else {
1099 // The slow path.
1100 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001101 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1102 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001103 }
1104 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001106 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1107 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 }
buzbeea0cd2d72014-06-01 09:33:49 -07001109 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001110 mir_to_lir->StoreValue(rl_dest, rl_result);
1111}
1112
1113/*
1114 * Let helper function take care of everything. Will
1115 * call Class::NewInstanceFromCode(type_idx, method);
1116 */
1117void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001118 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001119 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1120 } else {
1121 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123}
1124
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001125void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001127 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001128 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1129 } else {
1130 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1131 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132}
1133
1134// For final classes there are no sub-classes to check and so we can answer the instance-of
1135// question with simple comparisons.
1136void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1137 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001138 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001139 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001140
buzbeea0cd2d72014-06-01 09:33:49 -07001141 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001143 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001144 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001146 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001149 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150
buzbeea0cd2d72014-06-01 09:33:49 -07001151 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1152 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153
1154 LoadCurrMethodDirect(check_class);
1155 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001156 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1157 kNotVolatile);
1158 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1159 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 } else {
buzbee695d13a2014-04-19 13:32:20 -07001161 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001162 check_class, kNotVolatile);
1163 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1164 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001165 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001166 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
1168
buzbee695d13a2014-04-19 13:32:20 -07001169 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 if (cu_->instruction_set == kThumb2) {
1171 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001172 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001174 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001176 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178 LIR* target = NewLIR0(kPseudoTargetLabel);
1179 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 FreeTemp(object_class);
1181 FreeTemp(check_class);
1182 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001183 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 FreeTemp(result_reg);
1185 }
1186 StoreValue(rl_dest, rl_result);
1187}
1188
1189void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1190 bool type_known_abstract, bool use_declaring_class,
1191 bool can_assume_type_is_in_dex_cache,
1192 uint32_t type_idx, RegLocation rl_dest,
1193 RegLocation rl_src) {
1194 FlushAllRegs();
1195 // May generate a call - use explicit registers
1196 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001197 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001198 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001199 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001200 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1201 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 if (needs_access_check) {
1203 // Check we have access to type_idx and if not throw IllegalAccessError,
1204 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001205 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001206 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1207 type_idx, true);
1208 } else {
1209 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1210 type_idx, true);
1211 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001212 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1213 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001215 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001216 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001217 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001219 if (can_assume_type_is_in_dex_cache) {
1220 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001221 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001222 }
1223
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001225 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001226 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001227 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001228 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001230 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1231 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1232
1233 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001234 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001235
1236 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1237 public:
1238 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1239 RegLocation rl_src)
1240 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1241 rl_src_(rl_src) {
1242 }
1243
1244 void Compile() OVERRIDE {
1245 GenerateTargetLabel();
1246
1247 if (cu_->target64) {
1248 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1249 true);
1250 } else {
1251 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1252 true);
1253 }
1254 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1255 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
1256
1257 m2l_->OpUnconditionalBranch(cont_);
1258 }
1259
1260 private:
1261 uint32_t type_idx_;
1262 RegLocation rl_src_;
1263 };
1264
1265 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1266 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267 }
1268 }
1269 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001270 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001271 if (!IsSameReg(rl_result.reg, ref_reg)) {
1272 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001273 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001275 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276
1277 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001278 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001280 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1281 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1283 LIR* branchover = NULL;
1284 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001285 // rl_result == ref == class.
1286 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001287 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 } else {
1289 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001290 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001291 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1292 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001293 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 if (!type_known_abstract) {
1295 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001296 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001297 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001298 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001300 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001302 if (it != nullptr) {
1303 OpEndIT(it);
1304 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 FreeTemp(r_tgt);
1306 } else {
1307 if (!type_known_abstract) {
1308 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001309 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001310 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001312
Serguei Katkov9ee45192014-07-17 14:39:03 +07001313 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe90969af2014-07-15 23:02:11 -07001314 if (cu_->target64) {
1315 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial), false);
1316 } else {
1317 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial), false);
1318 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 }
1320 }
1321 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001322 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 /* branch targets here */
1324 LIR* target = NewLIR0(kPseudoTargetLabel);
1325 StoreValue(rl_dest, rl_result);
1326 branch1->target = target;
1327 if (branchover != NULL) {
1328 branchover->target = target;
1329 }
1330}
1331
1332void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1333 bool type_known_final, type_known_abstract, use_declaring_class;
1334 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1335 *cu_->dex_file,
1336 type_idx,
1337 &type_known_final,
1338 &type_known_abstract,
1339 &use_declaring_class);
1340 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1341 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1342
1343 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1344 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1345 } else {
1346 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1347 use_declaring_class, can_assume_type_is_in_dex_cache,
1348 type_idx, rl_dest, rl_src);
1349 }
1350}
1351
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001352void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 bool type_known_final, type_known_abstract, use_declaring_class;
1354 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1355 *cu_->dex_file,
1356 type_idx,
1357 &type_known_final,
1358 &type_known_abstract,
1359 &use_declaring_class);
1360 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1361 // of the exception throw path.
1362 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001363 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 // Verifier type analysis proved this check cast would never cause an exception.
1365 return;
1366 }
1367 FlushAllRegs();
1368 // May generate a call - use explicit registers
1369 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001370 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001371 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001372 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 if (needs_access_check) {
1374 // Check we have access to type_idx and if not throw IllegalAccessError,
1375 // returns Class* in kRet0
1376 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001377 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001378 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1379 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001380 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001381 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1382 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001383 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001384 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001386 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001387 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 } else {
1389 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001390 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001391 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001392 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001393 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1395 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001396 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1397 LIR* cont = NewLIR0(kPseudoTargetLabel);
1398
1399 // Slow path to initialize the type. Executed if the type is NULL.
1400 class SlowPath : public LIRSlowPath {
1401 public:
1402 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001403 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001404 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1405 class_reg_(class_reg) {
1406 }
1407
1408 void Compile() {
1409 GenerateTargetLabel();
1410
1411 // Call out to helper, which will return resolved type in kArg0
1412 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001413 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001414 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001415 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001416 } else {
1417 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001418 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001419 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001420 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001421 m2l_->OpUnconditionalBranch(cont_);
1422 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001423
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001424 public:
1425 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001426 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001427 };
1428
buzbee2700f7e2014-03-07 09:46:20 -08001429 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 }
1431 }
1432 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001433 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001434
1435 // Slow path for the case where the classes are not equal. In this case we need
1436 // to call a helper function to do the check.
1437 class SlowPath : public LIRSlowPath {
1438 public:
1439 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1440 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1441 }
1442
1443 void Compile() {
1444 GenerateTargetLabel();
1445
1446 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001447 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1448 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001449 }
buzbee33ae5582014-06-12 14:56:32 -07001450 if (m2l_->cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001451 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast),
1452 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1453 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001454 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001455 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast),
1456 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1457 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001458 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001459
1460 m2l_->OpUnconditionalBranch(cont_);
1461 }
1462
1463 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001464 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001465 };
1466
1467 if (type_known_abstract) {
1468 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001469 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001470 LIR* cont = NewLIR0(kPseudoTargetLabel);
1471 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1472 } else {
1473 // Harder, more common case. We need to generate a forward branch over the load
1474 // if the target is null. If it's non-null we perform the load and branch to the
1475 // slow path if the classes are not equal.
1476
1477 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001478 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001479 /* load object->klass_ */
1480 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001481 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1482 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001483
Andreas Gampeccc60262014-07-04 18:02:38 -07001484 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001485 LIR* cont = NewLIR0(kPseudoTargetLabel);
1486
1487 // Add the slow path that will not perform load since this is already done.
1488 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1489
1490 // Set the null check to branch to the continuation.
1491 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 }
1493}
1494
1495void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001496 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 RegLocation rl_result;
1498 if (cu_->instruction_set == kThumb2) {
1499 /*
1500 * NOTE: This is the one place in the code in which we might have
1501 * as many as six live temporary registers. There are 5 in the normal
1502 * set for Arm. Until we have spill capabilities, temporarily add
1503 * lr to the temp set. It is safe to do this locally, but note that
1504 * lr is used explicitly elsewhere in the code generator and cannot
1505 * normally be used as a general temp register.
1506 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001507 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1508 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 }
1510 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1511 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1512 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1513 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001514 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1515 RegStorage t_reg = AllocTemp();
1516 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1517 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1518 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 FreeTemp(t_reg);
1520 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001521 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1522 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 }
1524 /*
1525 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1526 * following StoreValueWide might need to allocate a temp register.
1527 * To further work around the lack of a spill capability, explicitly
1528 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1529 * Remove when spill is functional.
1530 */
1531 FreeRegLocTemps(rl_result, rl_src1);
1532 FreeRegLocTemps(rl_result, rl_src2);
1533 StoreValueWide(rl_dest, rl_result);
1534 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001535 Clobber(TargetReg(kLr, kNotWide));
1536 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 }
1538}
1539
1540
Andreas Gampe2f244e92014-05-08 03:35:25 -07001541template <size_t pointer_size>
1542static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1543 RegLocation rl_shift) {
1544 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545
1546 switch (opcode) {
1547 case Instruction::SHL_LONG:
1548 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001549 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 break;
1551 case Instruction::SHR_LONG:
1552 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001553 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 break;
1555 case Instruction::USHR_LONG:
1556 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001557 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 break;
1559 default:
1560 LOG(FATAL) << "Unexpected case";
1561 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001562 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1563 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1564}
1565
1566void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1567 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001568 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001569 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1570 } else {
1571 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1572 }
buzbeea0cd2d72014-06-01 09:33:49 -07001573 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 StoreValueWide(rl_dest, rl_result);
1575}
1576
1577
1578void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001579 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001580 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 OpKind op = kOpBkpt;
1582 bool is_div_rem = false;
1583 bool check_zero = false;
1584 bool unary = false;
1585 RegLocation rl_result;
1586 bool shift_op = false;
1587 switch (opcode) {
1588 case Instruction::NEG_INT:
1589 op = kOpNeg;
1590 unary = true;
1591 break;
1592 case Instruction::NOT_INT:
1593 op = kOpMvn;
1594 unary = true;
1595 break;
1596 case Instruction::ADD_INT:
1597 case Instruction::ADD_INT_2ADDR:
1598 op = kOpAdd;
1599 break;
1600 case Instruction::SUB_INT:
1601 case Instruction::SUB_INT_2ADDR:
1602 op = kOpSub;
1603 break;
1604 case Instruction::MUL_INT:
1605 case Instruction::MUL_INT_2ADDR:
1606 op = kOpMul;
1607 break;
1608 case Instruction::DIV_INT:
1609 case Instruction::DIV_INT_2ADDR:
1610 check_zero = true;
1611 op = kOpDiv;
1612 is_div_rem = true;
1613 break;
1614 /* NOTE: returns in kArg1 */
1615 case Instruction::REM_INT:
1616 case Instruction::REM_INT_2ADDR:
1617 check_zero = true;
1618 op = kOpRem;
1619 is_div_rem = true;
1620 break;
1621 case Instruction::AND_INT:
1622 case Instruction::AND_INT_2ADDR:
1623 op = kOpAnd;
1624 break;
1625 case Instruction::OR_INT:
1626 case Instruction::OR_INT_2ADDR:
1627 op = kOpOr;
1628 break;
1629 case Instruction::XOR_INT:
1630 case Instruction::XOR_INT_2ADDR:
1631 op = kOpXor;
1632 break;
1633 case Instruction::SHL_INT:
1634 case Instruction::SHL_INT_2ADDR:
1635 shift_op = true;
1636 op = kOpLsl;
1637 break;
1638 case Instruction::SHR_INT:
1639 case Instruction::SHR_INT_2ADDR:
1640 shift_op = true;
1641 op = kOpAsr;
1642 break;
1643 case Instruction::USHR_INT:
1644 case Instruction::USHR_INT_2ADDR:
1645 shift_op = true;
1646 op = kOpLsr;
1647 break;
1648 default:
1649 LOG(FATAL) << "Invalid word arith op: " << opcode;
1650 }
1651 if (!is_div_rem) {
1652 if (unary) {
1653 rl_src1 = LoadValue(rl_src1, kCoreReg);
1654 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001655 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001657 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001658 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001659 RegStorage t_reg = AllocTemp();
1660 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 rl_src1 = LoadValue(rl_src1, kCoreReg);
1662 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001663 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 FreeTemp(t_reg);
1665 } else {
1666 rl_src1 = LoadValue(rl_src1, kCoreReg);
1667 rl_src2 = LoadValue(rl_src2, kCoreReg);
1668 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001669 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 }
1671 }
1672 StoreValue(rl_dest, rl_result);
1673 } else {
Dave Allison70202782013-10-22 17:52:19 -07001674 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 +01001675 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 rl_src1 = LoadValue(rl_src1, kCoreReg);
1677 rl_src2 = LoadValue(rl_src2, kCoreReg);
1678 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001679 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 }
buzbee2700f7e2014-03-07 09:46:20 -08001681 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001682 done = true;
1683 } else if (cu_->instruction_set == kThumb2) {
1684 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1685 // Use ARM SDIV instruction for division. For remainder we also need to
1686 // calculate using a MUL and subtract.
1687 rl_src1 = LoadValue(rl_src1, kCoreReg);
1688 rl_src2 = LoadValue(rl_src2, kCoreReg);
1689 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001690 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001691 }
buzbee2700f7e2014-03-07 09:46:20 -08001692 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001693 done = true;
1694 }
1695 }
1696
1697 // If we haven't already generated the code use the callout function.
1698 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001699 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001700 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001701 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001702 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1703 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Andreas Gampeccc60262014-07-04 18:02:38 -07001704 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001706 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 }
Dave Allison70202782013-10-22 17:52:19 -07001708 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001709 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001710 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1711 } else {
1712 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1713 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001715 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 else
1717 rl_result = GetReturnAlt();
1718 }
1719 StoreValue(rl_dest, rl_result);
1720 }
1721}
1722
1723/*
1724 * The following are the first-level codegen routines that analyze the format
1725 * of each bytecode then either dispatch special purpose codegen routines
1726 * or produce corresponding Thumb instructions directly.
1727 */
1728
Brian Carlstrom7940e442013-07-12 13:46:57 -07001729// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001730static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 x &= x - 1;
1732 return (x & (x - 1)) == 0;
1733}
1734
Brian Carlstrom7940e442013-07-12 13:46:57 -07001735// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1736// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001737bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001738 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1740 return false;
1741 }
1742 // No divide instruction for Arm, so check for more special cases
1743 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001744 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001745 }
1746 int k = LowestSetBit(lit);
1747 if (k >= 30) {
1748 // Avoid special cases.
1749 return false;
1750 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 rl_src = LoadValue(rl_src, kCoreReg);
1752 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001753 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001754 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 if (lit == 2) {
1756 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001757 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1758 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1759 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001761 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001763 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1764 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 }
1766 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001767 RegStorage t_reg1 = AllocTemp();
1768 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001769 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001770 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1771 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001773 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001775 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001776 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001777 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001778 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001779 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001780 }
1781 }
1782 StoreValue(rl_dest, rl_result);
1783 return true;
1784}
1785
1786// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1787// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001788bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001789 if (lit < 0) {
1790 return false;
1791 }
1792 if (lit == 0) {
1793 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1794 LoadConstant(rl_result.reg, 0);
1795 StoreValue(rl_dest, rl_result);
1796 return true;
1797 }
1798 if (lit == 1) {
1799 rl_src = LoadValue(rl_src, kCoreReg);
1800 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1801 OpRegCopy(rl_result.reg, rl_src.reg);
1802 StoreValue(rl_dest, rl_result);
1803 return true;
1804 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001805 // There is RegRegRegShift on Arm, so check for more special cases
1806 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001807 return EasyMultiply(rl_src, rl_dest, lit);
1808 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001809 // Can we simplify this multiplication?
1810 bool power_of_two = false;
1811 bool pop_count_le2 = false;
1812 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001813 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 power_of_two = true;
1815 } else if (IsPopCountLE2(lit)) {
1816 pop_count_le2 = true;
1817 } else if (IsPowerOfTwo(lit + 1)) {
1818 power_of_two_minus_one = true;
1819 } else {
1820 return false;
1821 }
1822 rl_src = LoadValue(rl_src, kCoreReg);
1823 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1824 if (power_of_two) {
1825 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001826 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 } else if (pop_count_le2) {
1828 // Shift and add and shift.
1829 int first_bit = LowestSetBit(lit);
1830 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1831 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1832 } else {
1833 // Reverse subtract: (src << (shift + 1)) - src.
1834 DCHECK(power_of_two_minus_one);
1835 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001836 RegStorage t_reg = AllocTemp();
1837 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1838 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 }
1840 StoreValue(rl_dest, rl_result);
1841 return true;
1842}
1843
1844void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001845 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 RegLocation rl_result;
1847 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1848 int shift_op = false;
1849 bool is_div = false;
1850
1851 switch (opcode) {
1852 case Instruction::RSUB_INT_LIT8:
1853 case Instruction::RSUB_INT: {
1854 rl_src = LoadValue(rl_src, kCoreReg);
1855 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1856 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001857 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001859 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1860 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 }
1862 StoreValue(rl_dest, rl_result);
1863 return;
1864 }
1865
1866 case Instruction::SUB_INT:
1867 case Instruction::SUB_INT_2ADDR:
1868 lit = -lit;
1869 // Intended fallthrough
1870 case Instruction::ADD_INT:
1871 case Instruction::ADD_INT_2ADDR:
1872 case Instruction::ADD_INT_LIT8:
1873 case Instruction::ADD_INT_LIT16:
1874 op = kOpAdd;
1875 break;
1876 case Instruction::MUL_INT:
1877 case Instruction::MUL_INT_2ADDR:
1878 case Instruction::MUL_INT_LIT8:
1879 case Instruction::MUL_INT_LIT16: {
1880 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1881 return;
1882 }
1883 op = kOpMul;
1884 break;
1885 }
1886 case Instruction::AND_INT:
1887 case Instruction::AND_INT_2ADDR:
1888 case Instruction::AND_INT_LIT8:
1889 case Instruction::AND_INT_LIT16:
1890 op = kOpAnd;
1891 break;
1892 case Instruction::OR_INT:
1893 case Instruction::OR_INT_2ADDR:
1894 case Instruction::OR_INT_LIT8:
1895 case Instruction::OR_INT_LIT16:
1896 op = kOpOr;
1897 break;
1898 case Instruction::XOR_INT:
1899 case Instruction::XOR_INT_2ADDR:
1900 case Instruction::XOR_INT_LIT8:
1901 case Instruction::XOR_INT_LIT16:
1902 op = kOpXor;
1903 break;
1904 case Instruction::SHL_INT_LIT8:
1905 case Instruction::SHL_INT:
1906 case Instruction::SHL_INT_2ADDR:
1907 lit &= 31;
1908 shift_op = true;
1909 op = kOpLsl;
1910 break;
1911 case Instruction::SHR_INT_LIT8:
1912 case Instruction::SHR_INT:
1913 case Instruction::SHR_INT_2ADDR:
1914 lit &= 31;
1915 shift_op = true;
1916 op = kOpAsr;
1917 break;
1918 case Instruction::USHR_INT_LIT8:
1919 case Instruction::USHR_INT:
1920 case Instruction::USHR_INT_2ADDR:
1921 lit &= 31;
1922 shift_op = true;
1923 op = kOpLsr;
1924 break;
1925
1926 case Instruction::DIV_INT:
1927 case Instruction::DIV_INT_2ADDR:
1928 case Instruction::DIV_INT_LIT8:
1929 case Instruction::DIV_INT_LIT16:
1930 case Instruction::REM_INT:
1931 case Instruction::REM_INT_2ADDR:
1932 case Instruction::REM_INT_LIT8:
1933 case Instruction::REM_INT_LIT16: {
1934 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001935 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 return;
1937 }
buzbee11b63d12013-08-27 07:34:17 -07001938 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001940 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 (opcode == Instruction::DIV_INT_LIT16)) {
1942 is_div = true;
1943 } else {
1944 is_div = false;
1945 }
buzbee11b63d12013-08-27 07:34:17 -07001946 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1947 return;
1948 }
Dave Allison70202782013-10-22 17:52:19 -07001949
1950 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001951 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001953 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001954 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001955 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001956 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1957 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001958 } else if (cu_->instruction_set == kThumb2) {
1959 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1960 // Use ARM SDIV instruction for division. For remainder we also need to
1961 // calculate using a MUL and subtract.
1962 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001963 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001964 done = true;
1965 }
1966 }
1967
1968 if (!done) {
1969 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001970 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1971 Clobber(TargetReg(kArg0, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001972 if (cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001973 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, kNotWide),
1974 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001975 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001976 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, kNotWide),
1977 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001978 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001979 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001980 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 else
1982 rl_result = GetReturnAlt();
1983 }
1984 StoreValue(rl_dest, rl_result);
1985 return;
1986 }
1987 default:
1988 LOG(FATAL) << "Unexpected opcode " << opcode;
1989 }
1990 rl_src = LoadValue(rl_src, kCoreReg);
1991 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001992 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001994 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001995 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001996 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001997 }
1998 StoreValue(rl_dest, rl_result);
1999}
2000
Andreas Gampe2f244e92014-05-08 03:35:25 -07002001template <size_t pointer_size>
2002static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
2003 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 RegLocation rl_result;
2005 OpKind first_op = kOpBkpt;
2006 OpKind second_op = kOpBkpt;
2007 bool call_out = false;
2008 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002009 ThreadOffset<pointer_size> func_offset(-1);
Andreas Gampeccc60262014-07-04 18:02:38 -07002010 int ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011
2012 switch (opcode) {
2013 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07002014 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002015 mir_to_lir->GenNotLong(rl_dest, rl_src2);
2016 return;
2017 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002018 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
2019 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002021 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002022 RegStorage t_reg = mir_to_lir->AllocTemp();
2023 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2024 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2025 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2026 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002028 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2029 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002030 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002031 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 return;
2033 case Instruction::ADD_LONG:
2034 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002035 if (cu->instruction_set != kThumb2) {
2036 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 return;
2038 }
2039 first_op = kOpAdd;
2040 second_op = kOpAdc;
2041 break;
2042 case Instruction::SUB_LONG:
2043 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002044 if (cu->instruction_set != kThumb2) {
2045 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 return;
2047 }
2048 first_op = kOpSub;
2049 second_op = kOpSbc;
2050 break;
2051 case Instruction::MUL_LONG:
2052 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002053 if (cu->instruction_set != kMips) {
2054 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002055 return;
2056 } else {
2057 call_out = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002058 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002059 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 }
2061 break;
2062 case Instruction::DIV_LONG:
2063 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002064 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002065 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2066 return;
2067 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002068 call_out = true;
2069 check_zero = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002070 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 break;
2073 case Instruction::REM_LONG:
2074 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002075 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002076 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2077 return;
2078 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 call_out = true;
2080 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002081 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002082 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampeccc60262014-07-04 18:02:38 -07002083 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, kNotWide).GetReg() :
2084 mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002085 break;
2086 case Instruction::AND_LONG_2ADDR:
2087 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002088 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2089 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002090 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 }
2092 first_op = kOpAnd;
2093 second_op = kOpAnd;
2094 break;
2095 case Instruction::OR_LONG:
2096 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002097 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2098 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 return;
2101 }
2102 first_op = kOpOr;
2103 second_op = kOpOr;
2104 break;
2105 case Instruction::XOR_LONG:
2106 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002107 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2108 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002109 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002110 return;
2111 }
2112 first_op = kOpXor;
2113 second_op = kOpXor;
2114 break;
2115 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002116 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002117 return;
2118 }
2119 default:
2120 LOG(FATAL) << "Invalid long arith op";
2121 }
2122 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002123 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002124 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002125 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002126 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002127 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kWide);
2128 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kWide);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002129 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2130 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Andreas Gampeccc60262014-07-04 18:02:38 -07002131 mir_to_lir->GenDivZeroCheckWide(r_tmp2);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002132 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002133 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002134 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002135 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002136 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002137 }
2138 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampeccc60262014-07-04 18:02:38 -07002139 if (ret_reg == mir_to_lir->TargetReg(kRet0, kNotWide).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002140 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002141 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002142 rl_result = mir_to_lir->GetReturnWideAlt();
2143 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002144 }
2145}
2146
Andreas Gampe2f244e92014-05-08 03:35:25 -07002147void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2148 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002149 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002150 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2151 } else {
2152 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2153 }
2154}
2155
Mark Mendelle87f9b52014-04-30 14:13:18 -04002156void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2157 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2158 LoadConstantNoClobber(rl_result.reg, value);
2159 StoreValue(rl_dest, rl_result);
2160 if (value == 0) {
2161 Workaround7250540(rl_dest, rl_result.reg);
2162 }
2163}
2164
Andreas Gampe2f244e92014-05-08 03:35:25 -07002165template <size_t pointer_size>
2166void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002167 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002168 /*
2169 * Don't optimize the register usage since it calls out to support
2170 * functions
2171 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002172 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2173
Brian Carlstrom7940e442013-07-12 13:46:57 -07002174 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002175 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2176 if (rl_dest.wide) {
2177 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002178 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002179 StoreValueWide(rl_dest, rl_result);
2180 } else {
2181 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002182 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002183 StoreValue(rl_dest, rl_result);
2184 }
2185}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002186template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2187 RegLocation rl_dest, RegLocation rl_src);
2188template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2189 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002190
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002191class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2192 public:
2193 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2194 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2195 }
2196
2197 void Compile() OVERRIDE {
2198 m2l_->ResetRegPool();
2199 m2l_->ResetDefTracking();
2200 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002201 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002202 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2203 } else {
2204 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2205 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002206 if (cont_ != nullptr) {
2207 m2l_->OpUnconditionalBranch(cont_);
2208 }
2209 }
2210};
2211
Brian Carlstrom7940e442013-07-12 13:46:57 -07002212/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002213void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002214 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002215 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2216 return;
2217 }
2218 FlushAllRegs();
2219 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002220 LIR* cont = NewLIR0(kPseudoTargetLabel);
2221 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002222 } else {
2223 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2224 return;
2225 }
2226 FlushAllRegs(); // TODO: needed?
2227 LIR* inst = CheckSuspendUsingLoad();
2228 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002229 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002230}
2231
2232/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002233void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002234 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002235 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2236 OpUnconditionalBranch(target);
2237 return;
2238 }
2239 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002240 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002241 LIR* branch = OpUnconditionalBranch(nullptr);
2242 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002243 } else {
2244 // For the implicit suspend check, just perform the trigger
2245 // load and branch to the target.
2246 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2247 OpUnconditionalBranch(target);
2248 return;
2249 }
2250 FlushAllRegs();
2251 LIR* inst = CheckSuspendUsingLoad();
2252 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002253 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002254 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002255}
2256
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002257/* Call out to helper assembly routine that will null check obj and then lock it. */
2258void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2259 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002260 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002261 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2262 } else {
2263 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2264 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002265}
2266
2267/* Call out to helper assembly routine that will null check obj and then unlock it. */
2268void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2269 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002270 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002271 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2272 } else {
2273 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2274 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002275}
2276
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002277/* Generic code for generating a wide constant into a VR. */
2278void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2279 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002280 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002281 StoreValueWide(rl_dest, rl_result);
2282}
2283
Brian Carlstrom7940e442013-07-12 13:46:57 -07002284} // namespace art