blob: 8c3a11fb6c6cfd065e0100c893ff6f63482e025a [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
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070057LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, int reg, int imm_val, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg, imm_val);
59 LIR* branch;
60 if (c_code == kCondAl) {
61 branch = OpUnconditionalBranch(tgt);
62 } else {
63 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
64 }
65 // Remember branch target - will process later
66 throw_launchpads_.Insert(tgt);
67 return branch;
68}
69
Dave Allisonb373e092014-02-20 16:06:36 -080070
Brian Carlstrom7940e442013-07-12 13:46:57 -070071/* Perform null-check on a register. */
Dave Allisonb373e092014-02-20 16:06:36 -080072LIR* Mir2Lir::GenNullCheck(int m_reg, int opt_flags) {
73 if (Runtime::Current()->ExplicitNullChecks()) {
74 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
75 return NULL;
76 }
77 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 }
Dave Allisonb373e092014-02-20 16:06:36 -080079 return nullptr;
80}
81
82void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
83 if (!Runtime::Current()->ExplicitNullChecks()) {
84 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
85 return;
86 }
87 MarkSafepointPC(last_lir_insn_);
88 }
89}
90
91void Mir2Lir::MarkPossibleStackOverflowException() {
92 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
93 MarkSafepointPC(last_lir_insn_);
94 }
95}
96
97void Mir2Lir::ForceImplicitNullCheck(int reg, int opt_flags) {
98 if (!Runtime::Current()->ExplicitNullChecks()) {
99 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
100 return;
101 }
102 // Force an implicit null check by performing a memory operation (load) from the given
103 // register with offset 0. This will cause a signal if the register contains 0 (null).
104 int tmp = AllocTemp();
105 LIR* load = LoadWordDisp(reg, 0, tmp);
106 FreeTemp(tmp);
107 MarkSafepointPC(load);
108 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109}
110
111/* Perform check on two registers */
112LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700113 ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
115 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
116 // Remember branch target - will process later
117 throw_launchpads_.Insert(tgt);
118 return branch;
119}
120
121void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
122 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700123 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 ConditionCode cond;
125 switch (opcode) {
126 case Instruction::IF_EQ:
127 cond = kCondEq;
128 break;
129 case Instruction::IF_NE:
130 cond = kCondNe;
131 break;
132 case Instruction::IF_LT:
133 cond = kCondLt;
134 break;
135 case Instruction::IF_GE:
136 cond = kCondGe;
137 break;
138 case Instruction::IF_GT:
139 cond = kCondGt;
140 break;
141 case Instruction::IF_LE:
142 cond = kCondLe;
143 break;
144 default:
145 cond = static_cast<ConditionCode>(0);
146 LOG(FATAL) << "Unexpected opcode " << opcode;
147 }
148
149 // Normalize such that if either operand is constant, src2 will be constant
150 if (rl_src1.is_const) {
151 RegLocation rl_temp = rl_src1;
152 rl_src1 = rl_src2;
153 rl_src2 = rl_temp;
154 cond = FlipComparisonOrder(cond);
155 }
156
157 rl_src1 = LoadValue(rl_src1, kCoreReg);
158 // Is this really an immediate comparison?
159 if (rl_src2.is_const) {
160 // If it's already live in a register or not easily materialized, just keep going
161 RegLocation rl_temp = UpdateLoc(rl_src2);
162 if ((rl_temp.location == kLocDalvikFrame) &&
163 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
164 // OK - convert this to a compare immediate and branch
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000165 OpCmpImmBranch(cond, rl_src1.reg.GetReg(), mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 return;
167 }
168 }
169 rl_src2 = LoadValue(rl_src2, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000170 OpCmpBranch(cond, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171}
172
173void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700174 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 ConditionCode cond;
176 rl_src = LoadValue(rl_src, kCoreReg);
177 switch (opcode) {
178 case Instruction::IF_EQZ:
179 cond = kCondEq;
180 break;
181 case Instruction::IF_NEZ:
182 cond = kCondNe;
183 break;
184 case Instruction::IF_LTZ:
185 cond = kCondLt;
186 break;
187 case Instruction::IF_GEZ:
188 cond = kCondGe;
189 break;
190 case Instruction::IF_GTZ:
191 cond = kCondGt;
192 break;
193 case Instruction::IF_LEZ:
194 cond = kCondLe;
195 break;
196 default:
197 cond = static_cast<ConditionCode>(0);
198 LOG(FATAL) << "Unexpected opcode " << opcode;
199 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000200 OpCmpImmBranch(cond, rl_src.reg.GetReg(), 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201}
202
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700203void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
205 if (rl_src.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000206 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000208 LoadValueDirect(rl_src, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000210 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_result.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 StoreValueWide(rl_dest, rl_result);
212}
213
214void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700216 rl_src = LoadValue(rl_src, kCoreReg);
217 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
218 OpKind op = kOpInvalid;
219 switch (opcode) {
220 case Instruction::INT_TO_BYTE:
221 op = kOp2Byte;
222 break;
223 case Instruction::INT_TO_SHORT:
224 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700226 case Instruction::INT_TO_CHAR:
227 op = kOp2Char;
228 break;
229 default:
230 LOG(ERROR) << "Bad int conversion type";
231 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000232 OpRegReg(op, rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700233 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234}
235
236/*
237 * Let helper function take care of everything. Will call
238 * Array::AllocFromCode(type_idx, method, count);
239 * Note: AllocFromCode will handle checks for errNegativeArraySize.
240 */
241void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700242 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700244 ThreadOffset func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800245 const DexFile* dex_file = cu_->dex_file;
246 CompilerDriver* driver = cu_->compiler_driver;
247 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800249 bool is_type_initialized; // Ignored as an array does not have an initializer.
250 bool use_direct_type_ptr;
251 uintptr_t direct_type_ptr;
252 if (kEmbedClassInCode &&
253 driver->CanEmbedTypeInCode(*dex_file, type_idx,
254 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
255 // The fast path.
256 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800257 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800258 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
259 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
260 } else {
261 // Use the direct pointer.
262 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
263 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
264 }
265 } else {
266 // The slow path.
267 DCHECK_EQ(func_offset.Int32Value(), -1);
268 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
269 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
270 }
271 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700273 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800274 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 RegLocation rl_result = GetReturn(false);
277 StoreValue(rl_dest, rl_result);
278}
279
280/*
281 * Similar to GenNewArray, but with post-allocation initialization.
282 * Verifier guarantees we're dealing with an array class. Current
283 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
284 * Current code also throws internal unimp if not 'L', '[' or 'I'.
285 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700286void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 int elems = info->num_arg_words;
288 int type_idx = info->index;
289 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700290 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
292 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700293 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700295 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
297 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
298 FreeTemp(TargetReg(kArg2));
299 FreeTemp(TargetReg(kArg1));
300 /*
301 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
302 * return region. Because AllocFromCode placed the new array
303 * in kRet0, we'll just lock it into place. When debugger support is
304 * added, it may be necessary to additionally copy all return
305 * values to a home location in thread-local storage
306 */
307 LockTemp(TargetReg(kRet0));
308
309 // TODO: use the correct component size, currently all supported types
310 // share array alignment with ints (see comment at head of function)
311 size_t component_size = sizeof(int32_t);
312
313 // Having a range of 0 is legal
314 if (info->is_range && (elems > 0)) {
315 /*
316 * Bit of ugliness here. We're going generate a mem copy loop
317 * on the register range, but it is possible that some regs
318 * in the range have been promoted. This is unlikely, but
319 * before generating the copy, we'll just force a flush
320 * of any regs in the source range that have been promoted to
321 * home location.
322 */
323 for (int i = 0; i < elems; i++) {
324 RegLocation loc = UpdateLoc(info->args[i]);
325 if (loc.location == kLocPhysReg) {
326 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000327 loc.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 }
329 }
330 /*
331 * TUNING note: generated code here could be much improved, but
332 * this is an uncommon operation and isn't especially performance
333 * critical.
334 */
335 int r_src = AllocTemp();
336 int r_dst = AllocTemp();
337 int r_idx = AllocTemp();
338 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700339 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 case kThumb2:
341 r_val = TargetReg(kLr);
342 break;
343 case kX86:
344 FreeTemp(TargetReg(kRet0));
345 r_val = AllocTemp();
346 break;
347 case kMips:
348 r_val = AllocTemp();
349 break;
350 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
351 }
352 // Set up source pointer
353 RegLocation rl_first = info->args[0];
354 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
355 // Set up the target pointer
356 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
357 mirror::Array::DataOffset(component_size).Int32Value());
358 // Set up the loop counter (known to be > 0)
359 LoadConstant(r_idx, elems - 1);
360 // Generate the copy loop. Going backwards for convenience
361 LIR* target = NewLIR0(kPseudoTargetLabel);
362 // Copy next element
363 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
364 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
365 FreeTemp(r_val);
366 OpDecAndBranch(kCondGe, r_idx, target);
367 if (cu_->instruction_set == kX86) {
368 // Restore the target pointer
369 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
370 -mirror::Array::DataOffset(component_size).Int32Value());
371 }
372 } else if (!info->is_range) {
373 // TUNING: interleave
374 for (int i = 0; i < elems; i++) {
375 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
376 StoreBaseDisp(TargetReg(kRet0),
377 mirror::Array::DataOffset(component_size).Int32Value() +
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000378 i * 4, rl_arg.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 // If the LoadValue caused a temp to be allocated, free it
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000380 if (IsTemp(rl_arg.reg.GetReg())) {
381 FreeTemp(rl_arg.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383 }
384 }
385 if (info->result.location != kLocInvalid) {
386 StoreValue(info->result, GetReturn(false /* not fp */));
387 }
388}
389
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800390//
391// Slow path to ensure a class is initialized for sget/sput.
392//
393class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
394 public:
395 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
396 int storage_index, int r_base) :
397 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
398 r_base_(r_base) {
399 }
400
401 void Compile() {
402 LIR* unresolved_target = GenerateTargetLabel();
403 uninit_->target = unresolved_target;
404 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
405 storage_index_, true);
406 // Copy helper's result into r_base, a no-op on all but MIPS.
407 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
408
409 m2l_->OpUnconditionalBranch(cont_);
410 }
411
412 private:
413 LIR* const uninit_;
414 const int storage_index_;
415 const int r_base_;
416};
417
Vladimir Markobe0e5462014-02-26 11:24:15 +0000418void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700419 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000420 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
421 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
422 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
423 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800424 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000425 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 // Fast path, static storage base is this method's class
427 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800428 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000429 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800430 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000431 if (IsTemp(rl_method.reg.GetReg())) {
432 FreeTemp(rl_method.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 } else {
435 // Medium path, static storage base in a different class which requires checks that the other
436 // class is initialized.
437 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000438 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 // May do runtime call so everything to home locations.
440 FlushAllRegs();
441 // Using fixed register to sync with possible call to runtime support.
442 int r_method = TargetReg(kArg1);
443 LockTemp(r_method);
444 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800445 r_base = TargetReg(kArg0);
446 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800448 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
449 r_base);
450 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000451 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800452 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000453 if (!field_info.IsInitialized() &&
454 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800455 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800456
457 // The slow path is invoked if the r_base is NULL or the class pointed
458 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800459 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
460 int r_tmp = TargetReg(kArg2);
461 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800462 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800463 mirror::Class::StatusOffset().Int32Value(),
464 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800465 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800466
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800467 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
468 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()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 GenMemBarrier(kStoreStore);
483 }
484 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000485 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg(),
486 rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000488 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000490 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 GenMemBarrier(kStoreLoad);
492 }
493 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000494 MarkGCCard(rl_src.reg.GetReg(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800496 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 } else {
498 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700499 ThreadOffset setter_offset =
500 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
501 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
502 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000503 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 }
505}
506
Vladimir Markobe0e5462014-02-26 11:24:15 +0000507void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000509 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
510 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
511 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
512 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800513 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000514 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 // Fast path, static storage base is this method's class
516 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800517 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000518 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800519 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.
527 int r_method = TargetReg(kArg1);
528 LockTemp(r_method);
529 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800530 r_base = TargetReg(kArg0);
531 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800533 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
534 r_base);
535 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000536 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800537 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000538 if (!field_info.IsInitialized() &&
539 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800540 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800541
542 // The slow path is invoked if the r_base is NULL or the class pointed
543 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800544 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
545 int r_tmp = TargetReg(kArg2);
546 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800547 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800548 mirror::Class::StatusOffset().Int32Value(),
549 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800550 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800551
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800552 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
553 unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000554 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800555
556 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 FreeTemp(r_method);
559 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800560 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000562 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 GenMemBarrier(kLoadLoad);
564 }
565 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000566 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg(),
567 rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000569 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800571 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 if (is_long_or_double) {
573 StoreValueWide(rl_dest, rl_result);
574 } else {
575 StoreValue(rl_dest, rl_result);
576 }
577 } else {
578 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700579 ThreadOffset getterOffset =
580 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
581 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
582 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000583 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 if (is_long_or_double) {
585 RegLocation rl_result = GetReturnWide(rl_dest.fp);
586 StoreValueWide(rl_dest, rl_result);
587 } else {
588 RegLocation rl_result = GetReturn(rl_dest.fp);
589 StoreValue(rl_dest, rl_result);
590 }
591 }
592}
593
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800594// Generate code for all slow paths.
595void Mir2Lir::HandleSlowPaths() {
596 int n = slow_paths_.Size();
597 for (int i = 0; i < n; ++i) {
598 LIRSlowPath* slowpath = slow_paths_.Get(i);
599 slowpath->Compile();
600 }
601 slow_paths_.Reset();
602}
603
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700604void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700606 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 for (int i = 0; i < num_elems; i++) {
608 ResetRegPool();
609 ResetDefTracking();
610 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700611 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 current_dalvik_offset_ = lab->operands[1];
613 AppendLIR(lab);
614 int r_tgt = CallHelperSetup(helper_offset);
615 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
616 OpUnconditionalBranch(resume_lab);
617 }
618}
619
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700620void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 int num_elems = throw_launchpads_.Size();
622 for (int i = 0; i < num_elems; i++) {
623 ResetRegPool();
624 ResetDefTracking();
625 LIR* lab = throw_launchpads_.Get(i);
626 current_dalvik_offset_ = lab->operands[1];
627 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700628 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 int v1 = lab->operands[2];
630 int v2 = lab->operands[3];
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700631 const bool target_x86 = cu_->instruction_set == kX86;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 switch (lab->operands[0]) {
633 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700634 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700636 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
638 if (target_x86) {
639 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
640 } else {
641 OpRegCopy(TargetReg(kArg1), v1);
642 }
643 // Make sure the following LoadConstant doesn't mess with kArg1.
644 LockTemp(TargetReg(kArg1));
645 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700646 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 break;
648 case kThrowArrayBounds:
649 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
650 if (v2 != TargetReg(kArg0)) {
651 OpRegCopy(TargetReg(kArg0), v1);
652 if (target_x86) {
653 // x86 leaves the array pointer in v2, so load the array length that the handler expects
654 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
655 } else {
656 OpRegCopy(TargetReg(kArg1), v2);
657 }
658 } else {
659 if (v1 == TargetReg(kArg1)) {
660 // Swap v1 and v2, using kArg2 as a temp
661 OpRegCopy(TargetReg(kArg2), v1);
662 if (target_x86) {
663 // x86 leaves the array pointer in v2; load the array length that the handler expects
664 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
665 } else {
666 OpRegCopy(TargetReg(kArg1), v2);
667 }
668 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
669 } else {
670 if (target_x86) {
671 // x86 leaves the array pointer in v2; load the array length that the handler expects
672 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
673 } else {
674 OpRegCopy(TargetReg(kArg1), v2);
675 }
676 OpRegCopy(TargetReg(kArg0), v1);
677 }
678 }
Ian Rogers848871b2013-08-05 10:56:33 -0700679 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 break;
681 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700682 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 break;
684 case kThrowNoSuchMethod:
685 OpRegCopy(TargetReg(kArg0), v1);
686 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700687 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 default:
690 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
691 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000692 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 int r_tgt = CallHelperSetup(func_offset);
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700694 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */, true /* UseLink */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 }
696}
697
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700700 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000701 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
702 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
703 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 RegLocation rl_result;
705 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 rl_obj = LoadValue(rl_obj, kCoreReg);
708 if (is_long_or_double) {
709 DCHECK(rl_dest.wide);
Dave Allisonb373e092014-02-20 16:06:36 -0800710 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 if (cu_->instruction_set == kX86) {
712 rl_result = EvalLoc(rl_dest, reg_class, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800713 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000714 LoadBaseDispWide(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
Dave Allisonb373e092014-02-20 16:06:36 -0800715 rl_result.reg.GetReg(),
716 rl_result.reg.GetHighReg(), rl_obj.s_reg_low);
717 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000718 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 GenMemBarrier(kLoadLoad);
720 }
721 } else {
722 int reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000723 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000725 LoadBaseDispWide(reg_ptr, 0, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(),
726 INVALID_SREG);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000727 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 GenMemBarrier(kLoadLoad);
729 }
730 FreeTemp(reg_ptr);
731 }
732 StoreValueWide(rl_dest, rl_result);
733 } else {
734 rl_result = EvalLoc(rl_dest, reg_class, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800735 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000736 LoadBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
737 rl_result.reg.GetReg(), kWord, rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800738 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000739 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 GenMemBarrier(kLoadLoad);
741 }
742 StoreValue(rl_dest, rl_result);
743 }
744 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700745 ThreadOffset getterOffset =
746 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
747 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
748 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000749 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 if (is_long_or_double) {
751 RegLocation rl_result = GetReturnWide(rl_dest.fp);
752 StoreValueWide(rl_dest, rl_result);
753 } else {
754 RegLocation rl_result = GetReturn(rl_dest.fp);
755 StoreValue(rl_dest, rl_result);
756 }
757 }
758}
759
Vladimir Markobe0e5462014-02-26 11:24:15 +0000760void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700762 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000763 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
764 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
765 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000767 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 rl_obj = LoadValue(rl_obj, kCoreReg);
769 if (is_long_or_double) {
770 int reg_ptr;
771 rl_src = LoadValueWide(rl_src, kAnyReg);
Dave Allisonb373e092014-02-20 16:06:36 -0800772 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000774 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000775 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 GenMemBarrier(kStoreStore);
777 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000778 StoreBaseDispWide(reg_ptr, 0, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Dave Allisonb373e092014-02-20 16:06:36 -0800779 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000780 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 GenMemBarrier(kLoadLoad);
782 }
783 FreeTemp(reg_ptr);
784 } else {
785 rl_src = LoadValue(rl_src, reg_class);
Dave Allisonb373e092014-02-20 16:06:36 -0800786 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000787 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 GenMemBarrier(kStoreStore);
789 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000790 StoreBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
Dave Allisonb373e092014-02-20 16:06:36 -0800791 rl_src.reg.GetReg(), kWord);
792 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000793 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 GenMemBarrier(kLoadLoad);
795 }
796 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000797 MarkGCCard(rl_src.reg.GetReg(), rl_obj.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 }
799 }
800 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700801 ThreadOffset setter_offset =
802 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
803 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
804 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000805 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
806 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 }
808}
809
Ian Rogersa9a82542013-10-04 11:17:26 -0700810void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
811 RegLocation rl_src) {
812 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
813 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
814 (opt_flags & MIR_IGNORE_NULL_CHECK));
815 ThreadOffset helper = needs_range_check
816 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
817 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
818 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
819 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
820}
821
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700822void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700823 RegLocation rl_method = LoadCurrMethod();
824 int res_reg = AllocTemp();
825 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
826 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
827 *cu_->dex_file,
828 type_idx)) {
829 // Call out to helper which resolves type and verifies access.
830 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700831 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000832 type_idx, rl_method.reg.GetReg(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700833 RegLocation rl_result = GetReturn(false);
834 StoreValue(rl_dest, rl_result);
835 } else {
836 // We're don't need access checks, load type from dex cache
837 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700838 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000839 LoadWordDisp(rl_method.reg.GetReg(), dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840 int32_t offset_of_type =
841 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
842 * type_idx);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000843 LoadWordDisp(res_reg, offset_of_type, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
845 type_idx) || SLOW_TYPE_PATH) {
846 // Slow path, at runtime test if type is null and if so initialize
847 FlushAllRegs();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000848 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg.GetReg(), 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800849 LIR* cont = NewLIR0(kPseudoTargetLabel);
850
851 // Object to generate the slow path for class resolution.
852 class SlowPath : public LIRSlowPath {
853 public:
854 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
855 const RegLocation& rl_method, const RegLocation& rl_result) :
856 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
857 rl_method_(rl_method), rl_result_(rl_result) {
858 }
859
860 void Compile() {
861 GenerateTargetLabel();
862
863 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000864 rl_method_.reg.GetReg(), true);
865 m2l_->OpRegCopy(rl_result_.reg.GetReg(), m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800866
867 m2l_->OpUnconditionalBranch(cont_);
868 }
869
870 private:
871 const int type_idx_;
872 const RegLocation rl_method_;
873 const RegLocation rl_result_;
874 };
875
876 // Add to list for future.
877 AddSlowPath(new (arena_) SlowPath(this, branch, cont,
878 type_idx, rl_method, rl_result));
879
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800881 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 // Fast path, we're done - just store result
883 StoreValue(rl_dest, rl_result);
884 }
885 }
886}
887
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700888void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 /* NOTE: Most strings should be available at compile time */
890 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
891 (sizeof(mirror::String*) * string_idx);
892 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
893 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
894 // slow path, resolve string if not in dex cache
895 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700896 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800897
898 // If the Method* is already in a register, we can save a copy.
899 RegLocation rl_method = mir_graph_->GetMethodLoc();
900 int r_method;
901 if (rl_method.location == kLocPhysReg) {
902 // A temp would conflict with register use below.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000903 DCHECK(!IsTemp(rl_method.reg.GetReg()));
904 r_method = rl_method.reg.GetReg();
Mark Mendell766e9292014-01-27 07:55:47 -0800905 } else {
906 r_method = TargetReg(kArg2);
907 LoadCurrMethodDirect(r_method);
908 }
909 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
910 TargetReg(kArg0));
911
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800914 if (cu_->instruction_set == kThumb2 ||
915 cu_->instruction_set == kMips) {
916 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800917 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800918 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
919 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800921
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800922 // Object to generate the slow path for string resolution.
923 class SlowPath : public LIRSlowPath {
924 public:
925 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
926 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
927 }
928
929 void Compile() {
930 GenerateTargetLabel();
931
932 int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
933
934 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
935 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
936 m2l_->MarkSafepointPC(call_inst);
937 m2l_->FreeTemp(r_tgt);
938
939 m2l_->OpUnconditionalBranch(cont_);
940 }
941
942 private:
943 int r_method_;
944 };
945
946 // Add to list for future.
947 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 } else {
949 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800950 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
951 LoadConstant(TargetReg(kArg1), string_idx);
952 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
Ian Rogers7655f292013-07-29 11:07:13 -0700953 TargetReg(kArg1), true);
Mark Mendell766e9292014-01-27 07:55:47 -0800954 LIR* target = NewLIR0(kPseudoTargetLabel);
955 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 }
957 GenBarrier();
958 StoreValue(rl_dest, GetReturn(false));
959 } else {
960 RegLocation rl_method = LoadCurrMethod();
961 int res_reg = AllocTemp();
962 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000963 LoadWordDisp(rl_method.reg.GetReg(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700964 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000965 LoadWordDisp(res_reg, offset_of_string, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 StoreValue(rl_dest, rl_result);
967 }
968}
969
970/*
971 * Let helper function take care of everything. Will
972 * call Class::NewInstanceFromCode(type_idx, method);
973 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700974void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 FlushAllRegs(); /* Everything to home location */
976 // alloc will always check for resolution, do we also need to verify
977 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700978 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800979 const DexFile* dex_file = cu_->dex_file;
980 CompilerDriver* driver = cu_->compiler_driver;
981 if (driver->CanAccessInstantiableTypeWithoutChecks(
982 cu_->method_idx, *dex_file, type_idx)) {
983 bool is_type_initialized;
984 bool use_direct_type_ptr;
985 uintptr_t direct_type_ptr;
986 if (kEmbedClassInCode &&
987 driver->CanEmbedTypeInCode(*dex_file, type_idx,
988 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
989 // The fast path.
990 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800991 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800992 if (!is_type_initialized) {
993 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
994 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
995 } else {
996 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
997 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
998 }
999 } else {
1000 // Use the direct pointer.
1001 if (!is_type_initialized) {
1002 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1003 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1004 } else {
1005 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1006 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1007 }
1008 }
1009 } else {
1010 // The slow path.
1011 DCHECK_EQ(func_offset.Int32Value(), -1);
1012 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1013 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1014 }
1015 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001017 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001018 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 RegLocation rl_result = GetReturn(false);
1021 StoreValue(rl_dest, rl_result);
1022}
1023
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001024void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -07001026 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027}
1028
1029// For final classes there are no sub-classes to check and so we can answer the instance-of
1030// question with simple comparisons.
1031void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1032 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001033 // X86 has its own implementation.
1034 DCHECK_NE(cu_->instruction_set, kX86);
1035
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 RegLocation object = LoadValue(rl_src, kCoreReg);
1037 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001038 int result_reg = rl_result.reg.GetReg();
1039 if (result_reg == object.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 result_reg = AllocTypedTemp(false, kCoreReg);
1041 }
1042 LoadConstant(result_reg, 0); // assume false
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001043 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg.GetReg(), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044
1045 int check_class = AllocTypedTemp(false, kCoreReg);
1046 int object_class = AllocTypedTemp(false, kCoreReg);
1047
1048 LoadCurrMethodDirect(check_class);
1049 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001050 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001052 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001054 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001056 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 int32_t offset_of_type =
1058 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1059 (sizeof(mirror::Class*) * type_idx);
1060 LoadWordDisp(check_class, offset_of_type, check_class);
1061 }
1062
1063 LIR* ne_branchover = NULL;
1064 if (cu_->instruction_set == kThumb2) {
1065 OpRegReg(kOpCmp, check_class, object_class); // Same?
1066 OpIT(kCondEq, ""); // if-convert the test
1067 LoadConstant(result_reg, 1); // .eq case - load true
1068 } else {
1069 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1070 LoadConstant(result_reg, 1); // eq case - load true
1071 }
1072 LIR* target = NewLIR0(kPseudoTargetLabel);
1073 null_branchover->target = target;
1074 if (ne_branchover != NULL) {
1075 ne_branchover->target = target;
1076 }
1077 FreeTemp(object_class);
1078 FreeTemp(check_class);
1079 if (IsTemp(result_reg)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001080 OpRegCopy(rl_result.reg.GetReg(), result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 FreeTemp(result_reg);
1082 }
1083 StoreValue(rl_dest, rl_result);
1084}
1085
1086void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1087 bool type_known_abstract, bool use_declaring_class,
1088 bool can_assume_type_is_in_dex_cache,
1089 uint32_t type_idx, RegLocation rl_dest,
1090 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001091 // X86 has its own implementation.
1092 DCHECK_NE(cu_->instruction_set, kX86);
1093
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 FlushAllRegs();
1095 // May generate a call - use explicit registers
1096 LockCallTemps();
1097 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1098 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1099 if (needs_access_check) {
1100 // Check we have access to type_idx and if not throw IllegalAccessError,
1101 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001102 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 type_idx, true);
1104 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1105 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1106 } else if (use_declaring_class) {
1107 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1108 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001109 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 } else {
1111 // Load dex cache entry into class_reg (kArg2)
1112 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1113 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001114 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 int32_t offset_of_type =
1116 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1117 * type_idx);
1118 LoadWordDisp(class_reg, offset_of_type, class_reg);
1119 if (!can_assume_type_is_in_dex_cache) {
1120 // Need to test presence of type in dex cache at runtime
1121 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1122 // Not resolved
1123 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001124 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001125 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1127 // Rejoin code paths
1128 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1129 hop_branch->target = hop_target;
1130 }
1131 }
1132 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1133 RegLocation rl_result = GetReturn(false);
1134 if (cu_->instruction_set == kMips) {
1135 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001136 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 }
1138 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1139
1140 /* load object->klass_ */
1141 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1142 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1143 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1144 LIR* branchover = NULL;
1145 if (type_known_final) {
1146 // rl_result == ref == null == 0.
1147 if (cu_->instruction_set == kThumb2) {
1148 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1149 OpIT(kCondEq, "E"); // if-convert the test
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001150 LoadConstant(rl_result.reg.GetReg(), 1); // .eq case - load true
1151 LoadConstant(rl_result.reg.GetReg(), 0); // .ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001153 LoadConstant(rl_result.reg.GetReg(), 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001155 LoadConstant(rl_result.reg.GetReg(), 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157 } else {
1158 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001159 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 if (!type_known_abstract) {
1161 /* Uses conditional nullification */
1162 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1163 OpIT(kCondEq, "EE"); // if-convert the test
1164 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1165 }
1166 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1167 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1168 FreeTemp(r_tgt);
1169 } else {
1170 if (!type_known_abstract) {
1171 /* Uses branchovers */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001172 LoadConstant(rl_result.reg.GetReg(), 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1174 }
Mark Mendell6607d972014-02-10 06:54:18 -08001175 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1176 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1177 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1178 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 }
1180 }
1181 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001182 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 /* branch targets here */
1184 LIR* target = NewLIR0(kPseudoTargetLabel);
1185 StoreValue(rl_dest, rl_result);
1186 branch1->target = target;
1187 if (branchover != NULL) {
1188 branchover->target = target;
1189 }
1190}
1191
1192void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1193 bool type_known_final, type_known_abstract, use_declaring_class;
1194 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1195 *cu_->dex_file,
1196 type_idx,
1197 &type_known_final,
1198 &type_known_abstract,
1199 &use_declaring_class);
1200 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1201 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1202
1203 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1204 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1205 } else {
1206 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1207 use_declaring_class, can_assume_type_is_in_dex_cache,
1208 type_idx, rl_dest, rl_src);
1209 }
1210}
1211
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001212void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 bool type_known_final, type_known_abstract, use_declaring_class;
1214 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1215 *cu_->dex_file,
1216 type_idx,
1217 &type_known_final,
1218 &type_known_abstract,
1219 &use_declaring_class);
1220 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1221 // of the exception throw path.
1222 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001223 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 // Verifier type analysis proved this check cast would never cause an exception.
1225 return;
1226 }
1227 FlushAllRegs();
1228 // May generate a call - use explicit registers
1229 LockCallTemps();
1230 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1231 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1232 if (needs_access_check) {
1233 // Check we have access to type_idx and if not throw IllegalAccessError,
1234 // returns Class* in kRet0
1235 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001236 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 type_idx, TargetReg(kArg1), true);
1238 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1239 } else if (use_declaring_class) {
1240 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001241 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001242 } else {
1243 // Load dex cache entry into class_reg (kArg2)
1244 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001245 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 int32_t offset_of_type =
1247 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1248 (sizeof(mirror::Class*) * type_idx);
1249 LoadWordDisp(class_reg, offset_of_type, class_reg);
1250 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1251 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001252 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1253 LIR* cont = NewLIR0(kPseudoTargetLabel);
1254
1255 // Slow path to initialize the type. Executed if the type is NULL.
1256 class SlowPath : public LIRSlowPath {
1257 public:
1258 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1259 const int class_reg) :
1260 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1261 class_reg_(class_reg) {
1262 }
1263
1264 void Compile() {
1265 GenerateTargetLabel();
1266
1267 // Call out to helper, which will return resolved type in kArg0
1268 // InitializeTypeFromCode(idx, method)
1269 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1270 m2l_->TargetReg(kArg1), true);
1271 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1272 m2l_->OpUnconditionalBranch(cont_);
1273 }
1274 public:
1275 const int type_idx_;
1276 const int class_reg_;
1277 };
1278
1279 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1280 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 }
1282 }
1283 // At this point, class_reg (kArg2) has class
1284 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001285
1286 // Slow path for the case where the classes are not equal. In this case we need
1287 // to call a helper function to do the check.
1288 class SlowPath : public LIRSlowPath {
1289 public:
1290 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1291 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1292 }
1293
1294 void Compile() {
1295 GenerateTargetLabel();
1296
1297 if (load_) {
1298 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1299 m2l_->TargetReg(kArg1));
1300 }
1301 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1302 m2l_->TargetReg(kArg1), true);
1303
1304 m2l_->OpUnconditionalBranch(cont_);
1305 }
1306
1307 private:
1308 bool load_;
1309 };
1310
1311 if (type_known_abstract) {
1312 // Easier case, run slow path if target is non-null (slow path will load from target)
1313 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1314 LIR* cont = NewLIR0(kPseudoTargetLabel);
1315 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1316 } else {
1317 // Harder, more common case. We need to generate a forward branch over the load
1318 // if the target is null. If it's non-null we perform the load and branch to the
1319 // slow path if the classes are not equal.
1320
1321 /* Null is OK - continue */
1322 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1323 /* load object->klass_ */
1324 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1325 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1326 TargetReg(kArg1));
1327
1328 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1329 LIR* cont = NewLIR0(kPseudoTargetLabel);
1330
1331 // Add the slow path that will not perform load since this is already done.
1332 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1333
1334 // Set the null check to branch to the continuation.
1335 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 }
1337}
1338
1339void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001340 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 RegLocation rl_result;
1342 if (cu_->instruction_set == kThumb2) {
1343 /*
1344 * NOTE: This is the one place in the code in which we might have
1345 * as many as six live temporary registers. There are 5 in the normal
1346 * set for Arm. Until we have spill capabilities, temporarily add
1347 * lr to the temp set. It is safe to do this locally, but note that
1348 * lr is used explicitly elsewhere in the code generator and cannot
1349 * normally be used as a general temp register.
1350 */
1351 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1352 FreeTemp(TargetReg(kLr)); // and make it available
1353 }
1354 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1355 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1356 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1357 // The longs may overlap - use intermediate temp if so
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001358 if ((rl_result.reg.GetReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001360 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1361 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1362 OpRegCopy(rl_result.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 FreeTemp(t_reg);
1364 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001365 OpRegRegReg(first_op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1366 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(),
1367 rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 }
1369 /*
1370 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1371 * following StoreValueWide might need to allocate a temp register.
1372 * To further work around the lack of a spill capability, explicitly
1373 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1374 * Remove when spill is functional.
1375 */
1376 FreeRegLocTemps(rl_result, rl_src1);
1377 FreeRegLocTemps(rl_result, rl_src2);
1378 StoreValueWide(rl_dest, rl_result);
1379 if (cu_->instruction_set == kThumb2) {
1380 Clobber(TargetReg(kLr));
1381 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1382 }
1383}
1384
1385
1386void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001387 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001388 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389
1390 switch (opcode) {
1391 case Instruction::SHL_LONG:
1392 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001393 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 break;
1395 case Instruction::SHR_LONG:
1396 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001397 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 break;
1399 case Instruction::USHR_LONG:
1400 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001401 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 break;
1403 default:
1404 LOG(FATAL) << "Unexpected case";
1405 }
1406 FlushAllRegs(); /* Send everything to home location */
1407 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1408 RegLocation rl_result = GetReturnWide(false);
1409 StoreValueWide(rl_dest, rl_result);
1410}
1411
1412
1413void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001414 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001415 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 OpKind op = kOpBkpt;
1417 bool is_div_rem = false;
1418 bool check_zero = false;
1419 bool unary = false;
1420 RegLocation rl_result;
1421 bool shift_op = false;
1422 switch (opcode) {
1423 case Instruction::NEG_INT:
1424 op = kOpNeg;
1425 unary = true;
1426 break;
1427 case Instruction::NOT_INT:
1428 op = kOpMvn;
1429 unary = true;
1430 break;
1431 case Instruction::ADD_INT:
1432 case Instruction::ADD_INT_2ADDR:
1433 op = kOpAdd;
1434 break;
1435 case Instruction::SUB_INT:
1436 case Instruction::SUB_INT_2ADDR:
1437 op = kOpSub;
1438 break;
1439 case Instruction::MUL_INT:
1440 case Instruction::MUL_INT_2ADDR:
1441 op = kOpMul;
1442 break;
1443 case Instruction::DIV_INT:
1444 case Instruction::DIV_INT_2ADDR:
1445 check_zero = true;
1446 op = kOpDiv;
1447 is_div_rem = true;
1448 break;
1449 /* NOTE: returns in kArg1 */
1450 case Instruction::REM_INT:
1451 case Instruction::REM_INT_2ADDR:
1452 check_zero = true;
1453 op = kOpRem;
1454 is_div_rem = true;
1455 break;
1456 case Instruction::AND_INT:
1457 case Instruction::AND_INT_2ADDR:
1458 op = kOpAnd;
1459 break;
1460 case Instruction::OR_INT:
1461 case Instruction::OR_INT_2ADDR:
1462 op = kOpOr;
1463 break;
1464 case Instruction::XOR_INT:
1465 case Instruction::XOR_INT_2ADDR:
1466 op = kOpXor;
1467 break;
1468 case Instruction::SHL_INT:
1469 case Instruction::SHL_INT_2ADDR:
1470 shift_op = true;
1471 op = kOpLsl;
1472 break;
1473 case Instruction::SHR_INT:
1474 case Instruction::SHR_INT_2ADDR:
1475 shift_op = true;
1476 op = kOpAsr;
1477 break;
1478 case Instruction::USHR_INT:
1479 case Instruction::USHR_INT_2ADDR:
1480 shift_op = true;
1481 op = kOpLsr;
1482 break;
1483 default:
1484 LOG(FATAL) << "Invalid word arith op: " << opcode;
1485 }
1486 if (!is_div_rem) {
1487 if (unary) {
1488 rl_src1 = LoadValue(rl_src1, kCoreReg);
1489 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001490 OpRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 } else {
1492 if (shift_op) {
1493 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001494 rl_src2 = LoadValue(rl_src2, kCoreReg);
1495 t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001496 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 rl_src1 = LoadValue(rl_src1, kCoreReg);
1498 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001499 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 FreeTemp(t_reg);
1501 } else {
1502 rl_src1 = LoadValue(rl_src1, kCoreReg);
1503 rl_src2 = LoadValue(rl_src2, kCoreReg);
1504 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001505 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 }
1507 }
1508 StoreValue(rl_dest, rl_result);
1509 } else {
Dave Allison70202782013-10-22 17:52:19 -07001510 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 -07001511 if (cu_->instruction_set == kMips) {
1512 rl_src1 = LoadValue(rl_src1, kCoreReg);
1513 rl_src2 = LoadValue(rl_src2, kCoreReg);
1514 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001515 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001517 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001518 done = true;
1519 } else if (cu_->instruction_set == kThumb2) {
1520 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1521 // Use ARM SDIV instruction for division. For remainder we also need to
1522 // calculate using a MUL and subtract.
1523 rl_src1 = LoadValue(rl_src1, kCoreReg);
1524 rl_src2 = LoadValue(rl_src2, kCoreReg);
1525 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001526 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001527 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001528 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001529 done = true;
1530 }
1531 }
1532
1533 // If we haven't already generated the code use the callout function.
1534 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001535 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 FlushAllRegs(); /* Send everything to home location */
1537 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1538 int r_tgt = CallHelperSetup(func_offset);
1539 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1540 if (check_zero) {
1541 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1542 }
Dave Allison70202782013-10-22 17:52:19 -07001543 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001544 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001545 if (op == kOpDiv)
1546 rl_result = GetReturn(false);
1547 else
1548 rl_result = GetReturnAlt();
1549 }
1550 StoreValue(rl_dest, rl_result);
1551 }
1552}
1553
1554/*
1555 * The following are the first-level codegen routines that analyze the format
1556 * of each bytecode then either dispatch special purpose codegen routines
1557 * or produce corresponding Thumb instructions directly.
1558 */
1559
Brian Carlstrom7940e442013-07-12 13:46:57 -07001560// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001561static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 x &= x - 1;
1563 return (x & (x - 1)) == 0;
1564}
1565
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1567// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001568bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001569 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1571 return false;
1572 }
1573 // No divide instruction for Arm, so check for more special cases
1574 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001575 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001576 }
1577 int k = LowestSetBit(lit);
1578 if (k >= 30) {
1579 // Avoid special cases.
1580 return false;
1581 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 rl_src = LoadValue(rl_src, kCoreReg);
1583 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001584 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001585 int t_reg = AllocTemp();
1586 if (lit == 2) {
1587 // Division by 2 is by far the most common division by constant.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001588 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), 32 - k);
1589 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1590 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001591 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001592 OpRegRegImm(kOpAsr, t_reg, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001594 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1595 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 }
1597 } else {
1598 int t_reg1 = AllocTemp();
1599 int t_reg2 = AllocTemp();
1600 if (lit == 2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001601 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg.GetReg(), 32 - k);
1602 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001604 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001606 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001608 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001610 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
1612 }
1613 StoreValue(rl_dest, rl_result);
1614 return true;
1615}
1616
1617// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1618// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001619bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 // Can we simplify this multiplication?
1621 bool power_of_two = false;
1622 bool pop_count_le2 = false;
1623 bool power_of_two_minus_one = false;
1624 if (lit < 2) {
1625 // Avoid special cases.
1626 return false;
1627 } else if (IsPowerOfTwo(lit)) {
1628 power_of_two = true;
1629 } else if (IsPopCountLE2(lit)) {
1630 pop_count_le2 = true;
1631 } else if (IsPowerOfTwo(lit + 1)) {
1632 power_of_two_minus_one = true;
1633 } else {
1634 return false;
1635 }
1636 rl_src = LoadValue(rl_src, kCoreReg);
1637 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1638 if (power_of_two) {
1639 // Shift.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001640 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001641 } else if (pop_count_le2) {
1642 // Shift and add and shift.
1643 int first_bit = LowestSetBit(lit);
1644 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1645 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1646 } else {
1647 // Reverse subtract: (src << (shift + 1)) - src.
1648 DCHECK(power_of_two_minus_one);
1649 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1650 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001651 OpRegRegImm(kOpLsl, t_reg, rl_src.reg.GetReg(), LowestSetBit(lit + 1));
1652 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001653 }
1654 StoreValue(rl_dest, rl_result);
1655 return true;
1656}
1657
1658void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001659 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001660 RegLocation rl_result;
1661 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1662 int shift_op = false;
1663 bool is_div = false;
1664
1665 switch (opcode) {
1666 case Instruction::RSUB_INT_LIT8:
1667 case Instruction::RSUB_INT: {
1668 rl_src = LoadValue(rl_src, kCoreReg);
1669 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1670 if (cu_->instruction_set == kThumb2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001671 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001673 OpRegReg(kOpNeg, rl_result.reg.GetReg(), rl_src.reg.GetReg());
1674 OpRegImm(kOpAdd, rl_result.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 }
1676 StoreValue(rl_dest, rl_result);
1677 return;
1678 }
1679
1680 case Instruction::SUB_INT:
1681 case Instruction::SUB_INT_2ADDR:
1682 lit = -lit;
1683 // Intended fallthrough
1684 case Instruction::ADD_INT:
1685 case Instruction::ADD_INT_2ADDR:
1686 case Instruction::ADD_INT_LIT8:
1687 case Instruction::ADD_INT_LIT16:
1688 op = kOpAdd;
1689 break;
1690 case Instruction::MUL_INT:
1691 case Instruction::MUL_INT_2ADDR:
1692 case Instruction::MUL_INT_LIT8:
1693 case Instruction::MUL_INT_LIT16: {
1694 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1695 return;
1696 }
1697 op = kOpMul;
1698 break;
1699 }
1700 case Instruction::AND_INT:
1701 case Instruction::AND_INT_2ADDR:
1702 case Instruction::AND_INT_LIT8:
1703 case Instruction::AND_INT_LIT16:
1704 op = kOpAnd;
1705 break;
1706 case Instruction::OR_INT:
1707 case Instruction::OR_INT_2ADDR:
1708 case Instruction::OR_INT_LIT8:
1709 case Instruction::OR_INT_LIT16:
1710 op = kOpOr;
1711 break;
1712 case Instruction::XOR_INT:
1713 case Instruction::XOR_INT_2ADDR:
1714 case Instruction::XOR_INT_LIT8:
1715 case Instruction::XOR_INT_LIT16:
1716 op = kOpXor;
1717 break;
1718 case Instruction::SHL_INT_LIT8:
1719 case Instruction::SHL_INT:
1720 case Instruction::SHL_INT_2ADDR:
1721 lit &= 31;
1722 shift_op = true;
1723 op = kOpLsl;
1724 break;
1725 case Instruction::SHR_INT_LIT8:
1726 case Instruction::SHR_INT:
1727 case Instruction::SHR_INT_2ADDR:
1728 lit &= 31;
1729 shift_op = true;
1730 op = kOpAsr;
1731 break;
1732 case Instruction::USHR_INT_LIT8:
1733 case Instruction::USHR_INT:
1734 case Instruction::USHR_INT_2ADDR:
1735 lit &= 31;
1736 shift_op = true;
1737 op = kOpLsr;
1738 break;
1739
1740 case Instruction::DIV_INT:
1741 case Instruction::DIV_INT_2ADDR:
1742 case Instruction::DIV_INT_LIT8:
1743 case Instruction::DIV_INT_LIT16:
1744 case Instruction::REM_INT:
1745 case Instruction::REM_INT_2ADDR:
1746 case Instruction::REM_INT_LIT8:
1747 case Instruction::REM_INT_LIT16: {
1748 if (lit == 0) {
1749 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1750 return;
1751 }
buzbee11b63d12013-08-27 07:34:17 -07001752 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001753 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001754 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 (opcode == Instruction::DIV_INT_LIT16)) {
1756 is_div = true;
1757 } else {
1758 is_div = false;
1759 }
buzbee11b63d12013-08-27 07:34:17 -07001760 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1761 return;
1762 }
Dave Allison70202782013-10-22 17:52:19 -07001763
1764 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 if (cu_->instruction_set == kMips) {
1766 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001767 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001768 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001769 } else if (cu_->instruction_set == kX86) {
1770 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1771 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001772 } else if (cu_->instruction_set == kThumb2) {
1773 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1774 // Use ARM SDIV instruction for division. For remainder we also need to
1775 // calculate using a MUL and subtract.
1776 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001777 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001778 done = true;
1779 }
1780 }
1781
1782 if (!done) {
1783 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001784 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1785 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001786 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001787 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1788 if (is_div)
1789 rl_result = GetReturn(false);
1790 else
1791 rl_result = GetReturnAlt();
1792 }
1793 StoreValue(rl_dest, rl_result);
1794 return;
1795 }
1796 default:
1797 LOG(FATAL) << "Unexpected opcode " << opcode;
1798 }
1799 rl_src = LoadValue(rl_src, kCoreReg);
1800 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001801 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 if (shift_op && (lit == 0)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001803 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001804 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001805 OpRegRegImm(op, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001806 }
1807 StoreValue(rl_dest, rl_result);
1808}
1809
1810void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001811 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 RegLocation rl_result;
1813 OpKind first_op = kOpBkpt;
1814 OpKind second_op = kOpBkpt;
1815 bool call_out = false;
1816 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001817 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 int ret_reg = TargetReg(kRet0);
1819
1820 switch (opcode) {
1821 case Instruction::NOT_LONG:
1822 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1823 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1824 // Check for destructive overlap
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001825 if (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001827 OpRegCopy(t_reg, rl_src2.reg.GetHighReg());
1828 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1829 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001830 FreeTemp(t_reg);
1831 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001832 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1833 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 }
1835 StoreValueWide(rl_dest, rl_result);
1836 return;
1837 case Instruction::ADD_LONG:
1838 case Instruction::ADD_LONG_2ADDR:
1839 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001840 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001841 return;
1842 }
1843 first_op = kOpAdd;
1844 second_op = kOpAdc;
1845 break;
1846 case Instruction::SUB_LONG:
1847 case Instruction::SUB_LONG_2ADDR:
1848 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001849 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 return;
1851 }
1852 first_op = kOpSub;
1853 second_op = kOpSbc;
1854 break;
1855 case Instruction::MUL_LONG:
1856 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001857 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001858 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001859 return;
1860 } else {
1861 call_out = true;
1862 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001863 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001864 }
1865 break;
1866 case Instruction::DIV_LONG:
1867 case Instruction::DIV_LONG_2ADDR:
1868 call_out = true;
1869 check_zero = true;
1870 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001871 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 break;
1873 case Instruction::REM_LONG:
1874 case Instruction::REM_LONG_2ADDR:
1875 call_out = true;
1876 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001877 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001878 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1879 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1880 break;
1881 case Instruction::AND_LONG_2ADDR:
1882 case Instruction::AND_LONG:
1883 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001884 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001885 }
1886 first_op = kOpAnd;
1887 second_op = kOpAnd;
1888 break;
1889 case Instruction::OR_LONG:
1890 case Instruction::OR_LONG_2ADDR:
1891 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001892 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 return;
1894 }
1895 first_op = kOpOr;
1896 second_op = kOpOr;
1897 break;
1898 case Instruction::XOR_LONG:
1899 case Instruction::XOR_LONG_2ADDR:
1900 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001901 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001902 return;
1903 }
1904 first_op = kOpXor;
1905 second_op = kOpXor;
1906 break;
1907 case Instruction::NEG_LONG: {
1908 GenNegLong(rl_dest, rl_src2);
1909 return;
1910 }
1911 default:
1912 LOG(FATAL) << "Invalid long arith op";
1913 }
1914 if (!call_out) {
1915 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1916 } else {
1917 FlushAllRegs(); /* Send everything to home location */
1918 if (check_zero) {
1919 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1920 int r_tgt = CallHelperSetup(func_offset);
1921 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1922 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1923 // NOTE: callout here is not a safepoint
1924 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1925 } else {
1926 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1927 }
1928 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1929 if (ret_reg == TargetReg(kRet0))
1930 rl_result = GetReturnWide(false);
1931 else
1932 rl_result = GetReturnWideAlt();
1933 StoreValueWide(rl_dest, rl_result);
1934 }
1935}
1936
Ian Rogers848871b2013-08-05 10:56:33 -07001937void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001938 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001939 /*
1940 * Don't optimize the register usage since it calls out to support
1941 * functions
1942 */
1943 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001944 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1945 if (rl_dest.wide) {
1946 RegLocation rl_result;
1947 rl_result = GetReturnWide(rl_dest.fp);
1948 StoreValueWide(rl_dest, rl_result);
1949 } else {
1950 RegLocation rl_result;
1951 rl_result = GetReturn(rl_dest.fp);
1952 StoreValue(rl_dest, rl_result);
1953 }
1954}
1955
1956/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001957void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001958 if (Runtime::Current()->ExplicitSuspendChecks()) {
1959 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1960 return;
1961 }
1962 FlushAllRegs();
1963 LIR* branch = OpTestSuspend(NULL);
1964 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
1965 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1966 current_dalvik_offset_);
1967 branch->target = target;
1968 suspend_launchpads_.Insert(target);
1969 } else {
1970 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1971 return;
1972 }
1973 FlushAllRegs(); // TODO: needed?
1974 LIR* inst = CheckSuspendUsingLoad();
1975 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001976 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001977}
1978
1979/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001980void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08001981 if (Runtime::Current()->ExplicitSuspendChecks()) {
1982 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1983 OpUnconditionalBranch(target);
1984 return;
1985 }
1986 OpTestSuspend(target);
1987 LIR* launch_pad =
1988 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1989 current_dalvik_offset_);
1990 FlushAllRegs();
1991 OpUnconditionalBranch(launch_pad);
1992 suspend_launchpads_.Insert(launch_pad);
1993 } else {
1994 // For the implicit suspend check, just perform the trigger
1995 // load and branch to the target.
1996 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1997 OpUnconditionalBranch(target);
1998 return;
1999 }
2000 FlushAllRegs();
2001 LIR* inst = CheckSuspendUsingLoad();
2002 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002003 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002004 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002005}
2006
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002007/* Call out to helper assembly routine that will null check obj and then lock it. */
2008void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2009 FlushAllRegs();
2010 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
2011}
2012
2013/* Call out to helper assembly routine that will null check obj and then unlock it. */
2014void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2015 FlushAllRegs();
2016 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2017}
2018
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002019/* Generic code for generating a wide constant into a VR. */
2020void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2021 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002022 LoadConstantWide(rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002023 StoreValueWide(rl_dest, rl_result);
2024}
2025
Brian Carlstrom7940e442013-07-12 13:46:57 -07002026} // namespace art