blob: d5173b067839f9de7baa303f712e61010add7292 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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/* This file contains codegen for the Thumb2 ISA. */
18
19#include "arm_lir.h"
20#include "codegen_arm.h"
21#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "mirror/array.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
Ian Rogersd9c4fc92013-10-01 19:45:43 -070027LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, int src1, int src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070028 OpRegReg(kOpCmp, src1, src2);
29 return OpCondBranch(cond, target);
30}
31
32/*
33 * Generate a Thumb2 IT instruction, which can nullify up to
34 * four subsequent instructions based on a condition and its
35 * inverse. The condition applies to the first instruction, which
36 * is executed if the condition is met. The string "guide" consists
37 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
38 * A "T" means the instruction is executed if the condition is
39 * met, and an "E" means the instruction is executed if the condition
40 * is not met.
41 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070042LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 int mask;
44 int mask3 = 0;
45 int mask2 = 0;
46 int mask1 = 0;
47 ArmConditionCode code = ArmConditionEncoding(ccode);
48 int cond_bit = code & 1;
49 int alt_bit = cond_bit ^ 1;
50
Brian Carlstrom7934ac22013-07-26 10:54:15 -070051 // Note: case fallthroughs intentional
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 switch (strlen(guide)) {
53 case 3:
54 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
55 case 2:
56 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
57 case 1:
58 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
59 break;
60 case 0:
61 break;
62 default:
63 LOG(FATAL) << "OAT: bad case in OpIT";
64 }
65 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
66 (1 << (3 - strlen(guide)));
67 return NewLIR2(kThumb2It, code, mask);
68}
69
70/*
71 * 64-bit 3way compare function.
72 * mov rX, #-1
73 * cmp op1hi, op2hi
74 * blt done
75 * bgt flip
76 * sub rX, op1lo, op2lo (treat as unsigned)
77 * beq done
78 * ite hi
79 * mov(hi) rX, #-1
80 * mov(!hi) rX, #1
81 * flip:
82 * neg rX
83 * done:
84 */
85void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070086 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 LIR* target1;
88 LIR* target2;
89 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
90 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
91 int t_reg = AllocTemp();
92 LoadConstant(t_reg, -1);
93 OpRegReg(kOpCmp, rl_src1.high_reg, rl_src2.high_reg);
94 LIR* branch1 = OpCondBranch(kCondLt, NULL);
95 LIR* branch2 = OpCondBranch(kCondGt, NULL);
96 OpRegRegReg(kOpSub, t_reg, rl_src1.low_reg, rl_src2.low_reg);
97 LIR* branch3 = OpCondBranch(kCondEq, NULL);
98
99 OpIT(kCondHi, "E");
Vladimir Marko332b7aa2013-11-18 12:01:54 +0000100 NewLIR2(kThumb2MovI8M, t_reg, ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 LoadConstant(t_reg, 1);
102 GenBarrier();
103
104 target2 = NewLIR0(kPseudoTargetLabel);
105 OpRegReg(kOpNeg, t_reg, t_reg);
106
107 target1 = NewLIR0(kPseudoTargetLabel);
108
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700109 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 rl_temp.low_reg = t_reg;
111 StoreValue(rl_dest, rl_temp);
112 FreeTemp(t_reg);
113
114 branch1->target = target1;
115 branch2->target = target2;
116 branch3->target = branch1->target;
117}
118
119void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700120 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 int32_t val_lo = Low32Bits(val);
122 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700123 DCHECK_GE(ModifiedImmediate(val_lo), 0);
124 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700125 LIR* taken = &block_label_list_[bb->taken];
126 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
128 int32_t low_reg = rl_src1.low_reg;
129 int32_t high_reg = rl_src1.high_reg;
130
Brian Carlstromdf629502013-07-17 22:39:56 -0700131 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 case kCondEq:
133 case kCondNe:
134 LIR* target;
135 ConditionCode condition;
136 if (ccode == kCondEq) {
137 target = not_taken;
138 condition = kCondEq;
139 } else {
140 target = taken;
141 condition = kCondNe;
142 }
143 if (val == 0) {
144 int t_reg = AllocTemp();
145 NewLIR4(kThumb2OrrRRRs, t_reg, low_reg, high_reg, 0);
146 FreeTemp(t_reg);
147 OpCondBranch(condition, taken);
148 return;
149 }
150 OpCmpImmBranch(kCondNe, high_reg, val_hi, target);
151 break;
152 case kCondLt:
153 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
154 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
155 ccode = kCondCc;
156 break;
157 case kCondLe:
158 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
159 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
160 ccode = kCondLs;
161 break;
162 case kCondGt:
163 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
164 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
165 ccode = kCondHi;
166 break;
167 case kCondGe:
168 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
169 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
170 ccode = kCondCs;
171 break;
172 default:
173 LOG(FATAL) << "Unexpected ccode: " << ccode;
174 }
175 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
176}
177
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegLocation rl_result;
180 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 RegLocation rl_dest = mir_graph_->GetDest(mir);
182 rl_src = LoadValue(rl_src, kCoreReg);
183 if (mir->ssa_rep->num_uses == 1) {
184 // CONST case
185 int true_val = mir->dalvikInsn.vB;
186 int false_val = mir->dalvikInsn.vC;
187 rl_result = EvalLoc(rl_dest, kCoreReg, true);
188 if ((true_val == 1) && (false_val == 0)) {
189 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, 1);
190 OpIT(kCondCc, "");
191 LoadConstant(rl_result.low_reg, 0);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700192 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 } else if (InexpensiveConstantInt(true_val) && InexpensiveConstantInt(false_val)) {
194 OpRegImm(kOpCmp, rl_src.low_reg, 0);
195 OpIT(kCondEq, "E");
196 LoadConstant(rl_result.low_reg, true_val);
197 LoadConstant(rl_result.low_reg, false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700198 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 } else {
200 // Unlikely case - could be tuned.
201 int t_reg1 = AllocTemp();
202 int t_reg2 = AllocTemp();
203 LoadConstant(t_reg1, true_val);
204 LoadConstant(t_reg2, false_val);
205 OpRegImm(kOpCmp, rl_src.low_reg, 0);
206 OpIT(kCondEq, "E");
207 OpRegCopy(rl_result.low_reg, t_reg1);
208 OpRegCopy(rl_result.low_reg, t_reg2);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700209 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 }
211 } else {
212 // MOVE case
213 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
214 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
215 rl_true = LoadValue(rl_true, kCoreReg);
216 rl_false = LoadValue(rl_false, kCoreReg);
217 rl_result = EvalLoc(rl_dest, kCoreReg, true);
218 OpRegImm(kOpCmp, rl_src.low_reg, 0);
buzbee252254b2013-09-08 16:20:53 -0700219 if (rl_result.low_reg == rl_true.low_reg) { // Is the "true" case already in place?
220 OpIT(kCondNe, "");
221 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
222 } else if (rl_result.low_reg == rl_false.low_reg) { // False case in place?
223 OpIT(kCondEq, "");
224 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
225 } else { // Normal - select between the two.
226 OpIT(kCondEq, "E");
227 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
228 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
229 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700230 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 }
232 StoreValue(rl_dest, rl_result);
233}
234
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700235void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
237 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
238 // Normalize such that if either operand is constant, src2 will be constant.
239 ConditionCode ccode = static_cast<ConditionCode>(mir->dalvikInsn.arg[0]);
240 if (rl_src1.is_const) {
241 RegLocation rl_temp = rl_src1;
242 rl_src1 = rl_src2;
243 rl_src2 = rl_temp;
244 ccode = FlipComparisonOrder(ccode);
245 }
246 if (rl_src2.is_const) {
247 RegLocation rl_temp = UpdateLocWide(rl_src2);
248 // Do special compare/branch against simple const operand if not already in registers.
249 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
250 if ((rl_temp.location != kLocPhysReg) &&
251 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
252 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
253 return;
254 }
255 }
buzbee0d829482013-10-11 15:24:55 -0700256 LIR* taken = &block_label_list_[bb->taken];
257 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
259 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
260 OpRegReg(kOpCmp, rl_src1.high_reg, rl_src2.high_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700261 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 case kCondEq:
263 OpCondBranch(kCondNe, not_taken);
264 break;
265 case kCondNe:
266 OpCondBranch(kCondNe, taken);
267 break;
268 case kCondLt:
269 OpCondBranch(kCondLt, taken);
270 OpCondBranch(kCondGt, not_taken);
271 ccode = kCondCc;
272 break;
273 case kCondLe:
274 OpCondBranch(kCondLt, taken);
275 OpCondBranch(kCondGt, not_taken);
276 ccode = kCondLs;
277 break;
278 case kCondGt:
279 OpCondBranch(kCondGt, taken);
280 OpCondBranch(kCondLt, not_taken);
281 ccode = kCondHi;
282 break;
283 case kCondGe:
284 OpCondBranch(kCondGt, taken);
285 OpCondBranch(kCondLt, not_taken);
286 ccode = kCondCs;
287 break;
288 default:
289 LOG(FATAL) << "Unexpected ccode: " << ccode;
290 }
291 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
292 OpCondBranch(ccode, taken);
293}
294
295/*
296 * Generate a register comparison to an immediate and branch. Caller
297 * is responsible for setting branch target field.
298 */
299LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, int reg, int check_value,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700300 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700303 /*
304 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
305 * compare-and-branch if zero is ideal if it will reach. However, because null checks
306 * branch forward to a launch pad, they will frequently not reach - and thus have to
307 * be converted to a long form during assembly (which will trigger another assembly
308 * pass). Here we estimate the branch distance for checks, and if large directly
309 * generate the long form in an attempt to avoid an extra assembly pass.
310 * TODO: consider interspersing launchpads in code following unconditional branches.
311 */
312 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
313 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
314 if (!skip && (ARM_LOWREG(reg)) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
316 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
317 reg, 0);
318 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000319 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 branch = NewLIR2(kThumbBCond, 0, arm_cond);
321 }
322 branch->target = target;
323 return branch;
324}
325
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700326LIR* ArmMir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 LIR* res;
328 int opcode;
329 if (ARM_FPREG(r_dest) || ARM_FPREG(r_src))
330 return OpFpRegCopy(r_dest, r_src);
331 if (ARM_LOWREG(r_dest) && ARM_LOWREG(r_src))
332 opcode = kThumbMovRR;
333 else if (!ARM_LOWREG(r_dest) && !ARM_LOWREG(r_src))
334 opcode = kThumbMovRR_H2H;
335 else if (ARM_LOWREG(r_dest))
336 opcode = kThumbMovRR_H2L;
337 else
338 opcode = kThumbMovRR_L2H;
339 res = RawLIR(current_dalvik_offset_, opcode, r_dest, r_src);
340 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
341 res->flags.is_nop = true;
342 }
343 return res;
344}
345
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700346LIR* ArmMir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
348 AppendLIR(res);
349 return res;
350}
351
352void ArmMir2Lir::OpRegCopyWide(int dest_lo, int dest_hi, int src_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700353 int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 bool dest_fp = ARM_FPREG(dest_lo) && ARM_FPREG(dest_hi);
355 bool src_fp = ARM_FPREG(src_lo) && ARM_FPREG(src_hi);
356 DCHECK_EQ(ARM_FPREG(src_lo), ARM_FPREG(src_hi));
357 DCHECK_EQ(ARM_FPREG(dest_lo), ARM_FPREG(dest_hi));
358 if (dest_fp) {
359 if (src_fp) {
360 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
361 } else {
362 NewLIR3(kThumb2Fmdrr, S2d(dest_lo, dest_hi), src_lo, src_hi);
363 }
364 } else {
365 if (src_fp) {
366 NewLIR3(kThumb2Fmrrd, dest_lo, dest_hi, S2d(src_lo, src_hi));
367 } else {
368 // Handle overlap
369 if (src_hi == dest_lo) {
370 OpRegCopy(dest_hi, src_hi);
371 OpRegCopy(dest_lo, src_lo);
372 } else {
373 OpRegCopy(dest_lo, src_lo);
374 OpRegCopy(dest_hi, src_hi);
375 }
376 }
377 }
378}
379
380// Table of magic divisors
381struct MagicTable {
382 uint32_t magic;
383 uint32_t shift;
384 DividePattern pattern;
385};
386
387static const MagicTable magic_table[] = {
388 {0, 0, DivideNone}, // 0
389 {0, 0, DivideNone}, // 1
390 {0, 0, DivideNone}, // 2
391 {0x55555556, 0, Divide3}, // 3
392 {0, 0, DivideNone}, // 4
393 {0x66666667, 1, Divide5}, // 5
394 {0x2AAAAAAB, 0, Divide3}, // 6
395 {0x92492493, 2, Divide7}, // 7
396 {0, 0, DivideNone}, // 8
397 {0x38E38E39, 1, Divide5}, // 9
398 {0x66666667, 2, Divide5}, // 10
399 {0x2E8BA2E9, 1, Divide5}, // 11
400 {0x2AAAAAAB, 1, Divide5}, // 12
401 {0x4EC4EC4F, 2, Divide5}, // 13
402 {0x92492493, 3, Divide7}, // 14
403 {0x88888889, 3, Divide7}, // 15
404};
405
406// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700407bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700408 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
410 return false;
411 }
412 DividePattern pattern = magic_table[lit].pattern;
413 if (pattern == DivideNone) {
414 return false;
415 }
416 // Tuning: add rem patterns
buzbee11b63d12013-08-27 07:34:17 -0700417 if (!is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 return false;
419 }
420
421 int r_magic = AllocTemp();
422 LoadConstant(r_magic, magic_table[lit].magic);
423 rl_src = LoadValue(rl_src, kCoreReg);
424 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
425 int r_hi = AllocTemp();
426 int r_lo = AllocTemp();
427 NewLIR4(kThumb2Smull, r_lo, r_hi, r_magic, rl_src.low_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700428 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 case Divide3:
430 OpRegRegRegShift(kOpSub, rl_result.low_reg, r_hi,
431 rl_src.low_reg, EncodeShift(kArmAsr, 31));
432 break;
433 case Divide5:
434 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
435 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
436 EncodeShift(kArmAsr, magic_table[lit].shift));
437 break;
438 case Divide7:
439 OpRegReg(kOpAdd, r_hi, rl_src.low_reg);
440 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
441 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
442 EncodeShift(kArmAsr, magic_table[lit].shift));
443 break;
444 default:
445 LOG(FATAL) << "Unexpected pattern: " << pattern;
446 }
447 StoreValue(rl_dest, rl_result);
448 return true;
449}
450
451LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700452 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
454 return NULL;
455}
456
457RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700458 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700459 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
460
461 // Put the literal in a temp.
462 int lit_temp = AllocTemp();
463 LoadConstant(lit_temp, lit);
464 // Use the generic case for div/rem with arg2 in a register.
465 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
466 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
467 FreeTemp(lit_temp);
468
469 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470}
471
472RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700473 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700474 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
475 if (is_div) {
476 // Simple case, use sdiv instruction.
477 OpRegRegReg(kOpDiv, rl_result.low_reg, reg1, reg2);
478 } else {
479 // Remainder case, use the following code:
480 // temp = reg1 / reg2 - integer division
481 // temp = temp * reg2
482 // dest = reg1 - temp
483
484 int temp = AllocTemp();
485 OpRegRegReg(kOpDiv, temp, reg1, reg2);
486 OpRegReg(kOpMul, temp, reg2);
487 OpRegRegReg(kOpSub, rl_result.low_reg, reg1, temp);
488 FreeTemp(temp);
489 }
490
491 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492}
493
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700494bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 DCHECK_EQ(cu_->instruction_set, kThumb2);
496 RegLocation rl_src1 = info->args[0];
497 RegLocation rl_src2 = info->args[1];
498 rl_src1 = LoadValue(rl_src1, kCoreReg);
499 rl_src2 = LoadValue(rl_src2, kCoreReg);
500 RegLocation rl_dest = InlineTarget(info);
501 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
502 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
503 OpIT((is_min) ? kCondGt : kCondLt, "E");
504 OpRegReg(kOpMov, rl_result.low_reg, rl_src2.low_reg);
505 OpRegReg(kOpMov, rl_result.low_reg, rl_src1.low_reg);
506 GenBarrier();
507 StoreValue(rl_dest, rl_result);
508 return true;
509}
510
Vladimir Markoe508a202013-11-04 15:24:22 +0000511bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
512 RegLocation rl_src_address = info->args[0]; // long address
513 rl_src_address.wide = 0; // ignore high half in info->args[1]
514 RegLocation rl_dest = InlineTarget(info);
515 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
516 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
517 if (size == kLong) {
518 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
519 if (rl_address.low_reg != rl_result.low_reg) {
520 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
521 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
522 } else {
523 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
524 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
525 }
526 StoreValueWide(rl_dest, rl_result);
527 } else {
528 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
529 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
530 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
531 StoreValue(rl_dest, rl_result);
532 }
533 return true;
534}
535
536bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
537 RegLocation rl_src_address = info->args[0]; // long address
538 rl_src_address.wide = 0; // ignore high half in info->args[1]
539 RegLocation rl_src_value = info->args[2]; // [size] value
540 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
541 if (size == kLong) {
542 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
543 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
544 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, kWord);
545 StoreBaseDisp(rl_address.low_reg, 4, rl_value.high_reg, kWord);
546 } else {
547 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
548 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
549 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
550 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
551 }
552 return true;
553}
554
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 LOG(FATAL) << "Unexpected use of OpLea for Arm";
557}
558
Ian Rogers468532e2013-08-05 10:56:33 -0700559void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
561}
562
Vladimir Marko1c282e22013-11-21 14:49:47 +0000563bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 DCHECK_EQ(cu_->instruction_set, kThumb2);
565 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000566 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
567 RegLocation rl_src_offset = info->args[2]; // long low
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000569 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000570 // If is_long, high half is in info->args[5]
571 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
572 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 RegLocation rl_dest = InlineTarget(info); // boolean place for result
574
Vladimir Marko3e5af822013-11-21 15:01:20 +0000575 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
576 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
577 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
578 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
579 // into the same temps, reducing the number of required temps down to 5. We shall work
580 // around the potentially locked temp by using LR for r_ptr, unconditionally.
581 // TODO: Pass information about the need for more temps to the stack frame generation
582 // code so that we can rely on being able to allocate enough temps.
583 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
584 MarkTemp(rARM_LR);
585 FreeTemp(rARM_LR);
586 LockTemp(rARM_LR);
587 bool load_early = true;
588 if (is_long) {
589 bool expected_is_core_reg =
590 rl_src_expected.location == kLocPhysReg && !IsFpReg(rl_src_expected.low_reg);
591 bool new_value_is_core_reg =
592 rl_src_new_value.location == kLocPhysReg && !IsFpReg(rl_src_new_value.low_reg);
593 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(rl_src_expected.low_reg);
594 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(rl_src_new_value.low_reg);
595
596 if (!expected_is_good_reg && !new_value_is_good_reg) {
597 // None of expected/new_value is non-temp reg, need to load both late
598 load_early = false;
599 // Make sure they are not in the temp regs and the load will not be skipped.
600 if (expected_is_core_reg) {
601 FlushRegWide(rl_src_expected.low_reg, rl_src_expected.high_reg);
602 ClobberSReg(rl_src_expected.s_reg_low);
603 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
604 rl_src_expected.location = kLocDalvikFrame;
605 }
606 if (new_value_is_core_reg) {
607 FlushRegWide(rl_src_new_value.low_reg, rl_src_new_value.high_reg);
608 ClobberSReg(rl_src_new_value.s_reg_low);
609 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
610 rl_src_new_value.location = kLocDalvikFrame;
611 }
612 }
613 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614
615 // Release store semantics, get the barrier out of the way. TODO: revisit
616 GenMemBarrier(kStoreLoad);
617
618 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000619 RegLocation rl_new_value;
620 if (!is_long) {
621 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
622 } else if (load_early) {
623 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
624 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625
Vladimir Marko1c282e22013-11-21 14:49:47 +0000626 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 // Mark card for object assuming new value is stored.
628 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
629 }
630
631 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
632
Vladimir Marko3e5af822013-11-21 15:01:20 +0000633 int r_ptr = rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 OpRegRegReg(kOpAdd, r_ptr, rl_object.low_reg, rl_offset.low_reg);
635
636 // Free now unneeded rl_object and rl_offset to give more temps.
637 ClobberSReg(rl_object.s_reg_low);
638 FreeTemp(rl_object.low_reg);
639 ClobberSReg(rl_offset.s_reg_low);
640 FreeTemp(rl_offset.low_reg);
641
Vladimir Marko3e5af822013-11-21 15:01:20 +0000642 RegLocation rl_expected;
643 if (!is_long) {
644 rl_expected = LoadValue(rl_src_expected, kCoreReg);
645 } else if (load_early) {
646 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
647 } else {
648 rl_new_value.low_reg = rl_expected.low_reg = AllocTemp();
649 rl_new_value.high_reg = rl_expected.high_reg = AllocTemp();
650 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651
Vladimir Marko3e5af822013-11-21 15:01:20 +0000652 // do {
653 // tmp = [r_ptr] - expected;
654 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
655 // result = tmp != 0;
656
657 int r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700658 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700659
Vladimir Marko3e5af822013-11-21 15:01:20 +0000660 if (is_long) {
661 int r_tmp_high = AllocTemp();
662 if (!load_early) {
663 LoadValueDirectWide(rl_src_expected, rl_expected.low_reg, rl_expected.high_reg);
664 }
665 NewLIR3(kThumb2Ldrexd, r_tmp, r_tmp_high, r_ptr);
666 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
667 OpRegReg(kOpSub, r_tmp_high, rl_expected.high_reg);
668 if (!load_early) {
669 LoadValueDirectWide(rl_src_new_value, rl_new_value.low_reg, rl_new_value.high_reg);
670 }
671 // Make sure we use ORR that sets the ccode
672 if (ARM_LOWREG(r_tmp) && ARM_LOWREG(r_tmp_high)) {
673 NewLIR2(kThumbOrr, r_tmp, r_tmp_high);
674 } else {
675 NewLIR4(kThumb2OrrRRRs, r_tmp, r_tmp, r_tmp_high, 0);
676 }
677 FreeTemp(r_tmp_high); // Now unneeded
678
679 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
680 OpIT(kCondEq, "T");
681 NewLIR4(kThumb2Strexd /* eq */, r_tmp, rl_new_value.low_reg, rl_new_value.high_reg, r_ptr);
682
683 } else {
684 NewLIR3(kThumb2Ldrex, r_tmp, r_ptr, 0);
685 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
686 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
687 OpIT(kCondEq, "T");
688 NewLIR4(kThumb2Strex /* eq */, r_tmp, rl_new_value.low_reg, r_ptr, 0);
689 }
690
691 // Still one conditional left from OpIT(kCondEq, "T") from either branch
692 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700693 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694
Vladimir Marko3e5af822013-11-21 15:01:20 +0000695 if (!load_early) {
696 FreeTemp(rl_expected.low_reg); // Now unneeded.
697 FreeTemp(rl_expected.high_reg); // Now unneeded.
698 }
699
700 // result := (tmp1 != 0) ? 0 : 1;
701 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
702 OpRegRegImm(kOpRsub, rl_result.low_reg, r_tmp, 1);
703 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
704 OpIT(kCondCc, "");
705 LoadConstant(rl_result.low_reg, 0); /* cc */
706 FreeTemp(r_tmp); // Now unneeded.
707
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 StoreValue(rl_dest, rl_result);
709
Vladimir Marko3e5af822013-11-21 15:01:20 +0000710 // Now, restore lr to its non-temp status.
711 Clobber(rARM_LR);
712 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 return true;
714}
715
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700716LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
718}
719
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
722}
723
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700724LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
726}
727
728void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
729 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700730 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 OpRegRegRegShift(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg,
732 EncodeShift(kArmLsl, second_bit - first_bit));
733 if (first_bit != 0) {
734 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
735 }
736}
737
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700738void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 int t_reg = AllocTemp();
740 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
741 FreeTemp(t_reg);
742 GenCheck(kCondEq, kThrowDivZero);
743}
744
745// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700746LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
748 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
749}
750
751// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700752LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 // Combine sub & test using sub setflags encoding here
754 NewLIR3(kThumb2SubsRRI12, reg, reg, 1);
755 return OpCondBranch(c_code, target);
756}
757
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700758void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759#if ANDROID_SMP != 0
760 int dmb_flavor;
761 // TODO: revisit Arm barrier kinds
762 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800763 case kLoadStore: dmb_flavor = kISH; break;
764 case kLoadLoad: dmb_flavor = kISH; break;
765 case kStoreStore: dmb_flavor = kISHST; break;
766 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 default:
768 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
769 dmb_flavor = kSY; // quiet gcc.
770 break;
771 }
772 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700773 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774#endif
775}
776
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700777void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 rl_src = LoadValueWide(rl_src, kCoreReg);
779 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
780 int z_reg = AllocTemp();
781 LoadConstantNoClobber(z_reg, 0);
782 // Check for destructive overlap
783 if (rl_result.low_reg == rl_src.high_reg) {
784 int t_reg = AllocTemp();
785 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
786 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, t_reg);
787 FreeTemp(t_reg);
788 } else {
789 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
790 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, rl_src.high_reg);
791 }
792 FreeTemp(z_reg);
793 StoreValueWide(rl_dest, rl_result);
794}
795
796
797 /*
798 * Check to see if a result pair has a misaligned overlap with an operand pair. This
799 * is not usual for dx to generate, but it is legal (for now). In a future rev of
800 * dex, we'll want to make this case illegal.
801 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700802bool ArmMir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 DCHECK(rl_src.wide);
804 DCHECK(rl_dest.wide);
805 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
806}
807
808void ArmMir2Lir::GenMulLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700809 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 /*
811 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
812 * registers. Normally for Arm, we get 5. We can get to 6 by including
813 * lr in the temp set. The only problematic case is all operands and result are
814 * distinct, and none have been promoted. In that case, we can succeed by aggressively
815 * freeing operand temp registers after they are no longer needed. All other cases
816 * can proceed normally. We'll just punt on the case of the result having a misaligned
817 * overlap with either operand and send that case to a runtime handler.
818 */
819 RegLocation rl_result;
820 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700821 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 FlushAllRegs();
823 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
824 rl_result = GetReturnWide(false);
825 StoreValueWide(rl_dest, rl_result);
826 return;
827 }
828 // Temporarily add LR to the temp pool, and assign it to tmp1
829 MarkTemp(rARM_LR);
830 FreeTemp(rARM_LR);
831 int tmp1 = rARM_LR;
832 LockTemp(rARM_LR);
833
834 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
835 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
836
837 bool special_case = true;
838 // If operands are the same, or any pair has been promoted we're not the special case.
839 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
840 (!IsTemp(rl_src1.low_reg) && !IsTemp(rl_src1.high_reg)) ||
841 (!IsTemp(rl_src2.low_reg) && !IsTemp(rl_src2.high_reg))) {
842 special_case = false;
843 }
844 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
845 int res_lo = AllocTemp();
846 int res_hi;
847 if (rl_src1.low_reg == rl_src2.low_reg) {
848 res_hi = AllocTemp();
849 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.low_reg, rl_src1.high_reg);
850 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.low_reg, rl_src1.low_reg);
851 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
852 } else {
853 // In the special case, all temps are now allocated
854 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.low_reg, rl_src1.high_reg);
855 if (special_case) {
856 DCHECK_NE(rl_src1.low_reg, rl_src2.low_reg);
857 DCHECK_NE(rl_src1.high_reg, rl_src2.high_reg);
858 FreeTemp(rl_src1.high_reg);
859 }
860 res_hi = AllocTemp();
861
862 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.low_reg, rl_src1.low_reg);
863 NewLIR4(kThumb2Mla, tmp1, rl_src1.low_reg, rl_src2.high_reg, tmp1);
864 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
865 if (special_case) {
866 FreeTemp(rl_src1.low_reg);
867 Clobber(rl_src1.low_reg);
868 Clobber(rl_src1.high_reg);
869 }
870 }
871 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700872 rl_result = GetReturnWide(false); // Just using as a template.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 rl_result.low_reg = res_lo;
874 rl_result.high_reg = res_hi;
875 StoreValueWide(rl_dest, rl_result);
876 // Now, restore lr to its non-temp status.
877 Clobber(rARM_LR);
878 UnmarkTemp(rARM_LR);
879}
880
881void ArmMir2Lir::GenAddLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700882 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
884}
885
886void ArmMir2Lir::GenSubLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700887 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
889}
890
891void ArmMir2Lir::GenAndLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700892 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
894}
895
896void ArmMir2Lir::GenOrLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
899}
900
901void ArmMir2Lir::GenXorLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
904}
905
906/*
907 * Generate array load
908 */
909void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700910 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 RegisterClass reg_class = oat_reg_class_by_size(size);
912 int len_offset = mirror::Array::LengthOffset().Int32Value();
913 int data_offset;
914 RegLocation rl_result;
915 bool constant_index = rl_index.is_const;
916 rl_array = LoadValue(rl_array, kCoreReg);
917 if (!constant_index) {
918 rl_index = LoadValue(rl_index, kCoreReg);
919 }
920
921 if (rl_dest.wide) {
922 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
923 } else {
924 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
925 }
926
927 // If index is constant, just fold it into the data offset
928 if (constant_index) {
929 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
930 }
931
932 /* null object? */
933 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
934
935 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
936 int reg_len = INVALID_REG;
937 if (needs_range_check) {
938 reg_len = AllocTemp();
939 /* Get len */
940 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
941 }
942 if (rl_dest.wide || rl_dest.fp || constant_index) {
943 int reg_ptr;
944 if (constant_index) {
945 reg_ptr = rl_array.low_reg; // NOTE: must not alter reg_ptr in constant case.
946 } else {
947 // No special indexed operation, lea + load w/ displacement
948 reg_ptr = AllocTemp();
949 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
950 EncodeShift(kArmLsl, scale));
951 FreeTemp(rl_index.low_reg);
952 }
953 rl_result = EvalLoc(rl_dest, reg_class, true);
954
955 if (needs_range_check) {
956 if (constant_index) {
957 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
958 } else {
959 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
960 }
961 FreeTemp(reg_len);
962 }
963 if (rl_dest.wide) {
964 LoadBaseDispWide(reg_ptr, data_offset, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
965 if (!constant_index) {
966 FreeTemp(reg_ptr);
967 }
968 StoreValueWide(rl_dest, rl_result);
969 } else {
970 LoadBaseDisp(reg_ptr, data_offset, rl_result.low_reg, size, INVALID_SREG);
971 if (!constant_index) {
972 FreeTemp(reg_ptr);
973 }
974 StoreValue(rl_dest, rl_result);
975 }
976 } else {
977 // Offset base, then use indexed load
978 int reg_ptr = AllocTemp();
979 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
980 FreeTemp(rl_array.low_reg);
981 rl_result = EvalLoc(rl_dest, reg_class, true);
982
983 if (needs_range_check) {
984 // TODO: change kCondCS to a more meaningful name, is the sense of
985 // carry-set/clear flipped?
986 GenRegRegCheck(kCondCs, rl_index.low_reg, reg_len, kThrowArrayBounds);
987 FreeTemp(reg_len);
988 }
989 LoadBaseIndexed(reg_ptr, rl_index.low_reg, rl_result.low_reg, scale, size);
990 FreeTemp(reg_ptr);
991 StoreValue(rl_dest, rl_result);
992 }
993}
994
995/*
996 * Generate array store
997 *
998 */
999void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001000 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 RegisterClass reg_class = oat_reg_class_by_size(size);
1002 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 bool constant_index = rl_index.is_const;
1004
Ian Rogersa9a82542013-10-04 11:17:26 -07001005 int data_offset;
1006 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1008 } else {
1009 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1010 }
1011
1012 // If index is constant, just fold it into the data offset.
1013 if (constant_index) {
1014 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1015 }
1016
1017 rl_array = LoadValue(rl_array, kCoreReg);
1018 if (!constant_index) {
1019 rl_index = LoadValue(rl_index, kCoreReg);
1020 }
1021
1022 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001023 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 if (constant_index) {
1025 reg_ptr = rl_array.low_reg;
Ian Rogers379067c2013-10-15 15:06:58 -07001026 } else if (IsTemp(rl_array.low_reg) && !card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 Clobber(rl_array.low_reg);
1028 reg_ptr = rl_array.low_reg;
1029 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001030 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 reg_ptr = AllocTemp();
1032 }
1033
1034 /* null object? */
1035 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1036
1037 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1038 int reg_len = INVALID_REG;
1039 if (needs_range_check) {
1040 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001041 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 /* Get len */
1043 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
1044 }
1045 /* at this point, reg_ptr points to array, 2 live temps */
1046 if (rl_src.wide || rl_src.fp || constant_index) {
1047 if (rl_src.wide) {
1048 rl_src = LoadValueWide(rl_src, reg_class);
1049 } else {
1050 rl_src = LoadValue(rl_src, reg_class);
1051 }
1052 if (!constant_index) {
1053 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
1054 EncodeShift(kArmLsl, scale));
1055 }
1056 if (needs_range_check) {
1057 if (constant_index) {
1058 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1059 } else {
1060 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
1061 }
1062 FreeTemp(reg_len);
1063 }
1064
1065 if (rl_src.wide) {
1066 StoreBaseDispWide(reg_ptr, data_offset, rl_src.low_reg, rl_src.high_reg);
1067 } else {
1068 StoreBaseDisp(reg_ptr, data_offset, rl_src.low_reg, size);
1069 }
1070 } else {
1071 /* reg_ptr -> array data */
1072 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
1073 rl_src = LoadValue(rl_src, reg_class);
1074 if (needs_range_check) {
1075 GenRegRegCheck(kCondCs, rl_index.low_reg, reg_len, kThrowArrayBounds);
1076 FreeTemp(reg_len);
1077 }
1078 StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
1079 scale, size);
1080 }
Ian Rogers773aab12013-10-14 13:50:10 -07001081 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 FreeTemp(reg_ptr);
1083 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001084 if (card_mark) {
1085 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
1087}
1088
Ian Rogersa9a82542013-10-04 11:17:26 -07001089
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001091 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 rl_src = LoadValueWide(rl_src, kCoreReg);
1093 // Per spec, we only care about low 6 bits of shift amount.
1094 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1095 if (shift_amount == 0) {
1096 StoreValueWide(rl_dest, rl_src);
1097 return;
1098 }
1099 if (BadOverlap(rl_src, rl_dest)) {
1100 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1101 return;
1102 }
1103 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001104 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 case Instruction::SHL_LONG:
1106 case Instruction::SHL_LONG_2ADDR:
1107 if (shift_amount == 1) {
1108 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg);
1109 OpRegRegReg(kOpAdc, rl_result.high_reg, rl_src.high_reg, rl_src.high_reg);
1110 } else if (shift_amount == 32) {
1111 OpRegCopy(rl_result.high_reg, rl_src.low_reg);
1112 LoadConstant(rl_result.low_reg, 0);
1113 } else if (shift_amount > 31) {
1114 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.low_reg, shift_amount - 32);
1115 LoadConstant(rl_result.low_reg, 0);
1116 } else {
1117 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.high_reg, shift_amount);
1118 OpRegRegRegShift(kOpOr, rl_result.high_reg, rl_result.high_reg, rl_src.low_reg,
1119 EncodeShift(kArmLsr, 32 - shift_amount));
1120 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, shift_amount);
1121 }
1122 break;
1123 case Instruction::SHR_LONG:
1124 case Instruction::SHR_LONG_2ADDR:
1125 if (shift_amount == 32) {
1126 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1127 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1128 } else if (shift_amount > 31) {
1129 OpRegRegImm(kOpAsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1130 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1131 } else {
1132 int t_reg = AllocTemp();
1133 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1134 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1135 EncodeShift(kArmLsl, 32 - shift_amount));
1136 FreeTemp(t_reg);
1137 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1138 }
1139 break;
1140 case Instruction::USHR_LONG:
1141 case Instruction::USHR_LONG_2ADDR:
1142 if (shift_amount == 32) {
1143 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1144 LoadConstant(rl_result.high_reg, 0);
1145 } else if (shift_amount > 31) {
1146 OpRegRegImm(kOpLsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1147 LoadConstant(rl_result.high_reg, 0);
1148 } else {
1149 int t_reg = AllocTemp();
1150 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1151 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1152 EncodeShift(kArmLsl, 32 - shift_amount));
1153 FreeTemp(t_reg);
1154 OpRegRegImm(kOpLsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1155 }
1156 break;
1157 default:
1158 LOG(FATAL) << "Unexpected case";
1159 }
1160 StoreValueWide(rl_dest, rl_result);
1161}
1162
1163void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001164 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1166 if (!rl_src2.is_const) {
1167 // Don't bother with special handling for subtract from immediate.
1168 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1169 return;
1170 }
1171 } else {
1172 // Normalize
1173 if (!rl_src2.is_const) {
1174 DCHECK(rl_src1.is_const);
1175 RegLocation rl_temp = rl_src1;
1176 rl_src1 = rl_src2;
1177 rl_src2 = rl_temp;
1178 }
1179 }
1180 if (BadOverlap(rl_src1, rl_dest)) {
1181 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1182 return;
1183 }
1184 DCHECK(rl_src2.is_const);
1185 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1186 uint32_t val_lo = Low32Bits(val);
1187 uint32_t val_hi = High32Bits(val);
1188 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1189 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1190
1191 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001192 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 case Instruction::ADD_LONG:
1194 case Instruction::ADD_LONG_2ADDR:
1195 case Instruction::SUB_LONG:
1196 case Instruction::SUB_LONG_2ADDR:
1197 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1198 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1199 return;
1200 }
1201 break;
1202 default:
1203 break;
1204 }
1205 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1206 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1207 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1208 switch (opcode) {
1209 case Instruction::ADD_LONG:
1210 case Instruction::ADD_LONG_2ADDR:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001211 NewLIR3(kThumb2AddRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1212 NewLIR3(kThumb2AdcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 break;
1214 case Instruction::OR_LONG:
1215 case Instruction::OR_LONG_2ADDR:
1216 if ((val_lo != 0) || (rl_result.low_reg != rl_src1.low_reg)) {
1217 OpRegRegImm(kOpOr, rl_result.low_reg, rl_src1.low_reg, val_lo);
1218 }
1219 if ((val_hi != 0) || (rl_result.high_reg != rl_src1.high_reg)) {
1220 OpRegRegImm(kOpOr, rl_result.high_reg, rl_src1.high_reg, val_hi);
1221 }
1222 break;
1223 case Instruction::XOR_LONG:
1224 case Instruction::XOR_LONG_2ADDR:
1225 OpRegRegImm(kOpXor, rl_result.low_reg, rl_src1.low_reg, val_lo);
1226 OpRegRegImm(kOpXor, rl_result.high_reg, rl_src1.high_reg, val_hi);
1227 break;
1228 case Instruction::AND_LONG:
1229 case Instruction::AND_LONG_2ADDR:
1230 if ((val_lo != 0xffffffff) || (rl_result.low_reg != rl_src1.low_reg)) {
1231 OpRegRegImm(kOpAnd, rl_result.low_reg, rl_src1.low_reg, val_lo);
1232 }
1233 if ((val_hi != 0xffffffff) || (rl_result.high_reg != rl_src1.high_reg)) {
1234 OpRegRegImm(kOpAnd, rl_result.high_reg, rl_src1.high_reg, val_hi);
1235 }
1236 break;
1237 case Instruction::SUB_LONG_2ADDR:
1238 case Instruction::SUB_LONG:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001239 NewLIR3(kThumb2SubRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1240 NewLIR3(kThumb2SbcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 break;
1242 default:
1243 LOG(FATAL) << "Unexpected opcode " << opcode;
1244 }
1245 StoreValueWide(rl_dest, rl_result);
1246}
1247
1248} // namespace art