blob: fc72e02c55e690afaa3cf3aaca9cf780adf41f76 [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 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100486 }
487
488 // Load the magic constant in two instructions.
489 NewLIR3(WIDE(kA64Orr3Rrl), r_magic.GetReg(), rxzr, magic_table[lit].magic64_base);
490 if (magic_table[lit].magic64_eor >= 0) {
491 NewLIR3(WIDE(kA64Eor3Rrl), r_magic.GetReg(), r_magic.GetReg(),
492 magic_table[lit].magic64_eor);
493 } else {
494 NewLIR4(WIDE(kA64Add4RRdT), r_magic.GetReg(), r_magic.GetReg(), 1, 0);
495 }
496 } else {
497 LoadConstantWide(r_magic, magic_table[lit].magic64);
498 }
499
500 NewLIR3(kA64Smulh3xxx, r_long_mul.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
501 switch (pattern) {
502 case Divide3:
503 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
504 break;
505 case Divide5:
506 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
507 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
508 break;
509 case Divide7:
510 OpRegRegReg(kOpAdd, r_long_mul, rl_src.reg, r_long_mul);
511 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
512 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
513 break;
514 default:
515 LOG(FATAL) << "Unexpected pattern: " << pattern;
516 }
517 StoreValueWide(rl_dest, rl_result);
518 return true;
519}
520
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100521// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
522// and store the result in 'rl_dest'.
523bool Arm64Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
524 RegLocation rl_src, RegLocation rl_dest, int lit) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100525 return HandleEasyDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int>(lit));
526}
527
528// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
529// and store the result in 'rl_dest'.
530bool Arm64Mir2Lir::HandleEasyDivRem64(Instruction::Code dalvik_opcode, bool is_div,
531 RegLocation rl_src, RegLocation rl_dest, int64_t lit) {
532 const bool is_64bit = rl_dest.wide;
533 const int nbits = (is_64bit) ? 64 : 32;
534
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100535 if (lit < 2) {
536 return false;
537 }
538 if (!IsPowerOfTwo(lit)) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100539 if (is_64bit) {
540 return SmallLiteralDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, lit);
541 } else {
542 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int32_t>(lit));
543 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100544 }
545 int k = LowestSetBit(lit);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100546 if (k >= nbits - 2) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100547 // Avoid special cases.
548 return false;
549 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100550
551 RegLocation rl_result;
552 RegStorage t_reg;
553 if (is_64bit) {
554 rl_src = LoadValueWide(rl_src, kCoreReg);
555 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
556 t_reg = AllocTempWide();
557 } else {
558 rl_src = LoadValue(rl_src, kCoreReg);
559 rl_result = EvalLoc(rl_dest, kCoreReg, true);
560 t_reg = AllocTemp();
561 }
562
563 int shift = EncodeShift(kA64Lsr, nbits - k);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100564 if (is_div) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100565 if (lit == 2) {
566 // Division by 2 is by far the most common division by constant.
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100567 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100568 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
569 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100570 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
571 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100572 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
573 }
574 } else {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100575 if (lit == 2) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100576 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
577 OpRegRegImm64(kOpAnd, t_reg, t_reg, lit - 1);
578 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100579 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100580 RegStorage t_reg2 = (is_64bit) ? AllocTempWide() : AllocTemp();
581 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
582 OpRegRegRegShift(kOpAdd, t_reg2, rl_src.reg, t_reg, shift);
583 OpRegRegImm64(kOpAnd, t_reg2, t_reg2, lit - 1);
584 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg2, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100585 }
586 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100587
588 if (is_64bit) {
589 StoreValueWide(rl_dest, rl_result);
590 } else {
591 StoreValue(rl_dest, rl_result);
592 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100593 return true;
594}
595
Matteo Franchin43ec8732014-03-31 15:00:14 +0100596bool Arm64Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700597 UNUSED(rl_src, rl_dest, lit);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100598 LOG(FATAL) << "Unexpected use of EasyMultiply for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700599 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100600}
601
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700602RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit,
603 bool is_div) {
604 UNUSED(rl_dest, rl_src1, lit, is_div);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100605 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700606 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100607}
608
609RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
610 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
611
612 // Put the literal in a temp.
613 RegStorage lit_temp = AllocTemp();
614 LoadConstant(lit_temp, lit);
615 // Use the generic case for div/rem with arg2 in a register.
616 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
617 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
618 FreeTemp(lit_temp);
619
620 return rl_result;
621}
622
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100623RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -0700624 RegLocation rl_src2, bool is_div, int flags) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700625 UNUSED(rl_dest, rl_src1, rl_src2, is_div, flags);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100626 LOG(FATAL) << "Unexpected use of GenDivRem for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700627 UNREACHABLE();
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100628}
629
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100630RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage r_src1, RegStorage r_src2,
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100631 bool is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100632 CHECK_EQ(r_src1.Is64Bit(), r_src2.Is64Bit());
633
Matteo Franchin43ec8732014-03-31 15:00:14 +0100634 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
635 if (is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100636 OpRegRegReg(kOpDiv, rl_result.reg, r_src1, r_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100637 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100638 // temp = r_src1 / r_src2
639 // dest = r_src1 - temp * r_src2
640 RegStorage temp;
Matteo Franchin4163c532014-07-15 15:20:27 +0100641 A64Opcode wide;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100642 if (rl_result.reg.Is64Bit()) {
643 temp = AllocTempWide();
644 wide = WIDE(0);
645 } else {
646 temp = AllocTemp();
647 wide = UNWIDE(0);
648 }
649 OpRegRegReg(kOpDiv, temp, r_src1, r_src2);
650 NewLIR4(kA64Msub4rrrr | wide, rl_result.reg.GetReg(), temp.GetReg(),
651 r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100652 FreeTemp(temp);
653 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100654 return rl_result;
655}
656
Martyn Capewell9a8a5062014-08-07 11:31:48 +0100657bool Arm64Mir2Lir::GenInlinedAbsInt(CallInfo* info) {
658 RegLocation rl_src = info->args[0];
659 rl_src = LoadValue(rl_src, kCoreReg);
660 RegLocation rl_dest = InlineTarget(info);
661 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
662
663 // Compare the source value with zero. Write the negated value to the result if
664 // negative, otherwise write the original value.
665 OpRegImm(kOpCmp, rl_src.reg, 0);
666 NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_src.reg.GetReg(), rl_src.reg.GetReg(),
667 kArmCondPl);
668 StoreValue(rl_dest, rl_result);
669 return true;
670}
671
Serban Constantinescu169489b2014-06-11 16:43:35 +0100672bool Arm64Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
673 RegLocation rl_src = info->args[0];
674 rl_src = LoadValueWide(rl_src, kCoreReg);
675 RegLocation rl_dest = InlineTargetWide(info);
676 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Martyn Capewell9a8a5062014-08-07 11:31:48 +0100677
678 // Compare the source value with zero. Write the negated value to the result if
679 // negative, otherwise write the original value.
680 OpRegImm(kOpCmp, rl_src.reg, 0);
681 NewLIR4(WIDE(kA64Csneg4rrrc), rl_result.reg.GetReg(), rl_src.reg.GetReg(),
682 rl_src.reg.GetReg(), kArmCondPl);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100683 StoreValueWide(rl_dest, rl_result);
684 return true;
685}
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100686
Serban Constantinescu23abec92014-07-02 16:13:38 +0100687bool Arm64Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100688 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100689 RegLocation rl_src1 = info->args[0];
Serban Constantinescu23abec92014-07-02 16:13:38 +0100690 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
691 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
692 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
693 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100694 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
695 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100696 NewLIR4((is_long) ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc, rl_result.reg.GetReg(),
697 rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), (is_min) ? kArmCondLt : kArmCondGt);
698 (is_long) ? StoreValueWide(rl_dest, rl_result) :StoreValue(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100699 return true;
700}
701
702bool Arm64Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
703 RegLocation rl_src_address = info->args[0]; // long address
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100704 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
705 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100706 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100707
Andreas Gampe3c12c512014-06-24 18:46:29 +0000708 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100709 if (size == k64) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100710 StoreValueWide(rl_dest, rl_result);
711 } else {
712 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100713 StoreValue(rl_dest, rl_result);
714 }
715 return true;
716}
717
718bool Arm64Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
719 RegLocation rl_src_address = info->args[0]; // long address
Matteo Franchin43ec8732014-03-31 15:00:14 +0100720 RegLocation rl_src_value = info->args[2]; // [size] value
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100721 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100722
723 RegLocation rl_value;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100724 if (size == k64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100725 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100726 } else {
727 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100728 rl_value = LoadValue(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100729 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000730 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100731 return true;
732}
733
Matteo Franchin43ec8732014-03-31 15:00:14 +0100734bool Arm64Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100735 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100736 // Unused - RegLocation rl_src_unsafe = info->args[0];
737 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
738 RegLocation rl_src_offset = info->args[2]; // long low
Matteo Franchin43ec8732014-03-31 15:00:14 +0100739 RegLocation rl_src_expected = info->args[4]; // int, long or Object
740 // If is_long, high half is in info->args[5]
741 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
742 // If is_long, high half is in info->args[7]
743 RegLocation rl_dest = InlineTarget(info); // boolean place for result
744
Serban Constantinescu169489b2014-06-11 16:43:35 +0100745 // Load Object and offset
buzbeea0cd2d72014-06-01 09:33:49 -0700746 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100747 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100748
Matteo Franchin43ec8732014-03-31 15:00:14 +0100749 RegLocation rl_new_value;
Serban Constantinescu169489b2014-06-11 16:43:35 +0100750 RegLocation rl_expected;
751 if (is_long) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100752 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100753 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
754 } else {
755 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
756 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100757 }
758
759 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
760 // Mark card for object assuming new value is stored.
761 MarkGCCard(rl_new_value.reg, rl_object.reg);
762 }
763
Serban Constantinescu169489b2014-06-11 16:43:35 +0100764 RegStorage r_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100765 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
766
767 // Free now unneeded rl_object and rl_offset to give more temps.
768 ClobberSReg(rl_object.s_reg_low);
769 FreeTemp(rl_object.reg);
770 ClobberSReg(rl_offset.s_reg_low);
771 FreeTemp(rl_offset.reg);
772
Matteo Franchin43ec8732014-03-31 15:00:14 +0100773 // do {
774 // tmp = [r_ptr] - expected;
775 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
776 // result = tmp != 0;
777
Serban Constantinescu169489b2014-06-11 16:43:35 +0100778 RegStorage r_tmp;
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100779 RegStorage r_tmp_stored;
780 RegStorage rl_new_value_stored = rl_new_value.reg;
Matteo Franchin4163c532014-07-15 15:20:27 +0100781 A64Opcode wide = UNWIDE(0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100782 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100783 r_tmp_stored = r_tmp = AllocTempWide();
784 wide = WIDE(0);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100785 } else if (is_object) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100786 // References use 64-bit registers, but are stored as compressed 32-bit values.
787 // This means r_tmp_stored != r_tmp.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100788 r_tmp = AllocTempRef();
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100789 r_tmp_stored = As32BitReg(r_tmp);
790 rl_new_value_stored = As32BitReg(rl_new_value_stored);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100791 } else {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100792 r_tmp_stored = r_tmp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100793 }
794
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100795 RegStorage r_tmp32 = (r_tmp.Is32Bit()) ? r_tmp : As32BitReg(r_tmp);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100796 LIR* loop = NewLIR0(kPseudoTargetLabel);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100797 NewLIR2(kA64Ldaxr2rX | wide, r_tmp_stored.GetReg(), r_ptr.GetReg());
Serban Constantinescu169489b2014-06-11 16:43:35 +0100798 OpRegReg(kOpCmp, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100799 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Serban Constantinescu169489b2014-06-11 16:43:35 +0100800 LIR* early_exit = OpCondBranch(kCondNe, NULL);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100801 NewLIR3(kA64Stlxr3wrX | wide, r_tmp32.GetReg(), rl_new_value_stored.GetReg(), r_ptr.GetReg());
802 NewLIR3(kA64Cmp3RdT, r_tmp32.GetReg(), 0, ENCODE_NO_SHIFT);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100803 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
804 OpCondBranch(kCondNe, loop);
805
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100806 LIR* exit_loop = NewLIR0(kPseudoTargetLabel);
807 early_exit->target = exit_loop;
808
Serban Constantinescu169489b2014-06-11 16:43:35 +0100809 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100810 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondNe);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100811
Matteo Franchin43ec8732014-03-31 15:00:14 +0100812 FreeTemp(r_tmp); // Now unneeded.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100813 FreeTemp(r_ptr); // Now unneeded.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100814
815 StoreValue(rl_dest, rl_result);
816
Matteo Franchin43ec8732014-03-31 15:00:14 +0100817 return true;
818}
819
Zheng Xu947717a2014-08-07 14:05:23 +0800820bool Arm64Mir2Lir::GenInlinedArrayCopyCharArray(CallInfo* info) {
821 constexpr int kLargeArrayThreshold = 512;
822
823 RegLocation rl_src = info->args[0];
824 RegLocation rl_src_pos = info->args[1];
825 RegLocation rl_dst = info->args[2];
826 RegLocation rl_dst_pos = info->args[3];
827 RegLocation rl_length = info->args[4];
828 // Compile time check, handle exception by non-inline method to reduce related meta-data.
829 if ((rl_src_pos.is_const && (mir_graph_->ConstantValue(rl_src_pos) < 0)) ||
830 (rl_dst_pos.is_const && (mir_graph_->ConstantValue(rl_dst_pos) < 0)) ||
831 (rl_length.is_const && (mir_graph_->ConstantValue(rl_length) < 0))) {
832 return false;
833 }
834
835 ClobberCallerSave();
836 LockCallTemps(); // Prepare for explicit register usage.
837 RegStorage rs_src = rs_x0;
838 RegStorage rs_dst = rs_x1;
839 LoadValueDirectFixed(rl_src, rs_src);
840 LoadValueDirectFixed(rl_dst, rs_dst);
841
842 // Handle null pointer exception in slow-path.
843 LIR* src_check_branch = OpCmpImmBranch(kCondEq, rs_src, 0, nullptr);
844 LIR* dst_check_branch = OpCmpImmBranch(kCondEq, rs_dst, 0, nullptr);
845 // Handle potential overlapping in slow-path.
846 // TUNING: Support overlapping cases.
847 LIR* src_dst_same = OpCmpBranch(kCondEq, rs_src, rs_dst, nullptr);
848 // Handle exception or big length in slow-path.
849 RegStorage rs_length = rs_w2;
850 LoadValueDirectFixed(rl_length, rs_length);
851 LIR* len_neg_or_too_big = OpCmpImmBranch(kCondHi, rs_length, kLargeArrayThreshold, nullptr);
852 // Src bounds check.
853 RegStorage rs_src_pos = rs_w3;
854 RegStorage rs_arr_length = rs_w4;
855 LoadValueDirectFixed(rl_src_pos, rs_src_pos);
856 LIR* src_pos_negative = OpCmpImmBranch(kCondLt, rs_src_pos, 0, nullptr);
857 Load32Disp(rs_src, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
858 OpRegReg(kOpSub, rs_arr_length, rs_src_pos);
859 LIR* src_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
860 // Dst bounds check.
861 RegStorage rs_dst_pos = rs_w5;
862 LoadValueDirectFixed(rl_dst_pos, rs_dst_pos);
863 LIR* dst_pos_negative = OpCmpImmBranch(kCondLt, rs_dst_pos, 0, nullptr);
864 Load32Disp(rs_dst, mirror::Array::LengthOffset().Int32Value(), rs_arr_length);
865 OpRegReg(kOpSub, rs_arr_length, rs_dst_pos);
866 LIR* dst_bad_len = OpCmpBranch(kCondLt, rs_arr_length, rs_length, nullptr);
867
868 // Everything is checked now.
869 // Set rs_src to the address of the first element to be copied.
870 rs_src_pos = As64BitReg(rs_src_pos);
871 OpRegImm(kOpAdd, rs_src, mirror::Array::DataOffset(2).Int32Value());
872 OpRegRegImm(kOpLsl, rs_src_pos, rs_src_pos, 1);
873 OpRegReg(kOpAdd, rs_src, rs_src_pos);
874 // Set rs_src to the address of the first element to be copied.
875 rs_dst_pos = As64BitReg(rs_dst_pos);
876 OpRegImm(kOpAdd, rs_dst, mirror::Array::DataOffset(2).Int32Value());
877 OpRegRegImm(kOpLsl, rs_dst_pos, rs_dst_pos, 1);
878 OpRegReg(kOpAdd, rs_dst, rs_dst_pos);
879
880 // rs_arr_length won't be not used anymore.
881 RegStorage rs_tmp = rs_arr_length;
882 // Use 64-bit view since rs_length will be used as index.
883 rs_length = As64BitReg(rs_length);
884 OpRegRegImm(kOpLsl, rs_length, rs_length, 1);
885
886 // Copy one element.
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800887 LIR* jmp_to_copy_two = NewLIR3(WIDE(kA64Tbz3rht), rs_length.GetReg(), 1, 0);
Zheng Xu947717a2014-08-07 14:05:23 +0800888 OpRegImm(kOpSub, rs_length, 2);
889 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, kSignedHalf);
890 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, kSignedHalf);
891
892 // Copy two elements.
893 LIR *copy_two = NewLIR0(kPseudoTargetLabel);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800894 LIR* jmp_to_copy_four = NewLIR3(WIDE(kA64Tbz3rht), rs_length.GetReg(), 2, 0);
Zheng Xu947717a2014-08-07 14:05:23 +0800895 OpRegImm(kOpSub, rs_length, 4);
896 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k32);
897 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k32);
898
899 // Copy four elements.
900 LIR *copy_four = NewLIR0(kPseudoTargetLabel);
901 LIR* jmp_to_ret = OpCmpImmBranch(kCondEq, rs_length, 0, nullptr);
902 LIR *begin_loop = NewLIR0(kPseudoTargetLabel);
903 OpRegImm(kOpSub, rs_length, 8);
904 rs_tmp = As64BitReg(rs_tmp);
905 LoadBaseIndexed(rs_src, rs_length, rs_tmp, 0, k64);
906 StoreBaseIndexed(rs_dst, rs_length, rs_tmp, 0, k64);
907 LIR* jmp_to_loop = OpCmpImmBranch(kCondNe, rs_length, 0, nullptr);
908 LIR* loop_finished = OpUnconditionalBranch(nullptr);
909
910 LIR *check_failed = NewLIR0(kPseudoTargetLabel);
911 LIR* launchpad_branch = OpUnconditionalBranch(nullptr);
912 LIR* return_point = NewLIR0(kPseudoTargetLabel);
913
914 src_check_branch->target = check_failed;
915 dst_check_branch->target = check_failed;
916 src_dst_same->target = check_failed;
917 len_neg_or_too_big->target = check_failed;
918 src_pos_negative->target = check_failed;
919 src_bad_len->target = check_failed;
920 dst_pos_negative->target = check_failed;
921 dst_bad_len->target = check_failed;
922 jmp_to_copy_two->target = copy_two;
923 jmp_to_copy_four->target = copy_four;
924 jmp_to_ret->target = return_point;
925 jmp_to_loop->target = begin_loop;
926 loop_finished->target = return_point;
927
928 AddIntrinsicSlowPath(info, launchpad_branch, return_point);
Serguei Katkov9863daf2014-09-04 15:21:32 +0700929 ClobberCallerSave(); // We must clobber everything because slow path will return here
Zheng Xu947717a2014-08-07 14:05:23 +0800930
931 return true;
932}
933
Matteo Franchin43ec8732014-03-31 15:00:14 +0100934LIR* Arm64Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100935 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchin27cc0932014-09-08 18:29:24 +0100936 return RawLIR(current_dalvik_offset_, kA64Ldr2rp, As32BitReg(reg).GetReg(), 0, 0, 0, 0, target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100937}
938
939LIR* Arm64Mir2Lir::OpVldm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700940 UNUSED(r_base, count);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100941 LOG(FATAL) << "Unexpected use of OpVldm for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700942 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100943}
944
945LIR* Arm64Mir2Lir::OpVstm(RegStorage r_base, int count) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700946 UNUSED(r_base, count);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100947 LOG(FATAL) << "Unexpected use of OpVstm for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700948 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100949}
950
951void Arm64Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700952 RegLocation rl_result, int lit ATTRIBUTE_UNUSED,
953 int first_bit, int second_bit) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100954 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsl, second_bit - first_bit));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100955 if (first_bit != 0) {
956 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
957 }
958}
959
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700960void Arm64Mir2Lir::GenDivZeroCheckWide(RegStorage reg ATTRIBUTE_UNUSED) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100961 LOG(FATAL) << "Unexpected use of GenDivZero for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100962}
963
964// Test suspend flag, return target of taken suspend branch
965LIR* Arm64Mir2Lir::OpTestSuspend(LIR* target) {
Zheng Xubaa7c882014-06-30 14:26:50 +0800966 NewLIR3(kA64Subs3rRd, rwSUSPEND, rwSUSPEND, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100967 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
968}
969
970// Decrement register and branch on condition
971LIR* Arm64Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -0700972 // Combine sub & test using sub setflags encoding here. We need to make sure a
973 // subtract form that sets carry is used, so generate explicitly.
974 // TODO: might be best to add a new op, kOpSubs, and handle it generically.
Matteo Franchin4163c532014-07-15 15:20:27 +0100975 A64Opcode opcode = reg.Is64Bit() ? WIDE(kA64Subs3rRd) : UNWIDE(kA64Subs3rRd);
buzbee33ae5582014-06-12 14:56:32 -0700976 NewLIR3(opcode, reg.GetReg(), reg.GetReg(), 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100977 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100978 return OpCondBranch(c_code, target);
979}
980
Andreas Gampeb14329f2014-05-15 11:16:06 -0700981bool Arm64Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100982#if ANDROID_SMP != 0
983 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
984 LIR* barrier = last_lir_insn_;
985
986 int dmb_flavor;
987 // TODO: revisit Arm barrier kinds
988 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700989 case kAnyStore: dmb_flavor = kISH; break;
990 case kLoadAny: dmb_flavor = kISH; break;
991 // We conjecture that kISHLD is insufficient. It is documented
992 // to provide LoadLoad | StoreStore ordering. But if this were used
993 // to implement volatile loads, we suspect that the lack of store
994 // atomicity on ARM would cause us to allow incorrect results for
995 // the canonical IRIW example. But we're not sure.
996 // We should be using acquire loads instead.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100997 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -0700998 case kAnyAny: dmb_flavor = kISH; break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100999 default:
1000 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1001 dmb_flavor = kSY; // quiet gcc.
1002 break;
1003 }
1004
Andreas Gampeb14329f2014-05-15 11:16:06 -07001005 bool ret = false;
1006
Matteo Franchin43ec8732014-03-31 15:00:14 +01001007 // If the same barrier already exists, don't generate another.
1008 if (barrier == nullptr
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001009 || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) {
1010 barrier = NewLIR1(kA64Dmb1B, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001011 ret = true;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001012 }
1013
1014 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1015 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001016 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001017 return ret;
1018#else
1019 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001020#endif
1021}
1022
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001023void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
1024 RegLocation rl_result;
1025
1026 rl_src = LoadValue(rl_src, kCoreReg);
1027 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001028 NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001029 StoreValueWide(rl_dest, rl_result);
1030}
1031
1032void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001033 RegLocation rl_src1, RegLocation rl_src2, bool is_div, int flags) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +01001034 if (rl_src2.is_const) {
1035 DCHECK(rl_src2.wide);
1036 int64_t lit = mir_graph_->ConstantValueWide(rl_src2);
1037 if (HandleEasyDivRem64(opcode, is_div, rl_src1, rl_dest, lit)) {
1038 return;
1039 }
1040 }
1041
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001042 RegLocation rl_result;
1043 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1044 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001045 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
1046 GenDivZeroCheck(rl_src2.reg);
1047 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001048 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001049 StoreValueWide(rl_dest, rl_result);
1050}
1051
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001052void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1,
1053 RegLocation rl_src2) {
1054 RegLocation rl_result;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001055
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001056 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1057 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1058 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001059 OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT);
1060 StoreValueWide(rl_dest, rl_result);
1061}
1062
1063void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
1064 RegLocation rl_result;
1065
1066 rl_src = LoadValueWide(rl_src, kCoreReg);
1067 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1068 OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
1069 StoreValueWide(rl_dest, rl_result);
1070}
1071
1072void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
1073 RegLocation rl_result;
1074
1075 rl_src = LoadValueWide(rl_src, kCoreReg);
1076 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1077 OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001078 StoreValueWide(rl_dest, rl_result);
1079}
1080
Andreas Gampec76c6142014-08-04 16:30:03 -07001081void Arm64Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001082 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001083 switch (opcode) {
1084 case Instruction::NOT_LONG:
1085 GenNotLong(rl_dest, rl_src2);
1086 return;
1087 case Instruction::ADD_LONG:
1088 case Instruction::ADD_LONG_2ADDR:
1089 GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2);
1090 return;
1091 case Instruction::SUB_LONG:
1092 case Instruction::SUB_LONG_2ADDR:
1093 GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2);
1094 return;
1095 case Instruction::MUL_LONG:
1096 case Instruction::MUL_LONG_2ADDR:
1097 GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2);
1098 return;
1099 case Instruction::DIV_LONG:
1100 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001101 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001102 return;
1103 case Instruction::REM_LONG:
1104 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001105 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001106 return;
1107 case Instruction::AND_LONG_2ADDR:
1108 case Instruction::AND_LONG:
1109 GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2);
1110 return;
1111 case Instruction::OR_LONG:
1112 case Instruction::OR_LONG_2ADDR:
1113 GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2);
1114 return;
1115 case Instruction::XOR_LONG:
1116 case Instruction::XOR_LONG_2ADDR:
1117 GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2);
1118 return;
1119 case Instruction::NEG_LONG: {
1120 GenNegLong(rl_dest, rl_src2);
1121 return;
1122 }
1123 default:
1124 LOG(FATAL) << "Invalid long arith op";
1125 return;
1126 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001127}
1128
1129/*
1130 * Generate array load
1131 */
1132void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
1133 RegLocation rl_index, RegLocation rl_dest, int scale) {
1134 RegisterClass reg_class = RegClassBySize(size);
1135 int len_offset = mirror::Array::LengthOffset().Int32Value();
1136 int data_offset;
1137 RegLocation rl_result;
1138 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001139 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001140 if (!constant_index) {
1141 rl_index = LoadValue(rl_index, kCoreReg);
1142 }
1143
1144 if (rl_dest.wide) {
1145 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1146 } else {
1147 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1148 }
1149
1150 // If index is constant, just fold it into the data offset
1151 if (constant_index) {
1152 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1153 }
1154
1155 /* null object? */
1156 GenNullCheck(rl_array.reg, opt_flags);
1157
1158 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1159 RegStorage reg_len;
1160 if (needs_range_check) {
1161 reg_len = AllocTemp();
1162 /* Get len */
1163 Load32Disp(rl_array.reg, len_offset, reg_len);
1164 MarkPossibleNullPointerException(opt_flags);
1165 } else {
1166 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1167 }
1168 if (rl_dest.wide || rl_dest.fp || constant_index) {
1169 RegStorage reg_ptr;
1170 if (constant_index) {
1171 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
1172 } else {
1173 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001174 reg_ptr = AllocTempRef();
buzbee33ae5582014-06-12 14:56:32 -07001175 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1176 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001177 FreeTemp(rl_index.reg);
1178 }
1179 rl_result = EvalLoc(rl_dest, reg_class, true);
1180
1181 if (needs_range_check) {
1182 if (constant_index) {
1183 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1184 } else {
1185 GenArrayBoundsCheck(rl_index.reg, reg_len);
1186 }
1187 FreeTemp(reg_len);
1188 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001189 if (rl_result.ref) {
1190 LoadRefDisp(reg_ptr, data_offset, rl_result.reg, kNotVolatile);
1191 } else {
1192 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
1193 }
Vladimir Marko455759b2014-05-06 20:49:36 +01001194 if (!constant_index) {
1195 FreeTemp(reg_ptr);
1196 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001197 if (rl_dest.wide) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001198 StoreValueWide(rl_dest, rl_result);
1199 } else {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001200 StoreValue(rl_dest, rl_result);
1201 }
1202 } else {
1203 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001204 RegStorage reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001205 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1206 FreeTemp(rl_array.reg);
1207 rl_result = EvalLoc(rl_dest, reg_class, true);
1208
1209 if (needs_range_check) {
1210 GenArrayBoundsCheck(rl_index.reg, reg_len);
1211 FreeTemp(reg_len);
1212 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001213 if (rl_result.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001214 LoadRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001215 } else {
1216 LoadBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale, size);
1217 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001218 FreeTemp(reg_ptr);
1219 StoreValue(rl_dest, rl_result);
1220 }
1221}
1222
1223/*
1224 * Generate array store
1225 *
1226 */
1227void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
1228 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
1229 RegisterClass reg_class = RegClassBySize(size);
1230 int len_offset = mirror::Array::LengthOffset().Int32Value();
1231 bool constant_index = rl_index.is_const;
1232
1233 int data_offset;
1234 if (size == k64 || size == kDouble) {
1235 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1236 } else {
1237 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1238 }
1239
1240 // If index is constant, just fold it into the data offset.
1241 if (constant_index) {
1242 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1243 }
1244
buzbeea0cd2d72014-06-01 09:33:49 -07001245 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001246 if (!constant_index) {
1247 rl_index = LoadValue(rl_index, kCoreReg);
1248 }
1249
1250 RegStorage reg_ptr;
1251 bool allocated_reg_ptr_temp = false;
1252 if (constant_index) {
1253 reg_ptr = rl_array.reg;
1254 } else if (IsTemp(rl_array.reg) && !card_mark) {
1255 Clobber(rl_array.reg);
1256 reg_ptr = rl_array.reg;
1257 } else {
1258 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001259 reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001260 }
1261
1262 /* null object? */
1263 GenNullCheck(rl_array.reg, opt_flags);
1264
1265 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1266 RegStorage reg_len;
1267 if (needs_range_check) {
1268 reg_len = AllocTemp();
1269 // NOTE: max live temps(4) here.
1270 /* Get len */
1271 Load32Disp(rl_array.reg, len_offset, reg_len);
1272 MarkPossibleNullPointerException(opt_flags);
1273 } else {
1274 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1275 }
1276 /* at this point, reg_ptr points to array, 2 live temps */
1277 if (rl_src.wide || rl_src.fp || constant_index) {
1278 if (rl_src.wide) {
1279 rl_src = LoadValueWide(rl_src, reg_class);
1280 } else {
1281 rl_src = LoadValue(rl_src, reg_class);
1282 }
1283 if (!constant_index) {
buzbee33ae5582014-06-12 14:56:32 -07001284 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1285 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001286 }
1287 if (needs_range_check) {
1288 if (constant_index) {
1289 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1290 } else {
1291 GenArrayBoundsCheck(rl_index.reg, reg_len);
1292 }
1293 FreeTemp(reg_len);
1294 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001295 if (rl_src.ref) {
1296 StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile);
1297 } else {
1298 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
1299 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001300 } else {
1301 /* reg_ptr -> array data */
1302 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1303 rl_src = LoadValue(rl_src, reg_class);
1304 if (needs_range_check) {
1305 GenArrayBoundsCheck(rl_index.reg, reg_len);
1306 FreeTemp(reg_len);
1307 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001308 if (rl_src.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001309 StoreRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001310 } else {
1311 StoreBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale, size);
1312 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001313 }
1314 if (allocated_reg_ptr_temp) {
1315 FreeTemp(reg_ptr);
1316 }
1317 if (card_mark) {
1318 MarkGCCard(rl_src.reg, rl_array.reg);
1319 }
1320}
1321
Matteo Franchin43ec8732014-03-31 15:00:14 +01001322void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001323 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001324 int flags ATTRIBUTE_UNUSED) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001325 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001326 // Per spec, we only care about low 6 bits of shift amount.
1327 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001328 rl_src = LoadValueWide(rl_src, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001329 if (shift_amount == 0) {
1330 StoreValueWide(rl_dest, rl_src);
1331 return;
1332 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001333
1334 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001335 switch (opcode) {
1336 case Instruction::SHL_LONG:
1337 case Instruction::SHL_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001338 op = kOpLsl;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001339 break;
1340 case Instruction::SHR_LONG:
1341 case Instruction::SHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001342 op = kOpAsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001343 break;
1344 case Instruction::USHR_LONG:
1345 case Instruction::USHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001346 op = kOpLsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001347 break;
1348 default:
1349 LOG(FATAL) << "Unexpected case";
1350 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001351 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001352 StoreValueWide(rl_dest, rl_result);
1353}
1354
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001355void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001356 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001357 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001358 switch (opcode) {
1359 case Instruction::ADD_LONG:
1360 case Instruction::ADD_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001361 op = kOpAdd;
1362 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001363 case Instruction::SUB_LONG:
1364 case Instruction::SUB_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001365 op = kOpSub;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001366 break;
1367 case Instruction::AND_LONG:
1368 case Instruction::AND_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001369 op = kOpAnd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001370 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001371 case Instruction::OR_LONG:
1372 case Instruction::OR_LONG_2ADDR:
1373 op = kOpOr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001374 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001375 case Instruction::XOR_LONG:
1376 case Instruction::XOR_LONG_2ADDR:
1377 op = kOpXor;
1378 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001379 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001380 LOG(FATAL) << "Unexpected opcode";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001381 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001382
Matteo Franchinc763e352014-07-04 12:53:27 +01001383 if (op == kOpSub) {
1384 if (!rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001385 return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Matteo Franchinc763e352014-07-04 12:53:27 +01001386 }
1387 } else {
1388 // Associativity.
1389 if (!rl_src2.is_const) {
1390 DCHECK(rl_src1.is_const);
1391 std::swap(rl_src1, rl_src2);
1392 }
1393 }
1394 DCHECK(rl_src2.is_const);
1395 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1396
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001397 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1398 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +08001399 OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001400 StoreValueWide(rl_dest, rl_result);
1401}
1402
Andreas Gampef29ecd62014-07-29 00:35:00 -07001403static uint32_t ExtractReg(uint32_t reg_mask, int* reg) {
1404 // Find first register.
1405 int first_bit_set = CTZ(reg_mask) + 1;
1406 *reg = *reg + first_bit_set;
1407 reg_mask >>= first_bit_set;
1408 return reg_mask;
1409}
1410
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001411/**
1412 * @brief Split a register list in pairs or registers.
1413 *
1414 * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows:
1415 * @code
1416 * int reg1 = -1, reg2 = -1;
1417 * while (reg_mask) {
1418 * reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1419 * if (UNLIKELY(reg2 < 0)) {
1420 * // Single register in reg1.
1421 * } else {
1422 * // Pair in reg1, reg2.
1423 * }
1424 * }
1425 * @endcode
1426 */
Andreas Gampef29ecd62014-07-29 00:35:00 -07001427static uint32_t GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001428 // Find first register.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001429 int first_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001430 int reg = *reg1 + first_bit_set;
1431 reg_mask >>= first_bit_set;
1432
1433 if (LIKELY(reg_mask)) {
1434 // Save the first register, find the second and use the pair opcode.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001435 int second_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001436 *reg2 = reg;
1437 reg_mask >>= second_bit_set;
1438 *reg1 = reg + second_bit_set;
1439 return reg_mask;
1440 }
1441
1442 // Use the single opcode, as we just have one register.
1443 *reg1 = reg;
1444 *reg2 = -1;
1445 return reg_mask;
1446}
1447
Andreas Gampef29ecd62014-07-29 00:35:00 -07001448static void SpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001449 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001450 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001451
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001452 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001453 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1454 if (UNLIKELY(reg2 < 0)) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001455 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001456 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001457 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1458 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001459 }
1460 }
1461}
1462
1463// TODO(Arm64): consider using ld1 and st1?
Andreas Gampef29ecd62014-07-29 00:35:00 -07001464static void SpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001465 int reg1 = -1, reg2 = -1;
1466 const int reg_log2_size = 3;
1467
1468 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1469 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1470 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001471 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001472 offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001473 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001474 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1475 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001476 }
1477 }
1478}
1479
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001480static int SpillRegsPreSub(Arm64Mir2Lir* m2l, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1481 int frame_size) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001482 m2l->OpRegRegImm(kOpSub, rs_sp, rs_sp, frame_size);
1483
1484 int core_count = POPCOUNT(core_reg_mask);
1485
1486 if (fp_reg_mask != 0) {
1487 // Spill FP regs.
1488 int fp_count = POPCOUNT(fp_reg_mask);
1489 int spill_offset = frame_size - (core_count + fp_count) * kArm64PointerSize;
1490 SpillFPRegs(m2l, rs_sp, spill_offset, fp_reg_mask);
1491 }
1492
1493 if (core_reg_mask != 0) {
1494 // Spill core regs.
1495 int spill_offset = frame_size - (core_count * kArm64PointerSize);
1496 SpillCoreRegs(m2l, rs_sp, spill_offset, core_reg_mask);
1497 }
1498
1499 return frame_size;
1500}
1501
1502static int SpillRegsPreIndexed(Arm64Mir2Lir* m2l, RegStorage base, uint32_t core_reg_mask,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001503 uint32_t fp_reg_mask) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001504 // Otherwise, spill both core and fp regs at the same time.
1505 // The very first instruction will be an stp with pre-indexed address, moving the stack pointer
1506 // down. From then on, we fill upwards. This will generate overall the same number of instructions
1507 // as the specialized code above in most cases (exception being odd number of core and even
1508 // non-zero fp spills), but is more flexible, as the offsets are guaranteed small.
1509 //
1510 // Some demonstrative fill cases : (c) = core, (f) = fp
1511 // cc 44 cc 44 cc 22 cc 33 fc => 1[1/2]
1512 // fc => 23 fc => 23 ff => 11 ff => 22
1513 // ff 11 f 11 f 11
1514 //
1515 int reg1 = -1, reg2 = -1;
1516 int core_count = POPCOUNT(core_reg_mask);
1517 int fp_count = POPCOUNT(fp_reg_mask);
1518
1519 int combined = fp_count + core_count;
1520 int all_offset = RoundUp(combined, 2); // Needs to be 16B = 2-reg aligned.
1521
1522 int cur_offset = 2; // What's the starting offset after the first stp? We expect the base slot
1523 // to be filled.
1524
1525 // First figure out whether the bottom is FP or core.
1526 if (fp_count > 0) {
1527 // Some FP spills.
1528 //
1529 // Four cases: (d0 is dummy to fill up stp)
1530 // 1) Single FP, even number of core -> stp d0, fp_reg
1531 // 2) Single FP, odd number of core -> stp fp_reg, d0
1532 // 3) More FP, even number combined -> stp fp_reg1, fp_reg2
1533 // 4) More FP, odd number combined -> stp d0, fp_reg
1534 if (fp_count == 1) {
1535 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1536 DCHECK_EQ(fp_reg_mask, 0U);
1537 if (core_count % 2 == 0) {
1538 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1539 RegStorage::FloatSolo64(reg1).GetReg(),
1540 RegStorage::FloatSolo64(reg1).GetReg(),
1541 base.GetReg(), -all_offset);
1542 } else {
1543 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1544 RegStorage::FloatSolo64(reg1).GetReg(),
1545 RegStorage::FloatSolo64(reg1).GetReg(),
1546 base.GetReg(), -all_offset);
1547 cur_offset = 0; // That core reg needs to go into the upper half.
1548 }
1549 } else {
1550 if (combined % 2 == 0) {
1551 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1552 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1553 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), -all_offset);
1554 } else {
1555 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1556 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), rs_d0.GetReg(), RegStorage::FloatSolo64(reg1).GetReg(),
1557 base.GetReg(), -all_offset);
1558 }
1559 }
1560 } else {
1561 // No FP spills.
1562 //
1563 // Two cases:
1564 // 1) Even number of core -> stp core1, core2
1565 // 2) Odd number of core -> stp xzr, core1
1566 if (core_count % 2 == 1) {
1567 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1568 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), rs_xzr.GetReg(),
1569 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1570 } else {
1571 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1572 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), RegStorage::Solo64(reg2).GetReg(),
1573 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1574 }
1575 }
1576
1577 if (fp_count != 0) {
1578 for (; fp_reg_mask != 0;) {
1579 // Have some FP regs to do.
1580 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1581 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001582 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001583 cur_offset);
1584 // Do not increment offset here, as the second half will be filled by a core reg.
1585 } else {
1586 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1587 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), cur_offset);
1588 cur_offset += 2;
1589 }
1590 }
1591
1592 // Reset counting.
1593 reg1 = -1;
1594
1595 // If there is an odd number of core registers, we need to store the bottom now.
1596 if (core_count % 2 == 1) {
1597 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1598 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(),
1599 cur_offset + 1);
1600 cur_offset += 2; // Half-slot filled now.
1601 }
1602 }
1603
1604 // Spill the rest of the core regs. They are guaranteed to be even.
1605 DCHECK_EQ(POPCOUNT(core_reg_mask) % 2, 0);
1606 for (; core_reg_mask != 0; cur_offset += 2) {
1607 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1608 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1609 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), cur_offset);
1610 }
1611
1612 DCHECK_EQ(cur_offset, all_offset);
1613
1614 return all_offset * 8;
1615}
1616
1617int Arm64Mir2Lir::SpillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1618 int frame_size) {
1619 // If the frame size is small enough that all offsets would fit into the immediates, use that
1620 // setup, as it decrements sp early (kind of instruction scheduling), and is not worse
1621 // instruction-count wise than the complicated code below.
1622 //
1623 // This case is also optimal when we have an odd number of core spills, and an even (non-zero)
1624 // number of fp spills.
1625 if ((RoundUp(frame_size, 8) / 8 <= 63)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001626 return SpillRegsPreSub(this, core_reg_mask, fp_reg_mask, frame_size);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001627 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001628 return SpillRegsPreIndexed(this, base, core_reg_mask, fp_reg_mask);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001629 }
1630}
1631
1632static void UnSpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1633 int reg1 = -1, reg2 = -1;
1634 const int reg_log2_size = 3;
1635
1636 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1637 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1638 if (UNLIKELY(reg2 < 0)) {
1639 m2l->NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1640 } else {
1641 DCHECK_LE(offset, 63);
1642 m2l->NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1643 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1644 }
1645 }
1646}
1647
1648static void UnSpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1649 int reg1 = -1, reg2 = -1;
1650 const int reg_log2_size = 3;
1651
1652 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1653 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1654 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001655 m2l->NewLIR3(WIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001656 offset);
1657 } else {
1658 m2l->NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1659 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1660 }
1661 }
1662}
1663
1664void Arm64Mir2Lir::UnspillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1665 int frame_size) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001666 DCHECK(base == rs_sp);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001667 // Restore saves and drop stack frame.
1668 // 2 versions:
1669 //
1670 // 1. (Original): Try to address directly, then drop the whole frame.
1671 // Limitation: ldp is a 7b signed immediate.
1672 //
1673 // 2. (New): Drop the non-save-part. Then do similar to original, which is now guaranteed to be
1674 // in range. Then drop the rest.
1675 //
1676 // TODO: In methods with few spills but huge frame, it would be better to do non-immediate loads
1677 // in variant 1.
1678
1679 // "Magic" constant, 63 (max signed 7b) * 8.
1680 static constexpr int kMaxFramesizeForOffset = 63 * kArm64PointerSize;
1681
1682 const int num_core_spills = POPCOUNT(core_reg_mask);
1683 const int num_fp_spills = POPCOUNT(fp_reg_mask);
1684
1685 int early_drop = 0;
1686
1687 if (frame_size > kMaxFramesizeForOffset) {
1688 // Second variant. Drop the frame part.
1689
1690 // TODO: Always use the first formula, as num_fp_spills would be zero?
1691 if (fp_reg_mask != 0) {
1692 early_drop = frame_size - kArm64PointerSize * (num_fp_spills + num_core_spills);
1693 } else {
1694 early_drop = frame_size - kArm64PointerSize * num_core_spills;
1695 }
1696
1697 // Drop needs to be 16B aligned, so that SP keeps aligned.
1698 early_drop = RoundDown(early_drop, 16);
1699
1700 OpRegImm64(kOpAdd, rs_sp, early_drop);
1701 }
1702
1703 // Unspill.
1704 if (fp_reg_mask != 0) {
1705 int offset = frame_size - early_drop - kArm64PointerSize * (num_fp_spills + num_core_spills);
1706 UnSpillFPRegs(this, rs_sp, offset, fp_reg_mask);
1707 }
1708 if (core_reg_mask != 0) {
1709 int offset = frame_size - early_drop - kArm64PointerSize * num_core_spills;
1710 UnSpillCoreRegs(this, rs_sp, offset, core_reg_mask);
1711 }
1712
1713 // Drop the (rest of) the frame.
1714 OpRegImm64(kOpAdd, rs_sp, frame_size - early_drop);
1715}
1716
Serban Constantinescu23abec92014-07-02 16:13:38 +01001717bool Arm64Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001718 A64Opcode wide = IsWide(size) ? WIDE(0) : UNWIDE(0);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001719 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001720 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Serban Constantinescu23abec92014-07-02 16:13:38 +01001721 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001722 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001723 NewLIR2(kA64Rbit2rr | wide, rl_result.reg.GetReg(), rl_i.reg.GetReg());
Fred Shih37f05ef2014-07-16 18:38:08 -07001724 IsWide(size) ? StoreValueWide(rl_dest, rl_result) : StoreValue(rl_dest, rl_result);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001725 return true;
1726}
1727
Matteo Franchin43ec8732014-03-31 15:00:14 +01001728} // namespace art