blob: 7aff89e8b42f601214d2d4e21ec20e653ccea51c [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);
buzbee2c1ed452014-02-20 11:48:04 -080093 OpRegReg(kOpCmp, rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 LIR* branch1 = OpCondBranch(kCondLt, NULL);
95 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbee2c1ed452014-02-20 11:48:04 -080096 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 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
buzbee2c1ed452014-02-20 11:48:04 -0800110 rl_temp.reg.SetReg(t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 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);
buzbee2c1ed452014-02-20 11:48:04 -0800128 int32_t low_reg = rl_src1.reg.GetReg();
129 int32_t high_reg = rl_src1.reg.GetHighReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
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)) {
buzbee2c1ed452014-02-20 11:48:04 -0800181 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), 1);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000182 OpIT(kCondUlt, "");
buzbee2c1ed452014-02-20 11:48:04 -0800183 LoadConstant(rl_result.reg.GetReg(), 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)) {
buzbee2c1ed452014-02-20 11:48:04 -0800186 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 OpIT(kCondEq, "E");
buzbee2c1ed452014-02-20 11:48:04 -0800188 LoadConstant(rl_result.reg.GetReg(), true_val);
189 LoadConstant(rl_result.reg.GetReg(), 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);
buzbee2c1ed452014-02-20 11:48:04 -0800197 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 OpIT(kCondEq, "E");
buzbee2c1ed452014-02-20 11:48:04 -0800199 OpRegCopy(rl_result.reg.GetReg(), t_reg1);
200 OpRegCopy(rl_result.reg.GetReg(), 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);
buzbee2c1ed452014-02-20 11:48:04 -0800210 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
211 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
buzbee252254b2013-09-08 16:20:53 -0700212 OpIT(kCondNe, "");
buzbee2c1ed452014-02-20 11:48:04 -0800213 OpRegCopy(rl_result.reg.GetReg(), rl_false.reg.GetReg());
214 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
buzbee252254b2013-09-08 16:20:53 -0700215 OpIT(kCondEq, "");
buzbee2c1ed452014-02-20 11:48:04 -0800216 OpRegCopy(rl_result.reg.GetReg(), rl_true.reg.GetReg());
buzbee252254b2013-09-08 16:20:53 -0700217 } else { // Normal - select between the two.
218 OpIT(kCondEq, "E");
buzbee2c1ed452014-02-20 11:48:04 -0800219 OpRegCopy(rl_result.reg.GetReg(), rl_true.reg.GetReg());
220 OpRegCopy(rl_result.reg.GetReg(), rl_false.reg.GetReg());
buzbee252254b2013-09-08 16:20:53 -0700221 }
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);
buzbee2c1ed452014-02-20 11:48:04 -0800250 OpRegReg(kOpCmp, rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
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 }
buzbee2c1ed452014-02-20 11:48:04 -0800281 OpRegReg(kOpCmp, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 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) {
Vladimir Marko502c2a82014-02-06 11:52:07 +0000360 DCHECK_NE(src_lo, dest_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 OpRegCopy(dest_hi, src_hi);
362 OpRegCopy(dest_lo, src_lo);
363 } else {
364 OpRegCopy(dest_lo, src_lo);
365 OpRegCopy(dest_hi, src_hi);
366 }
367 }
368 }
369}
370
371// Table of magic divisors
372struct MagicTable {
373 uint32_t magic;
374 uint32_t shift;
375 DividePattern pattern;
376};
377
378static const MagicTable magic_table[] = {
379 {0, 0, DivideNone}, // 0
380 {0, 0, DivideNone}, // 1
381 {0, 0, DivideNone}, // 2
382 {0x55555556, 0, Divide3}, // 3
383 {0, 0, DivideNone}, // 4
384 {0x66666667, 1, Divide5}, // 5
385 {0x2AAAAAAB, 0, Divide3}, // 6
386 {0x92492493, 2, Divide7}, // 7
387 {0, 0, DivideNone}, // 8
388 {0x38E38E39, 1, Divide5}, // 9
389 {0x66666667, 2, Divide5}, // 10
390 {0x2E8BA2E9, 1, Divide5}, // 11
391 {0x2AAAAAAB, 1, Divide5}, // 12
392 {0x4EC4EC4F, 2, Divide5}, // 13
393 {0x92492493, 3, Divide7}, // 14
394 {0x88888889, 3, Divide7}, // 15
395};
396
397// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700398bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700399 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
401 return false;
402 }
403 DividePattern pattern = magic_table[lit].pattern;
404 if (pattern == DivideNone) {
405 return false;
406 }
407 // Tuning: add rem patterns
buzbee11b63d12013-08-27 07:34:17 -0700408 if (!is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 return false;
410 }
411
412 int r_magic = AllocTemp();
413 LoadConstant(r_magic, magic_table[lit].magic);
414 rl_src = LoadValue(rl_src, kCoreReg);
415 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
416 int r_hi = AllocTemp();
417 int r_lo = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -0800418 NewLIR4(kThumb2Smull, r_lo, r_hi, r_magic, rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700419 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 case Divide3:
buzbee2c1ed452014-02-20 11:48:04 -0800421 OpRegRegRegShift(kOpSub, rl_result.reg.GetReg(), r_hi,
422 rl_src.reg.GetReg(), EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 break;
424 case Divide5:
buzbee2c1ed452014-02-20 11:48:04 -0800425 OpRegRegImm(kOpAsr, r_lo, rl_src.reg.GetReg(), 31);
426 OpRegRegRegShift(kOpRsub, rl_result.reg.GetReg(), r_lo, r_hi,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 EncodeShift(kArmAsr, magic_table[lit].shift));
428 break;
429 case Divide7:
buzbee2c1ed452014-02-20 11:48:04 -0800430 OpRegReg(kOpAdd, r_hi, rl_src.reg.GetReg());
431 OpRegRegImm(kOpAsr, r_lo, rl_src.reg.GetReg(), 31);
432 OpRegRegRegShift(kOpRsub, rl_result.reg.GetReg(), r_lo, r_hi,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 EncodeShift(kArmAsr, magic_table[lit].shift));
434 break;
435 default:
436 LOG(FATAL) << "Unexpected pattern: " << pattern;
437 }
438 StoreValue(rl_dest, rl_result);
439 return true;
440}
441
442LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700443 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
445 return NULL;
446}
447
Mark Mendell2bf31e62014-01-23 12:13:40 -0800448RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
449 RegLocation rl_src2, bool is_div, bool check_zero) {
450 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
451 return rl_dest;
452}
453
454RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
455 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
456 return rl_dest;
457}
458
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700460 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700461 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
462
463 // Put the literal in a temp.
464 int lit_temp = AllocTemp();
465 LoadConstant(lit_temp, lit);
466 // Use the generic case for div/rem with arg2 in a register.
467 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
468 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
469 FreeTemp(lit_temp);
470
471 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472}
473
474RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700475 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700476 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
477 if (is_div) {
478 // Simple case, use sdiv instruction.
buzbee2c1ed452014-02-20 11:48:04 -0800479 OpRegRegReg(kOpDiv, rl_result.reg.GetReg(), reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700480 } else {
481 // Remainder case, use the following code:
482 // temp = reg1 / reg2 - integer division
483 // temp = temp * reg2
484 // dest = reg1 - temp
485
486 int temp = AllocTemp();
487 OpRegRegReg(kOpDiv, temp, reg1, reg2);
488 OpRegReg(kOpMul, temp, reg2);
buzbee2c1ed452014-02-20 11:48:04 -0800489 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700490 FreeTemp(temp);
491 }
492
493 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494}
495
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700496bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497 DCHECK_EQ(cu_->instruction_set, kThumb2);
498 RegLocation rl_src1 = info->args[0];
499 RegLocation rl_src2 = info->args[1];
500 rl_src1 = LoadValue(rl_src1, kCoreReg);
501 rl_src2 = LoadValue(rl_src2, kCoreReg);
502 RegLocation rl_dest = InlineTarget(info);
503 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2c1ed452014-02-20 11:48:04 -0800504 OpRegReg(kOpCmp, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2c1ed452014-02-20 11:48:04 -0800506 OpRegReg(kOpMov, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
507 OpRegReg(kOpMov, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 GenBarrier();
509 StoreValue(rl_dest, rl_result);
510 return true;
511}
512
Vladimir Markoe508a202013-11-04 15:24:22 +0000513bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
514 RegLocation rl_src_address = info->args[0]; // long address
515 rl_src_address.wide = 0; // ignore high half in info->args[1]
516 RegLocation rl_dest = InlineTarget(info);
517 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
518 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
519 if (size == kLong) {
520 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2c1ed452014-02-20 11:48:04 -0800521 if (rl_address.reg.GetReg() != rl_result.reg.GetReg()) {
522 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), kWord, INVALID_SREG);
523 LoadBaseDisp(rl_address.reg.GetReg(), 4, rl_result.reg.GetHighReg(), kWord, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000524 } else {
buzbee2c1ed452014-02-20 11:48:04 -0800525 LoadBaseDisp(rl_address.reg.GetReg(), 4, rl_result.reg.GetHighReg(), kWord, INVALID_SREG);
526 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), kWord, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000527 }
528 StoreValueWide(rl_dest, rl_result);
529 } else {
530 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
531 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
buzbee2c1ed452014-02-20 11:48:04 -0800532 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000533 StoreValue(rl_dest, rl_result);
534 }
535 return true;
536}
537
538bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
539 RegLocation rl_src_address = info->args[0]; // long address
540 rl_src_address.wide = 0; // ignore high half in info->args[1]
541 RegLocation rl_src_value = info->args[2]; // [size] value
542 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
543 if (size == kLong) {
544 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
545 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2c1ed452014-02-20 11:48:04 -0800546 StoreBaseDisp(rl_address.reg.GetReg(), 0, rl_value.reg.GetReg(), kWord);
547 StoreBaseDisp(rl_address.reg.GetReg(), 4, rl_value.reg.GetHighReg(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000548 } else {
549 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
550 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
551 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2c1ed452014-02-20 11:48:04 -0800552 StoreBaseDisp(rl_address.reg.GetReg(), 0, rl_value.reg.GetReg(), size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000553 }
554 return true;
555}
556
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700557void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 LOG(FATAL) << "Unexpected use of OpLea for Arm";
559}
560
Ian Rogers468532e2013-08-05 10:56:33 -0700561void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
563}
564
Vladimir Marko1c282e22013-11-21 14:49:47 +0000565bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 DCHECK_EQ(cu_->instruction_set, kThumb2);
567 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000568 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
569 RegLocation rl_src_offset = info->args[2]; // long low
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000571 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000572 // If is_long, high half is in info->args[5]
573 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
574 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 RegLocation rl_dest = InlineTarget(info); // boolean place for result
576
Vladimir Marko3e5af822013-11-21 15:01:20 +0000577 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
578 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
579 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
580 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
581 // into the same temps, reducing the number of required temps down to 5. We shall work
582 // around the potentially locked temp by using LR for r_ptr, unconditionally.
583 // TODO: Pass information about the need for more temps to the stack frame generation
584 // code so that we can rely on being able to allocate enough temps.
585 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
586 MarkTemp(rARM_LR);
587 FreeTemp(rARM_LR);
588 LockTemp(rARM_LR);
589 bool load_early = true;
590 if (is_long) {
591 bool expected_is_core_reg =
buzbee2c1ed452014-02-20 11:48:04 -0800592 rl_src_expected.location == kLocPhysReg && !IsFpReg(rl_src_expected.reg.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000593 bool new_value_is_core_reg =
buzbee2c1ed452014-02-20 11:48:04 -0800594 rl_src_new_value.location == kLocPhysReg && !IsFpReg(rl_src_new_value.reg.GetReg());
595 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(rl_src_expected.reg.GetReg());
596 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(rl_src_new_value.reg.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000597
598 if (!expected_is_good_reg && !new_value_is_good_reg) {
599 // None of expected/new_value is non-temp reg, need to load both late
600 load_early = false;
601 // Make sure they are not in the temp regs and the load will not be skipped.
602 if (expected_is_core_reg) {
buzbee2c1ed452014-02-20 11:48:04 -0800603 FlushRegWide(rl_src_expected.reg.GetReg(), rl_src_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000604 ClobberSReg(rl_src_expected.s_reg_low);
605 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
606 rl_src_expected.location = kLocDalvikFrame;
607 }
608 if (new_value_is_core_reg) {
buzbee2c1ed452014-02-20 11:48:04 -0800609 FlushRegWide(rl_src_new_value.reg.GetReg(), rl_src_new_value.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000610 ClobberSReg(rl_src_new_value.s_reg_low);
611 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
612 rl_src_new_value.location = kLocDalvikFrame;
613 }
614 }
615 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616
617 // Release store semantics, get the barrier out of the way. TODO: revisit
618 GenMemBarrier(kStoreLoad);
619
620 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000621 RegLocation rl_new_value;
622 if (!is_long) {
623 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
624 } else if (load_early) {
625 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
626 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627
Vladimir Marko1c282e22013-11-21 14:49:47 +0000628 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 // Mark card for object assuming new value is stored.
buzbee2c1ed452014-02-20 11:48:04 -0800630 MarkGCCard(rl_new_value.reg.GetReg(), rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
632
633 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
634
Vladimir Marko3e5af822013-11-21 15:01:20 +0000635 int r_ptr = rARM_LR;
buzbee2c1ed452014-02-20 11:48:04 -0800636 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg.GetReg(), rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637
638 // Free now unneeded rl_object and rl_offset to give more temps.
639 ClobberSReg(rl_object.s_reg_low);
buzbee2c1ed452014-02-20 11:48:04 -0800640 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 ClobberSReg(rl_offset.s_reg_low);
buzbee2c1ed452014-02-20 11:48:04 -0800642 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643
Vladimir Marko3e5af822013-11-21 15:01:20 +0000644 RegLocation rl_expected;
645 if (!is_long) {
646 rl_expected = LoadValue(rl_src_expected, kCoreReg);
647 } else if (load_early) {
648 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
649 } else {
buzbee2c1ed452014-02-20 11:48:04 -0800650 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
651 int low_reg = AllocTemp();
652 int high_reg = AllocTemp();
653 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
654 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000655 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656
Vladimir Marko3e5af822013-11-21 15:01:20 +0000657 // do {
658 // tmp = [r_ptr] - expected;
659 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
660 // result = tmp != 0;
661
662 int r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700663 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700664
Vladimir Marko3e5af822013-11-21 15:01:20 +0000665 if (is_long) {
666 int r_tmp_high = AllocTemp();
667 if (!load_early) {
buzbee2c1ed452014-02-20 11:48:04 -0800668 LoadValueDirectWide(rl_src_expected, rl_expected.reg.GetReg(), rl_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000669 }
670 NewLIR3(kThumb2Ldrexd, r_tmp, r_tmp_high, r_ptr);
buzbee2c1ed452014-02-20 11:48:04 -0800671 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetReg());
672 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000673 if (!load_early) {
buzbee2c1ed452014-02-20 11:48:04 -0800674 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg.GetReg(), rl_new_value.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000675 }
676 // Make sure we use ORR that sets the ccode
677 if (ARM_LOWREG(r_tmp) && ARM_LOWREG(r_tmp_high)) {
678 NewLIR2(kThumbOrr, r_tmp, r_tmp_high);
679 } else {
680 NewLIR4(kThumb2OrrRRRs, r_tmp, r_tmp, r_tmp_high, 0);
681 }
682 FreeTemp(r_tmp_high); // Now unneeded
683
684 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
685 OpIT(kCondEq, "T");
buzbee2c1ed452014-02-20 11:48:04 -0800686 NewLIR4(kThumb2Strexd /* eq */, r_tmp, rl_new_value.reg.GetReg(), rl_new_value.reg.GetHighReg(), r_ptr);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000687
688 } else {
689 NewLIR3(kThumb2Ldrex, r_tmp, r_ptr, 0);
buzbee2c1ed452014-02-20 11:48:04 -0800690 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000691 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
692 OpIT(kCondEq, "T");
buzbee2c1ed452014-02-20 11:48:04 -0800693 NewLIR4(kThumb2Strex /* eq */, r_tmp, rl_new_value.reg.GetReg(), r_ptr, 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000694 }
695
696 // Still one conditional left from OpIT(kCondEq, "T") from either branch
697 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700698 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699
Vladimir Marko3e5af822013-11-21 15:01:20 +0000700 if (!load_early) {
buzbee2c1ed452014-02-20 11:48:04 -0800701 FreeTemp(rl_expected.reg.GetReg()); // Now unneeded.
702 FreeTemp(rl_expected.reg.GetHighReg()); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000703 }
704
705 // result := (tmp1 != 0) ? 0 : 1;
706 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2c1ed452014-02-20 11:48:04 -0800707 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000708 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000709 OpIT(kCondUlt, "");
buzbee2c1ed452014-02-20 11:48:04 -0800710 LoadConstant(rl_result.reg.GetReg(), 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000711 FreeTemp(r_tmp); // Now unneeded.
712
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 StoreValue(rl_dest, rl_result);
714
Vladimir Marko3e5af822013-11-21 15:01:20 +0000715 // Now, restore lr to its non-temp status.
716 Clobber(rARM_LR);
717 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 return true;
719}
720
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700721LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
723}
724
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700725LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
727}
728
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700729LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
731}
732
733void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
734 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700735 int first_bit, int second_bit) {
buzbee2c1ed452014-02-20 11:48:04 -0800736 OpRegRegRegShift(kOpAdd, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 EncodeShift(kArmLsl, second_bit - first_bit));
738 if (first_bit != 0) {
buzbee2c1ed452014-02-20 11:48:04 -0800739 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_result.reg.GetReg(), first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 }
741}
742
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700743void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 int t_reg = AllocTemp();
745 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
746 FreeTemp(t_reg);
747 GenCheck(kCondEq, kThrowDivZero);
748}
749
750// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700751LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
753 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
754}
755
756// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700757LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 // Combine sub & test using sub setflags encoding here
759 NewLIR3(kThumb2SubsRRI12, reg, reg, 1);
760 return OpCondBranch(c_code, target);
761}
762
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700763void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764#if ANDROID_SMP != 0
765 int dmb_flavor;
766 // TODO: revisit Arm barrier kinds
767 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800768 case kLoadStore: dmb_flavor = kISH; break;
769 case kLoadLoad: dmb_flavor = kISH; break;
770 case kStoreStore: dmb_flavor = kISHST; break;
771 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 default:
773 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
774 dmb_flavor = kSY; // quiet gcc.
775 break;
776 }
777 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700778 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779#endif
780}
781
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700782void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 rl_src = LoadValueWide(rl_src, kCoreReg);
784 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
785 int z_reg = AllocTemp();
786 LoadConstantNoClobber(z_reg, 0);
787 // Check for destructive overlap
buzbee2c1ed452014-02-20 11:48:04 -0800788 if (rl_result.reg.GetReg() == rl_src.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 int t_reg = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -0800790 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
791 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 FreeTemp(t_reg);
793 } else {
buzbee2c1ed452014-02-20 11:48:04 -0800794 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
795 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 }
797 FreeTemp(z_reg);
798 StoreValueWide(rl_dest, rl_result);
799}
800
Mark Mendelle02d48f2014-01-15 11:19:23 -0800801void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
802 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 /*
804 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
805 * registers. Normally for Arm, we get 5. We can get to 6 by including
806 * lr in the temp set. The only problematic case is all operands and result are
807 * distinct, and none have been promoted. In that case, we can succeed by aggressively
808 * freeing operand temp registers after they are no longer needed. All other cases
809 * can proceed normally. We'll just punt on the case of the result having a misaligned
810 * overlap with either operand and send that case to a runtime handler.
811 */
812 RegLocation rl_result;
813 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700814 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 FlushAllRegs();
816 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
817 rl_result = GetReturnWide(false);
818 StoreValueWide(rl_dest, rl_result);
819 return;
820 }
821 // Temporarily add LR to the temp pool, and assign it to tmp1
822 MarkTemp(rARM_LR);
823 FreeTemp(rARM_LR);
824 int tmp1 = rARM_LR;
825 LockTemp(rARM_LR);
826
827 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
828 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
829
830 bool special_case = true;
831 // If operands are the same, or any pair has been promoted we're not the special case.
832 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
buzbee2c1ed452014-02-20 11:48:04 -0800833 (!IsTemp(rl_src1.reg.GetReg()) && !IsTemp(rl_src1.reg.GetHighReg())) ||
834 (!IsTemp(rl_src2.reg.GetReg()) && !IsTemp(rl_src2.reg.GetHighReg()))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 special_case = false;
836 }
837 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
838 int res_lo = AllocTemp();
839 int res_hi;
buzbee2c1ed452014-02-20 11:48:04 -0800840 if (rl_src1.reg.GetReg() == rl_src2.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841 res_hi = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -0800842 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.reg.GetReg(), rl_src1.reg.GetHighReg());
843 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
845 } else {
846 // In the special case, all temps are now allocated
buzbee2c1ed452014-02-20 11:48:04 -0800847 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.reg.GetReg(), rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 if (special_case) {
buzbee2c1ed452014-02-20 11:48:04 -0800849 DCHECK_NE(rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
850 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
851 FreeTemp(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852 }
853 res_hi = AllocTemp();
854
buzbee2c1ed452014-02-20 11:48:04 -0800855 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.reg.GetReg(), rl_src1.reg.GetReg());
856 NewLIR4(kThumb2Mla, tmp1, rl_src1.reg.GetReg(), rl_src2.reg.GetHighReg(), tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
858 if (special_case) {
buzbee2c1ed452014-02-20 11:48:04 -0800859 FreeTemp(rl_src1.reg.GetReg());
860 Clobber(rl_src1.reg.GetReg());
861 Clobber(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 }
863 }
864 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700865 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2c1ed452014-02-20 11:48:04 -0800866 rl_result.reg.SetReg(res_lo);
867 rl_result.reg.SetHighReg(res_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 StoreValueWide(rl_dest, rl_result);
869 // Now, restore lr to its non-temp status.
870 Clobber(rARM_LR);
871 UnmarkTemp(rARM_LR);
872}
873
Mark Mendelle02d48f2014-01-15 11:19:23 -0800874void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700875 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
877}
878
Mark Mendelle02d48f2014-01-15 11:19:23 -0800879void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700880 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
882}
883
Mark Mendelle02d48f2014-01-15 11:19:23 -0800884void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700885 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
887}
888
Mark Mendelle02d48f2014-01-15 11:19:23 -0800889void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700890 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
892}
893
Mark Mendelle02d48f2014-01-15 11:19:23 -0800894void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700895 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
897}
898
899/*
900 * Generate array load
901 */
902void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700903 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 RegisterClass reg_class = oat_reg_class_by_size(size);
905 int len_offset = mirror::Array::LengthOffset().Int32Value();
906 int data_offset;
907 RegLocation rl_result;
908 bool constant_index = rl_index.is_const;
909 rl_array = LoadValue(rl_array, kCoreReg);
910 if (!constant_index) {
911 rl_index = LoadValue(rl_index, kCoreReg);
912 }
913
914 if (rl_dest.wide) {
915 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
916 } else {
917 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
918 }
919
920 // If index is constant, just fold it into the data offset
921 if (constant_index) {
922 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
923 }
924
925 /* null object? */
buzbee2c1ed452014-02-20 11:48:04 -0800926 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927
928 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
929 int reg_len = INVALID_REG;
930 if (needs_range_check) {
931 reg_len = AllocTemp();
932 /* Get len */
buzbee2c1ed452014-02-20 11:48:04 -0800933 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 }
935 if (rl_dest.wide || rl_dest.fp || constant_index) {
936 int reg_ptr;
937 if (constant_index) {
buzbee2c1ed452014-02-20 11:48:04 -0800938 reg_ptr = rl_array.reg.GetReg(); // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 } else {
940 // No special indexed operation, lea + load w/ displacement
941 reg_ptr = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -0800942 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 EncodeShift(kArmLsl, scale));
buzbee2c1ed452014-02-20 11:48:04 -0800944 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700945 }
946 rl_result = EvalLoc(rl_dest, reg_class, true);
947
948 if (needs_range_check) {
949 if (constant_index) {
950 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
951 } else {
buzbee2c1ed452014-02-20 11:48:04 -0800952 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 }
954 FreeTemp(reg_len);
955 }
956 if (rl_dest.wide) {
buzbee2c1ed452014-02-20 11:48:04 -0800957 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 if (!constant_index) {
959 FreeTemp(reg_ptr);
960 }
961 StoreValueWide(rl_dest, rl_result);
962 } else {
buzbee2c1ed452014-02-20 11:48:04 -0800963 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg.GetReg(), size, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 if (!constant_index) {
965 FreeTemp(reg_ptr);
966 }
967 StoreValue(rl_dest, rl_result);
968 }
969 } else {
970 // Offset base, then use indexed load
971 int reg_ptr = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -0800972 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
973 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 rl_result = EvalLoc(rl_dest, reg_class, true);
975
976 if (needs_range_check) {
buzbee2c1ed452014-02-20 11:48:04 -0800977 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 FreeTemp(reg_len);
979 }
buzbee2c1ed452014-02-20 11:48:04 -0800980 LoadBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_result.reg.GetReg(), scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 FreeTemp(reg_ptr);
982 StoreValue(rl_dest, rl_result);
983 }
984}
985
986/*
987 * Generate array store
988 *
989 */
990void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700991 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 RegisterClass reg_class = oat_reg_class_by_size(size);
993 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 bool constant_index = rl_index.is_const;
995
Ian Rogersa9a82542013-10-04 11:17:26 -0700996 int data_offset;
997 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
999 } else {
1000 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1001 }
1002
1003 // If index is constant, just fold it into the data offset.
1004 if (constant_index) {
1005 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1006 }
1007
1008 rl_array = LoadValue(rl_array, kCoreReg);
1009 if (!constant_index) {
1010 rl_index = LoadValue(rl_index, kCoreReg);
1011 }
1012
1013 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001014 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 if (constant_index) {
buzbee2c1ed452014-02-20 11:48:04 -08001016 reg_ptr = rl_array.reg.GetReg();
1017 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1018 Clobber(rl_array.reg.GetReg());
1019 reg_ptr = rl_array.reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001021 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 reg_ptr = AllocTemp();
1023 }
1024
1025 /* null object? */
buzbee2c1ed452014-02-20 11:48:04 -08001026 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027
1028 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1029 int reg_len = INVALID_REG;
1030 if (needs_range_check) {
1031 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001032 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 /* Get len */
buzbee2c1ed452014-02-20 11:48:04 -08001034 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036 /* at this point, reg_ptr points to array, 2 live temps */
1037 if (rl_src.wide || rl_src.fp || constant_index) {
1038 if (rl_src.wide) {
1039 rl_src = LoadValueWide(rl_src, reg_class);
1040 } else {
1041 rl_src = LoadValue(rl_src, reg_class);
1042 }
1043 if (!constant_index) {
buzbee2c1ed452014-02-20 11:48:04 -08001044 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 EncodeShift(kArmLsl, scale));
1046 }
1047 if (needs_range_check) {
1048 if (constant_index) {
1049 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1050 } else {
buzbee2c1ed452014-02-20 11:48:04 -08001051 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 }
1053 FreeTemp(reg_len);
1054 }
1055
1056 if (rl_src.wide) {
buzbee2c1ed452014-02-20 11:48:04 -08001057 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 } else {
buzbee2c1ed452014-02-20 11:48:04 -08001059 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg.GetReg(), size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 }
1061 } else {
1062 /* reg_ptr -> array data */
buzbee2c1ed452014-02-20 11:48:04 -08001063 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 rl_src = LoadValue(rl_src, reg_class);
1065 if (needs_range_check) {
buzbee2c1ed452014-02-20 11:48:04 -08001066 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 FreeTemp(reg_len);
1068 }
buzbee2c1ed452014-02-20 11:48:04 -08001069 StoreBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 scale, size);
1071 }
Ian Rogers773aab12013-10-14 13:50:10 -07001072 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 FreeTemp(reg_ptr);
1074 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001075 if (card_mark) {
buzbee2c1ed452014-02-20 11:48:04 -08001076 MarkGCCard(rl_src.reg.GetReg(), rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 }
1078}
1079
Ian Rogersa9a82542013-10-04 11:17:26 -07001080
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 rl_src = LoadValueWide(rl_src, kCoreReg);
1084 // Per spec, we only care about low 6 bits of shift amount.
1085 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1086 if (shift_amount == 0) {
1087 StoreValueWide(rl_dest, rl_src);
1088 return;
1089 }
1090 if (BadOverlap(rl_src, rl_dest)) {
1091 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1092 return;
1093 }
1094 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001095 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 case Instruction::SHL_LONG:
1097 case Instruction::SHL_LONG_2ADDR:
1098 if (shift_amount == 1) {
buzbee2c1ed452014-02-20 11:48:04 -08001099 OpRegRegReg(kOpAdd, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg());
1100 OpRegRegReg(kOpAdc, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 } else if (shift_amount == 32) {
buzbee2c1ed452014-02-20 11:48:04 -08001102 OpRegCopy(rl_result.reg.GetHighReg(), rl_src.reg.GetReg());
1103 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 } else if (shift_amount > 31) {
buzbee2c1ed452014-02-20 11:48:04 -08001105 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetReg(), shift_amount - 32);
1106 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 } else {
buzbee2c1ed452014-02-20 11:48:04 -08001108 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
1109 OpRegRegRegShift(kOpOr, rl_result.reg.GetHighReg(), rl_result.reg.GetHighReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2c1ed452014-02-20 11:48:04 -08001111 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 }
1113 break;
1114 case Instruction::SHR_LONG:
1115 case Instruction::SHR_LONG_2ADDR:
1116 if (shift_amount == 32) {
buzbee2c1ed452014-02-20 11:48:04 -08001117 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1118 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 } else if (shift_amount > 31) {
buzbee2c1ed452014-02-20 11:48:04 -08001120 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1121 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
1123 int t_reg = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -08001124 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1125 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 EncodeShift(kArmLsl, 32 - shift_amount));
1127 FreeTemp(t_reg);
buzbee2c1ed452014-02-20 11:48:04 -08001128 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 }
1130 break;
1131 case Instruction::USHR_LONG:
1132 case Instruction::USHR_LONG_2ADDR:
1133 if (shift_amount == 32) {
buzbee2c1ed452014-02-20 11:48:04 -08001134 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1135 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 } else if (shift_amount > 31) {
buzbee2c1ed452014-02-20 11:48:04 -08001137 OpRegRegImm(kOpLsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1138 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 } else {
1140 int t_reg = AllocTemp();
buzbee2c1ed452014-02-20 11:48:04 -08001141 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1142 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 EncodeShift(kArmLsl, 32 - shift_amount));
1144 FreeTemp(t_reg);
buzbee2c1ed452014-02-20 11:48:04 -08001145 OpRegRegImm(kOpLsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 }
1147 break;
1148 default:
1149 LOG(FATAL) << "Unexpected case";
1150 }
1151 StoreValueWide(rl_dest, rl_result);
1152}
1153
1154void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001155 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1157 if (!rl_src2.is_const) {
1158 // Don't bother with special handling for subtract from immediate.
1159 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1160 return;
1161 }
1162 } else {
1163 // Normalize
1164 if (!rl_src2.is_const) {
1165 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001166 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
1168 }
1169 if (BadOverlap(rl_src1, rl_dest)) {
1170 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1171 return;
1172 }
1173 DCHECK(rl_src2.is_const);
1174 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1175 uint32_t val_lo = Low32Bits(val);
1176 uint32_t val_hi = High32Bits(val);
1177 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1178 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1179
1180 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001181 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 case Instruction::ADD_LONG:
1183 case Instruction::ADD_LONG_2ADDR:
1184 case Instruction::SUB_LONG:
1185 case Instruction::SUB_LONG_2ADDR:
1186 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1187 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1188 return;
1189 }
1190 break;
1191 default:
1192 break;
1193 }
1194 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1195 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1196 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1197 switch (opcode) {
1198 case Instruction::ADD_LONG:
1199 case Instruction::ADD_LONG_2ADDR:
buzbee2c1ed452014-02-20 11:48:04 -08001200 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1201 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 break;
1203 case Instruction::OR_LONG:
1204 case Instruction::OR_LONG_2ADDR:
buzbee2c1ed452014-02-20 11:48:04 -08001205 if ((val_lo != 0) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1206 OpRegRegImm(kOpOr, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001207 }
buzbee2c1ed452014-02-20 11:48:04 -08001208 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1209 OpRegRegImm(kOpOr, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 }
1211 break;
1212 case Instruction::XOR_LONG:
1213 case Instruction::XOR_LONG_2ADDR:
buzbee2c1ed452014-02-20 11:48:04 -08001214 OpRegRegImm(kOpXor, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
1215 OpRegRegImm(kOpXor, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 break;
1217 case Instruction::AND_LONG:
1218 case Instruction::AND_LONG_2ADDR:
buzbee2c1ed452014-02-20 11:48:04 -08001219 if ((val_lo != 0xffffffff) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1220 OpRegRegImm(kOpAnd, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 }
buzbee2c1ed452014-02-20 11:48:04 -08001222 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1223 OpRegRegImm(kOpAnd, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 }
1225 break;
1226 case Instruction::SUB_LONG_2ADDR:
1227 case Instruction::SUB_LONG:
buzbee2c1ed452014-02-20 11:48:04 -08001228 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1229 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 break;
1231 default:
1232 LOG(FATAL) << "Unexpected opcode " << opcode;
1233 }
1234 StoreValueWide(rl_dest, rl_result);
1235}
1236
1237} // namespace art