blob: 03e0e92aec7b2b36c0bc3e63fa6a472849a16dde [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) {
Andreas Gampecfe71e52015-01-05 19:30:59 -0800571 if (lit == 0) {
572 // Special case for *divide-by-zero*. The ops won't actually be used to generate code, as
573 // GenArithOpIntLit will directly generate exception-throwing code, and multiply-by-zero will
574 // have been optimized away earlier.
575 op->op = kOpInvalid;
576 return true;
577 }
578
Ian Rogerse2143c02014-03-28 08:47:16 -0700579 if (IsPowerOfTwo(lit)) {
580 op->op = kOpLsl;
Andreas Gampe7e499922015-01-06 08:28:12 -0800581 op->shift = CTZ(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700582 return true;
583 }
584
585 if (IsPowerOfTwo(lit - 1)) {
586 op->op = kOpAdd;
Andreas Gampe7e499922015-01-06 08:28:12 -0800587 op->shift = CTZ(lit - 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700588 return true;
589 }
590
591 if (IsPowerOfTwo(lit + 1)) {
592 op->op = kOpRsub;
Andreas Gampe7e499922015-01-06 08:28:12 -0800593 op->shift = CTZ(lit + 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700594 return true;
595 }
596
597 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100598 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700599 return false;
600}
601
602// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
603bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700604 if (GetEasyMultiplyOp(lit, &ops[0])) {
605 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100606 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700607 return true;
608 }
609
610 int lit1 = lit;
Andreas Gampe7e499922015-01-06 08:28:12 -0800611 uint32_t shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700612 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
613 ops[1].op = kOpLsl;
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 = kOpAdd;
622 ops[1].shift = shift;
623 return true;
624 }
625
626 lit1 = lit + 1;
Andreas Gampe7e499922015-01-06 08:28:12 -0800627 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700628 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
629 ops[1].op = kOpRsub;
630 ops[1].shift = shift;
631 return true;
632 }
633
634 return false;
635}
636
Zheng Xuf9719f92014-04-02 13:31:31 +0100637// Generate instructions to do multiply.
638// Additional temporary register is required,
639// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700640void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100641 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
642 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
643
644 RegStorage r_tmp1;
645 if (ops[1].op == kOpInvalid) {
646 r_tmp1 = r_dest;
647 } else if (r_dest.GetReg() != r_src.GetReg()) {
648 r_tmp1 = r_dest;
649 } else {
650 r_tmp1 = AllocTemp();
651 }
652
653 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700654 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100655 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700656 break;
657 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100658 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700659 break;
660 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100661 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700662 break;
663 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100664 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700665 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100666 }
667
668 switch (ops[1].op) {
669 case kOpInvalid:
670 return;
671 case kOpLsl:
672 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
673 break;
674 case kOpAdd:
675 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
676 break;
677 case kOpRsub:
678 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
679 break;
680 default:
681 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
682 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700683 }
684}
685
686bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
687 EasyMultiplyOp ops[2];
688
689 if (!GetEasyMultiplyTwoOps(lit, ops)) {
690 return false;
691 }
692
693 rl_src = LoadValue(rl_src, kCoreReg);
694 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
695
696 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
697 StoreValue(rl_dest, rl_result);
698 return true;
699}
700
Mark Mendell2bf31e62014-01-23 12:13:40 -0800701RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700702 RegLocation rl_src2, bool is_div, int flags) {
703 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800704 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700705 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800706}
707
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700708RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
709 bool is_div) {
710 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800711 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700712 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800713}
714
buzbee2700f7e2014-03-07 09:46:20 -0800715RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700716 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
717
718 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800719 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700720 LoadConstant(lit_temp, lit);
721 // Use the generic case for div/rem with arg2 in a register.
722 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
723 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
724 FreeTemp(lit_temp);
725
726 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727}
728
buzbee2700f7e2014-03-07 09:46:20 -0800729RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700730 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700731 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
732 if (is_div) {
733 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800734 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700735 } else {
736 // Remainder case, use the following code:
737 // temp = reg1 / reg2 - integer division
738 // temp = temp * reg2
739 // dest = reg1 - temp
740
buzbee2700f7e2014-03-07 09:46:20 -0800741 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700742 OpRegRegReg(kOpDiv, temp, reg1, reg2);
743 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800744 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700745 FreeTemp(temp);
746 }
747
748 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749}
750
Serban Constantinescu23abec92014-07-02 16:13:38 +0100751bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100753 if (is_long) {
754 return false;
755 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 RegLocation rl_src1 = info->args[0];
757 RegLocation rl_src2 = info->args[1];
758 rl_src1 = LoadValue(rl_src1, kCoreReg);
759 rl_src2 = LoadValue(rl_src2, kCoreReg);
760 RegLocation rl_dest = InlineTarget(info);
761 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800762 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700763 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800764 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
765 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700766 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 StoreValue(rl_dest, rl_result);
768 return true;
769}
770
Vladimir Markoe508a202013-11-04 15:24:22 +0000771bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
772 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800773 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000774 RegLocation rl_dest = InlineTarget(info);
775 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
776 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700777 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000778 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800779 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700780 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
781 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000782 } else {
buzbee695d13a2014-04-19 13:32:20 -0700783 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
784 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000785 }
786 StoreValueWide(rl_dest, rl_result);
787 } else {
buzbee695d13a2014-04-19 13:32:20 -0700788 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000789 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000790 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000791 StoreValue(rl_dest, rl_result);
792 }
793 return true;
794}
795
796bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
797 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800798 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000799 RegLocation rl_src_value = info->args[2]; // [size] value
800 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700801 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000802 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
803 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000804 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
805 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000806 } else {
buzbee695d13a2014-04-19 13:32:20 -0700807 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000808 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
809 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000810 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000811 }
812 return true;
813}
814
Hans Boehm48f5c472014-06-27 14:50:10 -0700815// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000816bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 DCHECK_EQ(cu_->instruction_set, kThumb2);
818 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000819 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
820 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800821 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000822 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000823 // If is_long, high half is in info->args[5]
824 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
825 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 RegLocation rl_dest = InlineTarget(info); // boolean place for result
827
Vladimir Marko3e5af822013-11-21 15:01:20 +0000828 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
829 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
830 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
831 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
832 // into the same temps, reducing the number of required temps down to 5. We shall work
833 // around the potentially locked temp by using LR for r_ptr, unconditionally.
834 // TODO: Pass information about the need for more temps to the stack frame generation
835 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700836 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
837 MarkTemp(rs_rARM_LR);
838 FreeTemp(rs_rARM_LR);
839 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000840 bool load_early = true;
841 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700842 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
843 rl_src_expected.reg;
844 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
845 rl_src_new_value.reg;
846 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
847 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800848 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
849 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000850
851 if (!expected_is_good_reg && !new_value_is_good_reg) {
852 // None of expected/new_value is non-temp reg, need to load both late
853 load_early = false;
854 // Make sure they are not in the temp regs and the load will not be skipped.
855 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800856 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000857 ClobberSReg(rl_src_expected.s_reg_low);
858 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
859 rl_src_expected.location = kLocDalvikFrame;
860 }
861 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800862 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000863 ClobberSReg(rl_src_new_value.s_reg_low);
864 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
865 rl_src_new_value.location = kLocDalvikFrame;
866 }
867 }
868 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869
Hans Boehm48f5c472014-06-27 14:50:10 -0700870 // Prevent reordering with prior memory operations.
871 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872
buzbeea0cd2d72014-06-01 09:33:49 -0700873 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000874 RegLocation rl_new_value;
875 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700876 rl_new_value = LoadValue(rl_src_new_value, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000877 } else if (load_early) {
878 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
879 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880
Vladimir Marko1c282e22013-11-21 14:49:47 +0000881 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 // Mark card for object assuming new value is stored.
Vladimir Marko743b98c2014-11-24 19:45:41 +0000883 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 }
885
886 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
887
buzbee2700f7e2014-03-07 09:46:20 -0800888 RegStorage r_ptr = rs_rARM_LR;
889 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890
891 // Free now unneeded rl_object and rl_offset to give more temps.
892 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700893 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700894 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700895 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896
Vladimir Marko3e5af822013-11-21 15:01:20 +0000897 RegLocation rl_expected;
898 if (!is_long) {
buzbee7c02e912014-10-03 13:14:17 -0700899 rl_expected = LoadValue(rl_src_expected, LocToRegClass(rl_src_new_value));
Vladimir Marko3e5af822013-11-21 15:01:20 +0000900 } else if (load_early) {
901 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
902 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000903 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700904 RegStorage low_reg = AllocTemp();
905 RegStorage high_reg = AllocTemp();
906 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000907 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000908 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700909
Vladimir Marko3e5af822013-11-21 15:01:20 +0000910 // do {
911 // tmp = [r_ptr] - expected;
912 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
913 // result = tmp != 0;
914
buzbee2700f7e2014-03-07 09:46:20 -0800915 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700916 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700917
Dave Allison3da67a52014-04-02 17:03:45 -0700918 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000919 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800920 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000921 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800922 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000923 }
buzbee2700f7e2014-03-07 09:46:20 -0800924 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
925 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
926 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000927 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800928 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000929 }
930 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700931 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800932 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000933 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800934 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000935 }
936 FreeTemp(r_tmp_high); // Now unneeded
937
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100938 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700939 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800940 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 +0000941
942 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800943 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
944 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100945 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700946 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800947 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000948 }
949
950 // Still one conditional left from OpIT(kCondEq, "T") from either branch
951 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700952 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700953
Jeff Hao2de2aa12013-09-12 17:20:31 -0700954 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955
Vladimir Marko3e5af822013-11-21 15:01:20 +0000956 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800957 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000958 }
959
Hans Boehm48f5c472014-06-27 14:50:10 -0700960 // Prevent reordering with subsequent memory operations.
961 GenMemBarrier(kLoadAny);
962
Vladimir Marko3e5af822013-11-21 15:01:20 +0000963 // result := (tmp1 != 0) ? 0 : 1;
964 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800965 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100966 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700967 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800968 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000969 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700970 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000971
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 StoreValue(rl_dest, rl_result);
973
Vladimir Marko3e5af822013-11-21 15:01:20 +0000974 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700975 Clobber(rs_rARM_LR);
976 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 return true;
978}
979
Zheng Xu947717a2014-08-07 14:05:23 +0800980bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
981 constexpr int kLargeArrayThreshold = 256;
982
983 RegLocation rl_src = info->args[0];
984 RegLocation rl_src_pos = info->args[1];
985 RegLocation rl_dst = info->args[2];
986 RegLocation rl_dst_pos = info->args[3];
987 RegLocation rl_length = info->args[4];
988 // Compile time check, handle exception by non-inline method to reduce related meta-data.
989 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
990 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
991 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
992 return false;
993 }
994
995 ClobberCallerSave();
996 LockCallTemps(); // Prepare for explicit register usage.
997 LockTemp(rs_r12);
998 RegStorage rs_src = rs_r0;
999 RegStorage rs_dst = rs_r1;
1000 LoadValueDirectFixed(rl_src, rs_src);
1001 LoadValueDirectFixed(rl_dst, rs_dst);
1002
1003 // Handle null pointer exception in slow-path.
1004 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
1005 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
1006 // Handle potential overlapping in slow-path.
1007 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
1008 // Handle exception or big length in slow-path.
1009 RegStorage rs_length = rs_r2;
1010 LoadValueDirectFixed(rl_length, rs_length);
1011 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
1012 // Src bounds check.
1013 RegStorage rs_pos = rs_r3;
1014 RegStorage rs_arr_length = rs_r12;
1015 LoadValueDirectFixed(rl_src_pos, rs_pos);
1016 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1017 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1018 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1019 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1020 // Dst bounds check.
1021 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1022 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1023 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1024 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1025 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1026
1027 // Everything is checked now.
1028 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1029 OpRegReg(kOpAdd, rs_dst, rs_pos);
1030 OpRegReg(kOpAdd, rs_dst, rs_pos);
1031 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1032 LoadValueDirectFixed(rl_src_pos, rs_pos);
1033 OpRegReg(kOpAdd, rs_src, rs_pos);
1034 OpRegReg(kOpAdd, rs_src, rs_pos);
1035
1036 RegStorage rs_tmp = rs_pos;
1037 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1038
1039 // Copy one element.
1040 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1041 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1042 OpRegImm(kOpSub, rs_length, 2);
1043 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1044 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1045
1046 // Copy two elements.
1047 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1048 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1049 OpRegImm(kOpSub, rs_length, 4);
1050 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1051 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1052 OpUnconditionalBranch(begin_loop);
1053
1054 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1055 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1056 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1057
1058 src_check_branch->target = check_failed;
1059 dst_check_branch->target = check_failed;
1060 src_dst_same->target = check_failed;
1061 len_neg_or_too_big->target = check_failed;
1062 src_pos_negative->target = check_failed;
1063 src_bad_len->target = check_failed;
1064 dst_pos_negative->target = check_failed;
1065 dst_bad_len->target = check_failed;
1066 jmp_to_begin_loop->target = begin_loop;
1067 jmp_to_ret->target = return_point;
1068
1069 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001070 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001071
1072 return true;
1073}
1074
buzbee2700f7e2014-03-07 09:46:20 -08001075LIR* ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
1076 return RawLIR(current_dalvik_offset_, kThumb2LdrPcRel12, reg.GetReg(), 0, 0, 0, 0, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077}
1078
buzbee2700f7e2014-03-07 09:46:20 -08001079LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001080 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081}
1082
buzbee2700f7e2014-03-07 09:46:20 -08001083LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001084 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085}
1086
Ningsheng Jiana262f772014-11-25 16:48:07 +08001087void ArmMir2Lir::GenMaddMsubInt(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1088 RegLocation rl_src3, bool is_sub) {
1089 rl_src1 = LoadValue(rl_src1, kCoreReg);
1090 rl_src2 = LoadValue(rl_src2, kCoreReg);
1091 rl_src3 = LoadValue(rl_src3, kCoreReg);
1092 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1093 NewLIR4(is_sub ? kThumb2Mls : kThumb2Mla, rl_result.reg.GetReg(), rl_src1.reg.GetReg(),
1094 rl_src2.reg.GetReg(), rl_src3.reg.GetReg());
1095 StoreValue(rl_dest, rl_result);
1096}
1097
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1099 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001100 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101 UNUSED(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001102 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103 EncodeShift(kArmLsl, second_bit - first_bit));
1104 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001105 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 }
1107}
1108
Mingyao Yange643a172014-04-08 11:02:52 -07001109void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001110 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1111 RegStorage t_reg = AllocTemp();
1112 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001114 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115}
1116
1117// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001118LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001119#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001120 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001122#else
1123 RegStorage t_reg = AllocTemp();
1124 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
Ian Rogers8ba17f62014-10-27 18:48:49 -07001125 t_reg, kUnsignedHalf, kNotVolatile);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001126 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1127 0, target);
1128 FreeTemp(t_reg);
1129 return cmp_branch;
1130#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131}
1132
1133// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001134LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001136 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001137 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 return OpCondBranch(c_code, target);
1139}
1140
Andreas Gampeb14329f2014-05-15 11:16:06 -07001141bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Elliott Hughes8366ca02014-11-17 12:02:05 -08001142 if (!cu_->GetInstructionSetFeatures()->IsSmp()) {
1143 return false;
1144 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001145 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1146 LIR* barrier = last_lir_insn_;
1147
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 int dmb_flavor;
1149 // TODO: revisit Arm barrier kinds
1150 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001151 case kAnyStore: dmb_flavor = kISH; break;
1152 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001153 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001154 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 default:
1156 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1157 dmb_flavor = kSY; // quiet gcc.
1158 break;
1159 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001160
Andreas Gampeb14329f2014-05-15 11:16:06 -07001161 bool ret = false;
1162
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001163 // If the same barrier already exists, don't generate another.
1164 if (barrier == nullptr
1165 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1166 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001167 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001168 }
1169
1170 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1171 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001172 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001173 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174}
1175
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001176void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 rl_src = LoadValueWide(rl_src, kCoreReg);
1178 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001179 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 LoadConstantNoClobber(z_reg, 0);
1181 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001182 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1183 RegStorage t_reg = AllocTemp();
Vladimir Marko2f340a82014-12-01 16:48:48 +00001184 OpRegCopy(t_reg, rl_result.reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -08001185 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1186 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 FreeTemp(t_reg);
1188 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001189 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1190 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 }
1192 FreeTemp(z_reg);
1193 StoreValueWide(rl_dest, rl_result);
1194}
1195
Mark Mendelle02d48f2014-01-15 11:19:23 -08001196void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1197 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001198 UNUSED(opcode);
1199 /*
1200 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1201 * dest = src1.lo * src2.lo;
1202 * tmp1 += src1.lo * src2.hi;
1203 * dest.hi += tmp1;
1204 *
1205 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1206 * registers. Normally for Arm, we get 5. We can get to 6 by including
1207 * lr in the temp set. The only problematic case is all operands and result are
1208 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1209 * freeing operand temp registers after they are no longer needed. All other cases
1210 * can proceed normally. We'll just punt on the case of the result having a misaligned
1211 * overlap with either operand and send that case to a runtime handler.
1212 */
1213 RegLocation rl_result;
1214 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1215 FlushAllRegs();
1216 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1217 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001218 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001219 return;
1220 }
1221
1222 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1223 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1224
1225 int reg_status = 0;
1226 RegStorage res_lo;
1227 RegStorage res_hi;
1228 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1229 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1230 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1231 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1232 // Check if rl_dest is *not* either operand and we have enough temp registers.
1233 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1234 (dest_promoted || src1_promoted || src2_promoted)) {
1235 // In this case, we do not need to manually allocate temp registers for result.
1236 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1237 res_lo = rl_result.reg.GetLow();
1238 res_hi = rl_result.reg.GetHigh();
1239 } else {
1240 res_lo = AllocTemp();
1241 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1242 // In this case, we have enough temp registers to be allocated for result.
1243 res_hi = AllocTemp();
1244 reg_status = 1;
1245 } else {
1246 // In this case, all temps are now allocated.
1247 // res_hi will be allocated after we can free src1_hi.
1248 reg_status = 2;
1249 }
1250 }
1251
1252 // Temporarily add LR to the temp pool, and assign it to tmp1
1253 MarkTemp(rs_rARM_LR);
1254 FreeTemp(rs_rARM_LR);
1255 RegStorage tmp1 = rs_rARM_LR;
1256 LockTemp(rs_rARM_LR);
1257
1258 if (rl_src1.reg == rl_src2.reg) {
1259 DCHECK(res_hi.Valid());
1260 DCHECK(res_lo.Valid());
1261 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1262 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1263 rl_src1.reg.GetLowReg());
1264 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1265 } else {
1266 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1267 if (reg_status == 2) {
1268 DCHECK(!res_hi.Valid());
1269 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1270 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1271 // Will force free src1_hi, so must clobber.
1272 Clobber(rl_src1.reg);
1273 FreeTemp(rl_src1.reg.GetHigh());
1274 res_hi = AllocTemp();
1275 }
1276 DCHECK(res_hi.Valid());
1277 DCHECK(res_lo.Valid());
1278 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1279 rl_src1.reg.GetLowReg());
1280 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1281 tmp1.GetReg());
1282 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1283 if (reg_status == 2) {
1284 FreeTemp(rl_src1.reg.GetLow());
1285 }
1286 }
1287
1288 // Now, restore lr to its non-temp status.
1289 FreeTemp(tmp1);
1290 Clobber(rs_rARM_LR);
1291 UnmarkTemp(rs_rARM_LR);
1292
1293 if (reg_status != 0) {
1294 // We had manually allocated registers for rl_result.
1295 // Now construct a RegLocation.
1296 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1297 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1298 }
1299
1300 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301}
1302
Andreas Gampec76c6142014-08-04 16:30:03 -07001303void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001304 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001305 switch (opcode) {
1306 case Instruction::MUL_LONG:
1307 case Instruction::MUL_LONG_2ADDR:
1308 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1309 return;
1310 case Instruction::NEG_LONG:
1311 GenNegLong(rl_dest, rl_src2);
1312 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313
Andreas Gampec76c6142014-08-04 16:30:03 -07001314 default:
1315 break;
1316 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317
Andreas Gampec76c6142014-08-04 16:30:03 -07001318 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001319 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320}
1321
1322/*
1323 * Generate array load
1324 */
1325void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001326 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001327 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 int len_offset = mirror::Array::LengthOffset().Int32Value();
1329 int data_offset;
1330 RegLocation rl_result;
1331 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001332 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 if (!constant_index) {
1334 rl_index = LoadValue(rl_index, kCoreReg);
1335 }
1336
1337 if (rl_dest.wide) {
1338 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1339 } else {
1340 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1341 }
1342
1343 // If index is constant, just fold it into the data offset
1344 if (constant_index) {
1345 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1346 }
1347
1348 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001349 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350
1351 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001352 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 if (needs_range_check) {
1354 reg_len = AllocTemp();
1355 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001356 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001357 MarkPossibleNullPointerException(opt_flags);
1358 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001359 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 }
1361 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001362 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001363 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001364 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 } else {
1366 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001367 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001368 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001369 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 }
1371 rl_result = EvalLoc(rl_dest, reg_class, true);
1372
1373 if (needs_range_check) {
1374 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001375 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001377 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 }
1379 FreeTemp(reg_len);
1380 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001381 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001382 if (!constant_index) {
1383 FreeTemp(reg_ptr);
1384 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001385 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 StoreValueWide(rl_dest, rl_result);
1387 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 StoreValue(rl_dest, rl_result);
1389 }
1390 } else {
1391 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001392 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001393 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001394 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 rl_result = EvalLoc(rl_dest, reg_class, true);
1396
1397 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001398 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 FreeTemp(reg_len);
1400 }
buzbee2700f7e2014-03-07 09:46:20 -08001401 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 FreeTemp(reg_ptr);
1403 StoreValue(rl_dest, rl_result);
1404 }
1405}
1406
1407/*
1408 * Generate array store
1409 *
1410 */
1411void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001412 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001413 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001415 bool constant_index = rl_index.is_const;
1416
Ian Rogersa9a82542013-10-04 11:17:26 -07001417 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001418 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1420 } else {
1421 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1422 }
1423
1424 // If index is constant, just fold it into the data offset.
1425 if (constant_index) {
1426 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1427 }
1428
buzbeea0cd2d72014-06-01 09:33:49 -07001429 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 if (!constant_index) {
1431 rl_index = LoadValue(rl_index, kCoreReg);
1432 }
1433
buzbee2700f7e2014-03-07 09:46:20 -08001434 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001435 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001437 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001438 } else if (IsTemp(rl_array.reg) && !card_mark) {
1439 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001440 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001441 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001442 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001443 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 }
1445
1446 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001447 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001448
1449 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001450 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 if (needs_range_check) {
1452 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001453 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001455 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001456 MarkPossibleNullPointerException(opt_flags);
1457 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001458 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 }
1460 /* at this point, reg_ptr points to array, 2 live temps */
1461 if (rl_src.wide || rl_src.fp || constant_index) {
1462 if (rl_src.wide) {
1463 rl_src = LoadValueWide(rl_src, reg_class);
1464 } else {
1465 rl_src = LoadValue(rl_src, reg_class);
1466 }
1467 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001468 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001469 }
1470 if (needs_range_check) {
1471 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001472 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001473 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001474 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001475 }
1476 FreeTemp(reg_len);
1477 }
1478
Andreas Gampe3c12c512014-06-24 18:46:29 +00001479 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 } else {
1481 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001482 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 rl_src = LoadValue(rl_src, reg_class);
1484 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001485 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001486 FreeTemp(reg_len);
1487 }
buzbee2700f7e2014-03-07 09:46:20 -08001488 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489 }
Ian Rogers773aab12013-10-14 13:50:10 -07001490 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001491 FreeTemp(reg_ptr);
1492 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001493 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001494 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 }
1496}
1497
Ian Rogersa9a82542013-10-04 11:17:26 -07001498
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001500 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1501 int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001503 rl_src = LoadValueWide(rl_src, kCoreReg);
1504 // Per spec, we only care about low 6 bits of shift amount.
1505 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1506 if (shift_amount == 0) {
1507 StoreValueWide(rl_dest, rl_src);
1508 return;
1509 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001510 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1512 return;
1513 }
1514 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001515 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 case Instruction::SHL_LONG:
1517 case Instruction::SHL_LONG_2ADDR:
1518 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001519 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1520 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001522 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1523 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001525 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1526 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001528 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001529 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001531 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 }
1533 break;
1534 case Instruction::SHR_LONG:
1535 case Instruction::SHR_LONG_2ADDR:
1536 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001537 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1538 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001540 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1541 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001542 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001543 RegStorage t_reg = AllocTemp();
1544 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001545 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 EncodeShift(kArmLsl, 32 - shift_amount));
1547 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001548 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 }
1550 break;
1551 case Instruction::USHR_LONG:
1552 case Instruction::USHR_LONG_2ADDR:
1553 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001554 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1555 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001557 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1558 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001560 RegStorage t_reg = AllocTemp();
1561 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001562 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563 EncodeShift(kArmLsl, 32 - shift_amount));
1564 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001565 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 }
1567 break;
1568 default:
1569 LOG(FATAL) << "Unexpected case";
1570 }
1571 StoreValueWide(rl_dest, rl_result);
1572}
1573
1574void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001575 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1576 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1578 if (!rl_src2.is_const) {
1579 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001580 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001581 return;
1582 }
1583 } else {
1584 // Normalize
1585 if (!rl_src2.is_const) {
1586 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001587 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588 }
1589 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001590 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001591 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001592 return;
1593 }
1594 DCHECK(rl_src2.is_const);
1595 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1596 uint32_t val_lo = Low32Bits(val);
1597 uint32_t val_hi = High32Bits(val);
1598 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1599 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1600
1601 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001602 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001603 case Instruction::ADD_LONG:
1604 case Instruction::ADD_LONG_2ADDR:
1605 case Instruction::SUB_LONG:
1606 case Instruction::SUB_LONG_2ADDR:
1607 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001608 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 return;
1610 }
1611 break;
1612 default:
1613 break;
1614 }
1615 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1616 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1617 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1618 switch (opcode) {
1619 case Instruction::ADD_LONG:
1620 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001621 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001622 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001623 break;
1624 case Instruction::OR_LONG:
1625 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001626 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1627 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001628 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001629 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001630 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001631 }
1632 break;
1633 case Instruction::XOR_LONG:
1634 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001635 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1636 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001637 break;
1638 case Instruction::AND_LONG:
1639 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001640 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1641 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001642 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001643 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001644 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001645 }
1646 break;
1647 case Instruction::SUB_LONG_2ADDR:
1648 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001649 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001650 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 break;
1652 default:
1653 LOG(FATAL) << "Unexpected opcode " << opcode;
1654 }
1655 StoreValueWide(rl_dest, rl_result);
1656}
1657
1658} // namespace art