blob: 2afa5ca815065611ebf9b6815251c2d1183480ea [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 */
16
17#include "dex/compiler_ir.h"
18#include "dex/compiler_internals.h"
Brian Carlstrom60d7a652014-03-13 18:10:08 -070019#include "dex/quick/arm/arm_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070021#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080023#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080025#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
29/*
30 * This source files contains "gen" codegen routines that should
31 * be applicable to most targets. Only mid-level support utilities
32 * and "op" calls may be used here.
33 */
34
35/*
buzbeeb48819d2013-09-14 16:15:25 -070036 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070037 * blocks.
38 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070039void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 LIR* barrier = NewLIR0(kPseudoBarrier);
41 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070042 DCHECK(!barrier->flags.use_def_invalid);
43 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070044}
45
buzbee0d829482013-10-11 15:24:55 -070046// TODO: need to do some work to split out targets with
Brian Carlstrom7940e442013-07-12 13:46:57 -070047// condition codes and those without
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070048LIR* Mir2Lir::GenCheck(ConditionCode c_code, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 DCHECK_NE(cu_->instruction_set, kMips);
50 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_);
51 LIR* branch = OpCondBranch(c_code, tgt);
52 // Remember branch target - will process later
53 throw_launchpads_.Insert(tgt);
54 return branch;
55}
56
buzbee2700f7e2014-03-07 09:46:20 -080057LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, RegStorage reg, int imm_val, ThrowKind kind) {
58 LIR* tgt;
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 LIR* branch;
60 if (c_code == kCondAl) {
buzbee2700f7e2014-03-07 09:46:20 -080061 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, RegStorage::kInvalidRegVal,
62 imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 branch = OpUnconditionalBranch(tgt);
64 } else {
buzbee2700f7e2014-03-07 09:46:20 -080065 tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg.GetReg(), imm_val);
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
67 }
68 // Remember branch target - will process later
69 throw_launchpads_.Insert(tgt);
70 return branch;
71}
72
Dave Allisonb373e092014-02-20 16:06:36 -080073
Brian Carlstrom7940e442013-07-12 13:46:57 -070074/* Perform null-check on a register. */
buzbee2700f7e2014-03-07 09:46:20 -080075LIR* Mir2Lir::GenNullCheck(RegStorage m_reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -080076 if (Runtime::Current()->ExplicitNullChecks()) {
77 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
78 return NULL;
79 }
80 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 }
Dave Allisonb373e092014-02-20 16:06:36 -080082 return nullptr;
83}
84
85void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
86 if (!Runtime::Current()->ExplicitNullChecks()) {
87 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
88 return;
89 }
90 MarkSafepointPC(last_lir_insn_);
91 }
92}
93
94void Mir2Lir::MarkPossibleStackOverflowException() {
95 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
96 MarkSafepointPC(last_lir_insn_);
97 }
98}
99
buzbee2700f7e2014-03-07 09:46:20 -0800100void Mir2Lir::ForceImplicitNullCheck(RegStorage reg, int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -0800101 if (!Runtime::Current()->ExplicitNullChecks()) {
102 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
103 return;
104 }
105 // Force an implicit null check by performing a memory operation (load) from the given
106 // register with offset 0. This will cause a signal if the register contains 0 (null).
buzbee2700f7e2014-03-07 09:46:20 -0800107 RegStorage tmp = AllocTemp();
108 // TODO: for Mips, would be best to use rZERO as the bogus register target.
Dave Allisonb373e092014-02-20 16:06:36 -0800109 LIR* load = LoadWordDisp(reg, 0, tmp);
110 FreeTemp(tmp);
111 MarkSafepointPC(load);
112 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113}
114
115/* Perform check on two registers */
buzbee2700f7e2014-03-07 09:46:20 -0800116LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700117 ThrowKind kind) {
buzbee2700f7e2014-03-07 09:46:20 -0800118 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1.GetReg(),
119 reg2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
121 // Remember branch target - will process later
122 throw_launchpads_.Insert(tgt);
123 return branch;
124}
125
126void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
127 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 ConditionCode cond;
130 switch (opcode) {
131 case Instruction::IF_EQ:
132 cond = kCondEq;
133 break;
134 case Instruction::IF_NE:
135 cond = kCondNe;
136 break;
137 case Instruction::IF_LT:
138 cond = kCondLt;
139 break;
140 case Instruction::IF_GE:
141 cond = kCondGe;
142 break;
143 case Instruction::IF_GT:
144 cond = kCondGt;
145 break;
146 case Instruction::IF_LE:
147 cond = kCondLe;
148 break;
149 default:
150 cond = static_cast<ConditionCode>(0);
151 LOG(FATAL) << "Unexpected opcode " << opcode;
152 }
153
154 // Normalize such that if either operand is constant, src2 will be constant
155 if (rl_src1.is_const) {
156 RegLocation rl_temp = rl_src1;
157 rl_src1 = rl_src2;
158 rl_src2 = rl_temp;
159 cond = FlipComparisonOrder(cond);
160 }
161
162 rl_src1 = LoadValue(rl_src1, kCoreReg);
163 // Is this really an immediate comparison?
164 if (rl_src2.is_const) {
165 // If it's already live in a register or not easily materialized, just keep going
166 RegLocation rl_temp = UpdateLoc(rl_src2);
167 if ((rl_temp.location == kLocDalvikFrame) &&
168 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
169 // OK - convert this to a compare immediate and branch
buzbee2700f7e2014-03-07 09:46:20 -0800170 OpCmpImmBranch(cond, rl_src1.reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 return;
172 }
173 }
174 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800175 OpCmpBranch(cond, rl_src1.reg, rl_src2.reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176}
177
178void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700179 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 ConditionCode cond;
181 rl_src = LoadValue(rl_src, kCoreReg);
182 switch (opcode) {
183 case Instruction::IF_EQZ:
184 cond = kCondEq;
185 break;
186 case Instruction::IF_NEZ:
187 cond = kCondNe;
188 break;
189 case Instruction::IF_LTZ:
190 cond = kCondLt;
191 break;
192 case Instruction::IF_GEZ:
193 cond = kCondGe;
194 break;
195 case Instruction::IF_GTZ:
196 cond = kCondGt;
197 break;
198 case Instruction::IF_LEZ:
199 cond = kCondLe;
200 break;
201 default:
202 cond = static_cast<ConditionCode>(0);
203 LOG(FATAL) << "Unexpected opcode " << opcode;
204 }
buzbee2700f7e2014-03-07 09:46:20 -0800205 OpCmpImmBranch(cond, rl_src.reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206}
207
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700208void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
210 if (rl_src.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800211 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800213 LoadValueDirect(rl_src, rl_result.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 }
buzbee2700f7e2014-03-07 09:46:20 -0800215 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_result.reg.GetLow(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 StoreValueWide(rl_dest, rl_result);
217}
218
219void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700220 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700221 rl_src = LoadValue(rl_src, kCoreReg);
222 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
223 OpKind op = kOpInvalid;
224 switch (opcode) {
225 case Instruction::INT_TO_BYTE:
226 op = kOp2Byte;
227 break;
228 case Instruction::INT_TO_SHORT:
229 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700231 case Instruction::INT_TO_CHAR:
232 op = kOp2Char;
233 break;
234 default:
235 LOG(ERROR) << "Bad int conversion type";
236 }
buzbee2700f7e2014-03-07 09:46:20 -0800237 OpRegReg(op, rl_result.reg, rl_src.reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700238 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239}
240
241/*
242 * Let helper function take care of everything. Will call
243 * Array::AllocFromCode(type_idx, method, count);
244 * Note: AllocFromCode will handle checks for errNegativeArraySize.
245 */
246void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700247 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700249 ThreadOffset func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800250 const DexFile* dex_file = cu_->dex_file;
251 CompilerDriver* driver = cu_->compiler_driver;
252 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800254 bool is_type_initialized; // Ignored as an array does not have an initializer.
255 bool use_direct_type_ptr;
256 uintptr_t direct_type_ptr;
257 if (kEmbedClassInCode &&
258 driver->CanEmbedTypeInCode(*dex_file, type_idx,
259 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
260 // The fast path.
261 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800262 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800263 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
264 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
265 } else {
266 // Use the direct pointer.
267 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
268 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
269 }
270 } else {
271 // The slow path.
272 DCHECK_EQ(func_offset.Int32Value(), -1);
273 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
274 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
275 }
276 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700278 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800279 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 RegLocation rl_result = GetReturn(false);
282 StoreValue(rl_dest, rl_result);
283}
284
285/*
286 * Similar to GenNewArray, but with post-allocation initialization.
287 * Verifier guarantees we're dealing with an array class. Current
288 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
289 * Current code also throws internal unimp if not 'L', '[' or 'I'.
290 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700291void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 int elems = info->num_arg_words;
293 int type_idx = info->index;
294 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700295 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
297 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700298 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700300 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 }
302 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
303 FreeTemp(TargetReg(kArg2));
304 FreeTemp(TargetReg(kArg1));
305 /*
306 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
307 * return region. Because AllocFromCode placed the new array
308 * in kRet0, we'll just lock it into place. When debugger support is
309 * added, it may be necessary to additionally copy all return
310 * values to a home location in thread-local storage
311 */
312 LockTemp(TargetReg(kRet0));
313
314 // TODO: use the correct component size, currently all supported types
315 // share array alignment with ints (see comment at head of function)
316 size_t component_size = sizeof(int32_t);
317
318 // Having a range of 0 is legal
319 if (info->is_range && (elems > 0)) {
320 /*
321 * Bit of ugliness here. We're going generate a mem copy loop
322 * on the register range, but it is possible that some regs
323 * in the range have been promoted. This is unlikely, but
324 * before generating the copy, we'll just force a flush
325 * of any regs in the source range that have been promoted to
326 * home location.
327 */
328 for (int i = 0; i < elems; i++) {
329 RegLocation loc = UpdateLoc(info->args[i]);
330 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800331 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low), loc.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 }
333 }
334 /*
335 * TUNING note: generated code here could be much improved, but
336 * this is an uncommon operation and isn't especially performance
337 * critical.
338 */
buzbee2700f7e2014-03-07 09:46:20 -0800339 RegStorage r_src = AllocTemp();
340 RegStorage r_dst = AllocTemp();
341 RegStorage r_idx = AllocTemp();
342 RegStorage r_val;
Brian Carlstromdf629502013-07-17 22:39:56 -0700343 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 case kThumb2:
345 r_val = TargetReg(kLr);
346 break;
347 case kX86:
348 FreeTemp(TargetReg(kRet0));
349 r_val = AllocTemp();
350 break;
351 case kMips:
352 r_val = AllocTemp();
353 break;
354 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
355 }
356 // Set up source pointer
357 RegLocation rl_first = info->args[0];
358 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
359 // Set up the target pointer
360 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
361 mirror::Array::DataOffset(component_size).Int32Value());
362 // Set up the loop counter (known to be > 0)
363 LoadConstant(r_idx, elems - 1);
364 // Generate the copy loop. Going backwards for convenience
365 LIR* target = NewLIR0(kPseudoTargetLabel);
366 // Copy next element
367 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
368 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
369 FreeTemp(r_val);
370 OpDecAndBranch(kCondGe, r_idx, target);
371 if (cu_->instruction_set == kX86) {
372 // Restore the target pointer
373 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
374 -mirror::Array::DataOffset(component_size).Int32Value());
375 }
376 } else if (!info->is_range) {
377 // TUNING: interleave
378 for (int i = 0; i < elems; i++) {
379 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
380 StoreBaseDisp(TargetReg(kRet0),
buzbee2700f7e2014-03-07 09:46:20 -0800381 mirror::Array::DataOffset(component_size).Int32Value() + i * 4,
382 rl_arg.reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 // If the LoadValue caused a temp to be allocated, free it
buzbee2700f7e2014-03-07 09:46:20 -0800384 if (IsTemp(rl_arg.reg)) {
385 FreeTemp(rl_arg.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
387 }
388 }
389 if (info->result.location != kLocInvalid) {
390 StoreValue(info->result, GetReturn(false /* not fp */));
391 }
392}
393
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800394//
395// Slow path to ensure a class is initialized for sget/sput.
396//
397class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
398 public:
buzbee2700f7e2014-03-07 09:46:20 -0800399 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont, int storage_index,
400 RegStorage r_base) :
401 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit),
402 storage_index_(storage_index), r_base_(r_base) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800403 }
404
405 void Compile() {
406 LIR* unresolved_target = GenerateTargetLabel();
407 uninit_->target = unresolved_target;
408 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
buzbee2700f7e2014-03-07 09:46:20 -0800409 storage_index_, true);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800410 // Copy helper's result into r_base, a no-op on all but MIPS.
411 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
412
413 m2l_->OpUnconditionalBranch(cont_);
414 }
415
416 private:
417 LIR* const uninit_;
418 const int storage_index_;
buzbee2700f7e2014-03-07 09:46:20 -0800419 const RegStorage r_base_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800420};
421
Vladimir Markobe0e5462014-02-26 11:24:15 +0000422void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700423 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000424 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
425 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
426 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
427 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800428 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000429 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 // Fast path, static storage base is this method's class
431 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800432 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800433 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
434 if (IsTemp(rl_method.reg)) {
435 FreeTemp(rl_method.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 }
437 } else {
438 // Medium path, static storage base in a different class which requires checks that the other
439 // class is initialized.
440 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000441 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 // May do runtime call so everything to home locations.
443 FlushAllRegs();
444 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800445 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 LockTemp(r_method);
447 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800448 r_base = TargetReg(kArg0);
449 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800450 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800451 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000452 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800453 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000454 if (!field_info.IsInitialized() &&
455 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800456 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800457
458 // The slow path is invoked if the r_base is NULL or the class pointed
459 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800460 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800461 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800462 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800463 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800464 mirror::Class::StatusOffset().Int32Value(),
465 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800466 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800467
buzbee2700f7e2014-03-07 09:46:20 -0800468 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000469 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800470
471 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 FreeTemp(r_method);
474 }
475 // rBase now holds static storage base
476 if (is_long_or_double) {
477 rl_src = LoadValueWide(rl_src, kAnyReg);
478 } else {
479 rl_src = LoadValue(rl_src, kAnyReg);
480 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000481 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800482 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 GenMemBarrier(kStoreStore);
484 }
485 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800486 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800488 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000490 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800491 // A load might follow the volatile store so insert a StoreLoad barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 GenMemBarrier(kStoreLoad);
493 }
494 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800495 MarkGCCard(rl_src.reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800497 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 } else {
499 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700500 ThreadOffset setter_offset =
501 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
502 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
503 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000504 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 }
506}
507
Vladimir Markobe0e5462014-02-26 11:24:15 +0000508void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700509 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000510 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
511 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
512 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
513 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -0800514 RegStorage r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000515 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 // Fast path, static storage base is this method's class
517 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800518 r_base = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800519 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 } else {
521 // Medium path, static storage base in a different class which requires checks that the other
522 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000523 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 // May do runtime call so everything to home locations.
525 FlushAllRegs();
526 // Using fixed register to sync with possible call to runtime support.
buzbee2700f7e2014-03-07 09:46:20 -0800527 RegStorage r_method = TargetReg(kArg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 LockTemp(r_method);
529 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800530 r_base = TargetReg(kArg0);
531 LockTemp(r_base);
buzbee2700f7e2014-03-07 09:46:20 -0800532 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800533 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000534 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800535 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000536 if (!field_info.IsInitialized() &&
537 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800538 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800539
540 // The slow path is invoked if the r_base is NULL or the class pointed
541 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800542 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800543 RegStorage r_tmp = TargetReg(kArg2);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800544 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800545 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800546 mirror::Class::StatusOffset().Int32Value(),
547 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800548 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800549
buzbee2700f7e2014-03-07 09:46:20 -0800550 AddSlowPath(new (arena_) StaticFieldSlowPath(this, unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000551 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800552
553 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 FreeTemp(r_method);
556 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800557 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800559
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 if (is_long_or_double) {
buzbee2700f7e2014-03-07 09:46:20 -0800561 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800563 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800565 FreeTemp(r_base);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800566
567 if (field_info.IsVolatile()) {
568 // Without context sensitive analysis, we must issue the most conservative barriers.
569 // In this case, either a load or store may follow so we issue both barriers.
570 GenMemBarrier(kLoadLoad);
571 GenMemBarrier(kLoadStore);
572 }
573
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 if (is_long_or_double) {
575 StoreValueWide(rl_dest, rl_result);
576 } else {
577 StoreValue(rl_dest, rl_result);
578 }
579 } else {
580 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700581 ThreadOffset getterOffset =
582 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
583 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
584 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000585 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 if (is_long_or_double) {
587 RegLocation rl_result = GetReturnWide(rl_dest.fp);
588 StoreValueWide(rl_dest, rl_result);
589 } else {
590 RegLocation rl_result = GetReturn(rl_dest.fp);
591 StoreValue(rl_dest, rl_result);
592 }
593 }
594}
595
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800596// Generate code for all slow paths.
597void Mir2Lir::HandleSlowPaths() {
598 int n = slow_paths_.Size();
599 for (int i = 0; i < n; ++i) {
600 LIRSlowPath* slowpath = slow_paths_.Get(i);
601 slowpath->Compile();
602 }
603 slow_paths_.Reset();
604}
605
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700606void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700608 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 for (int i = 0; i < num_elems; i++) {
610 ResetRegPool();
611 ResetDefTracking();
612 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700613 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 current_dalvik_offset_ = lab->operands[1];
615 AppendLIR(lab);
buzbee2700f7e2014-03-07 09:46:20 -0800616 RegStorage r_tgt = CallHelperSetup(helper_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
618 OpUnconditionalBranch(resume_lab);
619 }
620}
621
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700622void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 int num_elems = throw_launchpads_.Size();
624 for (int i = 0; i < num_elems; i++) {
625 ResetRegPool();
626 ResetDefTracking();
627 LIR* lab = throw_launchpads_.Get(i);
628 current_dalvik_offset_ = lab->operands[1];
629 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700630 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 int v1 = lab->operands[2];
632 int v2 = lab->operands[3];
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700633 const bool target_x86 = cu_->instruction_set == kX86;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 switch (lab->operands[0]) {
635 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700636 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700638 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
640 if (target_x86) {
buzbee2700f7e2014-03-07 09:46:20 -0800641 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v1),
642 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800644 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646 // Make sure the following LoadConstant doesn't mess with kArg1.
647 LockTemp(TargetReg(kArg1));
648 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700649 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 break;
651 case kThrowArrayBounds:
652 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee2700f7e2014-03-07 09:46:20 -0800653 if (v2 != TargetReg(kArg0).GetReg()) {
654 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 if (target_x86) {
656 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800657 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
658 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800660 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 }
662 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800663 if (v1 == TargetReg(kArg1).GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 // Swap v1 and v2, using kArg2 as a temp
buzbee2700f7e2014-03-07 09:46:20 -0800665 OpRegCopy(TargetReg(kArg2), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 if (target_x86) {
667 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800668 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
669 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800671 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 }
673 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
674 } else {
675 if (target_x86) {
676 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbee2700f7e2014-03-07 09:46:20 -0800677 OpRegMem(kOpMov, TargetReg(kArg1), RegStorage::Solo32(v2),
678 mirror::Array::LengthOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800680 OpRegCopy(TargetReg(kArg1), RegStorage::Solo32(v2));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 }
buzbee2700f7e2014-03-07 09:46:20 -0800682 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 }
684 }
Ian Rogers848871b2013-08-05 10:56:33 -0700685 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 break;
687 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700688 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 break;
690 case kThrowNoSuchMethod:
buzbee2700f7e2014-03-07 09:46:20 -0800691 OpRegCopy(TargetReg(kArg0), RegStorage::Solo32(v1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700693 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 default:
696 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
697 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000698 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800699 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700700 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 }
702}
703
Vladimir Markobe0e5462014-02-26 11:24:15 +0000704void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000707 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
708 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
709 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 RegLocation rl_result;
711 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000712 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 rl_obj = LoadValue(rl_obj, kCoreReg);
714 if (is_long_or_double) {
715 DCHECK(rl_dest.wide);
buzbee2700f7e2014-03-07 09:46:20 -0800716 GenNullCheck(rl_obj.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 if (cu_->instruction_set == kX86) {
718 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800719 // FIXME? duplicate null check?
720 GenNullCheck(rl_obj.reg, opt_flags);
721 LoadBaseDispWide(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg,
722 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800723 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000724 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800725 // Without context sensitive analysis, we must issue the most conservative barriers.
726 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800728 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 }
730 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800731 RegStorage reg_ptr = AllocTemp();
732 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800734 LoadBaseDispWide(reg_ptr, 0, rl_result.reg, INVALID_SREG);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000735 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800736 // Without context sensitive analysis, we must issue the most conservative barriers.
737 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800739 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 }
741 FreeTemp(reg_ptr);
742 }
743 StoreValueWide(rl_dest, rl_result);
744 } else {
745 rl_result = EvalLoc(rl_dest, reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800746 GenNullCheck(rl_obj.reg, opt_flags);
747 LoadBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_result.reg, kWord,
748 rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800749 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000750 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800751 // Without context sensitive analysis, we must issue the most conservative barriers.
752 // In this case, either a load or store may follow so we issue both barriers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800754 GenMemBarrier(kLoadStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700755 }
756 StoreValue(rl_dest, rl_result);
757 }
758 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700759 ThreadOffset getterOffset =
760 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
761 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
762 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000763 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 if (is_long_or_double) {
765 RegLocation rl_result = GetReturnWide(rl_dest.fp);
766 StoreValueWide(rl_dest, rl_result);
767 } else {
768 RegLocation rl_result = GetReturn(rl_dest.fp);
769 StoreValue(rl_dest, rl_result);
770 }
771 }
772}
773
Vladimir Markobe0e5462014-02-26 11:24:15 +0000774void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700776 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000777 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
778 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
779 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000781 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 rl_obj = LoadValue(rl_obj, kCoreReg);
783 if (is_long_or_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 rl_src = LoadValueWide(rl_src, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -0800785 GenNullCheck(rl_obj.reg, opt_flags);
786 RegStorage reg_ptr = AllocTemp();
787 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg, field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800789 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 GenMemBarrier(kStoreStore);
791 }
buzbee2700f7e2014-03-07 09:46:20 -0800792 StoreBaseDispWide(reg_ptr, 0, rl_src.reg);
Dave Allisonb373e092014-02-20 16:06:36 -0800793 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000794 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800795 // A load might follow the volatile store so insert a StoreLoad barrier.
796 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 }
798 FreeTemp(reg_ptr);
799 } else {
800 rl_src = LoadValue(rl_src, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800801 GenNullCheck(rl_obj.reg, opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000802 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800803 // There might have been a store before this volatile one so insert StoreStore barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 GenMemBarrier(kStoreStore);
805 }
buzbee2700f7e2014-03-07 09:46:20 -0800806 StoreBaseDisp(rl_obj.reg, field_info.FieldOffset().Int32Value(), rl_src.reg, kWord);
Dave Allisonb373e092014-02-20 16:06:36 -0800807 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000808 if (field_info.IsVolatile()) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800809 // A load might follow the volatile store so insert a StoreLoad barrier.
810 GenMemBarrier(kStoreLoad);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 }
812 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
buzbee2700f7e2014-03-07 09:46:20 -0800813 MarkGCCard(rl_src.reg, rl_obj.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814 }
815 }
816 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700817 ThreadOffset setter_offset =
818 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
819 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
820 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000821 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
822 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 }
824}
825
Ian Rogersa9a82542013-10-04 11:17:26 -0700826void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
827 RegLocation rl_src) {
828 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
829 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
830 (opt_flags & MIR_IGNORE_NULL_CHECK));
831 ThreadOffset helper = needs_range_check
832 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
833 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
834 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
835 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
836}
837
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700838void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800840 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
842 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
843 *cu_->dex_file,
844 type_idx)) {
845 // Call out to helper which resolves type and verifies access.
846 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700847 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
buzbee2700f7e2014-03-07 09:46:20 -0800848 type_idx, rl_method.reg, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 RegLocation rl_result = GetReturn(false);
850 StoreValue(rl_dest, rl_result);
851 } else {
852 // We're don't need access checks, load type from dex cache
853 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700854 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbee2700f7e2014-03-07 09:46:20 -0800855 LoadWordDisp(rl_method.reg, dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 int32_t offset_of_type =
857 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
858 * type_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800859 LoadWordDisp(res_reg, offset_of_type, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
861 type_idx) || SLOW_TYPE_PATH) {
862 // Slow path, at runtime test if type is null and if so initialize
863 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800864 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg, 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800865 LIR* cont = NewLIR0(kPseudoTargetLabel);
866
867 // Object to generate the slow path for class resolution.
868 class SlowPath : public LIRSlowPath {
869 public:
870 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
871 const RegLocation& rl_method, const RegLocation& rl_result) :
872 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
873 rl_method_(rl_method), rl_result_(rl_result) {
874 }
875
876 void Compile() {
877 GenerateTargetLabel();
878
879 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
buzbee2700f7e2014-03-07 09:46:20 -0800880 rl_method_.reg, true);
881 m2l_->OpRegCopy(rl_result_.reg, m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800882
883 m2l_->OpUnconditionalBranch(cont_);
884 }
885
886 private:
887 const int type_idx_;
888 const RegLocation rl_method_;
889 const RegLocation rl_result_;
890 };
891
892 // Add to list for future.
buzbee2700f7e2014-03-07 09:46:20 -0800893 AddSlowPath(new (arena_) SlowPath(this, branch, cont, type_idx, rl_method, rl_result));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800894
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800896 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 // Fast path, we're done - just store result
898 StoreValue(rl_dest, rl_result);
899 }
900 }
901}
902
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700903void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 /* NOTE: Most strings should be available at compile time */
905 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
906 (sizeof(mirror::String*) * string_idx);
907 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
908 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
909 // slow path, resolve string if not in dex cache
910 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700911 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800912
913 // If the Method* is already in a register, we can save a copy.
914 RegLocation rl_method = mir_graph_->GetMethodLoc();
buzbee2700f7e2014-03-07 09:46:20 -0800915 RegStorage r_method;
Mark Mendell766e9292014-01-27 07:55:47 -0800916 if (rl_method.location == kLocPhysReg) {
917 // A temp would conflict with register use below.
buzbee2700f7e2014-03-07 09:46:20 -0800918 DCHECK(!IsTemp(rl_method.reg));
919 r_method = rl_method.reg;
Mark Mendell766e9292014-01-27 07:55:47 -0800920 } else {
921 r_method = TargetReg(kArg2);
922 LoadCurrMethodDirect(r_method);
923 }
924 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
925 TargetReg(kArg0));
926
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800929 if (cu_->instruction_set == kThumb2 ||
930 cu_->instruction_set == kMips) {
931 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800932 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800933 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
934 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800936
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800937 // Object to generate the slow path for string resolution.
938 class SlowPath : public LIRSlowPath {
939 public:
buzbee2700f7e2014-03-07 09:46:20 -0800940 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, RegStorage r_method) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800941 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
942 }
943
944 void Compile() {
945 GenerateTargetLabel();
946
buzbee2700f7e2014-03-07 09:46:20 -0800947 RegStorage r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800948
949 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
950 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
951 m2l_->MarkSafepointPC(call_inst);
952 m2l_->FreeTemp(r_tgt);
953
954 m2l_->OpUnconditionalBranch(cont_);
955 }
956
957 private:
buzbee2700f7e2014-03-07 09:46:20 -0800958 RegStorage r_method_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800959 };
960
961 // Add to list for future.
962 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 } else {
964 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800965 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
966 LoadConstant(TargetReg(kArg1), string_idx);
buzbee2700f7e2014-03-07 09:46:20 -0800967 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method, TargetReg(kArg1),
968 true);
Mark Mendell766e9292014-01-27 07:55:47 -0800969 LIR* target = NewLIR0(kPseudoTargetLabel);
970 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 }
972 GenBarrier();
973 StoreValue(rl_dest, GetReturn(false));
974 } else {
975 RegLocation rl_method = LoadCurrMethod();
buzbee2700f7e2014-03-07 09:46:20 -0800976 RegStorage res_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800978 LoadWordDisp(rl_method.reg, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
979 LoadWordDisp(res_reg, offset_of_string, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 StoreValue(rl_dest, rl_result);
981 }
982}
983
984/*
985 * Let helper function take care of everything. Will
986 * call Class::NewInstanceFromCode(type_idx, method);
987 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700988void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 FlushAllRegs(); /* Everything to home location */
990 // alloc will always check for resolution, do we also need to verify
991 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700992 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800993 const DexFile* dex_file = cu_->dex_file;
994 CompilerDriver* driver = cu_->compiler_driver;
995 if (driver->CanAccessInstantiableTypeWithoutChecks(
996 cu_->method_idx, *dex_file, type_idx)) {
997 bool is_type_initialized;
998 bool use_direct_type_ptr;
999 uintptr_t direct_type_ptr;
1000 if (kEmbedClassInCode &&
1001 driver->CanEmbedTypeInCode(*dex_file, type_idx,
1002 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
1003 // The fast path.
1004 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -08001005 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001006 if (!is_type_initialized) {
1007 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1008 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1009 } else {
1010 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1011 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1012 }
1013 } else {
1014 // Use the direct pointer.
1015 if (!is_type_initialized) {
1016 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1017 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1018 } else {
1019 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1020 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1021 }
1022 }
1023 } else {
1024 // The slow path.
1025 DCHECK_EQ(func_offset.Int32Value(), -1);
1026 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1027 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1028 }
1029 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001031 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001032 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 RegLocation rl_result = GetReturn(false);
1035 StoreValue(rl_dest, rl_result);
1036}
1037
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001038void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -07001040 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041}
1042
1043// For final classes there are no sub-classes to check and so we can answer the instance-of
1044// question with simple comparisons.
1045void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1046 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001047 // X86 has its own implementation.
1048 DCHECK_NE(cu_->instruction_set, kX86);
1049
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 RegLocation object = LoadValue(rl_src, kCoreReg);
1051 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001052 RegStorage result_reg = rl_result.reg;
1053 if (result_reg == object.reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 result_reg = AllocTypedTemp(false, kCoreReg);
1055 }
1056 LoadConstant(result_reg, 0); // assume false
buzbee2700f7e2014-03-07 09:46:20 -08001057 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg, 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058
buzbee2700f7e2014-03-07 09:46:20 -08001059 RegStorage check_class = AllocTypedTemp(false, kCoreReg);
1060 RegStorage object_class = AllocTypedTemp(false, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061
1062 LoadCurrMethodDirect(check_class);
1063 if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001064 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(), check_class);
1065 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001067 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 check_class);
buzbee2700f7e2014-03-07 09:46:20 -08001069 LoadWordDisp(object.reg, mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 int32_t offset_of_type =
1071 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1072 (sizeof(mirror::Class*) * type_idx);
1073 LoadWordDisp(check_class, offset_of_type, check_class);
1074 }
1075
1076 LIR* ne_branchover = NULL;
1077 if (cu_->instruction_set == kThumb2) {
1078 OpRegReg(kOpCmp, check_class, object_class); // Same?
1079 OpIT(kCondEq, ""); // if-convert the test
1080 LoadConstant(result_reg, 1); // .eq case - load true
1081 } else {
1082 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1083 LoadConstant(result_reg, 1); // eq case - load true
1084 }
1085 LIR* target = NewLIR0(kPseudoTargetLabel);
1086 null_branchover->target = target;
1087 if (ne_branchover != NULL) {
1088 ne_branchover->target = target;
1089 }
1090 FreeTemp(object_class);
1091 FreeTemp(check_class);
1092 if (IsTemp(result_reg)) {
buzbee2700f7e2014-03-07 09:46:20 -08001093 OpRegCopy(rl_result.reg, result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 FreeTemp(result_reg);
1095 }
1096 StoreValue(rl_dest, rl_result);
1097}
1098
1099void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1100 bool type_known_abstract, bool use_declaring_class,
1101 bool can_assume_type_is_in_dex_cache,
1102 uint32_t type_idx, RegLocation rl_dest,
1103 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001104 // X86 has its own implementation.
1105 DCHECK_NE(cu_->instruction_set, kX86);
1106
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 FlushAllRegs();
1108 // May generate a call - use explicit registers
1109 LockCallTemps();
1110 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001111 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 if (needs_access_check) {
1113 // Check we have access to type_idx and if not throw IllegalAccessError,
1114 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001115 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 type_idx, true);
1117 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1118 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1119 } else if (use_declaring_class) {
1120 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001121 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1122 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 } else {
1124 // Load dex cache entry into class_reg (kArg2)
1125 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
buzbee2700f7e2014-03-07 09:46:20 -08001126 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1127 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 int32_t offset_of_type =
1129 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1130 * type_idx);
1131 LoadWordDisp(class_reg, offset_of_type, class_reg);
1132 if (!can_assume_type_is_in_dex_cache) {
1133 // Need to test presence of type in dex cache at runtime
1134 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1135 // Not resolved
1136 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001137 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001138 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1140 // Rejoin code paths
1141 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1142 hop_branch->target = hop_target;
1143 }
1144 }
1145 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1146 RegLocation rl_result = GetReturn(false);
1147 if (cu_->instruction_set == kMips) {
1148 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
buzbee2700f7e2014-03-07 09:46:20 -08001149 LoadConstant(rl_result.reg, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 }
1151 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1152
1153 /* load object->klass_ */
1154 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1155 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1156 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1157 LIR* branchover = NULL;
1158 if (type_known_final) {
1159 // rl_result == ref == null == 0.
1160 if (cu_->instruction_set == kThumb2) {
1161 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1162 OpIT(kCondEq, "E"); // if-convert the test
buzbee2700f7e2014-03-07 09:46:20 -08001163 LoadConstant(rl_result.reg, 1); // .eq case - load true
1164 LoadConstant(rl_result.reg, 0); // .ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001166 LoadConstant(rl_result.reg, 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
buzbee2700f7e2014-03-07 09:46:20 -08001168 LoadConstant(rl_result.reg, 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
1170 } else {
1171 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001172 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 if (!type_known_abstract) {
1174 /* Uses conditional nullification */
1175 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1176 OpIT(kCondEq, "EE"); // if-convert the test
1177 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1178 }
1179 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1180 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1181 FreeTemp(r_tgt);
1182 } else {
1183 if (!type_known_abstract) {
1184 /* Uses branchovers */
buzbee2700f7e2014-03-07 09:46:20 -08001185 LoadConstant(rl_result.reg, 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1187 }
buzbee2700f7e2014-03-07 09:46:20 -08001188 RegStorage r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Mark Mendell6607d972014-02-10 06:54:18 -08001189 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1190 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1191 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 }
1193 }
1194 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001195 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 /* branch targets here */
1197 LIR* target = NewLIR0(kPseudoTargetLabel);
1198 StoreValue(rl_dest, rl_result);
1199 branch1->target = target;
1200 if (branchover != NULL) {
1201 branchover->target = target;
1202 }
1203}
1204
1205void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1206 bool type_known_final, type_known_abstract, use_declaring_class;
1207 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1208 *cu_->dex_file,
1209 type_idx,
1210 &type_known_final,
1211 &type_known_abstract,
1212 &use_declaring_class);
1213 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1214 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1215
1216 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1217 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1218 } else {
1219 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1220 use_declaring_class, can_assume_type_is_in_dex_cache,
1221 type_idx, rl_dest, rl_src);
1222 }
1223}
1224
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001225void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 bool type_known_final, type_known_abstract, use_declaring_class;
1227 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1228 *cu_->dex_file,
1229 type_idx,
1230 &type_known_final,
1231 &type_known_abstract,
1232 &use_declaring_class);
1233 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1234 // of the exception throw path.
1235 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001236 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 // Verifier type analysis proved this check cast would never cause an exception.
1238 return;
1239 }
1240 FlushAllRegs();
1241 // May generate a call - use explicit registers
1242 LockCallTemps();
1243 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
buzbee2700f7e2014-03-07 09:46:20 -08001244 RegStorage class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 if (needs_access_check) {
1246 // Check we have access to type_idx and if not throw IllegalAccessError,
1247 // returns Class* in kRet0
1248 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001249 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 type_idx, TargetReg(kArg1), true);
1251 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1252 } else if (use_declaring_class) {
buzbee2700f7e2014-03-07 09:46:20 -08001253 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1254 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 } else {
1256 // Load dex cache entry into class_reg (kArg2)
buzbee2700f7e2014-03-07 09:46:20 -08001257 LoadWordDisp(TargetReg(kArg1), mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1258 class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 int32_t offset_of_type =
1260 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1261 (sizeof(mirror::Class*) * type_idx);
1262 LoadWordDisp(class_reg, offset_of_type, class_reg);
1263 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1264 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001265 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1266 LIR* cont = NewLIR0(kPseudoTargetLabel);
1267
1268 // Slow path to initialize the type. Executed if the type is NULL.
1269 class SlowPath : public LIRSlowPath {
1270 public:
1271 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
buzbee2700f7e2014-03-07 09:46:20 -08001272 const RegStorage class_reg) :
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001273 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1274 class_reg_(class_reg) {
1275 }
1276
1277 void Compile() {
1278 GenerateTargetLabel();
1279
1280 // Call out to helper, which will return resolved type in kArg0
1281 // InitializeTypeFromCode(idx, method)
1282 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1283 m2l_->TargetReg(kArg1), true);
1284 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1285 m2l_->OpUnconditionalBranch(cont_);
1286 }
1287 public:
1288 const int type_idx_;
buzbee2700f7e2014-03-07 09:46:20 -08001289 const RegStorage class_reg_;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001290 };
1291
buzbee2700f7e2014-03-07 09:46:20 -08001292 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont, type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
1294 }
1295 // At this point, class_reg (kArg2) has class
1296 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001297
1298 // Slow path for the case where the classes are not equal. In this case we need
1299 // to call a helper function to do the check.
1300 class SlowPath : public LIRSlowPath {
1301 public:
1302 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1303 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1304 }
1305
1306 void Compile() {
1307 GenerateTargetLabel();
1308
1309 if (load_) {
1310 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1311 m2l_->TargetReg(kArg1));
1312 }
1313 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1314 m2l_->TargetReg(kArg1), true);
1315
1316 m2l_->OpUnconditionalBranch(cont_);
1317 }
1318
1319 private:
1320 bool load_;
1321 };
1322
1323 if (type_known_abstract) {
1324 // Easier case, run slow path if target is non-null (slow path will load from target)
1325 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1326 LIR* cont = NewLIR0(kPseudoTargetLabel);
1327 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1328 } else {
1329 // Harder, more common case. We need to generate a forward branch over the load
1330 // if the target is null. If it's non-null we perform the load and branch to the
1331 // slow path if the classes are not equal.
1332
1333 /* Null is OK - continue */
1334 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1335 /* load object->klass_ */
1336 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
buzbee2700f7e2014-03-07 09:46:20 -08001337 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001338
1339 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1340 LIR* cont = NewLIR0(kPseudoTargetLabel);
1341
1342 // Add the slow path that will not perform load since this is already done.
1343 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1344
1345 // Set the null check to branch to the continuation.
1346 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 }
1348}
1349
1350void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001351 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 RegLocation rl_result;
1353 if (cu_->instruction_set == kThumb2) {
1354 /*
1355 * NOTE: This is the one place in the code in which we might have
1356 * as many as six live temporary registers. There are 5 in the normal
1357 * set for Arm. Until we have spill capabilities, temporarily add
1358 * lr to the temp set. It is safe to do this locally, but note that
1359 * lr is used explicitly elsewhere in the code generator and cannot
1360 * normally be used as a general temp register.
1361 */
1362 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1363 FreeTemp(TargetReg(kLr)); // and make it available
1364 }
1365 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1366 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1367 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1368 // The longs may overlap - use intermediate temp if so
buzbee2700f7e2014-03-07 09:46:20 -08001369 if ((rl_result.reg.GetLowReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg())) {
1370 RegStorage t_reg = AllocTemp();
1371 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1372 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
1373 OpRegCopy(rl_result.reg.GetLow(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 FreeTemp(t_reg);
1375 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001376 OpRegRegReg(first_op, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
1377 OpRegRegReg(second_op, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 }
1379 /*
1380 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1381 * following StoreValueWide might need to allocate a temp register.
1382 * To further work around the lack of a spill capability, explicitly
1383 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1384 * Remove when spill is functional.
1385 */
1386 FreeRegLocTemps(rl_result, rl_src1);
1387 FreeRegLocTemps(rl_result, rl_src2);
1388 StoreValueWide(rl_dest, rl_result);
1389 if (cu_->instruction_set == kThumb2) {
1390 Clobber(TargetReg(kLr));
1391 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1392 }
1393}
1394
1395
1396void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001397 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001398 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399
1400 switch (opcode) {
1401 case Instruction::SHL_LONG:
1402 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001403 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 break;
1405 case Instruction::SHR_LONG:
1406 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001407 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 break;
1409 case Instruction::USHR_LONG:
1410 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001411 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 break;
1413 default:
1414 LOG(FATAL) << "Unexpected case";
1415 }
1416 FlushAllRegs(); /* Send everything to home location */
1417 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1418 RegLocation rl_result = GetReturnWide(false);
1419 StoreValueWide(rl_dest, rl_result);
1420}
1421
1422
1423void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001424 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001425 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 OpKind op = kOpBkpt;
1427 bool is_div_rem = false;
1428 bool check_zero = false;
1429 bool unary = false;
1430 RegLocation rl_result;
1431 bool shift_op = false;
1432 switch (opcode) {
1433 case Instruction::NEG_INT:
1434 op = kOpNeg;
1435 unary = true;
1436 break;
1437 case Instruction::NOT_INT:
1438 op = kOpMvn;
1439 unary = true;
1440 break;
1441 case Instruction::ADD_INT:
1442 case Instruction::ADD_INT_2ADDR:
1443 op = kOpAdd;
1444 break;
1445 case Instruction::SUB_INT:
1446 case Instruction::SUB_INT_2ADDR:
1447 op = kOpSub;
1448 break;
1449 case Instruction::MUL_INT:
1450 case Instruction::MUL_INT_2ADDR:
1451 op = kOpMul;
1452 break;
1453 case Instruction::DIV_INT:
1454 case Instruction::DIV_INT_2ADDR:
1455 check_zero = true;
1456 op = kOpDiv;
1457 is_div_rem = true;
1458 break;
1459 /* NOTE: returns in kArg1 */
1460 case Instruction::REM_INT:
1461 case Instruction::REM_INT_2ADDR:
1462 check_zero = true;
1463 op = kOpRem;
1464 is_div_rem = true;
1465 break;
1466 case Instruction::AND_INT:
1467 case Instruction::AND_INT_2ADDR:
1468 op = kOpAnd;
1469 break;
1470 case Instruction::OR_INT:
1471 case Instruction::OR_INT_2ADDR:
1472 op = kOpOr;
1473 break;
1474 case Instruction::XOR_INT:
1475 case Instruction::XOR_INT_2ADDR:
1476 op = kOpXor;
1477 break;
1478 case Instruction::SHL_INT:
1479 case Instruction::SHL_INT_2ADDR:
1480 shift_op = true;
1481 op = kOpLsl;
1482 break;
1483 case Instruction::SHR_INT:
1484 case Instruction::SHR_INT_2ADDR:
1485 shift_op = true;
1486 op = kOpAsr;
1487 break;
1488 case Instruction::USHR_INT:
1489 case Instruction::USHR_INT_2ADDR:
1490 shift_op = true;
1491 op = kOpLsr;
1492 break;
1493 default:
1494 LOG(FATAL) << "Invalid word arith op: " << opcode;
1495 }
1496 if (!is_div_rem) {
1497 if (unary) {
1498 rl_src1 = LoadValue(rl_src1, kCoreReg);
1499 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001500 OpRegReg(op, rl_result.reg, rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 } else {
1502 if (shift_op) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001503 rl_src2 = LoadValue(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001504 RegStorage t_reg = AllocTemp();
1505 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 rl_src1 = LoadValue(rl_src1, kCoreReg);
1507 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegRegReg(op, rl_result.reg, rl_src1.reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 FreeTemp(t_reg);
1510 } else {
1511 rl_src1 = LoadValue(rl_src1, kCoreReg);
1512 rl_src2 = LoadValue(rl_src2, kCoreReg);
1513 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001514 OpRegRegReg(op, rl_result.reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
1516 }
1517 StoreValue(rl_dest, rl_result);
1518 } else {
Dave Allison70202782013-10-22 17:52:19 -07001519 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 -07001520 if (cu_->instruction_set == kMips) {
1521 rl_src1 = LoadValue(rl_src1, kCoreReg);
1522 rl_src2 = LoadValue(rl_src2, kCoreReg);
1523 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001524 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 }
buzbee2700f7e2014-03-07 09:46:20 -08001526 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001527 done = true;
1528 } else if (cu_->instruction_set == kThumb2) {
1529 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1530 // Use ARM SDIV instruction for division. For remainder we also need to
1531 // calculate using a MUL and subtract.
1532 rl_src1 = LoadValue(rl_src1, kCoreReg);
1533 rl_src2 = LoadValue(rl_src2, kCoreReg);
1534 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001535 GenImmedCheck(kCondEq, rl_src2.reg, 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001536 }
buzbee2700f7e2014-03-07 09:46:20 -08001537 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001538 done = true;
1539 }
1540 }
1541
1542 // If we haven't already generated the code use the callout function.
1543 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001544 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 FlushAllRegs(); /* Send everything to home location */
1546 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
buzbee2700f7e2014-03-07 09:46:20 -08001547 RegStorage r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1549 if (check_zero) {
1550 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1551 }
Dave Allison70202782013-10-22 17:52:19 -07001552 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001553 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 if (op == kOpDiv)
1555 rl_result = GetReturn(false);
1556 else
1557 rl_result = GetReturnAlt();
1558 }
1559 StoreValue(rl_dest, rl_result);
1560 }
1561}
1562
1563/*
1564 * The following are the first-level codegen routines that analyze the format
1565 * of each bytecode then either dispatch special purpose codegen routines
1566 * or produce corresponding Thumb instructions directly.
1567 */
1568
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001570static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 x &= x - 1;
1572 return (x & (x - 1)) == 0;
1573}
1574
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1576// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001577bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001578 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1580 return false;
1581 }
1582 // No divide instruction for Arm, so check for more special cases
1583 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001584 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 }
1586 int k = LowestSetBit(lit);
1587 if (k >= 30) {
1588 // Avoid special cases.
1589 return false;
1590 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 rl_src = LoadValue(rl_src, kCoreReg);
1592 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001593 if (is_div) {
buzbee2700f7e2014-03-07 09:46:20 -08001594 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 if (lit == 2) {
1596 // Division by 2 is by far the most common division by constant.
buzbee2700f7e2014-03-07 09:46:20 -08001597 OpRegRegImm(kOpLsr, t_reg, rl_src.reg, 32 - k);
1598 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1599 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001601 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001603 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg);
1604 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 }
1606 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001607 RegStorage t_reg1 = AllocTemp();
1608 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 if (lit == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg, 32 - k);
1611 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
buzbee2700f7e2014-03-07 09:46:20 -08001613 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001614 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001615 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg, 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001616 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
buzbee2700f7e2014-03-07 09:46:20 -08001617 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
buzbee2700f7e2014-03-07 09:46:20 -08001619 OpRegRegReg(kOpSub, rl_result.reg, t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
1621 }
1622 StoreValue(rl_dest, rl_result);
1623 return true;
1624}
1625
1626// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1627// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001628bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 // Can we simplify this multiplication?
1630 bool power_of_two = false;
1631 bool pop_count_le2 = false;
1632 bool power_of_two_minus_one = false;
Brian Carlstrom3654a6f2014-03-27 17:14:26 -07001633 if (lit < 2) {
1634 // Avoid special cases.
1635 return false;
1636 } else if (IsPowerOfTwo(lit)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 power_of_two = true;
1638 } else if (IsPopCountLE2(lit)) {
1639 pop_count_le2 = true;
1640 } else if (IsPowerOfTwo(lit + 1)) {
1641 power_of_two_minus_one = true;
1642 } else {
1643 return false;
1644 }
1645 rl_src = LoadValue(rl_src, kCoreReg);
1646 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1647 if (power_of_two) {
1648 // Shift.
buzbee2700f7e2014-03-07 09:46:20 -08001649 OpRegRegImm(kOpLsl, rl_result.reg, rl_src.reg, LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 } else if (pop_count_le2) {
1651 // Shift and add and shift.
1652 int first_bit = LowestSetBit(lit);
1653 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1654 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1655 } else {
1656 // Reverse subtract: (src << (shift + 1)) - src.
1657 DCHECK(power_of_two_minus_one);
1658 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbee2700f7e2014-03-07 09:46:20 -08001659 RegStorage t_reg = AllocTemp();
1660 OpRegRegImm(kOpLsl, t_reg, rl_src.reg, LowestSetBit(lit + 1));
1661 OpRegRegReg(kOpSub, rl_result.reg, t_reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001662 }
1663 StoreValue(rl_dest, rl_result);
1664 return true;
1665}
1666
1667void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001668 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 RegLocation rl_result;
1670 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1671 int shift_op = false;
1672 bool is_div = false;
1673
1674 switch (opcode) {
1675 case Instruction::RSUB_INT_LIT8:
1676 case Instruction::RSUB_INT: {
1677 rl_src = LoadValue(rl_src, kCoreReg);
1678 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1679 if (cu_->instruction_set == kThumb2) {
buzbee2700f7e2014-03-07 09:46:20 -08001680 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001681 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001682 OpRegReg(kOpNeg, rl_result.reg, rl_src.reg);
1683 OpRegImm(kOpAdd, rl_result.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 }
1685 StoreValue(rl_dest, rl_result);
1686 return;
1687 }
1688
1689 case Instruction::SUB_INT:
1690 case Instruction::SUB_INT_2ADDR:
1691 lit = -lit;
1692 // Intended fallthrough
1693 case Instruction::ADD_INT:
1694 case Instruction::ADD_INT_2ADDR:
1695 case Instruction::ADD_INT_LIT8:
1696 case Instruction::ADD_INT_LIT16:
1697 op = kOpAdd;
1698 break;
1699 case Instruction::MUL_INT:
1700 case Instruction::MUL_INT_2ADDR:
1701 case Instruction::MUL_INT_LIT8:
1702 case Instruction::MUL_INT_LIT16: {
1703 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1704 return;
1705 }
1706 op = kOpMul;
1707 break;
1708 }
1709 case Instruction::AND_INT:
1710 case Instruction::AND_INT_2ADDR:
1711 case Instruction::AND_INT_LIT8:
1712 case Instruction::AND_INT_LIT16:
1713 op = kOpAnd;
1714 break;
1715 case Instruction::OR_INT:
1716 case Instruction::OR_INT_2ADDR:
1717 case Instruction::OR_INT_LIT8:
1718 case Instruction::OR_INT_LIT16:
1719 op = kOpOr;
1720 break;
1721 case Instruction::XOR_INT:
1722 case Instruction::XOR_INT_2ADDR:
1723 case Instruction::XOR_INT_LIT8:
1724 case Instruction::XOR_INT_LIT16:
1725 op = kOpXor;
1726 break;
1727 case Instruction::SHL_INT_LIT8:
1728 case Instruction::SHL_INT:
1729 case Instruction::SHL_INT_2ADDR:
1730 lit &= 31;
1731 shift_op = true;
1732 op = kOpLsl;
1733 break;
1734 case Instruction::SHR_INT_LIT8:
1735 case Instruction::SHR_INT:
1736 case Instruction::SHR_INT_2ADDR:
1737 lit &= 31;
1738 shift_op = true;
1739 op = kOpAsr;
1740 break;
1741 case Instruction::USHR_INT_LIT8:
1742 case Instruction::USHR_INT:
1743 case Instruction::USHR_INT_2ADDR:
1744 lit &= 31;
1745 shift_op = true;
1746 op = kOpLsr;
1747 break;
1748
1749 case Instruction::DIV_INT:
1750 case Instruction::DIV_INT_2ADDR:
1751 case Instruction::DIV_INT_LIT8:
1752 case Instruction::DIV_INT_LIT16:
1753 case Instruction::REM_INT:
1754 case Instruction::REM_INT_2ADDR:
1755 case Instruction::REM_INT_LIT8:
1756 case Instruction::REM_INT_LIT16: {
1757 if (lit == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001758 GenImmedCheck(kCondAl, RegStorage::InvalidReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001759 return;
1760 }
buzbee11b63d12013-08-27 07:34:17 -07001761 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001762 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001763 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001764 (opcode == Instruction::DIV_INT_LIT16)) {
1765 is_div = true;
1766 } else {
1767 is_div = false;
1768 }
buzbee11b63d12013-08-27 07:34:17 -07001769 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1770 return;
1771 }
Dave Allison70202782013-10-22 17:52:19 -07001772
1773 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 if (cu_->instruction_set == kMips) {
1775 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001776 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001777 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001778 } else if (cu_->instruction_set == kX86) {
1779 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1780 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001781 } else if (cu_->instruction_set == kThumb2) {
1782 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1783 // Use ARM SDIV instruction for division. For remainder we also need to
1784 // calculate using a MUL and subtract.
1785 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -08001786 rl_result = GenDivRemLit(rl_dest, rl_src.reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001787 done = true;
1788 }
1789 }
1790
1791 if (!done) {
1792 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001793 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1794 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001795 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001796 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1797 if (is_div)
1798 rl_result = GetReturn(false);
1799 else
1800 rl_result = GetReturnAlt();
1801 }
1802 StoreValue(rl_dest, rl_result);
1803 return;
1804 }
1805 default:
1806 LOG(FATAL) << "Unexpected opcode " << opcode;
1807 }
1808 rl_src = LoadValue(rl_src, kCoreReg);
1809 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001810 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001811 if (shift_op && (lit == 0)) {
buzbee2700f7e2014-03-07 09:46:20 -08001812 OpRegCopy(rl_result.reg, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001813 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001814 OpRegRegImm(op, rl_result.reg, rl_src.reg, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001815 }
1816 StoreValue(rl_dest, rl_result);
1817}
1818
1819void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001820 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001821 RegLocation rl_result;
1822 OpKind first_op = kOpBkpt;
1823 OpKind second_op = kOpBkpt;
1824 bool call_out = false;
1825 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001826 ThreadOffset func_offset(-1);
buzbee2700f7e2014-03-07 09:46:20 -08001827 int ret_reg = TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001828
1829 switch (opcode) {
1830 case Instruction::NOT_LONG:
1831 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1832 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1833 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001834 if (rl_result.reg.GetLowReg() == rl_src2.reg.GetHighReg()) {
1835 RegStorage t_reg = AllocTemp();
1836 OpRegCopy(t_reg, rl_src2.reg.GetHigh());
1837 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1838 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001839 FreeTemp(t_reg);
1840 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001841 OpRegReg(kOpMvn, rl_result.reg.GetLow(), rl_src2.reg.GetLow());
1842 OpRegReg(kOpMvn, rl_result.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001843 }
1844 StoreValueWide(rl_dest, rl_result);
1845 return;
1846 case Instruction::ADD_LONG:
1847 case Instruction::ADD_LONG_2ADDR:
1848 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001849 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 return;
1851 }
1852 first_op = kOpAdd;
1853 second_op = kOpAdc;
1854 break;
1855 case Instruction::SUB_LONG:
1856 case Instruction::SUB_LONG_2ADDR:
1857 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001858 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001859 return;
1860 }
1861 first_op = kOpSub;
1862 second_op = kOpSbc;
1863 break;
1864 case Instruction::MUL_LONG:
1865 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001866 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001867 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001868 return;
1869 } else {
1870 call_out = true;
buzbee2700f7e2014-03-07 09:46:20 -08001871 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogers7655f292013-07-29 11:07:13 -07001872 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001873 }
1874 break;
1875 case Instruction::DIV_LONG:
1876 case Instruction::DIV_LONG_2ADDR:
1877 call_out = true;
1878 check_zero = true;
buzbee2700f7e2014-03-07 09:46:20 -08001879 ret_reg = TargetReg(kRet0).GetReg();
Ian Rogers7655f292013-07-29 11:07:13 -07001880 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001881 break;
1882 case Instruction::REM_LONG:
1883 case Instruction::REM_LONG_2ADDR:
1884 call_out = true;
1885 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001886 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbee2700f7e2014-03-07 09:46:20 -08001888 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2).GetReg() : TargetReg(kRet0).GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001889 break;
1890 case Instruction::AND_LONG_2ADDR:
1891 case Instruction::AND_LONG:
1892 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001893 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001894 }
1895 first_op = kOpAnd;
1896 second_op = kOpAnd;
1897 break;
1898 case Instruction::OR_LONG:
1899 case Instruction::OR_LONG_2ADDR:
1900 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001901 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 return;
1903 }
1904 first_op = kOpOr;
1905 second_op = kOpOr;
1906 break;
1907 case Instruction::XOR_LONG:
1908 case Instruction::XOR_LONG_2ADDR:
1909 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001910 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001911 return;
1912 }
1913 first_op = kOpXor;
1914 second_op = kOpXor;
1915 break;
1916 case Instruction::NEG_LONG: {
1917 GenNegLong(rl_dest, rl_src2);
1918 return;
1919 }
1920 default:
1921 LOG(FATAL) << "Invalid long arith op";
1922 }
1923 if (!call_out) {
1924 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1925 } else {
1926 FlushAllRegs(); /* Send everything to home location */
1927 if (check_zero) {
buzbee2700f7e2014-03-07 09:46:20 -08001928 RegStorage r_tmp1 = RegStorage::MakeRegPair(TargetReg(kArg0), TargetReg(kArg1));
1929 RegStorage r_tmp2 = RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3));
1930 LoadValueDirectWideFixed(rl_src2, r_tmp2);
1931 RegStorage r_tgt = CallHelperSetup(func_offset);
1932 GenDivZeroCheck(RegStorage::MakeRegPair(TargetReg(kArg2), TargetReg(kArg3)));
1933 LoadValueDirectWideFixed(rl_src1, r_tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001934 // NOTE: callout here is not a safepoint
1935 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1936 } else {
1937 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1938 }
1939 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbee2700f7e2014-03-07 09:46:20 -08001940 if (ret_reg == TargetReg(kRet0).GetReg())
Brian Carlstrom7940e442013-07-12 13:46:57 -07001941 rl_result = GetReturnWide(false);
1942 else
1943 rl_result = GetReturnWideAlt();
1944 StoreValueWide(rl_dest, rl_result);
1945 }
1946}
1947
Ian Rogers848871b2013-08-05 10:56:33 -07001948void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001949 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001950 /*
1951 * Don't optimize the register usage since it calls out to support
1952 * functions
1953 */
1954 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001955 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1956 if (rl_dest.wide) {
1957 RegLocation rl_result;
1958 rl_result = GetReturnWide(rl_dest.fp);
1959 StoreValueWide(rl_dest, rl_result);
1960 } else {
1961 RegLocation rl_result;
1962 rl_result = GetReturn(rl_dest.fp);
1963 StoreValue(rl_dest, rl_result);
1964 }
1965}
1966
1967/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001968void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001969 if (Runtime::Current()->ExplicitSuspendChecks()) {
1970 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1971 return;
1972 }
1973 FlushAllRegs();
1974 LIR* branch = OpTestSuspend(NULL);
1975 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
1976 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1977 current_dalvik_offset_);
1978 branch->target = target;
1979 suspend_launchpads_.Insert(target);
1980 } else {
1981 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1982 return;
1983 }
1984 FlushAllRegs(); // TODO: needed?
1985 LIR* inst = CheckSuspendUsingLoad();
1986 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001988}
1989
1990/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001991void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08001992 if (Runtime::Current()->ExplicitSuspendChecks()) {
1993 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1994 OpUnconditionalBranch(target);
1995 return;
1996 }
1997 OpTestSuspend(target);
1998 LIR* launch_pad =
1999 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
2000 current_dalvik_offset_);
2001 FlushAllRegs();
2002 OpUnconditionalBranch(launch_pad);
2003 suspend_launchpads_.Insert(launch_pad);
2004 } else {
2005 // For the implicit suspend check, just perform the trigger
2006 // load and branch to the target.
2007 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2008 OpUnconditionalBranch(target);
2009 return;
2010 }
2011 FlushAllRegs();
2012 LIR* inst = CheckSuspendUsingLoad();
2013 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002015 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002016}
2017
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002018/* Call out to helper assembly routine that will null check obj and then lock it. */
2019void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2020 FlushAllRegs();
2021 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
2022}
2023
2024/* Call out to helper assembly routine that will null check obj and then unlock it. */
2025void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2026 FlushAllRegs();
2027 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2028}
2029
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002030/* Generic code for generating a wide constant into a VR. */
2031void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2032 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08002033 LoadConstantWide(rl_result.reg, value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002034 StoreValueWide(rl_dest, rl_result);
2035}
2036
Brian Carlstrom7940e442013-07-12 13:46:57 -07002037} // namespace art