blob: 150794e75dd8488844b9f9283896ba356945eb69 [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
Mark Mendell2bf31e62014-01-23 12:13:40 -0800447RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
448 RegLocation rl_src2, bool is_div, bool check_zero) {
449 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
450 return rl_dest;
451}
452
453RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
454 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
455 return rl_dest;
456}
457
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700460 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
461
462 // Put the literal in a temp.
463 int lit_temp = AllocTemp();
464 LoadConstant(lit_temp, lit);
465 // Use the generic case for div/rem with arg2 in a register.
466 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
467 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
468 FreeTemp(lit_temp);
469
470 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471}
472
473RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700474 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700475 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
476 if (is_div) {
477 // Simple case, use sdiv instruction.
478 OpRegRegReg(kOpDiv, rl_result.low_reg, reg1, reg2);
479 } else {
480 // Remainder case, use the following code:
481 // temp = reg1 / reg2 - integer division
482 // temp = temp * reg2
483 // dest = reg1 - temp
484
485 int temp = AllocTemp();
486 OpRegRegReg(kOpDiv, temp, reg1, reg2);
487 OpRegReg(kOpMul, temp, reg2);
488 OpRegRegReg(kOpSub, rl_result.low_reg, reg1, temp);
489 FreeTemp(temp);
490 }
491
492 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493}
494
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700495bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 DCHECK_EQ(cu_->instruction_set, kThumb2);
497 RegLocation rl_src1 = info->args[0];
498 RegLocation rl_src2 = info->args[1];
499 rl_src1 = LoadValue(rl_src1, kCoreReg);
500 rl_src2 = LoadValue(rl_src2, kCoreReg);
501 RegLocation rl_dest = InlineTarget(info);
502 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
503 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
504 OpIT((is_min) ? kCondGt : kCondLt, "E");
505 OpRegReg(kOpMov, rl_result.low_reg, rl_src2.low_reg);
506 OpRegReg(kOpMov, rl_result.low_reg, rl_src1.low_reg);
507 GenBarrier();
508 StoreValue(rl_dest, rl_result);
509 return true;
510}
511
Vladimir Markoe508a202013-11-04 15:24:22 +0000512bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
513 RegLocation rl_src_address = info->args[0]; // long address
514 rl_src_address.wide = 0; // ignore high half in info->args[1]
515 RegLocation rl_dest = InlineTarget(info);
516 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
517 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
518 if (size == kLong) {
519 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
520 if (rl_address.low_reg != rl_result.low_reg) {
521 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
522 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
523 } else {
524 LoadBaseDisp(rl_address.low_reg, 4, rl_result.high_reg, kWord, INVALID_SREG);
525 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, kWord, INVALID_SREG);
526 }
527 StoreValueWide(rl_dest, rl_result);
528 } else {
529 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
530 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
531 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
532 StoreValue(rl_dest, rl_result);
533 }
534 return true;
535}
536
537bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
538 RegLocation rl_src_address = info->args[0]; // long address
539 rl_src_address.wide = 0; // ignore high half in info->args[1]
540 RegLocation rl_src_value = info->args[2]; // [size] value
541 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
542 if (size == kLong) {
543 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
544 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
545 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, kWord);
546 StoreBaseDisp(rl_address.low_reg, 4, rl_value.high_reg, kWord);
547 } else {
548 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
549 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
550 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
551 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
552 }
553 return true;
554}
555
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700556void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 LOG(FATAL) << "Unexpected use of OpLea for Arm";
558}
559
Ian Rogers468532e2013-08-05 10:56:33 -0700560void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
562}
563
Vladimir Marko1c282e22013-11-21 14:49:47 +0000564bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 DCHECK_EQ(cu_->instruction_set, kThumb2);
566 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000567 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
568 RegLocation rl_src_offset = info->args[2]; // long low
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000570 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000571 // If is_long, high half is in info->args[5]
572 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
573 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 RegLocation rl_dest = InlineTarget(info); // boolean place for result
575
Vladimir Marko3e5af822013-11-21 15:01:20 +0000576 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
577 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
578 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
579 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
580 // into the same temps, reducing the number of required temps down to 5. We shall work
581 // around the potentially locked temp by using LR for r_ptr, unconditionally.
582 // TODO: Pass information about the need for more temps to the stack frame generation
583 // code so that we can rely on being able to allocate enough temps.
584 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
585 MarkTemp(rARM_LR);
586 FreeTemp(rARM_LR);
587 LockTemp(rARM_LR);
588 bool load_early = true;
589 if (is_long) {
590 bool expected_is_core_reg =
591 rl_src_expected.location == kLocPhysReg && !IsFpReg(rl_src_expected.low_reg);
592 bool new_value_is_core_reg =
593 rl_src_new_value.location == kLocPhysReg && !IsFpReg(rl_src_new_value.low_reg);
594 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(rl_src_expected.low_reg);
595 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(rl_src_new_value.low_reg);
596
597 if (!expected_is_good_reg && !new_value_is_good_reg) {
598 // None of expected/new_value is non-temp reg, need to load both late
599 load_early = false;
600 // Make sure they are not in the temp regs and the load will not be skipped.
601 if (expected_is_core_reg) {
602 FlushRegWide(rl_src_expected.low_reg, rl_src_expected.high_reg);
603 ClobberSReg(rl_src_expected.s_reg_low);
604 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
605 rl_src_expected.location = kLocDalvikFrame;
606 }
607 if (new_value_is_core_reg) {
608 FlushRegWide(rl_src_new_value.low_reg, rl_src_new_value.high_reg);
609 ClobberSReg(rl_src_new_value.s_reg_low);
610 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
611 rl_src_new_value.location = kLocDalvikFrame;
612 }
613 }
614 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615
616 // Release store semantics, get the barrier out of the way. TODO: revisit
617 GenMemBarrier(kStoreLoad);
618
619 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000620 RegLocation rl_new_value;
621 if (!is_long) {
622 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
623 } else if (load_early) {
624 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
625 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626
Vladimir Marko1c282e22013-11-21 14:49:47 +0000627 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 // Mark card for object assuming new value is stored.
629 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
630 }
631
632 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
633
Vladimir Marko3e5af822013-11-21 15:01:20 +0000634 int r_ptr = rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 OpRegRegReg(kOpAdd, r_ptr, rl_object.low_reg, rl_offset.low_reg);
636
637 // Free now unneeded rl_object and rl_offset to give more temps.
638 ClobberSReg(rl_object.s_reg_low);
639 FreeTemp(rl_object.low_reg);
640 ClobberSReg(rl_offset.s_reg_low);
641 FreeTemp(rl_offset.low_reg);
642
Vladimir Marko3e5af822013-11-21 15:01:20 +0000643 RegLocation rl_expected;
644 if (!is_long) {
645 rl_expected = LoadValue(rl_src_expected, kCoreReg);
646 } else if (load_early) {
647 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
648 } else {
649 rl_new_value.low_reg = rl_expected.low_reg = AllocTemp();
650 rl_new_value.high_reg = rl_expected.high_reg = AllocTemp();
651 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652
Vladimir Marko3e5af822013-11-21 15:01:20 +0000653 // do {
654 // tmp = [r_ptr] - expected;
655 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
656 // result = tmp != 0;
657
658 int r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700659 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700660
Vladimir Marko3e5af822013-11-21 15:01:20 +0000661 if (is_long) {
662 int r_tmp_high = AllocTemp();
663 if (!load_early) {
664 LoadValueDirectWide(rl_src_expected, rl_expected.low_reg, rl_expected.high_reg);
665 }
666 NewLIR3(kThumb2Ldrexd, r_tmp, r_tmp_high, r_ptr);
667 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
668 OpRegReg(kOpSub, r_tmp_high, rl_expected.high_reg);
669 if (!load_early) {
670 LoadValueDirectWide(rl_src_new_value, rl_new_value.low_reg, rl_new_value.high_reg);
671 }
672 // Make sure we use ORR that sets the ccode
673 if (ARM_LOWREG(r_tmp) && ARM_LOWREG(r_tmp_high)) {
674 NewLIR2(kThumbOrr, r_tmp, r_tmp_high);
675 } else {
676 NewLIR4(kThumb2OrrRRRs, r_tmp, r_tmp, r_tmp_high, 0);
677 }
678 FreeTemp(r_tmp_high); // Now unneeded
679
680 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
681 OpIT(kCondEq, "T");
682 NewLIR4(kThumb2Strexd /* eq */, r_tmp, rl_new_value.low_reg, rl_new_value.high_reg, r_ptr);
683
684 } else {
685 NewLIR3(kThumb2Ldrex, r_tmp, r_ptr, 0);
686 OpRegReg(kOpSub, r_tmp, rl_expected.low_reg);
687 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
688 OpIT(kCondEq, "T");
689 NewLIR4(kThumb2Strex /* eq */, r_tmp, rl_new_value.low_reg, r_ptr, 0);
690 }
691
692 // Still one conditional left from OpIT(kCondEq, "T") from either branch
693 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700694 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695
Vladimir Marko3e5af822013-11-21 15:01:20 +0000696 if (!load_early) {
697 FreeTemp(rl_expected.low_reg); // Now unneeded.
698 FreeTemp(rl_expected.high_reg); // Now unneeded.
699 }
700
701 // result := (tmp1 != 0) ? 0 : 1;
702 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
703 OpRegRegImm(kOpRsub, rl_result.low_reg, r_tmp, 1);
704 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000705 OpIT(kCondUlt, "");
Vladimir Marko3e5af822013-11-21 15:01:20 +0000706 LoadConstant(rl_result.low_reg, 0); /* cc */
707 FreeTemp(r_tmp); // Now unneeded.
708
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 StoreValue(rl_dest, rl_result);
710
Vladimir Marko3e5af822013-11-21 15:01:20 +0000711 // Now, restore lr to its non-temp status.
712 Clobber(rARM_LR);
713 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 return true;
715}
716
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700717LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
719}
720
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700721LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
723}
724
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700725LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
727}
728
729void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
730 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700731 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 OpRegRegRegShift(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg,
733 EncodeShift(kArmLsl, second_bit - first_bit));
734 if (first_bit != 0) {
735 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
736 }
737}
738
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700739void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 int t_reg = AllocTemp();
741 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
742 FreeTemp(t_reg);
743 GenCheck(kCondEq, kThrowDivZero);
744}
745
746// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700747LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
749 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
750}
751
752// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700753LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 // Combine sub & test using sub setflags encoding here
755 NewLIR3(kThumb2SubsRRI12, reg, reg, 1);
756 return OpCondBranch(c_code, target);
757}
758
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700759void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760#if ANDROID_SMP != 0
761 int dmb_flavor;
762 // TODO: revisit Arm barrier kinds
763 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800764 case kLoadStore: dmb_flavor = kISH; break;
765 case kLoadLoad: dmb_flavor = kISH; break;
766 case kStoreStore: dmb_flavor = kISHST; break;
767 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 default:
769 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
770 dmb_flavor = kSY; // quiet gcc.
771 break;
772 }
773 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700774 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775#endif
776}
777
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700778void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 rl_src = LoadValueWide(rl_src, kCoreReg);
780 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
781 int z_reg = AllocTemp();
782 LoadConstantNoClobber(z_reg, 0);
783 // Check for destructive overlap
784 if (rl_result.low_reg == rl_src.high_reg) {
785 int t_reg = AllocTemp();
786 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
787 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, t_reg);
788 FreeTemp(t_reg);
789 } else {
790 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
791 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, rl_src.high_reg);
792 }
793 FreeTemp(z_reg);
794 StoreValueWide(rl_dest, rl_result);
795}
796
Mark Mendelle02d48f2014-01-15 11:19:23 -0800797void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
798 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 /*
800 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
801 * registers. Normally for Arm, we get 5. We can get to 6 by including
802 * lr in the temp set. The only problematic case is all operands and result are
803 * distinct, and none have been promoted. In that case, we can succeed by aggressively
804 * freeing operand temp registers after they are no longer needed. All other cases
805 * can proceed normally. We'll just punt on the case of the result having a misaligned
806 * overlap with either operand and send that case to a runtime handler.
807 */
808 RegLocation rl_result;
809 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700810 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 FlushAllRegs();
812 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
813 rl_result = GetReturnWide(false);
814 StoreValueWide(rl_dest, rl_result);
815 return;
816 }
817 // Temporarily add LR to the temp pool, and assign it to tmp1
818 MarkTemp(rARM_LR);
819 FreeTemp(rARM_LR);
820 int tmp1 = rARM_LR;
821 LockTemp(rARM_LR);
822
823 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
824 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
825
826 bool special_case = true;
827 // If operands are the same, or any pair has been promoted we're not the special case.
828 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
829 (!IsTemp(rl_src1.low_reg) && !IsTemp(rl_src1.high_reg)) ||
830 (!IsTemp(rl_src2.low_reg) && !IsTemp(rl_src2.high_reg))) {
831 special_case = false;
832 }
833 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
834 int res_lo = AllocTemp();
835 int res_hi;
836 if (rl_src1.low_reg == rl_src2.low_reg) {
837 res_hi = AllocTemp();
838 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.low_reg, rl_src1.high_reg);
839 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.low_reg, rl_src1.low_reg);
840 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
841 } else {
842 // In the special case, all temps are now allocated
843 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.low_reg, rl_src1.high_reg);
844 if (special_case) {
845 DCHECK_NE(rl_src1.low_reg, rl_src2.low_reg);
846 DCHECK_NE(rl_src1.high_reg, rl_src2.high_reg);
847 FreeTemp(rl_src1.high_reg);
848 }
849 res_hi = AllocTemp();
850
851 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.low_reg, rl_src1.low_reg);
852 NewLIR4(kThumb2Mla, tmp1, rl_src1.low_reg, rl_src2.high_reg, tmp1);
853 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
854 if (special_case) {
855 FreeTemp(rl_src1.low_reg);
856 Clobber(rl_src1.low_reg);
857 Clobber(rl_src1.high_reg);
858 }
859 }
860 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700861 rl_result = GetReturnWide(false); // Just using as a template.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 rl_result.low_reg = res_lo;
863 rl_result.high_reg = res_hi;
864 StoreValueWide(rl_dest, rl_result);
865 // Now, restore lr to its non-temp status.
866 Clobber(rARM_LR);
867 UnmarkTemp(rARM_LR);
868}
869
Mark Mendelle02d48f2014-01-15 11:19:23 -0800870void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700871 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
873}
874
Mark Mendelle02d48f2014-01-15 11:19:23 -0800875void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700876 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
878}
879
Mark Mendelle02d48f2014-01-15 11:19:23 -0800880void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700881 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
883}
884
Mark Mendelle02d48f2014-01-15 11:19:23 -0800885void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700886 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
888}
889
Mark Mendelle02d48f2014-01-15 11:19:23 -0800890void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700891 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
893}
894
895/*
896 * Generate array load
897 */
898void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700899 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 RegisterClass reg_class = oat_reg_class_by_size(size);
901 int len_offset = mirror::Array::LengthOffset().Int32Value();
902 int data_offset;
903 RegLocation rl_result;
904 bool constant_index = rl_index.is_const;
905 rl_array = LoadValue(rl_array, kCoreReg);
906 if (!constant_index) {
907 rl_index = LoadValue(rl_index, kCoreReg);
908 }
909
910 if (rl_dest.wide) {
911 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
912 } else {
913 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
914 }
915
916 // If index is constant, just fold it into the data offset
917 if (constant_index) {
918 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
919 }
920
921 /* null object? */
922 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
923
924 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
925 int reg_len = INVALID_REG;
926 if (needs_range_check) {
927 reg_len = AllocTemp();
928 /* Get len */
929 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
930 }
931 if (rl_dest.wide || rl_dest.fp || constant_index) {
932 int reg_ptr;
933 if (constant_index) {
934 reg_ptr = rl_array.low_reg; // NOTE: must not alter reg_ptr in constant case.
935 } else {
936 // No special indexed operation, lea + load w/ displacement
937 reg_ptr = AllocTemp();
938 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
939 EncodeShift(kArmLsl, scale));
940 FreeTemp(rl_index.low_reg);
941 }
942 rl_result = EvalLoc(rl_dest, reg_class, true);
943
944 if (needs_range_check) {
945 if (constant_index) {
946 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
947 } else {
948 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
949 }
950 FreeTemp(reg_len);
951 }
952 if (rl_dest.wide) {
953 LoadBaseDispWide(reg_ptr, data_offset, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
954 if (!constant_index) {
955 FreeTemp(reg_ptr);
956 }
957 StoreValueWide(rl_dest, rl_result);
958 } else {
959 LoadBaseDisp(reg_ptr, data_offset, rl_result.low_reg, size, INVALID_SREG);
960 if (!constant_index) {
961 FreeTemp(reg_ptr);
962 }
963 StoreValue(rl_dest, rl_result);
964 }
965 } else {
966 // Offset base, then use indexed load
967 int reg_ptr = AllocTemp();
968 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
969 FreeTemp(rl_array.low_reg);
970 rl_result = EvalLoc(rl_dest, reg_class, true);
971
972 if (needs_range_check) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000973 GenRegRegCheck(kCondUge, rl_index.low_reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 FreeTemp(reg_len);
975 }
976 LoadBaseIndexed(reg_ptr, rl_index.low_reg, rl_result.low_reg, scale, size);
977 FreeTemp(reg_ptr);
978 StoreValue(rl_dest, rl_result);
979 }
980}
981
982/*
983 * Generate array store
984 *
985 */
986void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700987 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 RegisterClass reg_class = oat_reg_class_by_size(size);
989 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 bool constant_index = rl_index.is_const;
991
Ian Rogersa9a82542013-10-04 11:17:26 -0700992 int data_offset;
993 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
995 } else {
996 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
997 }
998
999 // If index is constant, just fold it into the data offset.
1000 if (constant_index) {
1001 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1002 }
1003
1004 rl_array = LoadValue(rl_array, kCoreReg);
1005 if (!constant_index) {
1006 rl_index = LoadValue(rl_index, kCoreReg);
1007 }
1008
1009 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001010 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 if (constant_index) {
1012 reg_ptr = rl_array.low_reg;
Ian Rogers379067c2013-10-15 15:06:58 -07001013 } else if (IsTemp(rl_array.low_reg) && !card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 Clobber(rl_array.low_reg);
1015 reg_ptr = rl_array.low_reg;
1016 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001017 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 reg_ptr = AllocTemp();
1019 }
1020
1021 /* null object? */
1022 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
1023
1024 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1025 int reg_len = INVALID_REG;
1026 if (needs_range_check) {
1027 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001028 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 /* Get len */
1030 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
1031 }
1032 /* at this point, reg_ptr points to array, 2 live temps */
1033 if (rl_src.wide || rl_src.fp || constant_index) {
1034 if (rl_src.wide) {
1035 rl_src = LoadValueWide(rl_src, reg_class);
1036 } else {
1037 rl_src = LoadValue(rl_src, reg_class);
1038 }
1039 if (!constant_index) {
1040 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
1041 EncodeShift(kArmLsl, scale));
1042 }
1043 if (needs_range_check) {
1044 if (constant_index) {
1045 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1046 } else {
1047 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
1048 }
1049 FreeTemp(reg_len);
1050 }
1051
1052 if (rl_src.wide) {
1053 StoreBaseDispWide(reg_ptr, data_offset, rl_src.low_reg, rl_src.high_reg);
1054 } else {
1055 StoreBaseDisp(reg_ptr, data_offset, rl_src.low_reg, size);
1056 }
1057 } else {
1058 /* reg_ptr -> array data */
1059 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
1060 rl_src = LoadValue(rl_src, reg_class);
1061 if (needs_range_check) {
Vladimir Marko58af1f92013-12-19 13:31:15 +00001062 GenRegRegCheck(kCondUge, rl_index.low_reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 FreeTemp(reg_len);
1064 }
1065 StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
1066 scale, size);
1067 }
Ian Rogers773aab12013-10-14 13:50:10 -07001068 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069 FreeTemp(reg_ptr);
1070 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001071 if (card_mark) {
1072 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 }
1074}
1075
Ian Rogersa9a82542013-10-04 11:17:26 -07001076
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001078 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 rl_src = LoadValueWide(rl_src, kCoreReg);
1080 // Per spec, we only care about low 6 bits of shift amount.
1081 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1082 if (shift_amount == 0) {
1083 StoreValueWide(rl_dest, rl_src);
1084 return;
1085 }
1086 if (BadOverlap(rl_src, rl_dest)) {
1087 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1088 return;
1089 }
1090 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001091 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 case Instruction::SHL_LONG:
1093 case Instruction::SHL_LONG_2ADDR:
1094 if (shift_amount == 1) {
1095 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg);
1096 OpRegRegReg(kOpAdc, rl_result.high_reg, rl_src.high_reg, rl_src.high_reg);
1097 } else if (shift_amount == 32) {
1098 OpRegCopy(rl_result.high_reg, rl_src.low_reg);
1099 LoadConstant(rl_result.low_reg, 0);
1100 } else if (shift_amount > 31) {
1101 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.low_reg, shift_amount - 32);
1102 LoadConstant(rl_result.low_reg, 0);
1103 } else {
1104 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.high_reg, shift_amount);
1105 OpRegRegRegShift(kOpOr, rl_result.high_reg, rl_result.high_reg, rl_src.low_reg,
1106 EncodeShift(kArmLsr, 32 - shift_amount));
1107 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, shift_amount);
1108 }
1109 break;
1110 case Instruction::SHR_LONG:
1111 case Instruction::SHR_LONG_2ADDR:
1112 if (shift_amount == 32) {
1113 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1114 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1115 } else if (shift_amount > 31) {
1116 OpRegRegImm(kOpAsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1117 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1118 } else {
1119 int t_reg = AllocTemp();
1120 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1121 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1122 EncodeShift(kArmLsl, 32 - shift_amount));
1123 FreeTemp(t_reg);
1124 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1125 }
1126 break;
1127 case Instruction::USHR_LONG:
1128 case Instruction::USHR_LONG_2ADDR:
1129 if (shift_amount == 32) {
1130 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1131 LoadConstant(rl_result.high_reg, 0);
1132 } else if (shift_amount > 31) {
1133 OpRegRegImm(kOpLsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1134 LoadConstant(rl_result.high_reg, 0);
1135 } else {
1136 int t_reg = AllocTemp();
1137 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1138 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1139 EncodeShift(kArmLsl, 32 - shift_amount));
1140 FreeTemp(t_reg);
1141 OpRegRegImm(kOpLsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1142 }
1143 break;
1144 default:
1145 LOG(FATAL) << "Unexpected case";
1146 }
1147 StoreValueWide(rl_dest, rl_result);
1148}
1149
1150void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001151 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1153 if (!rl_src2.is_const) {
1154 // Don't bother with special handling for subtract from immediate.
1155 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1156 return;
1157 }
1158 } else {
1159 // Normalize
1160 if (!rl_src2.is_const) {
1161 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001162 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 }
1164 }
1165 if (BadOverlap(rl_src1, rl_dest)) {
1166 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1167 return;
1168 }
1169 DCHECK(rl_src2.is_const);
1170 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1171 uint32_t val_lo = Low32Bits(val);
1172 uint32_t val_hi = High32Bits(val);
1173 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1174 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1175
1176 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001177 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 case Instruction::ADD_LONG:
1179 case Instruction::ADD_LONG_2ADDR:
1180 case Instruction::SUB_LONG:
1181 case Instruction::SUB_LONG_2ADDR:
1182 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1183 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1184 return;
1185 }
1186 break;
1187 default:
1188 break;
1189 }
1190 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1191 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1192 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1193 switch (opcode) {
1194 case Instruction::ADD_LONG:
1195 case Instruction::ADD_LONG_2ADDR:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001196 NewLIR3(kThumb2AddRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1197 NewLIR3(kThumb2AdcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 break;
1199 case Instruction::OR_LONG:
1200 case Instruction::OR_LONG_2ADDR:
1201 if ((val_lo != 0) || (rl_result.low_reg != rl_src1.low_reg)) {
1202 OpRegRegImm(kOpOr, rl_result.low_reg, rl_src1.low_reg, val_lo);
1203 }
1204 if ((val_hi != 0) || (rl_result.high_reg != rl_src1.high_reg)) {
1205 OpRegRegImm(kOpOr, rl_result.high_reg, rl_src1.high_reg, val_hi);
1206 }
1207 break;
1208 case Instruction::XOR_LONG:
1209 case Instruction::XOR_LONG_2ADDR:
1210 OpRegRegImm(kOpXor, rl_result.low_reg, rl_src1.low_reg, val_lo);
1211 OpRegRegImm(kOpXor, rl_result.high_reg, rl_src1.high_reg, val_hi);
1212 break;
1213 case Instruction::AND_LONG:
1214 case Instruction::AND_LONG_2ADDR:
1215 if ((val_lo != 0xffffffff) || (rl_result.low_reg != rl_src1.low_reg)) {
1216 OpRegRegImm(kOpAnd, rl_result.low_reg, rl_src1.low_reg, val_lo);
1217 }
1218 if ((val_hi != 0xffffffff) || (rl_result.high_reg != rl_src1.high_reg)) {
1219 OpRegRegImm(kOpAnd, rl_result.high_reg, rl_src1.high_reg, val_hi);
1220 }
1221 break;
1222 case Instruction::SUB_LONG_2ADDR:
1223 case Instruction::SUB_LONG:
Vladimir Marko332b7aa2013-11-18 12:01:54 +00001224 NewLIR3(kThumb2SubRRI8M, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1225 NewLIR3(kThumb2SbcRRI8M, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 break;
1227 default:
1228 LOG(FATAL) << "Unexpected opcode " << opcode;
1229 }
1230 StoreValueWide(rl_dest, rl_result);
1231}
1232
1233} // namespace art