blob: db76cc6f53d678c3acf2a9adcf42ccdd70a52302 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080024#include "base/logging.h"
25#include "dex/compiler_ir.h"
26#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070028#include "dex/reg_storage_eq.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080029#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070030#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.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());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700141 LIR* branch1 = OpCondBranch(kCondLt, nullptr);
142 LIR* branch2 = OpCondBranch(kCondGt, nullptr);
buzbeea1983d42014-04-07 12:35:39 -0700143 OpRegRegReg(kOpSub, t_reg, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700144 LIR* branch3 = OpCondBranch(kCondEq, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145
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 */
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700392 bool skip = ((target != nullptr) && (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
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100474 if (r_src.GetHighReg() != r_dest.GetLowReg()) {
475 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
476 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
477 } else if (r_src.GetLowReg() != r_dest.GetHighReg()) {
buzbee7a11ab02014-04-28 20:02:38 -0700478 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
479 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
480 } else {
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100481 RegStorage r_tmp = AllocTemp();
482 OpRegCopy(r_tmp, r_src.GetHigh());
buzbee7a11ab02014-04-28 20:02:38 -0700483 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100484 OpRegCopy(r_dest.GetHigh(), r_tmp);
485 FreeTemp(r_tmp);
buzbee7a11ab02014-04-28 20:02:38 -0700486 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 }
488 }
489 }
490}
491
492// Table of magic divisors
493struct MagicTable {
494 uint32_t magic;
495 uint32_t shift;
496 DividePattern pattern;
497};
498
499static const MagicTable magic_table[] = {
500 {0, 0, DivideNone}, // 0
501 {0, 0, DivideNone}, // 1
502 {0, 0, DivideNone}, // 2
503 {0x55555556, 0, Divide3}, // 3
504 {0, 0, DivideNone}, // 4
505 {0x66666667, 1, Divide5}, // 5
506 {0x2AAAAAAB, 0, Divide3}, // 6
507 {0x92492493, 2, Divide7}, // 7
508 {0, 0, DivideNone}, // 8
509 {0x38E38E39, 1, Divide5}, // 9
510 {0x66666667, 2, Divide5}, // 10
511 {0x2E8BA2E9, 1, Divide5}, // 11
512 {0x2AAAAAAB, 1, Divide5}, // 12
513 {0x4EC4EC4F, 2, Divide5}, // 13
514 {0x92492493, 3, Divide7}, // 14
515 {0x88888889, 3, Divide7}, // 15
516};
517
518// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
buzbee11b63d12013-08-27 07:34:17 -0700519bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700520 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700521 UNUSED(dalvik_opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
523 return false;
524 }
525 DividePattern pattern = magic_table[lit].pattern;
526 if (pattern == DivideNone) {
527 return false;
528 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529
buzbee2700f7e2014-03-07 09:46:20 -0800530 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 LoadConstant(r_magic, magic_table[lit].magic);
532 rl_src = LoadValue(rl_src, kCoreReg);
533 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800534 RegStorage r_hi = AllocTemp();
535 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100536
537 // rl_dest and rl_src might overlap.
538 // Reuse r_hi to save the div result for reminder case.
539 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
540
buzbee2700f7e2014-03-07 09:46:20 -0800541 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700542 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100544 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 break;
546 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800547 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100548 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700549 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 break;
551 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800552 OpRegReg(kOpAdd, r_hi, rl_src.reg);
553 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100554 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700555 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 break;
557 default:
558 LOG(FATAL) << "Unexpected pattern: " << pattern;
559 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100560
561 if (!is_div) {
562 // div_result = src / lit
563 // tmp1 = div_result * lit
564 // dest = src - tmp1
565 RegStorage tmp1 = r_lo;
566 EasyMultiplyOp ops[2];
567
568 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
569 DCHECK_NE(canEasyMultiply, false);
570
571 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
572 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
573 }
574
Brian Carlstrom7940e442013-07-12 13:46:57 -0700575 StoreValue(rl_dest, rl_result);
576 return true;
577}
578
Ian Rogerse2143c02014-03-28 08:47:16 -0700579// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
580bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
Andreas Gampecfe71e52015-01-05 19:30:59 -0800581 if (lit == 0) {
582 // Special case for *divide-by-zero*. The ops won't actually be used to generate code, as
583 // GenArithOpIntLit will directly generate exception-throwing code, and multiply-by-zero will
584 // have been optimized away earlier.
585 op->op = kOpInvalid;
Dmitry Petrochenkoddf05aa2015-01-14 15:54:20 +0600586 op->shift = 0;
Andreas Gampecfe71e52015-01-05 19:30:59 -0800587 return true;
588 }
589
Ian Rogerse2143c02014-03-28 08:47:16 -0700590 if (IsPowerOfTwo(lit)) {
591 op->op = kOpLsl;
Andreas Gampe7e499922015-01-06 08:28:12 -0800592 op->shift = CTZ(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700593 return true;
594 }
595
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000596 // At this point lit != 1 (which is a power of two).
597 DCHECK_NE(lit, 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700598 if (IsPowerOfTwo(lit - 1)) {
599 op->op = kOpAdd;
Andreas Gampe7e499922015-01-06 08:28:12 -0800600 op->shift = CTZ(lit - 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700601 return true;
602 }
603
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000604 if (lit == -1) {
605 // Can be created as neg.
606 op->op = kOpNeg;
607 op->shift = 0;
608 return true;
609 } else if (IsPowerOfTwo(lit + 1)) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700610 op->op = kOpRsub;
Andreas Gampe7e499922015-01-06 08:28:12 -0800611 op->shift = CTZ(lit + 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700612 return true;
613 }
614
615 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100616 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700617 return false;
618}
619
620// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
621bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000622 DCHECK_NE(lit, 1); // A case of "1" should have been folded.
623 DCHECK_NE(lit, -1); // A case of "-1" should have been folded.
Ian Rogerse2143c02014-03-28 08:47:16 -0700624 if (GetEasyMultiplyOp(lit, &ops[0])) {
625 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100626 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700627 return true;
628 }
629
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000630 DCHECK_NE(lit, 0); // Should be handled above.
631 DCHECK(!IsPowerOfTwo(lit)); // Same.
632
633 int lit1 = lit; // With the DCHECKs, it's clear we don't get "0", "1" or "-1" for
634 uint32_t shift = CTZ(lit1); // lit1.
Ian Rogerse2143c02014-03-28 08:47:16 -0700635 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
636 ops[1].op = kOpLsl;
637 ops[1].shift = shift;
638 return true;
639 }
640
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000641 lit1 = lit - 1; // With the DCHECKs, it's clear we don't get "0" or "1" for lit1.
Andreas Gampe7e499922015-01-06 08:28:12 -0800642 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700643 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
644 ops[1].op = kOpAdd;
645 ops[1].shift = shift;
646 return true;
647 }
648
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000649 lit1 = lit + 1; // With the DCHECKs, it's clear we don't get "0" here.
Andreas Gampe7e499922015-01-06 08:28:12 -0800650 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700651 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
652 ops[1].op = kOpRsub;
653 ops[1].shift = shift;
654 return true;
655 }
656
Andrew Hsiehf2674ea2015-03-16 17:03:06 +0800657 ops[1].op = kOpInvalid;
658 ops[1].shift = 0;
659
Ian Rogerse2143c02014-03-28 08:47:16 -0700660 return false;
661}
662
Zheng Xuf9719f92014-04-02 13:31:31 +0100663// Generate instructions to do multiply.
664// Additional temporary register is required,
665// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700666void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000667 // tmp1 = (( src << shift1) + [ src | -src | 0 ] ) | -src
Zheng Xuf9719f92014-04-02 13:31:31 +0100668 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
669
670 RegStorage r_tmp1;
671 if (ops[1].op == kOpInvalid) {
672 r_tmp1 = r_dest;
673 } else if (r_dest.GetReg() != r_src.GetReg()) {
674 r_tmp1 = r_dest;
675 } else {
676 r_tmp1 = AllocTemp();
677 }
678
679 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700680 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100681 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700682 break;
683 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100684 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700685 break;
686 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100687 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700688 break;
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000689 case kOpNeg:
690 OpRegReg(kOpNeg, r_tmp1, r_src);
691 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700692 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100693 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700694 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100695 }
696
697 switch (ops[1].op) {
698 case kOpInvalid:
699 return;
700 case kOpLsl:
701 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
702 break;
703 case kOpAdd:
704 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
705 break;
706 case kOpRsub:
707 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
708 break;
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000709 // No negation allowed in second op.
Zheng Xuf9719f92014-04-02 13:31:31 +0100710 default:
711 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
712 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700713 }
714}
715
716bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
717 EasyMultiplyOp ops[2];
718
719 if (!GetEasyMultiplyTwoOps(lit, ops)) {
720 return false;
721 }
722
723 rl_src = LoadValue(rl_src, kCoreReg);
724 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
725
726 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
727 StoreValue(rl_dest, rl_result);
728 return true;
729}
730
Mark Mendell2bf31e62014-01-23 12:13:40 -0800731RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700732 RegLocation rl_src2, bool is_div, int flags) {
733 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800734 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700735 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800736}
737
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700738RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
739 bool is_div) {
740 UNUSED(rl_dest, rl_src1, lit, is_div);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800741 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700742 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800743}
744
buzbee2700f7e2014-03-07 09:46:20 -0800745RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700746 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
747
748 // Put the literal in a temp.
buzbee2700f7e2014-03-07 09:46:20 -0800749 RegStorage lit_temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700750 LoadConstant(lit_temp, lit);
751 // Use the generic case for div/rem with arg2 in a register.
752 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
753 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
754 FreeTemp(lit_temp);
755
756 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757}
758
buzbee2700f7e2014-03-07 09:46:20 -0800759RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest, RegStorage reg1, RegStorage reg2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700760 bool is_div) {
Dave Allison70202782013-10-22 17:52:19 -0700761 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
762 if (is_div) {
763 // Simple case, use sdiv instruction.
buzbee2700f7e2014-03-07 09:46:20 -0800764 OpRegRegReg(kOpDiv, rl_result.reg, reg1, reg2);
Dave Allison70202782013-10-22 17:52:19 -0700765 } else {
766 // Remainder case, use the following code:
767 // temp = reg1 / reg2 - integer division
768 // temp = temp * reg2
769 // dest = reg1 - temp
770
buzbee2700f7e2014-03-07 09:46:20 -0800771 RegStorage temp = AllocTemp();
Dave Allison70202782013-10-22 17:52:19 -0700772 OpRegRegReg(kOpDiv, temp, reg1, reg2);
773 OpRegReg(kOpMul, temp, reg2);
buzbee2700f7e2014-03-07 09:46:20 -0800774 OpRegRegReg(kOpSub, rl_result.reg, reg1, temp);
Dave Allison70202782013-10-22 17:52:19 -0700775 FreeTemp(temp);
776 }
777
778 return rl_result;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779}
780
Serban Constantinescu23abec92014-07-02 16:13:38 +0100781bool ArmMir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782 DCHECK_EQ(cu_->instruction_set, kThumb2);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100783 if (is_long) {
784 return false;
785 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 RegLocation rl_src1 = info->args[0];
787 RegLocation rl_src2 = info->args[1];
788 rl_src1 = LoadValue(rl_src1, kCoreReg);
789 rl_src2 = LoadValue(rl_src2, kCoreReg);
790 RegLocation rl_dest = InlineTarget(info);
791 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800792 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700793 LIR* it = OpIT((is_min) ? kCondGt : kCondLt, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800794 OpRegReg(kOpMov, rl_result.reg, rl_src2.reg);
795 OpRegReg(kOpMov, rl_result.reg, rl_src1.reg);
Dave Allison3da67a52014-04-02 17:03:45 -0700796 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 StoreValue(rl_dest, rl_result);
798 return true;
799}
800
Vladimir Markoe508a202013-11-04 15:24:22 +0000801bool ArmMir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
802 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800803 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000804 RegLocation rl_dest = InlineTarget(info);
805 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
806 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700807 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000808 // Fake unaligned LDRD by two unaligned LDR instructions on ARMv7 with SCTLR.A set to 0.
buzbee2700f7e2014-03-07 09:46:20 -0800809 if (rl_address.reg.GetReg() != rl_result.reg.GetLowReg()) {
buzbee695d13a2014-04-19 13:32:20 -0700810 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
811 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
Vladimir Markoe508a202013-11-04 15:24:22 +0000812 } else {
buzbee695d13a2014-04-19 13:32:20 -0700813 Load32Disp(rl_address.reg, 4, rl_result.reg.GetHigh());
814 Load32Disp(rl_address.reg, 0, rl_result.reg.GetLow());
Vladimir Markoe508a202013-11-04 15:24:22 +0000815 }
816 StoreValueWide(rl_dest, rl_result);
817 } else {
buzbee695d13a2014-04-19 13:32:20 -0700818 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000819 // Unaligned load with LDR and LDRSH is allowed on ARMv7 with SCTLR.A set to 0.
Andreas Gampe3c12c512014-06-24 18:46:29 +0000820 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000821 StoreValue(rl_dest, rl_result);
822 }
823 return true;
824}
825
826bool ArmMir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
827 RegLocation rl_src_address = info->args[0]; // long address
buzbee2700f7e2014-03-07 09:46:20 -0800828 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
Vladimir Markoe508a202013-11-04 15:24:22 +0000829 RegLocation rl_src_value = info->args[2]; // [size] value
830 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
buzbee695d13a2014-04-19 13:32:20 -0700831 if (size == k64) {
Vladimir Markoe508a202013-11-04 15:24:22 +0000832 // Fake unaligned STRD by two unaligned STR instructions on ARMv7 with SCTLR.A set to 0.
833 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000834 StoreBaseDisp(rl_address.reg, 0, rl_value.reg.GetLow(), k32, kNotVolatile);
835 StoreBaseDisp(rl_address.reg, 4, rl_value.reg.GetHigh(), k32, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000836 } else {
buzbee695d13a2014-04-19 13:32:20 -0700837 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Vladimir Markoe508a202013-11-04 15:24:22 +0000838 // Unaligned store with STR and STRSH is allowed on ARMv7 with SCTLR.A set to 0.
839 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000840 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Vladimir Markoe508a202013-11-04 15:24:22 +0000841 }
842 return true;
843}
844
Hans Boehm48f5c472014-06-27 14:50:10 -0700845// Generate a CAS with memory_order_seq_cst semantics.
Vladimir Marko1c282e22013-11-21 14:49:47 +0000846bool ArmMir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847 DCHECK_EQ(cu_->instruction_set, kThumb2);
848 // Unused - RegLocation rl_src_unsafe = info->args[0];
Vladimir Marko1c282e22013-11-21 14:49:47 +0000849 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
850 RegLocation rl_src_offset = info->args[2]; // long low
buzbee2700f7e2014-03-07 09:46:20 -0800851 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3]
Vladimir Marko1c282e22013-11-21 14:49:47 +0000852 RegLocation rl_src_expected = info->args[4]; // int, long or Object
Vladimir Marko3e5af822013-11-21 15:01:20 +0000853 // If is_long, high half is in info->args[5]
854 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
855 // If is_long, high half is in info->args[7]
Brian Carlstrom7940e442013-07-12 13:46:57 -0700856 RegLocation rl_dest = InlineTarget(info); // boolean place for result
857
Vladimir Marko3e5af822013-11-21 15:01:20 +0000858 // We have only 5 temporary registers available and actually only 4 if the InlineTarget
859 // above locked one of the temps. For a straightforward CAS64 we need 7 registers:
860 // r_ptr (1), new_value (2), expected(2) and ldrexd result (2). If neither expected nor
861 // new_value is in a non-temp core register we shall reload them in the ldrex/strex loop
862 // into the same temps, reducing the number of required temps down to 5. We shall work
863 // around the potentially locked temp by using LR for r_ptr, unconditionally.
864 // TODO: Pass information about the need for more temps to the stack frame generation
865 // code so that we can rely on being able to allocate enough temps.
buzbee091cc402014-03-31 10:14:40 -0700866 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
867 MarkTemp(rs_rARM_LR);
868 FreeTemp(rs_rARM_LR);
869 LockTemp(rs_rARM_LR);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000870 bool load_early = true;
871 if (is_long) {
buzbee091cc402014-03-31 10:14:40 -0700872 RegStorage expected_reg = rl_src_expected.reg.IsPair() ? rl_src_expected.reg.GetLow() :
873 rl_src_expected.reg;
874 RegStorage new_val_reg = rl_src_new_value.reg.IsPair() ? rl_src_new_value.reg.GetLow() :
875 rl_src_new_value.reg;
876 bool expected_is_core_reg = rl_src_expected.location == kLocPhysReg && !expected_reg.IsFloat();
877 bool new_value_is_core_reg = rl_src_new_value.location == kLocPhysReg && !new_val_reg.IsFloat();
buzbee2700f7e2014-03-07 09:46:20 -0800878 bool expected_is_good_reg = expected_is_core_reg && !IsTemp(expected_reg);
879 bool new_value_is_good_reg = new_value_is_core_reg && !IsTemp(new_val_reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000880
881 if (!expected_is_good_reg && !new_value_is_good_reg) {
882 // None of expected/new_value is non-temp reg, need to load both late
883 load_early = false;
884 // Make sure they are not in the temp regs and the load will not be skipped.
885 if (expected_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800886 FlushRegWide(rl_src_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000887 ClobberSReg(rl_src_expected.s_reg_low);
888 ClobberSReg(GetSRegHi(rl_src_expected.s_reg_low));
889 rl_src_expected.location = kLocDalvikFrame;
890 }
891 if (new_value_is_core_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800892 FlushRegWide(rl_src_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000893 ClobberSReg(rl_src_new_value.s_reg_low);
894 ClobberSReg(GetSRegHi(rl_src_new_value.s_reg_low));
895 rl_src_new_value.location = kLocDalvikFrame;
896 }
897 }
898 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899
Hans Boehm48f5c472014-06-27 14:50:10 -0700900 // Prevent reordering with prior memory operations.
901 GenMemBarrier(kAnyStore);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902
buzbeea0cd2d72014-06-01 09:33:49 -0700903 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000904 RegLocation rl_new_value;
905 if (!is_long) {
Vladimir Markofac10702015-04-22 11:51:52 +0100906 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000907 } else if (load_early) {
908 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
909 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910
Vladimir Marko1c282e22013-11-21 14:49:47 +0000911 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 // Mark card for object assuming new value is stored.
Vladimir Marko743b98c2014-11-24 19:45:41 +0000913 MarkGCCard(0, rl_new_value.reg, rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 }
915
916 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
917
buzbee2700f7e2014-03-07 09:46:20 -0800918 RegStorage r_ptr = rs_rARM_LR;
919 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920
921 // Free now unneeded rl_object and rl_offset to give more temps.
922 ClobberSReg(rl_object.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700923 FreeTemp(rl_object.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924 ClobberSReg(rl_offset.s_reg_low);
buzbee091cc402014-03-31 10:14:40 -0700925 FreeTemp(rl_offset.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926
Vladimir Marko3e5af822013-11-21 15:01:20 +0000927 RegLocation rl_expected;
928 if (!is_long) {
Vladimir Markofac10702015-04-22 11:51:52 +0100929 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000930 } else if (load_early) {
931 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
932 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000933 // NOTE: partially defined rl_expected & rl_new_value - but we just want the regs.
buzbee091cc402014-03-31 10:14:40 -0700934 RegStorage low_reg = AllocTemp();
935 RegStorage high_reg = AllocTemp();
936 rl_new_value.reg = RegStorage::MakeRegPair(low_reg, high_reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000937 rl_expected = rl_new_value;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000938 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939
Vladimir Marko3e5af822013-11-21 15:01:20 +0000940 // do {
941 // tmp = [r_ptr] - expected;
942 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
943 // result = tmp != 0;
944
buzbee2700f7e2014-03-07 09:46:20 -0800945 RegStorage r_tmp = AllocTemp();
Jeff Hao2de2aa12013-09-12 17:20:31 -0700946 LIR* target = NewLIR0(kPseudoTargetLabel);
Jeff Hao2de2aa12013-09-12 17:20:31 -0700947
Dave Allison3da67a52014-04-02 17:03:45 -0700948 LIR* it = nullptr;
Vladimir Marko3e5af822013-11-21 15:01:20 +0000949 if (is_long) {
buzbee2700f7e2014-03-07 09:46:20 -0800950 RegStorage r_tmp_high = AllocTemp();
Vladimir Marko3e5af822013-11-21 15:01:20 +0000951 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800952 LoadValueDirectWide(rl_src_expected, rl_expected.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000953 }
buzbee2700f7e2014-03-07 09:46:20 -0800954 NewLIR3(kThumb2Ldrexd, r_tmp.GetReg(), r_tmp_high.GetReg(), r_ptr.GetReg());
955 OpRegReg(kOpSub, r_tmp, rl_expected.reg.GetLow());
956 OpRegReg(kOpSub, r_tmp_high, rl_expected.reg.GetHigh());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000957 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800958 LoadValueDirectWide(rl_src_new_value, rl_new_value.reg);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000959 }
960 // Make sure we use ORR that sets the ccode
buzbee091cc402014-03-31 10:14:40 -0700961 if (r_tmp.Low8() && r_tmp_high.Low8()) {
buzbee2700f7e2014-03-07 09:46:20 -0800962 NewLIR2(kThumbOrr, r_tmp.GetReg(), r_tmp_high.GetReg());
Vladimir Marko3e5af822013-11-21 15:01:20 +0000963 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800964 NewLIR4(kThumb2OrrRRRs, r_tmp.GetReg(), r_tmp.GetReg(), r_tmp_high.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000965 }
966 FreeTemp(r_tmp_high); // Now unneeded
967
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100968 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700969 it = OpIT(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800970 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 +0000971
972 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800973 NewLIR3(kThumb2Ldrex, r_tmp.GetReg(), r_ptr.GetReg(), 0);
974 OpRegReg(kOpSub, r_tmp, rl_expected.reg);
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(kCondEq, "T");
buzbee2700f7e2014-03-07 09:46:20 -0800977 NewLIR4(kThumb2Strex /* eq */, r_tmp.GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg(), 0);
Vladimir Marko3e5af822013-11-21 15:01:20 +0000978 }
979
980 // Still one conditional left from OpIT(kCondEq, "T") from either branch
981 OpRegImm(kOpCmp /* eq */, r_tmp, 1);
Dave Allison3da67a52014-04-02 17:03:45 -0700982 OpEndIT(it);
Dave Allison43a065c2014-04-01 15:14:46 -0700983
Jeff Hao2de2aa12013-09-12 17:20:31 -0700984 OpCondBranch(kCondEq, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985
Vladimir Marko3e5af822013-11-21 15:01:20 +0000986 if (!load_early) {
buzbee2700f7e2014-03-07 09:46:20 -0800987 FreeTemp(rl_expected.reg); // Now unneeded.
Vladimir Marko3e5af822013-11-21 15:01:20 +0000988 }
989
Hans Boehm48f5c472014-06-27 14:50:10 -0700990 // Prevent reordering with subsequent memory operations.
991 GenMemBarrier(kLoadAny);
992
Vladimir Marko3e5af822013-11-21 15:01:20 +0000993 // result := (tmp1 != 0) ? 0 : 1;
994 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800995 OpRegRegImm(kOpRsub, rl_result.reg, r_tmp, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100996 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700997 it = OpIT(kCondUlt, "");
buzbee2700f7e2014-03-07 09:46:20 -0800998 LoadConstant(rl_result.reg, 0); /* cc */
Vladimir Marko3e5af822013-11-21 15:01:20 +0000999 FreeTemp(r_tmp); // Now unneeded.
Dave Allison3da67a52014-04-02 17:03:45 -07001000 OpEndIT(it); // Barrier to terminate OpIT.
Vladimir Marko3e5af822013-11-21 15:01:20 +00001001
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 StoreValue(rl_dest, rl_result);
1003
Vladimir Marko3e5af822013-11-21 15:01:20 +00001004 // Now, restore lr to its non-temp status.
buzbee091cc402014-03-31 10:14:40 -07001005 Clobber(rs_rARM_LR);
1006 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 return true;
1008}
1009
Zheng Xu947717a2014-08-07 14:05:23 +08001010bool ArmMir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
1011 constexpr int kLargeArrayThreshold = 256;
1012
1013 RegLocation rl_src = info->args[0];
1014 RegLocation rl_src_pos = info->args[1];
1015 RegLocation rl_dst = info->args[2];
1016 RegLocation rl_dst_pos = info->args[3];
1017 RegLocation rl_length = info->args[4];
1018 // Compile time check, handle exception by non-inline method to reduce related meta-data.
1019 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
1020 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
1021 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
1022 return false;
1023 }
1024
1025 ClobberCallerSave();
1026 LockCallTemps(); // Prepare for explicit register usage.
1027 LockTemp(rs_r12);
1028 RegStorage rs_src = rs_r0;
1029 RegStorage rs_dst = rs_r1;
1030 LoadValueDirectFixed(rl_src, rs_src);
1031 LoadValueDirectFixed(rl_dst, rs_dst);
1032
1033 // Handle null pointer exception in slow-path.
1034 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
1035 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
1036 // Handle potential overlapping in slow-path.
1037 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
1038 // Handle exception or big length in slow-path.
1039 RegStorage rs_length = rs_r2;
1040 LoadValueDirectFixed(rl_length, rs_length);
1041 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
1042 // Src bounds check.
1043 RegStorage rs_pos = rs_r3;
1044 RegStorage rs_arr_length = rs_r12;
1045 LoadValueDirectFixed(rl_src_pos, rs_pos);
1046 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1047 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1048 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1049 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1050 // Dst bounds check.
1051 LoadValueDirectFixed(rl_dst_pos, rs_pos);
1052 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_pos, 0, nullptr);
1053 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
1054 OpRegReg(kOpSub, rs_arr_length, rs_pos);
1055 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
1056
1057 // Everything is checked now.
1058 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
1059 OpRegReg(kOpAdd, rs_dst, rs_pos);
1060 OpRegReg(kOpAdd, rs_dst, rs_pos);
1061 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
1062 LoadValueDirectFixed(rl_src_pos, rs_pos);
1063 OpRegReg(kOpAdd, rs_src, rs_pos);
1064 OpRegReg(kOpAdd, rs_src, rs_pos);
1065
1066 RegStorage rs_tmp = rs_pos;
1067 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
1068
1069 // Copy one element.
1070 OpRegRegImm(kOpAnd, rs_tmp, rs_length, 2);
1071 LIR* jmp_to_begin_loop = OpCmpImmBranch(kCondEq, rs_tmp, 0, nullptr);
1072 OpRegImm(kOpSub, rs_length, 2);
1073 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
1074 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
1075
1076 // Copy two elements.
1077 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
1078 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
1079 OpRegImm(kOpSub, rs_length, 4);
1080 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
1081 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
1082 OpUnconditionalBranch(begin_loop);
1083
1084 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
1085 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
1086 LIR* return_point = NewLIR0(kPseudoTargetLabel);
1087
1088 src_check_branch->target = check_failed;
1089 dst_check_branch->target = check_failed;
1090 src_dst_same->target = check_failed;
1091 len_neg_or_too_big->target = check_failed;
1092 src_pos_negative->target = check_failed;
1093 src_bad_len->target = check_failed;
1094 dst_pos_negative->target = check_failed;
1095 dst_bad_len->target = check_failed;
1096 jmp_to_begin_loop->target = begin_loop;
1097 jmp_to_ret->target = return_point;
1098
1099 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +07001100 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +08001101
1102 return true;
1103}
1104
Vladimir Markof6737f72015-03-23 17:05:14 +00001105void ArmMir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Ningsheng Jian335c5552015-02-04 14:13:45 +08001106 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Vladimir Markof6737f72015-03-23 17:05:14 +00001107 LIR* lir = NewLIR2(kThumb2LdrPcRel12, reg.GetReg(), 0);
1108 lir->target = target;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109}
1110
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001111bool ArmMir2Lir::CanUseOpPcRelDexCacheArrayLoad() const {
1112 return dex_cache_arrays_layout_.Valid();
1113}
1114
Vladimir Markocc234812015-04-07 09:36:09 +01001115void ArmMir2Lir::OpPcRelDexCacheArrayAddr(const DexFile* dex_file, int offset, RegStorage r_dest) {
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001116 LIR* movw = NewLIR2(kThumb2MovImm16, r_dest.GetReg(), 0);
1117 LIR* movt = NewLIR2(kThumb2MovImm16H, r_dest.GetReg(), 0);
1118 ArmOpcode add_pc_opcode = (r_dest.GetRegNum() < 8) ? kThumbAddRRLH : kThumbAddRRHH;
1119 LIR* add_pc = NewLIR2(add_pc_opcode, r_dest.GetReg(), rs_rARM_PC.GetReg());
1120 add_pc->flags.fixup = kFixupLabel;
1121 movw->operands[2] = WrapPointer(dex_file);
1122 movw->operands[3] = offset;
1123 movw->operands[4] = WrapPointer(add_pc);
1124 movt->operands[2] = movw->operands[2];
1125 movt->operands[3] = movw->operands[3];
1126 movt->operands[4] = movw->operands[4];
1127 dex_cache_access_insns_.push_back(movw);
1128 dex_cache_access_insns_.push_back(movt);
Vladimir Markocc234812015-04-07 09:36:09 +01001129}
1130
Mathieu Chartiere401d142015-04-22 13:56:20 -07001131void ArmMir2Lir::OpPcRelDexCacheArrayLoad(const DexFile* dex_file, int offset, RegStorage r_dest,
1132 bool wide) {
1133 DCHECK(!wide) << "Unsupported";
Vladimir Markocc234812015-04-07 09:36:09 +01001134 if (dex_cache_arrays_base_reg_.Valid()) {
1135 LoadRefDisp(dex_cache_arrays_base_reg_, offset - dex_cache_arrays_min_offset_,
1136 r_dest, kNotVolatile);
1137 } else {
1138 OpPcRelDexCacheArrayAddr(dex_file, offset, r_dest);
1139 LoadRefDisp(r_dest, 0, r_dest, kNotVolatile);
1140 }
Vladimir Markoe5c76c52015-04-06 12:10:19 +01001141}
1142
buzbee2700f7e2014-03-07 09:46:20 -08001143LIR* ArmMir2Lir::OpVldm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001144 return NewLIR3(kThumb2Vldms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145}
1146
buzbee2700f7e2014-03-07 09:46:20 -08001147LIR* ArmMir2Lir::OpVstm(RegStorage r_base, int count) {
buzbee091cc402014-03-31 10:14:40 -07001148 return NewLIR3(kThumb2Vstms, r_base.GetReg(), rs_fr0.GetReg(), count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149}
1150
Ningsheng Jiana262f772014-11-25 16:48:07 +08001151void ArmMir2Lir::GenMaddMsubInt(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1152 RegLocation rl_src3, bool is_sub) {
1153 rl_src1 = LoadValue(rl_src1, kCoreReg);
1154 rl_src2 = LoadValue(rl_src2, kCoreReg);
1155 rl_src3 = LoadValue(rl_src3, kCoreReg);
1156 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1157 NewLIR4(is_sub ? kThumb2Mls : kThumb2Mla, rl_result.reg.GetReg(), rl_src1.reg.GetReg(),
1158 rl_src2.reg.GetReg(), rl_src3.reg.GetReg());
1159 StoreValue(rl_dest, rl_result);
1160}
1161
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162void ArmMir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
1163 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001164 int first_bit, int second_bit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -07001166 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 EncodeShift(kArmLsl, second_bit - first_bit));
1168 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001169 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 }
1171}
1172
Mingyao Yange643a172014-04-08 11:02:52 -07001173void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001174 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1175 RegStorage t_reg = AllocTemp();
1176 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001178 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179}
1180
1181// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001182LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001183#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001184 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001185 return OpCondBranch((target == nullptr) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001186#else
1187 RegStorage t_reg = AllocTemp();
1188 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
Ian Rogers8ba17f62014-10-27 18:48:49 -07001189 t_reg, kUnsignedHalf, kNotVolatile);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001190 LIR* cmp_branch = OpCmpImmBranch((target == nullptr) ? kCondNe : kCondEq, t_reg,
Wei Jin04f4d8a2014-05-29 18:04:29 -07001191 0, target);
1192 FreeTemp(t_reg);
1193 return cmp_branch;
1194#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195}
1196
1197// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001198LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001199 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001200 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001201 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202 return OpCondBranch(c_code, target);
1203}
1204
Andreas Gampeb14329f2014-05-15 11:16:06 -07001205bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001206 if (!cu_->compiler_driver->GetInstructionSetFeatures()->IsSmp()) {
Elliott Hughes8366ca02014-11-17 12:02:05 -08001207 return false;
1208 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001209 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1210 LIR* barrier = last_lir_insn_;
1211
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 int dmb_flavor;
1213 // TODO: revisit Arm barrier kinds
1214 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001215 case kAnyStore: dmb_flavor = kISH; break;
1216 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001217 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001218 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 default:
1220 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1221 dmb_flavor = kSY; // quiet gcc.
1222 break;
1223 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001224
Andreas Gampeb14329f2014-05-15 11:16:06 -07001225 bool ret = false;
1226
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001227 // If the same barrier already exists, don't generate another.
1228 if (barrier == nullptr
1229 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1230 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001231 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001232 }
1233
1234 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1235 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001236 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001237 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238}
1239
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001240void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 rl_src = LoadValueWide(rl_src, kCoreReg);
1242 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001243 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 LoadConstantNoClobber(z_reg, 0);
1245 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001246 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1247 RegStorage t_reg = AllocTemp();
Vladimir Marko2f340a82014-12-01 16:48:48 +00001248 OpRegCopy(t_reg, rl_result.reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -08001249 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1250 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 FreeTemp(t_reg);
1252 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001253 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1254 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001255 }
1256 FreeTemp(z_reg);
1257 StoreValueWide(rl_dest, rl_result);
1258}
1259
Mark Mendelle02d48f2014-01-15 11:19:23 -08001260void ArmMir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
1261 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001262 UNUSED(opcode);
1263 /*
1264 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1265 * dest = src1.lo * src2.lo;
1266 * tmp1 += src1.lo * src2.hi;
1267 * dest.hi += tmp1;
1268 *
1269 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1270 * registers. Normally for Arm, we get 5. We can get to 6 by including
1271 * lr in the temp set. The only problematic case is all operands and result are
1272 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1273 * freeing operand temp registers after they are no longer needed. All other cases
1274 * can proceed normally. We'll just punt on the case of the result having a misaligned
1275 * overlap with either operand and send that case to a runtime handler.
1276 */
1277 RegLocation rl_result;
1278 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1279 FlushAllRegs();
1280 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1281 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001282 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001283 return;
1284 }
1285
1286 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1287 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1288
1289 int reg_status = 0;
1290 RegStorage res_lo;
1291 RegStorage res_hi;
1292 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1293 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1294 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1295 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1296 // Check if rl_dest is *not* either operand and we have enough temp registers.
1297 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1298 (dest_promoted || src1_promoted || src2_promoted)) {
1299 // In this case, we do not need to manually allocate temp registers for result.
1300 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1301 res_lo = rl_result.reg.GetLow();
1302 res_hi = rl_result.reg.GetHigh();
1303 } else {
1304 res_lo = AllocTemp();
1305 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1306 // In this case, we have enough temp registers to be allocated for result.
1307 res_hi = AllocTemp();
1308 reg_status = 1;
1309 } else {
1310 // In this case, all temps are now allocated.
1311 // res_hi will be allocated after we can free src1_hi.
1312 reg_status = 2;
1313 }
1314 }
1315
1316 // Temporarily add LR to the temp pool, and assign it to tmp1
1317 MarkTemp(rs_rARM_LR);
1318 FreeTemp(rs_rARM_LR);
1319 RegStorage tmp1 = rs_rARM_LR;
1320 LockTemp(rs_rARM_LR);
1321
1322 if (rl_src1.reg == rl_src2.reg) {
1323 DCHECK(res_hi.Valid());
1324 DCHECK(res_lo.Valid());
1325 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1326 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1327 rl_src1.reg.GetLowReg());
1328 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1329 } else {
1330 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1331 if (reg_status == 2) {
1332 DCHECK(!res_hi.Valid());
1333 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1334 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1335 // Will force free src1_hi, so must clobber.
1336 Clobber(rl_src1.reg);
1337 FreeTemp(rl_src1.reg.GetHigh());
1338 res_hi = AllocTemp();
1339 }
1340 DCHECK(res_hi.Valid());
1341 DCHECK(res_lo.Valid());
1342 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1343 rl_src1.reg.GetLowReg());
1344 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1345 tmp1.GetReg());
1346 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1347 if (reg_status == 2) {
1348 FreeTemp(rl_src1.reg.GetLow());
1349 }
1350 }
1351
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 if (reg_status != 0) {
1353 // We had manually allocated registers for rl_result.
1354 // Now construct a RegLocation.
1355 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1356 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1357 }
1358
Vladimir Marko084f7d42015-04-23 17:00:40 +01001359 // Free tmp1 but keep LR as temp for StoreValueWide() if needed.
1360 FreeTemp(tmp1);
1361
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001362 StoreValueWide(rl_dest, rl_result);
Vladimir Marko084f7d42015-04-23 17:00:40 +01001363
1364 // Now, restore lr to its non-temp status.
1365 Clobber(rs_rARM_LR);
1366 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001367}
1368
Andreas Gampec76c6142014-08-04 16:30:03 -07001369void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001370 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001371 switch (opcode) {
1372 case Instruction::MUL_LONG:
1373 case Instruction::MUL_LONG_2ADDR:
1374 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1375 return;
1376 case Instruction::NEG_LONG:
1377 GenNegLong(rl_dest, rl_src2);
1378 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379
Andreas Gampec76c6142014-08-04 16:30:03 -07001380 default:
1381 break;
1382 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383
Andreas Gampec76c6142014-08-04 16:30:03 -07001384 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001385 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386}
1387
1388/*
1389 * Generate array load
1390 */
1391void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001392 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001393 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 int len_offset = mirror::Array::LengthOffset().Int32Value();
1395 int data_offset;
1396 RegLocation rl_result;
1397 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001398 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 if (!constant_index) {
1400 rl_index = LoadValue(rl_index, kCoreReg);
1401 }
1402
1403 if (rl_dest.wide) {
1404 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1405 } else {
1406 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1407 }
1408
1409 // If index is constant, just fold it into the data offset
1410 if (constant_index) {
1411 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1412 }
1413
1414 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001415 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001416
1417 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001418 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001419 if (needs_range_check) {
1420 reg_len = AllocTemp();
1421 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001422 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001423 MarkPossibleNullPointerException(opt_flags);
1424 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001425 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 }
1427 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001428 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001430 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431 } else {
1432 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001433 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001434 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001435 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001436 }
1437 rl_result = EvalLoc(rl_dest, reg_class, true);
1438
1439 if (needs_range_check) {
1440 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001441 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001443 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001444 }
1445 FreeTemp(reg_len);
1446 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001447 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001448 if (!constant_index) {
1449 FreeTemp(reg_ptr);
1450 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001451 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 StoreValueWide(rl_dest, rl_result);
1453 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 StoreValue(rl_dest, rl_result);
1455 }
1456 } else {
1457 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001458 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001459 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001460 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001461 rl_result = EvalLoc(rl_dest, reg_class, true);
1462
1463 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001464 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001465 FreeTemp(reg_len);
1466 }
buzbee2700f7e2014-03-07 09:46:20 -08001467 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001468 FreeTemp(reg_ptr);
1469 StoreValue(rl_dest, rl_result);
1470 }
1471}
1472
1473/*
1474 * Generate array store
1475 *
1476 */
1477void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001478 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001479 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 bool constant_index = rl_index.is_const;
1482
Ian Rogersa9a82542013-10-04 11:17:26 -07001483 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001484 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001485 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1486 } else {
1487 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1488 }
1489
1490 // If index is constant, just fold it into the data offset.
1491 if (constant_index) {
1492 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1493 }
1494
buzbeea0cd2d72014-06-01 09:33:49 -07001495 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 if (!constant_index) {
1497 rl_index = LoadValue(rl_index, kCoreReg);
1498 }
1499
buzbee2700f7e2014-03-07 09:46:20 -08001500 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001501 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001503 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001504 } else if (IsTemp(rl_array.reg) && !card_mark) {
1505 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001506 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001508 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001509 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001510 }
1511
1512 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001513 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001514
1515 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001516 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001517 if (needs_range_check) {
1518 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001519 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001521 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001522 MarkPossibleNullPointerException(opt_flags);
1523 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001524 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 }
1526 /* at this point, reg_ptr points to array, 2 live temps */
1527 if (rl_src.wide || rl_src.fp || constant_index) {
1528 if (rl_src.wide) {
1529 rl_src = LoadValueWide(rl_src, reg_class);
1530 } else {
1531 rl_src = LoadValue(rl_src, reg_class);
1532 }
1533 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001534 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 }
1536 if (needs_range_check) {
1537 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001538 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001540 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001541 }
1542 FreeTemp(reg_len);
1543 }
1544
Andreas Gampe3c12c512014-06-24 18:46:29 +00001545 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001546 } else {
1547 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001548 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001549 rl_src = LoadValue(rl_src, reg_class);
1550 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001551 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001552 FreeTemp(reg_len);
1553 }
buzbee2700f7e2014-03-07 09:46:20 -08001554 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 }
Ian Rogers773aab12013-10-14 13:50:10 -07001556 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001557 FreeTemp(reg_ptr);
1558 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001559 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001560 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001561 }
1562}
1563
Ian Rogersa9a82542013-10-04 11:17:26 -07001564
Brian Carlstrom7940e442013-07-12 13:46:57 -07001565void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001566 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
1567 int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001568 UNUSED(flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001569 rl_src = LoadValueWide(rl_src, kCoreReg);
1570 // Per spec, we only care about low 6 bits of shift amount.
1571 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1572 if (shift_amount == 0) {
1573 StoreValueWide(rl_dest, rl_src);
1574 return;
1575 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001576 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001577 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1578 return;
1579 }
1580 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001581 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001582 case Instruction::SHL_LONG:
1583 case Instruction::SHL_LONG_2ADDR:
1584 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001585 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1586 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001588 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1589 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001591 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1592 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001594 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001595 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001596 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001597 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001598 }
1599 break;
1600 case Instruction::SHR_LONG:
1601 case Instruction::SHR_LONG_2ADDR:
1602 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001603 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1604 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001606 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1607 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001608 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001609 RegStorage t_reg = AllocTemp();
1610 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001611 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 EncodeShift(kArmLsl, 32 - shift_amount));
1613 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001614 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001615 }
1616 break;
1617 case Instruction::USHR_LONG:
1618 case Instruction::USHR_LONG_2ADDR:
1619 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001620 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1621 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001623 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1624 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001625 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001626 RegStorage t_reg = AllocTemp();
1627 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001628 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 EncodeShift(kArmLsl, 32 - shift_amount));
1630 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001631 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001632 }
1633 break;
1634 default:
1635 LOG(FATAL) << "Unexpected case";
1636 }
1637 StoreValueWide(rl_dest, rl_result);
1638}
1639
1640void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001641 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1642 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001643 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1644 if (!rl_src2.is_const) {
1645 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001646 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001647 return;
1648 }
1649 } else {
1650 // Normalize
1651 if (!rl_src2.is_const) {
1652 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001653 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001654 }
1655 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001656 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001657 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001658 return;
1659 }
1660 DCHECK(rl_src2.is_const);
1661 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1662 uint32_t val_lo = Low32Bits(val);
1663 uint32_t val_hi = High32Bits(val);
1664 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1665 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1666
1667 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001668 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001669 case Instruction::ADD_LONG:
1670 case Instruction::ADD_LONG_2ADDR:
1671 case Instruction::SUB_LONG:
1672 case Instruction::SUB_LONG_2ADDR:
1673 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001674 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001675 return;
1676 }
1677 break;
1678 default:
1679 break;
1680 }
1681 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1682 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1683 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1684 switch (opcode) {
1685 case Instruction::ADD_LONG:
1686 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001687 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001688 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001689 break;
1690 case Instruction::OR_LONG:
1691 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001692 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1693 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001695 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001696 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001697 }
1698 break;
1699 case Instruction::XOR_LONG:
1700 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001701 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1702 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703 break;
1704 case Instruction::AND_LONG:
1705 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001706 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1707 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001709 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001710 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001711 }
1712 break;
1713 case Instruction::SUB_LONG_2ADDR:
1714 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001715 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001716 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001717 break;
1718 default:
1719 LOG(FATAL) << "Unexpected opcode " << opcode;
1720 }
1721 StoreValueWide(rl_dest, rl_result);
1722}
1723
Andreas Gamped500b532015-01-16 22:09:55 -08001724bool ArmMir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
1725 RegLocation rl_src, RegLocation rl_dest, int lit) {
1726 if (lit < 2) {
1727 return false;
1728 }
1729
1730 // ARM does either not support a division instruction, or it is potentially expensive. Look for
1731 // more special cases.
1732 if (!IsPowerOfTwo(lit)) {
1733 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1734 }
1735
1736 return Mir2Lir::HandleEasyDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1737}
1738
Brian Carlstrom7940e442013-07-12 13:46:57 -07001739} // namespace art