blob: 965759b59d27b33e609d96c0b922f162c89338c4 [file] [log] [blame]
Matteo Franchin43ec8732014-03-31 15:00:14 +01001/*
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
19#include "arm64_lir.h"
20#include "codegen_arm64.h"
21#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070022#include "dex/reg_storage_eq.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010023#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Andreas Gampef29ecd62014-07-29 00:35:00 -070025#include "utils.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010026
27namespace art {
28
29LIR* Arm64Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
30 OpRegReg(kOpCmp, src1, src2);
31 return OpCondBranch(cond, target);
32}
33
Matteo Franchin43ec8732014-03-31 15:00:14 +010034LIR* Arm64Mir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070035 UNUSED(ccode, guide);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010036 LOG(FATAL) << "Unexpected use of OpIT for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070037 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +010038}
39
40void Arm64Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070041 UNUSED(it);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010042 LOG(FATAL) << "Unexpected use of OpEndIT for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +010043}
44
45/*
46 * 64-bit 3way compare function.
Matteo Franchine45fb9e2014-05-06 10:10:30 +010047 * cmp xA, xB
Zheng Xu511c8a62014-06-03 16:22:23 +080048 * csinc wC, wzr, wzr, eq // wC = (xA == xB) ? 0 : 1
49 * csneg wC, wC, wC, ge // wC = (xA >= xB) ? wC : -wC
Matteo Franchin43ec8732014-03-31 15:00:14 +010050 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +010051void Arm64Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
52 RegLocation rl_src2) {
53 RegLocation rl_result;
Matteo Franchin43ec8732014-03-31 15:00:14 +010054 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
55 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010056 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +010057
Matteo Franchine45fb9e2014-05-06 10:10:30 +010058 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Zheng Xu511c8a62014-06-03 16:22:23 +080059 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondEq);
60 NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_result.reg.GetReg(),
61 rl_result.reg.GetReg(), kArmCondGe);
62 StoreValue(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010063}
64
65void Arm64Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
66 RegLocation rl_src1, RegLocation rl_shift) {
67 OpKind op = kOpBkpt;
68 switch (opcode) {
69 case Instruction::SHL_LONG:
70 case Instruction::SHL_LONG_2ADDR:
71 op = kOpLsl;
72 break;
73 case Instruction::SHR_LONG:
74 case Instruction::SHR_LONG_2ADDR:
75 op = kOpAsr;
76 break;
77 case Instruction::USHR_LONG:
78 case Instruction::USHR_LONG_2ADDR:
79 op = kOpLsr;
80 break;
81 default:
82 LOG(FATAL) << "Unexpected case: " << opcode;
83 }
Zheng Xue2eb29e2014-06-12 10:22:33 +080084 rl_shift = LoadValue(rl_shift, kCoreReg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010085 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
86 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +080087 OpRegRegReg(op, rl_result.reg, rl_src1.reg, As64BitReg(rl_shift.reg));
Serban Constantinescued65c5e2014-05-22 15:10:18 +010088 StoreValueWide(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +010089}
90
Andreas Gampe90969af2014-07-15 23:02:11 -070091static constexpr bool kUseDeltaEncodingInGenSelect = false;
Andreas Gampe381f8ac2014-07-10 03:23:41 -070092
Andreas Gampe90969af2014-07-15 23:02:11 -070093void Arm64Mir2Lir::GenSelect(int32_t true_val, int32_t false_val, ConditionCode ccode,
94 RegStorage rs_dest, int result_reg_class) {
95 if (false_val == 0 || // 0 is better as first operand.
96 true_val == 1 || // Potentially Csinc.
97 true_val == -1 || // Potentially Csinv.
98 true_val == false_val + 1) { // Potentially Csinc.
99 ccode = NegateComparison(ccode);
100 std::swap(true_val, false_val);
101 }
102
103 ArmConditionCode code = ArmConditionEncoding(ccode);
104
105 int opcode; // The opcode.
106 RegStorage left_op = RegStorage::InvalidReg(); // The operands.
107 RegStorage right_op = RegStorage::InvalidReg(); // The operands.
108
109 bool is_wide = rs_dest.Is64Bit();
110
111 RegStorage zero_reg = is_wide ? rs_xzr : rs_wzr;
112
113 if (true_val == 0) {
114 left_op = zero_reg;
115 } else {
116 left_op = rs_dest;
117 LoadConstantNoClobber(rs_dest, true_val);
118 }
119 if (false_val == 1) {
120 right_op = zero_reg;
121 opcode = kA64Csinc4rrrc;
122 } else if (false_val == -1) {
123 right_op = zero_reg;
124 opcode = kA64Csinv4rrrc;
125 } else if (false_val == true_val + 1) {
126 right_op = left_op;
127 opcode = kA64Csinc4rrrc;
128 } else if (false_val == -true_val) {
129 right_op = left_op;
130 opcode = kA64Csneg4rrrc;
131 } else if (false_val == ~true_val) {
132 right_op = left_op;
133 opcode = kA64Csinv4rrrc;
134 } else if (true_val == 0) {
135 // left_op is zero_reg.
136 right_op = rs_dest;
137 LoadConstantNoClobber(rs_dest, false_val);
138 opcode = kA64Csel4rrrc;
139 } else {
140 // Generic case.
141 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
142 if (is_wide) {
143 if (t_reg2.Is32Bit()) {
144 t_reg2 = As64BitReg(t_reg2);
145 }
146 } else {
147 if (t_reg2.Is64Bit()) {
148 t_reg2 = As32BitReg(t_reg2);
149 }
150 }
151
152 if (kUseDeltaEncodingInGenSelect) {
153 int32_t delta = false_val - true_val;
154 uint32_t abs_val = delta < 0 ? -delta : delta;
155
156 if (abs_val < 0x1000) { // TODO: Replace with InexpensiveConstant with opcode.
157 // Can encode as immediate to an add.
158 right_op = t_reg2;
159 OpRegRegImm(kOpAdd, t_reg2, left_op, delta);
160 }
161 }
162
163 // Load as constant.
164 if (!right_op.Valid()) {
165 LoadConstantNoClobber(t_reg2, false_val);
166 right_op = t_reg2;
167 }
168
169 opcode = kA64Csel4rrrc;
170 }
171
172 DCHECK(left_op.Valid() && right_op.Valid());
173 NewLIR4(is_wide ? WIDE(opcode) : opcode, rs_dest.GetReg(), left_op.GetReg(), right_op.GetReg(),
174 code);
175}
176
177void Arm64Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
178 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700179 RegisterClass dest_reg_class) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700180 DCHECK(rs_dest.Valid());
181 OpRegReg(kOpCmp, left_op, right_op);
182 GenSelect(true_val, false_val, code, rs_dest, dest_reg_class);
183}
184
185void Arm64Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700186 UNUSED(bb);
Andreas Gampe90969af2014-07-15 23:02:11 -0700187 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
188 rl_src = LoadValue(rl_src, rl_src.ref ? kRefReg : kCoreReg);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700189 // rl_src may be aliased with rl_result/rl_dest, so do compare early.
190 OpRegImm(kOpCmp, rl_src.reg, 0);
191
Andreas Gampe90969af2014-07-15 23:02:11 -0700192 RegLocation rl_dest = mir_graph_->GetDest(mir);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100193
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700194 // The kMirOpSelect has two variants, one for constants and one for moves.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700195 if (mir->ssa_rep->num_uses == 1) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700196 RegLocation rl_result = EvalLoc(rl_dest, rl_dest.ref ? kRefReg : kCoreReg, true);
197 GenSelect(mir->dalvikInsn.vB, mir->dalvikInsn.vC, mir->meta.ccode, rl_result.reg,
198 rl_dest.ref ? kRefReg : kCoreReg);
199 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700200 } else {
201 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
202 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
203
Andreas Gampe90969af2014-07-15 23:02:11 -0700204 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700205 rl_true = LoadValue(rl_true, result_reg_class);
206 rl_false = LoadValue(rl_false, result_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700207 RegLocation rl_result = EvalLoc(rl_dest, result_reg_class, true);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700208
Andreas Gampe90969af2014-07-15 23:02:11 -0700209 bool is_wide = rl_dest.ref || rl_dest.wide;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700210 int opcode = is_wide ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc;
211 NewLIR4(opcode, rl_result.reg.GetReg(),
Andreas Gampe90969af2014-07-15 23:02:11 -0700212 rl_true.reg.GetReg(), rl_false.reg.GetReg(), ArmConditionEncoding(mir->meta.ccode));
213 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700214 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100215}
216
217void Arm64Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
218 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
219 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100220 LIR* taken = &block_label_list_[bb->taken];
221 LIR* not_taken = &block_label_list_[bb->fall_through];
Matteo Franchin43ec8732014-03-31 15:00:14 +0100222 // Normalize such that if either operand is constant, src2 will be constant.
223 ConditionCode ccode = mir->meta.ccode;
224 if (rl_src1.is_const) {
225 std::swap(rl_src1, rl_src2);
226 ccode = FlipComparisonOrder(ccode);
227 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100228
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700229 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
230
Matteo Franchin43ec8732014-03-31 15:00:14 +0100231 if (rl_src2.is_const) {
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700232 // TODO: Optimize for rl_src1.is_const? (Does happen in the boot image at the moment.)
233
Matteo Franchin43ec8732014-03-31 15:00:14 +0100234 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100235 // Special handling using cbz & cbnz.
236 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
237 OpCmpImmBranch(ccode, rl_src1.reg, 0, taken);
238 OpCmpImmBranch(NegateComparison(ccode), rl_src1.reg, 0, not_taken);
239 return;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700240 }
241
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100242 // Only handle Imm if src2 is not already in a register.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700243 rl_src2 = UpdateLocWide(rl_src2);
244 if (rl_src2.location != kLocPhysReg) {
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100245 OpRegImm64(kOpCmp, rl_src1.reg, val);
246 OpCondBranch(ccode, taken);
247 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100248 return;
249 }
250 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100251
Matteo Franchin43ec8732014-03-31 15:00:14 +0100252 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100253 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100254 OpCondBranch(ccode, taken);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100255 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100256}
257
258/*
259 * Generate a register comparison to an immediate and branch. Caller
260 * is responsible for setting branch target field.
261 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100262LIR* Arm64Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value,
263 LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700264 LIR* branch = nullptr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100265 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
Andreas Gampe9522af92014-07-14 20:16:59 -0700266 if (check_value == 0) {
267 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100268 A64Opcode opcode = (arm_cond == kArmCondEq) ? kA64Cbz2rt : kA64Cbnz2rt;
269 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700270 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
271 } else if (arm_cond == kArmCondLs) {
272 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
273 // This case happens for a bounds check of array[0].
Matteo Franchin4163c532014-07-15 15:20:27 +0100274 A64Opcode opcode = kA64Cbz2rt;
275 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700276 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800277 } else if (arm_cond == kArmCondLt || arm_cond == kArmCondGe) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100278 A64Opcode opcode = (arm_cond == kArmCondLt) ? kA64Tbnz3rht : kA64Tbz3rht;
279 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800280 int value = reg.Is64Bit() ? 63 : 31;
281 branch = NewLIR3(opcode | wide, reg.GetReg(), value, 0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700282 }
283 }
284
285 if (branch == nullptr) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100286 OpRegImm(kOpCmp, reg, check_value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100287 branch = NewLIR2(kA64B2ct, arm_cond, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100288 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700289
Matteo Franchin43ec8732014-03-31 15:00:14 +0100290 branch->target = target;
291 return branch;
292}
293
Zheng Xu7c1c2632014-06-17 18:17:31 +0800294LIR* Arm64Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg,
295 RegStorage base_reg, int offset, int check_value,
Dave Allison69dfe512014-07-11 17:11:58 +0000296 LIR* target, LIR** compare) {
297 DCHECK(compare == nullptr);
Zheng Xu7c1c2632014-06-17 18:17:31 +0800298 // It is possible that temp register is 64-bit. (ArgReg or RefReg)
299 // Always compare 32-bit value no matter what temp_reg is.
300 if (temp_reg.Is64Bit()) {
301 temp_reg = As32BitReg(temp_reg);
302 }
303 Load32Disp(base_reg, offset, temp_reg);
304 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
305 return branch;
306}
307
Matteo Franchin43ec8732014-03-31 15:00:14 +0100308LIR* Arm64Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100309 bool dest_is_fp = r_dest.IsFloat();
310 bool src_is_fp = r_src.IsFloat();
Matteo Franchin4163c532014-07-15 15:20:27 +0100311 A64Opcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100312 LIR* res;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100313
314 if (LIKELY(dest_is_fp == src_is_fp)) {
315 if (LIKELY(!dest_is_fp)) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700316 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
317
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100318 // Core/core copy.
319 // Copies involving the sp register require a different instruction.
320 opcode = UNLIKELY(A64_REG_IS_SP(r_dest.GetReg())) ? kA64Add4RRdT : kA64Mov2rr;
321
322 // TODO(Arm64): kA64Add4RRdT formally has 4 args, but is used as a 2 args instruction.
323 // This currently works because the other arguments are set to 0 by default. We should
324 // rather introduce an alias kA64Mov2RR.
325
326 // core/core copy. Do a x/x copy only if both registers are x.
327 if (r_dest.Is64Bit() && r_src.Is64Bit()) {
328 opcode = WIDE(opcode);
329 }
330 } else {
331 // Float/float copy.
332 bool dest_is_double = r_dest.IsDouble();
333 bool src_is_double = r_src.IsDouble();
334
335 // We do not do float/double or double/float casts here.
336 DCHECK_EQ(dest_is_double, src_is_double);
337
338 // Homogeneous float/float copy.
Matteo Franchin4163c532014-07-15 15:20:27 +0100339 opcode = (dest_is_double) ? WIDE(kA64Fmov2ff) : kA64Fmov2ff;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100340 }
341 } else {
342 // Inhomogeneous register copy.
343 if (dest_is_fp) {
344 if (r_dest.IsDouble()) {
345 opcode = kA64Fmov2Sx;
346 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700347 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100348 opcode = kA64Fmov2sw;
349 }
350 } else {
351 if (r_src.IsDouble()) {
352 opcode = kA64Fmov2xS;
353 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700354 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100355 opcode = kA64Fmov2ws;
356 }
357 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100358 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100359
Matteo Franchin43ec8732014-03-31 15:00:14 +0100360 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100361
Matteo Franchin43ec8732014-03-31 15:00:14 +0100362 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
363 res->flags.is_nop = true;
364 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100365
Matteo Franchin43ec8732014-03-31 15:00:14 +0100366 return res;
367}
368
369void Arm64Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
370 if (r_dest != r_src) {
371 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
372 AppendLIR(res);
373 }
374}
375
376void Arm64Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100377 OpRegCopy(r_dest, r_src);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100378}
379
380// Table of magic divisors
381struct MagicTable {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100382 int magic64_base;
383 int magic64_eor;
384 uint64_t magic64;
385 uint32_t magic32;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100386 uint32_t shift;
387 DividePattern pattern;
388};
389
390static const MagicTable magic_table[] = {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100391 { 0, 0, 0, 0, 0, DivideNone}, // 0
392 { 0, 0, 0, 0, 0, DivideNone}, // 1
393 { 0, 0, 0, 0, 0, DivideNone}, // 2
394 {0x3c, -1, 0x5555555555555556, 0x55555556, 0, Divide3}, // 3
395 { 0, 0, 0, 0, 0, DivideNone}, // 4
396 {0xf9, -1, 0x6666666666666667, 0x66666667, 1, Divide5}, // 5
397 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 0, Divide3}, // 6
398 { -1, -1, 0x924924924924924A, 0x92492493, 2, Divide7}, // 7
399 { 0, 0, 0, 0, 0, DivideNone}, // 8
400 { -1, -1, 0x38E38E38E38E38E4, 0x38E38E39, 1, Divide5}, // 9
401 {0xf9, -1, 0x6666666666666667, 0x66666667, 2, Divide5}, // 10
402 { -1, -1, 0x2E8BA2E8BA2E8BA3, 0x2E8BA2E9, 1, Divide5}, // 11
403 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 1, Divide5}, // 12
404 { -1, -1, 0x4EC4EC4EC4EC4EC5, 0x4EC4EC4F, 2, Divide5}, // 13
405 { -1, -1, 0x924924924924924A, 0x92492493, 3, Divide7}, // 14
406 {0x78, -1, 0x8888888888888889, 0x88888889, 3, Divide7}, // 15
Matteo Franchin43ec8732014-03-31 15:00:14 +0100407};
408
409// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
410bool Arm64Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100411 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700412 UNUSED(dalvik_opcode);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100413 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100414 return false;
415 }
416 DividePattern pattern = magic_table[lit].pattern;
417 if (pattern == DivideNone) {
418 return false;
419 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100420 // Tuning: add rem patterns
421 if (!is_div) {
422 return false;
423 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100424
425 RegStorage r_magic = AllocTemp();
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100426 LoadConstant(r_magic, magic_table[lit].magic32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100427 rl_src = LoadValue(rl_src, kCoreReg);
428 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100429 RegStorage r_long_mul = AllocTemp();
430 NewLIR4(kA64Smaddl4xwwx, As64BitReg(r_long_mul).GetReg(),
431 r_magic.GetReg(), rl_src.reg.GetReg(), rxzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100432 switch (pattern) {
433 case Divide3:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100434 OpRegRegImm(kOpLsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul), 32);
435 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100436 break;
437 case Divide5:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100438 OpRegRegImm(kOpAsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul),
439 32 + magic_table[lit].shift);
440 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100441 break;
442 case Divide7:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100443 OpRegRegRegShift(kOpAdd, As64BitReg(r_long_mul), As64BitReg(rl_src.reg),
444 As64BitReg(r_long_mul), EncodeShift(kA64Lsr, 32));
445 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
446 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100447 break;
448 default:
449 LOG(FATAL) << "Unexpected pattern: " << pattern;
450 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100451 StoreValue(rl_dest, rl_result);
452 return true;
453}
454
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100455bool Arm64Mir2Lir::SmallLiteralDivRem64(Instruction::Code dalvik_opcode, bool is_div,
456 RegLocation rl_src, RegLocation rl_dest, int64_t lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700457 UNUSED(dalvik_opcode);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100458 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
459 return false;
460 }
461 DividePattern pattern = magic_table[lit].pattern;
462 if (pattern == DivideNone) {
463 return false;
464 }
465 // Tuning: add rem patterns
466 if (!is_div) {
467 return false;
468 }
469
470 RegStorage r_magic = AllocTempWide();
471 rl_src = LoadValueWide(rl_src, kCoreReg);
472 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
473 RegStorage r_long_mul = AllocTempWide();
474
475 if (magic_table[lit].magic64_base >= 0) {
476 // Check that the entry in the table is correct.
477 if (kIsDebugBuild) {
478 uint64_t reconstructed_imm;
479 uint64_t base = DecodeLogicalImmediate(/*is_wide*/true, magic_table[lit].magic64_base);
480 if (magic_table[lit].magic64_eor >= 0) {
481 uint64_t eor = DecodeLogicalImmediate(/*is_wide*/true, magic_table[lit].magic64_eor);
482 reconstructed_imm = base ^ eor;
483 } else {
484 reconstructed_imm = base + 1;
485 }
486 DCHECK_EQ(reconstructed_imm, magic_table[lit].magic64) << " for literal " << lit;
487 }
488
489 // Load the magic constant in two instructions.
490 NewLIR3(WIDE(kA64Orr3Rrl), r_magic.GetReg(), rxzr, magic_table[lit].magic64_base);
491 if (magic_table[lit].magic64_eor >= 0) {
492 NewLIR3(WIDE(kA64Eor3Rrl), r_magic.GetReg(), r_magic.GetReg(),
493 magic_table[lit].magic64_eor);
494 } else {
495 NewLIR4(WIDE(kA64Add4RRdT), r_magic.GetReg(), r_magic.GetReg(), 1, 0);
496 }
497 } else {
498 LoadConstantWide(r_magic, magic_table[lit].magic64);
499 }
500
501 NewLIR3(kA64Smulh3xxx, r_long_mul.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
502 switch (pattern) {
503 case Divide3:
504 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
505 break;
506 case Divide5:
507 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
508 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
509 break;
510 case Divide7:
511 OpRegRegReg(kOpAdd, r_long_mul, rl_src.reg, r_long_mul);
512 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
513 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
514 break;
515 default:
516 LOG(FATAL) << "Unexpected pattern: " << pattern;
517 }
518 StoreValueWide(rl_dest, rl_result);
519 return true;
520}
521
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100522// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
523// and store the result in 'rl_dest'.
524bool Arm64Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
525 RegLocation rl_src, RegLocation rl_dest, int lit) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100526 return HandleEasyDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int>(lit));
527}
528
529// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
530// and store the result in 'rl_dest'.
531bool Arm64Mir2Lir::HandleEasyDivRem64(Instruction::Code dalvik_opcode, bool is_div,
532 RegLocation rl_src, RegLocation rl_dest, int64_t lit) {
533 const bool is_64bit = rl_dest.wide;
534 const int nbits = (is_64bit) ? 64 : 32;
535
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100536 if (lit < 2) {
537 return false;
538 }
539 if (!IsPowerOfTwo(lit)) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100540 if (is_64bit) {
541 return SmallLiteralDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, lit);
542 } else {
543 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int32_t>(lit));
544 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100545 }
546 int k = LowestSetBit(lit);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100547 if (k >= nbits - 2) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100548 // Avoid special cases.
549 return false;
550 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100551
552 RegLocation rl_result;
553 RegStorage t_reg;
554 if (is_64bit) {
555 rl_src = LoadValueWide(rl_src, kCoreReg);
556 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
557 t_reg = AllocTempWide();
558 } else {
559 rl_src = LoadValue(rl_src, kCoreReg);
560 rl_result = EvalLoc(rl_dest, kCoreReg, true);
561 t_reg = AllocTemp();
562 }
563
564 int shift = EncodeShift(kA64Lsr, nbits - k);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100565 if (is_div) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100566 if (lit == 2) {
567 // Division by 2 is by far the most common division by constant.
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100568 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100569 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
570 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100571 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
572 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100573 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
574 }
575 } else {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100576 if (lit == 2) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100577 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
578 OpRegRegImm64(kOpAnd, t_reg, t_reg, lit - 1);
579 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100580 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100581 RegStorage t_reg2 = (is_64bit) ? AllocTempWide() : AllocTemp();
582 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
583 OpRegRegRegShift(kOpAdd, t_reg2, rl_src.reg, t_reg, shift);
584 OpRegRegImm64(kOpAnd, t_reg2, t_reg2, lit - 1);
585 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg2, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100586 }
587 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100588
589 if (is_64bit) {
590 StoreValueWide(rl_dest, rl_result);
591 } else {
592 StoreValue(rl_dest, rl_result);
593 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100594 return true;
595}
596
Matteo Franchin43ec8732014-03-31 15:00:14 +0100597bool Arm64Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700598 UNUSED(rl_src, rl_dest, lit);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100599 LOG(FATAL) << "Unexpected use of EasyMultiply for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700600 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100601}
602
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700603RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
604 bool is_div) {
605 UNUSED(rl_dest, rl_src1, lit, is_div);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100606 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700607 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100608}
609
610RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
611 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
612
613 // Put the literal in a temp.
614 RegStorage lit_temp = AllocTemp();
615 LoadConstant(lit_temp, lit);
616 // Use the generic case for div/rem with arg2 in a register.
617 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
618 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
619 FreeTemp(lit_temp);
620
621 return rl_result;
622}
623
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100624RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700625 RegLocation rl_src2, bool is_div, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100627 LOG(FATAL) << "Unexpected use of GenDivRem for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700628 UNREACHABLE();
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100629}
630
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100631RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage r_src1, RegStorage r_src2,
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100632 bool is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100633 CHECK_EQ(r_src1.Is64Bit(), r_src2.Is64Bit());
634
Matteo Franchin43ec8732014-03-31 15:00:14 +0100635 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
636 if (is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100637 OpRegRegReg(kOpDiv, rl_result.reg, r_src1, r_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100638 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100639 // temp = r_src1 / r_src2
640 // dest = r_src1 - temp * r_src2
641 RegStorage temp;
Matteo Franchin4163c532014-07-15 15:20:27 +0100642 A64Opcode wide;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100643 if (rl_result.reg.Is64Bit()) {
644 temp = AllocTempWide();
645 wide = WIDE(0);
646 } else {
647 temp = AllocTemp();
648 wide = UNWIDE(0);
649 }
650 OpRegRegReg(kOpDiv, temp, r_src1, r_src2);
651 NewLIR4(kA64Msub4rrrr | wide, rl_result.reg.GetReg(), temp.GetReg(),
652 r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100653 FreeTemp(temp);
654 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100655 return rl_result;
656}
657
Martyn Capewell9a8a5062014-08-07 11:31:48 +0100658bool Arm64Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
659 RegLocation rl_src = info->args[0];
660 rl_src = LoadValue(rl_src, kCoreReg);
661 RegLocation rl_dest = InlineTarget(info);
662 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
663
664 // Compare the source value with zero. Write the negated value to the result if
665 // negative, otherwise write the original value.
666 OpRegImm(kOpCmp, rl_src.reg, 0);
667 NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg(),
668 kArmCondPl);
669 StoreValue(rl_dest, rl_result);
670 return true;
671}
672
Serban Constantinescu169489b2014-06-11 16:43:35 +0100673bool Arm64Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
674 RegLocation rl_src = info->args[0];
675 rl_src = LoadValueWide(rl_src, kCoreReg);
676 RegLocation rl_dest = InlineTargetWide(info);
677 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Martyn Capewell9a8a5062014-08-07 11:31:48 +0100678
679 // Compare the source value with zero. Write the negated value to the result if
680 // negative, otherwise write the original value.
681 OpRegImm(kOpCmp, rl_src.reg, 0);
682 NewLIR4(WIDE(kA64Csneg4rrrc), rl_result.reg.GetReg(), rl_src.reg.GetReg(),
683 rl_src.reg.GetReg(), kArmCondPl);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100684 StoreValueWide(rl_dest, rl_result);
685 return true;
686}
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100687
Serban Constantinescu23abec92014-07-02 16:13:38 +0100688bool Arm64Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100689 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100690 RegLocation rl_src1 = info->args[0];
Serban Constantinescu23abec92014-07-02 16:13:38 +0100691 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
692 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
693 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
694 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100695 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
696 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100697 NewLIR4((is_long) ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc, rl_result.reg.GetReg(),
698 rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), (is_min) ? kArmCondLt : kArmCondGt);
699 (is_long) ? StoreValueWide(rl_dest, rl_result) :StoreValue(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100700 return true;
701}
702
703bool Arm64Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
704 RegLocation rl_src_address = info->args[0]; // long address
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100705 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
706 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100707 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100708
Andreas Gampe3c12c512014-06-24 18:46:29 +0000709 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100710 if (size == k64) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100711 StoreValueWide(rl_dest, rl_result);
712 } else {
713 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100714 StoreValue(rl_dest, rl_result);
715 }
716 return true;
717}
718
719bool Arm64Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
720 RegLocation rl_src_address = info->args[0]; // long address
Matteo Franchin43ec8732014-03-31 15:00:14 +0100721 RegLocation rl_src_value = info->args[2]; // [size] value
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100722 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100723
724 RegLocation rl_value;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100725 if (size == k64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100726 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 } else {
728 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100729 rl_value = LoadValue(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100730 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000731 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100732 return true;
733}
734
Matteo Franchin43ec8732014-03-31 15:00:14 +0100735bool Arm64Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100736 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100737 // Unused - RegLocation rl_src_unsafe = info->args[0];
738 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
739 RegLocation rl_src_offset = info->args[2]; // long low
Matteo Franchin43ec8732014-03-31 15:00:14 +0100740 RegLocation rl_src_expected = info->args[4]; // int, long or Object
741 // If is_long, high half is in info->args[5]
742 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
743 // If is_long, high half is in info->args[7]
744 RegLocation rl_dest = InlineTarget(info); // boolean place for result
745
Serban Constantinescu169489b2014-06-11 16:43:35 +0100746 // Load Object and offset
buzbeea0cd2d72014-06-01 09:33:49 -0700747 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100748 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100749
Matteo Franchin43ec8732014-03-31 15:00:14 +0100750 RegLocation rl_new_value;
Serban Constantinescu169489b2014-06-11 16:43:35 +0100751 RegLocation rl_expected;
752 if (is_long) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100753 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100754 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
755 } else {
756 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
757 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100758 }
759
760 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
761 // Mark card for object assuming new value is stored.
762 MarkGCCard(rl_new_value.reg, rl_object.reg);
763 }
764
Serban Constantinescu169489b2014-06-11 16:43:35 +0100765 RegStorage r_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100766 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
767
768 // Free now unneeded rl_object and rl_offset to give more temps.
769 ClobberSReg(rl_object.s_reg_low);
770 FreeTemp(rl_object.reg);
771 ClobberSReg(rl_offset.s_reg_low);
772 FreeTemp(rl_offset.reg);
773
Matteo Franchin43ec8732014-03-31 15:00:14 +0100774 // do {
775 // tmp = [r_ptr] - expected;
776 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
777 // result = tmp != 0;
778
Serban Constantinescu169489b2014-06-11 16:43:35 +0100779 RegStorage r_tmp;
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100780 RegStorage r_tmp_stored;
781 RegStorage rl_new_value_stored = rl_new_value.reg;
Matteo Franchin4163c532014-07-15 15:20:27 +0100782 A64Opcode wide = UNWIDE(0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100783 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100784 r_tmp_stored = r_tmp = AllocTempWide();
785 wide = WIDE(0);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100786 } else if (is_object) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100787 // References use 64-bit registers, but are stored as compressed 32-bit values.
788 // This means r_tmp_stored != r_tmp.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100789 r_tmp = AllocTempRef();
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100790 r_tmp_stored = As32BitReg(r_tmp);
791 rl_new_value_stored = As32BitReg(rl_new_value_stored);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100792 } else {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100793 r_tmp_stored = r_tmp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100794 }
795
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100796 RegStorage r_tmp32 = (r_tmp.Is32Bit()) ? r_tmp : As32BitReg(r_tmp);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100797 LIR* loop = NewLIR0(kPseudoTargetLabel);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100798 NewLIR2(kA64Ldaxr2rX | wide, r_tmp_stored.GetReg(), r_ptr.GetReg());
Serban Constantinescu169489b2014-06-11 16:43:35 +0100799 OpRegReg(kOpCmp, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100800 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Serban Constantinescu169489b2014-06-11 16:43:35 +0100801 LIR* early_exit = OpCondBranch(kCondNe, NULL);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100802 NewLIR3(kA64Stlxr3wrX | wide, r_tmp32.GetReg(), rl_new_value_stored.GetReg(), r_ptr.GetReg());
803 NewLIR3(kA64Cmp3RdT, r_tmp32.GetReg(), 0, ENCODE_NO_SHIFT);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100804 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
805 OpCondBranch(kCondNe, loop);
806
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100807 LIR* exit_loop = NewLIR0(kPseudoTargetLabel);
808 early_exit->target = exit_loop;
809
Serban Constantinescu169489b2014-06-11 16:43:35 +0100810 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100811 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondNe);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100812
Matteo Franchin43ec8732014-03-31 15:00:14 +0100813 FreeTemp(r_tmp); // Now unneeded.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100814 FreeTemp(r_ptr); // Now unneeded.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100815
816 StoreValue(rl_dest, rl_result);
817
Matteo Franchin43ec8732014-03-31 15:00:14 +0100818 return true;
819}
820
Zheng Xu947717a2014-08-07 14:05:23 +0800821bool Arm64Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
822 constexpr int kLargeArrayThreshold = 512;
823
824 RegLocation rl_src = info->args[0];
825 RegLocation rl_src_pos = info->args[1];
826 RegLocation rl_dst = info->args[2];
827 RegLocation rl_dst_pos = info->args[3];
828 RegLocation rl_length = info->args[4];
829 // Compile time check, handle exception by non-inline method to reduce related meta-data.
830 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
831 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
832 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
833 return false;
834 }
835
836 ClobberCallerSave();
837 LockCallTemps(); // Prepare for explicit register usage.
838 RegStorage rs_src = rs_x0;
839 RegStorage rs_dst = rs_x1;
840 LoadValueDirectFixed(rl_src, rs_src);
841 LoadValueDirectFixed(rl_dst, rs_dst);
842
843 // Handle null pointer exception in slow-path.
844 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
845 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
846 // Handle potential overlapping in slow-path.
847 // TUNING: Support overlapping cases.
848 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
849 // Handle exception or big length in slow-path.
850 RegStorage rs_length = rs_w2;
851 LoadValueDirectFixed(rl_length, rs_length);
852 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
853 // Src bounds check.
854 RegStorage rs_src_pos = rs_w3;
855 RegStorage rs_arr_length = rs_w4;
856 LoadValueDirectFixed(rl_src_pos, rs_src_pos);
857 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_src_pos, 0, nullptr);
858 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
859 OpRegReg(kOpSub, rs_arr_length, rs_src_pos);
860 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
861 // Dst bounds check.
862 RegStorage rs_dst_pos = rs_w5;
863 LoadValueDirectFixed(rl_dst_pos, rs_dst_pos);
864 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_dst_pos, 0, nullptr);
865 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
866 OpRegReg(kOpSub, rs_arr_length, rs_dst_pos);
867 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
868
869 // Everything is checked now.
870 // Set rs_src to the address of the first element to be copied.
871 rs_src_pos = As64BitReg(rs_src_pos);
872 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
873 OpRegRegImm(kOpLsl, rs_src_pos, rs_src_pos, 1);
874 OpRegReg(kOpAdd, rs_src, rs_src_pos);
875 // Set rs_src to the address of the first element to be copied.
876 rs_dst_pos = As64BitReg(rs_dst_pos);
877 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
878 OpRegRegImm(kOpLsl, rs_dst_pos, rs_dst_pos, 1);
879 OpRegReg(kOpAdd, rs_dst, rs_dst_pos);
880
881 // rs_arr_length won't be not used anymore.
882 RegStorage rs_tmp = rs_arr_length;
883 // Use 64-bit view since rs_length will be used as index.
884 rs_length = As64BitReg(rs_length);
885 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
886
887 // Copy one element.
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800888 LIR* jmp_to_copy_two = NewLIR3(WIDE(kA64Tbz3rht), rs_length.GetReg(), 1, 0);
Zheng Xu947717a2014-08-07 14:05:23 +0800889 OpRegImm(kOpSub, rs_length, 2);
890 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
891 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
892
893 // Copy two elements.
894 LIR *copy_two = NewLIR0(kPseudoTargetLabel);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800895 LIR* jmp_to_copy_four = NewLIR3(WIDE(kA64Tbz3rht), rs_length.GetReg(), 2, 0);
Zheng Xu947717a2014-08-07 14:05:23 +0800896 OpRegImm(kOpSub, rs_length, 4);
897 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
898 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
899
900 // Copy four elements.
901 LIR *copy_four = NewLIR0(kPseudoTargetLabel);
902 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
903 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
904 OpRegImm(kOpSub, rs_length, 8);
905 rs_tmp = As64BitReg(rs_tmp);
906 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k64);
907 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k64);
908 LIR* jmp_to_loop = OpCmpImmBranch(kCondNe, rs_length, 0, nullptr);
909 LIR* loop_finished = OpUnconditionalBranch(nullptr);
910
911 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
912 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
913 LIR* return_point = NewLIR0(kPseudoTargetLabel);
914
915 src_check_branch->target = check_failed;
916 dst_check_branch->target = check_failed;
917 src_dst_same->target = check_failed;
918 len_neg_or_too_big->target = check_failed;
919 src_pos_negative->target = check_failed;
920 src_bad_len->target = check_failed;
921 dst_pos_negative->target = check_failed;
922 dst_bad_len->target = check_failed;
923 jmp_to_copy_two->target = copy_two;
924 jmp_to_copy_four->target = copy_four;
925 jmp_to_ret->target = return_point;
926 jmp_to_loop->target = begin_loop;
927 loop_finished->target = return_point;
928
929 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700930 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +0800931
932 return true;
933}
934
Matteo Franchin43ec8732014-03-31 15:00:14 +0100935LIR* Arm64Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100936 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100937 return RawLIR(current_dalvik_offset_, kA64Ldr2rp, As32BitReg(reg).GetReg(), 0, 0, 0, 0, target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100938}
939
940LIR* Arm64Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941 UNUSED(r_base, count);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100942 LOG(FATAL) << "Unexpected use of OpVldm for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700943 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100944}
945
946LIR* Arm64Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700947 UNUSED(r_base, count);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100948 LOG(FATAL) << "Unexpected use of OpVstm for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700949 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100950}
951
952void Arm64Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700953 RegLocation rl_result, int lit ATTRIBUTE_UNUSED,
954 int first_bit, int second_bit) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100955 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsl, second_bit - first_bit));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100956 if (first_bit != 0) {
957 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
958 }
959}
960
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700961void Arm64Mir2Lir::GenDivZeroCheckWide(RegStorage reg ATTRIBUTE_UNUSED) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100962 LOG(FATAL) << "Unexpected use of GenDivZero for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100963}
964
965// Test suspend flag, return target of taken suspend branch
966LIR* Arm64Mir2Lir::OpTestSuspend(LIR* target) {
Zheng Xubaa7c882014-06-30 14:26:50 +0800967 NewLIR3(kA64Subs3rRd, rwSUSPEND, rwSUSPEND, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100968 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
969}
970
971// Decrement register and branch on condition
972LIR* Arm64Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -0700973 // Combine sub & test using sub setflags encoding here. We need to make sure a
974 // subtract form that sets carry is used, so generate explicitly.
975 // TODO: might be best to add a new op, kOpSubs, and handle it generically.
Matteo Franchin4163c532014-07-15 15:20:27 +0100976 A64Opcode opcode = reg.Is64Bit() ? WIDE(kA64Subs3rRd) : UNWIDE(kA64Subs3rRd);
buzbee33ae5582014-06-12 14:56:32 -0700977 NewLIR3(opcode, reg.GetReg(), reg.GetReg(), 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100978 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100979 return OpCondBranch(c_code, target);
980}
981
Andreas Gampeb14329f2014-05-15 11:16:06 -0700982bool Arm64Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100983#if ANDROID_SMP != 0
984 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
985 LIR* barrier = last_lir_insn_;
986
987 int dmb_flavor;
988 // TODO: revisit Arm barrier kinds
989 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700990 case kAnyStore: dmb_flavor = kISH; break;
991 case kLoadAny: dmb_flavor = kISH; break;
992 // We conjecture that kISHLD is insufficient. It is documented
993 // to provide LoadLoad | StoreStore ordering. But if this were used
994 // to implement volatile loads, we suspect that the lack of store
995 // atomicity on ARM would cause us to allow incorrect results for
996 // the canonical IRIW example. But we're not sure.
997 // We should be using acquire loads instead.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100998 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -0700999 case kAnyAny: dmb_flavor = kISH; break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001000 default:
1001 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1002 dmb_flavor = kSY; // quiet gcc.
1003 break;
1004 }
1005
Andreas Gampeb14329f2014-05-15 11:16:06 -07001006 bool ret = false;
1007
Matteo Franchin43ec8732014-03-31 15:00:14 +01001008 // If the same barrier already exists, don't generate another.
1009 if (barrier == nullptr
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001010 || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) {
1011 barrier = NewLIR1(kA64Dmb1B, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001012 ret = true;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001013 }
1014
1015 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1016 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001017 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001018 return ret;
1019#else
1020 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001021#endif
1022}
1023
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001024void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
1025 RegLocation rl_result;
1026
1027 rl_src = LoadValue(rl_src, kCoreReg);
1028 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001030 StoreValueWide(rl_dest, rl_result);
1031}
1032
1033void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001034 RegLocation rl_src1, RegLocation rl_src2, bool is_div, int flags) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +01001035 if (rl_src2.is_const) {
1036 DCHECK(rl_src2.wide);
1037 int64_t lit = mir_graph_->ConstantValueWide(rl_src2);
1038 if (HandleEasyDivRem64(opcode, is_div, rl_src1, rl_dest, lit)) {
1039 return;
1040 }
1041 }
1042
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001043 RegLocation rl_result;
1044 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1045 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001046 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
1047 GenDivZeroCheck(rl_src2.reg);
1048 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001049 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001050 StoreValueWide(rl_dest, rl_result);
1051}
1052
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001053void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1,
1054 RegLocation rl_src2) {
1055 RegLocation rl_result;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001056
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001057 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1058 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1059 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001060 OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT);
1061 StoreValueWide(rl_dest, rl_result);
1062}
1063
1064void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
1065 RegLocation rl_result;
1066
1067 rl_src = LoadValueWide(rl_src, kCoreReg);
1068 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1069 OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
1070 StoreValueWide(rl_dest, rl_result);
1071}
1072
1073void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
1074 RegLocation rl_result;
1075
1076 rl_src = LoadValueWide(rl_src, kCoreReg);
1077 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1078 OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001079 StoreValueWide(rl_dest, rl_result);
1080}
1081
Andreas Gampec76c6142014-08-04 16:30:03 -07001082void Arm64Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001083 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001084 switch (opcode) {
1085 case Instruction::NOT_LONG:
1086 GenNotLong(rl_dest, rl_src2);
1087 return;
1088 case Instruction::ADD_LONG:
1089 case Instruction::ADD_LONG_2ADDR:
1090 GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2);
1091 return;
1092 case Instruction::SUB_LONG:
1093 case Instruction::SUB_LONG_2ADDR:
1094 GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2);
1095 return;
1096 case Instruction::MUL_LONG:
1097 case Instruction::MUL_LONG_2ADDR:
1098 GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2);
1099 return;
1100 case Instruction::DIV_LONG:
1101 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001102 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001103 return;
1104 case Instruction::REM_LONG:
1105 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001106 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001107 return;
1108 case Instruction::AND_LONG_2ADDR:
1109 case Instruction::AND_LONG:
1110 GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2);
1111 return;
1112 case Instruction::OR_LONG:
1113 case Instruction::OR_LONG_2ADDR:
1114 GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2);
1115 return;
1116 case Instruction::XOR_LONG:
1117 case Instruction::XOR_LONG_2ADDR:
1118 GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2);
1119 return;
1120 case Instruction::NEG_LONG: {
1121 GenNegLong(rl_dest, rl_src2);
1122 return;
1123 }
1124 default:
1125 LOG(FATAL) << "Invalid long arith op";
1126 return;
1127 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001128}
1129
1130/*
1131 * Generate array load
1132 */
1133void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
1134 RegLocation rl_index, RegLocation rl_dest, int scale) {
1135 RegisterClass reg_class = RegClassBySize(size);
1136 int len_offset = mirror::Array::LengthOffset().Int32Value();
1137 int data_offset;
1138 RegLocation rl_result;
1139 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001140 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001141 if (!constant_index) {
1142 rl_index = LoadValue(rl_index, kCoreReg);
1143 }
1144
1145 if (rl_dest.wide) {
1146 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1147 } else {
1148 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1149 }
1150
1151 // If index is constant, just fold it into the data offset
1152 if (constant_index) {
1153 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1154 }
1155
1156 /* null object? */
1157 GenNullCheck(rl_array.reg, opt_flags);
1158
1159 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1160 RegStorage reg_len;
1161 if (needs_range_check) {
1162 reg_len = AllocTemp();
1163 /* Get len */
1164 Load32Disp(rl_array.reg, len_offset, reg_len);
1165 MarkPossibleNullPointerException(opt_flags);
1166 } else {
1167 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1168 }
1169 if (rl_dest.wide || rl_dest.fp || constant_index) {
1170 RegStorage reg_ptr;
1171 if (constant_index) {
1172 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
1173 } else {
1174 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001175 reg_ptr = AllocTempRef();
buzbee33ae5582014-06-12 14:56:32 -07001176 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1177 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001178 FreeTemp(rl_index.reg);
1179 }
1180 rl_result = EvalLoc(rl_dest, reg_class, true);
1181
1182 if (needs_range_check) {
1183 if (constant_index) {
1184 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1185 } else {
1186 GenArrayBoundsCheck(rl_index.reg, reg_len);
1187 }
1188 FreeTemp(reg_len);
1189 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001190 if (rl_result.ref) {
1191 LoadRefDisp(reg_ptr, data_offset, rl_result.reg, kNotVolatile);
1192 } else {
1193 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
1194 }
Vladimir Marko455759b2014-05-06 20:49:36 +01001195 if (!constant_index) {
1196 FreeTemp(reg_ptr);
1197 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001198 if (rl_dest.wide) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001199 StoreValueWide(rl_dest, rl_result);
1200 } else {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001201 StoreValue(rl_dest, rl_result);
1202 }
1203 } else {
1204 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001205 RegStorage reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001206 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1207 FreeTemp(rl_array.reg);
1208 rl_result = EvalLoc(rl_dest, reg_class, true);
1209
1210 if (needs_range_check) {
1211 GenArrayBoundsCheck(rl_index.reg, reg_len);
1212 FreeTemp(reg_len);
1213 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001214 if (rl_result.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001215 LoadRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001216 } else {
1217 LoadBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale, size);
1218 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001219 FreeTemp(reg_ptr);
1220 StoreValue(rl_dest, rl_result);
1221 }
1222}
1223
1224/*
1225 * Generate array store
1226 *
1227 */
1228void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
1229 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
1230 RegisterClass reg_class = RegClassBySize(size);
1231 int len_offset = mirror::Array::LengthOffset().Int32Value();
1232 bool constant_index = rl_index.is_const;
1233
1234 int data_offset;
1235 if (size == k64 || size == kDouble) {
1236 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1237 } else {
1238 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1239 }
1240
1241 // If index is constant, just fold it into the data offset.
1242 if (constant_index) {
1243 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1244 }
1245
buzbeea0cd2d72014-06-01 09:33:49 -07001246 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001247 if (!constant_index) {
1248 rl_index = LoadValue(rl_index, kCoreReg);
1249 }
1250
1251 RegStorage reg_ptr;
1252 bool allocated_reg_ptr_temp = false;
1253 if (constant_index) {
1254 reg_ptr = rl_array.reg;
1255 } else if (IsTemp(rl_array.reg) && !card_mark) {
1256 Clobber(rl_array.reg);
1257 reg_ptr = rl_array.reg;
1258 } else {
1259 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001260 reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001261 }
1262
1263 /* null object? */
1264 GenNullCheck(rl_array.reg, opt_flags);
1265
1266 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1267 RegStorage reg_len;
1268 if (needs_range_check) {
1269 reg_len = AllocTemp();
1270 // NOTE: max live temps(4) here.
1271 /* Get len */
1272 Load32Disp(rl_array.reg, len_offset, reg_len);
1273 MarkPossibleNullPointerException(opt_flags);
1274 } else {
1275 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1276 }
1277 /* at this point, reg_ptr points to array, 2 live temps */
1278 if (rl_src.wide || rl_src.fp || constant_index) {
1279 if (rl_src.wide) {
1280 rl_src = LoadValueWide(rl_src, reg_class);
1281 } else {
1282 rl_src = LoadValue(rl_src, reg_class);
1283 }
1284 if (!constant_index) {
buzbee33ae5582014-06-12 14:56:32 -07001285 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1286 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001287 }
1288 if (needs_range_check) {
1289 if (constant_index) {
1290 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1291 } else {
1292 GenArrayBoundsCheck(rl_index.reg, reg_len);
1293 }
1294 FreeTemp(reg_len);
1295 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001296 if (rl_src.ref) {
1297 StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile);
1298 } else {
1299 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
1300 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001301 } else {
1302 /* reg_ptr -> array data */
1303 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1304 rl_src = LoadValue(rl_src, reg_class);
1305 if (needs_range_check) {
1306 GenArrayBoundsCheck(rl_index.reg, reg_len);
1307 FreeTemp(reg_len);
1308 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001309 if (rl_src.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001310 StoreRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001311 } else {
1312 StoreBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale, size);
1313 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001314 }
1315 if (allocated_reg_ptr_temp) {
1316 FreeTemp(reg_ptr);
1317 }
1318 if (card_mark) {
1319 MarkGCCard(rl_src.reg, rl_array.reg);
1320 }
1321}
1322
Matteo Franchin43ec8732014-03-31 15:00:14 +01001323void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001324 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001325 int flags ATTRIBUTE_UNUSED) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001326 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001327 // Per spec, we only care about low 6 bits of shift amount.
1328 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001329 rl_src = LoadValueWide(rl_src, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001330 if (shift_amount == 0) {
1331 StoreValueWide(rl_dest, rl_src);
1332 return;
1333 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001334
1335 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001336 switch (opcode) {
1337 case Instruction::SHL_LONG:
1338 case Instruction::SHL_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001339 op = kOpLsl;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001340 break;
1341 case Instruction::SHR_LONG:
1342 case Instruction::SHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001343 op = kOpAsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001344 break;
1345 case Instruction::USHR_LONG:
1346 case Instruction::USHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001347 op = kOpLsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001348 break;
1349 default:
1350 LOG(FATAL) << "Unexpected case";
1351 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001352 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001353 StoreValueWide(rl_dest, rl_result);
1354}
1355
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001356void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001357 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001358 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001359 switch (opcode) {
1360 case Instruction::ADD_LONG:
1361 case Instruction::ADD_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001362 op = kOpAdd;
1363 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001364 case Instruction::SUB_LONG:
1365 case Instruction::SUB_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001366 op = kOpSub;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001367 break;
1368 case Instruction::AND_LONG:
1369 case Instruction::AND_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001370 op = kOpAnd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001371 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001372 case Instruction::OR_LONG:
1373 case Instruction::OR_LONG_2ADDR:
1374 op = kOpOr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001375 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001376 case Instruction::XOR_LONG:
1377 case Instruction::XOR_LONG_2ADDR:
1378 op = kOpXor;
1379 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001380 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001381 LOG(FATAL) << "Unexpected opcode";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001382 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001383
Matteo Franchinc763e352014-07-04 12:53:27 +01001384 if (op == kOpSub) {
1385 if (!rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001386 return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Matteo Franchinc763e352014-07-04 12:53:27 +01001387 }
1388 } else {
1389 // Associativity.
1390 if (!rl_src2.is_const) {
1391 DCHECK(rl_src1.is_const);
1392 std::swap(rl_src1, rl_src2);
1393 }
1394 }
1395 DCHECK(rl_src2.is_const);
1396 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1397
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001398 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1399 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +08001400 OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001401 StoreValueWide(rl_dest, rl_result);
1402}
1403
Andreas Gampef29ecd62014-07-29 00:35:00 -07001404static uint32_t ExtractReg(uint32_t reg_mask, int* reg) {
1405 // Find first register.
1406 int first_bit_set = CTZ(reg_mask) + 1;
1407 *reg = *reg + first_bit_set;
1408 reg_mask >>= first_bit_set;
1409 return reg_mask;
1410}
1411
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001412/**
1413 * @brief Split a register list in pairs or registers.
1414 *
1415 * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows:
1416 * @code
1417 * int reg1 = -1, reg2 = -1;
1418 * while (reg_mask) {
1419 * reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1420 * if (UNLIKELY(reg2 < 0)) {
1421 * // Single register in reg1.
1422 * } else {
1423 * // Pair in reg1, reg2.
1424 * }
1425 * }
1426 * @endcode
1427 */
Andreas Gampef29ecd62014-07-29 00:35:00 -07001428static uint32_t GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001429 // Find first register.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001430 int first_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001431 int reg = *reg1 + first_bit_set;
1432 reg_mask >>= first_bit_set;
1433
1434 if (LIKELY(reg_mask)) {
1435 // Save the first register, find the second and use the pair opcode.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001436 int second_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001437 *reg2 = reg;
1438 reg_mask >>= second_bit_set;
1439 *reg1 = reg + second_bit_set;
1440 return reg_mask;
1441 }
1442
1443 // Use the single opcode, as we just have one register.
1444 *reg1 = reg;
1445 *reg2 = -1;
1446 return reg_mask;
1447}
1448
Andreas Gampef29ecd62014-07-29 00:35:00 -07001449static void SpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001450 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001451 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001452
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001453 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001454 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1455 if (UNLIKELY(reg2 < 0)) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001456 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001457 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001458 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1459 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001460 }
1461 }
1462}
1463
1464// TODO(Arm64): consider using ld1 and st1?
Andreas Gampef29ecd62014-07-29 00:35:00 -07001465static void SpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001466 int reg1 = -1, reg2 = -1;
1467 const int reg_log2_size = 3;
1468
1469 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1470 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1471 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001472 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001473 offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001474 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001475 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1476 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001477 }
1478 }
1479}
1480
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001481static int SpillRegsPreSub(Arm64Mir2Lir* m2l, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1482 int frame_size) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001483 m2l->OpRegRegImm(kOpSub, rs_sp, rs_sp, frame_size);
1484
1485 int core_count = POPCOUNT(core_reg_mask);
1486
1487 if (fp_reg_mask != 0) {
1488 // Spill FP regs.
1489 int fp_count = POPCOUNT(fp_reg_mask);
1490 int spill_offset = frame_size - (core_count + fp_count) * kArm64PointerSize;
1491 SpillFPRegs(m2l, rs_sp, spill_offset, fp_reg_mask);
1492 }
1493
1494 if (core_reg_mask != 0) {
1495 // Spill core regs.
1496 int spill_offset = frame_size - (core_count * kArm64PointerSize);
1497 SpillCoreRegs(m2l, rs_sp, spill_offset, core_reg_mask);
1498 }
1499
1500 return frame_size;
1501}
1502
1503static int SpillRegsPreIndexed(Arm64Mir2Lir* m2l, RegStorage base, uint32_t core_reg_mask,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001504 uint32_t fp_reg_mask) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001505 // Otherwise, spill both core and fp regs at the same time.
1506 // The very first instruction will be an stp with pre-indexed address, moving the stack pointer
1507 // down. From then on, we fill upwards. This will generate overall the same number of instructions
1508 // as the specialized code above in most cases (exception being odd number of core and even
1509 // non-zero fp spills), but is more flexible, as the offsets are guaranteed small.
1510 //
1511 // Some demonstrative fill cases : (c) = core, (f) = fp
1512 // cc 44 cc 44 cc 22 cc 33 fc => 1[1/2]
1513 // fc => 23 fc => 23 ff => 11 ff => 22
1514 // ff 11 f 11 f 11
1515 //
1516 int reg1 = -1, reg2 = -1;
1517 int core_count = POPCOUNT(core_reg_mask);
1518 int fp_count = POPCOUNT(fp_reg_mask);
1519
1520 int combined = fp_count + core_count;
1521 int all_offset = RoundUp(combined, 2); // Needs to be 16B = 2-reg aligned.
1522
1523 int cur_offset = 2; // What's the starting offset after the first stp? We expect the base slot
1524 // to be filled.
1525
1526 // First figure out whether the bottom is FP or core.
1527 if (fp_count > 0) {
1528 // Some FP spills.
1529 //
1530 // Four cases: (d0 is dummy to fill up stp)
1531 // 1) Single FP, even number of core -> stp d0, fp_reg
1532 // 2) Single FP, odd number of core -> stp fp_reg, d0
1533 // 3) More FP, even number combined -> stp fp_reg1, fp_reg2
1534 // 4) More FP, odd number combined -> stp d0, fp_reg
1535 if (fp_count == 1) {
1536 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1537 DCHECK_EQ(fp_reg_mask, 0U);
1538 if (core_count % 2 == 0) {
1539 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1540 RegStorage::FloatSolo64(reg1).GetReg(),
1541 RegStorage::FloatSolo64(reg1).GetReg(),
1542 base.GetReg(), -all_offset);
1543 } else {
1544 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1545 RegStorage::FloatSolo64(reg1).GetReg(),
1546 RegStorage::FloatSolo64(reg1).GetReg(),
1547 base.GetReg(), -all_offset);
1548 cur_offset = 0; // That core reg needs to go into the upper half.
1549 }
1550 } else {
1551 if (combined % 2 == 0) {
1552 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1553 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1554 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), -all_offset);
1555 } else {
1556 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1557 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), rs_d0.GetReg(), RegStorage::FloatSolo64(reg1).GetReg(),
1558 base.GetReg(), -all_offset);
1559 }
1560 }
1561 } else {
1562 // No FP spills.
1563 //
1564 // Two cases:
1565 // 1) Even number of core -> stp core1, core2
1566 // 2) Odd number of core -> stp xzr, core1
1567 if (core_count % 2 == 1) {
1568 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1569 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), rs_xzr.GetReg(),
1570 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1571 } else {
1572 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1573 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), RegStorage::Solo64(reg2).GetReg(),
1574 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1575 }
1576 }
1577
1578 if (fp_count != 0) {
1579 for (; fp_reg_mask != 0;) {
1580 // Have some FP regs to do.
1581 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1582 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001583 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001584 cur_offset);
1585 // Do not increment offset here, as the second half will be filled by a core reg.
1586 } else {
1587 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1588 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), cur_offset);
1589 cur_offset += 2;
1590 }
1591 }
1592
1593 // Reset counting.
1594 reg1 = -1;
1595
1596 // If there is an odd number of core registers, we need to store the bottom now.
1597 if (core_count % 2 == 1) {
1598 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1599 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(),
1600 cur_offset + 1);
1601 cur_offset += 2; // Half-slot filled now.
1602 }
1603 }
1604
1605 // Spill the rest of the core regs. They are guaranteed to be even.
1606 DCHECK_EQ(POPCOUNT(core_reg_mask) % 2, 0);
1607 for (; core_reg_mask != 0; cur_offset += 2) {
1608 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1609 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1610 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), cur_offset);
1611 }
1612
1613 DCHECK_EQ(cur_offset, all_offset);
1614
1615 return all_offset * 8;
1616}
1617
1618int Arm64Mir2Lir::SpillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1619 int frame_size) {
1620 // If the frame size is small enough that all offsets would fit into the immediates, use that
1621 // setup, as it decrements sp early (kind of instruction scheduling), and is not worse
1622 // instruction-count wise than the complicated code below.
1623 //
1624 // This case is also optimal when we have an odd number of core spills, and an even (non-zero)
1625 // number of fp spills.
1626 if ((RoundUp(frame_size, 8) / 8 <= 63)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001627 return SpillRegsPreSub(this, core_reg_mask, fp_reg_mask, frame_size);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001628 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001629 return SpillRegsPreIndexed(this, base, core_reg_mask, fp_reg_mask);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001630 }
1631}
1632
1633static void UnSpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1634 int reg1 = -1, reg2 = -1;
1635 const int reg_log2_size = 3;
1636
1637 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1638 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1639 if (UNLIKELY(reg2 < 0)) {
1640 m2l->NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1641 } else {
1642 DCHECK_LE(offset, 63);
1643 m2l->NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1644 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1645 }
1646 }
1647}
1648
1649static void UnSpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1650 int reg1 = -1, reg2 = -1;
1651 const int reg_log2_size = 3;
1652
1653 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1654 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1655 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001656 m2l->NewLIR3(WIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001657 offset);
1658 } else {
1659 m2l->NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1660 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1661 }
1662 }
1663}
1664
1665void Arm64Mir2Lir::UnspillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1666 int frame_size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001667 DCHECK(base == rs_sp);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001668 // Restore saves and drop stack frame.
1669 // 2 versions:
1670 //
1671 // 1. (Original): Try to address directly, then drop the whole frame.
1672 // Limitation: ldp is a 7b signed immediate.
1673 //
1674 // 2. (New): Drop the non-save-part. Then do similar to original, which is now guaranteed to be
1675 // in range. Then drop the rest.
1676 //
1677 // TODO: In methods with few spills but huge frame, it would be better to do non-immediate loads
1678 // in variant 1.
1679
1680 // "Magic" constant, 63 (max signed 7b) * 8.
1681 static constexpr int kMaxFramesizeForOffset = 63 * kArm64PointerSize;
1682
1683 const int num_core_spills = POPCOUNT(core_reg_mask);
1684 const int num_fp_spills = POPCOUNT(fp_reg_mask);
1685
1686 int early_drop = 0;
1687
1688 if (frame_size > kMaxFramesizeForOffset) {
1689 // Second variant. Drop the frame part.
1690
1691 // TODO: Always use the first formula, as num_fp_spills would be zero?
1692 if (fp_reg_mask != 0) {
1693 early_drop = frame_size - kArm64PointerSize * (num_fp_spills + num_core_spills);
1694 } else {
1695 early_drop = frame_size - kArm64PointerSize * num_core_spills;
1696 }
1697
1698 // Drop needs to be 16B aligned, so that SP keeps aligned.
1699 early_drop = RoundDown(early_drop, 16);
1700
1701 OpRegImm64(kOpAdd, rs_sp, early_drop);
1702 }
1703
1704 // Unspill.
1705 if (fp_reg_mask != 0) {
1706 int offset = frame_size - early_drop - kArm64PointerSize * (num_fp_spills + num_core_spills);
1707 UnSpillFPRegs(this, rs_sp, offset, fp_reg_mask);
1708 }
1709 if (core_reg_mask != 0) {
1710 int offset = frame_size - early_drop - kArm64PointerSize * num_core_spills;
1711 UnSpillCoreRegs(this, rs_sp, offset, core_reg_mask);
1712 }
1713
1714 // Drop the (rest of) the frame.
1715 OpRegImm64(kOpAdd, rs_sp, frame_size - early_drop);
1716}
1717
Serban Constantinescu23abec92014-07-02 16:13:38 +01001718bool Arm64Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001719 A64Opcode wide = IsWide(size) ? WIDE(0) : UNWIDE(0);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001720 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001721 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Serban Constantinescu23abec92014-07-02 16:13:38 +01001722 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001723 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001724 NewLIR2(kA64Rbit2rr | wide, rl_result.reg.GetReg(), rl_i.reg.GetReg());
Fred Shih37f05ef2014-07-16 18:38:08 -07001725 IsWide(size) ? StoreValueWide(rl_dest, rl_result) : StoreValue(rl_dest, rl_result);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001726 return true;
1727}
1728
Matteo Franchin43ec8732014-03-31 15:00:14 +01001729} // namespace art