blob: b559cf5c8953ade4693ebf1b0aa35a7925b8b61b [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 Gampe4b537a82014-06-30 22:24:53 -0700130 RegStorage arg1_32 = m2l_->TargetReg(kArg1, false);
131 RegStorage arg0_32 = m2l_->TargetReg(kArg0, false);
132
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 Allison7fb36de2014-07-10 02:05:10 +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 Allison7fb36de2014-07-10 02:05:10 +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 Allison7fb36de2014-07-10 02:05:10 +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 Allison7fb36de2014-07-10 02:05:10 +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 Allison7fb36de2014-07-10 02:05:10 +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 Allison7fb36de2014-07-10 02:05:10 +0000219 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800220 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
221 return;
222 }
223 // Force an implicit null check by performing a memory operation (load) from the given
224 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800225 RegStorage tmp = AllocTemp();
226 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700227 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800228 FreeTemp(tmp);
229 MarkSafepointPC(load);
230 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231}
232
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
234 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700235 LIR* fall_through) {
buzbeea0cd2d72014-06-01 09:33:49 -0700236 DCHECK(!rl_src1.fp);
237 DCHECK(!rl_src2.fp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 ConditionCode cond;
239 switch (opcode) {
240 case Instruction::IF_EQ:
241 cond = kCondEq;
242 break;
243 case Instruction::IF_NE:
244 cond = kCondNe;
245 break;
246 case Instruction::IF_LT:
247 cond = kCondLt;
248 break;
249 case Instruction::IF_GE:
250 cond = kCondGe;
251 break;
252 case Instruction::IF_GT:
253 cond = kCondGt;
254 break;
255 case Instruction::IF_LE:
256 cond = kCondLe;
257 break;
258 default:
259 cond = static_cast<ConditionCode>(0);
260 LOG(FATAL) << "Unexpected opcode " << opcode;
261 }
262
263 // Normalize such that if either operand is constant, src2 will be constant
264 if (rl_src1.is_const) {
265 RegLocation rl_temp = rl_src1;
266 rl_src1 = rl_src2;
267 rl_src2 = rl_temp;
268 cond = FlipComparisonOrder(cond);
269 }
270
buzbeea0cd2d72014-06-01 09:33:49 -0700271 rl_src1 = LoadValue(rl_src1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 // Is this really an immediate comparison?
273 if (rl_src2.is_const) {
274 // If it's already live in a register or not easily materialized, just keep going
275 RegLocation rl_temp = UpdateLoc(rl_src2);
276 if ((rl_temp.location == kLocDalvikFrame) &&
277 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
278 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800279 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 return;
281 }
282 }
buzbeea0cd2d72014-06-01 09:33:49 -0700283 rl_src2 = LoadValue(rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800284 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285}
286
287void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700288 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 ConditionCode cond;
buzbeea0cd2d72014-06-01 09:33:49 -0700290 DCHECK(!rl_src.fp);
291 rl_src = LoadValue(rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 switch (opcode) {
293 case Instruction::IF_EQZ:
294 cond = kCondEq;
295 break;
296 case Instruction::IF_NEZ:
297 cond = kCondNe;
298 break;
299 case Instruction::IF_LTZ:
300 cond = kCondLt;
301 break;
302 case Instruction::IF_GEZ:
303 cond = kCondGe;
304 break;
305 case Instruction::IF_GTZ:
306 cond = kCondGt;
307 break;
308 case Instruction::IF_LEZ:
309 cond = kCondLe;
310 break;
311 default:
312 cond = static_cast<ConditionCode>(0);
313 LOG(FATAL) << "Unexpected opcode " << opcode;
314 }
buzbee2700f7e2014-03-07 09:46:20 -0800315 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316}
317
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700318void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
320 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800321 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800323 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 }
buzbee2700f7e2014-03-07 09:46:20 -0800325 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 StoreValueWide(rl_dest, rl_result);
327}
328
329void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700331 rl_src = LoadValue(rl_src, kCoreReg);
332 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
333 OpKind op = kOpInvalid;
334 switch (opcode) {
335 case Instruction::INT_TO_BYTE:
336 op = kOp2Byte;
337 break;
338 case Instruction::INT_TO_SHORT:
339 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700341 case Instruction::INT_TO_CHAR:
342 op = kOp2Char;
343 break;
344 default:
345 LOG(ERROR) << "Bad int conversion type";
346 }
buzbee2700f7e2014-03-07 09:46:20 -0800347 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700348 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349}
350
Andreas Gampe2f244e92014-05-08 03:35:25 -0700351template <size_t pointer_size>
352static void GenNewArrayImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu,
353 uint32_t type_idx, RegLocation rl_dest,
354 RegLocation rl_src) {
355 mir_to_lir->FlushAllRegs(); /* Everything to home location */
356 ThreadOffset<pointer_size> func_offset(-1);
357 const DexFile* dex_file = cu->dex_file;
358 CompilerDriver* driver = cu->compiler_driver;
359 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *dex_file,
360 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800361 bool is_type_initialized; // Ignored as an array does not have an initializer.
362 bool use_direct_type_ptr;
363 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700364 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800365 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700366 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
367 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800368 // The fast path.
369 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700370 mir_to_lir->LoadClassType(type_idx, kArg0);
371 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700372 mir_to_lir->CallRuntimeHelperRegMethodRegLocation(func_offset, mir_to_lir->TargetReg(kArg0, false),
Andreas Gampe2f244e92014-05-08 03:35:25 -0700373 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800374 } else {
375 // Use the direct pointer.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700376 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayResolved);
377 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src,
378 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800379 }
380 } else {
381 // The slow path.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700382 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArray);
383 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800384 }
385 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700387 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocArrayWithAccessCheck);
388 mir_to_lir->CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 }
buzbeea0cd2d72014-06-01 09:33:49 -0700390 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700391 mir_to_lir->StoreValue(rl_dest, rl_result);
392}
393
394/*
395 * Let helper function take care of everything. Will call
396 * Array::AllocFromCode(type_idx, method, count);
397 * Note: AllocFromCode will handle checks for errNegativeArraySize.
398 */
399void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
400 RegLocation rl_src) {
buzbee33ae5582014-06-12 14:56:32 -0700401 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700402 GenNewArrayImpl<8>(this, cu_, type_idx, rl_dest, rl_src);
403 } else {
404 GenNewArrayImpl<4>(this, cu_, type_idx, rl_dest, rl_src);
405 }
406}
407
408template <size_t pointer_size>
409static void GenFilledNewArrayCall(Mir2Lir* mir_to_lir, CompilationUnit* cu, int elems, int type_idx) {
410 ThreadOffset<pointer_size> func_offset(-1);
411 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx, *cu->dex_file,
412 type_idx)) {
413 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArray);
414 } else {
415 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pCheckAndAllocArrayWithAccessCheck);
416 }
417 mir_to_lir->CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418}
419
420/*
421 * Similar to GenNewArray, but with post-allocation initialization.
422 * Verifier guarantees we're dealing with an array class. Current
423 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
424 * Current code also throws internal unimp if not 'L', '[' or 'I'.
425 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 int elems = info->num_arg_words;
428 int type_idx = info->index;
429 FlushAllRegs(); /* Everything to home location */
buzbee33ae5582014-06-12 14:56:32 -0700430 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700431 GenFilledNewArrayCall<8>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700433 GenFilledNewArrayCall<4>(this, cu_, elems, type_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700435 FreeTemp(TargetReg(kArg2, false));
436 FreeTemp(TargetReg(kArg1, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 /*
438 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
439 * return region. Because AllocFromCode placed the new array
440 * in kRet0, we'll just lock it into place. When debugger support is
441 * added, it may be necessary to additionally copy all return
442 * values to a home location in thread-local storage
443 */
Chao-ying Fua77ee512014-07-01 17:43:41 -0700444 RegStorage ref_reg = TargetRefReg(kRet0);
445 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446
447 // TODO: use the correct component size, currently all supported types
448 // share array alignment with ints (see comment at head of function)
449 size_t component_size = sizeof(int32_t);
450
451 // Having a range of 0 is legal
452 if (info->is_range && (elems > 0)) {
453 /*
454 * Bit of ugliness here. We're going generate a mem copy loop
455 * on the register range, but it is possible that some regs
456 * in the range have been promoted. This is unlikely, but
457 * before generating the copy, we'll just force a flush
458 * of any regs in the source range that have been promoted to
459 * home location.
460 */
461 for (int i = 0; i < elems; i++) {
462 RegLocation loc = UpdateLoc(info->args[i]);
463 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100464 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700465 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 }
468 /*
469 * TUNING note: generated code here could be much improved, but
470 * this is an uncommon operation and isn't especially performance
471 * critical.
472 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700473 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700474 RegStorage r_src = AllocTempRef();
475 RegStorage r_dst = AllocTempRef();
476 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800477 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700478 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700480 case kArm64:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700481 r_val = TargetReg(kLr, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 break;
483 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700484 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700485 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 r_val = AllocTemp();
487 break;
488 case kMips:
489 r_val = AllocTemp();
490 break;
491 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
492 }
493 // Set up source pointer
494 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700495 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700497 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 mirror::Array::DataOffset(component_size).Int32Value());
499 // Set up the loop counter (known to be > 0)
500 LoadConstant(r_idx, elems - 1);
501 // Generate the copy loop. Going backwards for convenience
502 LIR* target = NewLIR0(kPseudoTargetLabel);
503 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100504 {
505 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
506 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
507 // NOTE: No dalvik register annotation, local optimizations will be stopped
508 // by the loop boundaries.
509 }
buzbee695d13a2014-04-19 13:32:20 -0700510 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 FreeTemp(r_val);
512 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700513 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700515 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 -mirror::Array::DataOffset(component_size).Int32Value());
517 }
518 } else if (!info->is_range) {
519 // TUNING: interleave
520 for (int i = 0; i < elems; i++) {
521 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700522 Store32Disp(ref_reg,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000523 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800525 if (IsTemp(rl_arg.reg)) {
526 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 }
528 }
529 }
530 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700531 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 }
533}
534
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800535//
536// Slow path to ensure a class is initialized for sget/sput.
537//
538class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
539 public:
buzbee2700f7e2014-03-07 09:46:20 -0800540 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
541 RegStorage r_base) :
542 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
543 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544 }
545
546 void Compile() {
547 LIR* unresolved_target = GenerateTargetLabel();
548 uninit_->target = unresolved_target;
buzbee33ae5582014-06-12 14:56:32 -0700549 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700550 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeStaticStorage),
551 storage_index_, true);
552 } else {
553 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
554 storage_index_, true);
555 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800556 // Copy helper's result into r_base, a no-op on all but MIPS.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700557 m2l_->OpRegCopy(r_base_, m2l_->TargetRefReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800558
559 m2l_->OpUnconditionalBranch(cont_);
560 }
561
562 private:
563 LIR* const uninit_;
564 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800565 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800566};
567
Andreas Gampe2f244e92014-05-08 03:35:25 -0700568template <size_t pointer_size>
569static void GenSputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
570 const MirSFieldLoweringInfo* field_info, RegLocation rl_src) {
571 ThreadOffset<pointer_size> setter_offset =
572 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Static)
573 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjStatic)
574 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Static));
575 mir_to_lir->CallRuntimeHelperImmRegLocation(setter_offset, field_info->FieldIndex(), rl_src,
576 true);
577}
578
Vladimir Markobe0e5462014-02-26 11:24:15 +0000579void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700580 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000581 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
582 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100583 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
584 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
585 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000586 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800587 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100590 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700591 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000592 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
593 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800594 if (IsTemp(rl_method.reg)) {
595 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
597 } else {
598 // Medium path, static storage base in a different class which requires checks that the other
599 // class is initialized.
600 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000601 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 // May do runtime call so everything to home locations.
603 FlushAllRegs();
604 // Using fixed register to sync with possible call to runtime support.
Andreas Gampe4b537a82014-06-30 22:24:53 -0700605 RegStorage r_method = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LockTemp(r_method);
607 LoadCurrMethodDirect(r_method);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700608 r_base = TargetRefReg(kArg0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000610 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
611 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000612 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000613 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000615 if (!field_info.IsInitialized() &&
616 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800617 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800618
619 // The slow path is invoked if the r_base is NULL or the class pointed
620 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800621 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700622 RegStorage r_tmp = TargetReg(kArg2, false);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800623 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800624 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800625 mirror::Class::StatusOffset().Int32Value(),
Dave Allison7fb36de2014-07-10 02:05:10 +0000626 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800627 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800628
buzbee2700f7e2014-03-07 09:46:20 -0800629 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000630 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800631
632 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700633 // Ensure load of status and load of value don't re-order.
634 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 FreeTemp(r_method);
637 }
638 // rBase now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100639 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100641 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100643 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000645 if (is_object) {
646 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
647 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100648 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000649 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, store_size,
650 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 }
652 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800653 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800655 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 } else {
657 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700658 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700659 GenSputCall<8>(this, is_long_or_double, is_object, &field_info, rl_src);
660 } else {
661 GenSputCall<4>(this, is_long_or_double, is_object, &field_info, rl_src);
662 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 }
664}
665
Andreas Gampe2f244e92014-05-08 03:35:25 -0700666template <size_t pointer_size>
667static void GenSgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
668 const MirSFieldLoweringInfo* field_info) {
669 ThreadOffset<pointer_size> getter_offset =
670 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Static)
671 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjStatic)
672 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Static));
673 mir_to_lir->CallRuntimeHelperImm(getter_offset, field_info->FieldIndex(), true);
674}
675
Vladimir Markobe0e5462014-02-26 11:24:15 +0000676void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700677 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000678 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
679 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Vladimir Marko674744e2014-04-24 15:18:26 +0100680 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
681 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
682 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000683 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800684 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000685 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 // Fast path, static storage base is this method's class
687 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700688 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000689 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
690 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 } else {
692 // Medium path, static storage base in a different class which requires checks that the other
693 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 // May do runtime call so everything to home locations.
696 FlushAllRegs();
697 // Using fixed register to sync with possible call to runtime support.
Chao-ying Fua77ee512014-07-01 17:43:41 -0700698 RegStorage r_method = TargetRefReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 LockTemp(r_method);
700 LoadCurrMethodDirect(r_method);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700701 r_base = TargetRefReg(kArg0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800702 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000703 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
704 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000705 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000706 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800707 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000708 if (!field_info.IsInitialized() &&
709 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800710 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800711
712 // The slow path is invoked if the r_base is NULL or the class pointed
713 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800714 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700715 RegStorage r_tmp = TargetReg(kArg2, false);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800716 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800717 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800718 mirror::Class::StatusOffset().Int32Value(),
Dave Allison7fb36de2014-07-10 02:05:10 +0000719 mirror::Class::kStatusInitialized, nullptr, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800720 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800721
buzbee2700f7e2014-03-07 09:46:20 -0800722 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000723 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800724
725 FreeTemp(r_tmp);
Ian Rogers03dbc042014-06-02 14:24:56 -0700726 // Ensure load of status and load of value don't re-order.
727 GenMemBarrier(kLoadLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 FreeTemp(r_method);
730 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800731 // r_base now holds static storage base
Vladimir Marko674744e2014-04-24 15:18:26 +0100732 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
733 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800734
Vladimir Marko674744e2014-04-24 15:18:26 +0100735 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000736 if (is_object) {
737 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
738 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100739 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000740 LoadBaseDisp(r_base, field_offset, rl_result.reg, load_size, field_info.IsVolatile() ?
741 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800742 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100743 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800744
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 if (is_long_or_double) {
746 StoreValueWide(rl_dest, rl_result);
747 } else {
748 StoreValue(rl_dest, rl_result);
749 }
750 } else {
751 FlushAllRegs(); // Everything to home locations
buzbee33ae5582014-06-12 14:56:32 -0700752 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700753 GenSgetCall<8>(this, is_long_or_double, is_object, &field_info);
754 } else {
755 GenSgetCall<4>(this, is_long_or_double, is_object, &field_info);
756 }
Douglas Leung2db3e262014-06-25 16:02:55 -0700757 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 if (is_long_or_double) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700759 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 StoreValueWide(rl_dest, rl_result);
761 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700762 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 StoreValue(rl_dest, rl_result);
764 }
765 }
766}
767
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800768// Generate code for all slow paths.
769void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700770 // We should check slow_paths_.Size() every time, because a new slow path
771 // may be created during slowpath->Compile().
772 for (size_t i = 0; i < slow_paths_.Size(); ++i) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800773 LIRSlowPath* slowpath = slow_paths_.Get(i);
774 slowpath->Compile();
775 }
776 slow_paths_.Reset();
777}
778
Andreas Gampe2f244e92014-05-08 03:35:25 -0700779template <size_t pointer_size>
780static void GenIgetCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
781 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj) {
782 ThreadOffset<pointer_size> getter_offset =
783 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet64Instance)
784 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pGetObjInstance)
785 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pGet32Instance));
786 mir_to_lir->CallRuntimeHelperImmRegLocation(getter_offset, field_info->FieldIndex(), rl_obj,
787 true);
788}
789
Vladimir Markobe0e5462014-02-26 11:24:15 +0000790void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700792 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000793 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
794 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Vladimir Marko674744e2014-04-24 15:18:26 +0100795 OpSize load_size = LoadStoreOpSize(is_long_or_double, is_object);
796 if (!SLOW_FIELD_PATH && field_info.FastGet() &&
797 (!field_info.IsVolatile() || SupportsVolatileLoadStore(load_size))) {
798 RegisterClass reg_class = RegClassForFieldLoadStore(load_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000799 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700800 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100801 GenNullCheck(rl_obj.reg, opt_flags);
802 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
803 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000804 LIR* load_lir;
805 if (is_object) {
806 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
807 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100808 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000809 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, load_size,
810 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100811 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000812 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 StoreValueWide(rl_dest, rl_result);
815 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 StoreValue(rl_dest, rl_result);
817 }
818 } else {
buzbee33ae5582014-06-12 14:56:32 -0700819 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700820 GenIgetCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj);
821 } else {
822 GenIgetCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj);
823 }
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700824 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 if (is_long_or_double) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700826 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 StoreValueWide(rl_dest, rl_result);
828 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700829 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 StoreValue(rl_dest, rl_result);
831 }
832 }
833}
834
Andreas Gampe2f244e92014-05-08 03:35:25 -0700835template <size_t pointer_size>
836static void GenIputCall(Mir2Lir* mir_to_lir, bool is_long_or_double, bool is_object,
837 const MirIFieldLoweringInfo* field_info, RegLocation rl_obj,
838 RegLocation rl_src) {
839 ThreadOffset<pointer_size> setter_offset =
840 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet64Instance)
841 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pSetObjInstance)
842 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pSet32Instance));
843 mir_to_lir->CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info->FieldIndex(),
844 rl_obj, rl_src, true);
845}
846
Vladimir Markobe0e5462014-02-26 11:24:15 +0000847void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700849 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000850 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
851 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Vladimir Marko674744e2014-04-24 15:18:26 +0100852 OpSize store_size = LoadStoreOpSize(is_long_or_double, is_object);
853 if (!SLOW_FIELD_PATH && field_info.FastPut() &&
854 (!field_info.IsVolatile() || SupportsVolatileLoadStore(store_size))) {
855 RegisterClass reg_class = RegClassForFieldLoadStore(store_size, field_info.IsVolatile());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000856 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700857 rl_obj = LoadValue(rl_obj, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 if (is_long_or_double) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100859 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 } else {
861 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100862 }
863 GenNullCheck(rl_obj.reg, opt_flags);
864 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000865 LIR* store;
866 if (is_object) {
867 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
868 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100869 } else {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000870 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, store_size,
871 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100872 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000873 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Vladimir Marko674744e2014-04-24 15:18:26 +0100874 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
875 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 }
877 } else {
buzbee33ae5582014-06-12 14:56:32 -0700878 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700879 GenIputCall<8>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
880 } else {
881 GenIputCall<4>(this, is_long_or_double, is_object, &field_info, rl_obj, rl_src);
882 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 }
884}
885
Andreas Gampe2f244e92014-05-08 03:35:25 -0700886template <size_t pointer_size>
887static void GenArrayObjPutCall(Mir2Lir* mir_to_lir, bool needs_range_check, bool needs_null_check,
888 RegLocation rl_array, RegLocation rl_index, RegLocation rl_src) {
889 ThreadOffset<pointer_size> helper = needs_range_check
890 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithNullAndBoundCheck)
891 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObjectWithBoundCheck))
892 : QUICK_ENTRYPOINT_OFFSET(pointer_size, pAputObject);
893 mir_to_lir->CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src,
894 true);
895}
896
Ian Rogersa9a82542013-10-04 11:17:26 -0700897void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
898 RegLocation rl_src) {
899 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
900 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
901 (opt_flags & MIR_IGNORE_NULL_CHECK));
buzbee33ae5582014-06-12 14:56:32 -0700902 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700903 GenArrayObjPutCall<8>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
904 } else {
905 GenArrayObjPutCall<4>(this, needs_range_check, needs_null_check, rl_array, rl_index, rl_src);
906 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700907}
908
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700911 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700912 RegStorage res_reg = AllocTempRef();
buzbeea0cd2d72014-06-01 09:33:49 -0700913 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700915 *cu_->dex_file,
916 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 // Call out to helper which resolves type and verifies access.
918 // Resolved type returned in kRet0.
buzbee33ae5582014-06-12 14:56:32 -0700919 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700920 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
921 type_idx, rl_method.reg, true);
922 } else {
923 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
924 type_idx, rl_method.reg, true);
925 }
buzbeea0cd2d72014-06-01 09:33:49 -0700926 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 StoreValue(rl_dest, rl_result);
928 } else {
929 // We're don't need access checks, load type from dex cache
930 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700931 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000932 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000933 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000934 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
936 type_idx) || SLOW_TYPE_PATH) {
937 // Slow path, at runtime test if type is null and if so initialize
938 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800939 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800940 LIR* cont = NewLIR0(kPseudoTargetLabel);
941
942 // Object to generate the slow path for class resolution.
943 class SlowPath : public LIRSlowPath {
944 public:
945 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
946 const RegLocation& rl_method, const RegLocation& rl_result) :
947 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
948 rl_method_(rl_method), rl_result_(rl_result) {
949 }
950
951 void Compile() {
952 GenerateTargetLabel();
953
buzbee33ae5582014-06-12 14:56:32 -0700954 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700955 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
956 rl_method_.reg, true);
957 } else {
958 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
959 rl_method_.reg, true);
960 }
Chao-ying Fua77ee512014-07-01 17:43:41 -0700961 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetRefReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800962
963 m2l_->OpUnconditionalBranch(cont_);
964 }
965
966 private:
967 const int type_idx_;
968 const RegLocation rl_method_;
969 const RegLocation rl_result_;
970 };
971
972 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800973 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800974
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800976 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 // Fast path, we're done - just store result
978 StoreValue(rl_dest, rl_result);
979 }
980 }
981}
982
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700983void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000985 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
986 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
988 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
989 // slow path, resolve string if not in dex cache
990 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700991 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800992
993 // If the Method* is already in a register, we can save a copy.
994 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800995 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800996 if (rl_method.location == kLocPhysReg) {
997 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800998 DCHECK(!IsTemp(rl_method.reg));
999 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001000 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001001 r_method = TargetRefReg(kArg2);
Mark Mendell766e9292014-01-27 07:55:47 -08001002 LoadCurrMethodDirect(r_method);
1003 }
buzbee695d13a2014-04-19 13:32:20 -07001004 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampe4b537a82014-06-30 22:24:53 -07001005 TargetRefReg(kArg0), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001006
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampe4b537a82014-06-30 22:24:53 -07001008 LoadRefDisp(TargetRefReg(kArg0), offset_of_string, TargetRefReg(kRet0), kNotVolatile);
1009 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetRefReg(kRet0), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001010 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001011
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001012 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001013 // Object to generate the slow path for string resolution.
1014 class SlowPath : public LIRSlowPath {
1015 public:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001016 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method, int32_t string_idx) :
1017 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont),
1018 r_method_(r_method), string_idx_(string_idx) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001019 }
1020
1021 void Compile() {
1022 GenerateTargetLabel();
buzbee33ae5582014-06-12 14:56:32 -07001023 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001024 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pResolveString),
1025 r_method_, string_idx_, true);
1026 } else {
1027 m2l_->CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pResolveString),
1028 r_method_, string_idx_, true);
1029 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001030 m2l_->OpUnconditionalBranch(cont_);
1031 }
1032
1033 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001034 const RegStorage r_method_;
1035 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001036 };
1037
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001038 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001040
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001042 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 } else {
1044 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001045 RegStorage res_reg = AllocTempRef();
1046 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001047 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1048 kNotVolatile);
1049 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 StoreValue(rl_dest, rl_result);
1051 }
1052}
1053
Andreas Gampe2f244e92014-05-08 03:35:25 -07001054template <size_t pointer_size>
1055static void GenNewInstanceImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, uint32_t type_idx,
1056 RegLocation rl_dest) {
1057 mir_to_lir->FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 // alloc will always check for resolution, do we also need to verify
1059 // access because the verifier was unable to?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001060 ThreadOffset<pointer_size> func_offset(-1);
1061 const DexFile* dex_file = cu->dex_file;
1062 CompilerDriver* driver = cu->compiler_driver;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001063 if (driver->CanAccessInstantiableTypeWithoutChecks(
Andreas Gampe2f244e92014-05-08 03:35:25 -07001064 cu->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001065 bool is_type_initialized;
1066 bool use_direct_type_ptr;
1067 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001068 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001069 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001070 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1071 &direct_type_ptr, &is_finalizable) &&
1072 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001073 // The fast path.
1074 if (!use_direct_type_ptr) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001075 mir_to_lir->LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001076 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001077 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001078 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetRefReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001079 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001080 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001081 mir_to_lir->CallRuntimeHelperRegMethod(func_offset, mir_to_lir->TargetRefReg(kArg0), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001082 }
1083 } else {
1084 // Use the direct pointer.
1085 if (!is_type_initialized) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001086 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectResolved);
1087 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001088 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001089 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectInitialized);
1090 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 }
1092 }
1093 } else {
1094 // The slow path.
1095 DCHECK_EQ(func_offset.Int32Value(), -1);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001096 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObject);
1097 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001098 }
1099 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001101 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pAllocObjectWithAccessCheck);
1102 mir_to_lir->CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 }
buzbeea0cd2d72014-06-01 09:33:49 -07001104 RegLocation rl_result = mir_to_lir->GetReturn(kRefReg);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001105 mir_to_lir->StoreValue(rl_dest, rl_result);
1106}
1107
1108/*
1109 * Let helper function take care of everything. Will
1110 * call Class::NewInstanceFromCode(type_idx, method);
1111 */
1112void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
buzbee33ae5582014-06-12 14:56:32 -07001113 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001114 GenNewInstanceImpl<8>(this, cu_, type_idx, rl_dest);
1115 } else {
1116 GenNewInstanceImpl<4>(this, cu_, type_idx, rl_dest);
1117 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118}
1119
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001120void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07001122 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001123 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pDeliverException), rl_src, true);
1124 } else {
1125 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
1126 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127}
1128
1129// For final classes there are no sub-classes to check and so we can answer the instance-of
1130// question with simple comparisons.
1131void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1132 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001133 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001134 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001135
buzbeea0cd2d72014-06-01 09:33:49 -07001136 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001138 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001139 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001141 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 }
1143 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001144 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145
buzbeea0cd2d72014-06-01 09:33:49 -07001146 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1147 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148
1149 LoadCurrMethodDirect(check_class);
1150 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001151 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1152 kNotVolatile);
1153 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1154 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 } else {
buzbee695d13a2014-04-19 13:32:20 -07001156 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001157 check_class, kNotVolatile);
1158 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1159 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001160 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001161 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 }
1163
1164 LIR* ne_branchover = NULL;
buzbee695d13a2014-04-19 13:32:20 -07001165 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 if (cu_->instruction_set == kThumb2) {
1167 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001168 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001170 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 } else {
1172 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1173 LoadConstant(result_reg, 1); // eq case - load true
1174 }
1175 LIR* target = NewLIR0(kPseudoTargetLabel);
1176 null_branchover->target = target;
1177 if (ne_branchover != NULL) {
1178 ne_branchover->target = target;
1179 }
1180 FreeTemp(object_class);
1181 FreeTemp(check_class);
1182 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001183 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001184 FreeTemp(result_reg);
1185 }
1186 StoreValue(rl_dest, rl_result);
1187}
1188
1189void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1190 bool type_known_abstract, bool use_declaring_class,
1191 bool can_assume_type_is_in_dex_cache,
1192 uint32_t type_idx, RegLocation rl_dest,
1193 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001194 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001195 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001196
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 FlushAllRegs();
1198 // May generate a call - use explicit registers
1199 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001200 RegStorage method_reg = TargetRefReg(kArg1);
1201 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1202 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 if (needs_access_check) {
1204 // Check we have access to type_idx and if not throw IllegalAccessError,
1205 // returns Class* in kArg0
buzbee33ae5582014-06-12 14:56:32 -07001206 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001207 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1208 type_idx, true);
1209 } else {
1210 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1211 type_idx, true);
1212 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001213 OpRegCopy(class_reg, TargetRefReg(kRet0)); // Align usage with fast path
1214 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 } else if (use_declaring_class) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001216 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001217 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001218 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 } else {
1220 // Load dex cache entry into class_reg (kArg2)
Chao-ying Fua77ee512014-07-01 17:43:41 -07001221 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001222 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001223 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001224 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001225 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 if (!can_assume_type_is_in_dex_cache) {
1227 // Need to test presence of type in dex cache at runtime
1228 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1229 // Not resolved
1230 // Call out to helper, which will return resolved type in kRet0
buzbee33ae5582014-06-12 14:56:32 -07001231 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001232 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx, true);
1233 } else {
1234 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
1235 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001236 OpRegCopy(TargetRefReg(kArg2), TargetRefReg(kRet0)); // Align usage with fast path
Chao-ying Fua77ee512014-07-01 17:43:41 -07001237 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); /* reload Ref */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 // Rejoin code paths
1239 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1240 hop_branch->target = hop_target;
1241 }
1242 }
1243 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001244 RegLocation rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 if (cu_->instruction_set == kMips) {
1246 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001247 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001249 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetRefReg(kArg0), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250
1251 /* load object->klass_ */
1252 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001253 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetRefReg(kArg1),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001254 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1256 LIR* branchover = NULL;
1257 if (type_known_final) {
1258 // rl_result == ref == null == 0.
1259 if (cu_->instruction_set == kThumb2) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001260 OpRegReg(kOpCmp, TargetRefReg(kArg1), TargetRefReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001261 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001262 LoadConstant(rl_result.reg, 1); // .eq case - load true
1263 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001264 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001266 LoadConstant(rl_result.reg, 0); // ne case - load false
Chao-ying Fua77ee512014-07-01 17:43:41 -07001267 branchover = OpCmpBranch(kCondNe, TargetRefReg(kArg1), TargetRefReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001268 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 }
1270 } else {
1271 if (cu_->instruction_set == kThumb2) {
buzbee33ae5582014-06-12 14:56:32 -07001272 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001273 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1274 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001275 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 if (!type_known_abstract) {
1277 /* Uses conditional nullification */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001278 OpRegReg(kOpCmp, TargetRefReg(kArg1), TargetRefReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001279 it = OpIT(kCondEq, "EE"); // if-convert the test
Chao-ying Fua77ee512014-07-01 17:43:41 -07001280 LoadConstant(TargetReg(kArg0, false), 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 }
Chao-ying Fua77ee512014-07-01 17:43:41 -07001282 OpRegCopy(TargetRefReg(kArg0), TargetRefReg(kArg2)); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001284 if (it != nullptr) {
1285 OpEndIT(it);
1286 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 FreeTemp(r_tgt);
1288 } else {
1289 if (!type_known_abstract) {
1290 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001291 LoadConstant(rl_result.reg, 1); // assume true
Chao-ying Fua77ee512014-07-01 17:43:41 -07001292 branchover = OpCmpBranch(kCondEq, TargetRefReg(kArg1), TargetRefReg(kArg2), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
buzbee33ae5582014-06-12 14:56:32 -07001294 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001295 LoadHelper(QUICK_ENTRYPOINT_OFFSET(8, pInstanceofNonTrivial)) :
1296 LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Chao-ying Fua77ee512014-07-01 17:43:41 -07001297 OpRegCopy(TargetRefReg(kArg0), TargetRefReg(kArg2)); // .ne case - arg0 <= class
Mark Mendell6607d972014-02-10 06:54:18 -08001298 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1299 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
1301 }
1302 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001303 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 /* branch targets here */
1305 LIR* target = NewLIR0(kPseudoTargetLabel);
1306 StoreValue(rl_dest, rl_result);
1307 branch1->target = target;
1308 if (branchover != NULL) {
1309 branchover->target = target;
1310 }
1311}
1312
1313void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1314 bool type_known_final, type_known_abstract, use_declaring_class;
1315 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1316 *cu_->dex_file,
1317 type_idx,
1318 &type_known_final,
1319 &type_known_abstract,
1320 &use_declaring_class);
1321 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1322 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1323
1324 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1325 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1326 } else {
1327 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1328 use_declaring_class, can_assume_type_is_in_dex_cache,
1329 type_idx, rl_dest, rl_src);
1330 }
1331}
1332
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001333void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 bool type_known_final, type_known_abstract, use_declaring_class;
1335 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1336 *cu_->dex_file,
1337 type_idx,
1338 &type_known_final,
1339 &type_known_abstract,
1340 &use_declaring_class);
1341 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1342 // of the exception throw path.
1343 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001344 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 // Verifier type analysis proved this check cast would never cause an exception.
1346 return;
1347 }
1348 FlushAllRegs();
1349 // May generate a call - use explicit registers
1350 LockCallTemps();
Andreas Gampe4b537a82014-06-30 22:24:53 -07001351 RegStorage method_reg = TargetRefReg(kArg1);
1352 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
1353 RegStorage class_reg = TargetRefReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 if (needs_access_check) {
1355 // Check we have access to type_idx and if not throw IllegalAccessError,
1356 // returns Class* in kRet0
1357 // InitializeTypeAndVerifyAccess(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001358 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001359 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(8, pInitializeTypeAndVerifyAccess),
1360 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001361 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001362 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
1363 type_idx, true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001364 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001365 OpRegCopy(class_reg, TargetRefReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001367 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001368 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001369 } else {
1370 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001371 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001372 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001373 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001374 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1376 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001377 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1378 LIR* cont = NewLIR0(kPseudoTargetLabel);
1379
1380 // Slow path to initialize the type. Executed if the type is NULL.
1381 class SlowPath : public LIRSlowPath {
1382 public:
1383 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001384 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001385 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1386 class_reg_(class_reg) {
1387 }
1388
1389 void Compile() {
1390 GenerateTargetLabel();
1391
1392 // Call out to helper, which will return resolved type in kArg0
1393 // InitializeTypeFromCode(idx, method)
buzbee33ae5582014-06-12 14:56:32 -07001394 if (m2l_->cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001395 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(8, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001396 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001397 } else {
1398 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Andreas Gampe4b537a82014-06-30 22:24:53 -07001399 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001400 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001401 m2l_->OpRegCopy(class_reg_, m2l_->TargetRefReg(kRet0)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001402 m2l_->OpUnconditionalBranch(cont_);
1403 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001404
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001405 public:
1406 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001407 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001408 };
1409
buzbee2700f7e2014-03-07 09:46:20 -08001410 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 }
1412 }
1413 // At this point, class_reg (kArg2) has class
Andreas Gampe4b537a82014-06-30 22:24:53 -07001414 LoadValueDirectFixed(rl_src, TargetRefReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001415
1416 // Slow path for the case where the classes are not equal. In this case we need
1417 // to call a helper function to do the check.
1418 class SlowPath : public LIRSlowPath {
1419 public:
1420 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1421 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1422 }
1423
1424 void Compile() {
1425 GenerateTargetLabel();
1426
1427 if (load_) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001428 m2l_->LoadRefDisp(m2l_->TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1429 m2l_->TargetRefReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001430 }
buzbee33ae5582014-06-12 14:56:32 -07001431 if (m2l_->cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001432 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pCheckCast), m2l_->TargetRefReg(kArg2),
1433 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001434 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001435 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetRefReg(kArg2),
1436 m2l_->TargetRefReg(kArg1), true);
Andreas Gampe2f244e92014-05-08 03:35:25 -07001437 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001438
1439 m2l_->OpUnconditionalBranch(cont_);
1440 }
1441
1442 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001443 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001444 };
1445
1446 if (type_known_abstract) {
1447 // Easier case, run slow path if target is non-null (slow path will load from target)
Chao-ying Fua77ee512014-07-01 17:43:41 -07001448 LIR* branch = OpCmpImmBranch(kCondNe, TargetRefReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001449 LIR* cont = NewLIR0(kPseudoTargetLabel);
1450 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1451 } else {
1452 // Harder, more common case. We need to generate a forward branch over the load
1453 // if the target is null. If it's non-null we perform the load and branch to the
1454 // slow path if the classes are not equal.
1455
1456 /* Null is OK - continue */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001457 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetRefReg(kArg0), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001458 /* load object->klass_ */
1459 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001460 LoadRefDisp(TargetRefReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1461 TargetRefReg(kArg1), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001462
Andreas Gampe4b537a82014-06-30 22:24:53 -07001463 LIR* branch2 = OpCmpBranch(kCondNe, TargetRefReg(kArg1), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001464 LIR* cont = NewLIR0(kPseudoTargetLabel);
1465
1466 // Add the slow path that will not perform load since this is already done.
1467 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1468
1469 // Set the null check to branch to the continuation.
1470 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 }
1472}
1473
1474void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001475 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001476 RegLocation rl_result;
1477 if (cu_->instruction_set == kThumb2) {
1478 /*
1479 * NOTE: This is the one place in the code in which we might have
1480 * as many as six live temporary registers. There are 5 in the normal
1481 * set for Arm. Until we have spill capabilities, temporarily add
1482 * lr to the temp set. It is safe to do this locally, but note that
1483 * lr is used explicitly elsewhere in the code generator and cannot
1484 * normally be used as a general temp register.
1485 */
1486 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1487 FreeTemp(TargetReg(kLr)); // and make it available
1488 }
1489 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1490 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1491 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1492 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001493 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1494 RegStorage t_reg = AllocTemp();
1495 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1496 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1497 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 FreeTemp(t_reg);
1499 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001500 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1501 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 }
1503 /*
1504 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1505 * following StoreValueWide might need to allocate a temp register.
1506 * To further work around the lack of a spill capability, explicitly
1507 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1508 * Remove when spill is functional.
1509 */
1510 FreeRegLocTemps(rl_result, rl_src1);
1511 FreeRegLocTemps(rl_result, rl_src2);
1512 StoreValueWide(rl_dest, rl_result);
1513 if (cu_->instruction_set == kThumb2) {
1514 Clobber(TargetReg(kLr));
1515 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1516 }
1517}
1518
1519
Andreas Gampe2f244e92014-05-08 03:35:25 -07001520template <size_t pointer_size>
1521static void GenShiftOpLongCall(Mir2Lir* mir_to_lir, Instruction::Code opcode, RegLocation rl_src1,
1522 RegLocation rl_shift) {
1523 ThreadOffset<pointer_size> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524
1525 switch (opcode) {
1526 case Instruction::SHL_LONG:
1527 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001528 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 break;
1530 case Instruction::SHR_LONG:
1531 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001532 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 break;
1534 case Instruction::USHR_LONG:
1535 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07001536 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 break;
1538 default:
1539 LOG(FATAL) << "Unexpected case";
1540 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001541 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
1542 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1543}
1544
1545void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1546 RegLocation rl_src1, RegLocation rl_shift) {
buzbee33ae5582014-06-12 14:56:32 -07001547 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001548 GenShiftOpLongCall<8>(this, opcode, rl_src1, rl_shift);
1549 } else {
1550 GenShiftOpLongCall<4>(this, opcode, rl_src1, rl_shift);
1551 }
buzbeea0cd2d72014-06-01 09:33:49 -07001552 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 StoreValueWide(rl_dest, rl_result);
1554}
1555
1556
1557void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001558 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001559 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560 OpKind op = kOpBkpt;
1561 bool is_div_rem = false;
1562 bool check_zero = false;
1563 bool unary = false;
1564 RegLocation rl_result;
1565 bool shift_op = false;
1566 switch (opcode) {
1567 case Instruction::NEG_INT:
1568 op = kOpNeg;
1569 unary = true;
1570 break;
1571 case Instruction::NOT_INT:
1572 op = kOpMvn;
1573 unary = true;
1574 break;
1575 case Instruction::ADD_INT:
1576 case Instruction::ADD_INT_2ADDR:
1577 op = kOpAdd;
1578 break;
1579 case Instruction::SUB_INT:
1580 case Instruction::SUB_INT_2ADDR:
1581 op = kOpSub;
1582 break;
1583 case Instruction::MUL_INT:
1584 case Instruction::MUL_INT_2ADDR:
1585 op = kOpMul;
1586 break;
1587 case Instruction::DIV_INT:
1588 case Instruction::DIV_INT_2ADDR:
1589 check_zero = true;
1590 op = kOpDiv;
1591 is_div_rem = true;
1592 break;
1593 /* NOTE: returns in kArg1 */
1594 case Instruction::REM_INT:
1595 case Instruction::REM_INT_2ADDR:
1596 check_zero = true;
1597 op = kOpRem;
1598 is_div_rem = true;
1599 break;
1600 case Instruction::AND_INT:
1601 case Instruction::AND_INT_2ADDR:
1602 op = kOpAnd;
1603 break;
1604 case Instruction::OR_INT:
1605 case Instruction::OR_INT_2ADDR:
1606 op = kOpOr;
1607 break;
1608 case Instruction::XOR_INT:
1609 case Instruction::XOR_INT_2ADDR:
1610 op = kOpXor;
1611 break;
1612 case Instruction::SHL_INT:
1613 case Instruction::SHL_INT_2ADDR:
1614 shift_op = true;
1615 op = kOpLsl;
1616 break;
1617 case Instruction::SHR_INT:
1618 case Instruction::SHR_INT_2ADDR:
1619 shift_op = true;
1620 op = kOpAsr;
1621 break;
1622 case Instruction::USHR_INT:
1623 case Instruction::USHR_INT_2ADDR:
1624 shift_op = true;
1625 op = kOpLsr;
1626 break;
1627 default:
1628 LOG(FATAL) << "Invalid word arith op: " << opcode;
1629 }
1630 if (!is_div_rem) {
1631 if (unary) {
1632 rl_src1 = LoadValue(rl_src1, kCoreReg);
1633 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001634 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001635 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001636 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001637 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001638 RegStorage t_reg = AllocTemp();
1639 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 rl_src1 = LoadValue(rl_src1, kCoreReg);
1641 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001642 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 FreeTemp(t_reg);
1644 } else {
1645 rl_src1 = LoadValue(rl_src1, kCoreReg);
1646 rl_src2 = LoadValue(rl_src2, kCoreReg);
1647 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001648 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 }
1650 }
1651 StoreValue(rl_dest, rl_result);
1652 } else {
Dave Allison70202782013-10-22 17:52:19 -07001653 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 +01001654 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 rl_src1 = LoadValue(rl_src1, kCoreReg);
1656 rl_src2 = LoadValue(rl_src2, kCoreReg);
1657 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001658 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 }
buzbee2700f7e2014-03-07 09:46:20 -08001660 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001661 done = true;
1662 } else if (cu_->instruction_set == kThumb2) {
1663 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1664 // Use ARM SDIV instruction for division. For remainder we also need to
1665 // calculate using a MUL and subtract.
1666 rl_src1 = LoadValue(rl_src1, kCoreReg);
1667 rl_src2 = LoadValue(rl_src2, kCoreReg);
1668 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001669 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001670 }
buzbee2700f7e2014-03-07 09:46:20 -08001671 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001672 done = true;
1673 }
1674 }
1675
1676 // If we haven't already generated the code use the callout function.
1677 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 FlushAllRegs(); /* Send everything to home location */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001679 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, false));
buzbee33ae5582014-06-12 14:56:32 -07001680 RegStorage r_tgt = cu_->target64 ?
Andreas Gampe2f244e92014-05-08 03:35:25 -07001681 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod)) :
1682 CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod));
Chao-ying Fua77ee512014-07-01 17:43:41 -07001683 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 if (check_zero) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001685 GenDivZeroCheck(TargetReg(kArg1, false));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 }
Dave Allison70202782013-10-22 17:52:19 -07001687 // NOTE: callout here is not a safepoint.
buzbee33ae5582014-06-12 14:56:32 -07001688 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07001689 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), false /* not a safepoint */);
1690 } else {
1691 CallHelper(r_tgt, QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), false /* not a safepoint */);
1692 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001693 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001694 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 else
1696 rl_result = GetReturnAlt();
1697 }
1698 StoreValue(rl_dest, rl_result);
1699 }
1700}
1701
1702/*
1703 * The following are the first-level codegen routines that analyze the format
1704 * of each bytecode then either dispatch special purpose codegen routines
1705 * or produce corresponding Thumb instructions directly.
1706 */
1707
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001709static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 x &= x - 1;
1711 return (x & (x - 1)) == 0;
1712}
1713
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1715// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001716bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001717 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1719 return false;
1720 }
1721 // No divide instruction for Arm, so check for more special cases
1722 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001723 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 }
1725 int k = LowestSetBit(lit);
1726 if (k >= 30) {
1727 // Avoid special cases.
1728 return false;
1729 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 rl_src = LoadValue(rl_src, kCoreReg);
1731 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001732 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001733 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 if (lit == 2) {
1735 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001736 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1737 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1738 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001740 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001741 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001742 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1743 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 }
1745 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001746 RegStorage t_reg1 = AllocTemp();
1747 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001748 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001749 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1750 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001751 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001752 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001754 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001756 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001758 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 }
1760 }
1761 StoreValue(rl_dest, rl_result);
1762 return true;
1763}
1764
1765// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1766// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001767bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001768 if (lit < 0) {
1769 return false;
1770 }
1771 if (lit == 0) {
1772 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1773 LoadConstant(rl_result.reg, 0);
1774 StoreValue(rl_dest, rl_result);
1775 return true;
1776 }
1777 if (lit == 1) {
1778 rl_src = LoadValue(rl_src, kCoreReg);
1779 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1780 OpRegCopy(rl_result.reg, rl_src.reg);
1781 StoreValue(rl_dest, rl_result);
1782 return true;
1783 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001784 // There is RegRegRegShift on Arm, so check for more special cases
1785 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001786 return EasyMultiply(rl_src, rl_dest, lit);
1787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001788 // Can we simplify this multiplication?
1789 bool power_of_two = false;
1790 bool pop_count_le2 = false;
1791 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001792 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 power_of_two = true;
1794 } else if (IsPopCountLE2(lit)) {
1795 pop_count_le2 = true;
1796 } else if (IsPowerOfTwo(lit + 1)) {
1797 power_of_two_minus_one = true;
1798 } else {
1799 return false;
1800 }
1801 rl_src = LoadValue(rl_src, kCoreReg);
1802 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1803 if (power_of_two) {
1804 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001805 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 } else if (pop_count_le2) {
1807 // Shift and add and shift.
1808 int first_bit = LowestSetBit(lit);
1809 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1810 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1811 } else {
1812 // Reverse subtract: (src << (shift + 1)) - src.
1813 DCHECK(power_of_two_minus_one);
1814 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001815 RegStorage t_reg = AllocTemp();
1816 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1817 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 }
1819 StoreValue(rl_dest, rl_result);
1820 return true;
1821}
1822
1823void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001824 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001825 RegLocation rl_result;
1826 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1827 int shift_op = false;
1828 bool is_div = false;
1829
1830 switch (opcode) {
1831 case Instruction::RSUB_INT_LIT8:
1832 case Instruction::RSUB_INT: {
1833 rl_src = LoadValue(rl_src, kCoreReg);
1834 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1835 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001836 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001837 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001838 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1839 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 }
1841 StoreValue(rl_dest, rl_result);
1842 return;
1843 }
1844
1845 case Instruction::SUB_INT:
1846 case Instruction::SUB_INT_2ADDR:
1847 lit = -lit;
1848 // Intended fallthrough
1849 case Instruction::ADD_INT:
1850 case Instruction::ADD_INT_2ADDR:
1851 case Instruction::ADD_INT_LIT8:
1852 case Instruction::ADD_INT_LIT16:
1853 op = kOpAdd;
1854 break;
1855 case Instruction::MUL_INT:
1856 case Instruction::MUL_INT_2ADDR:
1857 case Instruction::MUL_INT_LIT8:
1858 case Instruction::MUL_INT_LIT16: {
1859 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1860 return;
1861 }
1862 op = kOpMul;
1863 break;
1864 }
1865 case Instruction::AND_INT:
1866 case Instruction::AND_INT_2ADDR:
1867 case Instruction::AND_INT_LIT8:
1868 case Instruction::AND_INT_LIT16:
1869 op = kOpAnd;
1870 break;
1871 case Instruction::OR_INT:
1872 case Instruction::OR_INT_2ADDR:
1873 case Instruction::OR_INT_LIT8:
1874 case Instruction::OR_INT_LIT16:
1875 op = kOpOr;
1876 break;
1877 case Instruction::XOR_INT:
1878 case Instruction::XOR_INT_2ADDR:
1879 case Instruction::XOR_INT_LIT8:
1880 case Instruction::XOR_INT_LIT16:
1881 op = kOpXor;
1882 break;
1883 case Instruction::SHL_INT_LIT8:
1884 case Instruction::SHL_INT:
1885 case Instruction::SHL_INT_2ADDR:
1886 lit &= 31;
1887 shift_op = true;
1888 op = kOpLsl;
1889 break;
1890 case Instruction::SHR_INT_LIT8:
1891 case Instruction::SHR_INT:
1892 case Instruction::SHR_INT_2ADDR:
1893 lit &= 31;
1894 shift_op = true;
1895 op = kOpAsr;
1896 break;
1897 case Instruction::USHR_INT_LIT8:
1898 case Instruction::USHR_INT:
1899 case Instruction::USHR_INT_2ADDR:
1900 lit &= 31;
1901 shift_op = true;
1902 op = kOpLsr;
1903 break;
1904
1905 case Instruction::DIV_INT:
1906 case Instruction::DIV_INT_2ADDR:
1907 case Instruction::DIV_INT_LIT8:
1908 case Instruction::DIV_INT_LIT16:
1909 case Instruction::REM_INT:
1910 case Instruction::REM_INT_2ADDR:
1911 case Instruction::REM_INT_LIT8:
1912 case Instruction::REM_INT_LIT16: {
1913 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001914 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915 return;
1916 }
buzbee11b63d12013-08-27 07:34:17 -07001917 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001918 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001919 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 (opcode == Instruction::DIV_INT_LIT16)) {
1921 is_div = true;
1922 } else {
1923 is_div = false;
1924 }
buzbee11b63d12013-08-27 07:34:17 -07001925 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1926 return;
1927 }
Dave Allison70202782013-10-22 17:52:19 -07001928
1929 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001930 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001931 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001932 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001933 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001934 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001935 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1936 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001937 } else if (cu_->instruction_set == kThumb2) {
1938 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1939 // Use ARM SDIV instruction for division. For remainder we also need to
1940 // calculate using a MUL and subtract.
1941 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001942 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001943 done = true;
1944 }
1945 }
1946
1947 if (!done) {
1948 FlushAllRegs(); /* Everything to home location. */
Chao-ying Fua77ee512014-07-01 17:43:41 -07001949 LoadValueDirectFixed(rl_src, TargetReg(kArg0, false));
1950 Clobber(TargetReg(kArg0, false));
buzbee33ae5582014-06-12 14:56:32 -07001951 if (cu_->target64) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001952 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(8, pIdivmod), TargetReg(kArg0, false), lit,
Andreas Gampe2f244e92014-05-08 03:35:25 -07001953 false);
1954 } else {
Chao-ying Fua77ee512014-07-01 17:43:41 -07001955 CallRuntimeHelperRegImm(QUICK_ENTRYPOINT_OFFSET(4, pIdivmod), TargetReg(kArg0, false), lit,
Andreas Gampe2f244e92014-05-08 03:35:25 -07001956 false);
1957 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001958 if (is_div)
buzbeea0cd2d72014-06-01 09:33:49 -07001959 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 else
1961 rl_result = GetReturnAlt();
1962 }
1963 StoreValue(rl_dest, rl_result);
1964 return;
1965 }
1966 default:
1967 LOG(FATAL) << "Unexpected opcode " << opcode;
1968 }
1969 rl_src = LoadValue(rl_src, kCoreReg);
1970 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001971 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001972 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001973 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001975 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 }
1977 StoreValue(rl_dest, rl_result);
1978}
1979
Andreas Gampe2f244e92014-05-08 03:35:25 -07001980template <size_t pointer_size>
1981static void GenArithOpLongImpl(Mir2Lir* mir_to_lir, CompilationUnit* cu, Instruction::Code opcode,
1982 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001983 RegLocation rl_result;
1984 OpKind first_op = kOpBkpt;
1985 OpKind second_op = kOpBkpt;
1986 bool call_out = false;
1987 bool check_zero = false;
Andreas Gampe2f244e92014-05-08 03:35:25 -07001988 ThreadOffset<pointer_size> func_offset(-1);
Chao-ying Fua77ee512014-07-01 17:43:41 -07001989 int ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990
1991 switch (opcode) {
1992 case Instruction::NOT_LONG:
Chao-ying Fua0147762014-06-06 18:38:49 -07001993 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001994 mir_to_lir->GenNotLong(rl_dest, rl_src2);
1995 return;
1996 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001997 rl_src2 = mir_to_lir->LoadValueWide(rl_src2, kCoreReg);
1998 rl_result = mir_to_lir->EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08002000 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002001 RegStorage t_reg = mir_to_lir->AllocTemp();
2002 mir_to_lir->OpRegCopy(t_reg, rl_src2.reg.GetHigh());
2003 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2004 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2005 mir_to_lir->FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002007 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2008 mir_to_lir->OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07002010 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 return;
2012 case Instruction::ADD_LONG:
2013 case Instruction::ADD_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002014 if (cu->instruction_set != kThumb2) {
2015 mir_to_lir->GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016 return;
2017 }
2018 first_op = kOpAdd;
2019 second_op = kOpAdc;
2020 break;
2021 case Instruction::SUB_LONG:
2022 case Instruction::SUB_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002023 if (cu->instruction_set != kThumb2) {
2024 mir_to_lir->GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002025 return;
2026 }
2027 first_op = kOpSub;
2028 second_op = kOpSbc;
2029 break;
2030 case Instruction::MUL_LONG:
2031 case Instruction::MUL_LONG_2ADDR:
Andreas Gampe2f244e92014-05-08 03:35:25 -07002032 if (cu->instruction_set != kMips) {
2033 mir_to_lir->GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034 return;
2035 } else {
2036 call_out = true;
Chao-ying Fua77ee512014-07-01 17:43:41 -07002037 ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002038 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002039 }
2040 break;
2041 case Instruction::DIV_LONG:
2042 case Instruction::DIV_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002043 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002044 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true);
2045 return;
2046 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002047 call_out = true;
2048 check_zero = true;
Chao-ying Fua77ee512014-07-01 17:43:41 -07002049 ret_reg = mir_to_lir->TargetReg(kRet0, false).GetReg();
Andreas Gampe2f244e92014-05-08 03:35:25 -07002050 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 break;
2052 case Instruction::REM_LONG:
2053 case Instruction::REM_LONG_2ADDR:
Chao-ying Fua0147762014-06-06 18:38:49 -07002054 if (cu->instruction_set == kArm64 || cu->instruction_set == kX86_64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002055 mir_to_lir->GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false);
2056 return;
2057 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002058 call_out = true;
2059 check_zero = true;
Andreas Gampe2f244e92014-05-08 03:35:25 -07002060 func_offset = QUICK_ENTRYPOINT_OFFSET(pointer_size, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Chao-ying Fua77ee512014-07-01 17:43:41 -07002062 ret_reg = (cu->instruction_set == kThumb2) ? mir_to_lir->TargetReg(kArg2, false).GetReg() :
2063 mir_to_lir->TargetReg(kRet0, false).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002064 break;
2065 case Instruction::AND_LONG_2ADDR:
2066 case Instruction::AND_LONG:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002067 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2068 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002069 return mir_to_lir->GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 }
2071 first_op = kOpAnd;
2072 second_op = kOpAnd;
2073 break;
2074 case Instruction::OR_LONG:
2075 case Instruction::OR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002076 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2077 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002078 mir_to_lir->GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002079 return;
2080 }
2081 first_op = kOpOr;
2082 second_op = kOpOr;
2083 break;
2084 case Instruction::XOR_LONG:
2085 case Instruction::XOR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01002086 if (cu->instruction_set == kX86 || cu->instruction_set == kX86_64 ||
2087 cu->instruction_set == kArm64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002088 mir_to_lir->GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002089 return;
2090 }
2091 first_op = kOpXor;
2092 second_op = kOpXor;
2093 break;
2094 case Instruction::NEG_LONG: {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002095 mir_to_lir->GenNegLong(rl_dest, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002096 return;
2097 }
2098 default:
2099 LOG(FATAL) << "Invalid long arith op";
2100 }
2101 if (!call_out) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002102 mir_to_lir->GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002103 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002104 mir_to_lir->FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 if (check_zero) {
Chao-ying Fua77ee512014-07-01 17:43:41 -07002106 RegStorage r_tmp1 = mir_to_lir->TargetReg(kArg0, kArg1);
2107 RegStorage r_tmp2 = mir_to_lir->TargetReg(kArg2, kArg3);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002108 mir_to_lir->LoadValueDirectWideFixed(rl_src2, r_tmp2);
2109 RegStorage r_tgt = mir_to_lir->CallHelperSetup(func_offset);
Chao-ying Fua77ee512014-07-01 17:43:41 -07002110 mir_to_lir->GenDivZeroCheckWide(mir_to_lir->TargetReg(kArg2, kArg3));
Andreas Gampe2f244e92014-05-08 03:35:25 -07002111 mir_to_lir->LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002112 // NOTE: callout here is not a safepoint
Andreas Gampe2f244e92014-05-08 03:35:25 -07002113 mir_to_lir->CallHelper(r_tgt, func_offset, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002114 } else {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002115 mir_to_lir->CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002116 }
2117 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Chao-ying Fua77ee512014-07-01 17:43:41 -07002118 if (ret_reg == mir_to_lir->TargetReg(kRet0, false).GetReg())
buzbeea0cd2d72014-06-01 09:33:49 -07002119 rl_result = mir_to_lir->GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002120 else
Andreas Gampe2f244e92014-05-08 03:35:25 -07002121 rl_result = mir_to_lir->GetReturnWideAlt();
2122 mir_to_lir->StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002123 }
2124}
2125
Andreas Gampe2f244e92014-05-08 03:35:25 -07002126void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
2127 RegLocation rl_src1, RegLocation rl_src2) {
buzbee33ae5582014-06-12 14:56:32 -07002128 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002129 GenArithOpLongImpl<8>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2130 } else {
2131 GenArithOpLongImpl<4>(this, cu_, opcode, rl_dest, rl_src1, rl_src2);
2132 }
2133}
2134
Mark Mendelle87f9b52014-04-30 14:13:18 -04002135void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2136 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2137 LoadConstantNoClobber(rl_result.reg, value);
2138 StoreValue(rl_dest, rl_result);
2139 if (value == 0) {
2140 Workaround7250540(rl_dest, rl_result.reg);
2141 }
2142}
2143
Andreas Gampe2f244e92014-05-08 03:35:25 -07002144template <size_t pointer_size>
2145void Mir2Lir::GenConversionCall(ThreadOffset<pointer_size> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002146 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002147 /*
2148 * Don't optimize the register usage since it calls out to support
2149 * functions
2150 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002151 DCHECK_EQ(pointer_size, GetInstructionSetPointerSize(cu_->instruction_set));
2152
Brian Carlstrom7940e442013-07-12 13:46:57 -07002153 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002154 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2155 if (rl_dest.wide) {
2156 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002157 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002158 StoreValueWide(rl_dest, rl_result);
2159 } else {
2160 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002161 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002162 StoreValue(rl_dest, rl_result);
2163 }
2164}
Andreas Gampe2f244e92014-05-08 03:35:25 -07002165template void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
2166 RegLocation rl_dest, RegLocation rl_src);
2167template void Mir2Lir::GenConversionCall(ThreadOffset<8> func_offset,
2168 RegLocation rl_dest, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002169
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002170class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2171 public:
2172 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2173 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2174 }
2175
2176 void Compile() OVERRIDE {
2177 m2l_->ResetRegPool();
2178 m2l_->ResetDefTracking();
2179 GenerateTargetLabel(kPseudoSuspendTarget);
buzbee33ae5582014-06-12 14:56:32 -07002180 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002181 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(8, pTestSuspend), true);
2182 } else {
2183 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend), true);
2184 }
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002185 if (cont_ != nullptr) {
2186 m2l_->OpUnconditionalBranch(cont_);
2187 }
2188 }
2189};
2190
Brian Carlstrom7940e442013-07-12 13:46:57 -07002191/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002192void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison7fb36de2014-07-10 02:05:10 +00002193 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002194 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2195 return;
2196 }
2197 FlushAllRegs();
2198 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002199 LIR* cont = NewLIR0(kPseudoTargetLabel);
2200 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002201 } else {
2202 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2203 return;
2204 }
2205 FlushAllRegs(); // TODO: needed?
2206 LIR* inst = CheckSuspendUsingLoad();
2207 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002208 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002209}
2210
2211/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002212void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison7fb36de2014-07-10 02:05:10 +00002213 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002214 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2215 OpUnconditionalBranch(target);
2216 return;
2217 }
2218 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002219 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002220 LIR* branch = OpUnconditionalBranch(nullptr);
2221 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002222 } else {
2223 // For the implicit suspend check, just perform the trigger
2224 // load and branch to the target.
2225 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2226 OpUnconditionalBranch(target);
2227 return;
2228 }
2229 FlushAllRegs();
2230 LIR* inst = CheckSuspendUsingLoad();
2231 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002232 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002233 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002234}
2235
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002236/* Call out to helper assembly routine that will null check obj and then lock it. */
2237void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2238 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002239 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002240 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pLockObject), rl_src, true);
2241 } else {
2242 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
2243 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002244}
2245
2246/* Call out to helper assembly routine that will null check obj and then unlock it. */
2247void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2248 FlushAllRegs();
buzbee33ae5582014-06-12 14:56:32 -07002249 if (cu_->target64) {
Andreas Gampe2f244e92014-05-08 03:35:25 -07002250 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject), rl_src, true);
2251 } else {
2252 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
2253 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002254}
2255
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002256/* Generic code for generating a wide constant into a VR. */
2257void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2258 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002259 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002260 StoreValueWide(rl_dest, rl_result);
2261}
2262
Brian Carlstrom7940e442013-07-12 13:46:57 -07002263} // namespace art