blob: b2bd6faca2e9647bd857eb5e80ed80ef14dd5039 [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,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100219 RegisterClass dest_reg_class ATTRIBUTE_UNUSED) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700220 // TODO: Generalize the IT below to accept more than one-instruction loads.
221 DCHECK(InexpensiveConstantInt(true_val));
222 DCHECK(InexpensiveConstantInt(false_val));
223
224 if ((true_val == 0 && code == kCondEq) ||
225 (false_val == 0 && code == kCondNe)) {
226 OpRegRegReg(kOpSub, rs_dest, left_op, right_op);
227 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
228 LIR* it = OpIT(kCondNe, "");
229 LoadConstant(rs_dest, code == kCondEq ? false_val : true_val);
230 OpEndIT(it);
231 return;
232 }
233
234 OpRegReg(kOpCmp, left_op, right_op); // Same?
235 LIR* it = OpIT(code, "E"); // if-convert the test
236 LoadConstant(rs_dest, true_val); // .eq case - load true
237 LoadConstant(rs_dest, false_val); // .eq case - load true
238 OpEndIT(it);
239}
240
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100241void ArmMir2Lir::GenSelect(BasicBlock* bb ATTRIBUTE_UNUSED, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 RegLocation rl_result;
243 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -0700245 // Avoid using float regs here.
246 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
247 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
248 rl_src = LoadValue(rl_src, src_reg_class);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000249 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 if (mir->ssa_rep->num_uses == 1) {
251 // CONST case
252 int true_val = mir->dalvikInsn.vB;
253 int false_val = mir->dalvikInsn.vC;
buzbeea0cd2d72014-06-01 09:33:49 -0700254 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Vladimir Markoa1a70742014-03-03 10:28:05 +0000255 // Change kCondNe to kCondEq for the special cases below.
256 if (ccode == kCondNe) {
257 ccode = kCondEq;
258 std::swap(true_val, false_val);
259 }
260 bool cheap_false_val = InexpensiveConstantInt(false_val);
261 if (cheap_false_val && ccode == kCondEq && (true_val == 0 || true_val == -1)) {
buzbee2700f7e2014-03-07 09:46:20 -0800262 OpRegRegImm(kOpSub, rl_result.reg, rl_src.reg, -true_val);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100263 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700264 LIR* it = OpIT(true_val == 0 ? kCondNe : kCondUge, "");
buzbee2700f7e2014-03-07 09:46:20 -0800265 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700266 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000267 } else if (cheap_false_val && ccode == kCondEq && true_val == 1) {
buzbee2700f7e2014-03-07 09:46:20 -0800268 OpRegRegImm(kOpRsub, rl_result.reg, rl_src.reg, 1);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100269 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Dave Allison3da67a52014-04-02 17:03:45 -0700270 LIR* it = OpIT(kCondLs, "");
buzbee2700f7e2014-03-07 09:46:20 -0800271 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700272 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Vladimir Markoa1a70742014-03-03 10:28:05 +0000273 } else if (cheap_false_val && InexpensiveConstantInt(true_val)) {
buzbee2700f7e2014-03-07 09:46:20 -0800274 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700275 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800276 LoadConstant(rl_result.reg, true_val);
277 LoadConstant(rl_result.reg, false_val);
Dave Allison3da67a52014-04-02 17:03:45 -0700278 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 } else {
280 // Unlikely case - could be tuned.
buzbeea0cd2d72014-06-01 09:33:49 -0700281 RegStorage t_reg1 = AllocTypedTemp(false, result_reg_class);
282 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 LoadConstant(t_reg1, true_val);
284 LoadConstant(t_reg2, false_val);
buzbee2700f7e2014-03-07 09:46:20 -0800285 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700286 LIR* it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800287 OpRegCopy(rl_result.reg, t_reg1);
288 OpRegCopy(rl_result.reg, t_reg2);
Dave Allison3da67a52014-04-02 17:03:45 -0700289 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 }
291 } else {
292 // MOVE case
293 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
294 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -0700295 rl_true = LoadValue(rl_true, result_reg_class);
296 rl_false = LoadValue(rl_false, result_reg_class);
297 rl_result = EvalLoc(rl_dest, result_reg_class, true);
buzbee2700f7e2014-03-07 09:46:20 -0800298 OpRegImm(kOpCmp, rl_src.reg, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700299 LIR* it = nullptr;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000300 if (rl_result.reg.GetReg() == rl_true.reg.GetReg()) { // Is the "true" case already in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700301 it = OpIT(NegateComparison(ccode), "");
buzbee2700f7e2014-03-07 09:46:20 -0800302 OpRegCopy(rl_result.reg, rl_false.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000303 } else if (rl_result.reg.GetReg() == rl_false.reg.GetReg()) { // False case in place?
Dave Allison3da67a52014-04-02 17:03:45 -0700304 it = OpIT(ccode, "");
buzbee2700f7e2014-03-07 09:46:20 -0800305 OpRegCopy(rl_result.reg, rl_true.reg);
buzbee252254b2013-09-08 16:20:53 -0700306 } else { // Normal - select between the two.
Dave Allison3da67a52014-04-02 17:03:45 -0700307 it = OpIT(ccode, "E");
buzbee2700f7e2014-03-07 09:46:20 -0800308 OpRegCopy(rl_result.reg, rl_true.reg);
309 OpRegCopy(rl_result.reg, rl_false.reg);
buzbee252254b2013-09-08 16:20:53 -0700310 }
Dave Allison3da67a52014-04-02 17:03:45 -0700311 OpEndIT(it); // Add a scheduling barrier to keep the IT shadow intact
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 }
313 StoreValue(rl_dest, rl_result);
314}
315
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700316void ArmMir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
318 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
319 // Normalize such that if either operand is constant, src2 will be constant.
Vladimir Markoa8946072014-01-22 10:30:44 +0000320 ConditionCode ccode = mir->meta.ccode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 if (rl_src1.is_const) {
Vladimir Marko58af1f92013-12-19 13:31:15 +0000322 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 ccode = FlipComparisonOrder(ccode);
324 }
325 if (rl_src2.is_const) {
buzbee082833c2014-05-17 23:16:26 -0700326 rl_src2 = UpdateLocWide(rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 // Do special compare/branch against simple const operand if not already in registers.
328 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
buzbee082833c2014-05-17 23:16:26 -0700329 if ((rl_src2.location != kLocPhysReg) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 ((ModifiedImmediate(Low32Bits(val)) >= 0) && (ModifiedImmediate(High32Bits(val)) >= 0))) {
331 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
332 return;
333 }
334 }
buzbee0d829482013-10-11 15:24:55 -0700335 LIR* taken = &block_label_list_[bb->taken];
336 LIR* not_taken = &block_label_list_[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
338 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800339 OpRegReg(kOpCmp, rl_src1.reg.GetHigh(), rl_src2.reg.GetHigh());
Brian Carlstromdf629502013-07-17 22:39:56 -0700340 switch (ccode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 case kCondEq:
342 OpCondBranch(kCondNe, not_taken);
343 break;
344 case kCondNe:
345 OpCondBranch(kCondNe, taken);
346 break;
347 case kCondLt:
348 OpCondBranch(kCondLt, taken);
349 OpCondBranch(kCondGt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000350 ccode = kCondUlt;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 break;
352 case kCondLe:
353 OpCondBranch(kCondLt, taken);
354 OpCondBranch(kCondGt, not_taken);
355 ccode = kCondLs;
356 break;
357 case kCondGt:
358 OpCondBranch(kCondGt, taken);
359 OpCondBranch(kCondLt, not_taken);
360 ccode = kCondHi;
361 break;
362 case kCondGe:
363 OpCondBranch(kCondGt, taken);
364 OpCondBranch(kCondLt, not_taken);
Vladimir Marko58af1f92013-12-19 13:31:15 +0000365 ccode = kCondUge;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 break;
367 default:
368 LOG(FATAL) << "Unexpected ccode: " << ccode;
369 }
buzbee2700f7e2014-03-07 09:46:20 -0800370 OpRegReg(kOpCmp, rl_src1.reg.GetLow(), rl_src2.reg.GetLow());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 OpCondBranch(ccode, taken);
372}
373
374/*
375 * Generate a register comparison to an immediate and branch. Caller
376 * is responsible for setting branch target field.
377 */
buzbee2700f7e2014-03-07 09:46:20 -0800378LIR* ArmMir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700379 LIR* branch = nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
buzbeeb48819d2013-09-14 16:15:25 -0700381 /*
382 * A common use of OpCmpImmBranch is for null checks, and using the Thumb 16-bit
383 * compare-and-branch if zero is ideal if it will reach. However, because null checks
Mingyao Yang3a74d152014-04-21 15:39:44 -0700384 * branch forward to a slow path, they will frequently not reach - and thus have to
buzbeeb48819d2013-09-14 16:15:25 -0700385 * be converted to a long form during assembly (which will trigger another assembly
386 * pass). Here we estimate the branch distance for checks, and if large directly
387 * generate the long form in an attempt to avoid an extra assembly pass.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700388 * TODO: consider interspersing slowpaths in code following unconditional branches.
buzbeeb48819d2013-09-14 16:15:25 -0700389 */
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 bool skip = ((target != nullptr) && (target->opcode == kPseudoThrowTarget));
Razvan A Lupusoru75035972014-09-11 15:24:59 -0700391 skip &= ((mir_graph_->GetNumDalvikInsns() - current_dalvik_offset_) > 64);
Andreas Gampe9522af92014-07-14 20:16:59 -0700392 if (!skip && reg.Low8() && (check_value == 0)) {
393 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
394 branch = NewLIR2((arm_cond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
395 reg.GetReg(), 0);
396 } else if (arm_cond == kArmCondLs) {
397 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
398 // This case happens for a bounds check of array[0].
399 branch = NewLIR2(kThumb2Cbz, reg.GetReg(), 0);
400 }
401 }
402
403 if (branch == nullptr) {
Vladimir Marko22479842013-11-19 17:04:50 +0000404 OpRegImm(kOpCmp, reg, check_value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 branch = NewLIR2(kThumbBCond, 0, arm_cond);
406 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700407
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 branch->target = target;
409 return branch;
410}
411
buzbee2700f7e2014-03-07 09:46:20 -0800412LIR* ArmMir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 LIR* res;
414 int opcode;
buzbee2700f7e2014-03-07 09:46:20 -0800415 // If src or dest is a pair, we'll be using low reg.
416 if (r_dest.IsPair()) {
417 r_dest = r_dest.GetLow();
418 }
419 if (r_src.IsPair()) {
420 r_src = r_src.GetLow();
421 }
buzbee091cc402014-03-31 10:14:40 -0700422 if (r_dest.IsFloat() || r_src.IsFloat())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 return OpFpRegCopy(r_dest, r_src);
buzbee091cc402014-03-31 10:14:40 -0700424 if (r_dest.Low8() && r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 opcode = kThumbMovRR;
buzbee091cc402014-03-31 10:14:40 -0700426 else if (!r_dest.Low8() && !r_src.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 opcode = kThumbMovRR_H2H;
buzbee091cc402014-03-31 10:14:40 -0700428 else if (r_dest.Low8())
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 opcode = kThumbMovRR_H2L;
430 else
431 opcode = kThumbMovRR_L2H;
buzbee2700f7e2014-03-07 09:46:20 -0800432 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
434 res->flags.is_nop = true;
435 }
436 return res;
437}
438
buzbee7a11ab02014-04-28 20:02:38 -0700439void ArmMir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
440 if (r_dest != r_src) {
441 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
442 AppendLIR(res);
443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444}
445
buzbee2700f7e2014-03-07 09:46:20 -0800446void ArmMir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
buzbee7a11ab02014-04-28 20:02:38 -0700447 if (r_dest != r_src) {
buzbee091cc402014-03-31 10:14:40 -0700448 bool dest_fp = r_dest.IsFloat();
449 bool src_fp = r_src.IsFloat();
450 DCHECK(r_dest.Is64Bit());
451 DCHECK(r_src.Is64Bit());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800452 // Note: If the register is get by register allocator, it should never be a pair.
453 // But some functions in mir_2_lir assume 64-bit registers are 32-bit register pairs.
454 // TODO: Rework Mir2Lir::LoadArg() and Mir2Lir::LoadArgDirect().
455 if (dest_fp && r_dest.IsPair()) {
456 r_dest = As64BitFloatReg(r_dest);
457 }
458 if (src_fp && r_src.IsPair()) {
459 r_src = As64BitFloatReg(r_src);
460 }
buzbee7a11ab02014-04-28 20:02:38 -0700461 if (dest_fp) {
462 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700463 OpRegCopy(r_dest, r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 } else {
buzbee091cc402014-03-31 10:14:40 -0700465 NewLIR3(kThumb2Fmdrr, r_dest.GetReg(), r_src.GetLowReg(), r_src.GetHighReg());
buzbee7a11ab02014-04-28 20:02:38 -0700466 }
467 } else {
468 if (src_fp) {
buzbee091cc402014-03-31 10:14:40 -0700469 NewLIR3(kThumb2Fmrrd, r_dest.GetLowReg(), r_dest.GetHighReg(), r_src.GetReg());
buzbee7a11ab02014-04-28 20:02:38 -0700470 } else {
471 // Handle overlap
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100472 if (r_src.GetHighReg() != r_dest.GetLowReg()) {
473 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
474 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
475 } else if (r_src.GetLowReg() != r_dest.GetHighReg()) {
buzbee7a11ab02014-04-28 20:02:38 -0700476 OpRegCopy(r_dest.GetHigh(), r_src.GetHigh());
477 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
478 } else {
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100479 RegStorage r_tmp = AllocTemp();
480 OpRegCopy(r_tmp, r_src.GetHigh());
buzbee7a11ab02014-04-28 20:02:38 -0700481 OpRegCopy(r_dest.GetLow(), r_src.GetLow());
Vladimir Marko8958f7f2015-06-19 14:56:38 +0100482 OpRegCopy(r_dest.GetHigh(), r_tmp);
483 FreeTemp(r_tmp);
buzbee7a11ab02014-04-28 20:02:38 -0700484 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 }
486 }
487 }
488}
489
490// Table of magic divisors
491struct MagicTable {
492 uint32_t magic;
493 uint32_t shift;
494 DividePattern pattern;
495};
496
497static const MagicTable magic_table[] = {
498 {0, 0, DivideNone}, // 0
499 {0, 0, DivideNone}, // 1
500 {0, 0, DivideNone}, // 2
501 {0x55555556, 0, Divide3}, // 3
502 {0, 0, DivideNone}, // 4
503 {0x66666667, 1, Divide5}, // 5
504 {0x2AAAAAAB, 0, Divide3}, // 6
505 {0x92492493, 2, Divide7}, // 7
506 {0, 0, DivideNone}, // 8
507 {0x38E38E39, 1, Divide5}, // 9
508 {0x66666667, 2, Divide5}, // 10
509 {0x2E8BA2E9, 1, Divide5}, // 11
510 {0x2AAAAAAB, 1, Divide5}, // 12
511 {0x4EC4EC4F, 2, Divide5}, // 13
512 {0x92492493, 3, Divide7}, // 14
513 {0x88888889, 3, Divide7}, // 15
514};
515
516// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100517bool ArmMir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode ATTRIBUTE_UNUSED, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700518 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 if ((lit < 0) || (lit >= static_cast<int>(sizeof(magic_table)/sizeof(magic_table[0])))) {
520 return false;
521 }
522 DividePattern pattern = magic_table[lit].pattern;
523 if (pattern == DivideNone) {
524 return false;
525 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526
buzbee2700f7e2014-03-07 09:46:20 -0800527 RegStorage r_magic = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 LoadConstant(r_magic, magic_table[lit].magic);
529 rl_src = LoadValue(rl_src, kCoreReg);
530 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800531 RegStorage r_hi = AllocTemp();
532 RegStorage r_lo = AllocTemp();
Zheng Xuf9719f92014-04-02 13:31:31 +0100533
534 // rl_dest and rl_src might overlap.
535 // Reuse r_hi to save the div result for reminder case.
536 RegStorage r_div_result = is_div ? rl_result.reg : r_hi;
537
buzbee2700f7e2014-03-07 09:46:20 -0800538 NewLIR4(kThumb2Smull, r_lo.GetReg(), r_hi.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
Brian Carlstromdf629502013-07-17 22:39:56 -0700539 switch (pattern) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 case Divide3:
Zheng Xuf9719f92014-04-02 13:31:31 +0100541 OpRegRegRegShift(kOpSub, r_div_result, r_hi, rl_src.reg, EncodeShift(kArmAsr, 31));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 break;
543 case Divide5:
buzbee2700f7e2014-03-07 09:46:20 -0800544 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100545 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700546 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 break;
548 case Divide7:
buzbee2700f7e2014-03-07 09:46:20 -0800549 OpRegReg(kOpAdd, r_hi, rl_src.reg);
550 OpRegRegImm(kOpAsr, r_lo, rl_src.reg, 31);
Zheng Xuf9719f92014-04-02 13:31:31 +0100551 OpRegRegRegShift(kOpRsub, r_div_result, r_lo, r_hi,
Ian Rogerse2143c02014-03-28 08:47:16 -0700552 EncodeShift(kArmAsr, magic_table[lit].shift));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 break;
554 default:
555 LOG(FATAL) << "Unexpected pattern: " << pattern;
556 }
Zheng Xuf9719f92014-04-02 13:31:31 +0100557
558 if (!is_div) {
559 // div_result = src / lit
560 // tmp1 = div_result * lit
561 // dest = src - tmp1
562 RegStorage tmp1 = r_lo;
563 EasyMultiplyOp ops[2];
564
565 bool canEasyMultiply = GetEasyMultiplyTwoOps(lit, ops);
566 DCHECK_NE(canEasyMultiply, false);
567
568 GenEasyMultiplyTwoOps(tmp1, r_div_result, ops);
569 OpRegRegReg(kOpSub, rl_result.reg, rl_src.reg, tmp1);
570 }
571
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 StoreValue(rl_dest, rl_result);
573 return true;
574}
575
Ian Rogerse2143c02014-03-28 08:47:16 -0700576// Try to convert *lit to 1 RegRegRegShift/RegRegShift form.
577bool ArmMir2Lir::GetEasyMultiplyOp(int lit, ArmMir2Lir::EasyMultiplyOp* op) {
Andreas Gampecfe71e52015-01-05 19:30:59 -0800578 if (lit == 0) {
579 // Special case for *divide-by-zero*. The ops won't actually be used to generate code, as
580 // GenArithOpIntLit will directly generate exception-throwing code, and multiply-by-zero will
581 // have been optimized away earlier.
582 op->op = kOpInvalid;
Dmitry Petrochenkoddf05aa2015-01-14 15:54:20 +0600583 op->shift = 0;
Andreas Gampecfe71e52015-01-05 19:30:59 -0800584 return true;
585 }
586
Ian Rogerse2143c02014-03-28 08:47:16 -0700587 if (IsPowerOfTwo(lit)) {
588 op->op = kOpLsl;
Andreas Gampe7e499922015-01-06 08:28:12 -0800589 op->shift = CTZ(lit);
Ian Rogerse2143c02014-03-28 08:47:16 -0700590 return true;
591 }
592
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000593 // At this point lit != 1 (which is a power of two).
594 DCHECK_NE(lit, 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700595 if (IsPowerOfTwo(lit - 1)) {
596 op->op = kOpAdd;
Andreas Gampe7e499922015-01-06 08:28:12 -0800597 op->shift = CTZ(lit - 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700598 return true;
599 }
600
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000601 if (lit == -1) {
602 // Can be created as neg.
603 op->op = kOpNeg;
604 op->shift = 0;
605 return true;
606 } else if (IsPowerOfTwo(lit + 1)) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700607 op->op = kOpRsub;
Andreas Gampe7e499922015-01-06 08:28:12 -0800608 op->shift = CTZ(lit + 1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700609 return true;
610 }
611
612 op->op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100613 op->shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700614 return false;
615}
616
617// Try to convert *lit to 1~2 RegRegRegShift/RegRegShift forms.
618bool ArmMir2Lir::GetEasyMultiplyTwoOps(int lit, EasyMultiplyOp* ops) {
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000619 DCHECK_NE(lit, 1); // A case of "1" should have been folded.
620 DCHECK_NE(lit, -1); // A case of "-1" should have been folded.
Ian Rogerse2143c02014-03-28 08:47:16 -0700621 if (GetEasyMultiplyOp(lit, &ops[0])) {
622 ops[1].op = kOpInvalid;
Zheng Xuf9719f92014-04-02 13:31:31 +0100623 ops[1].shift = 0;
Ian Rogerse2143c02014-03-28 08:47:16 -0700624 return true;
625 }
626
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000627 DCHECK_NE(lit, 0); // Should be handled above.
628 DCHECK(!IsPowerOfTwo(lit)); // Same.
629
630 int lit1 = lit; // With the DCHECKs, it's clear we don't get "0", "1" or "-1" for
631 uint32_t shift = CTZ(lit1); // lit1.
Ian Rogerse2143c02014-03-28 08:47:16 -0700632 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
633 ops[1].op = kOpLsl;
634 ops[1].shift = shift;
635 return true;
636 }
637
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000638 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 -0800639 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700640 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
641 ops[1].op = kOpAdd;
642 ops[1].shift = shift;
643 return true;
644 }
645
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000646 lit1 = lit + 1; // With the DCHECKs, it's clear we don't get "0" here.
Andreas Gampe7e499922015-01-06 08:28:12 -0800647 shift = CTZ(lit1);
Ian Rogerse2143c02014-03-28 08:47:16 -0700648 if (GetEasyMultiplyOp(lit1 >> shift, &ops[0])) {
649 ops[1].op = kOpRsub;
650 ops[1].shift = shift;
651 return true;
652 }
653
Andrew Hsiehf2674ea2015-03-16 17:03:06 +0800654 ops[1].op = kOpInvalid;
655 ops[1].shift = 0;
656
Ian Rogerse2143c02014-03-28 08:47:16 -0700657 return false;
658}
659
Zheng Xuf9719f92014-04-02 13:31:31 +0100660// Generate instructions to do multiply.
661// Additional temporary register is required,
662// if it need to generate 2 instructions and src/dest overlap.
Ian Rogerse2143c02014-03-28 08:47:16 -0700663void ArmMir2Lir::GenEasyMultiplyTwoOps(RegStorage r_dest, RegStorage r_src, EasyMultiplyOp* ops) {
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000664 // tmp1 = (( src << shift1) + [ src | -src | 0 ] ) | -src
Zheng Xuf9719f92014-04-02 13:31:31 +0100665 // dest = (tmp1 << shift2) + [ src | -src | 0 ]
666
667 RegStorage r_tmp1;
668 if (ops[1].op == kOpInvalid) {
669 r_tmp1 = r_dest;
670 } else if (r_dest.GetReg() != r_src.GetReg()) {
671 r_tmp1 = r_dest;
672 } else {
673 r_tmp1 = AllocTemp();
674 }
675
676 switch (ops[0].op) {
Ian Rogerse2143c02014-03-28 08:47:16 -0700677 case kOpLsl:
Zheng Xuf9719f92014-04-02 13:31:31 +0100678 OpRegRegImm(kOpLsl, r_tmp1, r_src, ops[0].shift);
Ian Rogerse2143c02014-03-28 08:47:16 -0700679 break;
680 case kOpAdd:
Zheng Xuf9719f92014-04-02 13:31:31 +0100681 OpRegRegRegShift(kOpAdd, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700682 break;
683 case kOpRsub:
Zheng Xuf9719f92014-04-02 13:31:31 +0100684 OpRegRegRegShift(kOpRsub, r_tmp1, r_src, r_src, EncodeShift(kArmLsl, ops[0].shift));
Ian Rogerse2143c02014-03-28 08:47:16 -0700685 break;
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000686 case kOpNeg:
687 OpRegReg(kOpNeg, r_tmp1, r_src);
688 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700689 default:
Zheng Xuf9719f92014-04-02 13:31:31 +0100690 DCHECK_EQ(ops[0].op, kOpInvalid);
Ian Rogerse2143c02014-03-28 08:47:16 -0700691 break;
Zheng Xuf9719f92014-04-02 13:31:31 +0100692 }
693
694 switch (ops[1].op) {
695 case kOpInvalid:
696 return;
697 case kOpLsl:
698 OpRegRegImm(kOpLsl, r_dest, r_tmp1, ops[1].shift);
699 break;
700 case kOpAdd:
701 OpRegRegRegShift(kOpAdd, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
702 break;
703 case kOpRsub:
704 OpRegRegRegShift(kOpRsub, r_dest, r_src, r_tmp1, EncodeShift(kArmLsl, ops[1].shift));
705 break;
Andreas Gampe151ab8d2015-08-14 23:01:49 +0000706 // No negation allowed in second op.
Zheng Xuf9719f92014-04-02 13:31:31 +0100707 default:
708 LOG(FATAL) << "Unexpected opcode passed to GenEasyMultiplyTwoOps";
709 break;
Ian Rogerse2143c02014-03-28 08:47:16 -0700710 }
711}
712
713bool ArmMir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
714 EasyMultiplyOp ops[2];
715
716 if (!GetEasyMultiplyTwoOps(lit, ops)) {
717 return false;
718 }
719
720 rl_src = LoadValue(rl_src, kCoreReg);
721 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
722
723 GenEasyMultiplyTwoOps(rl_result.reg, rl_src.reg, ops);
724 StoreValue(rl_dest, rl_result);
725 return true;
726}
727
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100728RegLocation ArmMir2Lir::GenDivRem(RegLocation rl_dest ATTRIBUTE_UNUSED,
729 RegLocation rl_src1 ATTRIBUTE_UNUSED,
730 RegLocation rl_src2 ATTRIBUTE_UNUSED,
731 bool is_div ATTRIBUTE_UNUSED,
732 int flags ATTRIBUTE_UNUSED) {
Mark Mendell2bf31e62014-01-23 12:13:40 -0800733 LOG(FATAL) << "Unexpected use of GenDivRem for Arm";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700734 UNREACHABLE();
Mark Mendell2bf31e62014-01-23 12:13:40 -0800735}
736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100737RegLocation ArmMir2Lir::GenDivRemLit(RegLocation rl_dest ATTRIBUTE_UNUSED,
738 RegLocation rl_src1 ATTRIBUTE_UNUSED,
739 int lit ATTRIBUTE_UNUSED,
740 bool is_div ATTRIBUTE_UNUSED) {
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,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001163 RegLocation rl_result, int lit ATTRIBUTE_UNUSED,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001164 int first_bit, int second_bit) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001165 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 EncodeShift(kArmLsl, second_bit - first_bit));
1167 if (first_bit != 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001168 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
1170}
1171
Mingyao Yange643a172014-04-08 11:02:52 -07001172void ArmMir2Lir::GenDivZeroCheckWide(RegStorage reg) {
buzbee2700f7e2014-03-07 09:46:20 -08001173 DCHECK(reg.IsPair()); // TODO: support k64BitSolo.
1174 RegStorage t_reg = AllocTemp();
1175 NewLIR4(kThumb2OrrRRRs, t_reg.GetReg(), reg.GetLowReg(), reg.GetHighReg(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 FreeTemp(t_reg);
Mingyao Yange643a172014-04-08 11:02:52 -07001177 GenDivZeroCheck(kCondEq);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178}
1179
1180// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001181LIR* ArmMir2Lir::OpTestSuspend(LIR* target) {
Wei Jin04f4d8a2014-05-29 18:04:29 -07001182#ifdef ARM_R4_SUSPEND_FLAG
buzbee091cc402014-03-31 10:14:40 -07001183 NewLIR2(kThumbSubRI8, rs_rARM_SUSPEND.GetReg(), 1);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001184 return OpCondBranch((target == nullptr) ? kCondEq : kCondNe, target);
Wei Jin04f4d8a2014-05-29 18:04:29 -07001185#else
1186 RegStorage t_reg = AllocTemp();
1187 LoadBaseDisp(rs_rARM_SELF, Thread::ThreadFlagsOffset<4>().Int32Value(),
Ian Rogers8ba17f62014-10-27 18:48:49 -07001188 t_reg, kUnsignedHalf, kNotVolatile);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001189 LIR* cmp_branch = OpCmpImmBranch((target == nullptr) ? kCondNe : kCondEq, t_reg,
Wei Jin04f4d8a2014-05-29 18:04:29 -07001190 0, target);
1191 FreeTemp(t_reg);
1192 return cmp_branch;
1193#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194}
1195
1196// Decrement register and branch on condition
buzbee2700f7e2014-03-07 09:46:20 -08001197LIR* ArmMir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 // Combine sub & test using sub setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +00001199 OpRegRegImm(kOpSub, reg, reg, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001200 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 return OpCondBranch(c_code, target);
1202}
1203
Andreas Gampeb14329f2014-05-15 11:16:06 -07001204bool ArmMir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Andreas Gampe0b9203e2015-01-22 20:39:27 -08001205 if (!cu_->compiler_driver->GetInstructionSetFeatures()->IsSmp()) {
Elliott Hughes8366ca02014-11-17 12:02:05 -08001206 return false;
1207 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001208 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
1209 LIR* barrier = last_lir_insn_;
1210
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211 int dmb_flavor;
1212 // TODO: revisit Arm barrier kinds
1213 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001214 case kAnyStore: dmb_flavor = kISH; break;
1215 case kLoadAny: dmb_flavor = kISH; break;
Ian Rogersb122a4b2013-11-19 18:00:50 -08001216 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001217 case kAnyAny: dmb_flavor = kISH; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218 default:
1219 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1220 dmb_flavor = kSY; // quiet gcc.
1221 break;
1222 }
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001223
Andreas Gampeb14329f2014-05-15 11:16:06 -07001224 bool ret = false;
1225
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001226 // If the same barrier already exists, don't generate another.
1227 if (barrier == nullptr
1228 || (barrier != nullptr && (barrier->opcode != kThumb2Dmb || barrier->operands[0] != dmb_flavor))) {
1229 barrier = NewLIR1(kThumb2Dmb, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001230 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -08001231 }
1232
1233 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1234 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001235 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001236 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237}
1238
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001239void ArmMir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 rl_src = LoadValueWide(rl_src, kCoreReg);
1241 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -08001242 RegStorage z_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001243 LoadConstantNoClobber(z_reg, 0);
1244 // Check for destructive overlap
buzbee2700f7e2014-03-07 09:46:20 -08001245 if (rl_result.reg.GetLowReg() == rl_src.reg.GetHighReg()) {
1246 RegStorage t_reg = AllocTemp();
Vladimir Marko2f340a82014-12-01 16:48:48 +00001247 OpRegCopy(t_reg, rl_result.reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -08001248 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1249 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, t_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 FreeTemp(t_reg);
1251 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001252 OpRegRegReg(kOpSub, rl_result.reg.GetLow(), z_reg, rl_src.reg.GetLow());
1253 OpRegRegReg(kOpSbc, rl_result.reg.GetHigh(), z_reg, rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
1255 FreeTemp(z_reg);
1256 StoreValueWide(rl_dest, rl_result);
1257}
1258
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001259void ArmMir2Lir::GenMulLong(Instruction::Code opcode ATTRIBUTE_UNUSED, RegLocation rl_dest,
Mark Mendelle02d48f2014-01-15 11:19:23 -08001260 RegLocation rl_src1, RegLocation rl_src2) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001261 /*
1262 * tmp1 = src1.hi * src2.lo; // src1.hi is no longer needed
1263 * dest = src1.lo * src2.lo;
1264 * tmp1 += src1.lo * src2.hi;
1265 * dest.hi += tmp1;
1266 *
1267 * To pull off inline multiply, we have a worst-case requirement of 7 temporary
1268 * registers. Normally for Arm, we get 5. We can get to 6 by including
1269 * lr in the temp set. The only problematic case is all operands and result are
1270 * distinct, and none have been promoted. In that case, we can succeed by aggressively
1271 * freeing operand temp registers after they are no longer needed. All other cases
1272 * can proceed normally. We'll just punt on the case of the result having a misaligned
1273 * overlap with either operand and send that case to a runtime handler.
1274 */
1275 RegLocation rl_result;
1276 if (PartiallyIntersects(rl_src1, rl_dest) || (PartiallyIntersects(rl_src2, rl_dest))) {
1277 FlushAllRegs();
1278 CallRuntimeHelperRegLocationRegLocation(kQuickLmul, rl_src1, rl_src2, false);
1279 rl_result = GetReturnWide(kCoreReg);
Zheng Xud7f8e022014-03-13 13:40:30 +00001280 StoreValueWide(rl_dest, rl_result);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001281 return;
1282 }
1283
1284 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1285 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1286
1287 int reg_status = 0;
1288 RegStorage res_lo;
1289 RegStorage res_hi;
1290 bool dest_promoted = rl_dest.location == kLocPhysReg && rl_dest.reg.Valid() &&
1291 !IsTemp(rl_dest.reg.GetLow()) && !IsTemp(rl_dest.reg.GetHigh());
1292 bool src1_promoted = !IsTemp(rl_src1.reg.GetLow()) && !IsTemp(rl_src1.reg.GetHigh());
1293 bool src2_promoted = !IsTemp(rl_src2.reg.GetLow()) && !IsTemp(rl_src2.reg.GetHigh());
1294 // Check if rl_dest is *not* either operand and we have enough temp registers.
1295 if ((rl_dest.s_reg_low != rl_src1.s_reg_low && rl_dest.s_reg_low != rl_src2.s_reg_low) &&
1296 (dest_promoted || src1_promoted || src2_promoted)) {
1297 // In this case, we do not need to manually allocate temp registers for result.
1298 rl_result = EvalLoc(rl_dest, kCoreReg, true);
1299 res_lo = rl_result.reg.GetLow();
1300 res_hi = rl_result.reg.GetHigh();
1301 } else {
1302 res_lo = AllocTemp();
1303 if ((rl_src1.s_reg_low == rl_src2.s_reg_low) || src1_promoted || src2_promoted) {
1304 // In this case, we have enough temp registers to be allocated for result.
1305 res_hi = AllocTemp();
1306 reg_status = 1;
1307 } else {
1308 // In this case, all temps are now allocated.
1309 // res_hi will be allocated after we can free src1_hi.
1310 reg_status = 2;
1311 }
1312 }
1313
1314 // Temporarily add LR to the temp pool, and assign it to tmp1
1315 MarkTemp(rs_rARM_LR);
1316 FreeTemp(rs_rARM_LR);
1317 RegStorage tmp1 = rs_rARM_LR;
1318 LockTemp(rs_rARM_LR);
1319
1320 if (rl_src1.reg == rl_src2.reg) {
1321 DCHECK(res_hi.Valid());
1322 DCHECK(res_lo.Valid());
1323 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1324 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src1.reg.GetLowReg(),
1325 rl_src1.reg.GetLowReg());
1326 OpRegRegRegShift(kOpAdd, res_hi, res_hi, tmp1, EncodeShift(kArmLsl, 1));
1327 } else {
1328 NewLIR3(kThumb2MulRRR, tmp1.GetReg(), rl_src2.reg.GetLowReg(), rl_src1.reg.GetHighReg());
1329 if (reg_status == 2) {
1330 DCHECK(!res_hi.Valid());
1331 DCHECK_NE(rl_src1.reg.GetLowReg(), rl_src2.reg.GetLowReg());
1332 DCHECK_NE(rl_src1.reg.GetHighReg(), rl_src2.reg.GetHighReg());
1333 // Will force free src1_hi, so must clobber.
1334 Clobber(rl_src1.reg);
1335 FreeTemp(rl_src1.reg.GetHigh());
1336 res_hi = AllocTemp();
1337 }
1338 DCHECK(res_hi.Valid());
1339 DCHECK(res_lo.Valid());
1340 NewLIR4(kThumb2Umull, res_lo.GetReg(), res_hi.GetReg(), rl_src2.reg.GetLowReg(),
1341 rl_src1.reg.GetLowReg());
1342 NewLIR4(kThumb2Mla, tmp1.GetReg(), rl_src1.reg.GetLowReg(), rl_src2.reg.GetHighReg(),
1343 tmp1.GetReg());
1344 NewLIR4(kThumb2AddRRR, res_hi.GetReg(), tmp1.GetReg(), res_hi.GetReg(), 0);
1345 if (reg_status == 2) {
1346 FreeTemp(rl_src1.reg.GetLow());
1347 }
1348 }
1349
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001350 if (reg_status != 0) {
1351 // We had manually allocated registers for rl_result.
1352 // Now construct a RegLocation.
1353 rl_result = GetReturnWide(kCoreReg); // Just using as a template.
1354 rl_result.reg = RegStorage::MakeRegPair(res_lo, res_hi);
1355 }
1356
Vladimir Marko084f7d42015-04-23 17:00:40 +01001357 // Free tmp1 but keep LR as temp for StoreValueWide() if needed.
1358 FreeTemp(tmp1);
1359
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001360 StoreValueWide(rl_dest, rl_result);
Vladimir Marko084f7d42015-04-23 17:00:40 +01001361
1362 // Now, restore lr to its non-temp status.
1363 Clobber(rs_rARM_LR);
1364 UnmarkTemp(rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365}
1366
Andreas Gampec76c6142014-08-04 16:30:03 -07001367void ArmMir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001368 RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001369 switch (opcode) {
1370 case Instruction::MUL_LONG:
1371 case Instruction::MUL_LONG_2ADDR:
1372 GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1373 return;
1374 case Instruction::NEG_LONG:
1375 GenNegLong(rl_dest, rl_src2);
1376 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377
Andreas Gampec76c6142014-08-04 16:30:03 -07001378 default:
1379 break;
1380 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381
Andreas Gampec76c6142014-08-04 16:30:03 -07001382 // Fallback for all other ops.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001383 Mir2Lir::GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384}
1385
1386/*
1387 * Generate array load
1388 */
1389void ArmMir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001390 RegLocation rl_index, RegLocation rl_dest, int scale) {
buzbee091cc402014-03-31 10:14:40 -07001391 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 int len_offset = mirror::Array::LengthOffset().Int32Value();
1393 int data_offset;
1394 RegLocation rl_result;
1395 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001396 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397 if (!constant_index) {
1398 rl_index = LoadValue(rl_index, kCoreReg);
1399 }
1400
1401 if (rl_dest.wide) {
1402 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1403 } else {
1404 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1405 }
1406
1407 // If index is constant, just fold it into the data offset
1408 if (constant_index) {
1409 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1410 }
1411
1412 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001413 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001414
1415 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001416 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001417 if (needs_range_check) {
1418 reg_len = AllocTemp();
1419 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001420 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001421 MarkPossibleNullPointerException(opt_flags);
1422 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001423 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001424 }
1425 if (rl_dest.wide || rl_dest.fp || constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001426 RegStorage reg_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001428 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 } else {
1430 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001431 reg_ptr = AllocTempRef();
Ian Rogerse2143c02014-03-28 08:47:16 -07001432 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
buzbee091cc402014-03-31 10:14:40 -07001433 FreeTemp(rl_index.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001434 }
1435 rl_result = EvalLoc(rl_dest, reg_class, true);
1436
1437 if (needs_range_check) {
1438 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001439 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001441 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442 }
1443 FreeTemp(reg_len);
1444 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001445 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
Vladimir Marko455759b2014-05-06 20:49:36 +01001446 if (!constant_index) {
1447 FreeTemp(reg_ptr);
1448 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001449 if (rl_dest.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001450 StoreValueWide(rl_dest, rl_result);
1451 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001452 StoreValue(rl_dest, rl_result);
1453 }
1454 } else {
1455 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001456 RegStorage reg_ptr = AllocTempRef();
buzbee2700f7e2014-03-07 09:46:20 -08001457 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
buzbee091cc402014-03-31 10:14:40 -07001458 FreeTemp(rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 rl_result = EvalLoc(rl_dest, reg_class, true);
1460
1461 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001462 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001463 FreeTemp(reg_len);
1464 }
buzbee2700f7e2014-03-07 09:46:20 -08001465 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001466 FreeTemp(reg_ptr);
1467 StoreValue(rl_dest, rl_result);
1468 }
1469}
1470
1471/*
1472 * Generate array store
1473 *
1474 */
1475void ArmMir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -07001476 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
buzbee091cc402014-03-31 10:14:40 -07001477 RegisterClass reg_class = RegClassBySize(size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 bool constant_index = rl_index.is_const;
1480
Ian Rogersa9a82542013-10-04 11:17:26 -07001481 int data_offset;
buzbee695d13a2014-04-19 13:32:20 -07001482 if (size == k64 || size == kDouble) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001483 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1484 } else {
1485 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1486 }
1487
1488 // If index is constant, just fold it into the data offset.
1489 if (constant_index) {
1490 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1491 }
1492
buzbeea0cd2d72014-06-01 09:33:49 -07001493 rl_array = LoadValue(rl_array, kRefReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 if (!constant_index) {
1495 rl_index = LoadValue(rl_index, kCoreReg);
1496 }
1497
buzbee2700f7e2014-03-07 09:46:20 -08001498 RegStorage reg_ptr;
Ian Rogers773aab12013-10-14 13:50:10 -07001499 bool allocated_reg_ptr_temp = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001500 if (constant_index) {
buzbee2700f7e2014-03-07 09:46:20 -08001501 reg_ptr = rl_array.reg;
buzbee091cc402014-03-31 10:14:40 -07001502 } else if (IsTemp(rl_array.reg) && !card_mark) {
1503 Clobber(rl_array.reg);
buzbee2700f7e2014-03-07 09:46:20 -08001504 reg_ptr = rl_array.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 } else {
Ian Rogers773aab12013-10-14 13:50:10 -07001506 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001507 reg_ptr = AllocTempRef();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001508 }
1509
1510 /* null object? */
buzbee2700f7e2014-03-07 09:46:20 -08001511 GenNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001512
1513 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
buzbee2700f7e2014-03-07 09:46:20 -08001514 RegStorage reg_len;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 if (needs_range_check) {
1516 reg_len = AllocTemp();
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001517 // NOTE: max live temps(4) here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001518 /* Get len */
buzbee695d13a2014-04-19 13:32:20 -07001519 Load32Disp(rl_array.reg, len_offset, reg_len);
Dave Allisonb373e092014-02-20 16:06:36 -08001520 MarkPossibleNullPointerException(opt_flags);
1521 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001522 ForceImplicitNullCheck(rl_array.reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 }
1524 /* at this point, reg_ptr points to array, 2 live temps */
1525 if (rl_src.wide || rl_src.fp || constant_index) {
1526 if (rl_src.wide) {
1527 rl_src = LoadValueWide(rl_src, reg_class);
1528 } else {
1529 rl_src = LoadValue(rl_src, reg_class);
1530 }
1531 if (!constant_index) {
Ian Rogerse2143c02014-03-28 08:47:16 -07001532 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, rl_index.reg, EncodeShift(kArmLsl, scale));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001533 }
1534 if (needs_range_check) {
1535 if (constant_index) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001536 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001537 } else {
Mingyao Yang80365d92014-04-18 12:10:58 -07001538 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001539 }
1540 FreeTemp(reg_len);
1541 }
1542
Andreas Gampe3c12c512014-06-24 18:46:29 +00001543 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001544 } else {
1545 /* reg_ptr -> array data */
buzbee2700f7e2014-03-07 09:46:20 -08001546 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001547 rl_src = LoadValue(rl_src, reg_class);
1548 if (needs_range_check) {
Mingyao Yang80365d92014-04-18 12:10:58 -07001549 GenArrayBoundsCheck(rl_index.reg, reg_len);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 FreeTemp(reg_len);
1551 }
buzbee2700f7e2014-03-07 09:46:20 -08001552 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001553 }
Ian Rogers773aab12013-10-14 13:50:10 -07001554 if (allocated_reg_ptr_temp) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001555 FreeTemp(reg_ptr);
1556 }
Ian Rogersa9a82542013-10-04 11:17:26 -07001557 if (card_mark) {
Vladimir Marko743b98c2014-11-24 19:45:41 +00001558 MarkGCCard(opt_flags, rl_src.reg, rl_array.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 }
1560}
1561
Ian Rogersa9a82542013-10-04 11:17:26 -07001562
Brian Carlstrom7940e442013-07-12 13:46:57 -07001563void ArmMir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001564 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001565 int flags ATTRIBUTE_UNUSED) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001566 rl_src = LoadValueWide(rl_src, kCoreReg);
1567 // Per spec, we only care about low 6 bits of shift amount.
1568 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
1569 if (shift_amount == 0) {
1570 StoreValueWide(rl_dest, rl_src);
1571 return;
1572 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001573 if (PartiallyIntersects(rl_src, rl_dest)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001574 GenShiftOpLong(opcode, rl_dest, rl_src, rl_shift);
1575 return;
1576 }
1577 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Brian Carlstromdf629502013-07-17 22:39:56 -07001578 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001579 case Instruction::SHL_LONG:
1580 case Instruction::SHL_LONG_2ADDR:
1581 if (shift_amount == 1) {
buzbee2700f7e2014-03-07 09:46:20 -08001582 OpRegRegReg(kOpAdd, rl_result.reg.GetLow(), rl_src.reg.GetLow(), rl_src.reg.GetLow());
1583 OpRegRegReg(kOpAdc, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), rl_src.reg.GetHigh());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001584 } else if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001585 OpRegCopy(rl_result.reg.GetHigh(), rl_src.reg);
1586 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001587 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001588 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetLow(), shift_amount - 32);
1589 LoadConstant(rl_result.reg.GetLow(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001590 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001591 OpRegRegImm(kOpLsl, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001592 OpRegRegRegShift(kOpOr, rl_result.reg.GetHigh(), rl_result.reg.GetHigh(), rl_src.reg.GetLow(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001593 EncodeShift(kArmLsr, 32 - shift_amount));
buzbee2700f7e2014-03-07 09:46:20 -08001594 OpRegRegImm(kOpLsl, rl_result.reg.GetLow(), rl_src.reg.GetLow(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001595 }
1596 break;
1597 case Instruction::SHR_LONG:
1598 case Instruction::SHR_LONG_2ADDR:
1599 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001600 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1601 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001602 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001603 OpRegRegImm(kOpAsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1604 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), 31);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001605 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001606 RegStorage t_reg = AllocTemp();
1607 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001608 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001609 EncodeShift(kArmLsl, 32 - shift_amount));
1610 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001611 OpRegRegImm(kOpAsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001612 }
1613 break;
1614 case Instruction::USHR_LONG:
1615 case Instruction::USHR_LONG_2ADDR:
1616 if (shift_amount == 32) {
buzbee2700f7e2014-03-07 09:46:20 -08001617 OpRegCopy(rl_result.reg.GetLow(), rl_src.reg.GetHigh());
1618 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001619 } else if (shift_amount > 31) {
buzbee2700f7e2014-03-07 09:46:20 -08001620 OpRegRegImm(kOpLsr, rl_result.reg.GetLow(), rl_src.reg.GetHigh(), shift_amount - 32);
1621 LoadConstant(rl_result.reg.GetHigh(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001622 } else {
buzbee2700f7e2014-03-07 09:46:20 -08001623 RegStorage t_reg = AllocTemp();
1624 OpRegRegImm(kOpLsr, t_reg, rl_src.reg.GetLow(), shift_amount);
Ian Rogerse2143c02014-03-28 08:47:16 -07001625 OpRegRegRegShift(kOpOr, rl_result.reg.GetLow(), t_reg, rl_src.reg.GetHigh(),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001626 EncodeShift(kArmLsl, 32 - shift_amount));
1627 FreeTemp(t_reg);
buzbee2700f7e2014-03-07 09:46:20 -08001628 OpRegRegImm(kOpLsr, rl_result.reg.GetHigh(), rl_src.reg.GetHigh(), shift_amount);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001629 }
1630 break;
1631 default:
1632 LOG(FATAL) << "Unexpected case";
1633 }
1634 StoreValueWide(rl_dest, rl_result);
1635}
1636
1637void ArmMir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001638 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
1639 int flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001640 if ((opcode == Instruction::SUB_LONG_2ADDR) || (opcode == Instruction::SUB_LONG)) {
1641 if (!rl_src2.is_const) {
1642 // Don't bother with special handling for subtract from immediate.
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001643 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001644 return;
1645 }
1646 } else {
1647 // Normalize
1648 if (!rl_src2.is_const) {
1649 DCHECK(rl_src1.is_const);
Vladimir Marko58af1f92013-12-19 13:31:15 +00001650 std::swap(rl_src1, rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001651 }
1652 }
Alexei Zavjalovd8c3e362014-10-08 15:51:59 +07001653 if (PartiallyIntersects(rl_src1, rl_dest)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001654 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001655 return;
1656 }
1657 DCHECK(rl_src2.is_const);
1658 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1659 uint32_t val_lo = Low32Bits(val);
1660 uint32_t val_hi = High32Bits(val);
1661 int32_t mod_imm_lo = ModifiedImmediate(val_lo);
1662 int32_t mod_imm_hi = ModifiedImmediate(val_hi);
1663
1664 // Only a subset of add/sub immediate instructions set carry - so bail if we don't fit
Brian Carlstromdf629502013-07-17 22:39:56 -07001665 switch (opcode) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001666 case Instruction::ADD_LONG:
1667 case Instruction::ADD_LONG_2ADDR:
1668 case Instruction::SUB_LONG:
1669 case Instruction::SUB_LONG_2ADDR:
1670 if ((mod_imm_lo < 0) || (mod_imm_hi < 0)) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001671 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001672 return;
1673 }
1674 break;
1675 default:
1676 break;
1677 }
1678 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1679 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1680 // NOTE: once we've done the EvalLoc on dest, we can no longer bail.
1681 switch (opcode) {
1682 case Instruction::ADD_LONG:
1683 case Instruction::ADD_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001684 NewLIR3(kThumb2AddRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001685 NewLIR3(kThumb2AdcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001686 break;
1687 case Instruction::OR_LONG:
1688 case Instruction::OR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001689 if ((val_lo != 0) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1690 OpRegRegImm(kOpOr, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001691 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001692 if ((val_hi != 0) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001693 OpRegRegImm(kOpOr, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001694 }
1695 break;
1696 case Instruction::XOR_LONG:
1697 case Instruction::XOR_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001698 OpRegRegImm(kOpXor, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
1699 OpRegRegImm(kOpXor, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001700 break;
1701 case Instruction::AND_LONG:
1702 case Instruction::AND_LONG_2ADDR:
buzbee2700f7e2014-03-07 09:46:20 -08001703 if ((val_lo != 0xffffffff) || (rl_result.reg.GetLowReg() != rl_src1.reg.GetLowReg())) {
1704 OpRegRegImm(kOpAnd, rl_result.reg.GetLow(), rl_src1.reg.GetLow(), val_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001705 }
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001706 if ((val_hi != 0xffffffff) || (rl_result.reg.GetHighReg() != rl_src1.reg.GetHighReg())) {
buzbee2700f7e2014-03-07 09:46:20 -08001707 OpRegRegImm(kOpAnd, rl_result.reg.GetHigh(), rl_src1.reg.GetHigh(), val_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001708 }
1709 break;
1710 case Instruction::SUB_LONG_2ADDR:
1711 case Instruction::SUB_LONG:
buzbee2700f7e2014-03-07 09:46:20 -08001712 NewLIR3(kThumb2SubRRI8M, rl_result.reg.GetLowReg(), rl_src1.reg.GetLowReg(), mod_imm_lo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001713 NewLIR3(kThumb2SbcRRI8M, rl_result.reg.GetHighReg(), rl_src1.reg.GetHighReg(), mod_imm_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001714 break;
1715 default:
1716 LOG(FATAL) << "Unexpected opcode " << opcode;
1717 }
1718 StoreValueWide(rl_dest, rl_result);
1719}
1720
Andreas Gamped500b532015-01-16 22:09:55 -08001721bool ArmMir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
1722 RegLocation rl_src, RegLocation rl_dest, int lit) {
1723 if (lit < 2) {
1724 return false;
1725 }
1726
1727 // ARM does either not support a division instruction, or it is potentially expensive. Look for
1728 // more special cases.
1729 if (!IsPowerOfTwo(lit)) {
1730 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1731 }
1732
1733 return Mir2Lir::HandleEasyDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1734}
1735
Brian Carlstrom7940e442013-07-12 13:46:57 -07001736} // namespace art