blob: 38670ff8beab2bc9351752b827a46cae0db7d1c1 [file] [log] [blame]
Matteo Franchin43ec8732014-03-31 15:00:14 +01001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "arm64_lir.h"
18#include "codegen_arm64.h"
19#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070020#include "dex/reg_storage_eq.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010021
22namespace art {
23
Matteo Franchine45fb9e2014-05-06 10:10:30 +010024/* This file contains codegen for the A64 ISA. */
Matteo Franchin43ec8732014-03-31 15:00:14 +010025
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010026int32_t Arm64Mir2Lir::EncodeImmSingle(uint32_t bits) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010027 /*
28 * Valid values will have the form:
29 *
30 * aBbb.bbbc.defg.h000.0000.0000.0000.0000
31 *
32 * where B = not(b). In other words, if b == 1, then B == 0 and viceversa.
33 */
34
35 // bits[19..0] are cleared.
36 if ((bits & 0x0007ffff) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010037 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010038
39 // bits[29..25] are all set or all cleared.
40 uint32_t b_pattern = (bits >> 16) & 0x3e00;
41 if (b_pattern != 0 && b_pattern != 0x3e00)
42 return -1;
43
44 // bit[30] and bit[29] are opposite.
45 if (((bits ^ (bits << 1)) & 0x40000000) == 0)
46 return -1;
47
48 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
49 // bit7: a000.0000
50 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
51 // bit6: 0b00.0000
52 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
53 // bit5_to_0: 00cd.efgh
54 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
55 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010056}
57
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010058int32_t Arm64Mir2Lir::EncodeImmDouble(uint64_t bits) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010059 /*
60 * Valid values will have the form:
61 *
62 * aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
63 * 0000.0000.0000.0000.0000.0000.0000.0000
64 *
65 * where B = not(b).
66 */
67
68 // bits[47..0] are cleared.
69 if ((bits & UINT64_C(0xffffffffffff)) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010070 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010071
72 // bits[61..54] are all set or all cleared.
73 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
74 if (b_pattern != 0 && b_pattern != 0x3fc0)
75 return -1;
76
77 // bit[62] and bit[61] are opposite.
78 if (((bits ^ (bits << 1)) & UINT64_C(0x4000000000000000)) == 0)
79 return -1;
80
81 // bit7: a000.0000
82 uint32_t bit7 = ((bits >> 63) & 0x1) << 7;
83 // bit6: 0b00.0000
84 uint32_t bit6 = ((bits >> 61) & 0x1) << 6;
85 // bit5_to_0: 00cd.efgh
86 uint32_t bit5_to_0 = (bits >> 48) & 0x3f;
87 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010088}
89
Serban Constantinescu63999682014-07-15 17:44:21 +010090size_t Arm64Mir2Lir::GetLoadStoreSize(LIR* lir) {
91 bool opcode_is_wide = IS_WIDE(lir->opcode);
Matteo Franchin4163c532014-07-15 15:20:27 +010092 A64Opcode opcode = UNWIDE(lir->opcode);
Serban Constantinescu63999682014-07-15 17:44:21 +010093 DCHECK(!IsPseudoLirOp(opcode));
Matteo Franchin4163c532014-07-15 15:20:27 +010094 const A64EncodingMap *encoder = &EncodingMap[opcode];
Serban Constantinescu63999682014-07-15 17:44:21 +010095 uint32_t bits = opcode_is_wide ? encoder->xskeleton : encoder->wskeleton;
96 return (bits >> 30);
97}
98
99size_t Arm64Mir2Lir::GetInstructionOffset(LIR* lir) {
100 size_t offset = lir->operands[2];
101 uint64_t check_flags = GetTargetInstFlags(lir->opcode);
102 DCHECK((check_flags & IS_LOAD) || (check_flags & IS_STORE));
103 if (check_flags & SCALED_OFFSET_X0) {
104 DCHECK(check_flags & IS_TERTIARY_OP);
105 offset = offset * (1 << GetLoadStoreSize(lir));
106 }
107 return offset;
108}
109
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100110LIR* Arm64Mir2Lir::LoadFPConstantValue(RegStorage r_dest, int32_t value) {
111 DCHECK(r_dest.IsSingle());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100112 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100113 return NewLIR2(kA64Fmov2sw, r_dest.GetReg(), rwzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100114 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100115 int32_t encoded_imm = EncodeImmSingle((uint32_t)value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100116 if (encoded_imm >= 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100117 return NewLIR2(kA64Fmov2fI, r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100118 }
119 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100120
Matteo Franchin43ec8732014-03-31 15:00:14 +0100121 LIR* data_target = ScanLiteralPool(literal_list_, value, 0);
122 if (data_target == NULL) {
Andreas Gampef9879272014-06-18 23:19:07 -0700123 // Wide, as we need 8B alignment.
124 data_target = AddWideData(&literal_list_, value, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100125 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100126
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100127 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100128 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, kA64Ldr2fp,
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100129 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100130 AppendLIR(load_pc_rel);
131 return load_pc_rel;
132}
133
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100134LIR* Arm64Mir2Lir::LoadFPConstantValueWide(RegStorage r_dest, int64_t value) {
135 DCHECK(r_dest.IsDouble());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100136 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100137 return NewLIR2(kA64Fmov2Sx, r_dest.GetReg(), rxzr);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100138 } else {
139 int32_t encoded_imm = EncodeImmDouble(value);
140 if (encoded_imm >= 0) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100141 return NewLIR2(WIDE(kA64Fmov2fI), r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100142 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100143 }
144
145 // No short form - load from the literal pool.
146 int32_t val_lo = Low32Bits(value);
147 int32_t val_hi = High32Bits(value);
148 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
149 if (data_target == NULL) {
150 data_target = AddWideData(&literal_list_, val_lo, val_hi);
151 }
152
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100153 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchin4163c532014-07-15 15:20:27 +0100154 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2fp),
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100155 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100156 AppendLIR(load_pc_rel);
157 return load_pc_rel;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100158}
159
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100160static int CountLeadingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100161 return (is_wide) ? __builtin_clzll(value) : __builtin_clz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100162}
Matteo Franchin43ec8732014-03-31 15:00:14 +0100163
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100164static int CountTrailingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100165 return (is_wide) ? __builtin_ctzll(value) : __builtin_ctz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100166}
167
168static int CountSetBits(bool is_wide, uint64_t value) {
169 return ((is_wide) ?
Zheng Xue2eb29e2014-06-12 10:22:33 +0800170 __builtin_popcountll(value) : __builtin_popcount((uint32_t)value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100171}
172
173/**
174 * @brief Try encoding an immediate in the form required by logical instructions.
175 *
176 * @param is_wide Whether @p value is a 64-bit (as opposed to 32-bit) value.
177 * @param value An integer to be encoded. This is interpreted as 64-bit if @p is_wide is true and as
178 * 32-bit if @p is_wide is false.
179 * @return A non-negative integer containing the encoded immediate or -1 if the encoding failed.
180 * @note This is the inverse of Arm64Mir2Lir::DecodeLogicalImmediate().
181 */
182int Arm64Mir2Lir::EncodeLogicalImmediate(bool is_wide, uint64_t value) {
183 unsigned n, imm_s, imm_r;
184
185 // Logical immediates are encoded using parameters n, imm_s and imm_r using
186 // the following table:
187 //
188 // N imms immr size S R
189 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
190 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
191 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
192 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
193 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
194 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
195 // (s bits must not be all set)
196 //
197 // A pattern is constructed of size bits, where the least significant S+1
198 // bits are set. The pattern is rotated right by R, and repeated across a
199 // 32 or 64-bit value, depending on destination register width.
200 //
201 // To test if an arbitary immediate can be encoded using this scheme, an
202 // iterative algorithm is used.
203 //
204
205 // 1. If the value has all set or all clear bits, it can't be encoded.
206 if (value == 0 || value == ~UINT64_C(0) ||
207 (!is_wide && (uint32_t)value == ~UINT32_C(0))) {
208 return -1;
209 }
210
211 unsigned lead_zero = CountLeadingZeros(is_wide, value);
212 unsigned lead_one = CountLeadingZeros(is_wide, ~value);
213 unsigned trail_zero = CountTrailingZeros(is_wide, value);
214 unsigned trail_one = CountTrailingZeros(is_wide, ~value);
215 unsigned set_bits = CountSetBits(is_wide, value);
216
217 // The fixed bits in the immediate s field.
218 // If width == 64 (X reg), start at 0xFFFFFF80.
219 // If width == 32 (W reg), start at 0xFFFFFFC0, as the iteration for 64-bit
220 // widths won't be executed.
221 unsigned width = (is_wide) ? 64 : 32;
222 int imm_s_fixed = (is_wide) ? -128 : -64;
223 int imm_s_mask = 0x3f;
224
225 for (;;) {
226 // 2. If the value is two bits wide, it can be encoded.
227 if (width == 2) {
228 n = 0;
229 imm_s = 0x3C;
230 imm_r = (value & 3) - 1;
231 break;
232 }
233
234 n = (width == 64) ? 1 : 0;
235 imm_s = ((imm_s_fixed | (set_bits - 1)) & imm_s_mask);
236 if ((lead_zero + set_bits) == width) {
237 imm_r = 0;
238 } else {
239 imm_r = (lead_zero > 0) ? (width - trail_zero) : lead_one;
240 }
241
242 // 3. If the sum of leading zeros, trailing zeros and set bits is
243 // equal to the bit width of the value, it can be encoded.
244 if (lead_zero + trail_zero + set_bits == width) {
245 break;
246 }
247
248 // 4. If the sum of leading ones, trailing ones and unset bits in the
249 // value is equal to the bit width of the value, it can be encoded.
250 if (lead_one + trail_one + (width - set_bits) == width) {
251 break;
252 }
253
254 // 5. If the most-significant half of the bitwise value is equal to
255 // the least-significant half, return to step 2 using the
256 // least-significant half of the value.
257 uint64_t mask = (UINT64_C(1) << (width >> 1)) - 1;
258 if ((value & mask) == ((value >> (width >> 1)) & mask)) {
259 width >>= 1;
260 set_bits >>= 1;
261 imm_s_fixed >>= 1;
262 continue;
263 }
264
265 // 6. Otherwise, the value can't be encoded.
266 return -1;
267 }
268
269 return (n << 12 | imm_r << 6 | imm_s);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100270}
271
Matteo Franchinc763e352014-07-04 12:53:27 +0100272// Maximum number of instructions to use for encoding the immediate.
273static const int max_num_ops_per_const_load = 2;
274
275/**
276 * @brief Return the number of fast halfwords in the given uint64_t integer.
277 * @details The input integer is split into 4 halfwords (bits 0-15, 16-31, 32-47, 48-63). The
278 * number of fast halfwords (halfwords that are either 0 or 0xffff) is returned. See below for
279 * a more accurate description.
280 * @param value The input 64-bit integer.
281 * @return Return @c retval such that (retval & 0x7) is the maximum between n and m, where n is
282 * the number of halfwords with all bits unset (0) and m is the number of halfwords with all bits
283 * set (0xffff). Additionally (retval & 0x8) is set when m > n.
284 */
285static int GetNumFastHalfWords(uint64_t value) {
286 unsigned int num_0000_halfwords = 0;
287 unsigned int num_ffff_halfwords = 0;
288 for (int shift = 0; shift < 64; shift += 16) {
289 uint16_t halfword = static_cast<uint16_t>(value >> shift);
290 if (halfword == 0)
291 num_0000_halfwords++;
292 else if (halfword == UINT16_C(0xffff))
293 num_ffff_halfwords++;
294 }
295 if (num_0000_halfwords >= num_ffff_halfwords) {
296 DCHECK_LE(num_0000_halfwords, 4U);
297 return num_0000_halfwords;
298 } else {
299 DCHECK_LE(num_ffff_halfwords, 4U);
300 return num_ffff_halfwords | 0x8;
301 }
302}
303
304// The InexpensiveConstantXXX variants below are used in the promotion algorithm to determine how a
305// constant is considered for promotion. If the constant is "inexpensive" then the promotion
306// algorithm will give it a low priority for promotion, even when it is referenced many times in
307// the code.
308
Matteo Franchin43ec8732014-03-31 15:00:14 +0100309bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value) {
Matteo Franchinc763e352014-07-04 12:53:27 +0100310 // A 32-bit int can always be loaded with 2 instructions (and without using the literal pool).
311 // We therefore return true and give it a low priority for promotion.
312 return true;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100313}
314
315bool Arm64Mir2Lir::InexpensiveConstantFloat(int32_t value) {
316 return EncodeImmSingle(value) >= 0;
317}
318
319bool Arm64Mir2Lir::InexpensiveConstantLong(int64_t value) {
Matteo Franchinc763e352014-07-04 12:53:27 +0100320 int num_slow_halfwords = 4 - (GetNumFastHalfWords(value) & 0x7);
321 if (num_slow_halfwords <= max_num_ops_per_const_load) {
322 return true;
323 }
324 return (EncodeLogicalImmediate(/*is_wide=*/true, value) >= 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100325}
326
327bool Arm64Mir2Lir::InexpensiveConstantDouble(int64_t value) {
328 return EncodeImmDouble(value) >= 0;
329}
330
Matteo Franchinc763e352014-07-04 12:53:27 +0100331// The InexpensiveConstantXXX variants below are used to determine which A64 instructions to use
332// when one of the operands is an immediate (e.g. register version or immediate version of add).
333
334bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value, Instruction::Code opcode) {
335 switch (opcode) {
336 case Instruction::IF_EQ:
337 case Instruction::IF_NE:
338 case Instruction::IF_LT:
339 case Instruction::IF_GE:
340 case Instruction::IF_GT:
341 case Instruction::IF_LE:
342 case Instruction::ADD_INT:
343 case Instruction::ADD_INT_2ADDR:
344 case Instruction::SUB_INT:
345 case Instruction::SUB_INT_2ADDR:
346 // The code below is consistent with the implementation of OpRegRegImm().
347 {
348 int32_t abs_value = std::abs(value);
349 if (abs_value < 0x1000) {
350 return true;
351 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
352 return true;
353 }
354 return false;
355 }
356 case Instruction::SHL_INT:
357 case Instruction::SHL_INT_2ADDR:
358 case Instruction::SHR_INT:
359 case Instruction::SHR_INT_2ADDR:
360 case Instruction::USHR_INT:
361 case Instruction::USHR_INT_2ADDR:
362 return true;
363 case Instruction::AND_INT:
364 case Instruction::AND_INT_2ADDR:
365 case Instruction::AND_INT_LIT16:
366 case Instruction::AND_INT_LIT8:
367 case Instruction::OR_INT:
368 case Instruction::OR_INT_2ADDR:
369 case Instruction::OR_INT_LIT16:
370 case Instruction::OR_INT_LIT8:
371 case Instruction::XOR_INT:
372 case Instruction::XOR_INT_2ADDR:
373 case Instruction::XOR_INT_LIT16:
374 case Instruction::XOR_INT_LIT8:
375 if (value == 0 || value == INT32_C(-1)) {
376 return true;
377 }
378 return (EncodeLogicalImmediate(/*is_wide=*/false, value) >= 0);
379 default:
380 return false;
381 }
382}
383
Matteo Franchin43ec8732014-03-31 15:00:14 +0100384/*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100385 * Load a immediate using one single instruction when possible; otherwise
386 * use a pair of movz and movk instructions.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100387 *
388 * No additional register clobbering operation performed. Use this version when
389 * 1) r_dest is freshly returned from AllocTemp or
390 * 2) The codegen is under fixed register usage
391 */
392LIR* Arm64Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
393 LIR* res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100394
395 if (r_dest.IsFloat()) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100396 return LoadFPConstantValue(r_dest, value);
397 }
398
399 if (r_dest.Is64Bit()) {
400 return LoadConstantWide(r_dest, value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100401 }
402
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100403 // Loading SP/ZR with an immediate is not supported.
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100404 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
405 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100406
407 // Compute how many movk, movz instructions are needed to load the value.
408 uint16_t high_bits = High16Bits(value);
409 uint16_t low_bits = Low16Bits(value);
410
411 bool low_fast = ((uint16_t)(low_bits + 1) <= 1);
412 bool high_fast = ((uint16_t)(high_bits + 1) <= 1);
413
414 if (LIKELY(low_fast || high_fast)) {
415 // 1 instruction is enough to load the immediate.
416 if (LIKELY(low_bits == high_bits)) {
417 // Value is either 0 or -1: we can just use wzr.
Matteo Franchin4163c532014-07-15 15:20:27 +0100418 A64Opcode opcode = LIKELY(low_bits == 0) ? kA64Mov2rr : kA64Mvn2rr;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100419 res = NewLIR2(opcode, r_dest.GetReg(), rwzr);
420 } else {
421 uint16_t uniform_bits, useful_bits;
422 int shift;
423
424 if (LIKELY(high_fast)) {
425 shift = 0;
426 uniform_bits = high_bits;
427 useful_bits = low_bits;
428 } else {
429 shift = 1;
430 uniform_bits = low_bits;
431 useful_bits = high_bits;
432 }
433
434 if (UNLIKELY(uniform_bits != 0)) {
435 res = NewLIR3(kA64Movn3rdM, r_dest.GetReg(), ~useful_bits, shift);
436 } else {
437 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), useful_bits, shift);
438 }
439 }
440 } else {
441 // movk, movz require 2 instructions. Try detecting logical immediates.
442 int log_imm = EncodeLogicalImmediate(/*is_wide=*/false, value);
443 if (log_imm >= 0) {
444 res = NewLIR3(kA64Orr3Rrl, r_dest.GetReg(), rwzr, log_imm);
445 } else {
446 // Use 2 instructions.
447 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), low_bits, 0);
448 NewLIR3(kA64Movk3rdM, r_dest.GetReg(), high_bits, 1);
449 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100450 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100451
Matteo Franchin43ec8732014-03-31 15:00:14 +0100452 return res;
453}
454
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100455// TODO: clean up the names. LoadConstantWide() should really be LoadConstantNoClobberWide().
456LIR* Arm64Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100457 if (r_dest.IsFloat()) {
458 return LoadFPConstantValueWide(r_dest, value);
459 }
460
461 DCHECK(r_dest.Is64Bit());
462
463 // Loading SP/ZR with an immediate is not supported.
464 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
465 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
466
467 if (LIKELY(value == INT64_C(0) || value == INT64_C(-1))) {
468 // value is either 0 or -1: we can just use xzr.
Matteo Franchin4163c532014-07-15 15:20:27 +0100469 A64Opcode opcode = LIKELY(value == 0) ? WIDE(kA64Mov2rr) : WIDE(kA64Mvn2rr);
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100470 return NewLIR2(opcode, r_dest.GetReg(), rxzr);
471 }
472
473 // At least one in value's halfwords is not 0x0, nor 0xffff: find out how many.
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100474 uint64_t uvalue = static_cast<uint64_t>(value);
Matteo Franchinc763e352014-07-04 12:53:27 +0100475 int num_fast_halfwords = GetNumFastHalfWords(uvalue);
476 int num_slow_halfwords = 4 - (num_fast_halfwords & 0x7);
477 bool more_ffff_halfwords = (num_fast_halfwords & 0x8) != 0;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100478
Matteo Franchinc763e352014-07-04 12:53:27 +0100479 if (num_slow_halfwords > 1) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100480 // A single movz/movn is not enough. Try the logical immediate route.
481 int log_imm = EncodeLogicalImmediate(/*is_wide=*/true, value);
482 if (log_imm >= 0) {
483 return NewLIR3(WIDE(kA64Orr3Rrl), r_dest.GetReg(), rxzr, log_imm);
484 }
485 }
486
Matteo Franchinc763e352014-07-04 12:53:27 +0100487 if (num_slow_halfwords <= max_num_ops_per_const_load) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100488 // We can encode the number using a movz/movn followed by one or more movk.
Matteo Franchin4163c532014-07-15 15:20:27 +0100489 A64Opcode op;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100490 uint16_t background;
491 LIR* res = nullptr;
492
493 // Decide whether to use a movz or a movn.
Matteo Franchinc763e352014-07-04 12:53:27 +0100494 if (more_ffff_halfwords) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100495 op = WIDE(kA64Movn3rdM);
496 background = 0xffff;
Matteo Franchinc763e352014-07-04 12:53:27 +0100497 } else {
498 op = WIDE(kA64Movz3rdM);
499 background = 0;
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100500 }
501
502 // Emit the first instruction (movz, movn).
503 int shift;
504 for (shift = 0; shift < 4; shift++) {
505 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
506 if (halfword != background) {
507 res = NewLIR3(op, r_dest.GetReg(), halfword ^ background, shift);
508 break;
509 }
510 }
511
512 // Emit the movk instructions.
513 for (shift++; shift < 4; shift++) {
514 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
515 if (halfword != background) {
516 NewLIR3(WIDE(kA64Movk3rdM), r_dest.GetReg(), halfword, shift);
517 }
518 }
519 return res;
520 }
521
522 // Use the literal pool.
523 int32_t val_lo = Low32Bits(value);
524 int32_t val_hi = High32Bits(value);
525 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
526 if (data_target == NULL) {
527 data_target = AddWideData(&literal_list_, val_lo, val_hi);
528 }
529
530 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
531 LIR *res = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp),
532 r_dest.GetReg(), 0, 0, 0, 0, data_target);
533 AppendLIR(res);
534 return res;
535}
536
Matteo Franchin43ec8732014-03-31 15:00:14 +0100537LIR* Arm64Mir2Lir::OpUnconditionalBranch(LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100538 LIR* res = NewLIR1(kA64B1t, 0 /* offset to be patched during assembly */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100539 res->target = target;
540 return res;
541}
542
543LIR* Arm64Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100544 LIR* branch = NewLIR2(kA64B2ct, ArmConditionEncoding(cc),
545 0 /* offset to be patched */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100546 branch->target = target;
547 return branch;
548}
549
550LIR* Arm64Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100551 A64Opcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100552 switch (op) {
553 case kOpBlx:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100554 opcode = kA64Blr1x;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100555 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100556 default:
557 LOG(FATAL) << "Bad opcode " << op;
558 }
559 return NewLIR1(opcode, r_dest_src.GetReg());
560}
561
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100562LIR* Arm64Mir2Lir::OpRegRegShift(OpKind op, RegStorage r_dest_src1, RegStorage r_src2, int shift) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100563 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100564 CHECK_EQ(r_dest_src1.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin4163c532014-07-15 15:20:27 +0100565 A64Opcode opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100566
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100567 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100568 case kOpCmn:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100569 opcode = kA64Cmn3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100570 break;
571 case kOpCmp:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100572 opcode = kA64Cmp3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100573 break;
574 case kOpMov:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100575 opcode = kA64Mov2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100576 break;
577 case kOpMvn:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100578 opcode = kA64Mvn2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100579 break;
580 case kOpNeg:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100581 opcode = kA64Neg3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100582 break;
583 case kOpTst:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100584 opcode = kA64Tst3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100585 break;
586 case kOpRev:
587 DCHECK_EQ(shift, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100588 // Binary, but rm is encoded twice.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100589 return NewLIR2(kA64Rev2rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100590 break;
591 case kOpRevsh:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100592 // Binary, but rm is encoded twice.
Zheng Xua3fe7422014-07-09 14:03:15 +0800593 NewLIR2(kA64Rev162rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
594 // "sxth r1, r2" is "sbfm r1, r2, #0, #15"
595 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100596 break;
597 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";
627 return NULL;
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);
646 break;
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100647 default:
648 LOG(FATAL) << "Bad Opcode: " << opcode;
649 break;
650 }
651
652 DCHECK(!IsPseudoLirOp(opcode));
653 if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100654 A64EncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100655 if (kind == kFmtExtend) {
Zheng Xucedee472014-07-01 09:53:22 +0800656 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(),
657 EncodeExtend(ext, amount));
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100658 }
659 }
660
661 LOG(FATAL) << "Unexpected encoding operand count";
662 return NULL;
663}
664
Matteo Franchin43ec8732014-03-31 15:00:14 +0100665LIR* Arm64Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100666 /* RegReg operations with SP in first parameter need extended register instruction form.
Zheng Xucedee472014-07-01 09:53:22 +0800667 * Only CMN, CMP, ADD & SUB instructions are implemented.
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100668 */
Zheng Xubaa7c882014-06-30 14:26:50 +0800669 if (r_dest_src1 == rs_sp) {
Zheng Xucedee472014-07-01 09:53:22 +0800670 return OpRegRegExtend(op, r_dest_src1, r_src2, kA64Uxtx, 0);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100671 } else {
672 return OpRegRegShift(op, r_dest_src1, r_src2, ENCODE_NO_SHIFT);
673 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100674}
675
676LIR* Arm64Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type) {
677 UNIMPLEMENTED(FATAL);
678 return nullptr;
679}
680
681LIR* Arm64Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
682 UNIMPLEMENTED(FATAL);
683 return nullptr;
684}
685
686LIR* Arm64Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100687 LOG(FATAL) << "Unexpected use of OpCondRegReg for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100688 return NULL;
689}
690
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100691LIR* Arm64Mir2Lir::OpRegRegRegShift(OpKind op, RegStorage r_dest, RegStorage r_src1,
692 RegStorage r_src2, int shift) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100693 A64Opcode opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100694
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100695 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100696 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100697 opcode = kA64Add4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100698 break;
699 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100700 opcode = kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100701 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100702 // case kOpRsub:
703 // opcode = kA64RsubWWW;
704 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100705 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100706 opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100707 break;
708 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100709 opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100710 break;
711 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100712 opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100713 break;
714 case kOpMul:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100715 opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100716 break;
717 case kOpDiv:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100718 opcode = kA64Sdiv3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100719 break;
720 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100721 opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100722 break;
723 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100724 opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100725 break;
726 case kOpLsl:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100727 opcode = kA64Lsl3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100728 break;
729 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100730 opcode = kA64Lsr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100731 break;
732 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100733 opcode = kA64Asr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100734 break;
735 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100736 opcode = kA64Ror3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100737 break;
738 default:
739 LOG(FATAL) << "Bad opcode: " << op;
740 break;
741 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100742
743 // The instructions above belong to two kinds:
744 // - 4-operands instructions, where the last operand is a shift/extend immediate,
745 // - 3-operands instructions with no shift/extend.
Matteo Franchin4163c532014-07-15 15:20:27 +0100746 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100747 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
748 CHECK_EQ(r_dest.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100749 if (EncodingMap[opcode].flags & IS_QUAD_OP) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100750 DCHECK(!IsExtendEncoding(shift));
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100751 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100752 } else {
753 DCHECK(EncodingMap[opcode].flags & IS_TERTIARY_OP);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100754 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100755 return NewLIR3(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100756 }
757}
758
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700759LIR* Arm64Mir2Lir::OpRegRegRegExtend(OpKind op, RegStorage r_dest, RegStorage r_src1,
760 RegStorage r_src2, A64RegExtEncodings ext, uint8_t amount) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100761 A64Opcode opcode = kA64Brk1d;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700762
763 switch (op) {
764 case kOpAdd:
765 opcode = kA64Add4RRre;
766 break;
767 case kOpSub:
768 opcode = kA64Sub4RRre;
769 break;
770 default:
771 LOG(FATAL) << "Unimplemented opcode: " << op;
772 break;
773 }
Matteo Franchin4163c532014-07-15 15:20:27 +0100774 A64Opcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700775
776 if (r_dest.Is64Bit()) {
777 CHECK(r_src1.Is64Bit());
778
779 // dest determines whether the op is wide or not. Up-convert src2 when necessary.
780 // Note: this is not according to aarch64 specifications, but our encoding.
781 if (!r_src2.Is64Bit()) {
782 r_src2 = As64BitReg(r_src2);
783 }
784 } else {
785 CHECK(!r_src1.Is64Bit());
786 CHECK(!r_src2.Is64Bit());
787 }
788
789 // Sanity checks.
790 // 1) Amount is in the range 0..4
791 CHECK_LE(amount, 4);
792
793 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(),
794 EncodeExtend(ext, amount));
795}
796
Matteo Franchin43ec8732014-03-31 15:00:14 +0100797LIR* Arm64Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100798 return OpRegRegRegShift(op, r_dest, r_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100799}
800
801LIR* Arm64Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800802 return OpRegRegImm64(op, r_dest, r_src1, static_cast<int64_t>(value));
803}
804
805LIR* Arm64Mir2Lir::OpRegRegImm64(OpKind op, RegStorage r_dest, RegStorage r_src1, int64_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100806 LIR* res;
807 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100808 int64_t abs_value = (neg) ? -value : value;
Matteo Franchin4163c532014-07-15 15:20:27 +0100809 A64Opcode opcode = kA64Brk1d;
810 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinc763e352014-07-04 12:53:27 +0100811 bool is_logical = false;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100812 bool is_wide = r_dest.Is64Bit();
Matteo Franchin4163c532014-07-15 15:20:27 +0100813 A64Opcode wide = (is_wide) ? WIDE(0) : UNWIDE(0);
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700814 int info = 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100815
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100816 switch (op) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100817 case kOpLsl: {
818 // "lsl w1, w2, #imm" is an alias of "ubfm w1, w2, #(-imm MOD 32), #(31-imm)"
Zheng Xu2d41a652014-06-09 11:05:31 +0800819 // and "lsl x1, x2, #imm" of "ubfm x1, x2, #(-imm MOD 64), #(63-imm)".
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100820 // For now, we just use ubfm directly.
Zheng Xu2d41a652014-06-09 11:05:31 +0800821 int max_value = (is_wide) ? 63 : 31;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100822 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest.GetReg(), r_src1.GetReg(),
Zheng Xu2d41a652014-06-09 11:05:31 +0800823 (-value) & max_value, max_value - value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100824 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100825 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100826 return NewLIR3(kA64Lsr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100827 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100828 return NewLIR3(kA64Asr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100829 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100830 // "ror r1, r2, #imm" is an alias of "extr r1, r2, r2, #imm".
831 // For now, we just use extr directly.
832 return NewLIR4(kA64Extr4rrrd | wide, r_dest.GetReg(), r_src1.GetReg(), r_src1.GetReg(),
833 value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100834 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100835 neg = !neg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100836 // Note: intentional fallthrough
837 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100838 // Add and sub below read/write sp rather than xzr.
839 if (abs_value < 0x1000) {
840 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
841 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value, 0);
842 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
843 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
844 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value >> 12, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100845 } else {
Vladimir Marko903989d2014-07-01 17:21:18 +0100846 alt_opcode = (op == kOpAdd) ? kA64Add4RRre : kA64Sub4RRre;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700847 info = EncodeExtend(is_wide ? kA64Uxtx : kA64Uxtw, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100848 }
849 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100850 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100851 alt_opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100852 break;
853 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100854 alt_opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100855 break;
856 case kOpOr:
Matteo Franchinc763e352014-07-04 12:53:27 +0100857 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100858 opcode = kA64Orr3Rrl;
859 alt_opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100860 break;
861 case kOpAnd:
Matteo Franchinc763e352014-07-04 12:53:27 +0100862 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100863 opcode = kA64And3Rrl;
864 alt_opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100865 break;
866 case kOpXor:
Matteo Franchinc763e352014-07-04 12:53:27 +0100867 is_logical = true;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100868 opcode = kA64Eor3Rrl;
869 alt_opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100870 break;
871 case kOpMul:
872 // TUNING: power of 2, shift & add
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100873 alt_opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100874 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100875 default:
876 LOG(FATAL) << "Bad opcode: " << op;
877 }
878
Matteo Franchinc763e352014-07-04 12:53:27 +0100879 if (is_logical) {
880 int log_imm = EncodeLogicalImmediate(is_wide, value);
881 if (log_imm >= 0) {
882 return NewLIR3(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), log_imm);
Zheng Xue2eb29e2014-06-12 10:22:33 +0800883 } else {
Matteo Franchinc763e352014-07-04 12:53:27 +0100884 // When the immediate is either 0 or ~0, the logical operation can be trivially reduced
885 // to a - possibly negated - assignment.
886 if (value == 0) {
887 switch (op) {
888 case kOpOr:
889 case kOpXor:
890 // Or/Xor by zero reduces to an assignment.
891 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
892 default:
893 // And by zero reduces to a `mov rdest, xzr'.
894 DCHECK(op == kOpAnd);
895 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
896 }
897 } else if (value == INT64_C(-1)
898 || (!is_wide && static_cast<uint32_t>(value) == ~UINT32_C(0))) {
899 switch (op) {
900 case kOpAnd:
901 // And by -1 reduces to an assignment.
902 return NewLIR2(kA64Mov2rr | wide, r_dest.GetReg(), r_src1.GetReg());
903 case kOpXor:
904 // Xor by -1 reduces to an `mvn rdest, rsrc'.
905 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), r_src1.GetReg());
906 default:
907 // Or by -1 reduces to a `mvn rdest, xzr'.
908 DCHECK(op == kOpOr);
909 return NewLIR2(kA64Mvn2rr | wide, r_dest.GetReg(), (is_wide) ? rxzr : rwzr);
910 }
911 }
Zheng Xue2eb29e2014-06-12 10:22:33 +0800912 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100913 }
Matteo Franchinc763e352014-07-04 12:53:27 +0100914
915 RegStorage r_scratch;
916 if (is_wide) {
917 r_scratch = AllocTempWide();
918 LoadConstantWide(r_scratch, value);
919 } else {
920 r_scratch = AllocTemp();
921 LoadConstant(r_scratch, value);
922 }
923 if (EncodingMap[alt_opcode].flags & IS_QUAD_OP)
924 res = NewLIR4(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg(), info);
925 else
926 res = NewLIR3(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
927 FreeTemp(r_scratch);
928 return res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100929}
930
Matteo Franchin43ec8732014-03-31 15:00:14 +0100931LIR* Arm64Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100932 return OpRegImm64(op, r_dest_src1, static_cast<int64_t>(value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100933}
934
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100935LIR* Arm64Mir2Lir::OpRegImm64(OpKind op, RegStorage r_dest_src1, int64_t value) {
Matteo Franchin4163c532014-07-15 15:20:27 +0100936 A64Opcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
937 A64Opcode opcode = kA64Brk1d;
938 A64Opcode neg_opcode = kA64Brk1d;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100939 bool shift;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100940 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100941 uint64_t abs_value = (neg) ? -value : value;
942
943 if (LIKELY(abs_value < 0x1000)) {
944 // abs_value is a 12-bit immediate.
945 shift = false;
946 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
947 // abs_value is a shifted 12-bit immediate.
948 shift = true;
949 abs_value >>= 12;
Zheng Xue2eb29e2014-06-12 10:22:33 +0800950 } else if (LIKELY(abs_value < 0x1000000 && (op == kOpAdd || op == kOpSub))) {
951 // Note: It is better to use two ADD/SUB instead of loading a number to a temp register.
952 // This works for both normal registers and SP.
953 // For a frame size == 0x2468, it will be encoded as:
954 // sub sp, #0x2000
955 // sub sp, #0x468
956 if (neg) {
957 op = (op == kOpAdd) ? kOpSub : kOpAdd;
958 }
959 OpRegImm64(op, r_dest_src1, abs_value & (~INT64_C(0xfff)));
960 return OpRegImm64(op, r_dest_src1, abs_value & 0xfff);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100961 } else {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800962 RegStorage r_tmp;
963 LIR* res;
964 if (IS_WIDE(wide)) {
965 r_tmp = AllocTempWide();
966 res = LoadConstantWide(r_tmp, value);
967 } else {
968 r_tmp = AllocTemp();
969 res = LoadConstant(r_tmp, value);
970 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100971 OpRegReg(op, r_dest_src1, r_tmp);
972 FreeTemp(r_tmp);
973 return res;
974 }
975
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100976 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100977 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100978 neg_opcode = kA64Sub4RRdT;
979 opcode = kA64Add4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100980 break;
981 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100982 neg_opcode = kA64Add4RRdT;
983 opcode = kA64Sub4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100984 break;
985 case kOpCmp:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100986 neg_opcode = kA64Cmn3RdT;
987 opcode = kA64Cmp3RdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100988 break;
989 default:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100990 LOG(FATAL) << "Bad op-kind in OpRegImm: " << op;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100991 break;
992 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100993
994 if (UNLIKELY(neg))
995 opcode = neg_opcode;
996
997 if (EncodingMap[opcode].flags & IS_QUAD_OP)
998 return NewLIR4(opcode | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), abs_value,
999 (shift) ? 1 : 0);
1000 else
1001 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), abs_value, (shift) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001002}
1003
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001004int Arm64Mir2Lir::EncodeShift(int shift_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001005 DCHECK_EQ(shift_type & 0x3, shift_type);
1006 DCHECK_EQ(amount & 0x3f, amount);
Matteo Franchinc61b3c92014-06-18 11:52:47 +01001007 return ((shift_type & 0x3) << 7) | (amount & 0x3f);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001008}
1009
1010int Arm64Mir2Lir::EncodeExtend(int extend_type, int amount) {
Zheng Xucedee472014-07-01 09:53:22 +08001011 DCHECK_EQ(extend_type & 0x7, extend_type);
1012 DCHECK_EQ(amount & 0x7, amount);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001013 return (1 << 6) | ((extend_type & 0x7) << 3) | (amount & 0x7);
1014}
1015
1016bool Arm64Mir2Lir::IsExtendEncoding(int encoded_value) {
1017 return ((1 << 6) & encoded_value) != 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001018}
1019
1020LIR* Arm64Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001021 int scale, OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001022 LIR* load;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001023 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001024 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001025 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001026
1027 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1028 // register offset load (rather than doing the sign extension in a separate instruction).
1029 if (r_index.Is32Bit()) {
1030 // Assemble: ``sxtw xN, wN''.
1031 r_index = As64BitReg(r_index);
1032 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1033 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001034
1035 if (r_dest.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001036 if (r_dest.IsDouble()) {
1037 DCHECK(size == k64 || size == kDouble);
1038 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001039 opcode = WIDE(kA64Ldr4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001040 } else {
1041 DCHECK(r_dest.IsSingle());
1042 DCHECK(size == k32 || size == kSingle);
1043 expected_scale = 2;
1044 opcode = kA64Ldr4fXxG;
1045 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001046
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001047 DCHECK(scale == 0 || scale == expected_scale);
1048 return NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
1049 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001050 }
1051
1052 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001053 case kDouble:
1054 case kWord:
1055 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001056 r_dest = Check64BitReg(r_dest);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001057 opcode = WIDE(kA64Ldr4rXxG);
1058 expected_scale = 3;
1059 break;
Matteo Franchin255e0142014-07-04 13:50:41 +01001060 case kSingle: // Intentional fall-through.
1061 case k32: // Intentional fall-through.
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001062 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001063 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001064 opcode = kA64Ldr4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001065 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001066 break;
1067 case kUnsignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001068 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001069 opcode = kA64Ldrh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001070 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001071 break;
1072 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001073 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001074 opcode = kA64Ldrsh4rXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001075 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001076 break;
1077 case kUnsignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001078 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001079 opcode = kA64Ldrb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001080 break;
1081 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001082 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001083 opcode = kA64Ldrsb3rXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001084 break;
1085 default:
1086 LOG(FATAL) << "Bad size: " << size;
1087 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001088
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001089 if (UNLIKELY(expected_scale == 0)) {
1090 // This is a tertiary op (e.g. ldrb, ldrsb), it does not not support scale.
1091 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001092 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001093 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001094 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001095 DCHECK(scale == 0 || scale == expected_scale);
1096 load = NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001097 (scale != 0) ? 1 : 0);
1098 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001099
1100 return load;
1101}
1102
Matteo Franchin255e0142014-07-04 13:50:41 +01001103LIR* Arm64Mir2Lir::LoadRefIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
1104 int scale) {
1105 return LoadBaseIndexed(r_base, r_index, As32BitReg(r_dest), scale, kReference);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001106}
1107
Matteo Franchin43ec8732014-03-31 15:00:14 +01001108LIR* Arm64Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001109 int scale, OpSize size) {
1110 LIR* store;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001111 int expected_scale = 0;
Matteo Franchin4163c532014-07-15 15:20:27 +01001112 A64Opcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -07001113 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +01001114
1115 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
1116 // register offset store (rather than doing the sign extension in a separate instruction).
1117 if (r_index.Is32Bit()) {
1118 // Assemble: ``sxtw xN, wN''.
1119 r_index = As64BitReg(r_index);
1120 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
1121 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001122
1123 if (r_src.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001124 if (r_src.IsDouble()) {
1125 DCHECK(size == k64 || size == kDouble);
1126 expected_scale = 3;
Matteo Franchin4163c532014-07-15 15:20:27 +01001127 opcode = WIDE(kA64Str4fXxG);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001128 } else {
1129 DCHECK(r_src.IsSingle());
1130 DCHECK(size == k32 || size == kSingle);
1131 expected_scale = 2;
1132 opcode = kA64Str4fXxG;
1133 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001134
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001135 DCHECK(scale == 0 || scale == expected_scale);
1136 return NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1137 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001138 }
1139
1140 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001141 case kDouble: // Intentional fall-trough.
1142 case kWord: // Intentional fall-trough.
1143 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001144 r_src = Check64BitReg(r_src);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001145 opcode = WIDE(kA64Str4rXxG);
1146 expected_scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001147 break;
1148 case kSingle: // Intentional fall-trough.
1149 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001150 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001151 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001152 opcode = kA64Str4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001153 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001154 break;
1155 case kUnsignedHalf:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001156 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001157 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001158 opcode = kA64Strh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001159 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001160 break;
1161 case kUnsignedByte:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001162 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001163 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001164 opcode = kA64Strb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001165 break;
1166 default:
1167 LOG(FATAL) << "Bad size: " << size;
1168 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001169
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001170 if (UNLIKELY(expected_scale == 0)) {
1171 // This is a tertiary op (e.g. strb), it does not not support scale.
1172 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001173 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001174 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001175 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001176 store = NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1177 (scale != 0) ? 1 : 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001178 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001179
1180 return store;
1181}
1182
Matteo Franchin255e0142014-07-04 13:50:41 +01001183LIR* Arm64Mir2Lir::StoreRefIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
1184 int scale) {
1185 return StoreBaseIndexed(r_base, r_index, As32BitReg(r_src), scale, kReference);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001186}
1187
Matteo Franchin43ec8732014-03-31 15:00:14 +01001188/*
1189 * Load value from base + displacement. Optionally perform null check
1190 * on base (which must have an associated s_reg and MIR). If not
1191 * performing null check, incoming MIR can be null.
1192 */
1193LIR* Arm64Mir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001194 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001195 LIR* load = NULL;
Matteo Franchin4163c532014-07-15 15:20:27 +01001196 A64Opcode opcode = kA64Brk1d;
1197 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001198 int scale = 0;
1199
Matteo Franchin43ec8732014-03-31 15:00:14 +01001200 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001201 case kDouble: // Intentional fall-through.
1202 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001203 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001204 r_dest = Check64BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001205 scale = 3;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001206 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001207 DCHECK(r_dest.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001208 opcode = WIDE(kA64Ldr3fXD);
1209 alt_opcode = WIDE(kA64Ldur3fXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001210 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001211 opcode = WIDE(kA64Ldr3rXD);
1212 alt_opcode = WIDE(kA64Ldur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001213 }
1214 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001215 case kSingle: // Intentional fall-through.
1216 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001217 case kReference:
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);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001258 load = LoadBaseIndexed(r_base, r_scratch, r_dest, 0, size);
1259 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001260 }
1261
1262 // TODO: in future may need to differentiate Dalvik accesses w/ spills
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001263 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Zheng Xubaa7c882014-06-30 14:26:50 +08001264 DCHECK(r_base == rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001265 AnnotateDalvikRegAccess(load, displacement >> 2, true /* is_load */, r_dest.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001266 }
1267 return load;
1268}
1269
Andreas Gampe3c12c512014-06-24 18:46:29 +00001270LIR* Arm64Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1271 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +01001272 // LoadBaseDisp() will emit correct insn for atomic load on arm64
1273 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001274
1275 LIR* load = LoadBaseDispBody(r_base, displacement, r_dest, size);
1276
1277 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001278 // TODO: This should generate an acquire load instead of the barrier.
1279 GenMemBarrier(kLoadAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001280 }
1281
1282 return load;
Vladimir Marko674744e2014-04-24 15:18:26 +01001283}
1284
Andreas Gampe3c12c512014-06-24 18:46:29 +00001285LIR* Arm64Mir2Lir::LoadRefDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1286 VolatileKind is_volatile) {
1287 return LoadBaseDisp(r_base, displacement, As32BitReg(r_dest), kReference, is_volatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001288}
1289
Matteo Franchin43ec8732014-03-31 15:00:14 +01001290LIR* Arm64Mir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001291 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001292 LIR* store = NULL;
Matteo Franchin4163c532014-07-15 15:20:27 +01001293 A64Opcode opcode = kA64Brk1d;
1294 A64Opcode alt_opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001295 int scale = 0;
1296
Matteo Franchin43ec8732014-03-31 15:00:14 +01001297 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001298 case kDouble: // Intentional fall-through.
1299 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001300 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001301 r_src = Check64BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001302 scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001303 if (r_src.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001304 DCHECK(r_src.IsDouble());
Matteo Franchin4163c532014-07-15 15:20:27 +01001305 opcode = WIDE(kA64Str3fXD);
1306 alt_opcode = WIDE(kA64Stur3fXd);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001307 } else {
Matteo Franchin4163c532014-07-15 15:20:27 +01001308 opcode = WIDE(kA64Str3rXD);
1309 alt_opcode = WIDE(kA64Stur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001310 }
1311 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001312 case kSingle: // Intentional fall-through.
1313 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001314 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001315 r_src = Check32BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001316 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001317 if (r_src.IsFloat()) {
1318 DCHECK(r_src.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001319 opcode = kA64Str3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001320 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001321 opcode = kA64Str3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001322 }
1323 break;
1324 case kUnsignedHalf:
1325 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001326 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001327 opcode = kA64Strh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001328 break;
1329 case kUnsignedByte:
1330 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001331 opcode = kA64Strb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001332 break;
1333 default:
1334 LOG(FATAL) << "Bad size: " << size;
1335 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001336
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001337 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1338 int scaled_disp = displacement >> scale;
1339 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1340 // Can use scaled store.
1341 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), scaled_disp);
1342 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1343 // Can use unscaled store.
1344 store = NewLIR3(alt_opcode, r_src.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001345 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001346 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001347 RegStorage r_scratch = AllocTempWide();
1348 LoadConstantWide(r_scratch, displacement);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001349 store = StoreBaseIndexed(r_base, r_scratch, r_src, 0, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001350 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001351 }
1352
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001353 // TODO: In future, may need to differentiate Dalvik & spill accesses.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001354 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Zheng Xubaa7c882014-06-30 14:26:50 +08001355 DCHECK(r_base == rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001356 AnnotateDalvikRegAccess(store, displacement >> 2, false /* is_load */, r_src.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001357 }
1358 return store;
1359}
1360
Andreas Gampe3c12c512014-06-24 18:46:29 +00001361LIR* Arm64Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
1362 OpSize size, VolatileKind is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001363 // TODO: This should generate a release store and no barriers.
Andreas Gampe3c12c512014-06-24 18:46:29 +00001364 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001365 // Ensure that prior accesses become visible to other threads first.
1366 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001367 }
1368
Vladimir Marko674744e2014-04-24 15:18:26 +01001369 // StoreBaseDisp() will emit correct insn for atomic store on arm64
1370 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001371
1372 LIR* store = StoreBaseDispBody(r_base, displacement, r_src, size);
1373
1374 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001375 // Preserve order with respect to any subsequent volatile loads.
1376 // We need StoreLoad, but that generally requires the most expensive barrier.
1377 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001378 }
1379
1380 return store;
Vladimir Marko674744e2014-04-24 15:18:26 +01001381}
1382
Andreas Gampe3c12c512014-06-24 18:46:29 +00001383LIR* Arm64Mir2Lir::StoreRefDisp(RegStorage r_base, int displacement, RegStorage r_src,
1384 VolatileKind is_volatile) {
1385 return StoreBaseDisp(r_base, displacement, As32BitReg(r_src), kReference, is_volatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001386}
1387
Matteo Franchin43ec8732014-03-31 15:00:14 +01001388LIR* Arm64Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001389 LOG(FATAL) << "Unexpected use of OpFpRegCopy for Arm64";
1390 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001391}
1392
Matteo Franchin43ec8732014-03-31 15:00:14 +01001393LIR* Arm64Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001394 LOG(FATAL) << "Unexpected use of OpMem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001395 return NULL;
1396}
1397
Andreas Gampe98430592014-07-27 19:44:50 -07001398LIR* Arm64Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) {
1399 return OpReg(op, r_tgt);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001400}
1401
1402} // namespace art