blob: bb19516b96934929a86c51ba4e0fe9a289685625 [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"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
buzbee2700f7e2014-03-07 09:46:20 -080045LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
46 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 LIR* branch;
48 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080049 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
50 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 branch = OpUnconditionalBranch(tgt);
52 } else {
buzbee2700f7e2014-03-07 09:46:20 -080053 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
55 }
56 // Remember branch target - will process later
57 throw_launchpads_.Insert(tgt);
58 return branch;
59}
60
Mingyao Yange643a172014-04-08 11:02:52 -070061void Mir2Lir::GenDivZeroException() {
62 LIR* branch = OpUnconditionalBranch(nullptr);
63 AddDivZeroCheckSlowPath(branch);
64}
65
66void Mir2Lir::GenDivZeroCheck(ConditionCode c_code) {
Mingyao Yang42894562014-04-07 12:42:16 -070067 LIR* branch = OpCondBranch(c_code, nullptr);
68 AddDivZeroCheckSlowPath(branch);
69}
70
Mingyao Yange643a172014-04-08 11:02:52 -070071void Mir2Lir::GenDivZeroCheck(RegStorage reg) {
72 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
Mingyao Yang42894562014-04-07 12:42:16 -070073 AddDivZeroCheckSlowPath(branch);
74}
75
76void Mir2Lir::AddDivZeroCheckSlowPath(LIR* branch) {
77 class DivZeroCheckSlowPath : public Mir2Lir::LIRSlowPath {
78 public:
79 DivZeroCheckSlowPath(Mir2Lir* m2l, LIR* branch)
80 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
81 }
82
Mingyao Yange643a172014-04-08 11:02:52 -070083 void Compile() OVERRIDE {
Mingyao Yang42894562014-04-07 12:42:16 -070084 m2l_->ResetRegPool();
85 m2l_->ResetDefTracking();
86 GenerateTargetLabel();
87 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowDivZero), true);
88 }
89 };
90
91 AddSlowPath(new (arena_) DivZeroCheckSlowPath(this, branch));
92}
Dave Allisonb373e092014-02-20 16:06:36 -080093
Mingyao Yang9d463142014-04-17 15:22:09 -070094void Mir2Lir::GenArrayBoundsCheck(RegStorage index, RegStorage length) {
95 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
96 public:
97 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, RegStorage index, RegStorage length)
98 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
99 index_(index), length_(length) {
100 }
101
102 void Compile() OVERRIDE {
103 m2l_->ResetRegPool();
104 m2l_->ResetDefTracking();
105 GenerateTargetLabel();
106 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
107 index_, length_, true);
108 }
109
110 private:
111 RegStorage index_;
112 RegStorage length_;
113 };
114
115 LIR* branch = OpCmpBranch(kCondUge, index, length, nullptr);
116 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
117}
118
119void Mir2Lir::GenArrayBoundsCheck(int index, RegStorage length) {
120 class ArrayBoundsCheckSlowPath : public Mir2Lir::LIRSlowPath {
121 public:
122 ArrayBoundsCheckSlowPath(Mir2Lir* m2l, LIR* branch, int index, RegStorage length)
123 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch),
124 index_(index), length_(length) {
125 }
126
127 void Compile() OVERRIDE {
128 m2l_->ResetRegPool();
129 m2l_->ResetDefTracking();
130 GenerateTargetLabel();
131 // kArg0 will be used to hold the constant index.
132 if (length_.GetReg() == m2l_->TargetReg(kArg0).GetReg()) {
133 m2l_->OpRegCopy(m2l_->TargetReg(kArg1), length_);
134 length_ = m2l_->TargetReg(kArg1);
135 }
136 m2l_->LoadConstant(m2l_->TargetReg(kArg0), index_);
137 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pThrowArrayBounds),
138 m2l_->TargetReg(kArg0), length_, true);
139 }
140
141 private:
142 int index_;
143 RegStorage length_;
144 };
145
146 LIR* branch = OpCmpImmBranch(kCondLs, length, index, nullptr);
147 AddSlowPath(new (arena_) ArrayBoundsCheckSlowPath(this, branch, index, length));
148}
149
Mingyao Yange643a172014-04-08 11:02:52 -0700150LIR* Mir2Lir::GenNullCheck(RegStorage reg) {
151 class NullCheckSlowPath : public Mir2Lir::LIRSlowPath {
152 public:
153 NullCheckSlowPath(Mir2Lir* m2l, LIR* branch)
154 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch) {
155 }
156
157 void Compile() OVERRIDE {
158 m2l_->ResetRegPool();
159 m2l_->ResetDefTracking();
160 GenerateTargetLabel();
161 m2l_->CallRuntimeHelper(QUICK_ENTRYPOINT_OFFSET(4, pThrowNullPointer), true);
162 }
163 };
164
165 LIR* branch = OpCmpImmBranch(kCondEq, reg, 0, nullptr);
166 AddSlowPath(new (arena_) NullCheckSlowPath(this, branch));
167 return branch;
168}
169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -0800171LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800172 if (Runtime::Current()->ExplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700173 return GenExplicitNullCheck(m_reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 }
Dave Allisonb373e092014-02-20 16:06:36 -0800175 return nullptr;
176}
177
Dave Allisonf9439142014-03-27 15:10:22 -0700178/* Perform an explicit null-check on a register. */
179LIR* Mir2Lir::GenExplicitNullCheck(RegStorage m_reg, int opt_flags) {
180 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
181 return NULL;
182 }
Mingyao Yange643a172014-04-08 11:02:52 -0700183 return GenNullCheck(m_reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700184}
185
Dave Allisonb373e092014-02-20 16:06:36 -0800186void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
187 if (!Runtime::Current()->ExplicitNullChecks()) {
188 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
189 return;
190 }
191 MarkSafepointPC(last_lir_insn_);
192 }
193}
194
195void Mir2Lir::MarkPossibleStackOverflowException() {
196 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
197 MarkSafepointPC(last_lir_insn_);
198 }
199}
200
buzbee2700f7e2014-03-07 09:46:20 -0800201void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800202 if (!Runtime::Current()->ExplicitNullChecks()) {
203 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
204 return;
205 }
206 // Force an implicit null check by performing a memory operation (load) from the given
207 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800208 RegStorage tmp = AllocTemp();
209 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800210 LIR* load = LoadWordDisp(reg, 0, tmp);
211 FreeTemp(tmp);
212 MarkSafepointPC(load);
213 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214}
215
216/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800217LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700218 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800219 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
220 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
222 // Remember branch target - will process later
223 throw_launchpads_.Insert(tgt);
224 return branch;
225}
226
227void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
228 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700229 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 ConditionCode cond;
231 switch (opcode) {
232 case Instruction::IF_EQ:
233 cond = kCondEq;
234 break;
235 case Instruction::IF_NE:
236 cond = kCondNe;
237 break;
238 case Instruction::IF_LT:
239 cond = kCondLt;
240 break;
241 case Instruction::IF_GE:
242 cond = kCondGe;
243 break;
244 case Instruction::IF_GT:
245 cond = kCondGt;
246 break;
247 case Instruction::IF_LE:
248 cond = kCondLe;
249 break;
250 default:
251 cond = static_cast<ConditionCode>(0);
252 LOG(FATAL) << "Unexpected opcode " << opcode;
253 }
254
255 // Normalize such that if either operand is constant, src2 will be constant
256 if (rl_src1.is_const) {
257 RegLocation rl_temp = rl_src1;
258 rl_src1 = rl_src2;
259 rl_src2 = rl_temp;
260 cond = FlipComparisonOrder(cond);
261 }
262
263 rl_src1 = LoadValue(rl_src1, kCoreReg);
264 // Is this really an immediate comparison?
265 if (rl_src2.is_const) {
266 // If it's already live in a register or not easily materialized, just keep going
267 RegLocation rl_temp = UpdateLoc(rl_src2);
268 if ((rl_temp.location == kLocDalvikFrame) &&
269 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
270 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800271 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 return;
273 }
274 }
275 rl_src2 = LoadValue(rl_src2, kCoreReg);
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
279void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700280 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 ConditionCode cond;
282 rl_src = LoadValue(rl_src, kCoreReg);
283 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
342/*
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,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700348 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700350 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800351 const DexFile* dex_file = cu_->dex_file;
352 CompilerDriver* driver = cu_->compiler_driver;
353 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800355 bool is_type_initialized; // Ignored as an array does not have an initializer.
356 bool use_direct_type_ptr;
357 uintptr_t direct_type_ptr;
358 if (kEmbedClassInCode &&
359 driver->CanEmbedTypeInCode(*dex_file, type_idx,
360 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
361 // The fast path.
362 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800363 LoadClassType(type_idx, kArg0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700364 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800365 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
366 } else {
367 // Use the direct pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700368 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayResolved);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800369 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
370 }
371 } else {
372 // The slow path.
373 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700374 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocArray);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800375 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
376 }
377 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700379 func_offset= QUICK_ENTRYPOINT_OFFSET(4, pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800380 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 RegLocation rl_result = GetReturn(false);
383 StoreValue(rl_dest, rl_result);
384}
385
386/*
387 * Similar to GenNewArray, but with post-allocation initialization.
388 * Verifier guarantees we're dealing with an array class. Current
389 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
390 * Current code also throws internal unimp if not 'L', '[' or 'I'.
391 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700392void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 int elems = info->num_arg_words;
394 int type_idx = info->index;
395 FlushAllRegs(); /* Everything to home location */
Ian Rogersdd7624d2014-03-14 17:43:00 -0700396 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
398 type_idx)) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700399 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700401 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 }
403 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
404 FreeTemp(TargetReg(kArg2));
405 FreeTemp(TargetReg(kArg1));
406 /*
407 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
408 * return region. Because AllocFromCode placed the new array
409 * in kRet0, we'll just lock it into place. When debugger support is
410 * added, it may be necessary to additionally copy all return
411 * values to a home location in thread-local storage
412 */
413 LockTemp(TargetReg(kRet0));
414
415 // TODO: use the correct component size, currently all supported types
416 // share array alignment with ints (see comment at head of function)
417 size_t component_size = sizeof(int32_t);
418
419 // Having a range of 0 is legal
420 if (info->is_range && (elems > 0)) {
421 /*
422 * Bit of ugliness here. We're going generate a mem copy loop
423 * on the register range, but it is possible that some regs
424 * in the range have been promoted. This is unlikely, but
425 * before generating the copy, we'll just force a flush
426 * of any regs in the source range that have been promoted to
427 * home location.
428 */
429 for (int i = 0; i < elems; i++) {
430 RegLocation loc = UpdateLoc(info->args[i]);
431 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800432 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
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 */
buzbee2700f7e2014-03-07 09:46:20 -0800440 RegStorage r_src = AllocTemp();
441 RegStorage r_dst = AllocTemp();
442 RegStorage r_idx = AllocTemp();
443 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700444 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 case kThumb2:
446 r_val = TargetReg(kLr);
447 break;
448 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700449 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 FreeTemp(TargetReg(kRet0));
451 r_val = AllocTemp();
452 break;
453 case kMips:
454 r_val = AllocTemp();
455 break;
456 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
457 }
458 // Set up source pointer
459 RegLocation rl_first = info->args[0];
460 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
461 // Set up the target pointer
462 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
463 mirror::Array::DataOffset(component_size).Int32Value());
464 // Set up the loop counter (known to be > 0)
465 LoadConstant(r_idx, elems - 1);
466 // Generate the copy loop. Going backwards for convenience
467 LIR* target = NewLIR0(kPseudoTargetLabel);
468 // Copy next element
469 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
470 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
471 FreeTemp(r_val);
472 OpDecAndBranch(kCondGe, r_idx, target);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700473 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 // Restore the target pointer
475 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
476 -mirror::Array::DataOffset(component_size).Int32Value());
477 }
478 } else if (!info->is_range) {
479 // TUNING: interleave
480 for (int i = 0; i < elems; i++) {
481 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
482 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800483 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
484 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800486 if (IsTemp(rl_arg.reg)) {
487 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 }
489 }
490 }
491 if (info->result.location != kLocInvalid) {
492 StoreValue(info->result, GetReturn(false /* not fp */));
493 }
494}
495
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800496//
497// Slow path to ensure a class is initialized for sget/sput.
498//
499class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
500 public:
buzbee2700f7e2014-03-07 09:46:20 -0800501 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
502 RegStorage r_base) :
503 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
504 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800505 }
506
507 void Compile() {
508 LIR* unresolved_target = GenerateTargetLabel();
509 uninit_->target = unresolved_target;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700510 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800511 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800512 // Copy helper's result into r_base, a no-op on all but MIPS.
513 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
514
515 m2l_->OpUnconditionalBranch(cont_);
516 }
517
518 private:
519 LIR* const uninit_;
520 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800521 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800522};
523
Vladimir Markobe0e5462014-02-26 11:24:15 +0000524void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700525 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000526 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
527 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
528 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
529 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800530 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000531 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 // Fast path, static storage base is this method's class
533 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800534 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800535 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
536 if (IsTemp(rl_method.reg)) {
537 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 }
539 } else {
540 // Medium path, static storage base in a different class which requires checks that the other
541 // class is initialized.
542 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000543 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 // May do runtime call so everything to home locations.
545 FlushAllRegs();
546 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800547 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 LockTemp(r_method);
549 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800550 r_base = TargetReg(kArg0);
551 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800552 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800553 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800555 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000556 if (!field_info.IsInitialized() &&
557 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800558 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800559
560 // The slow path is invoked if the r_base is NULL or the class pointed
561 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800562 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800563 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800564 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800565 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800566 mirror::Class::StatusOffset().Int32Value(),
567 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800568 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800569
buzbee2700f7e2014-03-07 09:46:20 -0800570 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000571 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800572
573 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 FreeTemp(r_method);
576 }
577 // rBase now holds static storage base
578 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700579 RegisterClass register_kind = kAnyReg;
580 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
581 // Force long/double volatile stores into SSE registers to avoid tearing.
582 register_kind = kFPReg;
583 }
584 rl_src = LoadValueWide(rl_src, register_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 } else {
586 rl_src = LoadValue(rl_src, kAnyReg);
587 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000588 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800589 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 GenMemBarrier(kStoreStore);
591 }
592 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800593 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800595 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000597 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800598 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 GenMemBarrier(kStoreLoad);
600 }
601 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800602 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 } else {
606 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700607 ThreadOffset<4> setter_offset =
608 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Static)
609 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjStatic)
610 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000611 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 }
613}
614
Vladimir Markobe0e5462014-02-26 11:24:15 +0000615void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700616 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000617 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
618 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
619 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
620 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800621 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000622 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 // Fast path, static storage base is this method's class
624 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800625 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800626 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 } else {
628 // Medium path, static storage base in a different class which requires checks that the other
629 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000630 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 // May do runtime call so everything to home locations.
632 FlushAllRegs();
633 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800634 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 LockTemp(r_method);
636 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800637 r_base = TargetReg(kArg0);
638 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800639 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800640 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000641 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800642 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000643 if (!field_info.IsInitialized() &&
644 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800645 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800646
647 // The slow path is invoked if the r_base is NULL or the class pointed
648 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800649 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800650 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800651 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800652 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800653 mirror::Class::StatusOffset().Int32Value(),
654 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800655 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800656
buzbee2700f7e2014-03-07 09:46:20 -0800657 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000658 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800659
660 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 FreeTemp(r_method);
663 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800664 // r_base now holds static storage base
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700665 RegisterClass result_reg_kind = kAnyReg;
666 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
667 // Force long/double volatile loads into SSE registers to avoid tearing.
668 result_reg_kind = kFPReg;
669 }
670 RegLocation rl_result = EvalLoc(rl_dest, result_reg_kind, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800671
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800673 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800675 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800677 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800678
679 if (field_info.IsVolatile()) {
680 // Without context sensitive analysis, we must issue the most conservative barriers.
681 // In this case, either a load or store may follow so we issue both barriers.
682 GenMemBarrier(kLoadLoad);
683 GenMemBarrier(kLoadStore);
684 }
685
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 if (is_long_or_double) {
687 StoreValueWide(rl_dest, rl_result);
688 } else {
689 StoreValue(rl_dest, rl_result);
690 }
691 } else {
692 FlushAllRegs(); // Everything to home locations
Ian Rogersdd7624d2014-03-14 17:43:00 -0700693 ThreadOffset<4> getterOffset =
694 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Static)
695 :(is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjStatic)
696 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000697 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 if (is_long_or_double) {
699 RegLocation rl_result = GetReturnWide(rl_dest.fp);
700 StoreValueWide(rl_dest, rl_result);
701 } else {
702 RegLocation rl_result = GetReturn(rl_dest.fp);
703 StoreValue(rl_dest, rl_result);
704 }
705 }
706}
707
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800708// Generate code for all slow paths.
709void Mir2Lir::HandleSlowPaths() {
710 int n = slow_paths_.Size();
711 for (int i = 0; i < n; ++i) {
712 LIRSlowPath* slowpath = slow_paths_.Get(i);
713 slowpath->Compile();
714 }
715 slow_paths_.Reset();
716}
717
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700718void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 int num_elems = suspend_launchpads_.Size();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700720 ThreadOffset<4> helper_offset = QUICK_ENTRYPOINT_OFFSET(4, pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 for (int i = 0; i < num_elems; i++) {
722 ResetRegPool();
723 ResetDefTracking();
724 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700725 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 current_dalvik_offset_ = lab->operands[1];
727 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800728 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
730 OpUnconditionalBranch(resume_lab);
731 }
732}
733
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700734void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 int num_elems = throw_launchpads_.Size();
736 for (int i = 0; i < num_elems; i++) {
737 ResetRegPool();
738 ResetDefTracking();
739 LIR* lab = throw_launchpads_.Get(i);
740 current_dalvik_offset_ = lab->operands[1];
741 AppendLIR(lab);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700742 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 int v1 = lab->operands[2];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 switch (lab->operands[0]) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800746 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 func_offset =
Ian Rogersdd7624d2014-03-14 17:43:00 -0700748 QUICK_ENTRYPOINT_OFFSET(4, pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 default:
751 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
752 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000753 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800754 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700755 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 }
757}
758
Vladimir Markobe0e5462014-02-26 11:24:15 +0000759void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700761 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000762 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
763 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
764 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 RegLocation rl_result;
766 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000767 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 rl_obj = LoadValue(rl_obj, kCoreReg);
769 if (is_long_or_double) {
770 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800771 GenNullCheck(rl_obj.reg, opt_flags);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700772 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700773 RegisterClass result_reg_kind = kAnyReg;
774 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
775 // Force long/double volatile loads into SSE registers to avoid tearing.
776 result_reg_kind = kFPReg;
777 }
778 rl_result = EvalLoc(rl_dest, result_reg_kind, true);
buzbee2700f7e2014-03-07 09:46:20 -0800779 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
780 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800781 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000782 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800783 // Without context sensitive analysis, we must issue the most conservative barriers.
784 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800786 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 }
788 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800789 RegStorage reg_ptr = AllocTemp();
790 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800792 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Dave Allisonf9439142014-03-27 15:10:22 -0700793 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000794 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795 // Without context sensitive analysis, we must issue the most conservative barriers.
796 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800798 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 }
800 FreeTemp(reg_ptr);
801 }
802 StoreValueWide(rl_dest, rl_result);
803 } else {
804 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800805 GenNullCheck(rl_obj.reg, opt_flags);
806 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
807 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800808 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000809 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800810 // Without context sensitive analysis, we must issue the most conservative barriers.
811 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800813 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 }
815 StoreValue(rl_dest, rl_result);
816 }
817 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700818 ThreadOffset<4> getterOffset =
819 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pGet64Instance)
820 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pGetObjInstance)
821 : QUICK_ENTRYPOINT_OFFSET(4, pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000822 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 if (is_long_or_double) {
824 RegLocation rl_result = GetReturnWide(rl_dest.fp);
825 StoreValueWide(rl_dest, rl_result);
826 } else {
827 RegLocation rl_result = GetReturn(rl_dest.fp);
828 StoreValue(rl_dest, rl_result);
829 }
830 }
831}
832
Vladimir Markobe0e5462014-02-26 11:24:15 +0000833void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700835 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000836 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
837 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
838 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000840 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 rl_obj = LoadValue(rl_obj, kCoreReg);
842 if (is_long_or_double) {
Yevgeny Roubanb4b06672014-04-16 18:13:10 +0700843 RegisterClass src_reg_kind = kAnyReg;
844 if (field_info.IsVolatile() && cu_->instruction_set == kX86) {
845 // Force long/double volatile stores into SSE registers to avoid tearing.
846 src_reg_kind = kFPReg;
847 }
848 rl_src = LoadValueWide(rl_src, src_reg_kind);
buzbee2700f7e2014-03-07 09:46:20 -0800849 GenNullCheck(rl_obj.reg, opt_flags);
850 RegStorage reg_ptr = AllocTemp();
851 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000852 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800853 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 GenMemBarrier(kStoreStore);
855 }
buzbee2700f7e2014-03-07 09:46:20 -0800856 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800857 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000858 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800859 // A load might follow the volatile store so insert a StoreLoad barrier.
860 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 }
862 FreeTemp(reg_ptr);
863 } else {
864 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800865 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000866 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800867 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 GenMemBarrier(kStoreStore);
869 }
buzbee2700f7e2014-03-07 09:46:20 -0800870 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800871 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000872 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800873 // A load might follow the volatile store so insert a StoreLoad barrier.
874 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 }
876 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800877 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 }
879 }
880 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700881 ThreadOffset<4> setter_offset =
882 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(4, pSet64Instance)
883 : (is_object ? QUICK_ENTRYPOINT_OFFSET(4, pSetObjInstance)
884 : QUICK_ENTRYPOINT_OFFSET(4, pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000885 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
886 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 }
888}
889
Ian Rogersa9a82542013-10-04 11:17:26 -0700890void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
891 RegLocation rl_src) {
892 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
893 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
894 (opt_flags & MIR_IGNORE_NULL_CHECK));
Ian Rogersdd7624d2014-03-14 17:43:00 -0700895 ThreadOffset<4> helper = needs_range_check
896 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithNullAndBoundCheck)
897 : QUICK_ENTRYPOINT_OFFSET(4, pAputObjectWithBoundCheck))
898 : QUICK_ENTRYPOINT_OFFSET(4, pAputObject);
Ian Rogersa9a82542013-10-04 11:17:26 -0700899 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800904 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
906 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
907 *cu_->dex_file,
908 type_idx)) {
909 // Call out to helper which resolves type and verifies access.
910 // Resolved type returned in kRet0.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700911 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800912 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 RegLocation rl_result = GetReturn(false);
914 StoreValue(rl_dest, rl_result);
915 } else {
916 // We're don't need access checks, load type from dex cache
917 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700918 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800919 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 int32_t offset_of_type =
921 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
922 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800923 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
925 type_idx) || SLOW_TYPE_PATH) {
926 // Slow path, at runtime test if type is null and if so initialize
927 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800928 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800929 LIR* cont = NewLIR0(kPseudoTargetLabel);
930
931 // Object to generate the slow path for class resolution.
932 class SlowPath : public LIRSlowPath {
933 public:
934 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
935 const RegLocation& rl_method, const RegLocation& rl_result) :
936 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
937 rl_method_(rl_method), rl_result_(rl_result) {
938 }
939
940 void Compile() {
941 GenerateTargetLabel();
942
Ian Rogersdd7624d2014-03-14 17:43:00 -0700943 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800944 rl_method_.reg, true);
945 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800946
947 m2l_->OpUnconditionalBranch(cont_);
948 }
949
950 private:
951 const int type_idx_;
952 const RegLocation rl_method_;
953 const RegLocation rl_result_;
954 };
955
956 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800957 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800958
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800960 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 // Fast path, we're done - just store result
962 StoreValue(rl_dest, rl_result);
963 }
964 }
965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 /* NOTE: Most strings should be available at compile time */
969 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
970 (sizeof(mirror::String*) * string_idx);
971 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
972 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
973 // slow path, resolve string if not in dex cache
974 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700975 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800976
977 // If the Method* is already in a register, we can save a copy.
978 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800979 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800980 if (rl_method.location == kLocPhysReg) {
981 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800982 DCHECK(!IsTemp(rl_method.reg));
983 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800984 } else {
985 r_method = TargetReg(kArg2);
986 LoadCurrMethodDirect(r_method);
987 }
988 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
989 TargetReg(kArg0));
990
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800993 if (cu_->instruction_set == kThumb2 ||
994 cu_->instruction_set == kMips) {
995 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800996 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800997 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
998 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -08001000
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001001 // Object to generate the slow path for string resolution.
1002 class SlowPath : public LIRSlowPath {
1003 public:
buzbee2700f7e2014-03-07 09:46:20 -08001004 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001005 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
1006 }
1007
1008 void Compile() {
1009 GenerateTargetLabel();
1010
Dave Allisond6ed6422014-04-09 23:36:15 +00001011 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(4, pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001012
Dave Allisond6ed6422014-04-09 23:36:15 +00001013 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
1014 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
1015 m2l_->MarkSafepointPC(call_inst);
1016 m2l_->FreeTemp(r_tgt);
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001017
1018 m2l_->OpUnconditionalBranch(cont_);
1019 }
1020
1021 private:
buzbee2700f7e2014-03-07 09:46:20 -08001022 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001023 };
1024
1025 // Add to list for future.
1026 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 } else {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001028 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
Mark Mendell766e9292014-01-27 07:55:47 -08001029 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
1030 LoadConstant(TargetReg(kArg1), string_idx);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001031 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pResolveString), r_method, TargetReg(kArg1),
buzbee2700f7e2014-03-07 09:46:20 -08001032 true);
Mark Mendell766e9292014-01-27 07:55:47 -08001033 LIR* target = NewLIR0(kPseudoTargetLabel);
1034 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036 GenBarrier();
1037 StoreValue(rl_dest, GetReturn(false));
1038 } else {
1039 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -08001040 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001042 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
1043 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 StoreValue(rl_dest, rl_result);
1045 }
1046}
1047
1048/*
1049 * Let helper function take care of everything. Will
1050 * call Class::NewInstanceFromCode(type_idx, method);
1051 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 FlushAllRegs(); /* Everything to home location */
1054 // alloc will always check for resolution, do we also need to verify
1055 // access because the verifier was unable to?
Ian Rogersdd7624d2014-03-14 17:43:00 -07001056 ThreadOffset<4> func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001057 const DexFile* dex_file = cu_->dex_file;
1058 CompilerDriver* driver = cu_->compiler_driver;
1059 if (driver->CanAccessInstantiableTypeWithoutChecks(
1060 cu_->method_idx, *dex_file, type_idx)) {
1061 bool is_type_initialized;
1062 bool use_direct_type_ptr;
1063 uintptr_t direct_type_ptr;
1064 if (kEmbedClassInCode &&
1065 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1066 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1067 // The fast path.
1068 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001069 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001070 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001071 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001072 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1073 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001074 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001075 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1076 }
1077 } else {
1078 // Use the direct pointer.
1079 if (!is_type_initialized) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001080 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectResolved);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001081 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1082 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001083 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectInitialized);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001084 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1085 }
1086 }
1087 } else {
1088 // The slow path.
1089 DCHECK_EQ(func_offset.Int32Value(), -1);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001090 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObject);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001091 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1092 }
1093 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 } else {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001095 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001096 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 RegLocation rl_result = GetReturn(false);
1099 StoreValue(rl_dest, rl_result);
1100}
1101
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001102void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001104 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105}
1106
1107// For final classes there are no sub-classes to check and so we can answer the instance-of
1108// question with simple comparisons.
1109void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1110 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001111 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001112 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001113
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 RegLocation object = LoadValue(rl_src, kCoreReg);
1115 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001116 RegStorage result_reg = rl_result.reg;
1117 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 result_reg = AllocTypedTemp(false, kCoreReg);
1119 }
1120 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001121 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122
buzbee2700f7e2014-03-07 09:46:20 -08001123 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1124 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125
1126 LoadCurrMethodDirect(check_class);
1127 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001128 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1129 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001131 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001133 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 int32_t offset_of_type =
1135 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1136 (sizeof(mirror::Class*) * type_idx);
1137 LoadWordDisp(check_class, offset_of_type, check_class);
1138 }
1139
1140 LIR* ne_branchover = NULL;
1141 if (cu_->instruction_set == kThumb2) {
1142 OpRegReg(kOpCmp, check_class, object_class); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001143 LIR* it = OpIT(kCondEq, ""); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 LoadConstant(result_reg, 1); // .eq case - load true
Dave Allison3da67a52014-04-02 17:03:45 -07001145 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 } else {
1147 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1148 LoadConstant(result_reg, 1); // eq case - load true
1149 }
1150 LIR* target = NewLIR0(kPseudoTargetLabel);
1151 null_branchover->target = target;
1152 if (ne_branchover != NULL) {
1153 ne_branchover->target = target;
1154 }
1155 FreeTemp(object_class);
1156 FreeTemp(check_class);
1157 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001158 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 FreeTemp(result_reg);
1160 }
1161 StoreValue(rl_dest, rl_result);
1162}
1163
1164void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1165 bool type_known_abstract, bool use_declaring_class,
1166 bool can_assume_type_is_in_dex_cache,
1167 uint32_t type_idx, RegLocation rl_dest,
1168 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001169 // X86 has its own implementation.
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001170 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Mark Mendell6607d972014-02-10 06:54:18 -08001171
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 FlushAllRegs();
1173 // May generate a call - use explicit registers
1174 LockCallTemps();
1175 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001176 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 if (needs_access_check) {
1178 // Check we have access to type_idx and if not throw IllegalAccessError,
1179 // returns Class* in kArg0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001180 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 type_idx, true);
1182 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1183 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1184 } else if (use_declaring_class) {
1185 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001186 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1187 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 } else {
1189 // Load dex cache entry into class_reg (kArg2)
1190 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001191 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1192 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 int32_t offset_of_type =
1194 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1195 * type_idx);
1196 LoadWordDisp(class_reg, offset_of_type, class_reg);
1197 if (!can_assume_type_is_in_dex_cache) {
1198 // Need to test presence of type in dex cache at runtime
1199 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1200 // Not resolved
1201 // Call out to helper, which will return resolved type in kRet0
Ian Rogersdd7624d2014-03-14 17:43:00 -07001202 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001203 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1205 // Rejoin code paths
1206 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1207 hop_branch->target = hop_target;
1208 }
1209 }
1210 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1211 RegLocation rl_result = GetReturn(false);
1212 if (cu_->instruction_set == kMips) {
1213 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001214 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 }
1216 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1217
1218 /* load object->klass_ */
1219 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1220 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1221 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1222 LIR* branchover = NULL;
1223 if (type_known_final) {
1224 // rl_result == ref == null == 0.
1225 if (cu_->instruction_set == kThumb2) {
1226 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001227 LIR* it = OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001228 LoadConstant(rl_result.reg, 1); // .eq case - load true
1229 LoadConstant(rl_result.reg, 0); // .ne case - load false
Dave Allison3da67a52014-04-02 17:03:45 -07001230 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001232 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001234 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 }
1236 } else {
1237 if (cu_->instruction_set == kThumb2) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001238 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Dave Allison3da67a52014-04-02 17:03:45 -07001239 LIR* it = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 if (!type_known_abstract) {
1241 /* Uses conditional nullification */
1242 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
Dave Allison3da67a52014-04-02 17:03:45 -07001243 it = OpIT(kCondEq, "EE"); // if-convert the test
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1245 }
1246 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1247 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
Dave Allison3da67a52014-04-02 17:03:45 -07001248 if (it != nullptr) {
1249 OpEndIT(it);
1250 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 FreeTemp(r_tgt);
1252 } else {
1253 if (!type_known_abstract) {
1254 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001255 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1257 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001258 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(4, pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001259 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1260 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1261 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 }
1263 }
1264 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001265 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 /* branch targets here */
1267 LIR* target = NewLIR0(kPseudoTargetLabel);
1268 StoreValue(rl_dest, rl_result);
1269 branch1->target = target;
1270 if (branchover != NULL) {
1271 branchover->target = target;
1272 }
1273}
1274
1275void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1276 bool type_known_final, type_known_abstract, use_declaring_class;
1277 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1278 *cu_->dex_file,
1279 type_idx,
1280 &type_known_final,
1281 &type_known_abstract,
1282 &use_declaring_class);
1283 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1284 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1285
1286 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1287 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1288 } else {
1289 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1290 use_declaring_class, can_assume_type_is_in_dex_cache,
1291 type_idx, rl_dest, rl_src);
1292 }
1293}
1294
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001295void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 bool type_known_final, type_known_abstract, use_declaring_class;
1297 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1298 *cu_->dex_file,
1299 type_idx,
1300 &type_known_final,
1301 &type_known_abstract,
1302 &use_declaring_class);
1303 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1304 // of the exception throw path.
1305 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001306 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 // Verifier type analysis proved this check cast would never cause an exception.
1308 return;
1309 }
1310 FlushAllRegs();
1311 // May generate a call - use explicit registers
1312 LockCallTemps();
1313 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001314 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001315 if (needs_access_check) {
1316 // Check we have access to type_idx and if not throw IllegalAccessError,
1317 // returns Class* in kRet0
1318 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001319 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 type_idx, TargetReg(kArg1), true);
1321 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1322 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001323 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1324 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 } else {
1326 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001327 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1328 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 int32_t offset_of_type =
1330 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1331 (sizeof(mirror::Class*) * type_idx);
1332 LoadWordDisp(class_reg, offset_of_type, class_reg);
1333 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1334 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001335 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1336 LIR* cont = NewLIR0(kPseudoTargetLabel);
1337
1338 // Slow path to initialize the type. Executed if the type is NULL.
1339 class SlowPath : public LIRSlowPath {
1340 public:
1341 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001342 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001343 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1344 class_reg_(class_reg) {
1345 }
1346
1347 void Compile() {
1348 GenerateTargetLabel();
1349
1350 // Call out to helper, which will return resolved type in kArg0
1351 // InitializeTypeFromCode(idx, method)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001352 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(4, pInitializeType), type_idx_,
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001353 m2l_->TargetReg(kArg1), true);
1354 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1355 m2l_->OpUnconditionalBranch(cont_);
1356 }
1357 public:
1358 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001359 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001360 };
1361
buzbee2700f7e2014-03-07 09:46:20 -08001362 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 }
1364 }
1365 // At this point, class_reg (kArg2) has class
1366 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001367
1368 // Slow path for the case where the classes are not equal. In this case we need
1369 // to call a helper function to do the check.
1370 class SlowPath : public LIRSlowPath {
1371 public:
1372 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1373 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1374 }
1375
1376 void Compile() {
1377 GenerateTargetLabel();
1378
1379 if (load_) {
1380 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1381 m2l_->TargetReg(kArg1));
1382 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001383 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pCheckCast), m2l_->TargetReg(kArg2),
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001384 m2l_->TargetReg(kArg1), true);
1385
1386 m2l_->OpUnconditionalBranch(cont_);
1387 }
1388
1389 private:
1390 bool load_;
1391 };
1392
1393 if (type_known_abstract) {
1394 // Easier case, run slow path if target is non-null (slow path will load from target)
1395 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1396 LIR* cont = NewLIR0(kPseudoTargetLabel);
1397 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1398 } else {
1399 // Harder, more common case. We need to generate a forward branch over the load
1400 // if the target is null. If it's non-null we perform the load and branch to the
1401 // slow path if the classes are not equal.
1402
1403 /* Null is OK - continue */
1404 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1405 /* load object->klass_ */
1406 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001407 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001408
1409 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1410 LIR* cont = NewLIR0(kPseudoTargetLabel);
1411
1412 // Add the slow path that will not perform load since this is already done.
1413 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1414
1415 // Set the null check to branch to the continuation.
1416 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 }
1418}
1419
1420void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001421 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 RegLocation rl_result;
1423 if (cu_->instruction_set == kThumb2) {
1424 /*
1425 * NOTE: This is the one place in the code in which we might have
1426 * as many as six live temporary registers. There are 5 in the normal
1427 * set for Arm. Until we have spill capabilities, temporarily add
1428 * lr to the temp set. It is safe to do this locally, but note that
1429 * lr is used explicitly elsewhere in the code generator and cannot
1430 * normally be used as a general temp register.
1431 */
1432 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1433 FreeTemp(TargetReg(kLr)); // and make it available
1434 }
1435 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1436 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1437 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1438 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001439 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1440 RegStorage t_reg = AllocTemp();
1441 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1442 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1443 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 FreeTemp(t_reg);
1445 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001446 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1447 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 }
1449 /*
1450 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1451 * following StoreValueWide might need to allocate a temp register.
1452 * To further work around the lack of a spill capability, explicitly
1453 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1454 * Remove when spill is functional.
1455 */
1456 FreeRegLocTemps(rl_result, rl_src1);
1457 FreeRegLocTemps(rl_result, rl_src2);
1458 StoreValueWide(rl_dest, rl_result);
1459 if (cu_->instruction_set == kThumb2) {
1460 Clobber(TargetReg(kLr));
1461 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1462 }
1463}
1464
1465
1466void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001467 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001468 ThreadOffset<4> func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469
1470 switch (opcode) {
1471 case Instruction::SHL_LONG:
1472 case Instruction::SHL_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001473 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 break;
1475 case Instruction::SHR_LONG:
1476 case Instruction::SHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001477 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 break;
1479 case Instruction::USHR_LONG:
1480 case Instruction::USHR_LONG_2ADDR:
Ian Rogersdd7624d2014-03-14 17:43:00 -07001481 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 break;
1483 default:
1484 LOG(FATAL) << "Unexpected case";
1485 }
1486 FlushAllRegs(); /* Send everything to home location */
1487 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1488 RegLocation rl_result = GetReturnWide(false);
1489 StoreValueWide(rl_dest, rl_result);
1490}
1491
1492
1493void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001494 RegLocation rl_src1, RegLocation rl_src2) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001495 DCHECK(cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 OpKind op = kOpBkpt;
1497 bool is_div_rem = false;
1498 bool check_zero = false;
1499 bool unary = false;
1500 RegLocation rl_result;
1501 bool shift_op = false;
1502 switch (opcode) {
1503 case Instruction::NEG_INT:
1504 op = kOpNeg;
1505 unary = true;
1506 break;
1507 case Instruction::NOT_INT:
1508 op = kOpMvn;
1509 unary = true;
1510 break;
1511 case Instruction::ADD_INT:
1512 case Instruction::ADD_INT_2ADDR:
1513 op = kOpAdd;
1514 break;
1515 case Instruction::SUB_INT:
1516 case Instruction::SUB_INT_2ADDR:
1517 op = kOpSub;
1518 break;
1519 case Instruction::MUL_INT:
1520 case Instruction::MUL_INT_2ADDR:
1521 op = kOpMul;
1522 break;
1523 case Instruction::DIV_INT:
1524 case Instruction::DIV_INT_2ADDR:
1525 check_zero = true;
1526 op = kOpDiv;
1527 is_div_rem = true;
1528 break;
1529 /* NOTE: returns in kArg1 */
1530 case Instruction::REM_INT:
1531 case Instruction::REM_INT_2ADDR:
1532 check_zero = true;
1533 op = kOpRem;
1534 is_div_rem = true;
1535 break;
1536 case Instruction::AND_INT:
1537 case Instruction::AND_INT_2ADDR:
1538 op = kOpAnd;
1539 break;
1540 case Instruction::OR_INT:
1541 case Instruction::OR_INT_2ADDR:
1542 op = kOpOr;
1543 break;
1544 case Instruction::XOR_INT:
1545 case Instruction::XOR_INT_2ADDR:
1546 op = kOpXor;
1547 break;
1548 case Instruction::SHL_INT:
1549 case Instruction::SHL_INT_2ADDR:
1550 shift_op = true;
1551 op = kOpLsl;
1552 break;
1553 case Instruction::SHR_INT:
1554 case Instruction::SHR_INT_2ADDR:
1555 shift_op = true;
1556 op = kOpAsr;
1557 break;
1558 case Instruction::USHR_INT:
1559 case Instruction::USHR_INT_2ADDR:
1560 shift_op = true;
1561 op = kOpLsr;
1562 break;
1563 default:
1564 LOG(FATAL) << "Invalid word arith op: " << opcode;
1565 }
1566 if (!is_div_rem) {
1567 if (unary) {
1568 rl_src1 = LoadValue(rl_src1, kCoreReg);
1569 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001570 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 } else {
1572 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001573 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001574 RegStorage t_reg = AllocTemp();
1575 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 rl_src1 = LoadValue(rl_src1, kCoreReg);
1577 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001578 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 FreeTemp(t_reg);
1580 } else {
1581 rl_src1 = LoadValue(rl_src1, kCoreReg);
1582 rl_src2 = LoadValue(rl_src2, kCoreReg);
1583 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001584 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
1586 }
1587 StoreValue(rl_dest, rl_result);
1588 } else {
Dave Allison70202782013-10-22 17:52:19 -07001589 bool done = false; // Set to true if we happen to find a way to use a real instruction.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 if (cu_->instruction_set == kMips) {
1591 rl_src1 = LoadValue(rl_src1, kCoreReg);
1592 rl_src2 = LoadValue(rl_src2, kCoreReg);
1593 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001594 GenDivZeroCheck(rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 }
buzbee2700f7e2014-03-07 09:46:20 -08001596 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001597 done = true;
1598 } else if (cu_->instruction_set == kThumb2) {
1599 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1600 // Use ARM SDIV instruction for division. For remainder we also need to
1601 // calculate using a MUL and subtract.
1602 rl_src1 = LoadValue(rl_src1, kCoreReg);
1603 rl_src2 = LoadValue(rl_src2, kCoreReg);
1604 if (check_zero) {
Mingyao Yangd15f4e22014-04-17 18:46:24 -07001605 GenDivZeroCheck(rl_src2.reg);
Dave Allison70202782013-10-22 17:52:19 -07001606 }
buzbee2700f7e2014-03-07 09:46:20 -08001607 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001608 done = true;
1609 }
1610 }
1611
1612 // If we haven't already generated the code use the callout function.
1613 if (!done) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001614 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 FlushAllRegs(); /* Send everything to home location */
1616 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001617 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1619 if (check_zero) {
Mingyao Yange643a172014-04-08 11:02:52 -07001620 GenDivZeroCheck(TargetReg(kArg1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 }
Dave Allison70202782013-10-22 17:52:19 -07001622 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001623 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001624 if (op == kOpDiv)
1625 rl_result = GetReturn(false);
1626 else
1627 rl_result = GetReturnAlt();
1628 }
1629 StoreValue(rl_dest, rl_result);
1630 }
1631}
1632
1633/*
1634 * The following are the first-level codegen routines that analyze the format
1635 * of each bytecode then either dispatch special purpose codegen routines
1636 * or produce corresponding Thumb instructions directly.
1637 */
1638
Brian Carlstrom7940e442013-07-12 13:46:57 -07001639// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001640static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 x &= x - 1;
1642 return (x & (x - 1)) == 0;
1643}
1644
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1646// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001647bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001648 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1650 return false;
1651 }
1652 // No divide instruction for Arm, so check for more special cases
1653 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001654 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 }
1656 int k = LowestSetBit(lit);
1657 if (k >= 30) {
1658 // Avoid special cases.
1659 return false;
1660 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 rl_src = LoadValue(rl_src, kCoreReg);
1662 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001663 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001664 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001665 if (lit == 2) {
1666 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001667 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1668 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1669 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001670 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001671 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001673 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1674 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 }
1676 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001677 RegStorage t_reg1 = AllocTemp();
1678 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001679 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001680 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1681 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001682 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001683 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001685 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001687 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001688 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001689 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 }
1691 }
1692 StoreValue(rl_dest, rl_result);
1693 return true;
1694}
1695
1696// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1697// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001698bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001699 if (lit < 0) {
1700 return false;
1701 }
1702 if (lit == 0) {
1703 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1704 LoadConstant(rl_result.reg, 0);
1705 StoreValue(rl_dest, rl_result);
1706 return true;
1707 }
1708 if (lit == 1) {
1709 rl_src = LoadValue(rl_src, kCoreReg);
1710 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1711 OpRegCopy(rl_result.reg, rl_src.reg);
1712 StoreValue(rl_dest, rl_result);
1713 return true;
1714 }
Zheng Xuf9719f92014-04-02 13:31:31 +01001715 // There is RegRegRegShift on Arm, so check for more special cases
1716 if (cu_->instruction_set == kThumb2) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001717 return EasyMultiply(rl_src, rl_dest, lit);
1718 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001719 // Can we simplify this multiplication?
1720 bool power_of_two = false;
1721 bool pop_count_le2 = false;
1722 bool power_of_two_minus_one = false;
Ian Rogerse2143c02014-03-28 08:47:16 -07001723 if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724 power_of_two = true;
1725 } else if (IsPopCountLE2(lit)) {
1726 pop_count_le2 = true;
1727 } else if (IsPowerOfTwo(lit + 1)) {
1728 power_of_two_minus_one = true;
1729 } else {
1730 return false;
1731 }
1732 rl_src = LoadValue(rl_src, kCoreReg);
1733 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1734 if (power_of_two) {
1735 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001736 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001737 } else if (pop_count_le2) {
1738 // Shift and add and shift.
1739 int first_bit = LowestSetBit(lit);
1740 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1741 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1742 } else {
1743 // Reverse subtract: (src << (shift + 1)) - src.
1744 DCHECK(power_of_two_minus_one);
1745 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001746 RegStorage t_reg = AllocTemp();
1747 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1748 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001749 }
1750 StoreValue(rl_dest, rl_result);
1751 return true;
1752}
1753
1754void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001755 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001756 RegLocation rl_result;
1757 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1758 int shift_op = false;
1759 bool is_div = false;
1760
1761 switch (opcode) {
1762 case Instruction::RSUB_INT_LIT8:
1763 case Instruction::RSUB_INT: {
1764 rl_src = LoadValue(rl_src, kCoreReg);
1765 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1766 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001767 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001768 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001769 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1770 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001771 }
1772 StoreValue(rl_dest, rl_result);
1773 return;
1774 }
1775
1776 case Instruction::SUB_INT:
1777 case Instruction::SUB_INT_2ADDR:
1778 lit = -lit;
1779 // Intended fallthrough
1780 case Instruction::ADD_INT:
1781 case Instruction::ADD_INT_2ADDR:
1782 case Instruction::ADD_INT_LIT8:
1783 case Instruction::ADD_INT_LIT16:
1784 op = kOpAdd;
1785 break;
1786 case Instruction::MUL_INT:
1787 case Instruction::MUL_INT_2ADDR:
1788 case Instruction::MUL_INT_LIT8:
1789 case Instruction::MUL_INT_LIT16: {
1790 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1791 return;
1792 }
1793 op = kOpMul;
1794 break;
1795 }
1796 case Instruction::AND_INT:
1797 case Instruction::AND_INT_2ADDR:
1798 case Instruction::AND_INT_LIT8:
1799 case Instruction::AND_INT_LIT16:
1800 op = kOpAnd;
1801 break;
1802 case Instruction::OR_INT:
1803 case Instruction::OR_INT_2ADDR:
1804 case Instruction::OR_INT_LIT8:
1805 case Instruction::OR_INT_LIT16:
1806 op = kOpOr;
1807 break;
1808 case Instruction::XOR_INT:
1809 case Instruction::XOR_INT_2ADDR:
1810 case Instruction::XOR_INT_LIT8:
1811 case Instruction::XOR_INT_LIT16:
1812 op = kOpXor;
1813 break;
1814 case Instruction::SHL_INT_LIT8:
1815 case Instruction::SHL_INT:
1816 case Instruction::SHL_INT_2ADDR:
1817 lit &= 31;
1818 shift_op = true;
1819 op = kOpLsl;
1820 break;
1821 case Instruction::SHR_INT_LIT8:
1822 case Instruction::SHR_INT:
1823 case Instruction::SHR_INT_2ADDR:
1824 lit &= 31;
1825 shift_op = true;
1826 op = kOpAsr;
1827 break;
1828 case Instruction::USHR_INT_LIT8:
1829 case Instruction::USHR_INT:
1830 case Instruction::USHR_INT_2ADDR:
1831 lit &= 31;
1832 shift_op = true;
1833 op = kOpLsr;
1834 break;
1835
1836 case Instruction::DIV_INT:
1837 case Instruction::DIV_INT_2ADDR:
1838 case Instruction::DIV_INT_LIT8:
1839 case Instruction::DIV_INT_LIT16:
1840 case Instruction::REM_INT:
1841 case Instruction::REM_INT_2ADDR:
1842 case Instruction::REM_INT_LIT8:
1843 case Instruction::REM_INT_LIT16: {
1844 if (lit == 0) {
Mingyao Yange643a172014-04-08 11:02:52 -07001845 GenDivZeroException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 return;
1847 }
buzbee11b63d12013-08-27 07:34:17 -07001848 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001850 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001851 (opcode == Instruction::DIV_INT_LIT16)) {
1852 is_div = true;
1853 } else {
1854 is_div = false;
1855 }
buzbee11b63d12013-08-27 07:34:17 -07001856 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1857 return;
1858 }
Dave Allison70202782013-10-22 17:52:19 -07001859
1860 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 if (cu_->instruction_set == kMips) {
1862 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001863 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001864 done = true;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001865 } else if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendell2bf31e62014-01-23 12:13:40 -08001866 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1867 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001868 } else if (cu_->instruction_set == kThumb2) {
1869 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1870 // Use ARM SDIV instruction for division. For remainder we also need to
1871 // calculate using a MUL and subtract.
1872 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001873 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001874 done = true;
1875 }
1876 }
1877
1878 if (!done) {
1879 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1881 Clobber(TargetReg(kArg0));
Ian Rogersdd7624d2014-03-14 17:43:00 -07001882 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001883 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1884 if (is_div)
1885 rl_result = GetReturn(false);
1886 else
1887 rl_result = GetReturnAlt();
1888 }
1889 StoreValue(rl_dest, rl_result);
1890 return;
1891 }
1892 default:
1893 LOG(FATAL) << "Unexpected opcode " << opcode;
1894 }
1895 rl_src = LoadValue(rl_src, kCoreReg);
1896 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001897 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001898 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001899 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001900 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001901 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 }
1903 StoreValue(rl_dest, rl_result);
1904}
1905
1906void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001907 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001908 RegLocation rl_result;
1909 OpKind first_op = kOpBkpt;
1910 OpKind second_op = kOpBkpt;
1911 bool call_out = false;
1912 bool check_zero = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001913 ThreadOffset<4> func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001914 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001915
1916 switch (opcode) {
1917 case Instruction::NOT_LONG:
1918 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1919 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1920 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001921 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1922 RegStorage t_reg = AllocTemp();
1923 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1924 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1925 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001926 FreeTemp(t_reg);
1927 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001928 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1929 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001930 }
1931 StoreValueWide(rl_dest, rl_result);
1932 return;
1933 case Instruction::ADD_LONG:
1934 case Instruction::ADD_LONG_2ADDR:
1935 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001936 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001937 return;
1938 }
1939 first_op = kOpAdd;
1940 second_op = kOpAdc;
1941 break;
1942 case Instruction::SUB_LONG:
1943 case Instruction::SUB_LONG_2ADDR:
1944 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001945 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001946 return;
1947 }
1948 first_op = kOpSub;
1949 second_op = kOpSbc;
1950 break;
1951 case Instruction::MUL_LONG:
1952 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001953 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001954 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 return;
1956 } else {
1957 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001958 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001959 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001960 }
1961 break;
1962 case Instruction::DIV_LONG:
1963 case Instruction::DIV_LONG_2ADDR:
1964 call_out = true;
1965 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001966 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogersdd7624d2014-03-14 17:43:00 -07001967 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001968 break;
1969 case Instruction::REM_LONG:
1970 case Instruction::REM_LONG_2ADDR:
1971 call_out = true;
1972 check_zero = true;
Ian Rogersdd7624d2014-03-14 17:43:00 -07001973 func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001975 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 break;
1977 case Instruction::AND_LONG_2ADDR:
1978 case Instruction::AND_LONG:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001979 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001980 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001981 }
1982 first_op = kOpAnd;
1983 second_op = kOpAnd;
1984 break;
1985 case Instruction::OR_LONG:
1986 case Instruction::OR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001987 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001988 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001989 return;
1990 }
1991 first_op = kOpOr;
1992 second_op = kOpOr;
1993 break;
1994 case Instruction::XOR_LONG:
1995 case Instruction::XOR_LONG_2ADDR:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +07001996 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001997 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001998 return;
1999 }
2000 first_op = kOpXor;
2001 second_op = kOpXor;
2002 break;
2003 case Instruction::NEG_LONG: {
2004 GenNegLong(rl_dest, rl_src2);
2005 return;
2006 }
2007 default:
2008 LOG(FATAL) << "Invalid long arith op";
2009 }
2010 if (!call_out) {
2011 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
2012 } else {
2013 FlushAllRegs(); /* Send everything to home location */
2014 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08002015 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
2016 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
2017 LoadValueDirectWideFixed(rl_src2, r_tmp2);
2018 RegStorage r_tgt = CallHelperSetup(func_offset);
Mingyao Yange643a172014-04-08 11:02:52 -07002019 GenDivZeroCheckWide(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
buzbee2700f7e2014-03-07 09:46:20 -08002020 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002021 // NOTE: callout here is not a safepoint
2022 CallHelper(r_tgt, func_offset, false /* not safepoint */);
2023 } else {
2024 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
2025 }
2026 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08002027 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07002028 rl_result = GetReturnWide(false);
2029 else
2030 rl_result = GetReturnWideAlt();
2031 StoreValueWide(rl_dest, rl_result);
2032 }
2033}
2034
Ian Rogersdd7624d2014-03-14 17:43:00 -07002035void Mir2Lir::GenConversionCall(ThreadOffset<4> func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002036 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037 /*
2038 * Don't optimize the register usage since it calls out to support
2039 * functions
2040 */
2041 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07002042 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
2043 if (rl_dest.wide) {
2044 RegLocation rl_result;
2045 rl_result = GetReturnWide(rl_dest.fp);
2046 StoreValueWide(rl_dest, rl_result);
2047 } else {
2048 RegLocation rl_result;
2049 rl_result = GetReturn(rl_dest.fp);
2050 StoreValue(rl_dest, rl_result);
2051 }
2052}
2053
2054/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002055void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08002056 if (Runtime::Current()->ExplicitSuspendChecks()) {
2057 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2058 return;
2059 }
2060 FlushAllRegs();
2061 LIR* branch = OpTestSuspend(NULL);
2062 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
2063 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
2064 current_dalvik_offset_);
2065 branch->target = target;
2066 suspend_launchpads_.Insert(target);
2067 } else {
2068 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2069 return;
2070 }
2071 FlushAllRegs(); // TODO: needed?
2072 LIR* inst = CheckSuspendUsingLoad();
2073 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002074 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002075}
2076
2077/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07002078void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08002079 if (Runtime::Current()->ExplicitSuspendChecks()) {
2080 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2081 OpUnconditionalBranch(target);
2082 return;
2083 }
2084 OpTestSuspend(target);
2085 LIR* launch_pad =
2086 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2087 current_dalvik_offset_);
2088 FlushAllRegs();
2089 OpUnconditionalBranch(launch_pad);
2090 suspend_launchpads_.Insert(launch_pad);
2091 } else {
2092 // For the implicit suspend check, just perform the trigger
2093 // load and branch to the target.
2094 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2095 OpUnconditionalBranch(target);
2096 return;
2097 }
2098 FlushAllRegs();
2099 LIR* inst = CheckSuspendUsingLoad();
2100 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002101 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002102 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002103}
2104
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002105/* Call out to helper assembly routine that will null check obj and then lock it. */
2106void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2107 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002108 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pLockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002109}
2110
2111/* Call out to helper assembly routine that will null check obj and then unlock it. */
2112void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2113 FlushAllRegs();
Ian Rogersdd7624d2014-03-14 17:43:00 -07002114 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject), rl_src, true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002115}
2116
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002117/* Generic code for generating a wide constant into a VR. */
2118void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2119 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002120 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002121 StoreValueWide(rl_dest, rl_result);
2122}
2123
Brian Carlstrom7940e442013-07-12 13:46:57 -07002124} // namespace art