blob: c80297bc01bb59a9d5a0426337c5a0040c835f44 [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
buzbee2700f7e2014-03-07 09:46:20 -080027LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage 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
Dave Allison3da67a52014-04-02 17:03:45 -070070void ArmMir2Lir::OpEndIT(LIR* it) {
71 // TODO: use the 'it' pointer to do some checks with the LIR, for example
72 // we could check that the number of instructions matches the mask
73 // in the IT instruction.
74 CHECK(it != nullptr);
75 GenBarrier();
76}
77
Brian Carlstrom7940e442013-07-12 13:46:57 -070078/*
79 * 64-bit 3way compare function.
80 * mov rX, #-1
81 * cmp op1hi, op2hi
82 * blt done
83 * bgt flip
84 * sub rX, op1lo, op2lo (treat as unsigned)
85 * beq done
86 * ite hi
87 * mov(hi) rX, #-1
88 * mov(!hi) rX, #1
89 * flip:
90 * neg rX
91 * done:
92 */
93void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070094 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 LIR* target1;
96 LIR* target2;
97 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
98 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080099 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800101 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 LIR* branch1 = OpCondBranch(kCondLt, NULL);
103 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbee2700f7e2014-03-07 09:46:20 -0800104 OpRegRegReg(kOpSub, t_reg, rl_src1.reg, rl_src2.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 LIR* branch3 = OpCondBranch(kCondEq, NULL);
106
Dave Allison3da67a52014-04-02 17:03:45 -0700107 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800108 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700110 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
112 target2 = NewLIR0(kPseudoTargetLabel);
113 OpRegReg(kOpNeg, t_reg, t_reg);
114
115 target1 = NewLIR0(kPseudoTargetLabel);
116
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700117 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800118 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 StoreValue(rl_dest, rl_temp);
120 FreeTemp(t_reg);
121
122 branch1->target = target1;
123 branch2->target = target2;
124 branch3->target = branch1->target;
125}
126
127void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 int32_t val_lo = Low32Bits(val);
130 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700131 DCHECK_GE(ModifiedImmediate(val_lo), 0);
132 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700133 LIR* taken = &block_label_list_[bb->taken];
134 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800136 RegStorage low_reg = rl_src1.reg.GetLow();
137 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138
Vladimir Marko58af1f92013-12-19 13:31:15 +0000139 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800140 RegStorage t_reg = AllocTemp();
141 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000142 FreeTemp(t_reg);
143 OpCondBranch(ccode, taken);
144 return;
145 }
146
Brian Carlstromdf629502013-07-17 22:39:56 -0700147 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 case kCondEq:
149 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000150 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 break;
152 case kCondLt:
153 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
154 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000155 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 break;
157 case kCondLe:
158 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
159 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
160 ccode = kCondLs;
161 break;
162 case kCondGt:
163 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
164 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
165 ccode = kCondHi;
166 break;
167 case kCondGe:
168 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
169 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000170 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 break;
172 default:
173 LOG(FATAL) << "Unexpected ccode: " << ccode;
174 }
175 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
176}
177
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegLocation rl_result;
180 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 RegLocation rl_dest = mir_graph_->GetDest(mir);
182 rl_src = LoadValue(rl_src, kCoreReg);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000183 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 if (mir->ssa_rep->num_uses == 1) {
185 // CONST case
186 int true_val = mir->dalvikInsn.vB;
187 int false_val = mir->dalvikInsn.vC;
188 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000189 // Change kCondNe to kCondEq for the special cases below.
190 if (ccode == kCondNe) {
191 ccode = kCondEq;
192 std::swap(true_val, false_val);
193 }
194 bool cheap_false_val = InexpensiveConstantInt(false_val);
195 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800196 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000197 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700198 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800199 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700200 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000201 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800202 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000203 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700204 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800205 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700206 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000207 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800208 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700209 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800210 LoadConstant(rl_result.reg, true_val);
211 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700212 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 } else {
214 // Unlikely case - could be tuned.
buzbee2700f7e2014-03-07 09:46:20 -0800215 RegStorage t_reg1 = AllocTemp();
216 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 LoadConstant(t_reg1, true_val);
218 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800219 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700220 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800221 OpRegCopy(rl_result.reg, t_reg1);
222 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700223 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 }
225 } else {
226 // MOVE case
227 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
228 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
229 rl_true = LoadValue(rl_true, kCoreReg);
230 rl_false = LoadValue(rl_false, kCoreReg);
231 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800232 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700233 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000234 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700235 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800236 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000237 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700238 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800239 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700240 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700241 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800242 OpRegCopy(rl_result.reg, rl_true.reg);
243 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700244 }
Dave Allison3da67a52014-04-02 17:03:45 -0700245 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247 StoreValue(rl_dest, rl_result);
248}
249
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
252 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
253 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000254 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000256 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 ccode = FlipComparisonOrder(ccode);
258 }
259 if (rl_src2.is_const) {
260 RegLocation rl_temp = UpdateLocWide(rl_src2);
261 // Do special compare/branch against simple const operand if not already in registers.
262 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
263 if ((rl_temp.location != kLocPhysReg) &&
264 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
265 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
266 return;
267 }
268 }
buzbee0d829482013-10-11 15:24:55 -0700269 LIR* taken = &block_label_list_[bb->taken];
270 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
272 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800273 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700274 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 case kCondEq:
276 OpCondBranch(kCondNe, not_taken);
277 break;
278 case kCondNe:
279 OpCondBranch(kCondNe, taken);
280 break;
281 case kCondLt:
282 OpCondBranch(kCondLt, taken);
283 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000284 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 break;
286 case kCondLe:
287 OpCondBranch(kCondLt, taken);
288 OpCondBranch(kCondGt, not_taken);
289 ccode = kCondLs;
290 break;
291 case kCondGt:
292 OpCondBranch(kCondGt, taken);
293 OpCondBranch(kCondLt, not_taken);
294 ccode = kCondHi;
295 break;
296 case kCondGe:
297 OpCondBranch(kCondGt, taken);
298 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000299 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 break;
301 default:
302 LOG(FATAL) << "Unexpected ccode: " << ccode;
303 }
buzbee2700f7e2014-03-07 09:46:20 -0800304 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 OpCondBranch(ccode, taken);
306}
307
308/*
309 * Generate a register comparison to an immediate and branch. Caller
310 * is responsible for setting branch target field.
311 */
buzbee2700f7e2014-03-07 09:46:20 -0800312LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700315 /*
316 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
317 * compare-and-branch if zero is ideal if it will reach. However, because null checks
318 * branch forward to a launch pad, they will frequently not reach - and thus have to
319 * be converted to a long form during assembly (which will trigger another assembly
320 * pass). Here we estimate the branch distance for checks, and if large directly
321 * generate the long form in an attempt to avoid an extra assembly pass.
322 * TODO: consider interspersing launchpads in code following unconditional branches.
323 */
324 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
325 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
buzbee2700f7e2014-03-07 09:46:20 -0800326 if (!skip && (ARM_LOWREG(reg.GetReg())) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
328 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
buzbee2700f7e2014-03-07 09:46:20 -0800329 reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000331 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 branch = NewLIR2(kThumbBCond, 0, arm_cond);
333 }
334 branch->target = target;
335 return branch;
336}
337
buzbee2700f7e2014-03-07 09:46:20 -0800338LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 LIR* res;
340 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800341 // If src or dest is a pair, we'll be using low reg.
342 if (r_dest.IsPair()) {
343 r_dest = r_dest.GetLow();
344 }
345 if (r_src.IsPair()) {
346 r_src = r_src.GetLow();
347 }
348 if (ARM_FPREG(r_dest.GetReg()) || ARM_FPREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 return OpFpRegCopy(r_dest, r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800350 if (ARM_LOWREG(r_dest.GetReg()) && ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 opcode = kThumbMovRR;
buzbee2700f7e2014-03-07 09:46:20 -0800352 else if (!ARM_LOWREG(r_dest.GetReg()) && !ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 opcode = kThumbMovRR_H2H;
buzbee2700f7e2014-03-07 09:46:20 -0800354 else if (ARM_LOWREG(r_dest.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 opcode = kThumbMovRR_H2L;
356 else
357 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800358 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
360 res->flags.is_nop = true;
361 }
362 return res;
363}
364
buzbee2700f7e2014-03-07 09:46:20 -0800365LIR* ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
367 AppendLIR(res);
368 return res;
369}
370
buzbee2700f7e2014-03-07 09:46:20 -0800371void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
372 bool dest_fp = ARM_FPREG(r_dest.GetLowReg());
373 bool src_fp = ARM_FPREG(r_src.GetLowReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 if (dest_fp) {
375 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800376 // FIXME: handle 64-bit solo's here.
377 OpRegCopy(RegStorage::Solo64(S2d(r_dest.GetLowReg(), r_dest.GetHighReg())),
378 RegStorage::Solo64(S2d(r_src.GetLowReg(), r_src.GetHighReg())));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800380 NewLIR3(kThumb2Fmdrr, S2d(r_dest.GetLowReg(), r_dest.GetHighReg()),
381 r_src.GetLowReg(), r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383 } else {
384 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800385 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), S2d(r_src.GetLowReg(),
386 r_src.GetHighReg()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 } else {
388 // Handle overlap
buzbee2700f7e2014-03-07 09:46:20 -0800389 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
390 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
391 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
392 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800394 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
395 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
397 }
398 }
399}
400
401// Table of magic divisors
402struct MagicTable {
403 uint32_t magic;
404 uint32_t shift;
405 DividePattern pattern;
406};
407
408static const MagicTable magic_table[] = {
409 {0, 0, DivideNone}, // 0
410 {0, 0, DivideNone}, // 1
411 {0, 0, DivideNone}, // 2
412 {0x55555556, 0, Divide3}, // 3
413 {0, 0, DivideNone}, // 4
414 {0x66666667, 1, Divide5}, // 5
415 {0x2AAAAAAB, 0, Divide3}, // 6
416 {0x92492493, 2, Divide7}, // 7
417 {0, 0, DivideNone}, // 8
418 {0x38E38E39, 1, Divide5}, // 9
419 {0x66666667, 2, Divide5}, // 10
420 {0x2E8BA2E9, 1, Divide5}, // 11
421 {0x2AAAAAAB, 1, Divide5}, // 12
422 {0x4EC4EC4F, 2, Divide5}, // 13
423 {0x92492493, 3, Divide7}, // 14
424 {0x88888889, 3, Divide7}, // 15
425};
426
427// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700428bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700429 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
431 return false;
432 }
433 DividePattern pattern = magic_table[lit].pattern;
434 if (pattern == DivideNone) {
435 return false;
436 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437
buzbee2700f7e2014-03-07 09:46:20 -0800438 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 LoadConstant(r_magic, magic_table[lit].magic);
440 rl_src = LoadValue(rl_src, kCoreReg);
441 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800442 RegStorage r_hi = AllocTemp();
443 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100444
445 // rl_dest and rl_src might overlap.
446 // Reuse r_hi to save the div result for reminder case.
447 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
448
buzbee2700f7e2014-03-07 09:46:20 -0800449 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700450 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100452 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 break;
454 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800455 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100456 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700457 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 break;
459 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800460 OpRegReg(kOpAdd, r_hi, rl_src.reg);
461 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100462 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700463 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 break;
465 default:
466 LOG(FATAL) << "Unexpected pattern: " << pattern;
467 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100468
469 if (!is_div) {
470 // div_result = src / lit
471 // tmp1 = div_result * lit
472 // dest = src - tmp1
473 RegStorage tmp1 = r_lo;
474 EasyMultiplyOp ops[2];
475
476 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
477 DCHECK_NE(canEasyMultiply, false);
478
479 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
480 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
481 }
482
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 StoreValue(rl_dest, rl_result);
484 return true;
485}
486
Ian Rogerse2143c02014-03-28 08:47:16 -0700487// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
488bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
489 if (IsPowerOfTwo(lit)) {
490 op->op = kOpLsl;
491 op->shift = LowestSetBit(lit);
492 return true;
493 }
494
495 if (IsPowerOfTwo(lit - 1)) {
496 op->op = kOpAdd;
497 op->shift = LowestSetBit(lit - 1);
498 return true;
499 }
500
501 if (IsPowerOfTwo(lit + 1)) {
502 op->op = kOpRsub;
503 op->shift = LowestSetBit(lit + 1);
504 return true;
505 }
506
507 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100508 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700509 return false;
510}
511
512// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
513bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
514 GetEasyMultiplyOp(lit, &ops[0]);
515 if (GetEasyMultiplyOp(lit, &ops[0])) {
516 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100517 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700518 return true;
519 }
520
521 int lit1 = lit;
522 uint32_t shift = LowestSetBit(lit1);
523 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
524 ops[1].op = kOpLsl;
525 ops[1].shift = shift;
526 return true;
527 }
528
529 lit1 = lit - 1;
530 shift = LowestSetBit(lit1);
531 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
532 ops[1].op = kOpAdd;
533 ops[1].shift = shift;
534 return true;
535 }
536
537 lit1 = lit + 1;
538 shift = LowestSetBit(lit1);
539 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
540 ops[1].op = kOpRsub;
541 ops[1].shift = shift;
542 return true;
543 }
544
545 return false;
546}
547
Zheng Xuf9719f92014-04-02 13:31:31 +0100548// Generate instructions to do multiply.
549// Additional temporary register is required,
550// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700551void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100552 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
553 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
554
555 RegStorage r_tmp1;
556 if (ops[1].op == kOpInvalid) {
557 r_tmp1 = r_dest;
558 } else if (r_dest.GetReg() != r_src.GetReg()) {
559 r_tmp1 = r_dest;
560 } else {
561 r_tmp1 = AllocTemp();
562 }
563
564 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700565 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100566 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700567 break;
568 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100569 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700570 break;
571 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100572 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700573 break;
574 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100575 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700576 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100577 }
578
579 switch (ops[1].op) {
580 case kOpInvalid:
581 return;
582 case kOpLsl:
583 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
584 break;
585 case kOpAdd:
586 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
587 break;
588 case kOpRsub:
589 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
590 break;
591 default:
592 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
593 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700594 }
595}
596
597bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
598 EasyMultiplyOp ops[2];
599
600 if (!GetEasyMultiplyTwoOps(lit, ops)) {
601 return false;
602 }
603
604 rl_src = LoadValue(rl_src, kCoreReg);
605 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
606
607 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
608 StoreValue(rl_dest, rl_result);
609 return true;
610}
611
buzbee2700f7e2014-03-07 09:46:20 -0800612LIR* ArmMir2Lir::GenRegMemCheck(ConditionCode c_code, RegStorage reg1, RegStorage base,
613 int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 LOG(FATAL) << "Unexpected use of GenRegMemCheck for Arm";
615 return NULL;
616}
617
Mark Mendell2bf31e62014-01-23 12:13:40 -0800618RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
619 RegLocation rl_src2, bool is_div, bool check_zero) {
620 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
621 return rl_dest;
622}
623
624RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
625 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
626 return rl_dest;
627}
628
buzbee2700f7e2014-03-07 09:46:20 -0800629RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700630 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
631
632 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800633 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700634 LoadConstant(lit_temp, lit);
635 // Use the generic case for div/rem with arg2 in a register.
636 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
637 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
638 FreeTemp(lit_temp);
639
640 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641}
642
buzbee2700f7e2014-03-07 09:46:20 -0800643RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700644 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700645 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
646 if (is_div) {
647 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800648 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700649 } else {
650 // Remainder case, use the following code:
651 // temp = reg1 / reg2 - integer division
652 // temp = temp * reg2
653 // dest = reg1 - temp
654
buzbee2700f7e2014-03-07 09:46:20 -0800655 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700656 OpRegRegReg(kOpDiv, temp, reg1, reg2);
657 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800658 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700659 FreeTemp(temp);
660 }
661
662 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663}
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 DCHECK_EQ(cu_->instruction_set, kThumb2);
667 RegLocation rl_src1 = info->args[0];
668 RegLocation rl_src2 = info->args[1];
669 rl_src1 = LoadValue(rl_src1, kCoreReg);
670 rl_src2 = LoadValue(rl_src2, kCoreReg);
671 RegLocation rl_dest = InlineTarget(info);
672 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800673 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700674 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800675 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
676 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700677 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 StoreValue(rl_dest, rl_result);
679 return true;
680}
681
Vladimir Markoe508a202013-11-04 15:24:22 +0000682bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
683 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800684 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000685 RegLocation rl_dest = InlineTarget(info);
686 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
687 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
688 if (size == kLong) {
689 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800690 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
691 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
692 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000693 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800694 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
695 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000696 }
697 StoreValueWide(rl_dest, rl_result);
698 } else {
699 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
700 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800701 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000702 StoreValue(rl_dest, rl_result);
703 }
704 return true;
705}
706
707bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
708 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800709 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000710 RegLocation rl_src_value = info->args[2]; // [size] value
711 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
712 if (size == kLong) {
713 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
714 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800715 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), kWord);
716 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000717 } else {
718 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
719 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
720 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800721 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000722 }
723 return true;
724}
725
buzbee2700f7e2014-03-07 09:46:20 -0800726void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 LOG(FATAL) << "Unexpected use of OpLea for Arm";
728}
729
Ian Rogersdd7624d2014-03-14 17:43:00 -0700730void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
732}
733
Vladimir Marko1c282e22013-11-21 14:49:47 +0000734bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735 DCHECK_EQ(cu_->instruction_set, kThumb2);
736 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000737 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
738 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800739 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000740 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000741 // If is_long, high half is in info->args[5]
742 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
743 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 RegLocation rl_dest = InlineTarget(info); // boolean place for result
745
Vladimir Marko3e5af822013-11-21 15:01:20 +0000746 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
747 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
748 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
749 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
750 // into the same temps, reducing the number of required temps down to 5. We shall work
751 // around the potentially locked temp by using LR for r_ptr, unconditionally.
752 // TODO: Pass information about the need for more temps to the stack frame generation
753 // code so that we can rely on being able to allocate enough temps.
754 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
755 MarkTemp(rARM_LR);
756 FreeTemp(rARM_LR);
757 LockTemp(rARM_LR);
758 bool load_early = true;
759 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800760 int expected_reg = is_long ? rl_src_expected.reg.GetLowReg() : rl_src_expected.reg.GetReg();
761 int new_val_reg = is_long ? rl_src_new_value.reg.GetLowReg() : rl_src_new_value.reg.GetReg();
762 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !IsFpReg(expected_reg);
763 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !IsFpReg(new_val_reg);
764 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
765 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000766
767 if (!expected_is_good_reg && !new_value_is_good_reg) {
768 // None of expected/new_value is non-temp reg, need to load both late
769 load_early = false;
770 // Make sure they are not in the temp regs and the load will not be skipped.
771 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800772 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000773 ClobberSReg(rl_src_expected.s_reg_low);
774 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
775 rl_src_expected.location = kLocDalvikFrame;
776 }
777 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800778 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000779 ClobberSReg(rl_src_new_value.s_reg_low);
780 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
781 rl_src_new_value.location = kLocDalvikFrame;
782 }
783 }
784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785
786 // Release store semantics, get the barrier out of the way. TODO: revisit
787 GenMemBarrier(kStoreLoad);
788
789 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000790 RegLocation rl_new_value;
791 if (!is_long) {
792 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
793 } else if (load_early) {
794 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
795 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796
Vladimir Marko1c282e22013-11-21 14:49:47 +0000797 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800799 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 }
801
802 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
803
buzbee2700f7e2014-03-07 09:46:20 -0800804 RegStorage r_ptr = rs_rARM_LR;
805 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806
807 // Free now unneeded rl_object and rl_offset to give more temps.
808 ClobberSReg(rl_object.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000809 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700810 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000811 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812
Vladimir Marko3e5af822013-11-21 15:01:20 +0000813 RegLocation rl_expected;
814 if (!is_long) {
815 rl_expected = LoadValue(rl_src_expected, kCoreReg);
816 } else if (load_early) {
817 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
818 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000819 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee2700f7e2014-03-07 09:46:20 -0800820 int low_reg = AllocTemp().GetReg();
821 int high_reg = AllocTemp().GetReg();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000822 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
823 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000824 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825
Vladimir Marko3e5af822013-11-21 15:01:20 +0000826 // do {
827 // tmp = [r_ptr] - expected;
828 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
829 // result = tmp != 0;
830
buzbee2700f7e2014-03-07 09:46:20 -0800831 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700832 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700833
Dave Allison3da67a52014-04-02 17:03:45 -0700834 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000835 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800836 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000837 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800838 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000839 }
buzbee2700f7e2014-03-07 09:46:20 -0800840 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
841 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
842 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000843 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800844 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000845 }
846 // Make sure we use ORR that sets the ccode
buzbee2700f7e2014-03-07 09:46:20 -0800847 if (ARM_LOWREG(r_tmp.GetReg()) && ARM_LOWREG(r_tmp_high.GetReg())) {
848 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000849 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800850 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000851 }
852 FreeTemp(r_tmp_high); // Now unneeded
853
854 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700855 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800856 NewLIR4(kThumb2Strexd /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetLowReg(), rl_new_value.reg.GetHighReg(), r_ptr.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000857
858 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800859 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
860 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000861 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700862 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800863 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000864 }
865
866 // Still one conditional left from OpIT(kCondEq, "T") from either branch
867 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700868 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700869
Jeff Hao2de2aa12013-09-12 17:20:31 -0700870 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871
Vladimir Marko3e5af822013-11-21 15:01:20 +0000872 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800873 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000874 }
875
876 // result := (tmp1 != 0) ? 0 : 1;
877 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800878 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000879 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700880 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800881 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000882 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700883 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000884
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 StoreValue(rl_dest, rl_result);
886
Vladimir Marko3e5af822013-11-21 15:01:20 +0000887 // Now, restore lr to its non-temp status.
888 Clobber(rARM_LR);
889 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 return true;
891}
892
buzbee2700f7e2014-03-07 09:46:20 -0800893LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
894 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895}
896
buzbee2700f7e2014-03-07 09:46:20 -0800897LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
898 return NewLIR3(kThumb2Vldms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899}
900
buzbee2700f7e2014-03-07 09:46:20 -0800901LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
902 return NewLIR3(kThumb2Vstms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903}
904
905void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
906 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700908 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909 EncodeShift(kArmLsl, second_bit - first_bit));
910 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800911 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 }
913}
914
buzbee2700f7e2014-03-07 09:46:20 -0800915void ArmMir2Lir::GenDivZeroCheck(RegStorage reg) {
916 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
917 RegStorage t_reg = AllocTemp();
918 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 FreeTemp(t_reg);
Mingyao Yang42894562014-04-07 12:42:16 -0700920 AddDivZeroSlowPath(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921}
922
923// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700924LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
926 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
927}
928
929// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800930LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000932 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
933 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 return OpCondBranch(c_code, target);
935}
936
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700937void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700938#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800939 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
940 LIR* barrier = last_lir_insn_;
941
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 int dmb_flavor;
943 // TODO: revisit Arm barrier kinds
944 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800945 case kLoadStore: dmb_flavor = kISH; break;
946 case kLoadLoad: dmb_flavor = kISH; break;
947 case kStoreStore: dmb_flavor = kISHST; break;
948 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949 default:
950 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
951 dmb_flavor = kSY; // quiet gcc.
952 break;
953 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800954
955 // If the same barrier already exists, don't generate another.
956 if (barrier == nullptr
957 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
958 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
959 }
960
961 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
962 DCHECK(!barrier->flags.use_def_invalid);
963 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964#endif
965}
966
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700967void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 rl_src = LoadValueWide(rl_src, kCoreReg);
969 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800970 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 LoadConstantNoClobber(z_reg, 0);
972 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -0800973 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
974 RegStorage t_reg = AllocTemp();
975 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
976 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 FreeTemp(t_reg);
978 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800979 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
980 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 }
982 FreeTemp(z_reg);
983 StoreValueWide(rl_dest, rl_result);
984}
985
Mark Mendelle02d48f2014-01-15 11:19:23 -0800986void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
987 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 /*
Zheng Xud7f8e022014-03-13 13:40:30 +0000989 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
990 * dest = src1.lo * src2.lo;
991 * tmp1 += src1.lo * src2.hi;
992 * dest.hi += tmp1;
993 *
994 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 * registers. Normally for Arm, we get 5. We can get to 6 by including
996 * lr in the temp set. The only problematic case is all operands and result are
997 * distinct, and none have been promoted. In that case, we can succeed by aggressively
998 * freeing operand temp registers after they are no longer needed. All other cases
999 * can proceed normally. We'll just punt on the case of the result having a misaligned
1000 * overlap with either operand and send that case to a runtime handler.
1001 */
1002 RegLocation rl_result;
1003 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001004 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 FlushAllRegs();
1006 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1007 rl_result = GetReturnWide(false);
1008 StoreValueWide(rl_dest, rl_result);
1009 return;
1010 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001011
1012 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1013 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1014
1015 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001016 RegStorage res_lo;
1017 RegStorage res_hi;
1018 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1019 !IsTemp(rl_dest.reg.GetLowReg()) && !IsTemp(rl_dest.reg.GetHighReg());
1020 bool src1_promoted = !IsTemp(rl_src1.reg.GetLowReg()) && !IsTemp(rl_src1.reg.GetHighReg());
1021 bool src2_promoted = !IsTemp(rl_src2.reg.GetLowReg()) && !IsTemp(rl_src2.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001022 // Check if rl_dest is *not* either operand and we have enough temp registers.
1023 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1024 (dest_promoted || src1_promoted || src2_promoted)) {
1025 // In this case, we do not need to manually allocate temp registers for result.
1026 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001027 res_lo = rl_result.reg.GetLow();
1028 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001029 } else {
1030 res_lo = AllocTemp();
1031 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1032 // In this case, we have enough temp registers to be allocated for result.
1033 res_hi = AllocTemp();
1034 reg_status = 1;
1035 } else {
1036 // In this case, all temps are now allocated.
1037 // res_hi will be allocated after we can free src1_hi.
1038 reg_status = 2;
1039 }
1040 }
1041
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 // Temporarily add LR to the temp pool, and assign it to tmp1
1043 MarkTemp(rARM_LR);
1044 FreeTemp(rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001045 RegStorage tmp1 = rs_rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 LockTemp(rARM_LR);
1047
buzbee2700f7e2014-03-07 09:46:20 -08001048 if (rl_src1.reg == rl_src2.reg) {
1049 DCHECK(res_hi.Valid());
1050 DCHECK(res_lo.Valid());
1051 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1052 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1053 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001054 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001056 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001057 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001058 DCHECK(!res_hi.Valid());
1059 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001060 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1061 FreeTemp(rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001062 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 }
buzbee2700f7e2014-03-07 09:46:20 -08001064 DCHECK(res_hi.Valid());
1065 DCHECK(res_lo.Valid());
1066 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1067 rl_src1.reg.GetLowReg());
1068 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1069 tmp1.GetReg());
1070 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001071 if (reg_status == 2) {
1072 // Clobber rl_src1 since it was corrupted.
buzbee2700f7e2014-03-07 09:46:20 -08001073 FreeTemp(rl_src1.reg);
1074 Clobber(rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
1076 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001077
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001079 FreeTemp(tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 Clobber(rARM_LR);
1081 UnmarkTemp(rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001082
1083 if (reg_status != 0) {
1084 // We had manually allocated registers for rl_result.
1085 // Now construct a RegLocation.
1086 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001087 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001088 }
1089
1090 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091}
1092
Mark Mendelle02d48f2014-01-15 11:19:23 -08001093void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1096}
1097
Mark Mendelle02d48f2014-01-15 11:19:23 -08001098void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1101}
1102
Mark Mendelle02d48f2014-01-15 11:19:23 -08001103void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001104 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1106}
1107
Mark Mendelle02d48f2014-01-15 11:19:23 -08001108void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001109 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1111}
1112
Mark Mendelle02d48f2014-01-15 11:19:23 -08001113void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001114 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1116}
1117
1118/*
1119 * Generate array load
1120 */
1121void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001122 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 RegisterClass reg_class = oat_reg_class_by_size(size);
1124 int len_offset = mirror::Array::LengthOffset().Int32Value();
1125 int data_offset;
1126 RegLocation rl_result;
1127 bool constant_index = rl_index.is_const;
1128 rl_array = LoadValue(rl_array, kCoreReg);
1129 if (!constant_index) {
1130 rl_index = LoadValue(rl_index, kCoreReg);
1131 }
1132
1133 if (rl_dest.wide) {
1134 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1135 } else {
1136 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1137 }
1138
1139 // If index is constant, just fold it into the data offset
1140 if (constant_index) {
1141 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1142 }
1143
1144 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001145 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146
1147 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001148 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 if (needs_range_check) {
1150 reg_len = AllocTemp();
1151 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001152 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001153 MarkPossibleNullPointerException(opt_flags);
1154 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001155 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001158 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001160 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 } else {
1162 // No special indexed operation, lea + load w/ displacement
1163 reg_ptr = AllocTemp();
Ian Rogerse2143c02014-03-28 08:47:16 -07001164 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001165 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 }
1167 rl_result = EvalLoc(rl_dest, reg_class, true);
1168
1169 if (needs_range_check) {
1170 if (constant_index) {
1171 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1172 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001173 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 }
1175 FreeTemp(reg_len);
1176 }
1177 if (rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001178 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001179 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 if (!constant_index) {
1181 FreeTemp(reg_ptr);
1182 }
1183 StoreValueWide(rl_dest, rl_result);
1184 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001185 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001186 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 if (!constant_index) {
1188 FreeTemp(reg_ptr);
1189 }
1190 StoreValue(rl_dest, rl_result);
1191 }
1192 } else {
1193 // Offset base, then use indexed load
buzbee2700f7e2014-03-07 09:46:20 -08001194 RegStorage reg_ptr = AllocTemp();
1195 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001196 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 rl_result = EvalLoc(rl_dest, reg_class, true);
1198
1199 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001200 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 FreeTemp(reg_len);
1202 }
buzbee2700f7e2014-03-07 09:46:20 -08001203 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001204 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 FreeTemp(reg_ptr);
1206 StoreValue(rl_dest, rl_result);
1207 }
1208}
1209
1210/*
1211 * Generate array store
1212 *
1213 */
1214void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001215 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 RegisterClass reg_class = oat_reg_class_by_size(size);
1217 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 bool constant_index = rl_index.is_const;
1219
Ian Rogersa9a82542013-10-04 11:17:26 -07001220 int data_offset;
1221 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001222 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1223 } else {
1224 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1225 }
1226
1227 // If index is constant, just fold it into the data offset.
1228 if (constant_index) {
1229 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1230 }
1231
1232 rl_array = LoadValue(rl_array, kCoreReg);
1233 if (!constant_index) {
1234 rl_index = LoadValue(rl_index, kCoreReg);
1235 }
1236
buzbee2700f7e2014-03-07 09:46:20 -08001237 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001238 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001240 reg_ptr = rl_array.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001241 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1242 Clobber(rl_array.reg.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001243 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001245 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 reg_ptr = AllocTemp();
1247 }
1248
1249 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001250 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251
1252 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001253 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 if (needs_range_check) {
1255 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001256 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001258 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001259 MarkPossibleNullPointerException(opt_flags);
1260 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001261 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 }
1263 /* at this point, reg_ptr points to array, 2 live temps */
1264 if (rl_src.wide || rl_src.fp || constant_index) {
1265 if (rl_src.wide) {
1266 rl_src = LoadValueWide(rl_src, reg_class);
1267 } else {
1268 rl_src = LoadValue(rl_src, reg_class);
1269 }
1270 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001271 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 }
1273 if (needs_range_check) {
1274 if (constant_index) {
1275 GenImmedCheck(kCondLs, reg_len, mir_graph_->ConstantValue(rl_index), kThrowConstantArrayBounds);
1276 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001277 GenRegRegCheck(kCondLs, reg_len, rl_index.reg, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 }
1279 FreeTemp(reg_len);
1280 }
1281
1282 if (rl_src.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001283 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001285 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 }
Dave Allisonb373e092014-02-20 16:06:36 -08001287 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001288 } else {
1289 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001290 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 rl_src = LoadValue(rl_src, reg_class);
1292 if (needs_range_check) {
buzbee2700f7e2014-03-07 09:46:20 -08001293 GenRegRegCheck(kCondUge, rl_index.reg, reg_len, kThrowArrayBounds);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001294 FreeTemp(reg_len);
1295 }
buzbee2700f7e2014-03-07 09:46:20 -08001296 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001297 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 }
Ian Rogers773aab12013-10-14 13:50:10 -07001299 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 FreeTemp(reg_ptr);
1301 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001302 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001303 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 }
1305}
1306
Ian Rogersa9a82542013-10-04 11:17:26 -07001307
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001309 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 rl_src = LoadValueWide(rl_src, kCoreReg);
1311 // Per spec, we only care about low 6 bits of shift amount.
1312 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1313 if (shift_amount == 0) {
1314 StoreValueWide(rl_dest, rl_src);
1315 return;
1316 }
1317 if (BadOverlap(rl_src, rl_dest)) {
1318 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1319 return;
1320 }
1321 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001322 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 case Instruction::SHL_LONG:
1324 case Instruction::SHL_LONG_2ADDR:
1325 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001326 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1327 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001329 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1330 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001332 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1333 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001335 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001336 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001338 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 }
1340 break;
1341 case Instruction::SHR_LONG:
1342 case Instruction::SHR_LONG_2ADDR:
1343 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001344 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1345 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001347 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1348 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001350 RegStorage t_reg = AllocTemp();
1351 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001352 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 EncodeShift(kArmLsl, 32 - shift_amount));
1354 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001355 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 }
1357 break;
1358 case Instruction::USHR_LONG:
1359 case Instruction::USHR_LONG_2ADDR:
1360 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001361 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1362 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001364 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1365 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001367 RegStorage t_reg = AllocTemp();
1368 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001369 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 EncodeShift(kArmLsl, 32 - shift_amount));
1371 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001372 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 }
1374 break;
1375 default:
1376 LOG(FATAL) << "Unexpected case";
1377 }
1378 StoreValueWide(rl_dest, rl_result);
1379}
1380
1381void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001382 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1384 if (!rl_src2.is_const) {
1385 // Don't bother with special handling for subtract from immediate.
1386 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1387 return;
1388 }
1389 } else {
1390 // Normalize
1391 if (!rl_src2.is_const) {
1392 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001393 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 }
1395 }
1396 if (BadOverlap(rl_src1, rl_dest)) {
1397 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1398 return;
1399 }
1400 DCHECK(rl_src2.is_const);
1401 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1402 uint32_t val_lo = Low32Bits(val);
1403 uint32_t val_hi = High32Bits(val);
1404 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1405 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1406
1407 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001408 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 case Instruction::ADD_LONG:
1410 case Instruction::ADD_LONG_2ADDR:
1411 case Instruction::SUB_LONG:
1412 case Instruction::SUB_LONG_2ADDR:
1413 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1414 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1415 return;
1416 }
1417 break;
1418 default:
1419 break;
1420 }
1421 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1422 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1423 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1424 switch (opcode) {
1425 case Instruction::ADD_LONG:
1426 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001427 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001428 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 break;
1430 case Instruction::OR_LONG:
1431 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001432 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1433 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001435 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001436 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001437 }
1438 break;
1439 case Instruction::XOR_LONG:
1440 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001441 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1442 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 break;
1444 case Instruction::AND_LONG:
1445 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001446 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1447 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001449 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001450 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 }
1452 break;
1453 case Instruction::SUB_LONG_2ADDR:
1454 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001455 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001456 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 break;
1458 default:
1459 LOG(FATAL) << "Unexpected opcode " << opcode;
1460 }
1461 StoreValueWide(rl_dest, rl_result);
1462}
1463
1464} // namespace art