blob: 483231f931ad087906f349b2299a6bdd93048da9 [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
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700675LIR* Arm64Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset,
676 MoveType move_type) {
677 UNUSED(r_dest, r_base, offset, move_type);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100678 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700679 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100680}
681
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700682LIR* Arm64Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src,
683 MoveType move_type) {
684 UNUSED(r_base, offset, r_src, move_type);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100685 UNIMPLEMENTED(FATAL);
686 return nullptr;
687}
688
689LIR* Arm64Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700690 UNUSED(op, cc, r_dest, r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100691 LOG(FATAL) << "Unexpected use of OpCondRegReg for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700692 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100693}
694
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100695LIR* Arm64Mir2Lir::OpRegRegRegShift(OpKind op, RegStorage r_dest, RegStorage r_src1,
696 RegStorage r_src2, int shift) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100697 A64Opcode opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100698
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100699 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100700 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100701 opcode = kA64Add4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100702 break;
703 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100704 opcode = kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100705 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100706 // case kOpRsub:
707 // opcode = kA64RsubWWW;
708 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100709 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100710 opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100711 break;
712 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100713 opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100714 break;
715 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100716 opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100717 break;
718 case kOpMul:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100719 opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100720 break;
721 case kOpDiv:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100722 opcode = kA64Sdiv3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100723 break;
724 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100725 opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100726 break;
727 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100728 opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100729 break;
730 case kOpLsl:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100731 opcode = kA64Lsl3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100732 break;
733 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100734 opcode = kA64Lsr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100735 break;
736 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100737 opcode = kA64Asr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100738 break;
739 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100740 opcode = kA64Ror3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100741 break;
742 default:
743 LOG(FATAL) << "Bad opcode: " << op;
744 break;
745 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100746
747 // The instructions above belong to two kinds:
748 // - 4-operands instructions, where the last operand is a shift/extend immediate,
749 // - 3-operands instructions with no shift/extend.
Matteo Franchin4163c532014-07-15 15:20:27 +0100750 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100751 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
752 CHECK_EQ(r_dest.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100753 if (EncodingMap[opcode].flags & IS_QUAD_OP) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100754 DCHECK(!IsExtendEncoding(shift));
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100755 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100756 } else {
757 DCHECK(EncodingMap[opcode].flags & IS_TERTIARY_OP);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100758 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100759 return NewLIR3(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100760 }
761}
762
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700763LIR* Arm64Mir2Lir::OpRegRegRegExtend(OpKind op, RegStorage r_dest, RegStorage r_src1,
764 RegStorage r_src2, A64RegExtEncodings ext, uint8_t amount) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100765 A64Opcode opcode = kA64Brk1d;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700766
767 switch (op) {
768 case kOpAdd:
769 opcode = kA64Add4RRre;
770 break;
771 case kOpSub:
772 opcode = kA64Sub4RRre;
773 break;
774 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700775 UNIMPLEMENTED(FATAL) << "Unimplemented opcode: " << op;
776 UNREACHABLE();
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700777 }
Matteo Franchin4163c532014-07-15 15:20:27 +0100778 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700779
780 if (r_dest.Is64Bit()) {
781 CHECK(r_src1.Is64Bit());
782
783 // dest determines whether the op is wide or not. Up-convert src2 when necessary.
784 // Note: this is not according to aarch64 specifications, but our encoding.
785 if (!r_src2.Is64Bit()) {
786 r_src2 = As64BitReg(r_src2);
787 }
788 } else {
789 CHECK(!r_src1.Is64Bit());
790 CHECK(!r_src2.Is64Bit());
791 }
792
793 // Sanity checks.
794 // 1) Amount is in the range 0..4
795 CHECK_LE(amount, 4);
796
797 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(),
798 EncodeExtend(ext, amount));
799}
800
Matteo Franchin43ec8732014-03-31 15:00:14 +0100801LIR* Arm64Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100802 return OpRegRegRegShift(op, r_dest, r_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100803}
804
805LIR* Arm64Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800806 return OpRegRegImm64(op, r_dest, r_src1, static_cast<int64_t>(value));
807}
808
809LIR* Arm64Mir2Lir::OpRegRegImm64(OpKind op, RegStorage r_dest, RegStorage r_src1, int64_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100810 LIR* res;
811 bool neg = (value < 0);
buzbeeb504d2f2014-09-26 15:09:06 -0700812 uint64_t abs_value = (neg & !(value == LLONG_MIN)) ? -value : value;
Matteo Franchin4163c532014-07-15 15:20:27 +0100813 A64Opcode opcode = kA64Brk1d;
814 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinc763e352014-07-04 12:53:27 +0100815 bool is_logical = false;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100816 bool is_wide = r_dest.Is64Bit();
Matteo Franchin4163c532014-07-15 15:20:27 +0100817 A64Opcode wide = (is_wide) ? WIDE(0) : UNWIDE(0);
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700818 int info = 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100819
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100820 switch (op) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100821 case kOpLsl: {
822 // "lsl w1, w2, #imm" is an alias of "ubfm w1, w2, #(-imm MOD 32), #(31-imm)"
Zheng Xu2d41a652014-06-09 11:05:31 +0800823 // and "lsl x1, x2, #imm" of "ubfm x1, x2, #(-imm MOD 64), #(63-imm)".
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100824 // For now, we just use ubfm directly.
Zheng Xu2d41a652014-06-09 11:05:31 +0800825 int max_value = (is_wide) ? 63 : 31;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100826 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest.GetReg(), r_src1.GetReg(),
Zheng Xu2d41a652014-06-09 11:05:31 +0800827 (-value) & max_value, max_value - value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100828 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100829 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100830 return NewLIR3(kA64Lsr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100831 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100832 return NewLIR3(kA64Asr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100833 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100834 // "ror r1, r2, #imm" is an alias of "extr r1, r2, r2, #imm".
835 // For now, we just use extr directly.
836 return NewLIR4(kA64Extr4rrrd | wide, r_dest.GetReg(), r_src1.GetReg(), r_src1.GetReg(),
837 value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100838 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100839 neg = !neg;
Ian Rogersfc787ec2014-10-09 21:56:44 -0700840 FALLTHROUGH_INTENDED;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100841 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100842 // Add and sub below read/write sp rather than xzr.
843 if (abs_value < 0x1000) {
844 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
845 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value, 0);
846 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
847 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
848 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value >> 12, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100849 } else {
Vladimir Marko903989d2014-07-01 17:21:18 +0100850 alt_opcode = (op == kOpAdd) ? kA64Add4RRre : kA64Sub4RRre;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700851 info = EncodeExtend(is_wide ? kA64Uxtx : kA64Uxtw, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100852 }
853 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100854 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100855 alt_opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100856 break;
857 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100858 alt_opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100859 break;
860 case kOpOr:
Matteo Franchinc763e352014-07-04 12:53:27 +0100861 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100862 opcode = kA64Orr3Rrl;
863 alt_opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100864 break;
865 case kOpAnd:
Matteo Franchinc763e352014-07-04 12:53:27 +0100866 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100867 opcode = kA64And3Rrl;
868 alt_opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100869 break;
870 case kOpXor:
Matteo Franchinc763e352014-07-04 12:53:27 +0100871 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100872 opcode = kA64Eor3Rrl;
873 alt_opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100874 break;
875 case kOpMul:
876 // TUNING: power of 2, shift & add
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100877 alt_opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100878 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100879 default:
880 LOG(FATAL) << "Bad opcode: " << op;
881 }
882
Matteo Franchinc763e352014-07-04 12:53:27 +0100883 if (is_logical) {
884 int log_imm = EncodeLogicalImmediate(is_wide, value);
885 if (log_imm >= 0) {
886 return NewLIR3(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), log_imm);
Zheng Xue2eb29e2014-06-12 10:22:33 +0800887 } else {
Matteo Franchinc763e352014-07-04 12:53:27 +0100888 // When the immediate is either 0 or ~0, the logical operation can be trivially reduced
889 // to a - possibly negated - assignment.
890 if (value == 0) {
891 switch (op) {
892 case kOpOr:
893 case kOpXor:
894 // Or/Xor by zero reduces to an assignment.
895 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
896 default:
897 // And by zero reduces to a `mov rdest, xzr'.
898 DCHECK(op == kOpAnd);
899 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
900 }
901 } else if (value == INT64_C(-1)
902 || (!is_wide && static_cast<uint32_t>(value) == ~UINT32_C(0))) {
903 switch (op) {
904 case kOpAnd:
905 // And by -1 reduces to an assignment.
906 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
907 case kOpXor:
908 // Xor by -1 reduces to an `mvn rdest, rsrc'.
909 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), r_src1.GetReg());
910 default:
911 // Or by -1 reduces to a `mvn rdest, xzr'.
912 DCHECK(op == kOpOr);
913 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
914 }
915 }
Zheng Xue2eb29e2014-06-12 10:22:33 +0800916 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100917 }
Matteo Franchinc763e352014-07-04 12:53:27 +0100918
919 RegStorage r_scratch;
920 if (is_wide) {
921 r_scratch = AllocTempWide();
922 LoadConstantWide(r_scratch, value);
923 } else {
924 r_scratch = AllocTemp();
925 LoadConstant(r_scratch, value);
926 }
927 if (EncodingMap[alt_opcode].flags & IS_QUAD_OP)
928 res = NewLIR4(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg(), info);
929 else
930 res = NewLIR3(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
931 FreeTemp(r_scratch);
932 return res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100933}
934
Matteo Franchin43ec8732014-03-31 15:00:14 +0100935LIR* Arm64Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100936 return OpRegImm64(op, r_dest_src1, static_cast<int64_t>(value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100937}
938
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100939LIR* Arm64Mir2Lir::OpRegImm64(OpKind op, RegStorage r_dest_src1, int64_t value) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100940 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
941 A64Opcode opcode = kA64Brk1d;
942 A64Opcode neg_opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100943 bool shift;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100944 bool neg = (value < 0);
buzbeeb504d2f2014-09-26 15:09:06 -0700945 uint64_t abs_value = (neg & !(value == LLONG_MIN)) ? -value : value;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100946
947 if (LIKELY(abs_value < 0x1000)) {
948 // abs_value is a 12-bit immediate.
949 shift = false;
950 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
951 // abs_value is a shifted 12-bit immediate.
952 shift = true;
953 abs_value >>= 12;
Zheng Xue2eb29e2014-06-12 10:22:33 +0800954 } else if (LIKELY(abs_value < 0x1000000 && (op == kOpAdd || op == kOpSub))) {
955 // Note: It is better to use two ADD/SUB instead of loading a number to a temp register.
956 // This works for both normal registers and SP.
957 // For a frame size == 0x2468, it will be encoded as:
958 // sub sp, #0x2000
959 // sub sp, #0x468
960 if (neg) {
961 op = (op == kOpAdd) ? kOpSub : kOpAdd;
962 }
963 OpRegImm64(op, r_dest_src1, abs_value & (~INT64_C(0xfff)));
964 return OpRegImm64(op, r_dest_src1, abs_value & 0xfff);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100965 } else {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800966 RegStorage r_tmp;
967 LIR* res;
968 if (IS_WIDE(wide)) {
969 r_tmp = AllocTempWide();
970 res = LoadConstantWide(r_tmp, value);
971 } else {
972 r_tmp = AllocTemp();
973 res = LoadConstant(r_tmp, value);
974 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100975 OpRegReg(op, r_dest_src1, r_tmp);
976 FreeTemp(r_tmp);
977 return res;
978 }
979
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100980 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100981 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100982 neg_opcode = kA64Sub4RRdT;
983 opcode = kA64Add4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100984 break;
985 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100986 neg_opcode = kA64Add4RRdT;
987 opcode = kA64Sub4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100988 break;
989 case kOpCmp:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100990 neg_opcode = kA64Cmn3RdT;
991 opcode = kA64Cmp3RdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100992 break;
993 default:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100994 LOG(FATAL) << "Bad op-kind in OpRegImm: " << op;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100995 break;
996 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100997
998 if (UNLIKELY(neg))
999 opcode = neg_opcode;
1000
1001 if (EncodingMap[opcode].flags & IS_QUAD_OP)
1002 return NewLIR4(opcode | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), abs_value,
1003 (shift) ? 1 : 0);
1004 else
1005 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), abs_value, (shift) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001006}
1007
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001008int Arm64Mir2Lir::EncodeShift(int shift_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001009 DCHECK_EQ(shift_type & 0x3, shift_type);
1010 DCHECK_EQ(amount & 0x3f, amount);
Matteo Franchinc61b3c92014-06-18 11:52:47 +01001011 return ((shift_type & 0x3) << 7) | (amount & 0x3f);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001012}
1013
1014int Arm64Mir2Lir::EncodeExtend(int extend_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001015 DCHECK_EQ(extend_type & 0x7, extend_type);
1016 DCHECK_EQ(amount & 0x7, amount);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001017 return (1 << 6) | ((extend_type & 0x7) << 3) | (amount & 0x7);
1018}
1019
1020bool Arm64Mir2Lir::IsExtendEncoding(int encoded_value) {
1021 return ((1 << 6) & encoded_value) != 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001022}
1023
1024LIR* Arm64Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001025 int scale, OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001026 LIR* load;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001027 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001028 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001030
1031 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1032 // register offset load (rather than doing the sign extension in a separate instruction).
1033 if (r_index.Is32Bit()) {
1034 // Assemble: ``sxtw xN, wN''.
1035 r_index = As64BitReg(r_index);
1036 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1037 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001038
1039 if (r_dest.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001040 if (r_dest.IsDouble()) {
1041 DCHECK(size == k64 || size == kDouble);
1042 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001043 opcode = WIDE(kA64Ldr4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001044 } else {
1045 DCHECK(r_dest.IsSingle());
1046 DCHECK(size == k32 || size == kSingle);
1047 expected_scale = 2;
1048 opcode = kA64Ldr4fXxG;
1049 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001050
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001051 DCHECK(scale == 0 || scale == expected_scale);
1052 return NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
1053 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001054 }
1055
1056 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001057 case kDouble:
1058 case kWord:
1059 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001060 r_dest = Check64BitReg(r_dest);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001061 opcode = WIDE(kA64Ldr4rXxG);
1062 expected_scale = 3;
1063 break;
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001064 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001065 r_dest = As32BitReg(r_dest);
1066 FALLTHROUGH_INTENDED;
1067 case kSingle: // Intentional fall-through.
1068 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001069 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001070 opcode = kA64Ldr4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001071 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001072 break;
1073 case kUnsignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001074 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001075 opcode = kA64Ldrh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001076 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001077 break;
1078 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001079 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001080 opcode = kA64Ldrsh4rXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001081 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001082 break;
1083 case kUnsignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001084 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001085 opcode = kA64Ldrb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001086 break;
1087 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001088 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001089 opcode = kA64Ldrsb3rXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001090 break;
1091 default:
1092 LOG(FATAL) << "Bad size: " << size;
1093 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001094
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001095 if (UNLIKELY(expected_scale == 0)) {
1096 // This is a tertiary op (e.g. ldrb, ldrsb), it does not not support scale.
1097 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001098 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001099 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001100 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001101 DCHECK(scale == 0 || scale == expected_scale);
1102 load = NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001103 (scale != 0) ? 1 : 0);
1104 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001105
1106 return load;
1107}
1108
1109LIR* Arm64Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001110 int scale, OpSize size) {
1111 LIR* store;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001112 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001113 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001114 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001115
1116 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1117 // register offset store (rather than doing the sign extension in a separate instruction).
1118 if (r_index.Is32Bit()) {
1119 // Assemble: ``sxtw xN, wN''.
1120 r_index = As64BitReg(r_index);
1121 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1122 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001123
1124 if (r_src.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001125 if (r_src.IsDouble()) {
1126 DCHECK(size == k64 || size == kDouble);
1127 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001128 opcode = WIDE(kA64Str4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001129 } else {
1130 DCHECK(r_src.IsSingle());
1131 DCHECK(size == k32 || size == kSingle);
1132 expected_scale = 2;
1133 opcode = kA64Str4fXxG;
1134 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001135
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001136 DCHECK(scale == 0 || scale == expected_scale);
1137 return NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1138 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001139 }
1140
1141 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001142 case kDouble: // Intentional fall-trough.
1143 case kWord: // Intentional fall-trough.
1144 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001145 r_src = Check64BitReg(r_src);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001146 opcode = WIDE(kA64Str4rXxG);
1147 expected_scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001148 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001149 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001150 r_src = As32BitReg(r_src);
1151 FALLTHROUGH_INTENDED;
1152 case kSingle: // Intentional fall-trough.
1153 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001154 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001155 opcode = kA64Str4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001156 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001157 break;
1158 case kUnsignedHalf:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001159 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001160 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001161 opcode = kA64Strh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001162 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001163 break;
1164 case kUnsignedByte:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001165 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001166 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001167 opcode = kA64Strb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001168 break;
1169 default:
1170 LOG(FATAL) << "Bad size: " << size;
1171 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001172
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001173 if (UNLIKELY(expected_scale == 0)) {
1174 // This is a tertiary op (e.g. strb), it does not not support scale.
1175 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001176 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001177 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001178 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001179 store = NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1180 (scale != 0) ? 1 : 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001181 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001182
1183 return store;
1184}
1185
1186/*
1187 * Load value from base + displacement. Optionally perform null check
1188 * on base (which must have an associated s_reg and MIR). If not
1189 * performing null check, incoming MIR can be null.
1190 */
1191LIR* Arm64Mir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001192 OpSize size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001193 LIR* load = nullptr;
Matteo Franchin4163c532014-07-15 15:20:27 +01001194 A64Opcode opcode = kA64Brk1d;
1195 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001196 int scale = 0;
1197
Matteo Franchin43ec8732014-03-31 15:00:14 +01001198 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001199 case kDouble: // Intentional fall-through.
1200 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001201 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001202 r_dest = Check64BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001203 scale = 3;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001204 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001205 DCHECK(r_dest.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001206 opcode = WIDE(kA64Ldr3fXD);
1207 alt_opcode = WIDE(kA64Ldur3fXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001208 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001209 opcode = WIDE(kA64Ldr3rXD);
1210 alt_opcode = WIDE(kA64Ldur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001211 }
1212 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001213 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001214 r_dest = As32BitReg(r_dest);
1215 FALLTHROUGH_INTENDED;
1216 case kSingle: // Intentional fall-through.
1217 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001218 r_dest = Check32BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001219 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001220 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001221 DCHECK(r_dest.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001222 opcode = kA64Ldr3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001223 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001224 opcode = kA64Ldr3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001225 }
1226 break;
1227 case kUnsignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001228 scale = 1;
1229 opcode = kA64Ldrh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001230 break;
1231 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001232 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001233 opcode = kA64Ldrsh3rXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001234 break;
1235 case kUnsignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001236 opcode = kA64Ldrb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001237 break;
1238 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001239 opcode = kA64Ldrsb3rXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001240 break;
1241 default:
1242 LOG(FATAL) << "Bad size: " << size;
1243 }
1244
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001245 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1246 int scaled_disp = displacement >> scale;
1247 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1248 // Can use scaled load.
1249 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), scaled_disp);
1250 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1251 // Can use unscaled load.
1252 load = NewLIR3(alt_opcode, r_dest.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001253 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001254 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001255 // TODO: cleaner support for index/displacement registers? Not a reference, but must match width.
1256 RegStorage r_scratch = AllocTempWide();
1257 LoadConstantWide(r_scratch, displacement);
Andreas Gampe582f5412015-01-20 18:06:47 -08001258 load = LoadBaseIndexed(r_base, r_scratch,
1259 (size == kReference) ? As64BitReg(r_dest) : r_dest,
1260 0, size);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001261 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001262 }
1263
1264 // TODO: in future may need to differentiate Dalvik accesses w/ spills
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001265 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001266 DCHECK_EQ(r_base, rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001267 AnnotateDalvikRegAccess(load, displacement >> 2, true /* is_load */, r_dest.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001268 }
1269 return load;
1270}
1271
Andreas Gampe3c12c512014-06-24 18:46:29 +00001272LIR* Arm64Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1273 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +01001274 // LoadBaseDisp() will emit correct insn for atomic load on arm64
1275 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001276
1277 LIR* load = LoadBaseDispBody(r_base, displacement, r_dest, size);
1278
1279 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001280 // TODO: This should generate an acquire load instead of the barrier.
1281 GenMemBarrier(kLoadAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001282 }
1283
1284 return load;
Vladimir Marko674744e2014-04-24 15:18:26 +01001285}
1286
Matteo Franchin43ec8732014-03-31 15:00:14 +01001287LIR* Arm64Mir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001288 OpSize size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001289 LIR* store = nullptr;
Matteo Franchin4163c532014-07-15 15:20:27 +01001290 A64Opcode opcode = kA64Brk1d;
1291 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001292 int scale = 0;
1293
Matteo Franchin43ec8732014-03-31 15:00:14 +01001294 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001295 case kDouble: // Intentional fall-through.
1296 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001297 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001298 r_src = Check64BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001299 scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001300 if (r_src.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001301 DCHECK(r_src.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001302 opcode = WIDE(kA64Str3fXD);
1303 alt_opcode = WIDE(kA64Stur3fXd);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001304 } else {
Matteo Franchin4163c532014-07-15 15:20:27 +01001305 opcode = WIDE(kA64Str3rXD);
1306 alt_opcode = WIDE(kA64Stur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001307 }
1308 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001309 case kReference:
Andreas Gampef6815702015-01-20 09:53:48 -08001310 r_src = As32BitReg(r_src);
1311 FALLTHROUGH_INTENDED;
1312 case kSingle: // Intentional fall-through.
1313 case k32:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001314 r_src = Check32BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001315 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001316 if (r_src.IsFloat()) {
1317 DCHECK(r_src.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001318 opcode = kA64Str3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001319 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001320 opcode = kA64Str3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001321 }
1322 break;
1323 case kUnsignedHalf:
1324 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001325 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001326 opcode = kA64Strh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001327 break;
1328 case kUnsignedByte:
1329 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001330 opcode = kA64Strb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001331 break;
1332 default:
1333 LOG(FATAL) << "Bad size: " << size;
1334 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001335
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001336 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1337 int scaled_disp = displacement >> scale;
1338 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1339 // Can use scaled store.
1340 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), scaled_disp);
1341 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1342 // Can use unscaled store.
1343 store = NewLIR3(alt_opcode, r_src.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001344 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001345 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001346 RegStorage r_scratch = AllocTempWide();
1347 LoadConstantWide(r_scratch, displacement);
Andreas Gampe582f5412015-01-20 18:06:47 -08001348 store = StoreBaseIndexed(r_base, r_scratch,
1349 (size == kReference) ? As64BitReg(r_src) : r_src,
1350 0, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001351 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001352 }
1353
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001354 // TODO: In future, may need to differentiate Dalvik & spill accesses.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001355 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -08001356 DCHECK_EQ(r_base, rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001357 AnnotateDalvikRegAccess(store, displacement >> 2, false /* is_load */, r_src.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001358 }
1359 return store;
1360}
1361
Andreas Gampe3c12c512014-06-24 18:46:29 +00001362LIR* Arm64Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
1363 OpSize size, VolatileKind is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001364 // TODO: This should generate a release store and no barriers.
Andreas Gampe3c12c512014-06-24 18:46:29 +00001365 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001366 // Ensure that prior accesses become visible to other threads first.
1367 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001368 }
1369
Vladimir Marko674744e2014-04-24 15:18:26 +01001370 // StoreBaseDisp() will emit correct insn for atomic store on arm64
1371 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001372
1373 LIR* store = StoreBaseDispBody(r_base, displacement, r_src, size);
1374
1375 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001376 // Preserve order with respect to any subsequent volatile loads.
1377 // We need StoreLoad, but that generally requires the most expensive barrier.
1378 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001379 }
1380
1381 return store;
Vladimir Marko674744e2014-04-24 15:18:26 +01001382}
1383
Matteo Franchin43ec8732014-03-31 15:00:14 +01001384LIR* Arm64Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001385 UNUSED(r_dest, r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001386 LOG(FATAL) << "Unexpected use of OpFpRegCopy for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001387 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001388}
1389
Matteo Franchin43ec8732014-03-31 15:00:14 +01001390LIR* Arm64Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001391 UNUSED(op, r_base, disp);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001392 LOG(FATAL) << "Unexpected use of OpMem for Arm64";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001393 UNREACHABLE();
Matteo Franchin43ec8732014-03-31 15:00:14 +01001394}
1395
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001396LIR* Arm64Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt,
1397 QuickEntrypointEnum trampoline ATTRIBUTE_UNUSED) {
1398 // The address of the trampoline is already loaded into r_tgt.
Andreas Gampe98430592014-07-27 19:44:50 -07001399 return OpReg(op, r_tgt);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001400}
1401
1402} // namespace art