blob: ffe85af51e2be1d60a823b04f1090bbf2bada32e [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"
24
25namespace art {
26
27/*
28 * This source files contains "gen" codegen routines that should
29 * be applicable to most targets. Only mid-level support utilities
30 * and "op" calls may be used here.
31 */
32
33/*
buzbeeb48819d2013-09-14 16:15:25 -070034 * Generate a kPseudoBarrier marker to indicate the boundary of special
Brian Carlstrom7940e442013-07-12 13:46:57 -070035 * blocks.
36 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070037void Mir2Lir::GenBarrier() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 LIR* barrier = NewLIR0(kPseudoBarrier);
39 /* Mark all resources as being clobbered */
buzbeeb48819d2013-09-14 16:15:25 -070040 DCHECK(!barrier->flags.use_def_invalid);
41 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070042}
43
buzbee0d829482013-10-11 15:24:55 -070044// TODO: need to do some work to split out targets with
Brian Carlstrom7940e442013-07-12 13:46:57 -070045// condition codes and those without
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070046LIR* Mir2Lir::GenCheck(ConditionCode c_code, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 DCHECK_NE(cu_->instruction_set, kMips);
48 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_);
49 LIR* branch = OpCondBranch(c_code, tgt);
50 // Remember branch target - will process later
51 throw_launchpads_.Insert(tgt);
52 return branch;
53}
54
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070055LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, int reg, int imm_val, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg, imm_val);
57 LIR* branch;
58 if (c_code == kCondAl) {
59 branch = OpUnconditionalBranch(tgt);
60 } else {
61 branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
62 }
63 // Remember branch target - will process later
64 throw_launchpads_.Insert(tgt);
65 return branch;
66}
67
68/* Perform null-check on a register. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070069LIR* Mir2Lir::GenNullCheck(int s_reg, int m_reg, int opt_flags) {
Ian Rogersa9a82542013-10-04 11:17:26 -070070 if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 return NULL;
72 }
73 return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
74}
75
76/* Perform check on two registers */
77LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070078 ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
80 LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
81 // Remember branch target - will process later
82 throw_launchpads_.Insert(tgt);
83 return branch;
84}
85
86void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
87 RegLocation rl_src2, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070088 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 ConditionCode cond;
90 switch (opcode) {
91 case Instruction::IF_EQ:
92 cond = kCondEq;
93 break;
94 case Instruction::IF_NE:
95 cond = kCondNe;
96 break;
97 case Instruction::IF_LT:
98 cond = kCondLt;
99 break;
100 case Instruction::IF_GE:
101 cond = kCondGe;
102 break;
103 case Instruction::IF_GT:
104 cond = kCondGt;
105 break;
106 case Instruction::IF_LE:
107 cond = kCondLe;
108 break;
109 default:
110 cond = static_cast<ConditionCode>(0);
111 LOG(FATAL) << "Unexpected opcode " << opcode;
112 }
113
114 // Normalize such that if either operand is constant, src2 will be constant
115 if (rl_src1.is_const) {
116 RegLocation rl_temp = rl_src1;
117 rl_src1 = rl_src2;
118 rl_src2 = rl_temp;
119 cond = FlipComparisonOrder(cond);
120 }
121
122 rl_src1 = LoadValue(rl_src1, kCoreReg);
123 // Is this really an immediate comparison?
124 if (rl_src2.is_const) {
125 // If it's already live in a register or not easily materialized, just keep going
126 RegLocation rl_temp = UpdateLoc(rl_src2);
127 if ((rl_temp.location == kLocDalvikFrame) &&
128 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
129 // OK - convert this to a compare immediate and branch
130 OpCmpImmBranch(cond, rl_src1.low_reg, mir_graph_->ConstantValue(rl_src2), taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 return;
132 }
133 }
134 rl_src2 = LoadValue(rl_src2, kCoreReg);
135 OpCmpBranch(cond, rl_src1.low_reg, rl_src2.low_reg, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136}
137
138void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700139 LIR* fall_through) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 ConditionCode cond;
141 rl_src = LoadValue(rl_src, kCoreReg);
142 switch (opcode) {
143 case Instruction::IF_EQZ:
144 cond = kCondEq;
145 break;
146 case Instruction::IF_NEZ:
147 cond = kCondNe;
148 break;
149 case Instruction::IF_LTZ:
150 cond = kCondLt;
151 break;
152 case Instruction::IF_GEZ:
153 cond = kCondGe;
154 break;
155 case Instruction::IF_GTZ:
156 cond = kCondGt;
157 break;
158 case Instruction::IF_LEZ:
159 cond = kCondLe;
160 break;
161 default:
162 cond = static_cast<ConditionCode>(0);
163 LOG(FATAL) << "Unexpected opcode " << opcode;
164 }
165 OpCmpImmBranch(cond, rl_src.low_reg, 0, taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166}
167
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
170 if (rl_src.location == kLocPhysReg) {
171 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
172 } else {
173 LoadValueDirect(rl_src, rl_result.low_reg);
174 }
175 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
176 StoreValueWide(rl_dest, rl_result);
177}
178
179void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180 RegLocation rl_src) {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700181 rl_src = LoadValue(rl_src, kCoreReg);
182 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
183 OpKind op = kOpInvalid;
184 switch (opcode) {
185 case Instruction::INT_TO_BYTE:
186 op = kOp2Byte;
187 break;
188 case Instruction::INT_TO_SHORT:
189 op = kOp2Short;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 break;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700191 case Instruction::INT_TO_CHAR:
192 op = kOp2Char;
193 break;
194 default:
195 LOG(ERROR) << "Bad int conversion type";
196 }
197 OpRegReg(op, rl_result.low_reg, rl_src.low_reg);
198 StoreValue(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199}
200
201/*
202 * Let helper function take care of everything. Will call
203 * Array::AllocFromCode(type_idx, method, count);
204 * Note: AllocFromCode will handle checks for errNegativeArraySize.
205 */
206void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700207 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700209 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
211 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700212 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700214 func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 }
216 CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
217 RegLocation rl_result = GetReturn(false);
218 StoreValue(rl_dest, rl_result);
219}
220
221/*
222 * Similar to GenNewArray, but with post-allocation initialization.
223 * Verifier guarantees we're dealing with an array class. Current
224 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
225 * Current code also throws internal unimp if not 'L', '[' or 'I'.
226 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700227void Mir2Lir::GenFilledNewArray(CallInfo* info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 int elems = info->num_arg_words;
229 int type_idx = info->index;
230 FlushAllRegs(); /* Everything to home location */
Ian Rogers848871b2013-08-05 10:56:33 -0700231 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
233 type_idx)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700234 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700236 func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 }
238 CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
239 FreeTemp(TargetReg(kArg2));
240 FreeTemp(TargetReg(kArg1));
241 /*
242 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
243 * return region. Because AllocFromCode placed the new array
244 * in kRet0, we'll just lock it into place. When debugger support is
245 * added, it may be necessary to additionally copy all return
246 * values to a home location in thread-local storage
247 */
248 LockTemp(TargetReg(kRet0));
249
250 // TODO: use the correct component size, currently all supported types
251 // share array alignment with ints (see comment at head of function)
252 size_t component_size = sizeof(int32_t);
253
254 // Having a range of 0 is legal
255 if (info->is_range && (elems > 0)) {
256 /*
257 * Bit of ugliness here. We're going generate a mem copy loop
258 * on the register range, but it is possible that some regs
259 * in the range have been promoted. This is unlikely, but
260 * before generating the copy, we'll just force a flush
261 * of any regs in the source range that have been promoted to
262 * home location.
263 */
264 for (int i = 0; i < elems; i++) {
265 RegLocation loc = UpdateLoc(info->args[i]);
266 if (loc.location == kLocPhysReg) {
267 StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
268 loc.low_reg, kWord);
269 }
270 }
271 /*
272 * TUNING note: generated code here could be much improved, but
273 * this is an uncommon operation and isn't especially performance
274 * critical.
275 */
276 int r_src = AllocTemp();
277 int r_dst = AllocTemp();
278 int r_idx = AllocTemp();
279 int r_val = INVALID_REG;
Brian Carlstromdf629502013-07-17 22:39:56 -0700280 switch (cu_->instruction_set) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 case kThumb2:
282 r_val = TargetReg(kLr);
283 break;
284 case kX86:
285 FreeTemp(TargetReg(kRet0));
286 r_val = AllocTemp();
287 break;
288 case kMips:
289 r_val = AllocTemp();
290 break;
291 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
292 }
293 // Set up source pointer
294 RegLocation rl_first = info->args[0];
295 OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
296 // Set up the target pointer
297 OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
298 mirror::Array::DataOffset(component_size).Int32Value());
299 // Set up the loop counter (known to be > 0)
300 LoadConstant(r_idx, elems - 1);
301 // Generate the copy loop. Going backwards for convenience
302 LIR* target = NewLIR0(kPseudoTargetLabel);
303 // Copy next element
304 LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
305 StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
306 FreeTemp(r_val);
307 OpDecAndBranch(kCondGe, r_idx, target);
308 if (cu_->instruction_set == kX86) {
309 // Restore the target pointer
310 OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
311 -mirror::Array::DataOffset(component_size).Int32Value());
312 }
313 } else if (!info->is_range) {
314 // TUNING: interleave
315 for (int i = 0; i < elems; i++) {
316 RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
317 StoreBaseDisp(TargetReg(kRet0),
318 mirror::Array::DataOffset(component_size).Int32Value() +
319 i * 4, rl_arg.low_reg, kWord);
320 // If the LoadValue caused a temp to be allocated, free it
321 if (IsTemp(rl_arg.low_reg)) {
322 FreeTemp(rl_arg.low_reg);
323 }
324 }
325 }
326 if (info->result.location != kLocInvalid) {
327 StoreValue(info->result, GetReturn(false /* not fp */));
328 }
329}
330
331void Mir2Lir::GenSput(uint32_t field_idx, RegLocation rl_src, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700332 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800334 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 bool is_volatile;
336 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800337 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700339 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), true,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800340 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 if (fast_path && !SLOW_FIELD_PATH) {
342 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800343 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 if (is_referrers_class) {
345 // Fast path, static storage base is this method's class
346 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800347 r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800349 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 if (IsTemp(rl_method.low_reg)) {
351 FreeTemp(rl_method.low_reg);
352 }
353 } else {
354 // Medium path, static storage base in a different class which requires checks that the other
355 // class is initialized.
356 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800357 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 // May do runtime call so everything to home locations.
359 FlushAllRegs();
360 // Using fixed register to sync with possible call to runtime support.
361 int r_method = TargetReg(kArg1);
362 LockTemp(r_method);
363 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800364 r_base = TargetReg(kArg0);
365 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800367 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
368 r_base);
369 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
370 sizeof(int32_t*) * storage_index, r_base);
371 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
372 if (!is_initialized) {
373 // Check if r_base is NULL or a not yet initialized class.
374 // TUNING: fast path should fall through
375 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
376 int r_tmp = TargetReg(kArg2);
377 LockTemp(r_tmp);
378 // TODO: Fuse the compare of a constant with memory on X86 and avoid the load.
379 LoadWordDisp(r_base, mirror::Class::StatusOffset().Int32Value(), r_tmp);
380 LIR* initialized_branch = OpCmpImmBranch(kCondGe, r_tmp, mirror::Class::kStatusInitialized,
381 NULL);
382
383 LIR* unresolved_target = NewLIR0(kPseudoTargetLabel);
384 unresolved_branch->target = unresolved_target;
385 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage), storage_index,
386 true);
387 // Copy helper's result into r_base, a no-op on all but MIPS.
388 OpRegCopy(r_base, TargetReg(kRet0));
389
390 LIR* initialized_target = NewLIR0(kPseudoTargetLabel);
391 initialized_branch->target = initialized_target;
392
393 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 FreeTemp(r_method);
396 }
397 // rBase now holds static storage base
398 if (is_long_or_double) {
399 rl_src = LoadValueWide(rl_src, kAnyReg);
400 } else {
401 rl_src = LoadValue(rl_src, kAnyReg);
402 }
403 if (is_volatile) {
404 GenMemBarrier(kStoreStore);
405 }
406 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800407 StoreBaseDispWide(r_base, field_offset, rl_src.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 rl_src.high_reg);
409 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800410 StoreWordDisp(r_base, field_offset, rl_src.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 }
412 if (is_volatile) {
413 GenMemBarrier(kStoreLoad);
414 }
415 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800416 MarkGCCard(rl_src.low_reg, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800418 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 } else {
420 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700421 ThreadOffset setter_offset =
422 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
423 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
424 : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 CallRuntimeHelperImmRegLocation(setter_offset, field_idx, rl_src, true);
426 }
427}
428
429void Mir2Lir::GenSget(uint32_t field_idx, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700430 bool is_long_or_double, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 int field_offset;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800432 int storage_index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 bool is_volatile;
434 bool is_referrers_class;
Ian Rogers5ddb4102014-01-07 08:58:46 -0800435 bool is_initialized;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -0700437 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), false,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800438 &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 if (fast_path && !SLOW_FIELD_PATH) {
440 DCHECK_GE(field_offset, 0);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800441 int r_base;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 if (is_referrers_class) {
443 // Fast path, static storage base is this method's class
444 RegLocation rl_method = LoadCurrMethod();
Ian Rogers5ddb4102014-01-07 08:58:46 -0800445 r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 LoadWordDisp(rl_method.low_reg,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800447 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 } else {
449 // Medium path, static storage base in a different class which requires checks that the other
450 // class is initialized
Ian Rogers5ddb4102014-01-07 08:58:46 -0800451 DCHECK_GE(storage_index, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 // May do runtime call so everything to home locations.
453 FlushAllRegs();
454 // Using fixed register to sync with possible call to runtime support.
455 int r_method = TargetReg(kArg1);
456 LockTemp(r_method);
457 LoadCurrMethodDirect(r_method);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800458 r_base = TargetReg(kArg0);
459 LockTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 LoadWordDisp(r_method,
Ian Rogers5ddb4102014-01-07 08:58:46 -0800461 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
462 r_base);
463 LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
464 sizeof(int32_t*) * storage_index, r_base);
465 // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
466 if (!is_initialized) {
467 // Check if r_base is NULL or a not yet initialized class.
468 // TUNING: fast path should fall through
469 LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
470 int r_tmp = TargetReg(kArg2);
471 LockTemp(r_tmp);
472 // TODO: Fuse the compare of a constant with memory on X86 and avoid the load.
473 LoadWordDisp(r_base, mirror::Class::StatusOffset().Int32Value(), r_tmp);
474 LIR* initialized_branch = OpCmpImmBranch(kCondGe, r_tmp, mirror::Class::kStatusInitialized,
475 NULL);
476
477 LIR* unresolved_target = NewLIR0(kPseudoTargetLabel);
478 unresolved_branch->target = unresolved_target;
479 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage), storage_index,
480 true);
481 // Copy helper's result into r_base, a no-op on all but MIPS.
482 OpRegCopy(r_base, TargetReg(kRet0));
483
484 LIR* initialized_target = NewLIR0(kPseudoTargetLabel);
485 initialized_branch->target = initialized_target;
486
487 FreeTemp(r_tmp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 FreeTemp(r_method);
490 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800491 // r_base now holds static storage base
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
493 if (is_volatile) {
494 GenMemBarrier(kLoadLoad);
495 }
496 if (is_long_or_double) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800497 LoadBaseDispWide(r_base, field_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 rl_result.high_reg, INVALID_SREG);
499 } else {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800500 LoadWordDisp(r_base, field_offset, rl_result.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 }
Ian Rogers5ddb4102014-01-07 08:58:46 -0800502 FreeTemp(r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 if (is_long_or_double) {
504 StoreValueWide(rl_dest, rl_result);
505 } else {
506 StoreValue(rl_dest, rl_result);
507 }
508 } else {
509 FlushAllRegs(); // Everything to home locations
Ian Rogers848871b2013-08-05 10:56:33 -0700510 ThreadOffset getterOffset =
511 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
512 :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
513 : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 CallRuntimeHelperImm(getterOffset, field_idx, true);
515 if (is_long_or_double) {
516 RegLocation rl_result = GetReturnWide(rl_dest.fp);
517 StoreValueWide(rl_dest, rl_result);
518 } else {
519 RegLocation rl_result = GetReturn(rl_dest.fp);
520 StoreValue(rl_dest, rl_result);
521 }
522 }
523}
524
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700525void Mir2Lir::HandleSuspendLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 int num_elems = suspend_launchpads_.Size();
Ian Rogers848871b2013-08-05 10:56:33 -0700527 ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 for (int i = 0; i < num_elems; i++) {
529 ResetRegPool();
530 ResetDefTracking();
531 LIR* lab = suspend_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700532 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 current_dalvik_offset_ = lab->operands[1];
534 AppendLIR(lab);
535 int r_tgt = CallHelperSetup(helper_offset);
536 CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
537 OpUnconditionalBranch(resume_lab);
538 }
539}
540
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700541void Mir2Lir::HandleIntrinsicLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 int num_elems = intrinsic_launchpads_.Size();
543 for (int i = 0; i < num_elems; i++) {
544 ResetRegPool();
545 ResetDefTracking();
546 LIR* lab = intrinsic_launchpads_.Get(i);
buzbee0d829482013-10-11 15:24:55 -0700547 CallInfo* info = reinterpret_cast<CallInfo*>(UnwrapPointer(lab->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 current_dalvik_offset_ = info->offset;
549 AppendLIR(lab);
550 // NOTE: GenInvoke handles MarkSafepointPC
551 GenInvoke(info);
buzbee0d829482013-10-11 15:24:55 -0700552 LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 if (resume_lab != NULL) {
554 OpUnconditionalBranch(resume_lab);
555 }
556 }
557}
558
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700559void Mir2Lir::HandleThrowLaunchPads() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 int num_elems = throw_launchpads_.Size();
561 for (int i = 0; i < num_elems; i++) {
562 ResetRegPool();
563 ResetDefTracking();
564 LIR* lab = throw_launchpads_.Get(i);
565 current_dalvik_offset_ = lab->operands[1];
566 AppendLIR(lab);
Ian Rogers848871b2013-08-05 10:56:33 -0700567 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 int v1 = lab->operands[2];
569 int v2 = lab->operands[3];
570 bool target_x86 = (cu_->instruction_set == kX86);
571 switch (lab->operands[0]) {
572 case kThrowNullPointer:
Ian Rogers848871b2013-08-05 10:56:33 -0700573 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700575 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
577 if (target_x86) {
578 OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
579 } else {
580 OpRegCopy(TargetReg(kArg1), v1);
581 }
582 // Make sure the following LoadConstant doesn't mess with kArg1.
583 LockTemp(TargetReg(kArg1));
584 LoadConstant(TargetReg(kArg0), v2);
Ian Rogers848871b2013-08-05 10:56:33 -0700585 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 break;
587 case kThrowArrayBounds:
588 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
589 if (v2 != TargetReg(kArg0)) {
590 OpRegCopy(TargetReg(kArg0), v1);
591 if (target_x86) {
592 // x86 leaves the array pointer in v2, so load the array length that the handler expects
593 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
594 } else {
595 OpRegCopy(TargetReg(kArg1), v2);
596 }
597 } else {
598 if (v1 == TargetReg(kArg1)) {
599 // Swap v1 and v2, using kArg2 as a temp
600 OpRegCopy(TargetReg(kArg2), v1);
601 if (target_x86) {
602 // x86 leaves the array pointer in v2; load the array length that the handler expects
603 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
604 } else {
605 OpRegCopy(TargetReg(kArg1), v2);
606 }
607 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
608 } else {
609 if (target_x86) {
610 // x86 leaves the array pointer in v2; load the array length that the handler expects
611 OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
612 } else {
613 OpRegCopy(TargetReg(kArg1), v2);
614 }
615 OpRegCopy(TargetReg(kArg0), v1);
616 }
617 }
Ian Rogers848871b2013-08-05 10:56:33 -0700618 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 break;
620 case kThrowDivZero:
Ian Rogers848871b2013-08-05 10:56:33 -0700621 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 break;
623 case kThrowNoSuchMethod:
624 OpRegCopy(TargetReg(kArg0), v1);
625 func_offset =
Ian Rogers848871b2013-08-05 10:56:33 -0700626 QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 break;
628 case kThrowStackOverflow:
Ian Rogers848871b2013-08-05 10:56:33 -0700629 func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 // Restore stack alignment
631 if (target_x86) {
632 OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
633 } else {
634 OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
635 }
636 break;
637 default:
638 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
639 }
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000640 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 int r_tgt = CallHelperSetup(func_offset);
642 CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
643 }
644}
645
646void Mir2Lir::GenIGet(uint32_t field_idx, int opt_flags, OpSize size,
647 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700648 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 int field_offset;
650 bool is_volatile;
651
Ian Rogers9b297bf2013-09-06 11:11:25 -0700652 bool fast_path = FastInstance(field_idx, false, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653
654 if (fast_path && !SLOW_FIELD_PATH) {
655 RegLocation rl_result;
656 RegisterClass reg_class = oat_reg_class_by_size(size);
657 DCHECK_GE(field_offset, 0);
658 rl_obj = LoadValue(rl_obj, kCoreReg);
659 if (is_long_or_double) {
660 DCHECK(rl_dest.wide);
661 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
662 if (cu_->instruction_set == kX86) {
663 rl_result = EvalLoc(rl_dest, reg_class, true);
664 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
665 LoadBaseDispWide(rl_obj.low_reg, field_offset, rl_result.low_reg,
666 rl_result.high_reg, rl_obj.s_reg_low);
667 if (is_volatile) {
668 GenMemBarrier(kLoadLoad);
669 }
670 } else {
671 int reg_ptr = AllocTemp();
672 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
673 rl_result = EvalLoc(rl_dest, reg_class, true);
674 LoadBaseDispWide(reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
675 if (is_volatile) {
676 GenMemBarrier(kLoadLoad);
677 }
678 FreeTemp(reg_ptr);
679 }
680 StoreValueWide(rl_dest, rl_result);
681 } else {
682 rl_result = EvalLoc(rl_dest, reg_class, true);
683 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
684 LoadBaseDisp(rl_obj.low_reg, field_offset, rl_result.low_reg,
685 kWord, rl_obj.s_reg_low);
686 if (is_volatile) {
687 GenMemBarrier(kLoadLoad);
688 }
689 StoreValue(rl_dest, rl_result);
690 }
691 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700692 ThreadOffset getterOffset =
693 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
694 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
695 : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 CallRuntimeHelperImmRegLocation(getterOffset, field_idx, rl_obj, true);
697 if (is_long_or_double) {
698 RegLocation rl_result = GetReturnWide(rl_dest.fp);
699 StoreValueWide(rl_dest, rl_result);
700 } else {
701 RegLocation rl_result = GetReturn(rl_dest.fp);
702 StoreValue(rl_dest, rl_result);
703 }
704 }
705}
706
707void Mir2Lir::GenIPut(uint32_t field_idx, int opt_flags, OpSize size,
708 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709 bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 int field_offset;
711 bool is_volatile;
712
Ian Rogers9b297bf2013-09-06 11:11:25 -0700713 bool fast_path = FastInstance(field_idx, true, &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 if (fast_path && !SLOW_FIELD_PATH) {
715 RegisterClass reg_class = oat_reg_class_by_size(size);
716 DCHECK_GE(field_offset, 0);
717 rl_obj = LoadValue(rl_obj, kCoreReg);
718 if (is_long_or_double) {
719 int reg_ptr;
720 rl_src = LoadValueWide(rl_src, kAnyReg);
721 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
722 reg_ptr = AllocTemp();
723 OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
724 if (is_volatile) {
725 GenMemBarrier(kStoreStore);
726 }
727 StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
728 if (is_volatile) {
729 GenMemBarrier(kLoadLoad);
730 }
731 FreeTemp(reg_ptr);
732 } else {
733 rl_src = LoadValue(rl_src, reg_class);
734 GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
735 if (is_volatile) {
736 GenMemBarrier(kStoreStore);
737 }
738 StoreBaseDisp(rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
739 if (is_volatile) {
740 GenMemBarrier(kLoadLoad);
741 }
742 if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
743 MarkGCCard(rl_src.low_reg, rl_obj.low_reg);
744 }
745 }
746 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700747 ThreadOffset setter_offset =
748 is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
749 : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
750 : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_idx, rl_obj, rl_src, true);
752 }
753}
754
Ian Rogersa9a82542013-10-04 11:17:26 -0700755void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
756 RegLocation rl_src) {
757 bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
758 bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
759 (opt_flags & MIR_IGNORE_NULL_CHECK));
760 ThreadOffset helper = needs_range_check
761 ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
762 : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
763 : QUICK_ENTRYPOINT_OFFSET(pAputObject);
764 CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
765}
766
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700767void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 RegLocation rl_method = LoadCurrMethod();
769 int res_reg = AllocTemp();
770 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
771 if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
772 *cu_->dex_file,
773 type_idx)) {
774 // Call out to helper which resolves type and verifies access.
775 // Resolved type returned in kRet0.
Ian Rogers848871b2013-08-05 10:56:33 -0700776 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 type_idx, rl_method.low_reg, true);
778 RegLocation rl_result = GetReturn(false);
779 StoreValue(rl_dest, rl_result);
780 } else {
781 // We're don't need access checks, load type from dex cache
782 int32_t dex_cache_offset =
Brian Carlstromea46f952013-07-30 01:26:50 -0700783 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 LoadWordDisp(rl_method.low_reg, dex_cache_offset, res_reg);
785 int32_t offset_of_type =
786 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
787 * type_idx);
788 LoadWordDisp(res_reg, offset_of_type, rl_result.low_reg);
789 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
790 type_idx) || SLOW_TYPE_PATH) {
791 // Slow path, at runtime test if type is null and if so initialize
792 FlushAllRegs();
793 LIR* branch1 = OpCmpImmBranch(kCondEq, rl_result.low_reg, 0, NULL);
794 // Resolved, store and hop over following code
795 StoreValue(rl_dest, rl_result);
796 /*
797 * Because we have stores of the target value on two paths,
798 * clobber temp tracking for the destination using the ssa name
799 */
800 ClobberSReg(rl_dest.s_reg_low);
801 LIR* branch2 = OpUnconditionalBranch(0);
802 // TUNING: move slow path to end & remove unconditional branch
803 LIR* target1 = NewLIR0(kPseudoTargetLabel);
804 // Call out to helper, which will return resolved type in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -0700805 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 rl_method.low_reg, true);
807 RegLocation rl_result = GetReturn(false);
808 StoreValue(rl_dest, rl_result);
809 /*
810 * Because we have stores of the target value on two paths,
811 * clobber temp tracking for the destination using the ssa name
812 */
813 ClobberSReg(rl_dest.s_reg_low);
814 // Rejoin code paths
815 LIR* target2 = NewLIR0(kPseudoTargetLabel);
816 branch1->target = target1;
817 branch2->target = target2;
818 } else {
819 // Fast path, we're done - just store result
820 StoreValue(rl_dest, rl_result);
821 }
822 }
823}
824
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700825void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 /* NOTE: Most strings should be available at compile time */
827 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
828 (sizeof(mirror::String*) * string_idx);
829 if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
830 *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
831 // slow path, resolve string if not in dex cache
832 FlushAllRegs();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700833 LockCallTemps(); // Using explicit registers
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834 LoadCurrMethodDirect(TargetReg(kArg2));
835 LoadWordDisp(TargetReg(kArg2),
Brian Carlstromea46f952013-07-30 01:26:50 -0700836 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 // Might call out to helper, which will return resolved string in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -0700838 int r_tgt = CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
840 LoadConstant(TargetReg(kArg1), string_idx);
841 if (cu_->instruction_set == kThumb2) {
842 OpRegImm(kOpCmp, TargetReg(kRet0), 0); // Is resolved?
843 GenBarrier();
844 // For testing, always force through helper
845 if (!EXERCISE_SLOWEST_STRING_PATH) {
846 OpIT(kCondEq, "T");
847 }
848 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .eq
849 LIR* call_inst = OpReg(kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
850 MarkSafepointPC(call_inst);
851 FreeTemp(r_tgt);
852 } else if (cu_->instruction_set == kMips) {
853 LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
854 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .eq
855 LIR* call_inst = OpReg(kOpBlx, r_tgt);
856 MarkSafepointPC(call_inst);
857 FreeTemp(r_tgt);
858 LIR* target = NewLIR0(kPseudoTargetLabel);
859 branch->target = target;
860 } else {
861 DCHECK_EQ(cu_->instruction_set, kX86);
Ian Rogers848871b2013-08-05 10:56:33 -0700862 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), TargetReg(kArg2),
Ian Rogers7655f292013-07-29 11:07:13 -0700863 TargetReg(kArg1), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 }
865 GenBarrier();
866 StoreValue(rl_dest, GetReturn(false));
867 } else {
868 RegLocation rl_method = LoadCurrMethod();
869 int res_reg = AllocTemp();
870 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
871 LoadWordDisp(rl_method.low_reg,
Brian Carlstromea46f952013-07-30 01:26:50 -0700872 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 LoadWordDisp(res_reg, offset_of_string, rl_result.low_reg);
874 StoreValue(rl_dest, rl_result);
875 }
876}
877
878/*
879 * Let helper function take care of everything. Will
880 * call Class::NewInstanceFromCode(type_idx, method);
881 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700882void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 FlushAllRegs(); /* Everything to home location */
884 // alloc will always check for resolution, do we also need to verify
885 // access because the verifier was unable to?
Ian Rogers848871b2013-08-05 10:56:33 -0700886 ThreadOffset func_offset(-1);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800887 const DexFile* dex_file = cu_->dex_file;
888 CompilerDriver* driver = cu_->compiler_driver;
889 if (driver->CanAccessInstantiableTypeWithoutChecks(
890 cu_->method_idx, *dex_file, type_idx)) {
891 bool is_type_initialized;
892 bool use_direct_type_ptr;
893 uintptr_t direct_type_ptr;
894 if (kEmbedClassInCode &&
895 driver->CanEmbedTypeInCode(*dex_file, type_idx,
896 &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
897 // The fast path.
898 if (!use_direct_type_ptr) {
899 // Use the literal pool and a PC-relative load from a data word.
900 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
901 if (data_target == nullptr) {
902 data_target = AddWordData(&class_literal_list_, type_idx);
903 }
904 LIR* load_pc_rel = OpPcRelLoad(TargetReg(kArg0), data_target);
905 AppendLIR(load_pc_rel);
906 if (!is_type_initialized) {
907 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
908 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
909 } else {
910 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
911 CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
912 }
913 } else {
914 // Use the direct pointer.
915 if (!is_type_initialized) {
916 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
917 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
918 } else {
919 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
920 CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
921 }
922 }
923 } else {
924 // The slow path.
925 DCHECK_EQ(func_offset.Int32Value(), -1);
926 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
927 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
928 }
929 DCHECK_NE(func_offset.Int32Value(), -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700931 func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800932 CallRuntimeHelperImmMethod(func_offset, type_idx, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 RegLocation rl_result = GetReturn(false);
935 StoreValue(rl_dest, rl_result);
936}
937
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700938void Mir2Lir::GenThrow(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 FlushAllRegs();
Ian Rogers7655f292013-07-29 11:07:13 -0700940 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941}
942
943// For final classes there are no sub-classes to check and so we can answer the instance-of
944// question with simple comparisons.
945void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
946 RegLocation rl_src) {
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800947 // X86 has its own implementation.
948 DCHECK_NE(cu_->instruction_set, kX86);
949
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950 RegLocation object = LoadValue(rl_src, kCoreReg);
951 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
952 int result_reg = rl_result.low_reg;
953 if (result_reg == object.low_reg) {
954 result_reg = AllocTypedTemp(false, kCoreReg);
955 }
956 LoadConstant(result_reg, 0); // assume false
957 LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL);
958
959 int check_class = AllocTypedTemp(false, kCoreReg);
960 int object_class = AllocTypedTemp(false, kCoreReg);
961
962 LoadCurrMethodDirect(check_class);
963 if (use_declaring_class) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700964 LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 check_class);
966 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
967 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700968 LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 check_class);
970 LoadWordDisp(object.low_reg, mirror::Object::ClassOffset().Int32Value(), object_class);
971 int32_t offset_of_type =
972 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
973 (sizeof(mirror::Class*) * type_idx);
974 LoadWordDisp(check_class, offset_of_type, check_class);
975 }
976
977 LIR* ne_branchover = NULL;
978 if (cu_->instruction_set == kThumb2) {
979 OpRegReg(kOpCmp, check_class, object_class); // Same?
980 OpIT(kCondEq, ""); // if-convert the test
981 LoadConstant(result_reg, 1); // .eq case - load true
982 } else {
983 ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
984 LoadConstant(result_reg, 1); // eq case - load true
985 }
986 LIR* target = NewLIR0(kPseudoTargetLabel);
987 null_branchover->target = target;
988 if (ne_branchover != NULL) {
989 ne_branchover->target = target;
990 }
991 FreeTemp(object_class);
992 FreeTemp(check_class);
993 if (IsTemp(result_reg)) {
994 OpRegCopy(rl_result.low_reg, result_reg);
995 FreeTemp(result_reg);
996 }
997 StoreValue(rl_dest, rl_result);
998}
999
1000void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1001 bool type_known_abstract, bool use_declaring_class,
1002 bool can_assume_type_is_in_dex_cache,
1003 uint32_t type_idx, RegLocation rl_dest,
1004 RegLocation rl_src) {
1005 FlushAllRegs();
1006 // May generate a call - use explicit registers
1007 LockCallTemps();
1008 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1009 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1010 if (needs_access_check) {
1011 // Check we have access to type_idx and if not throw IllegalAccessError,
1012 // returns Class* in kArg0
Ian Rogers848871b2013-08-05 10:56:33 -07001013 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 type_idx, true);
1015 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1016 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1017 } else if (use_declaring_class) {
1018 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1019 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001020 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 } else {
1022 // Load dex cache entry into class_reg (kArg2)
1023 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1024 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001025 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 int32_t offset_of_type =
1027 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1028 * type_idx);
1029 LoadWordDisp(class_reg, offset_of_type, class_reg);
1030 if (!can_assume_type_is_in_dex_cache) {
1031 // Need to test presence of type in dex cache at runtime
1032 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1033 // Not resolved
1034 // Call out to helper, which will return resolved type in kRet0
Ian Rogers848871b2013-08-05 10:56:33 -07001035 CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001036 OpRegCopy(TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); /* reload Ref */
1038 // Rejoin code paths
1039 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1040 hop_branch->target = hop_target;
1041 }
1042 }
1043 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1044 RegLocation rl_result = GetReturn(false);
1045 if (cu_->instruction_set == kMips) {
1046 // On MIPS rArg0 != rl_result, place false in result if branch is taken.
1047 LoadConstant(rl_result.low_reg, 0);
1048 }
1049 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1050
1051 /* load object->klass_ */
1052 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1053 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1054 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1055 LIR* branchover = NULL;
1056 if (type_known_final) {
1057 // rl_result == ref == null == 0.
1058 if (cu_->instruction_set == kThumb2) {
1059 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1060 OpIT(kCondEq, "E"); // if-convert the test
1061 LoadConstant(rl_result.low_reg, 1); // .eq case - load true
1062 LoadConstant(rl_result.low_reg, 0); // .ne case - load false
1063 } else {
1064 LoadConstant(rl_result.low_reg, 0); // ne case - load false
1065 branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
1066 LoadConstant(rl_result.low_reg, 1); // eq case - load true
1067 }
1068 } else {
1069 if (cu_->instruction_set == kThumb2) {
Ian Rogers848871b2013-08-05 10:56:33 -07001070 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 if (!type_known_abstract) {
1072 /* Uses conditional nullification */
1073 OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
1074 OpIT(kCondEq, "EE"); // if-convert the test
1075 LoadConstant(TargetReg(kArg0), 1); // .eq case - load true
1076 }
1077 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1078 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1079 FreeTemp(r_tgt);
1080 } else {
1081 if (!type_known_abstract) {
1082 /* Uses branchovers */
1083 LoadConstant(rl_result.low_reg, 1); // assume true
1084 branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1085 }
1086 if (cu_->instruction_set != kX86) {
Ian Rogers848871b2013-08-05 10:56:33 -07001087 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1089 OpReg(kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1090 FreeTemp(r_tgt);
1091 } else {
1092 OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
Ian Rogers848871b2013-08-05 10:56:33 -07001093 OpThreadMem(kOpBlx, QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 }
1095 }
1096 }
1097 // TODO: only clobber when type isn't final?
Vladimir Marko31c2aac2013-12-09 16:31:19 +00001098 ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 /* branch targets here */
1100 LIR* target = NewLIR0(kPseudoTargetLabel);
1101 StoreValue(rl_dest, rl_result);
1102 branch1->target = target;
1103 if (branchover != NULL) {
1104 branchover->target = target;
1105 }
1106}
1107
1108void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1109 bool type_known_final, type_known_abstract, use_declaring_class;
1110 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1111 *cu_->dex_file,
1112 type_idx,
1113 &type_known_final,
1114 &type_known_abstract,
1115 &use_declaring_class);
1116 bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1117 cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1118
1119 if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1120 GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1121 } else {
1122 GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1123 use_declaring_class, can_assume_type_is_in_dex_cache,
1124 type_idx, rl_dest, rl_src);
1125 }
1126}
1127
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001128void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 bool type_known_final, type_known_abstract, use_declaring_class;
1130 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1131 *cu_->dex_file,
1132 type_idx,
1133 &type_known_final,
1134 &type_known_abstract,
1135 &use_declaring_class);
1136 // Note: currently type_known_final is unused, as optimizing will only improve the performance
1137 // of the exception throw path.
1138 DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
1139 const MethodReference mr(cu->GetDexFile(), cu->GetDexMethodIndex());
1140 if (!needs_access_check && cu_->compiler_driver->IsSafeCast(mr, insn_idx)) {
1141 // Verifier type analysis proved this check cast would never cause an exception.
1142 return;
1143 }
1144 FlushAllRegs();
1145 // May generate a call - use explicit registers
1146 LockCallTemps();
1147 LoadCurrMethodDirect(TargetReg(kArg1)); // kArg1 <= current Method*
1148 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1149 if (needs_access_check) {
1150 // Check we have access to type_idx and if not throw IllegalAccessError,
1151 // returns Class* in kRet0
1152 // InitializeTypeAndVerifyAccess(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001153 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 type_idx, TargetReg(kArg1), true);
1155 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
1156 } else if (use_declaring_class) {
1157 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001158 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 } else {
1160 // Load dex cache entry into class_reg (kArg2)
1161 LoadWordDisp(TargetReg(kArg1),
Brian Carlstromea46f952013-07-30 01:26:50 -07001162 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 int32_t offset_of_type =
1164 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1165 (sizeof(mirror::Class*) * type_idx);
1166 LoadWordDisp(class_reg, offset_of_type, class_reg);
1167 if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1168 // Need to test presence of type in dex cache at runtime
1169 LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1170 // Not resolved
1171 // Call out to helper, which will return resolved type in kArg0
1172 // InitializeTypeFromCode(idx, method)
Ian Rogers848871b2013-08-05 10:56:33 -07001173 CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx,
Ian Rogers7655f292013-07-29 11:07:13 -07001174 TargetReg(kArg1), true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001175 OpRegCopy(class_reg, TargetReg(kRet0)); // Align usage with fast path
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 // Rejoin code paths
1177 LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1178 hop_branch->target = hop_target;
1179 }
1180 }
1181 // At this point, class_reg (kArg2) has class
1182 LoadValueDirectFixed(rl_src, TargetReg(kArg0)); // kArg0 <= ref
1183 /* Null is OK - continue */
1184 LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1185 /* load object->klass_ */
1186 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1187 LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1188 /* kArg1 now contains object->klass_ */
1189 LIR* branch2 = NULL;
1190 if (!type_known_abstract) {
1191 branch2 = OpCmpBranch(kCondEq, TargetReg(kArg1), class_reg, NULL);
1192 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001193 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), TargetReg(kArg2),
1194 TargetReg(kArg1), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 /* branch target here */
1196 LIR* target = NewLIR0(kPseudoTargetLabel);
1197 branch1->target = target;
1198 if (branch2 != NULL) {
1199 branch2->target = target;
1200 }
1201}
1202
1203void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001204 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 RegLocation rl_result;
1206 if (cu_->instruction_set == kThumb2) {
1207 /*
1208 * NOTE: This is the one place in the code in which we might have
1209 * as many as six live temporary registers. There are 5 in the normal
1210 * set for Arm. Until we have spill capabilities, temporarily add
1211 * lr to the temp set. It is safe to do this locally, but note that
1212 * lr is used explicitly elsewhere in the code generator and cannot
1213 * normally be used as a general temp register.
1214 */
1215 MarkTemp(TargetReg(kLr)); // Add lr to the temp pool
1216 FreeTemp(TargetReg(kLr)); // and make it available
1217 }
1218 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1219 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1220 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1221 // The longs may overlap - use intermediate temp if so
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001222 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 int t_reg = AllocTemp();
1224 OpRegRegReg(first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1225 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1226 OpRegCopy(rl_result.low_reg, t_reg);
1227 FreeTemp(t_reg);
1228 } else {
1229 OpRegRegReg(first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1230 OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg,
1231 rl_src2.high_reg);
1232 }
1233 /*
1234 * NOTE: If rl_dest refers to a frame variable in a large frame, the
1235 * following StoreValueWide might need to allocate a temp register.
1236 * To further work around the lack of a spill capability, explicitly
1237 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1238 * Remove when spill is functional.
1239 */
1240 FreeRegLocTemps(rl_result, rl_src1);
1241 FreeRegLocTemps(rl_result, rl_src2);
1242 StoreValueWide(rl_dest, rl_result);
1243 if (cu_->instruction_set == kThumb2) {
1244 Clobber(TargetReg(kLr));
1245 UnmarkTemp(TargetReg(kLr)); // Remove lr from the temp pool
1246 }
1247}
1248
1249
1250void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001251 RegLocation rl_src1, RegLocation rl_shift) {
Ian Rogers848871b2013-08-05 10:56:33 -07001252 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253
1254 switch (opcode) {
1255 case Instruction::SHL_LONG:
1256 case Instruction::SHL_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001257 func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 break;
1259 case Instruction::SHR_LONG:
1260 case Instruction::SHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001261 func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 break;
1263 case Instruction::USHR_LONG:
1264 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7655f292013-07-29 11:07:13 -07001265 func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 break;
1267 default:
1268 LOG(FATAL) << "Unexpected case";
1269 }
1270 FlushAllRegs(); /* Send everything to home location */
1271 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1272 RegLocation rl_result = GetReturnWide(false);
1273 StoreValueWide(rl_dest, rl_result);
1274}
1275
1276
1277void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001278 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 OpKind op = kOpBkpt;
1280 bool is_div_rem = false;
1281 bool check_zero = false;
1282 bool unary = false;
1283 RegLocation rl_result;
1284 bool shift_op = false;
1285 switch (opcode) {
1286 case Instruction::NEG_INT:
1287 op = kOpNeg;
1288 unary = true;
1289 break;
1290 case Instruction::NOT_INT:
1291 op = kOpMvn;
1292 unary = true;
1293 break;
1294 case Instruction::ADD_INT:
1295 case Instruction::ADD_INT_2ADDR:
1296 op = kOpAdd;
1297 break;
1298 case Instruction::SUB_INT:
1299 case Instruction::SUB_INT_2ADDR:
1300 op = kOpSub;
1301 break;
1302 case Instruction::MUL_INT:
1303 case Instruction::MUL_INT_2ADDR:
1304 op = kOpMul;
1305 break;
1306 case Instruction::DIV_INT:
1307 case Instruction::DIV_INT_2ADDR:
1308 check_zero = true;
1309 op = kOpDiv;
1310 is_div_rem = true;
1311 break;
1312 /* NOTE: returns in kArg1 */
1313 case Instruction::REM_INT:
1314 case Instruction::REM_INT_2ADDR:
1315 check_zero = true;
1316 op = kOpRem;
1317 is_div_rem = true;
1318 break;
1319 case Instruction::AND_INT:
1320 case Instruction::AND_INT_2ADDR:
1321 op = kOpAnd;
1322 break;
1323 case Instruction::OR_INT:
1324 case Instruction::OR_INT_2ADDR:
1325 op = kOpOr;
1326 break;
1327 case Instruction::XOR_INT:
1328 case Instruction::XOR_INT_2ADDR:
1329 op = kOpXor;
1330 break;
1331 case Instruction::SHL_INT:
1332 case Instruction::SHL_INT_2ADDR:
1333 shift_op = true;
1334 op = kOpLsl;
1335 break;
1336 case Instruction::SHR_INT:
1337 case Instruction::SHR_INT_2ADDR:
1338 shift_op = true;
1339 op = kOpAsr;
1340 break;
1341 case Instruction::USHR_INT:
1342 case Instruction::USHR_INT_2ADDR:
1343 shift_op = true;
1344 op = kOpLsr;
1345 break;
1346 default:
1347 LOG(FATAL) << "Invalid word arith op: " << opcode;
1348 }
1349 if (!is_div_rem) {
1350 if (unary) {
1351 rl_src1 = LoadValue(rl_src1, kCoreReg);
1352 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1353 OpRegReg(op, rl_result.low_reg, rl_src1.low_reg);
1354 } else {
1355 if (shift_op) {
1356 int t_reg = INVALID_REG;
1357 if (cu_->instruction_set == kX86) {
1358 // X86 doesn't require masking and must use ECX
1359 t_reg = TargetReg(kCount); // rCX
1360 LoadValueDirectFixed(rl_src2, t_reg);
1361 } else {
1362 rl_src2 = LoadValue(rl_src2, kCoreReg);
1363 t_reg = AllocTemp();
1364 OpRegRegImm(kOpAnd, t_reg, rl_src2.low_reg, 31);
1365 }
1366 rl_src1 = LoadValue(rl_src1, kCoreReg);
1367 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1368 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1369 FreeTemp(t_reg);
1370 } else {
1371 rl_src1 = LoadValue(rl_src1, kCoreReg);
1372 rl_src2 = LoadValue(rl_src2, kCoreReg);
1373 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1374 OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1375 }
1376 }
1377 StoreValue(rl_dest, rl_result);
1378 } else {
Dave Allison70202782013-10-22 17:52:19 -07001379 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 -07001380 if (cu_->instruction_set == kMips) {
1381 rl_src1 = LoadValue(rl_src1, kCoreReg);
1382 rl_src2 = LoadValue(rl_src2, kCoreReg);
1383 if (check_zero) {
1384 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1385 }
1386 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
Dave Allison70202782013-10-22 17:52:19 -07001387 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001388 } else if (cu_->instruction_set == kX86) {
1389 rl_result = GenDivRem(rl_dest, rl_src1, rl_src2, op == kOpDiv, check_zero);
1390 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001391 } else if (cu_->instruction_set == kThumb2) {
1392 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1393 // Use ARM SDIV instruction for division. For remainder we also need to
1394 // calculate using a MUL and subtract.
1395 rl_src1 = LoadValue(rl_src1, kCoreReg);
1396 rl_src2 = LoadValue(rl_src2, kCoreReg);
1397 if (check_zero) {
1398 GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1399 }
1400 rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
1401 done = true;
1402 }
1403 }
1404
1405 // If we haven't already generated the code use the callout function.
1406 if (!done) {
Ian Rogers848871b2013-08-05 10:56:33 -07001407 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001408 FlushAllRegs(); /* Send everything to home location */
1409 LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1410 int r_tgt = CallHelperSetup(func_offset);
1411 LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1412 if (check_zero) {
1413 GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1414 }
Dave Allison70202782013-10-22 17:52:19 -07001415 // NOTE: callout here is not a safepoint.
Brian Carlstromdf629502013-07-17 22:39:56 -07001416 CallHelper(r_tgt, func_offset, false /* not a safepoint */);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 if (op == kOpDiv)
1418 rl_result = GetReturn(false);
1419 else
1420 rl_result = GetReturnAlt();
1421 }
1422 StoreValue(rl_dest, rl_result);
1423 }
1424}
1425
1426/*
1427 * The following are the first-level codegen routines that analyze the format
1428 * of each bytecode then either dispatch special purpose codegen routines
1429 * or produce corresponding Thumb instructions directly.
1430 */
1431
Brian Carlstrom7940e442013-07-12 13:46:57 -07001432// Returns true if no more than two bits are set in 'x'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001433static bool IsPopCountLE2(unsigned int x) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 x &= x - 1;
1435 return (x & (x - 1)) == 0;
1436}
1437
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1439// and store the result in 'rl_dest'.
buzbee11b63d12013-08-27 07:34:17 -07001440bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001441 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1443 return false;
1444 }
1445 // No divide instruction for Arm, so check for more special cases
1446 if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee11b63d12013-08-27 07:34:17 -07001447 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 }
1449 int k = LowestSetBit(lit);
1450 if (k >= 30) {
1451 // Avoid special cases.
1452 return false;
1453 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 rl_src = LoadValue(rl_src, kCoreReg);
1455 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee11b63d12013-08-27 07:34:17 -07001456 if (is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 int t_reg = AllocTemp();
1458 if (lit == 2) {
1459 // Division by 2 is by far the most common division by constant.
1460 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1461 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1462 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1463 } else {
1464 OpRegRegImm(kOpAsr, t_reg, rl_src.low_reg, 31);
1465 OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
1466 OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1467 OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1468 }
1469 } else {
1470 int t_reg1 = AllocTemp();
1471 int t_reg2 = AllocTemp();
1472 if (lit == 2) {
1473 OpRegRegImm(kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1474 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1475 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
1476 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1477 } else {
1478 OpRegRegImm(kOpAsr, t_reg1, rl_src.low_reg, 31);
1479 OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
1480 OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1481 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
1482 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1483 }
1484 }
1485 StoreValue(rl_dest, rl_result);
1486 return true;
1487}
1488
1489// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1490// and store the result in 'rl_dest'.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001491bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 // Can we simplify this multiplication?
1493 bool power_of_two = false;
1494 bool pop_count_le2 = false;
1495 bool power_of_two_minus_one = false;
1496 if (lit < 2) {
1497 // Avoid special cases.
1498 return false;
1499 } else if (IsPowerOfTwo(lit)) {
1500 power_of_two = true;
1501 } else if (IsPopCountLE2(lit)) {
1502 pop_count_le2 = true;
1503 } else if (IsPowerOfTwo(lit + 1)) {
1504 power_of_two_minus_one = true;
1505 } else {
1506 return false;
1507 }
1508 rl_src = LoadValue(rl_src, kCoreReg);
1509 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1510 if (power_of_two) {
1511 // Shift.
1512 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
1513 } else if (pop_count_le2) {
1514 // Shift and add and shift.
1515 int first_bit = LowestSetBit(lit);
1516 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1517 GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1518 } else {
1519 // Reverse subtract: (src << (shift + 1)) - src.
1520 DCHECK(power_of_two_minus_one);
1521 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1522 int t_reg = AllocTemp();
1523 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1524 OpRegRegReg(kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1525 }
1526 StoreValue(rl_dest, rl_result);
1527 return true;
1528}
1529
1530void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001531 int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 RegLocation rl_result;
1533 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
1534 int shift_op = false;
1535 bool is_div = false;
1536
1537 switch (opcode) {
1538 case Instruction::RSUB_INT_LIT8:
1539 case Instruction::RSUB_INT: {
1540 rl_src = LoadValue(rl_src, kCoreReg);
1541 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1542 if (cu_->instruction_set == kThumb2) {
1543 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1544 } else {
1545 OpRegReg(kOpNeg, rl_result.low_reg, rl_src.low_reg);
1546 OpRegImm(kOpAdd, rl_result.low_reg, lit);
1547 }
1548 StoreValue(rl_dest, rl_result);
1549 return;
1550 }
1551
1552 case Instruction::SUB_INT:
1553 case Instruction::SUB_INT_2ADDR:
1554 lit = -lit;
1555 // Intended fallthrough
1556 case Instruction::ADD_INT:
1557 case Instruction::ADD_INT_2ADDR:
1558 case Instruction::ADD_INT_LIT8:
1559 case Instruction::ADD_INT_LIT16:
1560 op = kOpAdd;
1561 break;
1562 case Instruction::MUL_INT:
1563 case Instruction::MUL_INT_2ADDR:
1564 case Instruction::MUL_INT_LIT8:
1565 case Instruction::MUL_INT_LIT16: {
1566 if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1567 return;
1568 }
1569 op = kOpMul;
1570 break;
1571 }
1572 case Instruction::AND_INT:
1573 case Instruction::AND_INT_2ADDR:
1574 case Instruction::AND_INT_LIT8:
1575 case Instruction::AND_INT_LIT16:
1576 op = kOpAnd;
1577 break;
1578 case Instruction::OR_INT:
1579 case Instruction::OR_INT_2ADDR:
1580 case Instruction::OR_INT_LIT8:
1581 case Instruction::OR_INT_LIT16:
1582 op = kOpOr;
1583 break;
1584 case Instruction::XOR_INT:
1585 case Instruction::XOR_INT_2ADDR:
1586 case Instruction::XOR_INT_LIT8:
1587 case Instruction::XOR_INT_LIT16:
1588 op = kOpXor;
1589 break;
1590 case Instruction::SHL_INT_LIT8:
1591 case Instruction::SHL_INT:
1592 case Instruction::SHL_INT_2ADDR:
1593 lit &= 31;
1594 shift_op = true;
1595 op = kOpLsl;
1596 break;
1597 case Instruction::SHR_INT_LIT8:
1598 case Instruction::SHR_INT:
1599 case Instruction::SHR_INT_2ADDR:
1600 lit &= 31;
1601 shift_op = true;
1602 op = kOpAsr;
1603 break;
1604 case Instruction::USHR_INT_LIT8:
1605 case Instruction::USHR_INT:
1606 case Instruction::USHR_INT_2ADDR:
1607 lit &= 31;
1608 shift_op = true;
1609 op = kOpLsr;
1610 break;
1611
1612 case Instruction::DIV_INT:
1613 case Instruction::DIV_INT_2ADDR:
1614 case Instruction::DIV_INT_LIT8:
1615 case Instruction::DIV_INT_LIT16:
1616 case Instruction::REM_INT:
1617 case Instruction::REM_INT_2ADDR:
1618 case Instruction::REM_INT_LIT8:
1619 case Instruction::REM_INT_LIT16: {
1620 if (lit == 0) {
1621 GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1622 return;
1623 }
buzbee11b63d12013-08-27 07:34:17 -07001624 if ((opcode == Instruction::DIV_INT) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee11b63d12013-08-27 07:34:17 -07001626 (opcode == Instruction::DIV_INT_LIT8) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -07001627 (opcode == Instruction::DIV_INT_LIT16)) {
1628 is_div = true;
1629 } else {
1630 is_div = false;
1631 }
buzbee11b63d12013-08-27 07:34:17 -07001632 if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1633 return;
1634 }
Dave Allison70202782013-10-22 17:52:19 -07001635
1636 bool done = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 if (cu_->instruction_set == kMips) {
1638 rl_src = LoadValue(rl_src, kCoreReg);
1639 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
Dave Allison70202782013-10-22 17:52:19 -07001640 done = true;
Mark Mendell2bf31e62014-01-23 12:13:40 -08001641 } else if (cu_->instruction_set == kX86) {
1642 rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1643 done = true;
Dave Allison70202782013-10-22 17:52:19 -07001644 } else if (cu_->instruction_set == kThumb2) {
1645 if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1646 // Use ARM SDIV instruction for division. For remainder we also need to
1647 // calculate using a MUL and subtract.
1648 rl_src = LoadValue(rl_src, kCoreReg);
1649 rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
1650 done = true;
1651 }
1652 }
1653
1654 if (!done) {
1655 FlushAllRegs(); /* Everything to home location. */
Brian Carlstrom7940e442013-07-12 13:46:57 -07001656 LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1657 Clobber(TargetReg(kArg0));
Ian Rogers848871b2013-08-05 10:56:33 -07001658 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001659 CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1660 if (is_div)
1661 rl_result = GetReturn(false);
1662 else
1663 rl_result = GetReturnAlt();
1664 }
1665 StoreValue(rl_dest, rl_result);
1666 return;
1667 }
1668 default:
1669 LOG(FATAL) << "Unexpected opcode " << opcode;
1670 }
1671 rl_src = LoadValue(rl_src, kCoreReg);
1672 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Dave Allison70202782013-10-22 17:52:19 -07001673 // Avoid shifts by literal 0 - no support in Thumb. Change to copy.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 if (shift_op && (lit == 0)) {
1675 OpRegCopy(rl_result.low_reg, rl_src.low_reg);
1676 } else {
1677 OpRegRegImm(op, rl_result.low_reg, rl_src.low_reg, lit);
1678 }
1679 StoreValue(rl_dest, rl_result);
1680}
1681
1682void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001683 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001684 RegLocation rl_result;
1685 OpKind first_op = kOpBkpt;
1686 OpKind second_op = kOpBkpt;
1687 bool call_out = false;
1688 bool check_zero = false;
Ian Rogers848871b2013-08-05 10:56:33 -07001689 ThreadOffset func_offset(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001690 int ret_reg = TargetReg(kRet0);
1691
1692 switch (opcode) {
1693 case Instruction::NOT_LONG:
1694 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1695 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1696 // Check for destructive overlap
1697 if (rl_result.low_reg == rl_src2.high_reg) {
1698 int t_reg = AllocTemp();
1699 OpRegCopy(t_reg, rl_src2.high_reg);
1700 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1701 OpRegReg(kOpMvn, rl_result.high_reg, t_reg);
1702 FreeTemp(t_reg);
1703 } else {
1704 OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1705 OpRegReg(kOpMvn, rl_result.high_reg, rl_src2.high_reg);
1706 }
1707 StoreValueWide(rl_dest, rl_result);
1708 return;
1709 case Instruction::ADD_LONG:
1710 case Instruction::ADD_LONG_2ADDR:
1711 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001712 GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001713 return;
1714 }
1715 first_op = kOpAdd;
1716 second_op = kOpAdc;
1717 break;
1718 case Instruction::SUB_LONG:
1719 case Instruction::SUB_LONG_2ADDR:
1720 if (cu_->instruction_set != kThumb2) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001721 GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001722 return;
1723 }
1724 first_op = kOpSub;
1725 second_op = kOpSbc;
1726 break;
1727 case Instruction::MUL_LONG:
1728 case Instruction::MUL_LONG_2ADDR:
Mark Mendell4708dcd2014-01-22 09:05:18 -08001729 if (cu_->instruction_set != kMips) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001730 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001731 return;
1732 } else {
1733 call_out = true;
1734 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001735 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736 }
1737 break;
1738 case Instruction::DIV_LONG:
1739 case Instruction::DIV_LONG_2ADDR:
1740 call_out = true;
1741 check_zero = true;
1742 ret_reg = TargetReg(kRet0);
Ian Rogers7655f292013-07-29 11:07:13 -07001743 func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001744 break;
1745 case Instruction::REM_LONG:
1746 case Instruction::REM_LONG_2ADDR:
1747 call_out = true;
1748 check_zero = true;
Ian Rogersa9a82542013-10-04 11:17:26 -07001749 func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001750 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1751 ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1752 break;
1753 case Instruction::AND_LONG_2ADDR:
1754 case Instruction::AND_LONG:
1755 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001756 return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001757 }
1758 first_op = kOpAnd;
1759 second_op = kOpAnd;
1760 break;
1761 case Instruction::OR_LONG:
1762 case Instruction::OR_LONG_2ADDR:
1763 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001764 GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001765 return;
1766 }
1767 first_op = kOpOr;
1768 second_op = kOpOr;
1769 break;
1770 case Instruction::XOR_LONG:
1771 case Instruction::XOR_LONG_2ADDR:
1772 if (cu_->instruction_set == kX86) {
Mark Mendelle02d48f2014-01-15 11:19:23 -08001773 GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001774 return;
1775 }
1776 first_op = kOpXor;
1777 second_op = kOpXor;
1778 break;
1779 case Instruction::NEG_LONG: {
1780 GenNegLong(rl_dest, rl_src2);
1781 return;
1782 }
1783 default:
1784 LOG(FATAL) << "Invalid long arith op";
1785 }
1786 if (!call_out) {
1787 GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1788 } else {
1789 FlushAllRegs(); /* Send everything to home location */
1790 if (check_zero) {
1791 LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1792 int r_tgt = CallHelperSetup(func_offset);
1793 GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1794 LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1795 // NOTE: callout here is not a safepoint
1796 CallHelper(r_tgt, func_offset, false /* not safepoint */);
1797 } else {
1798 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1799 }
1800 // Adjust return regs in to handle case of rem returning kArg2/kArg3
1801 if (ret_reg == TargetReg(kRet0))
1802 rl_result = GetReturnWide(false);
1803 else
1804 rl_result = GetReturnWideAlt();
1805 StoreValueWide(rl_dest, rl_result);
1806 }
1807}
1808
Ian Rogers848871b2013-08-05 10:56:33 -07001809void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001810 RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001811 /*
1812 * Don't optimize the register usage since it calls out to support
1813 * functions
1814 */
1815 FlushAllRegs(); /* Send everything to home location */
1816 if (rl_src.wide) {
1817 LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1818 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1819 } else {
1820 LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1821 }
1822 CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1823 if (rl_dest.wide) {
1824 RegLocation rl_result;
1825 rl_result = GetReturnWide(rl_dest.fp);
1826 StoreValueWide(rl_dest, rl_result);
1827 } else {
1828 RegLocation rl_result;
1829 rl_result = GetReturn(rl_dest.fp);
1830 StoreValue(rl_dest, rl_result);
1831 }
1832}
1833
1834/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001835void Mir2Lir::GenSuspendTest(int opt_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001836 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1837 return;
1838 }
1839 FlushAllRegs();
1840 LIR* branch = OpTestSuspend(NULL);
1841 LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
buzbee0d829482013-10-11 15:24:55 -07001842 LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1843 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001844 branch->target = target;
1845 suspend_launchpads_.Insert(target);
1846}
1847
1848/* Check if we need to check for pending suspend request */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001849void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001850 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1851 OpUnconditionalBranch(target);
1852 return;
1853 }
1854 OpTestSuspend(target);
1855 LIR* launch_pad =
buzbee0d829482013-10-11 15:24:55 -07001856 RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1857 current_dalvik_offset_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001858 FlushAllRegs();
1859 OpUnconditionalBranch(launch_pad);
1860 suspend_launchpads_.Insert(launch_pad);
1861}
1862
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001863/* Call out to helper assembly routine that will null check obj and then lock it. */
1864void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1865 FlushAllRegs();
1866 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
1867}
1868
1869/* Call out to helper assembly routine that will null check obj and then unlock it. */
1870void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
1871 FlushAllRegs();
1872 CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
1873}
1874
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001875/* Generic code for generating a wide constant into a VR. */
1876void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
1877 RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
1878 LoadConstantWide(rl_result.low_reg, rl_result.high_reg, value);
1879 StoreValueWide(rl_dest, rl_result);
1880}
1881
Brian Carlstrom7940e442013-07-12 13:46:57 -07001882} // namespace art