blob: 2556788bede577523aef37d9b242e913b7e76069 [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
Andreas Gampeb14329f2014-05-15 11:16:06 -070070void ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
71 int mask;
72 int mask3 = 0;
73 int mask2 = 0;
74 int mask1 = 0;
75 ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
76 int cond_bit = code & 1;
77 int alt_bit = cond_bit ^ 1;
78
79 // Note: case fallthroughs intentional
80 switch (strlen(new_guide)) {
81 case 3:
82 mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
83 case 2:
84 mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
85 case 1:
86 mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
87 break;
88 case 0:
89 break;
90 default:
91 LOG(FATAL) << "OAT: bad case in UpdateIT";
92 }
93 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
94 (1 << (3 - strlen(new_guide)));
95 it->operands[1] = mask;
96}
97
Dave Allison3da67a52014-04-02 17:03:45 -070098void ArmMir2Lir::OpEndIT(LIR* it) {
99 // TODO: use the 'it' pointer to do some checks with the LIR, for example
100 // we could check that the number of instructions matches the mask
101 // in the IT instruction.
102 CHECK(it != nullptr);
103 GenBarrier();
104}
105
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106/*
107 * 64-bit 3way compare function.
108 * mov rX, #-1
109 * cmp op1hi, op2hi
110 * blt done
111 * bgt flip
112 * sub rX, op1lo, op2lo (treat as unsigned)
113 * beq done
114 * ite hi
115 * mov(hi) rX, #-1
116 * mov(!hi) rX, #1
117 * flip:
118 * neg rX
119 * done:
120 */
buzbeea1983d42014-04-07 12:35:39 -0700121void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 LIR* target1;
123 LIR* target2;
124 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
125 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800126 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800128 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 LIR* branch1 = OpCondBranch(kCondLt, NULL);
130 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700131 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 LIR* branch3 = OpCondBranch(kCondEq, NULL);
133
Dave Allison3da67a52014-04-02 17:03:45 -0700134 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800135 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700137 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138
139 target2 = NewLIR0(kPseudoTargetLabel);
140 OpRegReg(kOpNeg, t_reg, t_reg);
141
142 target1 = NewLIR0(kPseudoTargetLabel);
143
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700144 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800145 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 StoreValue(rl_dest, rl_temp);
147 FreeTemp(t_reg);
148
149 branch1->target = target1;
150 branch2->target = target2;
151 branch3->target = branch1->target;
152}
153
154void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700155 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 int32_t val_lo = Low32Bits(val);
157 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700158 DCHECK_GE(ModifiedImmediate(val_lo), 0);
159 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700160 LIR* taken = &block_label_list_[bb->taken];
161 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800163 RegStorage low_reg = rl_src1.reg.GetLow();
164 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165
Vladimir Marko58af1f92013-12-19 13:31:15 +0000166 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800167 RegStorage t_reg = AllocTemp();
168 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000169 FreeTemp(t_reg);
170 OpCondBranch(ccode, taken);
171 return;
172 }
173
Brian Carlstromdf629502013-07-17 22:39:56 -0700174 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 case kCondEq:
176 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000177 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 break;
179 case kCondLt:
180 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
181 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000182 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 break;
184 case kCondLe:
185 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
186 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
187 ccode = kCondLs;
188 break;
189 case kCondGt:
190 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
191 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
192 ccode = kCondHi;
193 break;
194 case kCondGe:
195 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
196 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000197 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 break;
199 default:
200 LOG(FATAL) << "Unexpected ccode: " << ccode;
201 }
202 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
203}
204
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700205void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 RegLocation rl_result;
207 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 RegLocation rl_dest = mir_graph_->GetDest(mir);
209 rl_src = LoadValue(rl_src, kCoreReg);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000210 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 if (mir->ssa_rep->num_uses == 1) {
212 // CONST case
213 int true_val = mir->dalvikInsn.vB;
214 int false_val = mir->dalvikInsn.vC;
215 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000216 // Change kCondNe to kCondEq for the special cases below.
217 if (ccode == kCondNe) {
218 ccode = kCondEq;
219 std::swap(true_val, false_val);
220 }
221 bool cheap_false_val = InexpensiveConstantInt(false_val);
222 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800223 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000224 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700225 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800226 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700227 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000228 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800229 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000230 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700231 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800232 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700233 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000234 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800235 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700236 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800237 LoadConstant(rl_result.reg, true_val);
238 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700239 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 } else {
241 // Unlikely case - could be tuned.
buzbee2700f7e2014-03-07 09:46:20 -0800242 RegStorage t_reg1 = AllocTemp();
243 RegStorage t_reg2 = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 LoadConstant(t_reg1, true_val);
245 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800246 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700247 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800248 OpRegCopy(rl_result.reg, t_reg1);
249 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700250 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 }
252 } else {
253 // MOVE case
254 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
255 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
256 rl_true = LoadValue(rl_true, kCoreReg);
257 rl_false = LoadValue(rl_false, kCoreReg);
258 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800259 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700260 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000261 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700262 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000264 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700265 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800266 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700267 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700268 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800269 OpRegCopy(rl_result.reg, rl_true.reg);
270 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700271 }
Dave Allison3da67a52014-04-02 17:03:45 -0700272 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 }
274 StoreValue(rl_dest, rl_result);
275}
276
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700277void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
279 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
280 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000281 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000283 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 ccode = FlipComparisonOrder(ccode);
285 }
286 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700287 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 // Do special compare/branch against simple const operand if not already in registers.
289 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700290 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
292 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
293 return;
294 }
295 }
buzbee0d829482013-10-11 15:24:55 -0700296 LIR* taken = &block_label_list_[bb->taken];
297 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
299 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800300 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700301 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 case kCondEq:
303 OpCondBranch(kCondNe, not_taken);
304 break;
305 case kCondNe:
306 OpCondBranch(kCondNe, taken);
307 break;
308 case kCondLt:
309 OpCondBranch(kCondLt, taken);
310 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000311 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 break;
313 case kCondLe:
314 OpCondBranch(kCondLt, taken);
315 OpCondBranch(kCondGt, not_taken);
316 ccode = kCondLs;
317 break;
318 case kCondGt:
319 OpCondBranch(kCondGt, taken);
320 OpCondBranch(kCondLt, not_taken);
321 ccode = kCondHi;
322 break;
323 case kCondGe:
324 OpCondBranch(kCondGt, taken);
325 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000326 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 break;
328 default:
329 LOG(FATAL) << "Unexpected ccode: " << ccode;
330 }
buzbee2700f7e2014-03-07 09:46:20 -0800331 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 OpCondBranch(ccode, taken);
333}
334
335/*
336 * Generate a register comparison to an immediate and branch. Caller
337 * is responsible for setting branch target field.
338 */
buzbee2700f7e2014-03-07 09:46:20 -0800339LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 LIR* branch;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700342 /*
343 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
344 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700345 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700346 * be converted to a long form during assembly (which will trigger another assembly
347 * pass). Here we estimate the branch distance for checks, and if large directly
348 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700349 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700350 */
351 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
352 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
buzbee091cc402014-03-31 10:14:40 -0700353 if (!skip && reg.Low8() && (check_value == 0) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 ((arm_cond == kArmCondEq) || (arm_cond == kArmCondNe))) {
355 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
buzbee2700f7e2014-03-07 09:46:20 -0800356 reg.GetReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 } else {
Vladimir Marko22479842013-11-19 17:04:50 +0000358 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 branch = NewLIR2(kThumbBCond, 0, arm_cond);
360 }
361 branch->target = target;
362 return branch;
363}
364
buzbee2700f7e2014-03-07 09:46:20 -0800365LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 LIR* res;
367 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800368 // If src or dest is a pair, we'll be using low reg.
369 if (r_dest.IsPair()) {
370 r_dest = r_dest.GetLow();
371 }
372 if (r_src.IsPair()) {
373 r_src = r_src.GetLow();
374 }
buzbee091cc402014-03-31 10:14:40 -0700375 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700377 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700379 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700381 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 opcode = kThumbMovRR_H2L;
383 else
384 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800385 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
387 res->flags.is_nop = true;
388 }
389 return res;
390}
391
buzbee7a11ab02014-04-28 20:02:38 -0700392void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
393 if (r_dest != r_src) {
394 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
395 AppendLIR(res);
396 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397}
398
buzbee2700f7e2014-03-07 09:46:20 -0800399void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700400 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700401 bool dest_fp = r_dest.IsFloat();
402 bool src_fp = r_src.IsFloat();
403 DCHECK(r_dest.Is64Bit());
404 DCHECK(r_src.Is64Bit());
buzbee7a11ab02014-04-28 20:02:38 -0700405 if (dest_fp) {
406 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700407 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 } else {
buzbee091cc402014-03-31 10:14:40 -0700409 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700410 }
411 } else {
412 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700413 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700414 } else {
415 // Handle overlap
416 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
417 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
418 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
419 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
420 } else {
421 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
422 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
423 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
425 }
426 }
427}
428
429// Table of magic divisors
430struct MagicTable {
431 uint32_t magic;
432 uint32_t shift;
433 DividePattern pattern;
434};
435
436static const MagicTable magic_table[] = {
437 {0, 0, DivideNone}, // 0
438 {0, 0, DivideNone}, // 1
439 {0, 0, DivideNone}, // 2
440 {0x55555556, 0, Divide3}, // 3
441 {0, 0, DivideNone}, // 4
442 {0x66666667, 1, Divide5}, // 5
443 {0x2AAAAAAB, 0, Divide3}, // 6
444 {0x92492493, 2, Divide7}, // 7
445 {0, 0, DivideNone}, // 8
446 {0x38E38E39, 1, Divide5}, // 9
447 {0x66666667, 2, Divide5}, // 10
448 {0x2E8BA2E9, 1, Divide5}, // 11
449 {0x2AAAAAAB, 1, Divide5}, // 12
450 {0x4EC4EC4F, 2, Divide5}, // 13
451 {0x92492493, 3, Divide7}, // 14
452 {0x88888889, 3, Divide7}, // 15
453};
454
455// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700456bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700457 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
459 return false;
460 }
461 DividePattern pattern = magic_table[lit].pattern;
462 if (pattern == DivideNone) {
463 return false;
464 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465
buzbee2700f7e2014-03-07 09:46:20 -0800466 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 LoadConstant(r_magic, magic_table[lit].magic);
468 rl_src = LoadValue(rl_src, kCoreReg);
469 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800470 RegStorage r_hi = AllocTemp();
471 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100472
473 // rl_dest and rl_src might overlap.
474 // Reuse r_hi to save the div result for reminder case.
475 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
476
buzbee2700f7e2014-03-07 09:46:20 -0800477 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700478 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100480 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 break;
482 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800483 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100484 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700485 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 break;
487 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800488 OpRegReg(kOpAdd, r_hi, rl_src.reg);
489 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100490 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700491 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 break;
493 default:
494 LOG(FATAL) << "Unexpected pattern: " << pattern;
495 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100496
497 if (!is_div) {
498 // div_result = src / lit
499 // tmp1 = div_result * lit
500 // dest = src - tmp1
501 RegStorage tmp1 = r_lo;
502 EasyMultiplyOp ops[2];
503
504 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
505 DCHECK_NE(canEasyMultiply, false);
506
507 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
508 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
509 }
510
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 StoreValue(rl_dest, rl_result);
512 return true;
513}
514
Ian Rogerse2143c02014-03-28 08:47:16 -0700515// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
516bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
517 if (IsPowerOfTwo(lit)) {
518 op->op = kOpLsl;
519 op->shift = LowestSetBit(lit);
520 return true;
521 }
522
523 if (IsPowerOfTwo(lit - 1)) {
524 op->op = kOpAdd;
525 op->shift = LowestSetBit(lit - 1);
526 return true;
527 }
528
529 if (IsPowerOfTwo(lit + 1)) {
530 op->op = kOpRsub;
531 op->shift = LowestSetBit(lit + 1);
532 return true;
533 }
534
535 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100536 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700537 return false;
538}
539
540// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
541bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
542 GetEasyMultiplyOp(lit, &ops[0]);
543 if (GetEasyMultiplyOp(lit, &ops[0])) {
544 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100545 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700546 return true;
547 }
548
549 int lit1 = lit;
550 uint32_t shift = LowestSetBit(lit1);
551 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
552 ops[1].op = kOpLsl;
553 ops[1].shift = shift;
554 return true;
555 }
556
557 lit1 = lit - 1;
558 shift = LowestSetBit(lit1);
559 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
560 ops[1].op = kOpAdd;
561 ops[1].shift = shift;
562 return true;
563 }
564
565 lit1 = lit + 1;
566 shift = LowestSetBit(lit1);
567 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
568 ops[1].op = kOpRsub;
569 ops[1].shift = shift;
570 return true;
571 }
572
573 return false;
574}
575
Zheng Xuf9719f92014-04-02 13:31:31 +0100576// Generate instructions to do multiply.
577// Additional temporary register is required,
578// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700579void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100580 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
581 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
582
583 RegStorage r_tmp1;
584 if (ops[1].op == kOpInvalid) {
585 r_tmp1 = r_dest;
586 } else if (r_dest.GetReg() != r_src.GetReg()) {
587 r_tmp1 = r_dest;
588 } else {
589 r_tmp1 = AllocTemp();
590 }
591
592 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700593 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100594 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700595 break;
596 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100597 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700598 break;
599 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100600 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700601 break;
602 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100603 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700604 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100605 }
606
607 switch (ops[1].op) {
608 case kOpInvalid:
609 return;
610 case kOpLsl:
611 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
612 break;
613 case kOpAdd:
614 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
615 break;
616 case kOpRsub:
617 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
618 break;
619 default:
620 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
621 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700622 }
623}
624
625bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
626 EasyMultiplyOp ops[2];
627
628 if (!GetEasyMultiplyTwoOps(lit, ops)) {
629 return false;
630 }
631
632 rl_src = LoadValue(rl_src, kCoreReg);
633 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
634
635 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
636 StoreValue(rl_dest, rl_result);
637 return true;
638}
639
Mark Mendell2bf31e62014-01-23 12:13:40 -0800640RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
641 RegLocation rl_src2, bool is_div, bool check_zero) {
642 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
643 return rl_dest;
644}
645
646RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
647 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
648 return rl_dest;
649}
650
buzbee2700f7e2014-03-07 09:46:20 -0800651RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700652 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
653
654 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800655 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700656 LoadConstant(lit_temp, lit);
657 // Use the generic case for div/rem with arg2 in a register.
658 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
659 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
660 FreeTemp(lit_temp);
661
662 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663}
664
buzbee2700f7e2014-03-07 09:46:20 -0800665RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700666 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700667 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
668 if (is_div) {
669 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800670 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700671 } else {
672 // Remainder case, use the following code:
673 // temp = reg1 / reg2 - integer division
674 // temp = temp * reg2
675 // dest = reg1 - temp
676
buzbee2700f7e2014-03-07 09:46:20 -0800677 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700678 OpRegRegReg(kOpDiv, temp, reg1, reg2);
679 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800680 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700681 FreeTemp(temp);
682 }
683
684 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685}
686
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700687bool ArmMir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 DCHECK_EQ(cu_->instruction_set, kThumb2);
689 RegLocation rl_src1 = info->args[0];
690 RegLocation rl_src2 = info->args[1];
691 rl_src1 = LoadValue(rl_src1, kCoreReg);
692 rl_src2 = LoadValue(rl_src2, kCoreReg);
693 RegLocation rl_dest = InlineTarget(info);
694 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800695 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700696 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800697 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
698 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700699 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 StoreValue(rl_dest, rl_result);
701 return true;
702}
703
Vladimir Markoe508a202013-11-04 15:24:22 +0000704bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
705 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800706 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000707 RegLocation rl_dest = InlineTarget(info);
708 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
709 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700710 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000711 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800712 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700713 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
714 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000715 } else {
buzbee695d13a2014-04-19 13:32:20 -0700716 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
717 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000718 }
719 StoreValueWide(rl_dest, rl_result);
720 } else {
buzbee695d13a2014-04-19 13:32:20 -0700721 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000722 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100723 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000724 StoreValue(rl_dest, rl_result);
725 }
726 return true;
727}
728
729bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
730 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800731 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000732 RegLocation rl_src_value = info->args[2]; // [size] value
733 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700734 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000735 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
736 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700737 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32);
738 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000739 } else {
buzbee695d13a2014-04-19 13:32:20 -0700740 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000741 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
742 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800743 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size);
Vladimir Markoe508a202013-11-04 15:24:22 +0000744 }
745 return true;
746}
747
buzbee2700f7e2014-03-07 09:46:20 -0800748void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 LOG(FATAL) << "Unexpected use of OpLea for Arm";
750}
751
Ian Rogersdd7624d2014-03-14 17:43:00 -0700752void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
754}
755
Andreas Gampe2f244e92014-05-08 03:35:25 -0700756void ArmMir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
757 UNIMPLEMENTED(FATAL) << "Should not be called.";
758}
759
Vladimir Marko1c282e22013-11-21 14:49:47 +0000760bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 DCHECK_EQ(cu_->instruction_set, kThumb2);
762 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000763 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
764 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800765 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000766 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000767 // If is_long, high half is in info->args[5]
768 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
769 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 RegLocation rl_dest = InlineTarget(info); // boolean place for result
771
Vladimir Marko3e5af822013-11-21 15:01:20 +0000772 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
773 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
774 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
775 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
776 // into the same temps, reducing the number of required temps down to 5. We shall work
777 // around the potentially locked temp by using LR for r_ptr, unconditionally.
778 // TODO: Pass information about the need for more temps to the stack frame generation
779 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700780 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
781 MarkTemp(rs_rARM_LR);
782 FreeTemp(rs_rARM_LR);
783 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000784 bool load_early = true;
785 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700786 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
787 rl_src_expected.reg;
788 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
789 rl_src_new_value.reg;
790 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
791 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800792 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
793 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000794
795 if (!expected_is_good_reg && !new_value_is_good_reg) {
796 // None of expected/new_value is non-temp reg, need to load both late
797 load_early = false;
798 // Make sure they are not in the temp regs and the load will not be skipped.
799 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800800 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000801 ClobberSReg(rl_src_expected.s_reg_low);
802 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
803 rl_src_expected.location = kLocDalvikFrame;
804 }
805 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800806 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000807 ClobberSReg(rl_src_new_value.s_reg_low);
808 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
809 rl_src_new_value.location = kLocDalvikFrame;
810 }
811 }
812 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813
814 // Release store semantics, get the barrier out of the way. TODO: revisit
815 GenMemBarrier(kStoreLoad);
816
817 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000818 RegLocation rl_new_value;
819 if (!is_long) {
820 rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
821 } else if (load_early) {
822 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
823 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700824
Vladimir Marko1c282e22013-11-21 14:49:47 +0000825 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800827 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700828 }
829
830 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
831
buzbee2700f7e2014-03-07 09:46:20 -0800832 RegStorage r_ptr = rs_rARM_LR;
833 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700834
835 // Free now unneeded rl_object and rl_offset to give more temps.
836 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700837 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700839 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840
Vladimir Marko3e5af822013-11-21 15:01:20 +0000841 RegLocation rl_expected;
842 if (!is_long) {
843 rl_expected = LoadValue(rl_src_expected, kCoreReg);
844 } else if (load_early) {
845 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
846 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000847 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700848 RegStorage low_reg = AllocTemp();
849 RegStorage high_reg = AllocTemp();
850 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000851 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000852 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853
Vladimir Marko3e5af822013-11-21 15:01:20 +0000854 // do {
855 // tmp = [r_ptr] - expected;
856 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
857 // result = tmp != 0;
858
buzbee2700f7e2014-03-07 09:46:20 -0800859 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700860 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700861
Dave Allison3da67a52014-04-02 17:03:45 -0700862 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000863 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800864 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000865 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800866 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000867 }
buzbee2700f7e2014-03-07 09:46:20 -0800868 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
869 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
870 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000871 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800872 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000873 }
874 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700875 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800876 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000877 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800878 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000879 }
880 FreeTemp(r_tmp_high); // Now unneeded
881
882 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700883 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800884 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 +0000885
886 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800887 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
888 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000889 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700890 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800891 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000892 }
893
894 // Still one conditional left from OpIT(kCondEq, "T") from either branch
895 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700896 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700897
Jeff Hao2de2aa12013-09-12 17:20:31 -0700898 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899
Vladimir Marko3e5af822013-11-21 15:01:20 +0000900 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800901 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000902 }
903
904 // result := (tmp1 != 0) ? 0 : 1;
905 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800906 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000907 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Dave Allison3da67a52014-04-02 17:03:45 -0700908 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800909 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000910 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700911 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000912
Brian Carlstrom7940e442013-07-12 13:46:57 -0700913 StoreValue(rl_dest, rl_result);
914
Vladimir Marko3e5af822013-11-21 15:01:20 +0000915 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700916 Clobber(rs_rARM_LR);
917 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 return true;
919}
920
buzbee2700f7e2014-03-07 09:46:20 -0800921LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
922 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923}
924
buzbee2700f7e2014-03-07 09:46:20 -0800925LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -0700926 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927}
928
buzbee2700f7e2014-03-07 09:46:20 -0800929LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -0700930 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931}
932
933void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
934 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700935 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700936 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 EncodeShift(kArmLsl, second_bit - first_bit));
938 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800939 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 }
941}
942
Mingyao Yange643a172014-04-08 11:02:52 -0700943void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800944 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
945 RegStorage t_reg = AllocTemp();
946 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -0700948 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949}
950
951// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700952LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
buzbee091cc402014-03-31 10:14:40 -0700953 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
955}
956
957// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800958LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000960 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
961 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 return OpCondBranch(c_code, target);
963}
964
Andreas Gampeb14329f2014-05-15 11:16:06 -0700965bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800967 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
968 LIR* barrier = last_lir_insn_;
969
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 int dmb_flavor;
971 // TODO: revisit Arm barrier kinds
972 switch (barrier_kind) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800973 case kLoadStore: dmb_flavor = kISH; break;
974 case kLoadLoad: dmb_flavor = kISH; break;
975 case kStoreStore: dmb_flavor = kISHST; break;
976 case kStoreLoad: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 default:
978 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
979 dmb_flavor = kSY; // quiet gcc.
980 break;
981 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800982
Andreas Gampeb14329f2014-05-15 11:16:06 -0700983 bool ret = false;
984
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800985 // If the same barrier already exists, don't generate another.
986 if (barrier == nullptr
987 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
988 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700989 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800990 }
991
992 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
993 DCHECK(!barrier->flags.use_def_invalid);
994 barrier->u.m.def_mask = ENCODE_ALL;
Andreas Gampeb14329f2014-05-15 11:16:06 -0700995 return ret;
996#else
997 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998#endif
999}
1000
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001001void ArmMir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
1002 LOG(FATAL) << "Unexpected use GenNotLong()";
1003}
1004
1005void ArmMir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
1006 RegLocation rl_src2, bool is_div) {
1007 LOG(FATAL) << "Unexpected use GenDivRemLong()";
1008}
1009
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001010void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 rl_src = LoadValueWide(rl_src, kCoreReg);
1012 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001013 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 LoadConstantNoClobber(z_reg, 0);
1015 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001016 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1017 RegStorage t_reg = AllocTemp();
1018 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1019 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 FreeTemp(t_reg);
1021 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001022 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1023 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
1025 FreeTemp(z_reg);
1026 StoreValueWide(rl_dest, rl_result);
1027}
1028
Mark Mendelle02d48f2014-01-15 11:19:23 -08001029void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1030 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 /*
Zheng Xud7f8e022014-03-13 13:40:30 +00001032 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1033 * dest = src1.lo * src2.lo;
1034 * tmp1 += src1.lo * src2.hi;
1035 * dest.hi += tmp1;
1036 *
1037 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 * registers. Normally for Arm, we get 5. We can get to 6 by including
1039 * lr in the temp set. The only problematic case is all operands and result are
1040 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1041 * freeing operand temp registers after they are no longer needed. All other cases
1042 * can proceed normally. We'll just punt on the case of the result having a misaligned
1043 * overlap with either operand and send that case to a runtime handler.
1044 */
1045 RegLocation rl_result;
1046 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001047 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 FlushAllRegs();
1049 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1050 rl_result = GetReturnWide(false);
1051 StoreValueWide(rl_dest, rl_result);
1052 return;
1053 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001054
1055 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1056 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1057
1058 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001059 RegStorage res_lo;
1060 RegStorage res_hi;
1061 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
buzbee091cc402014-03-31 10:14:40 -07001062 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1063 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1064 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001065 // Check if rl_dest is *not* either operand and we have enough temp registers.
1066 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1067 (dest_promoted || src1_promoted || src2_promoted)) {
1068 // In this case, we do not need to manually allocate temp registers for result.
1069 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001070 res_lo = rl_result.reg.GetLow();
1071 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001072 } else {
1073 res_lo = AllocTemp();
1074 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1075 // In this case, we have enough temp registers to be allocated for result.
1076 res_hi = AllocTemp();
1077 reg_status = 1;
1078 } else {
1079 // In this case, all temps are now allocated.
1080 // res_hi will be allocated after we can free src1_hi.
1081 reg_status = 2;
1082 }
1083 }
1084
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 // Temporarily add LR to the temp pool, and assign it to tmp1
buzbee091cc402014-03-31 10:14:40 -07001086 MarkTemp(rs_rARM_LR);
1087 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001088 RegStorage tmp1 = rs_rARM_LR;
buzbee091cc402014-03-31 10:14:40 -07001089 LockTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090
buzbee2700f7e2014-03-07 09:46:20 -08001091 if (rl_src1.reg == rl_src2.reg) {
1092 DCHECK(res_hi.Valid());
1093 DCHECK(res_lo.Valid());
1094 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1095 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1096 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001097 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001099 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001100 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001101 DCHECK(!res_hi.Valid());
1102 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001103 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
buzbee082833c2014-05-17 23:16:26 -07001104 // Will force free src1_hi, so must clobber.
1105 Clobber(rl_src1.reg);
buzbee091cc402014-03-31 10:14:40 -07001106 FreeTemp(rl_src1.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001107 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 }
buzbee2700f7e2014-03-07 09:46:20 -08001109 DCHECK(res_hi.Valid());
1110 DCHECK(res_lo.Valid());
1111 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1112 rl_src1.reg.GetLowReg());
1113 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1114 tmp1.GetReg());
1115 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001116 if (reg_status == 2) {
buzbee082833c2014-05-17 23:16:26 -07001117 FreeTemp(rl_src1.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 }
1119 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001120
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001122 FreeTemp(tmp1);
buzbee091cc402014-03-31 10:14:40 -07001123 Clobber(rs_rARM_LR);
1124 UnmarkTemp(rs_rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001125
1126 if (reg_status != 0) {
1127 // We had manually allocated registers for rl_result.
1128 // Now construct a RegLocation.
1129 rl_result = GetReturnWide(false); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001130 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001131 }
1132
1133 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134}
1135
Mark Mendelle02d48f2014-01-15 11:19:23 -08001136void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001137 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1139}
1140
Mark Mendelle02d48f2014-01-15 11:19:23 -08001141void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001142 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1144}
1145
Mark Mendelle02d48f2014-01-15 11:19:23 -08001146void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001147 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1149}
1150
Mark Mendelle02d48f2014-01-15 11:19:23 -08001151void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001152 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1154}
1155
Mark Mendelle02d48f2014-01-15 11:19:23 -08001156void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1159}
1160
1161/*
1162 * Generate array load
1163 */
1164void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001165 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001166 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 int len_offset = mirror::Array::LengthOffset().Int32Value();
1168 int data_offset;
1169 RegLocation rl_result;
1170 bool constant_index = rl_index.is_const;
1171 rl_array = LoadValue(rl_array, kCoreReg);
1172 if (!constant_index) {
1173 rl_index = LoadValue(rl_index, kCoreReg);
1174 }
1175
1176 if (rl_dest.wide) {
1177 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1178 } else {
1179 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1180 }
1181
1182 // If index is constant, just fold it into the data offset
1183 if (constant_index) {
1184 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1185 }
1186
1187 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001188 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189
1190 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001191 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 if (needs_range_check) {
1193 reg_len = AllocTemp();
1194 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001195 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001196 MarkPossibleNullPointerException(opt_flags);
1197 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001198 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 }
1200 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001201 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001203 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 } else {
1205 // No special indexed operation, lea + load w/ displacement
1206 reg_ptr = AllocTemp();
Ian Rogerse2143c02014-03-28 08:47:16 -07001207 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001208 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001209 }
1210 rl_result = EvalLoc(rl_dest, reg_class, true);
1211
1212 if (needs_range_check) {
1213 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001214 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001216 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 }
1218 FreeTemp(reg_len);
1219 }
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001220 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size);
Vladimir Marko455759b2014-05-06 20:49:36 +01001221 MarkPossibleNullPointerException(opt_flags);
1222 if (!constant_index) {
1223 FreeTemp(reg_ptr);
1224 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001226 StoreValueWide(rl_dest, rl_result);
1227 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 StoreValue(rl_dest, rl_result);
1229 }
1230 } else {
1231 // Offset base, then use indexed load
buzbee2700f7e2014-03-07 09:46:20 -08001232 RegStorage reg_ptr = AllocTemp();
1233 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001234 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 rl_result = EvalLoc(rl_dest, reg_class, true);
1236
1237 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001238 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 FreeTemp(reg_len);
1240 }
buzbee2700f7e2014-03-07 09:46:20 -08001241 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001242 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 FreeTemp(reg_ptr);
1244 StoreValue(rl_dest, rl_result);
1245 }
1246}
1247
1248/*
1249 * Generate array store
1250 *
1251 */
1252void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001253 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001254 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 bool constant_index = rl_index.is_const;
1257
Ian Rogersa9a82542013-10-04 11:17:26 -07001258 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001259 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1261 } else {
1262 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1263 }
1264
1265 // If index is constant, just fold it into the data offset.
1266 if (constant_index) {
1267 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1268 }
1269
1270 rl_array = LoadValue(rl_array, kCoreReg);
1271 if (!constant_index) {
1272 rl_index = LoadValue(rl_index, kCoreReg);
1273 }
1274
buzbee2700f7e2014-03-07 09:46:20 -08001275 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001276 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001278 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001279 } else if (IsTemp(rl_array.reg) && !card_mark) {
1280 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001281 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001283 allocated_reg_ptr_temp = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 reg_ptr = AllocTemp();
1285 }
1286
1287 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001288 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289
1290 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001291 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 if (needs_range_check) {
1293 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001294 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001295 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001296 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001297 MarkPossibleNullPointerException(opt_flags);
1298 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001299 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
1301 /* at this point, reg_ptr points to array, 2 live temps */
1302 if (rl_src.wide || rl_src.fp || constant_index) {
1303 if (rl_src.wide) {
1304 rl_src = LoadValueWide(rl_src, reg_class);
1305 } else {
1306 rl_src = LoadValue(rl_src, reg_class);
1307 }
1308 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001309 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 }
1311 if (needs_range_check) {
1312 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001313 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001315 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 }
1317 FreeTemp(reg_len);
1318 }
1319
Vladimir Marko455759b2014-05-06 20:49:36 +01001320 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001321 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 } else {
1323 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001324 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 rl_src = LoadValue(rl_src, reg_class);
1326 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001327 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 FreeTemp(reg_len);
1329 }
buzbee2700f7e2014-03-07 09:46:20 -08001330 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001331 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
Ian Rogers773aab12013-10-14 13:50:10 -07001333 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 FreeTemp(reg_ptr);
1335 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001336 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001337 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 }
1339}
1340
Ian Rogersa9a82542013-10-04 11:17:26 -07001341
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001343 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 rl_src = LoadValueWide(rl_src, kCoreReg);
1345 // Per spec, we only care about low 6 bits of shift amount.
1346 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1347 if (shift_amount == 0) {
1348 StoreValueWide(rl_dest, rl_src);
1349 return;
1350 }
1351 if (BadOverlap(rl_src, rl_dest)) {
1352 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1353 return;
1354 }
1355 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001356 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 case Instruction::SHL_LONG:
1358 case Instruction::SHL_LONG_2ADDR:
1359 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001360 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1361 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001363 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1364 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001366 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1367 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001369 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001370 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001372 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 }
1374 break;
1375 case Instruction::SHR_LONG:
1376 case Instruction::SHR_LONG_2ADDR:
1377 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001378 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1379 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001381 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1382 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001384 RegStorage t_reg = AllocTemp();
1385 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001386 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 EncodeShift(kArmLsl, 32 - shift_amount));
1388 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001389 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
1391 break;
1392 case Instruction::USHR_LONG:
1393 case Instruction::USHR_LONG_2ADDR:
1394 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001395 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1396 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001398 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1399 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001401 RegStorage t_reg = AllocTemp();
1402 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001403 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 EncodeShift(kArmLsl, 32 - shift_amount));
1405 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001406 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 }
1408 break;
1409 default:
1410 LOG(FATAL) << "Unexpected case";
1411 }
1412 StoreValueWide(rl_dest, rl_result);
1413}
1414
1415void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001416 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1418 if (!rl_src2.is_const) {
1419 // Don't bother with special handling for subtract from immediate.
1420 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1421 return;
1422 }
1423 } else {
1424 // Normalize
1425 if (!rl_src2.is_const) {
1426 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001427 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 }
1429 }
1430 if (BadOverlap(rl_src1, rl_dest)) {
1431 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1432 return;
1433 }
1434 DCHECK(rl_src2.is_const);
1435 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1436 uint32_t val_lo = Low32Bits(val);
1437 uint32_t val_hi = High32Bits(val);
1438 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1439 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1440
1441 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001442 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 case Instruction::ADD_LONG:
1444 case Instruction::ADD_LONG_2ADDR:
1445 case Instruction::SUB_LONG:
1446 case Instruction::SUB_LONG_2ADDR:
1447 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1448 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1449 return;
1450 }
1451 break;
1452 default:
1453 break;
1454 }
1455 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1456 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1457 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1458 switch (opcode) {
1459 case Instruction::ADD_LONG:
1460 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001461 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001462 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 break;
1464 case Instruction::OR_LONG:
1465 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001466 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1467 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001469 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001470 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 }
1472 break;
1473 case Instruction::XOR_LONG:
1474 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001475 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1476 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 break;
1478 case Instruction::AND_LONG:
1479 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001480 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1481 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001483 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001484 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 }
1486 break;
1487 case Instruction::SUB_LONG_2ADDR:
1488 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001489 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001490 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 break;
1492 default:
1493 LOG(FATAL) << "Unexpected opcode " << opcode;
1494 }
1495 StoreValueWide(rl_dest, rl_result);
1496}
1497
1498} // namespace art