blob: b94e8166b5e029156ef3f15b0c7ea0222fbcd387 [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);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700276 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 if ((rl_temp.location == kLocDalvikFrame) &&
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700278 InexpensiveConstantInt(constant_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800280 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 return;
282 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700283
284 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
285 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
286 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
287 // the 32b literal 0 for null.
288 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
289 // Use the OpCmpImmBranch and ignore the value in the register.
290 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
291 return;
292 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700294
buzbeea0cd2d72014-06-01 09:33:49 -0700295 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800296 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297}
298
299void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700300 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700302 DCHECK(!rl_src.fp);
303 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 switch (opcode) {
305 case Instruction::IF_EQZ:
306 cond = kCondEq;
307 break;
308 case Instruction::IF_NEZ:
309 cond = kCondNe;
310 break;
311 case Instruction::IF_LTZ:
312 cond = kCondLt;
313 break;
314 case Instruction::IF_GEZ:
315 cond = kCondGe;
316 break;
317 case Instruction::IF_GTZ:
318 cond = kCondGt;
319 break;
320 case Instruction::IF_LEZ:
321 cond = kCondLe;
322 break;
323 default:
324 cond = static_cast<ConditionCode>(0);
325 LOG(FATAL) << "Unexpected opcode " << opcode;
326 }
buzbee2700f7e2014-03-07 09:46:20 -0800327 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328}
329
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
332 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800333 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800335 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 }
buzbee2700f7e2014-03-07 09:46:20 -0800337 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 StoreValueWide(rl_dest, rl_result);
339}
340
341void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700342 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700343 rl_src = LoadValue(rl_src, kCoreReg);
344 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
345 OpKind op = kOpInvalid;
346 switch (opcode) {
347 case Instruction::INT_TO_BYTE:
348 op = kOp2Byte;
349 break;
350 case Instruction::INT_TO_SHORT:
351 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700353 case Instruction::INT_TO_CHAR:
354 op = kOp2Char;
355 break;
356 default:
357 LOG(ERROR) << "Bad int conversion type";
358 }
buzbee2700f7e2014-03-07 09:46:20 -0800359 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700360 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361}
362
Andreas Gampe2f244e92014-05-08 03:35:25 -0700363template <size_t pointer_size>
364static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
365 uint32_t type_idx, RegLocation rl_dest,
366 RegLocation rl_src) {
367 mir_to_lir->FlushAllRegs(); /* Everything to home location */
368 ThreadOffset<pointer_size> func_offset(-1);
369 const DexFile* dex_file = cu->dex_file;
370 CompilerDriver* driver = cu->compiler_driver;
371 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
372 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800373 bool is_type_initialized; // Ignored as an array does not have an initializer.
374 bool use_direct_type_ptr;
375 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700376 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800377 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700378 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
379 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800380 // The fast path.
381 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700382 mir_to_lir->LoadClassType(type_idx, kArg0);
383 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -0700384 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset,
385 mir_to_lir->TargetReg(kArg0, kNotWide),
Andreas Gampe2f244e92014-05-08 03:35:25 -0700386 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800387 } else {
388 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700389 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
390 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
391 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800392 }
393 } else {
394 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700395 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
396 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800397 }
398 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700400 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
401 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 }
buzbeea0cd2d72014-06-01 09:33:49 -0700403 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700404 mir_to_lir->StoreValue(rl_dest, rl_result);
405}
406
407/*
408 * Let helper function take care of everything. Will call
409 * Array::AllocFromCode(type_idx, method, count);
410 * Note: AllocFromCode will handle checks for errNegativeArraySize.
411 */
412void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
413 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700414 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700415 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
416 } else {
417 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
418 }
419}
420
421template <size_t pointer_size>
422static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
423 ThreadOffset<pointer_size> func_offset(-1);
424 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
425 type_idx)) {
426 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
427 } else {
428 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
429 }
430 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431}
432
433/*
434 * Similar to GenNewArray, but with post-allocation initialization.
435 * Verifier guarantees we're dealing with an array class. Current
436 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
437 * Current code also throws internal unimp if not 'L', '[' or 'I'.
438 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700439void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 int elems = info->num_arg_words;
441 int type_idx = info->index;
442 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700443 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700444 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700446 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700448 FreeTemp(TargetReg(kArg2, kNotWide));
449 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 /*
451 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
452 * return region. Because AllocFromCode placed the new array
453 * in kRet0, we'll just lock it into place. When debugger support is
454 * added, it may be necessary to additionally copy all return
455 * values to a home location in thread-local storage
456 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700457 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700458 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459
460 // TODO: use the correct component size, currently all supported types
461 // share array alignment with ints (see comment at head of function)
462 size_t component_size = sizeof(int32_t);
463
464 // Having a range of 0 is legal
465 if (info->is_range && (elems > 0)) {
466 /*
467 * Bit of ugliness here. We're going generate a mem copy loop
468 * on the register range, but it is possible that some regs
469 * in the range have been promoted. This is unlikely, but
470 * before generating the copy, we'll just force a flush
471 * of any regs in the source range that have been promoted to
472 * home location.
473 */
474 for (int i = 0; i < elems; i++) {
475 RegLocation loc = UpdateLoc(info->args[i]);
476 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100477 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700478 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 }
480 }
481 /*
482 * TUNING note: generated code here could be much improved, but
483 * this is an uncommon operation and isn't especially performance
484 * critical.
485 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700486 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700487 RegStorage r_src = AllocTempRef();
488 RegStorage r_dst = AllocTempRef();
489 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800490 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700491 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700493 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700494 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 break;
496 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700497 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700498 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 r_val = AllocTemp();
500 break;
501 case kMips:
502 r_val = AllocTemp();
503 break;
504 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
505 }
506 // Set up source pointer
507 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700508 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700510 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 mirror::Array::DataOffset(component_size).Int32Value());
512 // Set up the loop counter (known to be > 0)
513 LoadConstant(r_idx, elems - 1);
514 // Generate the copy loop. Going backwards for convenience
515 LIR* target = NewLIR0(kPseudoTargetLabel);
516 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100517 {
518 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
519 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
520 // NOTE: No dalvik register annotation, local optimizations will be stopped
521 // by the loop boundaries.
522 }
buzbee695d13a2014-04-19 13:32:20 -0700523 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 FreeTemp(r_val);
525 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700526 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700528 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 -mirror::Array::DataOffset(component_size).Int32Value());
530 }
531 } else if (!info->is_range) {
532 // TUNING: interleave
533 for (int i = 0; i < elems; i++) {
534 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700535 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000536 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800538 if (IsTemp(rl_arg.reg)) {
539 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
541 }
542 }
543 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700544 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 }
546}
547
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800548//
549// Slow path to ensure a class is initialized for sget/sput.
550//
551class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
552 public:
buzbee2700f7e2014-03-07 09:46:20 -0800553 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
554 RegStorage r_base) :
555 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
556 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800557 }
558
559 void Compile() {
560 LIR* unresolved_target = GenerateTargetLabel();
561 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700562 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700563 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
564 storage_index_, true);
565 } else {
566 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
567 storage_index_, true);
568 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800569 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700570 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800571
572 m2l_->OpUnconditionalBranch(cont_);
573 }
574
575 private:
576 LIR* const uninit_;
577 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800578 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800579};
580
Andreas Gampe2f244e92014-05-08 03:35:25 -0700581template <size_t pointer_size>
582static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
583 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
584 ThreadOffset<pointer_size> setter_offset =
585 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
586 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
587 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
588 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
589 true);
590}
591
Vladimir Markobe0e5462014-02-26 11:24:15 +0000592void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700593 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000594 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
595 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100596 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700597 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000598 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800599 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000600 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100602 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700603 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000604 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
605 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800606 if (IsTemp(rl_method.reg)) {
607 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 }
609 } else {
610 // Medium path, static storage base in a different class which requires checks that the other
611 // class is initialized.
612 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 // May do runtime call so everything to home locations.
615 FlushAllRegs();
616 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700617 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 LockTemp(r_method);
619 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700620 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000622 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
623 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000624 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000625 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000627 if (!field_info.IsInitialized() &&
628 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800629 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800630
631 // The slow path is invoked if the r_base is NULL or the class pointed
632 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800633 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700634 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800636 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800637 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000638 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800639 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800640
buzbee2700f7e2014-03-07 09:46:20 -0800641 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000642 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800643
644 FreeTemp(r_tmp);
Hans Boehm48f5c472014-06-27 14:50:10 -0700645 // Ensure load of status and store of value don't re-order.
646 // TODO: Presumably the actual value store is control-dependent on the status load,
647 // and will thus not be reordered in any case, since stores are never speculated.
648 // Does later code "know" that the class is now initialized? If so, we still
649 // need the barrier to guard later static loads.
650 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 FreeTemp(r_method);
653 }
654 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100655 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100657 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100659 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000661 if (is_object) {
662 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
663 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100664 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000665 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
666 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 }
668 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800669 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800671 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 } else {
673 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700674 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700675 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
676 } else {
677 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
678 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
680}
681
Andreas Gampe2f244e92014-05-08 03:35:25 -0700682template <size_t pointer_size>
683static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
684 const MirSFieldLoweringInfo* field_info) {
685 ThreadOffset<pointer_size> getter_offset =
686 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
687 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
688 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
689 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
690}
691
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700693 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
695 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100696 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700697 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800699 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000700 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 // Fast path, static storage base is this method's class
702 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700703 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000704 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
705 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 } else {
707 // Medium path, static storage base in a different class which requires checks that the other
708 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 // May do runtime call so everything to home locations.
711 FlushAllRegs();
712 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700713 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 LockTemp(r_method);
715 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700716 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800717 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000718 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
719 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000720 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000721 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800722 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000723 if (!field_info.IsInitialized() &&
724 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800725 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800726
727 // The slow path is invoked if the r_base is NULL or the class pointed
728 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800729 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Andreas Gampeccc60262014-07-04 18:02:38 -0700730 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800731 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800732 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800733 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000734 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800735 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800736
buzbee2700f7e2014-03-07 09:46:20 -0800737 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000738 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800739
740 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700741 // Ensure load of status and load of value don't re-order.
Hans Boehm48f5c472014-06-27 14:50:10 -0700742 GenMemBarrier(kLoadAny);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 FreeTemp(r_method);
745 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800746 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100747 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
748 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800749
Vladimir Marko674744e2014-04-24 15:18:26 +0100750 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000751 if (is_object) {
752 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
753 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100754 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000755 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
756 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800757 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100758 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800759
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 if (is_long_or_double) {
761 StoreValueWide(rl_dest, rl_result);
762 } else {
763 StoreValue(rl_dest, rl_result);
764 }
765 } else {
766 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700767 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700768 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
769 } else {
770 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
771 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700772 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700774 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 StoreValueWide(rl_dest, rl_result);
776 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700777 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 StoreValue(rl_dest, rl_result);
779 }
780 }
781}
782
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800783// Generate code for all slow paths.
784void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700785 // We should check slow_paths_.Size() every time, because a new slow path
786 // may be created during slowpath->Compile().
787 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800788 LIRSlowPath* slowpath = slow_paths_.Get(i);
789 slowpath->Compile();
790 }
791 slow_paths_.Reset();
792}
793
Andreas Gampe2f244e92014-05-08 03:35:25 -0700794template <size_t pointer_size>
795static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
796 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
797 ThreadOffset<pointer_size> getter_offset =
798 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
799 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
800 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700801 // Second argument of pGetXXInstance is always a reference.
802 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700803 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
804 true);
805}
806
Vladimir Markobe0e5462014-02-26 11:24:15 +0000807void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700809 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000810 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
811 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100812 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700813 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100814 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000815 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700816 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100817 GenNullCheck(rl_obj.reg, opt_flags);
818 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
819 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000820 LIR* load_lir;
821 if (is_object) {
822 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
823 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100824 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000825 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
826 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100827 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000828 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 StoreValueWide(rl_dest, rl_result);
831 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 StoreValue(rl_dest, rl_result);
833 }
834 } else {
buzbee33ae5582014-06-12 14:56:32 -0700835 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700836 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
837 } else {
838 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
839 }
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700840 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700842 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 StoreValueWide(rl_dest, rl_result);
844 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700845 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 StoreValue(rl_dest, rl_result);
847 }
848 }
849}
850
Andreas Gampe2f244e92014-05-08 03:35:25 -0700851template <size_t pointer_size>
852static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
853 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
854 RegLocation rl_src) {
855 ThreadOffset<pointer_size> setter_offset =
856 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
857 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
858 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
859 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
860 rl_obj, rl_src, true);
861}
862
Vladimir Markobe0e5462014-02-26 11:24:15 +0000863void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700865 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000866 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
867 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100868 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700869 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100870 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000871 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700872 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100874 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 } else {
876 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100877 }
878 GenNullCheck(rl_obj.reg, opt_flags);
879 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000880 LIR* store;
881 if (is_object) {
882 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
883 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100884 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000885 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
886 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100887 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000888 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100889 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
890 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 }
892 } else {
buzbee33ae5582014-06-12 14:56:32 -0700893 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700894 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
895 } else {
896 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
897 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 }
899}
900
Andreas Gampe2f244e92014-05-08 03:35:25 -0700901template <size_t pointer_size>
902static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
903 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
904 ThreadOffset<pointer_size> helper = needs_range_check
905 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
906 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
907 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
908 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
909 true);
910}
911
Ian Rogersa9a82542013-10-04 11:17:26 -0700912void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
913 RegLocation rl_src) {
914 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
915 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
916 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700917 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700918 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
919 } else {
920 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
921 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700922}
923
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700924void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700926 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700927 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700928 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700929 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700930 *cu_->dex_file,
931 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 // Call out to helper which resolves type and verifies access.
933 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700934 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700935 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
936 type_idx, rl_method.reg, true);
937 } else {
938 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
939 type_idx, rl_method.reg, true);
940 }
buzbeea0cd2d72014-06-01 09:33:49 -0700941 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 StoreValue(rl_dest, rl_result);
943 } else {
944 // We're don't need access checks, load type from dex cache
945 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700946 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000947 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000948 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000949 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
951 type_idx) || SLOW_TYPE_PATH) {
952 // Slow path, at runtime test if type is null and if so initialize
953 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800954 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800955 LIR* cont = NewLIR0(kPseudoTargetLabel);
956
957 // Object to generate the slow path for class resolution.
958 class SlowPath : public LIRSlowPath {
959 public:
960 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
961 const RegLocation& rl_method, const RegLocation& rl_result) :
962 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
963 rl_method_(rl_method), rl_result_(rl_result) {
964 }
965
966 void Compile() {
967 GenerateTargetLabel();
968
buzbee33ae5582014-06-12 14:56:32 -0700969 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700970 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
971 rl_method_.reg, true);
972 } else {
973 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
974 rl_method_.reg, true);
975 }
Andreas Gampeccc60262014-07-04 18:02:38 -0700976 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800977
978 m2l_->OpUnconditionalBranch(cont_);
979 }
980
981 private:
982 const int type_idx_;
983 const RegLocation rl_method_;
984 const RegLocation rl_result_;
985 };
986
987 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800988 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800989
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800991 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 // Fast path, we're done - just store result
993 StoreValue(rl_dest, rl_result);
994 }
995 }
996}
997
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700998void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001000 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1001 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1003 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1004 // slow path, resolve string if not in dex cache
1005 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001006 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001007
1008 // If the Method* is already in a register, we can save a copy.
1009 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001010 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001011 if (rl_method.location == kLocPhysReg) {
1012 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001013 DCHECK(!IsTemp(rl_method.reg));
1014 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001015 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001016 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001017 LoadCurrMethodDirect(r_method);
1018 }
buzbee695d13a2014-04-19 13:32:20 -07001019 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001020 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001021
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001023 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1024 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001025 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001026
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001027 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001028 // Object to generate the slow path for string resolution.
1029 class SlowPath : public LIRSlowPath {
1030 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001031 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1032 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1033 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001034 }
1035
1036 void Compile() {
1037 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001038 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001039 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1040 r_method_, string_idx_, true);
1041 } else {
1042 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1043 r_method_, string_idx_, true);
1044 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001045 m2l_->OpUnconditionalBranch(cont_);
1046 }
1047
1048 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001049 const RegStorage r_method_;
1050 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001051 };
1052
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001053 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001055
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001057 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 } else {
1059 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001060 RegStorage res_reg = AllocTempRef();
1061 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001062 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1063 kNotVolatile);
1064 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 StoreValue(rl_dest, rl_result);
1066 }
1067}
1068
Andreas Gampe2f244e92014-05-08 03:35:25 -07001069template <size_t pointer_size>
1070static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1071 RegLocation rl_dest) {
1072 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 // alloc will always check for resolution, do we also need to verify
1074 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001075 ThreadOffset<pointer_size> func_offset(-1);
1076 const DexFile* dex_file = cu->dex_file;
1077 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001078 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001079 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001080 bool is_type_initialized;
1081 bool use_direct_type_ptr;
1082 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001083 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001084 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001085 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1086 &direct_type_ptr, &is_finalizable) &&
1087 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001088 // The fast path.
1089 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001090 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001092 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Andreas Gampeccc60262014-07-04 18:02:38 -07001093 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1094 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);
Andreas Gampeccc60262014-07-04 18:02:38 -07001097 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetReg(kArg0, kRef),
1098 true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001099 }
1100 } else {
1101 // Use the direct pointer.
1102 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001103 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1104 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001105 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001106 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1107 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001108 }
1109 }
1110 } else {
1111 // The slow path.
1112 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001113 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1114 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001115 }
1116 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001118 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1119 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 }
buzbeea0cd2d72014-06-01 09:33:49 -07001121 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001122 mir_to_lir->StoreValue(rl_dest, rl_result);
1123}
1124
1125/*
1126 * Let helper function take care of everything. Will
1127 * call Class::NewInstanceFromCode(type_idx, method);
1128 */
1129void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001130 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001131 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1132 } else {
1133 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1134 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135}
1136
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001137void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001139 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001140 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1141 } else {
1142 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1143 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144}
1145
1146// For final classes there are no sub-classes to check and so we can answer the instance-of
1147// question with simple comparisons.
1148void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1149 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001150 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001151 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001152
buzbeea0cd2d72014-06-01 09:33:49 -07001153 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001155 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001156 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001158 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001161 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162
buzbeea0cd2d72014-06-01 09:33:49 -07001163 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1164 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165
1166 LoadCurrMethodDirect(check_class);
1167 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001168 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1169 kNotVolatile);
1170 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1171 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 } else {
buzbee695d13a2014-04-19 13:32:20 -07001173 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001174 check_class, kNotVolatile);
1175 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1176 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001177 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001178 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 }
1180
buzbee695d13a2014-04-19 13:32:20 -07001181 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 if (cu_->instruction_set == kThumb2) {
1183 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001184 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001186 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001188 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 }
1190 LIR* target = NewLIR0(kPseudoTargetLabel);
1191 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 FreeTemp(object_class);
1193 FreeTemp(check_class);
1194 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001195 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 FreeTemp(result_reg);
1197 }
1198 StoreValue(rl_dest, rl_result);
1199}
1200
1201void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1202 bool type_known_abstract, bool use_declaring_class,
1203 bool can_assume_type_is_in_dex_cache,
1204 uint32_t type_idx, RegLocation rl_dest,
1205 RegLocation rl_src) {
1206 FlushAllRegs();
1207 // May generate a call - use explicit registers
1208 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001209 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001210 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001211 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001212 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1213 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 if (needs_access_check) {
1215 // Check we have access to type_idx and if not throw IllegalAccessError,
1216 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001217 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001218 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1219 type_idx, true);
1220 } else {
1221 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1222 type_idx, true);
1223 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001224 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1225 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001227 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001228 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001229 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001231 if (can_assume_type_is_in_dex_cache) {
1232 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001233 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001234 }
1235
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001237 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001238 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001239 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001240 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001242 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1243 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1244
1245 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001246 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001247
1248 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1249 public:
1250 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx,
1251 RegLocation rl_src)
1252 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx),
1253 rl_src_(rl_src) {
1254 }
1255
1256 void Compile() OVERRIDE {
1257 GenerateTargetLabel();
1258
1259 if (cu_->target64) {
1260 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
1261 true);
1262 } else {
1263 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
1264 true);
1265 }
1266 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1267 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
1268
1269 m2l_->OpUnconditionalBranch(cont_);
1270 }
1271
1272 private:
1273 uint32_t type_idx_;
1274 RegLocation rl_src_;
1275 };
1276
1277 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1278 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 }
1280 }
1281 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001282 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001283 if (!IsSameReg(rl_result.reg, ref_reg)) {
1284 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001285 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001287 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288
1289 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001290 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001292 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1293 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1295 LIR* branchover = NULL;
1296 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001297 // rl_result == ref == class.
1298 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001299 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 } else {
1301 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001302 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001303 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1304 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001305 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 if (!type_known_abstract) {
1307 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001308 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001309 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001310 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001312 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001314 if (it != nullptr) {
1315 OpEndIT(it);
1316 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 FreeTemp(r_tgt);
1318 } else {
1319 if (!type_known_abstract) {
1320 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001321 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001322 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001324
Serguei Katkov9ee45192014-07-17 14:39:03 +07001325 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe90969af2014-07-15 23:02:11 -07001326 if (cu_->target64) {
1327 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial), false);
1328 } else {
1329 CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial), false);
1330 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 }
1332 }
1333 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001334 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 /* branch targets here */
1336 LIR* target = NewLIR0(kPseudoTargetLabel);
1337 StoreValue(rl_dest, rl_result);
1338 branch1->target = target;
1339 if (branchover != NULL) {
1340 branchover->target = target;
1341 }
1342}
1343
1344void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1345 bool type_known_final, type_known_abstract, use_declaring_class;
1346 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1347 *cu_->dex_file,
1348 type_idx,
1349 &type_known_final,
1350 &type_known_abstract,
1351 &use_declaring_class);
1352 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1353 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1354
1355 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1356 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1357 } else {
1358 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1359 use_declaring_class, can_assume_type_is_in_dex_cache,
1360 type_idx, rl_dest, rl_src);
1361 }
1362}
1363
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001364void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 bool type_known_final, type_known_abstract, use_declaring_class;
1366 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1367 *cu_->dex_file,
1368 type_idx,
1369 &type_known_final,
1370 &type_known_abstract,
1371 &use_declaring_class);
1372 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1373 // of the exception throw path.
1374 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001375 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 // Verifier type analysis proved this check cast would never cause an exception.
1377 return;
1378 }
1379 FlushAllRegs();
1380 // May generate a call - use explicit registers
1381 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001382 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001383 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001384 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 if (needs_access_check) {
1386 // Check we have access to type_idx and if not throw IllegalAccessError,
1387 // returns Class* in kRet0
1388 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001389 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001390 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1391 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001392 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001393 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1394 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001395 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001396 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001398 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001399 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 } else {
1401 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001402 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001403 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001404 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001405 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1407 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001408 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1409 LIR* cont = NewLIR0(kPseudoTargetLabel);
1410
1411 // Slow path to initialize the type. Executed if the type is NULL.
1412 class SlowPath : public LIRSlowPath {
1413 public:
1414 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001415 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001416 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1417 class_reg_(class_reg) {
1418 }
1419
1420 void Compile() {
1421 GenerateTargetLabel();
1422
1423 // Call out to helper, which will return resolved type in kArg0
1424 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001425 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001426 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001427 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001428 } else {
1429 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampeccc60262014-07-04 18:02:38 -07001430 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001431 }
Andreas Gampeccc60262014-07-04 18:02:38 -07001432 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001433 m2l_->OpUnconditionalBranch(cont_);
1434 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001435
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001436 public:
1437 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001438 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001439 };
1440
buzbee2700f7e2014-03-07 09:46:20 -08001441 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 }
1443 }
1444 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001445 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001446
1447 // Slow path for the case where the classes are not equal. In this case we need
1448 // to call a helper function to do the check.
1449 class SlowPath : public LIRSlowPath {
1450 public:
1451 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1452 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1453 }
1454
1455 void Compile() {
1456 GenerateTargetLabel();
1457
1458 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001459 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1460 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001461 }
buzbee33ae5582014-06-12 14:56:32 -07001462 if (m2l_->cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001463 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast),
1464 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1465 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001466 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001467 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast),
1468 m2l_->TargetReg(kArg2, kRef), m2l_->TargetReg(kArg1, kRef),
1469 true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001470 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001471
1472 m2l_->OpUnconditionalBranch(cont_);
1473 }
1474
1475 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001476 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001477 };
1478
1479 if (type_known_abstract) {
1480 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001481 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001482 LIR* cont = NewLIR0(kPseudoTargetLabel);
1483 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1484 } else {
1485 // Harder, more common case. We need to generate a forward branch over the load
1486 // if the target is null. If it's non-null we perform the load and branch to the
1487 // slow path if the classes are not equal.
1488
1489 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001490 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001491 /* load object->klass_ */
1492 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001493 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1494 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001495
Andreas Gampeccc60262014-07-04 18:02:38 -07001496 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001497 LIR* cont = NewLIR0(kPseudoTargetLabel);
1498
1499 // Add the slow path that will not perform load since this is already done.
1500 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1501
1502 // Set the null check to branch to the continuation.
1503 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 }
1505}
1506
1507void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001508 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 RegLocation rl_result;
1510 if (cu_->instruction_set == kThumb2) {
1511 /*
1512 * NOTE: This is the one place in the code in which we might have
1513 * as many as six live temporary registers. There are 5 in the normal
1514 * set for Arm. Until we have spill capabilities, temporarily add
1515 * lr to the temp set. It is safe to do this locally, but note that
1516 * lr is used explicitly elsewhere in the code generator and cannot
1517 * normally be used as a general temp register.
1518 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001519 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1520 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 }
1522 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1523 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1524 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1525 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001526 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1527 RegStorage t_reg = AllocTemp();
1528 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1529 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1530 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 FreeTemp(t_reg);
1532 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001533 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1534 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 }
1536 /*
1537 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1538 * following StoreValueWide might need to allocate a temp register.
1539 * To further work around the lack of a spill capability, explicitly
1540 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1541 * Remove when spill is functional.
1542 */
1543 FreeRegLocTemps(rl_result, rl_src1);
1544 FreeRegLocTemps(rl_result, rl_src2);
1545 StoreValueWide(rl_dest, rl_result);
1546 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001547 Clobber(TargetReg(kLr, kNotWide));
1548 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
1550}
1551
1552
Andreas Gampe2f244e92014-05-08 03:35:25 -07001553template <size_t pointer_size>
1554static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1555 RegLocation rl_shift) {
1556 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557
1558 switch (opcode) {
1559 case Instruction::SHL_LONG:
1560 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001561 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 break;
1563 case Instruction::SHR_LONG:
1564 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001565 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 break;
1567 case Instruction::USHR_LONG:
1568 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001569 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 break;
1571 default:
1572 LOG(FATAL) << "Unexpected case";
1573 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001574 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1575 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1576}
1577
1578void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1579 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001580 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001581 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1582 } else {
1583 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1584 }
buzbeea0cd2d72014-06-01 09:33:49 -07001585 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001586 StoreValueWide(rl_dest, rl_result);
1587}
1588
1589
1590void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001591 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001592 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 OpKind op = kOpBkpt;
1594 bool is_div_rem = false;
1595 bool check_zero = false;
1596 bool unary = false;
1597 RegLocation rl_result;
1598 bool shift_op = false;
1599 switch (opcode) {
1600 case Instruction::NEG_INT:
1601 op = kOpNeg;
1602 unary = true;
1603 break;
1604 case Instruction::NOT_INT:
1605 op = kOpMvn;
1606 unary = true;
1607 break;
1608 case Instruction::ADD_INT:
1609 case Instruction::ADD_INT_2ADDR:
1610 op = kOpAdd;
1611 break;
1612 case Instruction::SUB_INT:
1613 case Instruction::SUB_INT_2ADDR:
1614 op = kOpSub;
1615 break;
1616 case Instruction::MUL_INT:
1617 case Instruction::MUL_INT_2ADDR:
1618 op = kOpMul;
1619 break;
1620 case Instruction::DIV_INT:
1621 case Instruction::DIV_INT_2ADDR:
1622 check_zero = true;
1623 op = kOpDiv;
1624 is_div_rem = true;
1625 break;
1626 /* NOTE: returns in kArg1 */
1627 case Instruction::REM_INT:
1628 case Instruction::REM_INT_2ADDR:
1629 check_zero = true;
1630 op = kOpRem;
1631 is_div_rem = true;
1632 break;
1633 case Instruction::AND_INT:
1634 case Instruction::AND_INT_2ADDR:
1635 op = kOpAnd;
1636 break;
1637 case Instruction::OR_INT:
1638 case Instruction::OR_INT_2ADDR:
1639 op = kOpOr;
1640 break;
1641 case Instruction::XOR_INT:
1642 case Instruction::XOR_INT_2ADDR:
1643 op = kOpXor;
1644 break;
1645 case Instruction::SHL_INT:
1646 case Instruction::SHL_INT_2ADDR:
1647 shift_op = true;
1648 op = kOpLsl;
1649 break;
1650 case Instruction::SHR_INT:
1651 case Instruction::SHR_INT_2ADDR:
1652 shift_op = true;
1653 op = kOpAsr;
1654 break;
1655 case Instruction::USHR_INT:
1656 case Instruction::USHR_INT_2ADDR:
1657 shift_op = true;
1658 op = kOpLsr;
1659 break;
1660 default:
1661 LOG(FATAL) << "Invalid word arith op: " << opcode;
1662 }
1663 if (!is_div_rem) {
1664 if (unary) {
1665 rl_src1 = LoadValue(rl_src1, kCoreReg);
1666 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001667 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001669 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001670 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001671 RegStorage t_reg = AllocTemp();
1672 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001673 rl_src1 = LoadValue(rl_src1, kCoreReg);
1674 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001675 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001676 FreeTemp(t_reg);
1677 } else {
1678 rl_src1 = LoadValue(rl_src1, kCoreReg);
1679 rl_src2 = LoadValue(rl_src2, kCoreReg);
1680 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001681 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 }
1683 }
1684 StoreValue(rl_dest, rl_result);
1685 } else {
Dave Allison70202782013-10-22 17:52:19 -07001686 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 +01001687 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 rl_src1 = LoadValue(rl_src1, kCoreReg);
1689 rl_src2 = LoadValue(rl_src2, kCoreReg);
1690 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001691 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 }
buzbee2700f7e2014-03-07 09:46:20 -08001693 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001694 done = true;
1695 } else if (cu_->instruction_set == kThumb2) {
1696 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1697 // Use ARM SDIV instruction for division. For remainder we also need to
1698 // calculate using a MUL and subtract.
1699 rl_src1 = LoadValue(rl_src1, kCoreReg);
1700 rl_src2 = LoadValue(rl_src2, kCoreReg);
1701 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001702 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001703 }
buzbee2700f7e2014-03-07 09:46:20 -08001704 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001705 done = true;
1706 }
1707 }
1708
1709 // If we haven't already generated the code use the callout function.
1710 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001712 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001713 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001714 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1715 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Andreas Gampeccc60262014-07-04 18:02:38 -07001716 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001718 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 }
Dave Allison70202782013-10-22 17:52:19 -07001720 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001721 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001722 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1723 } else {
1724 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1725 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001726 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001727 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 else
1729 rl_result = GetReturnAlt();
1730 }
1731 StoreValue(rl_dest, rl_result);
1732 }
1733}
1734
1735/*
1736 * The following are the first-level codegen routines that analyze the format
1737 * of each bytecode then either dispatch special purpose codegen routines
1738 * or produce corresponding Thumb instructions directly.
1739 */
1740
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001742static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001743 x &= x - 1;
1744 return (x & (x - 1)) == 0;
1745}
1746
Brian Carlstrom7940e442013-07-12 13:46:57 -07001747// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1748// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001749bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001750 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1752 return false;
1753 }
1754 // No divide instruction for Arm, so check for more special cases
1755 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001756 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 }
1758 int k = LowestSetBit(lit);
1759 if (k >= 30) {
1760 // Avoid special cases.
1761 return false;
1762 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 rl_src = LoadValue(rl_src, kCoreReg);
1764 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001765 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001766 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001767 if (lit == 2) {
1768 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001769 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1770 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1771 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001773 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001775 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1776 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001777 }
1778 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001779 RegStorage t_reg1 = AllocTemp();
1780 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001781 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001782 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1783 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001785 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001787 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001789 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001790 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001791 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001792 }
1793 }
1794 StoreValue(rl_dest, rl_result);
1795 return true;
1796}
1797
1798// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1799// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001800bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001801 if (lit < 0) {
1802 return false;
1803 }
1804 if (lit == 0) {
1805 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1806 LoadConstant(rl_result.reg, 0);
1807 StoreValue(rl_dest, rl_result);
1808 return true;
1809 }
1810 if (lit == 1) {
1811 rl_src = LoadValue(rl_src, kCoreReg);
1812 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1813 OpRegCopy(rl_result.reg, rl_src.reg);
1814 StoreValue(rl_dest, rl_result);
1815 return true;
1816 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001817 // There is RegRegRegShift on Arm, so check for more special cases
1818 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001819 return EasyMultiply(rl_src, rl_dest, lit);
1820 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001821 // Can we simplify this multiplication?
1822 bool power_of_two = false;
1823 bool pop_count_le2 = false;
1824 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001825 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 power_of_two = true;
1827 } else if (IsPopCountLE2(lit)) {
1828 pop_count_le2 = true;
1829 } else if (IsPowerOfTwo(lit + 1)) {
1830 power_of_two_minus_one = true;
1831 } else {
1832 return false;
1833 }
1834 rl_src = LoadValue(rl_src, kCoreReg);
1835 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1836 if (power_of_two) {
1837 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001838 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 } else if (pop_count_le2) {
1840 // Shift and add and shift.
1841 int first_bit = LowestSetBit(lit);
1842 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1843 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1844 } else {
1845 // Reverse subtract: (src << (shift + 1)) - src.
1846 DCHECK(power_of_two_minus_one);
1847 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001848 RegStorage t_reg = AllocTemp();
1849 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1850 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001851 }
1852 StoreValue(rl_dest, rl_result);
1853 return true;
1854}
1855
1856void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001857 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 RegLocation rl_result;
1859 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1860 int shift_op = false;
1861 bool is_div = false;
1862
1863 switch (opcode) {
1864 case Instruction::RSUB_INT_LIT8:
1865 case Instruction::RSUB_INT: {
1866 rl_src = LoadValue(rl_src, kCoreReg);
1867 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1868 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001869 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001870 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001871 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1872 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001873 }
1874 StoreValue(rl_dest, rl_result);
1875 return;
1876 }
1877
1878 case Instruction::SUB_INT:
1879 case Instruction::SUB_INT_2ADDR:
1880 lit = -lit;
1881 // Intended fallthrough
1882 case Instruction::ADD_INT:
1883 case Instruction::ADD_INT_2ADDR:
1884 case Instruction::ADD_INT_LIT8:
1885 case Instruction::ADD_INT_LIT16:
1886 op = kOpAdd;
1887 break;
1888 case Instruction::MUL_INT:
1889 case Instruction::MUL_INT_2ADDR:
1890 case Instruction::MUL_INT_LIT8:
1891 case Instruction::MUL_INT_LIT16: {
1892 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1893 return;
1894 }
1895 op = kOpMul;
1896 break;
1897 }
1898 case Instruction::AND_INT:
1899 case Instruction::AND_INT_2ADDR:
1900 case Instruction::AND_INT_LIT8:
1901 case Instruction::AND_INT_LIT16:
1902 op = kOpAnd;
1903 break;
1904 case Instruction::OR_INT:
1905 case Instruction::OR_INT_2ADDR:
1906 case Instruction::OR_INT_LIT8:
1907 case Instruction::OR_INT_LIT16:
1908 op = kOpOr;
1909 break;
1910 case Instruction::XOR_INT:
1911 case Instruction::XOR_INT_2ADDR:
1912 case Instruction::XOR_INT_LIT8:
1913 case Instruction::XOR_INT_LIT16:
1914 op = kOpXor;
1915 break;
1916 case Instruction::SHL_INT_LIT8:
1917 case Instruction::SHL_INT:
1918 case Instruction::SHL_INT_2ADDR:
1919 lit &= 31;
1920 shift_op = true;
1921 op = kOpLsl;
1922 break;
1923 case Instruction::SHR_INT_LIT8:
1924 case Instruction::SHR_INT:
1925 case Instruction::SHR_INT_2ADDR:
1926 lit &= 31;
1927 shift_op = true;
1928 op = kOpAsr;
1929 break;
1930 case Instruction::USHR_INT_LIT8:
1931 case Instruction::USHR_INT:
1932 case Instruction::USHR_INT_2ADDR:
1933 lit &= 31;
1934 shift_op = true;
1935 op = kOpLsr;
1936 break;
1937
1938 case Instruction::DIV_INT:
1939 case Instruction::DIV_INT_2ADDR:
1940 case Instruction::DIV_INT_LIT8:
1941 case Instruction::DIV_INT_LIT16:
1942 case Instruction::REM_INT:
1943 case Instruction::REM_INT_2ADDR:
1944 case Instruction::REM_INT_LIT8:
1945 case Instruction::REM_INT_LIT16: {
1946 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001947 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001948 return;
1949 }
buzbee11b63d12013-08-27 07:34:17 -07001950 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001951 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001952 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001953 (opcode == Instruction::DIV_INT_LIT16)) {
1954 is_div = true;
1955 } else {
1956 is_div = false;
1957 }
buzbee11b63d12013-08-27 07:34:17 -07001958 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1959 return;
1960 }
Dave Allison70202782013-10-22 17:52:19 -07001961
1962 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001963 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001964 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001965 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001966 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001967 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001968 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1969 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001970 } else if (cu_->instruction_set == kThumb2) {
1971 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1972 // Use ARM SDIV instruction for division. For remainder we also need to
1973 // calculate using a MUL and subtract.
1974 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001975 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001976 done = true;
1977 }
1978 }
1979
1980 if (!done) {
1981 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001982 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1983 Clobber(TargetReg(kArg0, kNotWide));
buzbee33ae5582014-06-12 14:56:32 -07001984 if (cu_->target64) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001985 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, kNotWide),
1986 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001987 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001988 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, kNotWide),
1989 lit, false);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001990 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001991 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001992 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 else
1994 rl_result = GetReturnAlt();
1995 }
1996 StoreValue(rl_dest, rl_result);
1997 return;
1998 }
1999 default:
2000 LOG(FATAL) << "Unexpected opcode " << opcode;
2001 }
2002 rl_src = LoadValue(rl_src, kCoreReg);
2003 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07002004 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08002006 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002007 } else {
buzbee2700f7e2014-03-07 09:46:20 -08002008 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 }
2010 StoreValue(rl_dest, rl_result);
2011}
2012
Andreas Gampe2f244e92014-05-08 03:35:25 -07002013template <size_t pointer_size>
2014static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
2015 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 RegLocation rl_result;
2017 OpKind first_op = kOpBkpt;
2018 OpKind second_op = kOpBkpt;
2019 bool call_out = false;
2020 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002021 ThreadOffset<pointer_size> func_offset(-1);
Andreas Gampeccc60262014-07-04 18:02:38 -07002022 int ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002023
2024 switch (opcode) {
2025 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07002026 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002027 mir_to_lir->GenNotLong(rl_dest, rl_src2);
2028 return;
2029 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002030 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
2031 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002032 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002033 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002034 RegStorage t_reg = mir_to_lir->AllocTemp();
2035 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2036 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2037 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2038 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002040 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2041 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002043 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002044 return;
2045 case Instruction::ADD_LONG:
2046 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002047 if (cu->instruction_set != kThumb2) {
2048 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002049 return;
2050 }
2051 first_op = kOpAdd;
2052 second_op = kOpAdc;
2053 break;
2054 case Instruction::SUB_LONG:
2055 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002056 if (cu->instruction_set != kThumb2) {
2057 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 return;
2059 }
2060 first_op = kOpSub;
2061 second_op = kOpSbc;
2062 break;
2063 case Instruction::MUL_LONG:
2064 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002065 if (cu->instruction_set != kMips) {
2066 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002067 return;
2068 } else {
2069 call_out = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002070 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002071 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 }
2073 break;
2074 case Instruction::DIV_LONG:
2075 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002076 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002077 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2078 return;
2079 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002080 call_out = true;
2081 check_zero = true;
Andreas Gampeccc60262014-07-04 18:02:38 -07002082 ret_reg = mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002083 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002084 break;
2085 case Instruction::REM_LONG:
2086 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002087 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002088 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2089 return;
2090 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002091 call_out = true;
2092 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002093 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002094 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampeccc60262014-07-04 18:02:38 -07002095 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, kNotWide).GetReg() :
2096 mir_to_lir->TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002097 break;
2098 case Instruction::AND_LONG_2ADDR:
2099 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002100 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2101 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002102 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002103 }
2104 first_op = kOpAnd;
2105 second_op = kOpAnd;
2106 break;
2107 case Instruction::OR_LONG:
2108 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002109 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2110 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002111 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 return;
2113 }
2114 first_op = kOpOr;
2115 second_op = kOpOr;
2116 break;
2117 case Instruction::XOR_LONG:
2118 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002119 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2120 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002121 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002122 return;
2123 }
2124 first_op = kOpXor;
2125 second_op = kOpXor;
2126 break;
2127 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002128 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002129 return;
2130 }
2131 default:
2132 LOG(FATAL) << "Invalid long arith op";
2133 }
2134 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002135 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002136 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002137 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002138 if (check_zero) {
Andreas Gampeccc60262014-07-04 18:02:38 -07002139 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kWide);
2140 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kWide);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002141 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2142 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Andreas Gampeccc60262014-07-04 18:02:38 -07002143 mir_to_lir->GenDivZeroCheckWide(r_tmp2);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002144 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002145 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002146 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002147 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002148 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002149 }
2150 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampeccc60262014-07-04 18:02:38 -07002151 if (ret_reg == mir_to_lir->TargetReg(kRet0, kNotWide).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002152 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002153 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002154 rl_result = mir_to_lir->GetReturnWideAlt();
2155 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002156 }
2157}
2158
Andreas Gampe2f244e92014-05-08 03:35:25 -07002159void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2160 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002161 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002162 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2163 } else {
2164 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2165 }
2166}
2167
Mark Mendelle87f9b52014-04-30 14:13:18 -04002168void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2169 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2170 LoadConstantNoClobber(rl_result.reg, value);
2171 StoreValue(rl_dest, rl_result);
2172 if (value == 0) {
2173 Workaround7250540(rl_dest, rl_result.reg);
2174 }
2175}
2176
Andreas Gampe2f244e92014-05-08 03:35:25 -07002177template <size_t pointer_size>
2178void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002179 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002180 /*
2181 * Don't optimize the register usage since it calls out to support
2182 * functions
2183 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002184 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2185
Brian Carlstrom7940e442013-07-12 13:46:57 -07002186 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002187 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2188 if (rl_dest.wide) {
2189 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002190 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002191 StoreValueWide(rl_dest, rl_result);
2192 } else {
2193 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002194 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002195 StoreValue(rl_dest, rl_result);
2196 }
2197}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002198template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2199 RegLocation rl_dest, RegLocation rl_src);
2200template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2201 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002202
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002203class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2204 public:
2205 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2206 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2207 }
2208
2209 void Compile() OVERRIDE {
2210 m2l_->ResetRegPool();
2211 m2l_->ResetDefTracking();
2212 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002213 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002214 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2215 } else {
2216 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2217 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002218 if (cont_ != nullptr) {
2219 m2l_->OpUnconditionalBranch(cont_);
2220 }
2221 }
2222};
2223
Brian Carlstrom7940e442013-07-12 13:46:57 -07002224/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002225void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002226 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002227 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2228 return;
2229 }
2230 FlushAllRegs();
2231 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002232 LIR* cont = NewLIR0(kPseudoTargetLabel);
2233 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002234 } else {
2235 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2236 return;
2237 }
2238 FlushAllRegs(); // TODO: needed?
2239 LIR* inst = CheckSuspendUsingLoad();
2240 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002242}
2243
2244/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002245void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002246 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002247 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2248 OpUnconditionalBranch(target);
2249 return;
2250 }
2251 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002252 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002253 LIR* branch = OpUnconditionalBranch(nullptr);
2254 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002255 } else {
2256 // For the implicit suspend check, just perform the trigger
2257 // load and branch to the target.
2258 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2259 OpUnconditionalBranch(target);
2260 return;
2261 }
2262 FlushAllRegs();
2263 LIR* inst = CheckSuspendUsingLoad();
2264 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002265 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002266 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002267}
2268
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002269/* Call out to helper assembly routine that will null check obj and then lock it. */
2270void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2271 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002272 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002273 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2274 } else {
2275 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2276 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002277}
2278
2279/* Call out to helper assembly routine that will null check obj and then unlock it. */
2280void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2281 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002282 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002283 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2284 } else {
2285 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2286 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002287}
2288
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002289/* Generic code for generating a wide constant into a VR. */
2290void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2291 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002292 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002293 StoreValueWide(rl_dest, rl_result);
2294}
2295
Brian Carlstrom7940e442013-07-12 13:46:57 -07002296} // namespace art