blob: 58db98479378abecaee61e084dd5f4d707f4e474 [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"
19#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070020#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mirror/array.h"
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080022#include "mirror/object-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "verifier/method_verifier.h"
Dave Allisonbcec6fb2014-01-17 12:52:22 -080024#include <functional>
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets. Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
buzbeeb48819d2013-09-14 16:15:25 -070035 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * blocks.
37 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070038void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 LIR* barrier = NewLIR0(kPseudoBarrier);
40 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070041 DCHECK(!barrier->flags.use_def_invalid);
42 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043}
44
buzbee0d829482013-10-11 15:24:55 -070045// TODO: need to do some work to split out targets with
Brian Carlstrom7940e442013-07-12 13:46:57 -070046// condition codes and those without
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070047LIR* Mir2Lir::GenCheck(ConditionCode c_code, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 DCHECK_NE(cu_->instruction_set, kMips);
49 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_);
50 LIR* branch = OpCondBranch(c_code, tgt);
51 // Remember branch target - will process later
52 throw_launchpads_.Insert(tgt);
53 return branch;
54}
55
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070056LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, int reg, int imm_val, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg, imm_val);
58 LIR* branch;
59 if (c_code == kCondAl) {
60 branch = OpUnconditionalBranch(tgt);
61 } else {
62 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
63 }
64 // Remember branch target - will process later
65 throw_launchpads_.Insert(tgt);
66 return branch;
67}
68
Dave Allisonb373e092014-02-20 16:06:36 -080069
Brian Carlstrom7940e442013-07-12 13:46:57 -070070/* Perform null-check on a register. */
Dave Allisonb373e092014-02-20 16:06:36 -080071LIR* Mir2Lir::GenNullCheck(int m_reg, int opt_flags) {
72 if (Runtime::Current()->ExplicitNullChecks()) {
73 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
74 return NULL;
75 }
76 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 }
Dave Allisonb373e092014-02-20 16:06:36 -080078 return nullptr;
79}
80
81void Mir2Lir::MarkPossibleNullPointerException(int opt_flags) {
82 if (!Runtime::Current()->ExplicitNullChecks()) {
83 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
84 return;
85 }
86 MarkSafepointPC(last_lir_insn_);
87 }
88}
89
90void Mir2Lir::MarkPossibleStackOverflowException() {
91 if (!Runtime::Current()->ExplicitStackOverflowChecks()) {
92 MarkSafepointPC(last_lir_insn_);
93 }
94}
95
96void Mir2Lir::ForceImplicitNullCheck(int reg, int opt_flags) {
97 if (!Runtime::Current()->ExplicitNullChecks()) {
98 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
99 return;
100 }
101 // Force an implicit null check by performing a memory operation (load) from the given
102 // register with offset 0. This will cause a signal if the register contains 0 (null).
103 int tmp = AllocTemp();
104 LIR* load = LoadWordDisp(reg, 0, tmp);
105 FreeTemp(tmp);
106 MarkSafepointPC(load);
107 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108}
109
110/* Perform check on two registers */
111LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700112 ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
114 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
115 // Remember branch target - will process later
116 throw_launchpads_.Insert(tgt);
117 return branch;
118}
119
120void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
121 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700122 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 ConditionCode cond;
124 switch (opcode) {
125 case Instruction::IF_EQ:
126 cond = kCondEq;
127 break;
128 case Instruction::IF_NE:
129 cond = kCondNe;
130 break;
131 case Instruction::IF_LT:
132 cond = kCondLt;
133 break;
134 case Instruction::IF_GE:
135 cond = kCondGe;
136 break;
137 case Instruction::IF_GT:
138 cond = kCondGt;
139 break;
140 case Instruction::IF_LE:
141 cond = kCondLe;
142 break;
143 default:
144 cond = static_cast<ConditionCode>(0);
145 LOG(FATAL) << "Unexpected opcode " << opcode;
146 }
147
148 // Normalize such that if either operand is constant, src2 will be constant
149 if (rl_src1.is_const) {
150 RegLocation rl_temp = rl_src1;
151 rl_src1 = rl_src2;
152 rl_src2 = rl_temp;
153 cond = FlipComparisonOrder(cond);
154 }
155
156 rl_src1 = LoadValue(rl_src1, kCoreReg);
157 // Is this really an immediate comparison?
158 if (rl_src2.is_const) {
159 // If it's already live in a register or not easily materialized, just keep going
160 RegLocation rl_temp = UpdateLoc(rl_src2);
161 if ((rl_temp.location == kLocDalvikFrame) &&
162 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
163 // OK - convert this to a compare immediate and branch
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000164 OpCmpImmBranch(cond, rl_src1.reg.GetReg(), mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 return;
166 }
167 }
168 rl_src2 = LoadValue(rl_src2, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000169 OpCmpBranch(cond, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
172void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 ConditionCode cond;
175 rl_src = LoadValue(rl_src, kCoreReg);
176 switch (opcode) {
177 case Instruction::IF_EQZ:
178 cond = kCondEq;
179 break;
180 case Instruction::IF_NEZ:
181 cond = kCondNe;
182 break;
183 case Instruction::IF_LTZ:
184 cond = kCondLt;
185 break;
186 case Instruction::IF_GEZ:
187 cond = kCondGe;
188 break;
189 case Instruction::IF_GTZ:
190 cond = kCondGt;
191 break;
192 case Instruction::IF_LEZ:
193 cond = kCondLe;
194 break;
195 default:
196 cond = static_cast<ConditionCode>(0);
197 LOG(FATAL) << "Unexpected opcode " << opcode;
198 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000199 OpCmpImmBranch(cond, rl_src.reg.GetReg(), 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200}
201
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700202void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
204 if (rl_src.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000205 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000207 LoadValueDirect(rl_src, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000209 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_result.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 StoreValueWide(rl_dest, rl_result);
211}
212
213void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700214 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700215 rl_src = LoadValue(rl_src, kCoreReg);
216 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
217 OpKind op = kOpInvalid;
218 switch (opcode) {
219 case Instruction::INT_TO_BYTE:
220 op = kOp2Byte;
221 break;
222 case Instruction::INT_TO_SHORT:
223 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700225 case Instruction::INT_TO_CHAR:
226 op = kOp2Char;
227 break;
228 default:
229 LOG(ERROR) << "Bad int conversion type";
230 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000231 OpRegReg(op, rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700232 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233}
234
235/*
236 * Let helper function take care of everything. Will call
237 * Array::AllocFromCode(type_idx, method, count);
238 * Note: AllocFromCode will handle checks for errNegativeArraySize.
239 */
240void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700241 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700243 ThreadOffset func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800244 const DexFile* dex_file = cu_->dex_file;
245 CompilerDriver* driver = cu_->compiler_driver;
246 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800248 bool is_type_initialized; // Ignored as an array does not have an initializer.
249 bool use_direct_type_ptr;
250 uintptr_t direct_type_ptr;
251 if (kEmbedClassInCode &&
252 driver->CanEmbedTypeInCode(*dex_file, type_idx,
253 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
254 // The fast path.
255 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800256 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800257 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
258 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
259 } else {
260 // Use the direct pointer.
261 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
262 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
263 }
264 } else {
265 // The slow path.
266 DCHECK_EQ(func_offset.Int32Value(), -1);
267 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
268 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
269 }
270 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700272 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800273 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 RegLocation rl_result = GetReturn(false);
276 StoreValue(rl_dest, rl_result);
277}
278
279/*
280 * Similar to GenNewArray, but with post-allocation initialization.
281 * Verifier guarantees we're dealing with an array class. Current
282 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
283 * Current code also throws internal unimp if not 'L', '[' or 'I'.
284 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 int elems = info->num_arg_words;
287 int type_idx = info->index;
288 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700289 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
291 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700292 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700294 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 }
296 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
297 FreeTemp(TargetReg(kArg2));
298 FreeTemp(TargetReg(kArg1));
299 /*
300 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
301 * return region. Because AllocFromCode placed the new array
302 * in kRet0, we'll just lock it into place. When debugger support is
303 * added, it may be necessary to additionally copy all return
304 * values to a home location in thread-local storage
305 */
306 LockTemp(TargetReg(kRet0));
307
308 // TODO: use the correct component size, currently all supported types
309 // share array alignment with ints (see comment at head of function)
310 size_t component_size = sizeof(int32_t);
311
312 // Having a range of 0 is legal
313 if (info->is_range && (elems > 0)) {
314 /*
315 * Bit of ugliness here. We're going generate a mem copy loop
316 * on the register range, but it is possible that some regs
317 * in the range have been promoted. This is unlikely, but
318 * before generating the copy, we'll just force a flush
319 * of any regs in the source range that have been promoted to
320 * home location.
321 */
322 for (int i = 0; i < elems; i++) {
323 RegLocation loc = UpdateLoc(info->args[i]);
324 if (loc.location == kLocPhysReg) {
325 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000326 loc.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 }
328 }
329 /*
330 * TUNING note: generated code here could be much improved, but
331 * this is an uncommon operation and isn't especially performance
332 * critical.
333 */
334 int r_src = AllocTemp();
335 int r_dst = AllocTemp();
336 int r_idx = AllocTemp();
337 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700338 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 case kThumb2:
340 r_val = TargetReg(kLr);
341 break;
342 case kX86:
343 FreeTemp(TargetReg(kRet0));
344 r_val = AllocTemp();
345 break;
346 case kMips:
347 r_val = AllocTemp();
348 break;
349 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
350 }
351 // Set up source pointer
352 RegLocation rl_first = info->args[0];
353 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
354 // Set up the target pointer
355 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
356 mirror::Array::DataOffset(component_size).Int32Value());
357 // Set up the loop counter (known to be > 0)
358 LoadConstant(r_idx, elems - 1);
359 // Generate the copy loop. Going backwards for convenience
360 LIR* target = NewLIR0(kPseudoTargetLabel);
361 // Copy next element
362 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
363 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
364 FreeTemp(r_val);
365 OpDecAndBranch(kCondGe, r_idx, target);
366 if (cu_->instruction_set == kX86) {
367 // Restore the target pointer
368 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
369 -mirror::Array::DataOffset(component_size).Int32Value());
370 }
371 } else if (!info->is_range) {
372 // TUNING: interleave
373 for (int i = 0; i < elems; i++) {
374 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
375 StoreBaseDisp(TargetReg(kRet0),
376 mirror::Array::DataOffset(component_size).Int32Value() +
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000377 i * 4, rl_arg.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 // If the LoadValue caused a temp to be allocated, free it
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000379 if (IsTemp(rl_arg.reg.GetReg())) {
380 FreeTemp(rl_arg.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
382 }
383 }
384 if (info->result.location != kLocInvalid) {
385 StoreValue(info->result, GetReturn(false /* not fp */));
386 }
387}
388
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800389//
390// Slow path to ensure a class is initialized for sget/sput.
391//
392class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
393 public:
394 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
395 int storage_index, int r_base) :
396 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
397 r_base_(r_base) {
398 }
399
400 void Compile() {
401 LIR* unresolved_target = GenerateTargetLabel();
402 uninit_->target = unresolved_target;
403 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
404 storage_index_, true);
405 // Copy helper's result into r_base, a no-op on all but MIPS.
406 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
407
408 m2l_->OpUnconditionalBranch(cont_);
409 }
410
411 private:
412 LIR* const uninit_;
413 const int storage_index_;
414 const int r_base_;
415};
416
Vladimir Markobe0e5462014-02-26 11:24:15 +0000417void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700418 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000419 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
420 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
421 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
422 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800423 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000424 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 // Fast path, static storage base is this method's class
426 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800427 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000428 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800429 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000430 if (IsTemp(rl_method.reg.GetReg())) {
431 FreeTemp(rl_method.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
433 } else {
434 // Medium path, static storage base in a different class which requires checks that the other
435 // class is initialized.
436 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000437 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 // May do runtime call so everything to home locations.
439 FlushAllRegs();
440 // Using fixed register to sync with possible call to runtime support.
441 int r_method = TargetReg(kArg1);
442 LockTemp(r_method);
443 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800444 r_base = TargetReg(kArg0);
445 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800447 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
448 r_base);
449 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000450 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800451 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000452 if (!field_info.IsInitialized() &&
453 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800454 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800455
456 // The slow path is invoked if the r_base is NULL or the class pointed
457 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800458 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
459 int r_tmp = TargetReg(kArg2);
460 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800461 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800462 mirror::Class::StatusOffset().Int32Value(),
463 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800464 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800465
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800466 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
467 unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000468 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800469
470 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 FreeTemp(r_method);
473 }
474 // rBase now holds static storage base
475 if (is_long_or_double) {
476 rl_src = LoadValueWide(rl_src, kAnyReg);
477 } else {
478 rl_src = LoadValue(rl_src, kAnyReg);
479 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000480 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 GenMemBarrier(kStoreStore);
482 }
483 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000484 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg(),
485 rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000487 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000489 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 GenMemBarrier(kStoreLoad);
491 }
492 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000493 MarkGCCard(rl_src.reg.GetReg(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800495 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 } else {
497 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700498 ThreadOffset setter_offset =
499 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
500 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
501 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000502 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 }
504}
505
Vladimir Markobe0e5462014-02-26 11:24:15 +0000506void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700507 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000508 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
509 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
510 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
511 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800512 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000513 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 // Fast path, static storage base is this method's class
515 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800516 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000517 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800518 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 } else {
520 // Medium path, static storage base in a different class which requires checks that the other
521 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000522 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 // May do runtime call so everything to home locations.
524 FlushAllRegs();
525 // Using fixed register to sync with possible call to runtime support.
526 int r_method = TargetReg(kArg1);
527 LockTemp(r_method);
528 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800529 r_base = TargetReg(kArg0);
530 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800532 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
533 r_base);
534 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000535 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800536 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000537 if (!field_info.IsInitialized() &&
538 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) == 0) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800539 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800540
541 // The slow path is invoked if the r_base is NULL or the class pointed
542 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800543 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
544 int r_tmp = TargetReg(kArg2);
545 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800546 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800547 mirror::Class::StatusOffset().Int32Value(),
548 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800549 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800550
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800551 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
552 unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000553 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800554
555 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 FreeTemp(r_method);
558 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800559 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000561 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 GenMemBarrier(kLoadLoad);
563 }
564 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000565 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg(),
566 rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000568 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800570 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 if (is_long_or_double) {
572 StoreValueWide(rl_dest, rl_result);
573 } else {
574 StoreValue(rl_dest, rl_result);
575 }
576 } else {
577 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700578 ThreadOffset getterOffset =
579 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
580 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
581 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000582 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 if (is_long_or_double) {
584 RegLocation rl_result = GetReturnWide(rl_dest.fp);
585 StoreValueWide(rl_dest, rl_result);
586 } else {
587 RegLocation rl_result = GetReturn(rl_dest.fp);
588 StoreValue(rl_dest, rl_result);
589 }
590 }
591}
592
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800593// Generate code for all slow paths.
594void Mir2Lir::HandleSlowPaths() {
595 int n = slow_paths_.Size();
596 for (int i = 0; i < n; ++i) {
597 LIRSlowPath* slowpath = slow_paths_.Get(i);
598 slowpath->Compile();
599 }
600 slow_paths_.Reset();
601}
602
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700603void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700605 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 for (int i = 0; i < num_elems; i++) {
607 ResetRegPool();
608 ResetDefTracking();
609 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700610 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 current_dalvik_offset_ = lab->operands[1];
612 AppendLIR(lab);
613 int r_tgt = CallHelperSetup(helper_offset);
614 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
615 OpUnconditionalBranch(resume_lab);
616 }
617}
618
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700619void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 int num_elems = throw_launchpads_.Size();
621 for (int i = 0; i < num_elems; i++) {
622 ResetRegPool();
623 ResetDefTracking();
624 LIR* lab = throw_launchpads_.Get(i);
625 current_dalvik_offset_ = lab->operands[1];
626 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700627 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 int v1 = lab->operands[2];
629 int v2 = lab->operands[3];
630 bool target_x86 = (cu_->instruction_set == kX86);
631 switch (lab->operands[0]) {
632 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700633 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700635 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
637 if (target_x86) {
638 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
639 } else {
640 OpRegCopy(TargetReg(kArg1), v1);
641 }
642 // Make sure the following LoadConstant doesn't mess with kArg1.
643 LockTemp(TargetReg(kArg1));
644 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700645 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 break;
647 case kThrowArrayBounds:
648 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
649 if (v2 != TargetReg(kArg0)) {
650 OpRegCopy(TargetReg(kArg0), v1);
651 if (target_x86) {
652 // x86 leaves the array pointer in v2, so load the array length that the handler expects
653 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
654 } else {
655 OpRegCopy(TargetReg(kArg1), v2);
656 }
657 } else {
658 if (v1 == TargetReg(kArg1)) {
659 // Swap v1 and v2, using kArg2 as a temp
660 OpRegCopy(TargetReg(kArg2), v1);
661 if (target_x86) {
662 // x86 leaves the array pointer in v2; load the array length that the handler expects
663 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
664 } else {
665 OpRegCopy(TargetReg(kArg1), v2);
666 }
667 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
668 } else {
669 if (target_x86) {
670 // x86 leaves the array pointer in v2; load the array length that the handler expects
671 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
672 } else {
673 OpRegCopy(TargetReg(kArg1), v2);
674 }
675 OpRegCopy(TargetReg(kArg0), v1);
676 }
677 }
Ian Rogers848871b2013-08-05 10:56:33 -0700678 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 break;
680 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700681 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 break;
683 case kThrowNoSuchMethod:
684 OpRegCopy(TargetReg(kArg0), v1);
685 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700686 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 break;
688 case kThrowStackOverflow:
Ian Rogers848871b2013-08-05 10:56:33 -0700689 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 // Restore stack alignment
691 if (target_x86) {
692 OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
693 } else {
694 OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
695 }
696 break;
697 default:
698 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
699 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000700 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 int r_tgt = CallHelperSetup(func_offset);
702 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
703 }
704}
705
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700708 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
710 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
711 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 RegLocation rl_result;
713 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000714 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 rl_obj = LoadValue(rl_obj, kCoreReg);
716 if (is_long_or_double) {
717 DCHECK(rl_dest.wide);
Dave Allisonb373e092014-02-20 16:06:36 -0800718 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 if (cu_->instruction_set == kX86) {
720 rl_result = EvalLoc(rl_dest, reg_class, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800721 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000722 LoadBaseDispWide(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
Dave Allisonb373e092014-02-20 16:06:36 -0800723 rl_result.reg.GetReg(),
724 rl_result.reg.GetHighReg(), rl_obj.s_reg_low);
725 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000726 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 GenMemBarrier(kLoadLoad);
728 }
729 } else {
730 int reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000731 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000733 LoadBaseDispWide(reg_ptr, 0, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(),
734 INVALID_SREG);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000735 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 GenMemBarrier(kLoadLoad);
737 }
738 FreeTemp(reg_ptr);
739 }
740 StoreValueWide(rl_dest, rl_result);
741 } else {
742 rl_result = EvalLoc(rl_dest, reg_class, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800743 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000744 LoadBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
745 rl_result.reg.GetReg(), kWord, rl_obj.s_reg_low);
Dave Allisonb373e092014-02-20 16:06:36 -0800746 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000747 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 GenMemBarrier(kLoadLoad);
749 }
750 StoreValue(rl_dest, rl_result);
751 }
752 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700753 ThreadOffset getterOffset =
754 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
755 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
756 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000757 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 if (is_long_or_double) {
759 RegLocation rl_result = GetReturnWide(rl_dest.fp);
760 StoreValueWide(rl_dest, rl_result);
761 } else {
762 RegLocation rl_result = GetReturn(rl_dest.fp);
763 StoreValue(rl_dest, rl_result);
764 }
765 }
766}
767
Vladimir Markobe0e5462014-02-26 11:24:15 +0000768void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700769 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700770 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000771 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
772 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
773 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000775 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 rl_obj = LoadValue(rl_obj, kCoreReg);
777 if (is_long_or_double) {
778 int reg_ptr;
779 rl_src = LoadValueWide(rl_src, kAnyReg);
Dave Allisonb373e092014-02-20 16:06:36 -0800780 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000782 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000783 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 GenMemBarrier(kStoreStore);
785 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000786 StoreBaseDispWide(reg_ptr, 0, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Dave Allisonb373e092014-02-20 16:06:36 -0800787 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000788 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 GenMemBarrier(kLoadLoad);
790 }
791 FreeTemp(reg_ptr);
792 } else {
793 rl_src = LoadValue(rl_src, reg_class);
Dave Allisonb373e092014-02-20 16:06:36 -0800794 GenNullCheck(rl_obj.reg.GetReg(), opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000795 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 GenMemBarrier(kStoreStore);
797 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000798 StoreBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
Dave Allisonb373e092014-02-20 16:06:36 -0800799 rl_src.reg.GetReg(), kWord);
800 MarkPossibleNullPointerException(opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000801 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 GenMemBarrier(kLoadLoad);
803 }
804 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000805 MarkGCCard(rl_src.reg.GetReg(), rl_obj.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 }
807 }
808 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700809 ThreadOffset setter_offset =
810 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
811 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
812 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000813 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
814 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 }
816}
817
Ian Rogersa9a82542013-10-04 11:17:26 -0700818void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
819 RegLocation rl_src) {
820 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
821 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
822 (opt_flags & MIR_IGNORE_NULL_CHECK));
823 ThreadOffset helper = needs_range_check
824 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
825 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
826 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
827 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
828}
829
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700830void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 RegLocation rl_method = LoadCurrMethod();
832 int res_reg = AllocTemp();
833 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
834 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
835 *cu_->dex_file,
836 type_idx)) {
837 // Call out to helper which resolves type and verifies access.
838 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700839 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000840 type_idx, rl_method.reg.GetReg(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 RegLocation rl_result = GetReturn(false);
842 StoreValue(rl_dest, rl_result);
843 } else {
844 // We're don't need access checks, load type from dex cache
845 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700846 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000847 LoadWordDisp(rl_method.reg.GetReg(), dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 int32_t offset_of_type =
849 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
850 * type_idx);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000851 LoadWordDisp(res_reg, offset_of_type, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
853 type_idx) || SLOW_TYPE_PATH) {
854 // Slow path, at runtime test if type is null and if so initialize
855 FlushAllRegs();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000856 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg.GetReg(), 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800857 LIR* cont = NewLIR0(kPseudoTargetLabel);
858
859 // Object to generate the slow path for class resolution.
860 class SlowPath : public LIRSlowPath {
861 public:
862 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
863 const RegLocation& rl_method, const RegLocation& rl_result) :
864 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
865 rl_method_(rl_method), rl_result_(rl_result) {
866 }
867
868 void Compile() {
869 GenerateTargetLabel();
870
871 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000872 rl_method_.reg.GetReg(), true);
873 m2l_->OpRegCopy(rl_result_.reg.GetReg(), m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800874
875 m2l_->OpUnconditionalBranch(cont_);
876 }
877
878 private:
879 const int type_idx_;
880 const RegLocation rl_method_;
881 const RegLocation rl_result_;
882 };
883
884 // Add to list for future.
885 AddSlowPath(new (arena_) SlowPath(this, branch, cont,
886 type_idx, rl_method, rl_result));
887
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800889 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 // Fast path, we're done - just store result
891 StoreValue(rl_dest, rl_result);
892 }
893 }
894}
895
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 /* NOTE: Most strings should be available at compile time */
898 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
899 (sizeof(mirror::String*) * string_idx);
900 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
901 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
902 // slow path, resolve string if not in dex cache
903 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700904 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800905
906 // If the Method* is already in a register, we can save a copy.
907 RegLocation rl_method = mir_graph_->GetMethodLoc();
908 int r_method;
909 if (rl_method.location == kLocPhysReg) {
910 // A temp would conflict with register use below.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000911 DCHECK(!IsTemp(rl_method.reg.GetReg()));
912 r_method = rl_method.reg.GetReg();
Mark Mendell766e9292014-01-27 07:55:47 -0800913 } else {
914 r_method = TargetReg(kArg2);
915 LoadCurrMethodDirect(r_method);
916 }
917 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
918 TargetReg(kArg0));
919
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800922 if (cu_->instruction_set == kThumb2 ||
923 cu_->instruction_set == kMips) {
924 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800925 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800926 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
927 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800929
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800930 // Object to generate the slow path for string resolution.
931 class SlowPath : public LIRSlowPath {
932 public:
933 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
934 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
935 }
936
937 void Compile() {
938 GenerateTargetLabel();
939
940 int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
941
942 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
943 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
944 m2l_->MarkSafepointPC(call_inst);
945 m2l_->FreeTemp(r_tgt);
946
947 m2l_->OpUnconditionalBranch(cont_);
948 }
949
950 private:
951 int r_method_;
952 };
953
954 // Add to list for future.
955 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 } else {
957 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800958 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
959 LoadConstant(TargetReg(kArg1), string_idx);
960 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
Ian Rogers7655f292013-07-29 11:07:13 -0700961 TargetReg(kArg1), true);
Mark Mendell766e9292014-01-27 07:55:47 -0800962 LIR* target = NewLIR0(kPseudoTargetLabel);
963 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 }
965 GenBarrier();
966 StoreValue(rl_dest, GetReturn(false));
967 } else {
968 RegLocation rl_method = LoadCurrMethod();
969 int res_reg = AllocTemp();
970 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000971 LoadWordDisp(rl_method.reg.GetReg(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700972 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000973 LoadWordDisp(res_reg, offset_of_string, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 StoreValue(rl_dest, rl_result);
975 }
976}
977
978/*
979 * Let helper function take care of everything. Will
980 * call Class::NewInstanceFromCode(type_idx, method);
981 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700982void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 FlushAllRegs(); /* Everything to home location */
984 // alloc will always check for resolution, do we also need to verify
985 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700986 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800987 const DexFile* dex_file = cu_->dex_file;
988 CompilerDriver* driver = cu_->compiler_driver;
989 if (driver->CanAccessInstantiableTypeWithoutChecks(
990 cu_->method_idx, *dex_file, type_idx)) {
991 bool is_type_initialized;
992 bool use_direct_type_ptr;
993 uintptr_t direct_type_ptr;
994 if (kEmbedClassInCode &&
995 driver->CanEmbedTypeInCode(*dex_file, type_idx,
996 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
997 // The fast path.
998 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800999 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001000 if (!is_type_initialized) {
1001 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1002 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1003 } else {
1004 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1005 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
1006 }
1007 } else {
1008 // Use the direct pointer.
1009 if (!is_type_initialized) {
1010 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1011 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1012 } else {
1013 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1014 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1015 }
1016 }
1017 } else {
1018 // The slow path.
1019 DCHECK_EQ(func_offset.Int32Value(), -1);
1020 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1021 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1022 }
1023 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001025 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -08001026 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 RegLocation rl_result = GetReturn(false);
1029 StoreValue(rl_dest, rl_result);
1030}
1031
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001032void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -07001034 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035}
1036
1037// For final classes there are no sub-classes to check and so we can answer the instance-of
1038// question with simple comparisons.
1039void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1040 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001041 // X86 has its own implementation.
1042 DCHECK_NE(cu_->instruction_set, kX86);
1043
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 RegLocation object = LoadValue(rl_src, kCoreReg);
1045 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001046 int result_reg = rl_result.reg.GetReg();
1047 if (result_reg == object.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 result_reg = AllocTypedTemp(false, kCoreReg);
1049 }
1050 LoadConstant(result_reg, 0); // assume false
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001051 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg.GetReg(), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052
1053 int check_class = AllocTypedTemp(false, kCoreReg);
1054 int object_class = AllocTypedTemp(false, kCoreReg);
1055
1056 LoadCurrMethodDirect(check_class);
1057 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001058 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001060 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001062 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001064 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 int32_t offset_of_type =
1066 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1067 (sizeof(mirror::Class*) * type_idx);
1068 LoadWordDisp(check_class, offset_of_type, check_class);
1069 }
1070
1071 LIR* ne_branchover = NULL;
1072 if (cu_->instruction_set == kThumb2) {
1073 OpRegReg(kOpCmp, check_class, object_class); // Same?
1074 OpIT(kCondEq, ""); // if-convert the test
1075 LoadConstant(result_reg, 1); // .eq case - load true
1076 } else {
1077 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1078 LoadConstant(result_reg, 1); // eq case - load true
1079 }
1080 LIR* target = NewLIR0(kPseudoTargetLabel);
1081 null_branchover->target = target;
1082 if (ne_branchover != NULL) {
1083 ne_branchover->target = target;
1084 }
1085 FreeTemp(object_class);
1086 FreeTemp(check_class);
1087 if (IsTemp(result_reg)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001088 OpRegCopy(rl_result.reg.GetReg(), result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 FreeTemp(result_reg);
1090 }
1091 StoreValue(rl_dest, rl_result);
1092}
1093
1094void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1095 bool type_known_abstract, bool use_declaring_class,
1096 bool can_assume_type_is_in_dex_cache,
1097 uint32_t type_idx, RegLocation rl_dest,
1098 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001099 // X86 has its own implementation.
1100 DCHECK_NE(cu_->instruction_set, kX86);
1101
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 FlushAllRegs();
1103 // May generate a call - use explicit registers
1104 LockCallTemps();
1105 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1106 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1107 if (needs_access_check) {
1108 // Check we have access to type_idx and if not throw IllegalAccessError,
1109 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001110 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 type_idx, true);
1112 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1113 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1114 } else if (use_declaring_class) {
1115 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1116 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001117 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 } else {
1119 // Load dex cache entry into class_reg (kArg2)
1120 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1121 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001122 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 int32_t offset_of_type =
1124 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1125 * type_idx);
1126 LoadWordDisp(class_reg, offset_of_type, class_reg);
1127 if (!can_assume_type_is_in_dex_cache) {
1128 // Need to test presence of type in dex cache at runtime
1129 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1130 // Not resolved
1131 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001132 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001133 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1135 // Rejoin code paths
1136 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1137 hop_branch->target = hop_target;
1138 }
1139 }
1140 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1141 RegLocation rl_result = GetReturn(false);
1142 if (cu_->instruction_set == kMips) {
1143 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001144 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 }
1146 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1147
1148 /* load object->klass_ */
1149 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1150 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1151 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1152 LIR* branchover = NULL;
1153 if (type_known_final) {
1154 // rl_result == ref == null == 0.
1155 if (cu_->instruction_set == kThumb2) {
1156 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1157 OpIT(kCondEq, "E"); // if-convert the test
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001158 LoadConstant(rl_result.reg.GetReg(), 1); // .eq case - load true
1159 LoadConstant(rl_result.reg.GetReg(), 0); // .ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001161 LoadConstant(rl_result.reg.GetReg(), 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001163 LoadConstant(rl_result.reg.GetReg(), 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 } else {
1166 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001167 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 if (!type_known_abstract) {
1169 /* Uses conditional nullification */
1170 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1171 OpIT(kCondEq, "EE"); // if-convert the test
1172 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1173 }
1174 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1175 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1176 FreeTemp(r_tgt);
1177 } else {
1178 if (!type_known_abstract) {
1179 /* Uses branchovers */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001180 LoadConstant(rl_result.reg.GetReg(), 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1182 }
Mark Mendell6607d972014-02-10 06:54:18 -08001183 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1184 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1185 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1186 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 }
1188 }
1189 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001190 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 /* branch targets here */
1192 LIR* target = NewLIR0(kPseudoTargetLabel);
1193 StoreValue(rl_dest, rl_result);
1194 branch1->target = target;
1195 if (branchover != NULL) {
1196 branchover->target = target;
1197 }
1198}
1199
1200void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1201 bool type_known_final, type_known_abstract, use_declaring_class;
1202 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1203 *cu_->dex_file,
1204 type_idx,
1205 &type_known_final,
1206 &type_known_abstract,
1207 &use_declaring_class);
1208 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1209 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1210
1211 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1212 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1213 } else {
1214 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1215 use_declaring_class, can_assume_type_is_in_dex_cache,
1216 type_idx, rl_dest, rl_src);
1217 }
1218}
1219
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001220void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 bool type_known_final, type_known_abstract, use_declaring_class;
1222 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1223 *cu_->dex_file,
1224 type_idx,
1225 &type_known_final,
1226 &type_known_abstract,
1227 &use_declaring_class);
1228 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1229 // of the exception throw path.
1230 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001231 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 // Verifier type analysis proved this check cast would never cause an exception.
1233 return;
1234 }
1235 FlushAllRegs();
1236 // May generate a call - use explicit registers
1237 LockCallTemps();
1238 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1239 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1240 if (needs_access_check) {
1241 // Check we have access to type_idx and if not throw IllegalAccessError,
1242 // returns Class* in kRet0
1243 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001244 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 type_idx, TargetReg(kArg1), true);
1246 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1247 } else if (use_declaring_class) {
1248 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001249 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 } else {
1251 // Load dex cache entry into class_reg (kArg2)
1252 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001253 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 int32_t offset_of_type =
1255 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1256 (sizeof(mirror::Class*) * type_idx);
1257 LoadWordDisp(class_reg, offset_of_type, class_reg);
1258 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1259 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001260 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1261 LIR* cont = NewLIR0(kPseudoTargetLabel);
1262
1263 // Slow path to initialize the type. Executed if the type is NULL.
1264 class SlowPath : public LIRSlowPath {
1265 public:
1266 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1267 const int class_reg) :
1268 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1269 class_reg_(class_reg) {
1270 }
1271
1272 void Compile() {
1273 GenerateTargetLabel();
1274
1275 // Call out to helper, which will return resolved type in kArg0
1276 // InitializeTypeFromCode(idx, method)
1277 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1278 m2l_->TargetReg(kArg1), true);
1279 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1280 m2l_->OpUnconditionalBranch(cont_);
1281 }
1282 public:
1283 const int type_idx_;
1284 const int class_reg_;
1285 };
1286
1287 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1288 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 }
1290 }
1291 // At this point, class_reg (kArg2) has class
1292 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001293
1294 // Slow path for the case where the classes are not equal. In this case we need
1295 // to call a helper function to do the check.
1296 class SlowPath : public LIRSlowPath {
1297 public:
1298 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1299 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1300 }
1301
1302 void Compile() {
1303 GenerateTargetLabel();
1304
1305 if (load_) {
1306 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1307 m2l_->TargetReg(kArg1));
1308 }
1309 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1310 m2l_->TargetReg(kArg1), true);
1311
1312 m2l_->OpUnconditionalBranch(cont_);
1313 }
1314
1315 private:
1316 bool load_;
1317 };
1318
1319 if (type_known_abstract) {
1320 // Easier case, run slow path if target is non-null (slow path will load from target)
1321 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1322 LIR* cont = NewLIR0(kPseudoTargetLabel);
1323 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1324 } else {
1325 // Harder, more common case. We need to generate a forward branch over the load
1326 // if the target is null. If it's non-null we perform the load and branch to the
1327 // slow path if the classes are not equal.
1328
1329 /* Null is OK - continue */
1330 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1331 /* load object->klass_ */
1332 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1333 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1334 TargetReg(kArg1));
1335
1336 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1337 LIR* cont = NewLIR0(kPseudoTargetLabel);
1338
1339 // Add the slow path that will not perform load since this is already done.
1340 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1341
1342 // Set the null check to branch to the continuation.
1343 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 }
1345}
1346
1347void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001348 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 RegLocation rl_result;
1350 if (cu_->instruction_set == kThumb2) {
1351 /*
1352 * NOTE: This is the one place in the code in which we might have
1353 * as many as six live temporary registers. There are 5 in the normal
1354 * set for Arm. Until we have spill capabilities, temporarily add
1355 * lr to the temp set. It is safe to do this locally, but note that
1356 * lr is used explicitly elsewhere in the code generator and cannot
1357 * normally be used as a general temp register.
1358 */
1359 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1360 FreeTemp(TargetReg(kLr)); // and make it available
1361 }
1362 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1363 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1364 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1365 // The longs may overlap - use intermediate temp if so
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001366 if ((rl_result.reg.GetReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001368 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1369 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1370 OpRegCopy(rl_result.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 FreeTemp(t_reg);
1372 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001373 OpRegRegReg(first_op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1374 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(),
1375 rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 }
1377 /*
1378 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1379 * following StoreValueWide might need to allocate a temp register.
1380 * To further work around the lack of a spill capability, explicitly
1381 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1382 * Remove when spill is functional.
1383 */
1384 FreeRegLocTemps(rl_result, rl_src1);
1385 FreeRegLocTemps(rl_result, rl_src2);
1386 StoreValueWide(rl_dest, rl_result);
1387 if (cu_->instruction_set == kThumb2) {
1388 Clobber(TargetReg(kLr));
1389 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1390 }
1391}
1392
1393
1394void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001395 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001396 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397
1398 switch (opcode) {
1399 case Instruction::SHL_LONG:
1400 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001401 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 break;
1403 case Instruction::SHR_LONG:
1404 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001405 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 break;
1407 case Instruction::USHR_LONG:
1408 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001409 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 break;
1411 default:
1412 LOG(FATAL) << "Unexpected case";
1413 }
1414 FlushAllRegs(); /* Send everything to home location */
1415 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1416 RegLocation rl_result = GetReturnWide(false);
1417 StoreValueWide(rl_dest, rl_result);
1418}
1419
1420
1421void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001422 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001423 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 OpKind op = kOpBkpt;
1425 bool is_div_rem = false;
1426 bool check_zero = false;
1427 bool unary = false;
1428 RegLocation rl_result;
1429 bool shift_op = false;
1430 switch (opcode) {
1431 case Instruction::NEG_INT:
1432 op = kOpNeg;
1433 unary = true;
1434 break;
1435 case Instruction::NOT_INT:
1436 op = kOpMvn;
1437 unary = true;
1438 break;
1439 case Instruction::ADD_INT:
1440 case Instruction::ADD_INT_2ADDR:
1441 op = kOpAdd;
1442 break;
1443 case Instruction::SUB_INT:
1444 case Instruction::SUB_INT_2ADDR:
1445 op = kOpSub;
1446 break;
1447 case Instruction::MUL_INT:
1448 case Instruction::MUL_INT_2ADDR:
1449 op = kOpMul;
1450 break;
1451 case Instruction::DIV_INT:
1452 case Instruction::DIV_INT_2ADDR:
1453 check_zero = true;
1454 op = kOpDiv;
1455 is_div_rem = true;
1456 break;
1457 /* NOTE: returns in kArg1 */
1458 case Instruction::REM_INT:
1459 case Instruction::REM_INT_2ADDR:
1460 check_zero = true;
1461 op = kOpRem;
1462 is_div_rem = true;
1463 break;
1464 case Instruction::AND_INT:
1465 case Instruction::AND_INT_2ADDR:
1466 op = kOpAnd;
1467 break;
1468 case Instruction::OR_INT:
1469 case Instruction::OR_INT_2ADDR:
1470 op = kOpOr;
1471 break;
1472 case Instruction::XOR_INT:
1473 case Instruction::XOR_INT_2ADDR:
1474 op = kOpXor;
1475 break;
1476 case Instruction::SHL_INT:
1477 case Instruction::SHL_INT_2ADDR:
1478 shift_op = true;
1479 op = kOpLsl;
1480 break;
1481 case Instruction::SHR_INT:
1482 case Instruction::SHR_INT_2ADDR:
1483 shift_op = true;
1484 op = kOpAsr;
1485 break;
1486 case Instruction::USHR_INT:
1487 case Instruction::USHR_INT_2ADDR:
1488 shift_op = true;
1489 op = kOpLsr;
1490 break;
1491 default:
1492 LOG(FATAL) << "Invalid word arith op: " << opcode;
1493 }
1494 if (!is_div_rem) {
1495 if (unary) {
1496 rl_src1 = LoadValue(rl_src1, kCoreReg);
1497 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001498 OpRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 } else {
1500 if (shift_op) {
1501 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001502 rl_src2 = LoadValue(rl_src2, kCoreReg);
1503 t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001504 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 rl_src1 = LoadValue(rl_src1, kCoreReg);
1506 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001507 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 FreeTemp(t_reg);
1509 } else {
1510 rl_src1 = LoadValue(rl_src1, kCoreReg);
1511 rl_src2 = LoadValue(rl_src2, kCoreReg);
1512 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001513 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 }
1515 }
1516 StoreValue(rl_dest, rl_result);
1517 } else {
Dave Allison70202782013-10-22 17:52:19 -07001518 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 -07001519 if (cu_->instruction_set == kMips) {
1520 rl_src1 = LoadValue(rl_src1, kCoreReg);
1521 rl_src2 = LoadValue(rl_src2, kCoreReg);
1522 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001523 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001525 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001526 done = true;
1527 } else if (cu_->instruction_set == kThumb2) {
1528 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1529 // Use ARM SDIV instruction for division. For remainder we also need to
1530 // calculate using a MUL and subtract.
1531 rl_src1 = LoadValue(rl_src1, kCoreReg);
1532 rl_src2 = LoadValue(rl_src2, kCoreReg);
1533 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001534 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001535 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001536 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001537 done = true;
1538 }
1539 }
1540
1541 // If we haven't already generated the code use the callout function.
1542 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001543 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 FlushAllRegs(); /* Send everything to home location */
1545 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1546 int r_tgt = CallHelperSetup(func_offset);
1547 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1548 if (check_zero) {
1549 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1550 }
Dave Allison70202782013-10-22 17:52:19 -07001551 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001552 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 if (op == kOpDiv)
1554 rl_result = GetReturn(false);
1555 else
1556 rl_result = GetReturnAlt();
1557 }
1558 StoreValue(rl_dest, rl_result);
1559 }
1560}
1561
1562/*
1563 * The following are the first-level codegen routines that analyze the format
1564 * of each bytecode then either dispatch special purpose codegen routines
1565 * or produce corresponding Thumb instructions directly.
1566 */
1567
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001569static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001570 x &= x - 1;
1571 return (x & (x - 1)) == 0;
1572}
1573
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1575// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001576bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001577 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1579 return false;
1580 }
1581 // No divide instruction for Arm, so check for more special cases
1582 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001583 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 }
1585 int k = LowestSetBit(lit);
1586 if (k >= 30) {
1587 // Avoid special cases.
1588 return false;
1589 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 rl_src = LoadValue(rl_src, kCoreReg);
1591 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001592 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 int t_reg = AllocTemp();
1594 if (lit == 2) {
1595 // Division by 2 is by far the most common division by constant.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001596 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), 32 - k);
1597 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1598 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001599 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001600 OpRegRegImm(kOpAsr, t_reg, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001602 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1603 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 }
1605 } else {
1606 int t_reg1 = AllocTemp();
1607 int t_reg2 = AllocTemp();
1608 if (lit == 2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001609 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg.GetReg(), 32 - k);
1610 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001612 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001613 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001614 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001616 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001618 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 }
1620 }
1621 StoreValue(rl_dest, rl_result);
1622 return true;
1623}
1624
1625// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1626// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001627bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 // Can we simplify this multiplication?
1629 bool power_of_two = false;
1630 bool pop_count_le2 = false;
1631 bool power_of_two_minus_one = false;
1632 if (lit < 2) {
1633 // Avoid special cases.
1634 return false;
1635 } else if (IsPowerOfTwo(lit)) {
1636 power_of_two = true;
1637 } else if (IsPopCountLE2(lit)) {
1638 pop_count_le2 = true;
1639 } else if (IsPowerOfTwo(lit + 1)) {
1640 power_of_two_minus_one = true;
1641 } else {
1642 return false;
1643 }
1644 rl_src = LoadValue(rl_src, kCoreReg);
1645 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1646 if (power_of_two) {
1647 // Shift.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001648 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001649 } else if (pop_count_le2) {
1650 // Shift and add and shift.
1651 int first_bit = LowestSetBit(lit);
1652 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1653 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1654 } else {
1655 // Reverse subtract: (src << (shift + 1)) - src.
1656 DCHECK(power_of_two_minus_one);
1657 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1658 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001659 OpRegRegImm(kOpLsl, t_reg, rl_src.reg.GetReg(), LowestSetBit(lit + 1));
1660 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001661 }
1662 StoreValue(rl_dest, rl_result);
1663 return true;
1664}
1665
1666void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001667 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001668 RegLocation rl_result;
1669 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1670 int shift_op = false;
1671 bool is_div = false;
1672
1673 switch (opcode) {
1674 case Instruction::RSUB_INT_LIT8:
1675 case Instruction::RSUB_INT: {
1676 rl_src = LoadValue(rl_src, kCoreReg);
1677 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1678 if (cu_->instruction_set == kThumb2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001679 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001680 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001681 OpRegReg(kOpNeg, rl_result.reg.GetReg(), rl_src.reg.GetReg());
1682 OpRegImm(kOpAdd, rl_result.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 }
1684 StoreValue(rl_dest, rl_result);
1685 return;
1686 }
1687
1688 case Instruction::SUB_INT:
1689 case Instruction::SUB_INT_2ADDR:
1690 lit = -lit;
1691 // Intended fallthrough
1692 case Instruction::ADD_INT:
1693 case Instruction::ADD_INT_2ADDR:
1694 case Instruction::ADD_INT_LIT8:
1695 case Instruction::ADD_INT_LIT16:
1696 op = kOpAdd;
1697 break;
1698 case Instruction::MUL_INT:
1699 case Instruction::MUL_INT_2ADDR:
1700 case Instruction::MUL_INT_LIT8:
1701 case Instruction::MUL_INT_LIT16: {
1702 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1703 return;
1704 }
1705 op = kOpMul;
1706 break;
1707 }
1708 case Instruction::AND_INT:
1709 case Instruction::AND_INT_2ADDR:
1710 case Instruction::AND_INT_LIT8:
1711 case Instruction::AND_INT_LIT16:
1712 op = kOpAnd;
1713 break;
1714 case Instruction::OR_INT:
1715 case Instruction::OR_INT_2ADDR:
1716 case Instruction::OR_INT_LIT8:
1717 case Instruction::OR_INT_LIT16:
1718 op = kOpOr;
1719 break;
1720 case Instruction::XOR_INT:
1721 case Instruction::XOR_INT_2ADDR:
1722 case Instruction::XOR_INT_LIT8:
1723 case Instruction::XOR_INT_LIT16:
1724 op = kOpXor;
1725 break;
1726 case Instruction::SHL_INT_LIT8:
1727 case Instruction::SHL_INT:
1728 case Instruction::SHL_INT_2ADDR:
1729 lit &= 31;
1730 shift_op = true;
1731 op = kOpLsl;
1732 break;
1733 case Instruction::SHR_INT_LIT8:
1734 case Instruction::SHR_INT:
1735 case Instruction::SHR_INT_2ADDR:
1736 lit &= 31;
1737 shift_op = true;
1738 op = kOpAsr;
1739 break;
1740 case Instruction::USHR_INT_LIT8:
1741 case Instruction::USHR_INT:
1742 case Instruction::USHR_INT_2ADDR:
1743 lit &= 31;
1744 shift_op = true;
1745 op = kOpLsr;
1746 break;
1747
1748 case Instruction::DIV_INT:
1749 case Instruction::DIV_INT_2ADDR:
1750 case Instruction::DIV_INT_LIT8:
1751 case Instruction::DIV_INT_LIT16:
1752 case Instruction::REM_INT:
1753 case Instruction::REM_INT_2ADDR:
1754 case Instruction::REM_INT_LIT8:
1755 case Instruction::REM_INT_LIT16: {
1756 if (lit == 0) {
1757 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1758 return;
1759 }
buzbee11b63d12013-08-27 07:34:17 -07001760 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001761 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001762 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001763 (opcode == Instruction::DIV_INT_LIT16)) {
1764 is_div = true;
1765 } else {
1766 is_div = false;
1767 }
buzbee11b63d12013-08-27 07:34:17 -07001768 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1769 return;
1770 }
Dave Allison70202782013-10-22 17:52:19 -07001771
1772 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001773 if (cu_->instruction_set == kMips) {
1774 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001775 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001776 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001777 } else if (cu_->instruction_set == kX86) {
1778 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1779 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001780 } else if (cu_->instruction_set == kThumb2) {
1781 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1782 // Use ARM SDIV instruction for division. For remainder we also need to
1783 // calculate using a MUL and subtract.
1784 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001785 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001786 done = true;
1787 }
1788 }
1789
1790 if (!done) {
1791 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001792 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1793 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001794 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001795 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1796 if (is_div)
1797 rl_result = GetReturn(false);
1798 else
1799 rl_result = GetReturnAlt();
1800 }
1801 StoreValue(rl_dest, rl_result);
1802 return;
1803 }
1804 default:
1805 LOG(FATAL) << "Unexpected opcode " << opcode;
1806 }
1807 rl_src = LoadValue(rl_src, kCoreReg);
1808 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001809 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001810 if (shift_op && (lit == 0)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001811 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001812 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001813 OpRegRegImm(op, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001814 }
1815 StoreValue(rl_dest, rl_result);
1816}
1817
1818void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001819 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001820 RegLocation rl_result;
1821 OpKind first_op = kOpBkpt;
1822 OpKind second_op = kOpBkpt;
1823 bool call_out = false;
1824 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001825 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001826 int ret_reg = TargetReg(kRet0);
1827
1828 switch (opcode) {
1829 case Instruction::NOT_LONG:
1830 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1831 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1832 // Check for destructive overlap
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001833 if (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001834 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001835 OpRegCopy(t_reg, rl_src2.reg.GetHighReg());
1836 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1837 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001838 FreeTemp(t_reg);
1839 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001840 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1841 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001842 }
1843 StoreValueWide(rl_dest, rl_result);
1844 return;
1845 case Instruction::ADD_LONG:
1846 case Instruction::ADD_LONG_2ADDR:
1847 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001848 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001849 return;
1850 }
1851 first_op = kOpAdd;
1852 second_op = kOpAdc;
1853 break;
1854 case Instruction::SUB_LONG:
1855 case Instruction::SUB_LONG_2ADDR:
1856 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001857 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 return;
1859 }
1860 first_op = kOpSub;
1861 second_op = kOpSbc;
1862 break;
1863 case Instruction::MUL_LONG:
1864 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001865 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001866 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001867 return;
1868 } else {
1869 call_out = true;
1870 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001871 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001872 }
1873 break;
1874 case Instruction::DIV_LONG:
1875 case Instruction::DIV_LONG_2ADDR:
1876 call_out = true;
1877 check_zero = true;
1878 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001879 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001880 break;
1881 case Instruction::REM_LONG:
1882 case Instruction::REM_LONG_2ADDR:
1883 call_out = true;
1884 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001885 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001886 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1887 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1888 break;
1889 case Instruction::AND_LONG_2ADDR:
1890 case Instruction::AND_LONG:
1891 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001892 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001893 }
1894 first_op = kOpAnd;
1895 second_op = kOpAnd;
1896 break;
1897 case Instruction::OR_LONG:
1898 case Instruction::OR_LONG_2ADDR:
1899 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001900 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001901 return;
1902 }
1903 first_op = kOpOr;
1904 second_op = kOpOr;
1905 break;
1906 case Instruction::XOR_LONG:
1907 case Instruction::XOR_LONG_2ADDR:
1908 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001909 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001910 return;
1911 }
1912 first_op = kOpXor;
1913 second_op = kOpXor;
1914 break;
1915 case Instruction::NEG_LONG: {
1916 GenNegLong(rl_dest, rl_src2);
1917 return;
1918 }
1919 default:
1920 LOG(FATAL) << "Invalid long arith op";
1921 }
1922 if (!call_out) {
1923 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1924 } else {
1925 FlushAllRegs(); /* Send everything to home location */
1926 if (check_zero) {
1927 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1928 int r_tgt = CallHelperSetup(func_offset);
1929 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1930 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1931 // NOTE: callout here is not a safepoint
1932 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1933 } else {
1934 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1935 }
1936 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1937 if (ret_reg == TargetReg(kRet0))
1938 rl_result = GetReturnWide(false);
1939 else
1940 rl_result = GetReturnWideAlt();
1941 StoreValueWide(rl_dest, rl_result);
1942 }
1943}
1944
Ian Rogers848871b2013-08-05 10:56:33 -07001945void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001946 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001947 /*
1948 * Don't optimize the register usage since it calls out to support
1949 * functions
1950 */
1951 FlushAllRegs(); /* Send everything to home location */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001952 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1953 if (rl_dest.wide) {
1954 RegLocation rl_result;
1955 rl_result = GetReturnWide(rl_dest.fp);
1956 StoreValueWide(rl_dest, rl_result);
1957 } else {
1958 RegLocation rl_result;
1959 rl_result = GetReturn(rl_dest.fp);
1960 StoreValue(rl_dest, rl_result);
1961 }
1962}
1963
1964/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001965void Mir2Lir::GenSuspendTest(int opt_flags) {
Dave Allisonb373e092014-02-20 16:06:36 -08001966 if (Runtime::Current()->ExplicitSuspendChecks()) {
1967 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1968 return;
1969 }
1970 FlushAllRegs();
1971 LIR* branch = OpTestSuspend(NULL);
1972 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
1973 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1974 current_dalvik_offset_);
1975 branch->target = target;
1976 suspend_launchpads_.Insert(target);
1977 } else {
1978 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1979 return;
1980 }
1981 FlushAllRegs(); // TODO: needed?
1982 LIR* inst = CheckSuspendUsingLoad();
1983 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001984 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001985}
1986
1987/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001988void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Dave Allisonb373e092014-02-20 16:06:36 -08001989 if (Runtime::Current()->ExplicitSuspendChecks()) {
1990 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1991 OpUnconditionalBranch(target);
1992 return;
1993 }
1994 OpTestSuspend(target);
1995 LIR* launch_pad =
1996 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1997 current_dalvik_offset_);
1998 FlushAllRegs();
1999 OpUnconditionalBranch(launch_pad);
2000 suspend_launchpads_.Insert(launch_pad);
2001 } else {
2002 // For the implicit suspend check, just perform the trigger
2003 // load and branch to the target.
2004 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
2005 OpUnconditionalBranch(target);
2006 return;
2007 }
2008 FlushAllRegs();
2009 LIR* inst = CheckSuspendUsingLoad();
2010 MarkSafepointPC(inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002011 OpUnconditionalBranch(target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002013}
2014
Ian Rogersd9c4fc92013-10-01 19:45:43 -07002015/* Call out to helper assembly routine that will null check obj and then lock it. */
2016void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
2017 FlushAllRegs();
2018 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
2019}
2020
2021/* Call out to helper assembly routine that will null check obj and then unlock it. */
2022void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
2023 FlushAllRegs();
2024 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2025}
2026
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002027/* Generic code for generating a wide constant into a VR. */
2028void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2029 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00002030 LoadConstantWide(rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00002031 StoreValueWide(rl_dest, rl_result);
2032}
2033
Brian Carlstrom7940e442013-07-12 13:46:57 -07002034} // namespace art