blob: 0e006983880797ac5e2b0fbd89a95c47ed8c2fd3 [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
Elliott Hughes8366ca02014-11-17 12:02:05 -080019#include "arch/instruction_set_features.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010020#include "arm64_lir.h"
21#include "codegen_arm64.h"
22#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010024#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Andreas Gampef29ecd62014-07-29 00:35:00 -070026#include "utils.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010027
28namespace art {
29
30LIR* Arm64Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
31 OpRegReg(kOpCmp, src1, src2);
32 return OpCondBranch(cond, target);
33}
34
Matteo Franchin43ec8732014-03-31 15:00:14 +010035LIR* Arm64Mir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070036 UNUSED(ccode, guide);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010037 LOG(FATAL) << "Unexpected use of OpIT for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070038 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +010039}
40
41void Arm64Mir2Lir::OpEndIT(LIR* it) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070042 UNUSED(it);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010043 LOG(FATAL) << "Unexpected use of OpEndIT for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +010044}
45
46/*
47 * 64-bit 3way compare function.
Matteo Franchine45fb9e2014-05-06 10:10:30 +010048 * cmp xA, xB
Zheng Xu511c8a62014-06-03 16:22:23 +080049 * csinc wC, wzr, wzr, eq // wC = (xA == xB) ? 0 : 1
50 * csneg wC, wC, wC, ge // wC = (xA >= xB) ? wC : -wC
Matteo Franchin43ec8732014-03-31 15:00:14 +010051 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +010052void Arm64Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
53 RegLocation rl_src2) {
54 RegLocation rl_result;
Matteo Franchin43ec8732014-03-31 15:00:14 +010055 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
56 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010057 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +010058
Matteo Franchine45fb9e2014-05-06 10:10:30 +010059 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Zheng Xu511c8a62014-06-03 16:22:23 +080060 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondEq);
61 NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_result.reg.GetReg(),
62 rl_result.reg.GetReg(), kArmCondGe);
63 StoreValue(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010064}
65
66void Arm64Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
67 RegLocation rl_src1, RegLocation rl_shift) {
68 OpKind op = kOpBkpt;
69 switch (opcode) {
70 case Instruction::SHL_LONG:
71 case Instruction::SHL_LONG_2ADDR:
72 op = kOpLsl;
73 break;
74 case Instruction::SHR_LONG:
75 case Instruction::SHR_LONG_2ADDR:
76 op = kOpAsr;
77 break;
78 case Instruction::USHR_LONG:
79 case Instruction::USHR_LONG_2ADDR:
80 op = kOpLsr;
81 break;
82 default:
83 LOG(FATAL) << "Unexpected case: " << opcode;
84 }
Zheng Xue2eb29e2014-06-12 10:22:33 +080085 rl_shift = LoadValue(rl_shift, kCoreReg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010086 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
87 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +080088 OpRegRegReg(op, rl_result.reg, rl_src1.reg, As64BitReg(rl_shift.reg));
Serban Constantinescued65c5e2014-05-22 15:10:18 +010089 StoreValueWide(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +010090}
91
Andreas Gampe90969af2014-07-15 23:02:11 -070092static constexpr bool kUseDeltaEncodingInGenSelect = false;
Andreas Gampe381f8ac2014-07-10 03:23:41 -070093
Andreas Gampe90969af2014-07-15 23:02:11 -070094void Arm64Mir2Lir::GenSelect(int32_t true_val, int32_t false_val, ConditionCode ccode,
95 RegStorage rs_dest, int result_reg_class) {
96 if (false_val == 0 || // 0 is better as first operand.
97 true_val == 1 || // Potentially Csinc.
98 true_val == -1 || // Potentially Csinv.
99 true_val == false_val + 1) { // Potentially Csinc.
100 ccode = NegateComparison(ccode);
101 std::swap(true_val, false_val);
102 }
103
104 ArmConditionCode code = ArmConditionEncoding(ccode);
105
106 int opcode; // The opcode.
107 RegStorage left_op = RegStorage::InvalidReg(); // The operands.
108 RegStorage right_op = RegStorage::InvalidReg(); // The operands.
109
110 bool is_wide = rs_dest.Is64Bit();
111
112 RegStorage zero_reg = is_wide ? rs_xzr : rs_wzr;
113
114 if (true_val == 0) {
115 left_op = zero_reg;
116 } else {
117 left_op = rs_dest;
118 LoadConstantNoClobber(rs_dest, true_val);
119 }
120 if (false_val == 1) {
121 right_op = zero_reg;
122 opcode = kA64Csinc4rrrc;
123 } else if (false_val == -1) {
124 right_op = zero_reg;
125 opcode = kA64Csinv4rrrc;
126 } else if (false_val == true_val + 1) {
127 right_op = left_op;
128 opcode = kA64Csinc4rrrc;
129 } else if (false_val == -true_val) {
130 right_op = left_op;
131 opcode = kA64Csneg4rrrc;
132 } else if (false_val == ~true_val) {
133 right_op = left_op;
134 opcode = kA64Csinv4rrrc;
135 } else if (true_val == 0) {
136 // left_op is zero_reg.
137 right_op = rs_dest;
138 LoadConstantNoClobber(rs_dest, false_val);
139 opcode = kA64Csel4rrrc;
140 } else {
141 // Generic case.
142 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
143 if (is_wide) {
144 if (t_reg2.Is32Bit()) {
145 t_reg2 = As64BitReg(t_reg2);
146 }
147 } else {
148 if (t_reg2.Is64Bit()) {
149 t_reg2 = As32BitReg(t_reg2);
150 }
151 }
152
153 if (kUseDeltaEncodingInGenSelect) {
154 int32_t delta = false_val - true_val;
155 uint32_t abs_val = delta < 0 ? -delta : delta;
156
157 if (abs_val < 0x1000) { // TODO: Replace with InexpensiveConstant with opcode.
158 // Can encode as immediate to an add.
159 right_op = t_reg2;
160 OpRegRegImm(kOpAdd, t_reg2, left_op, delta);
161 }
162 }
163
164 // Load as constant.
165 if (!right_op.Valid()) {
166 LoadConstantNoClobber(t_reg2, false_val);
167 right_op = t_reg2;
168 }
169
170 opcode = kA64Csel4rrrc;
171 }
172
173 DCHECK(left_op.Valid() && right_op.Valid());
174 NewLIR4(is_wide ? WIDE(opcode) : opcode, rs_dest.GetReg(), left_op.GetReg(), right_op.GetReg(),
175 code);
176}
177
178void Arm64Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
179 int32_t true_val, int32_t false_val, RegStorage rs_dest,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700180 RegisterClass dest_reg_class) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700181 DCHECK(rs_dest.Valid());
182 OpRegReg(kOpCmp, left_op, right_op);
183 GenSelect(true_val, false_val, code, rs_dest, dest_reg_class);
184}
185
186void Arm64Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700187 UNUSED(bb);
Andreas Gampe90969af2014-07-15 23:02:11 -0700188 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
189 rl_src = LoadValue(rl_src, rl_src.ref ? kRefReg : kCoreReg);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700190 // rl_src may be aliased with rl_result/rl_dest, so do compare early.
191 OpRegImm(kOpCmp, rl_src.reg, 0);
192
Andreas Gampe90969af2014-07-15 23:02:11 -0700193 RegLocation rl_dest = mir_graph_->GetDest(mir);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100194
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700195 // The kMirOpSelect has two variants, one for constants and one for moves.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700196 if (mir->ssa_rep->num_uses == 1) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700197 RegLocation rl_result = EvalLoc(rl_dest, rl_dest.ref ? kRefReg : kCoreReg, true);
198 GenSelect(mir->dalvikInsn.vB, mir->dalvikInsn.vC, mir->meta.ccode, rl_result.reg,
199 rl_dest.ref ? kRefReg : kCoreReg);
200 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700201 } else {
202 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
203 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
204
Andreas Gampe90969af2014-07-15 23:02:11 -0700205 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700206 rl_true = LoadValue(rl_true, result_reg_class);
207 rl_false = LoadValue(rl_false, result_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700208 RegLocation rl_result = EvalLoc(rl_dest, result_reg_class, true);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700209
Andreas Gampe90969af2014-07-15 23:02:11 -0700210 bool is_wide = rl_dest.ref || rl_dest.wide;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700211 int opcode = is_wide ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc;
212 NewLIR4(opcode, rl_result.reg.GetReg(),
Andreas Gampe90969af2014-07-15 23:02:11 -0700213 rl_true.reg.GetReg(), rl_false.reg.GetReg(), ArmConditionEncoding(mir->meta.ccode));
214 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700215 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100216}
217
218void Arm64Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
219 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
220 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100221 LIR* taken = &block_label_list_[bb->taken];
222 LIR* not_taken = &block_label_list_[bb->fall_through];
Matteo Franchin43ec8732014-03-31 15:00:14 +0100223 // Normalize such that if either operand is constant, src2 will be constant.
224 ConditionCode ccode = mir->meta.ccode;
225 if (rl_src1.is_const) {
226 std::swap(rl_src1, rl_src2);
227 ccode = FlipComparisonOrder(ccode);
228 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100229
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700230 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
231
Matteo Franchin43ec8732014-03-31 15:00:14 +0100232 if (rl_src2.is_const) {
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700233 // TODO: Optimize for rl_src1.is_const? (Does happen in the boot image at the moment.)
234
Matteo Franchin43ec8732014-03-31 15:00:14 +0100235 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100236 // Special handling using cbz & cbnz.
237 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
238 OpCmpImmBranch(ccode, rl_src1.reg, 0, taken);
239 OpCmpImmBranch(NegateComparison(ccode), rl_src1.reg, 0, not_taken);
240 return;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700241 }
242
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100243 // Only handle Imm if src2 is not already in a register.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700244 rl_src2 = UpdateLocWide(rl_src2);
245 if (rl_src2.location != kLocPhysReg) {
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100246 OpRegImm64(kOpCmp, rl_src1.reg, val);
247 OpCondBranch(ccode, taken);
248 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100249 return;
250 }
251 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100252
Matteo Franchin43ec8732014-03-31 15:00:14 +0100253 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100254 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100255 OpCondBranch(ccode, taken);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100256 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100257}
258
259/*
260 * Generate a register comparison to an immediate and branch. Caller
261 * is responsible for setting branch target field.
262 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100263LIR* Arm64Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value,
264 LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700265 LIR* branch = nullptr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100266 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
Andreas Gampe9522af92014-07-14 20:16:59 -0700267 if (check_value == 0) {
268 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100269 A64Opcode opcode = (arm_cond == kArmCondEq) ? kA64Cbz2rt : kA64Cbnz2rt;
270 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700271 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
272 } else if (arm_cond == kArmCondLs) {
273 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
274 // This case happens for a bounds check of array[0].
Matteo Franchin4163c532014-07-15 15:20:27 +0100275 A64Opcode opcode = kA64Cbz2rt;
276 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700277 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800278 } else if (arm_cond == kArmCondLt || arm_cond == kArmCondGe) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100279 A64Opcode opcode = (arm_cond == kArmCondLt) ? kA64Tbnz3rht : kA64Tbz3rht;
280 A64Opcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
Zheng Xu5d7cdec2014-08-18 17:28:22 +0800281 int value = reg.Is64Bit() ? 63 : 31;
282 branch = NewLIR3(opcode | wide, reg.GetReg(), value, 0);
Andreas Gampe9522af92014-07-14 20:16:59 -0700283 }
284 }
285
286 if (branch == nullptr) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100287 OpRegImm(kOpCmp, reg, check_value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100288 branch = NewLIR2(kA64B2ct, arm_cond, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100289 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700290
Matteo Franchin43ec8732014-03-31 15:00:14 +0100291 branch->target = target;
292 return branch;
293}
294
Zheng Xu7c1c2632014-06-17 18:17:31 +0800295LIR* Arm64Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg,
296 RegStorage base_reg, int offset, int check_value,
Dave Allison69dfe512014-07-11 17:11:58 +0000297 LIR* target, LIR** compare) {
298 DCHECK(compare == nullptr);
Zheng Xu7c1c2632014-06-17 18:17:31 +0800299 // It is possible that temp register is 64-bit. (ArgReg or RefReg)
300 // Always compare 32-bit value no matter what temp_reg is.
301 if (temp_reg.Is64Bit()) {
302 temp_reg = As32BitReg(temp_reg);
303 }
304 Load32Disp(base_reg, offset, temp_reg);
305 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
306 return branch;
307}
308
Matteo Franchin43ec8732014-03-31 15:00:14 +0100309LIR* Arm64Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100310 bool dest_is_fp = r_dest.IsFloat();
311 bool src_is_fp = r_src.IsFloat();
Matteo Franchin4163c532014-07-15 15:20:27 +0100312 A64Opcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100313 LIR* res;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100314
315 if (LIKELY(dest_is_fp == src_is_fp)) {
316 if (LIKELY(!dest_is_fp)) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700317 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
318
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100319 // Core/core copy.
320 // Copies involving the sp register require a different instruction.
321 opcode = UNLIKELY(A64_REG_IS_SP(r_dest.GetReg())) ? kA64Add4RRdT : kA64Mov2rr;
322
323 // TODO(Arm64): kA64Add4RRdT formally has 4 args, but is used as a 2 args instruction.
324 // This currently works because the other arguments are set to 0 by default. We should
325 // rather introduce an alias kA64Mov2RR.
326
327 // core/core copy. Do a x/x copy only if both registers are x.
328 if (r_dest.Is64Bit() && r_src.Is64Bit()) {
329 opcode = WIDE(opcode);
330 }
331 } else {
332 // Float/float copy.
333 bool dest_is_double = r_dest.IsDouble();
334 bool src_is_double = r_src.IsDouble();
335
336 // We do not do float/double or double/float casts here.
337 DCHECK_EQ(dest_is_double, src_is_double);
338
339 // Homogeneous float/float copy.
Matteo Franchin4163c532014-07-15 15:20:27 +0100340 opcode = (dest_is_double) ? WIDE(kA64Fmov2ff) : kA64Fmov2ff;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100341 }
342 } else {
343 // Inhomogeneous register copy.
344 if (dest_is_fp) {
345 if (r_dest.IsDouble()) {
346 opcode = kA64Fmov2Sx;
347 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700348 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100349 opcode = kA64Fmov2sw;
350 }
351 } else {
352 if (r_src.IsDouble()) {
353 opcode = kA64Fmov2xS;
354 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700355 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100356 opcode = kA64Fmov2ws;
357 }
358 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100359 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100360
Matteo Franchin43ec8732014-03-31 15:00:14 +0100361 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100362
Matteo Franchin43ec8732014-03-31 15:00:14 +0100363 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
364 res->flags.is_nop = true;
365 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100366
Matteo Franchin43ec8732014-03-31 15:00:14 +0100367 return res;
368}
369
370void Arm64Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
371 if (r_dest != r_src) {
372 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
373 AppendLIR(res);
374 }
375}
376
377void Arm64Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100378 OpRegCopy(r_dest, r_src);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100379}
380
381// Table of magic divisors
382struct MagicTable {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100383 int magic64_base;
384 int magic64_eor;
385 uint64_t magic64;
386 uint32_t magic32;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100387 uint32_t shift;
388 DividePattern pattern;
389};
390
391static const MagicTable magic_table[] = {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100392 { 0, 0, 0, 0, 0, DivideNone}, // 0
393 { 0, 0, 0, 0, 0, DivideNone}, // 1
394 { 0, 0, 0, 0, 0, DivideNone}, // 2
395 {0x3c, -1, 0x5555555555555556, 0x55555556, 0, Divide3}, // 3
396 { 0, 0, 0, 0, 0, DivideNone}, // 4
397 {0xf9, -1, 0x6666666666666667, 0x66666667, 1, Divide5}, // 5
398 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 0, Divide3}, // 6
399 { -1, -1, 0x924924924924924A, 0x92492493, 2, Divide7}, // 7
400 { 0, 0, 0, 0, 0, DivideNone}, // 8
401 { -1, -1, 0x38E38E38E38E38E4, 0x38E38E39, 1, Divide5}, // 9
402 {0xf9, -1, 0x6666666666666667, 0x66666667, 2, Divide5}, // 10
403 { -1, -1, 0x2E8BA2E8BA2E8BA3, 0x2E8BA2E9, 1, Divide5}, // 11
404 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 1, Divide5}, // 12
405 { -1, -1, 0x4EC4EC4EC4EC4EC5, 0x4EC4EC4F, 2, Divide5}, // 13
406 { -1, -1, 0x924924924924924A, 0x92492493, 3, Divide7}, // 14
407 {0x78, -1, 0x8888888888888889, 0x88888889, 3, Divide7}, // 15
Matteo Franchin43ec8732014-03-31 15:00:14 +0100408};
409
410// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
411bool Arm64Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100412 RegLocation rl_src, RegLocation rl_dest, int lit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700413 UNUSED(dalvik_opcode);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100414 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100415 return false;
416 }
417 DividePattern pattern = magic_table[lit].pattern;
418 if (pattern == DivideNone) {
419 return false;
420 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100421 // Tuning: add rem patterns
422 if (!is_div) {
423 return false;
424 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100425
426 RegStorage r_magic = AllocTemp();
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100427 LoadConstant(r_magic, magic_table[lit].magic32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100428 rl_src = LoadValue(rl_src, kCoreReg);
429 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100430 RegStorage r_long_mul = AllocTemp();
Matteo Franchin65420b22014-10-27 13:29:30 +0000431 NewLIR3(kA64Smull3xww, As64BitReg(r_long_mul).GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
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(),
Matteo Franchin65420b22014-10-27 13:29:30 +0000651 r_src2.GetReg(), r_src1.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) {
Elliott Hughes8366ca02014-11-17 12:02:05 -0800982 if (!cu_->GetInstructionSetFeatures()->IsSmp()) {
983 return false;
984 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100985 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
986 LIR* barrier = last_lir_insn_;
987
988 int dmb_flavor;
989 // TODO: revisit Arm barrier kinds
990 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700991 case kAnyStore: dmb_flavor = kISH; break;
992 case kLoadAny: dmb_flavor = kISH; break;
993 // We conjecture that kISHLD is insufficient. It is documented
994 // to provide LoadLoad | StoreStore ordering. But if this were used
995 // to implement volatile loads, we suspect that the lack of store
996 // atomicity on ARM would cause us to allow incorrect results for
997 // the canonical IRIW example. But we're not sure.
998 // We should be using acquire loads instead.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100999 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -07001000 case kAnyAny: dmb_flavor = kISH; break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001001 default:
1002 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
1003 dmb_flavor = kSY; // quiet gcc.
1004 break;
1005 }
1006
Andreas Gampeb14329f2014-05-15 11:16:06 -07001007 bool ret = false;
1008
Matteo Franchin43ec8732014-03-31 15:00:14 +01001009 // If the same barrier already exists, don't generate another.
1010 if (barrier == nullptr
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001011 || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) {
1012 barrier = NewLIR1(kA64Dmb1B, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -07001013 ret = true;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001014 }
1015
1016 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
1017 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001018 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -07001019 return ret;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001020}
1021
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001022void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
1023 RegLocation rl_result;
1024
1025 rl_src = LoadValue(rl_src, kCoreReg);
1026 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001027 NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001028 StoreValueWide(rl_dest, rl_result);
1029}
1030
1031void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001032 RegLocation rl_src1, RegLocation rl_src2, bool is_div, int flags) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +01001033 if (rl_src2.is_const) {
1034 DCHECK(rl_src2.wide);
1035 int64_t lit = mir_graph_->ConstantValueWide(rl_src2);
1036 if (HandleEasyDivRem64(opcode, is_div, rl_src1, rl_dest, lit)) {
1037 return;
1038 }
1039 }
1040
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001041 RegLocation rl_result;
1042 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1043 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001044 if ((flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
1045 GenDivZeroCheck(rl_src2.reg);
1046 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001047 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001048 StoreValueWide(rl_dest, rl_result);
1049}
1050
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001051void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1,
1052 RegLocation rl_src2) {
1053 RegLocation rl_result;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001054
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001055 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1056 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1057 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001058 OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT);
1059 StoreValueWide(rl_dest, rl_result);
1060}
1061
1062void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
1063 RegLocation rl_result;
1064
1065 rl_src = LoadValueWide(rl_src, kCoreReg);
1066 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1067 OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
1068 StoreValueWide(rl_dest, rl_result);
1069}
1070
1071void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
1072 RegLocation rl_result;
1073
1074 rl_src = LoadValueWide(rl_src, kCoreReg);
1075 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
1076 OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001077 StoreValueWide(rl_dest, rl_result);
1078}
1079
Andreas Gampec76c6142014-08-04 16:30:03 -07001080void Arm64Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001081 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Andreas Gampec76c6142014-08-04 16:30:03 -07001082 switch (opcode) {
1083 case Instruction::NOT_LONG:
1084 GenNotLong(rl_dest, rl_src2);
1085 return;
1086 case Instruction::ADD_LONG:
1087 case Instruction::ADD_LONG_2ADDR:
1088 GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2);
1089 return;
1090 case Instruction::SUB_LONG:
1091 case Instruction::SUB_LONG_2ADDR:
1092 GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2);
1093 return;
1094 case Instruction::MUL_LONG:
1095 case Instruction::MUL_LONG_2ADDR:
1096 GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2);
1097 return;
1098 case Instruction::DIV_LONG:
1099 case Instruction::DIV_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001100 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ true, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001101 return;
1102 case Instruction::REM_LONG:
1103 case Instruction::REM_LONG_2ADDR:
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001104 GenDivRemLong(opcode, rl_dest, rl_src1, rl_src2, /*is_div*/ false, flags);
Andreas Gampec76c6142014-08-04 16:30:03 -07001105 return;
1106 case Instruction::AND_LONG_2ADDR:
1107 case Instruction::AND_LONG:
1108 GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2);
1109 return;
1110 case Instruction::OR_LONG:
1111 case Instruction::OR_LONG_2ADDR:
1112 GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2);
1113 return;
1114 case Instruction::XOR_LONG:
1115 case Instruction::XOR_LONG_2ADDR:
1116 GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2);
1117 return;
1118 case Instruction::NEG_LONG: {
1119 GenNegLong(rl_dest, rl_src2);
1120 return;
1121 }
1122 default:
1123 LOG(FATAL) << "Invalid long arith op";
1124 return;
1125 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001126}
1127
1128/*
1129 * Generate array load
1130 */
1131void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
1132 RegLocation rl_index, RegLocation rl_dest, int scale) {
1133 RegisterClass reg_class = RegClassBySize(size);
1134 int len_offset = mirror::Array::LengthOffset().Int32Value();
1135 int data_offset;
1136 RegLocation rl_result;
1137 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -07001138 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001139 if (!constant_index) {
1140 rl_index = LoadValue(rl_index, kCoreReg);
1141 }
1142
1143 if (rl_dest.wide) {
1144 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1145 } else {
1146 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1147 }
1148
Matteo Franchin43ec8732014-03-31 15:00:14 +01001149 /* null object? */
1150 GenNullCheck(rl_array.reg, opt_flags);
1151
1152 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1153 RegStorage reg_len;
1154 if (needs_range_check) {
1155 reg_len = AllocTemp();
1156 /* Get len */
1157 Load32Disp(rl_array.reg, len_offset, reg_len);
1158 MarkPossibleNullPointerException(opt_flags);
1159 } else {
1160 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1161 }
Vladimir Markoe08785b2014-11-07 16:11:00 +00001162 if (constant_index) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001163 rl_result = EvalLoc(rl_dest, reg_class, true);
1164
1165 if (needs_range_check) {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001166 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001167 FreeTemp(reg_len);
1168 }
Vladimir Markoe08785b2014-11-07 16:11:00 +00001169 // Fold the constant index into the data offset.
1170 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
Andreas Gampe3c12c512014-06-24 18:46:29 +00001171 if (rl_result.ref) {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001172 LoadRefDisp(rl_array.reg, data_offset, rl_result.reg, kNotVolatile);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001173 } else {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001174 LoadBaseDisp(rl_array.reg, data_offset, rl_result.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001175 }
1176 } else {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001177 // Offset base, then use indexed load.
buzbeea0cd2d72014-06-01 09:33:49 -07001178 RegStorage reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001179 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1180 FreeTemp(rl_array.reg);
1181 rl_result = EvalLoc(rl_dest, reg_class, true);
1182
1183 if (needs_range_check) {
1184 GenArrayBoundsCheck(rl_index.reg, reg_len);
1185 FreeTemp(reg_len);
1186 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001187 if (rl_result.ref) {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001188 LoadRefIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001189 } else {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001190 LoadBaseIndexed(reg_ptr, rl_index.reg, rl_result.reg, scale, size);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001191 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001192 FreeTemp(reg_ptr);
Vladimir Markoe08785b2014-11-07 16:11:00 +00001193 }
1194 if (rl_dest.wide) {
1195 StoreValueWide(rl_dest, rl_result);
1196 } else {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001197 StoreValue(rl_dest, rl_result);
1198 }
1199}
1200
1201/*
1202 * Generate array store
1203 *
1204 */
1205void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
1206 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
1207 RegisterClass reg_class = RegClassBySize(size);
1208 int len_offset = mirror::Array::LengthOffset().Int32Value();
1209 bool constant_index = rl_index.is_const;
1210
1211 int data_offset;
1212 if (size == k64 || size == kDouble) {
1213 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1214 } else {
1215 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1216 }
1217
buzbeea0cd2d72014-06-01 09:33:49 -07001218 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001219 if (!constant_index) {
1220 rl_index = LoadValue(rl_index, kCoreReg);
1221 }
1222
1223 RegStorage reg_ptr;
1224 bool allocated_reg_ptr_temp = false;
1225 if (constant_index) {
1226 reg_ptr = rl_array.reg;
1227 } else if (IsTemp(rl_array.reg) && !card_mark) {
1228 Clobber(rl_array.reg);
1229 reg_ptr = rl_array.reg;
1230 } else {
1231 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001232 reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001233 }
1234
1235 /* null object? */
1236 GenNullCheck(rl_array.reg, opt_flags);
1237
1238 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1239 RegStorage reg_len;
1240 if (needs_range_check) {
1241 reg_len = AllocTemp();
1242 // NOTE: max live temps(4) here.
1243 /* Get len */
1244 Load32Disp(rl_array.reg, len_offset, reg_len);
1245 MarkPossibleNullPointerException(opt_flags);
1246 } else {
1247 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1248 }
1249 /* at this point, reg_ptr points to array, 2 live temps */
Vladimir Markoe08785b2014-11-07 16:11:00 +00001250 if (rl_src.wide) {
1251 rl_src = LoadValueWide(rl_src, reg_class);
1252 } else {
1253 rl_src = LoadValue(rl_src, reg_class);
1254 }
1255 if (constant_index) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001256 if (needs_range_check) {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001257 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001258 FreeTemp(reg_len);
1259 }
Vladimir Markoe08785b2014-11-07 16:11:00 +00001260 // Fold the constant index into the data offset.
1261 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
Andreas Gampe3c12c512014-06-24 18:46:29 +00001262 if (rl_src.ref) {
1263 StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile);
1264 } else {
1265 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
1266 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001267 } else {
1268 /* reg_ptr -> array data */
1269 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001270 if (needs_range_check) {
1271 GenArrayBoundsCheck(rl_index.reg, reg_len);
1272 FreeTemp(reg_len);
1273 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001274 if (rl_src.ref) {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001275 StoreRefIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001276 } else {
Vladimir Markoe08785b2014-11-07 16:11:00 +00001277 StoreBaseIndexed(reg_ptr, rl_index.reg, rl_src.reg, scale, size);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001278 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001279 }
1280 if (allocated_reg_ptr_temp) {
1281 FreeTemp(reg_ptr);
1282 }
1283 if (card_mark) {
1284 MarkGCCard(rl_src.reg, rl_array.reg);
1285 }
1286}
1287
Matteo Franchin43ec8732014-03-31 15:00:14 +01001288void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001289 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001290 int flags ATTRIBUTE_UNUSED) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001291 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001292 // Per spec, we only care about low 6 bits of shift amount.
1293 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001294 rl_src = LoadValueWide(rl_src, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001295 if (shift_amount == 0) {
1296 StoreValueWide(rl_dest, rl_src);
1297 return;
1298 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001299
1300 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001301 switch (opcode) {
1302 case Instruction::SHL_LONG:
1303 case Instruction::SHL_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001304 op = kOpLsl;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001305 break;
1306 case Instruction::SHR_LONG:
1307 case Instruction::SHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001308 op = kOpAsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001309 break;
1310 case Instruction::USHR_LONG:
1311 case Instruction::USHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001312 op = kOpLsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001313 break;
1314 default:
1315 LOG(FATAL) << "Unexpected case";
1316 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001317 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001318 StoreValueWide(rl_dest, rl_result);
1319}
1320
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001321void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001322 RegLocation rl_src1, RegLocation rl_src2, int flags) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001323 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001324 switch (opcode) {
1325 case Instruction::ADD_LONG:
1326 case Instruction::ADD_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001327 op = kOpAdd;
1328 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001329 case Instruction::SUB_LONG:
1330 case Instruction::SUB_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001331 op = kOpSub;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001332 break;
1333 case Instruction::AND_LONG:
1334 case Instruction::AND_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001335 op = kOpAnd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001336 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001337 case Instruction::OR_LONG:
1338 case Instruction::OR_LONG_2ADDR:
1339 op = kOpOr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001340 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001341 case Instruction::XOR_LONG:
1342 case Instruction::XOR_LONG_2ADDR:
1343 op = kOpXor;
1344 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001345 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001346 LOG(FATAL) << "Unexpected opcode";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001347 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001348
Matteo Franchinc763e352014-07-04 12:53:27 +01001349 if (op == kOpSub) {
1350 if (!rl_src2.is_const) {
Razvan A Lupusoru5c5676b2014-09-29 16:42:11 -07001351 return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2, flags);
Matteo Franchinc763e352014-07-04 12:53:27 +01001352 }
1353 } else {
1354 // Associativity.
1355 if (!rl_src2.is_const) {
1356 DCHECK(rl_src1.is_const);
1357 std::swap(rl_src1, rl_src2);
1358 }
1359 }
1360 DCHECK(rl_src2.is_const);
1361 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1362
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001363 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1364 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +08001365 OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001366 StoreValueWide(rl_dest, rl_result);
1367}
1368
Andreas Gampef29ecd62014-07-29 00:35:00 -07001369static uint32_t ExtractReg(uint32_t reg_mask, int* reg) {
1370 // Find first register.
1371 int first_bit_set = CTZ(reg_mask) + 1;
1372 *reg = *reg + first_bit_set;
1373 reg_mask >>= first_bit_set;
1374 return reg_mask;
1375}
1376
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001377/**
1378 * @brief Split a register list in pairs or registers.
1379 *
1380 * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows:
1381 * @code
1382 * int reg1 = -1, reg2 = -1;
1383 * while (reg_mask) {
1384 * reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1385 * if (UNLIKELY(reg2 < 0)) {
1386 * // Single register in reg1.
1387 * } else {
1388 * // Pair in reg1, reg2.
1389 * }
1390 * }
1391 * @endcode
1392 */
Andreas Gampef29ecd62014-07-29 00:35:00 -07001393static uint32_t GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001394 // Find first register.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001395 int first_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001396 int reg = *reg1 + first_bit_set;
1397 reg_mask >>= first_bit_set;
1398
1399 if (LIKELY(reg_mask)) {
1400 // Save the first register, find the second and use the pair opcode.
Andreas Gampef29ecd62014-07-29 00:35:00 -07001401 int second_bit_set = CTZ(reg_mask) + 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001402 *reg2 = reg;
1403 reg_mask >>= second_bit_set;
1404 *reg1 = reg + second_bit_set;
1405 return reg_mask;
1406 }
1407
1408 // Use the single opcode, as we just have one register.
1409 *reg1 = reg;
1410 *reg2 = -1;
1411 return reg_mask;
1412}
1413
Andreas Gampef29ecd62014-07-29 00:35:00 -07001414static void SpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001415 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001416 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001417
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001418 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001419 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1420 if (UNLIKELY(reg2 < 0)) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001421 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001422 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001423 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1424 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001425 }
1426 }
1427}
1428
1429// TODO(Arm64): consider using ld1 and st1?
Andreas Gampef29ecd62014-07-29 00:35:00 -07001430static void SpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001431 int reg1 = -1, reg2 = -1;
1432 const int reg_log2_size = 3;
1433
1434 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1435 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1436 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001437 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001438 offset);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001439 } else {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001440 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1441 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001442 }
1443 }
1444}
1445
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001446static int SpillRegsPreSub(Arm64Mir2Lir* m2l, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1447 int frame_size) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001448 m2l->OpRegRegImm(kOpSub, rs_sp, rs_sp, frame_size);
1449
1450 int core_count = POPCOUNT(core_reg_mask);
1451
1452 if (fp_reg_mask != 0) {
1453 // Spill FP regs.
1454 int fp_count = POPCOUNT(fp_reg_mask);
1455 int spill_offset = frame_size - (core_count + fp_count) * kArm64PointerSize;
1456 SpillFPRegs(m2l, rs_sp, spill_offset, fp_reg_mask);
1457 }
1458
1459 if (core_reg_mask != 0) {
1460 // Spill core regs.
1461 int spill_offset = frame_size - (core_count * kArm64PointerSize);
1462 SpillCoreRegs(m2l, rs_sp, spill_offset, core_reg_mask);
1463 }
1464
1465 return frame_size;
1466}
1467
1468static int SpillRegsPreIndexed(Arm64Mir2Lir* m2l, RegStorage base, uint32_t core_reg_mask,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001469 uint32_t fp_reg_mask) {
Andreas Gampef29ecd62014-07-29 00:35:00 -07001470 // Otherwise, spill both core and fp regs at the same time.
1471 // The very first instruction will be an stp with pre-indexed address, moving the stack pointer
1472 // down. From then on, we fill upwards. This will generate overall the same number of instructions
1473 // as the specialized code above in most cases (exception being odd number of core and even
1474 // non-zero fp spills), but is more flexible, as the offsets are guaranteed small.
1475 //
1476 // Some demonstrative fill cases : (c) = core, (f) = fp
1477 // cc 44 cc 44 cc 22 cc 33 fc => 1[1/2]
1478 // fc => 23 fc => 23 ff => 11 ff => 22
1479 // ff 11 f 11 f 11
1480 //
1481 int reg1 = -1, reg2 = -1;
1482 int core_count = POPCOUNT(core_reg_mask);
1483 int fp_count = POPCOUNT(fp_reg_mask);
1484
1485 int combined = fp_count + core_count;
1486 int all_offset = RoundUp(combined, 2); // Needs to be 16B = 2-reg aligned.
1487
1488 int cur_offset = 2; // What's the starting offset after the first stp? We expect the base slot
1489 // to be filled.
1490
1491 // First figure out whether the bottom is FP or core.
1492 if (fp_count > 0) {
1493 // Some FP spills.
1494 //
1495 // Four cases: (d0 is dummy to fill up stp)
1496 // 1) Single FP, even number of core -> stp d0, fp_reg
1497 // 2) Single FP, odd number of core -> stp fp_reg, d0
1498 // 3) More FP, even number combined -> stp fp_reg1, fp_reg2
1499 // 4) More FP, odd number combined -> stp d0, fp_reg
1500 if (fp_count == 1) {
1501 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1502 DCHECK_EQ(fp_reg_mask, 0U);
1503 if (core_count % 2 == 0) {
1504 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1505 RegStorage::FloatSolo64(reg1).GetReg(),
1506 RegStorage::FloatSolo64(reg1).GetReg(),
1507 base.GetReg(), -all_offset);
1508 } else {
1509 m2l->NewLIR4(WIDE(kA64StpPre4ffXD),
1510 RegStorage::FloatSolo64(reg1).GetReg(),
1511 RegStorage::FloatSolo64(reg1).GetReg(),
1512 base.GetReg(), -all_offset);
1513 cur_offset = 0; // That core reg needs to go into the upper half.
1514 }
1515 } else {
1516 if (combined % 2 == 0) {
1517 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1518 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1519 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), -all_offset);
1520 } else {
1521 fp_reg_mask = ExtractReg(fp_reg_mask, &reg1);
1522 m2l->NewLIR4(WIDE(kA64StpPre4ffXD), rs_d0.GetReg(), RegStorage::FloatSolo64(reg1).GetReg(),
1523 base.GetReg(), -all_offset);
1524 }
1525 }
1526 } else {
1527 // No FP spills.
1528 //
1529 // Two cases:
1530 // 1) Even number of core -> stp core1, core2
1531 // 2) Odd number of core -> stp xzr, core1
1532 if (core_count % 2 == 1) {
1533 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1534 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), rs_xzr.GetReg(),
1535 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1536 } else {
1537 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1538 m2l->NewLIR4(WIDE(kA64StpPre4rrXD), RegStorage::Solo64(reg2).GetReg(),
1539 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), -all_offset);
1540 }
1541 }
1542
1543 if (fp_count != 0) {
1544 for (; fp_reg_mask != 0;) {
1545 // Have some FP regs to do.
1546 fp_reg_mask = GenPairWise(fp_reg_mask, &reg1, &reg2);
1547 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001548 m2l->NewLIR3(WIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001549 cur_offset);
1550 // Do not increment offset here, as the second half will be filled by a core reg.
1551 } else {
1552 m2l->NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1553 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), cur_offset);
1554 cur_offset += 2;
1555 }
1556 }
1557
1558 // Reset counting.
1559 reg1 = -1;
1560
1561 // If there is an odd number of core registers, we need to store the bottom now.
1562 if (core_count % 2 == 1) {
1563 core_reg_mask = ExtractReg(core_reg_mask, &reg1);
1564 m2l->NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(),
1565 cur_offset + 1);
1566 cur_offset += 2; // Half-slot filled now.
1567 }
1568 }
1569
1570 // Spill the rest of the core regs. They are guaranteed to be even.
1571 DCHECK_EQ(POPCOUNT(core_reg_mask) % 2, 0);
1572 for (; core_reg_mask != 0; cur_offset += 2) {
1573 core_reg_mask = GenPairWise(core_reg_mask, &reg1, &reg2);
1574 m2l->NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1575 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), cur_offset);
1576 }
1577
1578 DCHECK_EQ(cur_offset, all_offset);
1579
1580 return all_offset * 8;
1581}
1582
1583int Arm64Mir2Lir::SpillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1584 int frame_size) {
1585 // If the frame size is small enough that all offsets would fit into the immediates, use that
1586 // setup, as it decrements sp early (kind of instruction scheduling), and is not worse
1587 // instruction-count wise than the complicated code below.
1588 //
1589 // This case is also optimal when we have an odd number of core spills, and an even (non-zero)
1590 // number of fp spills.
1591 if ((RoundUp(frame_size, 8) / 8 <= 63)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001592 return SpillRegsPreSub(this, core_reg_mask, fp_reg_mask, frame_size);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001593 } else {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001594 return SpillRegsPreIndexed(this, base, core_reg_mask, fp_reg_mask);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001595 }
1596}
1597
1598static void UnSpillCoreRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1599 int reg1 = -1, reg2 = -1;
1600 const int reg_log2_size = 3;
1601
1602 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1603 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1604 if (UNLIKELY(reg2 < 0)) {
1605 m2l->NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1606 } else {
1607 DCHECK_LE(offset, 63);
1608 m2l->NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1609 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1610 }
1611 }
1612}
1613
1614static void UnSpillFPRegs(Arm64Mir2Lir* m2l, RegStorage base, int offset, uint32_t reg_mask) {
1615 int reg1 = -1, reg2 = -1;
1616 const int reg_log2_size = 3;
1617
1618 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1619 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1620 if (UNLIKELY(reg2 < 0)) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001621 m2l->NewLIR3(WIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(),
Andreas Gampef29ecd62014-07-29 00:35:00 -07001622 offset);
1623 } else {
1624 m2l->NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1625 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1626 }
1627 }
1628}
1629
1630void Arm64Mir2Lir::UnspillRegs(RegStorage base, uint32_t core_reg_mask, uint32_t fp_reg_mask,
1631 int frame_size) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001632 DCHECK_EQ(base, rs_sp);
Andreas Gampef29ecd62014-07-29 00:35:00 -07001633 // Restore saves and drop stack frame.
1634 // 2 versions:
1635 //
1636 // 1. (Original): Try to address directly, then drop the whole frame.
1637 // Limitation: ldp is a 7b signed immediate.
1638 //
1639 // 2. (New): Drop the non-save-part. Then do similar to original, which is now guaranteed to be
1640 // in range. Then drop the rest.
1641 //
1642 // TODO: In methods with few spills but huge frame, it would be better to do non-immediate loads
1643 // in variant 1.
1644
1645 // "Magic" constant, 63 (max signed 7b) * 8.
1646 static constexpr int kMaxFramesizeForOffset = 63 * kArm64PointerSize;
1647
1648 const int num_core_spills = POPCOUNT(core_reg_mask);
1649 const int num_fp_spills = POPCOUNT(fp_reg_mask);
1650
1651 int early_drop = 0;
1652
1653 if (frame_size > kMaxFramesizeForOffset) {
1654 // Second variant. Drop the frame part.
1655
1656 // TODO: Always use the first formula, as num_fp_spills would be zero?
1657 if (fp_reg_mask != 0) {
1658 early_drop = frame_size - kArm64PointerSize * (num_fp_spills + num_core_spills);
1659 } else {
1660 early_drop = frame_size - kArm64PointerSize * num_core_spills;
1661 }
1662
1663 // Drop needs to be 16B aligned, so that SP keeps aligned.
1664 early_drop = RoundDown(early_drop, 16);
1665
1666 OpRegImm64(kOpAdd, rs_sp, early_drop);
1667 }
1668
1669 // Unspill.
1670 if (fp_reg_mask != 0) {
1671 int offset = frame_size - early_drop - kArm64PointerSize * (num_fp_spills + num_core_spills);
1672 UnSpillFPRegs(this, rs_sp, offset, fp_reg_mask);
1673 }
1674 if (core_reg_mask != 0) {
1675 int offset = frame_size - early_drop - kArm64PointerSize * num_core_spills;
1676 UnSpillCoreRegs(this, rs_sp, offset, core_reg_mask);
1677 }
1678
1679 // Drop the (rest of) the frame.
1680 OpRegImm64(kOpAdd, rs_sp, frame_size - early_drop);
1681}
1682
Serban Constantinescu23abec92014-07-02 16:13:38 +01001683bool Arm64Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
Matteo Franchin4163c532014-07-15 15:20:27 +01001684 A64Opcode wide = IsWide(size) ? WIDE(0) : UNWIDE(0);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001685 RegLocation rl_src_i = info->args[0];
Fred Shih37f05ef2014-07-16 18:38:08 -07001686 RegLocation rl_dest = IsWide(size) ? InlineTargetWide(info) : InlineTarget(info); // result reg
Serban Constantinescu23abec92014-07-02 16:13:38 +01001687 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Fred Shih37f05ef2014-07-16 18:38:08 -07001688 RegLocation rl_i = IsWide(size) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001689 NewLIR2(kA64Rbit2rr | wide, rl_result.reg.GetReg(), rl_i.reg.GetReg());
Fred Shih37f05ef2014-07-16 18:38:08 -07001690 IsWide(size) ? StoreValueWide(rl_dest, rl_result) : StoreValue(rl_dest, rl_result);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001691 return true;
1692}
1693
Matteo Franchin43ec8732014-03-31 15:00:14 +01001694} // namespace art