blob: fb2096f1e9b48957083a7233feee6504d1b1db9e [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);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000175 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 if (mir->ssa_rep->num_uses == 1) {
177 // CONST case
178 int true_val = mir->dalvikInsn.vB;
179 int false_val = mir->dalvikInsn.vC;
180 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000181 // Change kCondNe to kCondEq for the special cases below.
182 if (ccode == kCondNe) {
183 ccode = kCondEq;
184 std::swap(true_val, false_val);
185 }
186 bool cheap_false_val = InexpensiveConstantInt(false_val);
187 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
188 OpRegRegImm(kOpSub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), -true_val);
189 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
190 OpIT(true_val == 0 ? kCondNe : kCondUge, "");
191 LoadConstant(rl_result.reg.GetReg(), false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700192 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000193 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
194 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), rl_src.reg.GetReg(), 1);
195 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
196 OpIT(kCondLs, "");
197 LoadConstant(rl_result.reg.GetReg(), false_val);
198 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
199 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000200 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000201 OpIT(ccode, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000202 LoadConstant(rl_result.reg.GetReg(), true_val);
203 LoadConstant(rl_result.reg.GetReg(), false_val);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700204 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 } else {
206 // Unlikely case - could be tuned.
207 int t_reg1 = AllocTemp();
208 int t_reg2 = AllocTemp();
209 LoadConstant(t_reg1, true_val);
210 LoadConstant(t_reg2, false_val);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000211 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000212 OpIT(ccode, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000213 OpRegCopy(rl_result.reg.GetReg(), t_reg1);
214 OpRegCopy(rl_result.reg.GetReg(), t_reg2);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700215 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 }
217 } else {
218 // MOVE case
219 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
220 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
221 rl_true = LoadValue(rl_true, kCoreReg);
222 rl_false = LoadValue(rl_false, kCoreReg);
223 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000224 OpRegImm(kOpCmp, rl_src.reg.GetReg(), 0);
225 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Vladimir Markoa1a70742014-03-03 10:28:05 +0000226 OpIT(NegateComparison(ccode), "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000227 OpRegCopy(rl_result.reg.GetReg(), rl_false.reg.GetReg());
228 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Vladimir Markoa1a70742014-03-03 10:28:05 +0000229 OpIT(ccode, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000230 OpRegCopy(rl_result.reg.GetReg(), rl_true.reg.GetReg());
buzbee252254b2013-09-08 16:20:53 -0700231 } else { // Normal - select between the two.
Vladimir Markoa1a70742014-03-03 10:28:05 +0000232 OpIT(ccode, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000233 OpRegCopy(rl_result.reg.GetReg(), rl_true.reg.GetReg());
234 OpRegCopy(rl_result.reg.GetReg(), rl_false.reg.GetReg());
buzbee252254b2013-09-08 16:20:53 -0700235 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700236 GenBarrier(); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 }
238 StoreValue(rl_dest, rl_result);
239}
240
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700241void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
243 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
244 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000245 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000247 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 ccode = FlipComparisonOrder(ccode);
249 }
250 if (rl_src2.is_const) {
251 RegLocation rl_temp = UpdateLocWide(rl_src2);
252 // Do special compare/branch against simple const operand if not already in registers.
253 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
254 if ((rl_temp.location != kLocPhysReg) &&
255 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
256 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
257 return;
258 }
259 }
buzbee0d829482013-10-11 15:24:55 -0700260 LIR* taken = &block_label_list_[bb->taken];
261 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
263 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000264 OpRegReg(kOpCmp, rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700265 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 case kCondEq:
267 OpCondBranch(kCondNe, not_taken);
268 break;
269 case kCondNe:
270 OpCondBranch(kCondNe, taken);
271 break;
272 case kCondLt:
273 OpCondBranch(kCondLt, taken);
274 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000275 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 break;
277 case kCondLe:
278 OpCondBranch(kCondLt, taken);
279 OpCondBranch(kCondGt, not_taken);
280 ccode = kCondLs;
281 break;
282 case kCondGt:
283 OpCondBranch(kCondGt, taken);
284 OpCondBranch(kCondLt, not_taken);
285 ccode = kCondHi;
286 break;
287 case kCondGe:
288 OpCondBranch(kCondGt, taken);
289 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000290 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 break;
292 default:
293 LOG(FATAL) << "Unexpected ccode: " << ccode;
294 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000295 OpRegReg(kOpCmp, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 OpCondBranch(ccode, taken);
297}
298
299/*
300 * Generate a register comparison to an immediate and branch. Caller
301 * is responsible for setting branch target field.
302 */
303LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, int reg, int check_value,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700304 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700307 /*
308 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
309 * compare-and-branch if zero is ideal if it will reach. However, because null checks
310 * branch forward to a launch pad, they will frequently not reach - and thus have to
311 * be converted to a long form during assembly (which will trigger another assembly
312 * pass). Here we estimate the branch distance for checks, and if large directly
313 * generate the long form in an attempt to avoid an extra assembly pass.
314 * TODO: consider interspersing launchpads in code following unconditional branches.
315 */
316 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
317 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
318 if (!skip && (ARM_LOWREG(reg)) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
320 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
321 reg, 0);
322 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000323 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 branch = NewLIR2(kThumbBCond, 0, arm_cond);
325 }
326 branch->target = target;
327 return branch;
328}
329
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330LIR* ArmMir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 LIR* res;
332 int opcode;
333 if (ARM_FPREG(r_dest) || ARM_FPREG(r_src))
334 return OpFpRegCopy(r_dest, r_src);
335 if (ARM_LOWREG(r_dest) && ARM_LOWREG(r_src))
336 opcode = kThumbMovRR;
337 else if (!ARM_LOWREG(r_dest) && !ARM_LOWREG(r_src))
338 opcode = kThumbMovRR_H2H;
339 else if (ARM_LOWREG(r_dest))
340 opcode = kThumbMovRR_H2L;
341 else
342 opcode = kThumbMovRR_L2H;
343 res = RawLIR(current_dalvik_offset_, opcode, r_dest, r_src);
344 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
345 res->flags.is_nop = true;
346 }
347 return res;
348}
349
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700350LIR* ArmMir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
352 AppendLIR(res);
353 return res;
354}
355
356void ArmMir2Lir::OpRegCopyWide(int dest_lo, int dest_hi, int src_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700357 int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 bool dest_fp = ARM_FPREG(dest_lo) && ARM_FPREG(dest_hi);
359 bool src_fp = ARM_FPREG(src_lo) && ARM_FPREG(src_hi);
360 DCHECK_EQ(ARM_FPREG(src_lo), ARM_FPREG(src_hi));
361 DCHECK_EQ(ARM_FPREG(dest_lo), ARM_FPREG(dest_hi));
362 if (dest_fp) {
363 if (src_fp) {
364 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
365 } else {
366 NewLIR3(kThumb2Fmdrr, S2d(dest_lo, dest_hi), src_lo, src_hi);
367 }
368 } else {
369 if (src_fp) {
370 NewLIR3(kThumb2Fmrrd, dest_lo, dest_hi, S2d(src_lo, src_hi));
371 } else {
372 // Handle overlap
373 if (src_hi == dest_lo) {
Vladimir Marko502c2a82014-02-06 11:52:07 +0000374 DCHECK_NE(src_lo, dest_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 OpRegCopy(dest_hi, src_hi);
376 OpRegCopy(dest_lo, src_lo);
377 } else {
378 OpRegCopy(dest_lo, src_lo);
379 OpRegCopy(dest_hi, src_hi);
380 }
381 }
382 }
383}
384
385// Table of magic divisors
386struct MagicTable {
387 uint32_t magic;
388 uint32_t shift;
389 DividePattern pattern;
390};
391
392static const MagicTable magic_table[] = {
393 {0, 0, DivideNone}, // 0
394 {0, 0, DivideNone}, // 1
395 {0, 0, DivideNone}, // 2
396 {0x55555556, 0, Divide3}, // 3
397 {0, 0, DivideNone}, // 4
398 {0x66666667, 1, Divide5}, // 5
399 {0x2AAAAAAB, 0, Divide3}, // 6
400 {0x92492493, 2, Divide7}, // 7
401 {0, 0, DivideNone}, // 8
402 {0x38E38E39, 1, Divide5}, // 9
403 {0x66666667, 2, Divide5}, // 10
404 {0x2E8BA2E9, 1, Divide5}, // 11
405 {0x2AAAAAAB, 1, Divide5}, // 12
406 {0x4EC4EC4F, 2, Divide5}, // 13
407 {0x92492493, 3, Divide7}, // 14
408 {0x88888889, 3, Divide7}, // 15
409};
410
411// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700412bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700413 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
415 return false;
416 }
417 DividePattern pattern = magic_table[lit].pattern;
418 if (pattern == DivideNone) {
419 return false;
420 }
421 // Tuning: add rem patterns
buzbee11b63d12013-08-27 07:34:17 -0700422 if (!is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 return false;
424 }
425
426 int r_magic = AllocTemp();
427 LoadConstant(r_magic, magic_table[lit].magic);
428 rl_src = LoadValue(rl_src, kCoreReg);
429 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
430 int r_hi = AllocTemp();
431 int r_lo = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000432 NewLIR4(kThumb2Smull, r_lo, r_hi, r_magic, rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700433 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 case Divide3:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000435 OpRegRegRegShift(kOpSub, rl_result.reg.GetReg(), r_hi,
436 rl_src.reg.GetReg(), EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 break;
438 case Divide5:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000439 OpRegRegImm(kOpAsr, r_lo, rl_src.reg.GetReg(), 31);
440 OpRegRegRegShift(kOpRsub, rl_result.reg.GetReg(), r_lo, r_hi,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 EncodeShift(kArmAsr, magic_table[lit].shift));
442 break;
443 case Divide7:
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000444 OpRegReg(kOpAdd, r_hi, rl_src.reg.GetReg());
445 OpRegRegImm(kOpAsr, r_lo, rl_src.reg.GetReg(), 31);
446 OpRegRegRegShift(kOpRsub, rl_result.reg.GetReg(), r_lo, r_hi,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 EncodeShift(kArmAsr, magic_table[lit].shift));
448 break;
449 default:
450 LOG(FATAL) << "Unexpected pattern: " << pattern;
451 }
452 StoreValue(rl_dest, rl_result);
453 return true;
454}
455
456LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700457 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
459 return NULL;
460}
461
Mark Mendell2bf31e62014-01-23 12:13:40 -0800462RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
463 RegLocation rl_src2, bool is_div, bool check_zero) {
464 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
465 return rl_dest;
466}
467
468RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
469 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
470 return rl_dest;
471}
472
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, int reg1, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700474 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700475 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
476
477 // Put the literal in a temp.
478 int lit_temp = AllocTemp();
479 LoadConstant(lit_temp, lit);
480 // Use the generic case for div/rem with arg2 in a register.
481 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
482 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
483 FreeTemp(lit_temp);
484
485 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486}
487
488RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, int reg1, int reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700489 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700490 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
491 if (is_div) {
492 // Simple case, use sdiv instruction.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000493 OpRegRegReg(kOpDiv, rl_result.reg.GetReg(), reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700494 } else {
495 // Remainder case, use the following code:
496 // temp = reg1 / reg2 - integer division
497 // temp = temp * reg2
498 // dest = reg1 - temp
499
500 int temp = AllocTemp();
501 OpRegRegReg(kOpDiv, temp, reg1, reg2);
502 OpRegReg(kOpMul, temp, reg2);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000503 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700504 FreeTemp(temp);
505 }
506
507 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508}
509
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700510bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 DCHECK_EQ(cu_->instruction_set, kThumb2);
512 RegLocation rl_src1 = info->args[0];
513 RegLocation rl_src2 = info->args[1];
514 rl_src1 = LoadValue(rl_src1, kCoreReg);
515 rl_src2 = LoadValue(rl_src2, kCoreReg);
516 RegLocation rl_dest = InlineTarget(info);
517 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000518 OpRegReg(kOpCmp, rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 OpIT((is_min) ? kCondGt : kCondLt, "E");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000520 OpRegReg(kOpMov, rl_result.reg.GetReg(), rl_src2.reg.GetReg());
521 OpRegReg(kOpMov, rl_result.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 GenBarrier();
523 StoreValue(rl_dest, rl_result);
524 return true;
525}
526
Vladimir Markoe508a202013-11-04 15:24:22 +0000527bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
528 RegLocation rl_src_address = info->args[0]; // long address
529 rl_src_address.wide = 0; // ignore high half in info->args[1]
530 RegLocation rl_dest = InlineTarget(info);
531 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
532 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
533 if (size == kLong) {
534 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000535 if (rl_address.reg.GetReg() != rl_result.reg.GetReg()) {
536 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), kWord, INVALID_SREG);
537 LoadBaseDisp(rl_address.reg.GetReg(), 4, rl_result.reg.GetHighReg(), kWord, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000538 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000539 LoadBaseDisp(rl_address.reg.GetReg(), 4, rl_result.reg.GetHighReg(), kWord, INVALID_SREG);
540 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), kWord, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000541 }
542 StoreValueWide(rl_dest, rl_result);
543 } else {
544 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
545 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000546 LoadBaseDisp(rl_address.reg.GetReg(), 0, rl_result.reg.GetReg(), size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000547 StoreValue(rl_dest, rl_result);
548 }
549 return true;
550}
551
552bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
553 RegLocation rl_src_address = info->args[0]; // long address
554 rl_src_address.wide = 0; // ignore high half in info->args[1]
555 RegLocation rl_src_value = info->args[2]; // [size] value
556 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
557 if (size == kLong) {
558 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
559 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000560 StoreBaseDisp(rl_address.reg.GetReg(), 0, rl_value.reg.GetReg(), kWord);
561 StoreBaseDisp(rl_address.reg.GetReg(), 4, rl_value.reg.GetHighReg(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000562 } else {
563 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
564 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
565 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000566 StoreBaseDisp(rl_address.reg.GetReg(), 0, rl_value.reg.GetReg(), size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000567 }
568 return true;
569}
570
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700571void ArmMir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 LOG(FATAL) << "Unexpected use of OpLea for Arm";
573}
574
Ian Rogers468532e2013-08-05 10:56:33 -0700575void ArmMir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
577}
578
Vladimir Marko1c282e22013-11-21 14:49:47 +0000579bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 DCHECK_EQ(cu_->instruction_set, kThumb2);
581 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000582 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
583 RegLocation rl_src_offset = info->args[2]; // long low
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 rl_src_offset.wide = 0; // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000585 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000586 // If is_long, high half is in info->args[5]
587 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
588 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 RegLocation rl_dest = InlineTarget(info); // boolean place for result
590
Vladimir Marko3e5af822013-11-21 15:01:20 +0000591 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
592 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
593 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
594 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
595 // into the same temps, reducing the number of required temps down to 5. We shall work
596 // around the potentially locked temp by using LR for r_ptr, unconditionally.
597 // TODO: Pass information about the need for more temps to the stack frame generation
598 // code so that we can rely on being able to allocate enough temps.
599 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
600 MarkTemp(rARM_LR);
601 FreeTemp(rARM_LR);
602 LockTemp(rARM_LR);
603 bool load_early = true;
604 if (is_long) {
605 bool expected_is_core_reg =
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000606 rl_src_expected.location == kLocPhysReg && !IsFpReg(rl_src_expected.reg.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000607 bool new_value_is_core_reg =
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000608 rl_src_new_value.location == kLocPhysReg && !IsFpReg(rl_src_new_value.reg.GetReg());
609 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(rl_src_expected.reg.GetReg());
610 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 +0000611
612 if (!expected_is_good_reg && !new_value_is_good_reg) {
613 // None of expected/new_value is non-temp reg, need to load both late
614 load_early = false;
615 // Make sure they are not in the temp regs and the load will not be skipped.
616 if (expected_is_core_reg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000617 FlushRegWide(rl_src_expected.reg.GetReg(), rl_src_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000618 ClobberSReg(rl_src_expected.s_reg_low);
619 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
620 rl_src_expected.location = kLocDalvikFrame;
621 }
622 if (new_value_is_core_reg) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000623 FlushRegWide(rl_src_new_value.reg.GetReg(), rl_src_new_value.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000624 ClobberSReg(rl_src_new_value.s_reg_low);
625 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
626 rl_src_new_value.location = kLocDalvikFrame;
627 }
628 }
629 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630
631 // Release store semantics, get the barrier out of the way. TODO: revisit
632 GenMemBarrier(kStoreLoad);
633
634 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000635 RegLocation rl_new_value;
636 if (!is_long) {
637 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
638 } else if (load_early) {
639 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
640 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641
Vladimir Marko1c282e22013-11-21 14:49:47 +0000642 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 // Mark card for object assuming new value is stored.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000644 MarkGCCard(rl_new_value.reg.GetReg(), rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646
647 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
648
Vladimir Marko3e5af822013-11-21 15:01:20 +0000649 int r_ptr = rARM_LR;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000650 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg.GetReg(), rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651
652 // Free now unneeded rl_object and rl_offset to give more temps.
653 ClobberSReg(rl_object.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000654 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000656 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657
Vladimir Marko3e5af822013-11-21 15:01:20 +0000658 RegLocation rl_expected;
659 if (!is_long) {
660 rl_expected = LoadValue(rl_src_expected, kCoreReg);
661 } else if (load_early) {
662 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
663 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000664 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
665 int low_reg = AllocTemp();
666 int high_reg = AllocTemp();
667 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
668 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000669 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670
Vladimir Marko3e5af822013-11-21 15:01:20 +0000671 // do {
672 // tmp = [r_ptr] - expected;
673 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
674 // result = tmp != 0;
675
676 int r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700677 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700678
Vladimir Marko3e5af822013-11-21 15:01:20 +0000679 if (is_long) {
680 int r_tmp_high = AllocTemp();
681 if (!load_early) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000682 LoadValueDirectWide(rl_src_expected, rl_expected.reg.GetReg(), rl_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000683 }
684 NewLIR3(kThumb2Ldrexd, r_tmp, r_tmp_high, r_ptr);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000685 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetReg());
686 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000687 if (!load_early) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000688 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg.GetReg(), rl_new_value.reg.GetHighReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000689 }
690 // Make sure we use ORR that sets the ccode
691 if (ARM_LOWREG(r_tmp) && ARM_LOWREG(r_tmp_high)) {
692 NewLIR2(kThumbOrr, r_tmp, r_tmp_high);
693 } else {
694 NewLIR4(kThumb2OrrRRRs, r_tmp, r_tmp, r_tmp_high, 0);
695 }
696 FreeTemp(r_tmp_high); // Now unneeded
697
698 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
699 OpIT(kCondEq, "T");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000700 NewLIR4(kThumb2Strexd /* eq */, r_tmp, rl_new_value.reg.GetReg(), rl_new_value.reg.GetHighReg(), r_ptr);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000701
702 } else {
703 NewLIR3(kThumb2Ldrex, r_tmp, r_ptr, 0);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000704 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000705 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
706 OpIT(kCondEq, "T");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000707 NewLIR4(kThumb2Strex /* eq */, r_tmp, rl_new_value.reg.GetReg(), r_ptr, 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000708 }
709
710 // Still one conditional left from OpIT(kCondEq, "T") from either branch
711 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700712 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713
Vladimir Marko3e5af822013-11-21 15:01:20 +0000714 if (!load_early) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000715 FreeTemp(rl_expected.reg.GetReg()); // Now unneeded.
716 FreeTemp(rl_expected.reg.GetHighReg()); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000717 }
718
719 // result := (tmp1 != 0) ? 0 : 1;
720 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000721 OpRegRegImm(kOpRsub, rl_result.reg.GetReg(), r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000722 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000723 OpIT(kCondUlt, "");
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000724 LoadConstant(rl_result.reg.GetReg(), 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000725 FreeTemp(r_tmp); // Now unneeded.
726
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 StoreValue(rl_dest, rl_result);
728
Vladimir Marko3e5af822013-11-21 15:01:20 +0000729 // Now, restore lr to its non-temp status.
730 Clobber(rARM_LR);
731 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 return true;
733}
734
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700735LIR* ArmMir2Lir::OpPcRelLoad(int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg, 0, 0, 0, 0, target);
737}
738
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700739LIR* ArmMir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 return NewLIR3(kThumb2Vldms, rBase, fr0, count);
741}
742
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700743LIR* ArmMir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 return NewLIR3(kThumb2Vstms, rBase, fr0, count);
745}
746
747void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
748 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700749 int first_bit, int second_bit) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000750 OpRegRegRegShift(kOpAdd, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 EncodeShift(kArmLsl, second_bit - first_bit));
752 if (first_bit != 0) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000753 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_result.reg.GetReg(), first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 }
755}
756
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700757void ArmMir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 int t_reg = AllocTemp();
759 NewLIR4(kThumb2OrrRRRs, t_reg, reg_lo, reg_hi, 0);
760 FreeTemp(t_reg);
761 GenCheck(kCondEq, kThrowDivZero);
762}
763
764// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700765LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700766 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
767 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
768}
769
770// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700771LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700772 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000773 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
774 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 return OpCondBranch(c_code, target);
776}
777
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700778void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779#if ANDROID_SMP != 0
780 int dmb_flavor;
781 // TODO: revisit Arm barrier kinds
782 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800783 case kLoadStore: dmb_flavor = kISH; break;
784 case kLoadLoad: dmb_flavor = kISH; break;
785 case kStoreStore: dmb_flavor = kISHST; break;
786 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 default:
788 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
789 dmb_flavor = kSY; // quiet gcc.
790 break;
791 }
792 LIR* dmb = NewLIR1(kThumb2Dmb, dmb_flavor);
buzbeeb48819d2013-09-14 16:15:25 -0700793 dmb->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794#endif
795}
796
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700797void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 rl_src = LoadValueWide(rl_src, kCoreReg);
799 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
800 int z_reg = AllocTemp();
801 LoadConstantNoClobber(z_reg, 0);
802 // Check for destructive overlap
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000803 if (rl_result.reg.GetReg() == rl_src.reg.GetHighReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000805 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
806 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 FreeTemp(t_reg);
808 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000809 OpRegRegReg(kOpSub, rl_result.reg.GetReg(), z_reg, rl_src.reg.GetReg());
810 OpRegRegReg(kOpSbc, rl_result.reg.GetHighReg(), z_reg, rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 }
812 FreeTemp(z_reg);
813 StoreValueWide(rl_dest, rl_result);
814}
815
Mark Mendelle02d48f2014-01-15 11:19:23 -0800816void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
817 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 /*
819 * To pull off inline multiply, we have a worst-case requirement of 8 temporary
820 * registers. Normally for Arm, we get 5. We can get to 6 by including
821 * lr in the temp set. The only problematic case is all operands and result are
822 * distinct, and none have been promoted. In that case, we can succeed by aggressively
823 * freeing operand temp registers after they are no longer needed. All other cases
824 * can proceed normally. We'll just punt on the case of the result having a misaligned
825 * overlap with either operand and send that case to a runtime handler.
826 */
827 RegLocation rl_result;
828 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogers468532e2013-08-05 10:56:33 -0700829 ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700830 FlushAllRegs();
831 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
832 rl_result = GetReturnWide(false);
833 StoreValueWide(rl_dest, rl_result);
834 return;
835 }
836 // Temporarily add LR to the temp pool, and assign it to tmp1
837 MarkTemp(rARM_LR);
838 FreeTemp(rARM_LR);
839 int tmp1 = rARM_LR;
840 LockTemp(rARM_LR);
841
842 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
843 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
844
845 bool special_case = true;
846 // If operands are the same, or any pair has been promoted we're not the special case.
847 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) ||
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000848 (!IsTemp(rl_src1.reg.GetReg()) && !IsTemp(rl_src1.reg.GetHighReg())) ||
849 (!IsTemp(rl_src2.reg.GetReg()) && !IsTemp(rl_src2.reg.GetHighReg()))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 special_case = false;
851 }
852 // Tuning: if rl_dest has been promoted and is *not* either operand, could use directly.
853 int res_lo = AllocTemp();
854 int res_hi;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000855 if (rl_src1.reg.GetReg() == rl_src2.reg.GetReg()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 res_hi = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000857 NewLIR3(kThumb2MulRRR, tmp1, rl_src1.reg.GetReg(), rl_src1.reg.GetHighReg());
858 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src1.reg.GetReg(), rl_src1.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
860 } else {
861 // In the special case, all temps are now allocated
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000862 NewLIR3(kThumb2MulRRR, tmp1, rl_src2.reg.GetReg(), rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 if (special_case) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000864 DCHECK_NE(rl_src1.reg.GetReg(), rl_src2.reg.GetReg());
865 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
866 FreeTemp(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 }
868 res_hi = AllocTemp();
869
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000870 NewLIR4(kThumb2Umull, res_lo, res_hi, rl_src2.reg.GetReg(), rl_src1.reg.GetReg());
871 NewLIR4(kThumb2Mla, tmp1, rl_src1.reg.GetReg(), rl_src2.reg.GetHighReg(), tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872 NewLIR4(kThumb2AddRRR, res_hi, tmp1, res_hi, 0);
873 if (special_case) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000874 FreeTemp(rl_src1.reg.GetReg());
875 Clobber(rl_src1.reg.GetReg());
876 Clobber(rl_src1.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 }
878 }
879 FreeTemp(tmp1);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700880 rl_result = GetReturnWide(false); // Just using as a template.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000881 rl_result.reg.SetReg(res_lo);
882 rl_result.reg.SetHighReg(res_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 StoreValueWide(rl_dest, rl_result);
884 // Now, restore lr to its non-temp status.
885 Clobber(rARM_LR);
886 UnmarkTemp(rARM_LR);
887}
888
Mark Mendelle02d48f2014-01-15 11:19:23 -0800889void ArmMir2Lir::GenAddLong(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 GenAddLong for Arm";
892}
893
Mark Mendelle02d48f2014-01-15 11:19:23 -0800894void ArmMir2Lir::GenSubLong(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 GenSubLong for Arm";
897}
898
Mark Mendelle02d48f2014-01-15 11:19:23 -0800899void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700900 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
902}
903
Mark Mendelle02d48f2014-01-15 11:19:23 -0800904void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700905 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
907}
908
Mark Mendelle02d48f2014-01-15 11:19:23 -0800909void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700910 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
912}
913
914/*
915 * Generate array load
916 */
917void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700918 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 RegisterClass reg_class = oat_reg_class_by_size(size);
920 int len_offset = mirror::Array::LengthOffset().Int32Value();
921 int data_offset;
922 RegLocation rl_result;
923 bool constant_index = rl_index.is_const;
924 rl_array = LoadValue(rl_array, kCoreReg);
925 if (!constant_index) {
926 rl_index = LoadValue(rl_index, kCoreReg);
927 }
928
929 if (rl_dest.wide) {
930 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
931 } else {
932 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
933 }
934
935 // If index is constant, just fold it into the data offset
936 if (constant_index) {
937 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
938 }
939
940 /* null object? */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000941 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942
943 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
944 int reg_len = INVALID_REG;
945 if (needs_range_check) {
946 reg_len = AllocTemp();
947 /* Get len */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000948 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 }
950 if (rl_dest.wide || rl_dest.fp || constant_index) {
951 int reg_ptr;
952 if (constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000953 reg_ptr = rl_array.reg.GetReg(); // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 } else {
955 // No special indexed operation, lea + load w/ displacement
956 reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000957 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000959 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 }
961 rl_result = EvalLoc(rl_dest, reg_class, true);
962
963 if (needs_range_check) {
964 if (constant_index) {
965 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
966 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000967 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 }
969 FreeTemp(reg_len);
970 }
971 if (rl_dest.wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000972 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg.GetReg(), rl_result.reg.GetHighReg(), INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 if (!constant_index) {
974 FreeTemp(reg_ptr);
975 }
976 StoreValueWide(rl_dest, rl_result);
977 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000978 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg.GetReg(), size, INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 if (!constant_index) {
980 FreeTemp(reg_ptr);
981 }
982 StoreValue(rl_dest, rl_result);
983 }
984 } else {
985 // Offset base, then use indexed load
986 int reg_ptr = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000987 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
988 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 rl_result = EvalLoc(rl_dest, reg_class, true);
990
991 if (needs_range_check) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000992 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 FreeTemp(reg_len);
994 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000995 LoadBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_result.reg.GetReg(), scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 FreeTemp(reg_ptr);
997 StoreValue(rl_dest, rl_result);
998 }
999}
1000
1001/*
1002 * Generate array store
1003 *
1004 */
1005void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001006 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 RegisterClass reg_class = oat_reg_class_by_size(size);
1008 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 bool constant_index = rl_index.is_const;
1010
Ian Rogersa9a82542013-10-04 11:17:26 -07001011 int data_offset;
1012 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1014 } else {
1015 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1016 }
1017
1018 // If index is constant, just fold it into the data offset.
1019 if (constant_index) {
1020 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1021 }
1022
1023 rl_array = LoadValue(rl_array, kCoreReg);
1024 if (!constant_index) {
1025 rl_index = LoadValue(rl_index, kCoreReg);
1026 }
1027
1028 int reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001029 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 if (constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001031 reg_ptr = rl_array.reg.GetReg();
1032 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1033 Clobber(rl_array.reg.GetReg());
1034 reg_ptr = rl_array.reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001036 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001037 reg_ptr = AllocTemp();
1038 }
1039
1040 /* null object? */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001041 GenNullCheck(rl_array.s_reg_low, rl_array.reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042
1043 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1044 int reg_len = INVALID_REG;
1045 if (needs_range_check) {
1046 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001047 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 /* Get len */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001049 LoadWordDisp(rl_array.reg.GetReg(), len_offset, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 }
1051 /* at this point, reg_ptr points to array, 2 live temps */
1052 if (rl_src.wide || rl_src.fp || constant_index) {
1053 if (rl_src.wide) {
1054 rl_src = LoadValueWide(rl_src, reg_class);
1055 } else {
1056 rl_src = LoadValue(rl_src, reg_class);
1057 }
1058 if (!constant_index) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001059 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg.GetReg(), rl_index.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 EncodeShift(kArmLsl, scale));
1061 }
1062 if (needs_range_check) {
1063 if (constant_index) {
1064 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1065 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001066 GenRegRegCheck(kCondLs, reg_len, rl_index.reg.GetReg(), kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 }
1068 FreeTemp(reg_len);
1069 }
1070
1071 if (rl_src.wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001072 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg.GetReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001074 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg.GetReg(), size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
1076 } else {
1077 /* reg_ptr -> array data */
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001078 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg.GetReg(), data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 rl_src = LoadValue(rl_src, reg_class);
1080 if (needs_range_check) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001081 GenRegRegCheck(kCondUge, rl_index.reg.GetReg(), reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 FreeTemp(reg_len);
1083 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001084 StoreBaseIndexed(reg_ptr, rl_index.reg.GetReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 scale, size);
1086 }
Ian Rogers773aab12013-10-14 13:50:10 -07001087 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 FreeTemp(reg_ptr);
1089 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001090 if (card_mark) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001091 MarkGCCard(rl_src.reg.GetReg(), rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092 }
1093}
1094
Ian Rogersa9a82542013-10-04 11:17:26 -07001095
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001097 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 rl_src = LoadValueWide(rl_src, kCoreReg);
1099 // Per spec, we only care about low 6 bits of shift amount.
1100 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1101 if (shift_amount == 0) {
1102 StoreValueWide(rl_dest, rl_src);
1103 return;
1104 }
1105 if (BadOverlap(rl_src, rl_dest)) {
1106 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1107 return;
1108 }
1109 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001110 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 case Instruction::SHL_LONG:
1112 case Instruction::SHL_LONG_2ADDR:
1113 if (shift_amount == 1) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001114 OpRegRegReg(kOpAdd, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg());
1115 OpRegRegReg(kOpAdc, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), rl_src.reg.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 } else if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001117 OpRegCopy(rl_result.reg.GetHighReg(), rl_src.reg.GetReg());
1118 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001120 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetReg(), shift_amount - 32);
1121 LoadConstant(rl_result.reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001123 OpRegRegImm(kOpLsl, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
1124 OpRegRegRegShift(kOpOr, rl_result.reg.GetHighReg(), rl_result.reg.GetHighReg(), rl_src.reg.GetReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 EncodeShift(kArmLsr, 32 - shift_amount));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001126 OpRegRegImm(kOpLsl, rl_result.reg.GetReg(), rl_src.reg.GetReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 }
1128 break;
1129 case Instruction::SHR_LONG:
1130 case Instruction::SHR_LONG_2ADDR:
1131 if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001132 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1133 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001135 OpRegRegImm(kOpAsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1136 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 } else {
1138 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001139 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1140 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001141 EncodeShift(kArmLsl, 32 - shift_amount));
1142 FreeTemp(t_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001143 OpRegRegImm(kOpAsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 }
1145 break;
1146 case Instruction::USHR_LONG:
1147 case Instruction::USHR_LONG_2ADDR:
1148 if (shift_amount == 32) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001149 OpRegCopy(rl_result.reg.GetReg(), rl_src.reg.GetHighReg());
1150 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 } else if (shift_amount > 31) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001152 OpRegRegImm(kOpLsr, rl_result.reg.GetReg(), rl_src.reg.GetHighReg(), shift_amount - 32);
1153 LoadConstant(rl_result.reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 } else {
1155 int t_reg = AllocTemp();
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001156 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetReg(), shift_amount);
1157 OpRegRegRegShift(kOpOr, rl_result.reg.GetReg(), t_reg, rl_src.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 EncodeShift(kArmLsl, 32 - shift_amount));
1159 FreeTemp(t_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001160 OpRegRegImm(kOpLsr, rl_result.reg.GetHighReg(), rl_src.reg.GetHighReg(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 }
1162 break;
1163 default:
1164 LOG(FATAL) << "Unexpected case";
1165 }
1166 StoreValueWide(rl_dest, rl_result);
1167}
1168
1169void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001170 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1172 if (!rl_src2.is_const) {
1173 // Don't bother with special handling for subtract from immediate.
1174 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1175 return;
1176 }
1177 } else {
1178 // Normalize
1179 if (!rl_src2.is_const) {
1180 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001181 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 }
1183 }
1184 if (BadOverlap(rl_src1, rl_dest)) {
1185 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1186 return;
1187 }
1188 DCHECK(rl_src2.is_const);
1189 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1190 uint32_t val_lo = Low32Bits(val);
1191 uint32_t val_hi = High32Bits(val);
1192 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1193 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1194
1195 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001196 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 case Instruction::ADD_LONG:
1198 case Instruction::ADD_LONG_2ADDR:
1199 case Instruction::SUB_LONG:
1200 case Instruction::SUB_LONG_2ADDR:
1201 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1202 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1203 return;
1204 }
1205 break;
1206 default:
1207 break;
1208 }
1209 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1210 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1211 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1212 switch (opcode) {
1213 case Instruction::ADD_LONG:
1214 case Instruction::ADD_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001215 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1216 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 break;
1218 case Instruction::OR_LONG:
1219 case Instruction::OR_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001220 if ((val_lo != 0) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1221 OpRegRegImm(kOpOr, 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 != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1224 OpRegRegImm(kOpOr, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 }
1226 break;
1227 case Instruction::XOR_LONG:
1228 case Instruction::XOR_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001229 OpRegRegImm(kOpXor, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
1230 OpRegRegImm(kOpXor, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 break;
1232 case Instruction::AND_LONG:
1233 case Instruction::AND_LONG_2ADDR:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001234 if ((val_lo != 0xffffffff) || (rl_result.reg.GetReg() != rl_src1.reg.GetReg())) {
1235 OpRegRegImm(kOpAnd, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001237 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
1238 OpRegRegImm(kOpAnd, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240 break;
1241 case Instruction::SUB_LONG_2ADDR:
1242 case Instruction::SUB_LONG:
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001243 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetReg(), rl_src1.reg.GetReg(), mod_imm_lo);
1244 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001245 break;
1246 default:
1247 LOG(FATAL) << "Unexpected opcode " << opcode;
1248 }
1249 StoreValueWide(rl_dest, rl_result);
1250}
1251
1252} // namespace art