blob: 62903afbbce482c64a5337714a81f37a4941611b [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
Andreas Gampe0b9203e2015-01-22 20:39:27 -080019#include "codegen_arm.h"
20
Elliott Hughes8366ca02014-11-17 12:02:05 -080021#include "arch/instruction_set_features.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "arm_lir.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "base/logging.h"
24#include "dex/compiler_ir.h"
25#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070027#include "dex/reg_storage_eq.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080028#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080031#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
33namespace art {
34
buzbee2700f7e2014-03-07 09:46:20 -080035LIR* ArmMir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 OpRegReg(kOpCmp, src1, src2);
37 return OpCondBranch(cond, target);
38}
39
40/*
41 * Generate a Thumb2 IT instruction, which can nullify up to
42 * four subsequent instructions based on a condition and its
43 * inverse. The condition applies to the first instruction, which
44 * is executed if the condition is met. The string "guide" consists
45 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
46 * A "T" means the instruction is executed if the condition is
47 * met, and an "E" means the instruction is executed if the condition
48 * is not met.
49 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070050LIR* ArmMir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 int mask;
52 int mask3 = 0;
53 int mask2 = 0;
54 int mask1 = 0;
55 ArmConditionCode code = ArmConditionEncoding(ccode);
56 int cond_bit = code & 1;
57 int alt_bit = cond_bit ^ 1;
58
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 switch (strlen(guide)) {
60 case 3:
61 mask1 = (guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070062 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 case 2:
64 mask2 = (guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070065 FALLTHROUGH_INTENDED;
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 case 1:
67 mask3 = (guide[0] == 'T') ? cond_bit : alt_bit;
68 break;
69 case 0:
70 break;
71 default:
72 LOG(FATAL) << "OAT: bad case in OpIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -070073 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 }
75 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
76 (1 << (3 - strlen(guide)));
77 return NewLIR2(kThumb2It, code, mask);
78}
79
Andreas Gampeb14329f2014-05-15 11:16:06 -070080void ArmMir2Lir::UpdateIT(LIR* it, const char* new_guide) {
81 int mask;
82 int mask3 = 0;
83 int mask2 = 0;
84 int mask1 = 0;
85 ArmConditionCode code = static_cast<ArmConditionCode>(it->operands[0]);
86 int cond_bit = code & 1;
87 int alt_bit = cond_bit ^ 1;
88
Andreas Gampeb14329f2014-05-15 11:16:06 -070089 switch (strlen(new_guide)) {
90 case 3:
91 mask1 = (new_guide[2] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070092 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070093 case 2:
94 mask2 = (new_guide[1] == 'T') ? cond_bit : alt_bit;
Ian Rogersfc787ec2014-10-09 21:56:44 -070095 FALLTHROUGH_INTENDED;
Andreas Gampeb14329f2014-05-15 11:16:06 -070096 case 1:
97 mask3 = (new_guide[0] == 'T') ? cond_bit : alt_bit;
98 break;
99 case 0:
100 break;
101 default:
102 LOG(FATAL) << "OAT: bad case in UpdateIT";
Ian Rogersfc787ec2014-10-09 21:56:44 -0700103 UNREACHABLE();
Andreas Gampeb14329f2014-05-15 11:16:06 -0700104 }
105 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
106 (1 << (3 - strlen(new_guide)));
107 it->operands[1] = mask;
108}
109
Dave Allison3da67a52014-04-02 17:03:45 -0700110void ArmMir2Lir::OpEndIT(LIR* it) {
111 // TODO: use the 'it' pointer to do some checks with the LIR, for example
112 // we could check that the number of instructions matches the mask
113 // in the IT instruction.
114 CHECK(it != nullptr);
115 GenBarrier();
116}
117
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118/*
119 * 64-bit 3way compare function.
120 * mov rX, #-1
121 * cmp op1hi, op2hi
122 * blt done
123 * bgt flip
124 * sub rX, op1lo, op2lo (treat as unsigned)
125 * beq done
126 * ite hi
127 * mov(hi) rX, #-1
128 * mov(!hi) rX, #1
129 * flip:
130 * neg rX
131 * done:
132 */
buzbeea1983d42014-04-07 12:35:39 -0700133void ArmMir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 LIR* target1;
135 LIR* target2;
136 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
137 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800138 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 LoadConstant(t_reg, -1);
buzbee2700f7e2014-03-07 09:46:20 -0800140 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 LIR* branch1 = OpCondBranch(kCondLt, NULL);
142 LIR* branch2 = OpCondBranch(kCondGt, NULL);
buzbeea1983d42014-04-07 12:35:39 -0700143 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 LIR* branch3 = OpCondBranch(kCondEq, NULL);
145
Dave Allison3da67a52014-04-02 17:03:45 -0700146 LIR* it = OpIT(kCondHi, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800147 NewLIR2(kThumb2MovI8M, t_reg.GetReg(), ModifiedImmediate(-1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 LoadConstant(t_reg, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700149 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150
151 target2 = NewLIR0(kPseudoTargetLabel);
152 OpRegReg(kOpNeg, t_reg, t_reg);
153
154 target1 = NewLIR0(kPseudoTargetLabel);
155
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700156 RegLocation rl_temp = LocCReturn(); // Just using as template, will change
buzbee2700f7e2014-03-07 09:46:20 -0800157 rl_temp.reg.SetReg(t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 StoreValue(rl_dest, rl_temp);
159 FreeTemp(t_reg);
160
161 branch1->target = target1;
162 branch2->target = target2;
163 branch3->target = branch1->target;
164}
165
166void ArmMir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700167 int64_t val, ConditionCode ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 int32_t val_lo = Low32Bits(val);
169 int32_t val_hi = High32Bits(val);
Brian Carlstrom42748892013-07-18 18:04:08 -0700170 DCHECK_GE(ModifiedImmediate(val_lo), 0);
171 DCHECK_GE(ModifiedImmediate(val_hi), 0);
buzbee0d829482013-10-11 15:24:55 -0700172 LIR* taken = &block_label_list_[bb->taken];
173 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800175 RegStorage low_reg = rl_src1.reg.GetLow();
176 RegStorage high_reg = rl_src1.reg.GetHigh();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177
Vladimir Marko58af1f92013-12-19 13:31:15 +0000178 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
buzbee2700f7e2014-03-07 09:46:20 -0800179 RegStorage t_reg = AllocTemp();
180 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), low_reg.GetReg(), high_reg.GetReg(), 0);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000181 FreeTemp(t_reg);
182 OpCondBranch(ccode, taken);
183 return;
184 }
185
Brian Carlstromdf629502013-07-17 22:39:56 -0700186 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 case kCondEq:
188 case kCondNe:
Vladimir Marko58af1f92013-12-19 13:31:15 +0000189 OpCmpImmBranch(kCondNe, high_reg, val_hi, (ccode == kCondEq) ? not_taken : taken);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 break;
191 case kCondLt:
192 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
193 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000194 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 break;
196 case kCondLe:
197 OpCmpImmBranch(kCondLt, high_reg, val_hi, taken);
198 OpCmpImmBranch(kCondGt, high_reg, val_hi, not_taken);
199 ccode = kCondLs;
200 break;
201 case kCondGt:
202 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
203 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
204 ccode = kCondHi;
205 break;
206 case kCondGe:
207 OpCmpImmBranch(kCondGt, high_reg, val_hi, taken);
208 OpCmpImmBranch(kCondLt, high_reg, val_hi, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000209 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 break;
211 default:
212 LOG(FATAL) << "Unexpected ccode: " << ccode;
213 }
214 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
215}
216
Andreas Gampe90969af2014-07-15 23:02:11 -0700217void ArmMir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
218 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700219 RegisterClass dest_reg_class) {
220 UNUSED(dest_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700221 // TODO: Generalize the IT below to accept more than one-instruction loads.
222 DCHECK(InexpensiveConstantInt(true_val));
223 DCHECK(InexpensiveConstantInt(false_val));
224
225 if ((true_val == 0 && code == kCondEq) ||
226 (false_val == 0 && code == kCondNe)) {
227 OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
228 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
229 LIR* it = OpIT(kCondNe, "");
230 LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
231 OpEndIT(it);
232 return;
233 }
234
235 OpRegReg(kOpCmp, left_op, right_op); // Same?
236 LIR* it = OpIT(code, "E"); // if-convert the test
237 LoadConstant(rs_dest, true_val); // .eq case - load true
238 LoadConstant(rs_dest, false_val); // .eq case - load true
239 OpEndIT(it);
240}
241
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700242void ArmMir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700243 UNUSED(bb);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 RegLocation rl_result;
245 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700247 // Avoid using float regs here.
248 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
249 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
250 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000251 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 if (mir->ssa_rep->num_uses == 1) {
253 // CONST case
254 int true_val = mir->dalvikInsn.vB;
255 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700256 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000257 // Change kCondNe to kCondEq for the special cases below.
258 if (ccode == kCondNe) {
259 ccode = kCondEq;
260 std::swap(true_val, false_val);
261 }
262 bool cheap_false_val = InexpensiveConstantInt(false_val);
263 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800264 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100265 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700266 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800267 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700268 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000269 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800270 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100271 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700272 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800273 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700274 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000275 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800276 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700277 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800278 LoadConstant(rl_result.reg, true_val);
279 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700280 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 } else {
282 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700283 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
284 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 LoadConstant(t_reg1, true_val);
286 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800287 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700288 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800289 OpRegCopy(rl_result.reg, t_reg1);
290 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700291 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 }
293 } else {
294 // MOVE case
295 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
296 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700297 rl_true = LoadValue(rl_true, result_reg_class);
298 rl_false = LoadValue(rl_false, result_reg_class);
299 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800300 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700301 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000302 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700303 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800304 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000305 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700306 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800307 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700308 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700309 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800310 OpRegCopy(rl_result.reg, rl_true.reg);
311 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700312 }
Dave Allison3da67a52014-04-02 17:03:45 -0700313 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 }
315 StoreValue(rl_dest, rl_result);
316}
317
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700318void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
320 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
321 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000322 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000324 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 ccode = FlipComparisonOrder(ccode);
326 }
327 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700328 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 // Do special compare/branch against simple const operand if not already in registers.
330 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700331 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
333 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
334 return;
335 }
336 }
buzbee0d829482013-10-11 15:24:55 -0700337 LIR* taken = &block_label_list_[bb->taken];
338 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
340 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800341 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700342 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 case kCondEq:
344 OpCondBranch(kCondNe, not_taken);
345 break;
346 case kCondNe:
347 OpCondBranch(kCondNe, taken);
348 break;
349 case kCondLt:
350 OpCondBranch(kCondLt, taken);
351 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000352 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 break;
354 case kCondLe:
355 OpCondBranch(kCondLt, taken);
356 OpCondBranch(kCondGt, not_taken);
357 ccode = kCondLs;
358 break;
359 case kCondGt:
360 OpCondBranch(kCondGt, taken);
361 OpCondBranch(kCondLt, not_taken);
362 ccode = kCondHi;
363 break;
364 case kCondGe:
365 OpCondBranch(kCondGt, taken);
366 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000367 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 break;
369 default:
370 LOG(FATAL) << "Unexpected ccode: " << ccode;
371 }
buzbee2700f7e2014-03-07 09:46:20 -0800372 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 OpCondBranch(ccode, taken);
374}
375
376/*
377 * Generate a register comparison to an immediate and branch. Caller
378 * is responsible for setting branch target field.
379 */
buzbee2700f7e2014-03-07 09:46:20 -0800380LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700381 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700383 /*
384 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
385 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700386 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700387 * be converted to a long form during assembly (which will trigger another assembly
388 * pass). Here we estimate the branch distance for checks, and if large directly
389 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700390 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700391 */
392 bool skip = ((target != NULL) && (target->opcode == kPseudoThrowTarget));
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700393 skip &= ((mir_graph_->GetNumDalvikInsns() - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700394 if (!skip && reg.Low8() && (check_value == 0)) {
395 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
396 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
397 reg.GetReg(), 0);
398 } else if (arm_cond == kArmCondLs) {
399 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
400 // This case happens for a bounds check of array[0].
401 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
402 }
403 }
404
405 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000406 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 branch = NewLIR2(kThumbBCond, 0, arm_cond);
408 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700409
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 branch->target = target;
411 return branch;
412}
413
buzbee2700f7e2014-03-07 09:46:20 -0800414LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 LIR* res;
416 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800417 // If src or dest is a pair, we'll be using low reg.
418 if (r_dest.IsPair()) {
419 r_dest = r_dest.GetLow();
420 }
421 if (r_src.IsPair()) {
422 r_src = r_src.GetLow();
423 }
buzbee091cc402014-03-31 10:14:40 -0700424 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700426 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700428 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700430 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 opcode = kThumbMovRR_H2L;
432 else
433 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800434 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
436 res->flags.is_nop = true;
437 }
438 return res;
439}
440
buzbee7a11ab02014-04-28 20:02:38 -0700441void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
442 if (r_dest != r_src) {
443 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
444 AppendLIR(res);
445 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446}
447
buzbee2700f7e2014-03-07 09:46:20 -0800448void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700449 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700450 bool dest_fp = r_dest.IsFloat();
451 bool src_fp = r_src.IsFloat();
452 DCHECK(r_dest.Is64Bit());
453 DCHECK(r_src.Is64Bit());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800454 // Note: If the register is get by register allocator, it should never be a pair.
455 // But some functions in mir_2_lir assume 64-bit registers are 32-bit register pairs.
456 // TODO: Rework Mir2Lir::LoadArg() and Mir2Lir::LoadArgDirect().
457 if (dest_fp && r_dest.IsPair()) {
458 r_dest = As64BitFloatReg(r_dest);
459 }
460 if (src_fp && r_src.IsPair()) {
461 r_src = As64BitFloatReg(r_src);
462 }
buzbee7a11ab02014-04-28 20:02:38 -0700463 if (dest_fp) {
464 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700465 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 } else {
buzbee091cc402014-03-31 10:14:40 -0700467 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700468 }
469 } else {
470 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700471 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700472 } else {
473 // Handle overlap
474 if (r_src.GetHighReg() == r_dest.GetLowReg()) {
475 DCHECK_NE(r_src.GetLowReg(), r_dest.GetHighReg());
476 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
477 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
478 } else {
479 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
480 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
481 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 }
483 }
484 }
485}
486
487// Table of magic divisors
488struct MagicTable {
489 uint32_t magic;
490 uint32_t shift;
491 DividePattern pattern;
492};
493
494static const MagicTable magic_table[] = {
495 {0, 0, DivideNone}, // 0
496 {0, 0, DivideNone}, // 1
497 {0, 0, DivideNone}, // 2
498 {0x55555556, 0, Divide3}, // 3
499 {0, 0, DivideNone}, // 4
500 {0x66666667, 1, Divide5}, // 5
501 {0x2AAAAAAB, 0, Divide3}, // 6
502 {0x92492493, 2, Divide7}, // 7
503 {0, 0, DivideNone}, // 8
504 {0x38E38E39, 1, Divide5}, // 9
505 {0x66666667, 2, Divide5}, // 10
506 {0x2E8BA2E9, 1, Divide5}, // 11
507 {0x2AAAAAAB, 1, Divide5}, // 12
508 {0x4EC4EC4F, 2, Divide5}, // 13
509 {0x92492493, 3, Divide7}, // 14
510 {0x88888889, 3, Divide7}, // 15
511};
512
513// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700514bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700515 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700516 UNUSED(dalvik_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
518 return false;
519 }
520 DividePattern pattern = magic_table[lit].pattern;
521 if (pattern == DivideNone) {
522 return false;
523 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524
buzbee2700f7e2014-03-07 09:46:20 -0800525 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 LoadConstant(r_magic, magic_table[lit].magic);
527 rl_src = LoadValue(rl_src, kCoreReg);
528 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800529 RegStorage r_hi = AllocTemp();
530 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100531
532 // rl_dest and rl_src might overlap.
533 // Reuse r_hi to save the div result for reminder case.
534 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
535
buzbee2700f7e2014-03-07 09:46:20 -0800536 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700537 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100539 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 break;
541 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800542 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100543 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700544 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 break;
546 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800547 OpRegReg(kOpAdd, r_hi, rl_src.reg);
548 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100549 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700550 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 break;
552 default:
553 LOG(FATAL) << "Unexpected pattern: " << pattern;
554 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100555
556 if (!is_div) {
557 // div_result = src / lit
558 // tmp1 = div_result * lit
559 // dest = src - tmp1
560 RegStorage tmp1 = r_lo;
561 EasyMultiplyOp ops[2];
562
563 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
564 DCHECK_NE(canEasyMultiply, false);
565
566 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
567 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
568 }
569
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 StoreValue(rl_dest, rl_result);
571 return true;
572}
573
Ian Rogerse2143c02014-03-28 08:47:16 -0700574// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
575bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
Andreas Gampecfe71e52015-01-05 19:30:59 -0800576 if (lit == 0) {
577 // Special case for *divide-by-zero*. The ops won't actually be used to generate code, as
578 // GenArithOpIntLit will directly generate exception-throwing code, and multiply-by-zero will
579 // have been optimized away earlier.
580 op->op = kOpInvalid;
Dmitry Petrochenkoddf05aa2015-01-14 15:54:20 +0600581 op->shift = 0;
Andreas Gampecfe71e52015-01-05 19:30:59 -0800582 return true;
583 }
584
Ian Rogerse2143c02014-03-28 08:47:16 -0700585 if (IsPowerOfTwo(lit)) {
586 op->op = kOpLsl;
Andreas Gampe7e499922015-01-06 08:28:12 -0800587 op->shift = CTZ(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700588 return true;
589 }
590
591 if (IsPowerOfTwo(lit - 1)) {
592 op->op = kOpAdd;
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 if (IsPowerOfTwo(lit + 1)) {
598 op->op = kOpRsub;
Andreas Gampe7e499922015-01-06 08:28:12 -0800599 op->shift = CTZ(lit + 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700600 return true;
601 }
602
603 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100604 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700605 return false;
606}
607
608// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
609bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700610 if (GetEasyMultiplyOp(lit, &ops[0])) {
611 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100612 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700613 return true;
614 }
615
616 int lit1 = lit;
Andreas Gampe7e499922015-01-06 08:28:12 -0800617 uint32_t shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700618 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
619 ops[1].op = kOpLsl;
620 ops[1].shift = shift;
621 return true;
622 }
623
624 lit1 = lit - 1;
Andreas Gampe7e499922015-01-06 08:28:12 -0800625 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700626 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
627 ops[1].op = kOpAdd;
628 ops[1].shift = shift;
629 return true;
630 }
631
632 lit1 = lit + 1;
Andreas Gampe7e499922015-01-06 08:28:12 -0800633 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700634 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
635 ops[1].op = kOpRsub;
636 ops[1].shift = shift;
637 return true;
638 }
639
Andrew Hsiehf2674ea2015-03-16 17:03:06 +0800640 ops[1].op = kOpInvalid;
641 ops[1].shift = 0;
642
Ian Rogerse2143c02014-03-28 08:47:16 -0700643 return false;
644}
645
Zheng Xuf9719f92014-04-02 13:31:31 +0100646// Generate instructions to do multiply.
647// Additional temporary register is required,
648// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700649void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Zheng Xuf9719f92014-04-02 13:31:31 +0100650 // tmp1 = ( src << shift1) + [ src | -src | 0 ]
651 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
652
653 RegStorage r_tmp1;
654 if (ops[1].op == kOpInvalid) {
655 r_tmp1 = r_dest;
656 } else if (r_dest.GetReg() != r_src.GetReg()) {
657 r_tmp1 = r_dest;
658 } else {
659 r_tmp1 = AllocTemp();
660 }
661
662 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700663 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100664 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700665 break;
666 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100667 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700668 break;
669 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100670 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700671 break;
672 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100673 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700674 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100675 }
676
677 switch (ops[1].op) {
678 case kOpInvalid:
679 return;
680 case kOpLsl:
681 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
682 break;
683 case kOpAdd:
684 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
685 break;
686 case kOpRsub:
687 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
688 break;
689 default:
690 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
691 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700692 }
693}
694
695bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
696 EasyMultiplyOp ops[2];
697
698 if (!GetEasyMultiplyTwoOps(lit, ops)) {
699 return false;
700 }
701
702 rl_src = LoadValue(rl_src, kCoreReg);
703 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
704
705 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
706 StoreValue(rl_dest, rl_result);
707 return true;
708}
709
Mark Mendell2bf31e62014-01-23 12:13:40 -0800710RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700711 RegLocation rl_src2, bool is_div, int flags) {
712 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800713 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700714 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800715}
716
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700717RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
718 bool is_div) {
719 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800720 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700721 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800722}
723
buzbee2700f7e2014-03-07 09:46:20 -0800724RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700725 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
726
727 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800728 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700729 LoadConstant(lit_temp, lit);
730 // Use the generic case for div/rem with arg2 in a register.
731 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
732 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
733 FreeTemp(lit_temp);
734
735 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736}
737
buzbee2700f7e2014-03-07 09:46:20 -0800738RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700739 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700740 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
741 if (is_div) {
742 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800743 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700744 } else {
745 // Remainder case, use the following code:
746 // temp = reg1 / reg2 - integer division
747 // temp = temp * reg2
748 // dest = reg1 - temp
749
buzbee2700f7e2014-03-07 09:46:20 -0800750 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700751 OpRegRegReg(kOpDiv, temp, reg1, reg2);
752 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800753 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700754 FreeTemp(temp);
755 }
756
757 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758}
759
Serban Constantinescu23abec92014-07-02 16:13:38 +0100760bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100762 if (is_long) {
763 return false;
764 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 RegLocation rl_src1 = info->args[0];
766 RegLocation rl_src2 = info->args[1];
767 rl_src1 = LoadValue(rl_src1, kCoreReg);
768 rl_src2 = LoadValue(rl_src2, kCoreReg);
769 RegLocation rl_dest = InlineTarget(info);
770 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800771 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700772 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800773 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
774 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700775 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 StoreValue(rl_dest, rl_result);
777 return true;
778}
779
Vladimir Markoe508a202013-11-04 15:24:22 +0000780bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
781 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800782 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000783 RegLocation rl_dest = InlineTarget(info);
784 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
785 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700786 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000787 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800788 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700789 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
790 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000791 } else {
buzbee695d13a2014-04-19 13:32:20 -0700792 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
793 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000794 }
795 StoreValueWide(rl_dest, rl_result);
796 } else {
buzbee695d13a2014-04-19 13:32:20 -0700797 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000798 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000799 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000800 StoreValue(rl_dest, rl_result);
801 }
802 return true;
803}
804
805bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
806 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800807 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000808 RegLocation rl_src_value = info->args[2]; // [size] value
809 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700810 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000811 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
812 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000813 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
814 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000815 } else {
buzbee695d13a2014-04-19 13:32:20 -0700816 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000817 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
818 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000819 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000820 }
821 return true;
822}
823
Hans Boehm48f5c472014-06-27 14:50:10 -0700824// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000825bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700826 DCHECK_EQ(cu_->instruction_set, kThumb2);
827 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000828 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
829 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800830 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000831 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000832 // If is_long, high half is in info->args[5]
833 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
834 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 RegLocation rl_dest = InlineTarget(info); // boolean place for result
836
Vladimir Marko3e5af822013-11-21 15:01:20 +0000837 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
838 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
839 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
840 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
841 // into the same temps, reducing the number of required temps down to 5. We shall work
842 // around the potentially locked temp by using LR for r_ptr, unconditionally.
843 // TODO: Pass information about the need for more temps to the stack frame generation
844 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700845 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
846 MarkTemp(rs_rARM_LR);
847 FreeTemp(rs_rARM_LR);
848 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000849 bool load_early = true;
850 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700851 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
852 rl_src_expected.reg;
853 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
854 rl_src_new_value.reg;
855 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
856 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800857 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
858 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000859
860 if (!expected_is_good_reg && !new_value_is_good_reg) {
861 // None of expected/new_value is non-temp reg, need to load both late
862 load_early = false;
863 // Make sure they are not in the temp regs and the load will not be skipped.
864 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800865 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000866 ClobberSReg(rl_src_expected.s_reg_low);
867 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
868 rl_src_expected.location = kLocDalvikFrame;
869 }
870 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800871 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000872 ClobberSReg(rl_src_new_value.s_reg_low);
873 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
874 rl_src_new_value.location = kLocDalvikFrame;
875 }
876 }
877 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878
Hans Boehm48f5c472014-06-27 14:50:10 -0700879 // Prevent reordering with prior memory operations.
880 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881
buzbeea0cd2d72014-06-01 09:33:49 -0700882 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000883 RegLocation rl_new_value;
884 if (!is_long) {
Vladimir Markofac10702015-04-22 11:51:52 +0100885 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000886 } else if (load_early) {
887 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
888 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889
Vladimir Marko1c282e22013-11-21 14:49:47 +0000890 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 // Mark card for object assuming new value is stored.
Vladimir Marko743b98c2014-11-24 19:45:41 +0000892 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700893 }
894
895 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
896
buzbee2700f7e2014-03-07 09:46:20 -0800897 RegStorage r_ptr = rs_rARM_LR;
898 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899
900 // Free now unneeded rl_object and rl_offset to give more temps.
901 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700902 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700904 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905
Vladimir Marko3e5af822013-11-21 15:01:20 +0000906 RegLocation rl_expected;
907 if (!is_long) {
Vladimir Markofac10702015-04-22 11:51:52 +0100908 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000909 } else if (load_early) {
910 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
911 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000912 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700913 RegStorage low_reg = AllocTemp();
914 RegStorage high_reg = AllocTemp();
915 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000916 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000917 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918
Vladimir Marko3e5af822013-11-21 15:01:20 +0000919 // do {
920 // tmp = [r_ptr] - expected;
921 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
922 // result = tmp != 0;
923
buzbee2700f7e2014-03-07 09:46:20 -0800924 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700925 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700926
Dave Allison3da67a52014-04-02 17:03:45 -0700927 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000928 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800929 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000930 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800931 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000932 }
buzbee2700f7e2014-03-07 09:46:20 -0800933 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
934 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
935 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000936 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800937 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000938 }
939 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700940 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800941 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000942 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800943 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000944 }
945 FreeTemp(r_tmp_high); // Now unneeded
946
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100947 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700948 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800949 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 +0000950
951 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800952 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
953 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100954 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700955 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800956 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000957 }
958
959 // Still one conditional left from OpIT(kCondEq, "T") from either branch
960 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700961 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700962
Jeff Hao2de2aa12013-09-12 17:20:31 -0700963 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964
Vladimir Marko3e5af822013-11-21 15:01:20 +0000965 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800966 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000967 }
968
Hans Boehm48f5c472014-06-27 14:50:10 -0700969 // Prevent reordering with subsequent memory operations.
970 GenMemBarrier(kLoadAny);
971
Vladimir Marko3e5af822013-11-21 15:01:20 +0000972 // result := (tmp1 != 0) ? 0 : 1;
973 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800974 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100975 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700976 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800977 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000978 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -0700979 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000980
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 StoreValue(rl_dest, rl_result);
982
Vladimir Marko3e5af822013-11-21 15:01:20 +0000983 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -0700984 Clobber(rs_rARM_LR);
985 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 return true;
987}
988
Zheng Xu947717a2014-08-07 14:05:23 +0800989bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
990 constexpr int kLargeArrayThreshold = 256;
991
992 RegLocation rl_src = info->args[0];
993 RegLocation rl_src_pos = info->args[1];
994 RegLocation rl_dst = info->args[2];
995 RegLocation rl_dst_pos = info->args[3];
996 RegLocation rl_length = info->args[4];
997 // Compile time check, handle exception by non-inline method to reduce related meta-data.
998 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
999 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
1000 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
1001 return false;
1002 }
1003
1004 ClobberCallerSave();
1005 LockCallTemps(); // Prepare for explicit register usage.
1006 LockTemp(rs_r12);
1007 RegStorage rs_src = rs_r0;
1008 RegStorage rs_dst = rs_r1;
1009 LoadValueDirectFixed(rl_src, rs_src);
1010 LoadValueDirectFixed(rl_dst, rs_dst);
1011
1012 // Handle null pointer exception in slow-path.
1013 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
1014 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
1015 // Handle potential overlapping in slow-path.
1016 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
1017 // Handle exception or big length in slow-path.
1018 RegStorage rs_length = rs_r2;
1019 LoadValueDirectFixed(rl_length, rs_length);
1020 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
1021 // Src bounds check.
1022 RegStorage rs_pos = rs_r3;
1023 RegStorage rs_arr_length = rs_r12;
1024 LoadValueDirectFixed(rl_src_pos, rs_pos);
1025 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1026 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1027 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1028 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1029 // Dst bounds check.
1030 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1031 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1032 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1033 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1034 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1035
1036 // Everything is checked now.
1037 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1038 OpRegReg(kOpAdd, rs_dst, rs_pos);
1039 OpRegReg(kOpAdd, rs_dst, rs_pos);
1040 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1041 LoadValueDirectFixed(rl_src_pos, rs_pos);
1042 OpRegReg(kOpAdd, rs_src, rs_pos);
1043 OpRegReg(kOpAdd, rs_src, rs_pos);
1044
1045 RegStorage rs_tmp = rs_pos;
1046 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1047
1048 // Copy one element.
1049 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1050 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1051 OpRegImm(kOpSub, rs_length, 2);
1052 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1053 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1054
1055 // Copy two elements.
1056 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1057 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1058 OpRegImm(kOpSub, rs_length, 4);
1059 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1060 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1061 OpUnconditionalBranch(begin_loop);
1062
1063 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1064 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1065 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1066
1067 src_check_branch->target = check_failed;
1068 dst_check_branch->target = check_failed;
1069 src_dst_same->target = check_failed;
1070 len_neg_or_too_big->target = check_failed;
1071 src_pos_negative->target = check_failed;
1072 src_bad_len->target = check_failed;
1073 dst_pos_negative->target = check_failed;
1074 dst_bad_len->target = check_failed;
1075 jmp_to_begin_loop->target = begin_loop;
1076 jmp_to_ret->target = return_point;
1077
1078 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001079 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001080
1081 return true;
1082}
1083
Vladimir Markof6737f72015-03-23 17:05:14 +00001084void ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Ningsheng Jian335c5552015-02-04 14:13:45 +08001085 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Vladimir Markof6737f72015-03-23 17:05:14 +00001086 LIR* lir = NewLIR2(kThumb2LdrPcRel12, reg.GetReg(), 0);
1087 lir->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088}
1089
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001090bool ArmMir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1091 return dex_cache_arrays_layout_.Valid();
1092}
1093
Vladimir Markocc234812015-04-07 09:36:09 +01001094void ArmMir2Lir::OpPcRelDexCacheArrayAddr(const DexFile* dex_file, int offset, RegStorage r_dest) {
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001095 LIR* movw = NewLIR2(kThumb2MovImm16, r_dest.GetReg(), 0);
1096 LIR* movt = NewLIR2(kThumb2MovImm16H, r_dest.GetReg(), 0);
1097 ArmOpcode add_pc_opcode = (r_dest.GetRegNum() < 8) ? kThumbAddRRLH : kThumbAddRRHH;
1098 LIR* add_pc = NewLIR2(add_pc_opcode, r_dest.GetReg(), rs_rARM_PC.GetReg());
1099 add_pc->flags.fixup = kFixupLabel;
1100 movw->operands[2] = WrapPointer(dex_file);
1101 movw->operands[3] = offset;
1102 movw->operands[4] = WrapPointer(add_pc);
1103 movt->operands[2] = movw->operands[2];
1104 movt->operands[3] = movw->operands[3];
1105 movt->operands[4] = movw->operands[4];
1106 dex_cache_access_insns_.push_back(movw);
1107 dex_cache_access_insns_.push_back(movt);
Vladimir Markocc234812015-04-07 09:36:09 +01001108}
1109
1110void ArmMir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file, int offset, RegStorage r_dest) {
1111 if (dex_cache_arrays_base_reg_.Valid()) {
1112 LoadRefDisp(dex_cache_arrays_base_reg_, offset - dex_cache_arrays_min_offset_,
1113 r_dest, kNotVolatile);
1114 } else {
1115 OpPcRelDexCacheArrayAddr(dex_file, offset, r_dest);
1116 LoadRefDisp(r_dest, 0, r_dest, kNotVolatile);
1117 }
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001118}
1119
buzbee2700f7e2014-03-07 09:46:20 -08001120LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001121 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122}
1123
buzbee2700f7e2014-03-07 09:46:20 -08001124LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001125 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126}
1127
Ningsheng Jiana262f772014-11-25 16:48:07 +08001128void ArmMir2Lir::GenMaddMsubInt(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1129 RegLocation rl_src3, bool is_sub) {
1130 rl_src1 = LoadValue(rl_src1, kCoreReg);
1131 rl_src2 = LoadValue(rl_src2, kCoreReg);
1132 rl_src3 = LoadValue(rl_src3, kCoreReg);
1133 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1134 NewLIR4(is_sub ? kThumb2Mls : kThumb2Mla, rl_result.reg.GetReg(), rl_src1.reg.GetReg(),
1135 rl_src2.reg.GetReg(), rl_src3.reg.GetReg());
1136 StoreValue(rl_dest, rl_result);
1137}
1138
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1140 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001141 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001142 UNUSED(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001143 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 EncodeShift(kArmLsl, second_bit - first_bit));
1145 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001146 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 }
1148}
1149
Mingyao Yange643a172014-04-08 11:02:52 -07001150void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001151 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1152 RegStorage t_reg = AllocTemp();
1153 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001154 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001155 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156}
1157
1158// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001159LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001160#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001161 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001163#else
1164 RegStorage t_reg = AllocTemp();
1165 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
Ian Rogers8ba17f62014-10-27 18:48:49 -07001166 t_reg, kUnsignedHalf, kNotVolatile);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001167 LIR* cmp_branch = OpCmpImmBranch((target == NULL) ? kCondNe : kCondEq, t_reg,
1168 0, target);
1169 FreeTemp(t_reg);
1170 return cmp_branch;
1171#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172}
1173
1174// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001175LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001177 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001178 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 return OpCondBranch(c_code, target);
1180}
1181
Andreas Gampeb14329f2014-05-15 11:16:06 -07001182bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001183 if (!cu_->compiler_driver->GetInstructionSetFeatures()->IsSmp()) {
Elliott Hughes8366ca02014-11-17 12:02:05 -08001184 return false;
1185 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001186 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1187 LIR* barrier = last_lir_insn_;
1188
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 int dmb_flavor;
1190 // TODO: revisit Arm barrier kinds
1191 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001192 case kAnyStore: dmb_flavor = kISH; break;
1193 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001194 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001195 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 default:
1197 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1198 dmb_flavor = kSY; // quiet gcc.
1199 break;
1200 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001201
Andreas Gampeb14329f2014-05-15 11:16:06 -07001202 bool ret = false;
1203
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001204 // If the same barrier already exists, don't generate another.
1205 if (barrier == nullptr
1206 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1207 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001208 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001209 }
1210
1211 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1212 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001213 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001214 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215}
1216
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001217void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 rl_src = LoadValueWide(rl_src, kCoreReg);
1219 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001220 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 LoadConstantNoClobber(z_reg, 0);
1222 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001223 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1224 RegStorage t_reg = AllocTemp();
Vladimir Marko2f340a82014-12-01 16:48:48 +00001225 OpRegCopy(t_reg, rl_result.reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -08001226 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1227 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 FreeTemp(t_reg);
1229 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001230 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1231 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 }
1233 FreeTemp(z_reg);
1234 StoreValueWide(rl_dest, rl_result);
1235}
1236
Mark Mendelle02d48f2014-01-15 11:19:23 -08001237void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1238 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001239 UNUSED(opcode);
1240 /*
1241 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1242 * dest = src1.lo * src2.lo;
1243 * tmp1 += src1.lo * src2.hi;
1244 * dest.hi += tmp1;
1245 *
1246 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1247 * registers. Normally for Arm, we get 5. We can get to 6 by including
1248 * lr in the temp set. The only problematic case is all operands and result are
1249 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1250 * freeing operand temp registers after they are no longer needed. All other cases
1251 * can proceed normally. We'll just punt on the case of the result having a misaligned
1252 * overlap with either operand and send that case to a runtime handler.
1253 */
1254 RegLocation rl_result;
1255 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1256 FlushAllRegs();
1257 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1258 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001259 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001260 return;
1261 }
1262
1263 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1264 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1265
1266 int reg_status = 0;
1267 RegStorage res_lo;
1268 RegStorage res_hi;
1269 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1270 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1271 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1272 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1273 // Check if rl_dest is *not* either operand and we have enough temp registers.
1274 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1275 (dest_promoted || src1_promoted || src2_promoted)) {
1276 // In this case, we do not need to manually allocate temp registers for result.
1277 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1278 res_lo = rl_result.reg.GetLow();
1279 res_hi = rl_result.reg.GetHigh();
1280 } else {
1281 res_lo = AllocTemp();
1282 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1283 // In this case, we have enough temp registers to be allocated for result.
1284 res_hi = AllocTemp();
1285 reg_status = 1;
1286 } else {
1287 // In this case, all temps are now allocated.
1288 // res_hi will be allocated after we can free src1_hi.
1289 reg_status = 2;
1290 }
1291 }
1292
1293 // Temporarily add LR to the temp pool, and assign it to tmp1
1294 MarkTemp(rs_rARM_LR);
1295 FreeTemp(rs_rARM_LR);
1296 RegStorage tmp1 = rs_rARM_LR;
1297 LockTemp(rs_rARM_LR);
1298
1299 if (rl_src1.reg == rl_src2.reg) {
1300 DCHECK(res_hi.Valid());
1301 DCHECK(res_lo.Valid());
1302 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1303 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1304 rl_src1.reg.GetLowReg());
1305 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1306 } else {
1307 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1308 if (reg_status == 2) {
1309 DCHECK(!res_hi.Valid());
1310 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1311 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1312 // Will force free src1_hi, so must clobber.
1313 Clobber(rl_src1.reg);
1314 FreeTemp(rl_src1.reg.GetHigh());
1315 res_hi = AllocTemp();
1316 }
1317 DCHECK(res_hi.Valid());
1318 DCHECK(res_lo.Valid());
1319 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1320 rl_src1.reg.GetLowReg());
1321 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1322 tmp1.GetReg());
1323 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1324 if (reg_status == 2) {
1325 FreeTemp(rl_src1.reg.GetLow());
1326 }
1327 }
1328
1329 // Now, restore lr to its non-temp status.
1330 FreeTemp(tmp1);
1331 Clobber(rs_rARM_LR);
1332 UnmarkTemp(rs_rARM_LR);
1333
1334 if (reg_status != 0) {
1335 // We had manually allocated registers for rl_result.
1336 // Now construct a RegLocation.
1337 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1338 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1339 }
1340
1341 StoreValueWide(rl_dest, rl_result);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342}
1343
Andreas Gampec76c6142014-08-04 16:30:03 -07001344void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001345 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001346 switch (opcode) {
1347 case Instruction::MUL_LONG:
1348 case Instruction::MUL_LONG_2ADDR:
1349 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1350 return;
1351 case Instruction::NEG_LONG:
1352 GenNegLong(rl_dest, rl_src2);
1353 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354
Andreas Gampec76c6142014-08-04 16:30:03 -07001355 default:
1356 break;
1357 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358
Andreas Gampec76c6142014-08-04 16:30:03 -07001359 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001360 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361}
1362
1363/*
1364 * Generate array load
1365 */
1366void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001367 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001368 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001369 int len_offset = mirror::Array::LengthOffset().Int32Value();
1370 int data_offset;
1371 RegLocation rl_result;
1372 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001373 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 if (!constant_index) {
1375 rl_index = LoadValue(rl_index, kCoreReg);
1376 }
1377
1378 if (rl_dest.wide) {
1379 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1380 } else {
1381 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1382 }
1383
1384 // If index is constant, just fold it into the data offset
1385 if (constant_index) {
1386 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1387 }
1388
1389 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001390 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391
1392 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001393 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 if (needs_range_check) {
1395 reg_len = AllocTemp();
1396 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001397 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001398 MarkPossibleNullPointerException(opt_flags);
1399 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001400 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 }
1402 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001403 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001405 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001406 } else {
1407 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001408 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001409 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001410 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001411 }
1412 rl_result = EvalLoc(rl_dest, reg_class, true);
1413
1414 if (needs_range_check) {
1415 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001416 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001418 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 }
1420 FreeTemp(reg_len);
1421 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001422 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001423 if (!constant_index) {
1424 FreeTemp(reg_ptr);
1425 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 StoreValueWide(rl_dest, rl_result);
1428 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 StoreValue(rl_dest, rl_result);
1430 }
1431 } else {
1432 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001433 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001434 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001435 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 rl_result = EvalLoc(rl_dest, reg_class, true);
1437
1438 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001439 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 FreeTemp(reg_len);
1441 }
buzbee2700f7e2014-03-07 09:46:20 -08001442 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001443 FreeTemp(reg_ptr);
1444 StoreValue(rl_dest, rl_result);
1445 }
1446}
1447
1448/*
1449 * Generate array store
1450 *
1451 */
1452void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001453 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001454 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001455 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001456 bool constant_index = rl_index.is_const;
1457
Ian Rogersa9a82542013-10-04 11:17:26 -07001458 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001459 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1461 } else {
1462 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1463 }
1464
1465 // If index is constant, just fold it into the data offset.
1466 if (constant_index) {
1467 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1468 }
1469
buzbeea0cd2d72014-06-01 09:33:49 -07001470 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001471 if (!constant_index) {
1472 rl_index = LoadValue(rl_index, kCoreReg);
1473 }
1474
buzbee2700f7e2014-03-07 09:46:20 -08001475 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001476 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001478 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001479 } else if (IsTemp(rl_array.reg) && !card_mark) {
1480 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001481 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001482 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001483 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001484 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 }
1486
1487 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001488 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001489
1490 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001491 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492 if (needs_range_check) {
1493 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001494 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001495 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001496 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001497 MarkPossibleNullPointerException(opt_flags);
1498 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001499 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 }
1501 /* at this point, reg_ptr points to array, 2 live temps */
1502 if (rl_src.wide || rl_src.fp || constant_index) {
1503 if (rl_src.wide) {
1504 rl_src = LoadValueWide(rl_src, reg_class);
1505 } else {
1506 rl_src = LoadValue(rl_src, reg_class);
1507 }
1508 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001509 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 }
1511 if (needs_range_check) {
1512 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001513 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001515 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001516 }
1517 FreeTemp(reg_len);
1518 }
1519
Andreas Gampe3c12c512014-06-24 18:46:29 +00001520 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001521 } else {
1522 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001523 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 rl_src = LoadValue(rl_src, reg_class);
1525 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001526 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 FreeTemp(reg_len);
1528 }
buzbee2700f7e2014-03-07 09:46:20 -08001529 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 }
Ian Rogers773aab12013-10-14 13:50:10 -07001531 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 FreeTemp(reg_ptr);
1533 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001534 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001535 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001536 }
1537}
1538
Ian Rogersa9a82542013-10-04 11:17:26 -07001539
Brian Carlstrom7940e442013-07-12 13:46:57 -07001540void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001541 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1542 int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001543 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 rl_src = LoadValueWide(rl_src, kCoreReg);
1545 // Per spec, we only care about low 6 bits of shift amount.
1546 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1547 if (shift_amount == 0) {
1548 StoreValueWide(rl_dest, rl_src);
1549 return;
1550 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001551 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1553 return;
1554 }
1555 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001556 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 case Instruction::SHL_LONG:
1558 case Instruction::SHL_LONG_2ADDR:
1559 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001560 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1561 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001563 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1564 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001566 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1567 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001568 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001569 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001570 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001571 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001572 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001573 }
1574 break;
1575 case Instruction::SHR_LONG:
1576 case Instruction::SHR_LONG_2ADDR:
1577 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001578 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1579 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001580 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001581 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1582 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001583 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001584 RegStorage t_reg = AllocTemp();
1585 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001586 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 EncodeShift(kArmLsl, 32 - shift_amount));
1588 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001589 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 }
1591 break;
1592 case Instruction::USHR_LONG:
1593 case Instruction::USHR_LONG_2ADDR:
1594 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001595 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1596 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001597 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001598 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1599 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001600 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001601 RegStorage t_reg = AllocTemp();
1602 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001603 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001604 EncodeShift(kArmLsl, 32 - shift_amount));
1605 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001606 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001607 }
1608 break;
1609 default:
1610 LOG(FATAL) << "Unexpected case";
1611 }
1612 StoreValueWide(rl_dest, rl_result);
1613}
1614
1615void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001616 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1617 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001618 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1619 if (!rl_src2.is_const) {
1620 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001621 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 return;
1623 }
1624 } else {
1625 // Normalize
1626 if (!rl_src2.is_const) {
1627 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001628 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 }
1630 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001631 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001632 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001633 return;
1634 }
1635 DCHECK(rl_src2.is_const);
1636 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1637 uint32_t val_lo = Low32Bits(val);
1638 uint32_t val_hi = High32Bits(val);
1639 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1640 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1641
1642 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001643 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 case Instruction::ADD_LONG:
1645 case Instruction::ADD_LONG_2ADDR:
1646 case Instruction::SUB_LONG:
1647 case Instruction::SUB_LONG_2ADDR:
1648 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001649 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001650 return;
1651 }
1652 break;
1653 default:
1654 break;
1655 }
1656 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1657 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1658 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1659 switch (opcode) {
1660 case Instruction::ADD_LONG:
1661 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001662 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001663 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664 break;
1665 case Instruction::OR_LONG:
1666 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001667 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1668 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001670 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001671 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 }
1673 break;
1674 case Instruction::XOR_LONG:
1675 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001676 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1677 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001678 break;
1679 case Instruction::AND_LONG:
1680 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001681 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1682 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001683 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001684 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001685 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 }
1687 break;
1688 case Instruction::SUB_LONG_2ADDR:
1689 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001690 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001691 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001692 break;
1693 default:
1694 LOG(FATAL) << "Unexpected opcode " << opcode;
1695 }
1696 StoreValueWide(rl_dest, rl_result);
1697}
1698
Andreas Gamped500b532015-01-16 22:09:55 -08001699bool ArmMir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
1700 RegLocation rl_src, RegLocation rl_dest, int lit) {
1701 if (lit < 2) {
1702 return false;
1703 }
1704
1705 // ARM does either not support a division instruction, or it is potentially expensive. Look for
1706 // more special cases.
1707 if (!IsPowerOfTwo(lit)) {
1708 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1709 }
1710
1711 return Mir2Lir::HandleEasyDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1712}
1713
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714} // namespace art