blob: 9dd87e6f22a7ec029440b3fd9dd83e6856fe2a7c [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
Elliott Hughes8366ca02014-11-17 12:02:05 -080019#include "arch/instruction_set_features.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "arm_lir.h"
21#include "codegen_arm.h"
22#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Ian Rogers166db042013-07-26 12:05:57 -070024#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080026#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29
buzbee2700f7e2014-03-07 09:46:20 -080030LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 OpRegReg(kOpCmp, src1, src2);
32 return OpCondBranch(cond, target);
33}
34
35/*
36 * Generate a Thumb2 IT instruction, which can nullify up to
37 * four subsequent instructions based on a condition and its
38 * inverse. The condition applies to the first instruction, which
39 * is executed if the condition is met. The string "guide" consists
40 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
41 * A "T" means the instruction is executed if the condition is
42 * met, and an "E" means the instruction is executed if the condition
43 * is not met.
44 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070045LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 int mask;
47 int mask3 = 0;
48 int mask2 = 0;
49 int mask1 = 0;
50 ArmConditionCode code = ArmConditionEncoding(ccode);
51 int cond_bit = code & 1;
52 int alt_bit = cond_bit ^ 1;
53
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 switch (strlen(guide)) {
55 case 3:
56 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070057 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 case 2:
59 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070060 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 case 1:
62 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
63 break;
64 case 0:
65 break;
66 default:
67 LOG(FATAL) << "OAT: bad case in OpIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -070068 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 }
70 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
71 (1 << (3 - strlen(guide)));
72 return NewLIR2(kThumb2It, code, mask);
73}
74
Andreas Gampeb14329f2014-05-15 11:16:06 -070075void ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
76 int mask;
77 int mask3 = 0;
78 int mask2 = 0;
79 int mask1 = 0;
80 ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
81 int cond_bit = code & 1;
82 int alt_bit = cond_bit ^ 1;
83
Andreas Gampeb14329f2014-05-15 11:16:06 -070084 switch (strlen(new_guide)) {
85 case 3:
86 mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070087 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070088 case 2:
89 mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070090 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070091 case 1:
92 mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
93 break;
94 case 0:
95 break;
96 default:
97 LOG(FATAL) << "OAT: bad case in UpdateIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -070098 UNREACHABLE();
Andreas Gampeb14329f2014-05-15 11:16:06 -070099 }
100 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
101 (1 << (3 - strlen(new_guide)));
102 it->operands[1] = mask;
103}
104
Dave Allison3da67a52014-04-02 17:03:45 -0700105void ArmMir2Lir::OpEndIT(LIR* it) {
106 // TODO: use the 'it' pointer to do some checks with the LIR, for example
107 // we could check that the number of instructions matches the mask
108 // in the IT instruction.
109 CHECK(it != nullptr);
110 GenBarrier();
111}
112
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113/*
114 * 64-bit 3way compare function.
115 * mov rX, #-1
116 * cmp op1hi, op2hi
117 * blt done
118 * bgt flip
119 * sub rX, op1lo, op2lo (treat as unsigned)
120 * beq done
121 * ite hi
122 * mov(hi) rX, #-1
123 * mov(!hi) rX, #1
124 * flip:
125 * neg rX
126 * done:
127 */
buzbeea1983d42014-04-07 12:35:39 -0700128void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 LIR* target1;
130 LIR* target2;
131 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
132 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800133 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800135 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 LIR* branch1 = OpCondBranch(kCondLt, NULL);
137 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700138 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 LIR* branch3 = OpCondBranch(kCondEq, NULL);
140
Dave Allison3da67a52014-04-02 17:03:45 -0700141 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800142 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700144 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145
146 target2 = NewLIR0(kPseudoTargetLabel);
147 OpRegReg(kOpNeg, t_reg, t_reg);
148
149 target1 = NewLIR0(kPseudoTargetLabel);
150
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700151 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800152 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 StoreValue(rl_dest, rl_temp);
154 FreeTemp(t_reg);
155
156 branch1->target = target1;
157 branch2->target = target2;
158 branch3->target = branch1->target;
159}
160
161void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700162 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 int32_t val_lo = Low32Bits(val);
164 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700165 DCHECK_GE(ModifiedImmediate(val_lo), 0);
166 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700167 LIR* taken = &block_label_list_[bb->taken];
168 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800170 RegStorage low_reg = rl_src1.reg.GetLow();
171 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172
Vladimir Marko58af1f92013-12-19 13:31:15 +0000173 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800174 RegStorage t_reg = AllocTemp();
175 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000176 FreeTemp(t_reg);
177 OpCondBranch(ccode, taken);
178 return;
179 }
180
Brian Carlstromdf629502013-07-17 22:39:56 -0700181 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 case kCondEq:
183 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000184 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 break;
186 case kCondLt:
187 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
188 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000189 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 break;
191 case kCondLe:
192 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
193 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
194 ccode = kCondLs;
195 break;
196 case kCondGt:
197 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
198 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
199 ccode = kCondHi;
200 break;
201 case kCondGe:
202 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
203 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000204 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 break;
206 default:
207 LOG(FATAL) << "Unexpected ccode: " << ccode;
208 }
209 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
210}
211
Andreas Gampe90969af2014-07-15 23:02:11 -0700212void ArmMir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
213 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214 RegisterClass dest_reg_class) {
215 UNUSED(dest_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700216 // TODO: Generalize the IT below to accept more than one-instruction loads.
217 DCHECK(InexpensiveConstantInt(true_val));
218 DCHECK(InexpensiveConstantInt(false_val));
219
220 if ((true_val == 0 && code == kCondEq) ||
221 (false_val == 0 && code == kCondNe)) {
222 OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
223 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
224 LIR* it = OpIT(kCondNe, "");
225 LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
226 OpEndIT(it);
227 return;
228 }
229
230 OpRegReg(kOpCmp, left_op, right_op); // Same?
231 LIR* it = OpIT(code, "E"); // if-convert the test
232 LoadConstant(rs_dest, true_val); // .eq case - load true
233 LoadConstant(rs_dest, false_val); // .eq case - load true
234 OpEndIT(it);
235}
236
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700237void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700238 UNUSED(bb);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 RegLocation rl_result;
240 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700242 // Avoid using float regs here.
243 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
244 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
245 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000246 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 if (mir->ssa_rep->num_uses == 1) {
248 // CONST case
249 int true_val = mir->dalvikInsn.vB;
250 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700251 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000252 // Change kCondNe to kCondEq for the special cases below.
253 if (ccode == kCondNe) {
254 ccode = kCondEq;
255 std::swap(true_val, false_val);
256 }
257 bool cheap_false_val = InexpensiveConstantInt(false_val);
258 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800259 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100260 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700261 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800262 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700263 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000264 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800265 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100266 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700267 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800268 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700269 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000270 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800271 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700272 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800273 LoadConstant(rl_result.reg, true_val);
274 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700275 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 } else {
277 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700278 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
279 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 LoadConstant(t_reg1, true_val);
281 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800282 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700283 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800284 OpRegCopy(rl_result.reg, t_reg1);
285 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700286 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
288 } else {
289 // MOVE case
290 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
291 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700292 rl_true = LoadValue(rl_true, result_reg_class);
293 rl_false = LoadValue(rl_false, result_reg_class);
294 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800295 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700296 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000297 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700298 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800299 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000300 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700301 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800302 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700303 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700304 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800305 OpRegCopy(rl_result.reg, rl_true.reg);
306 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700307 }
Dave Allison3da67a52014-04-02 17:03:45 -0700308 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 }
310 StoreValue(rl_dest, rl_result);
311}
312
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
315 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
316 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000317 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000319 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 ccode = FlipComparisonOrder(ccode);
321 }
322 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700323 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 // Do special compare/branch against simple const operand if not already in registers.
325 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700326 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
328 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
329 return;
330 }
331 }
buzbee0d829482013-10-11 15:24:55 -0700332 LIR* taken = &block_label_list_[bb->taken];
333 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
335 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800336 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700337 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 case kCondEq:
339 OpCondBranch(kCondNe, not_taken);
340 break;
341 case kCondNe:
342 OpCondBranch(kCondNe, taken);
343 break;
344 case kCondLt:
345 OpCondBranch(kCondLt, taken);
346 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000347 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 break;
349 case kCondLe:
350 OpCondBranch(kCondLt, taken);
351 OpCondBranch(kCondGt, not_taken);
352 ccode = kCondLs;
353 break;
354 case kCondGt:
355 OpCondBranch(kCondGt, taken);
356 OpCondBranch(kCondLt, not_taken);
357 ccode = kCondHi;
358 break;
359 case kCondGe:
360 OpCondBranch(kCondGt, taken);
361 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000362 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 break;
364 default:
365 LOG(FATAL) << "Unexpected ccode: " << ccode;
366 }
buzbee2700f7e2014-03-07 09:46:20 -0800367 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 OpCondBranch(ccode, taken);
369}
370
371/*
372 * Generate a register comparison to an immediate and branch. Caller
373 * is responsible for setting branch target field.
374 */
buzbee2700f7e2014-03-07 09:46:20 -0800375LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700376 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700378 /*
379 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
380 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700381 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700382 * be converted to a long form during assembly (which will trigger another assembly
383 * pass). Here we estimate the branch distance for checks, and if large directly
384 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700385 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700386 */
387 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700388 skip &= ((mir_graph_->GetNumDalvikInsns() - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700389 if (!skip && reg.Low8() && (check_value == 0)) {
390 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
391 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
392 reg.GetReg(), 0);
393 } else if (arm_cond == kArmCondLs) {
394 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
395 // This case happens for a bounds check of array[0].
396 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
397 }
398 }
399
400 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000401 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 branch = NewLIR2(kThumbBCond, 0, arm_cond);
403 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700404
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 branch->target = target;
406 return branch;
407}
408
buzbee2700f7e2014-03-07 09:46:20 -0800409LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 LIR* res;
411 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800412 // If src or dest is a pair, we'll be using low reg.
413 if (r_dest.IsPair()) {
414 r_dest = r_dest.GetLow();
415 }
416 if (r_src.IsPair()) {
417 r_src = r_src.GetLow();
418 }
buzbee091cc402014-03-31 10:14:40 -0700419 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700421 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700423 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700425 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 opcode = kThumbMovRR_H2L;
427 else
428 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800429 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
431 res->flags.is_nop = true;
432 }
433 return res;
434}
435
buzbee7a11ab02014-04-28 20:02:38 -0700436void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
437 if (r_dest != r_src) {
438 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
439 AppendLIR(res);
440 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441}
442
buzbee2700f7e2014-03-07 09:46:20 -0800443void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700444 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700445 bool dest_fp = r_dest.IsFloat();
446 bool src_fp = r_src.IsFloat();
447 DCHECK(r_dest.Is64Bit());
448 DCHECK(r_src.Is64Bit());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800449 // Note: If the register is get by register allocator, it should never be a pair.
450 // But some functions in mir_2_lir assume 64-bit registers are 32-bit register pairs.
451 // TODO: Rework Mir2Lir::LoadArg() and Mir2Lir::LoadArgDirect().
452 if (dest_fp && r_dest.IsPair()) {
453 r_dest = As64BitFloatReg(r_dest);
454 }
455 if (src_fp && r_src.IsPair()) {
456 r_src = As64BitFloatReg(r_src);
457 }
buzbee7a11ab02014-04-28 20:02:38 -0700458 if (dest_fp) {
459 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700460 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 } else {
buzbee091cc402014-03-31 10:14:40 -0700462 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700463 }
464 } else {
465 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700466 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700467 } else {
468 // Handle overlap
469 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
470 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
471 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
472 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
473 } else {
474 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
475 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
476 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 }
478 }
479 }
480}
481
482// Table of magic divisors
483struct MagicTable {
484 uint32_t magic;
485 uint32_t shift;
486 DividePattern pattern;
487};
488
489static const MagicTable magic_table[] = {
490 {0, 0, DivideNone}, // 0
491 {0, 0, DivideNone}, // 1
492 {0, 0, DivideNone}, // 2
493 {0x55555556, 0, Divide3}, // 3
494 {0, 0, DivideNone}, // 4
495 {0x66666667, 1, Divide5}, // 5
496 {0x2AAAAAAB, 0, Divide3}, // 6
497 {0x92492493, 2, Divide7}, // 7
498 {0, 0, DivideNone}, // 8
499 {0x38E38E39, 1, Divide5}, // 9
500 {0x66666667, 2, Divide5}, // 10
501 {0x2E8BA2E9, 1, Divide5}, // 11
502 {0x2AAAAAAB, 1, Divide5}, // 12
503 {0x4EC4EC4F, 2, Divide5}, // 13
504 {0x92492493, 3, Divide7}, // 14
505 {0x88888889, 3, Divide7}, // 15
506};
507
508// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700509bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700510 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700511 UNUSED(dalvik_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
513 return false;
514 }
515 DividePattern pattern = magic_table[lit].pattern;
516 if (pattern == DivideNone) {
517 return false;
518 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519
buzbee2700f7e2014-03-07 09:46:20 -0800520 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 LoadConstant(r_magic, magic_table[lit].magic);
522 rl_src = LoadValue(rl_src, kCoreReg);
523 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800524 RegStorage r_hi = AllocTemp();
525 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100526
527 // rl_dest and rl_src might overlap.
528 // Reuse r_hi to save the div result for reminder case.
529 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
530
buzbee2700f7e2014-03-07 09:46:20 -0800531 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700532 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100534 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 break;
536 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800537 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100538 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700539 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 break;
541 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800542 OpRegReg(kOpAdd, r_hi, rl_src.reg);
543 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100544 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700545 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 break;
547 default:
548 LOG(FATAL) << "Unexpected pattern: " << pattern;
549 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100550
551 if (!is_div) {
552 // div_result = src / lit
553 // tmp1 = div_result * lit
554 // dest = src - tmp1
555 RegStorage tmp1 = r_lo;
556 EasyMultiplyOp ops[2];
557
558 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
559 DCHECK_NE(canEasyMultiply, false);
560
561 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
562 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
563 }
564
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 StoreValue(rl_dest, rl_result);
566 return true;
567}
568
Ian Rogerse2143c02014-03-28 08:47:16 -0700569// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
570bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
571 if (IsPowerOfTwo(lit)) {
572 op->op = kOpLsl;
Andreas Gampe7e499922015-01-06 08:28:12 -0800573 op->shift = CTZ(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700574 return true;
575 }
576
577 if (IsPowerOfTwo(lit - 1)) {
578 op->op = kOpAdd;
Andreas Gampe7e499922015-01-06 08:28:12 -0800579 op->shift = CTZ(lit - 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700580 return true;
581 }
582
583 if (IsPowerOfTwo(lit + 1)) {
584 op->op = kOpRsub;
Andreas Gampe7e499922015-01-06 08:28:12 -0800585 op->shift = CTZ(lit + 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700586 return true;
587 }
588
589 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100590 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700591 return false;
592}
593
594// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
595bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700596 if (GetEasyMultiplyOp(lit, &ops[0])) {
597 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100598 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700599 return true;
600 }
601
602 int lit1 = lit;
Andreas Gampe7e499922015-01-06 08:28:12 -0800603 uint32_t shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700604 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
605 ops[1].op = kOpLsl;
606 ops[1].shift = shift;
607 return true;
608 }
609
610 lit1 = lit - 1;
Andreas Gampe7e499922015-01-06 08:28:12 -0800611 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700612 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
613 ops[1].op = kOpAdd;
614 ops[1].shift = shift;
615 return true;
616 }
617
618 lit1 = lit + 1;
Andreas Gampe7e499922015-01-06 08:28:12 -0800619 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700620 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
621 ops[1].op = kOpRsub;
622 ops[1].shift = shift;
623 return true;
624 }
625
626 return false;
627}
628
Zheng Xuf9719f92014-04-02 13:31:31 +0100629// Generate instructions to do multiply.
630// Additional temporary register is required,
631// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700632void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100633 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
634 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
635
636 RegStorage r_tmp1;
637 if (ops[1].op == kOpInvalid) {
638 r_tmp1 = r_dest;
639 } else if (r_dest.GetReg() != r_src.GetReg()) {
640 r_tmp1 = r_dest;
641 } else {
642 r_tmp1 = AllocTemp();
643 }
644
645 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700646 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100647 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700648 break;
649 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100650 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700651 break;
652 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100653 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700654 break;
655 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100656 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700657 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100658 }
659
660 switch (ops[1].op) {
661 case kOpInvalid:
662 return;
663 case kOpLsl:
664 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
665 break;
666 case kOpAdd:
667 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
668 break;
669 case kOpRsub:
670 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
671 break;
672 default:
673 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
674 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700675 }
676}
677
678bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
679 EasyMultiplyOp ops[2];
680
681 if (!GetEasyMultiplyTwoOps(lit, ops)) {
682 return false;
683 }
684
685 rl_src = LoadValue(rl_src, kCoreReg);
686 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
687
688 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
689 StoreValue(rl_dest, rl_result);
690 return true;
691}
692
Mark Mendell2bf31e62014-01-23 12:13:40 -0800693RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700694 RegLocation rl_src2, bool is_div, int flags) {
695 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800696 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700697 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800698}
699
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700700RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
701 bool is_div) {
702 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800703 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700704 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800705}
706
buzbee2700f7e2014-03-07 09:46:20 -0800707RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700708 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
709
710 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800711 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700712 LoadConstant(lit_temp, lit);
713 // Use the generic case for div/rem with arg2 in a register.
714 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
715 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
716 FreeTemp(lit_temp);
717
718 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719}
720
buzbee2700f7e2014-03-07 09:46:20 -0800721RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700722 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700723 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
724 if (is_div) {
725 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800726 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700727 } else {
728 // Remainder case, use the following code:
729 // temp = reg1 / reg2 - integer division
730 // temp = temp * reg2
731 // dest = reg1 - temp
732
buzbee2700f7e2014-03-07 09:46:20 -0800733 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700734 OpRegRegReg(kOpDiv, temp, reg1, reg2);
735 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800736 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700737 FreeTemp(temp);
738 }
739
740 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741}
742
Serban Constantinescu23abec92014-07-02 16:13:38 +0100743bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100745 if (is_long) {
746 return false;
747 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700748 RegLocation rl_src1 = info->args[0];
749 RegLocation rl_src2 = info->args[1];
750 rl_src1 = LoadValue(rl_src1, kCoreReg);
751 rl_src2 = LoadValue(rl_src2, kCoreReg);
752 RegLocation rl_dest = InlineTarget(info);
753 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800754 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700755 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800756 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
757 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700758 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 StoreValue(rl_dest, rl_result);
760 return true;
761}
762
Vladimir Markoe508a202013-11-04 15:24:22 +0000763bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
764 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800765 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000766 RegLocation rl_dest = InlineTarget(info);
767 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
768 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700769 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000770 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800771 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700772 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
773 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000774 } else {
buzbee695d13a2014-04-19 13:32:20 -0700775 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
776 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000777 }
778 StoreValueWide(rl_dest, rl_result);
779 } else {
buzbee695d13a2014-04-19 13:32:20 -0700780 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000781 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000782 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000783 StoreValue(rl_dest, rl_result);
784 }
785 return true;
786}
787
788bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
789 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800790 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000791 RegLocation rl_src_value = info->args[2]; // [size] value
792 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700793 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000794 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
795 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000796 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
797 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000798 } else {
buzbee695d13a2014-04-19 13:32:20 -0700799 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000800 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
801 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000802 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000803 }
804 return true;
805}
806
Hans Boehm48f5c472014-06-27 14:50:10 -0700807// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000808bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 DCHECK_EQ(cu_->instruction_set, kThumb2);
810 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000811 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
812 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800813 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000814 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000815 // If is_long, high half is in info->args[5]
816 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
817 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 RegLocation rl_dest = InlineTarget(info); // boolean place for result
819
Vladimir Marko3e5af822013-11-21 15:01:20 +0000820 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
821 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
822 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
823 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
824 // into the same temps, reducing the number of required temps down to 5. We shall work
825 // around the potentially locked temp by using LR for r_ptr, unconditionally.
826 // TODO: Pass information about the need for more temps to the stack frame generation
827 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700828 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
829 MarkTemp(rs_rARM_LR);
830 FreeTemp(rs_rARM_LR);
831 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000832 bool load_early = true;
833 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700834 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
835 rl_src_expected.reg;
836 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
837 rl_src_new_value.reg;
838 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
839 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800840 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
841 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000842
843 if (!expected_is_good_reg && !new_value_is_good_reg) {
844 // None of expected/new_value is non-temp reg, need to load both late
845 load_early = false;
846 // Make sure they are not in the temp regs and the load will not be skipped.
847 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800848 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000849 ClobberSReg(rl_src_expected.s_reg_low);
850 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
851 rl_src_expected.location = kLocDalvikFrame;
852 }
853 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800854 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000855 ClobberSReg(rl_src_new_value.s_reg_low);
856 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
857 rl_src_new_value.location = kLocDalvikFrame;
858 }
859 }
860 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861
Hans Boehm48f5c472014-06-27 14:50:10 -0700862 // Prevent reordering with prior memory operations.
863 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864
buzbeea0cd2d72014-06-01 09:33:49 -0700865 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000866 RegLocation rl_new_value;
867 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700868 rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000869 } else if (load_early) {
870 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
871 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872
Vladimir Marko1c282e22013-11-21 14:49:47 +0000873 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 // Mark card for object assuming new value is stored.
Vladimir Marko743b98c2014-11-24 19:45:41 +0000875 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 }
877
878 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
879
buzbee2700f7e2014-03-07 09:46:20 -0800880 RegStorage r_ptr = rs_rARM_LR;
881 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882
883 // Free now unneeded rl_object and rl_offset to give more temps.
884 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700885 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700887 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888
Vladimir Marko3e5af822013-11-21 15:01:20 +0000889 RegLocation rl_expected;
890 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700891 rl_expected = LoadValue(rl_src_expected, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000892 } else if (load_early) {
893 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
894 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000895 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700896 RegStorage low_reg = AllocTemp();
897 RegStorage high_reg = AllocTemp();
898 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000899 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000900 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901
Vladimir Marko3e5af822013-11-21 15:01:20 +0000902 // do {
903 // tmp = [r_ptr] - expected;
904 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
905 // result = tmp != 0;
906
buzbee2700f7e2014-03-07 09:46:20 -0800907 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700908 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700909
Dave Allison3da67a52014-04-02 17:03:45 -0700910 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000911 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800912 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000913 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800914 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000915 }
buzbee2700f7e2014-03-07 09:46:20 -0800916 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
917 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
918 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000919 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800920 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000921 }
922 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700923 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800924 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000925 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800926 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000927 }
928 FreeTemp(r_tmp_high); // Now unneeded
929
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(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 +0000933
934 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800935 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
936 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100937 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700938 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800939 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000940 }
941
942 // Still one conditional left from OpIT(kCondEq, "T") from either branch
943 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700944 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700945
Jeff Hao2de2aa12013-09-12 17:20:31 -0700946 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947
Vladimir Marko3e5af822013-11-21 15:01:20 +0000948 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800949 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000950 }
951
Hans Boehm48f5c472014-06-27 14:50:10 -0700952 // Prevent reordering with subsequent memory operations.
953 GenMemBarrier(kLoadAny);
954
Vladimir Marko3e5af822013-11-21 15:01:20 +0000955 // result := (tmp1 != 0) ? 0 : 1;
956 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800957 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100958 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700959 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800960 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000961 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700962 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000963
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 StoreValue(rl_dest, rl_result);
965
Vladimir Marko3e5af822013-11-21 15:01:20 +0000966 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700967 Clobber(rs_rARM_LR);
968 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 return true;
970}
971
Zheng Xu947717a2014-08-07 14:05:23 +0800972bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
973 constexpr int kLargeArrayThreshold = 256;
974
975 RegLocation rl_src = info->args[0];
976 RegLocation rl_src_pos = info->args[1];
977 RegLocation rl_dst = info->args[2];
978 RegLocation rl_dst_pos = info->args[3];
979 RegLocation rl_length = info->args[4];
980 // Compile time check, handle exception by non-inline method to reduce related meta-data.
981 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
982 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
983 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
984 return false;
985 }
986
987 ClobberCallerSave();
988 LockCallTemps(); // Prepare for explicit register usage.
989 LockTemp(rs_r12);
990 RegStorage rs_src = rs_r0;
991 RegStorage rs_dst = rs_r1;
992 LoadValueDirectFixed(rl_src, rs_src);
993 LoadValueDirectFixed(rl_dst, rs_dst);
994
995 // Handle null pointer exception in slow-path.
996 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
997 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
998 // Handle potential overlapping in slow-path.
999 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
1000 // Handle exception or big length in slow-path.
1001 RegStorage rs_length = rs_r2;
1002 LoadValueDirectFixed(rl_length, rs_length);
1003 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
1004 // Src bounds check.
1005 RegStorage rs_pos = rs_r3;
1006 RegStorage rs_arr_length = rs_r12;
1007 LoadValueDirectFixed(rl_src_pos, rs_pos);
1008 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1009 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1010 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1011 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1012 // Dst bounds check.
1013 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1014 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1015 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1016 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1017 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1018
1019 // Everything is checked now.
1020 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1021 OpRegReg(kOpAdd, rs_dst, rs_pos);
1022 OpRegReg(kOpAdd, rs_dst, rs_pos);
1023 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1024 LoadValueDirectFixed(rl_src_pos, rs_pos);
1025 OpRegReg(kOpAdd, rs_src, rs_pos);
1026 OpRegReg(kOpAdd, rs_src, rs_pos);
1027
1028 RegStorage rs_tmp = rs_pos;
1029 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1030
1031 // Copy one element.
1032 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1033 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1034 OpRegImm(kOpSub, rs_length, 2);
1035 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1036 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1037
1038 // Copy two elements.
1039 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1040 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1041 OpRegImm(kOpSub, rs_length, 4);
1042 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1043 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1044 OpUnconditionalBranch(begin_loop);
1045
1046 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1047 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1048 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1049
1050 src_check_branch->target = check_failed;
1051 dst_check_branch->target = check_failed;
1052 src_dst_same->target = check_failed;
1053 len_neg_or_too_big->target = check_failed;
1054 src_pos_negative->target = check_failed;
1055 src_bad_len->target = check_failed;
1056 dst_pos_negative->target = check_failed;
1057 dst_bad_len->target = check_failed;
1058 jmp_to_begin_loop->target = begin_loop;
1059 jmp_to_ret->target = return_point;
1060
1061 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001062 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001063
1064 return true;
1065}
1066
buzbee2700f7e2014-03-07 09:46:20 -08001067LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
1068 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069}
1070
buzbee2700f7e2014-03-07 09:46:20 -08001071LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001072 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073}
1074
buzbee2700f7e2014-03-07 09:46:20 -08001075LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001076 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077}
1078
Ningsheng Jiana262f772014-11-25 16:48:07 +08001079void ArmMir2Lir::GenMaddMsubInt(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1080 RegLocation rl_src3, bool is_sub) {
1081 rl_src1 = LoadValue(rl_src1, kCoreReg);
1082 rl_src2 = LoadValue(rl_src2, kCoreReg);
1083 rl_src3 = LoadValue(rl_src3, kCoreReg);
1084 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1085 NewLIR4(is_sub ? kThumb2Mls : kThumb2Mla, rl_result.reg.GetReg(), rl_src1.reg.GetReg(),
1086 rl_src2.reg.GetReg(), rl_src3.reg.GetReg());
1087 StoreValue(rl_dest, rl_result);
1088}
1089
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1091 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001092 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001093 UNUSED(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001094 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 EncodeShift(kArmLsl, second_bit - first_bit));
1096 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001097 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 }
1099}
1100
Mingyao Yange643a172014-04-08 11:02:52 -07001101void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001102 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1103 RegStorage t_reg = AllocTemp();
1104 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001106 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107}
1108
1109// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001110LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001111#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001112 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001114#else
1115 RegStorage t_reg = AllocTemp();
1116 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
Ian Rogers8ba17f62014-10-27 18:48:49 -07001117 t_reg, kUnsignedHalf, kNotVolatile);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001118 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1119 0, target);
1120 FreeTemp(t_reg);
1121 return cmp_branch;
1122#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123}
1124
1125// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001126LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001128 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001129 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 return OpCondBranch(c_code, target);
1131}
1132
Andreas Gampeb14329f2014-05-15 11:16:06 -07001133bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Elliott Hughes8366ca02014-11-17 12:02:05 -08001134 if (!cu_->GetInstructionSetFeatures()->IsSmp()) {
1135 return false;
1136 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001137 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1138 LIR* barrier = last_lir_insn_;
1139
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 int dmb_flavor;
1141 // TODO: revisit Arm barrier kinds
1142 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001143 case kAnyStore: dmb_flavor = kISH; break;
1144 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001145 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001146 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 default:
1148 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1149 dmb_flavor = kSY; // quiet gcc.
1150 break;
1151 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001152
Andreas Gampeb14329f2014-05-15 11:16:06 -07001153 bool ret = false;
1154
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001155 // If the same barrier already exists, don't generate another.
1156 if (barrier == nullptr
1157 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1158 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001159 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001160 }
1161
1162 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1163 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001164 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001165 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166}
1167
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001168void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 rl_src = LoadValueWide(rl_src, kCoreReg);
1170 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001171 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 LoadConstantNoClobber(z_reg, 0);
1173 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001174 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1175 RegStorage t_reg = AllocTemp();
Vladimir Marko2f340a82014-12-01 16:48:48 +00001176 OpRegCopy(t_reg, rl_result.reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -08001177 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1178 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 FreeTemp(t_reg);
1180 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001181 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1182 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 }
1184 FreeTemp(z_reg);
1185 StoreValueWide(rl_dest, rl_result);
1186}
1187
Mark Mendelle02d48f2014-01-15 11:19:23 -08001188void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1189 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001190 UNUSED(opcode);
1191 /*
1192 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1193 * dest = src1.lo * src2.lo;
1194 * tmp1 += src1.lo * src2.hi;
1195 * dest.hi += tmp1;
1196 *
1197 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1198 * registers. Normally for Arm, we get 5. We can get to 6 by including
1199 * lr in the temp set. The only problematic case is all operands and result are
1200 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1201 * freeing operand temp registers after they are no longer needed. All other cases
1202 * can proceed normally. We'll just punt on the case of the result having a misaligned
1203 * overlap with either operand and send that case to a runtime handler.
1204 */
1205 RegLocation rl_result;
1206 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1207 FlushAllRegs();
1208 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1209 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001210 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001211 return;
1212 }
1213
1214 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1215 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1216
1217 int reg_status = 0;
1218 RegStorage res_lo;
1219 RegStorage res_hi;
1220 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1221 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1222 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1223 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1224 // Check if rl_dest is *not* either operand and we have enough temp registers.
1225 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1226 (dest_promoted || src1_promoted || src2_promoted)) {
1227 // In this case, we do not need to manually allocate temp registers for result.
1228 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1229 res_lo = rl_result.reg.GetLow();
1230 res_hi = rl_result.reg.GetHigh();
1231 } else {
1232 res_lo = AllocTemp();
1233 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1234 // In this case, we have enough temp registers to be allocated for result.
1235 res_hi = AllocTemp();
1236 reg_status = 1;
1237 } else {
1238 // In this case, all temps are now allocated.
1239 // res_hi will be allocated after we can free src1_hi.
1240 reg_status = 2;
1241 }
1242 }
1243
1244 // Temporarily add LR to the temp pool, and assign it to tmp1
1245 MarkTemp(rs_rARM_LR);
1246 FreeTemp(rs_rARM_LR);
1247 RegStorage tmp1 = rs_rARM_LR;
1248 LockTemp(rs_rARM_LR);
1249
1250 if (rl_src1.reg == rl_src2.reg) {
1251 DCHECK(res_hi.Valid());
1252 DCHECK(res_lo.Valid());
1253 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1254 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1255 rl_src1.reg.GetLowReg());
1256 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1257 } else {
1258 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1259 if (reg_status == 2) {
1260 DCHECK(!res_hi.Valid());
1261 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1262 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1263 // Will force free src1_hi, so must clobber.
1264 Clobber(rl_src1.reg);
1265 FreeTemp(rl_src1.reg.GetHigh());
1266 res_hi = AllocTemp();
1267 }
1268 DCHECK(res_hi.Valid());
1269 DCHECK(res_lo.Valid());
1270 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1271 rl_src1.reg.GetLowReg());
1272 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1273 tmp1.GetReg());
1274 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1275 if (reg_status == 2) {
1276 FreeTemp(rl_src1.reg.GetLow());
1277 }
1278 }
1279
1280 // Now, restore lr to its non-temp status.
1281 FreeTemp(tmp1);
1282 Clobber(rs_rARM_LR);
1283 UnmarkTemp(rs_rARM_LR);
1284
1285 if (reg_status != 0) {
1286 // We had manually allocated registers for rl_result.
1287 // Now construct a RegLocation.
1288 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1289 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1290 }
1291
1292 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293}
1294
Andreas Gampec76c6142014-08-04 16:30:03 -07001295void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001296 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001297 switch (opcode) {
1298 case Instruction::MUL_LONG:
1299 case Instruction::MUL_LONG_2ADDR:
1300 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1301 return;
1302 case Instruction::NEG_LONG:
1303 GenNegLong(rl_dest, rl_src2);
1304 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305
Andreas Gampec76c6142014-08-04 16:30:03 -07001306 default:
1307 break;
1308 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001309
Andreas Gampec76c6142014-08-04 16:30:03 -07001310 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001311 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001312}
1313
1314/*
1315 * Generate array load
1316 */
1317void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001318 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001319 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 int len_offset = mirror::Array::LengthOffset().Int32Value();
1321 int data_offset;
1322 RegLocation rl_result;
1323 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001324 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 if (!constant_index) {
1326 rl_index = LoadValue(rl_index, kCoreReg);
1327 }
1328
1329 if (rl_dest.wide) {
1330 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1331 } else {
1332 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1333 }
1334
1335 // If index is constant, just fold it into the data offset
1336 if (constant_index) {
1337 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1338 }
1339
1340 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001341 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342
1343 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001344 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 if (needs_range_check) {
1346 reg_len = AllocTemp();
1347 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001348 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001349 MarkPossibleNullPointerException(opt_flags);
1350 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001351 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 }
1353 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001354 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001356 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 } else {
1358 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001359 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001360 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001361 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 }
1363 rl_result = EvalLoc(rl_dest, reg_class, true);
1364
1365 if (needs_range_check) {
1366 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001367 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001369 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 }
1371 FreeTemp(reg_len);
1372 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001373 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001374 if (!constant_index) {
1375 FreeTemp(reg_ptr);
1376 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 StoreValueWide(rl_dest, rl_result);
1379 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001380 StoreValue(rl_dest, rl_result);
1381 }
1382 } else {
1383 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001384 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001385 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001386 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 rl_result = EvalLoc(rl_dest, reg_class, true);
1388
1389 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001390 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 FreeTemp(reg_len);
1392 }
buzbee2700f7e2014-03-07 09:46:20 -08001393 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 FreeTemp(reg_ptr);
1395 StoreValue(rl_dest, rl_result);
1396 }
1397}
1398
1399/*
1400 * Generate array store
1401 *
1402 */
1403void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001404 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001405 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407 bool constant_index = rl_index.is_const;
1408
Ian Rogersa9a82542013-10-04 11:17:26 -07001409 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001410 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1412 } else {
1413 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1414 }
1415
1416 // If index is constant, just fold it into the data offset.
1417 if (constant_index) {
1418 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1419 }
1420
buzbeea0cd2d72014-06-01 09:33:49 -07001421 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 if (!constant_index) {
1423 rl_index = LoadValue(rl_index, kCoreReg);
1424 }
1425
buzbee2700f7e2014-03-07 09:46:20 -08001426 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001427 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001429 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001430 } else if (IsTemp(rl_array.reg) && !card_mark) {
1431 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001432 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001433 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001434 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001435 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 }
1437
1438 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001439 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440
1441 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001442 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 if (needs_range_check) {
1444 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001445 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001446 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001447 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001448 MarkPossibleNullPointerException(opt_flags);
1449 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001450 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 }
1452 /* at this point, reg_ptr points to array, 2 live temps */
1453 if (rl_src.wide || rl_src.fp || constant_index) {
1454 if (rl_src.wide) {
1455 rl_src = LoadValueWide(rl_src, reg_class);
1456 } else {
1457 rl_src = LoadValue(rl_src, reg_class);
1458 }
1459 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001460 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 }
1462 if (needs_range_check) {
1463 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001464 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001466 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001467 }
1468 FreeTemp(reg_len);
1469 }
1470
Andreas Gampe3c12c512014-06-24 18:46:29 +00001471 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 } else {
1473 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001474 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 rl_src = LoadValue(rl_src, reg_class);
1476 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001477 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 FreeTemp(reg_len);
1479 }
buzbee2700f7e2014-03-07 09:46:20 -08001480 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 }
Ian Rogers773aab12013-10-14 13:50:10 -07001482 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 FreeTemp(reg_ptr);
1484 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001485 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001486 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001487 }
1488}
1489
Ian Rogersa9a82542013-10-04 11:17:26 -07001490
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001492 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1493 int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001494 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 rl_src = LoadValueWide(rl_src, kCoreReg);
1496 // Per spec, we only care about low 6 bits of shift amount.
1497 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1498 if (shift_amount == 0) {
1499 StoreValueWide(rl_dest, rl_src);
1500 return;
1501 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001502 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1504 return;
1505 }
1506 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001507 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 case Instruction::SHL_LONG:
1509 case Instruction::SHL_LONG_2ADDR:
1510 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001511 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1512 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001513 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001514 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1515 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001517 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1518 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001519 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001520 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001521 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001523 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
1525 break;
1526 case Instruction::SHR_LONG:
1527 case Instruction::SHR_LONG_2ADDR:
1528 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001529 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1530 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001531 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001532 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1533 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
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(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 }
1542 break;
1543 case Instruction::USHR_LONG:
1544 case Instruction::USHR_LONG_2ADDR:
1545 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001546 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1547 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001548 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001549 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1550 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001551 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001552 RegStorage t_reg = AllocTemp();
1553 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001554 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 EncodeShift(kArmLsl, 32 - shift_amount));
1556 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001557 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001558 }
1559 break;
1560 default:
1561 LOG(FATAL) << "Unexpected case";
1562 }
1563 StoreValueWide(rl_dest, rl_result);
1564}
1565
1566void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001567 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1568 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1570 if (!rl_src2.is_const) {
1571 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001572 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 return;
1574 }
1575 } else {
1576 // Normalize
1577 if (!rl_src2.is_const) {
1578 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001579 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 }
1581 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001582 if (PartiallyIntersects(rl_src1, rl_dest)) {
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 DCHECK(rl_src2.is_const);
1587 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1588 uint32_t val_lo = Low32Bits(val);
1589 uint32_t val_hi = High32Bits(val);
1590 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1591 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1592
1593 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001594 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 case Instruction::ADD_LONG:
1596 case Instruction::ADD_LONG_2ADDR:
1597 case Instruction::SUB_LONG:
1598 case Instruction::SUB_LONG_2ADDR:
1599 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001600 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001601 return;
1602 }
1603 break;
1604 default:
1605 break;
1606 }
1607 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1608 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1609 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1610 switch (opcode) {
1611 case Instruction::ADD_LONG:
1612 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001613 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001614 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 break;
1616 case Instruction::OR_LONG:
1617 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001618 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1619 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001620 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001621 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001622 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 }
1624 break;
1625 case Instruction::XOR_LONG:
1626 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001627 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1628 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 break;
1630 case Instruction::AND_LONG:
1631 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001632 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1633 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001634 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001635 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001636 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 }
1638 break;
1639 case Instruction::SUB_LONG_2ADDR:
1640 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001641 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001642 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 break;
1644 default:
1645 LOG(FATAL) << "Unexpected opcode " << opcode;
1646 }
1647 StoreValueWide(rl_dest, rl_result);
1648}
1649
1650} // namespace art