blob: ebf1905579811559fa06dc411e71da403afeff10 [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"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.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 Carlstrom7940e442013-07-12 13:46:57 -070052 switch (strlen(guide)) {
53 case 3:
54 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070055 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 case 2:
57 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070058 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 case 1:
60 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
61 break;
62 case 0:
63 break;
64 default:
65 LOG(FATAL) << "OAT: bad case in OpIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -070066 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 }
68 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
69 (1 << (3 - strlen(guide)));
70 return NewLIR2(kThumb2It, code, mask);
71}
72
Andreas Gampeb14329f2014-05-15 11:16:06 -070073void ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
74 int mask;
75 int mask3 = 0;
76 int mask2 = 0;
77 int mask1 = 0;
78 ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
79 int cond_bit = code & 1;
80 int alt_bit = cond_bit ^ 1;
81
Andreas Gampeb14329f2014-05-15 11:16:06 -070082 switch (strlen(new_guide)) {
83 case 3:
84 mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070085 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070086 case 2:
87 mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070088 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070089 case 1:
90 mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
91 break;
92 case 0:
93 break;
94 default:
95 LOG(FATAL) << "OAT: bad case in UpdateIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -070096 UNREACHABLE();
Andreas Gampeb14329f2014-05-15 11:16:06 -070097 }
98 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
99 (1 << (3 - strlen(new_guide)));
100 it->operands[1] = mask;
101}
102
Dave Allison3da67a52014-04-02 17:03:45 -0700103void ArmMir2Lir::OpEndIT(LIR* it) {
104 // TODO: use the 'it' pointer to do some checks with the LIR, for example
105 // we could check that the number of instructions matches the mask
106 // in the IT instruction.
107 CHECK(it != nullptr);
108 GenBarrier();
109}
110
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111/*
112 * 64-bit 3way compare function.
113 * mov rX, #-1
114 * cmp op1hi, op2hi
115 * blt done
116 * bgt flip
117 * sub rX, op1lo, op2lo (treat as unsigned)
118 * beq done
119 * ite hi
120 * mov(hi) rX, #-1
121 * mov(!hi) rX, #1
122 * flip:
123 * neg rX
124 * done:
125 */
buzbeea1983d42014-04-07 12:35:39 -0700126void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 LIR* target1;
128 LIR* target2;
129 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
130 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800131 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800133 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 LIR* branch1 = OpCondBranch(kCondLt, NULL);
135 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700136 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 LIR* branch3 = OpCondBranch(kCondEq, NULL);
138
Dave Allison3da67a52014-04-02 17:03:45 -0700139 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800140 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700142 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143
144 target2 = NewLIR0(kPseudoTargetLabel);
145 OpRegReg(kOpNeg, t_reg, t_reg);
146
147 target1 = NewLIR0(kPseudoTargetLabel);
148
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700149 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800150 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 StoreValue(rl_dest, rl_temp);
152 FreeTemp(t_reg);
153
154 branch1->target = target1;
155 branch2->target = target2;
156 branch3->target = branch1->target;
157}
158
159void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 int32_t val_lo = Low32Bits(val);
162 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700163 DCHECK_GE(ModifiedImmediate(val_lo), 0);
164 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700165 LIR* taken = &block_label_list_[bb->taken];
166 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800168 RegStorage low_reg = rl_src1.reg.GetLow();
169 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170
Vladimir Marko58af1f92013-12-19 13:31:15 +0000171 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800172 RegStorage t_reg = AllocTemp();
173 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000174 FreeTemp(t_reg);
175 OpCondBranch(ccode, taken);
176 return;
177 }
178
Brian Carlstromdf629502013-07-17 22:39:56 -0700179 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 case kCondEq:
181 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000182 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 break;
184 case kCondLt:
185 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
186 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000187 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 break;
189 case kCondLe:
190 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
191 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
192 ccode = kCondLs;
193 break;
194 case kCondGt:
195 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
196 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
197 ccode = kCondHi;
198 break;
199 case kCondGe:
200 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
201 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000202 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 break;
204 default:
205 LOG(FATAL) << "Unexpected ccode: " << ccode;
206 }
207 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
208}
209
Andreas Gampe90969af2014-07-15 23:02:11 -0700210void ArmMir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
211 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700212 RegisterClass dest_reg_class) {
213 UNUSED(dest_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700214 // TODO: Generalize the IT below to accept more than one-instruction loads.
215 DCHECK(InexpensiveConstantInt(true_val));
216 DCHECK(InexpensiveConstantInt(false_val));
217
218 if ((true_val == 0 && code == kCondEq) ||
219 (false_val == 0 && code == kCondNe)) {
220 OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
221 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
222 LIR* it = OpIT(kCondNe, "");
223 LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
224 OpEndIT(it);
225 return;
226 }
227
228 OpRegReg(kOpCmp, left_op, right_op); // Same?
229 LIR* it = OpIT(code, "E"); // if-convert the test
230 LoadConstant(rs_dest, true_val); // .eq case - load true
231 LoadConstant(rs_dest, false_val); // .eq case - load true
232 OpEndIT(it);
233}
234
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700235void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700236 UNUSED(bb);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 RegLocation rl_result;
238 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700240 // Avoid using float regs here.
241 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
242 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
243 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000244 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 if (mir->ssa_rep->num_uses == 1) {
246 // CONST case
247 int true_val = mir->dalvikInsn.vB;
248 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700249 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000250 // Change kCondNe to kCondEq for the special cases below.
251 if (ccode == kCondNe) {
252 ccode = kCondEq;
253 std::swap(true_val, false_val);
254 }
255 bool cheap_false_val = InexpensiveConstantInt(false_val);
256 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800257 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100258 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700259 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800260 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700261 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000262 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100264 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700265 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800266 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700267 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000268 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800269 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700270 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800271 LoadConstant(rl_result.reg, true_val);
272 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700273 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 } else {
275 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700276 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
277 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 LoadConstant(t_reg1, true_val);
279 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800280 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700281 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800282 OpRegCopy(rl_result.reg, t_reg1);
283 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700284 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 }
286 } else {
287 // MOVE case
288 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
289 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700290 rl_true = LoadValue(rl_true, result_reg_class);
291 rl_false = LoadValue(rl_false, result_reg_class);
292 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800293 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700294 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000295 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700296 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800297 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000298 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700299 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800300 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700301 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700302 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800303 OpRegCopy(rl_result.reg, rl_true.reg);
304 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700305 }
Dave Allison3da67a52014-04-02 17:03:45 -0700306 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 }
308 StoreValue(rl_dest, rl_result);
309}
310
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700311void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
313 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
314 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000315 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000317 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 ccode = FlipComparisonOrder(ccode);
319 }
320 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700321 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 // Do special compare/branch against simple const operand if not already in registers.
323 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700324 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
326 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
327 return;
328 }
329 }
buzbee0d829482013-10-11 15:24:55 -0700330 LIR* taken = &block_label_list_[bb->taken];
331 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
333 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800334 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700335 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 case kCondEq:
337 OpCondBranch(kCondNe, not_taken);
338 break;
339 case kCondNe:
340 OpCondBranch(kCondNe, taken);
341 break;
342 case kCondLt:
343 OpCondBranch(kCondLt, taken);
344 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000345 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 break;
347 case kCondLe:
348 OpCondBranch(kCondLt, taken);
349 OpCondBranch(kCondGt, not_taken);
350 ccode = kCondLs;
351 break;
352 case kCondGt:
353 OpCondBranch(kCondGt, taken);
354 OpCondBranch(kCondLt, not_taken);
355 ccode = kCondHi;
356 break;
357 case kCondGe:
358 OpCondBranch(kCondGt, taken);
359 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000360 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 break;
362 default:
363 LOG(FATAL) << "Unexpected ccode: " << ccode;
364 }
buzbee2700f7e2014-03-07 09:46:20 -0800365 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 OpCondBranch(ccode, taken);
367}
368
369/*
370 * Generate a register comparison to an immediate and branch. Caller
371 * is responsible for setting branch target field.
372 */
buzbee2700f7e2014-03-07 09:46:20 -0800373LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700374 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700376 /*
377 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
378 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700379 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700380 * be converted to a long form during assembly (which will trigger another assembly
381 * pass). Here we estimate the branch distance for checks, and if large directly
382 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700383 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700384 */
385 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700386 skip &= ((mir_graph_->GetNumDalvikInsns() - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700387 if (!skip && reg.Low8() && (check_value == 0)) {
388 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
389 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
390 reg.GetReg(), 0);
391 } else if (arm_cond == kArmCondLs) {
392 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
393 // This case happens for a bounds check of array[0].
394 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
395 }
396 }
397
398 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000399 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 branch = NewLIR2(kThumbBCond, 0, arm_cond);
401 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700402
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 branch->target = target;
404 return branch;
405}
406
buzbee2700f7e2014-03-07 09:46:20 -0800407LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 LIR* res;
409 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800410 // If src or dest is a pair, we'll be using low reg.
411 if (r_dest.IsPair()) {
412 r_dest = r_dest.GetLow();
413 }
414 if (r_src.IsPair()) {
415 r_src = r_src.GetLow();
416 }
buzbee091cc402014-03-31 10:14:40 -0700417 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700419 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700421 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700423 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 opcode = kThumbMovRR_H2L;
425 else
426 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800427 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
429 res->flags.is_nop = true;
430 }
431 return res;
432}
433
buzbee7a11ab02014-04-28 20:02:38 -0700434void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
435 if (r_dest != r_src) {
436 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
437 AppendLIR(res);
438 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439}
440
buzbee2700f7e2014-03-07 09:46:20 -0800441void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700442 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700443 bool dest_fp = r_dest.IsFloat();
444 bool src_fp = r_src.IsFloat();
445 DCHECK(r_dest.Is64Bit());
446 DCHECK(r_src.Is64Bit());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800447 // Note: If the register is get by register allocator, it should never be a pair.
448 // But some functions in mir_2_lir assume 64-bit registers are 32-bit register pairs.
449 // TODO: Rework Mir2Lir::LoadArg() and Mir2Lir::LoadArgDirect().
450 if (dest_fp && r_dest.IsPair()) {
451 r_dest = As64BitFloatReg(r_dest);
452 }
453 if (src_fp && r_src.IsPair()) {
454 r_src = As64BitFloatReg(r_src);
455 }
buzbee7a11ab02014-04-28 20:02:38 -0700456 if (dest_fp) {
457 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700458 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 } else {
buzbee091cc402014-03-31 10:14:40 -0700460 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700461 }
462 } else {
463 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700464 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700465 } else {
466 // Handle overlap
467 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
468 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
469 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
470 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
471 } else {
472 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
473 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
474 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 }
476 }
477 }
478}
479
480// Table of magic divisors
481struct MagicTable {
482 uint32_t magic;
483 uint32_t shift;
484 DividePattern pattern;
485};
486
487static const MagicTable magic_table[] = {
488 {0, 0, DivideNone}, // 0
489 {0, 0, DivideNone}, // 1
490 {0, 0, DivideNone}, // 2
491 {0x55555556, 0, Divide3}, // 3
492 {0, 0, DivideNone}, // 4
493 {0x66666667, 1, Divide5}, // 5
494 {0x2AAAAAAB, 0, Divide3}, // 6
495 {0x92492493, 2, Divide7}, // 7
496 {0, 0, DivideNone}, // 8
497 {0x38E38E39, 1, Divide5}, // 9
498 {0x66666667, 2, Divide5}, // 10
499 {0x2E8BA2E9, 1, Divide5}, // 11
500 {0x2AAAAAAB, 1, Divide5}, // 12
501 {0x4EC4EC4F, 2, Divide5}, // 13
502 {0x92492493, 3, Divide7}, // 14
503 {0x88888889, 3, Divide7}, // 15
504};
505
506// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700507bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700509 UNUSED(dalvik_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
511 return false;
512 }
513 DividePattern pattern = magic_table[lit].pattern;
514 if (pattern == DivideNone) {
515 return false;
516 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517
buzbee2700f7e2014-03-07 09:46:20 -0800518 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 LoadConstant(r_magic, magic_table[lit].magic);
520 rl_src = LoadValue(rl_src, kCoreReg);
521 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800522 RegStorage r_hi = AllocTemp();
523 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100524
525 // rl_dest and rl_src might overlap.
526 // Reuse r_hi to save the div result for reminder case.
527 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
528
buzbee2700f7e2014-03-07 09:46:20 -0800529 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700530 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100532 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 break;
534 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800535 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100536 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700537 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 break;
539 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800540 OpRegReg(kOpAdd, r_hi, rl_src.reg);
541 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100542 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700543 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 break;
545 default:
546 LOG(FATAL) << "Unexpected pattern: " << pattern;
547 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100548
549 if (!is_div) {
550 // div_result = src / lit
551 // tmp1 = div_result * lit
552 // dest = src - tmp1
553 RegStorage tmp1 = r_lo;
554 EasyMultiplyOp ops[2];
555
556 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
557 DCHECK_NE(canEasyMultiply, false);
558
559 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
560 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
561 }
562
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 StoreValue(rl_dest, rl_result);
564 return true;
565}
566
Ian Rogerse2143c02014-03-28 08:47:16 -0700567// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
568bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
569 if (IsPowerOfTwo(lit)) {
570 op->op = kOpLsl;
571 op->shift = LowestSetBit(lit);
572 return true;
573 }
574
575 if (IsPowerOfTwo(lit - 1)) {
576 op->op = kOpAdd;
577 op->shift = LowestSetBit(lit - 1);
578 return true;
579 }
580
581 if (IsPowerOfTwo(lit + 1)) {
582 op->op = kOpRsub;
583 op->shift = LowestSetBit(lit + 1);
584 return true;
585 }
586
587 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100588 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700589 return false;
590}
591
592// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
593bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
594 GetEasyMultiplyOp(lit, &ops[0]);
595 if (GetEasyMultiplyOp(lit, &ops[0])) {
596 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100597 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700598 return true;
599 }
600
601 int lit1 = lit;
602 uint32_t shift = LowestSetBit(lit1);
603 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
604 ops[1].op = kOpLsl;
605 ops[1].shift = shift;
606 return true;
607 }
608
609 lit1 = lit - 1;
610 shift = LowestSetBit(lit1);
611 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
612 ops[1].op = kOpAdd;
613 ops[1].shift = shift;
614 return true;
615 }
616
617 lit1 = lit + 1;
618 shift = LowestSetBit(lit1);
619 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
620 ops[1].op = kOpRsub;
621 ops[1].shift = shift;
622 return true;
623 }
624
625 return false;
626}
627
Zheng Xuf9719f92014-04-02 13:31:31 +0100628// Generate instructions to do multiply.
629// Additional temporary register is required,
630// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700631void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100632 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
633 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
634
635 RegStorage r_tmp1;
636 if (ops[1].op == kOpInvalid) {
637 r_tmp1 = r_dest;
638 } else if (r_dest.GetReg() != r_src.GetReg()) {
639 r_tmp1 = r_dest;
640 } else {
641 r_tmp1 = AllocTemp();
642 }
643
644 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700645 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100646 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700647 break;
648 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100649 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700650 break;
651 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100652 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700653 break;
654 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100655 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700656 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100657 }
658
659 switch (ops[1].op) {
660 case kOpInvalid:
661 return;
662 case kOpLsl:
663 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
664 break;
665 case kOpAdd:
666 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
667 break;
668 case kOpRsub:
669 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
670 break;
671 default:
672 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
673 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700674 }
675}
676
677bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
678 EasyMultiplyOp ops[2];
679
680 if (!GetEasyMultiplyTwoOps(lit, ops)) {
681 return false;
682 }
683
684 rl_src = LoadValue(rl_src, kCoreReg);
685 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
686
687 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
688 StoreValue(rl_dest, rl_result);
689 return true;
690}
691
Mark Mendell2bf31e62014-01-23 12:13:40 -0800692RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700693 RegLocation rl_src2, bool is_div, int flags) {
694 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800695 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700696 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800697}
698
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700699RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
700 bool is_div) {
701 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800702 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700703 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800704}
705
buzbee2700f7e2014-03-07 09:46:20 -0800706RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
708
709 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800710 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700711 LoadConstant(lit_temp, lit);
712 // Use the generic case for div/rem with arg2 in a register.
713 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
714 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
715 FreeTemp(lit_temp);
716
717 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718}
719
buzbee2700f7e2014-03-07 09:46:20 -0800720RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700721 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700722 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
723 if (is_div) {
724 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800725 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700726 } else {
727 // Remainder case, use the following code:
728 // temp = reg1 / reg2 - integer division
729 // temp = temp * reg2
730 // dest = reg1 - temp
731
buzbee2700f7e2014-03-07 09:46:20 -0800732 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700733 OpRegRegReg(kOpDiv, temp, reg1, reg2);
734 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800735 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700736 FreeTemp(temp);
737 }
738
739 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740}
741
Serban Constantinescu23abec92014-07-02 16:13:38 +0100742bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100744 if (is_long) {
745 return false;
746 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 RegLocation rl_src1 = info->args[0];
748 RegLocation rl_src2 = info->args[1];
749 rl_src1 = LoadValue(rl_src1, kCoreReg);
750 rl_src2 = LoadValue(rl_src2, kCoreReg);
751 RegLocation rl_dest = InlineTarget(info);
752 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800753 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700754 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800755 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
756 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700757 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 StoreValue(rl_dest, rl_result);
759 return true;
760}
761
Vladimir Markoe508a202013-11-04 15:24:22 +0000762bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
763 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800764 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000765 RegLocation rl_dest = InlineTarget(info);
766 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
767 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700768 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000769 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800770 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700771 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
772 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000773 } else {
buzbee695d13a2014-04-19 13:32:20 -0700774 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
775 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000776 }
777 StoreValueWide(rl_dest, rl_result);
778 } else {
buzbee695d13a2014-04-19 13:32:20 -0700779 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000780 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000781 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000782 StoreValue(rl_dest, rl_result);
783 }
784 return true;
785}
786
787bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
788 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800789 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000790 RegLocation rl_src_value = info->args[2]; // [size] value
791 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700792 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000793 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
794 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000795 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
796 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000797 } else {
buzbee695d13a2014-04-19 13:32:20 -0700798 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000799 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
800 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000801 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000802 }
803 return true;
804}
805
Hans Boehm48f5c472014-06-27 14:50:10 -0700806// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000807bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 DCHECK_EQ(cu_->instruction_set, kThumb2);
809 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000810 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
811 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800812 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000813 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000814 // If is_long, high half is in info->args[5]
815 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
816 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 RegLocation rl_dest = InlineTarget(info); // boolean place for result
818
Vladimir Marko3e5af822013-11-21 15:01:20 +0000819 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
820 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
821 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
822 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
823 // into the same temps, reducing the number of required temps down to 5. We shall work
824 // around the potentially locked temp by using LR for r_ptr, unconditionally.
825 // TODO: Pass information about the need for more temps to the stack frame generation
826 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700827 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
828 MarkTemp(rs_rARM_LR);
829 FreeTemp(rs_rARM_LR);
830 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000831 bool load_early = true;
832 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700833 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
834 rl_src_expected.reg;
835 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
836 rl_src_new_value.reg;
837 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
838 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800839 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
840 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000841
842 if (!expected_is_good_reg && !new_value_is_good_reg) {
843 // None of expected/new_value is non-temp reg, need to load both late
844 load_early = false;
845 // Make sure they are not in the temp regs and the load will not be skipped.
846 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800847 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000848 ClobberSReg(rl_src_expected.s_reg_low);
849 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
850 rl_src_expected.location = kLocDalvikFrame;
851 }
852 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800853 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000854 ClobberSReg(rl_src_new_value.s_reg_low);
855 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
856 rl_src_new_value.location = kLocDalvikFrame;
857 }
858 }
859 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860
Hans Boehm48f5c472014-06-27 14:50:10 -0700861 // Prevent reordering with prior memory operations.
862 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863
buzbeea0cd2d72014-06-01 09:33:49 -0700864 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000865 RegLocation rl_new_value;
866 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700867 rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000868 } else if (load_early) {
869 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
870 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871
Vladimir Marko1c282e22013-11-21 14:49:47 +0000872 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800874 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 }
876
877 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
878
buzbee2700f7e2014-03-07 09:46:20 -0800879 RegStorage r_ptr = rs_rARM_LR;
880 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881
882 // Free now unneeded rl_object and rl_offset to give more temps.
883 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700884 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700886 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887
Vladimir Marko3e5af822013-11-21 15:01:20 +0000888 RegLocation rl_expected;
889 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700890 rl_expected = LoadValue(rl_src_expected, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000891 } else if (load_early) {
892 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
893 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000894 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700895 RegStorage low_reg = AllocTemp();
896 RegStorage high_reg = AllocTemp();
897 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000898 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000899 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900
Vladimir Marko3e5af822013-11-21 15:01:20 +0000901 // do {
902 // tmp = [r_ptr] - expected;
903 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
904 // result = tmp != 0;
905
buzbee2700f7e2014-03-07 09:46:20 -0800906 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700907 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700908
Dave Allison3da67a52014-04-02 17:03:45 -0700909 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000910 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800911 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000912 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800913 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000914 }
buzbee2700f7e2014-03-07 09:46:20 -0800915 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
916 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
917 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000918 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800919 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000920 }
921 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700922 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800923 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000924 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800925 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000926 }
927 FreeTemp(r_tmp_high); // Now unneeded
928
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100929 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700930 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800931 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 +0000932
933 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800934 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
935 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100936 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700937 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800938 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000939 }
940
941 // Still one conditional left from OpIT(kCondEq, "T") from either branch
942 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700943 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700944
Jeff Hao2de2aa12013-09-12 17:20:31 -0700945 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946
Vladimir Marko3e5af822013-11-21 15:01:20 +0000947 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800948 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000949 }
950
Hans Boehm48f5c472014-06-27 14:50:10 -0700951 // Prevent reordering with subsequent memory operations.
952 GenMemBarrier(kLoadAny);
953
Vladimir Marko3e5af822013-11-21 15:01:20 +0000954 // result := (tmp1 != 0) ? 0 : 1;
955 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800956 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100957 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700958 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800959 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000960 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700961 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000962
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963 StoreValue(rl_dest, rl_result);
964
Vladimir Marko3e5af822013-11-21 15:01:20 +0000965 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700966 Clobber(rs_rARM_LR);
967 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 return true;
969}
970
Zheng Xu947717a2014-08-07 14:05:23 +0800971bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
972 constexpr int kLargeArrayThreshold = 256;
973
974 RegLocation rl_src = info->args[0];
975 RegLocation rl_src_pos = info->args[1];
976 RegLocation rl_dst = info->args[2];
977 RegLocation rl_dst_pos = info->args[3];
978 RegLocation rl_length = info->args[4];
979 // Compile time check, handle exception by non-inline method to reduce related meta-data.
980 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
981 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
982 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
983 return false;
984 }
985
986 ClobberCallerSave();
987 LockCallTemps(); // Prepare for explicit register usage.
988 LockTemp(rs_r12);
989 RegStorage rs_src = rs_r0;
990 RegStorage rs_dst = rs_r1;
991 LoadValueDirectFixed(rl_src, rs_src);
992 LoadValueDirectFixed(rl_dst, rs_dst);
993
994 // Handle null pointer exception in slow-path.
995 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
996 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
997 // Handle potential overlapping in slow-path.
998 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
999 // Handle exception or big length in slow-path.
1000 RegStorage rs_length = rs_r2;
1001 LoadValueDirectFixed(rl_length, rs_length);
1002 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
1003 // Src bounds check.
1004 RegStorage rs_pos = rs_r3;
1005 RegStorage rs_arr_length = rs_r12;
1006 LoadValueDirectFixed(rl_src_pos, rs_pos);
1007 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1008 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1009 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1010 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1011 // Dst bounds check.
1012 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1013 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1014 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1015 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1016 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1017
1018 // Everything is checked now.
1019 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1020 OpRegReg(kOpAdd, rs_dst, rs_pos);
1021 OpRegReg(kOpAdd, rs_dst, rs_pos);
1022 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1023 LoadValueDirectFixed(rl_src_pos, rs_pos);
1024 OpRegReg(kOpAdd, rs_src, rs_pos);
1025 OpRegReg(kOpAdd, rs_src, rs_pos);
1026
1027 RegStorage rs_tmp = rs_pos;
1028 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1029
1030 // Copy one element.
1031 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1032 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1033 OpRegImm(kOpSub, rs_length, 2);
1034 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1035 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1036
1037 // Copy two elements.
1038 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1039 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1040 OpRegImm(kOpSub, rs_length, 4);
1041 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1042 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1043 OpUnconditionalBranch(begin_loop);
1044
1045 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1046 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1047 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1048
1049 src_check_branch->target = check_failed;
1050 dst_check_branch->target = check_failed;
1051 src_dst_same->target = check_failed;
1052 len_neg_or_too_big->target = check_failed;
1053 src_pos_negative->target = check_failed;
1054 src_bad_len->target = check_failed;
1055 dst_pos_negative->target = check_failed;
1056 dst_bad_len->target = check_failed;
1057 jmp_to_begin_loop->target = begin_loop;
1058 jmp_to_ret->target = return_point;
1059
1060 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001061 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001062
1063 return true;
1064}
1065
buzbee2700f7e2014-03-07 09:46:20 -08001066LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
1067 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068}
1069
buzbee2700f7e2014-03-07 09:46:20 -08001070LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001071 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072}
1073
buzbee2700f7e2014-03-07 09:46:20 -08001074LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001075 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076}
1077
1078void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1079 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001080 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001081 UNUSED(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001082 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 EncodeShift(kArmLsl, second_bit - first_bit));
1084 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001085 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 }
1087}
1088
Mingyao Yange643a172014-04-08 11:02:52 -07001089void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001090 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1091 RegStorage t_reg = AllocTemp();
1092 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001094 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095}
1096
1097// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001098LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001099#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001100 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001102#else
1103 RegStorage t_reg = AllocTemp();
1104 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
1105 t_reg, kUnsignedHalf);
1106 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1107 0, target);
1108 FreeTemp(t_reg);
1109 return cmp_branch;
1110#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111}
1112
1113// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001114LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001116 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001117 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 return OpCondBranch(c_code, target);
1119}
1120
Andreas Gampeb14329f2014-05-15 11:16:06 -07001121bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001123 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1124 LIR* barrier = last_lir_insn_;
1125
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 int dmb_flavor;
1127 // TODO: revisit Arm barrier kinds
1128 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001129 case kAnyStore: dmb_flavor = kISH; break;
1130 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001131 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001132 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 default:
1134 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1135 dmb_flavor = kSY; // quiet gcc.
1136 break;
1137 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001138
Andreas Gampeb14329f2014-05-15 11:16:06 -07001139 bool ret = false;
1140
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001141 // If the same barrier already exists, don't generate another.
1142 if (barrier == nullptr
1143 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1144 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001145 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001146 }
1147
1148 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1149 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001150 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001151 return ret;
1152#else
1153 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154#endif
1155}
1156
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 rl_src = LoadValueWide(rl_src, kCoreReg);
1159 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001160 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 LoadConstantNoClobber(z_reg, 0);
1162 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001163 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1164 RegStorage t_reg = AllocTemp();
1165 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1166 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 FreeTemp(t_reg);
1168 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001169 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1170 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 }
1172 FreeTemp(z_reg);
1173 StoreValueWide(rl_dest, rl_result);
1174}
1175
Mark Mendelle02d48f2014-01-15 11:19:23 -08001176void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1177 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001178 UNUSED(opcode);
1179 /*
1180 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1181 * dest = src1.lo * src2.lo;
1182 * tmp1 += src1.lo * src2.hi;
1183 * dest.hi += tmp1;
1184 *
1185 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1186 * registers. Normally for Arm, we get 5. We can get to 6 by including
1187 * lr in the temp set. The only problematic case is all operands and result are
1188 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1189 * freeing operand temp registers after they are no longer needed. All other cases
1190 * can proceed normally. We'll just punt on the case of the result having a misaligned
1191 * overlap with either operand and send that case to a runtime handler.
1192 */
1193 RegLocation rl_result;
1194 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1195 FlushAllRegs();
1196 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1197 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001198 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001199 return;
1200 }
1201
1202 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1203 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1204
1205 int reg_status = 0;
1206 RegStorage res_lo;
1207 RegStorage res_hi;
1208 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1209 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1210 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1211 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1212 // Check if rl_dest is *not* either operand and we have enough temp registers.
1213 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1214 (dest_promoted || src1_promoted || src2_promoted)) {
1215 // In this case, we do not need to manually allocate temp registers for result.
1216 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1217 res_lo = rl_result.reg.GetLow();
1218 res_hi = rl_result.reg.GetHigh();
1219 } else {
1220 res_lo = AllocTemp();
1221 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1222 // In this case, we have enough temp registers to be allocated for result.
1223 res_hi = AllocTemp();
1224 reg_status = 1;
1225 } else {
1226 // In this case, all temps are now allocated.
1227 // res_hi will be allocated after we can free src1_hi.
1228 reg_status = 2;
1229 }
1230 }
1231
1232 // Temporarily add LR to the temp pool, and assign it to tmp1
1233 MarkTemp(rs_rARM_LR);
1234 FreeTemp(rs_rARM_LR);
1235 RegStorage tmp1 = rs_rARM_LR;
1236 LockTemp(rs_rARM_LR);
1237
1238 if (rl_src1.reg == rl_src2.reg) {
1239 DCHECK(res_hi.Valid());
1240 DCHECK(res_lo.Valid());
1241 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1242 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1243 rl_src1.reg.GetLowReg());
1244 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1245 } else {
1246 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1247 if (reg_status == 2) {
1248 DCHECK(!res_hi.Valid());
1249 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1250 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1251 // Will force free src1_hi, so must clobber.
1252 Clobber(rl_src1.reg);
1253 FreeTemp(rl_src1.reg.GetHigh());
1254 res_hi = AllocTemp();
1255 }
1256 DCHECK(res_hi.Valid());
1257 DCHECK(res_lo.Valid());
1258 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1259 rl_src1.reg.GetLowReg());
1260 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1261 tmp1.GetReg());
1262 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1263 if (reg_status == 2) {
1264 FreeTemp(rl_src1.reg.GetLow());
1265 }
1266 }
1267
1268 // Now, restore lr to its non-temp status.
1269 FreeTemp(tmp1);
1270 Clobber(rs_rARM_LR);
1271 UnmarkTemp(rs_rARM_LR);
1272
1273 if (reg_status != 0) {
1274 // We had manually allocated registers for rl_result.
1275 // Now construct a RegLocation.
1276 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1277 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1278 }
1279
1280 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281}
1282
Andreas Gampec76c6142014-08-04 16:30:03 -07001283void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001284 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001285 switch (opcode) {
1286 case Instruction::MUL_LONG:
1287 case Instruction::MUL_LONG_2ADDR:
1288 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1289 return;
1290 case Instruction::NEG_LONG:
1291 GenNegLong(rl_dest, rl_src2);
1292 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293
Andreas Gampec76c6142014-08-04 16:30:03 -07001294 default:
1295 break;
1296 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297
Andreas Gampec76c6142014-08-04 16:30:03 -07001298 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001299 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300}
1301
1302/*
1303 * Generate array load
1304 */
1305void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001306 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001307 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 int len_offset = mirror::Array::LengthOffset().Int32Value();
1309 int data_offset;
1310 RegLocation rl_result;
1311 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001312 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 if (!constant_index) {
1314 rl_index = LoadValue(rl_index, kCoreReg);
1315 }
1316
1317 if (rl_dest.wide) {
1318 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1319 } else {
1320 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1321 }
1322
1323 // If index is constant, just fold it into the data offset
1324 if (constant_index) {
1325 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1326 }
1327
1328 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001329 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330
1331 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001332 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 if (needs_range_check) {
1334 reg_len = AllocTemp();
1335 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001336 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001337 MarkPossibleNullPointerException(opt_flags);
1338 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001339 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 }
1341 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001342 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001344 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 } else {
1346 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001347 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001348 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001349 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 }
1351 rl_result = EvalLoc(rl_dest, reg_class, true);
1352
1353 if (needs_range_check) {
1354 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001355 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001357 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 }
1359 FreeTemp(reg_len);
1360 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001361 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001362 if (!constant_index) {
1363 FreeTemp(reg_ptr);
1364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 StoreValueWide(rl_dest, rl_result);
1367 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 StoreValue(rl_dest, rl_result);
1369 }
1370 } else {
1371 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001372 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001373 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001374 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 rl_result = EvalLoc(rl_dest, reg_class, true);
1376
1377 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001378 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 FreeTemp(reg_len);
1380 }
buzbee2700f7e2014-03-07 09:46:20 -08001381 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001382 FreeTemp(reg_ptr);
1383 StoreValue(rl_dest, rl_result);
1384 }
1385}
1386
1387/*
1388 * Generate array store
1389 *
1390 */
1391void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001392 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001393 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 bool constant_index = rl_index.is_const;
1396
Ian Rogersa9a82542013-10-04 11:17:26 -07001397 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001398 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1400 } else {
1401 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1402 }
1403
1404 // If index is constant, just fold it into the data offset.
1405 if (constant_index) {
1406 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1407 }
1408
buzbeea0cd2d72014-06-01 09:33:49 -07001409 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 if (!constant_index) {
1411 rl_index = LoadValue(rl_index, kCoreReg);
1412 }
1413
buzbee2700f7e2014-03-07 09:46:20 -08001414 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001415 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001417 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001418 } else if (IsTemp(rl_array.reg) && !card_mark) {
1419 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001420 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001421 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001422 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001423 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
1425
1426 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001427 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428
1429 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001430 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 if (needs_range_check) {
1432 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001433 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001435 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001436 MarkPossibleNullPointerException(opt_flags);
1437 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001438 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001439 }
1440 /* at this point, reg_ptr points to array, 2 live temps */
1441 if (rl_src.wide || rl_src.fp || constant_index) {
1442 if (rl_src.wide) {
1443 rl_src = LoadValueWide(rl_src, reg_class);
1444 } else {
1445 rl_src = LoadValue(rl_src, reg_class);
1446 }
1447 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001448 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 }
1450 if (needs_range_check) {
1451 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001452 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001453 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001454 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 }
1456 FreeTemp(reg_len);
1457 }
1458
Andreas Gampe3c12c512014-06-24 18:46:29 +00001459 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 } else {
1461 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001462 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 rl_src = LoadValue(rl_src, reg_class);
1464 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001465 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 FreeTemp(reg_len);
1467 }
buzbee2700f7e2014-03-07 09:46:20 -08001468 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 }
Ian Rogers773aab12013-10-14 13:50:10 -07001470 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 FreeTemp(reg_ptr);
1472 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001473 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001474 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 }
1476}
1477
Ian Rogersa9a82542013-10-04 11:17:26 -07001478
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001480 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1481 int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001482 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 rl_src = LoadValueWide(rl_src, kCoreReg);
1484 // Per spec, we only care about low 6 bits of shift amount.
1485 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1486 if (shift_amount == 0) {
1487 StoreValueWide(rl_dest, rl_src);
1488 return;
1489 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001490 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1492 return;
1493 }
1494 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001495 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 case Instruction::SHL_LONG:
1497 case Instruction::SHL_LONG_2ADDR:
1498 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001499 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1500 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001501 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001502 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1503 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001505 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1506 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001508 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001509 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001511 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 }
1513 break;
1514 case Instruction::SHR_LONG:
1515 case Instruction::SHR_LONG_2ADDR:
1516 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001517 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1518 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001520 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1521 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001523 RegStorage t_reg = AllocTemp();
1524 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001525 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 EncodeShift(kArmLsl, 32 - shift_amount));
1527 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001528 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 }
1530 break;
1531 case Instruction::USHR_LONG:
1532 case Instruction::USHR_LONG_2ADDR:
1533 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001534 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1535 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001537 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1538 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001540 RegStorage t_reg = AllocTemp();
1541 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001542 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 EncodeShift(kArmLsl, 32 - shift_amount));
1544 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001545 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 }
1547 break;
1548 default:
1549 LOG(FATAL) << "Unexpected case";
1550 }
1551 StoreValueWide(rl_dest, rl_result);
1552}
1553
1554void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001555 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1556 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1558 if (!rl_src2.is_const) {
1559 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001560 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 return;
1562 }
1563 } else {
1564 // Normalize
1565 if (!rl_src2.is_const) {
1566 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001567 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 }
1569 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001570 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001571 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001572 return;
1573 }
1574 DCHECK(rl_src2.is_const);
1575 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1576 uint32_t val_lo = Low32Bits(val);
1577 uint32_t val_hi = High32Bits(val);
1578 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1579 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1580
1581 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001582 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 case Instruction::ADD_LONG:
1584 case Instruction::ADD_LONG_2ADDR:
1585 case Instruction::SUB_LONG:
1586 case Instruction::SUB_LONG_2ADDR:
1587 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001588 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 return;
1590 }
1591 break;
1592 default:
1593 break;
1594 }
1595 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1596 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1597 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1598 switch (opcode) {
1599 case Instruction::ADD_LONG:
1600 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001601 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001602 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 break;
1604 case Instruction::OR_LONG:
1605 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001606 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1607 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001609 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
1612 break;
1613 case Instruction::XOR_LONG:
1614 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001615 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1616 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 break;
1618 case Instruction::AND_LONG:
1619 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001620 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1621 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001623 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001624 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 }
1626 break;
1627 case Instruction::SUB_LONG_2ADDR:
1628 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001629 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001630 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 break;
1632 default:
1633 LOG(FATAL) << "Unexpected opcode " << opcode;
1634 }
1635 StoreValueWide(rl_dest, rl_result);
1636}
1637
1638} // namespace art