blob: ee523f3f5b3136e961375caffaf7f04ac969ba86 [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"
Andreas Gampeaa910d52014-07-30 18:59:05 -070024#include "mirror/object_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080026#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29
Andreas Gampe9c3b0892014-04-24 17:33:34 +000030// Shortcuts to repeatedly used long types.
31typedef mirror::ObjectArray<mirror::Object> ObjArray;
32typedef mirror::ObjectArray<mirror::Class> ClassArray;
33
Brian Carlstrom7940e442013-07-12 13:46:57 -070034/*
35 * This source files contains "gen" codegen routines that should
36 * be applicable to most targets. Only mid-level support utilities
37 * and "op" calls may be used here.
38 */
39
40/*
buzbeeb48819d2013-09-14 16:15:25 -070041 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 * blocks.
43 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070044void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 LIR* barrier = NewLIR0(kPseudoBarrier);
46 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070047 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010048 barrier->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070049}
50
Mingyao Yange643a172014-04-08 11:02:52 -070051void Mir2Lir::GenDivZeroException() {
52 LIR* branch = OpUnconditionalBranch(nullptr);
53 AddDivZeroCheckSlowPath(branch);
54}
55
56void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070057 LIR* branch = OpCondBranch(c_code, nullptr);
58 AddDivZeroCheckSlowPath(branch);
59}
60
Mingyao Yange643a172014-04-08 11:02:52 -070061void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
62 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070063 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
67 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
68 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080069 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch_in)
70 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in) {
Mingyao Yang42894562014-04-07 12:42:16 -070071 }
72
Mingyao Yange643a172014-04-08 11:02:52 -070073 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070074 m2l_->ResetRegPool();
75 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070076 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070077 m2l_->CallRuntimeHelper(kQuickThrowDivZero, true);
Mingyao Yang42894562014-04-07 12:42:16 -070078 }
79 };
80
81 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
82}
Dave Allisonb373e092014-02-20 16:06:36 -080083
Mingyao Yang80365d92014-04-18 12:10:58 -070084void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
85 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
86 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -080087 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, RegStorage index_in,
88 RegStorage length_in)
89 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
90 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -070091 }
92
93 void Compile() OVERRIDE {
94 m2l_->ResetRegPool();
95 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -070096 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -070097 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, index_, length_, true);
Mingyao Yang80365d92014-04-18 12:10:58 -070098 }
99
100 private:
101 const RegStorage index_;
102 const RegStorage length_;
103 };
104
105 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
106 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
107}
108
109void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
110 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
111 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800112 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch_in, int index_in, RegStorage length_in)
113 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch_in),
114 index_(index_in), length_(length_in) {
Mingyao Yang80365d92014-04-18 12:10:58 -0700115 }
116
117 void Compile() OVERRIDE {
118 m2l_->ResetRegPool();
119 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700120 GenerateTargetLabel(kPseudoThrowTarget);
Mingyao Yang80365d92014-04-18 12:10:58 -0700121
Andreas Gampeccc60262014-07-04 18:02:38 -0700122 RegStorage arg1_32 = m2l_->TargetReg(kArg1, kNotWide);
123 RegStorage arg0_32 = m2l_->TargetReg(kArg0, kNotWide);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700124
125 m2l_->OpRegCopy(arg1_32, length_);
126 m2l_->LoadConstant(arg0_32, index_);
Andreas Gampe98430592014-07-27 19:44:50 -0700127 m2l_->CallRuntimeHelperRegReg(kQuickThrowArrayBounds, arg0_32, arg1_32, true);
Mingyao Yang80365d92014-04-18 12:10:58 -0700128 }
129
130 private:
131 const int32_t index_;
132 const RegStorage length_;
133 };
134
135 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
136 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
137}
138
Mingyao Yange643a172014-04-08 11:02:52 -0700139LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
140 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
141 public:
142 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
143 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
144 }
145
146 void Compile() OVERRIDE {
147 m2l_->ResetRegPool();
148 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700149 GenerateTargetLabel(kPseudoThrowTarget);
Andreas Gampe98430592014-07-27 19:44:50 -0700150 m2l_->CallRuntimeHelper(kQuickThrowNullPointer, true);
Mingyao Yange643a172014-04-08 11:02:52 -0700151 }
152 };
153
154 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
155 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
156 return branch;
157}
158
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800160LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000161 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700162 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 }
Dave Allisonb373e092014-02-20 16:06:36 -0800164 return nullptr;
165}
166
Dave Allisonf9439142014-03-27 15:10:22 -0700167/* Perform an explicit null-check on a register. */
168LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
169 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
170 return NULL;
171 }
Mingyao Yange643a172014-04-08 11:02:52 -0700172 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700173}
174
Dave Allisonb373e092014-02-20 16:06:36 -0800175void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000176 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800177 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
178 return;
179 }
Dave Allison69dfe512014-07-11 17:11:58 +0000180 // Insert after last instruction.
Dave Allisonb373e092014-02-20 16:06:36 -0800181 MarkSafepointPC(last_lir_insn_);
182 }
183}
184
Andreas Gampe3c12c512014-06-24 18:46:29 +0000185void Mir2Lir::MarkPossibleNullPointerExceptionAfter(int opt_flags, LIR* after) {
Dave Allison69dfe512014-07-11 17:11:58 +0000186 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000187 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
188 return;
189 }
190 MarkSafepointPCAfter(after);
191 }
192}
193
Dave Allisonb373e092014-02-20 16:06:36 -0800194void Mir2Lir::MarkPossibleStackOverflowException() {
Dave Allison69dfe512014-07-11 17:11:58 +0000195 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800196 MarkSafepointPC(last_lir_insn_);
197 }
198}
199
buzbee2700f7e2014-03-07 09:46:20 -0800200void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +0000201 if (cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -0800202 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
203 return;
204 }
205 // Force an implicit null check by performing a memory operation (load) from the given
206 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800207 RegStorage tmp = AllocTemp();
208 // TODO: for Mips, would be best to use rZERO as the bogus register target.
buzbee695d13a2014-04-19 13:32:20 -0700209 LIR* load = Load32Disp(reg, 0, tmp);
Dave Allisonb373e092014-02-20 16:06:36 -0800210 FreeTemp(tmp);
211 MarkSafepointPC(load);
212 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213}
214
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700216 RegLocation rl_src2, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700218 RegisterClass reg_class = (rl_src1.ref || rl_src2.ref) ? kRefReg : kCoreReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 switch (opcode) {
220 case Instruction::IF_EQ:
221 cond = kCondEq;
222 break;
223 case Instruction::IF_NE:
224 cond = kCondNe;
225 break;
226 case Instruction::IF_LT:
227 cond = kCondLt;
228 break;
229 case Instruction::IF_GE:
230 cond = kCondGe;
231 break;
232 case Instruction::IF_GT:
233 cond = kCondGt;
234 break;
235 case Instruction::IF_LE:
236 cond = kCondLe;
237 break;
238 default:
239 cond = static_cast<ConditionCode>(0);
240 LOG(FATAL) << "Unexpected opcode " << opcode;
241 }
242
243 // Normalize such that if either operand is constant, src2 will be constant
244 if (rl_src1.is_const) {
245 RegLocation rl_temp = rl_src1;
246 rl_src1 = rl_src2;
247 rl_src2 = rl_temp;
248 cond = FlipComparisonOrder(cond);
249 }
250
buzbee7c02e912014-10-03 13:14:17 -0700251 rl_src1 = LoadValue(rl_src1, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 // Is this really an immediate comparison?
253 if (rl_src2.is_const) {
254 // If it's already live in a register or not easily materialized, just keep going
255 RegLocation rl_temp = UpdateLoc(rl_src2);
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700256 int32_t constant_value = mir_graph_->ConstantValue(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if ((rl_temp.location == kLocDalvikFrame) &&
Matteo Franchinc763e352014-07-04 12:53:27 +0100258 InexpensiveConstantInt(constant_value, opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800260 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 return;
262 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700263
264 // It's also commonly more efficient to have a test against zero with Eq/Ne. This is not worse
265 // for x86, and allows a cbz/cbnz for Arm and Mips. At the same time, it works around a register
266 // mismatch for 64b systems, where a reference is compared against null, as dex bytecode uses
267 // the 32b literal 0 for null.
268 if (constant_value == 0 && (cond == kCondEq || cond == kCondNe)) {
269 // Use the OpCmpImmBranch and ignore the value in the register.
270 OpCmpImmBranch(cond, rl_src1.reg, 0, taken);
271 return;
272 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 }
Andreas Gampeb07c1f92014-07-26 01:40:39 -0700274
buzbee7c02e912014-10-03 13:14:17 -0700275 rl_src2 = LoadValue(rl_src2, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800276 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277}
278
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700279void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 ConditionCode cond;
buzbee7c02e912014-10-03 13:14:17 -0700281 RegisterClass reg_class = rl_src.ref ? kRefReg : kCoreReg;
282 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 switch (opcode) {
284 case Instruction::IF_EQZ:
285 cond = kCondEq;
286 break;
287 case Instruction::IF_NEZ:
288 cond = kCondNe;
289 break;
290 case Instruction::IF_LTZ:
291 cond = kCondLt;
292 break;
293 case Instruction::IF_GEZ:
294 cond = kCondGe;
295 break;
296 case Instruction::IF_GTZ:
297 cond = kCondGt;
298 break;
299 case Instruction::IF_LEZ:
300 cond = kCondLe;
301 break;
302 default:
303 cond = static_cast<ConditionCode>(0);
304 LOG(FATAL) << "Unexpected opcode " << opcode;
305 }
buzbee2700f7e2014-03-07 09:46:20 -0800306 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307}
308
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700309void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
311 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800312 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800314 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 }
buzbee2700f7e2014-03-07 09:46:20 -0800316 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 StoreValueWide(rl_dest, rl_result);
318}
319
320void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700321 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700322 rl_src = LoadValue(rl_src, kCoreReg);
323 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
324 OpKind op = kOpInvalid;
325 switch (opcode) {
326 case Instruction::INT_TO_BYTE:
327 op = kOp2Byte;
328 break;
329 case Instruction::INT_TO_SHORT:
330 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700332 case Instruction::INT_TO_CHAR:
333 op = kOp2Char;
334 break;
335 default:
336 LOG(ERROR) << "Bad int conversion type";
337 }
buzbee2700f7e2014-03-07 09:46:20 -0800338 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700339 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340}
341
Andreas Gampe98430592014-07-27 19:44:50 -0700342/*
343 * Let helper function take care of everything. Will call
344 * Array::AllocFromCode(type_idx, method, count);
345 * Note: AllocFromCode will handle checks for errNegativeArraySize.
346 */
347void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
348 RegLocation rl_src) {
349 FlushAllRegs(); /* Everything to home location */
350 const DexFile* dex_file = cu_->dex_file;
351 CompilerDriver* driver = cu_->compiler_driver;
352 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800353 bool is_type_initialized; // Ignored as an array does not have an initializer.
354 bool use_direct_type_ptr;
355 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700356 bool is_finalizable;
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800357 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700358 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
359 &direct_type_ptr, &is_finalizable)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800360 // The fast path.
361 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -0700362 LoadClassType(*dex_file, type_idx, kArg0);
Andreas Gampe98430592014-07-27 19:44:50 -0700363 CallRuntimeHelperRegMethodRegLocation(kQuickAllocArrayResolved, TargetReg(kArg0, kNotWide),
364 rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800365 } else {
366 // Use the direct pointer.
Andreas Gampe98430592014-07-27 19:44:50 -0700367 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayResolved, direct_type_ptr, rl_src,
368 true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800369 }
370 } else {
371 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -0700372 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArray, type_idx, rl_src, true);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800373 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700375 CallRuntimeHelperImmMethodRegLocation(kQuickAllocArrayWithAccessCheck, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 }
Andreas Gampe98430592014-07-27 19:44:50 -0700377 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378}
379
380/*
381 * Similar to GenNewArray, but with post-allocation initialization.
382 * Verifier guarantees we're dealing with an array class. Current
383 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
384 * Current code also throws internal unimp if not 'L', '[' or 'I'.
385 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700386void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 int elems = info->num_arg_words;
388 int type_idx = info->index;
389 FlushAllRegs(); /* Everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -0700390 QuickEntrypointEnum target;
391 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
392 type_idx)) {
393 target = kQuickCheckAndAllocArray;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 } else {
Andreas Gampe98430592014-07-27 19:44:50 -0700395 target = kQuickCheckAndAllocArrayWithAccessCheck;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
Andreas Gampe98430592014-07-27 19:44:50 -0700397 CallRuntimeHelperImmMethodImm(target, type_idx, elems, true);
Andreas Gampeccc60262014-07-04 18:02:38 -0700398 FreeTemp(TargetReg(kArg2, kNotWide));
399 FreeTemp(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 /*
401 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
402 * return region. Because AllocFromCode placed the new array
403 * in kRet0, we'll just lock it into place. When debugger support is
404 * added, it may be necessary to additionally copy all return
405 * values to a home location in thread-local storage
406 */
Andreas Gampeccc60262014-07-04 18:02:38 -0700407 RegStorage ref_reg = TargetReg(kRet0, kRef);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700408 LockTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409
410 // TODO: use the correct component size, currently all supported types
411 // share array alignment with ints (see comment at head of function)
412 size_t component_size = sizeof(int32_t);
413
414 // Having a range of 0 is legal
415 if (info->is_range && (elems > 0)) {
416 /*
417 * Bit of ugliness here. We're going generate a mem copy loop
418 * on the register range, but it is possible that some regs
419 * in the range have been promoted. This is unlikely, but
420 * before generating the copy, we'll just force a flush
421 * of any regs in the source range that have been promoted to
422 * home location.
423 */
424 for (int i = 0; i < elems; i++) {
425 RegLocation loc = UpdateLoc(info->args[i]);
426 if (loc.location == kLocPhysReg) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100427 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Serguei Katkov27503542014-11-06 14:45:44 +0600428 if (loc.ref) {
429 StoreRefDisp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kNotVolatile);
430 } else {
431 Store32Disp(TargetPtrReg(kSp), SRegOffset(loc.s_reg_low), loc.reg);
432 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 }
435 /*
436 * TUNING note: generated code here could be much improved, but
437 * this is an uncommon operation and isn't especially performance
438 * critical.
439 */
Chao-ying Fu7e399fd2014-06-10 18:11:11 -0700440 // This is addressing the stack, which may be out of the 4G area.
buzbee33ae5582014-06-12 14:56:32 -0700441 RegStorage r_src = AllocTempRef();
442 RegStorage r_dst = AllocTempRef();
443 RegStorage r_idx = AllocTempRef(); // Not really a reference, but match src/dst.
buzbee2700f7e2014-03-07 09:46:20 -0800444 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700445 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 case kThumb2:
buzbee33ae5582014-06-12 14:56:32 -0700447 case kArm64:
Andreas Gampeccc60262014-07-04 18:02:38 -0700448 r_val = TargetReg(kLr, kNotWide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 break;
450 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700451 case kX86_64:
Chao-ying Fua77ee512014-07-01 17:43:41 -0700452 FreeTemp(ref_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 r_val = AllocTemp();
454 break;
455 case kMips:
456 r_val = AllocTemp();
457 break;
458 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
459 }
460 // Set up source pointer
461 RegLocation rl_first = info->args[0];
Chao-ying Fua77ee512014-07-01 17:43:41 -0700462 OpRegRegImm(kOpAdd, r_src, TargetPtrReg(kSp), SRegOffset(rl_first.s_reg_low));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 // Set up the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700464 OpRegRegImm(kOpAdd, r_dst, ref_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 mirror::Array::DataOffset(component_size).Int32Value());
466 // Set up the loop counter (known to be > 0)
467 LoadConstant(r_idx, elems - 1);
468 // Generate the copy loop. Going backwards for convenience
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800469 LIR* loop_head_target = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 // Copy next element
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100471 {
472 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
473 LoadBaseIndexed(r_src, r_idx, r_val, 2, k32);
474 // NOTE: No dalvik register annotation, local optimizations will be stopped
475 // by the loop boundaries.
476 }
buzbee695d13a2014-04-19 13:32:20 -0700477 StoreBaseIndexed(r_dst, r_idx, r_val, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 FreeTemp(r_val);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800479 OpDecAndBranch(kCondGe, r_idx, loop_head_target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700480 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 // Restore the target pointer
Chao-ying Fua77ee512014-07-01 17:43:41 -0700482 OpRegRegImm(kOpAdd, ref_reg, r_dst,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 -mirror::Array::DataOffset(component_size).Int32Value());
484 }
485 } else if (!info->is_range) {
486 // TUNING: interleave
487 for (int i = 0; i < elems; i++) {
Serguei Katkov27503542014-11-06 14:45:44 +0600488 RegLocation rl_arg;
489 if (info->args[i].ref) {
490 rl_arg = LoadValue(info->args[i], kRefReg);
491 StoreRefDisp(ref_reg,
492 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg,
493 kNotVolatile);
494 } else {
495 rl_arg = LoadValue(info->args[i], kCoreReg);
496 Store32Disp(ref_reg,
497 mirror::Array::DataOffset(component_size).Int32Value() + i * 4, rl_arg.reg);
498 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800500 if (IsTemp(rl_arg.reg)) {
501 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 }
503 }
504 }
505 if (info->result.location != kLocInvalid) {
buzbeea0cd2d72014-06-01 09:33:49 -0700506 StoreValue(info->result, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 }
508}
509
Ian Rogers832336b2014-10-08 15:35:22 -0700510/*
511 * Array data table format:
512 * ushort ident = 0x0300 magic value
513 * ushort width width of each element in the table
514 * uint size number of elements in the table
515 * ubyte data[size*width] table of data values (may contain a single-byte
516 * padding at the end)
517 *
518 * Total size is 4+(width * size + 1)/2 16-bit code units.
519 */
520void Mir2Lir::GenFillArrayData(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
521 if (kIsDebugBuild) {
522 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
523 const Instruction::ArrayDataPayload* payload =
524 reinterpret_cast<const Instruction::ArrayDataPayload*>(table);
525 CHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
526 }
527 uint32_t table_offset_from_start = mir->offset + static_cast<int32_t>(table_offset);
528 CallRuntimeHelperImmRegLocation(kQuickHandleFillArrayData, table_offset_from_start, rl_src, true);
529}
530
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800531//
532// Slow path to ensure a class is initialized for sget/sput.
533//
534class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
535 public:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100536 // There are up to two branches to the static field slow path, the "unresolved" when the type
537 // entry in the dex cache is null, and the "uninit" when the class is not yet initialized.
538 // At least one will be non-null here, otherwise we wouldn't generate the slow path.
buzbee2700f7e2014-03-07 09:46:20 -0800539 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100540 RegStorage r_base)
541 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved != nullptr ? unresolved : uninit, cont),
542 second_branch_(unresolved != nullptr ? uninit : nullptr),
543 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800544 }
545
546 void Compile() {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100547 LIR* target = GenerateTargetLabel();
548 if (second_branch_ != nullptr) {
549 second_branch_->target = target;
550 }
Andreas Gampe98430592014-07-27 19:44:50 -0700551 m2l_->CallRuntimeHelperImm(kQuickInitializeStaticStorage, storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800552 // Copy helper's result into r_base, a no-op on all but MIPS.
Andreas Gampeccc60262014-07-04 18:02:38 -0700553 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800554
555 m2l_->OpUnconditionalBranch(cont_);
556 }
557
558 private:
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100559 // Second branch to the slow path, or null if there's only one branch.
560 LIR* const second_branch_;
561
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800562 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800563 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800564};
565
Fred Shih37f05ef2014-07-16 18:38:08 -0700566void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, OpSize size) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000567 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
568 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700569 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000570 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800571 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000572 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 // Fast path, static storage base is this method's class
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100574 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700575 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000576 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
577 kNotVolatile);
buzbee2700f7e2014-03-07 09:46:20 -0800578 if (IsTemp(rl_method.reg)) {
579 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 }
581 } else {
582 // Medium path, static storage base in a different class which requires checks that the other
583 // class is initialized.
584 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 // May do runtime call so everything to home locations.
587 FlushAllRegs();
588 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700589 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 LockTemp(r_method);
591 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700592 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800593 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000594 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
595 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000596 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000597 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800598 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100599 LIR* unresolved_branch = nullptr;
600 if (!field_info.IsClassInDexCache() &&
601 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
602 // Check if r_base is NULL.
603 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
604 }
605 LIR* uninit_branch = nullptr;
606 if (!field_info.IsClassInitialized() &&
607 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
608 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700609 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800610 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100611 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800612 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000613 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100614 FreeTemp(r_tmp);
615 }
616 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
617 // The slow path is invoked if the r_base is NULL or the class pointed
618 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800619 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800620 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000621 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100623 if (uninit_branch != nullptr) {
624 // Ensure load of status and store of value don't re-order.
625 // TODO: Presumably the actual value store is control-dependent on the status load,
626 // and will thus not be reordered in any case, since stores are never speculated.
627 // Does later code "know" that the class is now initialized? If so, we still
628 // need the barrier to guard later static loads.
629 GenMemBarrier(kLoadAny);
630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 FreeTemp(r_method);
633 }
634 // rBase now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700635 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
636 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100637 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 } else {
Vladimir Marko674744e2014-04-24 15:18:26 +0100639 rl_src = LoadValue(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700641 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000642 StoreRefDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg,
643 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100644 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700645 StoreBaseDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000646 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700648 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800649 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800651 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 } else {
653 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700654 QuickEntrypointEnum target;
655 switch (size) {
656 case kReference:
657 target = kQuickSetObjStatic;
658 break;
659 case k64:
660 case kDouble:
661 target = kQuickSet64Static;
662 break;
663 case k32:
664 case kSingle:
665 target = kQuickSet32Static;
666 break;
667 case kSignedHalf:
668 case kUnsignedHalf:
669 target = kQuickSet16Static;
670 break;
671 case kSignedByte:
672 case kUnsignedByte:
673 target = kQuickSet8Static;
674 break;
675 case kWord: // Intentional fallthrough.
676 default:
677 LOG(FATAL) << "Can't determine entrypoint for: " << size;
678 target = kQuickSet32Static;
679 }
Andreas Gampe98430592014-07-27 19:44:50 -0700680 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 }
682}
683
Fred Shih37f05ef2014-07-16 18:38:08 -0700684void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest, OpSize size, Primitive::Type type) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000685 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
686 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
Fred Shih37f05ef2014-07-16 18:38:08 -0700687
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700688 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800690 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000691 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 // Fast path, static storage base is this method's class
693 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -0700694 r_base = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000695 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base,
696 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 } else {
698 // Medium path, static storage base in a different class which requires checks that the other
699 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000700 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 // May do runtime call so everything to home locations.
702 FlushAllRegs();
703 // Using fixed register to sync with possible call to runtime support.
Andreas Gampeccc60262014-07-04 18:02:38 -0700704 RegStorage r_method = TargetReg(kArg1, kRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 LockTemp(r_method);
706 LoadCurrMethodDirect(r_method);
Andreas Gampeccc60262014-07-04 18:02:38 -0700707 r_base = TargetReg(kArg0, kRef);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800708 LockTemp(r_base);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000709 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base,
710 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000711 int32_t offset_of_field = ObjArray::OffsetOfElement(field_info.StorageIndex()).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000712 LoadRefDisp(r_base, offset_of_field, r_base, kNotVolatile);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800713 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100714 LIR* unresolved_branch = nullptr;
715 if (!field_info.IsClassInDexCache() &&
716 (mir->optimization_flags & MIR_CLASS_IS_IN_DEX_CACHE) == 0) {
717 // Check if r_base is NULL.
718 unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
719 }
720 LIR* uninit_branch = nullptr;
721 if (!field_info.IsClassInitialized() &&
722 (mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0) {
723 // Check if r_base is not yet initialized class.
Andreas Gampeccc60262014-07-04 18:02:38 -0700724 RegStorage r_tmp = TargetReg(kArg2, kNotWide);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800725 LockTemp(r_tmp);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100726 uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800727 mirror::Class::StatusOffset().Int32Value(),
Dave Allison69dfe512014-07-11 17:11:58 +0000728 mirror::Class::kStatusInitialized, nullptr, nullptr);
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100729 FreeTemp(r_tmp);
730 }
731 if (unresolved_branch != nullptr || uninit_branch != nullptr) {
732 // The slow path is invoked if the r_base is NULL or the class pointed
733 // to by it is not initialized.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800734 LIR* cont = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800735 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000736 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800737
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100738 if (uninit_branch != nullptr) {
739 // Ensure load of status and load of value don't re-order.
740 GenMemBarrier(kLoadAny);
741 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 FreeTemp(r_method);
744 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800745 // r_base now holds static storage base
Fred Shih37f05ef2014-07-16 18:38:08 -0700746 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Vladimir Marko674744e2014-04-24 15:18:26 +0100747 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800748
Vladimir Marko674744e2014-04-24 15:18:26 +0100749 int field_offset = field_info.FieldOffset().Int32Value();
Fred Shih37f05ef2014-07-16 18:38:08 -0700750 if (IsRef(size)) {
751 // TODO: DCHECK?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000752 LoadRefDisp(r_base, field_offset, rl_result.reg, field_info.IsVolatile() ? kVolatile :
753 kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100754 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700755 LoadBaseDisp(r_base, field_offset, rl_result.reg, size, field_info.IsVolatile() ?
Andreas Gampe3c12c512014-06-24 18:46:29 +0000756 kVolatile : kNotVolatile);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800757 }
Vladimir Marko674744e2014-04-24 15:18:26 +0100758 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800759
Fred Shih37f05ef2014-07-16 18:38:08 -0700760 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 StoreValueWide(rl_dest, rl_result);
762 } else {
763 StoreValue(rl_dest, rl_result);
764 }
765 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700766 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 FlushAllRegs(); // Everything to home locations
Fred Shih37f05ef2014-07-16 18:38:08 -0700768 QuickEntrypointEnum target;
769 switch (type) {
770 case Primitive::kPrimNot:
771 target = kQuickGetObjStatic;
772 break;
773 case Primitive::kPrimLong:
774 case Primitive::kPrimDouble:
775 target = kQuickGet64Static;
776 break;
777 case Primitive::kPrimInt:
778 case Primitive::kPrimFloat:
779 target = kQuickGet32Static;
780 break;
781 case Primitive::kPrimShort:
782 target = kQuickGetShortStatic;
783 break;
784 case Primitive::kPrimChar:
785 target = kQuickGetCharStatic;
786 break;
787 case Primitive::kPrimByte:
788 target = kQuickGetByteStatic;
789 break;
790 case Primitive::kPrimBoolean:
791 target = kQuickGetBooleanStatic;
792 break;
793 case Primitive::kPrimVoid: // Intentional fallthrough.
794 default:
795 LOG(FATAL) << "Can't determine entrypoint for: " << type;
796 target = kQuickGet32Static;
797 }
Andreas Gampe98430592014-07-27 19:44:50 -0700798 CallRuntimeHelperImm(target, field_info.FieldIndex(), true);
799
Douglas Leung2db3e262014-06-25 16:02:55 -0700800 // FIXME: pGetXXStatic always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700801 if (IsWide(size)) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700802 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 StoreValueWide(rl_dest, rl_result);
804 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700805 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 StoreValue(rl_dest, rl_result);
807 }
808 }
809}
810
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800811// Generate code for all slow paths.
812void Mir2Lir::HandleSlowPaths() {
Chao-ying Fu8159af62014-07-07 17:13:52 -0700813 // We should check slow_paths_.Size() every time, because a new slow path
814 // may be created during slowpath->Compile().
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100815 for (LIRSlowPath* slowpath : slow_paths_) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800816 slowpath->Compile();
817 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100818 slow_paths_.clear();
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800819}
820
Fred Shih37f05ef2014-07-16 18:38:08 -0700821void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size, Primitive::Type type,
822 RegLocation rl_dest, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000823 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
824 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700825 if (!SLOW_FIELD_PATH && field_info.FastGet()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700826 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700827 // A load of the class will lead to an iget with offset 0.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000828 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbeea0cd2d72014-06-01 09:33:49 -0700829 rl_obj = LoadValue(rl_obj, kRefReg);
Vladimir Marko674744e2014-04-24 15:18:26 +0100830 GenNullCheck(rl_obj.reg, opt_flags);
831 RegLocation rl_result = EvalLoc(rl_dest, reg_class, true);
832 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000833 LIR* load_lir;
Fred Shih37f05ef2014-07-16 18:38:08 -0700834 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000835 load_lir = LoadRefDisp(rl_obj.reg, field_offset, rl_result.reg, field_info.IsVolatile() ?
836 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100837 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700838 load_lir = LoadBaseDisp(rl_obj.reg, field_offset, rl_result.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000839 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100840 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000841 MarkPossibleNullPointerExceptionAfter(opt_flags, load_lir);
Fred Shih37f05ef2014-07-16 18:38:08 -0700842 if (IsWide(size)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 StoreValueWide(rl_dest, rl_result);
844 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 StoreValue(rl_dest, rl_result);
846 }
847 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700848 DCHECK(SizeMatchesTypeForEntrypoint(size, type));
849 QuickEntrypointEnum target;
850 switch (type) {
851 case Primitive::kPrimNot:
852 target = kQuickGetObjInstance;
853 break;
854 case Primitive::kPrimLong:
855 case Primitive::kPrimDouble:
856 target = kQuickGet64Instance;
857 break;
858 case Primitive::kPrimFloat:
859 case Primitive::kPrimInt:
860 target = kQuickGet32Instance;
861 break;
862 case Primitive::kPrimShort:
863 target = kQuickGetShortInstance;
864 break;
865 case Primitive::kPrimChar:
866 target = kQuickGetCharInstance;
867 break;
868 case Primitive::kPrimByte:
869 target = kQuickGetByteInstance;
870 break;
871 case Primitive::kPrimBoolean:
872 target = kQuickGetBooleanInstance;
873 break;
874 case Primitive::kPrimVoid: // Intentional fallthrough.
875 default:
876 LOG(FATAL) << "Can't determine entrypoint for: " << type;
877 target = kQuickGet32Instance;
878 }
Andreas Gampe98430592014-07-27 19:44:50 -0700879 // Second argument of pGetXXInstance is always a reference.
880 DCHECK_EQ(static_cast<unsigned int>(rl_obj.wide), 0U);
881 CallRuntimeHelperImmRegLocation(target, field_info.FieldIndex(), rl_obj, true);
882
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700883 // FIXME: pGetXXInstance always return an int or int64 regardless of rl_dest.fp.
Fred Shih37f05ef2014-07-16 18:38:08 -0700884 if (IsWide(size)) {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700885 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 StoreValueWide(rl_dest, rl_result);
887 } else {
Serguei Katkov4eca9f52014-07-08 00:45:45 +0700888 RegLocation rl_result = GetReturn(rl_dest.ref ? kRefReg : kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 StoreValue(rl_dest, rl_result);
890 }
891 }
892}
893
Vladimir Markobe0e5462014-02-26 11:24:15 +0000894void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Fred Shih37f05ef2014-07-16 18:38:08 -0700895 RegLocation rl_src, RegLocation rl_obj) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000896 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
897 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700898 if (!SLOW_FIELD_PATH && field_info.FastPut()) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700899 RegisterClass reg_class = RegClassForFieldLoadStore(size, field_info.IsVolatile());
Andreas Gampeaa910d52014-07-30 18:59:05 -0700900 // Dex code never writes to the class field.
901 DCHECK_GE(static_cast<uint32_t>(field_info.FieldOffset().Int32Value()),
902 sizeof(mirror::HeapReference<mirror::Class>));
buzbeea0cd2d72014-06-01 09:33:49 -0700903 rl_obj = LoadValue(rl_obj, kRefReg);
Fred Shih37f05ef2014-07-16 18:38:08 -0700904 if (IsWide(size)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100905 rl_src = LoadValueWide(rl_src, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 } else {
907 rl_src = LoadValue(rl_src, reg_class);
Vladimir Marko674744e2014-04-24 15:18:26 +0100908 }
909 GenNullCheck(rl_obj.reg, opt_flags);
910 int field_offset = field_info.FieldOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000911 LIR* store;
Fred Shih37f05ef2014-07-16 18:38:08 -0700912 if (IsRef(size)) {
Andreas Gampe3c12c512014-06-24 18:46:29 +0000913 store = StoreRefDisp(rl_obj.reg, field_offset, rl_src.reg, field_info.IsVolatile() ?
914 kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100915 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700916 store = StoreBaseDisp(rl_obj.reg, field_offset, rl_src.reg, size,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000917 field_info.IsVolatile() ? kVolatile : kNotVolatile);
Vladimir Marko674744e2014-04-24 15:18:26 +0100918 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000919 MarkPossibleNullPointerExceptionAfter(opt_flags, store);
Fred Shih37f05ef2014-07-16 18:38:08 -0700920 if (IsRef(size) && !mir_graph_->IsConstantNullRef(rl_src)) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100921 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 }
923 } else {
Fred Shih37f05ef2014-07-16 18:38:08 -0700924 QuickEntrypointEnum target;
925 switch (size) {
926 case kReference:
927 target = kQuickSetObjInstance;
928 break;
929 case k64:
930 case kDouble:
931 target = kQuickSet64Instance;
932 break;
933 case k32:
934 case kSingle:
935 target = kQuickSet32Instance;
936 break;
937 case kSignedHalf:
938 case kUnsignedHalf:
939 target = kQuickSet16Instance;
940 break;
941 case kSignedByte:
942 case kUnsignedByte:
943 target = kQuickSet8Instance;
944 break;
945 case kWord: // Intentional fallthrough.
946 default:
947 LOG(FATAL) << "Can't determine entrypoint for: " << size;
948 target = kQuickSet32Instance;
949 }
Andreas Gampe98430592014-07-27 19:44:50 -0700950 CallRuntimeHelperImmRegLocationRegLocation(target, field_info.FieldIndex(), rl_obj, rl_src,
951 true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700952 }
953}
954
Ian Rogersa9a82542013-10-04 11:17:26 -0700955void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
956 RegLocation rl_src) {
957 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
958 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
959 (opt_flags & MIR_IGNORE_NULL_CHECK));
Andreas Gampe98430592014-07-27 19:44:50 -0700960 QuickEntrypointEnum target = needs_range_check
961 ? (needs_null_check ? kQuickAputObjectWithNullAndBoundCheck
962 : kQuickAputObjectWithBoundCheck)
963 : kQuickAputObject;
964 CallRuntimeHelperRegLocationRegLocationRegLocation(target, rl_array, rl_index, rl_src, true);
Ian Rogersa9a82542013-10-04 11:17:26 -0700965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 RegLocation rl_method = LoadCurrMethod();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700969 CheckRegLocation(rl_method);
buzbee33ae5582014-06-12 14:56:32 -0700970 RegStorage res_reg = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
Andreas Gampe4b537a82014-06-30 22:24:53 -0700972 *cu_->dex_file,
973 type_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 // Call out to helper which resolves type and verifies access.
975 // Resolved type returned in kRet0.
Andreas Gampe98430592014-07-27 19:44:50 -0700976 CallRuntimeHelperImmReg(kQuickInitializeTypeAndVerifyAccess, type_idx, rl_method.reg, true);
buzbeea0cd2d72014-06-01 09:33:49 -0700977 RegLocation rl_result = GetReturn(kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 StoreValue(rl_dest, rl_result);
979 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800980 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 // We're don't need access checks, load type from dex cache
982 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700983 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000984 LoadRefDisp(rl_method.reg, dex_cache_offset, res_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +0000985 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000986 LoadRefDisp(res_reg, offset_of_type, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
988 type_idx) || SLOW_TYPE_PATH) {
989 // Slow path, at runtime test if type is null and if so initialize
990 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800991 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800992 LIR* cont = NewLIR0(kPseudoTargetLabel);
993
994 // Object to generate the slow path for class resolution.
995 class SlowPath : public LIRSlowPath {
996 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800997 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
998 const RegLocation& rl_method_in, const RegLocation& rl_result_in) :
999 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1000 type_idx_(type_idx_in), rl_method_(rl_method_in), rl_result_(rl_result_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001001 }
1002
1003 void Compile() {
1004 GenerateTargetLabel();
1005
Andreas Gampe98430592014-07-27 19:44:50 -07001006 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_, rl_method_.reg, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001007 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0, kRef));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001008 m2l_->OpUnconditionalBranch(cont_);
1009 }
1010
1011 private:
1012 const int type_idx_;
1013 const RegLocation rl_method_;
1014 const RegLocation rl_result_;
1015 };
1016
1017 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -08001018 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001019
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001021 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 // Fast path, we're done - just store result
1023 StoreValue(rl_dest, rl_result);
1024 }
1025 }
1026}
1027
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001028void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 /* NOTE: Most strings should be available at compile time */
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001030 int32_t offset_of_string = mirror::ObjectArray<mirror::String>::OffsetOfElement(string_idx).
1031 Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001032 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
1033 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
1034 // slow path, resolve string if not in dex cache
1035 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001036 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -08001037
1038 // If the Method* is already in a register, we can save a copy.
1039 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -08001040 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -08001041 if (rl_method.location == kLocPhysReg) {
1042 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -08001043 DCHECK(!IsTemp(rl_method.reg));
1044 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -08001045 } else {
Andreas Gampeccc60262014-07-04 18:02:38 -07001046 r_method = TargetReg(kArg2, kRef);
Mark Mendell766e9292014-01-27 07:55:47 -08001047 LoadCurrMethodDirect(r_method);
1048 }
buzbee695d13a2014-04-19 13:32:20 -07001049 LoadRefDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
Andreas Gampeccc60262014-07-04 18:02:38 -07001050 TargetReg(kArg0, kRef), kNotVolatile);
Mark Mendell766e9292014-01-27 07:55:47 -08001051
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 // Might call out to helper, which will return resolved string in kRet0
Andreas Gampeccc60262014-07-04 18:02:38 -07001053 LoadRefDisp(TargetReg(kArg0, kRef), offset_of_string, TargetReg(kRet0, kRef), kNotVolatile);
1054 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0, kRef), 0, NULL);
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001055 LIR* cont = NewLIR0(kPseudoTargetLabel);
Mark Mendell766e9292014-01-27 07:55:47 -08001056
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001057 {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001058 // Object to generate the slow path for string resolution.
1059 class SlowPath : public LIRSlowPath {
1060 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001061 SlowPath(Mir2Lir* m2l, LIR* fromfast_in, LIR* cont_in, RegStorage r_method_in,
1062 int32_t string_idx_in) :
1063 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast_in, cont_in),
1064 r_method_(r_method_in), string_idx_(string_idx_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001065 }
1066
1067 void Compile() {
1068 GenerateTargetLabel();
Andreas Gampe98430592014-07-27 19:44:50 -07001069 m2l_->CallRuntimeHelperRegImm(kQuickResolveString, r_method_, string_idx_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001070 m2l_->OpUnconditionalBranch(cont_);
1071 }
1072
1073 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001074 const RegStorage r_method_;
1075 const int32_t string_idx_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001076 };
1077
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001078 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method, string_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001080
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 GenBarrier();
buzbeea0cd2d72014-06-01 09:33:49 -07001082 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 } else {
1084 RegLocation rl_method = LoadCurrMethod();
buzbeea0cd2d72014-06-01 09:33:49 -07001085 RegStorage res_reg = AllocTempRef();
1086 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001087 LoadRefDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg,
1088 kNotVolatile);
1089 LoadRefDisp(res_reg, offset_of_string, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 StoreValue(rl_dest, rl_result);
1091 }
1092}
1093
Andreas Gampe98430592014-07-27 19:44:50 -07001094/*
1095 * Let helper function take care of everything. Will
1096 * call Class::NewInstanceFromCode(type_idx, method);
1097 */
1098void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
1099 FlushAllRegs(); /* Everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 // alloc will always check for resolution, do we also need to verify
1101 // access because the verifier was unable to?
Andreas Gampe98430592014-07-27 19:44:50 -07001102 const DexFile* dex_file = cu_->dex_file;
1103 CompilerDriver* driver = cu_->compiler_driver;
1104 if (driver->CanAccessInstantiableTypeWithoutChecks(cu_->method_idx, *dex_file, type_idx)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001105 bool is_type_initialized;
1106 bool use_direct_type_ptr;
1107 uintptr_t direct_type_ptr;
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001108 bool is_finalizable;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001109 if (kEmbedClassInCode &&
Mathieu Chartier8668c3c2014-04-24 16:48:11 -07001110 driver->CanEmbedTypeInCode(*dex_file, type_idx, &is_type_initialized, &use_direct_type_ptr,
1111 &direct_type_ptr, &is_finalizable) &&
1112 !is_finalizable) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001113 // The fast path.
1114 if (!use_direct_type_ptr) {
Fred Shihe7f82e22014-08-06 10:46:37 -07001115 LoadClassType(*dex_file, type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001116 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001117 CallRuntimeHelperRegMethod(kQuickAllocObjectResolved, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001118 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001119 CallRuntimeHelperRegMethod(kQuickAllocObjectInitialized, TargetReg(kArg0, kRef), true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001120 }
1121 } else {
1122 // Use the direct pointer.
1123 if (!is_type_initialized) {
Andreas Gampe98430592014-07-27 19:44:50 -07001124 CallRuntimeHelperImmMethod(kQuickAllocObjectResolved, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001125 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001126 CallRuntimeHelperImmMethod(kQuickAllocObjectInitialized, direct_type_ptr, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001127 }
1128 }
1129 } else {
1130 // The slow path.
Andreas Gampe98430592014-07-27 19:44:50 -07001131 CallRuntimeHelperImmMethod(kQuickAllocObject, type_idx, true);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001132 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07001134 CallRuntimeHelperImmMethod(kQuickAllocObjectWithAccessCheck, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 }
Andreas Gampe98430592014-07-27 19:44:50 -07001136 StoreValue(rl_dest, GetReturn(kRefReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137}
1138
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001139void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001141 CallRuntimeHelperRegLocation(kQuickDeliverException, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142}
1143
1144// For final classes there are no sub-classes to check and so we can answer the instance-of
1145// question with simple comparisons.
1146void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1147 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001148 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001149 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001150
buzbeea0cd2d72014-06-01 09:33:49 -07001151 RegLocation object = LoadValue(rl_src, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001153 RegStorage result_reg = rl_result.reg;
buzbeeb5860fb2014-06-21 15:31:01 -07001154 if (IsSameReg(result_reg, object.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 result_reg = AllocTypedTemp(false, kCoreReg);
buzbeeb5860fb2014-06-21 15:31:01 -07001156 DCHECK(!IsSameReg(result_reg, object.reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 }
1158 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001159 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160
buzbeea0cd2d72014-06-01 09:33:49 -07001161 RegStorage check_class = AllocTypedTemp(false, kRefReg);
1162 RegStorage object_class = AllocTypedTemp(false, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163
1164 LoadCurrMethodDirect(check_class);
1165 if (use_declaring_class) {
Andreas Gampe3c12c512014-06-24 18:46:29 +00001166 LoadRefDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class,
1167 kNotVolatile);
1168 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1169 kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 } else {
buzbee695d13a2014-04-19 13:32:20 -07001171 LoadRefDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001172 check_class, kNotVolatile);
1173 LoadRefDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class,
1174 kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001175 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001176 LoadRefDisp(check_class, offset_of_type, check_class, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 }
1178
buzbee695d13a2014-04-19 13:32:20 -07001179 // FIXME: what should we be comparing here? compressed or decompressed references?
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 if (cu_->instruction_set == kThumb2) {
1181 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001182 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001184 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001185 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001186 GenSelectConst32(check_class, object_class, kCondEq, 1, 0, result_reg, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 }
1188 LIR* target = NewLIR0(kPseudoTargetLabel);
1189 null_branchover->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 FreeTemp(object_class);
1191 FreeTemp(check_class);
1192 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001193 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 FreeTemp(result_reg);
1195 }
1196 StoreValue(rl_dest, rl_result);
1197}
1198
1199void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1200 bool type_known_abstract, bool use_declaring_class,
1201 bool can_assume_type_is_in_dex_cache,
1202 uint32_t type_idx, RegLocation rl_dest,
1203 RegLocation rl_src) {
1204 FlushAllRegs();
1205 // May generate a call - use explicit registers
1206 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001207 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001208 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001209 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Serguei Katkov9ee45192014-07-17 14:39:03 +07001210 RegStorage ref_reg = TargetReg(kArg0, kRef); // kArg0 will hold the ref.
1211 RegStorage ret_reg = GetReturn(kRefReg).reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 if (needs_access_check) {
1213 // Check we have access to type_idx and if not throw IllegalAccessError,
1214 // returns Class* in kArg0
Andreas Gampe98430592014-07-27 19:44:50 -07001215 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001216 OpRegCopy(class_reg, ret_reg); // Align usage with fast path
1217 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 } else if (use_declaring_class) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001219 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe4b537a82014-06-30 22:24:53 -07001220 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001221 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 } else {
Andreas Gampe90969af2014-07-15 23:02:11 -07001223 if (can_assume_type_is_in_dex_cache) {
1224 // Conditionally, as in the other case we will also load it.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001225 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001226 }
1227
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001229 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001230 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001231 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001232 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 if (!can_assume_type_is_in_dex_cache) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001234 LIR* slow_path_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1235 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
1236
1237 // Should load value here.
Serguei Katkov9ee45192014-07-17 14:39:03 +07001238 LoadValueDirectFixed(rl_src, ref_reg); // kArg0 <= ref
Andreas Gampe90969af2014-07-15 23:02:11 -07001239
1240 class InitTypeSlowPath : public Mir2Lir::LIRSlowPath {
1241 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001242 InitTypeSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont, uint32_t type_idx_in,
1243 RegLocation rl_src_in)
1244 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont), type_idx_(type_idx_in),
1245 rl_src_(rl_src_in) {
Andreas Gampe90969af2014-07-15 23:02:11 -07001246 }
1247
1248 void Compile() OVERRIDE {
1249 GenerateTargetLabel();
1250
Andreas Gampe98430592014-07-27 19:44:50 -07001251 m2l_->CallRuntimeHelperImm(kQuickInitializeType, type_idx_, true);
Andreas Gampe90969af2014-07-15 23:02:11 -07001252 m2l_->OpRegCopy(m2l_->TargetReg(kArg2, kRef),
1253 m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Andreas Gampe90969af2014-07-15 23:02:11 -07001254 m2l_->OpUnconditionalBranch(cont_);
1255 }
1256
1257 private:
1258 uint32_t type_idx_;
1259 RegLocation rl_src_;
1260 };
1261
1262 AddSlowPath(new (arena_) InitTypeSlowPath(this, slow_path_branch, slow_path_target,
1263 type_idx, rl_src));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 }
1265 }
1266 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
Andreas Gampe4b537a82014-06-30 22:24:53 -07001267 RegLocation rl_result = GetReturn(kCoreReg);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001268 if (!IsSameReg(rl_result.reg, ref_reg)) {
1269 // On MIPS and x86_64 rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001270 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001272 LIR* branch1 = OpCmpImmBranch(kCondEq, ref_reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273
1274 /* load object->klass_ */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001275 RegStorage ref_class_reg = TargetReg(kArg1, kRef); // kArg1 will hold the Class* of ref.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Serguei Katkov9ee45192014-07-17 14:39:03 +07001277 LoadRefDisp(ref_reg, mirror::Object::ClassOffset().Int32Value(),
1278 ref_class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1280 LIR* branchover = NULL;
1281 if (type_known_final) {
Serguei Katkov9ee45192014-07-17 14:39:03 +07001282 // rl_result == ref == class.
1283 GenSelectConst32(ref_class_reg, class_reg, kCondEq, 1, 0, rl_result.reg,
Andreas Gampe90969af2014-07-15 23:02:11 -07001284 kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 } else {
1286 if (cu_->instruction_set == kThumb2) {
Andreas Gampe98430592014-07-27 19:44:50 -07001287 RegStorage r_tgt = LoadHelper(kQuickInstanceofNonTrivial);
Dave Allison3da67a52014-04-02 17:03:45 -07001288 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 if (!type_known_abstract) {
1290 /* Uses conditional nullification */
Serguei Katkov9ee45192014-07-17 14:39:03 +07001291 OpRegReg(kOpCmp, ref_class_reg, class_reg); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001292 it = OpIT(kCondEq, "EE"); // if-convert the test
Serguei Katkov9ee45192014-07-17 14:39:03 +07001293 LoadConstant(rl_result.reg, 1); // .eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 }
Serguei Katkov9ee45192014-07-17 14:39:03 +07001295 OpRegCopy(ref_reg, class_reg); // .ne case - arg0 <= class
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001297 if (it != nullptr) {
1298 OpEndIT(it);
1299 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 FreeTemp(r_tgt);
1301 } else {
1302 if (!type_known_abstract) {
1303 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001304 LoadConstant(rl_result.reg, 1); // assume true
Andreas Gampeccc60262014-07-04 18:02:38 -07001305 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1, kRef), TargetReg(kArg2, kRef), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 }
Andreas Gampe90969af2014-07-15 23:02:11 -07001307
Serguei Katkov9ee45192014-07-17 14:39:03 +07001308 OpRegCopy(TargetReg(kArg0, kRef), class_reg); // .ne case - arg0 <= class
Andreas Gampe98430592014-07-27 19:44:50 -07001309 CallRuntimeHelper(kQuickInstanceofNonTrivial, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 }
1311 }
1312 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001313 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 /* branch targets here */
1315 LIR* target = NewLIR0(kPseudoTargetLabel);
1316 StoreValue(rl_dest, rl_result);
1317 branch1->target = target;
Andreas Gampe98430592014-07-27 19:44:50 -07001318 if (branchover != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001319 branchover->target = target;
1320 }
1321}
1322
1323void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1324 bool type_known_final, type_known_abstract, use_declaring_class;
1325 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1326 *cu_->dex_file,
1327 type_idx,
1328 &type_known_final,
1329 &type_known_abstract,
1330 &use_declaring_class);
1331 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1332 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1333
1334 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1335 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1336 } else {
1337 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1338 use_declaring_class, can_assume_type_is_in_dex_cache,
1339 type_idx, rl_dest, rl_src);
1340 }
1341}
1342
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001343void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 bool type_known_final, type_known_abstract, use_declaring_class;
1345 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1346 *cu_->dex_file,
1347 type_idx,
1348 &type_known_final,
1349 &type_known_abstract,
1350 &use_declaring_class);
1351 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1352 // of the exception throw path.
1353 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001354 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 // Verifier type analysis proved this check cast would never cause an exception.
1356 return;
1357 }
1358 FlushAllRegs();
1359 // May generate a call - use explicit registers
1360 LockCallTemps();
Andreas Gampeccc60262014-07-04 18:02:38 -07001361 RegStorage method_reg = TargetReg(kArg1, kRef);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001362 LoadCurrMethodDirect(method_reg); // kArg1 <= current Method*
Andreas Gampeccc60262014-07-04 18:02:38 -07001363 RegStorage class_reg = TargetReg(kArg2, kRef); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 if (needs_access_check) {
1365 // Check we have access to type_idx and if not throw IllegalAccessError,
1366 // returns Class* in kRet0
1367 // InitializeTypeAndVerifyAccess(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001368 CallRuntimeHelperImm(kQuickInitializeTypeAndVerifyAccess, type_idx, true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001369 OpRegCopy(class_reg, TargetReg(kRet0, kRef)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 } else if (use_declaring_class) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001371 LoadRefDisp(method_reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001372 class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 } else {
1374 // Load dex cache entry into class_reg (kArg2)
Andreas Gampe4b537a82014-06-30 22:24:53 -07001375 LoadRefDisp(method_reg, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Andreas Gampe3c12c512014-06-24 18:46:29 +00001376 class_reg, kNotVolatile);
Andreas Gampe9c3b0892014-04-24 17:33:34 +00001377 int32_t offset_of_type = ClassArray::OffsetOfElement(type_idx).Int32Value();
Andreas Gampe3c12c512014-06-24 18:46:29 +00001378 LoadRefDisp(class_reg, offset_of_type, class_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1380 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001381 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1382 LIR* cont = NewLIR0(kPseudoTargetLabel);
1383
1384 // Slow path to initialize the type. Executed if the type is NULL.
1385 class SlowPath : public LIRSlowPath {
1386 public:
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001387 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont_in, const int type_idx_in,
1388 const RegStorage class_reg_in) :
1389 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont_in),
1390 type_idx_(type_idx_in), class_reg_(class_reg_in) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001391 }
1392
1393 void Compile() {
1394 GenerateTargetLabel();
1395
1396 // Call out to helper, which will return resolved type in kArg0
1397 // InitializeTypeFromCode(idx, method)
Andreas Gampe98430592014-07-27 19:44:50 -07001398 m2l_->CallRuntimeHelperImmReg(kQuickInitializeType, type_idx_,
1399 m2l_->TargetReg(kArg1, kRef), true);
Andreas Gampeccc60262014-07-04 18:02:38 -07001400 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0, kRef)); // Align usage with fast path
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001401 m2l_->OpUnconditionalBranch(cont_);
1402 }
Andreas Gampe2f244e92014-05-08 03:35:25 -07001403
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001404 public:
1405 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001406 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001407 };
1408
buzbee2700f7e2014-03-07 09:46:20 -08001409 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 }
1411 }
1412 // At this point, class_reg (kArg2) has class
Andreas Gampeccc60262014-07-04 18:02:38 -07001413 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kRef)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001414
1415 // Slow path for the case where the classes are not equal. In this case we need
1416 // to call a helper function to do the check.
1417 class SlowPath : public LIRSlowPath {
1418 public:
1419 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1420 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1421 }
1422
1423 void Compile() {
1424 GenerateTargetLabel();
1425
1426 if (load_) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001427 m2l_->LoadRefDisp(m2l_->TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1428 m2l_->TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001429 }
Andreas Gampe98430592014-07-27 19:44:50 -07001430 m2l_->CallRuntimeHelperRegReg(kQuickCheckCast, m2l_->TargetReg(kArg2, kRef),
1431 m2l_->TargetReg(kArg1, kRef), true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001432 m2l_->OpUnconditionalBranch(cont_);
1433 }
1434
1435 private:
Mingyao Yang3b004ba2014-04-29 15:55:37 -07001436 const bool load_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001437 };
1438
1439 if (type_known_abstract) {
1440 // Easier case, run slow path if target is non-null (slow path will load from target)
Andreas Gampeccc60262014-07-04 18:02:38 -07001441 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001442 LIR* cont = NewLIR0(kPseudoTargetLabel);
1443 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1444 } else {
1445 // Harder, more common case. We need to generate a forward branch over the load
1446 // if the target is null. If it's non-null we perform the load and branch to the
1447 // slow path if the classes are not equal.
1448
1449 /* Null is OK - continue */
Andreas Gampeccc60262014-07-04 18:02:38 -07001450 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0, kRef), 0, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001451 /* load object->klass_ */
1452 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
Andreas Gampeccc60262014-07-04 18:02:38 -07001453 LoadRefDisp(TargetReg(kArg0, kRef), mirror::Object::ClassOffset().Int32Value(),
1454 TargetReg(kArg1, kRef), kNotVolatile);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001455
Andreas Gampeccc60262014-07-04 18:02:38 -07001456 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1, kRef), class_reg, nullptr);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001457 LIR* cont = NewLIR0(kPseudoTargetLabel);
1458
1459 // Add the slow path that will not perform load since this is already done.
1460 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1461
1462 // Set the null check to branch to the continuation.
1463 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 }
1465}
1466
1467void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001468 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 RegLocation rl_result;
1470 if (cu_->instruction_set == kThumb2) {
1471 /*
1472 * NOTE: This is the one place in the code in which we might have
1473 * as many as six live temporary registers. There are 5 in the normal
1474 * set for Arm. Until we have spill capabilities, temporarily add
1475 * lr to the temp set. It is safe to do this locally, but note that
1476 * lr is used explicitly elsewhere in the code generator and cannot
1477 * normally be used as a general temp register.
1478 */
Andreas Gampeccc60262014-07-04 18:02:38 -07001479 MarkTemp(TargetReg(kLr, kNotWide)); // Add lr to the temp pool
1480 FreeTemp(TargetReg(kLr, kNotWide)); // and make it available
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 }
1482 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1483 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1484 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1485 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001486 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1487 RegStorage t_reg = AllocTemp();
1488 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1489 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1490 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 FreeTemp(t_reg);
1492 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001493 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1494 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 }
1496 /*
1497 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1498 * following StoreValueWide might need to allocate a temp register.
1499 * To further work around the lack of a spill capability, explicitly
1500 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1501 * Remove when spill is functional.
1502 */
1503 FreeRegLocTemps(rl_result, rl_src1);
1504 FreeRegLocTemps(rl_result, rl_src2);
1505 StoreValueWide(rl_dest, rl_result);
1506 if (cu_->instruction_set == kThumb2) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001507 Clobber(TargetReg(kLr, kNotWide));
1508 UnmarkTemp(TargetReg(kLr, kNotWide)); // Remove lr from the temp pool
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 }
1510}
1511
Andreas Gampe98430592014-07-27 19:44:50 -07001512void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1513 RegLocation rl_src1, RegLocation rl_shift) {
1514 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 switch (opcode) {
1516 case Instruction::SHL_LONG:
1517 case Instruction::SHL_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001518 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 break;
1520 case Instruction::SHR_LONG:
1521 case Instruction::SHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001522 target = kQuickShrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 break;
1524 case Instruction::USHR_LONG:
1525 case Instruction::USHR_LONG_2ADDR:
Andreas Gampe98430592014-07-27 19:44:50 -07001526 target = kQuickUshrLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 break;
1528 default:
1529 LOG(FATAL) << "Unexpected case";
Andreas Gampe98430592014-07-27 19:44:50 -07001530 target = kQuickShlLong;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 }
Andreas Gampe98430592014-07-27 19:44:50 -07001532 FlushAllRegs(); /* Send everything to home location */
1533 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_shift, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001534 RegLocation rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 StoreValueWide(rl_dest, rl_result);
1536}
1537
1538
1539void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001540 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001541 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 OpKind op = kOpBkpt;
1543 bool is_div_rem = false;
1544 bool check_zero = false;
1545 bool unary = false;
1546 RegLocation rl_result;
1547 bool shift_op = false;
1548 switch (opcode) {
1549 case Instruction::NEG_INT:
1550 op = kOpNeg;
1551 unary = true;
1552 break;
1553 case Instruction::NOT_INT:
1554 op = kOpMvn;
1555 unary = true;
1556 break;
1557 case Instruction::ADD_INT:
1558 case Instruction::ADD_INT_2ADDR:
1559 op = kOpAdd;
1560 break;
1561 case Instruction::SUB_INT:
1562 case Instruction::SUB_INT_2ADDR:
1563 op = kOpSub;
1564 break;
1565 case Instruction::MUL_INT:
1566 case Instruction::MUL_INT_2ADDR:
1567 op = kOpMul;
1568 break;
1569 case Instruction::DIV_INT:
1570 case Instruction::DIV_INT_2ADDR:
1571 check_zero = true;
1572 op = kOpDiv;
1573 is_div_rem = true;
1574 break;
1575 /* NOTE: returns in kArg1 */
1576 case Instruction::REM_INT:
1577 case Instruction::REM_INT_2ADDR:
1578 check_zero = true;
1579 op = kOpRem;
1580 is_div_rem = true;
1581 break;
1582 case Instruction::AND_INT:
1583 case Instruction::AND_INT_2ADDR:
1584 op = kOpAnd;
1585 break;
1586 case Instruction::OR_INT:
1587 case Instruction::OR_INT_2ADDR:
1588 op = kOpOr;
1589 break;
1590 case Instruction::XOR_INT:
1591 case Instruction::XOR_INT_2ADDR:
1592 op = kOpXor;
1593 break;
1594 case Instruction::SHL_INT:
1595 case Instruction::SHL_INT_2ADDR:
1596 shift_op = true;
1597 op = kOpLsl;
1598 break;
1599 case Instruction::SHR_INT:
1600 case Instruction::SHR_INT_2ADDR:
1601 shift_op = true;
1602 op = kOpAsr;
1603 break;
1604 case Instruction::USHR_INT:
1605 case Instruction::USHR_INT_2ADDR:
1606 shift_op = true;
1607 op = kOpLsr;
1608 break;
1609 default:
1610 LOG(FATAL) << "Invalid word arith op: " << opcode;
1611 }
1612 if (!is_div_rem) {
1613 if (unary) {
1614 rl_src1 = LoadValue(rl_src1, kCoreReg);
1615 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001616 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001618 if ((shift_op) && (cu_->instruction_set != kArm64)) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001619 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001620 RegStorage t_reg = AllocTemp();
1621 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 rl_src1 = LoadValue(rl_src1, kCoreReg);
1623 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001624 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 FreeTemp(t_reg);
1626 } else {
1627 rl_src1 = LoadValue(rl_src1, kCoreReg);
1628 rl_src2 = LoadValue(rl_src2, kCoreReg);
1629 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001630 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 }
1632 }
1633 StoreValue(rl_dest, rl_result);
1634 } else {
Dave Allison70202782013-10-22 17:52:19 -07001635 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 +01001636 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 rl_src1 = LoadValue(rl_src1, kCoreReg);
1638 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001639 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001640 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 }
buzbee2700f7e2014-03-07 09:46:20 -08001642 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001643 done = true;
1644 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001645 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1646 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001647 // Use ARM SDIV instruction for division. For remainder we also need to
1648 // calculate using a MUL and subtract.
1649 rl_src1 = LoadValue(rl_src1, kCoreReg);
1650 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001651 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001652 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001653 }
buzbee2700f7e2014-03-07 09:46:20 -08001654 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001655 done = true;
1656 }
1657 }
1658
1659 // If we haven't already generated the code use the callout function.
1660 if (!done) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 FlushAllRegs(); /* Send everything to home location */
Andreas Gampeccc60262014-07-04 18:02:38 -07001662 LoadValueDirectFixed(rl_src2, TargetReg(kArg1, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001663 RegStorage r_tgt = CallHelperSetup(kQuickIdivmod);
Andreas Gampeccc60262014-07-04 18:02:38 -07001664 LoadValueDirectFixed(rl_src1, TargetReg(kArg0, kNotWide));
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001665 if (check_zero && (flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
Andreas Gampeccc60262014-07-04 18:02:38 -07001666 GenDivZeroCheck(TargetReg(kArg1, kNotWide));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001667 }
Dave Allison70202782013-10-22 17:52:19 -07001668 // NOTE: callout here is not a safepoint.
Andreas Gampe98430592014-07-27 19:44:50 -07001669 CallHelper(r_tgt, kQuickIdivmod, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 if (op == kOpDiv)
buzbeea0cd2d72014-06-01 09:33:49 -07001671 rl_result = GetReturn(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 else
1673 rl_result = GetReturnAlt();
1674 }
1675 StoreValue(rl_dest, rl_result);
1676 }
1677}
1678
1679/*
1680 * The following are the first-level codegen routines that analyze the format
1681 * of each bytecode then either dispatch special purpose codegen routines
1682 * or produce corresponding Thumb instructions directly.
1683 */
1684
Brian Carlstrom7940e442013-07-12 13:46:57 -07001685// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001686static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687 x &= x - 1;
1688 return (x & (x - 1)) == 0;
1689}
1690
Brian Carlstrom7940e442013-07-12 13:46:57 -07001691// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1692// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001693bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001694 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001695 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1696 return false;
1697 }
1698 // No divide instruction for Arm, so check for more special cases
1699 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001700 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001701 }
1702 int k = LowestSetBit(lit);
1703 if (k >= 30) {
1704 // Avoid special cases.
1705 return false;
1706 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001707 rl_src = LoadValue(rl_src, kCoreReg);
1708 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001709 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001710 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 if (lit == 2) {
1712 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001713 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1714 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1715 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001716 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001717 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001718 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001719 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1720 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 }
1722 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001723 RegStorage t_reg1 = AllocTemp();
1724 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001725 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001726 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1727 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001729 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001730 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001731 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001732 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001733 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001734 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001735 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 }
1737 }
1738 StoreValue(rl_dest, rl_result);
1739 return true;
1740}
1741
1742// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1743// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001744bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001745 if (lit < 0) {
1746 return false;
1747 }
1748 if (lit == 0) {
1749 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1750 LoadConstant(rl_result.reg, 0);
1751 StoreValue(rl_dest, rl_result);
1752 return true;
1753 }
1754 if (lit == 1) {
1755 rl_src = LoadValue(rl_src, kCoreReg);
1756 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1757 OpRegCopy(rl_result.reg, rl_src.reg);
1758 StoreValue(rl_dest, rl_result);
1759 return true;
1760 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001761 // There is RegRegRegShift on Arm, so check for more special cases
1762 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001763 return EasyMultiply(rl_src, rl_dest, lit);
1764 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 // Can we simplify this multiplication?
1766 bool power_of_two = false;
1767 bool pop_count_le2 = false;
1768 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001769 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 power_of_two = true;
1771 } else if (IsPopCountLE2(lit)) {
1772 pop_count_le2 = true;
1773 } else if (IsPowerOfTwo(lit + 1)) {
1774 power_of_two_minus_one = true;
1775 } else {
1776 return false;
1777 }
1778 rl_src = LoadValue(rl_src, kCoreReg);
1779 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1780 if (power_of_two) {
1781 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001782 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001783 } else if (pop_count_le2) {
1784 // Shift and add and shift.
1785 int first_bit = LowestSetBit(lit);
1786 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1787 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1788 } else {
1789 // Reverse subtract: (src << (shift + 1)) - src.
1790 DCHECK(power_of_two_minus_one);
1791 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001792 RegStorage t_reg = AllocTemp();
1793 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1794 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 }
1796 StoreValue(rl_dest, rl_result);
1797 return true;
1798}
1799
Ningsheng Jian675e09b2014-10-23 13:48:36 +08001800// Returns true if it generates instructions.
1801bool Mir2Lir::HandleEasyFloatingPointDiv(RegLocation rl_dest, RegLocation rl_src1,
1802 RegLocation rl_src2) {
1803 if (!rl_src2.is_const ||
1804 ((cu_->instruction_set != kThumb2) && (cu_->instruction_set != kArm64))) {
1805 return false;
1806 }
1807
1808 if (!rl_src2.wide) {
1809 int32_t divisor = mir_graph_->ConstantValue(rl_src2);
1810 if (CanDivideByReciprocalMultiplyFloat(divisor)) {
1811 // Generate multiply by reciprocal instead of div.
1812 float recip = 1.0f/bit_cast<int32_t, float>(divisor);
1813 GenMultiplyByConstantFloat(rl_dest, rl_src1, bit_cast<float, int32_t>(recip));
1814 return true;
1815 }
1816 } else {
1817 int64_t divisor = mir_graph_->ConstantValueWide(rl_src2);
1818 if (CanDivideByReciprocalMultiplyDouble(divisor)) {
1819 // Generate multiply by reciprocal instead of div.
1820 double recip = 1.0/bit_cast<double, int64_t>(divisor);
1821 GenMultiplyByConstantDouble(rl_dest, rl_src1, bit_cast<double, int64_t>(recip));
1822 return true;
1823 }
1824 }
1825 return false;
1826}
1827
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001829 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 RegLocation rl_result;
1831 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1832 int shift_op = false;
1833 bool is_div = false;
1834
1835 switch (opcode) {
1836 case Instruction::RSUB_INT_LIT8:
1837 case Instruction::RSUB_INT: {
1838 rl_src = LoadValue(rl_src, kCoreReg);
1839 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1840 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001841 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001842 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001843 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1844 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001845 }
1846 StoreValue(rl_dest, rl_result);
1847 return;
1848 }
1849
1850 case Instruction::SUB_INT:
1851 case Instruction::SUB_INT_2ADDR:
1852 lit = -lit;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001853 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001854 case Instruction::ADD_INT:
1855 case Instruction::ADD_INT_2ADDR:
1856 case Instruction::ADD_INT_LIT8:
1857 case Instruction::ADD_INT_LIT16:
1858 op = kOpAdd;
1859 break;
1860 case Instruction::MUL_INT:
1861 case Instruction::MUL_INT_2ADDR:
1862 case Instruction::MUL_INT_LIT8:
1863 case Instruction::MUL_INT_LIT16: {
1864 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1865 return;
1866 }
1867 op = kOpMul;
1868 break;
1869 }
1870 case Instruction::AND_INT:
1871 case Instruction::AND_INT_2ADDR:
1872 case Instruction::AND_INT_LIT8:
1873 case Instruction::AND_INT_LIT16:
1874 op = kOpAnd;
1875 break;
1876 case Instruction::OR_INT:
1877 case Instruction::OR_INT_2ADDR:
1878 case Instruction::OR_INT_LIT8:
1879 case Instruction::OR_INT_LIT16:
1880 op = kOpOr;
1881 break;
1882 case Instruction::XOR_INT:
1883 case Instruction::XOR_INT_2ADDR:
1884 case Instruction::XOR_INT_LIT8:
1885 case Instruction::XOR_INT_LIT16:
1886 op = kOpXor;
1887 break;
1888 case Instruction::SHL_INT_LIT8:
1889 case Instruction::SHL_INT:
1890 case Instruction::SHL_INT_2ADDR:
1891 lit &= 31;
1892 shift_op = true;
1893 op = kOpLsl;
1894 break;
1895 case Instruction::SHR_INT_LIT8:
1896 case Instruction::SHR_INT:
1897 case Instruction::SHR_INT_2ADDR:
1898 lit &= 31;
1899 shift_op = true;
1900 op = kOpAsr;
1901 break;
1902 case Instruction::USHR_INT_LIT8:
1903 case Instruction::USHR_INT:
1904 case Instruction::USHR_INT_2ADDR:
1905 lit &= 31;
1906 shift_op = true;
1907 op = kOpLsr;
1908 break;
1909
1910 case Instruction::DIV_INT:
1911 case Instruction::DIV_INT_2ADDR:
1912 case Instruction::DIV_INT_LIT8:
1913 case Instruction::DIV_INT_LIT16:
1914 case Instruction::REM_INT:
1915 case Instruction::REM_INT_2ADDR:
1916 case Instruction::REM_INT_LIT8:
1917 case Instruction::REM_INT_LIT16: {
1918 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001919 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001920 return;
1921 }
buzbee11b63d12013-08-27 07:34:17 -07001922 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001923 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001924 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001925 (opcode == Instruction::DIV_INT_LIT16)) {
1926 is_div = true;
1927 } else {
1928 is_div = false;
1929 }
buzbee11b63d12013-08-27 07:34:17 -07001930 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1931 return;
1932 }
Dave Allison70202782013-10-22 17:52:19 -07001933
1934 bool done = false;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001935 if (cu_->instruction_set == kMips || cu_->instruction_set == kArm64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001936 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001937 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001938 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001939 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001940 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1941 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001942 } else if (cu_->instruction_set == kThumb2) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001943 if (cu_->GetInstructionSetFeatures()->AsArmInstructionSetFeatures()->
1944 HasDivideInstruction()) {
Dave Allison70202782013-10-22 17:52:19 -07001945 // Use ARM SDIV instruction for division. For remainder we also need to
1946 // calculate using a MUL and subtract.
1947 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001948 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001949 done = true;
1950 }
1951 }
1952
1953 if (!done) {
1954 FlushAllRegs(); /* Everything to home location. */
Andreas Gampeccc60262014-07-04 18:02:38 -07001955 LoadValueDirectFixed(rl_src, TargetReg(kArg0, kNotWide));
1956 Clobber(TargetReg(kArg0, kNotWide));
Andreas Gampe98430592014-07-27 19:44:50 -07001957 CallRuntimeHelperRegImm(kQuickIdivmod, TargetReg(kArg0, kNotWide), lit, false);
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 Gampe98430592014-07-27 19:44:50 -07001980void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001981 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 RegLocation rl_result;
1983 OpKind first_op = kOpBkpt;
1984 OpKind second_op = kOpBkpt;
1985 bool call_out = false;
1986 bool check_zero = false;
Andreas Gampe98430592014-07-27 19:44:50 -07001987 int ret_reg = TargetReg(kRet0, kNotWide).GetReg();
1988 QuickEntrypointEnum target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989
1990 switch (opcode) {
1991 case Instruction::NOT_LONG:
Andreas Gampe98430592014-07-27 19:44:50 -07001992 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1993 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001994 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001995 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
Andreas Gampe98430592014-07-27 19:44:50 -07001996 RegStorage t_reg = AllocTemp();
1997 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1998 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1999 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
2000 FreeTemp(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002001 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002002 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
2003 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 }
Andreas Gampe98430592014-07-27 19:44:50 -07002005 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006 return;
2007 case Instruction::ADD_LONG:
2008 case Instruction::ADD_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002009 first_op = kOpAdd;
2010 second_op = kOpAdc;
2011 break;
2012 case Instruction::SUB_LONG:
2013 case Instruction::SUB_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 first_op = kOpSub;
2015 second_op = kOpSbc;
2016 break;
2017 case Instruction::MUL_LONG:
2018 case Instruction::MUL_LONG_2ADDR:
Andreas Gampec76c6142014-08-04 16:30:03 -07002019 call_out = true;
2020 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2021 target = kQuickLmul;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002022 break;
2023 case Instruction::DIV_LONG:
2024 case Instruction::DIV_LONG_2ADDR:
2025 call_out = true;
2026 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002027 ret_reg = TargetReg(kRet0, kNotWide).GetReg();
2028 target = kQuickLdiv;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002029 break;
2030 case Instruction::REM_LONG:
2031 case Instruction::REM_LONG_2ADDR:
2032 call_out = true;
2033 check_zero = true;
Andreas Gampe98430592014-07-27 19:44:50 -07002034 target = kQuickLmod;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002035 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
Andreas Gampe98430592014-07-27 19:44:50 -07002036 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2, kNotWide).GetReg() :
2037 TargetReg(kRet0, kNotWide).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002038 break;
2039 case Instruction::AND_LONG_2ADDR:
2040 case Instruction::AND_LONG:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002041 first_op = kOpAnd;
2042 second_op = kOpAnd;
2043 break;
2044 case Instruction::OR_LONG:
2045 case Instruction::OR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002046 first_op = kOpOr;
2047 second_op = kOpOr;
2048 break;
2049 case Instruction::XOR_LONG:
2050 case Instruction::XOR_LONG_2ADDR:
Brian Carlstrom7940e442013-07-12 13:46:57 -07002051 first_op = kOpXor;
2052 second_op = kOpXor;
2053 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002054 default:
2055 LOG(FATAL) << "Invalid long arith op";
2056 }
2057 if (!call_out) {
Andreas Gampe98430592014-07-27 19:44:50 -07002058 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002059 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002060 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002061 if (check_zero) {
Andreas Gampe98430592014-07-27 19:44:50 -07002062 RegStorage r_tmp1 = TargetReg(kArg0, kWide);
2063 RegStorage r_tmp2 = TargetReg(kArg2, kWide);
2064 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2065 RegStorage r_tgt = CallHelperSetup(target);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07002066 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
2067 GenDivZeroCheckWide(r_tmp2);
2068 }
Andreas Gampe98430592014-07-27 19:44:50 -07002069 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002070 // NOTE: callout here is not a safepoint
Andreas Gampe98430592014-07-27 19:44:50 -07002071 CallHelper(r_tgt, target, false /* not safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002072 } else {
Andreas Gampe98430592014-07-27 19:44:50 -07002073 CallRuntimeHelperRegLocationRegLocation(target, rl_src1, rl_src2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 }
2075 // Adjust return regs in to handle case of rem returning kArg2/kArg3
Andreas Gampe98430592014-07-27 19:44:50 -07002076 if (ret_reg == TargetReg(kRet0, kNotWide).GetReg())
2077 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002078 else
Andreas Gampe98430592014-07-27 19:44:50 -07002079 rl_result = GetReturnWideAlt();
2080 StoreValueWide(rl_dest, rl_result);
Andreas Gampe2f244e92014-05-08 03:35:25 -07002081 }
2082}
2083
Mark Mendelle87f9b52014-04-30 14:13:18 -04002084void Mir2Lir::GenConst(RegLocation rl_dest, int value) {
2085 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2086 LoadConstantNoClobber(rl_result.reg, value);
2087 StoreValue(rl_dest, rl_result);
2088 if (value == 0) {
2089 Workaround7250540(rl_dest, rl_result.reg);
2090 }
2091}
2092
Andreas Gampe98430592014-07-27 19:44:50 -07002093void Mir2Lir::GenConversionCall(QuickEntrypointEnum trampoline, RegLocation rl_dest,
2094 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002095 /*
2096 * Don't optimize the register usage since it calls out to support
2097 * functions
2098 */
Andreas Gampe2f244e92014-05-08 03:35:25 -07002099
Brian Carlstrom7940e442013-07-12 13:46:57 -07002100 FlushAllRegs(); /* Send everything to home location */
Andreas Gampe98430592014-07-27 19:44:50 -07002101 CallRuntimeHelperRegLocation(trampoline, rl_src, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102 if (rl_dest.wide) {
2103 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002104 rl_result = GetReturnWide(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002105 StoreValueWide(rl_dest, rl_result);
2106 } else {
2107 RegLocation rl_result;
buzbeea0cd2d72014-06-01 09:33:49 -07002108 rl_result = GetReturn(LocToRegClass(rl_dest));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002109 StoreValue(rl_dest, rl_result);
2110 }
2111}
2112
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002113class SuspendCheckSlowPath : public Mir2Lir::LIRSlowPath {
2114 public:
2115 SuspendCheckSlowPath(Mir2Lir* m2l, LIR* branch, LIR* cont)
2116 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, cont) {
2117 }
2118
2119 void Compile() OVERRIDE {
2120 m2l_->ResetRegPool();
2121 m2l_->ResetDefTracking();
2122 GenerateTargetLabel(kPseudoSuspendTarget);
Andreas Gampe98430592014-07-27 19:44:50 -07002123 m2l_->CallRuntimeHelper(kQuickTestSuspend, true);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002124 if (cont_ != nullptr) {
2125 m2l_->OpUnconditionalBranch(cont_);
2126 }
2127 }
2128};
2129
Brian Carlstrom7940e442013-07-12 13:46:57 -07002130/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002131void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allison69dfe512014-07-11 17:11:58 +00002132 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002133 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2134 return;
2135 }
2136 FlushAllRegs();
2137 LIR* branch = OpTestSuspend(NULL);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002138 LIR* cont = NewLIR0(kPseudoTargetLabel);
2139 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, cont));
Dave Allisonb373e092014-02-20 16:06:36 -08002140 } else {
2141 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2142 return;
2143 }
2144 FlushAllRegs(); // TODO: needed?
2145 LIR* inst = CheckSuspendUsingLoad();
2146 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002147 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002148}
2149
2150/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002151void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allison69dfe512014-07-11 17:11:58 +00002152 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitSuspendChecks()) {
Dave Allisonb373e092014-02-20 16:06:36 -08002153 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2154 OpUnconditionalBranch(target);
2155 return;
2156 }
2157 OpTestSuspend(target);
Dave Allisonb373e092014-02-20 16:06:36 -08002158 FlushAllRegs();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07002159 LIR* branch = OpUnconditionalBranch(nullptr);
2160 AddSlowPath(new (arena_) SuspendCheckSlowPath(this, branch, target));
Dave Allisonb373e092014-02-20 16:06:36 -08002161 } else {
2162 // For the implicit suspend check, just perform the trigger
2163 // load and branch to the target.
2164 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2165 OpUnconditionalBranch(target);
2166 return;
2167 }
2168 FlushAllRegs();
2169 LIR* inst = CheckSuspendUsingLoad();
2170 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002171 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002172 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002173}
2174
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002175/* Call out to helper assembly routine that will null check obj and then lock it. */
2176void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002177 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002178 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002179 CallRuntimeHelperRegLocation(kQuickLockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002180}
2181
2182/* Call out to helper assembly routine that will null check obj and then unlock it. */
2183void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002184 UNUSED(opt_flags); // TODO: avoid null check with specialized non-null helper.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002185 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07002186 CallRuntimeHelperRegLocation(kQuickUnlockObject, rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002187}
2188
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002189/* Generic code for generating a wide constant into a VR. */
2190void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2191 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002192 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002193 StoreValueWide(rl_dest, rl_result);
2194}
2195
Andreas Gampe48971b32014-08-06 10:09:01 -07002196void Mir2Lir::GenSmallPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002197 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002198 const uint16_t entries = table[1];
2199 // Chained cmp-and-branch.
2200 const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002201 int32_t starting_key = as_int32[0];
Andreas Gampe48971b32014-08-06 10:09:01 -07002202 const int32_t* targets = &as_int32[1];
2203 rl_src = LoadValue(rl_src, kCoreReg);
2204 int i = 0;
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002205 for (; i < entries; i++) {
2206 if (!InexpensiveConstantInt(starting_key + i, Instruction::Code::IF_EQ)) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002207 // Switch to using a temp and add.
2208 break;
2209 }
2210 BasicBlock* case_block =
2211 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002212 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002213 }
2214 if (i < entries) {
2215 // The rest do not seem to be inexpensive. Try to allocate a temp and use add.
2216 RegStorage key_temp = AllocTypedTemp(false, kCoreReg, false);
2217 if (key_temp.Valid()) {
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002218 LoadConstantNoClobber(key_temp, starting_key + i);
2219 for (; i < entries - 1; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002220 BasicBlock* case_block =
2221 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2222 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2223 OpRegImm(kOpAdd, key_temp, 1); // Increment key.
2224 }
2225 BasicBlock* case_block =
2226 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2227 OpCmpBranch(kCondEq, rl_src.reg, key_temp, &block_label_list_[case_block->id]);
2228 } else {
2229 // No free temp, just finish the old loop.
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002230 for (; i < entries; i++) {
Andreas Gampe48971b32014-08-06 10:09:01 -07002231 BasicBlock* case_block =
2232 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
Ian Rogers7d4ecd52014-10-30 15:10:02 -07002233 OpCmpImmBranch(kCondEq, rl_src.reg, starting_key + i, &block_label_list_[case_block->id]);
Andreas Gampe48971b32014-08-06 10:09:01 -07002234 }
2235 }
2236 }
2237}
2238
2239void Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002240 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002241 if (cu_->verbose) {
2242 DumpSparseSwitchTable(table);
2243 }
2244
2245 const uint16_t entries = table[1];
2246 if (entries <= kSmallSwitchThreshold) {
2247 GenSmallPackedSwitch(mir, table_offset, rl_src);
2248 } else {
2249 // Use the backend-specific implementation.
2250 GenLargePackedSwitch(mir, table_offset, rl_src);
2251 }
2252}
2253
2254void Mir2Lir::GenSmallSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002255 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002256 const uint16_t entries = table[1];
2257 // Chained cmp-and-branch.
2258 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
2259 const int32_t* targets = &keys[entries];
2260 rl_src = LoadValue(rl_src, kCoreReg);
2261 for (int i = 0; i < entries; i++) {
2262 int key = keys[i];
2263 BasicBlock* case_block =
2264 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
2265 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
2266 }
2267}
2268
2269void Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07002270 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Andreas Gampe48971b32014-08-06 10:09:01 -07002271 if (cu_->verbose) {
2272 DumpSparseSwitchTable(table);
2273 }
2274
2275 const uint16_t entries = table[1];
2276 if (entries <= kSmallSwitchThreshold) {
2277 GenSmallSparseSwitch(mir, table_offset, rl_src);
2278 } else {
2279 // Use the backend-specific implementation.
2280 GenLargeSparseSwitch(mir, table_offset, rl_src);
2281 }
2282}
2283
Fred Shih37f05ef2014-07-16 18:38:08 -07002284bool Mir2Lir::SizeMatchesTypeForEntrypoint(OpSize size, Primitive::Type type) {
2285 switch (size) {
2286 case kReference:
2287 return type == Primitive::kPrimNot;
2288 case k64:
2289 case kDouble:
2290 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
2291 case k32:
2292 case kSingle:
2293 return type == Primitive::kPrimInt || type == Primitive::kPrimFloat;
2294 case kSignedHalf:
2295 return type == Primitive::kPrimShort;
2296 case kUnsignedHalf:
2297 return type == Primitive::kPrimChar;
2298 case kSignedByte:
2299 return type == Primitive::kPrimByte;
2300 case kUnsignedByte:
2301 return type == Primitive::kPrimBoolean;
2302 case kWord: // Intentional fallthrough.
2303 default:
2304 return false; // There are no sane types with this op size.
2305 }
2306}
2307
Brian Carlstrom7940e442013-07-12 13:46:57 -07002308} // namespace art