blob: 8e08f5fb9d14221be5e2791c7c3a054852769724 [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());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800445 // Note: If the register is get by register allocator, it should never be a pair.
446 // But some functions in mir_2_lir assume 64-bit registers are 32-bit register pairs.
447 // TODO: Rework Mir2Lir::LoadArg() and Mir2Lir::LoadArgDirect().
448 if (dest_fp && r_dest.IsPair()) {
449 r_dest = As64BitFloatReg(r_dest);
450 }
451 if (src_fp && r_src.IsPair()) {
452 r_src = As64BitFloatReg(r_src);
453 }
buzbee7a11ab02014-04-28 20:02:38 -0700454 if (dest_fp) {
455 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700456 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 } else {
buzbee091cc402014-03-31 10:14:40 -0700458 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700459 }
460 } else {
461 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700462 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700463 } else {
464 // Handle overlap
465 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
466 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
467 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
468 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
469 } else {
470 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
471 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
472 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 }
474 }
475 }
476}
477
478// Table of magic divisors
479struct MagicTable {
480 uint32_t magic;
481 uint32_t shift;
482 DividePattern pattern;
483};
484
485static const MagicTable magic_table[] = {
486 {0, 0, DivideNone}, // 0
487 {0, 0, DivideNone}, // 1
488 {0, 0, DivideNone}, // 2
489 {0x55555556, 0, Divide3}, // 3
490 {0, 0, DivideNone}, // 4
491 {0x66666667, 1, Divide5}, // 5
492 {0x2AAAAAAB, 0, Divide3}, // 6
493 {0x92492493, 2, Divide7}, // 7
494 {0, 0, DivideNone}, // 8
495 {0x38E38E39, 1, Divide5}, // 9
496 {0x66666667, 2, Divide5}, // 10
497 {0x2E8BA2E9, 1, Divide5}, // 11
498 {0x2AAAAAAB, 1, Divide5}, // 12
499 {0x4EC4EC4F, 2, Divide5}, // 13
500 {0x92492493, 3, Divide7}, // 14
501 {0x88888889, 3, Divide7}, // 15
502};
503
504// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700505bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700506 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
508 return false;
509 }
510 DividePattern pattern = magic_table[lit].pattern;
511 if (pattern == DivideNone) {
512 return false;
513 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514
buzbee2700f7e2014-03-07 09:46:20 -0800515 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 LoadConstant(r_magic, magic_table[lit].magic);
517 rl_src = LoadValue(rl_src, kCoreReg);
518 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800519 RegStorage r_hi = AllocTemp();
520 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100521
522 // rl_dest and rl_src might overlap.
523 // Reuse r_hi to save the div result for reminder case.
524 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
525
buzbee2700f7e2014-03-07 09:46:20 -0800526 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700527 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100529 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 break;
531 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800532 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100533 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700534 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 break;
536 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800537 OpRegReg(kOpAdd, r_hi, rl_src.reg);
538 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100539 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700540 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 break;
542 default:
543 LOG(FATAL) << "Unexpected pattern: " << pattern;
544 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100545
546 if (!is_div) {
547 // div_result = src / lit
548 // tmp1 = div_result * lit
549 // dest = src - tmp1
550 RegStorage tmp1 = r_lo;
551 EasyMultiplyOp ops[2];
552
553 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
554 DCHECK_NE(canEasyMultiply, false);
555
556 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
557 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
558 }
559
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 StoreValue(rl_dest, rl_result);
561 return true;
562}
563
Ian Rogerse2143c02014-03-28 08:47:16 -0700564// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
565bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
566 if (IsPowerOfTwo(lit)) {
567 op->op = kOpLsl;
568 op->shift = LowestSetBit(lit);
569 return true;
570 }
571
572 if (IsPowerOfTwo(lit - 1)) {
573 op->op = kOpAdd;
574 op->shift = LowestSetBit(lit - 1);
575 return true;
576 }
577
578 if (IsPowerOfTwo(lit + 1)) {
579 op->op = kOpRsub;
580 op->shift = LowestSetBit(lit + 1);
581 return true;
582 }
583
584 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100585 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700586 return false;
587}
588
589// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
590bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
591 GetEasyMultiplyOp(lit, &ops[0]);
592 if (GetEasyMultiplyOp(lit, &ops[0])) {
593 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100594 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700595 return true;
596 }
597
598 int lit1 = lit;
599 uint32_t shift = LowestSetBit(lit1);
600 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
601 ops[1].op = kOpLsl;
602 ops[1].shift = shift;
603 return true;
604 }
605
606 lit1 = lit - 1;
607 shift = LowestSetBit(lit1);
608 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
609 ops[1].op = kOpAdd;
610 ops[1].shift = shift;
611 return true;
612 }
613
614 lit1 = lit + 1;
615 shift = LowestSetBit(lit1);
616 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
617 ops[1].op = kOpRsub;
618 ops[1].shift = shift;
619 return true;
620 }
621
622 return false;
623}
624
Zheng Xuf9719f92014-04-02 13:31:31 +0100625// Generate instructions to do multiply.
626// Additional temporary register is required,
627// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700628void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100629 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
630 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
631
632 RegStorage r_tmp1;
633 if (ops[1].op == kOpInvalid) {
634 r_tmp1 = r_dest;
635 } else if (r_dest.GetReg() != r_src.GetReg()) {
636 r_tmp1 = r_dest;
637 } else {
638 r_tmp1 = AllocTemp();
639 }
640
641 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700642 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100643 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700644 break;
645 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100646 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700647 break;
648 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100649 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700650 break;
651 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100652 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700653 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100654 }
655
656 switch (ops[1].op) {
657 case kOpInvalid:
658 return;
659 case kOpLsl:
660 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
661 break;
662 case kOpAdd:
663 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
664 break;
665 case kOpRsub:
666 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
667 break;
668 default:
669 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
670 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700671 }
672}
673
674bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
675 EasyMultiplyOp ops[2];
676
677 if (!GetEasyMultiplyTwoOps(lit, ops)) {
678 return false;
679 }
680
681 rl_src = LoadValue(rl_src, kCoreReg);
682 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
683
684 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
685 StoreValue(rl_dest, rl_result);
686 return true;
687}
688
Mark Mendell2bf31e62014-01-23 12:13:40 -0800689RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700690 RegLocation rl_src2, bool is_div, int flags) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800691 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
692 return rl_dest;
693}
694
695RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
696 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
697 return rl_dest;
698}
699
buzbee2700f7e2014-03-07 09:46:20 -0800700RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700701 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
702
703 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800704 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700705 LoadConstant(lit_temp, lit);
706 // Use the generic case for div/rem with arg2 in a register.
707 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
708 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
709 FreeTemp(lit_temp);
710
711 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712}
713
buzbee2700f7e2014-03-07 09:46:20 -0800714RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700715 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700716 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
717 if (is_div) {
718 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800719 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700720 } else {
721 // Remainder case, use the following code:
722 // temp = reg1 / reg2 - integer division
723 // temp = temp * reg2
724 // dest = reg1 - temp
725
buzbee2700f7e2014-03-07 09:46:20 -0800726 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700727 OpRegRegReg(kOpDiv, temp, reg1, reg2);
728 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800729 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700730 FreeTemp(temp);
731 }
732
733 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734}
735
Serban Constantinescu23abec92014-07-02 16:13:38 +0100736bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100738 if (is_long) {
739 return false;
740 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 RegLocation rl_src1 = info->args[0];
742 RegLocation rl_src2 = info->args[1];
743 rl_src1 = LoadValue(rl_src1, kCoreReg);
744 rl_src2 = LoadValue(rl_src2, kCoreReg);
745 RegLocation rl_dest = InlineTarget(info);
746 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800747 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700748 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800749 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
750 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700751 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 StoreValue(rl_dest, rl_result);
753 return true;
754}
755
Vladimir Markoe508a202013-11-04 15:24:22 +0000756bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
757 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800758 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000759 RegLocation rl_dest = InlineTarget(info);
760 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
761 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700762 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000763 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800764 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700765 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
766 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000767 } else {
buzbee695d13a2014-04-19 13:32:20 -0700768 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
769 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000770 }
771 StoreValueWide(rl_dest, rl_result);
772 } else {
buzbee695d13a2014-04-19 13:32:20 -0700773 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000774 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000775 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000776 StoreValue(rl_dest, rl_result);
777 }
778 return true;
779}
780
781bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
782 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800783 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000784 RegLocation rl_src_value = info->args[2]; // [size] value
785 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700786 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000787 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
788 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000789 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
790 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000791 } else {
buzbee695d13a2014-04-19 13:32:20 -0700792 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000793 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
794 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000795 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000796 }
797 return true;
798}
799
Hans Boehm48f5c472014-06-27 14:50:10 -0700800// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000801bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 DCHECK_EQ(cu_->instruction_set, kThumb2);
803 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000804 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
805 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800806 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000807 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000808 // If is_long, high half is in info->args[5]
809 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
810 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700811 RegLocation rl_dest = InlineTarget(info); // boolean place for result
812
Vladimir Marko3e5af822013-11-21 15:01:20 +0000813 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
814 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
815 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
816 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
817 // into the same temps, reducing the number of required temps down to 5. We shall work
818 // around the potentially locked temp by using LR for r_ptr, unconditionally.
819 // TODO: Pass information about the need for more temps to the stack frame generation
820 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700821 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
822 MarkTemp(rs_rARM_LR);
823 FreeTemp(rs_rARM_LR);
824 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000825 bool load_early = true;
826 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700827 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
828 rl_src_expected.reg;
829 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
830 rl_src_new_value.reg;
831 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
832 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800833 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
834 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000835
836 if (!expected_is_good_reg && !new_value_is_good_reg) {
837 // None of expected/new_value is non-temp reg, need to load both late
838 load_early = false;
839 // Make sure they are not in the temp regs and the load will not be skipped.
840 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800841 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000842 ClobberSReg(rl_src_expected.s_reg_low);
843 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
844 rl_src_expected.location = kLocDalvikFrame;
845 }
846 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800847 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000848 ClobberSReg(rl_src_new_value.s_reg_low);
849 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
850 rl_src_new_value.location = kLocDalvikFrame;
851 }
852 }
853 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854
Hans Boehm48f5c472014-06-27 14:50:10 -0700855 // Prevent reordering with prior memory operations.
856 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857
buzbeea0cd2d72014-06-01 09:33:49 -0700858 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000859 RegLocation rl_new_value;
860 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700861 rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000862 } else if (load_early) {
863 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
864 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865
Vladimir Marko1c282e22013-11-21 14:49:47 +0000866 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 // Mark card for object assuming new value is stored.
buzbee2700f7e2014-03-07 09:46:20 -0800868 MarkGCCard(rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 }
870
871 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
872
buzbee2700f7e2014-03-07 09:46:20 -0800873 RegStorage r_ptr = rs_rARM_LR;
874 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875
876 // Free now unneeded rl_object and rl_offset to give more temps.
877 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700878 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700880 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881
Vladimir Marko3e5af822013-11-21 15:01:20 +0000882 RegLocation rl_expected;
883 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700884 rl_expected = LoadValue(rl_src_expected, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000885 } else if (load_early) {
886 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
887 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000888 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700889 RegStorage low_reg = AllocTemp();
890 RegStorage high_reg = AllocTemp();
891 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000892 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000893 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894
Vladimir Marko3e5af822013-11-21 15:01:20 +0000895 // do {
896 // tmp = [r_ptr] - expected;
897 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
898 // result = tmp != 0;
899
buzbee2700f7e2014-03-07 09:46:20 -0800900 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700901 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700902
Dave Allison3da67a52014-04-02 17:03:45 -0700903 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000904 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800905 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000906 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800907 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000908 }
buzbee2700f7e2014-03-07 09:46:20 -0800909 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
910 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
911 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000912 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800913 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000914 }
915 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700916 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800917 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000918 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800919 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000920 }
921 FreeTemp(r_tmp_high); // Now unneeded
922
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100923 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700924 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800925 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 +0000926
927 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800928 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
929 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100930 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700931 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800932 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000933 }
934
935 // Still one conditional left from OpIT(kCondEq, "T") from either branch
936 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700937 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700938
Jeff Hao2de2aa12013-09-12 17:20:31 -0700939 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940
Vladimir Marko3e5af822013-11-21 15:01:20 +0000941 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800942 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000943 }
944
Hans Boehm48f5c472014-06-27 14:50:10 -0700945 // Prevent reordering with subsequent memory operations.
946 GenMemBarrier(kLoadAny);
947
Vladimir Marko3e5af822013-11-21 15:01:20 +0000948 // result := (tmp1 != 0) ? 0 : 1;
949 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800950 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100951 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700952 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800953 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000954 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700955 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000956
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 StoreValue(rl_dest, rl_result);
958
Vladimir Marko3e5af822013-11-21 15:01:20 +0000959 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700960 Clobber(rs_rARM_LR);
961 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 return true;
963}
964
Zheng Xu947717a2014-08-07 14:05:23 +0800965bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
966 constexpr int kLargeArrayThreshold = 256;
967
968 RegLocation rl_src = info->args[0];
969 RegLocation rl_src_pos = info->args[1];
970 RegLocation rl_dst = info->args[2];
971 RegLocation rl_dst_pos = info->args[3];
972 RegLocation rl_length = info->args[4];
973 // Compile time check, handle exception by non-inline method to reduce related meta-data.
974 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
975 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
976 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
977 return false;
978 }
979
980 ClobberCallerSave();
981 LockCallTemps(); // Prepare for explicit register usage.
982 LockTemp(rs_r12);
983 RegStorage rs_src = rs_r0;
984 RegStorage rs_dst = rs_r1;
985 LoadValueDirectFixed(rl_src, rs_src);
986 LoadValueDirectFixed(rl_dst, rs_dst);
987
988 // Handle null pointer exception in slow-path.
989 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
990 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
991 // Handle potential overlapping in slow-path.
992 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
993 // Handle exception or big length in slow-path.
994 RegStorage rs_length = rs_r2;
995 LoadValueDirectFixed(rl_length, rs_length);
996 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
997 // Src bounds check.
998 RegStorage rs_pos = rs_r3;
999 RegStorage rs_arr_length = rs_r12;
1000 LoadValueDirectFixed(rl_src_pos, rs_pos);
1001 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1002 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1003 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1004 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1005 // Dst bounds check.
1006 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1007 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1008 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1009 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1010 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1011
1012 // Everything is checked now.
1013 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1014 OpRegReg(kOpAdd, rs_dst, rs_pos);
1015 OpRegReg(kOpAdd, rs_dst, rs_pos);
1016 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1017 LoadValueDirectFixed(rl_src_pos, rs_pos);
1018 OpRegReg(kOpAdd, rs_src, rs_pos);
1019 OpRegReg(kOpAdd, rs_src, rs_pos);
1020
1021 RegStorage rs_tmp = rs_pos;
1022 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1023
1024 // Copy one element.
1025 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1026 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1027 OpRegImm(kOpSub, rs_length, 2);
1028 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1029 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1030
1031 // Copy two elements.
1032 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1033 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1034 OpRegImm(kOpSub, rs_length, 4);
1035 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1036 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1037 OpUnconditionalBranch(begin_loop);
1038
1039 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1040 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1041 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1042
1043 src_check_branch->target = check_failed;
1044 dst_check_branch->target = check_failed;
1045 src_dst_same->target = check_failed;
1046 len_neg_or_too_big->target = check_failed;
1047 src_pos_negative->target = check_failed;
1048 src_bad_len->target = check_failed;
1049 dst_pos_negative->target = check_failed;
1050 dst_bad_len->target = check_failed;
1051 jmp_to_begin_loop->target = begin_loop;
1052 jmp_to_ret->target = return_point;
1053
1054 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001055 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001056
1057 return true;
1058}
1059
buzbee2700f7e2014-03-07 09:46:20 -08001060LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
1061 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062}
1063
buzbee2700f7e2014-03-07 09:46:20 -08001064LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001065 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066}
1067
buzbee2700f7e2014-03-07 09:46:20 -08001068LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001069 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070}
1071
1072void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1073 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001074 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001075 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 EncodeShift(kArmLsl, second_bit - first_bit));
1077 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001078 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
1080}
1081
Mingyao Yange643a172014-04-08 11:02:52 -07001082void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001083 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1084 RegStorage t_reg = AllocTemp();
1085 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001087 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088}
1089
1090// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001091LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001092#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001093 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001095#else
1096 RegStorage t_reg = AllocTemp();
1097 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
1098 t_reg, kUnsignedHalf);
1099 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1100 0, target);
1101 FreeTemp(t_reg);
1102 return cmp_branch;
1103#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104}
1105
1106// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001107LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001109 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001110 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 return OpCondBranch(c_code, target);
1112}
1113
Andreas Gampeb14329f2014-05-15 11:16:06 -07001114bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001116 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1117 LIR* barrier = last_lir_insn_;
1118
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 int dmb_flavor;
1120 // TODO: revisit Arm barrier kinds
1121 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001122 case kAnyStore: dmb_flavor = kISH; break;
1123 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001124 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001125 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 default:
1127 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1128 dmb_flavor = kSY; // quiet gcc.
1129 break;
1130 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001131
Andreas Gampeb14329f2014-05-15 11:16:06 -07001132 bool ret = false;
1133
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001134 // If the same barrier already exists, don't generate another.
1135 if (barrier == nullptr
1136 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1137 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001138 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001139 }
1140
1141 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1142 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001143 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001144 return ret;
1145#else
1146 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147#endif
1148}
1149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001150void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 rl_src = LoadValueWide(rl_src, kCoreReg);
1152 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001153 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 LoadConstantNoClobber(z_reg, 0);
1155 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001156 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1157 RegStorage t_reg = AllocTemp();
1158 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1159 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 FreeTemp(t_reg);
1161 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001162 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1163 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
1165 FreeTemp(z_reg);
1166 StoreValueWide(rl_dest, rl_result);
1167}
1168
Mark Mendelle02d48f2014-01-15 11:19:23 -08001169void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1170 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 /*
Zheng Xud7f8e022014-03-13 13:40:30 +00001172 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1173 * dest = src1.lo * src2.lo;
1174 * tmp1 += src1.lo * src2.hi;
1175 * dest.hi += tmp1;
1176 *
1177 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 * registers. Normally for Arm, we get 5. We can get to 6 by including
1179 * lr in the temp set. The only problematic case is all operands and result are
1180 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1181 * freeing operand temp registers after they are no longer needed. All other cases
1182 * can proceed normally. We'll just punt on the case of the result having a misaligned
1183 * overlap with either operand and send that case to a runtime handler.
1184 */
1185 RegLocation rl_result;
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001186 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 FlushAllRegs();
Andreas Gampe98430592014-07-27 19:44:50 -07001188 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
buzbeea0cd2d72014-06-01 09:33:49 -07001189 rl_result = GetReturnWide(kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 StoreValueWide(rl_dest, rl_result);
1191 return;
1192 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001193
1194 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1195 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1196
1197 int reg_status = 0;
buzbee2700f7e2014-03-07 09:46:20 -08001198 RegStorage res_lo;
1199 RegStorage res_hi;
1200 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
buzbee091cc402014-03-31 10:14:40 -07001201 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1202 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1203 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001204 // Check if rl_dest is *not* either operand and we have enough temp registers.
1205 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1206 (dest_promoted || src1_promoted || src2_promoted)) {
1207 // In this case, we do not need to manually allocate temp registers for result.
1208 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001209 res_lo = rl_result.reg.GetLow();
1210 res_hi = rl_result.reg.GetHigh();
Zheng Xud7f8e022014-03-13 13:40:30 +00001211 } else {
1212 res_lo = AllocTemp();
1213 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1214 // In this case, we have enough temp registers to be allocated for result.
1215 res_hi = AllocTemp();
1216 reg_status = 1;
1217 } else {
1218 // In this case, all temps are now allocated.
1219 // res_hi will be allocated after we can free src1_hi.
1220 reg_status = 2;
1221 }
1222 }
1223
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 // Temporarily add LR to the temp pool, and assign it to tmp1
buzbee091cc402014-03-31 10:14:40 -07001225 MarkTemp(rs_rARM_LR);
1226 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -08001227 RegStorage tmp1 = rs_rARM_LR;
buzbee091cc402014-03-31 10:14:40 -07001228 LockTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229
buzbee2700f7e2014-03-07 09:46:20 -08001230 if (rl_src1.reg == rl_src2.reg) {
1231 DCHECK(res_hi.Valid());
1232 DCHECK(res_lo.Valid());
1233 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1234 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1235 rl_src1.reg.GetLowReg());
Ian Rogerse2143c02014-03-28 08:47:16 -07001236 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001238 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
Zheng Xud7f8e022014-03-13 13:40:30 +00001239 if (reg_status == 2) {
buzbee2700f7e2014-03-07 09:46:20 -08001240 DCHECK(!res_hi.Valid());
1241 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001242 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
buzbee082833c2014-05-17 23:16:26 -07001243 // Will force free src1_hi, so must clobber.
1244 Clobber(rl_src1.reg);
buzbee091cc402014-03-31 10:14:40 -07001245 FreeTemp(rl_src1.reg.GetHigh());
Zheng Xud7f8e022014-03-13 13:40:30 +00001246 res_hi = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 }
buzbee2700f7e2014-03-07 09:46:20 -08001248 DCHECK(res_hi.Valid());
1249 DCHECK(res_lo.Valid());
1250 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1251 rl_src1.reg.GetLowReg());
1252 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1253 tmp1.GetReg());
1254 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
Zheng Xud7f8e022014-03-13 13:40:30 +00001255 if (reg_status == 2) {
buzbee082833c2014-05-17 23:16:26 -07001256 FreeTemp(rl_src1.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257 }
1258 }
Zheng Xud7f8e022014-03-13 13:40:30 +00001259
Brian Carlstrom7940e442013-07-12 13:46:57 -07001260 // Now, restore lr to its non-temp status.
Zheng Xud7f8e022014-03-13 13:40:30 +00001261 FreeTemp(tmp1);
buzbee091cc402014-03-31 10:14:40 -07001262 Clobber(rs_rARM_LR);
1263 UnmarkTemp(rs_rARM_LR);
Zheng Xud7f8e022014-03-13 13:40:30 +00001264
1265 if (reg_status != 0) {
1266 // We had manually allocated registers for rl_result.
1267 // Now construct a RegLocation.
buzbeea0cd2d72014-06-01 09:33:49 -07001268 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
buzbee2700f7e2014-03-07 09:46:20 -08001269 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
Zheng Xud7f8e022014-03-13 13:40:30 +00001270 }
1271
1272 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273}
1274
Andreas Gampec76c6142014-08-04 16:30:03 -07001275void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001276 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001277 switch (opcode) {
1278 case Instruction::MUL_LONG:
1279 case Instruction::MUL_LONG_2ADDR:
1280 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1281 return;
1282 case Instruction::NEG_LONG:
1283 GenNegLong(rl_dest, rl_src2);
1284 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285
Andreas Gampec76c6142014-08-04 16:30:03 -07001286 default:
1287 break;
1288 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289
Andreas Gampec76c6142014-08-04 16:30:03 -07001290 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001291 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292}
1293
1294/*
1295 * Generate array load
1296 */
1297void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001298 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001299 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 int len_offset = mirror::Array::LengthOffset().Int32Value();
1301 int data_offset;
1302 RegLocation rl_result;
1303 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001304 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 if (!constant_index) {
1306 rl_index = LoadValue(rl_index, kCoreReg);
1307 }
1308
1309 if (rl_dest.wide) {
1310 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1311 } else {
1312 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1313 }
1314
1315 // If index is constant, just fold it into the data offset
1316 if (constant_index) {
1317 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1318 }
1319
1320 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001321 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322
1323 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001324 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 if (needs_range_check) {
1326 reg_len = AllocTemp();
1327 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001328 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001329 MarkPossibleNullPointerException(opt_flags);
1330 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001331 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 }
1333 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001334 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001336 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 } else {
1338 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001339 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001340 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001341 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 }
1343 rl_result = EvalLoc(rl_dest, reg_class, true);
1344
1345 if (needs_range_check) {
1346 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001347 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001349 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 }
1351 FreeTemp(reg_len);
1352 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001353 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001354 MarkPossibleNullPointerException(opt_flags);
1355 if (!constant_index) {
1356 FreeTemp(reg_ptr);
1357 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 StoreValueWide(rl_dest, rl_result);
1360 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 StoreValue(rl_dest, rl_result);
1362 }
1363 } else {
1364 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001365 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001366 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001367 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 rl_result = EvalLoc(rl_dest, reg_class, true);
1369
1370 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001371 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 FreeTemp(reg_len);
1373 }
buzbee2700f7e2014-03-07 09:46:20 -08001374 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001375 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 FreeTemp(reg_ptr);
1377 StoreValue(rl_dest, rl_result);
1378 }
1379}
1380
1381/*
1382 * Generate array store
1383 *
1384 */
1385void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001386 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001387 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 bool constant_index = rl_index.is_const;
1390
Ian Rogersa9a82542013-10-04 11:17:26 -07001391 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001392 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1394 } else {
1395 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1396 }
1397
1398 // If index is constant, just fold it into the data offset.
1399 if (constant_index) {
1400 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1401 }
1402
buzbeea0cd2d72014-06-01 09:33:49 -07001403 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 if (!constant_index) {
1405 rl_index = LoadValue(rl_index, kCoreReg);
1406 }
1407
buzbee2700f7e2014-03-07 09:46:20 -08001408 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001409 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001410 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001411 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001412 } else if (IsTemp(rl_array.reg) && !card_mark) {
1413 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001414 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001415 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001416 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001417 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001418 }
1419
1420 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001421 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422
1423 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001424 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425 if (needs_range_check) {
1426 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001427 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001429 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001430 MarkPossibleNullPointerException(opt_flags);
1431 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001432 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 }
1434 /* at this point, reg_ptr points to array, 2 live temps */
1435 if (rl_src.wide || rl_src.fp || constant_index) {
1436 if (rl_src.wide) {
1437 rl_src = LoadValueWide(rl_src, reg_class);
1438 } else {
1439 rl_src = LoadValue(rl_src, reg_class);
1440 }
1441 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001442 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 }
1444 if (needs_range_check) {
1445 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001446 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001447 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001448 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 }
1450 FreeTemp(reg_len);
1451 }
1452
Andreas Gampe3c12c512014-06-24 18:46:29 +00001453 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Dave Allisonb373e092014-02-20 16:06:36 -08001454 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 } else {
1456 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001457 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 rl_src = LoadValue(rl_src, reg_class);
1459 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001460 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 FreeTemp(reg_len);
1462 }
buzbee2700f7e2014-03-07 09:46:20 -08001463 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Dave Allisonb373e092014-02-20 16:06:36 -08001464 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 }
Ian Rogers773aab12013-10-14 13:50:10 -07001466 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 FreeTemp(reg_ptr);
1468 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001469 if (card_mark) {
buzbee2700f7e2014-03-07 09:46:20 -08001470 MarkGCCard(rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 }
1472}
1473
Ian Rogersa9a82542013-10-04 11:17:26 -07001474
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001476 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1477 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 rl_src = LoadValueWide(rl_src, kCoreReg);
1479 // Per spec, we only care about low 6 bits of shift amount.
1480 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1481 if (shift_amount == 0) {
1482 StoreValueWide(rl_dest, rl_src);
1483 return;
1484 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001485 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1487 return;
1488 }
1489 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001490 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 case Instruction::SHL_LONG:
1492 case Instruction::SHL_LONG_2ADDR:
1493 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001494 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1495 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001497 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1498 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001500 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1501 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001503 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001504 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001506 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 }
1508 break;
1509 case Instruction::SHR_LONG:
1510 case Instruction::SHR_LONG_2ADDR:
1511 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001512 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1513 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001515 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1516 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001518 RegStorage t_reg = AllocTemp();
1519 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001520 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 EncodeShift(kArmLsl, 32 - shift_amount));
1522 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001523 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
1525 break;
1526 case Instruction::USHR_LONG:
1527 case Instruction::USHR_LONG_2ADDR:
1528 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001529 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1530 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001532 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1533 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001534 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001535 RegStorage t_reg = AllocTemp();
1536 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001537 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538 EncodeShift(kArmLsl, 32 - shift_amount));
1539 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001540 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 }
1542 break;
1543 default:
1544 LOG(FATAL) << "Unexpected case";
1545 }
1546 StoreValueWide(rl_dest, rl_result);
1547}
1548
1549void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001550 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1551 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1553 if (!rl_src2.is_const) {
1554 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001555 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 return;
1557 }
1558 } else {
1559 // Normalize
1560 if (!rl_src2.is_const) {
1561 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001562 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 }
1564 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001565 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001566 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001567 return;
1568 }
1569 DCHECK(rl_src2.is_const);
1570 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1571 uint32_t val_lo = Low32Bits(val);
1572 uint32_t val_hi = High32Bits(val);
1573 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1574 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1575
1576 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001577 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001578 case Instruction::ADD_LONG:
1579 case Instruction::ADD_LONG_2ADDR:
1580 case Instruction::SUB_LONG:
1581 case Instruction::SUB_LONG_2ADDR:
1582 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001583 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 return;
1585 }
1586 break;
1587 default:
1588 break;
1589 }
1590 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1591 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1592 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1593 switch (opcode) {
1594 case Instruction::ADD_LONG:
1595 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001596 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001597 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 break;
1599 case Instruction::OR_LONG:
1600 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001601 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1602 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001604 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001605 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001606 }
1607 break;
1608 case Instruction::XOR_LONG:
1609 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001610 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1611 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 break;
1613 case Instruction::AND_LONG:
1614 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001615 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1616 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001617 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001618 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001619 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
1621 break;
1622 case Instruction::SUB_LONG_2ADDR:
1623 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001624 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001625 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 break;
1627 default:
1628 LOG(FATAL) << "Unexpected opcode " << opcode;
1629 }
1630 StoreValueWide(rl_dest, rl_result);
1631}
1632
1633} // namespace art