blob: 94c8844dc0a3586c72c6709fe690d46d950de77f [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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +000093 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +000096 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
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000110 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000128 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)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000181 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), 1);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000182 OpIT(kCondUlt, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000183 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)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000186 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 OpIT(kCondEq, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000188 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000197 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 OpIT(kCondEq, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000199 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000210 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, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000213 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, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000216 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");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000219 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000250 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 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000281 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();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000418 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:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000421 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:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000425 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:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000430 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.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000479 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000489 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000504 OpRegReg(kOpCmp, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 OpIT((is_min) ? kCondGt : kCondLt, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000506 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.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000521 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 {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000525 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.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000532 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000546 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000552 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 =
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000592 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 =
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000594 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000603 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000609 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.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000630 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;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000636 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000640 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000642 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 {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000650 // 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000668 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000671 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000674 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");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000686 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000690 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");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000693 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000701 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);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000707 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, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000710 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000736 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) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000739 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
Vladimir Markodbb8c492014-02-28 17:36:39 +0000759 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
760 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 return OpCondBranch(c_code, target);
762}
763
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700764void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765#if ANDROID_SMP != 0
766 int dmb_flavor;
767 // TODO: revisit Arm barrier kinds
768 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800769 case kLoadStore: dmb_flavor = kISH; break;
770 case kLoadLoad: dmb_flavor = kISH; break;
771 case kStoreStore: dmb_flavor = kISHST; break;
772 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 default:
774 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
775 dmb_flavor = kSY; // quiet gcc.
776 break;
777 }
778 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700779 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700780#endif
781}
782
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700783void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 rl_src = LoadValueWide(rl_src, kCoreReg);
785 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
786 int z_reg = AllocTemp();
787 LoadConstantNoClobber(z_reg, 0);
788 // Check for destructive overlap
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000789 if (rl_result.reg.GetReg() == rl_src.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000791 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
792 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 FreeTemp(t_reg);
794 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000795 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
796 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 }
798 FreeTemp(z_reg);
799 StoreValueWide(rl_dest, rl_result);
800}
801
Mark Mendelle02d48f2014-01-15 11:19:23 -0800802void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
803 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 /*
805 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
806 * registers. Normally for Arm, we get 5. We can get to 6 by including
807 * lr in the temp set. The only problematic case is all operands and result are
808 * distinct, and none have been promoted. In that case, we can succeed by aggressively
809 * freeing operand temp registers after they are no longer needed. All other cases
810 * can proceed normally. We'll just punt on the case of the result having a misaligned
811 * overlap with either operand and send that case to a runtime handler.
812 */
813 RegLocation rl_result;
814 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700815 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 FlushAllRegs();
817 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
818 rl_result = GetReturnWide(false);
819 StoreValueWide(rl_dest, rl_result);
820 return;
821 }
822 // Temporarily add LR to the temp pool, and assign it to tmp1
823 MarkTemp(rARM_LR);
824 FreeTemp(rARM_LR);
825 int tmp1 = rARM_LR;
826 LockTemp(rARM_LR);
827
828 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
829 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
830
831 bool special_case = true;
832 // If operands are the same, or any pair has been promoted we're not the special case.
833 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000834 (!IsTemp(rl_src1.reg.GetReg()) && !IsTemp(rl_src1.reg.GetHighReg())) ||
835 (!IsTemp(rl_src2.reg.GetReg()) && !IsTemp(rl_src2.reg.GetHighReg()))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 special_case = false;
837 }
838 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
839 int res_lo = AllocTemp();
840 int res_hi;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000841 if (rl_src1.reg.GetReg() == rl_src2.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 res_hi = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000843 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.reg.GetReg(), rl_src1.reg.GetHighReg());
844 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
846 } else {
847 // In the special case, all temps are now allocated
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000848 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.reg.GetReg(), rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 if (special_case) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000850 DCHECK_NE(rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
851 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
852 FreeTemp(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 }
854 res_hi = AllocTemp();
855
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000856 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.reg.GetReg(), rl_src1.reg.GetReg());
857 NewLIR4(kThumb2Mla, tmp1, rl_src1.reg.GetReg(), rl_src2.reg.GetHighReg(), tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
859 if (special_case) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000860 FreeTemp(rl_src1.reg.GetReg());
861 Clobber(rl_src1.reg.GetReg());
862 Clobber(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 }
864 }
865 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700866 rl_result = GetReturnWide(false); // Just using as a template.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000867 rl_result.reg.SetReg(res_lo);
868 rl_result.reg.SetHighReg(res_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 StoreValueWide(rl_dest, rl_result);
870 // Now, restore lr to its non-temp status.
871 Clobber(rARM_LR);
872 UnmarkTemp(rARM_LR);
873}
874
Mark Mendelle02d48f2014-01-15 11:19:23 -0800875void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700876 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
878}
879
Mark Mendelle02d48f2014-01-15 11:19:23 -0800880void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700881 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
883}
884
Mark Mendelle02d48f2014-01-15 11:19:23 -0800885void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700886 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
888}
889
Mark Mendelle02d48f2014-01-15 11:19:23 -0800890void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700891 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
893}
894
Mark Mendelle02d48f2014-01-15 11:19:23 -0800895void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
898}
899
900/*
901 * Generate array load
902 */
903void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700904 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 RegisterClass reg_class = oat_reg_class_by_size(size);
906 int len_offset = mirror::Array::LengthOffset().Int32Value();
907 int data_offset;
908 RegLocation rl_result;
909 bool constant_index = rl_index.is_const;
910 rl_array = LoadValue(rl_array, kCoreReg);
911 if (!constant_index) {
912 rl_index = LoadValue(rl_index, kCoreReg);
913 }
914
915 if (rl_dest.wide) {
916 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
917 } else {
918 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
919 }
920
921 // If index is constant, just fold it into the data offset
922 if (constant_index) {
923 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
924 }
925
926 /* null object? */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000927 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700928
929 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
930 int reg_len = INVALID_REG;
931 if (needs_range_check) {
932 reg_len = AllocTemp();
933 /* Get len */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000934 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 }
936 if (rl_dest.wide || rl_dest.fp || constant_index) {
937 int reg_ptr;
938 if (constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000939 reg_ptr = rl_array.reg.GetReg(); // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 } else {
941 // No special indexed operation, lea + load w/ displacement
942 reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000943 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000945 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 }
947 rl_result = EvalLoc(rl_dest, reg_class, true);
948
949 if (needs_range_check) {
950 if (constant_index) {
951 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
952 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000953 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 }
955 FreeTemp(reg_len);
956 }
957 if (rl_dest.wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000958 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 if (!constant_index) {
960 FreeTemp(reg_ptr);
961 }
962 StoreValueWide(rl_dest, rl_result);
963 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000964 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg.GetReg(), size, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 if (!constant_index) {
966 FreeTemp(reg_ptr);
967 }
968 StoreValue(rl_dest, rl_result);
969 }
970 } else {
971 // Offset base, then use indexed load
972 int reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000973 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
974 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 rl_result = EvalLoc(rl_dest, reg_class, true);
976
977 if (needs_range_check) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000978 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 FreeTemp(reg_len);
980 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000981 LoadBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_result.reg.GetReg(), scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 FreeTemp(reg_ptr);
983 StoreValue(rl_dest, rl_result);
984 }
985}
986
987/*
988 * Generate array store
989 *
990 */
991void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700992 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 RegisterClass reg_class = oat_reg_class_by_size(size);
994 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 bool constant_index = rl_index.is_const;
996
Ian Rogersa9a82542013-10-04 11:17:26 -0700997 int data_offset;
998 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1000 } else {
1001 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1002 }
1003
1004 // If index is constant, just fold it into the data offset.
1005 if (constant_index) {
1006 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1007 }
1008
1009 rl_array = LoadValue(rl_array, kCoreReg);
1010 if (!constant_index) {
1011 rl_index = LoadValue(rl_index, kCoreReg);
1012 }
1013
1014 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001015 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 if (constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001017 reg_ptr = rl_array.reg.GetReg();
1018 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1019 Clobber(rl_array.reg.GetReg());
1020 reg_ptr = rl_array.reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001022 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 reg_ptr = AllocTemp();
1024 }
1025
1026 /* null object? */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001027 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028
1029 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1030 int reg_len = INVALID_REG;
1031 if (needs_range_check) {
1032 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001033 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 /* Get len */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001035 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 }
1037 /* at this point, reg_ptr points to array, 2 live temps */
1038 if (rl_src.wide || rl_src.fp || constant_index) {
1039 if (rl_src.wide) {
1040 rl_src = LoadValueWide(rl_src, reg_class);
1041 } else {
1042 rl_src = LoadValue(rl_src, reg_class);
1043 }
1044 if (!constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001045 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 EncodeShift(kArmLsl, scale));
1047 }
1048 if (needs_range_check) {
1049 if (constant_index) {
1050 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1051 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001052 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 }
1054 FreeTemp(reg_len);
1055 }
1056
1057 if (rl_src.wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001058 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001060 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg.GetReg(), size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 }
1062 } else {
1063 /* reg_ptr -> array data */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001064 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 rl_src = LoadValue(rl_src, reg_class);
1066 if (needs_range_check) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001067 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 FreeTemp(reg_len);
1069 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001070 StoreBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 scale, size);
1072 }
Ian Rogers773aab12013-10-14 13:50:10 -07001073 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 FreeTemp(reg_ptr);
1075 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001076 if (card_mark) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001077 MarkGCCard(rl_src.reg.GetReg(), rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 }
1079}
1080
Ian Rogersa9a82542013-10-04 11:17:26 -07001081
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001083 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 rl_src = LoadValueWide(rl_src, kCoreReg);
1085 // Per spec, we only care about low 6 bits of shift amount.
1086 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1087 if (shift_amount == 0) {
1088 StoreValueWide(rl_dest, rl_src);
1089 return;
1090 }
1091 if (BadOverlap(rl_src, rl_dest)) {
1092 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1093 return;
1094 }
1095 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001096 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001097 case Instruction::SHL_LONG:
1098 case Instruction::SHL_LONG_2ADDR:
1099 if (shift_amount == 1) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001100 OpRegRegReg(kOpAdd, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg());
1101 OpRegRegReg(kOpAdc, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 } else if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001103 OpRegCopy(rl_result.reg.GetHighReg(), rl_src.reg.GetReg());
1104 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001106 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetReg(), shift_amount - 32);
1107 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001109 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
1110 OpRegRegRegShift(kOpOr, rl_result.reg.GetHighReg(), rl_result.reg.GetHighReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 EncodeShift(kArmLsr, 32 - shift_amount));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001112 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
1114 break;
1115 case Instruction::SHR_LONG:
1116 case Instruction::SHR_LONG_2ADDR:
1117 if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001118 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1119 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001121 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1122 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 } else {
1124 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001125 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1126 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 EncodeShift(kArmLsl, 32 - shift_amount));
1128 FreeTemp(t_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001129 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 }
1131 break;
1132 case Instruction::USHR_LONG:
1133 case Instruction::USHR_LONG_2ADDR:
1134 if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001135 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1136 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001138 OpRegRegImm(kOpLsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1139 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 } else {
1141 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001142 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1143 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 EncodeShift(kArmLsl, 32 - shift_amount));
1145 FreeTemp(t_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001146 OpRegRegImm(kOpLsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148 break;
1149 default:
1150 LOG(FATAL) << "Unexpected case";
1151 }
1152 StoreValueWide(rl_dest, rl_result);
1153}
1154
1155void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001156 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1158 if (!rl_src2.is_const) {
1159 // Don't bother with special handling for subtract from immediate.
1160 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1161 return;
1162 }
1163 } else {
1164 // Normalize
1165 if (!rl_src2.is_const) {
1166 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001167 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 }
1169 }
1170 if (BadOverlap(rl_src1, rl_dest)) {
1171 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1172 return;
1173 }
1174 DCHECK(rl_src2.is_const);
1175 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1176 uint32_t val_lo = Low32Bits(val);
1177 uint32_t val_hi = High32Bits(val);
1178 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1179 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1180
1181 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001182 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 case Instruction::ADD_LONG:
1184 case Instruction::ADD_LONG_2ADDR:
1185 case Instruction::SUB_LONG:
1186 case Instruction::SUB_LONG_2ADDR:
1187 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1188 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1189 return;
1190 }
1191 break;
1192 default:
1193 break;
1194 }
1195 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1196 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1197 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1198 switch (opcode) {
1199 case Instruction::ADD_LONG:
1200 case Instruction::ADD_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001201 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1202 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 break;
1204 case Instruction::OR_LONG:
1205 case Instruction::OR_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001206 if ((val_lo != 0) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1207 OpRegRegImm(kOpOr, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001209 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1210 OpRegRegImm(kOpOr, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 }
1212 break;
1213 case Instruction::XOR_LONG:
1214 case Instruction::XOR_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001215 OpRegRegImm(kOpXor, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
1216 OpRegRegImm(kOpXor, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 break;
1218 case Instruction::AND_LONG:
1219 case Instruction::AND_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001220 if ((val_lo != 0xffffffff) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1221 OpRegRegImm(kOpAnd, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001223 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1224 OpRegRegImm(kOpAnd, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 }
1226 break;
1227 case Instruction::SUB_LONG_2ADDR:
1228 case Instruction::SUB_LONG:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001229 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1230 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 break;
1232 default:
1233 LOG(FATAL) << "Unexpected opcode " << opcode;
1234 }
1235 StoreValueWide(rl_dest, rl_result);
1236}
1237
1238} // namespace art