blob: f9f85f4223cae897d1b91308c2a83e9034991c13 [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
Andreas Gampe90969af2014-07-15 23:02:11 -070088static constexpr bool kUseDeltaEncodingInGenSelect = false;
Andreas Gampe381f8ac2014-07-10 03:23:41 -070089
Andreas Gampe90969af2014-07-15 23:02:11 -070090void Arm64Mir2Lir::GenSelect(int32_t true_val, int32_t false_val, ConditionCode ccode,
91 RegStorage rs_dest, int result_reg_class) {
92 if (false_val == 0 || // 0 is better as first operand.
93 true_val == 1 || // Potentially Csinc.
94 true_val == -1 || // Potentially Csinv.
95 true_val == false_val + 1) { // Potentially Csinc.
96 ccode = NegateComparison(ccode);
97 std::swap(true_val, false_val);
98 }
99
100 ArmConditionCode code = ArmConditionEncoding(ccode);
101
102 int opcode; // The opcode.
103 RegStorage left_op = RegStorage::InvalidReg(); // The operands.
104 RegStorage right_op = RegStorage::InvalidReg(); // The operands.
105
106 bool is_wide = rs_dest.Is64Bit();
107
108 RegStorage zero_reg = is_wide ? rs_xzr : rs_wzr;
109
110 if (true_val == 0) {
111 left_op = zero_reg;
112 } else {
113 left_op = rs_dest;
114 LoadConstantNoClobber(rs_dest, true_val);
115 }
116 if (false_val == 1) {
117 right_op = zero_reg;
118 opcode = kA64Csinc4rrrc;
119 } else if (false_val == -1) {
120 right_op = zero_reg;
121 opcode = kA64Csinv4rrrc;
122 } else if (false_val == true_val + 1) {
123 right_op = left_op;
124 opcode = kA64Csinc4rrrc;
125 } else if (false_val == -true_val) {
126 right_op = left_op;
127 opcode = kA64Csneg4rrrc;
128 } else if (false_val == ~true_val) {
129 right_op = left_op;
130 opcode = kA64Csinv4rrrc;
131 } else if (true_val == 0) {
132 // left_op is zero_reg.
133 right_op = rs_dest;
134 LoadConstantNoClobber(rs_dest, false_val);
135 opcode = kA64Csel4rrrc;
136 } else {
137 // Generic case.
138 RegStorage t_reg2 = AllocTypedTemp(false, result_reg_class);
139 if (is_wide) {
140 if (t_reg2.Is32Bit()) {
141 t_reg2 = As64BitReg(t_reg2);
142 }
143 } else {
144 if (t_reg2.Is64Bit()) {
145 t_reg2 = As32BitReg(t_reg2);
146 }
147 }
148
149 if (kUseDeltaEncodingInGenSelect) {
150 int32_t delta = false_val - true_val;
151 uint32_t abs_val = delta < 0 ? -delta : delta;
152
153 if (abs_val < 0x1000) { // TODO: Replace with InexpensiveConstant with opcode.
154 // Can encode as immediate to an add.
155 right_op = t_reg2;
156 OpRegRegImm(kOpAdd, t_reg2, left_op, delta);
157 }
158 }
159
160 // Load as constant.
161 if (!right_op.Valid()) {
162 LoadConstantNoClobber(t_reg2, false_val);
163 right_op = t_reg2;
164 }
165
166 opcode = kA64Csel4rrrc;
167 }
168
169 DCHECK(left_op.Valid() && right_op.Valid());
170 NewLIR4(is_wide ? WIDE(opcode) : opcode, rs_dest.GetReg(), left_op.GetReg(), right_op.GetReg(),
171 code);
172}
173
174void Arm64Mir2Lir::GenSelectConst32(RegStorage left_op, RegStorage right_op, ConditionCode code,
175 int32_t true_val, int32_t false_val, RegStorage rs_dest,
176 int dest_reg_class) {
177 DCHECK(rs_dest.Valid());
178 OpRegReg(kOpCmp, left_op, right_op);
179 GenSelect(true_val, false_val, code, rs_dest, dest_reg_class);
180}
181
182void Arm64Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
183 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
184 rl_src = LoadValue(rl_src, rl_src.ref ? kRefReg : kCoreReg);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700185 // rl_src may be aliased with rl_result/rl_dest, so do compare early.
186 OpRegImm(kOpCmp, rl_src.reg, 0);
187
Andreas Gampe90969af2014-07-15 23:02:11 -0700188 RegLocation rl_dest = mir_graph_->GetDest(mir);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100189
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700190 // The kMirOpSelect has two variants, one for constants and one for moves.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700191 if (mir->ssa_rep->num_uses == 1) {
Andreas Gampe90969af2014-07-15 23:02:11 -0700192 RegLocation rl_result = EvalLoc(rl_dest, rl_dest.ref ? kRefReg : kCoreReg, true);
193 GenSelect(mir->dalvikInsn.vB, mir->dalvikInsn.vC, mir->meta.ccode, rl_result.reg,
194 rl_dest.ref ? kRefReg : kCoreReg);
195 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700196 } else {
197 RegLocation rl_true = mir_graph_->reg_location_[mir->ssa_rep->uses[1]];
198 RegLocation rl_false = mir_graph_->reg_location_[mir->ssa_rep->uses[2]];
199
Andreas Gampe90969af2014-07-15 23:02:11 -0700200 RegisterClass result_reg_class = rl_dest.ref ? kRefReg : kCoreReg;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700201 rl_true = LoadValue(rl_true, result_reg_class);
202 rl_false = LoadValue(rl_false, result_reg_class);
Andreas Gampe90969af2014-07-15 23:02:11 -0700203 RegLocation rl_result = EvalLoc(rl_dest, result_reg_class, true);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700204
Andreas Gampe90969af2014-07-15 23:02:11 -0700205 bool is_wide = rl_dest.ref || rl_dest.wide;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700206 int opcode = is_wide ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc;
207 NewLIR4(opcode, rl_result.reg.GetReg(),
Andreas Gampe90969af2014-07-15 23:02:11 -0700208 rl_true.reg.GetReg(), rl_false.reg.GetReg(), ArmConditionEncoding(mir->meta.ccode));
209 StoreValue(rl_dest, rl_result);
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700210 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100211}
212
213void Arm64Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
214 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
215 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100216 LIR* taken = &block_label_list_[bb->taken];
217 LIR* not_taken = &block_label_list_[bb->fall_through];
Matteo Franchin43ec8732014-03-31 15:00:14 +0100218 // Normalize such that if either operand is constant, src2 will be constant.
219 ConditionCode ccode = mir->meta.ccode;
220 if (rl_src1.is_const) {
221 std::swap(rl_src1, rl_src2);
222 ccode = FlipComparisonOrder(ccode);
223 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100224
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700225 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
226
Matteo Franchin43ec8732014-03-31 15:00:14 +0100227 if (rl_src2.is_const) {
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700228 // TODO: Optimize for rl_src1.is_const? (Does happen in the boot image at the moment.)
229
Matteo Franchin43ec8732014-03-31 15:00:14 +0100230 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100231 // Special handling using cbz & cbnz.
232 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
233 OpCmpImmBranch(ccode, rl_src1.reg, 0, taken);
234 OpCmpImmBranch(NegateComparison(ccode), rl_src1.reg, 0, not_taken);
235 return;
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700236 }
237
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100238 // Only handle Imm if src2 is not already in a register.
Andreas Gampe381f8ac2014-07-10 03:23:41 -0700239 rl_src2 = UpdateLocWide(rl_src2);
240 if (rl_src2.location != kLocPhysReg) {
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100241 OpRegImm64(kOpCmp, rl_src1.reg, val);
242 OpCondBranch(ccode, taken);
243 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100244 return;
245 }
246 }
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100247
Matteo Franchin43ec8732014-03-31 15:00:14 +0100248 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100249 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100250 OpCondBranch(ccode, taken);
Serban Constantinescu05e27ff2014-05-28 13:21:45 +0100251 OpCondBranch(NegateComparison(ccode), not_taken);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100252}
253
254/*
255 * Generate a register comparison to an immediate and branch. Caller
256 * is responsible for setting branch target field.
257 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100258LIR* Arm64Mir2Lir::OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value,
259 LIR* target) {
Andreas Gampe9522af92014-07-14 20:16:59 -0700260 LIR* branch = nullptr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100261 ArmConditionCode arm_cond = ArmConditionEncoding(cond);
Andreas Gampe9522af92014-07-14 20:16:59 -0700262 if (check_value == 0) {
263 if (arm_cond == kArmCondEq || arm_cond == kArmCondNe) {
264 ArmOpcode opcode = (arm_cond == kArmCondEq) ? kA64Cbz2rt : kA64Cbnz2rt;
265 ArmOpcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
266 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
267 } else if (arm_cond == kArmCondLs) {
268 // kArmCondLs is an unsigned less or equal. A comparison r <= 0 is then the same as cbz.
269 // This case happens for a bounds check of array[0].
270 ArmOpcode opcode = kA64Cbz2rt;
271 ArmOpcode wide = reg.Is64Bit() ? WIDE(0) : UNWIDE(0);
272 branch = NewLIR2(opcode | wide, reg.GetReg(), 0);
273 }
274 }
275
276 if (branch == nullptr) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100277 OpRegImm(kOpCmp, reg, check_value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100278 branch = NewLIR2(kA64B2ct, arm_cond, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100279 }
Andreas Gampe9522af92014-07-14 20:16:59 -0700280
Matteo Franchin43ec8732014-03-31 15:00:14 +0100281 branch->target = target;
282 return branch;
283}
284
Zheng Xu7c1c2632014-06-17 18:17:31 +0800285LIR* Arm64Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg,
286 RegStorage base_reg, int offset, int check_value,
Dave Allison69dfe512014-07-11 17:11:58 +0000287 LIR* target, LIR** compare) {
288 DCHECK(compare == nullptr);
Zheng Xu7c1c2632014-06-17 18:17:31 +0800289 // It is possible that temp register is 64-bit. (ArgReg or RefReg)
290 // Always compare 32-bit value no matter what temp_reg is.
291 if (temp_reg.Is64Bit()) {
292 temp_reg = As32BitReg(temp_reg);
293 }
294 Load32Disp(base_reg, offset, temp_reg);
295 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
296 return branch;
297}
298
Matteo Franchin43ec8732014-03-31 15:00:14 +0100299LIR* Arm64Mir2Lir::OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100300 bool dest_is_fp = r_dest.IsFloat();
301 bool src_is_fp = r_src.IsFloat();
302 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100303 LIR* res;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100304
305 if (LIKELY(dest_is_fp == src_is_fp)) {
306 if (LIKELY(!dest_is_fp)) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700307 DCHECK_EQ(r_dest.Is64Bit(), r_src.Is64Bit());
308
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100309 // Core/core copy.
310 // Copies involving the sp register require a different instruction.
311 opcode = UNLIKELY(A64_REG_IS_SP(r_dest.GetReg())) ? kA64Add4RRdT : kA64Mov2rr;
312
313 // TODO(Arm64): kA64Add4RRdT formally has 4 args, but is used as a 2 args instruction.
314 // This currently works because the other arguments are set to 0 by default. We should
315 // rather introduce an alias kA64Mov2RR.
316
317 // core/core copy. Do a x/x copy only if both registers are x.
318 if (r_dest.Is64Bit() && r_src.Is64Bit()) {
319 opcode = WIDE(opcode);
320 }
321 } else {
322 // Float/float copy.
323 bool dest_is_double = r_dest.IsDouble();
324 bool src_is_double = r_src.IsDouble();
325
326 // We do not do float/double or double/float casts here.
327 DCHECK_EQ(dest_is_double, src_is_double);
328
329 // Homogeneous float/float copy.
330 opcode = (dest_is_double) ? FWIDE(kA64Fmov2ff) : kA64Fmov2ff;
331 }
332 } else {
333 // Inhomogeneous register copy.
334 if (dest_is_fp) {
335 if (r_dest.IsDouble()) {
336 opcode = kA64Fmov2Sx;
337 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700338 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100339 opcode = kA64Fmov2sw;
340 }
341 } else {
342 if (r_src.IsDouble()) {
343 opcode = kA64Fmov2xS;
344 } else {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700345 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100346 opcode = kA64Fmov2ws;
347 }
348 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100349 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100350
Matteo Franchin43ec8732014-03-31 15:00:14 +0100351 res = RawLIR(current_dalvik_offset_, opcode, r_dest.GetReg(), r_src.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100352
Matteo Franchin43ec8732014-03-31 15:00:14 +0100353 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
354 res->flags.is_nop = true;
355 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100356
Matteo Franchin43ec8732014-03-31 15:00:14 +0100357 return res;
358}
359
360void Arm64Mir2Lir::OpRegCopy(RegStorage r_dest, RegStorage r_src) {
361 if (r_dest != r_src) {
362 LIR* res = OpRegCopyNoInsert(r_dest, r_src);
363 AppendLIR(res);
364 }
365}
366
367void Arm64Mir2Lir::OpRegCopyWide(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100368 OpRegCopy(r_dest, r_src);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100369}
370
371// Table of magic divisors
372struct MagicTable {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100373 int magic64_base;
374 int magic64_eor;
375 uint64_t magic64;
376 uint32_t magic32;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100377 uint32_t shift;
378 DividePattern pattern;
379};
380
381static const MagicTable magic_table[] = {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100382 { 0, 0, 0, 0, 0, DivideNone}, // 0
383 { 0, 0, 0, 0, 0, DivideNone}, // 1
384 { 0, 0, 0, 0, 0, DivideNone}, // 2
385 {0x3c, -1, 0x5555555555555556, 0x55555556, 0, Divide3}, // 3
386 { 0, 0, 0, 0, 0, DivideNone}, // 4
387 {0xf9, -1, 0x6666666666666667, 0x66666667, 1, Divide5}, // 5
388 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 0, Divide3}, // 6
389 { -1, -1, 0x924924924924924A, 0x92492493, 2, Divide7}, // 7
390 { 0, 0, 0, 0, 0, DivideNone}, // 8
391 { -1, -1, 0x38E38E38E38E38E4, 0x38E38E39, 1, Divide5}, // 9
392 {0xf9, -1, 0x6666666666666667, 0x66666667, 2, Divide5}, // 10
393 { -1, -1, 0x2E8BA2E8BA2E8BA3, 0x2E8BA2E9, 1, Divide5}, // 11
394 {0x7c, 0x1041, 0x2AAAAAAAAAAAAAAB, 0x2AAAAAAB, 1, Divide5}, // 12
395 { -1, -1, 0x4EC4EC4EC4EC4EC5, 0x4EC4EC4F, 2, Divide5}, // 13
396 { -1, -1, 0x924924924924924A, 0x92492493, 3, Divide7}, // 14
397 {0x78, -1, 0x8888888888888889, 0x88888889, 3, Divide7}, // 15
Matteo Franchin43ec8732014-03-31 15:00:14 +0100398};
399
400// Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4)
401bool Arm64Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100402 RegLocation rl_src, RegLocation rl_dest, int lit) {
403 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100404 return false;
405 }
406 DividePattern pattern = magic_table[lit].pattern;
407 if (pattern == DivideNone) {
408 return false;
409 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100410 // Tuning: add rem patterns
411 if (!is_div) {
412 return false;
413 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100414
415 RegStorage r_magic = AllocTemp();
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100416 LoadConstant(r_magic, magic_table[lit].magic32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100417 rl_src = LoadValue(rl_src, kCoreReg);
418 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100419 RegStorage r_long_mul = AllocTemp();
420 NewLIR4(kA64Smaddl4xwwx, As64BitReg(r_long_mul).GetReg(),
421 r_magic.GetReg(), rl_src.reg.GetReg(), rxzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100422 switch (pattern) {
423 case Divide3:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100424 OpRegRegImm(kOpLsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul), 32);
425 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100426 break;
427 case Divide5:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100428 OpRegRegImm(kOpAsr, As64BitReg(r_long_mul), As64BitReg(r_long_mul),
429 32 + magic_table[lit].shift);
430 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100431 break;
432 case Divide7:
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100433 OpRegRegRegShift(kOpAdd, As64BitReg(r_long_mul), As64BitReg(rl_src.reg),
434 As64BitReg(r_long_mul), EncodeShift(kA64Lsr, 32));
435 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
436 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 31));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100437 break;
438 default:
439 LOG(FATAL) << "Unexpected pattern: " << pattern;
440 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100441 StoreValue(rl_dest, rl_result);
442 return true;
443}
444
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100445bool Arm64Mir2Lir::SmallLiteralDivRem64(Instruction::Code dalvik_opcode, bool is_div,
446 RegLocation rl_src, RegLocation rl_dest, int64_t lit) {
447 if ((lit < 0) || (lit >= static_cast<int>(arraysize(magic_table)))) {
448 return false;
449 }
450 DividePattern pattern = magic_table[lit].pattern;
451 if (pattern == DivideNone) {
452 return false;
453 }
454 // Tuning: add rem patterns
455 if (!is_div) {
456 return false;
457 }
458
459 RegStorage r_magic = AllocTempWide();
460 rl_src = LoadValueWide(rl_src, kCoreReg);
461 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
462 RegStorage r_long_mul = AllocTempWide();
463
464 if (magic_table[lit].magic64_base >= 0) {
465 // Check that the entry in the table is correct.
466 if (kIsDebugBuild) {
467 uint64_t reconstructed_imm;
468 uint64_t base = DecodeLogicalImmediate(/*is_wide*/true, magic_table[lit].magic64_base);
469 if (magic_table[lit].magic64_eor >= 0) {
470 uint64_t eor = DecodeLogicalImmediate(/*is_wide*/true, magic_table[lit].magic64_eor);
471 reconstructed_imm = base ^ eor;
472 } else {
473 reconstructed_imm = base + 1;
474 }
475 DCHECK_EQ(reconstructed_imm, magic_table[lit].magic64) << " for literal " << lit;
476 }
477
478 // Load the magic constant in two instructions.
479 NewLIR3(WIDE(kA64Orr3Rrl), r_magic.GetReg(), rxzr, magic_table[lit].magic64_base);
480 if (magic_table[lit].magic64_eor >= 0) {
481 NewLIR3(WIDE(kA64Eor3Rrl), r_magic.GetReg(), r_magic.GetReg(),
482 magic_table[lit].magic64_eor);
483 } else {
484 NewLIR4(WIDE(kA64Add4RRdT), r_magic.GetReg(), r_magic.GetReg(), 1, 0);
485 }
486 } else {
487 LoadConstantWide(r_magic, magic_table[lit].magic64);
488 }
489
490 NewLIR3(kA64Smulh3xxx, r_long_mul.GetReg(), r_magic.GetReg(), rl_src.reg.GetReg());
491 switch (pattern) {
492 case Divide3:
493 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
494 break;
495 case Divide5:
496 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
497 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
498 break;
499 case Divide7:
500 OpRegRegReg(kOpAdd, r_long_mul, rl_src.reg, r_long_mul);
501 OpRegRegImm(kOpAsr, r_long_mul, r_long_mul, magic_table[lit].shift);
502 OpRegRegRegShift(kOpSub, rl_result.reg, r_long_mul, rl_src.reg, EncodeShift(kA64Asr, 63));
503 break;
504 default:
505 LOG(FATAL) << "Unexpected pattern: " << pattern;
506 }
507 StoreValueWide(rl_dest, rl_result);
508 return true;
509}
510
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100511// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
512// and store the result in 'rl_dest'.
513bool Arm64Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
514 RegLocation rl_src, RegLocation rl_dest, int lit) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100515 return HandleEasyDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int>(lit));
516}
517
518// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
519// and store the result in 'rl_dest'.
520bool Arm64Mir2Lir::HandleEasyDivRem64(Instruction::Code dalvik_opcode, bool is_div,
521 RegLocation rl_src, RegLocation rl_dest, int64_t lit) {
522 const bool is_64bit = rl_dest.wide;
523 const int nbits = (is_64bit) ? 64 : 32;
524
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100525 if (lit < 2) {
526 return false;
527 }
528 if (!IsPowerOfTwo(lit)) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100529 if (is_64bit) {
530 return SmallLiteralDivRem64(dalvik_opcode, is_div, rl_src, rl_dest, lit);
531 } else {
532 return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, static_cast<int32_t>(lit));
533 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100534 }
535 int k = LowestSetBit(lit);
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100536 if (k >= nbits - 2) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100537 // Avoid special cases.
538 return false;
539 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100540
541 RegLocation rl_result;
542 RegStorage t_reg;
543 if (is_64bit) {
544 rl_src = LoadValueWide(rl_src, kCoreReg);
545 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
546 t_reg = AllocTempWide();
547 } else {
548 rl_src = LoadValue(rl_src, kCoreReg);
549 rl_result = EvalLoc(rl_dest, kCoreReg, true);
550 t_reg = AllocTemp();
551 }
552
553 int shift = EncodeShift(kA64Lsr, nbits - k);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100554 if (is_div) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100555 if (lit == 2) {
556 // Division by 2 is by far the most common division by constant.
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100557 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100558 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
559 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100560 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
561 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100562 OpRegRegImm(kOpAsr, rl_result.reg, t_reg, k);
563 }
564 } else {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100565 if (lit == 2) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100566 OpRegRegRegShift(kOpAdd, t_reg, rl_src.reg, rl_src.reg, shift);
567 OpRegRegImm64(kOpAnd, t_reg, t_reg, lit - 1);
568 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg, rl_src.reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100569 } else {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100570 RegStorage t_reg2 = (is_64bit) ? AllocTempWide() : AllocTemp();
571 OpRegRegImm(kOpAsr, t_reg, rl_src.reg, nbits - 1);
572 OpRegRegRegShift(kOpAdd, t_reg2, rl_src.reg, t_reg, shift);
573 OpRegRegImm64(kOpAnd, t_reg2, t_reg2, lit - 1);
574 OpRegRegRegShift(kOpSub, rl_result.reg, t_reg2, t_reg, shift);
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100575 }
576 }
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100577
578 if (is_64bit) {
579 StoreValueWide(rl_dest, rl_result);
580 } else {
581 StoreValue(rl_dest, rl_result);
582 }
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100583 return true;
584}
585
Matteo Franchin43ec8732014-03-31 15:00:14 +0100586bool Arm64Mir2Lir::EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100587 LOG(FATAL) << "Unexpected use of EasyMultiply for Arm64";
588 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100589}
590
Matteo Franchin43ec8732014-03-31 15:00:14 +0100591RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegLocation rl_src1, int lit, bool is_div) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100592 LOG(FATAL) << "Unexpected use of GenDivRemLit for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100593 return rl_dest;
594}
595
596RegLocation Arm64Mir2Lir::GenDivRemLit(RegLocation rl_dest, RegStorage reg1, int lit, bool is_div) {
597 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
598
599 // Put the literal in a temp.
600 RegStorage lit_temp = AllocTemp();
601 LoadConstant(lit_temp, lit);
602 // Use the generic case for div/rem with arg2 in a register.
603 // TODO: The literal temp can be freed earlier during a modulus to reduce reg pressure.
604 rl_result = GenDivRem(rl_result, reg1, lit_temp, is_div);
605 FreeTemp(lit_temp);
606
607 return rl_result;
608}
609
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100610RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegLocation rl_src1,
611 RegLocation rl_src2, bool is_div, bool check_zero) {
612 LOG(FATAL) << "Unexpected use of GenDivRem for Arm64";
613 return rl_dest;
614}
615
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100616RegLocation Arm64Mir2Lir::GenDivRem(RegLocation rl_dest, RegStorage r_src1, RegStorage r_src2,
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100617 bool is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100618 CHECK_EQ(r_src1.Is64Bit(), r_src2.Is64Bit());
619
Matteo Franchin43ec8732014-03-31 15:00:14 +0100620 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
621 if (is_div) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100622 OpRegRegReg(kOpDiv, rl_result.reg, r_src1, r_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100623 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100624 // temp = r_src1 / r_src2
625 // dest = r_src1 - temp * r_src2
626 RegStorage temp;
627 ArmOpcode wide;
628 if (rl_result.reg.Is64Bit()) {
629 temp = AllocTempWide();
630 wide = WIDE(0);
631 } else {
632 temp = AllocTemp();
633 wide = UNWIDE(0);
634 }
635 OpRegRegReg(kOpDiv, temp, r_src1, r_src2);
636 NewLIR4(kA64Msub4rrrr | wide, rl_result.reg.GetReg(), temp.GetReg(),
637 r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100638 FreeTemp(temp);
639 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100640 return rl_result;
641}
642
Serban Constantinescu169489b2014-06-11 16:43:35 +0100643bool Arm64Mir2Lir::GenInlinedAbsLong(CallInfo* info) {
644 RegLocation rl_src = info->args[0];
645 rl_src = LoadValueWide(rl_src, kCoreReg);
646 RegLocation rl_dest = InlineTargetWide(info);
647 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
648 RegStorage sign_reg = AllocTempWide();
649 // abs(x) = y<=x>>63, (x+y)^y.
650 OpRegRegImm(kOpAsr, sign_reg, rl_src.reg, 63);
651 OpRegRegReg(kOpAdd, rl_result.reg, rl_src.reg, sign_reg);
652 OpRegReg(kOpXor, rl_result.reg, sign_reg);
653 StoreValueWide(rl_dest, rl_result);
654 return true;
655}
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100656
Serban Constantinescu23abec92014-07-02 16:13:38 +0100657bool Arm64Mir2Lir::GenInlinedMinMax(CallInfo* info, bool is_min, bool is_long) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100658 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100659 RegLocation rl_src1 = info->args[0];
Serban Constantinescu23abec92014-07-02 16:13:38 +0100660 RegLocation rl_src2 = (is_long) ? info->args[2] : info->args[1];
661 rl_src1 = (is_long) ? LoadValueWide(rl_src1, kCoreReg) : LoadValue(rl_src1, kCoreReg);
662 rl_src2 = (is_long) ? LoadValueWide(rl_src2, kCoreReg) : LoadValue(rl_src2, kCoreReg);
663 RegLocation rl_dest = (is_long) ? InlineTargetWide(info) : InlineTarget(info);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100664 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
665 OpRegReg(kOpCmp, rl_src1.reg, rl_src2.reg);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100666 NewLIR4((is_long) ? WIDE(kA64Csel4rrrc) : kA64Csel4rrrc, rl_result.reg.GetReg(),
667 rl_src1.reg.GetReg(), rl_src2.reg.GetReg(), (is_min) ? kArmCondLt : kArmCondGt);
668 (is_long) ? StoreValueWide(rl_dest, rl_result) :StoreValue(rl_dest, rl_result);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100669 return true;
670}
671
672bool Arm64Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
673 RegLocation rl_src_address = info->args[0]; // long address
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100674 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info);
675 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100676 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100677
Andreas Gampe3c12c512014-06-24 18:46:29 +0000678 LoadBaseDisp(rl_address.reg, 0, rl_result.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100679 if (size == k64) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100680 StoreValueWide(rl_dest, rl_result);
681 } else {
682 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100683 StoreValue(rl_dest, rl_result);
684 }
685 return true;
686}
687
688bool Arm64Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
689 RegLocation rl_src_address = info->args[0]; // long address
Matteo Franchin43ec8732014-03-31 15:00:14 +0100690 RegLocation rl_src_value = info->args[2]; // [size] value
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100691 RegLocation rl_address = LoadValueWide(rl_src_address, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100692
693 RegLocation rl_value;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100694 if (size == k64) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100695 rl_value = LoadValueWide(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100696 } else {
697 DCHECK(size == kSignedByte || size == kSignedHalf || size == k32);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100698 rl_value = LoadValue(rl_src_value, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100699 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000700 StoreBaseDisp(rl_address.reg, 0, rl_value.reg, size, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100701 return true;
702}
703
Matteo Franchin43ec8732014-03-31 15:00:14 +0100704bool Arm64Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Serban Constantinescu169489b2014-06-11 16:43:35 +0100705 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100706 // Unused - RegLocation rl_src_unsafe = info->args[0];
707 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
708 RegLocation rl_src_offset = info->args[2]; // long low
Matteo Franchin43ec8732014-03-31 15:00:14 +0100709 RegLocation rl_src_expected = info->args[4]; // int, long or Object
710 // If is_long, high half is in info->args[5]
711 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
712 // If is_long, high half is in info->args[7]
713 RegLocation rl_dest = InlineTarget(info); // boolean place for result
714
Serban Constantinescu169489b2014-06-11 16:43:35 +0100715 // Load Object and offset
buzbeea0cd2d72014-06-01 09:33:49 -0700716 RegLocation rl_object = LoadValue(rl_src_obj, kRefReg);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100717 RegLocation rl_offset = LoadValueWide(rl_src_offset, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100718
Matteo Franchin43ec8732014-03-31 15:00:14 +0100719 RegLocation rl_new_value;
Serban Constantinescu169489b2014-06-11 16:43:35 +0100720 RegLocation rl_expected;
721 if (is_long) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100722 rl_new_value = LoadValueWide(rl_src_new_value, kCoreReg);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100723 rl_expected = LoadValueWide(rl_src_expected, kCoreReg);
724 } else {
725 rl_new_value = LoadValue(rl_src_new_value, is_object ? kRefReg : kCoreReg);
726 rl_expected = LoadValue(rl_src_expected, is_object ? kRefReg : kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 }
728
729 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
730 // Mark card for object assuming new value is stored.
731 MarkGCCard(rl_new_value.reg, rl_object.reg);
732 }
733
Serban Constantinescu169489b2014-06-11 16:43:35 +0100734 RegStorage r_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100735 OpRegRegReg(kOpAdd, r_ptr, rl_object.reg, rl_offset.reg);
736
737 // Free now unneeded rl_object and rl_offset to give more temps.
738 ClobberSReg(rl_object.s_reg_low);
739 FreeTemp(rl_object.reg);
740 ClobberSReg(rl_offset.s_reg_low);
741 FreeTemp(rl_offset.reg);
742
Matteo Franchin43ec8732014-03-31 15:00:14 +0100743 // do {
744 // tmp = [r_ptr] - expected;
745 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
746 // result = tmp != 0;
747
Serban Constantinescu169489b2014-06-11 16:43:35 +0100748 RegStorage r_tmp;
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100749 RegStorage r_tmp_stored;
750 RegStorage rl_new_value_stored = rl_new_value.reg;
751 ArmOpcode wide = UNWIDE(0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100752 if (is_long) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100753 r_tmp_stored = r_tmp = AllocTempWide();
754 wide = WIDE(0);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100755 } else if (is_object) {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100756 // References use 64-bit registers, but are stored as compressed 32-bit values.
757 // This means r_tmp_stored != r_tmp.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100758 r_tmp = AllocTempRef();
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100759 r_tmp_stored = As32BitReg(r_tmp);
760 rl_new_value_stored = As32BitReg(rl_new_value_stored);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100761 } else {
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100762 r_tmp_stored = r_tmp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100763 }
764
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100765 RegStorage r_tmp32 = (r_tmp.Is32Bit()) ? r_tmp : As32BitReg(r_tmp);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100766 LIR* loop = NewLIR0(kPseudoTargetLabel);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100767 NewLIR2(kA64Ldaxr2rX | wide, r_tmp_stored.GetReg(), r_ptr.GetReg());
Serban Constantinescu169489b2014-06-11 16:43:35 +0100768 OpRegReg(kOpCmp, r_tmp, rl_expected.reg);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100769 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Serban Constantinescu169489b2014-06-11 16:43:35 +0100770 LIR* early_exit = OpCondBranch(kCondNe, NULL);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100771 NewLIR3(kA64Stlxr3wrX | wide, r_tmp32.GetReg(), rl_new_value_stored.GetReg(), r_ptr.GetReg());
772 NewLIR3(kA64Cmp3RdT, r_tmp32.GetReg(), 0, ENCODE_NO_SHIFT);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100773 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
774 OpCondBranch(kCondNe, loop);
775
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100776 LIR* exit_loop = NewLIR0(kPseudoTargetLabel);
777 early_exit->target = exit_loop;
778
Serban Constantinescu169489b2014-06-11 16:43:35 +0100779 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100780 NewLIR4(kA64Csinc4rrrc, rl_result.reg.GetReg(), rwzr, rwzr, kArmCondNe);
Serban Constantinescu169489b2014-06-11 16:43:35 +0100781
Matteo Franchin43ec8732014-03-31 15:00:14 +0100782 FreeTemp(r_tmp); // Now unneeded.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100783 FreeTemp(r_ptr); // Now unneeded.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100784
785 StoreValue(rl_dest, rl_result);
786
Matteo Franchin43ec8732014-03-31 15:00:14 +0100787 return true;
788}
789
790LIR* Arm64Mir2Lir::OpPcRelLoad(RegStorage reg, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100791 return RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp), reg.GetReg(), 0, 0, 0, 0, target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100792}
793
794LIR* Arm64Mir2Lir::OpVldm(RegStorage r_base, int count) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100795 LOG(FATAL) << "Unexpected use of OpVldm for Arm64";
796 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100797}
798
799LIR* Arm64Mir2Lir::OpVstm(RegStorage r_base, int count) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100800 LOG(FATAL) << "Unexpected use of OpVstm for Arm64";
801 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100802}
803
804void Arm64Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
805 RegLocation rl_result, int lit,
806 int first_bit, int second_bit) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100807 OpRegRegRegShift(kOpAdd, rl_result.reg, rl_src.reg, rl_src.reg, EncodeShift(kA64Lsl, second_bit - first_bit));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100808 if (first_bit != 0) {
809 OpRegRegImm(kOpLsl, rl_result.reg, rl_result.reg, first_bit);
810 }
811}
812
813void Arm64Mir2Lir::GenDivZeroCheckWide(RegStorage reg) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100814 LOG(FATAL) << "Unexpected use of GenDivZero for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100815}
816
817// Test suspend flag, return target of taken suspend branch
818LIR* Arm64Mir2Lir::OpTestSuspend(LIR* target) {
Zheng Xubaa7c882014-06-30 14:26:50 +0800819 NewLIR3(kA64Subs3rRd, rwSUSPEND, rwSUSPEND, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100820 return OpCondBranch((target == NULL) ? kCondEq : kCondNe, target);
821}
822
823// Decrement register and branch on condition
824LIR* Arm64Mir2Lir::OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target) {
buzbee33ae5582014-06-12 14:56:32 -0700825 // Combine sub & test using sub setflags encoding here. We need to make sure a
826 // subtract form that sets carry is used, so generate explicitly.
827 // TODO: might be best to add a new op, kOpSubs, and handle it generically.
828 ArmOpcode opcode = reg.Is64Bit() ? WIDE(kA64Subs3rRd) : UNWIDE(kA64Subs3rRd);
829 NewLIR3(opcode, reg.GetReg(), reg.GetReg(), 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100830 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100831 return OpCondBranch(c_code, target);
832}
833
Andreas Gampeb14329f2014-05-15 11:16:06 -0700834bool Arm64Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100835#if ANDROID_SMP != 0
836 // Start off with using the last LIR as the barrier. If it is not enough, then we will generate one.
837 LIR* barrier = last_lir_insn_;
838
839 int dmb_flavor;
840 // TODO: revisit Arm barrier kinds
841 switch (barrier_kind) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700842 case kAnyStore: dmb_flavor = kISH; break;
843 case kLoadAny: dmb_flavor = kISH; break;
844 // We conjecture that kISHLD is insufficient. It is documented
845 // to provide LoadLoad | StoreStore ordering. But if this were used
846 // to implement volatile loads, we suspect that the lack of store
847 // atomicity on ARM would cause us to allow incorrect results for
848 // the canonical IRIW example. But we're not sure.
849 // We should be using acquire loads instead.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100850 case kStoreStore: dmb_flavor = kISHST; break;
Hans Boehm48f5c472014-06-27 14:50:10 -0700851 case kAnyAny: dmb_flavor = kISH; break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100852 default:
853 LOG(FATAL) << "Unexpected MemBarrierKind: " << barrier_kind;
854 dmb_flavor = kSY; // quiet gcc.
855 break;
856 }
857
Andreas Gampeb14329f2014-05-15 11:16:06 -0700858 bool ret = false;
859
Matteo Franchin43ec8732014-03-31 15:00:14 +0100860 // If the same barrier already exists, don't generate another.
861 if (barrier == nullptr
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100862 || (barrier->opcode != kA64Dmb1B || barrier->operands[0] != dmb_flavor)) {
863 barrier = NewLIR1(kA64Dmb1B, dmb_flavor);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700864 ret = true;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100865 }
866
867 // At this point we must have a memory barrier. Mark it as a scheduling barrier as well.
868 DCHECK(!barrier->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100869 barrier->u.m.def_mask = &kEncodeAll;
Andreas Gampeb14329f2014-05-15 11:16:06 -0700870 return ret;
871#else
872 return false;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100873#endif
874}
875
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100876void Arm64Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
877 RegLocation rl_result;
878
879 rl_src = LoadValue(rl_src, kCoreReg);
880 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700881 NewLIR4(WIDE(kA64Sbfm4rrdd), rl_result.reg.GetReg(), As64BitReg(rl_src.reg).GetReg(), 0, 31);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100882 StoreValueWide(rl_dest, rl_result);
883}
884
885void Arm64Mir2Lir::GenDivRemLong(Instruction::Code opcode, RegLocation rl_dest,
886 RegLocation rl_src1, RegLocation rl_src2, bool is_div) {
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +0100887 if (rl_src2.is_const) {
888 DCHECK(rl_src2.wide);
889 int64_t lit = mir_graph_->ConstantValueWide(rl_src2);
890 if (HandleEasyDivRem64(opcode, is_div, rl_src1, rl_dest, lit)) {
891 return;
892 }
893 }
894
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100895 RegLocation rl_result;
896 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
897 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
898 GenDivZeroCheck(rl_src2.reg);
899 rl_result = GenDivRem(rl_dest, rl_src1.reg, rl_src2.reg, is_div);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100900 StoreValueWide(rl_dest, rl_result);
901}
902
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100903void Arm64Mir2Lir::GenLongOp(OpKind op, RegLocation rl_dest, RegLocation rl_src1,
904 RegLocation rl_src2) {
905 RegLocation rl_result;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100906
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100907 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
908 rl_src2 = LoadValueWide(rl_src2, kCoreReg);
909 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100910 OpRegRegRegShift(op, rl_result.reg, rl_src1.reg, rl_src2.reg, ENCODE_NO_SHIFT);
911 StoreValueWide(rl_dest, rl_result);
912}
913
914void Arm64Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
915 RegLocation rl_result;
916
917 rl_src = LoadValueWide(rl_src, kCoreReg);
918 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
919 OpRegRegShift(kOpNeg, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
920 StoreValueWide(rl_dest, rl_result);
921}
922
923void Arm64Mir2Lir::GenNotLong(RegLocation rl_dest, RegLocation rl_src) {
924 RegLocation rl_result;
925
926 rl_src = LoadValueWide(rl_src, kCoreReg);
927 rl_result = EvalLocWide(rl_dest, kCoreReg, true);
928 OpRegRegShift(kOpMvn, rl_result.reg, rl_src.reg, ENCODE_NO_SHIFT);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100929 StoreValueWide(rl_dest, rl_result);
930}
931
Matteo Franchin43ec8732014-03-31 15:00:14 +0100932void Arm64Mir2Lir::GenMulLong(Instruction::Code opcode, RegLocation rl_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100933 RegLocation rl_src1, RegLocation rl_src2) {
934 GenLongOp(kOpMul, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100935}
936
937void Arm64Mir2Lir::GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100938 RegLocation rl_src2) {
939 GenLongOp(kOpAdd, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100940}
941
942void Arm64Mir2Lir::GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
943 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100944 GenLongOp(kOpSub, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100945}
946
947void Arm64Mir2Lir::GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
948 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100949 GenLongOp(kOpAnd, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100950}
951
952void Arm64Mir2Lir::GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
953 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100954 GenLongOp(kOpOr, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100955}
956
957void Arm64Mir2Lir::GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
958 RegLocation rl_src2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100959 GenLongOp(kOpXor, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100960}
961
962/*
963 * Generate array load
964 */
965void Arm64Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
966 RegLocation rl_index, RegLocation rl_dest, int scale) {
967 RegisterClass reg_class = RegClassBySize(size);
968 int len_offset = mirror::Array::LengthOffset().Int32Value();
969 int data_offset;
970 RegLocation rl_result;
971 bool constant_index = rl_index.is_const;
buzbeea0cd2d72014-06-01 09:33:49 -0700972 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100973 if (!constant_index) {
974 rl_index = LoadValue(rl_index, kCoreReg);
975 }
976
977 if (rl_dest.wide) {
978 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
979 } else {
980 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
981 }
982
983 // If index is constant, just fold it into the data offset
984 if (constant_index) {
985 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
986 }
987
988 /* null object? */
989 GenNullCheck(rl_array.reg, opt_flags);
990
991 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
992 RegStorage reg_len;
993 if (needs_range_check) {
994 reg_len = AllocTemp();
995 /* Get len */
996 Load32Disp(rl_array.reg, len_offset, reg_len);
997 MarkPossibleNullPointerException(opt_flags);
998 } else {
999 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1000 }
1001 if (rl_dest.wide || rl_dest.fp || constant_index) {
1002 RegStorage reg_ptr;
1003 if (constant_index) {
1004 reg_ptr = rl_array.reg; // NOTE: must not alter reg_ptr in constant case.
1005 } else {
1006 // No special indexed operation, lea + load w/ displacement
buzbeea0cd2d72014-06-01 09:33:49 -07001007 reg_ptr = AllocTempRef();
buzbee33ae5582014-06-12 14:56:32 -07001008 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1009 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001010 FreeTemp(rl_index.reg);
1011 }
1012 rl_result = EvalLoc(rl_dest, reg_class, true);
1013
1014 if (needs_range_check) {
1015 if (constant_index) {
1016 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1017 } else {
1018 GenArrayBoundsCheck(rl_index.reg, reg_len);
1019 }
1020 FreeTemp(reg_len);
1021 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001022 if (rl_result.ref) {
1023 LoadRefDisp(reg_ptr, data_offset, rl_result.reg, kNotVolatile);
1024 } else {
1025 LoadBaseDisp(reg_ptr, data_offset, rl_result.reg, size, kNotVolatile);
1026 }
Vladimir Marko455759b2014-05-06 20:49:36 +01001027 MarkPossibleNullPointerException(opt_flags);
1028 if (!constant_index) {
1029 FreeTemp(reg_ptr);
1030 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001031 if (rl_dest.wide) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001032 StoreValueWide(rl_dest, rl_result);
1033 } else {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001034 StoreValue(rl_dest, rl_result);
1035 }
1036 } else {
1037 // Offset base, then use indexed load
buzbeea0cd2d72014-06-01 09:33:49 -07001038 RegStorage reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001039 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1040 FreeTemp(rl_array.reg);
1041 rl_result = EvalLoc(rl_dest, reg_class, true);
1042
1043 if (needs_range_check) {
1044 GenArrayBoundsCheck(rl_index.reg, reg_len);
1045 FreeTemp(reg_len);
1046 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001047 if (rl_result.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001048 LoadRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001049 } else {
1050 LoadBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_result.reg, scale, size);
1051 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001052 MarkPossibleNullPointerException(opt_flags);
1053 FreeTemp(reg_ptr);
1054 StoreValue(rl_dest, rl_result);
1055 }
1056}
1057
1058/*
1059 * Generate array store
1060 *
1061 */
1062void Arm64Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
1063 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
1064 RegisterClass reg_class = RegClassBySize(size);
1065 int len_offset = mirror::Array::LengthOffset().Int32Value();
1066 bool constant_index = rl_index.is_const;
1067
1068 int data_offset;
1069 if (size == k64 || size == kDouble) {
1070 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
1071 } else {
1072 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
1073 }
1074
1075 // If index is constant, just fold it into the data offset.
1076 if (constant_index) {
1077 data_offset += mir_graph_->ConstantValue(rl_index) << scale;
1078 }
1079
buzbeea0cd2d72014-06-01 09:33:49 -07001080 rl_array = LoadValue(rl_array, kRefReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001081 if (!constant_index) {
1082 rl_index = LoadValue(rl_index, kCoreReg);
1083 }
1084
1085 RegStorage reg_ptr;
1086 bool allocated_reg_ptr_temp = false;
1087 if (constant_index) {
1088 reg_ptr = rl_array.reg;
1089 } else if (IsTemp(rl_array.reg) && !card_mark) {
1090 Clobber(rl_array.reg);
1091 reg_ptr = rl_array.reg;
1092 } else {
1093 allocated_reg_ptr_temp = true;
buzbeea0cd2d72014-06-01 09:33:49 -07001094 reg_ptr = AllocTempRef();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001095 }
1096
1097 /* null object? */
1098 GenNullCheck(rl_array.reg, opt_flags);
1099
1100 bool needs_range_check = (!(opt_flags & MIR_IGNORE_RANGE_CHECK));
1101 RegStorage reg_len;
1102 if (needs_range_check) {
1103 reg_len = AllocTemp();
1104 // NOTE: max live temps(4) here.
1105 /* Get len */
1106 Load32Disp(rl_array.reg, len_offset, reg_len);
1107 MarkPossibleNullPointerException(opt_flags);
1108 } else {
1109 ForceImplicitNullCheck(rl_array.reg, opt_flags);
1110 }
1111 /* at this point, reg_ptr points to array, 2 live temps */
1112 if (rl_src.wide || rl_src.fp || constant_index) {
1113 if (rl_src.wide) {
1114 rl_src = LoadValueWide(rl_src, reg_class);
1115 } else {
1116 rl_src = LoadValue(rl_src, reg_class);
1117 }
1118 if (!constant_index) {
buzbee33ae5582014-06-12 14:56:32 -07001119 OpRegRegRegShift(kOpAdd, reg_ptr, rl_array.reg, As64BitReg(rl_index.reg),
1120 EncodeShift(kA64Lsl, scale));
Matteo Franchin43ec8732014-03-31 15:00:14 +01001121 }
1122 if (needs_range_check) {
1123 if (constant_index) {
1124 GenArrayBoundsCheck(mir_graph_->ConstantValue(rl_index), reg_len);
1125 } else {
1126 GenArrayBoundsCheck(rl_index.reg, reg_len);
1127 }
1128 FreeTemp(reg_len);
1129 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001130 if (rl_src.ref) {
1131 StoreRefDisp(reg_ptr, data_offset, rl_src.reg, kNotVolatile);
1132 } else {
1133 StoreBaseDisp(reg_ptr, data_offset, rl_src.reg, size, kNotVolatile);
1134 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001135 MarkPossibleNullPointerException(opt_flags);
1136 } else {
1137 /* reg_ptr -> array data */
1138 OpRegRegImm(kOpAdd, reg_ptr, rl_array.reg, data_offset);
1139 rl_src = LoadValue(rl_src, reg_class);
1140 if (needs_range_check) {
1141 GenArrayBoundsCheck(rl_index.reg, reg_len);
1142 FreeTemp(reg_len);
1143 }
Andreas Gampe3c12c512014-06-24 18:46:29 +00001144 if (rl_src.ref) {
Matteo Franchin255e0142014-07-04 13:50:41 +01001145 StoreRefIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001146 } else {
1147 StoreBaseIndexed(reg_ptr, As64BitReg(rl_index.reg), rl_src.reg, scale, size);
1148 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001149 MarkPossibleNullPointerException(opt_flags);
1150 }
1151 if (allocated_reg_ptr_temp) {
1152 FreeTemp(reg_ptr);
1153 }
1154 if (card_mark) {
1155 MarkGCCard(rl_src.reg, rl_array.reg);
1156 }
1157}
1158
Matteo Franchin43ec8732014-03-31 15:00:14 +01001159void Arm64Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode,
Matteo Franchin7c6c2ac2014-07-01 18:03:08 +01001160 RegLocation rl_dest, RegLocation rl_src, RegLocation rl_shift) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001161 OpKind op = kOpBkpt;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001162 // Per spec, we only care about low 6 bits of shift amount.
1163 int shift_amount = mir_graph_->ConstantValue(rl_shift) & 0x3f;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001164 rl_src = LoadValueWide(rl_src, kCoreReg);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001165 if (shift_amount == 0) {
1166 StoreValueWide(rl_dest, rl_src);
1167 return;
1168 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001169
1170 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001171 switch (opcode) {
1172 case Instruction::SHL_LONG:
1173 case Instruction::SHL_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001174 op = kOpLsl;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001175 break;
1176 case Instruction::SHR_LONG:
1177 case Instruction::SHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001178 op = kOpAsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001179 break;
1180 case Instruction::USHR_LONG:
1181 case Instruction::USHR_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001182 op = kOpLsr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001183 break;
1184 default:
1185 LOG(FATAL) << "Unexpected case";
1186 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001187 OpRegRegImm(op, rl_result.reg, rl_src.reg, shift_amount);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001188 StoreValueWide(rl_dest, rl_result);
1189}
1190
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001191void Arm64Mir2Lir::GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
1192 RegLocation rl_src1, RegLocation rl_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001193 if ((opcode == Instruction::SUB_LONG) || (opcode == Instruction::SUB_LONG_2ADDR)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001194 if (!rl_src2.is_const) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001195 return GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001196 }
1197 } else {
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001198 // Associativity.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001199 if (!rl_src2.is_const) {
1200 DCHECK(rl_src1.is_const);
1201 std::swap(rl_src1, rl_src2);
1202 }
1203 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001204 DCHECK(rl_src2.is_const);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001205
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001206 OpKind op = kOpBkpt;
1207 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
1208
Matteo Franchin43ec8732014-03-31 15:00:14 +01001209 switch (opcode) {
1210 case Instruction::ADD_LONG:
1211 case Instruction::ADD_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001212 op = kOpAdd;
1213 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001214 case Instruction::SUB_LONG:
1215 case Instruction::SUB_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001216 op = kOpSub;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001217 break;
1218 case Instruction::AND_LONG:
1219 case Instruction::AND_LONG_2ADDR:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001220 op = kOpAnd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001221 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001222 case Instruction::OR_LONG:
1223 case Instruction::OR_LONG_2ADDR:
1224 op = kOpOr;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001225 break;
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001226 case Instruction::XOR_LONG:
1227 case Instruction::XOR_LONG_2ADDR:
1228 op = kOpXor;
1229 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001230 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001231 LOG(FATAL) << "Unexpected opcode";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001232 }
Serban Constantinescued65c5e2014-05-22 15:10:18 +01001233
1234 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1235 RegLocation rl_result = EvalLocWide(rl_dest, kCoreReg, true);
Zheng Xue2eb29e2014-06-12 10:22:33 +08001236 OpRegRegImm64(op, rl_result.reg, rl_src1.reg, val);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001237 StoreValueWide(rl_dest, rl_result);
1238}
1239
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001240/**
1241 * @brief Split a register list in pairs or registers.
1242 *
1243 * Given a list of registers in @p reg_mask, split the list in pairs. Use as follows:
1244 * @code
1245 * int reg1 = -1, reg2 = -1;
1246 * while (reg_mask) {
1247 * reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1248 * if (UNLIKELY(reg2 < 0)) {
1249 * // Single register in reg1.
1250 * } else {
1251 * // Pair in reg1, reg2.
1252 * }
1253 * }
1254 * @endcode
1255 */
1256uint32_t Arm64Mir2Lir::GenPairWise(uint32_t reg_mask, int* reg1, int* reg2) {
1257 // Find first register.
1258 int first_bit_set = __builtin_ctz(reg_mask) + 1;
1259 int reg = *reg1 + first_bit_set;
1260 reg_mask >>= first_bit_set;
1261
1262 if (LIKELY(reg_mask)) {
1263 // Save the first register, find the second and use the pair opcode.
1264 int second_bit_set = __builtin_ctz(reg_mask) + 1;
1265 *reg2 = reg;
1266 reg_mask >>= second_bit_set;
1267 *reg1 = reg + second_bit_set;
1268 return reg_mask;
1269 }
1270
1271 // Use the single opcode, as we just have one register.
1272 *reg1 = reg;
1273 *reg2 = -1;
1274 return reg_mask;
1275}
1276
1277void Arm64Mir2Lir::UnSpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) {
1278 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001279 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001280
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001281 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001282 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1283 if (UNLIKELY(reg2 < 0)) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001284 NewLIR3(WIDE(kA64Ldr3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001285 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001286 DCHECK_LE(offset, 63);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001287 NewLIR4(WIDE(kA64Ldp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1288 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001289 }
1290 }
1291}
1292
1293void Arm64Mir2Lir::SpillCoreRegs(RegStorage base, int offset, uint32_t reg_mask) {
1294 int reg1 = -1, reg2 = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001295 const int reg_log2_size = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001296
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001297 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001298 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1299 if (UNLIKELY(reg2 < 0)) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001300 NewLIR3(WIDE(kA64Str3rXD), RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001301 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001302 NewLIR4(WIDE(kA64Stp4rrXD), RegStorage::Solo64(reg2).GetReg(),
1303 RegStorage::Solo64(reg1).GetReg(), base.GetReg(), offset);
1304 }
1305 }
1306}
1307
1308void Arm64Mir2Lir::UnSpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) {
1309 int reg1 = -1, reg2 = -1;
1310 const int reg_log2_size = 3;
1311
1312 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1313 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1314 if (UNLIKELY(reg2 < 0)) {
1315 NewLIR3(FWIDE(kA64Ldr3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1316 } else {
1317 NewLIR4(WIDE(kA64Ldp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1318 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1319 }
1320 }
1321}
1322
1323// TODO(Arm64): consider using ld1 and st1?
1324void Arm64Mir2Lir::SpillFPRegs(RegStorage base, int offset, uint32_t reg_mask) {
1325 int reg1 = -1, reg2 = -1;
1326 const int reg_log2_size = 3;
1327
1328 for (offset = (offset >> reg_log2_size); reg_mask; offset += 2) {
1329 reg_mask = GenPairWise(reg_mask, & reg1, & reg2);
1330 if (UNLIKELY(reg2 < 0)) {
1331 NewLIR3(FWIDE(kA64Str3fXD), RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
1332 } else {
1333 NewLIR4(WIDE(kA64Stp4ffXD), RegStorage::FloatSolo64(reg2).GetReg(),
1334 RegStorage::FloatSolo64(reg1).GetReg(), base.GetReg(), offset);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001335 }
1336 }
1337}
1338
Serban Constantinescu23abec92014-07-02 16:13:38 +01001339bool Arm64Mir2Lir::GenInlinedReverseBits(CallInfo* info, OpSize size) {
1340 ArmOpcode wide = (size == k64) ? WIDE(0) : UNWIDE(0);
1341 RegLocation rl_src_i = info->args[0];
1342 RegLocation rl_dest = (size == k64) ? InlineTargetWide(info) : InlineTarget(info); // result reg
1343 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1344 RegLocation rl_i = (size == k64) ? LoadValueWide(rl_src_i, kCoreReg) : LoadValue(rl_src_i, kCoreReg);
1345 NewLIR2(kA64Rbit2rr | wide, rl_result.reg.GetReg(), rl_i.reg.GetReg());
1346 (size == k64) ? StoreValueWide(rl_dest, rl_result) : StoreValue(rl_dest, rl_result);
1347 return true;
1348}
1349
Matteo Franchin43ec8732014-03-31 15:00:14 +01001350} // namespace art