blob: 58769ea9cccac2695dc500479f6ffc4a87b93fbf [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
Matteo Franchin43ec8732014-03-31 15:00:14 +010017#include "codegen_arm64.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080018
19#include "arm64_lir.h"
20#include "base/logging.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010021#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
24namespace art {
25
Matteo Franchine45fb9e2014-05-06 10:10:30 +010026/* This file contains codegen for the A64 ISA. */
Matteo Franchin43ec8732014-03-31 15:00:14 +010027
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010028int32_t Arm64Mir2Lir::EncodeImmSingle(uint32_t bits) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010029 /*
30 * Valid values will have the form:
31 *
32 * aBbb.bbbc.defg.h000.0000.0000.0000.0000
33 *
34 * where B = not(b). In other words, if b == 1, then B == 0 and viceversa.
35 */
36
37 // bits[19..0] are cleared.
38 if ((bits & 0x0007ffff) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010039 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010040
41 // bits[29..25] are all set or all cleared.
42 uint32_t b_pattern = (bits >> 16) & 0x3e00;
43 if (b_pattern != 0 && b_pattern != 0x3e00)
44 return -1;
45
46 // bit[30] and bit[29] are opposite.
47 if (((bits ^ (bits << 1)) & 0x40000000) == 0)
48 return -1;
49
50 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
51 // bit7: a000.0000
52 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
53 // bit6: 0b00.0000
54 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
55 // bit5_to_0: 00cd.efgh
56 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
57 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010058}
59
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010060int32_t Arm64Mir2Lir::EncodeImmDouble(uint64_t bits) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010061 /*
62 * Valid values will have the form:
63 *
64 * aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
65 * 0000.0000.0000.0000.0000.0000.0000.0000
66 *
67 * where B = not(b).
68 */
69
70 // bits[47..0] are cleared.
71 if ((bits & UINT64_C(0xffffffffffff)) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010072 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010073
74 // bits[61..54] are all set or all cleared.
75 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
76 if (b_pattern != 0 && b_pattern != 0x3fc0)
77 return -1;
78
79 // bit[62] and bit[61] are opposite.
80 if (((bits ^ (bits << 1)) & UINT64_C(0x4000000000000000)) == 0)
81 return -1;
82
83 // bit7: a000.0000
84 uint32_t bit7 = ((bits >> 63) & 0x1) << 7;
85 // bit6: 0b00.0000
86 uint32_t bit6 = ((bits >> 61) & 0x1) << 6;
87 // bit5_to_0: 00cd.efgh
88 uint32_t bit5_to_0 = (bits >> 48) & 0x3f;
89 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010090}
91
Serban Constantinescu63999682014-07-15 17:44:21 +010092size_t Arm64Mir2Lir::GetLoadStoreSize(LIR* lir) {
93 bool opcode_is_wide = IS_WIDE(lir->opcode);
Matteo Franchin4163c532014-07-15 15:20:27 +010094 A64Opcode opcode = UNWIDE(lir->opcode);
Serban Constantinescu63999682014-07-15 17:44:21 +010095 DCHECK(!IsPseudoLirOp(opcode));
Matteo Franchin4163c532014-07-15 15:20:27 +010096 const A64EncodingMap *encoder = &EncodingMap[opcode];
Serban Constantinescu63999682014-07-15 17:44:21 +010097 uint32_t bits = opcode_is_wide ? encoder->xskeleton : encoder->wskeleton;
98 return (bits >> 30);
99}
100
101size_t Arm64Mir2Lir::GetInstructionOffset(LIR* lir) {
102 size_t offset = lir->operands[2];
103 uint64_t check_flags = GetTargetInstFlags(lir->opcode);
104 DCHECK((check_flags & IS_LOAD) || (check_flags & IS_STORE));
105 if (check_flags & SCALED_OFFSET_X0) {
106 DCHECK(check_flags & IS_TERTIARY_OP);
107 offset = offset * (1 << GetLoadStoreSize(lir));
108 }
109 return offset;
110}
111
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100112LIR* Arm64Mir2Lir::LoadFPConstantValue(RegStorage r_dest, int32_t value) {
113 DCHECK(r_dest.IsSingle());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100114 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100115 return NewLIR2(kA64Fmov2sw, r_dest.GetReg(), rwzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100116 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100117 int32_t encoded_imm = EncodeImmSingle((uint32_t)value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100118 if (encoded_imm >= 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100119 return NewLIR2(kA64Fmov2fI, r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100120 }
121 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100122
Matteo Franchin43ec8732014-03-31 15:00:14 +0100123 LIR* data_target = ScanLiteralPool(literal_list_, value, 0);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 if (data_target == nullptr) {
Andreas Gampef9879272014-06-18 23:19:07 -0700125 // Wide, as we need 8B alignment.
126 data_target = AddWideData(&literal_list_, value, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100127 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100128
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100129 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100130 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, kA64Ldr2fp,
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100131 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100132 AppendLIR(load_pc_rel);
133 return load_pc_rel;
134}
135
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100136LIR* Arm64Mir2Lir::LoadFPConstantValueWide(RegStorage r_dest, int64_t value) {
137 DCHECK(r_dest.IsDouble());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100138 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100139 return NewLIR2(kA64Fmov2Sx, r_dest.GetReg(), rxzr);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100140 } else {
141 int32_t encoded_imm = EncodeImmDouble(value);
142 if (encoded_imm >= 0) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100143 return NewLIR2(WIDE(kA64Fmov2fI), r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100144 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100145 }
146
147 // No short form - load from the literal pool.
148 int32_t val_lo = Low32Bits(value);
149 int32_t val_hi = High32Bits(value);
150 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700151 if (data_target == nullptr) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100152 data_target = AddWideData(&literal_list_, val_lo, val_hi);
153 }
154
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100155 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchin4163c532014-07-15 15:20:27 +0100156 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2fp),
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100157 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100158 AppendLIR(load_pc_rel);
159 return load_pc_rel;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100160}
161
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100162static int CountLeadingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100163 return (is_wide) ? __builtin_clzll(value) : __builtin_clz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100164}
Matteo Franchin43ec8732014-03-31 15:00:14 +0100165
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100166static int CountTrailingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100167 return (is_wide) ? __builtin_ctzll(value) : __builtin_ctz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100168}
169
170static int CountSetBits(bool is_wide, uint64_t value) {
171 return ((is_wide) ?
Zheng Xue2eb29e2014-06-12 10:22:33 +0800172 __builtin_popcountll(value) : __builtin_popcount((uint32_t)value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100173}
174
175/**
176 * @brief Try encoding an immediate in the form required by logical instructions.
177 *
178 * @param is_wide Whether @p value is a 64-bit (as opposed to 32-bit) value.
179 * @param value An integer to be encoded. This is interpreted as 64-bit if @p is_wide is true and as
180 * 32-bit if @p is_wide is false.
181 * @return A non-negative integer containing the encoded immediate or -1 if the encoding failed.
182 * @note This is the inverse of Arm64Mir2Lir::DecodeLogicalImmediate().
183 */
184int Arm64Mir2Lir::EncodeLogicalImmediate(bool is_wide, uint64_t value) {
185 unsigned n, imm_s, imm_r;
186
187 // Logical immediates are encoded using parameters n, imm_s and imm_r using
188 // the following table:
189 //
190 // N imms immr size S R
191 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
192 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
193 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
194 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
195 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
196 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
197 // (s bits must not be all set)
198 //
199 // A pattern is constructed of size bits, where the least significant S+1
200 // bits are set. The pattern is rotated right by R, and repeated across a
201 // 32 or 64-bit value, depending on destination register width.
202 //
203 // To test if an arbitary immediate can be encoded using this scheme, an
204 // iterative algorithm is used.
205 //
206
207 // 1. If the value has all set or all clear bits, it can't be encoded.
208 if (value == 0 || value == ~UINT64_C(0) ||
209 (!is_wide && (uint32_t)value == ~UINT32_C(0))) {
210 return -1;
211 }
212
213 unsigned lead_zero = CountLeadingZeros(is_wide, value);
214 unsigned lead_one = CountLeadingZeros(is_wide, ~value);
215 unsigned trail_zero = CountTrailingZeros(is_wide, value);
216 unsigned trail_one = CountTrailingZeros(is_wide, ~value);
217 unsigned set_bits = CountSetBits(is_wide, value);
218
219 // The fixed bits in the immediate s field.
220 // If width == 64 (X reg), start at 0xFFFFFF80.
221 // If width == 32 (W reg), start at 0xFFFFFFC0, as the iteration for 64-bit
222 // widths won't be executed.
223 unsigned width = (is_wide) ? 64 : 32;
224 int imm_s_fixed = (is_wide) ? -128 : -64;
225 int imm_s_mask = 0x3f;
226
227 for (;;) {
228 // 2. If the value is two bits wide, it can be encoded.
229 if (width == 2) {
230 n = 0;
231 imm_s = 0x3C;
232 imm_r = (value & 3) - 1;
233 break;
234 }
235
236 n = (width == 64) ? 1 : 0;
237 imm_s = ((imm_s_fixed | (set_bits - 1)) & imm_s_mask);
238 if ((lead_zero + set_bits) == width) {
239 imm_r = 0;
240 } else {
241 imm_r = (lead_zero > 0) ? (width - trail_zero) : lead_one;
242 }
243
244 // 3. If the sum of leading zeros, trailing zeros and set bits is
245 // equal to the bit width of the value, it can be encoded.
246 if (lead_zero + trail_zero + set_bits == width) {
247 break;
248 }
249
250 // 4. If the sum of leading ones, trailing ones and unset bits in the
251 // value is equal to the bit width of the value, it can be encoded.
252 if (lead_one + trail_one + (width - set_bits) == width) {
253 break;
254 }
255
256 // 5. If the most-significant half of the bitwise value is equal to
257 // the least-significant half, return to step 2 using the
258 // least-significant half of the value.
259 uint64_t mask = (UINT64_C(1) << (width >> 1)) - 1;
260 if ((value & mask) == ((value >> (width >> 1)) & mask)) {
261 width >>= 1;
262 set_bits >>= 1;
263 imm_s_fixed >>= 1;
264 continue;
265 }
266
267 // 6. Otherwise, the value can't be encoded.
268 return -1;
269 }
270
271 return (n << 12 | imm_r << 6 | imm_s);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100272}
273
Matteo Franchinc763e352014-07-04 12:53:27 +0100274// Maximum number of instructions to use for encoding the immediate.
275static const int max_num_ops_per_const_load = 2;
276
277/**
278 * @brief Return the number of fast halfwords in the given uint64_t integer.
279 * @details The input integer is split into 4 halfwords (bits 0-15, 16-31, 32-47, 48-63). The
280 * number of fast halfwords (halfwords that are either 0 or 0xffff) is returned. See below for
281 * a more accurate description.
282 * @param value The input 64-bit integer.
283 * @return Return @c retval such that (retval & 0x7) is the maximum between n and m, where n is
284 * the number of halfwords with all bits unset (0) and m is the number of halfwords with all bits
285 * set (0xffff). Additionally (retval & 0x8) is set when m > n.
286 */
287static int GetNumFastHalfWords(uint64_t value) {
288 unsigned int num_0000_halfwords = 0;
289 unsigned int num_ffff_halfwords = 0;
290 for (int shift = 0; shift < 64; shift += 16) {
291 uint16_t halfword = static_cast<uint16_t>(value >> shift);
292 if (halfword == 0)
293 num_0000_halfwords++;
294 else if (halfword == UINT16_C(0xffff))
295 num_ffff_halfwords++;
296 }
297 if (num_0000_halfwords >= num_ffff_halfwords) {
298 DCHECK_LE(num_0000_halfwords, 4U);
299 return num_0000_halfwords;
300 } else {
301 DCHECK_LE(num_ffff_halfwords, 4U);
302 return num_ffff_halfwords | 0x8;
303 }
304}
305
306// The InexpensiveConstantXXX variants below are used in the promotion algorithm to determine how a
307// constant is considered for promotion. If the constant is "inexpensive" then the promotion
308// algorithm will give it a low priority for promotion, even when it is referenced many times in
309// the code.
310
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700311bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value ATTRIBUTE_UNUSED) {
Matteo Franchinc763e352014-07-04 12:53:27 +0100312 // A 32-bit int can always be loaded with 2 instructions (and without using the literal pool).
313 // We therefore return true and give it a low priority for promotion.
314 return true;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100315}
316
317bool Arm64Mir2Lir::InexpensiveConstantFloat(int32_t value) {
318 return EncodeImmSingle(value) >= 0;
319}
320
321bool Arm64Mir2Lir::InexpensiveConstantLong(int64_t value) {
Matteo Franchinc763e352014-07-04 12:53:27 +0100322 int num_slow_halfwords = 4 - (GetNumFastHalfWords(value) & 0x7);
323 if (num_slow_halfwords <= max_num_ops_per_const_load) {
324 return true;
325 }
326 return (EncodeLogicalImmediate(/*is_wide=*/true, value) >= 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100327}
328
329bool Arm64Mir2Lir::InexpensiveConstantDouble(int64_t value) {
330 return EncodeImmDouble(value) >= 0;
331}
332
Matteo Franchinc763e352014-07-04 12:53:27 +0100333// The InexpensiveConstantXXX variants below are used to determine which A64 instructions to use
334// when one of the operands is an immediate (e.g. register version or immediate version of add).
335
336bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value, Instruction::Code opcode) {
337 switch (opcode) {
338 case Instruction::IF_EQ:
339 case Instruction::IF_NE:
340 case Instruction::IF_LT:
341 case Instruction::IF_GE:
342 case Instruction::IF_GT:
343 case Instruction::IF_LE:
344 case Instruction::ADD_INT:
345 case Instruction::ADD_INT_2ADDR:
346 case Instruction::SUB_INT:
347 case Instruction::SUB_INT_2ADDR:
348 // The code below is consistent with the implementation of OpRegRegImm().
349 {
buzbeeb504d2f2014-09-26 15:09:06 -0700350 uint32_t abs_value = (value == INT_MIN) ? value : std::abs(value);
Matteo Franchinc763e352014-07-04 12:53:27 +0100351 if (abs_value < 0x1000) {
352 return true;
353 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
354 return true;
355 }
356 return false;
357 }
358 case Instruction::SHL_INT:
359 case Instruction::SHL_INT_2ADDR:
360 case Instruction::SHR_INT:
361 case Instruction::SHR_INT_2ADDR:
362 case Instruction::USHR_INT:
363 case Instruction::USHR_INT_2ADDR:
364 return true;
365 case Instruction::AND_INT:
366 case Instruction::AND_INT_2ADDR:
367 case Instruction::AND_INT_LIT16:
368 case Instruction::AND_INT_LIT8:
369 case Instruction::OR_INT:
370 case Instruction::OR_INT_2ADDR:
371 case Instruction::OR_INT_LIT16:
372 case Instruction::OR_INT_LIT8:
373 case Instruction::XOR_INT:
374 case Instruction::XOR_INT_2ADDR:
375 case Instruction::XOR_INT_LIT16:
376 case Instruction::XOR_INT_LIT8:
377 if (value == 0 || value == INT32_C(-1)) {
378 return true;
379 }
380 return (EncodeLogicalImmediate(/*is_wide=*/false, value) >= 0);
381 default:
382 return false;
383 }
384}
385
Matteo Franchin43ec8732014-03-31 15:00:14 +0100386/*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100387 * Load a immediate using one single instruction when possible; otherwise
388 * use a pair of movz and movk instructions.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100389 *
390 * No additional register clobbering operation performed. Use this version when
391 * 1) r_dest is freshly returned from AllocTemp or
392 * 2) The codegen is under fixed register usage
393 */
394LIR* Arm64Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
395 LIR* res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100396
397 if (r_dest.IsFloat()) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100398 return LoadFPConstantValue(r_dest, value);
399 }
400
401 if (r_dest.Is64Bit()) {
402 return LoadConstantWide(r_dest, value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100403 }
404
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100405 // Loading SP/ZR with an immediate is not supported.
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100406 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
407 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100408
409 // Compute how many movk, movz instructions are needed to load the value.
410 uint16_t high_bits = High16Bits(value);
411 uint16_t low_bits = Low16Bits(value);
412
413 bool low_fast = ((uint16_t)(low_bits + 1) <= 1);
414 bool high_fast = ((uint16_t)(high_bits + 1) <= 1);
415
416 if (LIKELY(low_fast || high_fast)) {
417 // 1 instruction is enough to load the immediate.
418 if (LIKELY(low_bits == high_bits)) {
419 // Value is either 0 or -1: we can just use wzr.
Matteo Franchin4163c532014-07-15 15:20:27 +0100420 A64Opcode opcode = LIKELY(low_bits == 0) ? kA64Mov2rr : kA64Mvn2rr;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100421 res = NewLIR2(opcode, r_dest.GetReg(), rwzr);
422 } else {
423 uint16_t uniform_bits, useful_bits;
424 int shift;
425
426 if (LIKELY(high_fast)) {
427 shift = 0;
428 uniform_bits = high_bits;
429 useful_bits = low_bits;
430 } else {
431 shift = 1;
432 uniform_bits = low_bits;
433 useful_bits = high_bits;
434 }
435
436 if (UNLIKELY(uniform_bits != 0)) {
437 res = NewLIR3(kA64Movn3rdM, r_dest.GetReg(), ~useful_bits, shift);
438 } else {
439 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), useful_bits, shift);
440 }
441 }
442 } else {
443 // movk, movz require 2 instructions. Try detecting logical immediates.
444 int log_imm = EncodeLogicalImmediate(/*is_wide=*/false, value);
445 if (log_imm >= 0) {
446 res = NewLIR3(kA64Orr3Rrl, r_dest.GetReg(), rwzr, log_imm);
447 } else {
448 // Use 2 instructions.
449 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), low_bits, 0);
450 NewLIR3(kA64Movk3rdM, r_dest.GetReg(), high_bits, 1);
451 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100452 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100453
Matteo Franchin43ec8732014-03-31 15:00:14 +0100454 return res;
455}
456
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100457// TODO: clean up the names. LoadConstantWide() should really be LoadConstantNoClobberWide().
458LIR* Arm64Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100459 if (r_dest.IsFloat()) {
460 return LoadFPConstantValueWide(r_dest, value);
461 }
462
463 DCHECK(r_dest.Is64Bit());
464
465 // Loading SP/ZR with an immediate is not supported.
466 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
467 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
468
469 if (LIKELY(value == INT64_C(0) || value == INT64_C(-1))) {
470 // value is either 0 or -1: we can just use xzr.
Matteo Franchin4163c532014-07-15 15:20:27 +0100471 A64Opcode opcode = LIKELY(value == 0) ? WIDE(kA64Mov2rr) : WIDE(kA64Mvn2rr);
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100472 return NewLIR2(opcode, r_dest.GetReg(), rxzr);
473 }
474
475 // At least one in value's halfwords is not 0x0, nor 0xffff: find out how many.
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100476 uint64_t uvalue = static_cast<uint64_t>(value);
Matteo Franchinc763e352014-07-04 12:53:27 +0100477 int num_fast_halfwords = GetNumFastHalfWords(uvalue);
478 int num_slow_halfwords = 4 - (num_fast_halfwords & 0x7);
479 bool more_ffff_halfwords = (num_fast_halfwords & 0x8) != 0;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100480
Matteo Franchinc763e352014-07-04 12:53:27 +0100481 if (num_slow_halfwords > 1) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100482 // A single movz/movn is not enough. Try the logical immediate route.
483 int log_imm = EncodeLogicalImmediate(/*is_wide=*/true, value);
484 if (log_imm >= 0) {
485 return NewLIR3(WIDE(kA64Orr3Rrl), r_dest.GetReg(), rxzr, log_imm);
486 }
487 }
488
Matteo Franchinc763e352014-07-04 12:53:27 +0100489 if (num_slow_halfwords <= max_num_ops_per_const_load) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100490 // We can encode the number using a movz/movn followed by one or more movk.
Matteo Franchin4163c532014-07-15 15:20:27 +0100491 A64Opcode op;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100492 uint16_t background;
493 LIR* res = nullptr;
494
495 // Decide whether to use a movz or a movn.
Matteo Franchinc763e352014-07-04 12:53:27 +0100496 if (more_ffff_halfwords) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100497 op = WIDE(kA64Movn3rdM);
498 background = 0xffff;
Matteo Franchinc763e352014-07-04 12:53:27 +0100499 } else {
500 op = WIDE(kA64Movz3rdM);
501 background = 0;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100502 }
503
504 // Emit the first instruction (movz, movn).
505 int shift;
506 for (shift = 0; shift < 4; shift++) {
507 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
508 if (halfword != background) {
509 res = NewLIR3(op, r_dest.GetReg(), halfword ^ background, shift);
510 break;
511 }
512 }
513
514 // Emit the movk instructions.
515 for (shift++; shift < 4; shift++) {
516 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
517 if (halfword != background) {
518 NewLIR3(WIDE(kA64Movk3rdM), r_dest.GetReg(), halfword, shift);
519 }
520 }
521 return res;
522 }
523
524 // Use the literal pool.
525 int32_t val_lo = Low32Bits(value);
526 int32_t val_hi = High32Bits(value);
527 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700528 if (data_target == nullptr) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100529 data_target = AddWideData(&literal_list_, val_lo, val_hi);
530 }
531
532 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
533 LIR *res = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp),
534 r_dest.GetReg(), 0, 0, 0, 0, data_target);
535 AppendLIR(res);
536 return res;
537}
538
Matteo Franchin43ec8732014-03-31 15:00:14 +0100539LIR* Arm64Mir2Lir::OpUnconditionalBranch(LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100540 LIR* res = NewLIR1(kA64B1t, 0 /* offset to be patched during assembly */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100541 res->target = target;
542 return res;
543}
544
545LIR* Arm64Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100546 LIR* branch = NewLIR2(kA64B2ct, ArmConditionEncoding(cc),
547 0 /* offset to be patched */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100548 branch->target = target;
549 return branch;
550}
551
552LIR* Arm64Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100553 A64Opcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100554 switch (op) {
555 case kOpBlx:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100556 opcode = kA64Blr1x;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100557 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100558 default:
559 LOG(FATAL) << "Bad opcode " << op;
560 }
561 return NewLIR1(opcode, r_dest_src.GetReg());
562}
563
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100564LIR* Arm64Mir2Lir::OpRegRegShift(OpKind op, RegStorage r_dest_src1, RegStorage r_src2, int shift) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100565 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100566 CHECK_EQ(r_dest_src1.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin4163c532014-07-15 15:20:27 +0100567 A64Opcode opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100568
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100569 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100570 case kOpCmn:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100571 opcode = kA64Cmn3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100572 break;
573 case kOpCmp:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100574 opcode = kA64Cmp3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100575 break;
576 case kOpMov:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100577 opcode = kA64Mov2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100578 break;
579 case kOpMvn:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100580 opcode = kA64Mvn2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100581 break;
582 case kOpNeg:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100583 opcode = kA64Neg3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100584 break;
585 case kOpTst:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100586 opcode = kA64Tst3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100587 break;
588 case kOpRev:
589 DCHECK_EQ(shift, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100590 // Binary, but rm is encoded twice.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100591 return NewLIR2(kA64Rev2rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100592 case kOpRevsh:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100593 // Binary, but rm is encoded twice.
Zheng Xua3fe7422014-07-09 14:03:15 +0800594 NewLIR2(kA64Rev162rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
595 // "sxth r1, r2" is "sbfm r1, r2, #0, #15"
596 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100597 case kOp2Byte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100598 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
599 // "sbfx r1, r2, #imm1, #imm2" is "sbfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
600 // For now we use sbfm directly.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100601 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 7);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100602 case kOp2Short:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100603 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
604 // For now we use sbfm rather than its alias, sbfx.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100605 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100606 case kOp2Char:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100607 // "ubfx r1, r2, #imm1, #imm2" is "ubfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
608 // For now we use ubfm directly.
609 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100610 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100611 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100612 return OpRegRegRegShift(op, r_dest_src1, r_dest_src1, r_src2, shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100613 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100614
Matteo Franchin43ec8732014-03-31 15:00:14 +0100615 DCHECK(!IsPseudoLirOp(opcode));
616 if (EncodingMap[opcode].flags & IS_BINARY_OP) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100617 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100618 return NewLIR2(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100619 } else if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100620 A64EncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100621 if (kind == kFmtShift) {
622 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100623 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100624 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100625
626 LOG(FATAL) << "Unexpected encoding operand count";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700627 return nullptr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100628}
629
Zheng Xucedee472014-07-01 09:53:22 +0800630LIR* Arm64Mir2Lir::OpRegRegExtend(OpKind op, RegStorage r_dest_src1, RegStorage r_src2,
631 A64RegExtEncodings ext, uint8_t amount) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100632 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
633 A64Opcode opcode = kA64Brk1d;
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100634
635 switch (op) {
636 case kOpCmn:
637 opcode = kA64Cmn3Rre;
638 break;
639 case kOpCmp:
640 opcode = kA64Cmp3Rre;
641 break;
Zheng Xucedee472014-07-01 09:53:22 +0800642 case kOpAdd:
643 // Note: intentional fallthrough
644 case kOpSub:
645 return OpRegRegRegExtend(op, r_dest_src1, r_dest_src1, r_src2, ext, amount);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100646 default:
647 LOG(FATAL) << "Bad Opcode: " << opcode;
Andreas Gampe65b798e2015-04-06 09:35:22 -0700648 UNREACHABLE();
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100649 }
650
651 DCHECK(!IsPseudoLirOp(opcode));
652 if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100653 A64EncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100654 if (kind == kFmtExtend) {
Zheng Xucedee472014-07-01 09:53:22 +0800655 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(),
656 EncodeExtend(ext, amount));
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100657 }
658 }
659
660 LOG(FATAL) << "Unexpected encoding operand count";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700661 return nullptr;
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100662}
663
Matteo Franchin43ec8732014-03-31 15:00:14 +0100664LIR* Arm64Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100665 /* RegReg operations with SP in first parameter need extended register instruction form.
Zheng Xucedee472014-07-01 09:53:22 +0800666 * Only CMN, CMP, ADD & SUB instructions are implemented.
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100667 */
Zheng Xubaa7c882014-06-30 14:26:50 +0800668 if (r_dest_src1 == rs_sp) {
Zheng Xucedee472014-07-01 09:53:22 +0800669 return OpRegRegExtend(op, r_dest_src1, r_src2, kA64Uxtx, 0);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100670 } else {
671 return OpRegRegShift(op, r_dest_src1, r_src2, ENCODE_NO_SHIFT);
672 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100673}
674
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100675LIR* Arm64Mir2Lir::OpMovRegMem(RegStorage r_dest ATTRIBUTE_UNUSED,
676 RegStorage r_base ATTRIBUTE_UNUSED,
677 int offset ATTRIBUTE_UNUSED,
678 MoveType move_type ATTRIBUTE_UNUSED) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100679 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700680 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100681}
682
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100683LIR* Arm64Mir2Lir::OpMovMemReg(RegStorage r_base ATTRIBUTE_UNUSED,
684 int offset ATTRIBUTE_UNUSED,
685 RegStorage r_src ATTRIBUTE_UNUSED,
686 MoveType move_type ATTRIBUTE_UNUSED) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100687 UNIMPLEMENTED(FATAL);
688 return nullptr;
689}
690
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100691LIR* Arm64Mir2Lir::OpCondRegReg(OpKind op ATTRIBUTE_UNUSED,
692 ConditionCode cc ATTRIBUTE_UNUSED,
693 RegStorage r_dest ATTRIBUTE_UNUSED,
694 RegStorage r_src ATTRIBUTE_UNUSED) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100695 LOG(FATAL) << "Unexpected use of OpCondRegReg for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700696 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100697}
698
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100699LIR* Arm64Mir2Lir::OpRegRegRegShift(OpKind op, RegStorage r_dest, RegStorage r_src1,
700 RegStorage r_src2, int shift) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100701 A64Opcode opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100702
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100703 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100704 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100705 opcode = kA64Add4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100706 break;
707 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100708 opcode = kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100709 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100710 // case kOpRsub:
711 // opcode = kA64RsubWWW;
712 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100713 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100714 opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100715 break;
716 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100717 opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100718 break;
719 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100720 opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100721 break;
722 case kOpMul:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100723 opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100724 break;
725 case kOpDiv:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100726 opcode = kA64Sdiv3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 break;
728 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100729 opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100730 break;
731 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100732 opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100733 break;
734 case kOpLsl:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100735 opcode = kA64Lsl3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100736 break;
737 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100738 opcode = kA64Lsr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100739 break;
740 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100741 opcode = kA64Asr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100742 break;
743 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100744 opcode = kA64Ror3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100745 break;
746 default:
747 LOG(FATAL) << "Bad opcode: " << op;
748 break;
749 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100750
751 // The instructions above belong to two kinds:
752 // - 4-operands instructions, where the last operand is a shift/extend immediate,
753 // - 3-operands instructions with no shift/extend.
Matteo Franchin4163c532014-07-15 15:20:27 +0100754 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100755 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
756 CHECK_EQ(r_dest.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100757 if (EncodingMap[opcode].flags & IS_QUAD_OP) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100758 DCHECK(!IsExtendEncoding(shift));
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100759 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100760 } else {
761 DCHECK(EncodingMap[opcode].flags & IS_TERTIARY_OP);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100762 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100763 return NewLIR3(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100764 }
765}
766
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700767LIR* Arm64Mir2Lir::OpRegRegRegExtend(OpKind op, RegStorage r_dest, RegStorage r_src1,
768 RegStorage r_src2, A64RegExtEncodings ext, uint8_t amount) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100769 A64Opcode opcode = kA64Brk1d;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700770
771 switch (op) {
772 case kOpAdd:
773 opcode = kA64Add4RRre;
774 break;
775 case kOpSub:
776 opcode = kA64Sub4RRre;
777 break;
778 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700779 UNIMPLEMENTED(FATAL) << "Unimplemented opcode: " << op;
780 UNREACHABLE();
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700781 }
Matteo Franchin4163c532014-07-15 15:20:27 +0100782 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700783
784 if (r_dest.Is64Bit()) {
785 CHECK(r_src1.Is64Bit());
786
787 // dest determines whether the op is wide or not. Up-convert src2 when necessary.
788 // Note: this is not according to aarch64 specifications, but our encoding.
789 if (!r_src2.Is64Bit()) {
790 r_src2 = As64BitReg(r_src2);
791 }
792 } else {
793 CHECK(!r_src1.Is64Bit());
794 CHECK(!r_src2.Is64Bit());
795 }
796
797 // Sanity checks.
798 // 1) Amount is in the range 0..4
799 CHECK_LE(amount, 4);
800
801 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(),
802 EncodeExtend(ext, amount));
803}
804
Matteo Franchin43ec8732014-03-31 15:00:14 +0100805LIR* Arm64Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100806 return OpRegRegRegShift(op, r_dest, r_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100807}
808
809LIR* Arm64Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800810 return OpRegRegImm64(op, r_dest, r_src1, static_cast<int64_t>(value));
811}
812
813LIR* Arm64Mir2Lir::OpRegRegImm64(OpKind op, RegStorage r_dest, RegStorage r_src1, int64_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100814 LIR* res;
815 bool neg = (value < 0);
buzbeeb504d2f2014-09-26 15:09:06 -0700816 uint64_t abs_value = (neg & !(value == LLONG_MIN)) ? -value : value;
Matteo Franchin4163c532014-07-15 15:20:27 +0100817 A64Opcode opcode = kA64Brk1d;
818 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinc763e352014-07-04 12:53:27 +0100819 bool is_logical = false;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100820 bool is_wide = r_dest.Is64Bit();
Matteo Franchin4163c532014-07-15 15:20:27 +0100821 A64Opcode wide = (is_wide) ? WIDE(0) : UNWIDE(0);
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700822 int info = 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100823
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100824 switch (op) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100825 case kOpLsl: {
826 // "lsl w1, w2, #imm" is an alias of "ubfm w1, w2, #(-imm MOD 32), #(31-imm)"
Zheng Xu2d41a652014-06-09 11:05:31 +0800827 // and "lsl x1, x2, #imm" of "ubfm x1, x2, #(-imm MOD 64), #(63-imm)".
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100828 // For now, we just use ubfm directly.
Zheng Xu2d41a652014-06-09 11:05:31 +0800829 int max_value = (is_wide) ? 63 : 31;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100830 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest.GetReg(), r_src1.GetReg(),
Zheng Xu2d41a652014-06-09 11:05:31 +0800831 (-value) & max_value, max_value - value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100832 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100833 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100834 return NewLIR3(kA64Lsr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100835 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100836 return NewLIR3(kA64Asr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100837 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100838 // "ror r1, r2, #imm" is an alias of "extr r1, r2, r2, #imm".
839 // For now, we just use extr directly.
840 return NewLIR4(kA64Extr4rrrd | wide, r_dest.GetReg(), r_src1.GetReg(), r_src1.GetReg(),
841 value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100842 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100843 neg = !neg;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700844 FALLTHROUGH_INTENDED;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100845 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100846 // Add and sub below read/write sp rather than xzr.
847 if (abs_value < 0x1000) {
848 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
849 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value, 0);
850 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
851 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
852 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value >> 12, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100853 } else {
Vladimir Marko903989d2014-07-01 17:21:18 +0100854 alt_opcode = (op == kOpAdd) ? kA64Add4RRre : kA64Sub4RRre;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700855 info = EncodeExtend(is_wide ? kA64Uxtx : kA64Uxtw, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100856 }
857 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100858 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100859 alt_opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100860 break;
861 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100862 alt_opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100863 break;
864 case kOpOr:
Matteo Franchinc763e352014-07-04 12:53:27 +0100865 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100866 opcode = kA64Orr3Rrl;
867 alt_opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100868 break;
869 case kOpAnd:
Matteo Franchinc763e352014-07-04 12:53:27 +0100870 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100871 opcode = kA64And3Rrl;
872 alt_opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100873 break;
874 case kOpXor:
Matteo Franchinc763e352014-07-04 12:53:27 +0100875 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100876 opcode = kA64Eor3Rrl;
877 alt_opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100878 break;
879 case kOpMul:
880 // TUNING: power of 2, shift & add
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100881 alt_opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100882 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100883 default:
884 LOG(FATAL) << "Bad opcode: " << op;
885 }
886
Matteo Franchinc763e352014-07-04 12:53:27 +0100887 if (is_logical) {
888 int log_imm = EncodeLogicalImmediate(is_wide, value);
889 if (log_imm >= 0) {
890 return NewLIR3(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), log_imm);
Zheng Xue2eb29e2014-06-12 10:22:33 +0800891 } else {
Matteo Franchinc763e352014-07-04 12:53:27 +0100892 // When the immediate is either 0 or ~0, the logical operation can be trivially reduced
893 // to a - possibly negated - assignment.
894 if (value == 0) {
895 switch (op) {
896 case kOpOr:
897 case kOpXor:
898 // Or/Xor by zero reduces to an assignment.
899 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
900 default:
901 // And by zero reduces to a `mov rdest, xzr'.
902 DCHECK(op == kOpAnd);
903 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
904 }
905 } else if (value == INT64_C(-1)
906 || (!is_wide && static_cast<uint32_t>(value) == ~UINT32_C(0))) {
907 switch (op) {
908 case kOpAnd:
909 // And by -1 reduces to an assignment.
910 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
911 case kOpXor:
912 // Xor by -1 reduces to an `mvn rdest, rsrc'.
913 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), r_src1.GetReg());
914 default:
915 // Or by -1 reduces to a `mvn rdest, xzr'.
916 DCHECK(op == kOpOr);
917 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
918 }
919 }
Zheng Xue2eb29e2014-06-12 10:22:33 +0800920 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100921 }
Matteo Franchinc763e352014-07-04 12:53:27 +0100922
923 RegStorage r_scratch;
924 if (is_wide) {
925 r_scratch = AllocTempWide();
926 LoadConstantWide(r_scratch, value);
927 } else {
928 r_scratch = AllocTemp();
929 LoadConstant(r_scratch, value);
930 }
931 if (EncodingMap[alt_opcode].flags & IS_QUAD_OP)
932 res = NewLIR4(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg(), info);
933 else
934 res = NewLIR3(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
935 FreeTemp(r_scratch);
936 return res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100937}
938
Matteo Franchin43ec8732014-03-31 15:00:14 +0100939LIR* Arm64Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100940 return OpRegImm64(op, r_dest_src1, static_cast<int64_t>(value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100941}
942
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100943LIR* Arm64Mir2Lir::OpRegImm64(OpKind op, RegStorage r_dest_src1, int64_t value) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100944 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
945 A64Opcode opcode = kA64Brk1d;
946 A64Opcode neg_opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100947 bool shift;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100948 bool neg = (value < 0);
buzbeeb504d2f2014-09-26 15:09:06 -0700949 uint64_t abs_value = (neg & !(value == LLONG_MIN)) ? -value : value;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100950
951 if (LIKELY(abs_value < 0x1000)) {
952 // abs_value is a 12-bit immediate.
953 shift = false;
954 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
955 // abs_value is a shifted 12-bit immediate.
956 shift = true;
957 abs_value >>= 12;
Zheng Xue2eb29e2014-06-12 10:22:33 +0800958 } else if (LIKELY(abs_value < 0x1000000 && (op == kOpAdd || op == kOpSub))) {
959 // Note: It is better to use two ADD/SUB instead of loading a number to a temp register.
960 // This works for both normal registers and SP.
961 // For a frame size == 0x2468, it will be encoded as:
962 // sub sp, #0x2000
963 // sub sp, #0x468
964 if (neg) {
965 op = (op == kOpAdd) ? kOpSub : kOpAdd;
966 }
967 OpRegImm64(op, r_dest_src1, abs_value & (~INT64_C(0xfff)));
968 return OpRegImm64(op, r_dest_src1, abs_value & 0xfff);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100969 } else {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800970 RegStorage r_tmp;
971 LIR* res;
972 if (IS_WIDE(wide)) {
973 r_tmp = AllocTempWide();
974 res = LoadConstantWide(r_tmp, value);
975 } else {
976 r_tmp = AllocTemp();
977 res = LoadConstant(r_tmp, value);
978 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100979 OpRegReg(op, r_dest_src1, r_tmp);
980 FreeTemp(r_tmp);
981 return res;
982 }
983
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100984 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100985 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100986 neg_opcode = kA64Sub4RRdT;
987 opcode = kA64Add4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100988 break;
989 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100990 neg_opcode = kA64Add4RRdT;
991 opcode = kA64Sub4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100992 break;
993 case kOpCmp:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100994 neg_opcode = kA64Cmn3RdT;
995 opcode = kA64Cmp3RdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100996 break;
997 default:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100998 LOG(FATAL) << "Bad op-kind in OpRegImm: " << op;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100999 break;
1000 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001001
1002 if (UNLIKELY(neg))
1003 opcode = neg_opcode;
1004
1005 if (EncodingMap[opcode].flags & IS_QUAD_OP)
1006 return NewLIR4(opcode | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), abs_value,
1007 (shift) ? 1 : 0);
1008 else
1009 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), abs_value, (shift) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001010}
1011
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001012int Arm64Mir2Lir::EncodeShift(int shift_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001013 DCHECK_EQ(shift_type & 0x3, shift_type);
1014 DCHECK_EQ(amount & 0x3f, amount);
Matteo Franchinc61b3c92014-06-18 11:52:47 +01001015 return ((shift_type & 0x3) << 7) | (amount & 0x3f);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001016}
1017
1018int Arm64Mir2Lir::EncodeExtend(int extend_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001019 DCHECK_EQ(extend_type & 0x7, extend_type);
1020 DCHECK_EQ(amount & 0x7, amount);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001021 return (1 << 6) | ((extend_type & 0x7) << 3) | (amount & 0x7);
1022}
1023
1024bool Arm64Mir2Lir::IsExtendEncoding(int encoded_value) {
1025 return ((1 << 6) & encoded_value) != 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001026}
1027
1028LIR* Arm64Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001029 int scale, OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001030 LIR* load;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001031 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001032 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001033 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001034
1035 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1036 // register offset load (rather than doing the sign extension in a separate instruction).
1037 if (r_index.Is32Bit()) {
1038 // Assemble: ``sxtw xN, wN''.
1039 r_index = As64BitReg(r_index);
1040 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1041 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001042
1043 if (r_dest.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001044 if (r_dest.IsDouble()) {
1045 DCHECK(size == k64 || size == kDouble);
1046 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001047 opcode = WIDE(kA64Ldr4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001048 } else {
1049 DCHECK(r_dest.IsSingle());
1050 DCHECK(size == k32 || size == kSingle);
1051 expected_scale = 2;
1052 opcode = kA64Ldr4fXxG;
1053 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001054
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001055 DCHECK(scale == 0 || scale == expected_scale);
1056 return NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
1057 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001058 }
1059
1060 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001061 case kDouble:
1062 case kWord:
1063 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001064 r_dest = Check64BitReg(r_dest);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001065 opcode = WIDE(kA64Ldr4rXxG);
1066 expected_scale = 3;
1067 break;
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001068 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001069 r_dest = As32BitReg(r_dest);
1070 FALLTHROUGH_INTENDED;
1071 case kSingle: // Intentional fall-through.
1072 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001073 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001074 opcode = kA64Ldr4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001075 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001076 break;
1077 case kUnsignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001078 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001079 opcode = kA64Ldrh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001080 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001081 break;
1082 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001083 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001084 opcode = kA64Ldrsh4rXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001085 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001086 break;
1087 case kUnsignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001088 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001089 opcode = kA64Ldrb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001090 break;
1091 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001092 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001093 opcode = kA64Ldrsb3rXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001094 break;
1095 default:
1096 LOG(FATAL) << "Bad size: " << size;
1097 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001098
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001099 if (UNLIKELY(expected_scale == 0)) {
1100 // This is a tertiary op (e.g. ldrb, ldrsb), it does not not support scale.
1101 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001102 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001103 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001104 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001105 DCHECK(scale == 0 || scale == expected_scale);
1106 load = NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001107 (scale != 0) ? 1 : 0);
1108 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001109
1110 return load;
1111}
1112
1113LIR* Arm64Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001114 int scale, OpSize size) {
1115 LIR* store;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001116 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001117 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001118 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001119
1120 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1121 // register offset store (rather than doing the sign extension in a separate instruction).
1122 if (r_index.Is32Bit()) {
1123 // Assemble: ``sxtw xN, wN''.
1124 r_index = As64BitReg(r_index);
1125 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1126 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001127
1128 if (r_src.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001129 if (r_src.IsDouble()) {
1130 DCHECK(size == k64 || size == kDouble);
1131 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001132 opcode = WIDE(kA64Str4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001133 } else {
1134 DCHECK(r_src.IsSingle());
1135 DCHECK(size == k32 || size == kSingle);
1136 expected_scale = 2;
1137 opcode = kA64Str4fXxG;
1138 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001139
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001140 DCHECK(scale == 0 || scale == expected_scale);
1141 return NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1142 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001143 }
1144
1145 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001146 case kDouble: // Intentional fall-trough.
1147 case kWord: // Intentional fall-trough.
1148 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001149 r_src = Check64BitReg(r_src);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001150 opcode = WIDE(kA64Str4rXxG);
1151 expected_scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001152 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001153 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001154 r_src = As32BitReg(r_src);
1155 FALLTHROUGH_INTENDED;
1156 case kSingle: // Intentional fall-trough.
1157 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001158 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001159 opcode = kA64Str4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001160 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001161 break;
1162 case kUnsignedHalf:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001163 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001164 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001165 opcode = kA64Strh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001166 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001167 break;
1168 case kUnsignedByte:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001169 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001170 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001171 opcode = kA64Strb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001172 break;
1173 default:
1174 LOG(FATAL) << "Bad size: " << size;
1175 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001176
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001177 if (UNLIKELY(expected_scale == 0)) {
1178 // This is a tertiary op (e.g. strb), it does not not support scale.
1179 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001180 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001181 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001182 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001183 store = NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1184 (scale != 0) ? 1 : 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001185 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001186
1187 return store;
1188}
1189
1190/*
1191 * Load value from base + displacement. Optionally perform null check
1192 * on base (which must have an associated s_reg and MIR). If not
1193 * performing null check, incoming MIR can be null.
1194 */
1195LIR* Arm64Mir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001196 OpSize size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001197 LIR* load = nullptr;
Matteo Franchin4163c532014-07-15 15:20:27 +01001198 A64Opcode opcode = kA64Brk1d;
1199 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001200 int scale = 0;
1201
Matteo Franchin43ec8732014-03-31 15:00:14 +01001202 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001203 case kDouble: // Intentional fall-through.
1204 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001205 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001206 r_dest = Check64BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001207 scale = 3;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001208 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001209 DCHECK(r_dest.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001210 opcode = WIDE(kA64Ldr3fXD);
1211 alt_opcode = WIDE(kA64Ldur3fXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001212 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001213 opcode = WIDE(kA64Ldr3rXD);
1214 alt_opcode = WIDE(kA64Ldur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001215 }
1216 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001217 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001218 r_dest = As32BitReg(r_dest);
1219 FALLTHROUGH_INTENDED;
1220 case kSingle: // Intentional fall-through.
1221 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001222 r_dest = Check32BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001223 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001224 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001225 DCHECK(r_dest.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001226 opcode = kA64Ldr3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001227 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001228 opcode = kA64Ldr3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001229 }
1230 break;
1231 case kUnsignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001232 scale = 1;
1233 opcode = kA64Ldrh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001234 break;
1235 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001236 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001237 opcode = kA64Ldrsh3rXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001238 break;
1239 case kUnsignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001240 opcode = kA64Ldrb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001241 break;
1242 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001243 opcode = kA64Ldrsb3rXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001244 break;
1245 default:
1246 LOG(FATAL) << "Bad size: " << size;
1247 }
1248
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001249 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1250 int scaled_disp = displacement >> scale;
1251 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1252 // Can use scaled load.
1253 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), scaled_disp);
1254 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1255 // Can use unscaled load.
1256 load = NewLIR3(alt_opcode, r_dest.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001257 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001258 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001259 // TODO: cleaner support for index/displacement registers? Not a reference, but must match width.
1260 RegStorage r_scratch = AllocTempWide();
1261 LoadConstantWide(r_scratch, displacement);
Andreas Gampe582f5412015-01-20 18:06:47 -08001262 load = LoadBaseIndexed(r_base, r_scratch,
1263 (size == kReference) ? As64BitReg(r_dest) : r_dest,
1264 0, size);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001265 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001266 }
1267
1268 // TODO: in future may need to differentiate Dalvik accesses w/ spills
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001269 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001270 DCHECK_EQ(r_base, rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001271 AnnotateDalvikRegAccess(load, displacement >> 2, true /* is_load */, r_dest.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001272 }
1273 return load;
1274}
1275
Andreas Gampe3c12c512014-06-24 18:46:29 +00001276LIR* Arm64Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1277 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +01001278 // LoadBaseDisp() will emit correct insn for atomic load on arm64
1279 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001280
1281 LIR* load = LoadBaseDispBody(r_base, displacement, r_dest, size);
1282
1283 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001284 // TODO: This should generate an acquire load instead of the barrier.
1285 GenMemBarrier(kLoadAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001286 }
1287
1288 return load;
Vladimir Marko674744e2014-04-24 15:18:26 +01001289}
1290
Matteo Franchin43ec8732014-03-31 15:00:14 +01001291LIR* Arm64Mir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001292 OpSize size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001293 LIR* store = nullptr;
Matteo Franchin4163c532014-07-15 15:20:27 +01001294 A64Opcode opcode = kA64Brk1d;
1295 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001296 int scale = 0;
1297
Matteo Franchin43ec8732014-03-31 15:00:14 +01001298 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001299 case kDouble: // Intentional fall-through.
1300 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001301 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001302 r_src = Check64BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001303 scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001304 if (r_src.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001305 DCHECK(r_src.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001306 opcode = WIDE(kA64Str3fXD);
1307 alt_opcode = WIDE(kA64Stur3fXd);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001308 } else {
Matteo Franchin4163c532014-07-15 15:20:27 +01001309 opcode = WIDE(kA64Str3rXD);
1310 alt_opcode = WIDE(kA64Stur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001311 }
1312 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001313 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001314 r_src = As32BitReg(r_src);
1315 FALLTHROUGH_INTENDED;
1316 case kSingle: // Intentional fall-through.
1317 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001318 r_src = Check32BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001319 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001320 if (r_src.IsFloat()) {
1321 DCHECK(r_src.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001322 opcode = kA64Str3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001323 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001324 opcode = kA64Str3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001325 }
1326 break;
1327 case kUnsignedHalf:
1328 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001329 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001330 opcode = kA64Strh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001331 break;
1332 case kUnsignedByte:
1333 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001334 opcode = kA64Strb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001335 break;
1336 default:
1337 LOG(FATAL) << "Bad size: " << size;
1338 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001339
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001340 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1341 int scaled_disp = displacement >> scale;
1342 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1343 // Can use scaled store.
1344 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), scaled_disp);
1345 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1346 // Can use unscaled store.
1347 store = NewLIR3(alt_opcode, r_src.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001348 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001349 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001350 RegStorage r_scratch = AllocTempWide();
1351 LoadConstantWide(r_scratch, displacement);
Andreas Gampe582f5412015-01-20 18:06:47 -08001352 store = StoreBaseIndexed(r_base, r_scratch,
1353 (size == kReference) ? As64BitReg(r_src) : r_src,
1354 0, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001355 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001356 }
1357
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001358 // TODO: In future, may need to differentiate Dalvik & spill accesses.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001359 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001360 DCHECK_EQ(r_base, rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001361 AnnotateDalvikRegAccess(store, displacement >> 2, false /* is_load */, r_src.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001362 }
1363 return store;
1364}
1365
Andreas Gampe3c12c512014-06-24 18:46:29 +00001366LIR* Arm64Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
1367 OpSize size, VolatileKind is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001368 // TODO: This should generate a release store and no barriers.
Andreas Gampe3c12c512014-06-24 18:46:29 +00001369 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001370 // Ensure that prior accesses become visible to other threads first.
1371 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001372 }
1373
Vladimir Marko674744e2014-04-24 15:18:26 +01001374 // StoreBaseDisp() will emit correct insn for atomic store on arm64
1375 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001376
1377 LIR* store = StoreBaseDispBody(r_base, displacement, r_src, size);
1378
1379 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001380 // Preserve order with respect to any subsequent volatile loads.
1381 // We need StoreLoad, but that generally requires the most expensive barrier.
1382 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001383 }
1384
1385 return store;
Vladimir Marko674744e2014-04-24 15:18:26 +01001386}
1387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001388LIR* Arm64Mir2Lir::OpFpRegCopy(RegStorage r_dest ATTRIBUTE_UNUSED,
1389 RegStorage r_src ATTRIBUTE_UNUSED) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001390 LOG(FATAL) << "Unexpected use of OpFpRegCopy for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001391 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001392}
1393
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001394LIR* Arm64Mir2Lir::OpMem(OpKind op ATTRIBUTE_UNUSED,
1395 RegStorage r_base ATTRIBUTE_UNUSED,
1396 int disp ATTRIBUTE_UNUSED) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001397 LOG(FATAL) << "Unexpected use of OpMem for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001398 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001399}
1400
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001401LIR* Arm64Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt,
1402 QuickEntrypointEnum trampoline ATTRIBUTE_UNUSED) {
1403 // The address of the trampoline is already loaded into r_tgt.
Andreas Gampe98430592014-07-27 19:44:50 -07001404 return OpReg(op, r_tgt);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001405}
1406
1407} // namespace art