blob: 1c5f6a0fdf5db6e84f272e5794eaa6a82bebcfd3 [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
69/* Perform null-check on a register. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070070LIR* Mir2Lir::GenNullCheck(int s_reg, int m_reg, int opt_flags) {
Ian Rogersa9a82542013-10-04 11:17:26 -070071 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 return NULL;
73 }
74 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
75}
76
77/* Perform check on two registers */
78LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070079 ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
81 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
82 // Remember branch target - will process later
83 throw_launchpads_.Insert(tgt);
84 return branch;
85}
86
87void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
88 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070089 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 ConditionCode cond;
91 switch (opcode) {
92 case Instruction::IF_EQ:
93 cond = kCondEq;
94 break;
95 case Instruction::IF_NE:
96 cond = kCondNe;
97 break;
98 case Instruction::IF_LT:
99 cond = kCondLt;
100 break;
101 case Instruction::IF_GE:
102 cond = kCondGe;
103 break;
104 case Instruction::IF_GT:
105 cond = kCondGt;
106 break;
107 case Instruction::IF_LE:
108 cond = kCondLe;
109 break;
110 default:
111 cond = static_cast<ConditionCode>(0);
112 LOG(FATAL) << "Unexpected opcode " << opcode;
113 }
114
115 // Normalize such that if either operand is constant, src2 will be constant
116 if (rl_src1.is_const) {
117 RegLocation rl_temp = rl_src1;
118 rl_src1 = rl_src2;
119 rl_src2 = rl_temp;
120 cond = FlipComparisonOrder(cond);
121 }
122
123 rl_src1 = LoadValue(rl_src1, kCoreReg);
124 // Is this really an immediate comparison?
125 if (rl_src2.is_const) {
126 // If it's already live in a register or not easily materialized, just keep going
127 RegLocation rl_temp = UpdateLoc(rl_src2);
128 if ((rl_temp.location == kLocDalvikFrame) &&
129 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
130 // OK - convert this to a compare immediate and branch
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000131 OpCmpImmBranch(cond, rl_src1.reg.GetReg(), mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 return;
133 }
134 }
135 rl_src2 = LoadValue(rl_src2, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000136 OpCmpBranch(cond, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137}
138
139void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700140 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 ConditionCode cond;
142 rl_src = LoadValue(rl_src, kCoreReg);
143 switch (opcode) {
144 case Instruction::IF_EQZ:
145 cond = kCondEq;
146 break;
147 case Instruction::IF_NEZ:
148 cond = kCondNe;
149 break;
150 case Instruction::IF_LTZ:
151 cond = kCondLt;
152 break;
153 case Instruction::IF_GEZ:
154 cond = kCondGe;
155 break;
156 case Instruction::IF_GTZ:
157 cond = kCondGt;
158 break;
159 case Instruction::IF_LEZ:
160 cond = kCondLe;
161 break;
162 default:
163 cond = static_cast<ConditionCode>(0);
164 LOG(FATAL) << "Unexpected opcode " << opcode;
165 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000166 OpCmpImmBranch(cond, rl_src.reg.GetReg(), 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167}
168
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700169void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
171 if (rl_src.location == kLocPhysReg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000172 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000174 LoadValueDirect(rl_src, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000176 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_result.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 StoreValueWide(rl_dest, rl_result);
178}
179
180void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700182 rl_src = LoadValue(rl_src, kCoreReg);
183 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
184 OpKind op = kOpInvalid;
185 switch (opcode) {
186 case Instruction::INT_TO_BYTE:
187 op = kOp2Byte;
188 break;
189 case Instruction::INT_TO_SHORT:
190 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700192 case Instruction::INT_TO_CHAR:
193 op = kOp2Char;
194 break;
195 default:
196 LOG(ERROR) << "Bad int conversion type";
197 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000198 OpRegReg(op, rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700199 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200}
201
202/*
203 * Let helper function take care of everything. Will call
204 * Array::AllocFromCode(type_idx, method, count);
205 * Note: AllocFromCode will handle checks for errNegativeArraySize.
206 */
207void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700208 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700210 ThreadOffset func_offset(-1);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800211 const DexFile* dex_file = cu_->dex_file;
212 CompilerDriver* driver = cu_->compiler_driver;
213 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 type_idx)) {
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800215 bool is_type_initialized; // Ignored as an array does not have an initializer.
216 bool use_direct_type_ptr;
217 uintptr_t direct_type_ptr;
218 if (kEmbedClassInCode &&
219 driver->CanEmbedTypeInCode(*dex_file, type_idx,
220 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
221 // The fast path.
222 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800223 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800224 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
225 CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
226 } else {
227 // Use the direct pointer.
228 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
229 CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
230 }
231 } else {
232 // The slow path.
233 DCHECK_EQ(func_offset.Int32Value(), -1);
234 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
235 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
236 }
237 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700239 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800240 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 RegLocation rl_result = GetReturn(false);
243 StoreValue(rl_dest, rl_result);
244}
245
246/*
247 * Similar to GenNewArray, but with post-allocation initialization.
248 * Verifier guarantees we're dealing with an array class. Current
249 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
250 * Current code also throws internal unimp if not 'L', '[' or 'I'.
251 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700252void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 int elems = info->num_arg_words;
254 int type_idx = info->index;
255 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700256 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
258 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700259 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700261 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 }
263 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
264 FreeTemp(TargetReg(kArg2));
265 FreeTemp(TargetReg(kArg1));
266 /*
267 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
268 * return region. Because AllocFromCode placed the new array
269 * in kRet0, we'll just lock it into place. When debugger support is
270 * added, it may be necessary to additionally copy all return
271 * values to a home location in thread-local storage
272 */
273 LockTemp(TargetReg(kRet0));
274
275 // TODO: use the correct component size, currently all supported types
276 // share array alignment with ints (see comment at head of function)
277 size_t component_size = sizeof(int32_t);
278
279 // Having a range of 0 is legal
280 if (info->is_range && (elems > 0)) {
281 /*
282 * Bit of ugliness here. We're going generate a mem copy loop
283 * on the register range, but it is possible that some regs
284 * in the range have been promoted. This is unlikely, but
285 * before generating the copy, we'll just force a flush
286 * of any regs in the source range that have been promoted to
287 * home location.
288 */
289 for (int i = 0; i < elems; i++) {
290 RegLocation loc = UpdateLoc(info->args[i]);
291 if (loc.location == kLocPhysReg) {
292 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000293 loc.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 }
295 }
296 /*
297 * TUNING note: generated code here could be much improved, but
298 * this is an uncommon operation and isn't especially performance
299 * critical.
300 */
301 int r_src = AllocTemp();
302 int r_dst = AllocTemp();
303 int r_idx = AllocTemp();
304 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700305 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 case kThumb2:
307 r_val = TargetReg(kLr);
308 break;
309 case kX86:
310 FreeTemp(TargetReg(kRet0));
311 r_val = AllocTemp();
312 break;
313 case kMips:
314 r_val = AllocTemp();
315 break;
316 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
317 }
318 // Set up source pointer
319 RegLocation rl_first = info->args[0];
320 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
321 // Set up the target pointer
322 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
323 mirror::Array::DataOffset(component_size).Int32Value());
324 // Set up the loop counter (known to be > 0)
325 LoadConstant(r_idx, elems - 1);
326 // Generate the copy loop. Going backwards for convenience
327 LIR* target = NewLIR0(kPseudoTargetLabel);
328 // Copy next element
329 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
330 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
331 FreeTemp(r_val);
332 OpDecAndBranch(kCondGe, r_idx, target);
333 if (cu_->instruction_set == kX86) {
334 // Restore the target pointer
335 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
336 -mirror::Array::DataOffset(component_size).Int32Value());
337 }
338 } else if (!info->is_range) {
339 // TUNING: interleave
340 for (int i = 0; i < elems; i++) {
341 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
342 StoreBaseDisp(TargetReg(kRet0),
343 mirror::Array::DataOffset(component_size).Int32Value() +
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000344 i * 4, rl_arg.reg.GetReg(), kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 // If the LoadValue caused a temp to be allocated, free it
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000346 if (IsTemp(rl_arg.reg.GetReg())) {
347 FreeTemp(rl_arg.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 }
349 }
350 }
351 if (info->result.location != kLocInvalid) {
352 StoreValue(info->result, GetReturn(false /* not fp */));
353 }
354}
355
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800356//
357// Slow path to ensure a class is initialized for sget/sput.
358//
359class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
360 public:
361 StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
362 int storage_index, int r_base) :
363 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
364 r_base_(r_base) {
365 }
366
367 void Compile() {
368 LIR* unresolved_target = GenerateTargetLabel();
369 uninit_->target = unresolved_target;
370 m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
371 storage_index_, true);
372 // Copy helper's result into r_base, a no-op on all but MIPS.
373 m2l_->OpRegCopy(r_base_, m2l_->TargetReg(kRet0));
374
375 m2l_->OpUnconditionalBranch(cont_);
376 }
377
378 private:
379 LIR* const uninit_;
380 const int storage_index_;
381 const int r_base_;
382};
383
Vladimir Markobe0e5462014-02-26 11:24:15 +0000384void Mir2Lir::GenSput(MIR* mir, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700385 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000386 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
387 cu_->compiler_driver->ProcessedStaticField(field_info.FastPut(), field_info.IsReferrersClass());
388 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
389 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800390 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000391 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 // Fast path, static storage base is this method's class
393 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800394 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000395 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800396 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000397 if (IsTemp(rl_method.reg.GetReg())) {
398 FreeTemp(rl_method.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
400 } else {
401 // Medium path, static storage base in a different class which requires checks that the other
402 // class is initialized.
403 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000404 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 // May do runtime call so everything to home locations.
406 FlushAllRegs();
407 // Using fixed register to sync with possible call to runtime support.
408 int r_method = TargetReg(kArg1);
409 LockTemp(r_method);
410 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800411 r_base = TargetReg(kArg0);
412 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800414 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
415 r_base);
416 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000417 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800418 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000419 if (!field_info.IsInitialized()) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800420 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800421
422 // The slow path is invoked if the r_base is NULL or the class pointed
423 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800424 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
425 int r_tmp = TargetReg(kArg2);
426 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800427 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800428 mirror::Class::StatusOffset().Int32Value(),
429 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800430 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800431
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800432 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
433 unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000434 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800435
436 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 FreeTemp(r_method);
439 }
440 // rBase now holds static storage base
441 if (is_long_or_double) {
442 rl_src = LoadValueWide(rl_src, kAnyReg);
443 } else {
444 rl_src = LoadValue(rl_src, kAnyReg);
445 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000446 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 GenMemBarrier(kStoreStore);
448 }
449 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000450 StoreBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg(),
451 rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000453 StoreWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 }
Vladimir Markobe0e5462014-02-26 11:24:15 +0000455 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 GenMemBarrier(kStoreLoad);
457 }
458 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000459 MarkGCCard(rl_src.reg.GetReg(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800461 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 } else {
463 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700464 ThreadOffset setter_offset =
465 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
466 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
467 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000468 CallRuntimeHelperImmRegLocation(setter_offset, field_info.FieldIndex(), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 }
470}
471
Vladimir Markobe0e5462014-02-26 11:24:15 +0000472void Mir2Lir::GenSget(MIR* mir, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700473 bool is_long_or_double, bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000474 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
475 cu_->compiler_driver->ProcessedStaticField(field_info.FastGet(), field_info.IsReferrersClass());
476 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
477 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800478 int r_base;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000479 if (field_info.IsReferrersClass()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 // Fast path, static storage base is this method's class
481 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800482 r_base = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000483 LoadWordDisp(rl_method.reg.GetReg(),
Ian Rogers5ddb4102014-01-07 08:58:46 -0800484 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 } else {
486 // Medium path, static storage base in a different class which requires checks that the other
487 // class is initialized
Vladimir Markobe0e5462014-02-26 11:24:15 +0000488 DCHECK_NE(field_info.StorageIndex(), DexFile::kDexNoIndex);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 // May do runtime call so everything to home locations.
490 FlushAllRegs();
491 // Using fixed register to sync with possible call to runtime support.
492 int r_method = TargetReg(kArg1);
493 LockTemp(r_method);
494 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800495 r_base = TargetReg(kArg0);
496 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800498 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
499 r_base);
500 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
Vladimir Markobe0e5462014-02-26 11:24:15 +0000501 sizeof(int32_t*) * field_info.StorageIndex(), r_base);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800502 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000503 if (!field_info.IsInitialized()) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800504 // Check if r_base is NULL or a not yet initialized class.
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800505
506 // The slow path is invoked if the r_base is NULL or the class pointed
507 // to by it is not initialized.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800508 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
509 int r_tmp = TargetReg(kArg2);
510 LockTemp(r_tmp);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800511 LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
Mark Mendell766e9292014-01-27 07:55:47 -0800512 mirror::Class::StatusOffset().Int32Value(),
513 mirror::Class::kStatusInitialized, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800514 LIR* cont = NewLIR0(kPseudoTargetLabel);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800515
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800516 AddSlowPath(new (arena_) StaticFieldSlowPath(this,
517 unresolved_branch, uninit_branch, cont,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000518 field_info.StorageIndex(), r_base));
Ian Rogers5ddb4102014-01-07 08:58:46 -0800519
520 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 FreeTemp(r_method);
523 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800524 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000526 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 GenMemBarrier(kLoadLoad);
528 }
529 if (is_long_or_double) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000530 LoadBaseDispWide(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg(),
531 rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000533 LoadWordDisp(r_base, field_info.FieldOffset().Int32Value(), rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800535 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 if (is_long_or_double) {
537 StoreValueWide(rl_dest, rl_result);
538 } else {
539 StoreValue(rl_dest, rl_result);
540 }
541 } else {
542 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700543 ThreadOffset getterOffset =
544 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
545 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
546 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000547 CallRuntimeHelperImm(getterOffset, field_info.FieldIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 if (is_long_or_double) {
549 RegLocation rl_result = GetReturnWide(rl_dest.fp);
550 StoreValueWide(rl_dest, rl_result);
551 } else {
552 RegLocation rl_result = GetReturn(rl_dest.fp);
553 StoreValue(rl_dest, rl_result);
554 }
555 }
556}
557
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800558// Generate code for all slow paths.
559void Mir2Lir::HandleSlowPaths() {
560 int n = slow_paths_.Size();
561 for (int i = 0; i < n; ++i) {
562 LIRSlowPath* slowpath = slow_paths_.Get(i);
563 slowpath->Compile();
564 }
565 slow_paths_.Reset();
566}
567
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700568void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700570 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 for (int i = 0; i < num_elems; i++) {
572 ResetRegPool();
573 ResetDefTracking();
574 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700575 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 current_dalvik_offset_ = lab->operands[1];
577 AppendLIR(lab);
578 int r_tgt = CallHelperSetup(helper_offset);
579 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
580 OpUnconditionalBranch(resume_lab);
581 }
582}
583
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700584void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 int num_elems = throw_launchpads_.Size();
586 for (int i = 0; i < num_elems; i++) {
587 ResetRegPool();
588 ResetDefTracking();
589 LIR* lab = throw_launchpads_.Get(i);
590 current_dalvik_offset_ = lab->operands[1];
591 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700592 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 int v1 = lab->operands[2];
594 int v2 = lab->operands[3];
595 bool target_x86 = (cu_->instruction_set == kX86);
596 switch (lab->operands[0]) {
597 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700598 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700600 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
602 if (target_x86) {
603 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
604 } else {
605 OpRegCopy(TargetReg(kArg1), v1);
606 }
607 // Make sure the following LoadConstant doesn't mess with kArg1.
608 LockTemp(TargetReg(kArg1));
609 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700610 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 break;
612 case kThrowArrayBounds:
613 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
614 if (v2 != TargetReg(kArg0)) {
615 OpRegCopy(TargetReg(kArg0), v1);
616 if (target_x86) {
617 // x86 leaves the array pointer in v2, so load the array length that the handler expects
618 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
619 } else {
620 OpRegCopy(TargetReg(kArg1), v2);
621 }
622 } else {
623 if (v1 == TargetReg(kArg1)) {
624 // Swap v1 and v2, using kArg2 as a temp
625 OpRegCopy(TargetReg(kArg2), v1);
626 if (target_x86) {
627 // x86 leaves the array pointer in v2; load the array length that the handler expects
628 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
629 } else {
630 OpRegCopy(TargetReg(kArg1), v2);
631 }
632 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
633 } else {
634 if (target_x86) {
635 // x86 leaves the array pointer in v2; load the array length that the handler expects
636 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
637 } else {
638 OpRegCopy(TargetReg(kArg1), v2);
639 }
640 OpRegCopy(TargetReg(kArg0), v1);
641 }
642 }
Ian Rogers848871b2013-08-05 10:56:33 -0700643 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 break;
645 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700646 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 break;
648 case kThrowNoSuchMethod:
649 OpRegCopy(TargetReg(kArg0), v1);
650 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700651 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 break;
653 case kThrowStackOverflow:
Ian Rogers848871b2013-08-05 10:56:33 -0700654 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 // Restore stack alignment
656 if (target_x86) {
657 OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
658 } else {
659 OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
660 }
661 break;
662 default:
663 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
664 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000665 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 int r_tgt = CallHelperSetup(func_offset);
667 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
668 }
669}
670
Vladimir Markobe0e5462014-02-26 11:24:15 +0000671void Mir2Lir::GenIGet(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700673 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000674 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
675 cu_->compiler_driver->ProcessedInstanceField(field_info.FastGet());
676 if (field_info.FastGet() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 RegLocation rl_result;
678 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000679 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 rl_obj = LoadValue(rl_obj, kCoreReg);
681 if (is_long_or_double) {
682 DCHECK(rl_dest.wide);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000683 GenNullCheck(rl_obj.s_reg_low, rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 if (cu_->instruction_set == kX86) {
685 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000686 GenNullCheck(rl_obj.s_reg_low, rl_obj.reg.GetReg(), opt_flags);
687 LoadBaseDispWide(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
688 rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), rl_obj.s_reg_low);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 GenMemBarrier(kLoadLoad);
691 }
692 } else {
693 int reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000694 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000696 LoadBaseDispWide(reg_ptr, 0, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(),
697 INVALID_SREG);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 GenMemBarrier(kLoadLoad);
700 }
701 FreeTemp(reg_ptr);
702 }
703 StoreValueWide(rl_dest, rl_result);
704 } else {
705 rl_result = EvalLoc(rl_dest, reg_class, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000706 GenNullCheck(rl_obj.s_reg_low, rl_obj.reg.GetReg(), opt_flags);
707 LoadBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
708 rl_result.reg.GetReg(), kWord, rl_obj.s_reg_low);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000709 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 GenMemBarrier(kLoadLoad);
711 }
712 StoreValue(rl_dest, rl_result);
713 }
714 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700715 ThreadOffset getterOffset =
716 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
717 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
718 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000719 CallRuntimeHelperImmRegLocation(getterOffset, field_info.FieldIndex(), rl_obj, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 if (is_long_or_double) {
721 RegLocation rl_result = GetReturnWide(rl_dest.fp);
722 StoreValueWide(rl_dest, rl_result);
723 } else {
724 RegLocation rl_result = GetReturn(rl_dest.fp);
725 StoreValue(rl_dest, rl_result);
726 }
727 }
728}
729
Vladimir Markobe0e5462014-02-26 11:24:15 +0000730void Mir2Lir::GenIPut(MIR* mir, int opt_flags, OpSize size,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700732 bool is_object) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000733 const MirIFieldLoweringInfo& field_info = mir_graph_->GetIFieldLoweringInfo(mir);
734 cu_->compiler_driver->ProcessedInstanceField(field_info.FastPut());
735 if (field_info.FastPut() && !SLOW_FIELD_PATH) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 RegisterClass reg_class = oat_reg_class_by_size(size);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000737 DCHECK_GE(field_info.FieldOffset().Int32Value(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 rl_obj = LoadValue(rl_obj, kCoreReg);
739 if (is_long_or_double) {
740 int reg_ptr;
741 rl_src = LoadValueWide(rl_src, kAnyReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000742 GenNullCheck(rl_obj.s_reg_low, rl_obj.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000744 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000745 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 GenMemBarrier(kStoreStore);
747 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000748 StoreBaseDispWide(reg_ptr, 0, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000749 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 GenMemBarrier(kLoadLoad);
751 }
752 FreeTemp(reg_ptr);
753 } else {
754 rl_src = LoadValue(rl_src, reg_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000755 GenNullCheck(rl_obj.s_reg_low, rl_obj.reg.GetReg(), opt_flags);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000756 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 GenMemBarrier(kStoreStore);
758 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000759 StoreBaseDisp(rl_obj.reg.GetReg(), field_info.FieldOffset().Int32Value(),
760 rl_src.reg.GetReg(), kWord);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000761 if (field_info.IsVolatile()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 GenMemBarrier(kLoadLoad);
763 }
764 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000765 MarkGCCard(rl_src.reg.GetReg(), rl_obj.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 }
767 }
768 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700769 ThreadOffset setter_offset =
770 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
771 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
772 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000773 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_info.FieldIndex(),
774 rl_obj, rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 }
776}
777
Ian Rogersa9a82542013-10-04 11:17:26 -0700778void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
779 RegLocation rl_src) {
780 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
781 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
782 (opt_flags & MIR_IGNORE_NULL_CHECK));
783 ThreadOffset helper = needs_range_check
784 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
785 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
786 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
787 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
788}
789
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700790void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 RegLocation rl_method = LoadCurrMethod();
792 int res_reg = AllocTemp();
793 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
794 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
795 *cu_->dex_file,
796 type_idx)) {
797 // Call out to helper which resolves type and verifies access.
798 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700799 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000800 type_idx, rl_method.reg.GetReg(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 RegLocation rl_result = GetReturn(false);
802 StoreValue(rl_dest, rl_result);
803 } else {
804 // We're don't need access checks, load type from dex cache
805 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700806 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000807 LoadWordDisp(rl_method.reg.GetReg(), dex_cache_offset, res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 int32_t offset_of_type =
809 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
810 * type_idx);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000811 LoadWordDisp(res_reg, offset_of_type, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
813 type_idx) || SLOW_TYPE_PATH) {
814 // Slow path, at runtime test if type is null and if so initialize
815 FlushAllRegs();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000816 LIR* branch = OpCmpImmBranch(kCondEq, rl_result.reg.GetReg(), 0, NULL);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800817 LIR* cont = NewLIR0(kPseudoTargetLabel);
818
819 // Object to generate the slow path for class resolution.
820 class SlowPath : public LIRSlowPath {
821 public:
822 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
823 const RegLocation& rl_method, const RegLocation& rl_result) :
824 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
825 rl_method_(rl_method), rl_result_(rl_result) {
826 }
827
828 void Compile() {
829 GenerateTargetLabel();
830
831 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000832 rl_method_.reg.GetReg(), true);
833 m2l_->OpRegCopy(rl_result_.reg.GetReg(), m2l_->TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800834
835 m2l_->OpUnconditionalBranch(cont_);
836 }
837
838 private:
839 const int type_idx_;
840 const RegLocation rl_method_;
841 const RegLocation rl_result_;
842 };
843
844 // Add to list for future.
845 AddSlowPath(new (arena_) SlowPath(this, branch, cont,
846 type_idx, rl_method, rl_result));
847
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 StoreValue(rl_dest, rl_result);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800849 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 // Fast path, we're done - just store result
851 StoreValue(rl_dest, rl_result);
852 }
853 }
854}
855
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700856void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857 /* NOTE: Most strings should be available at compile time */
858 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
859 (sizeof(mirror::String*) * string_idx);
860 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
861 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
862 // slow path, resolve string if not in dex cache
863 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700864 LockCallTemps(); // Using explicit registers
Mark Mendell766e9292014-01-27 07:55:47 -0800865
866 // If the Method* is already in a register, we can save a copy.
867 RegLocation rl_method = mir_graph_->GetMethodLoc();
868 int r_method;
869 if (rl_method.location == kLocPhysReg) {
870 // A temp would conflict with register use below.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000871 DCHECK(!IsTemp(rl_method.reg.GetReg()));
872 r_method = rl_method.reg.GetReg();
Mark Mendell766e9292014-01-27 07:55:47 -0800873 } else {
874 r_method = TargetReg(kArg2);
875 LoadCurrMethodDirect(r_method);
876 }
877 LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
878 TargetReg(kArg0));
879
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 // Might call out to helper, which will return resolved string in kRet0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800882 if (cu_->instruction_set == kThumb2 ||
883 cu_->instruction_set == kMips) {
884 // OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
Mark Mendell766e9292014-01-27 07:55:47 -0800885 LoadConstant(TargetReg(kArg1), string_idx);
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800886 LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
887 LIR* cont = NewLIR0(kPseudoTargetLabel);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 GenBarrier();
Mark Mendell766e9292014-01-27 07:55:47 -0800889
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800890 // Object to generate the slow path for string resolution.
891 class SlowPath : public LIRSlowPath {
892 public:
893 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
894 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
895 }
896
897 void Compile() {
898 GenerateTargetLabel();
899
900 int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
901
902 m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_); // .eq
903 LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
904 m2l_->MarkSafepointPC(call_inst);
905 m2l_->FreeTemp(r_tgt);
906
907 m2l_->OpUnconditionalBranch(cont_);
908 }
909
910 private:
911 int r_method_;
912 };
913
914 // Add to list for future.
915 AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 } else {
917 DCHECK_EQ(cu_->instruction_set, kX86);
Mark Mendell766e9292014-01-27 07:55:47 -0800918 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
919 LoadConstant(TargetReg(kArg1), string_idx);
920 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
Ian Rogers7655f292013-07-29 11:07:13 -0700921 TargetReg(kArg1), true);
Mark Mendell766e9292014-01-27 07:55:47 -0800922 LIR* target = NewLIR0(kPseudoTargetLabel);
923 branch->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 }
925 GenBarrier();
926 StoreValue(rl_dest, GetReturn(false));
927 } else {
928 RegLocation rl_method = LoadCurrMethod();
929 int res_reg = AllocTemp();
930 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000931 LoadWordDisp(rl_method.reg.GetReg(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700932 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000933 LoadWordDisp(res_reg, offset_of_string, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 StoreValue(rl_dest, rl_result);
935 }
936}
937
938/*
939 * Let helper function take care of everything. Will
940 * call Class::NewInstanceFromCode(type_idx, method);
941 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700942void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 FlushAllRegs(); /* Everything to home location */
944 // alloc will always check for resolution, do we also need to verify
945 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700946 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800947 const DexFile* dex_file = cu_->dex_file;
948 CompilerDriver* driver = cu_->compiler_driver;
949 if (driver->CanAccessInstantiableTypeWithoutChecks(
950 cu_->method_idx, *dex_file, type_idx)) {
951 bool is_type_initialized;
952 bool use_direct_type_ptr;
953 uintptr_t direct_type_ptr;
954 if (kEmbedClassInCode &&
955 driver->CanEmbedTypeInCode(*dex_file, type_idx,
956 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
957 // The fast path.
958 if (!use_direct_type_ptr) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800959 LoadClassType(type_idx, kArg0);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800960 if (!is_type_initialized) {
961 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
962 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
963 } else {
964 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
965 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
966 }
967 } else {
968 // Use the direct pointer.
969 if (!is_type_initialized) {
970 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
971 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
972 } else {
973 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
974 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
975 }
976 }
977 } else {
978 // The slow path.
979 DCHECK_EQ(func_offset.Int32Value(), -1);
980 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
981 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
982 }
983 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700985 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800986 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 RegLocation rl_result = GetReturn(false);
989 StoreValue(rl_dest, rl_result);
990}
991
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700992void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -0700994 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995}
996
997// For final classes there are no sub-classes to check and so we can answer the instance-of
998// question with simple comparisons.
999void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1000 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -08001001 // X86 has its own implementation.
1002 DCHECK_NE(cu_->instruction_set, kX86);
1003
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 RegLocation object = LoadValue(rl_src, kCoreReg);
1005 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001006 int result_reg = rl_result.reg.GetReg();
1007 if (result_reg == object.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 result_reg = AllocTypedTemp(false, kCoreReg);
1009 }
1010 LoadConstant(result_reg, 0); // assume false
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001011 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.reg.GetReg(), 0, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012
1013 int check_class = AllocTypedTemp(false, kCoreReg);
1014 int object_class = AllocTypedTemp(false, kCoreReg);
1015
1016 LoadCurrMethodDirect(check_class);
1017 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001018 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001020 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -07001022 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 check_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001024 LoadWordDisp(object.reg.GetReg(), mirror::Object::ClassOffset().Int32Value(), object_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 int32_t offset_of_type =
1026 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1027 (sizeof(mirror::Class*) * type_idx);
1028 LoadWordDisp(check_class, offset_of_type, check_class);
1029 }
1030
1031 LIR* ne_branchover = NULL;
1032 if (cu_->instruction_set == kThumb2) {
1033 OpRegReg(kOpCmp, check_class, object_class); // Same?
1034 OpIT(kCondEq, ""); // if-convert the test
1035 LoadConstant(result_reg, 1); // .eq case - load true
1036 } else {
1037 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1038 LoadConstant(result_reg, 1); // eq case - load true
1039 }
1040 LIR* target = NewLIR0(kPseudoTargetLabel);
1041 null_branchover->target = target;
1042 if (ne_branchover != NULL) {
1043 ne_branchover->target = target;
1044 }
1045 FreeTemp(object_class);
1046 FreeTemp(check_class);
1047 if (IsTemp(result_reg)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001048 OpRegCopy(rl_result.reg.GetReg(), result_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 FreeTemp(result_reg);
1050 }
1051 StoreValue(rl_dest, rl_result);
1052}
1053
1054void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1055 bool type_known_abstract, bool use_declaring_class,
1056 bool can_assume_type_is_in_dex_cache,
1057 uint32_t type_idx, RegLocation rl_dest,
1058 RegLocation rl_src) {
Mark Mendell6607d972014-02-10 06:54:18 -08001059 // X86 has its own implementation.
1060 DCHECK_NE(cu_->instruction_set, kX86);
1061
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 FlushAllRegs();
1063 // May generate a call - use explicit registers
1064 LockCallTemps();
1065 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1066 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1067 if (needs_access_check) {
1068 // Check we have access to type_idx and if not throw IllegalAccessError,
1069 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001070 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 type_idx, true);
1072 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1073 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1074 } else if (use_declaring_class) {
1075 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1076 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001077 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 } else {
1079 // Load dex cache entry into class_reg (kArg2)
1080 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1081 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001082 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 int32_t offset_of_type =
1084 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1085 * type_idx);
1086 LoadWordDisp(class_reg, offset_of_type, class_reg);
1087 if (!can_assume_type_is_in_dex_cache) {
1088 // Need to test presence of type in dex cache at runtime
1089 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1090 // Not resolved
1091 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001092 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001093 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1095 // Rejoin code paths
1096 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1097 hop_branch->target = hop_target;
1098 }
1099 }
1100 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1101 RegLocation rl_result = GetReturn(false);
1102 if (cu_->instruction_set == kMips) {
1103 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001104 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 }
1106 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1107
1108 /* load object->klass_ */
1109 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1110 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1111 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1112 LIR* branchover = NULL;
1113 if (type_known_final) {
1114 // rl_result == ref == null == 0.
1115 if (cu_->instruction_set == kThumb2) {
1116 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1117 OpIT(kCondEq, "E"); // if-convert the test
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001118 LoadConstant(rl_result.reg.GetReg(), 1); // .eq case - load true
1119 LoadConstant(rl_result.reg.GetReg(), 0); // .ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001121 LoadConstant(rl_result.reg.GetReg(), 0); // ne case - load false
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001123 LoadConstant(rl_result.reg.GetReg(), 1); // eq case - load true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 }
1125 } else {
1126 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001127 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 if (!type_known_abstract) {
1129 /* Uses conditional nullification */
1130 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1131 OpIT(kCondEq, "EE"); // if-convert the test
1132 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1133 }
1134 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1135 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1136 FreeTemp(r_tgt);
1137 } else {
1138 if (!type_known_abstract) {
1139 /* Uses branchovers */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001140 LoadConstant(rl_result.reg.GetReg(), 1); // assume true
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1142 }
Mark Mendell6607d972014-02-10 06:54:18 -08001143 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1144 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1145 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1146 FreeTemp(r_tgt);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148 }
1149 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001150 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 /* branch targets here */
1152 LIR* target = NewLIR0(kPseudoTargetLabel);
1153 StoreValue(rl_dest, rl_result);
1154 branch1->target = target;
1155 if (branchover != NULL) {
1156 branchover->target = target;
1157 }
1158}
1159
1160void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1161 bool type_known_final, type_known_abstract, use_declaring_class;
1162 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1163 *cu_->dex_file,
1164 type_idx,
1165 &type_known_final,
1166 &type_known_abstract,
1167 &use_declaring_class);
1168 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1169 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1170
1171 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1172 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1173 } else {
1174 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1175 use_declaring_class, can_assume_type_is_in_dex_cache,
1176 type_idx, rl_dest, rl_src);
1177 }
1178}
1179
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001180void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 bool type_known_final, type_known_abstract, use_declaring_class;
1182 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1183 *cu_->dex_file,
1184 type_idx,
1185 &type_known_final,
1186 &type_known_abstract,
1187 &use_declaring_class);
1188 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1189 // of the exception throw path.
1190 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
Vladimir Marko2730db02014-01-27 11:15:17 +00001191 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 // Verifier type analysis proved this check cast would never cause an exception.
1193 return;
1194 }
1195 FlushAllRegs();
1196 // May generate a call - use explicit registers
1197 LockCallTemps();
1198 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1199 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1200 if (needs_access_check) {
1201 // Check we have access to type_idx and if not throw IllegalAccessError,
1202 // returns Class* in kRet0
1203 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001204 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 type_idx, TargetReg(kArg1), true);
1206 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1207 } else if (use_declaring_class) {
1208 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001209 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 } else {
1211 // Load dex cache entry into class_reg (kArg2)
1212 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001213 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 int32_t offset_of_type =
1215 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1216 (sizeof(mirror::Class*) * type_idx);
1217 LoadWordDisp(class_reg, offset_of_type, class_reg);
1218 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1219 // Need to test presence of type in dex cache at runtime
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001220 LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1221 LIR* cont = NewLIR0(kPseudoTargetLabel);
1222
1223 // Slow path to initialize the type. Executed if the type is NULL.
1224 class SlowPath : public LIRSlowPath {
1225 public:
1226 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1227 const int class_reg) :
1228 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1229 class_reg_(class_reg) {
1230 }
1231
1232 void Compile() {
1233 GenerateTargetLabel();
1234
1235 // Call out to helper, which will return resolved type in kArg0
1236 // InitializeTypeFromCode(idx, method)
1237 m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1238 m2l_->TargetReg(kArg1), true);
1239 m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0)); // Align usage with fast path
1240 m2l_->OpUnconditionalBranch(cont_);
1241 }
1242 public:
1243 const int type_idx_;
1244 const int class_reg_;
1245 };
1246
1247 AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1248 type_idx, class_reg));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 }
1250 }
1251 // At this point, class_reg (kArg2) has class
1252 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001253
1254 // Slow path for the case where the classes are not equal. In this case we need
1255 // to call a helper function to do the check.
1256 class SlowPath : public LIRSlowPath {
1257 public:
1258 SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1259 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1260 }
1261
1262 void Compile() {
1263 GenerateTargetLabel();
1264
1265 if (load_) {
1266 m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1267 m2l_->TargetReg(kArg1));
1268 }
1269 m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1270 m2l_->TargetReg(kArg1), true);
1271
1272 m2l_->OpUnconditionalBranch(cont_);
1273 }
1274
1275 private:
1276 bool load_;
1277 };
1278
1279 if (type_known_abstract) {
1280 // Easier case, run slow path if target is non-null (slow path will load from target)
1281 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1282 LIR* cont = NewLIR0(kPseudoTargetLabel);
1283 AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1284 } else {
1285 // Harder, more common case. We need to generate a forward branch over the load
1286 // if the target is null. If it's non-null we perform the load and branch to the
1287 // slow path if the classes are not equal.
1288
1289 /* Null is OK - continue */
1290 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1291 /* load object->klass_ */
1292 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1293 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1294 TargetReg(kArg1));
1295
1296 LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1297 LIR* cont = NewLIR0(kPseudoTargetLabel);
1298
1299 // Add the slow path that will not perform load since this is already done.
1300 AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1301
1302 // Set the null check to branch to the continuation.
1303 branch1->target = cont;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 }
1305}
1306
1307void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001308 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309 RegLocation rl_result;
1310 if (cu_->instruction_set == kThumb2) {
1311 /*
1312 * NOTE: This is the one place in the code in which we might have
1313 * as many as six live temporary registers. There are 5 in the normal
1314 * set for Arm. Until we have spill capabilities, temporarily add
1315 * lr to the temp set. It is safe to do this locally, but note that
1316 * lr is used explicitly elsewhere in the code generator and cannot
1317 * normally be used as a general temp register.
1318 */
1319 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1320 FreeTemp(TargetReg(kLr)); // and make it available
1321 }
1322 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1323 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1324 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1325 // The longs may overlap - use intermediate temp if so
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001326 if ((rl_result.reg.GetReg() == rl_src1.reg.GetHighReg()) || (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001328 OpRegRegReg(first_op, t_reg, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1329 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1330 OpRegCopy(rl_result.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 FreeTemp(t_reg);
1332 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001333 OpRegRegReg(first_op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
1334 OpRegRegReg(second_op, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(),
1335 rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001336 }
1337 /*
1338 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1339 * following StoreValueWide might need to allocate a temp register.
1340 * To further work around the lack of a spill capability, explicitly
1341 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1342 * Remove when spill is functional.
1343 */
1344 FreeRegLocTemps(rl_result, rl_src1);
1345 FreeRegLocTemps(rl_result, rl_src2);
1346 StoreValueWide(rl_dest, rl_result);
1347 if (cu_->instruction_set == kThumb2) {
1348 Clobber(TargetReg(kLr));
1349 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1350 }
1351}
1352
1353
1354void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001355 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001356 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357
1358 switch (opcode) {
1359 case Instruction::SHL_LONG:
1360 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001361 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 break;
1363 case Instruction::SHR_LONG:
1364 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001365 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 break;
1367 case Instruction::USHR_LONG:
1368 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001369 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 break;
1371 default:
1372 LOG(FATAL) << "Unexpected case";
1373 }
1374 FlushAllRegs(); /* Send everything to home location */
1375 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1376 RegLocation rl_result = GetReturnWide(false);
1377 StoreValueWide(rl_dest, rl_result);
1378}
1379
1380
1381void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001382 RegLocation rl_src1, RegLocation rl_src2) {
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001383 DCHECK_NE(cu_->instruction_set, kX86);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 OpKind op = kOpBkpt;
1385 bool is_div_rem = false;
1386 bool check_zero = false;
1387 bool unary = false;
1388 RegLocation rl_result;
1389 bool shift_op = false;
1390 switch (opcode) {
1391 case Instruction::NEG_INT:
1392 op = kOpNeg;
1393 unary = true;
1394 break;
1395 case Instruction::NOT_INT:
1396 op = kOpMvn;
1397 unary = true;
1398 break;
1399 case Instruction::ADD_INT:
1400 case Instruction::ADD_INT_2ADDR:
1401 op = kOpAdd;
1402 break;
1403 case Instruction::SUB_INT:
1404 case Instruction::SUB_INT_2ADDR:
1405 op = kOpSub;
1406 break;
1407 case Instruction::MUL_INT:
1408 case Instruction::MUL_INT_2ADDR:
1409 op = kOpMul;
1410 break;
1411 case Instruction::DIV_INT:
1412 case Instruction::DIV_INT_2ADDR:
1413 check_zero = true;
1414 op = kOpDiv;
1415 is_div_rem = true;
1416 break;
1417 /* NOTE: returns in kArg1 */
1418 case Instruction::REM_INT:
1419 case Instruction::REM_INT_2ADDR:
1420 check_zero = true;
1421 op = kOpRem;
1422 is_div_rem = true;
1423 break;
1424 case Instruction::AND_INT:
1425 case Instruction::AND_INT_2ADDR:
1426 op = kOpAnd;
1427 break;
1428 case Instruction::OR_INT:
1429 case Instruction::OR_INT_2ADDR:
1430 op = kOpOr;
1431 break;
1432 case Instruction::XOR_INT:
1433 case Instruction::XOR_INT_2ADDR:
1434 op = kOpXor;
1435 break;
1436 case Instruction::SHL_INT:
1437 case Instruction::SHL_INT_2ADDR:
1438 shift_op = true;
1439 op = kOpLsl;
1440 break;
1441 case Instruction::SHR_INT:
1442 case Instruction::SHR_INT_2ADDR:
1443 shift_op = true;
1444 op = kOpAsr;
1445 break;
1446 case Instruction::USHR_INT:
1447 case Instruction::USHR_INT_2ADDR:
1448 shift_op = true;
1449 op = kOpLsr;
1450 break;
1451 default:
1452 LOG(FATAL) << "Invalid word arith op: " << opcode;
1453 }
1454 if (!is_div_rem) {
1455 if (unary) {
1456 rl_src1 = LoadValue(rl_src1, kCoreReg);
1457 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001458 OpRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 } else {
1460 if (shift_op) {
1461 int t_reg = INVALID_REG;
Mark Mendellfeb2b4e2014-01-28 12:59:49 -08001462 rl_src2 = LoadValue(rl_src2, kCoreReg);
1463 t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001464 OpRegRegImm(kOpAnd, t_reg, rl_src2.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 rl_src1 = LoadValue(rl_src1, kCoreReg);
1466 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001467 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 FreeTemp(t_reg);
1469 } else {
1470 rl_src1 = LoadValue(rl_src1, kCoreReg);
1471 rl_src2 = LoadValue(rl_src2, kCoreReg);
1472 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001473 OpRegRegReg(op, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 }
1475 }
1476 StoreValue(rl_dest, rl_result);
1477 } else {
Dave Allison70202782013-10-22 17:52:19 -07001478 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 -07001479 if (cu_->instruction_set == kMips) {
1480 rl_src1 = LoadValue(rl_src1, kCoreReg);
1481 rl_src2 = LoadValue(rl_src2, kCoreReg);
1482 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001483 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001484 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001485 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001486 done = true;
1487 } else if (cu_->instruction_set == kThumb2) {
1488 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1489 // Use ARM SDIV instruction for division. For remainder we also need to
1490 // calculate using a MUL and subtract.
1491 rl_src1 = LoadValue(rl_src1, kCoreReg);
1492 rl_src2 = LoadValue(rl_src2, kCoreReg);
1493 if (check_zero) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001494 GenImmedCheck(kCondEq, rl_src2.reg.GetReg(), 0, kThrowDivZero);
Dave Allison70202782013-10-22 17:52:19 -07001495 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001496 rl_result = GenDivRem(rl_dest, rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001497 done = true;
1498 }
1499 }
1500
1501 // If we haven't already generated the code use the callout function.
1502 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001503 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 FlushAllRegs(); /* Send everything to home location */
1505 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1506 int r_tgt = CallHelperSetup(func_offset);
1507 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1508 if (check_zero) {
1509 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1510 }
Dave Allison70202782013-10-22 17:52:19 -07001511 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001512 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 if (op == kOpDiv)
1514 rl_result = GetReturn(false);
1515 else
1516 rl_result = GetReturnAlt();
1517 }
1518 StoreValue(rl_dest, rl_result);
1519 }
1520}
1521
1522/*
1523 * The following are the first-level codegen routines that analyze the format
1524 * of each bytecode then either dispatch special purpose codegen routines
1525 * or produce corresponding Thumb instructions directly.
1526 */
1527
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001529static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 x &= x - 1;
1531 return (x & (x - 1)) == 0;
1532}
1533
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1535// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001536bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001537 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1539 return false;
1540 }
1541 // No divide instruction for Arm, so check for more special cases
1542 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001543 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 }
1545 int k = LowestSetBit(lit);
1546 if (k >= 30) {
1547 // Avoid special cases.
1548 return false;
1549 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 rl_src = LoadValue(rl_src, kCoreReg);
1551 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001552 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 int t_reg = AllocTemp();
1554 if (lit == 2) {
1555 // Division by 2 is by far the most common division by constant.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001556 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), 32 - k);
1557 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1558 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001560 OpRegRegImm(kOpAsr, t_reg, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001562 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.reg.GetReg());
1563 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), t_reg, k);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001564 }
1565 } else {
1566 int t_reg1 = AllocTemp();
1567 int t_reg2 = AllocTemp();
1568 if (lit == 2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001569 OpRegRegImm(kOpLsr, t_reg1, rl_src.reg.GetReg(), 32 - k);
1570 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001572 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001574 OpRegRegImm(kOpAsr, t_reg1, rl_src.reg.GetReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001576 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001578 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg2, t_reg1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 }
1580 }
1581 StoreValue(rl_dest, rl_result);
1582 return true;
1583}
1584
1585// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1586// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001587bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 // Can we simplify this multiplication?
1589 bool power_of_two = false;
1590 bool pop_count_le2 = false;
1591 bool power_of_two_minus_one = false;
1592 if (lit < 2) {
1593 // Avoid special cases.
1594 return false;
1595 } else if (IsPowerOfTwo(lit)) {
1596 power_of_two = true;
1597 } else if (IsPopCountLE2(lit)) {
1598 pop_count_le2 = true;
1599 } else if (IsPowerOfTwo(lit + 1)) {
1600 power_of_two_minus_one = true;
1601 } else {
1602 return false;
1603 }
1604 rl_src = LoadValue(rl_src, kCoreReg);
1605 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1606 if (power_of_two) {
1607 // Shift.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001608 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), LowestSetBit(lit));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 } else if (pop_count_le2) {
1610 // Shift and add and shift.
1611 int first_bit = LowestSetBit(lit);
1612 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1613 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1614 } else {
1615 // Reverse subtract: (src << (shift + 1)) - src.
1616 DCHECK(power_of_two_minus_one);
1617 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1618 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001619 OpRegRegImm(kOpLsl, t_reg, rl_src.reg.GetReg(), LowestSetBit(lit + 1));
1620 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001621 }
1622 StoreValue(rl_dest, rl_result);
1623 return true;
1624}
1625
1626void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001627 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 RegLocation rl_result;
1629 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1630 int shift_op = false;
1631 bool is_div = false;
1632
1633 switch (opcode) {
1634 case Instruction::RSUB_INT_LIT8:
1635 case Instruction::RSUB_INT: {
1636 rl_src = LoadValue(rl_src, kCoreReg);
1637 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1638 if (cu_->instruction_set == kThumb2) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001639 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001641 OpRegReg(kOpNeg, rl_result.reg.GetReg(), rl_src.reg.GetReg());
1642 OpRegImm(kOpAdd, rl_result.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 }
1644 StoreValue(rl_dest, rl_result);
1645 return;
1646 }
1647
1648 case Instruction::SUB_INT:
1649 case Instruction::SUB_INT_2ADDR:
1650 lit = -lit;
1651 // Intended fallthrough
1652 case Instruction::ADD_INT:
1653 case Instruction::ADD_INT_2ADDR:
1654 case Instruction::ADD_INT_LIT8:
1655 case Instruction::ADD_INT_LIT16:
1656 op = kOpAdd;
1657 break;
1658 case Instruction::MUL_INT:
1659 case Instruction::MUL_INT_2ADDR:
1660 case Instruction::MUL_INT_LIT8:
1661 case Instruction::MUL_INT_LIT16: {
1662 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1663 return;
1664 }
1665 op = kOpMul;
1666 break;
1667 }
1668 case Instruction::AND_INT:
1669 case Instruction::AND_INT_2ADDR:
1670 case Instruction::AND_INT_LIT8:
1671 case Instruction::AND_INT_LIT16:
1672 op = kOpAnd;
1673 break;
1674 case Instruction::OR_INT:
1675 case Instruction::OR_INT_2ADDR:
1676 case Instruction::OR_INT_LIT8:
1677 case Instruction::OR_INT_LIT16:
1678 op = kOpOr;
1679 break;
1680 case Instruction::XOR_INT:
1681 case Instruction::XOR_INT_2ADDR:
1682 case Instruction::XOR_INT_LIT8:
1683 case Instruction::XOR_INT_LIT16:
1684 op = kOpXor;
1685 break;
1686 case Instruction::SHL_INT_LIT8:
1687 case Instruction::SHL_INT:
1688 case Instruction::SHL_INT_2ADDR:
1689 lit &= 31;
1690 shift_op = true;
1691 op = kOpLsl;
1692 break;
1693 case Instruction::SHR_INT_LIT8:
1694 case Instruction::SHR_INT:
1695 case Instruction::SHR_INT_2ADDR:
1696 lit &= 31;
1697 shift_op = true;
1698 op = kOpAsr;
1699 break;
1700 case Instruction::USHR_INT_LIT8:
1701 case Instruction::USHR_INT:
1702 case Instruction::USHR_INT_2ADDR:
1703 lit &= 31;
1704 shift_op = true;
1705 op = kOpLsr;
1706 break;
1707
1708 case Instruction::DIV_INT:
1709 case Instruction::DIV_INT_2ADDR:
1710 case Instruction::DIV_INT_LIT8:
1711 case Instruction::DIV_INT_LIT16:
1712 case Instruction::REM_INT:
1713 case Instruction::REM_INT_2ADDR:
1714 case Instruction::REM_INT_LIT8:
1715 case Instruction::REM_INT_LIT16: {
1716 if (lit == 0) {
1717 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1718 return;
1719 }
buzbee11b63d12013-08-27 07:34:17 -07001720 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001721 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001722 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001723 (opcode == Instruction::DIV_INT_LIT16)) {
1724 is_div = true;
1725 } else {
1726 is_div = false;
1727 }
buzbee11b63d12013-08-27 07:34:17 -07001728 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1729 return;
1730 }
Dave Allison70202782013-10-22 17:52:19 -07001731
1732 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001733 if (cu_->instruction_set == kMips) {
1734 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001735 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001736 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001737 } else if (cu_->instruction_set == kX86) {
1738 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1739 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001740 } else if (cu_->instruction_set == kThumb2) {
1741 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1742 // Use ARM SDIV instruction for division. For remainder we also need to
1743 // calculate using a MUL and subtract.
1744 rl_src = LoadValue(rl_src, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001745 rl_result = GenDivRemLit(rl_dest, rl_src.reg.GetReg(), lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001746 done = true;
1747 }
1748 }
1749
1750 if (!done) {
1751 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001752 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1753 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001754 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001755 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1756 if (is_div)
1757 rl_result = GetReturn(false);
1758 else
1759 rl_result = GetReturnAlt();
1760 }
1761 StoreValue(rl_dest, rl_result);
1762 return;
1763 }
1764 default:
1765 LOG(FATAL) << "Unexpected opcode " << opcode;
1766 }
1767 rl_src = LoadValue(rl_src, kCoreReg);
1768 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001769 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001770 if (shift_op && (lit == 0)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001771 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001772 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001773 OpRegRegImm(op, rl_result.reg.GetReg(), rl_src.reg.GetReg(), lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 }
1775 StoreValue(rl_dest, rl_result);
1776}
1777
1778void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001779 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001780 RegLocation rl_result;
1781 OpKind first_op = kOpBkpt;
1782 OpKind second_op = kOpBkpt;
1783 bool call_out = false;
1784 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001785 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001786 int ret_reg = TargetReg(kRet0);
1787
1788 switch (opcode) {
1789 case Instruction::NOT_LONG:
1790 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1791 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1792 // Check for destructive overlap
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001793 if (rl_result.reg.GetReg() == rl_src2.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001794 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001795 OpRegCopy(t_reg, rl_src2.reg.GetHighReg());
1796 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1797 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001798 FreeTemp(t_reg);
1799 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001800 OpRegReg(kOpMvn, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
1801 OpRegReg(kOpMvn, rl_result.reg.GetHighReg(), rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001802 }
1803 StoreValueWide(rl_dest, rl_result);
1804 return;
1805 case Instruction::ADD_LONG:
1806 case Instruction::ADD_LONG_2ADDR:
1807 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001808 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001809 return;
1810 }
1811 first_op = kOpAdd;
1812 second_op = kOpAdc;
1813 break;
1814 case Instruction::SUB_LONG:
1815 case Instruction::SUB_LONG_2ADDR:
1816 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001817 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001818 return;
1819 }
1820 first_op = kOpSub;
1821 second_op = kOpSbc;
1822 break;
1823 case Instruction::MUL_LONG:
1824 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001825 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001826 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001827 return;
1828 } else {
1829 call_out = true;
1830 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001831 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001832 }
1833 break;
1834 case Instruction::DIV_LONG:
1835 case Instruction::DIV_LONG_2ADDR:
1836 call_out = true;
1837 check_zero = true;
1838 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001839 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001840 break;
1841 case Instruction::REM_LONG:
1842 case Instruction::REM_LONG_2ADDR:
1843 call_out = true;
1844 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001845 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001846 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1847 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1848 break;
1849 case Instruction::AND_LONG_2ADDR:
1850 case Instruction::AND_LONG:
1851 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001852 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001853 }
1854 first_op = kOpAnd;
1855 second_op = kOpAnd;
1856 break;
1857 case Instruction::OR_LONG:
1858 case Instruction::OR_LONG_2ADDR:
1859 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001860 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001861 return;
1862 }
1863 first_op = kOpOr;
1864 second_op = kOpOr;
1865 break;
1866 case Instruction::XOR_LONG:
1867 case Instruction::XOR_LONG_2ADDR:
1868 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001869 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001870 return;
1871 }
1872 first_op = kOpXor;
1873 second_op = kOpXor;
1874 break;
1875 case Instruction::NEG_LONG: {
1876 GenNegLong(rl_dest, rl_src2);
1877 return;
1878 }
1879 default:
1880 LOG(FATAL) << "Invalid long arith op";
1881 }
1882 if (!call_out) {
1883 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1884 } else {
1885 FlushAllRegs(); /* Send everything to home location */
1886 if (check_zero) {
1887 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1888 int r_tgt = CallHelperSetup(func_offset);
1889 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1890 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1891 // NOTE: callout here is not a safepoint
1892 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1893 } else {
1894 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1895 }
1896 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1897 if (ret_reg == TargetReg(kRet0))
1898 rl_result = GetReturnWide(false);
1899 else
1900 rl_result = GetReturnWideAlt();
1901 StoreValueWide(rl_dest, rl_result);
1902 }
1903}
1904
Ian Rogers848871b2013-08-05 10:56:33 -07001905void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001906 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001907 /*
1908 * Don't optimize the register usage since it calls out to support
1909 * functions
1910 */
1911 FlushAllRegs(); /* Send everything to home location */
1912 if (rl_src.wide) {
1913 LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1914 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1915 } else {
1916 LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1917 }
1918 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1919 if (rl_dest.wide) {
1920 RegLocation rl_result;
1921 rl_result = GetReturnWide(rl_dest.fp);
1922 StoreValueWide(rl_dest, rl_result);
1923 } else {
1924 RegLocation rl_result;
1925 rl_result = GetReturn(rl_dest.fp);
1926 StoreValue(rl_dest, rl_result);
1927 }
1928}
1929
1930/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001931void Mir2Lir::GenSuspendTest(int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001932 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1933 return;
1934 }
1935 FlushAllRegs();
1936 LIR* branch = OpTestSuspend(NULL);
1937 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001938 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1939 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001940 branch->target = target;
1941 suspend_launchpads_.Insert(target);
1942}
1943
1944/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001945void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001946 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1947 OpUnconditionalBranch(target);
1948 return;
1949 }
1950 OpTestSuspend(target);
1951 LIR* launch_pad =
buzbee0d829482013-10-11 15:24:55 -07001952 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1953 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001954 FlushAllRegs();
1955 OpUnconditionalBranch(launch_pad);
1956 suspend_launchpads_.Insert(launch_pad);
1957}
1958
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001959/* Call out to helper assembly routine that will null check obj and then lock it. */
1960void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1961 FlushAllRegs();
1962 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
1963}
1964
1965/* Call out to helper assembly routine that will null check obj and then unlock it. */
1966void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
1967 FlushAllRegs();
1968 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
1969}
1970
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001971/* Generic code for generating a wide constant into a VR. */
1972void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
1973 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001974 LoadConstantWide(rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), value);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001975 StoreValueWide(rl_dest, rl_result);
1976}
1977
Brian Carlstrom7940e442013-07-12 13:46:57 -07001978} // namespace art