blob: 9742243632393b4e58e30c6882795e56f72ab8b9 [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,
212 int dest_reg_class) {
213 // TODO: Generalize the IT below to accept more than one-instruction loads.
214 DCHECK(InexpensiveConstantInt(true_val));
215 DCHECK(InexpensiveConstantInt(false_val));
216
217 if ((true_val == 0 && code == kCondEq) ||
218 (false_val == 0 && code == kCondNe)) {
219 OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
220 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
221 LIR* it = OpIT(kCondNe, "");
222 LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
223 OpEndIT(it);
224 return;
225 }
226
227 OpRegReg(kOpCmp, left_op, right_op); // Same?
228 LIR* it = OpIT(code, "E"); // if-convert the test
229 LoadConstant(rs_dest, true_val); // .eq case - load true
230 LoadConstant(rs_dest, false_val); // .eq case - load true
231 OpEndIT(it);
232}
233
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700234void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 RegLocation rl_result;
236 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700238 // Avoid using float regs here.
239 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
240 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
241 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000242 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 if (mir->ssa_rep->num_uses == 1) {
244 // CONST case
245 int true_val = mir->dalvikInsn.vB;
246 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700247 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000248 // Change kCondNe to kCondEq for the special cases below.
249 if (ccode == kCondNe) {
250 ccode = kCondEq;
251 std::swap(true_val, false_val);
252 }
253 bool cheap_false_val = InexpensiveConstantInt(false_val);
254 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800255 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100256 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700257 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800258 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700259 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000260 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800261 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100262 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700263 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800264 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700265 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000266 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800267 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700268 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800269 LoadConstant(rl_result.reg, true_val);
270 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700271 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 } else {
273 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700274 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
275 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 LoadConstant(t_reg1, true_val);
277 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800278 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700279 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800280 OpRegCopy(rl_result.reg, t_reg1);
281 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700282 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 }
284 } else {
285 // MOVE case
286 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
287 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700288 rl_true = LoadValue(rl_true, result_reg_class);
289 rl_false = LoadValue(rl_false, result_reg_class);
290 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800291 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700292 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000293 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700294 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800295 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000296 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700297 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800298 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700299 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700300 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800301 OpRegCopy(rl_result.reg, rl_true.reg);
302 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700303 }
Dave Allison3da67a52014-04-02 17:03:45 -0700304 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 }
306 StoreValue(rl_dest, rl_result);
307}
308
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700309void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
311 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
312 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000313 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000315 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 ccode = FlipComparisonOrder(ccode);
317 }
318 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700319 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 // Do special compare/branch against simple const operand if not already in registers.
321 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700322 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
324 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
325 return;
326 }
327 }
buzbee0d829482013-10-11 15:24:55 -0700328 LIR* taken = &block_label_list_[bb->taken];
329 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
331 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800332 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700333 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 case kCondEq:
335 OpCondBranch(kCondNe, not_taken);
336 break;
337 case kCondNe:
338 OpCondBranch(kCondNe, taken);
339 break;
340 case kCondLt:
341 OpCondBranch(kCondLt, taken);
342 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000343 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 break;
345 case kCondLe:
346 OpCondBranch(kCondLt, taken);
347 OpCondBranch(kCondGt, not_taken);
348 ccode = kCondLs;
349 break;
350 case kCondGt:
351 OpCondBranch(kCondGt, taken);
352 OpCondBranch(kCondLt, not_taken);
353 ccode = kCondHi;
354 break;
355 case kCondGe:
356 OpCondBranch(kCondGt, taken);
357 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000358 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 break;
360 default:
361 LOG(FATAL) << "Unexpected ccode: " << ccode;
362 }
buzbee2700f7e2014-03-07 09:46:20 -0800363 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 OpCondBranch(ccode, taken);
365}
366
367/*
368 * Generate a register comparison to an immediate and branch. Caller
369 * is responsible for setting branch target field.
370 */
buzbee2700f7e2014-03-07 09:46:20 -0800371LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700372 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700374 /*
375 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
376 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700377 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700378 * be converted to a long form during assembly (which will trigger another assembly
379 * pass). Here we estimate the branch distance for checks, and if large directly
380 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700381 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700382 */
383 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700384 skip &= ((mir_graph_->GetNumDalvikInsns() - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700385 if (!skip && reg.Low8() && (check_value == 0)) {
386 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
387 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
388 reg.GetReg(), 0);
389 } else if (arm_cond == kArmCondLs) {
390 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
391 // This case happens for a bounds check of array[0].
392 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
393 }
394 }
395
396 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000397 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 branch = NewLIR2(kThumbBCond, 0, arm_cond);
399 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700400
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 branch->target = target;
402 return branch;
403}
404
buzbee2700f7e2014-03-07 09:46:20 -0800405LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 LIR* res;
407 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800408 // If src or dest is a pair, we'll be using low reg.
409 if (r_dest.IsPair()) {
410 r_dest = r_dest.GetLow();
411 }
412 if (r_src.IsPair()) {
413 r_src = r_src.GetLow();
414 }
buzbee091cc402014-03-31 10:14:40 -0700415 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700417 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700419 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700421 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 opcode = kThumbMovRR_H2L;
423 else
424 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800425 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
427 res->flags.is_nop = true;
428 }
429 return res;
430}
431
buzbee7a11ab02014-04-28 20:02:38 -0700432void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
433 if (r_dest != r_src) {
434 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
435 AppendLIR(res);
436 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437}
438
buzbee2700f7e2014-03-07 09:46:20 -0800439void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700440 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700441 bool dest_fp = r_dest.IsFloat();
442 bool src_fp = r_src.IsFloat();
443 DCHECK(r_dest.Is64Bit());
444 DCHECK(r_src.Is64Bit());
buzbee7a11ab02014-04-28 20:02:38 -0700445 if (dest_fp) {
446 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700447 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 } else {
buzbee091cc402014-03-31 10:14:40 -0700449 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700450 }
451 } else {
452 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700453 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700454 } else {
455 // Handle overlap
456 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
457 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
458 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
459 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
460 } else {
461 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
462 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
463 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
465 }
466 }
467}
468
469// Table of magic divisors
470struct MagicTable {
471 uint32_t magic;
472 uint32_t shift;
473 DividePattern pattern;
474};
475
476static const MagicTable magic_table[] = {
477 {0, 0, DivideNone}, // 0
478 {0, 0, DivideNone}, // 1
479 {0, 0, DivideNone}, // 2
480 {0x55555556, 0, Divide3}, // 3
481 {0, 0, DivideNone}, // 4
482 {0x66666667, 1, Divide5}, // 5
483 {0x2AAAAAAB, 0, Divide3}, // 6
484 {0x92492493, 2, Divide7}, // 7
485 {0, 0, DivideNone}, // 8
486 {0x38E38E39, 1, Divide5}, // 9
487 {0x66666667, 2, Divide5}, // 10
488 {0x2E8BA2E9, 1, Divide5}, // 11
489 {0x2AAAAAAB, 1, Divide5}, // 12
490 {0x4EC4EC4F, 2, Divide5}, // 13
491 {0x92492493, 3, Divide7}, // 14
492 {0x88888889, 3, Divide7}, // 15
493};
494
495// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700496bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
499 return false;
500 }
501 DividePattern pattern = magic_table[lit].pattern;
502 if (pattern == DivideNone) {
503 return false;
504 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505
buzbee2700f7e2014-03-07 09:46:20 -0800506 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 LoadConstant(r_magic, magic_table[lit].magic);
508 rl_src = LoadValue(rl_src, kCoreReg);
509 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800510 RegStorage r_hi = AllocTemp();
511 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100512
513 // rl_dest and rl_src might overlap.
514 // Reuse r_hi to save the div result for reminder case.
515 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
516
buzbee2700f7e2014-03-07 09:46:20 -0800517 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700518 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100520 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 break;
522 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800523 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100524 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700525 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 break;
527 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800528 OpRegReg(kOpAdd, r_hi, rl_src.reg);
529 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100530 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700531 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 break;
533 default:
534 LOG(FATAL) << "Unexpected pattern: " << pattern;
535 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100536
537 if (!is_div) {
538 // div_result = src / lit
539 // tmp1 = div_result * lit
540 // dest = src - tmp1
541 RegStorage tmp1 = r_lo;
542 EasyMultiplyOp ops[2];
543
544 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
545 DCHECK_NE(canEasyMultiply, false);
546
547 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
548 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
549 }
550
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 StoreValue(rl_dest, rl_result);
552 return true;
553}
554
Ian Rogerse2143c02014-03-28 08:47:16 -0700555// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
556bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
557 if (IsPowerOfTwo(lit)) {
558 op->op = kOpLsl;
559 op->shift = LowestSetBit(lit);
560 return true;
561 }
562
563 if (IsPowerOfTwo(lit - 1)) {
564 op->op = kOpAdd;
565 op->shift = LowestSetBit(lit - 1);
566 return true;
567 }
568
569 if (IsPowerOfTwo(lit + 1)) {
570 op->op = kOpRsub;
571 op->shift = LowestSetBit(lit + 1);
572 return true;
573 }
574
575 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100576 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700577 return false;
578}
579
580// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
581bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
582 GetEasyMultiplyOp(lit, &ops[0]);
583 if (GetEasyMultiplyOp(lit, &ops[0])) {
584 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100585 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700586 return true;
587 }
588
589 int lit1 = lit;
590 uint32_t shift = LowestSetBit(lit1);
591 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
592 ops[1].op = kOpLsl;
593 ops[1].shift = shift;
594 return true;
595 }
596
597 lit1 = lit - 1;
598 shift = LowestSetBit(lit1);
599 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
600 ops[1].op = kOpAdd;
601 ops[1].shift = shift;
602 return true;
603 }
604
605 lit1 = lit + 1;
606 shift = LowestSetBit(lit1);
607 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
608 ops[1].op = kOpRsub;
609 ops[1].shift = shift;
610 return true;
611 }
612
613 return false;
614}
615
Zheng Xuf9719f92014-04-02 13:31:31 +0100616// Generate instructions to do multiply.
617// Additional temporary register is required,
618// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700619void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100620 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
621 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
622
623 RegStorage r_tmp1;
624 if (ops[1].op == kOpInvalid) {
625 r_tmp1 = r_dest;
626 } else if (r_dest.GetReg() != r_src.GetReg()) {
627 r_tmp1 = r_dest;
628 } else {
629 r_tmp1 = AllocTemp();
630 }
631
632 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700633 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100634 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700635 break;
636 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100637 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700638 break;
639 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100640 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700641 break;
642 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100643 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700644 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100645 }
646
647 switch (ops[1].op) {
648 case kOpInvalid:
649 return;
650 case kOpLsl:
651 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
652 break;
653 case kOpAdd:
654 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
655 break;
656 case kOpRsub:
657 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
658 break;
659 default:
660 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
661 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700662 }
663}
664
665bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
666 EasyMultiplyOp ops[2];
667
668 if (!GetEasyMultiplyTwoOps(lit, ops)) {
669 return false;
670 }
671
672 rl_src = LoadValue(rl_src, kCoreReg);
673 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
674
675 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
676 StoreValue(rl_dest, rl_result);
677 return true;
678}
679
Mark Mendell2bf31e62014-01-23 12:13:40 -0800680RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700681 RegLocation rl_src2, bool is_div, int flags) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800682 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
683 return rl_dest;
684}
685
686RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
687 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
688 return rl_dest;
689}
690
buzbee2700f7e2014-03-07 09:46:20 -0800691RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700692 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
693
694 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800695 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700696 LoadConstant(lit_temp, lit);
697 // Use the generic case for div/rem with arg2 in a register.
698 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
699 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
700 FreeTemp(lit_temp);
701
702 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703}
704
buzbee2700f7e2014-03-07 09:46:20 -0800705RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
708 if (is_div) {
709 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800710 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700711 } else {
712 // Remainder case, use the following code:
713 // temp = reg1 / reg2 - integer division
714 // temp = temp * reg2
715 // dest = reg1 - temp
716
buzbee2700f7e2014-03-07 09:46:20 -0800717 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700718 OpRegRegReg(kOpDiv, temp, reg1, reg2);
719 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800720 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700721 FreeTemp(temp);
722 }
723
724 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725}
726
Serban Constantinescu23abec92014-07-02 16:13:38 +0100727bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100729 if (is_long) {
730 return false;
731 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 RegLocation rl_src1 = info->args[0];
733 RegLocation rl_src2 = info->args[1];
734 rl_src1 = LoadValue(rl_src1, kCoreReg);
735 rl_src2 = LoadValue(rl_src2, kCoreReg);
736 RegLocation rl_dest = InlineTarget(info);
737 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800738 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700739 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800740 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
741 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700742 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 StoreValue(rl_dest, rl_result);
744 return true;
745}
746
Vladimir Markoe508a202013-11-04 15:24:22 +0000747bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
748 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800749 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000750 RegLocation rl_dest = InlineTarget(info);
751 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
752 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700753 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000754 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800755 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700756 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
757 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000758 } else {
buzbee695d13a2014-04-19 13:32:20 -0700759 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
760 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000761 }
762 StoreValueWide(rl_dest, rl_result);
763 } else {
buzbee695d13a2014-04-19 13:32:20 -0700764 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000765 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000766 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000767 StoreValue(rl_dest, rl_result);
768 }
769 return true;
770}
771
772bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
773 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800774 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000775 RegLocation rl_src_value = info->args[2]; // [size] value
776 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700777 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000778 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
779 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000780 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
781 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000782 } else {
buzbee695d13a2014-04-19 13:32:20 -0700783 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000784 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
785 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000786 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000787 }
788 return true;
789}
790
Hans Boehm48f5c472014-06-27 14:50:10 -0700791// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000792bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 DCHECK_EQ(cu_->instruction_set, kThumb2);
794 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000795 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
796 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800797 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000798 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000799 // If is_long, high half is in info->args[5]
800 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
801 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 RegLocation rl_dest = InlineTarget(info); // boolean place for result
803
Vladimir Marko3e5af822013-11-21 15:01:20 +0000804 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
805 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
806 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
807 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
808 // into the same temps, reducing the number of required temps down to 5. We shall work
809 // around the potentially locked temp by using LR for r_ptr, unconditionally.
810 // TODO: Pass information about the need for more temps to the stack frame generation
811 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700812 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
813 MarkTemp(rs_rARM_LR);
814 FreeTemp(rs_rARM_LR);
815 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000816 bool load_early = true;
817 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700818 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
819 rl_src_expected.reg;
820 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
821 rl_src_new_value.reg;
822 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
823 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800824 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
825 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000826
827 if (!expected_is_good_reg && !new_value_is_good_reg) {
828 // None of expected/new_value is non-temp reg, need to load both late
829 load_early = false;
830 // Make sure they are not in the temp regs and the load will not be skipped.
831 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800832 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000833 ClobberSReg(rl_src_expected.s_reg_low);
834 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
835 rl_src_expected.location = kLocDalvikFrame;
836 }
837 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800838 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000839 ClobberSReg(rl_src_new_value.s_reg_low);
840 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
841 rl_src_new_value.location = kLocDalvikFrame;
842 }
843 }
844 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845
Hans Boehm48f5c472014-06-27 14:50:10 -0700846 // Prevent reordering with prior memory operations.
847 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848
buzbeea0cd2d72014-06-01 09:33:49 -0700849 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000850 RegLocation rl_new_value;
851 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700852 rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000853 } else if (load_early) {
854 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
855 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856
Vladimir Marko1c282e22013-11-21 14:49:47 +0000857 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800859 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 }
861
862 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
863
buzbee2700f7e2014-03-07 09:46:20 -0800864 RegStorage r_ptr = rs_rARM_LR;
865 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866
867 // Free now unneeded rl_object and rl_offset to give more temps.
868 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700869 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700871 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872
Vladimir Marko3e5af822013-11-21 15:01:20 +0000873 RegLocation rl_expected;
874 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700875 rl_expected = LoadValue(rl_src_expected, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000876 } else if (load_early) {
877 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
878 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000879 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700880 RegStorage low_reg = AllocTemp();
881 RegStorage high_reg = AllocTemp();
882 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000883 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000884 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885
Vladimir Marko3e5af822013-11-21 15:01:20 +0000886 // do {
887 // tmp = [r_ptr] - expected;
888 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
889 // result = tmp != 0;
890
buzbee2700f7e2014-03-07 09:46:20 -0800891 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700892 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700893
Dave Allison3da67a52014-04-02 17:03:45 -0700894 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000895 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800896 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000897 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800898 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000899 }
buzbee2700f7e2014-03-07 09:46:20 -0800900 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
901 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
902 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000903 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800904 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000905 }
906 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700907 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800908 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000909 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800910 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000911 }
912 FreeTemp(r_tmp_high); // Now unneeded
913
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100914 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700915 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800916 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 +0000917
918 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800919 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
920 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100921 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700922 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800923 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000924 }
925
926 // Still one conditional left from OpIT(kCondEq, "T") from either branch
927 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700928 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700929
Jeff Hao2de2aa12013-09-12 17:20:31 -0700930 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700931
Vladimir Marko3e5af822013-11-21 15:01:20 +0000932 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800933 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000934 }
935
Hans Boehm48f5c472014-06-27 14:50:10 -0700936 // Prevent reordering with subsequent memory operations.
937 GenMemBarrier(kLoadAny);
938
Vladimir Marko3e5af822013-11-21 15:01:20 +0000939 // result := (tmp1 != 0) ? 0 : 1;
940 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800941 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100942 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700943 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800944 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000945 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700946 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000947
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 StoreValue(rl_dest, rl_result);
949
Vladimir Marko3e5af822013-11-21 15:01:20 +0000950 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700951 Clobber(rs_rARM_LR);
952 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 return true;
954}
955
Zheng Xu947717a2014-08-07 14:05:23 +0800956bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
957 constexpr int kLargeArrayThreshold = 256;
958
959 RegLocation rl_src = info->args[0];
960 RegLocation rl_src_pos = info->args[1];
961 RegLocation rl_dst = info->args[2];
962 RegLocation rl_dst_pos = info->args[3];
963 RegLocation rl_length = info->args[4];
964 // Compile time check, handle exception by non-inline method to reduce related meta-data.
965 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
966 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
967 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
968 return false;
969 }
970
971 ClobberCallerSave();
972 LockCallTemps(); // Prepare for explicit register usage.
973 LockTemp(rs_r12);
974 RegStorage rs_src = rs_r0;
975 RegStorage rs_dst = rs_r1;
976 LoadValueDirectFixed(rl_src, rs_src);
977 LoadValueDirectFixed(rl_dst, rs_dst);
978
979 // Handle null pointer exception in slow-path.
980 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
981 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
982 // Handle potential overlapping in slow-path.
983 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
984 // Handle exception or big length in slow-path.
985 RegStorage rs_length = rs_r2;
986 LoadValueDirectFixed(rl_length, rs_length);
987 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
988 // Src bounds check.
989 RegStorage rs_pos = rs_r3;
990 RegStorage rs_arr_length = rs_r12;
991 LoadValueDirectFixed(rl_src_pos, rs_pos);
992 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
993 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
994 OpRegReg(kOpSub, rs_arr_length, rs_pos);
995 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
996 // Dst bounds check.
997 LoadValueDirectFixed(rl_dst_pos, rs_pos);
998 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
999 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1000 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1001 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1002
1003 // Everything is checked now.
1004 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1005 OpRegReg(kOpAdd, rs_dst, rs_pos);
1006 OpRegReg(kOpAdd, rs_dst, rs_pos);
1007 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1008 LoadValueDirectFixed(rl_src_pos, rs_pos);
1009 OpRegReg(kOpAdd, rs_src, rs_pos);
1010 OpRegReg(kOpAdd, rs_src, rs_pos);
1011
1012 RegStorage rs_tmp = rs_pos;
1013 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1014
1015 // Copy one element.
1016 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1017 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1018 OpRegImm(kOpSub, rs_length, 2);
1019 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1020 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1021
1022 // Copy two elements.
1023 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1024 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1025 OpRegImm(kOpSub, rs_length, 4);
1026 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1027 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1028 OpUnconditionalBranch(begin_loop);
1029
1030 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1031 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1032 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1033
1034 src_check_branch->target = check_failed;
1035 dst_check_branch->target = check_failed;
1036 src_dst_same->target = check_failed;
1037 len_neg_or_too_big->target = check_failed;
1038 src_pos_negative->target = check_failed;
1039 src_bad_len->target = check_failed;
1040 dst_pos_negative->target = check_failed;
1041 dst_bad_len->target = check_failed;
1042 jmp_to_begin_loop->target = begin_loop;
1043 jmp_to_ret->target = return_point;
1044
1045 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001046 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001047
1048 return true;
1049}
1050
buzbee2700f7e2014-03-07 09:46:20 -08001051LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
1052 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053}
1054
buzbee2700f7e2014-03-07 09:46:20 -08001055LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001056 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057}
1058
buzbee2700f7e2014-03-07 09:46:20 -08001059LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001060 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061}
1062
1063void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1064 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001065 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001066 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 EncodeShift(kArmLsl, second_bit - first_bit));
1068 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001069 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 }
1071}
1072
Mingyao Yange643a172014-04-08 11:02:52 -07001073void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001074 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1075 RegStorage t_reg = AllocTemp();
1076 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001078 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079}
1080
1081// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001083#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001084 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001086#else
1087 RegStorage t_reg = AllocTemp();
1088 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
1089 t_reg, kUnsignedHalf);
1090 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1091 0, target);
1092 FreeTemp(t_reg);
1093 return cmp_branch;
1094#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095}
1096
1097// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001098LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001100 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001101 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 return OpCondBranch(c_code, target);
1103}
1104
Andreas Gampeb14329f2014-05-15 11:16:06 -07001105bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001107 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1108 LIR* barrier = last_lir_insn_;
1109
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 int dmb_flavor;
1111 // TODO: revisit Arm barrier kinds
1112 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001113 case kAnyStore: dmb_flavor = kISH; break;
1114 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001115 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001116 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 default:
1118 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1119 dmb_flavor = kSY; // quiet gcc.
1120 break;
1121 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001122
Andreas Gampeb14329f2014-05-15 11:16:06 -07001123 bool ret = false;
1124
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001125 // If the same barrier already exists, don't generate another.
1126 if (barrier == nullptr
1127 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1128 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001129 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001130 }
1131
1132 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1133 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001134 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001135 return ret;
1136#else
1137 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138#endif
1139}
1140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001141void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 rl_src = LoadValueWide(rl_src, kCoreReg);
1143 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001144 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 LoadConstantNoClobber(z_reg, 0);
1146 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001147 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1148 RegStorage t_reg = AllocTemp();
1149 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1150 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 FreeTemp(t_reg);
1152 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001153 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1154 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 }
1156 FreeTemp(z_reg);
1157 StoreValueWide(rl_dest, rl_result);
1158}
1159
Mark Mendelle02d48f2014-01-15 11:19:23 -08001160void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1161 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 /*
Zheng Xud7f8e022014-03-13 13:40:30 +00001163 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1164 * dest = src1.lo * src2.lo;
1165 * tmp1 += src1.lo * src2.hi;
1166 * dest.hi += tmp1;
1167 *
1168 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 * registers. Normally for Arm, we get 5. We can get to 6 by including
1170 * lr in the temp set. The only problematic case is all operands and result are
1171 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1172 * freeing operand temp registers after they are no longer needed. All other cases
1173 * can proceed normally. We'll just punt on the case of the result having a misaligned
1174 * overlap with either operand and send that case to a runtime handler.
1175 */
1176 RegLocation rl_result;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001177 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001179 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001180 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 StoreValueWide(rl_dest, rl_result);
1182 return;
1183 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001184
1185 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1186 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1187
1188 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001189 RegStorage res_lo;
1190 RegStorage res_hi;
1191 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
buzbee091cc402014-03-31 10:14:40 -07001192 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1193 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1194 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001195 // Check if rl_dest is *not* either operand and we have enough temp registers.
1196 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1197 (dest_promoted || src1_promoted || src2_promoted)) {
1198 // In this case, we do not need to manually allocate temp registers for result.
1199 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001200 res_lo = rl_result.reg.GetLow();
1201 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001202 } else {
1203 res_lo = AllocTemp();
1204 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1205 // In this case, we have enough temp registers to be allocated for result.
1206 res_hi = AllocTemp();
1207 reg_status = 1;
1208 } else {
1209 // In this case, all temps are now allocated.
1210 // res_hi will be allocated after we can free src1_hi.
1211 reg_status = 2;
1212 }
1213 }
1214
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 // Temporarily add LR to the temp pool, and assign it to tmp1
buzbee091cc402014-03-31 10:14:40 -07001216 MarkTemp(rs_rARM_LR);
1217 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001218 RegStorage tmp1 = rs_rARM_LR;
buzbee091cc402014-03-31 10:14:40 -07001219 LockTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220
buzbee2700f7e2014-03-07 09:46:20 -08001221 if (rl_src1.reg == rl_src2.reg) {
1222 DCHECK(res_hi.Valid());
1223 DCHECK(res_lo.Valid());
1224 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1225 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1226 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001227 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001229 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001230 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001231 DCHECK(!res_hi.Valid());
1232 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001233 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
buzbee082833c2014-05-17 23:16:26 -07001234 // Will force free src1_hi, so must clobber.
1235 Clobber(rl_src1.reg);
buzbee091cc402014-03-31 10:14:40 -07001236 FreeTemp(rl_src1.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001237 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
buzbee2700f7e2014-03-07 09:46:20 -08001239 DCHECK(res_hi.Valid());
1240 DCHECK(res_lo.Valid());
1241 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1242 rl_src1.reg.GetLowReg());
1243 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1244 tmp1.GetReg());
1245 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001246 if (reg_status == 2) {
buzbee082833c2014-05-17 23:16:26 -07001247 FreeTemp(rl_src1.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 }
1249 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001250
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001252 FreeTemp(tmp1);
buzbee091cc402014-03-31 10:14:40 -07001253 Clobber(rs_rARM_LR);
1254 UnmarkTemp(rs_rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001255
1256 if (reg_status != 0) {
1257 // We had manually allocated registers for rl_result.
1258 // Now construct a RegLocation.
buzbeea0cd2d72014-06-01 09:33:49 -07001259 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001260 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001261 }
1262
1263 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264}
1265
Andreas Gampec76c6142014-08-04 16:30:03 -07001266void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001267 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001268 switch (opcode) {
1269 case Instruction::MUL_LONG:
1270 case Instruction::MUL_LONG_2ADDR:
1271 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1272 return;
1273 case Instruction::NEG_LONG:
1274 GenNegLong(rl_dest, rl_src2);
1275 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276
Andreas Gampec76c6142014-08-04 16:30:03 -07001277 default:
1278 break;
1279 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280
Andreas Gampec76c6142014-08-04 16:30:03 -07001281 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001282 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283}
1284
1285/*
1286 * Generate array load
1287 */
1288void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001289 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001290 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 int len_offset = mirror::Array::LengthOffset().Int32Value();
1292 int data_offset;
1293 RegLocation rl_result;
1294 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001295 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 if (!constant_index) {
1297 rl_index = LoadValue(rl_index, kCoreReg);
1298 }
1299
1300 if (rl_dest.wide) {
1301 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1302 } else {
1303 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1304 }
1305
1306 // If index is constant, just fold it into the data offset
1307 if (constant_index) {
1308 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1309 }
1310
1311 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001312 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313
1314 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001315 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001316 if (needs_range_check) {
1317 reg_len = AllocTemp();
1318 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001319 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001320 MarkPossibleNullPointerException(opt_flags);
1321 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001322 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323 }
1324 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001325 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001327 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 } else {
1329 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001330 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001331 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001332 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 }
1334 rl_result = EvalLoc(rl_dest, reg_class, true);
1335
1336 if (needs_range_check) {
1337 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001338 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001339 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001340 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 }
1342 FreeTemp(reg_len);
1343 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001344 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001345 MarkPossibleNullPointerException(opt_flags);
1346 if (!constant_index) {
1347 FreeTemp(reg_ptr);
1348 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 StoreValueWide(rl_dest, rl_result);
1351 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 StoreValue(rl_dest, rl_result);
1353 }
1354 } else {
1355 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001356 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001357 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001358 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 rl_result = EvalLoc(rl_dest, reg_class, true);
1360
1361 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001362 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 FreeTemp(reg_len);
1364 }
buzbee2700f7e2014-03-07 09:46:20 -08001365 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001366 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367 FreeTemp(reg_ptr);
1368 StoreValue(rl_dest, rl_result);
1369 }
1370}
1371
1372/*
1373 * Generate array store
1374 *
1375 */
1376void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001377 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001378 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 bool constant_index = rl_index.is_const;
1381
Ian Rogersa9a82542013-10-04 11:17:26 -07001382 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001383 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1385 } else {
1386 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1387 }
1388
1389 // If index is constant, just fold it into the data offset.
1390 if (constant_index) {
1391 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1392 }
1393
buzbeea0cd2d72014-06-01 09:33:49 -07001394 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 if (!constant_index) {
1396 rl_index = LoadValue(rl_index, kCoreReg);
1397 }
1398
buzbee2700f7e2014-03-07 09:46:20 -08001399 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001400 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001402 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001403 } else if (IsTemp(rl_array.reg) && !card_mark) {
1404 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001405 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001407 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001408 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001409 }
1410
1411 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001412 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413
1414 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001415 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416 if (needs_range_check) {
1417 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001418 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001420 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001421 MarkPossibleNullPointerException(opt_flags);
1422 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001423 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
1425 /* at this point, reg_ptr points to array, 2 live temps */
1426 if (rl_src.wide || rl_src.fp || constant_index) {
1427 if (rl_src.wide) {
1428 rl_src = LoadValueWide(rl_src, reg_class);
1429 } else {
1430 rl_src = LoadValue(rl_src, reg_class);
1431 }
1432 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001433 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 }
1435 if (needs_range_check) {
1436 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001437 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001439 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 }
1441 FreeTemp(reg_len);
1442 }
1443
Andreas Gampe3c12c512014-06-24 18:46:29 +00001444 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Dave Allisonb373e092014-02-20 16:06:36 -08001445 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 } else {
1447 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001448 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 rl_src = LoadValue(rl_src, reg_class);
1450 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001451 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 FreeTemp(reg_len);
1453 }
buzbee2700f7e2014-03-07 09:46:20 -08001454 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001455 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 }
Ian Rogers773aab12013-10-14 13:50:10 -07001457 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 FreeTemp(reg_ptr);
1459 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001460 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001461 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 }
1463}
1464
Ian Rogersa9a82542013-10-04 11:17:26 -07001465
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001467 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1468 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 rl_src = LoadValueWide(rl_src, kCoreReg);
1470 // Per spec, we only care about low 6 bits of shift amount.
1471 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1472 if (shift_amount == 0) {
1473 StoreValueWide(rl_dest, rl_src);
1474 return;
1475 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001476 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1478 return;
1479 }
1480 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001481 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 case Instruction::SHL_LONG:
1483 case Instruction::SHL_LONG_2ADDR:
1484 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001485 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1486 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001488 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1489 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001491 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1492 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001494 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001495 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001497 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001498 }
1499 break;
1500 case Instruction::SHR_LONG:
1501 case Instruction::SHR_LONG_2ADDR:
1502 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001503 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1504 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001506 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1507 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001509 RegStorage t_reg = AllocTemp();
1510 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001511 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512 EncodeShift(kArmLsl, 32 - shift_amount));
1513 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001514 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 }
1516 break;
1517 case Instruction::USHR_LONG:
1518 case Instruction::USHR_LONG_2ADDR:
1519 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001520 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1521 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001523 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1524 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001526 RegStorage t_reg = AllocTemp();
1527 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001528 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001529 EncodeShift(kArmLsl, 32 - shift_amount));
1530 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001531 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 }
1533 break;
1534 default:
1535 LOG(FATAL) << "Unexpected case";
1536 }
1537 StoreValueWide(rl_dest, rl_result);
1538}
1539
1540void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001541 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1542 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001543 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1544 if (!rl_src2.is_const) {
1545 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001546 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 return;
1548 }
1549 } else {
1550 // Normalize
1551 if (!rl_src2.is_const) {
1552 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001553 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001554 }
1555 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001556 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001557 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 return;
1559 }
1560 DCHECK(rl_src2.is_const);
1561 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1562 uint32_t val_lo = Low32Bits(val);
1563 uint32_t val_hi = High32Bits(val);
1564 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1565 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1566
1567 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001568 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 case Instruction::ADD_LONG:
1570 case Instruction::ADD_LONG_2ADDR:
1571 case Instruction::SUB_LONG:
1572 case Instruction::SUB_LONG_2ADDR:
1573 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001574 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001575 return;
1576 }
1577 break;
1578 default:
1579 break;
1580 }
1581 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1582 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1583 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1584 switch (opcode) {
1585 case Instruction::ADD_LONG:
1586 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001587 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001588 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001589 break;
1590 case Instruction::OR_LONG:
1591 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001592 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1593 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001594 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001595 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001596 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 }
1598 break;
1599 case Instruction::XOR_LONG:
1600 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001601 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1602 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 break;
1604 case Instruction::AND_LONG:
1605 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001606 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1607 OpRegRegImm(kOpAnd, 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 != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001611 }
1612 break;
1613 case Instruction::SUB_LONG_2ADDR:
1614 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001615 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001616 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 break;
1618 default:
1619 LOG(FATAL) << "Unexpected opcode " << opcode;
1620 }
1621 StoreValueWide(rl_dest, rl_result);
1622}
1623
1624} // namespace art