blob: 940ebc26c98b92e50818604f74f8460f05754f48 [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 */
buzbeea1983d42014-04-07 12:35:39 -070093void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 LIR* target1;
95 LIR* target2;
96 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
97 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080098 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800100 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 LIR* branch1 = OpCondBranch(kCondLt, NULL);
102 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700103 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 LIR* branch3 = OpCondBranch(kCondEq, NULL);
105
Dave Allison3da67a52014-04-02 17:03:45 -0700106 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800107 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700109 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110
111 target2 = NewLIR0(kPseudoTargetLabel);
112 OpRegReg(kOpNeg, t_reg, t_reg);
113
114 target1 = NewLIR0(kPseudoTargetLabel);
115
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700116 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800117 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 StoreValue(rl_dest, rl_temp);
119 FreeTemp(t_reg);
120
121 branch1->target = target1;
122 branch2->target = target2;
123 branch3->target = branch1->target;
124}
125
126void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700127 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 int32_t val_lo = Low32Bits(val);
129 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700130 DCHECK_GE(ModifiedImmediate(val_lo), 0);
131 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700132 LIR* taken = &block_label_list_[bb->taken];
133 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800135 RegStorage low_reg = rl_src1.reg.GetLow();
136 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137
Vladimir Marko58af1f92013-12-19 13:31:15 +0000138 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800139 RegStorage t_reg = AllocTemp();
140 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000141 FreeTemp(t_reg);
142 OpCondBranch(ccode, taken);
143 return;
144 }
145
Brian Carlstromdf629502013-07-17 22:39:56 -0700146 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 case kCondEq:
148 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000149 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 break;
151 case kCondLt:
152 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
153 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000154 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 break;
156 case kCondLe:
157 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
158 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
159 ccode = kCondLs;
160 break;
161 case kCondGt:
162 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
163 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
164 ccode = kCondHi;
165 break;
166 case kCondGe:
167 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
168 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000169 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 break;
171 default:
172 LOG(FATAL) << "Unexpected ccode: " << ccode;
173 }
174 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
175}
176
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700177void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 RegLocation rl_result;
179 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 RegLocation rl_dest = mir_graph_->GetDest(mir);
181 rl_src = LoadValue(rl_src, kCoreReg);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000182 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 if (mir->ssa_rep->num_uses == 1) {
184 // CONST case
185 int true_val = mir->dalvikInsn.vB;
186 int false_val = mir->dalvikInsn.vC;
187 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000188 // Change kCondNe to kCondEq for the special cases below.
189 if (ccode == kCondNe) {
190 ccode = kCondEq;
191 std::swap(true_val, false_val);
192 }
193 bool cheap_false_val = InexpensiveConstantInt(false_val);
194 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800195 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000196 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700197 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800198 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700199 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000200 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800201 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000202 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700203 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800204 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700205 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000206 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800207 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700208 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800209 LoadConstant(rl_result.reg, true_val);
210 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700211 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 } else {
213 // Unlikely case - could be tuned.
buzbee2700f7e2014-03-07 09:46:20 -0800214 RegStorage t_reg1 = AllocTemp();
215 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 LoadConstant(t_reg1, true_val);
217 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800218 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700219 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800220 OpRegCopy(rl_result.reg, t_reg1);
221 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700222 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 }
224 } else {
225 // MOVE case
226 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
227 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
228 rl_true = LoadValue(rl_true, kCoreReg);
229 rl_false = LoadValue(rl_false, kCoreReg);
230 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800231 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700232 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000233 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700234 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800235 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000236 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700237 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800238 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700239 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700240 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800241 OpRegCopy(rl_result.reg, rl_true.reg);
242 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700243 }
Dave Allison3da67a52014-04-02 17:03:45 -0700244 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 }
246 StoreValue(rl_dest, rl_result);
247}
248
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700249void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
251 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
252 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000253 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000255 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 ccode = FlipComparisonOrder(ccode);
257 }
258 if (rl_src2.is_const) {
259 RegLocation rl_temp = UpdateLocWide(rl_src2);
260 // Do special compare/branch against simple const operand if not already in registers.
261 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
262 if ((rl_temp.location != kLocPhysReg) &&
263 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
264 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
265 return;
266 }
267 }
buzbee0d829482013-10-11 15:24:55 -0700268 LIR* taken = &block_label_list_[bb->taken];
269 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
271 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800272 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700273 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 case kCondEq:
275 OpCondBranch(kCondNe, not_taken);
276 break;
277 case kCondNe:
278 OpCondBranch(kCondNe, taken);
279 break;
280 case kCondLt:
281 OpCondBranch(kCondLt, taken);
282 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000283 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 break;
285 case kCondLe:
286 OpCondBranch(kCondLt, taken);
287 OpCondBranch(kCondGt, not_taken);
288 ccode = kCondLs;
289 break;
290 case kCondGt:
291 OpCondBranch(kCondGt, taken);
292 OpCondBranch(kCondLt, not_taken);
293 ccode = kCondHi;
294 break;
295 case kCondGe:
296 OpCondBranch(kCondGt, taken);
297 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000298 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 break;
300 default:
301 LOG(FATAL) << "Unexpected ccode: " << ccode;
302 }
buzbee2700f7e2014-03-07 09:46:20 -0800303 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 OpCondBranch(ccode, taken);
305}
306
307/*
308 * Generate a register comparison to an immediate and branch. Caller
309 * is responsible for setting branch target field.
310 */
buzbee2700f7e2014-03-07 09:46:20 -0800311LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700314 /*
315 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
316 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700317 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700318 * be converted to a long form during assembly (which will trigger another assembly
319 * pass). Here we estimate the branch distance for checks, and if large directly
320 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700321 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700322 */
323 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
324 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
buzbee2700f7e2014-03-07 09:46:20 -0800325 if (!skip && (ARM_LOWREG(reg.GetReg())) && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
327 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
buzbee2700f7e2014-03-07 09:46:20 -0800328 reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000330 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 branch = NewLIR2(kThumbBCond, 0, arm_cond);
332 }
333 branch->target = target;
334 return branch;
335}
336
buzbee2700f7e2014-03-07 09:46:20 -0800337LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 LIR* res;
339 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800340 // If src or dest is a pair, we'll be using low reg.
341 if (r_dest.IsPair()) {
342 r_dest = r_dest.GetLow();
343 }
344 if (r_src.IsPair()) {
345 r_src = r_src.GetLow();
346 }
347 if (ARM_FPREG(r_dest.GetReg()) || ARM_FPREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 return OpFpRegCopy(r_dest, r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800349 if (ARM_LOWREG(r_dest.GetReg()) && ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 opcode = kThumbMovRR;
buzbee2700f7e2014-03-07 09:46:20 -0800351 else if (!ARM_LOWREG(r_dest.GetReg()) && !ARM_LOWREG(r_src.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 opcode = kThumbMovRR_H2H;
buzbee2700f7e2014-03-07 09:46:20 -0800353 else if (ARM_LOWREG(r_dest.GetReg()))
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 opcode = kThumbMovRR_H2L;
355 else
356 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800357 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
359 res->flags.is_nop = true;
360 }
361 return res;
362}
363
buzbee2700f7e2014-03-07 09:46:20 -0800364LIR* ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
366 AppendLIR(res);
367 return res;
368}
369
buzbee2700f7e2014-03-07 09:46:20 -0800370void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
371 bool dest_fp = ARM_FPREG(r_dest.GetLowReg());
372 bool src_fp = ARM_FPREG(r_src.GetLowReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 if (dest_fp) {
374 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800375 // FIXME: handle 64-bit solo's here.
376 OpRegCopy(RegStorage::Solo64(S2d(r_dest.GetLowReg(), r_dest.GetHighReg())),
377 RegStorage::Solo64(S2d(r_src.GetLowReg(), r_src.GetHighReg())));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800379 NewLIR3(kThumb2Fmdrr, S2d(r_dest.GetLowReg(), r_dest.GetHighReg()),
380 r_src.GetLowReg(), r_src.GetHighReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
382 } else {
383 if (src_fp) {
buzbee2700f7e2014-03-07 09:46:20 -0800384 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), S2d(r_src.GetLowReg(),
385 r_src.GetHighReg()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 } else {
387 // Handle overlap
buzbee2700f7e2014-03-07 09:46:20 -0800388 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
389 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
390 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
391 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800393 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
394 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 }
396 }
397 }
398}
399
400// Table of magic divisors
401struct MagicTable {
402 uint32_t magic;
403 uint32_t shift;
404 DividePattern pattern;
405};
406
407static const MagicTable magic_table[] = {
408 {0, 0, DivideNone}, // 0
409 {0, 0, DivideNone}, // 1
410 {0, 0, DivideNone}, // 2
411 {0x55555556, 0, Divide3}, // 3
412 {0, 0, DivideNone}, // 4
413 {0x66666667, 1, Divide5}, // 5
414 {0x2AAAAAAB, 0, Divide3}, // 6
415 {0x92492493, 2, Divide7}, // 7
416 {0, 0, DivideNone}, // 8
417 {0x38E38E39, 1, Divide5}, // 9
418 {0x66666667, 2, Divide5}, // 10
419 {0x2E8BA2E9, 1, Divide5}, // 11
420 {0x2AAAAAAB, 1, Divide5}, // 12
421 {0x4EC4EC4F, 2, Divide5}, // 13
422 {0x92492493, 3, Divide7}, // 14
423 {0x88888889, 3, Divide7}, // 15
424};
425
426// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700427bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700428 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
430 return false;
431 }
432 DividePattern pattern = magic_table[lit].pattern;
433 if (pattern == DivideNone) {
434 return false;
435 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436
buzbee2700f7e2014-03-07 09:46:20 -0800437 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 LoadConstant(r_magic, magic_table[lit].magic);
439 rl_src = LoadValue(rl_src, kCoreReg);
440 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800441 RegStorage r_hi = AllocTemp();
442 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100443
444 // rl_dest and rl_src might overlap.
445 // Reuse r_hi to save the div result for reminder case.
446 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
447
buzbee2700f7e2014-03-07 09:46:20 -0800448 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700449 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100451 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 break;
453 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800454 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100455 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700456 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 break;
458 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800459 OpRegReg(kOpAdd, r_hi, rl_src.reg);
460 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100461 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700462 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 break;
464 default:
465 LOG(FATAL) << "Unexpected pattern: " << pattern;
466 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100467
468 if (!is_div) {
469 // div_result = src / lit
470 // tmp1 = div_result * lit
471 // dest = src - tmp1
472 RegStorage tmp1 = r_lo;
473 EasyMultiplyOp ops[2];
474
475 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
476 DCHECK_NE(canEasyMultiply, false);
477
478 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
479 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
480 }
481
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 StoreValue(rl_dest, rl_result);
483 return true;
484}
485
Ian Rogerse2143c02014-03-28 08:47:16 -0700486// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
487bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
488 if (IsPowerOfTwo(lit)) {
489 op->op = kOpLsl;
490 op->shift = LowestSetBit(lit);
491 return true;
492 }
493
494 if (IsPowerOfTwo(lit - 1)) {
495 op->op = kOpAdd;
496 op->shift = LowestSetBit(lit - 1);
497 return true;
498 }
499
500 if (IsPowerOfTwo(lit + 1)) {
501 op->op = kOpRsub;
502 op->shift = LowestSetBit(lit + 1);
503 return true;
504 }
505
506 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100507 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700508 return false;
509}
510
511// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
512bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
513 GetEasyMultiplyOp(lit, &ops[0]);
514 if (GetEasyMultiplyOp(lit, &ops[0])) {
515 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100516 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700517 return true;
518 }
519
520 int lit1 = lit;
521 uint32_t shift = LowestSetBit(lit1);
522 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
523 ops[1].op = kOpLsl;
524 ops[1].shift = shift;
525 return true;
526 }
527
528 lit1 = lit - 1;
529 shift = LowestSetBit(lit1);
530 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
531 ops[1].op = kOpAdd;
532 ops[1].shift = shift;
533 return true;
534 }
535
536 lit1 = lit + 1;
537 shift = LowestSetBit(lit1);
538 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
539 ops[1].op = kOpRsub;
540 ops[1].shift = shift;
541 return true;
542 }
543
544 return false;
545}
546
Zheng Xuf9719f92014-04-02 13:31:31 +0100547// Generate instructions to do multiply.
548// Additional temporary register is required,
549// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700550void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100551 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
552 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
553
554 RegStorage r_tmp1;
555 if (ops[1].op == kOpInvalid) {
556 r_tmp1 = r_dest;
557 } else if (r_dest.GetReg() != r_src.GetReg()) {
558 r_tmp1 = r_dest;
559 } else {
560 r_tmp1 = AllocTemp();
561 }
562
563 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700564 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100565 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700566 break;
567 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100568 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700569 break;
570 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100571 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700572 break;
573 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100574 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700575 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100576 }
577
578 switch (ops[1].op) {
579 case kOpInvalid:
580 return;
581 case kOpLsl:
582 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
583 break;
584 case kOpAdd:
585 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
586 break;
587 case kOpRsub:
588 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
589 break;
590 default:
591 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
592 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700593 }
594}
595
596bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
597 EasyMultiplyOp ops[2];
598
599 if (!GetEasyMultiplyTwoOps(lit, ops)) {
600 return false;
601 }
602
603 rl_src = LoadValue(rl_src, kCoreReg);
604 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
605
606 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
607 StoreValue(rl_dest, rl_result);
608 return true;
609}
610
Mark Mendell2bf31e62014-01-23 12:13:40 -0800611RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
612 RegLocation rl_src2, bool is_div, bool check_zero) {
613 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
614 return rl_dest;
615}
616
617RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
618 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
619 return rl_dest;
620}
621
buzbee2700f7e2014-03-07 09:46:20 -0800622RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700623 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
624
625 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800626 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700627 LoadConstant(lit_temp, lit);
628 // Use the generic case for div/rem with arg2 in a register.
629 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
630 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
631 FreeTemp(lit_temp);
632
633 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634}
635
buzbee2700f7e2014-03-07 09:46:20 -0800636RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700637 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700638 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
639 if (is_div) {
640 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800641 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700642 } else {
643 // Remainder case, use the following code:
644 // temp = reg1 / reg2 - integer division
645 // temp = temp * reg2
646 // dest = reg1 - temp
647
buzbee2700f7e2014-03-07 09:46:20 -0800648 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700649 OpRegRegReg(kOpDiv, temp, reg1, reg2);
650 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800651 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700652 FreeTemp(temp);
653 }
654
655 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656}
657
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700658bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 DCHECK_EQ(cu_->instruction_set, kThumb2);
660 RegLocation rl_src1 = info->args[0];
661 RegLocation rl_src2 = info->args[1];
662 rl_src1 = LoadValue(rl_src1, kCoreReg);
663 rl_src2 = LoadValue(rl_src2, kCoreReg);
664 RegLocation rl_dest = InlineTarget(info);
665 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800666 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700667 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800668 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
669 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700670 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 StoreValue(rl_dest, rl_result);
672 return true;
673}
674
Vladimir Markoe508a202013-11-04 15:24:22 +0000675bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
676 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800677 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000678 RegLocation rl_dest = InlineTarget(info);
679 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
680 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
681 if (size == kLong) {
682 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800683 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
684 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
685 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000686 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800687 LoadWordDisp(rl_address.reg, 4, rl_result.reg.GetHigh());
688 LoadWordDisp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000689 }
690 StoreValueWide(rl_dest, rl_result);
691 } else {
692 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
693 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800694 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, INVALID_SREG);
Vladimir Markoe508a202013-11-04 15:24:22 +0000695 StoreValue(rl_dest, rl_result);
696 }
697 return true;
698}
699
700bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
701 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800702 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000703 RegLocation rl_src_value = info->args[2]; // [size] value
704 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
705 if (size == kLong) {
706 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
707 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800708 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), kWord);
709 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), kWord);
Vladimir Markoe508a202013-11-04 15:24:22 +0000710 } else {
711 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
712 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
713 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800714 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000715 }
716 return true;
717}
718
buzbee2700f7e2014-03-07 09:46:20 -0800719void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 LOG(FATAL) << "Unexpected use of OpLea for Arm";
721}
722
Ian Rogersdd7624d2014-03-14 17:43:00 -0700723void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
725}
726
Vladimir Marko1c282e22013-11-21 14:49:47 +0000727bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 DCHECK_EQ(cu_->instruction_set, kThumb2);
729 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000730 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
731 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800732 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000733 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000734 // If is_long, high half is in info->args[5]
735 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
736 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 RegLocation rl_dest = InlineTarget(info); // boolean place for result
738
Vladimir Marko3e5af822013-11-21 15:01:20 +0000739 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
740 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
741 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
742 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
743 // into the same temps, reducing the number of required temps down to 5. We shall work
744 // around the potentially locked temp by using LR for r_ptr, unconditionally.
745 // TODO: Pass information about the need for more temps to the stack frame generation
746 // code so that we can rely on being able to allocate enough temps.
747 DCHECK(!reg_pool_->core_regs[rARM_LR].is_temp);
748 MarkTemp(rARM_LR);
749 FreeTemp(rARM_LR);
750 LockTemp(rARM_LR);
751 bool load_early = true;
752 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800753 int expected_reg = is_long ? rl_src_expected.reg.GetLowReg() : rl_src_expected.reg.GetReg();
754 int new_val_reg = is_long ? rl_src_new_value.reg.GetLowReg() : rl_src_new_value.reg.GetReg();
755 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !IsFpReg(expected_reg);
756 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !IsFpReg(new_val_reg);
757 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
758 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000759
760 if (!expected_is_good_reg && !new_value_is_good_reg) {
761 // None of expected/new_value is non-temp reg, need to load both late
762 load_early = false;
763 // Make sure they are not in the temp regs and the load will not be skipped.
764 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800765 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000766 ClobberSReg(rl_src_expected.s_reg_low);
767 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
768 rl_src_expected.location = kLocDalvikFrame;
769 }
770 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800771 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000772 ClobberSReg(rl_src_new_value.s_reg_low);
773 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
774 rl_src_new_value.location = kLocDalvikFrame;
775 }
776 }
777 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778
779 // Release store semantics, get the barrier out of the way. TODO: revisit
780 GenMemBarrier(kStoreLoad);
781
782 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000783 RegLocation rl_new_value;
784 if (!is_long) {
785 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
786 } else if (load_early) {
787 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
788 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789
Vladimir Marko1c282e22013-11-21 14:49:47 +0000790 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800792 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 }
794
795 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
796
buzbee2700f7e2014-03-07 09:46:20 -0800797 RegStorage r_ptr = rs_rARM_LR;
798 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799
800 // Free now unneeded rl_object and rl_offset to give more temps.
801 ClobberSReg(rl_object.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000802 FreeTemp(rl_object.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 ClobberSReg(rl_offset.s_reg_low);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000804 FreeTemp(rl_offset.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805
Vladimir Marko3e5af822013-11-21 15:01:20 +0000806 RegLocation rl_expected;
807 if (!is_long) {
808 rl_expected = LoadValue(rl_src_expected, kCoreReg);
809 } else if (load_early) {
810 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
811 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000812 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee2700f7e2014-03-07 09:46:20 -0800813 int low_reg = AllocTemp().GetReg();
814 int high_reg = AllocTemp().GetReg();
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000815 rl_new_value.reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
816 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000817 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818
Vladimir Marko3e5af822013-11-21 15:01:20 +0000819 // do {
820 // tmp = [r_ptr] - expected;
821 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
822 // result = tmp != 0;
823
buzbee2700f7e2014-03-07 09:46:20 -0800824 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700825 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700826
Dave Allison3da67a52014-04-02 17:03:45 -0700827 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000828 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800829 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000830 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800831 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000832 }
buzbee2700f7e2014-03-07 09:46:20 -0800833 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
834 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
835 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000836 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800837 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000838 }
839 // Make sure we use ORR that sets the ccode
buzbee2700f7e2014-03-07 09:46:20 -0800840 if (ARM_LOWREG(r_tmp.GetReg()) && ARM_LOWREG(r_tmp_high.GetReg())) {
841 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000842 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800843 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000844 }
845 FreeTemp(r_tmp_high); // Now unneeded
846
847 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700848 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800849 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 +0000850
851 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800852 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
853 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000854 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(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000857 }
858
859 // Still one conditional left from OpIT(kCondEq, "T") from either branch
860 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700861 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700862
Jeff Hao2de2aa12013-09-12 17:20:31 -0700863 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864
Vladimir Marko3e5af822013-11-21 15:01:20 +0000865 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800866 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000867 }
868
869 // result := (tmp1 != 0) ? 0 : 1;
870 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800871 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000872 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700873 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800874 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000875 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700876 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000877
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 StoreValue(rl_dest, rl_result);
879
Vladimir Marko3e5af822013-11-21 15:01:20 +0000880 // Now, restore lr to its non-temp status.
881 Clobber(rARM_LR);
882 UnmarkTemp(rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 return true;
884}
885
buzbee2700f7e2014-03-07 09:46:20 -0800886LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
887 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888}
889
buzbee2700f7e2014-03-07 09:46:20 -0800890LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
891 return NewLIR3(kThumb2Vldms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892}
893
buzbee2700f7e2014-03-07 09:46:20 -0800894LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
895 return NewLIR3(kThumb2Vstms, r_base.GetReg(), fr0, count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896}
897
898void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
899 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700900 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700901 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 EncodeShift(kArmLsl, second_bit - first_bit));
903 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800904 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 }
906}
907
Mingyao Yange643a172014-04-08 11:02:52 -0700908void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800909 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
910 RegStorage t_reg = AllocTemp();
911 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -0700913 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914}
915
916// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700917LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 NewLIR2(kThumbSubRI8, rARM_SUSPEND, 1);
919 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
920}
921
922// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800923LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000925 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
926 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 return OpCondBranch(c_code, target);
928}
929
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700930void ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800932 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
933 LIR* barrier = last_lir_insn_;
934
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 int dmb_flavor;
936 // TODO: revisit Arm barrier kinds
937 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800938 case kLoadStore: dmb_flavor = kISH; break;
939 case kLoadLoad: dmb_flavor = kISH; break;
940 case kStoreStore: dmb_flavor = kISHST; break;
941 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 default:
943 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
944 dmb_flavor = kSY; // quiet gcc.
945 break;
946 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800947
948 // If the same barrier already exists, don't generate another.
949 if (barrier == nullptr
950 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
951 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
952 }
953
954 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
955 DCHECK(!barrier->flags.use_def_invalid);
956 barrier->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957#endif
958}
959
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700960void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 rl_src = LoadValueWide(rl_src, kCoreReg);
962 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800963 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 LoadConstantNoClobber(z_reg, 0);
965 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -0800966 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
967 RegStorage t_reg = AllocTemp();
968 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
969 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 FreeTemp(t_reg);
971 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800972 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
973 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 }
975 FreeTemp(z_reg);
976 StoreValueWide(rl_dest, rl_result);
977}
978
Mark Mendelle02d48f2014-01-15 11:19:23 -0800979void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
980 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 /*
Zheng Xud7f8e022014-03-13 13:40:30 +0000982 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
983 * dest = src1.lo * src2.lo;
984 * tmp1 += src1.lo * src2.hi;
985 * dest.hi += tmp1;
986 *
987 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 * registers. Normally for Arm, we get 5. We can get to 6 by including
989 * lr in the temp set. The only problematic case is all operands and result are
990 * distinct, and none have been promoted. In that case, we can succeed by aggressively
991 * freeing operand temp registers after they are no longer needed. All other cases
992 * can proceed normally. We'll just punt on the case of the result having a misaligned
993 * overlap with either operand and send that case to a runtime handler.
994 */
995 RegLocation rl_result;
996 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700997 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 FlushAllRegs();
999 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1000 rl_result = GetReturnWide(false);
1001 StoreValueWide(rl_dest, rl_result);
1002 return;
1003 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001004
1005 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1006 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1007
1008 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001009 RegStorage res_lo;
1010 RegStorage res_hi;
1011 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1012 !IsTemp(rl_dest.reg.GetLowReg()) && !IsTemp(rl_dest.reg.GetHighReg());
1013 bool src1_promoted = !IsTemp(rl_src1.reg.GetLowReg()) && !IsTemp(rl_src1.reg.GetHighReg());
1014 bool src2_promoted = !IsTemp(rl_src2.reg.GetLowReg()) && !IsTemp(rl_src2.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001015 // Check if rl_dest is *not* either operand and we have enough temp registers.
1016 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1017 (dest_promoted || src1_promoted || src2_promoted)) {
1018 // In this case, we do not need to manually allocate temp registers for result.
1019 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001020 res_lo = rl_result.reg.GetLow();
1021 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001022 } else {
1023 res_lo = AllocTemp();
1024 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1025 // In this case, we have enough temp registers to be allocated for result.
1026 res_hi = AllocTemp();
1027 reg_status = 1;
1028 } else {
1029 // In this case, all temps are now allocated.
1030 // res_hi will be allocated after we can free src1_hi.
1031 reg_status = 2;
1032 }
1033 }
1034
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 // Temporarily add LR to the temp pool, and assign it to tmp1
1036 MarkTemp(rARM_LR);
1037 FreeTemp(rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001038 RegStorage tmp1 = rs_rARM_LR;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 LockTemp(rARM_LR);
1040
buzbee2700f7e2014-03-07 09:46:20 -08001041 if (rl_src1.reg == rl_src2.reg) {
1042 DCHECK(res_hi.Valid());
1043 DCHECK(res_lo.Valid());
1044 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1045 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1046 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001047 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001049 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001050 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001051 DCHECK(!res_hi.Valid());
1052 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001053 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1054 FreeTemp(rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001055 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 }
buzbee2700f7e2014-03-07 09:46:20 -08001057 DCHECK(res_hi.Valid());
1058 DCHECK(res_lo.Valid());
1059 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1060 rl_src1.reg.GetLowReg());
1061 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1062 tmp1.GetReg());
1063 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001064 if (reg_status == 2) {
1065 // Clobber rl_src1 since it was corrupted.
buzbee2700f7e2014-03-07 09:46:20 -08001066 FreeTemp(rl_src1.reg);
1067 Clobber(rl_src1.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 }
1069 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001070
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001072 FreeTemp(tmp1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 Clobber(rARM_LR);
1074 UnmarkTemp(rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001075
1076 if (reg_status != 0) {
1077 // We had manually allocated registers for rl_result.
1078 // Now construct a RegLocation.
1079 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001080 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001081 }
1082
1083 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084}
1085
Mark Mendelle02d48f2014-01-15 11:19:23 -08001086void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001087 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1089}
1090
Mark Mendelle02d48f2014-01-15 11:19:23 -08001091void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001092 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1094}
1095
Mark Mendelle02d48f2014-01-15 11:19:23 -08001096void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001097 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1099}
1100
Mark Mendelle02d48f2014-01-15 11:19:23 -08001101void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001102 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1104}
1105
Mark Mendelle02d48f2014-01-15 11:19:23 -08001106void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001107 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1109}
1110
1111/*
1112 * Generate array load
1113 */
1114void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001115 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 RegisterClass reg_class = oat_reg_class_by_size(size);
1117 int len_offset = mirror::Array::LengthOffset().Int32Value();
1118 int data_offset;
1119 RegLocation rl_result;
1120 bool constant_index = rl_index.is_const;
1121 rl_array = LoadValue(rl_array, kCoreReg);
1122 if (!constant_index) {
1123 rl_index = LoadValue(rl_index, kCoreReg);
1124 }
1125
1126 if (rl_dest.wide) {
1127 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1128 } else {
1129 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1130 }
1131
1132 // If index is constant, just fold it into the data offset
1133 if (constant_index) {
1134 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1135 }
1136
1137 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001138 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139
1140 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001141 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 if (needs_range_check) {
1143 reg_len = AllocTemp();
1144 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001145 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001146 MarkPossibleNullPointerException(opt_flags);
1147 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001148 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
1150 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001151 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001152 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001153 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 } else {
1155 // No special indexed operation, lea + load w/ displacement
1156 reg_ptr = AllocTemp();
Ian Rogerse2143c02014-03-28 08:47:16 -07001157 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001158 FreeTemp(rl_index.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160 rl_result = EvalLoc(rl_dest, reg_class, true);
1161
1162 if (needs_range_check) {
1163 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001164 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001166 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 }
1168 FreeTemp(reg_len);
1169 }
1170 if (rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001171 LoadBaseDispWide(reg_ptr, data_offset, rl_result.reg, INVALID_SREG);
Dave Allisonb373e092014-02-20 16:06:36 -08001172 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 if (!constant_index) {
1174 FreeTemp(reg_ptr);
1175 }
1176 StoreValueWide(rl_dest, rl_result);
1177 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001178 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, 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 StoreValue(rl_dest, rl_result);
1184 }
1185 } else {
1186 // Offset base, then use indexed load
buzbee2700f7e2014-03-07 09:46:20 -08001187 RegStorage reg_ptr = AllocTemp();
1188 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001189 FreeTemp(rl_array.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 rl_result = EvalLoc(rl_dest, reg_class, true);
1191
1192 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001193 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 FreeTemp(reg_len);
1195 }
buzbee2700f7e2014-03-07 09:46:20 -08001196 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001197 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 FreeTemp(reg_ptr);
1199 StoreValue(rl_dest, rl_result);
1200 }
1201}
1202
1203/*
1204 * Generate array store
1205 *
1206 */
1207void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001208 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 RegisterClass reg_class = oat_reg_class_by_size(size);
1210 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 bool constant_index = rl_index.is_const;
1212
Ian Rogersa9a82542013-10-04 11:17:26 -07001213 int data_offset;
1214 if (size == kLong || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1216 } else {
1217 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1218 }
1219
1220 // If index is constant, just fold it into the data offset.
1221 if (constant_index) {
1222 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1223 }
1224
1225 rl_array = LoadValue(rl_array, kCoreReg);
1226 if (!constant_index) {
1227 rl_index = LoadValue(rl_index, kCoreReg);
1228 }
1229
buzbee2700f7e2014-03-07 09:46:20 -08001230 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001231 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001233 reg_ptr = rl_array.reg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001234 } else if (IsTemp(rl_array.reg.GetReg()) && !card_mark) {
1235 Clobber(rl_array.reg.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001236 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001238 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 reg_ptr = AllocTemp();
1240 }
1241
1242 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001243 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244
1245 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001246 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 if (needs_range_check) {
1248 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001249 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 /* Get len */
buzbee2700f7e2014-03-07 09:46:20 -08001251 LoadWordDisp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001252 MarkPossibleNullPointerException(opt_flags);
1253 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001254 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
1256 /* at this point, reg_ptr points to array, 2 live temps */
1257 if (rl_src.wide || rl_src.fp || constant_index) {
1258 if (rl_src.wide) {
1259 rl_src = LoadValueWide(rl_src, reg_class);
1260 } else {
1261 rl_src = LoadValue(rl_src, reg_class);
1262 }
1263 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001264 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 }
1266 if (needs_range_check) {
1267 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001268 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001269 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001270 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 }
1272 FreeTemp(reg_len);
1273 }
1274
1275 if (rl_src.wide) {
buzbee2700f7e2014-03-07 09:46:20 -08001276 StoreBaseDispWide(reg_ptr, data_offset, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001278 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 }
Dave Allisonb373e092014-02-20 16:06:36 -08001280 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 } else {
1282 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001283 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 rl_src = LoadValue(rl_src, reg_class);
1285 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001286 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 FreeTemp(reg_len);
1288 }
buzbee2700f7e2014-03-07 09:46:20 -08001289 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001290 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 }
Ian Rogers773aab12013-10-14 13:50:10 -07001292 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 FreeTemp(reg_ptr);
1294 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001295 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001296 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 }
1298}
1299
Ian Rogersa9a82542013-10-04 11:17:26 -07001300
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001302 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 rl_src = LoadValueWide(rl_src, kCoreReg);
1304 // Per spec, we only care about low 6 bits of shift amount.
1305 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1306 if (shift_amount == 0) {
1307 StoreValueWide(rl_dest, rl_src);
1308 return;
1309 }
1310 if (BadOverlap(rl_src, rl_dest)) {
1311 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1312 return;
1313 }
1314 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001315 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 case Instruction::SHL_LONG:
1317 case Instruction::SHL_LONG_2ADDR:
1318 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001319 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1320 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001322 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1323 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001325 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1326 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001328 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001329 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001331 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
1333 break;
1334 case Instruction::SHR_LONG:
1335 case Instruction::SHR_LONG_2ADDR:
1336 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001337 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1338 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001340 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1341 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001343 RegStorage t_reg = AllocTemp();
1344 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001345 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 EncodeShift(kArmLsl, 32 - shift_amount));
1347 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001348 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 }
1350 break;
1351 case Instruction::USHR_LONG:
1352 case Instruction::USHR_LONG_2ADDR:
1353 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001354 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1355 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001357 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1358 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001360 RegStorage t_reg = AllocTemp();
1361 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001362 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 EncodeShift(kArmLsl, 32 - shift_amount));
1364 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001365 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 }
1367 break;
1368 default:
1369 LOG(FATAL) << "Unexpected case";
1370 }
1371 StoreValueWide(rl_dest, rl_result);
1372}
1373
1374void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001375 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1377 if (!rl_src2.is_const) {
1378 // Don't bother with special handling for subtract from immediate.
1379 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1380 return;
1381 }
1382 } else {
1383 // Normalize
1384 if (!rl_src2.is_const) {
1385 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001386 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 }
1388 }
1389 if (BadOverlap(rl_src1, rl_dest)) {
1390 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1391 return;
1392 }
1393 DCHECK(rl_src2.is_const);
1394 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1395 uint32_t val_lo = Low32Bits(val);
1396 uint32_t val_hi = High32Bits(val);
1397 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1398 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1399
1400 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001401 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 case Instruction::ADD_LONG:
1403 case Instruction::ADD_LONG_2ADDR:
1404 case Instruction::SUB_LONG:
1405 case Instruction::SUB_LONG_2ADDR:
1406 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1407 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1408 return;
1409 }
1410 break;
1411 default:
1412 break;
1413 }
1414 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1415 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1416 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1417 switch (opcode) {
1418 case Instruction::ADD_LONG:
1419 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001420 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001421 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 break;
1423 case Instruction::OR_LONG:
1424 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001425 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1426 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001428 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001429 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 }
1431 break;
1432 case Instruction::XOR_LONG:
1433 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001434 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1435 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 break;
1437 case Instruction::AND_LONG:
1438 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001439 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1440 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001442 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001443 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 }
1445 break;
1446 case Instruction::SUB_LONG_2ADDR:
1447 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001448 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001449 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001450 break;
1451 default:
1452 LOG(FATAL) << "Unexpected opcode " << opcode;
1453 }
1454 StoreValueWide(rl_dest, rl_result);
1455}
1456
1457} // namespace art