blob: 2fcc3a5abc09142d52efcee2ecb16980dcdea65e [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"
buzbeeb5860fb2014-06-21 15:31:01 -070022#include "dex/reg_storage_eq.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "mirror/array.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025
26namespace art {
27
buzbee2700f7e2014-03-07 09:46:20 -080028LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070029 OpRegReg(kOpCmp, src1, src2);
30 return OpCondBranch(cond, target);
31}
32
33/*
34 * Generate a Thumb2 IT instruction, which can nullify up to
35 * four subsequent instructions based on a condition and its
36 * inverse. The condition applies to the first instruction, which
37 * is executed if the condition is met. The string "guide" consists
38 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
39 * A "T" means the instruction is executed if the condition is
40 * met, and an "E" means the instruction is executed if the condition
41 * is not met.
42 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 int mask;
45 int mask3 = 0;
46 int mask2 = 0;
47 int mask1 = 0;
48 ArmConditionCode code = ArmConditionEncoding(ccode);
49 int cond_bit = code & 1;
50 int alt_bit = cond_bit ^ 1;
51
Brian Carlstrom7934ac22013-07-26 10:54:15 -070052 // Note: case fallthroughs intentional
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 switch (strlen(guide)) {
54 case 3:
55 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
56 case 2:
57 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
58 case 1:
59 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
60 break;
61 case 0:
62 break;
63 default:
64 LOG(FATAL) << "OAT: bad case in OpIT";
65 }
66 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
67 (1 << (3 - strlen(guide)));
68 return NewLIR2(kThumb2It, code, mask);
69}
70
Andreas Gampeb14329f2014-05-15 11:16:06 -070071void ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
72 int mask;
73 int mask3 = 0;
74 int mask2 = 0;
75 int mask1 = 0;
76 ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
77 int cond_bit = code & 1;
78 int alt_bit = cond_bit ^ 1;
79
80 // Note: case fallthroughs intentional
81 switch (strlen(new_guide)) {
82 case 3:
83 mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
84 case 2:
85 mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
86 case 1:
87 mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
88 break;
89 case 0:
90 break;
91 default:
92 LOG(FATAL) << "OAT: bad case in UpdateIT";
93 }
94 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
95 (1 << (3 - strlen(new_guide)));
96 it->operands[1] = mask;
97}
98
Dave Allison3da67a52014-04-02 17:03:45 -070099void ArmMir2Lir::OpEndIT(LIR* it) {
100 // TODO: use the 'it' pointer to do some checks with the LIR, for example
101 // we could check that the number of instructions matches the mask
102 // in the IT instruction.
103 CHECK(it != nullptr);
104 GenBarrier();
105}
106
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107/*
108 * 64-bit 3way compare function.
109 * mov rX, #-1
110 * cmp op1hi, op2hi
111 * blt done
112 * bgt flip
113 * sub rX, op1lo, op2lo (treat as unsigned)
114 * beq done
115 * ite hi
116 * mov(hi) rX, #-1
117 * mov(!hi) rX, #1
118 * flip:
119 * neg rX
120 * done:
121 */
buzbeea1983d42014-04-07 12:35:39 -0700122void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 LIR* target1;
124 LIR* target2;
125 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
126 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800127 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800129 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 LIR* branch1 = OpCondBranch(kCondLt, NULL);
131 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700132 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 LIR* branch3 = OpCondBranch(kCondEq, NULL);
134
Dave Allison3da67a52014-04-02 17:03:45 -0700135 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800136 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700138 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139
140 target2 = NewLIR0(kPseudoTargetLabel);
141 OpRegReg(kOpNeg, t_reg, t_reg);
142
143 target1 = NewLIR0(kPseudoTargetLabel);
144
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700145 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800146 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 StoreValue(rl_dest, rl_temp);
148 FreeTemp(t_reg);
149
150 branch1->target = target1;
151 branch2->target = target2;
152 branch3->target = branch1->target;
153}
154
155void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700156 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 int32_t val_lo = Low32Bits(val);
158 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700159 DCHECK_GE(ModifiedImmediate(val_lo), 0);
160 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700161 LIR* taken = &block_label_list_[bb->taken];
162 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800164 RegStorage low_reg = rl_src1.reg.GetLow();
165 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166
Vladimir Marko58af1f92013-12-19 13:31:15 +0000167 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800168 RegStorage t_reg = AllocTemp();
169 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000170 FreeTemp(t_reg);
171 OpCondBranch(ccode, taken);
172 return;
173 }
174
Brian Carlstromdf629502013-07-17 22:39:56 -0700175 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 case kCondEq:
177 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000178 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 break;
180 case kCondLt:
181 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
182 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000183 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 break;
185 case kCondLe:
186 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
187 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
188 ccode = kCondLs;
189 break;
190 case kCondGt:
191 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
192 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
193 ccode = kCondHi;
194 break;
195 case kCondGe:
196 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
197 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000198 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 break;
200 default:
201 LOG(FATAL) << "Unexpected ccode: " << ccode;
202 }
203 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
204}
205
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700206void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 RegLocation rl_result;
208 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700210 // Avoid using float regs here.
211 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
212 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
213 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000214 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 if (mir->ssa_rep->num_uses == 1) {
216 // CONST case
217 int true_val = mir->dalvikInsn.vB;
218 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700219 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000220 // Change kCondNe to kCondEq for the special cases below.
221 if (ccode == kCondNe) {
222 ccode = kCondEq;
223 std::swap(true_val, false_val);
224 }
225 bool cheap_false_val = InexpensiveConstantInt(false_val);
226 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800227 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100228 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700229 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800230 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700231 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000232 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800233 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100234 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700235 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800236 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700237 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000238 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800239 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700240 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800241 LoadConstant(rl_result.reg, true_val);
242 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700243 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 } else {
245 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700246 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
247 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 LoadConstant(t_reg1, true_val);
249 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800250 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700251 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800252 OpRegCopy(rl_result.reg, t_reg1);
253 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700254 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 }
256 } else {
257 // MOVE case
258 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
259 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700260 rl_true = LoadValue(rl_true, result_reg_class);
261 rl_false = LoadValue(rl_false, result_reg_class);
262 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700264 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000265 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700266 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800267 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000268 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700269 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800270 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700271 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700272 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800273 OpRegCopy(rl_result.reg, rl_true.reg);
274 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700275 }
Dave Allison3da67a52014-04-02 17:03:45 -0700276 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 }
278 StoreValue(rl_dest, rl_result);
279}
280
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700281void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
283 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
284 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000285 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000287 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 ccode = FlipComparisonOrder(ccode);
289 }
290 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700291 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 // Do special compare/branch against simple const operand if not already in registers.
293 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700294 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
296 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
297 return;
298 }
299 }
buzbee0d829482013-10-11 15:24:55 -0700300 LIR* taken = &block_label_list_[bb->taken];
301 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
303 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800304 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700305 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 case kCondEq:
307 OpCondBranch(kCondNe, not_taken);
308 break;
309 case kCondNe:
310 OpCondBranch(kCondNe, taken);
311 break;
312 case kCondLt:
313 OpCondBranch(kCondLt, taken);
314 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000315 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 break;
317 case kCondLe:
318 OpCondBranch(kCondLt, taken);
319 OpCondBranch(kCondGt, not_taken);
320 ccode = kCondLs;
321 break;
322 case kCondGt:
323 OpCondBranch(kCondGt, taken);
324 OpCondBranch(kCondLt, not_taken);
325 ccode = kCondHi;
326 break;
327 case kCondGe:
328 OpCondBranch(kCondGt, taken);
329 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000330 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 break;
332 default:
333 LOG(FATAL) << "Unexpected ccode: " << ccode;
334 }
buzbee2700f7e2014-03-07 09:46:20 -0800335 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 OpCondBranch(ccode, taken);
337}
338
339/*
340 * Generate a register comparison to an immediate and branch. Caller
341 * is responsible for setting branch target field.
342 */
buzbee2700f7e2014-03-07 09:46:20 -0800343LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700344 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700346 /*
347 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
348 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700349 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700350 * be converted to a long form during assembly (which will trigger another assembly
351 * pass). Here we estimate the branch distance for checks, and if large directly
352 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700353 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700354 */
355 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
356 skip &= ((cu_->code_item->insns_size_in_code_units_ - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700357 if (!skip && reg.Low8() && (check_value == 0)) {
358 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
359 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
360 reg.GetReg(), 0);
361 } else if (arm_cond == kArmCondLs) {
362 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
363 // This case happens for a bounds check of array[0].
364 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
365 }
366 }
367
368 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000369 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 branch = NewLIR2(kThumbBCond, 0, arm_cond);
371 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700372
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 branch->target = target;
374 return branch;
375}
376
buzbee2700f7e2014-03-07 09:46:20 -0800377LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 LIR* res;
379 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800380 // If src or dest is a pair, we'll be using low reg.
381 if (r_dest.IsPair()) {
382 r_dest = r_dest.GetLow();
383 }
384 if (r_src.IsPair()) {
385 r_src = r_src.GetLow();
386 }
buzbee091cc402014-03-31 10:14:40 -0700387 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700389 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700391 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700393 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 opcode = kThumbMovRR_H2L;
395 else
396 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800397 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
399 res->flags.is_nop = true;
400 }
401 return res;
402}
403
buzbee7a11ab02014-04-28 20:02:38 -0700404void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
405 if (r_dest != r_src) {
406 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
407 AppendLIR(res);
408 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409}
410
buzbee2700f7e2014-03-07 09:46:20 -0800411void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700412 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700413 bool dest_fp = r_dest.IsFloat();
414 bool src_fp = r_src.IsFloat();
415 DCHECK(r_dest.Is64Bit());
416 DCHECK(r_src.Is64Bit());
buzbee7a11ab02014-04-28 20:02:38 -0700417 if (dest_fp) {
418 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700419 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 } else {
buzbee091cc402014-03-31 10:14:40 -0700421 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700422 }
423 } else {
424 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700425 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700426 } else {
427 // Handle overlap
428 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
429 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
430 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
431 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
432 } else {
433 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
434 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
435 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 }
437 }
438 }
439}
440
441// Table of magic divisors
442struct MagicTable {
443 uint32_t magic;
444 uint32_t shift;
445 DividePattern pattern;
446};
447
448static const MagicTable magic_table[] = {
449 {0, 0, DivideNone}, // 0
450 {0, 0, DivideNone}, // 1
451 {0, 0, DivideNone}, // 2
452 {0x55555556, 0, Divide3}, // 3
453 {0, 0, DivideNone}, // 4
454 {0x66666667, 1, Divide5}, // 5
455 {0x2AAAAAAB, 0, Divide3}, // 6
456 {0x92492493, 2, Divide7}, // 7
457 {0, 0, DivideNone}, // 8
458 {0x38E38E39, 1, Divide5}, // 9
459 {0x66666667, 2, Divide5}, // 10
460 {0x2E8BA2E9, 1, Divide5}, // 11
461 {0x2AAAAAAB, 1, Divide5}, // 12
462 {0x4EC4EC4F, 2, Divide5}, // 13
463 {0x92492493, 3, Divide7}, // 14
464 {0x88888889, 3, Divide7}, // 15
465};
466
467// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700468bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700469 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
471 return false;
472 }
473 DividePattern pattern = magic_table[lit].pattern;
474 if (pattern == DivideNone) {
475 return false;
476 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477
buzbee2700f7e2014-03-07 09:46:20 -0800478 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 LoadConstant(r_magic, magic_table[lit].magic);
480 rl_src = LoadValue(rl_src, kCoreReg);
481 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800482 RegStorage r_hi = AllocTemp();
483 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100484
485 // rl_dest and rl_src might overlap.
486 // Reuse r_hi to save the div result for reminder case.
487 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
488
buzbee2700f7e2014-03-07 09:46:20 -0800489 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700490 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100492 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 break;
494 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800495 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100496 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700497 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 break;
499 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800500 OpRegReg(kOpAdd, r_hi, rl_src.reg);
501 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100502 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700503 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 break;
505 default:
506 LOG(FATAL) << "Unexpected pattern: " << pattern;
507 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100508
509 if (!is_div) {
510 // div_result = src / lit
511 // tmp1 = div_result * lit
512 // dest = src - tmp1
513 RegStorage tmp1 = r_lo;
514 EasyMultiplyOp ops[2];
515
516 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
517 DCHECK_NE(canEasyMultiply, false);
518
519 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
520 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
521 }
522
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 StoreValue(rl_dest, rl_result);
524 return true;
525}
526
Ian Rogerse2143c02014-03-28 08:47:16 -0700527// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
528bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
529 if (IsPowerOfTwo(lit)) {
530 op->op = kOpLsl;
531 op->shift = LowestSetBit(lit);
532 return true;
533 }
534
535 if (IsPowerOfTwo(lit - 1)) {
536 op->op = kOpAdd;
537 op->shift = LowestSetBit(lit - 1);
538 return true;
539 }
540
541 if (IsPowerOfTwo(lit + 1)) {
542 op->op = kOpRsub;
543 op->shift = LowestSetBit(lit + 1);
544 return true;
545 }
546
547 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100548 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700549 return false;
550}
551
552// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
553bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
554 GetEasyMultiplyOp(lit, &ops[0]);
555 if (GetEasyMultiplyOp(lit, &ops[0])) {
556 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100557 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700558 return true;
559 }
560
561 int lit1 = lit;
562 uint32_t shift = LowestSetBit(lit1);
563 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
564 ops[1].op = kOpLsl;
565 ops[1].shift = shift;
566 return true;
567 }
568
569 lit1 = lit - 1;
570 shift = LowestSetBit(lit1);
571 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
572 ops[1].op = kOpAdd;
573 ops[1].shift = shift;
574 return true;
575 }
576
577 lit1 = lit + 1;
578 shift = LowestSetBit(lit1);
579 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
580 ops[1].op = kOpRsub;
581 ops[1].shift = shift;
582 return true;
583 }
584
585 return false;
586}
587
Zheng Xuf9719f92014-04-02 13:31:31 +0100588// Generate instructions to do multiply.
589// Additional temporary register is required,
590// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700591void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100592 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
593 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
594
595 RegStorage r_tmp1;
596 if (ops[1].op == kOpInvalid) {
597 r_tmp1 = r_dest;
598 } else if (r_dest.GetReg() != r_src.GetReg()) {
599 r_tmp1 = r_dest;
600 } else {
601 r_tmp1 = AllocTemp();
602 }
603
604 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700605 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100606 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700607 break;
608 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100609 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700610 break;
611 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100612 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700613 break;
614 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100615 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700616 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100617 }
618
619 switch (ops[1].op) {
620 case kOpInvalid:
621 return;
622 case kOpLsl:
623 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
624 break;
625 case kOpAdd:
626 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
627 break;
628 case kOpRsub:
629 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
630 break;
631 default:
632 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
633 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700634 }
635}
636
637bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
638 EasyMultiplyOp ops[2];
639
640 if (!GetEasyMultiplyTwoOps(lit, ops)) {
641 return false;
642 }
643
644 rl_src = LoadValue(rl_src, kCoreReg);
645 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
646
647 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
648 StoreValue(rl_dest, rl_result);
649 return true;
650}
651
Mark Mendell2bf31e62014-01-23 12:13:40 -0800652RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
653 RegLocation rl_src2, bool is_div, bool check_zero) {
654 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
655 return rl_dest;
656}
657
658RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
659 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
660 return rl_dest;
661}
662
buzbee2700f7e2014-03-07 09:46:20 -0800663RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700664 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
665
666 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800667 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700668 LoadConstant(lit_temp, lit);
669 // Use the generic case for div/rem with arg2 in a register.
670 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
671 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
672 FreeTemp(lit_temp);
673
674 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675}
676
buzbee2700f7e2014-03-07 09:46:20 -0800677RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700678 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700679 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
680 if (is_div) {
681 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800682 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700683 } else {
684 // Remainder case, use the following code:
685 // temp = reg1 / reg2 - integer division
686 // temp = temp * reg2
687 // dest = reg1 - temp
688
buzbee2700f7e2014-03-07 09:46:20 -0800689 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700690 OpRegRegReg(kOpDiv, temp, reg1, reg2);
691 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800692 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700693 FreeTemp(temp);
694 }
695
696 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697}
698
Serban Constantinescu23abec92014-07-02 16:13:38 +0100699bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100701 if (is_long) {
702 return false;
703 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704 RegLocation rl_src1 = info->args[0];
705 RegLocation rl_src2 = info->args[1];
706 rl_src1 = LoadValue(rl_src1, kCoreReg);
707 rl_src2 = LoadValue(rl_src2, kCoreReg);
708 RegLocation rl_dest = InlineTarget(info);
709 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800710 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700711 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800712 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
713 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700714 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 StoreValue(rl_dest, rl_result);
716 return true;
717}
718
Vladimir Markoe508a202013-11-04 15:24:22 +0000719bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
720 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800721 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000722 RegLocation rl_dest = InlineTarget(info);
723 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
724 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700725 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000726 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800727 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700728 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
729 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000730 } else {
buzbee695d13a2014-04-19 13:32:20 -0700731 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
732 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000733 }
734 StoreValueWide(rl_dest, rl_result);
735 } else {
buzbee695d13a2014-04-19 13:32:20 -0700736 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000737 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000738 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000739 StoreValue(rl_dest, rl_result);
740 }
741 return true;
742}
743
744bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
745 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800746 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000747 RegLocation rl_src_value = info->args[2]; // [size] value
748 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700749 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000750 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
751 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000752 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
753 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000754 } else {
buzbee695d13a2014-04-19 13:32:20 -0700755 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000756 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
757 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000758 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000759 }
760 return true;
761}
762
buzbee2700f7e2014-03-07 09:46:20 -0800763void ArmMir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 LOG(FATAL) << "Unexpected use of OpLea for Arm";
765}
766
Ian Rogersdd7624d2014-03-14 17:43:00 -0700767void ArmMir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm";
769}
770
Andreas Gampe2f244e92014-05-08 03:35:25 -0700771void ArmMir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
772 UNIMPLEMENTED(FATAL) << "Should not be called.";
773}
774
Hans Boehm48f5c472014-06-27 14:50:10 -0700775// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000776bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 DCHECK_EQ(cu_->instruction_set, kThumb2);
778 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000779 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
780 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800781 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000782 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000783 // If is_long, high half is in info->args[5]
784 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
785 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 RegLocation rl_dest = InlineTarget(info); // boolean place for result
787
Vladimir Marko3e5af822013-11-21 15:01:20 +0000788 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
789 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
790 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
791 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
792 // into the same temps, reducing the number of required temps down to 5. We shall work
793 // around the potentially locked temp by using LR for r_ptr, unconditionally.
794 // TODO: Pass information about the need for more temps to the stack frame generation
795 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700796 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
797 MarkTemp(rs_rARM_LR);
798 FreeTemp(rs_rARM_LR);
799 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000800 bool load_early = true;
801 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700802 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
803 rl_src_expected.reg;
804 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
805 rl_src_new_value.reg;
806 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
807 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800808 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
809 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000810
811 if (!expected_is_good_reg && !new_value_is_good_reg) {
812 // None of expected/new_value is non-temp reg, need to load both late
813 load_early = false;
814 // Make sure they are not in the temp regs and the load will not be skipped.
815 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800816 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000817 ClobberSReg(rl_src_expected.s_reg_low);
818 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
819 rl_src_expected.location = kLocDalvikFrame;
820 }
821 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800822 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000823 ClobberSReg(rl_src_new_value.s_reg_low);
824 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
825 rl_src_new_value.location = kLocDalvikFrame;
826 }
827 }
828 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829
Hans Boehm48f5c472014-06-27 14:50:10 -0700830 // Prevent reordering with prior memory operations.
831 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832
buzbeea0cd2d72014-06-01 09:33:49 -0700833 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000834 RegLocation rl_new_value;
835 if (!is_long) {
buzbeea0cd2d72014-06-01 09:33:49 -0700836 rl_new_value = LoadValue(rl_src_new_value);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000837 } else if (load_early) {
838 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
839 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700840
Vladimir Marko1c282e22013-11-21 14:49:47 +0000841 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700842 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800843 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 }
845
846 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
847
buzbee2700f7e2014-03-07 09:46:20 -0800848 RegStorage r_ptr = rs_rARM_LR;
849 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850
851 // Free now unneeded rl_object and rl_offset to give more temps.
852 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700853 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700855 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856
Vladimir Marko3e5af822013-11-21 15:01:20 +0000857 RegLocation rl_expected;
858 if (!is_long) {
buzbeea0cd2d72014-06-01 09:33:49 -0700859 rl_expected = LoadValue(rl_src_expected);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000860 } else if (load_early) {
861 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
862 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000863 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700864 RegStorage low_reg = AllocTemp();
865 RegStorage high_reg = AllocTemp();
866 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000867 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000868 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869
Vladimir Marko3e5af822013-11-21 15:01:20 +0000870 // do {
871 // tmp = [r_ptr] - expected;
872 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
873 // result = tmp != 0;
874
buzbee2700f7e2014-03-07 09:46:20 -0800875 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700876 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700877
Dave Allison3da67a52014-04-02 17:03:45 -0700878 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000879 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800880 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000881 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800882 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000883 }
buzbee2700f7e2014-03-07 09:46:20 -0800884 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
885 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
886 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000887 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800888 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000889 }
890 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700891 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800892 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000893 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800894 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000895 }
896 FreeTemp(r_tmp_high); // Now unneeded
897
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100898 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700899 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800900 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 +0000901
902 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800903 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
904 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100905 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700906 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800907 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000908 }
909
910 // Still one conditional left from OpIT(kCondEq, "T") from either branch
911 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700912 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700913
Jeff Hao2de2aa12013-09-12 17:20:31 -0700914 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915
Vladimir Marko3e5af822013-11-21 15:01:20 +0000916 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800917 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000918 }
919
Hans Boehm48f5c472014-06-27 14:50:10 -0700920 // Prevent reordering with subsequent memory operations.
921 GenMemBarrier(kLoadAny);
922
Vladimir Marko3e5af822013-11-21 15:01:20 +0000923 // result := (tmp1 != 0) ? 0 : 1;
924 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800925 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100926 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700927 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800928 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000929 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700930 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000931
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 StoreValue(rl_dest, rl_result);
933
Vladimir Marko3e5af822013-11-21 15:01:20 +0000934 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700935 Clobber(rs_rARM_LR);
936 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 return true;
938}
939
buzbee2700f7e2014-03-07 09:46:20 -0800940LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
941 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942}
943
buzbee2700f7e2014-03-07 09:46:20 -0800944LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -0700945 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946}
947
buzbee2700f7e2014-03-07 09:46:20 -0800948LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -0700949 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950}
951
952void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
953 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700954 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700955 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 EncodeShift(kArmLsl, second_bit - first_bit));
957 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800958 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 }
960}
961
Mingyao Yange643a172014-04-08 11:02:52 -0700962void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800963 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
964 RegStorage t_reg = AllocTemp();
965 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -0700967 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968}
969
970// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700971LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -0700972#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -0700973 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -0700975#else
976 RegStorage t_reg = AllocTemp();
977 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
978 t_reg, kUnsignedHalf);
979 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
980 0, target);
981 FreeTemp(t_reg);
982 return cmp_branch;
983#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984}
985
986// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -0800987LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +0000989 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100990 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 return OpCondBranch(c_code, target);
992}
993
Andreas Gampeb14329f2014-05-15 11:16:06 -0700994bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800996 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
997 LIR* barrier = last_lir_insn_;
998
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 int dmb_flavor;
1000 // TODO: revisit Arm barrier kinds
1001 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001002 case kAnyStore: dmb_flavor = kISH; break;
1003 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001004 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001005 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 default:
1007 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1008 dmb_flavor = kSY; // quiet gcc.
1009 break;
1010 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001011
Andreas Gampeb14329f2014-05-15 11:16:06 -07001012 bool ret = false;
1013
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001014 // If the same barrier already exists, don't generate another.
1015 if (barrier == nullptr
1016 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1017 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001018 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001019 }
1020
1021 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1022 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001023 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001024 return ret;
1025#else
1026 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027#endif
1028}
1029
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001030void ArmMir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
1031 LOG(FATAL) << "Unexpected use GenNotLong()";
1032}
1033
1034void ArmMir2Lir::GenDivRemLong(Instruction::Code, RegLocation rl_dest, RegLocation rl_src1,
1035 RegLocation rl_src2, bool is_div) {
1036 LOG(FATAL) << "Unexpected use GenDivRemLong()";
1037}
1038
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001039void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 rl_src = LoadValueWide(rl_src, kCoreReg);
1041 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001042 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 LoadConstantNoClobber(z_reg, 0);
1044 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001045 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1046 RegStorage t_reg = AllocTemp();
1047 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1048 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 FreeTemp(t_reg);
1050 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001051 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1052 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 }
1054 FreeTemp(z_reg);
1055 StoreValueWide(rl_dest, rl_result);
1056}
1057
Mark Mendelle02d48f2014-01-15 11:19:23 -08001058void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1059 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 /*
Zheng Xud7f8e022014-03-13 13:40:30 +00001061 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1062 * dest = src1.lo * src2.lo;
1063 * tmp1 += src1.lo * src2.hi;
1064 * dest.hi += tmp1;
1065 *
1066 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 * registers. Normally for Arm, we get 5. We can get to 6 by including
1068 * lr in the temp set. The only problematic case is all operands and result are
1069 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1070 * freeing operand temp registers after they are no longer needed. All other cases
1071 * can proceed normally. We'll just punt on the case of the result having a misaligned
1072 * overlap with either operand and send that case to a runtime handler.
1073 */
1074 RegLocation rl_result;
1075 if (BadOverlap(rl_src1, rl_dest) || (BadOverlap(rl_src2, rl_dest))) {
Ian Rogersdd7624d2014-03-14 17:43:00 -07001076 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pLmul);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 FlushAllRegs();
1078 CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001079 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 StoreValueWide(rl_dest, rl_result);
1081 return;
1082 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001083
1084 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1085 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1086
1087 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001088 RegStorage res_lo;
1089 RegStorage res_hi;
1090 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
buzbee091cc402014-03-31 10:14:40 -07001091 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1092 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1093 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001094 // Check if rl_dest is *not* either operand and we have enough temp registers.
1095 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1096 (dest_promoted || src1_promoted || src2_promoted)) {
1097 // In this case, we do not need to manually allocate temp registers for result.
1098 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001099 res_lo = rl_result.reg.GetLow();
1100 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001101 } else {
1102 res_lo = AllocTemp();
1103 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1104 // In this case, we have enough temp registers to be allocated for result.
1105 res_hi = AllocTemp();
1106 reg_status = 1;
1107 } else {
1108 // In this case, all temps are now allocated.
1109 // res_hi will be allocated after we can free src1_hi.
1110 reg_status = 2;
1111 }
1112 }
1113
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 // Temporarily add LR to the temp pool, and assign it to tmp1
buzbee091cc402014-03-31 10:14:40 -07001115 MarkTemp(rs_rARM_LR);
1116 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001117 RegStorage tmp1 = rs_rARM_LR;
buzbee091cc402014-03-31 10:14:40 -07001118 LockTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119
buzbee2700f7e2014-03-07 09:46:20 -08001120 if (rl_src1.reg == rl_src2.reg) {
1121 DCHECK(res_hi.Valid());
1122 DCHECK(res_lo.Valid());
1123 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1124 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1125 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001126 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001128 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001129 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001130 DCHECK(!res_hi.Valid());
1131 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001132 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
buzbee082833c2014-05-17 23:16:26 -07001133 // Will force free src1_hi, so must clobber.
1134 Clobber(rl_src1.reg);
buzbee091cc402014-03-31 10:14:40 -07001135 FreeTemp(rl_src1.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001136 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137 }
buzbee2700f7e2014-03-07 09:46:20 -08001138 DCHECK(res_hi.Valid());
1139 DCHECK(res_lo.Valid());
1140 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1141 rl_src1.reg.GetLowReg());
1142 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1143 tmp1.GetReg());
1144 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001145 if (reg_status == 2) {
buzbee082833c2014-05-17 23:16:26 -07001146 FreeTemp(rl_src1.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001149
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001151 FreeTemp(tmp1);
buzbee091cc402014-03-31 10:14:40 -07001152 Clobber(rs_rARM_LR);
1153 UnmarkTemp(rs_rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001154
1155 if (reg_status != 0) {
1156 // We had manually allocated registers for rl_result.
1157 // Now construct a RegLocation.
buzbeea0cd2d72014-06-01 09:33:49 -07001158 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001159 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001160 }
1161
1162 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163}
1164
Mark Mendelle02d48f2014-01-15 11:19:23 -08001165void ArmMir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001166 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 LOG(FATAL) << "Unexpected use of GenAddLong for Arm";
1168}
1169
Mark Mendelle02d48f2014-01-15 11:19:23 -08001170void ArmMir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001171 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 LOG(FATAL) << "Unexpected use of GenSubLong for Arm";
1173}
1174
Mark Mendelle02d48f2014-01-15 11:19:23 -08001175void ArmMir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001176 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 LOG(FATAL) << "Unexpected use of GenAndLong for Arm";
1178}
1179
Mark Mendelle02d48f2014-01-15 11:19:23 -08001180void ArmMir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001181 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 LOG(FATAL) << "Unexpected use of GenOrLong for Arm";
1183}
1184
Mark Mendelle02d48f2014-01-15 11:19:23 -08001185void ArmMir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001186 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 LOG(FATAL) << "Unexpected use of genXoLong for Arm";
1188}
1189
1190/*
1191 * Generate array load
1192 */
1193void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001194 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001195 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 int len_offset = mirror::Array::LengthOffset().Int32Value();
1197 int data_offset;
1198 RegLocation rl_result;
1199 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001200 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 if (!constant_index) {
1202 rl_index = LoadValue(rl_index, kCoreReg);
1203 }
1204
1205 if (rl_dest.wide) {
1206 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1207 } else {
1208 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1209 }
1210
1211 // If index is constant, just fold it into the data offset
1212 if (constant_index) {
1213 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1214 }
1215
1216 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001217 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218
1219 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001220 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 if (needs_range_check) {
1222 reg_len = AllocTemp();
1223 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001224 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001225 MarkPossibleNullPointerException(opt_flags);
1226 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001227 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 }
1229 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001230 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001232 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 } else {
1234 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001235 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001236 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001237 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
1239 rl_result = EvalLoc(rl_dest, reg_class, true);
1240
1241 if (needs_range_check) {
1242 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001243 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001245 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 }
1247 FreeTemp(reg_len);
1248 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001249 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001250 MarkPossibleNullPointerException(opt_flags);
1251 if (!constant_index) {
1252 FreeTemp(reg_ptr);
1253 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 StoreValueWide(rl_dest, rl_result);
1256 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 StoreValue(rl_dest, rl_result);
1258 }
1259 } else {
1260 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001261 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001262 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001263 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 rl_result = EvalLoc(rl_dest, reg_class, true);
1265
1266 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001267 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 FreeTemp(reg_len);
1269 }
buzbee2700f7e2014-03-07 09:46:20 -08001270 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001271 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 FreeTemp(reg_ptr);
1273 StoreValue(rl_dest, rl_result);
1274 }
1275}
1276
1277/*
1278 * Generate array store
1279 *
1280 */
1281void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001282 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001283 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001284 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 bool constant_index = rl_index.is_const;
1286
Ian Rogersa9a82542013-10-04 11:17:26 -07001287 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001288 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1290 } else {
1291 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1292 }
1293
1294 // If index is constant, just fold it into the data offset.
1295 if (constant_index) {
1296 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1297 }
1298
buzbeea0cd2d72014-06-01 09:33:49 -07001299 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 if (!constant_index) {
1301 rl_index = LoadValue(rl_index, kCoreReg);
1302 }
1303
buzbee2700f7e2014-03-07 09:46:20 -08001304 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001305 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001307 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001308 } else if (IsTemp(rl_array.reg) && !card_mark) {
1309 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001310 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001312 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001313 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 }
1315
1316 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001317 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318
1319 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001320 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 if (needs_range_check) {
1322 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001323 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001325 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001326 MarkPossibleNullPointerException(opt_flags);
1327 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001328 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 }
1330 /* at this point, reg_ptr points to array, 2 live temps */
1331 if (rl_src.wide || rl_src.fp || constant_index) {
1332 if (rl_src.wide) {
1333 rl_src = LoadValueWide(rl_src, reg_class);
1334 } else {
1335 rl_src = LoadValue(rl_src, reg_class);
1336 }
1337 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001338 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 }
1340 if (needs_range_check) {
1341 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001342 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001344 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 }
1346 FreeTemp(reg_len);
1347 }
1348
Andreas Gampe3c12c512014-06-24 18:46:29 +00001349 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Dave Allisonb373e092014-02-20 16:06:36 -08001350 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 } else {
1352 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001353 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 rl_src = LoadValue(rl_src, reg_class);
1355 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001356 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 FreeTemp(reg_len);
1358 }
buzbee2700f7e2014-03-07 09:46:20 -08001359 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001360 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 }
Ian Rogers773aab12013-10-14 13:50:10 -07001362 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 FreeTemp(reg_ptr);
1364 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001365 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001366 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 }
1368}
1369
Ian Rogersa9a82542013-10-04 11:17:26 -07001370
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001372 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001373 rl_src = LoadValueWide(rl_src, kCoreReg);
1374 // Per spec, we only care about low 6 bits of shift amount.
1375 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1376 if (shift_amount == 0) {
1377 StoreValueWide(rl_dest, rl_src);
1378 return;
1379 }
1380 if (BadOverlap(rl_src, rl_dest)) {
1381 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1382 return;
1383 }
1384 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001385 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 case Instruction::SHL_LONG:
1387 case Instruction::SHL_LONG_2ADDR:
1388 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001389 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1390 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001392 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1393 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001395 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1396 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001398 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001399 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001400 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001401 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 }
1403 break;
1404 case Instruction::SHR_LONG:
1405 case Instruction::SHR_LONG_2ADDR:
1406 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001407 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1408 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001410 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1411 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001412 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001413 RegStorage t_reg = AllocTemp();
1414 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001415 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 EncodeShift(kArmLsl, 32 - shift_amount));
1417 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001418 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 }
1420 break;
1421 case Instruction::USHR_LONG:
1422 case Instruction::USHR_LONG_2ADDR:
1423 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001424 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1425 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001427 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1428 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001430 RegStorage t_reg = AllocTemp();
1431 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001432 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 EncodeShift(kArmLsl, 32 - shift_amount));
1434 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001435 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 }
1437 break;
1438 default:
1439 LOG(FATAL) << "Unexpected case";
1440 }
1441 StoreValueWide(rl_dest, rl_result);
1442}
1443
1444void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001445 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1447 if (!rl_src2.is_const) {
1448 // Don't bother with special handling for subtract from immediate.
1449 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1450 return;
1451 }
1452 } else {
1453 // Normalize
1454 if (!rl_src2.is_const) {
1455 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001456 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001457 }
1458 }
1459 if (BadOverlap(rl_src1, rl_dest)) {
1460 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1461 return;
1462 }
1463 DCHECK(rl_src2.is_const);
1464 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1465 uint32_t val_lo = Low32Bits(val);
1466 uint32_t val_hi = High32Bits(val);
1467 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1468 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1469
1470 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001471 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 case Instruction::ADD_LONG:
1473 case Instruction::ADD_LONG_2ADDR:
1474 case Instruction::SUB_LONG:
1475 case Instruction::SUB_LONG_2ADDR:
1476 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
1477 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
1478 return;
1479 }
1480 break;
1481 default:
1482 break;
1483 }
1484 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1485 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1486 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1487 switch (opcode) {
1488 case Instruction::ADD_LONG:
1489 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001490 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001491 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 break;
1493 case Instruction::OR_LONG:
1494 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001495 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1496 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001497 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001498 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001499 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 }
1501 break;
1502 case Instruction::XOR_LONG:
1503 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001504 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1505 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001506 break;
1507 case Instruction::AND_LONG:
1508 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001509 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1510 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001512 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001513 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 }
1515 break;
1516 case Instruction::SUB_LONG_2ADDR:
1517 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001518 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001519 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 break;
1521 default:
1522 LOG(FATAL) << "Unexpected opcode " << opcode;
1523 }
1524 StoreValueWide(rl_dest, rl_result);
1525}
1526
1527} // namespace art