blob: e0161e55fc7d9b78a23d00c2a5692e7783da613c [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
Vladimir Marko58af1f92013-12-19 13:31:15 +0000131 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
132 int t_reg = AllocTemp();
133 NewLIR4(kThumb2OrrRRRs, t_reg, low_reg, high_reg, 0);
134 FreeTemp(t_reg);
135 OpCondBranch(ccode, taken);
136 return;
137 }
138
Brian Carlstromdf629502013-07-17 22:39:56 -0700139 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 case kCondEq:
141 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000142 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 break;
144 case kCondLt:
145 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
146 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000147 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 break;
149 case kCondLe:
150 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
151 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
152 ccode = kCondLs;
153 break;
154 case kCondGt:
155 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
156 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
157 ccode = kCondHi;
158 break;
159 case kCondGe:
160 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
161 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000162 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 break;
164 default:
165 LOG(FATAL) << "Unexpected ccode: " << ccode;
166 }
167 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
168}
169
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700170void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 RegLocation rl_result;
172 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 RegLocation rl_dest = mir_graph_->GetDest(mir);
174 rl_src = LoadValue(rl_src, kCoreReg);
175 if (mir->ssa_rep->num_uses == 1) {
176 // CONST case
177 int true_val = mir->dalvikInsn.vB;
178 int false_val = mir->dalvikInsn.vC;
179 rl_result = EvalLoc(rl_dest, kCoreReg, true);
180 if ((true_val == 1) && (false_val == 0)) {
181 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, 1);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000182 OpIT(kCondUlt, "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 LoadConstant(rl_result.low_reg, 0);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700184 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 } else if (InexpensiveConstantInt(true_val) && InexpensiveConstantInt(false_val)) {
186 OpRegImm(kOpCmp, rl_src.low_reg, 0);
187 OpIT(kCondEq, "E");
188 LoadConstant(rl_result.low_reg, true_val);
189 LoadConstant(rl_result.low_reg, false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700190 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 } else {
192 // Unlikely case - could be tuned.
193 int t_reg1 = AllocTemp();
194 int t_reg2 = AllocTemp();
195 LoadConstant(t_reg1, true_val);
196 LoadConstant(t_reg2, false_val);
197 OpRegImm(kOpCmp, rl_src.low_reg, 0);
198 OpIT(kCondEq, "E");
199 OpRegCopy(rl_result.low_reg, t_reg1);
200 OpRegCopy(rl_result.low_reg, t_reg2);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 }
203 } else {
204 // MOVE case
205 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
206 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
207 rl_true = LoadValue(rl_true, kCoreReg);
208 rl_false = LoadValue(rl_false, kCoreReg);
209 rl_result = EvalLoc(rl_dest, kCoreReg, true);
210 OpRegImm(kOpCmp, rl_src.low_reg, 0);
buzbee252254b2013-09-08 16:20:53 -0700211 if (rl_result.low_reg == rl_true.low_reg) { // Is the "true" case already in place?
212 OpIT(kCondNe, "");
213 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
214 } else if (rl_result.low_reg == rl_false.low_reg) { // False case in place?
215 OpIT(kCondEq, "");
216 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
217 } else { // Normal - select between the two.
218 OpIT(kCondEq, "E");
219 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
220 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
221 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700222 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 }
224 StoreValue(rl_dest, rl_result);
225}
226
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700227void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
229 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
230 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000231 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000233 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 ccode = FlipComparisonOrder(ccode);
235 }
236 if (rl_src2.is_const) {
237 RegLocation rl_temp = UpdateLocWide(rl_src2);
238 // Do special compare/branch against simple const operand if not already in registers.
239 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
240 if ((rl_temp.location != kLocPhysReg) &&
241 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
242 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
243 return;
244 }
245 }
buzbee0d829482013-10-11 15:24:55 -0700246 LIR* taken = &block_label_list_[bb->taken];
247 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
249 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
250 OpRegReg(kOpCmp, rl_src1.high_reg, rl_src2.high_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700251 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 case kCondEq:
253 OpCondBranch(kCondNe, not_taken);
254 break;
255 case kCondNe:
256 OpCondBranch(kCondNe, taken);
257 break;
258 case kCondLt:
259 OpCondBranch(kCondLt, taken);
260 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000261 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 break;
263 case kCondLe:
264 OpCondBranch(kCondLt, taken);
265 OpCondBranch(kCondGt, not_taken);
266 ccode = kCondLs;
267 break;
268 case kCondGt:
269 OpCondBranch(kCondGt, taken);
270 OpCondBranch(kCondLt, not_taken);
271 ccode = kCondHi;
272 break;
273 case kCondGe:
274 OpCondBranch(kCondGt, taken);
275 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000276 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 break;
278 default:
279 LOG(FATAL) << "Unexpected ccode: " << ccode;
280 }
281 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
282 OpCondBranch(ccode, taken);
283}
284
285/*
286 * Generate a register comparison to an immediate and branch. Caller
287 * is responsible for setting branch target field.
288 */
289LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, int reg, int check_value,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700290 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700293 /*
294 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
295 * compare-and-branch if zero is ideal if it will reach. However, because null checks
296 * branch forward to a launch pad, they will frequently not reach - and thus have to
297 * be converted to a long form during assembly (which will trigger another assembly
298 * pass). Here we estimate the branch distance for checks, and if large directly
299 * generate the long form in an attempt to avoid an extra assembly pass.
300 * TODO: consider interspersing launchpads in code following unconditional branches.
301 */
302 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
303 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
304 if (!skip && (ARM_LOWREG(reg)) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
306 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
307 reg, 0);
308 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000309 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 branch = NewLIR2(kThumbBCond, 0, arm_cond);
311 }
312 branch->target = target;
313 return branch;
314}
315
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700316LIR* ArmMir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 LIR* res;
318 int opcode;
319 if (ARM_FPREG(r_dest) || ARM_FPREG(r_src))
320 return OpFpRegCopy(r_dest, r_src);
321 if (ARM_LOWREG(r_dest) && ARM_LOWREG(r_src))
322 opcode = kThumbMovRR;
323 else if (!ARM_LOWREG(r_dest) && !ARM_LOWREG(r_src))
324 opcode = kThumbMovRR_H2H;
325 else if (ARM_LOWREG(r_dest))
326 opcode = kThumbMovRR_H2L;
327 else
328 opcode = kThumbMovRR_L2H;
329 res = RawLIR(current_dalvik_offset_, opcode, r_dest, r_src);
330 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
331 res->flags.is_nop = true;
332 }
333 return res;
334}
335
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700336LIR* ArmMir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
338 AppendLIR(res);
339 return res;
340}
341
342void ArmMir2Lir::OpRegCopyWide(int dest_lo, int dest_hi, int src_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700343 int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 bool dest_fp = ARM_FPREG(dest_lo) && ARM_FPREG(dest_hi);
345 bool src_fp = ARM_FPREG(src_lo) && ARM_FPREG(src_hi);
346 DCHECK_EQ(ARM_FPREG(src_lo), ARM_FPREG(src_hi));
347 DCHECK_EQ(ARM_FPREG(dest_lo), ARM_FPREG(dest_hi));
348 if (dest_fp) {
349 if (src_fp) {
350 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
351 } else {
352 NewLIR3(kThumb2Fmdrr, S2d(dest_lo, dest_hi), src_lo, src_hi);
353 }
354 } else {
355 if (src_fp) {
356 NewLIR3(kThumb2Fmrrd, dest_lo, dest_hi, S2d(src_lo, src_hi));
357 } else {
358 // Handle overlap
359 if (src_hi == dest_lo) {
360 OpRegCopy(dest_hi, src_hi);
361 OpRegCopy(dest_lo, src_lo);
362 } else {
363 OpRegCopy(dest_lo, src_lo);
364 OpRegCopy(dest_hi, src_hi);
365 }
366 }
367 }
368}
369
370// Table of magic divisors
371struct MagicTable {
372 uint32_t magic;
373 uint32_t shift;
374 DividePattern pattern;
375};
376
377static const MagicTable magic_table[] = {
378 {0, 0, DivideNone}, // 0
379 {0, 0, DivideNone}, // 1
380 {0, 0, DivideNone}, // 2
381 {0x55555556, 0, Divide3}, // 3
382 {0, 0, DivideNone}, // 4
383 {0x66666667, 1, Divide5}, // 5
384 {0x2AAAAAAB, 0, Divide3}, // 6
385 {0x92492493, 2, Divide7}, // 7
386 {0, 0, DivideNone}, // 8
387 {0x38E38E39, 1, Divide5}, // 9
388 {0x66666667, 2, Divide5}, // 10
389 {0x2E8BA2E9, 1, Divide5}, // 11
390 {0x2AAAAAAB, 1, Divide5}, // 12
391 {0x4EC4EC4F, 2, Divide5}, // 13
392 {0x92492493, 3, Divide7}, // 14
393 {0x88888889, 3, Divide7}, // 15
394};
395
396// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700397bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700398 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
400 return false;
401 }
402 DividePattern pattern = magic_table[lit].pattern;
403 if (pattern == DivideNone) {
404 return false;
405 }
406 // Tuning: add rem patterns
buzbee11b63d12013-08-27 07:34:17 -0700407 if (!is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 return false;
409 }
410
411 int r_magic = AllocTemp();
412 LoadConstant(r_magic, magic_table[lit].magic);
413 rl_src = LoadValue(rl_src, kCoreReg);
414 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
415 int r_hi = AllocTemp();
416 int r_lo = AllocTemp();
417 NewLIR4(kThumb2Smull, r_lo, r_hi, r_magic, rl_src.low_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700418 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 case Divide3:
420 OpRegRegRegShift(kOpSub, rl_result.low_reg, r_hi,
421 rl_src.low_reg, EncodeShift(kArmAsr, 31));
422 break;
423 case Divide5:
424 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
425 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
426 EncodeShift(kArmAsr, magic_table[lit].shift));
427 break;
428 case Divide7:
429 OpRegReg(kOpAdd, r_hi, rl_src.low_reg);
430 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
431 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
432 EncodeShift(kArmAsr, magic_table[lit].shift));
433 break;
434 default:
435 LOG(FATAL) << "Unexpected pattern: " << pattern;
436 }
437 StoreValue(rl_dest, rl_result);
438 return true;
439}
440
441LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700442 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
444 return NULL;
445}
446
447RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700448 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700449 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
450
451 // Put the literal in a temp.
452 int lit_temp = AllocTemp();
453 LoadConstant(lit_temp, lit);
454 // Use the generic case for div/rem with arg2 in a register.
455 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
456 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
457 FreeTemp(lit_temp);
458
459 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460}
461
462RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700463 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700464 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
465 if (is_div) {
466 // Simple case, use sdiv instruction.
467 OpRegRegReg(kOpDiv, rl_result.low_reg, reg1, reg2);
468 } else {
469 // Remainder case, use the following code:
470 // temp = reg1 / reg2 - integer division
471 // temp = temp * reg2
472 // dest = reg1 - temp
473
474 int temp = AllocTemp();
475 OpRegRegReg(kOpDiv, temp, reg1, reg2);
476 OpRegReg(kOpMul, temp, reg2);
477 OpRegRegReg(kOpSub, rl_result.low_reg, reg1, temp);
478 FreeTemp(temp);
479 }
480
481 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482}
483
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700484bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 DCHECK_EQ(cu_->instruction_set, kThumb2);
486 RegLocation rl_src1 = info->args[0];
487 RegLocation rl_src2 = info->args[1];
488 rl_src1 = LoadValue(rl_src1, kCoreReg);
489 rl_src2 = LoadValue(rl_src2, kCoreReg);
490 RegLocation rl_dest = InlineTarget(info);
491 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
492 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
493 OpIT((is_min) ? kCondGt : kCondLt, "E");
494 OpRegReg(kOpMov, rl_result.low_reg, rl_src2.low_reg);
495 OpRegReg(kOpMov, rl_result.low_reg, rl_src1.low_reg);
496 GenBarrier();
497 StoreValue(rl_dest, rl_result);
498 return true;
499}
500
Vladimir Markoe508a202013-11-04 15:24:22 +0000501bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
502 RegLocation rl_src_address = info->args[0]; // long address
503 rl_src_address.wide = 0; // ignore high half in info->args[1]
504 RegLocation rl_dest = InlineTarget(info);
505 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
506 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
507 if (size == kLong) {
508 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
509 if (rl_address.low_reg != rl_result.low_reg) {
510 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
511 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
512 } else {
513 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
514 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
515 }
516 StoreValueWide(rl_dest, rl_result);
517 } else {
518 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
519 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
520 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
521 StoreValue(rl_dest, rl_result);
522 }
523 return true;
524}
525
526bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
527 RegLocation rl_src_address = info->args[0]; // long address
528 rl_src_address.wide = 0; // ignore high half in info->args[1]
529 RegLocation rl_src_value = info->args[2]; // [size] value
530 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
531 if (size == kLong) {
532 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
533 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
534 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, kWord);
535 StoreBaseDisp(rl_address.low_reg, 4, rl_value.high_reg, kWord);
536 } else {
537 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
538 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
539 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
540 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
541 }
542 return true;
543}
544
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700545void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 LOG(FATAL) << "Unexpected use of OpLea for Arm";
547}
548
Ian Rogers468532e2013-08-05 10:56:33 -0700549void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
551}
552
Vladimir Marko1c282e22013-11-21 14:49:47 +0000553bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 DCHECK_EQ(cu_->instruction_set, kThumb2);
555 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000556 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
557 RegLocation rl_src_offset = info->args[2]; // long low
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000559 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000560 // If is_long, high half is in info->args[5]
561 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
562 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 RegLocation rl_dest = InlineTarget(info); // boolean place for result
564
Vladimir Marko3e5af822013-11-21 15:01:20 +0000565 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
566 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
567 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
568 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
569 // into the same temps, reducing the number of required temps down to 5. We shall work
570 // around the potentially locked temp by using LR for r_ptr, unconditionally.
571 // TODO: Pass information about the need for more temps to the stack frame generation
572 // code so that we can rely on being able to allocate enough temps.
573 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
574 MarkTemp(rARM_LR);
575 FreeTemp(rARM_LR);
576 LockTemp(rARM_LR);
577 bool load_early = true;
578 if (is_long) {
579 bool expected_is_core_reg =
580 rl_src_expected.location == kLocPhysReg && !IsFpReg(rl_src_expected.low_reg);
581 bool new_value_is_core_reg =
582 rl_src_new_value.location == kLocPhysReg && !IsFpReg(rl_src_new_value.low_reg);
583 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(rl_src_expected.low_reg);
584 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(rl_src_new_value.low_reg);
585
586 if (!expected_is_good_reg && !new_value_is_good_reg) {
587 // None of expected/new_value is non-temp reg, need to load both late
588 load_early = false;
589 // Make sure they are not in the temp regs and the load will not be skipped.
590 if (expected_is_core_reg) {
591 FlushRegWide(rl_src_expected.low_reg, rl_src_expected.high_reg);
592 ClobberSReg(rl_src_expected.s_reg_low);
593 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
594 rl_src_expected.location = kLocDalvikFrame;
595 }
596 if (new_value_is_core_reg) {
597 FlushRegWide(rl_src_new_value.low_reg, rl_src_new_value.high_reg);
598 ClobberSReg(rl_src_new_value.s_reg_low);
599 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
600 rl_src_new_value.location = kLocDalvikFrame;
601 }
602 }
603 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604
605 // Release store semantics, get the barrier out of the way. TODO: revisit
606 GenMemBarrier(kStoreLoad);
607
608 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000609 RegLocation rl_new_value;
610 if (!is_long) {
611 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
612 } else if (load_early) {
613 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
614 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615
Vladimir Marko1c282e22013-11-21 14:49:47 +0000616 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 // Mark card for object assuming new value is stored.
618 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
619 }
620
621 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
622
Vladimir Marko3e5af822013-11-21 15:01:20 +0000623 int r_ptr = rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 OpRegRegReg(kOpAdd, r_ptr, rl_object.low_reg, rl_offset.low_reg);
625
626 // Free now unneeded rl_object and rl_offset to give more temps.
627 ClobberSReg(rl_object.s_reg_low);
628 FreeTemp(rl_object.low_reg);
629 ClobberSReg(rl_offset.s_reg_low);
630 FreeTemp(rl_offset.low_reg);
631
Vladimir Marko3e5af822013-11-21 15:01:20 +0000632 RegLocation rl_expected;
633 if (!is_long) {
634 rl_expected = LoadValue(rl_src_expected, kCoreReg);
635 } else if (load_early) {
636 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
637 } else {
638 rl_new_value.low_reg = rl_expected.low_reg = AllocTemp();
639 rl_new_value.high_reg = rl_expected.high_reg = AllocTemp();
640 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641
Vladimir Marko3e5af822013-11-21 15:01:20 +0000642 // do {
643 // tmp = [r_ptr] - expected;
644 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
645 // result = tmp != 0;
646
647 int r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700648 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700649
Vladimir Marko3e5af822013-11-21 15:01:20 +0000650 if (is_long) {
651 int r_tmp_high = AllocTemp();
652 if (!load_early) {
653 LoadValueDirectWide(rl_src_expected, rl_expected.low_reg, rl_expected.high_reg);
654 }
655 NewLIR3(kThumb2Ldrexd, r_tmp, r_tmp_high, r_ptr);
656 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
657 OpRegReg(kOpSub, r_tmp_high, rl_expected.high_reg);
658 if (!load_early) {
659 LoadValueDirectWide(rl_src_new_value, rl_new_value.low_reg, rl_new_value.high_reg);
660 }
661 // Make sure we use ORR that sets the ccode
662 if (ARM_LOWREG(r_tmp) && ARM_LOWREG(r_tmp_high)) {
663 NewLIR2(kThumbOrr, r_tmp, r_tmp_high);
664 } else {
665 NewLIR4(kThumb2OrrRRRs, r_tmp, r_tmp, r_tmp_high, 0);
666 }
667 FreeTemp(r_tmp_high); // Now unneeded
668
669 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
670 OpIT(kCondEq, "T");
671 NewLIR4(kThumb2Strexd /* eq */, r_tmp, rl_new_value.low_reg, rl_new_value.high_reg, r_ptr);
672
673 } else {
674 NewLIR3(kThumb2Ldrex, r_tmp, r_ptr, 0);
675 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
676 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
677 OpIT(kCondEq, "T");
678 NewLIR4(kThumb2Strex /* eq */, r_tmp, rl_new_value.low_reg, r_ptr, 0);
679 }
680
681 // Still one conditional left from OpIT(kCondEq, "T") from either branch
682 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700683 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684
Vladimir Marko3e5af822013-11-21 15:01:20 +0000685 if (!load_early) {
686 FreeTemp(rl_expected.low_reg); // Now unneeded.
687 FreeTemp(rl_expected.high_reg); // Now unneeded.
688 }
689
690 // result := (tmp1 != 0) ? 0 : 1;
691 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
692 OpRegRegImm(kOpRsub, rl_result.low_reg, r_tmp, 1);
693 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000694 OpIT(kCondUlt, "");
Vladimir Marko3e5af822013-11-21 15:01:20 +0000695 LoadConstant(rl_result.low_reg, 0); /* cc */
696 FreeTemp(r_tmp); // Now unneeded.
697
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 StoreValue(rl_dest, rl_result);
699
Vladimir Marko3e5af822013-11-21 15:01:20 +0000700 // Now, restore lr to its non-temp status.
701 Clobber(rARM_LR);
702 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 return true;
704}
705
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
708}
709
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700710LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
712}
713
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700714LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
716}
717
718void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
719 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 OpRegRegRegShift(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg,
722 EncodeShift(kArmLsl, second_bit - first_bit));
723 if (first_bit != 0) {
724 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
725 }
726}
727
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700728void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 int t_reg = AllocTemp();
730 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
731 FreeTemp(t_reg);
732 GenCheck(kCondEq, kThrowDivZero);
733}
734
735// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700736LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
738 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
739}
740
741// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700742LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 // Combine sub & test using sub setflags encoding here
744 NewLIR3(kThumb2SubsRRI12, reg, reg, 1);
745 return OpCondBranch(c_code, target);
746}
747
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700748void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749#if ANDROID_SMP != 0
750 int dmb_flavor;
751 // TODO: revisit Arm barrier kinds
752 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800753 case kLoadStore: dmb_flavor = kISH; break;
754 case kLoadLoad: dmb_flavor = kISH; break;
755 case kStoreStore: dmb_flavor = kISHST; break;
756 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 default:
758 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
759 dmb_flavor = kSY; // quiet gcc.
760 break;
761 }
762 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700763 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764#endif
765}
766
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700767void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 rl_src = LoadValueWide(rl_src, kCoreReg);
769 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
770 int z_reg = AllocTemp();
771 LoadConstantNoClobber(z_reg, 0);
772 // Check for destructive overlap
773 if (rl_result.low_reg == rl_src.high_reg) {
774 int t_reg = AllocTemp();
775 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
776 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, t_reg);
777 FreeTemp(t_reg);
778 } else {
779 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
780 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, rl_src.high_reg);
781 }
782 FreeTemp(z_reg);
783 StoreValueWide(rl_dest, rl_result);
784}
785
786
787 /*
788 * Check to see if a result pair has a misaligned overlap with an operand pair. This
789 * is not usual for dx to generate, but it is legal (for now). In a future rev of
790 * dex, we'll want to make this case illegal.
791 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700792bool ArmMir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 DCHECK(rl_src.wide);
794 DCHECK(rl_dest.wide);
795 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
796}
797
798void ArmMir2Lir::GenMulLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700799 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 /*
801 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
802 * registers. Normally for Arm, we get 5. We can get to 6 by including
803 * lr in the temp set. The only problematic case is all operands and result are
804 * distinct, and none have been promoted. In that case, we can succeed by aggressively
805 * freeing operand temp registers after they are no longer needed. All other cases
806 * can proceed normally. We'll just punt on the case of the result having a misaligned
807 * overlap with either operand and send that case to a runtime handler.
808 */
809 RegLocation rl_result;
810 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700811 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 FlushAllRegs();
813 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
814 rl_result = GetReturnWide(false);
815 StoreValueWide(rl_dest, rl_result);
816 return;
817 }
818 // Temporarily add LR to the temp pool, and assign it to tmp1
819 MarkTemp(rARM_LR);
820 FreeTemp(rARM_LR);
821 int tmp1 = rARM_LR;
822 LockTemp(rARM_LR);
823
824 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
825 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
826
827 bool special_case = true;
828 // If operands are the same, or any pair has been promoted we're not the special case.
829 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
830 (!IsTemp(rl_src1.low_reg) && !IsTemp(rl_src1.high_reg)) ||
831 (!IsTemp(rl_src2.low_reg) && !IsTemp(rl_src2.high_reg))) {
832 special_case = false;
833 }
834 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
835 int res_lo = AllocTemp();
836 int res_hi;
837 if (rl_src1.low_reg == rl_src2.low_reg) {
838 res_hi = AllocTemp();
839 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.low_reg, rl_src1.high_reg);
840 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.low_reg, rl_src1.low_reg);
841 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
842 } else {
843 // In the special case, all temps are now allocated
844 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.low_reg, rl_src1.high_reg);
845 if (special_case) {
846 DCHECK_NE(rl_src1.low_reg, rl_src2.low_reg);
847 DCHECK_NE(rl_src1.high_reg, rl_src2.high_reg);
848 FreeTemp(rl_src1.high_reg);
849 }
850 res_hi = AllocTemp();
851
852 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.low_reg, rl_src1.low_reg);
853 NewLIR4(kThumb2Mla, tmp1, rl_src1.low_reg, rl_src2.high_reg, tmp1);
854 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
855 if (special_case) {
856 FreeTemp(rl_src1.low_reg);
857 Clobber(rl_src1.low_reg);
858 Clobber(rl_src1.high_reg);
859 }
860 }
861 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700862 rl_result = GetReturnWide(false); // Just using as a template.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 rl_result.low_reg = res_lo;
864 rl_result.high_reg = res_hi;
865 StoreValueWide(rl_dest, rl_result);
866 // Now, restore lr to its non-temp status.
867 Clobber(rARM_LR);
868 UnmarkTemp(rARM_LR);
869}
870
871void ArmMir2Lir::GenAddLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700872 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
874}
875
876void ArmMir2Lir::GenSubLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700877 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
879}
880
881void ArmMir2Lir::GenAndLong(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 GenAndLong for Arm";
884}
885
886void ArmMir2Lir::GenOrLong(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 GenOrLong for Arm";
889}
890
891void ArmMir2Lir::GenXorLong(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 genXoLong for Arm";
894}
895
896/*
897 * Generate array load
898 */
899void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700900 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 RegisterClass reg_class = oat_reg_class_by_size(size);
902 int len_offset = mirror::Array::LengthOffset().Int32Value();
903 int data_offset;
904 RegLocation rl_result;
905 bool constant_index = rl_index.is_const;
906 rl_array = LoadValue(rl_array, kCoreReg);
907 if (!constant_index) {
908 rl_index = LoadValue(rl_index, kCoreReg);
909 }
910
911 if (rl_dest.wide) {
912 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
913 } else {
914 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
915 }
916
917 // If index is constant, just fold it into the data offset
918 if (constant_index) {
919 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
920 }
921
922 /* null object? */
923 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
924
925 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
926 int reg_len = INVALID_REG;
927 if (needs_range_check) {
928 reg_len = AllocTemp();
929 /* Get len */
930 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
931 }
932 if (rl_dest.wide || rl_dest.fp || constant_index) {
933 int reg_ptr;
934 if (constant_index) {
935 reg_ptr = rl_array.low_reg; // NOTE: must not alter reg_ptr in constant case.
936 } else {
937 // No special indexed operation, lea + load w/ displacement
938 reg_ptr = AllocTemp();
939 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
940 EncodeShift(kArmLsl, scale));
941 FreeTemp(rl_index.low_reg);
942 }
943 rl_result = EvalLoc(rl_dest, reg_class, true);
944
945 if (needs_range_check) {
946 if (constant_index) {
947 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
948 } else {
949 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
950 }
951 FreeTemp(reg_len);
952 }
953 if (rl_dest.wide) {
954 LoadBaseDispWide(reg_ptr, data_offset, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
955 if (!constant_index) {
956 FreeTemp(reg_ptr);
957 }
958 StoreValueWide(rl_dest, rl_result);
959 } else {
960 LoadBaseDisp(reg_ptr, data_offset, rl_result.low_reg, size, INVALID_SREG);
961 if (!constant_index) {
962 FreeTemp(reg_ptr);
963 }
964 StoreValue(rl_dest, rl_result);
965 }
966 } else {
967 // Offset base, then use indexed load
968 int reg_ptr = AllocTemp();
969 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
970 FreeTemp(rl_array.low_reg);
971 rl_result = EvalLoc(rl_dest, reg_class, true);
972
973 if (needs_range_check) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000974 GenRegRegCheck(kCondUge, rl_index.low_reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 FreeTemp(reg_len);
976 }
977 LoadBaseIndexed(reg_ptr, rl_index.low_reg, rl_result.low_reg, scale, size);
978 FreeTemp(reg_ptr);
979 StoreValue(rl_dest, rl_result);
980 }
981}
982
983/*
984 * Generate array store
985 *
986 */
987void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700988 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 RegisterClass reg_class = oat_reg_class_by_size(size);
990 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 bool constant_index = rl_index.is_const;
992
Ian Rogersa9a82542013-10-04 11:17:26 -0700993 int data_offset;
994 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
996 } else {
997 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
998 }
999
1000 // If index is constant, just fold it into the data offset.
1001 if (constant_index) {
1002 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1003 }
1004
1005 rl_array = LoadValue(rl_array, kCoreReg);
1006 if (!constant_index) {
1007 rl_index = LoadValue(rl_index, kCoreReg);
1008 }
1009
1010 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001011 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 if (constant_index) {
1013 reg_ptr = rl_array.low_reg;
Ian Rogers379067c2013-10-15 15:06:58 -07001014 } else if (IsTemp(rl_array.low_reg) && !card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 Clobber(rl_array.low_reg);
1016 reg_ptr = rl_array.low_reg;
1017 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001018 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 reg_ptr = AllocTemp();
1020 }
1021
1022 /* null object? */
1023 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1024
1025 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1026 int reg_len = INVALID_REG;
1027 if (needs_range_check) {
1028 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001029 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 /* Get len */
1031 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
1032 }
1033 /* at this point, reg_ptr points to array, 2 live temps */
1034 if (rl_src.wide || rl_src.fp || constant_index) {
1035 if (rl_src.wide) {
1036 rl_src = LoadValueWide(rl_src, reg_class);
1037 } else {
1038 rl_src = LoadValue(rl_src, reg_class);
1039 }
1040 if (!constant_index) {
1041 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
1042 EncodeShift(kArmLsl, scale));
1043 }
1044 if (needs_range_check) {
1045 if (constant_index) {
1046 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1047 } else {
1048 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
1049 }
1050 FreeTemp(reg_len);
1051 }
1052
1053 if (rl_src.wide) {
1054 StoreBaseDispWide(reg_ptr, data_offset, rl_src.low_reg, rl_src.high_reg);
1055 } else {
1056 StoreBaseDisp(reg_ptr, data_offset, rl_src.low_reg, size);
1057 }
1058 } else {
1059 /* reg_ptr -> array data */
1060 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
1061 rl_src = LoadValue(rl_src, reg_class);
1062 if (needs_range_check) {
Vladimir Marko58af1f92013-12-19 13:31:15 +00001063 GenRegRegCheck(kCondUge, rl_index.low_reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 FreeTemp(reg_len);
1065 }
1066 StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
1067 scale, size);
1068 }
Ian Rogers773aab12013-10-14 13:50:10 -07001069 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 FreeTemp(reg_ptr);
1071 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001072 if (card_mark) {
1073 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 }
1075}
1076
Ian Rogersa9a82542013-10-04 11:17:26 -07001077
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001079 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 rl_src = LoadValueWide(rl_src, kCoreReg);
1081 // Per spec, we only care about low 6 bits of shift amount.
1082 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1083 if (shift_amount == 0) {
1084 StoreValueWide(rl_dest, rl_src);
1085 return;
1086 }
1087 if (BadOverlap(rl_src, rl_dest)) {
1088 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1089 return;
1090 }
1091 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001092 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 case Instruction::SHL_LONG:
1094 case Instruction::SHL_LONG_2ADDR:
1095 if (shift_amount == 1) {
1096 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg);
1097 OpRegRegReg(kOpAdc, rl_result.high_reg, rl_src.high_reg, rl_src.high_reg);
1098 } else if (shift_amount == 32) {
1099 OpRegCopy(rl_result.high_reg, rl_src.low_reg);
1100 LoadConstant(rl_result.low_reg, 0);
1101 } else if (shift_amount > 31) {
1102 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.low_reg, shift_amount - 32);
1103 LoadConstant(rl_result.low_reg, 0);
1104 } else {
1105 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.high_reg, shift_amount);
1106 OpRegRegRegShift(kOpOr, rl_result.high_reg, rl_result.high_reg, rl_src.low_reg,
1107 EncodeShift(kArmLsr, 32 - shift_amount));
1108 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, shift_amount);
1109 }
1110 break;
1111 case Instruction::SHR_LONG:
1112 case Instruction::SHR_LONG_2ADDR:
1113 if (shift_amount == 32) {
1114 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1115 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1116 } else if (shift_amount > 31) {
1117 OpRegRegImm(kOpAsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1118 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1119 } else {
1120 int t_reg = AllocTemp();
1121 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1122 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1123 EncodeShift(kArmLsl, 32 - shift_amount));
1124 FreeTemp(t_reg);
1125 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1126 }
1127 break;
1128 case Instruction::USHR_LONG:
1129 case Instruction::USHR_LONG_2ADDR:
1130 if (shift_amount == 32) {
1131 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1132 LoadConstant(rl_result.high_reg, 0);
1133 } else if (shift_amount > 31) {
1134 OpRegRegImm(kOpLsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1135 LoadConstant(rl_result.high_reg, 0);
1136 } else {
1137 int t_reg = AllocTemp();
1138 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1139 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1140 EncodeShift(kArmLsl, 32 - shift_amount));
1141 FreeTemp(t_reg);
1142 OpRegRegImm(kOpLsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1143 }
1144 break;
1145 default:
1146 LOG(FATAL) << "Unexpected case";
1147 }
1148 StoreValueWide(rl_dest, rl_result);
1149}
1150
1151void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001152 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1154 if (!rl_src2.is_const) {
1155 // Don't bother with special handling for subtract from immediate.
1156 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1157 return;
1158 }
1159 } else {
1160 // Normalize
1161 if (!rl_src2.is_const) {
1162 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001163 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 }
1166 if (BadOverlap(rl_src1, rl_dest)) {
1167 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1168 return;
1169 }
1170 DCHECK(rl_src2.is_const);
1171 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1172 uint32_t val_lo = Low32Bits(val);
1173 uint32_t val_hi = High32Bits(val);
1174 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1175 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1176
1177 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001178 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 case Instruction::ADD_LONG:
1180 case Instruction::ADD_LONG_2ADDR:
1181 case Instruction::SUB_LONG:
1182 case Instruction::SUB_LONG_2ADDR:
1183 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1184 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1185 return;
1186 }
1187 break;
1188 default:
1189 break;
1190 }
1191 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1192 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1193 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1194 switch (opcode) {
1195 case Instruction::ADD_LONG:
1196 case Instruction::ADD_LONG_2ADDR:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001197 NewLIR3(kThumb2AddRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1198 NewLIR3(kThumb2AdcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 break;
1200 case Instruction::OR_LONG:
1201 case Instruction::OR_LONG_2ADDR:
1202 if ((val_lo != 0) || (rl_result.low_reg != rl_src1.low_reg)) {
1203 OpRegRegImm(kOpOr, rl_result.low_reg, rl_src1.low_reg, val_lo);
1204 }
1205 if ((val_hi != 0) || (rl_result.high_reg != rl_src1.high_reg)) {
1206 OpRegRegImm(kOpOr, rl_result.high_reg, rl_src1.high_reg, val_hi);
1207 }
1208 break;
1209 case Instruction::XOR_LONG:
1210 case Instruction::XOR_LONG_2ADDR:
1211 OpRegRegImm(kOpXor, rl_result.low_reg, rl_src1.low_reg, val_lo);
1212 OpRegRegImm(kOpXor, rl_result.high_reg, rl_src1.high_reg, val_hi);
1213 break;
1214 case Instruction::AND_LONG:
1215 case Instruction::AND_LONG_2ADDR:
1216 if ((val_lo != 0xffffffff) || (rl_result.low_reg != rl_src1.low_reg)) {
1217 OpRegRegImm(kOpAnd, rl_result.low_reg, rl_src1.low_reg, val_lo);
1218 }
1219 if ((val_hi != 0xffffffff) || (rl_result.high_reg != rl_src1.high_reg)) {
1220 OpRegRegImm(kOpAnd, rl_result.high_reg, rl_src1.high_reg, val_hi);
1221 }
1222 break;
1223 case Instruction::SUB_LONG_2ADDR:
1224 case Instruction::SUB_LONG:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001225 NewLIR3(kThumb2SubRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1226 NewLIR3(kThumb2SbcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 break;
1228 default:
1229 LOG(FATAL) << "Unexpected opcode " << opcode;
1230 }
1231 StoreValueWide(rl_dest, rl_result);
1232}
1233
1234} // namespace art