blob: cf80ee71a4ee23cb1469347c1ec0b59bc3056624 [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);
585 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
586 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000587 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800588 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000589 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100591 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700592 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000593 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
594 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800595 if (IsTemp(rl_method.reg)) {
596 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 }
598 } else {
599 // Medium path, static storage base in a different class which requires checks that the other
600 // class is initialized.
601 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000602 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 // May do runtime call so everything to home locations.
604 FlushAllRegs();
605 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700606 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 LockTemp(r_method);
608 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700609 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800610 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000611 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
612 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000613 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000614 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800615 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000616 if (!field_info.IsInitialized() &&
617 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800619
620 // The slow path is invoked if the r_base is NULL or the class pointed
621 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700623 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800624 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800625 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800626 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000627 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800628 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800629
buzbee2700f7e2014-03-07 09:46:20 -0800630 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000631 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800632
633 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700634 // Ensure load of status and store of value don't re-order.
635 // TODO: Presumably the actual value store is control-dependent on the status load,
636 // and will thus not be reordered in any case, since stores are never speculated.
637 // Does later code "know" that the class is now initialized? If so, we still
638 // need the barrier to guard later static loads.
639 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 FreeTemp(r_method);
642 }
643 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100644 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100646 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100648 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000650 if (is_object) {
651 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
652 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100653 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000654 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
655 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 }
657 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800658 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800660 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 } else {
662 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700663 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700664 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
665 } else {
666 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
667 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 }
669}
670
Andreas Gampe2f244e92014-05-08 03:35:25 -0700671template <size_t pointer_size>
672static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
673 const MirSFieldLoweringInfo* field_info) {
674 ThreadOffset<pointer_size> getter_offset =
675 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
676 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
677 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
678 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
679}
680
Vladimir Markobe0e5462014-02-26 11:24:15 +0000681void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700682 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000683 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
684 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100685 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
686 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
687 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000688 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800689 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000690 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 // Fast path, static storage base is this method's class
692 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700693 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000694 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
695 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 } else {
697 // Medium path, static storage base in a different class which requires checks that the other
698 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000699 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 // May do runtime call so everything to home locations.
701 FlushAllRegs();
702 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700703 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 LockTemp(r_method);
705 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700706 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800707 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000708 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
709 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000710 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000711 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800712 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000713 if (!field_info.IsInitialized() &&
714 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800715 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800716
717 // The slow path is invoked if the r_base is NULL or the class pointed
718 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800719 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700720 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800721 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800722 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800723 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000724 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800725 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800726
buzbee2700f7e2014-03-07 09:46:20 -0800727 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000728 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800729
730 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700731 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700732 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 FreeTemp(r_method);
735 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800736 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100737 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
738 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800739
Vladimir Marko674744e2014-04-24 15:18:26 +0100740 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000741 if (is_object) {
742 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
743 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100744 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000745 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
746 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800747 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100748 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800749
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 if (is_long_or_double) {
751 StoreValueWide(rl_dest, rl_result);
752 } else {
753 StoreValue(rl_dest, rl_result);
754 }
755 } else {
756 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700757 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700758 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
759 } else {
760 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
761 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700762 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700764 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 StoreValueWide(rl_dest, rl_result);
766 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700767 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 StoreValue(rl_dest, rl_result);
769 }
770 }
771}
772
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800773// Generate code for all slow paths.
774void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700775 // We should check slow_paths_.Size() every time, because a new slow path
776 // may be created during slowpath->Compile().
777 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800778 LIRSlowPath* slowpath = slow_paths_.Get(i);
779 slowpath->Compile();
780 }
781 slow_paths_.Reset();
782}
783
Andreas Gampe2f244e92014-05-08 03:35:25 -0700784template <size_t pointer_size>
785static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
786 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
787 ThreadOffset<pointer_size> getter_offset =
788 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
789 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
790 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
791 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);
801 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
802 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
803 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000804 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700805 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100806 GenNullCheck(rl_obj.reg, opt_flags);
807 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
808 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000809 LIR* load_lir;
810 if (is_object) {
811 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
812 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100813 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000814 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
815 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100816 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000817 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 StoreValueWide(rl_dest, rl_result);
820 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 StoreValue(rl_dest, rl_result);
822 }
823 } else {
buzbee33ae5582014-06-12 14:56:32 -0700824 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700825 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
826 } else {
827 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
828 }
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700829 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700831 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValueWide(rl_dest, rl_result);
833 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700834 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 StoreValue(rl_dest, rl_result);
836 }
837 }
838}
839
Andreas Gampe2f244e92014-05-08 03:35:25 -0700840template <size_t pointer_size>
841static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
842 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
843 RegLocation rl_src) {
844 ThreadOffset<pointer_size> setter_offset =
845 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
846 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
847 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
848 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
849 rl_obj, rl_src, true);
850}
851
Vladimir Markobe0e5462014-02-26 11:24:15 +0000852void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700854 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000855 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
856 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100857 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
858 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
859 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
860 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000861 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700862 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100864 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 } else {
866 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100867 }
868 GenNullCheck(rl_obj.reg, opt_flags);
869 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000870 LIR* store;
871 if (is_object) {
872 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
873 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100874 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000875 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
876 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100877 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000878 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100879 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
880 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 }
882 } else {
buzbee33ae5582014-06-12 14:56:32 -0700883 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700884 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
885 } else {
886 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
887 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 }
889}
890
Andreas Gampe2f244e92014-05-08 03:35:25 -0700891template <size_t pointer_size>
892static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
893 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
894 ThreadOffset<pointer_size> helper = needs_range_check
895 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
896 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
897 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
898 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
899 true);
900}
901
Ian Rogersa9a82542013-10-04 11:17:26 -0700902void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
903 RegLocation rl_src) {
904 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
905 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
906 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700907 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700908 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
909 } else {
910 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
911 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700912}
913
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700914void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700916 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700917 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700918 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700920 *cu_->dex_file,
921 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 // Call out to helper which resolves type and verifies access.
923 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700924 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700925 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
926 type_idx, rl_method.reg, true);
927 } else {
928 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
929 type_idx, rl_method.reg, true);
930 }
buzbeea0cd2d72014-06-01 09:33:49 -0700931 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 StoreValue(rl_dest, rl_result);
933 } else {
934 // We're don't need access checks, load type from dex cache
935 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700936 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000937 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000938 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000939 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
941 type_idx) || SLOW_TYPE_PATH) {
942 // Slow path, at runtime test if type is null and if so initialize
943 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800944 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800945 LIR* cont = NewLIR0(kPseudoTargetLabel);
946
947 // Object to generate the slow path for class resolution.
948 class SlowPath : public LIRSlowPath {
949 public:
950 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
951 const RegLocation& rl_method, const RegLocation& rl_result) :
952 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
953 rl_method_(rl_method), rl_result_(rl_result) {
954 }
955
956 void Compile() {
957 GenerateTargetLabel();
958
buzbee33ae5582014-06-12 14:56:32 -0700959 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700960 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
961 rl_method_.reg, true);
962 } else {
963 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
964 rl_method_.reg, true);
965 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700966 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800967
968 m2l_->OpUnconditionalBranch(cont_);
969 }
970
971 private:
972 const int type_idx_;
973 const RegLocation rl_method_;
974 const RegLocation rl_result_;
975 };
976
977 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800978 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800979
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800981 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 // Fast path, we're done - just store result
983 StoreValue(rl_dest, rl_result);
984 }
985 }
986}
987
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700988void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000990 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
991 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
993 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
994 // slow path, resolve string if not in dex cache
995 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700996 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800997
998 // If the Method* is already in a register, we can save a copy.
999 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001000 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001001 if (rl_method.location == kLocPhysReg) {
1002 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001003 DCHECK(!IsTemp(rl_method.reg));
1004 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001005 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001006 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001007 LoadCurrMethodDirect(r_method);
1008 }
buzbee695d13a2014-04-19 13:32:20 -07001009 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001010 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001011
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001013 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1014 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001015 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001016
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001017 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001018 // Object to generate the slow path for string resolution.
1019 class SlowPath : public LIRSlowPath {
1020 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001021 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1022 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1023 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001024 }
1025
1026 void Compile() {
1027 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001028 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001029 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1030 r_method_, string_idx_, true);
1031 } else {
1032 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1033 r_method_, string_idx_, true);
1034 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001035 m2l_->OpUnconditionalBranch(cont_);
1036 }
1037
1038 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001039 const RegStorage r_method_;
1040 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001041 };
1042
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001043 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001045
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001047 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 } else {
1049 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001050 RegStorage res_reg = AllocTempRef();
1051 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001052 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1053 kNotVolatile);
1054 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 StoreValue(rl_dest, rl_result);
1056 }
1057}
1058
Andreas Gampe2f244e92014-05-08 03:35:25 -07001059template <size_t pointer_size>
1060static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1061 RegLocation rl_dest) {
1062 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 // alloc will always check for resolution, do we also need to verify
1064 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001065 ThreadOffset<pointer_size> func_offset(-1);
1066 const DexFile* dex_file = cu->dex_file;
1067 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001068 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001069 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 bool is_type_initialized;
1071 bool use_direct_type_ptr;
1072 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001073 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001074 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001075 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1076 &direct_type_ptr, &is_finalizable) &&
1077 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001078 // The fast path.
1079 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001080 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001081 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001082 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -07001083 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1084 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001085 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001086 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
Andreas Gampeccc60262014-07-04 18:02:38 -07001087 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1088 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001089 }
1090 } else {
1091 // Use the direct pointer.
1092 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001093 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1094 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001095 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001096 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1097 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001098 }
1099 }
1100 } else {
1101 // The slow path.
1102 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001103 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1104 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001105 }
1106 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001108 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1109 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 }
buzbeea0cd2d72014-06-01 09:33:49 -07001111 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001112 mir_to_lir->StoreValue(rl_dest, rl_result);
1113}
1114
1115/*
1116 * Let helper function take care of everything. Will
1117 * call Class::NewInstanceFromCode(type_idx, method);
1118 */
1119void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001120 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001121 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1122 } else {
1123 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1124 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125}
1126
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001127void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001129 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001130 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1131 } else {
1132 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1133 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134}
1135
1136// For final classes there are no sub-classes to check and so we can answer the instance-of
1137// question with simple comparisons.
1138void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1139 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001140 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001141 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001142
buzbeea0cd2d72014-06-01 09:33:49 -07001143 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001145 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001146 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001148 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
1150 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001151 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152
buzbeea0cd2d72014-06-01 09:33:49 -07001153 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1154 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155
1156 LoadCurrMethodDirect(check_class);
1157 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001158 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1159 kNotVolatile);
1160 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1161 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 } else {
buzbee695d13a2014-04-19 13:32:20 -07001163 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001164 check_class, kNotVolatile);
1165 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1166 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001167 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001168 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
1170
1171 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001172 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 if (cu_->instruction_set == kThumb2) {
1174 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001175 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001177 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 } else {
1179 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1180 LoadConstant(result_reg, 1); // eq case - load true
1181 }
1182 LIR* target = NewLIR0(kPseudoTargetLabel);
1183 null_branchover->target = target;
1184 if (ne_branchover != NULL) {
1185 ne_branchover->target = target;
1186 }
1187 FreeTemp(object_class);
1188 FreeTemp(check_class);
1189 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001190 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 FreeTemp(result_reg);
1192 }
1193 StoreValue(rl_dest, rl_result);
1194}
1195
1196void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1197 bool type_known_abstract, bool use_declaring_class,
1198 bool can_assume_type_is_in_dex_cache,
1199 uint32_t type_idx, RegLocation rl_dest,
1200 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001201 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001202 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001203
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 FlushAllRegs();
1205 // May generate a call - use explicit registers
1206 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001207 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001208 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001209 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 if (needs_access_check) {
1211 // Check we have access to type_idx and if not throw IllegalAccessError,
1212 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001213 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001214 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1215 type_idx, true);
1216 } else {
1217 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1218 type_idx, true);
1219 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001220 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
1221 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 } else if (use_declaring_class) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001223 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001224 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001225 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 } else {
1227 // Load dex cache entry into class_reg (kArg2)
Andreas Gampeccc60262014-07-04 18:02:38 -07001228 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001229 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001230 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001231 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001232 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 if (!can_assume_type_is_in_dex_cache) {
1234 // Need to test presence of type in dex cache at runtime
1235 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1236 // Not resolved
1237 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001238 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001239 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1240 } else {
1241 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1242 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001243 OpRegCopy(TargetReg(kArg2, kRef), TargetReg(kRet0, kRef)); // Align usage with fast path
1244 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); /* reload Ref */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 // Rejoin code paths
1246 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1247 hop_branch->target = hop_target;
1248 }
1249 }
1250 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001251 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001252 if (cu_->instruction_set == kMips) {
1253 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001254 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001256 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257
1258 /* load object->klass_ */
1259 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001260 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1261 TargetReg(kArg1, kRef), kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1263 LIR* branchover = NULL;
1264 if (type_known_final) {
1265 // rl_result == ref == null == 0.
1266 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001267 OpRegReg(kOpCmp, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001268 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001269 LoadConstant(rl_result.reg, 1); // .eq case - load true
1270 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001271 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001273 LoadConstant(rl_result.reg, 0); // ne case - load false
Andreas Gampeccc60262014-07-04 18:02:38 -07001274 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001275 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 }
1277 } else {
1278 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001279 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001280 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1281 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001282 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 if (!type_known_abstract) {
1284 /* Uses conditional nullification */
Andreas Gampeccc60262014-07-04 18:02:38 -07001285 OpRegReg(kOpCmp, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001286 it = OpIT(kCondEq, "EE"); // if-convert the test
Andreas Gampeccc60262014-07-04 18:02:38 -07001287 LoadConstant(TargetReg(kArg0, kNotWide), 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001289 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001291 if (it != nullptr) {
1292 OpEndIT(it);
1293 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 FreeTemp(r_tgt);
1295 } else {
1296 if (!type_known_abstract) {
1297 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001298 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001299 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
buzbee33ae5582014-06-12 14:56:32 -07001301 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001302 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1303 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Andreas Gampeccc60262014-07-04 18:02:38 -07001304 OpRegCopy(TargetReg(kArg0, kRef), TargetReg(kArg2, kRef)); // .ne case - arg0 <= class
Mark Mendell6607d972014-02-10 06:54:18 -08001305 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1306 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 }
1308 }
1309 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001310 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 /* branch targets here */
1312 LIR* target = NewLIR0(kPseudoTargetLabel);
1313 StoreValue(rl_dest, rl_result);
1314 branch1->target = target;
1315 if (branchover != NULL) {
1316 branchover->target = target;
1317 }
1318}
1319
1320void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1321 bool type_known_final, type_known_abstract, use_declaring_class;
1322 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1323 *cu_->dex_file,
1324 type_idx,
1325 &type_known_final,
1326 &type_known_abstract,
1327 &use_declaring_class);
1328 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1329 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1330
1331 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1332 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1333 } else {
1334 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1335 use_declaring_class, can_assume_type_is_in_dex_cache,
1336 type_idx, rl_dest, rl_src);
1337 }
1338}
1339
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001340void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 bool type_known_final, type_known_abstract, use_declaring_class;
1342 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1343 *cu_->dex_file,
1344 type_idx,
1345 &type_known_final,
1346 &type_known_abstract,
1347 &use_declaring_class);
1348 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1349 // of the exception throw path.
1350 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001351 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 // Verifier type analysis proved this check cast would never cause an exception.
1353 return;
1354 }
1355 FlushAllRegs();
1356 // May generate a call - use explicit registers
1357 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001358 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001359 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001360 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 if (needs_access_check) {
1362 // Check we have access to type_idx and if not throw IllegalAccessError,
1363 // returns Class* in kRet0
1364 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001365 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001366 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1367 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001368 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001369 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1370 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001371 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001372 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001374 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001375 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 } else {
1377 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001378 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001379 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001380 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001381 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1383 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001384 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1385 LIR* cont = NewLIR0(kPseudoTargetLabel);
1386
1387 // Slow path to initialize the type. Executed if the type is NULL.
1388 class SlowPath : public LIRSlowPath {
1389 public:
1390 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001391 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001392 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1393 class_reg_(class_reg) {
1394 }
1395
1396 void Compile() {
1397 GenerateTargetLabel();
1398
1399 // Call out to helper, which will return resolved type in kArg0
1400 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001401 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001402 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001403 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001404 } else {
1405 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001406 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001407 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001408 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001409 m2l_->OpUnconditionalBranch(cont_);
1410 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001411
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001412 public:
1413 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001414 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415 };
1416
buzbee2700f7e2014-03-07 09:46:20 -08001417 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418 }
1419 }
1420 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001421 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001422
1423 // Slow path for the case where the classes are not equal. In this case we need
1424 // to call a helper function to do the check.
1425 class SlowPath : public LIRSlowPath {
1426 public:
1427 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1428 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1429 }
1430
1431 void Compile() {
1432 GenerateTargetLabel();
1433
1434 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001435 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1436 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001437 }
buzbee33ae5582014-06-12 14:56:32 -07001438 if (m2l_->cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001439 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast),
1440 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1441 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001442 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001443 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast),
1444 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1445 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001446 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001447
1448 m2l_->OpUnconditionalBranch(cont_);
1449 }
1450
1451 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001452 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001453 };
1454
1455 if (type_known_abstract) {
1456 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001457 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001458 LIR* cont = NewLIR0(kPseudoTargetLabel);
1459 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1460 } else {
1461 // Harder, more common case. We need to generate a forward branch over the load
1462 // if the target is null. If it's non-null we perform the load and branch to the
1463 // slow path if the classes are not equal.
1464
1465 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001466 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001467 /* load object->klass_ */
1468 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001469 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1470 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001471
Andreas Gampeccc60262014-07-04 18:02:38 -07001472 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001473 LIR* cont = NewLIR0(kPseudoTargetLabel);
1474
1475 // Add the slow path that will not perform load since this is already done.
1476 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1477
1478 // Set the null check to branch to the continuation.
1479 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 }
1481}
1482
1483void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001484 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 RegLocation rl_result;
1486 if (cu_->instruction_set == kThumb2) {
1487 /*
1488 * NOTE: This is the one place in the code in which we might have
1489 * as many as six live temporary registers. There are 5 in the normal
1490 * set for Arm. Until we have spill capabilities, temporarily add
1491 * lr to the temp set. It is safe to do this locally, but note that
1492 * lr is used explicitly elsewhere in the code generator and cannot
1493 * normally be used as a general temp register.
1494 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001495 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1496 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 }
1498 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1499 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1500 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1501 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001502 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1503 RegStorage t_reg = AllocTemp();
1504 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1505 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1506 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 FreeTemp(t_reg);
1508 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001509 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1510 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 }
1512 /*
1513 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1514 * following StoreValueWide might need to allocate a temp register.
1515 * To further work around the lack of a spill capability, explicitly
1516 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1517 * Remove when spill is functional.
1518 */
1519 FreeRegLocTemps(rl_result, rl_src1);
1520 FreeRegLocTemps(rl_result, rl_src2);
1521 StoreValueWide(rl_dest, rl_result);
1522 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001523 Clobber(TargetReg(kLr, kNotWide));
1524 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 }
1526}
1527
1528
Andreas Gampe2f244e92014-05-08 03:35:25 -07001529template <size_t pointer_size>
1530static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1531 RegLocation rl_shift) {
1532 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533
1534 switch (opcode) {
1535 case Instruction::SHL_LONG:
1536 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001537 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 break;
1539 case Instruction::SHR_LONG:
1540 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001541 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 break;
1543 case Instruction::USHR_LONG:
1544 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001545 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 break;
1547 default:
1548 LOG(FATAL) << "Unexpected case";
1549 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001550 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1551 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1552}
1553
1554void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1555 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001556 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001557 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1558 } else {
1559 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1560 }
buzbeea0cd2d72014-06-01 09:33:49 -07001561 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 StoreValueWide(rl_dest, rl_result);
1563}
1564
1565
1566void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001567 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001568 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 OpKind op = kOpBkpt;
1570 bool is_div_rem = false;
1571 bool check_zero = false;
1572 bool unary = false;
1573 RegLocation rl_result;
1574 bool shift_op = false;
1575 switch (opcode) {
1576 case Instruction::NEG_INT:
1577 op = kOpNeg;
1578 unary = true;
1579 break;
1580 case Instruction::NOT_INT:
1581 op = kOpMvn;
1582 unary = true;
1583 break;
1584 case Instruction::ADD_INT:
1585 case Instruction::ADD_INT_2ADDR:
1586 op = kOpAdd;
1587 break;
1588 case Instruction::SUB_INT:
1589 case Instruction::SUB_INT_2ADDR:
1590 op = kOpSub;
1591 break;
1592 case Instruction::MUL_INT:
1593 case Instruction::MUL_INT_2ADDR:
1594 op = kOpMul;
1595 break;
1596 case Instruction::DIV_INT:
1597 case Instruction::DIV_INT_2ADDR:
1598 check_zero = true;
1599 op = kOpDiv;
1600 is_div_rem = true;
1601 break;
1602 /* NOTE: returns in kArg1 */
1603 case Instruction::REM_INT:
1604 case Instruction::REM_INT_2ADDR:
1605 check_zero = true;
1606 op = kOpRem;
1607 is_div_rem = true;
1608 break;
1609 case Instruction::AND_INT:
1610 case Instruction::AND_INT_2ADDR:
1611 op = kOpAnd;
1612 break;
1613 case Instruction::OR_INT:
1614 case Instruction::OR_INT_2ADDR:
1615 op = kOpOr;
1616 break;
1617 case Instruction::XOR_INT:
1618 case Instruction::XOR_INT_2ADDR:
1619 op = kOpXor;
1620 break;
1621 case Instruction::SHL_INT:
1622 case Instruction::SHL_INT_2ADDR:
1623 shift_op = true;
1624 op = kOpLsl;
1625 break;
1626 case Instruction::SHR_INT:
1627 case Instruction::SHR_INT_2ADDR:
1628 shift_op = true;
1629 op = kOpAsr;
1630 break;
1631 case Instruction::USHR_INT:
1632 case Instruction::USHR_INT_2ADDR:
1633 shift_op = true;
1634 op = kOpLsr;
1635 break;
1636 default:
1637 LOG(FATAL) << "Invalid word arith op: " << opcode;
1638 }
1639 if (!is_div_rem) {
1640 if (unary) {
1641 rl_src1 = LoadValue(rl_src1, kCoreReg);
1642 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001643 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001645 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001646 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001647 RegStorage t_reg = AllocTemp();
1648 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 rl_src1 = LoadValue(rl_src1, kCoreReg);
1650 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001651 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001652 FreeTemp(t_reg);
1653 } else {
1654 rl_src1 = LoadValue(rl_src1, kCoreReg);
1655 rl_src2 = LoadValue(rl_src2, kCoreReg);
1656 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001657 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 }
1659 }
1660 StoreValue(rl_dest, rl_result);
1661 } else {
Dave Allison70202782013-10-22 17:52:19 -07001662 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 +01001663 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 rl_src1 = LoadValue(rl_src1, kCoreReg);
1665 rl_src2 = LoadValue(rl_src2, kCoreReg);
1666 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001667 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 }
buzbee2700f7e2014-03-07 09:46:20 -08001669 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001670 done = true;
1671 } else if (cu_->instruction_set == kThumb2) {
1672 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1673 // Use ARM SDIV instruction for division. For remainder we also need to
1674 // calculate using a MUL and subtract.
1675 rl_src1 = LoadValue(rl_src1, kCoreReg);
1676 rl_src2 = LoadValue(rl_src2, kCoreReg);
1677 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001678 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001679 }
buzbee2700f7e2014-03-07 09:46:20 -08001680 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001681 done = true;
1682 }
1683 }
1684
1685 // If we haven't already generated the code use the callout function.
1686 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001688 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001689 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001690 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1691 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Andreas Gampeccc60262014-07-04 18:02:38 -07001692 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001694 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 }
Dave Allison70202782013-10-22 17:52:19 -07001696 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001697 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001698 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1699 } else {
1700 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1701 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001702 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001703 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001704 else
1705 rl_result = GetReturnAlt();
1706 }
1707 StoreValue(rl_dest, rl_result);
1708 }
1709}
1710
1711/*
1712 * The following are the first-level codegen routines that analyze the format
1713 * of each bytecode then either dispatch special purpose codegen routines
1714 * or produce corresponding Thumb instructions directly.
1715 */
1716
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001718static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 x &= x - 1;
1720 return (x & (x - 1)) == 0;
1721}
1722
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1724// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001725bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001726 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001727 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1728 return false;
1729 }
1730 // No divide instruction for Arm, so check for more special cases
1731 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001732 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 }
1734 int k = LowestSetBit(lit);
1735 if (k >= 30) {
1736 // Avoid special cases.
1737 return false;
1738 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 rl_src = LoadValue(rl_src, kCoreReg);
1740 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001741 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001742 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 if (lit == 2) {
1744 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001745 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1746 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1747 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001749 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001751 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1752 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 }
1754 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001755 RegStorage t_reg1 = AllocTemp();
1756 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001758 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1759 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001760 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001761 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001763 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001765 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001766 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001767 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 }
1769 }
1770 StoreValue(rl_dest, rl_result);
1771 return true;
1772}
1773
1774// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1775// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001776bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001777 if (lit < 0) {
1778 return false;
1779 }
1780 if (lit == 0) {
1781 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1782 LoadConstant(rl_result.reg, 0);
1783 StoreValue(rl_dest, rl_result);
1784 return true;
1785 }
1786 if (lit == 1) {
1787 rl_src = LoadValue(rl_src, kCoreReg);
1788 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1789 OpRegCopy(rl_result.reg, rl_src.reg);
1790 StoreValue(rl_dest, rl_result);
1791 return true;
1792 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001793 // There is RegRegRegShift on Arm, so check for more special cases
1794 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001795 return EasyMultiply(rl_src, rl_dest, lit);
1796 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001797 // Can we simplify this multiplication?
1798 bool power_of_two = false;
1799 bool pop_count_le2 = false;
1800 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001801 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 power_of_two = true;
1803 } else if (IsPopCountLE2(lit)) {
1804 pop_count_le2 = true;
1805 } else if (IsPowerOfTwo(lit + 1)) {
1806 power_of_two_minus_one = true;
1807 } else {
1808 return false;
1809 }
1810 rl_src = LoadValue(rl_src, kCoreReg);
1811 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1812 if (power_of_two) {
1813 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001814 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 } else if (pop_count_le2) {
1816 // Shift and add and shift.
1817 int first_bit = LowestSetBit(lit);
1818 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1819 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1820 } else {
1821 // Reverse subtract: (src << (shift + 1)) - src.
1822 DCHECK(power_of_two_minus_one);
1823 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001824 RegStorage t_reg = AllocTemp();
1825 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1826 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 }
1828 StoreValue(rl_dest, rl_result);
1829 return true;
1830}
1831
1832void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001833 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 RegLocation rl_result;
1835 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1836 int shift_op = false;
1837 bool is_div = false;
1838
1839 switch (opcode) {
1840 case Instruction::RSUB_INT_LIT8:
1841 case Instruction::RSUB_INT: {
1842 rl_src = LoadValue(rl_src, kCoreReg);
1843 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1844 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001845 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001847 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1848 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 }
1850 StoreValue(rl_dest, rl_result);
1851 return;
1852 }
1853
1854 case Instruction::SUB_INT:
1855 case Instruction::SUB_INT_2ADDR:
1856 lit = -lit;
1857 // Intended fallthrough
1858 case Instruction::ADD_INT:
1859 case Instruction::ADD_INT_2ADDR:
1860 case Instruction::ADD_INT_LIT8:
1861 case Instruction::ADD_INT_LIT16:
1862 op = kOpAdd;
1863 break;
1864 case Instruction::MUL_INT:
1865 case Instruction::MUL_INT_2ADDR:
1866 case Instruction::MUL_INT_LIT8:
1867 case Instruction::MUL_INT_LIT16: {
1868 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1869 return;
1870 }
1871 op = kOpMul;
1872 break;
1873 }
1874 case Instruction::AND_INT:
1875 case Instruction::AND_INT_2ADDR:
1876 case Instruction::AND_INT_LIT8:
1877 case Instruction::AND_INT_LIT16:
1878 op = kOpAnd;
1879 break;
1880 case Instruction::OR_INT:
1881 case Instruction::OR_INT_2ADDR:
1882 case Instruction::OR_INT_LIT8:
1883 case Instruction::OR_INT_LIT16:
1884 op = kOpOr;
1885 break;
1886 case Instruction::XOR_INT:
1887 case Instruction::XOR_INT_2ADDR:
1888 case Instruction::XOR_INT_LIT8:
1889 case Instruction::XOR_INT_LIT16:
1890 op = kOpXor;
1891 break;
1892 case Instruction::SHL_INT_LIT8:
1893 case Instruction::SHL_INT:
1894 case Instruction::SHL_INT_2ADDR:
1895 lit &= 31;
1896 shift_op = true;
1897 op = kOpLsl;
1898 break;
1899 case Instruction::SHR_INT_LIT8:
1900 case Instruction::SHR_INT:
1901 case Instruction::SHR_INT_2ADDR:
1902 lit &= 31;
1903 shift_op = true;
1904 op = kOpAsr;
1905 break;
1906 case Instruction::USHR_INT_LIT8:
1907 case Instruction::USHR_INT:
1908 case Instruction::USHR_INT_2ADDR:
1909 lit &= 31;
1910 shift_op = true;
1911 op = kOpLsr;
1912 break;
1913
1914 case Instruction::DIV_INT:
1915 case Instruction::DIV_INT_2ADDR:
1916 case Instruction::DIV_INT_LIT8:
1917 case Instruction::DIV_INT_LIT16:
1918 case Instruction::REM_INT:
1919 case Instruction::REM_INT_2ADDR:
1920 case Instruction::REM_INT_LIT8:
1921 case Instruction::REM_INT_LIT16: {
1922 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001923 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001924 return;
1925 }
buzbee11b63d12013-08-27 07:34:17 -07001926 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001927 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001928 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001929 (opcode == Instruction::DIV_INT_LIT16)) {
1930 is_div = true;
1931 } else {
1932 is_div = false;
1933 }
buzbee11b63d12013-08-27 07:34:17 -07001934 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1935 return;
1936 }
Dave Allison70202782013-10-22 17:52:19 -07001937
1938 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001939 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001941 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001942 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001943 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001944 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1945 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001946 } else if (cu_->instruction_set == kThumb2) {
1947 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1948 // Use ARM SDIV instruction for division. For remainder we also need to
1949 // calculate using a MUL and subtract.
1950 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001951 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001952 done = true;
1953 }
1954 }
1955
1956 if (!done) {
1957 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001958 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1959 Clobber(TargetReg(kArg0, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001960 if (cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001961 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, kNotWide),
1962 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001963 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001964 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, kNotWide),
1965 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001966 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001967 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001968 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001969 else
1970 rl_result = GetReturnAlt();
1971 }
1972 StoreValue(rl_dest, rl_result);
1973 return;
1974 }
1975 default:
1976 LOG(FATAL) << "Unexpected opcode " << opcode;
1977 }
1978 rl_src = LoadValue(rl_src, kCoreReg);
1979 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001980 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001982 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001984 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985 }
1986 StoreValue(rl_dest, rl_result);
1987}
1988
Andreas Gampe2f244e92014-05-08 03:35:25 -07001989template <size_t pointer_size>
1990static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1991 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001992 RegLocation rl_result;
1993 OpKind first_op = kOpBkpt;
1994 OpKind second_op = kOpBkpt;
1995 bool call_out = false;
1996 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001997 ThreadOffset<pointer_size> func_offset(-1);
Andreas Gampeccc60262014-07-04 18:02:38 -07001998 int ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999
2000 switch (opcode) {
2001 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07002002 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002003 mir_to_lir->GenNotLong(rl_dest, rl_src2);
2004 return;
2005 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002006 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
2007 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002008 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002009 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010 RegStorage t_reg = mir_to_lir->AllocTemp();
2011 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2012 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2013 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2014 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002016 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2017 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002018 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002019 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002020 return;
2021 case Instruction::ADD_LONG:
2022 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002023 if (cu->instruction_set != kThumb2) {
2024 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002025 return;
2026 }
2027 first_op = kOpAdd;
2028 second_op = kOpAdc;
2029 break;
2030 case Instruction::SUB_LONG:
2031 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002032 if (cu->instruction_set != kThumb2) {
2033 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 return;
2035 }
2036 first_op = kOpSub;
2037 second_op = kOpSbc;
2038 break;
2039 case Instruction::MUL_LONG:
2040 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002041 if (cu->instruction_set != kMips) {
2042 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002043 return;
2044 } else {
2045 call_out = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002046 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002047 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002048 }
2049 break;
2050 case Instruction::DIV_LONG:
2051 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002052 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002053 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2054 return;
2055 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002056 call_out = true;
2057 check_zero = 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, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002060 break;
2061 case Instruction::REM_LONG:
2062 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002063 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002064 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2065 return;
2066 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 call_out = true;
2068 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002069 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampeccc60262014-07-04 18:02:38 -07002071 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, kNotWide).GetReg() :
2072 mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002073 break;
2074 case Instruction::AND_LONG_2ADDR:
2075 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002076 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2077 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002078 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 }
2080 first_op = kOpAnd;
2081 second_op = kOpAnd;
2082 break;
2083 case Instruction::OR_LONG:
2084 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002085 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2086 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002087 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002088 return;
2089 }
2090 first_op = kOpOr;
2091 second_op = kOpOr;
2092 break;
2093 case Instruction::XOR_LONG:
2094 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002095 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2096 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002097 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002098 return;
2099 }
2100 first_op = kOpXor;
2101 second_op = kOpXor;
2102 break;
2103 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002104 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 return;
2106 }
2107 default:
2108 LOG(FATAL) << "Invalid long arith op";
2109 }
2110 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002111 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002113 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002114 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002115 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kWide);
2116 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kWide);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002117 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2118 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Andreas Gampeccc60262014-07-04 18:02:38 -07002119 mir_to_lir->GenDivZeroCheckWide(r_tmp2);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002120 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002121 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002122 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002123 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002124 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002125 }
2126 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampeccc60262014-07-04 18:02:38 -07002127 if (ret_reg == mir_to_lir->TargetReg(kRet0, kNotWide).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002128 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002129 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002130 rl_result = mir_to_lir->GetReturnWideAlt();
2131 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002132 }
2133}
2134
Andreas Gampe2f244e92014-05-08 03:35:25 -07002135void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2136 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002137 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002138 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2139 } else {
2140 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2141 }
2142}
2143
Mark Mendelle87f9b52014-04-30 14:13:18 -04002144void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2145 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2146 LoadConstantNoClobber(rl_result.reg, value);
2147 StoreValue(rl_dest, rl_result);
2148 if (value == 0) {
2149 Workaround7250540(rl_dest, rl_result.reg);
2150 }
2151}
2152
Andreas Gampe2f244e92014-05-08 03:35:25 -07002153template <size_t pointer_size>
2154void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002155 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002156 /*
2157 * Don't optimize the register usage since it calls out to support
2158 * functions
2159 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002160 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2161
Brian Carlstrom7940e442013-07-12 13:46:57 -07002162 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002163 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2164 if (rl_dest.wide) {
2165 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002166 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002167 StoreValueWide(rl_dest, rl_result);
2168 } else {
2169 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002170 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002171 StoreValue(rl_dest, rl_result);
2172 }
2173}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002174template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2175 RegLocation rl_dest, RegLocation rl_src);
2176template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2177 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002178
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002179class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2180 public:
2181 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2182 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2183 }
2184
2185 void Compile() OVERRIDE {
2186 m2l_->ResetRegPool();
2187 m2l_->ResetDefTracking();
2188 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002189 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002190 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2191 } else {
2192 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2193 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002194 if (cont_ != nullptr) {
2195 m2l_->OpUnconditionalBranch(cont_);
2196 }
2197 }
2198};
2199
Brian Carlstrom7940e442013-07-12 13:46:57 -07002200/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002201void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002202 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002203 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2204 return;
2205 }
2206 FlushAllRegs();
2207 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002208 LIR* cont = NewLIR0(kPseudoTargetLabel);
2209 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002210 } else {
2211 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2212 return;
2213 }
2214 FlushAllRegs(); // TODO: needed?
2215 LIR* inst = CheckSuspendUsingLoad();
2216 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002217 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002218}
2219
2220/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002221void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002222 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002223 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2224 OpUnconditionalBranch(target);
2225 return;
2226 }
2227 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002228 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002229 LIR* branch = OpUnconditionalBranch(nullptr);
2230 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002231 } else {
2232 // For the implicit suspend check, just perform the trigger
2233 // load and branch to the target.
2234 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2235 OpUnconditionalBranch(target);
2236 return;
2237 }
2238 FlushAllRegs();
2239 LIR* inst = CheckSuspendUsingLoad();
2240 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002241 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002242 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002243}
2244
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002245/* Call out to helper assembly routine that will null check obj and then lock it. */
2246void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2247 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002248 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002249 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2250 } else {
2251 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2252 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002253}
2254
2255/* Call out to helper assembly routine that will null check obj and then unlock it. */
2256void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2257 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002258 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002259 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2260 } else {
2261 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2262 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002263}
2264
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002265/* Generic code for generating a wide constant into a VR. */
2266void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2267 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002268 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002269 StoreValueWide(rl_dest, rl_result);
2270}
2271
Brian Carlstrom7940e442013-07-12 13:46:57 -07002272} // namespace art