blob: 4fa038763c564396355ed0a4a026b9d5d358c2ce [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");
100 NewLIR2(kThumb2MovImmShift, t_reg, ModifiedImmediate(-1));
101 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);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 LIR* taken = &block_label_list_[bb->taken->id];
126 LIR* not_taken = &block_label_list_[bb->fall_through->id];
127 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
128 int32_t low_reg = rl_src1.low_reg;
129 int32_t high_reg = rl_src1.high_reg;
130
Brian Carlstromdf629502013-07-17 22:39:56 -0700131 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 case kCondEq:
133 case kCondNe:
134 LIR* target;
135 ConditionCode condition;
136 if (ccode == kCondEq) {
137 target = not_taken;
138 condition = kCondEq;
139 } else {
140 target = taken;
141 condition = kCondNe;
142 }
143 if (val == 0) {
144 int t_reg = AllocTemp();
145 NewLIR4(kThumb2OrrRRRs, t_reg, low_reg, high_reg, 0);
146 FreeTemp(t_reg);
147 OpCondBranch(condition, taken);
148 return;
149 }
150 OpCmpImmBranch(kCondNe, high_reg, val_hi, target);
151 break;
152 case kCondLt:
153 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
154 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
155 ccode = kCondCc;
156 break;
157 case kCondLe:
158 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
159 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
160 ccode = kCondLs;
161 break;
162 case kCondGt:
163 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
164 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
165 ccode = kCondHi;
166 break;
167 case kCondGe:
168 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
169 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
170 ccode = kCondCs;
171 break;
172 default:
173 LOG(FATAL) << "Unexpected ccode: " << ccode;
174 }
175 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
176}
177
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegLocation rl_result;
180 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
181 // Temporary debugging code
182 int dest_sreg = mir->ssa_rep->defs[0];
183 if ((dest_sreg < 0) || (dest_sreg >= mir_graph_->GetNumSSARegs())) {
184 LOG(INFO) << "Bad target sreg: " << dest_sreg << ", in "
Brian Carlstromb1eba212013-07-17 18:07:19 -0700185 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 LOG(INFO) << "at dex offset 0x" << std::hex << mir->offset;
187 LOG(INFO) << "vreg = " << mir_graph_->SRegToVReg(dest_sreg);
188 LOG(INFO) << "num uses = " << mir->ssa_rep->num_uses;
189 if (mir->ssa_rep->num_uses == 1) {
190 LOG(INFO) << "CONST case, vals = " << mir->dalvikInsn.vB << ", " << mir->dalvikInsn.vC;
191 } else {
192 LOG(INFO) << "MOVE case, operands = " << mir->ssa_rep->uses[1] << ", "
193 << mir->ssa_rep->uses[2];
194 }
195 CHECK(false) << "Invalid target sreg on Select.";
196 }
197 // End temporary debugging code
198 RegLocation rl_dest = mir_graph_->GetDest(mir);
199 rl_src = LoadValue(rl_src, kCoreReg);
200 if (mir->ssa_rep->num_uses == 1) {
201 // CONST case
202 int true_val = mir->dalvikInsn.vB;
203 int false_val = mir->dalvikInsn.vC;
204 rl_result = EvalLoc(rl_dest, kCoreReg, true);
205 if ((true_val == 1) && (false_val == 0)) {
206 OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, 1);
207 OpIT(kCondCc, "");
208 LoadConstant(rl_result.low_reg, 0);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700209 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 } else if (InexpensiveConstantInt(true_val) && InexpensiveConstantInt(false_val)) {
211 OpRegImm(kOpCmp, rl_src.low_reg, 0);
212 OpIT(kCondEq, "E");
213 LoadConstant(rl_result.low_reg, true_val);
214 LoadConstant(rl_result.low_reg, false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700215 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 } else {
217 // Unlikely case - could be tuned.
218 int t_reg1 = AllocTemp();
219 int t_reg2 = AllocTemp();
220 LoadConstant(t_reg1, true_val);
221 LoadConstant(t_reg2, false_val);
222 OpRegImm(kOpCmp, rl_src.low_reg, 0);
223 OpIT(kCondEq, "E");
224 OpRegCopy(rl_result.low_reg, t_reg1);
225 OpRegCopy(rl_result.low_reg, t_reg2);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700226 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 }
228 } else {
229 // MOVE case
230 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
231 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
232 rl_true = LoadValue(rl_true, kCoreReg);
233 rl_false = LoadValue(rl_false, kCoreReg);
234 rl_result = EvalLoc(rl_dest, kCoreReg, true);
235 OpRegImm(kOpCmp, rl_src.low_reg, 0);
buzbee252254b2013-09-08 16:20:53 -0700236 if (rl_result.low_reg == rl_true.low_reg) { // Is the "true" case already in place?
237 OpIT(kCondNe, "");
238 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
239 } else if (rl_result.low_reg == rl_false.low_reg) { // False case in place?
240 OpIT(kCondEq, "");
241 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
242 } else { // Normal - select between the two.
243 OpIT(kCondEq, "E");
244 OpRegCopy(rl_result.low_reg, rl_true.low_reg);
245 OpRegCopy(rl_result.low_reg, rl_false.low_reg);
246 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700247 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 }
249 StoreValue(rl_dest, rl_result);
250}
251
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700252void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
254 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
255 // Normalize such that if either operand is constant, src2 will be constant.
256 ConditionCode ccode = static_cast<ConditionCode>(mir->dalvikInsn.arg[0]);
257 if (rl_src1.is_const) {
258 RegLocation rl_temp = rl_src1;
259 rl_src1 = rl_src2;
260 rl_src2 = rl_temp;
261 ccode = FlipComparisonOrder(ccode);
262 }
263 if (rl_src2.is_const) {
264 RegLocation rl_temp = UpdateLocWide(rl_src2);
265 // Do special compare/branch against simple const operand if not already in registers.
266 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
267 if ((rl_temp.location != kLocPhysReg) &&
268 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
269 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
270 return;
271 }
272 }
273 LIR* taken = &block_label_list_[bb->taken->id];
274 LIR* not_taken = &block_label_list_[bb->fall_through->id];
275 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
276 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
277 OpRegReg(kOpCmp, rl_src1.high_reg, rl_src2.high_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700278 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 case kCondEq:
280 OpCondBranch(kCondNe, not_taken);
281 break;
282 case kCondNe:
283 OpCondBranch(kCondNe, taken);
284 break;
285 case kCondLt:
286 OpCondBranch(kCondLt, taken);
287 OpCondBranch(kCondGt, not_taken);
288 ccode = kCondCc;
289 break;
290 case kCondLe:
291 OpCondBranch(kCondLt, taken);
292 OpCondBranch(kCondGt, not_taken);
293 ccode = kCondLs;
294 break;
295 case kCondGt:
296 OpCondBranch(kCondGt, taken);
297 OpCondBranch(kCondLt, not_taken);
298 ccode = kCondHi;
299 break;
300 case kCondGe:
301 OpCondBranch(kCondGt, taken);
302 OpCondBranch(kCondLt, not_taken);
303 ccode = kCondCs;
304 break;
305 default:
306 LOG(FATAL) << "Unexpected ccode: " << ccode;
307 }
308 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
309 OpCondBranch(ccode, taken);
310}
311
312/*
313 * Generate a register comparison to an immediate and branch. Caller
314 * is responsible for setting branch target field.
315 */
316LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, int reg, int check_value,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700317 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 LIR* branch;
319 int mod_imm;
320 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
321 if ((ARM_LOWREG(reg)) && (check_value == 0) &&
322 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
323 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
324 reg, 0);
325 } else {
326 mod_imm = ModifiedImmediate(check_value);
327 if (ARM_LOWREG(reg) && ((check_value & 0xff) == check_value)) {
328 NewLIR2(kThumbCmpRI8, reg, check_value);
329 } else if (mod_imm >= 0) {
330 NewLIR2(kThumb2CmpRI12, reg, mod_imm);
331 } else {
332 int t_reg = AllocTemp();
333 LoadConstant(t_reg, check_value);
334 OpRegReg(kOpCmp, reg, t_reg);
335 }
336 branch = NewLIR2(kThumbBCond, 0, arm_cond);
337 }
338 branch->target = target;
339 return branch;
340}
341
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700342LIR* ArmMir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 LIR* res;
344 int opcode;
345 if (ARM_FPREG(r_dest) || ARM_FPREG(r_src))
346 return OpFpRegCopy(r_dest, r_src);
347 if (ARM_LOWREG(r_dest) && ARM_LOWREG(r_src))
348 opcode = kThumbMovRR;
349 else if (!ARM_LOWREG(r_dest) && !ARM_LOWREG(r_src))
350 opcode = kThumbMovRR_H2H;
351 else if (ARM_LOWREG(r_dest))
352 opcode = kThumbMovRR_H2L;
353 else
354 opcode = kThumbMovRR_L2H;
355 res = RawLIR(current_dalvik_offset_, opcode, r_dest, r_src);
356 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
357 res->flags.is_nop = true;
358 }
359 return res;
360}
361
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700362LIR* ArmMir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
364 AppendLIR(res);
365 return res;
366}
367
368void ArmMir2Lir::OpRegCopyWide(int dest_lo, int dest_hi, int src_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369 int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 bool dest_fp = ARM_FPREG(dest_lo) && ARM_FPREG(dest_hi);
371 bool src_fp = ARM_FPREG(src_lo) && ARM_FPREG(src_hi);
372 DCHECK_EQ(ARM_FPREG(src_lo), ARM_FPREG(src_hi));
373 DCHECK_EQ(ARM_FPREG(dest_lo), ARM_FPREG(dest_hi));
374 if (dest_fp) {
375 if (src_fp) {
376 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
377 } else {
378 NewLIR3(kThumb2Fmdrr, S2d(dest_lo, dest_hi), src_lo, src_hi);
379 }
380 } else {
381 if (src_fp) {
382 NewLIR3(kThumb2Fmrrd, dest_lo, dest_hi, S2d(src_lo, src_hi));
383 } else {
384 // Handle overlap
385 if (src_hi == dest_lo) {
386 OpRegCopy(dest_hi, src_hi);
387 OpRegCopy(dest_lo, src_lo);
388 } else {
389 OpRegCopy(dest_lo, src_lo);
390 OpRegCopy(dest_hi, src_hi);
391 }
392 }
393 }
394}
395
396// Table of magic divisors
397struct MagicTable {
398 uint32_t magic;
399 uint32_t shift;
400 DividePattern pattern;
401};
402
403static const MagicTable magic_table[] = {
404 {0, 0, DivideNone}, // 0
405 {0, 0, DivideNone}, // 1
406 {0, 0, DivideNone}, // 2
407 {0x55555556, 0, Divide3}, // 3
408 {0, 0, DivideNone}, // 4
409 {0x66666667, 1, Divide5}, // 5
410 {0x2AAAAAAB, 0, Divide3}, // 6
411 {0x92492493, 2, Divide7}, // 7
412 {0, 0, DivideNone}, // 8
413 {0x38E38E39, 1, Divide5}, // 9
414 {0x66666667, 2, Divide5}, // 10
415 {0x2E8BA2E9, 1, Divide5}, // 11
416 {0x2AAAAAAB, 1, Divide5}, // 12
417 {0x4EC4EC4F, 2, Divide5}, // 13
418 {0x92492493, 3, Divide7}, // 14
419 {0x88888889, 3, Divide7}, // 15
420};
421
422// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700423bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700424 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
426 return false;
427 }
428 DividePattern pattern = magic_table[lit].pattern;
429 if (pattern == DivideNone) {
430 return false;
431 }
432 // Tuning: add rem patterns
buzbee11b63d12013-08-27 07:34:17 -0700433 if (!is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 return false;
435 }
436
437 int r_magic = AllocTemp();
438 LoadConstant(r_magic, magic_table[lit].magic);
439 rl_src = LoadValue(rl_src, kCoreReg);
440 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
441 int r_hi = AllocTemp();
442 int r_lo = AllocTemp();
443 NewLIR4(kThumb2Smull, r_lo, r_hi, r_magic, rl_src.low_reg);
Brian Carlstromdf629502013-07-17 22:39:56 -0700444 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 case Divide3:
446 OpRegRegRegShift(kOpSub, rl_result.low_reg, r_hi,
447 rl_src.low_reg, EncodeShift(kArmAsr, 31));
448 break;
449 case Divide5:
450 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
451 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
452 EncodeShift(kArmAsr, magic_table[lit].shift));
453 break;
454 case Divide7:
455 OpRegReg(kOpAdd, r_hi, rl_src.low_reg);
456 OpRegRegImm(kOpAsr, r_lo, rl_src.low_reg, 31);
457 OpRegRegRegShift(kOpRsub, rl_result.low_reg, r_lo, r_hi,
458 EncodeShift(kArmAsr, magic_table[lit].shift));
459 break;
460 default:
461 LOG(FATAL) << "Unexpected pattern: " << pattern;
462 }
463 StoreValue(rl_dest, rl_result);
464 return true;
465}
466
467LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
470 return NULL;
471}
472
473RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700474 bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
476 return rl_dest;
477}
478
479RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700480 bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
482 return rl_dest;
483}
484
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700485bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 DCHECK_EQ(cu_->instruction_set, kThumb2);
487 RegLocation rl_src1 = info->args[0];
488 RegLocation rl_src2 = info->args[1];
489 rl_src1 = LoadValue(rl_src1, kCoreReg);
490 rl_src2 = LoadValue(rl_src2, kCoreReg);
491 RegLocation rl_dest = InlineTarget(info);
492 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
493 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
494 OpIT((is_min) ? kCondGt : kCondLt, "E");
495 OpRegReg(kOpMov, rl_result.low_reg, rl_src2.low_reg);
496 OpRegReg(kOpMov, rl_result.low_reg, rl_src1.low_reg);
497 GenBarrier();
498 StoreValue(rl_dest, rl_result);
499 return true;
500}
501
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700502void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 LOG(FATAL) << "Unexpected use of OpLea for Arm";
504}
505
Ian Rogers468532e2013-08-05 10:56:33 -0700506void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
508}
509
510bool ArmMir2Lir::GenInlinedCas32(CallInfo* info, bool need_write_barrier) {
511 DCHECK_EQ(cu_->instruction_set, kThumb2);
512 // Unused - RegLocation rl_src_unsafe = info->args[0];
513 RegLocation rl_src_obj= info->args[1]; // Object - known non-null
514 RegLocation rl_src_offset= info->args[2]; // long low
515 rl_src_offset.wide = 0; // ignore high half in info->args[3]
516 RegLocation rl_src_expected= info->args[4]; // int or Object
517 RegLocation rl_src_new_value= info->args[5]; // int or Object
518 RegLocation rl_dest = InlineTarget(info); // boolean place for result
519
520
521 // Release store semantics, get the barrier out of the way. TODO: revisit
522 GenMemBarrier(kStoreLoad);
523
524 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
525 RegLocation rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
526
527 if (need_write_barrier && !mir_graph_->IsConstantNullRef(rl_new_value)) {
528 // Mark card for object assuming new value is stored.
529 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
530 }
531
532 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
533
534 int r_ptr = AllocTemp();
535 OpRegRegReg(kOpAdd, r_ptr, rl_object.low_reg, rl_offset.low_reg);
536
537 // Free now unneeded rl_object and rl_offset to give more temps.
538 ClobberSReg(rl_object.s_reg_low);
539 FreeTemp(rl_object.low_reg);
540 ClobberSReg(rl_offset.s_reg_low);
541 FreeTemp(rl_offset.low_reg);
542
Jeff Hao2de2aa12013-09-12 17:20:31 -0700543 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
544 LoadConstant(rl_result.low_reg, 0); // r_result := 0
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545
Jeff Hao2de2aa12013-09-12 17:20:31 -0700546 // while ([r_ptr] == rExpected && r_result == 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 // [r_ptr] <- r_new_value && r_result := success ? 0 : 1
548 // r_result ^= 1
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 // }
Jeff Hao2de2aa12013-09-12 17:20:31 -0700550 int r_old_value = AllocTemp();
551 LIR* target = NewLIR0(kPseudoTargetLabel);
552 NewLIR3(kThumb2Ldrex, r_old_value, r_ptr, 0);
553
554 RegLocation rl_expected = LoadValue(rl_src_expected, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 OpRegReg(kOpCmp, r_old_value, rl_expected.low_reg);
556 FreeTemp(r_old_value); // Now unneeded.
Jeff Hao2de2aa12013-09-12 17:20:31 -0700557 OpIT(kCondEq, "TT");
558 NewLIR4(kThumb2Strex /* eq */, rl_result.low_reg, rl_new_value.low_reg, r_ptr, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 FreeTemp(r_ptr); // Now unneeded.
Jeff Hao2de2aa12013-09-12 17:20:31 -0700560 OpRegImm(kOpXor /* eq */, rl_result.low_reg, 1);
561 OpRegImm(kOpCmp /* eq */, rl_result.low_reg, 0);
562 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563
564 StoreValue(rl_dest, rl_result);
565
566 return true;
567}
568
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700569LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
571}
572
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700573LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
575}
576
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700577LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
579}
580
581void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
582 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700583 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 OpRegRegRegShift(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg,
585 EncodeShift(kArmLsl, second_bit - first_bit));
586 if (first_bit != 0) {
587 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
588 }
589}
590
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700591void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 int t_reg = AllocTemp();
593 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
594 FreeTemp(t_reg);
595 GenCheck(kCondEq, kThrowDivZero);
596}
597
598// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700599LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
601 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
602}
603
604// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700605LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 // Combine sub & test using sub setflags encoding here
607 NewLIR3(kThumb2SubsRRI12, reg, reg, 1);
608 return OpCondBranch(c_code, target);
609}
610
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700611void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612#if ANDROID_SMP != 0
613 int dmb_flavor;
614 // TODO: revisit Arm barrier kinds
615 switch (barrier_kind) {
616 case kLoadStore: dmb_flavor = kSY; break;
617 case kLoadLoad: dmb_flavor = kSY; break;
618 case kStoreStore: dmb_flavor = kST; break;
619 case kStoreLoad: dmb_flavor = kSY; break;
620 default:
621 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
622 dmb_flavor = kSY; // quiet gcc.
623 break;
624 }
625 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
626 dmb->def_mask = ENCODE_ALL;
627#endif
628}
629
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700630void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 rl_src = LoadValueWide(rl_src, kCoreReg);
632 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
633 int z_reg = AllocTemp();
634 LoadConstantNoClobber(z_reg, 0);
635 // Check for destructive overlap
636 if (rl_result.low_reg == rl_src.high_reg) {
637 int t_reg = AllocTemp();
638 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
639 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, t_reg);
640 FreeTemp(t_reg);
641 } else {
642 OpRegRegReg(kOpSub, rl_result.low_reg, z_reg, rl_src.low_reg);
643 OpRegRegReg(kOpSbc, rl_result.high_reg, z_reg, rl_src.high_reg);
644 }
645 FreeTemp(z_reg);
646 StoreValueWide(rl_dest, rl_result);
647}
648
649
650 /*
651 * Check to see if a result pair has a misaligned overlap with an operand pair. This
652 * is not usual for dx to generate, but it is legal (for now). In a future rev of
653 * dex, we'll want to make this case illegal.
654 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700655bool ArmMir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 DCHECK(rl_src.wide);
657 DCHECK(rl_dest.wide);
658 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
659}
660
661void ArmMir2Lir::GenMulLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700662 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 /*
664 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
665 * registers. Normally for Arm, we get 5. We can get to 6 by including
666 * lr in the temp set. The only problematic case is all operands and result are
667 * distinct, and none have been promoted. In that case, we can succeed by aggressively
668 * freeing operand temp registers after they are no longer needed. All other cases
669 * can proceed normally. We'll just punt on the case of the result having a misaligned
670 * overlap with either operand and send that case to a runtime handler.
671 */
672 RegLocation rl_result;
673 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700674 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 FlushAllRegs();
676 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
677 rl_result = GetReturnWide(false);
678 StoreValueWide(rl_dest, rl_result);
679 return;
680 }
681 // Temporarily add LR to the temp pool, and assign it to tmp1
682 MarkTemp(rARM_LR);
683 FreeTemp(rARM_LR);
684 int tmp1 = rARM_LR;
685 LockTemp(rARM_LR);
686
687 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
688 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
689
690 bool special_case = true;
691 // If operands are the same, or any pair has been promoted we're not the special case.
692 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
693 (!IsTemp(rl_src1.low_reg) && !IsTemp(rl_src1.high_reg)) ||
694 (!IsTemp(rl_src2.low_reg) && !IsTemp(rl_src2.high_reg))) {
695 special_case = false;
696 }
697 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
698 int res_lo = AllocTemp();
699 int res_hi;
700 if (rl_src1.low_reg == rl_src2.low_reg) {
701 res_hi = AllocTemp();
702 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.low_reg, rl_src1.high_reg);
703 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.low_reg, rl_src1.low_reg);
704 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
705 } else {
706 // In the special case, all temps are now allocated
707 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.low_reg, rl_src1.high_reg);
708 if (special_case) {
709 DCHECK_NE(rl_src1.low_reg, rl_src2.low_reg);
710 DCHECK_NE(rl_src1.high_reg, rl_src2.high_reg);
711 FreeTemp(rl_src1.high_reg);
712 }
713 res_hi = AllocTemp();
714
715 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.low_reg, rl_src1.low_reg);
716 NewLIR4(kThumb2Mla, tmp1, rl_src1.low_reg, rl_src2.high_reg, tmp1);
717 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
718 if (special_case) {
719 FreeTemp(rl_src1.low_reg);
720 Clobber(rl_src1.low_reg);
721 Clobber(rl_src1.high_reg);
722 }
723 }
724 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700725 rl_result = GetReturnWide(false); // Just using as a template.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 rl_result.low_reg = res_lo;
727 rl_result.high_reg = res_hi;
728 StoreValueWide(rl_dest, rl_result);
729 // Now, restore lr to its non-temp status.
730 Clobber(rARM_LR);
731 UnmarkTemp(rARM_LR);
732}
733
734void ArmMir2Lir::GenAddLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700735 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
737}
738
739void ArmMir2Lir::GenSubLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700740 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
742}
743
744void ArmMir2Lir::GenAndLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700745 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
747}
748
749void ArmMir2Lir::GenOrLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700750 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
752}
753
754void ArmMir2Lir::GenXorLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700755 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
757}
758
759/*
760 * Generate array load
761 */
762void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700763 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 RegisterClass reg_class = oat_reg_class_by_size(size);
765 int len_offset = mirror::Array::LengthOffset().Int32Value();
766 int data_offset;
767 RegLocation rl_result;
768 bool constant_index = rl_index.is_const;
769 rl_array = LoadValue(rl_array, kCoreReg);
770 if (!constant_index) {
771 rl_index = LoadValue(rl_index, kCoreReg);
772 }
773
774 if (rl_dest.wide) {
775 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
776 } else {
777 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
778 }
779
780 // If index is constant, just fold it into the data offset
781 if (constant_index) {
782 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
783 }
784
785 /* null object? */
786 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
787
788 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
789 int reg_len = INVALID_REG;
790 if (needs_range_check) {
791 reg_len = AllocTemp();
792 /* Get len */
793 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
794 }
795 if (rl_dest.wide || rl_dest.fp || constant_index) {
796 int reg_ptr;
797 if (constant_index) {
798 reg_ptr = rl_array.low_reg; // NOTE: must not alter reg_ptr in constant case.
799 } else {
800 // No special indexed operation, lea + load w/ displacement
801 reg_ptr = AllocTemp();
802 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
803 EncodeShift(kArmLsl, scale));
804 FreeTemp(rl_index.low_reg);
805 }
806 rl_result = EvalLoc(rl_dest, reg_class, true);
807
808 if (needs_range_check) {
809 if (constant_index) {
810 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
811 } else {
812 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
813 }
814 FreeTemp(reg_len);
815 }
816 if (rl_dest.wide) {
817 LoadBaseDispWide(reg_ptr, data_offset, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
818 if (!constant_index) {
819 FreeTemp(reg_ptr);
820 }
821 StoreValueWide(rl_dest, rl_result);
822 } else {
823 LoadBaseDisp(reg_ptr, data_offset, rl_result.low_reg, size, INVALID_SREG);
824 if (!constant_index) {
825 FreeTemp(reg_ptr);
826 }
827 StoreValue(rl_dest, rl_result);
828 }
829 } else {
830 // Offset base, then use indexed load
831 int reg_ptr = AllocTemp();
832 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
833 FreeTemp(rl_array.low_reg);
834 rl_result = EvalLoc(rl_dest, reg_class, true);
835
836 if (needs_range_check) {
837 // TODO: change kCondCS to a more meaningful name, is the sense of
838 // carry-set/clear flipped?
839 GenRegRegCheck(kCondCs, rl_index.low_reg, reg_len, kThrowArrayBounds);
840 FreeTemp(reg_len);
841 }
842 LoadBaseIndexed(reg_ptr, rl_index.low_reg, rl_result.low_reg, scale, size);
843 FreeTemp(reg_ptr);
844 StoreValue(rl_dest, rl_result);
845 }
846}
847
848/*
849 * Generate array store
850 *
851 */
852void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700853 RegLocation rl_index, RegLocation rl_src, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 RegisterClass reg_class = oat_reg_class_by_size(size);
855 int len_offset = mirror::Array::LengthOffset().Int32Value();
856 int data_offset;
857 bool constant_index = rl_index.is_const;
858
859 if (rl_src.wide) {
860 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
861 } else {
862 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
863 }
864
865 // If index is constant, just fold it into the data offset.
866 if (constant_index) {
867 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
868 }
869
870 rl_array = LoadValue(rl_array, kCoreReg);
871 if (!constant_index) {
872 rl_index = LoadValue(rl_index, kCoreReg);
873 }
874
875 int reg_ptr;
876 if (constant_index) {
877 reg_ptr = rl_array.low_reg;
878 } else if (IsTemp(rl_array.low_reg)) {
879 Clobber(rl_array.low_reg);
880 reg_ptr = rl_array.low_reg;
881 } else {
882 reg_ptr = AllocTemp();
883 }
884
885 /* null object? */
886 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
887
888 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
889 int reg_len = INVALID_REG;
890 if (needs_range_check) {
891 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700892 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 /* Get len */
894 LoadWordDisp(rl_array.low_reg, len_offset, reg_len);
895 }
896 /* at this point, reg_ptr points to array, 2 live temps */
897 if (rl_src.wide || rl_src.fp || constant_index) {
898 if (rl_src.wide) {
899 rl_src = LoadValueWide(rl_src, reg_class);
900 } else {
901 rl_src = LoadValue(rl_src, reg_class);
902 }
903 if (!constant_index) {
904 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.low_reg, rl_index.low_reg,
905 EncodeShift(kArmLsl, scale));
906 }
907 if (needs_range_check) {
908 if (constant_index) {
909 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
910 } else {
911 GenRegRegCheck(kCondLs, reg_len, rl_index.low_reg, kThrowArrayBounds);
912 }
913 FreeTemp(reg_len);
914 }
915
916 if (rl_src.wide) {
917 StoreBaseDispWide(reg_ptr, data_offset, rl_src.low_reg, rl_src.high_reg);
918 } else {
919 StoreBaseDisp(reg_ptr, data_offset, rl_src.low_reg, size);
920 }
921 } else {
922 /* reg_ptr -> array data */
923 OpRegRegImm(kOpAdd, reg_ptr, rl_array.low_reg, data_offset);
924 rl_src = LoadValue(rl_src, reg_class);
925 if (needs_range_check) {
926 GenRegRegCheck(kCondCs, rl_index.low_reg, reg_len, kThrowArrayBounds);
927 FreeTemp(reg_len);
928 }
929 StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
930 scale, size);
931 }
932 if (!constant_index) {
933 FreeTemp(reg_ptr);
934 }
935}
936
937/*
938 * Generate array store
939 *
940 */
941void ArmMir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700942 RegLocation rl_index, RegLocation rl_src, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 int len_offset = mirror::Array::LengthOffset().Int32Value();
944 int data_offset = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value();
945
946 FlushAllRegs(); // Use explicit registers
947 LockCallTemps();
948
949 int r_value = TargetReg(kArg0); // Register holding value
950 int r_array_class = TargetReg(kArg1); // Register holding array's Class
951 int r_array = TargetReg(kArg2); // Register holding array
952 int r_index = TargetReg(kArg3); // Register holding index into array
953
954 LoadValueDirectFixed(rl_array, r_array); // Grab array
955 LoadValueDirectFixed(rl_src, r_value); // Grab value
956 LoadValueDirectFixed(rl_index, r_index); // Grab index
957
958 GenNullCheck(rl_array.s_reg_low, r_array, opt_flags); // NPE?
959
960 // Store of null?
961 LIR* null_value_check = OpCmpImmBranch(kCondEq, r_value, 0, NULL);
962
963 // Get the array's class.
964 LoadWordDisp(r_array, mirror::Object::ClassOffset().Int32Value(), r_array_class);
Ian Rogers468532e2013-08-05 10:56:33 -0700965 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCanPutArrayElement), r_value,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 r_array_class, true);
967 // Redo LoadValues in case they didn't survive the call.
968 LoadValueDirectFixed(rl_array, r_array); // Reload array
969 LoadValueDirectFixed(rl_index, r_index); // Reload index
970 LoadValueDirectFixed(rl_src, r_value); // Reload value
971 r_array_class = INVALID_REG;
972
973 // Branch here if value to be stored == null
974 LIR* target = NewLIR0(kPseudoTargetLabel);
975 null_value_check->target = target;
976
977 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
978 int reg_len = INVALID_REG;
979 if (needs_range_check) {
980 reg_len = TargetReg(kArg1);
981 LoadWordDisp(r_array, len_offset, reg_len); // Get len
982 }
983 /* r_ptr -> array data */
984 int r_ptr = AllocTemp();
985 OpRegRegImm(kOpAdd, r_ptr, r_array, data_offset);
986 if (needs_range_check) {
987 GenRegRegCheck(kCondCs, r_index, reg_len, kThrowArrayBounds);
988 }
989 StoreBaseIndexed(r_ptr, r_index, r_value, scale, kWord);
990 FreeTemp(r_ptr);
991 FreeTemp(r_index);
992 if (!mir_graph_->IsConstantNullRef(rl_src)) {
993 MarkGCCard(r_value, r_array);
994 }
995}
996
997void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700998 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 rl_src = LoadValueWide(rl_src, kCoreReg);
1000 // Per spec, we only care about low 6 bits of shift amount.
1001 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1002 if (shift_amount == 0) {
1003 StoreValueWide(rl_dest, rl_src);
1004 return;
1005 }
1006 if (BadOverlap(rl_src, rl_dest)) {
1007 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1008 return;
1009 }
1010 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001011 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 case Instruction::SHL_LONG:
1013 case Instruction::SHL_LONG_2ADDR:
1014 if (shift_amount == 1) {
1015 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, rl_src.low_reg);
1016 OpRegRegReg(kOpAdc, rl_result.high_reg, rl_src.high_reg, rl_src.high_reg);
1017 } else if (shift_amount == 32) {
1018 OpRegCopy(rl_result.high_reg, rl_src.low_reg);
1019 LoadConstant(rl_result.low_reg, 0);
1020 } else if (shift_amount > 31) {
1021 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.low_reg, shift_amount - 32);
1022 LoadConstant(rl_result.low_reg, 0);
1023 } else {
1024 OpRegRegImm(kOpLsl, rl_result.high_reg, rl_src.high_reg, shift_amount);
1025 OpRegRegRegShift(kOpOr, rl_result.high_reg, rl_result.high_reg, rl_src.low_reg,
1026 EncodeShift(kArmLsr, 32 - shift_amount));
1027 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, shift_amount);
1028 }
1029 break;
1030 case Instruction::SHR_LONG:
1031 case Instruction::SHR_LONG_2ADDR:
1032 if (shift_amount == 32) {
1033 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1034 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1035 } else if (shift_amount > 31) {
1036 OpRegRegImm(kOpAsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1037 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, 31);
1038 } else {
1039 int t_reg = AllocTemp();
1040 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1041 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1042 EncodeShift(kArmLsl, 32 - shift_amount));
1043 FreeTemp(t_reg);
1044 OpRegRegImm(kOpAsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1045 }
1046 break;
1047 case Instruction::USHR_LONG:
1048 case Instruction::USHR_LONG_2ADDR:
1049 if (shift_amount == 32) {
1050 OpRegCopy(rl_result.low_reg, rl_src.high_reg);
1051 LoadConstant(rl_result.high_reg, 0);
1052 } else if (shift_amount > 31) {
1053 OpRegRegImm(kOpLsr, rl_result.low_reg, rl_src.high_reg, shift_amount - 32);
1054 LoadConstant(rl_result.high_reg, 0);
1055 } else {
1056 int t_reg = AllocTemp();
1057 OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, shift_amount);
1058 OpRegRegRegShift(kOpOr, rl_result.low_reg, t_reg, rl_src.high_reg,
1059 EncodeShift(kArmLsl, 32 - shift_amount));
1060 FreeTemp(t_reg);
1061 OpRegRegImm(kOpLsr, rl_result.high_reg, rl_src.high_reg, shift_amount);
1062 }
1063 break;
1064 default:
1065 LOG(FATAL) << "Unexpected case";
1066 }
1067 StoreValueWide(rl_dest, rl_result);
1068}
1069
1070void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001071 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1073 if (!rl_src2.is_const) {
1074 // Don't bother with special handling for subtract from immediate.
1075 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1076 return;
1077 }
1078 } else {
1079 // Normalize
1080 if (!rl_src2.is_const) {
1081 DCHECK(rl_src1.is_const);
1082 RegLocation rl_temp = rl_src1;
1083 rl_src1 = rl_src2;
1084 rl_src2 = rl_temp;
1085 }
1086 }
1087 if (BadOverlap(rl_src1, rl_dest)) {
1088 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1089 return;
1090 }
1091 DCHECK(rl_src2.is_const);
1092 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1093 uint32_t val_lo = Low32Bits(val);
1094 uint32_t val_hi = High32Bits(val);
1095 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1096 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1097
1098 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001099 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 case Instruction::ADD_LONG:
1101 case Instruction::ADD_LONG_2ADDR:
1102 case Instruction::SUB_LONG:
1103 case Instruction::SUB_LONG_2ADDR:
1104 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1105 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1106 return;
1107 }
1108 break;
1109 default:
1110 break;
1111 }
1112 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1113 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1114 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1115 switch (opcode) {
1116 case Instruction::ADD_LONG:
1117 case Instruction::ADD_LONG_2ADDR:
1118 NewLIR3(kThumb2AddRRI8, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1119 NewLIR3(kThumb2AdcRRI8, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
1120 break;
1121 case Instruction::OR_LONG:
1122 case Instruction::OR_LONG_2ADDR:
1123 if ((val_lo != 0) || (rl_result.low_reg != rl_src1.low_reg)) {
1124 OpRegRegImm(kOpOr, rl_result.low_reg, rl_src1.low_reg, val_lo);
1125 }
1126 if ((val_hi != 0) || (rl_result.high_reg != rl_src1.high_reg)) {
1127 OpRegRegImm(kOpOr, rl_result.high_reg, rl_src1.high_reg, val_hi);
1128 }
1129 break;
1130 case Instruction::XOR_LONG:
1131 case Instruction::XOR_LONG_2ADDR:
1132 OpRegRegImm(kOpXor, rl_result.low_reg, rl_src1.low_reg, val_lo);
1133 OpRegRegImm(kOpXor, rl_result.high_reg, rl_src1.high_reg, val_hi);
1134 break;
1135 case Instruction::AND_LONG:
1136 case Instruction::AND_LONG_2ADDR:
1137 if ((val_lo != 0xffffffff) || (rl_result.low_reg != rl_src1.low_reg)) {
1138 OpRegRegImm(kOpAnd, rl_result.low_reg, rl_src1.low_reg, val_lo);
1139 }
1140 if ((val_hi != 0xffffffff) || (rl_result.high_reg != rl_src1.high_reg)) {
1141 OpRegRegImm(kOpAnd, rl_result.high_reg, rl_src1.high_reg, val_hi);
1142 }
1143 break;
1144 case Instruction::SUB_LONG_2ADDR:
1145 case Instruction::SUB_LONG:
1146 NewLIR3(kThumb2SubRRI8, rl_result.low_reg, rl_src1.low_reg, mod_imm_lo);
1147 NewLIR3(kThumb2SbcRRI8, rl_result.high_reg, rl_src1.high_reg, mod_imm_hi);
1148 break;
1149 default:
1150 LOG(FATAL) << "Unexpected opcode " << opcode;
1151 }
1152 StoreValueWide(rl_dest, rl_result);
1153}
1154
1155} // namespace art