blob: 51c8723d8cca5ccfee7afbe0f3ca8f19338e6219 [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"
24#include "mirror/array.h"
25
26namespace art {
27
28LIR* Arm64Mir2Lir::OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target) {
29 OpRegReg(kOpCmp, src1, src2);
30 return OpCondBranch(cond, target);
31}
32
Matteo Franchin43ec8732014-03-31 15:00:14 +010033LIR* Arm64Mir2Lir::OpIT(ConditionCode ccode, const char* guide) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010034 LOG(FATAL) << "Unexpected use of OpIT for Arm64";
35 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +010036}
37
38void Arm64Mir2Lir::OpEndIT(LIR* it) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010039 LOG(FATAL) << "Unexpected use of OpEndIT for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +010040}
41
42/*
43 * 64-bit 3way compare function.
Matteo Franchine45fb9e2014-05-06 10:10:30 +010044 * cmp xA, xB
Zheng Xu511c8a62014-06-03 16:22:23 +080045 * csinc wC, wzr, wzr, eq // wC = (xA == xB) ? 0 : 1
46 * csneg wC, wC, wC, ge // wC = (xA >= xB) ? wC : -wC
Matteo Franchin43ec8732014-03-31 15:00:14 +010047 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +010048void Arm64Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
49 RegLocation rl_src2) {
50 RegLocation rl_result;
Matteo Franchin43ec8732014-03-31 15:00:14 +010051 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
52 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010053 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +010054
Matteo Franchine45fb9e2014-05-06 10:10:30 +010055 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Zheng Xu511c8a62014-06-03 16:22:23 +080056 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondEq);
57 NewLIR4(kA64Csneg4rrrc, rl_result.reg.GetReg(), rl_result.reg.GetReg(),
58 rl_result.reg.GetReg(), kArmCondGe);
59 StoreValue(rl_dest, rl_result);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010060}
61
62void Arm64Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
63 RegLocation rl_src1, RegLocation rl_shift) {
64 OpKind op = kOpBkpt;
65 switch (opcode) {
66 case Instruction::SHL_LONG:
67 case Instruction::SHL_LONG_2ADDR:
68 op = kOpLsl;
69 break;
70 case Instruction::SHR_LONG:
71 case Instruction::SHR_LONG_2ADDR:
72 op = kOpAsr;
73 break;
74 case Instruction::USHR_LONG:
75 case Instruction::USHR_LONG_2ADDR:
76 op = kOpLsr;
77 break;
78 default:
79 LOG(FATAL) << "Unexpected case: " << opcode;
80 }
Zheng Xue2eb29e2014-06-12 10:22:33 +080081 rl_shift = LoadValue(rl_shift, kCoreReg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +010082 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
83 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +080084 OpRegRegReg(op, rl_result.reg, rl_src1.reg, As64BitReg(rl_shift.reg));
Serban Constantinescued65c5e2014-05-22 15:10:18 +010085 StoreValueWide(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +010086}
87
Matteo Franchin43ec8732014-03-31 15:00:14 +010088void Arm64Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
89 RegLocation rl_result;
90 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
91 RegLocation rl_dest = mir_graph_->GetDest(mir);
buzbeea0cd2d72014-06-01 09:33:49 -070092 RegisterClass src_reg_class = rl_src.ref ? kRefReg : kCoreReg;
93 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
94 rl_src = LoadValue(rl_src, src_reg_class);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +010095 ArmConditionCode code = ArmConditionEncoding(mir->meta.ccode);
96
97 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
98 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
buzbeea0cd2d72014-06-01 09:33:49 -070099 rl_true = LoadValue(rl_true, result_reg_class);
100 rl_false = LoadValue(rl_false, result_reg_class);
101 rl_result = EvalLoc(rl_dest, result_reg_class, true);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100102 OpRegImm(kOpCmp, rl_src.reg, 0);
103 NewLIR4(kA64Csel4rrrc, rl_result.reg.GetReg(), rl_true.reg.GetReg(),
104 rl_false.reg.GetReg(), code);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100105 StoreValue(rl_dest, rl_result);
106}
107
108void Arm64Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
109 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
110 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100111 LIR* taken = &block_label_list_[bb->taken];
112 LIR* not_taken = &block_label_list_[bb->fall_through];
113 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100114 // Normalize such that if either operand is constant, src2 will be constant.
115 ConditionCode ccode = mir->meta.ccode;
116 if (rl_src1.is_const) {
117 std::swap(rl_src1, rl_src2);
118 ccode = FlipComparisonOrder(ccode);
119 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100120
Matteo Franchin43ec8732014-03-31 15:00:14 +0100121 if (rl_src2.is_const) {
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100122 rl_src2 = UpdateLocWide(rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100123 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100124 // Special handling using cbz & cbnz.
125 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
126 OpCmpImmBranch(ccode, rl_src1.reg, 0, taken);
127 OpCmpImmBranch(NegateComparison(ccode), rl_src1.reg, 0, not_taken);
128 return;
129 // Only handle Imm if src2 is not already in a register.
130 } else if (rl_src2.location != kLocPhysReg) {
131 OpRegImm64(kOpCmp, rl_src1.reg, val);
132 OpCondBranch(ccode, taken);
133 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100134 return;
135 }
136 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100137
Matteo Franchin43ec8732014-03-31 15:00:14 +0100138 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100139 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100140 OpCondBranch(ccode, taken);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100141 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100142}
143
144/*
145 * Generate a register comparison to an immediate and branch. Caller
146 * is responsible for setting branch target field.
147 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100148LIR* Arm64Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value,
149 LIR* target) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100150 LIR* branch;
151 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100152 if (check_value == 0 && (arm_cond == kArmCondEq || arm_cond == kArmCondNe)) {
153 ArmOpcode opcode = (arm_cond == kArmCondEq) ? kA64Cbz2rt : kA64Cbnz2rt;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100154 ArmOpcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
155 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100156 } else {
157 OpRegImm(kOpCmp, reg, check_value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100158 branch = NewLIR2(kA64B2ct, arm_cond, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100159 }
160 branch->target = target;
161 return branch;
162}
163
Zheng Xu7c1c2632014-06-17 18:17:31 +0800164LIR* Arm64Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg,
165 RegStorage base_reg, int offset, int check_value,
166 LIR* target) {
167 // It is possible that temp register is 64-bit. (ArgReg or RefReg)
168 // Always compare 32-bit value no matter what temp_reg is.
169 if (temp_reg.Is64Bit()) {
170 temp_reg = As32BitReg(temp_reg);
171 }
172 Load32Disp(base_reg, offset, temp_reg);
173 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
174 return branch;
175}
176
Matteo Franchin43ec8732014-03-31 15:00:14 +0100177LIR* Arm64Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100178 bool dest_is_fp = r_dest.IsFloat();
179 bool src_is_fp = r_src.IsFloat();
180 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100181 LIR* res;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100182
183 if (LIKELY(dest_is_fp == src_is_fp)) {
184 if (LIKELY(!dest_is_fp)) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700185 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
186
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100187 // Core/core copy.
188 // Copies involving the sp register require a different instruction.
189 opcode = UNLIKELY(A64_REG_IS_SP(r_dest.GetReg())) ? kA64Add4RRdT : kA64Mov2rr;
190
191 // TODO(Arm64): kA64Add4RRdT formally has 4 args, but is used as a 2 args instruction.
192 // This currently works because the other arguments are set to 0 by default. We should
193 // rather introduce an alias kA64Mov2RR.
194
195 // core/core copy. Do a x/x copy only if both registers are x.
196 if (r_dest.Is64Bit() && r_src.Is64Bit()) {
197 opcode = WIDE(opcode);
198 }
199 } else {
200 // Float/float copy.
201 bool dest_is_double = r_dest.IsDouble();
202 bool src_is_double = r_src.IsDouble();
203
204 // We do not do float/double or double/float casts here.
205 DCHECK_EQ(dest_is_double, src_is_double);
206
207 // Homogeneous float/float copy.
208 opcode = (dest_is_double) ? FWIDE(kA64Fmov2ff) : kA64Fmov2ff;
209 }
210 } else {
211 // Inhomogeneous register copy.
212 if (dest_is_fp) {
213 if (r_dest.IsDouble()) {
214 opcode = kA64Fmov2Sx;
215 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700216 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100217 opcode = kA64Fmov2sw;
218 }
219 } else {
220 if (r_src.IsDouble()) {
221 opcode = kA64Fmov2xS;
222 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700223 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100224 opcode = kA64Fmov2ws;
225 }
226 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100227 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100228
Matteo Franchin43ec8732014-03-31 15:00:14 +0100229 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100230
Matteo Franchin43ec8732014-03-31 15:00:14 +0100231 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
232 res->flags.is_nop = true;
233 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100234
Matteo Franchin43ec8732014-03-31 15:00:14 +0100235 return res;
236}
237
238void Arm64Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
239 if (r_dest != r_src) {
240 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
241 AppendLIR(res);
242 }
243}
244
245void Arm64Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100246 OpRegCopy(r_dest, r_src);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100247}
248
249// Table of magic divisors
250struct MagicTable {
251 uint32_t magic;
252 uint32_t shift;
253 DividePattern pattern;
254};
255
256static const MagicTable magic_table[] = {
257 {0, 0, DivideNone}, // 0
258 {0, 0, DivideNone}, // 1
259 {0, 0, DivideNone}, // 2
260 {0x55555556, 0, Divide3}, // 3
261 {0, 0, DivideNone}, // 4
262 {0x66666667, 1, Divide5}, // 5
263 {0x2AAAAAAB, 0, Divide3}, // 6
264 {0x92492493, 2, Divide7}, // 7
265 {0, 0, DivideNone}, // 8
266 {0x38E38E39, 1, Divide5}, // 9
267 {0x66666667, 2, Divide5}, // 10
268 {0x2E8BA2E9, 1, Divide5}, // 11
269 {0x2AAAAAAB, 1, Divide5}, // 12
270 {0x4EC4EC4F, 2, Divide5}, // 13
271 {0x92492493, 3, Divide7}, // 14
272 {0x88888889, 3, Divide7}, // 15
273};
274
275// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
276bool Arm64Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100277 RegLocation rl_src, RegLocation rl_dest, int lit) {
278 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100279 return false;
280 }
281 DividePattern pattern = magic_table[lit].pattern;
282 if (pattern == DivideNone) {
283 return false;
284 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100285 // Tuning: add rem patterns
286 if (!is_div) {
287 return false;
288 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100289
290 RegStorage r_magic = AllocTemp();
291 LoadConstant(r_magic, magic_table[lit].magic);
292 rl_src = LoadValue(rl_src, kCoreReg);
293 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100294 RegStorage r_long_mul = AllocTemp();
295 NewLIR4(kA64Smaddl4xwwx, As64BitReg(r_long_mul).GetReg(),
296 r_magic.GetReg(), rl_src.reg.GetReg(), rxzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100297 switch (pattern) {
298 case Divide3:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100299 OpRegRegImm(kOpLsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul), 32);
300 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100301 break;
302 case Divide5:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100303 OpRegRegImm(kOpAsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul),
304 32 + magic_table[lit].shift);
305 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100306 break;
307 case Divide7:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100308 OpRegRegRegShift(kOpAdd, As64BitReg(r_long_mul), As64BitReg(rl_src.reg),
309 As64BitReg(r_long_mul), EncodeShift(kA64Lsr, 32));
310 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
311 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100312 break;
313 default:
314 LOG(FATAL) << "Unexpected pattern: " << pattern;
315 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100316 StoreValue(rl_dest, rl_result);
317 return true;
318}
319
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100320// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
321// and store the result in 'rl_dest'.
322bool Arm64Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
323 RegLocation rl_src, RegLocation rl_dest, int lit) {
324 if (lit < 2) {
325 return false;
326 }
327 if (!IsPowerOfTwo(lit)) {
328 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
329 }
330 int k = LowestSetBit(lit);
331 if (k >= 30) {
332 // Avoid special cases.
333 return false;
334 }
335 rl_src = LoadValue(rl_src, kCoreReg);
336 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
337 if (is_div) {
338 RegStorage t_reg = AllocTemp();
339 if (lit == 2) {
340 // Division by 2 is by far the most common division by constant.
341 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k));
342 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
343 } else {
344 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
345 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, t_reg, EncodeShift(kA64Lsr, 32 - k));
346 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
347 }
348 } else {
349 RegStorage t_reg = AllocTemp();
350 if (lit == 2) {
351 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k));
352 OpRegRegImm(kOpAnd, t_reg, t_reg, lit - 1);
353 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg, rl_src.reg, EncodeShift(kA64Lsr, 32 - k));
354 } else {
355 RegStorage t_reg2 = AllocTemp();
356 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, 31);
357 OpRegRegRegShift(kOpAdd, t_reg2, rl_src.reg, t_reg, EncodeShift(kA64Lsr, 32 - k));
358 OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
359 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg2, t_reg, EncodeShift(kA64Lsr, 32 - k));
360 }
361 }
362 StoreValue(rl_dest, rl_result);
363 return true;
364}
365
Matteo Franchin43ec8732014-03-31 15:00:14 +0100366bool Arm64Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100367 LOG(FATAL) << "Unexpected use of EasyMultiply for Arm64";
368 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100369}
370
371RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100372 RegLocation rl_src2, bool is_div, bool check_zero) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100373 LOG(FATAL) << "Unexpected use of GenDivRem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100374 return rl_dest;
375}
376
377RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100378 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100379 return rl_dest;
380}
381
382RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
383 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
384
385 // Put the literal in a temp.
386 RegStorage lit_temp = AllocTemp();
387 LoadConstant(lit_temp, lit);
388 // Use the generic case for div/rem with arg2 in a register.
389 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
390 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
391 FreeTemp(lit_temp);
392
393 return rl_result;
394}
395
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100396RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage r_src1, RegStorage r_src2,
Matteo Franchin43ec8732014-03-31 15:00:14 +0100397 bool is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100398 CHECK_EQ(r_src1.Is64Bit(), r_src2.Is64Bit());
399
Matteo Franchin43ec8732014-03-31 15:00:14 +0100400 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
401 if (is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100402 OpRegRegReg(kOpDiv, rl_result.reg, r_src1, r_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100403 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100404 // temp = r_src1 / r_src2
405 // dest = r_src1 - temp * r_src2
406 RegStorage temp;
407 ArmOpcode wide;
408 if (rl_result.reg.Is64Bit()) {
409 temp = AllocTempWide();
410 wide = WIDE(0);
411 } else {
412 temp = AllocTemp();
413 wide = UNWIDE(0);
414 }
415 OpRegRegReg(kOpDiv, temp, r_src1, r_src2);
416 NewLIR4(kA64Msub4rrrr | wide, rl_result.reg.GetReg(), temp.GetReg(),
417 r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100418 FreeTemp(temp);
419 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100420 return rl_result;
421}
422
Serban Constantinescu169489b2014-06-11 16:43:35 +0100423bool Arm64Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
424 RegLocation rl_src = info->args[0];
425 rl_src = LoadValueWide(rl_src, kCoreReg);
426 RegLocation rl_dest = InlineTargetWide(info);
427 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
428 RegStorage sign_reg = AllocTempWide();
429 // abs(x) = y<=x>>63, (x+y)^y.
430 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
431 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
432 OpRegReg(kOpXor, rl_result.reg, sign_reg);
433 StoreValueWide(rl_dest, rl_result);
434 return true;
435}
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100436
Serban Constantinescu169489b2014-06-11 16:43:35 +0100437bool Arm64Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
438 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100439 RegLocation rl_src1 = info->args[0];
440 RegLocation rl_src2 = info->args[1];
441 rl_src1 = LoadValue(rl_src1, kCoreReg);
442 rl_src2 = LoadValue(rl_src2, kCoreReg);
443 RegLocation rl_dest = InlineTarget(info);
444 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
445 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100446 NewLIR4(kA64Csel4rrrc, rl_result.reg.GetReg(), rl_src1.reg.GetReg(),
447 rl_src2.reg.GetReg(), (is_min) ? kArmCondLt : kArmCondGt);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100448 StoreValue(rl_dest, rl_result);
449 return true;
450}
451
452bool Arm64Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
453 RegLocation rl_src_address = info->args[0]; // long address
Serban Constantinescu169489b2014-06-11 16:43:35 +0100454 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1] ?
Matteo Franchin43ec8732014-03-31 15:00:14 +0100455 RegLocation rl_dest = InlineTarget(info);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100456 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); // kRefReg
Matteo Franchin43ec8732014-03-31 15:00:14 +0100457 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100458
Andreas Gampe3c12c512014-06-24 18:46:29 +0000459 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100460 if (size == k64) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100461 StoreValueWide(rl_dest, rl_result);
462 } else {
463 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100464 StoreValue(rl_dest, rl_result);
465 }
466 return true;
467}
468
469bool Arm64Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
470 RegLocation rl_src_address = info->args[0]; // long address
471 rl_src_address = NarrowRegLoc(rl_src_address); // ignore high half in info->args[1]
472 RegLocation rl_src_value = info->args[2]; // [size] value
Serban Constantinescu169489b2014-06-11 16:43:35 +0100473 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg); // kRefReg
474
475 RegLocation rl_value;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100476 if (size == k64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100477 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100478 } else {
479 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100480 rl_value = LoadValue(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100481 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000482 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100483 return true;
484}
485
486void Arm64Mir2Lir::OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100487 LOG(FATAL) << "Unexpected use of OpLea for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100488}
489
Andreas Gampe2f244e92014-05-08 03:35:25 -0700490void Arm64Mir2Lir::OpTlsCmp(ThreadOffset<4> offset, int val) {
491 UNIMPLEMENTED(FATAL) << "Should not be used.";
492}
493
494void Arm64Mir2Lir::OpTlsCmp(ThreadOffset<8> offset, int val) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100495 LOG(FATAL) << "Unexpected use of OpTlsCmp for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100496}
497
498bool Arm64Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100499 DCHECK_EQ(cu_->instruction_set, kArm64);
500 ArmOpcode wide = is_long ? WIDE(0) : UNWIDE(0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100501 // Unused - RegLocation rl_src_unsafe = info->args[0];
502 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
503 RegLocation rl_src_offset = info->args[2]; // long low
Serban Constantinescu169489b2014-06-11 16:43:35 +0100504 rl_src_offset = NarrowRegLoc(rl_src_offset); // ignore high half in info->args[3] //TODO: do we really need this
Matteo Franchin43ec8732014-03-31 15:00:14 +0100505 RegLocation rl_src_expected = info->args[4]; // int, long or Object
506 // If is_long, high half is in info->args[5]
507 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
508 // If is_long, high half is in info->args[7]
509 RegLocation rl_dest = InlineTarget(info); // boolean place for result
510
Serban Constantinescu169489b2014-06-11 16:43:35 +0100511 // Load Object and offset
buzbeea0cd2d72014-06-01 09:33:49 -0700512 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100513 RegLocation rl_offset = LoadValue(rl_src_offset, kRefReg);
514
Matteo Franchin43ec8732014-03-31 15:00:14 +0100515 RegLocation rl_new_value;
Serban Constantinescu169489b2014-06-11 16:43:35 +0100516 RegLocation rl_expected;
517 if (is_long) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100518 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100519 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
520 } else {
521 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
522 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100523 }
524
525 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
526 // Mark card for object assuming new value is stored.
527 MarkGCCard(rl_new_value.reg, rl_object.reg);
528 }
529
Serban Constantinescu169489b2014-06-11 16:43:35 +0100530 RegStorage r_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100531 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
532
533 // Free now unneeded rl_object and rl_offset to give more temps.
534 ClobberSReg(rl_object.s_reg_low);
535 FreeTemp(rl_object.reg);
536 ClobberSReg(rl_offset.s_reg_low);
537 FreeTemp(rl_offset.reg);
538
Matteo Franchin43ec8732014-03-31 15:00:14 +0100539 // do {
540 // tmp = [r_ptr] - expected;
541 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
542 // result = tmp != 0;
543
Serban Constantinescu169489b2014-06-11 16:43:35 +0100544 RegStorage r_tmp;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100545 if (is_long) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100546 r_tmp = AllocTempWide();
547 } else if (is_object) {
548 r_tmp = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100549 } else {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100550 r_tmp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100551 }
552
Serban Constantinescu169489b2014-06-11 16:43:35 +0100553 LIR* loop = NewLIR0(kPseudoTargetLabel);
554 NewLIR2(kA64Ldaxr2rX | wide, r_tmp.GetReg(), r_ptr.GetReg());
555 OpRegReg(kOpCmp, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100556 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Serban Constantinescu169489b2014-06-11 16:43:35 +0100557 LIR* early_exit = OpCondBranch(kCondNe, NULL);
558
559 NewLIR3(kA64Stlxr3wrX | wide, As32BitReg(r_tmp).GetReg(), rl_new_value.reg.GetReg(), r_ptr.GetReg());
560 NewLIR3(kA64Cmp3RdT, As32BitReg(r_tmp).GetReg(), 0, ENCODE_NO_SHIFT);
561 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
562 OpCondBranch(kCondNe, loop);
563
564 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
565 LIR* exit = NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondNe);
566 early_exit->target = exit;
567
Matteo Franchin43ec8732014-03-31 15:00:14 +0100568 FreeTemp(r_tmp); // Now unneeded.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100569 FreeTemp(r_ptr); // Now unneeded.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100570
571 StoreValue(rl_dest, rl_result);
572
Matteo Franchin43ec8732014-03-31 15:00:14 +0100573 return true;
574}
575
576LIR* Arm64Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100577 return RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp), reg.GetReg(), 0, 0, 0, 0, target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100578}
579
580LIR* Arm64Mir2Lir::OpVldm(RegStorage r_base, int count) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100581 LOG(FATAL) << "Unexpected use of OpVldm for Arm64";
582 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100583}
584
585LIR* Arm64Mir2Lir::OpVstm(RegStorage r_base, int count) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100586 LOG(FATAL) << "Unexpected use of OpVstm for Arm64";
587 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100588}
589
590void Arm64Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
591 RegLocation rl_result, int lit,
592 int first_bit, int second_bit) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100593 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsl, second_bit - first_bit));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100594 if (first_bit != 0) {
595 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
596 }
597}
598
599void Arm64Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100600 LOG(FATAL) << "Unexpected use of GenDivZero for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100601}
602
603// Test suspend flag, return target of taken suspend branch
604LIR* Arm64Mir2Lir::OpTestSuspend(LIR* target) {
Zheng Xubaa7c882014-06-30 14:26:50 +0800605 NewLIR3(kA64Subs3rRd, rwSUSPEND, rwSUSPEND, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100606 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
607}
608
609// Decrement register and branch on condition
610LIR* Arm64Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -0700611 // Combine sub & test using sub setflags encoding here. We need to make sure a
612 // subtract form that sets carry is used, so generate explicitly.
613 // TODO: might be best to add a new op, kOpSubs, and handle it generically.
614 ArmOpcode opcode = reg.Is64Bit() ? WIDE(kA64Subs3rRd) : UNWIDE(kA64Subs3rRd);
615 NewLIR3(opcode, reg.GetReg(), reg.GetReg(), 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100616 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100617 return OpCondBranch(c_code, target);
618}
619
Andreas Gampeb14329f2014-05-15 11:16:06 -0700620bool Arm64Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100621#if ANDROID_SMP != 0
622 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
623 LIR* barrier = last_lir_insn_;
624
625 int dmb_flavor;
626 // TODO: revisit Arm barrier kinds
627 switch (barrier_kind) {
628 case kLoadStore: dmb_flavor = kISH; break;
629 case kLoadLoad: dmb_flavor = kISH; break;
630 case kStoreStore: dmb_flavor = kISHST; break;
631 case kStoreLoad: dmb_flavor = kISH; break;
632 default:
633 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
634 dmb_flavor = kSY; // quiet gcc.
635 break;
636 }
637
Andreas Gampeb14329f2014-05-15 11:16:06 -0700638 bool ret = false;
639
Matteo Franchin43ec8732014-03-31 15:00:14 +0100640 // If the same barrier already exists, don't generate another.
641 if (barrier == nullptr
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100642 || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) {
643 barrier = NewLIR1(kA64Dmb1B, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700644 ret = true;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100645 }
646
647 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
648 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100649 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -0700650 return ret;
651#else
652 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100653#endif
654}
655
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100656void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
657 RegLocation rl_result;
658
659 rl_src = LoadValue(rl_src, kCoreReg);
660 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700661 NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100662 StoreValueWide(rl_dest, rl_result);
663}
664
665void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest,
666 RegLocation rl_src1, RegLocation rl_src2, bool is_div) {
667 RegLocation rl_result;
668 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
669 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
670 GenDivZeroCheck(rl_src2.reg);
671 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100672 StoreValueWide(rl_dest, rl_result);
673}
674
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100675void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1,
676 RegLocation rl_src2) {
677 RegLocation rl_result;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100678
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100679 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
680 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
681 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100682 OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT);
683 StoreValueWide(rl_dest, rl_result);
684}
685
686void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
687 RegLocation rl_result;
688
689 rl_src = LoadValueWide(rl_src, kCoreReg);
690 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
691 OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
692 StoreValueWide(rl_dest, rl_result);
693}
694
695void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
696 RegLocation rl_result;
697
698 rl_src = LoadValueWide(rl_src, kCoreReg);
699 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
700 OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100701 StoreValueWide(rl_dest, rl_result);
702}
703
Matteo Franchin43ec8732014-03-31 15:00:14 +0100704void Arm64Mir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100705 RegLocation rl_src1, RegLocation rl_src2) {
706 GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100707}
708
709void Arm64Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100710 RegLocation rl_src2) {
711 GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100712}
713
714void Arm64Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
715 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100716 GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100717}
718
719void Arm64Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
720 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100721 GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100722}
723
724void Arm64Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
725 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100726 GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727}
728
729void Arm64Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
730 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100731 GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100732}
733
734/*
735 * Generate array load
736 */
737void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
738 RegLocation rl_index, RegLocation rl_dest, int scale) {
739 RegisterClass reg_class = RegClassBySize(size);
740 int len_offset = mirror::Array::LengthOffset().Int32Value();
741 int data_offset;
742 RegLocation rl_result;
743 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -0700744 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100745 if (!constant_index) {
746 rl_index = LoadValue(rl_index, kCoreReg);
747 }
748
749 if (rl_dest.wide) {
750 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
751 } else {
752 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
753 }
754
755 // If index is constant, just fold it into the data offset
756 if (constant_index) {
757 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
758 }
759
760 /* null object? */
761 GenNullCheck(rl_array.reg, opt_flags);
762
763 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
764 RegStorage reg_len;
765 if (needs_range_check) {
766 reg_len = AllocTemp();
767 /* Get len */
768 Load32Disp(rl_array.reg, len_offset, reg_len);
769 MarkPossibleNullPointerException(opt_flags);
770 } else {
771 ForceImplicitNullCheck(rl_array.reg, opt_flags);
772 }
773 if (rl_dest.wide || rl_dest.fp || constant_index) {
774 RegStorage reg_ptr;
775 if (constant_index) {
776 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
777 } else {
778 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -0700779 reg_ptr = AllocTempRef();
buzbee33ae5582014-06-12 14:56:32 -0700780 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
781 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100782 FreeTemp(rl_index.reg);
783 }
784 rl_result = EvalLoc(rl_dest, reg_class, true);
785
786 if (needs_range_check) {
787 if (constant_index) {
788 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
789 } else {
790 GenArrayBoundsCheck(rl_index.reg, reg_len);
791 }
792 FreeTemp(reg_len);
793 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000794 if (rl_result.ref) {
795 LoadRefDisp(reg_ptr, data_offset, rl_result.reg, kNotVolatile);
796 } else {
797 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
798 }
Vladimir Marko455759b2014-05-06 20:49:36 +0100799 MarkPossibleNullPointerException(opt_flags);
800 if (!constant_index) {
801 FreeTemp(reg_ptr);
802 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100803 if (rl_dest.wide) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100804 StoreValueWide(rl_dest, rl_result);
805 } else {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100806 StoreValue(rl_dest, rl_result);
807 }
808 } else {
809 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -0700810 RegStorage reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100811 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
812 FreeTemp(rl_array.reg);
813 rl_result = EvalLoc(rl_dest, reg_class, true);
814
815 if (needs_range_check) {
816 GenArrayBoundsCheck(rl_index.reg, reg_len);
817 FreeTemp(reg_len);
818 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000819 if (rl_result.ref) {
820 LoadRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg);
821 } else {
822 LoadBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale, size);
823 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100824 MarkPossibleNullPointerException(opt_flags);
825 FreeTemp(reg_ptr);
826 StoreValue(rl_dest, rl_result);
827 }
828}
829
830/*
831 * Generate array store
832 *
833 */
834void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
835 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
836 RegisterClass reg_class = RegClassBySize(size);
837 int len_offset = mirror::Array::LengthOffset().Int32Value();
838 bool constant_index = rl_index.is_const;
839
840 int data_offset;
841 if (size == k64 || size == kDouble) {
842 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
843 } else {
844 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
845 }
846
847 // If index is constant, just fold it into the data offset.
848 if (constant_index) {
849 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
850 }
851
buzbeea0cd2d72014-06-01 09:33:49 -0700852 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100853 if (!constant_index) {
854 rl_index = LoadValue(rl_index, kCoreReg);
855 }
856
857 RegStorage reg_ptr;
858 bool allocated_reg_ptr_temp = false;
859 if (constant_index) {
860 reg_ptr = rl_array.reg;
861 } else if (IsTemp(rl_array.reg) && !card_mark) {
862 Clobber(rl_array.reg);
863 reg_ptr = rl_array.reg;
864 } else {
865 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -0700866 reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100867 }
868
869 /* null object? */
870 GenNullCheck(rl_array.reg, opt_flags);
871
872 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
873 RegStorage reg_len;
874 if (needs_range_check) {
875 reg_len = AllocTemp();
876 // NOTE: max live temps(4) here.
877 /* Get len */
878 Load32Disp(rl_array.reg, len_offset, reg_len);
879 MarkPossibleNullPointerException(opt_flags);
880 } else {
881 ForceImplicitNullCheck(rl_array.reg, opt_flags);
882 }
883 /* at this point, reg_ptr points to array, 2 live temps */
884 if (rl_src.wide || rl_src.fp || constant_index) {
885 if (rl_src.wide) {
886 rl_src = LoadValueWide(rl_src, reg_class);
887 } else {
888 rl_src = LoadValue(rl_src, reg_class);
889 }
890 if (!constant_index) {
buzbee33ae5582014-06-12 14:56:32 -0700891 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
892 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100893 }
894 if (needs_range_check) {
895 if (constant_index) {
896 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
897 } else {
898 GenArrayBoundsCheck(rl_index.reg, reg_len);
899 }
900 FreeTemp(reg_len);
901 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000902 if (rl_src.ref) {
903 StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile);
904 } else {
905 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
906 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100907 MarkPossibleNullPointerException(opt_flags);
908 } else {
909 /* reg_ptr -> array data */
910 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
911 rl_src = LoadValue(rl_src, reg_class);
912 if (needs_range_check) {
913 GenArrayBoundsCheck(rl_index.reg, reg_len);
914 FreeTemp(reg_len);
915 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000916 if (rl_src.ref) {
917 StoreRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg);
918 } else {
919 StoreBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale, size);
920 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100921 MarkPossibleNullPointerException(opt_flags);
922 }
923 if (allocated_reg_ptr_temp) {
924 FreeTemp(reg_ptr);
925 }
926 if (card_mark) {
927 MarkGCCard(rl_src.reg, rl_array.reg);
928 }
929}
930
Matteo Franchin43ec8732014-03-31 15:00:14 +0100931void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
932 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100933 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100934 // Per spec, we only care about low 6 bits of shift amount.
935 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100936 rl_src = LoadValueWide(rl_src, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100937 if (shift_amount == 0) {
938 StoreValueWide(rl_dest, rl_src);
939 return;
940 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100941
942 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100943 switch (opcode) {
944 case Instruction::SHL_LONG:
945 case Instruction::SHL_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100946 op = kOpLsl;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100947 break;
948 case Instruction::SHR_LONG:
949 case Instruction::SHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100950 op = kOpAsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100951 break;
952 case Instruction::USHR_LONG:
953 case Instruction::USHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100954 op = kOpLsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100955 break;
956 default:
957 LOG(FATAL) << "Unexpected case";
958 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100959 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100960 StoreValueWide(rl_dest, rl_result);
961}
962
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100963void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
964 RegLocation rl_src1, RegLocation rl_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100965 if ((opcode == Instruction::SUB_LONG) || (opcode == Instruction::SUB_LONG_2ADDR)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100966 if (!rl_src2.is_const) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100967 return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100968 }
969 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100970 // Associativity.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100971 if (!rl_src2.is_const) {
972 DCHECK(rl_src1.is_const);
973 std::swap(rl_src1, rl_src2);
974 }
975 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100976 DCHECK(rl_src2.is_const);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100977
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100978 OpKind op = kOpBkpt;
979 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
980
Matteo Franchin43ec8732014-03-31 15:00:14 +0100981 switch (opcode) {
982 case Instruction::ADD_LONG:
983 case Instruction::ADD_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100984 op = kOpAdd;
985 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100986 case Instruction::SUB_LONG:
987 case Instruction::SUB_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100988 op = kOpSub;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100989 break;
990 case Instruction::AND_LONG:
991 case Instruction::AND_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100992 op = kOpAnd;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100993 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100994 case Instruction::OR_LONG:
995 case Instruction::OR_LONG_2ADDR:
996 op = kOpOr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100997 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100998 case Instruction::XOR_LONG:
999 case Instruction::XOR_LONG_2ADDR:
1000 op = kOpXor;
1001 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001002 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001003 LOG(FATAL) << "Unexpected opcode";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001004 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001005
1006 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1007 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +08001008 OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001009 StoreValueWide(rl_dest, rl_result);
1010}
1011
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001012/**
1013 * @brief Split a register list in pairs or registers.
1014 *
1015 * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows:
1016 * @code
1017 * int reg1 = -1, reg2 = -1;
1018 * while (reg_mask) {
1019 * reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1020 * if (UNLIKELY(reg2 < 0)) {
1021 * // Single register in reg1.
1022 * } else {
1023 * // Pair in reg1, reg2.
1024 * }
1025 * }
1026 * @endcode
1027 */
1028uint32_t Arm64Mir2Lir::GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) {
1029 // Find first register.
1030 int first_bit_set = __builtin_ctz(reg_mask) + 1;
1031 int reg = *reg1 + first_bit_set;
1032 reg_mask >>= first_bit_set;
1033
1034 if (LIKELY(reg_mask)) {
1035 // Save the first register, find the second and use the pair opcode.
1036 int second_bit_set = __builtin_ctz(reg_mask) + 1;
1037 *reg2 = reg;
1038 reg_mask >>= second_bit_set;
1039 *reg1 = reg + second_bit_set;
1040 return reg_mask;
1041 }
1042
1043 // Use the single opcode, as we just have one register.
1044 *reg1 = reg;
1045 *reg2 = -1;
1046 return reg_mask;
1047}
1048
1049void Arm64Mir2Lir::UnSpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) {
1050 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001051 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001052
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001053 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001054 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1055 if (UNLIKELY(reg2 < 0)) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001056 NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001057 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001058 DCHECK_LE(offset, 63);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001059 NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1060 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001061 }
1062 }
1063}
1064
1065void Arm64Mir2Lir::SpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) {
1066 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001067 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001068
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001069 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001070 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1071 if (UNLIKELY(reg2 < 0)) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001072 NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001073 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001074 NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1075 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1076 }
1077 }
1078}
1079
1080void Arm64Mir2Lir::UnSpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) {
1081 int reg1 = -1, reg2 = -1;
1082 const int reg_log2_size = 3;
1083
1084 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1085 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1086 if (UNLIKELY(reg2 < 0)) {
1087 NewLIR3(FWIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1088 } else {
1089 NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1090 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1091 }
1092 }
1093}
1094
1095// TODO(Arm64): consider using ld1 and st1?
1096void Arm64Mir2Lir::SpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) {
1097 int reg1 = -1, reg2 = -1;
1098 const int reg_log2_size = 3;
1099
1100 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1101 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1102 if (UNLIKELY(reg2 < 0)) {
1103 NewLIR3(FWIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1104 } else {
1105 NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1106 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001107 }
1108 }
1109}
1110
Matteo Franchin43ec8732014-03-31 15:00:14 +01001111} // namespace art