blob: f6c140f0ab2dc59e1fc575f893150e68f58de7bc [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
Matteo Franchine45fb9e2014-05-06 10:10:30 +010026static int32_t EncodeImmSingle(uint32_t bits) {
27 /*
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
Matteo Franchine45fb9e2014-05-06 10:10:30 +010058static int32_t EncodeImmDouble(uint64_t bits) {
59 /*
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
Matteo Franchinc41e6dc2014-06-13 19:16:28 +010090LIR* Arm64Mir2Lir::LoadFPConstantValue(RegStorage r_dest, int32_t value) {
91 DCHECK(r_dest.IsSingle());
Matteo Franchin43ec8732014-03-31 15:00:14 +010092 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +010093 return NewLIR2(kA64Fmov2sw, r_dest.GetReg(), rwzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +010094 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010095 int32_t encoded_imm = EncodeImmSingle((uint32_t)value);
Matteo Franchin43ec8732014-03-31 15:00:14 +010096 if (encoded_imm >= 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +010097 return NewLIR2(kA64Fmov2fI, r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +010098 }
99 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100100
Matteo Franchin43ec8732014-03-31 15:00:14 +0100101 LIR* data_target = ScanLiteralPool(literal_list_, value, 0);
102 if (data_target == NULL) {
Andreas Gampef9879272014-06-18 23:19:07 -0700103 // Wide, as we need 8B alignment.
104 data_target = AddWideData(&literal_list_, value, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100105 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100106
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100107 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100108 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, kA64Ldr2fp,
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100109 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100110 AppendLIR(load_pc_rel);
111 return load_pc_rel;
112}
113
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100114LIR* Arm64Mir2Lir::LoadFPConstantValueWide(RegStorage r_dest, int64_t value) {
115 DCHECK(r_dest.IsDouble());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100116 if (value == 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100117 return NewLIR2(kA64Fmov2Sx, r_dest.GetReg(), rxzr);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100118 } else {
119 int32_t encoded_imm = EncodeImmDouble(value);
120 if (encoded_imm >= 0) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100121 return NewLIR2(FWIDE(kA64Fmov2fI), r_dest.GetReg(), encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100122 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100123 }
124
125 // No short form - load from the literal pool.
126 int32_t val_lo = Low32Bits(value);
127 int32_t val_hi = High32Bits(value);
128 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
129 if (data_target == NULL) {
130 data_target = AddWideData(&literal_list_, val_lo, val_hi);
131 }
132
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100133 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100134 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, FWIDE(kA64Ldr2fp),
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100135 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100136 AppendLIR(load_pc_rel);
137 return load_pc_rel;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100138}
139
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100140static int CountLeadingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100141 return (is_wide) ? __builtin_clzll(value) : __builtin_clz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100142}
Matteo Franchin43ec8732014-03-31 15:00:14 +0100143
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100144static int CountTrailingZeros(bool is_wide, uint64_t value) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100145 return (is_wide) ? __builtin_ctzll(value) : __builtin_ctz((uint32_t)value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100146}
147
148static int CountSetBits(bool is_wide, uint64_t value) {
149 return ((is_wide) ?
Zheng Xue2eb29e2014-06-12 10:22:33 +0800150 __builtin_popcountll(value) : __builtin_popcount((uint32_t)value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100151}
152
153/**
154 * @brief Try encoding an immediate in the form required by logical instructions.
155 *
156 * @param is_wide Whether @p value is a 64-bit (as opposed to 32-bit) value.
157 * @param value An integer to be encoded. This is interpreted as 64-bit if @p is_wide is true and as
158 * 32-bit if @p is_wide is false.
159 * @return A non-negative integer containing the encoded immediate or -1 if the encoding failed.
160 * @note This is the inverse of Arm64Mir2Lir::DecodeLogicalImmediate().
161 */
162int Arm64Mir2Lir::EncodeLogicalImmediate(bool is_wide, uint64_t value) {
163 unsigned n, imm_s, imm_r;
164
165 // Logical immediates are encoded using parameters n, imm_s and imm_r using
166 // the following table:
167 //
168 // N imms immr size S R
169 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
170 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
171 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
172 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
173 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
174 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
175 // (s bits must not be all set)
176 //
177 // A pattern is constructed of size bits, where the least significant S+1
178 // bits are set. The pattern is rotated right by R, and repeated across a
179 // 32 or 64-bit value, depending on destination register width.
180 //
181 // To test if an arbitary immediate can be encoded using this scheme, an
182 // iterative algorithm is used.
183 //
184
185 // 1. If the value has all set or all clear bits, it can't be encoded.
186 if (value == 0 || value == ~UINT64_C(0) ||
187 (!is_wide && (uint32_t)value == ~UINT32_C(0))) {
188 return -1;
189 }
190
191 unsigned lead_zero = CountLeadingZeros(is_wide, value);
192 unsigned lead_one = CountLeadingZeros(is_wide, ~value);
193 unsigned trail_zero = CountTrailingZeros(is_wide, value);
194 unsigned trail_one = CountTrailingZeros(is_wide, ~value);
195 unsigned set_bits = CountSetBits(is_wide, value);
196
197 // The fixed bits in the immediate s field.
198 // If width == 64 (X reg), start at 0xFFFFFF80.
199 // If width == 32 (W reg), start at 0xFFFFFFC0, as the iteration for 64-bit
200 // widths won't be executed.
201 unsigned width = (is_wide) ? 64 : 32;
202 int imm_s_fixed = (is_wide) ? -128 : -64;
203 int imm_s_mask = 0x3f;
204
205 for (;;) {
206 // 2. If the value is two bits wide, it can be encoded.
207 if (width == 2) {
208 n = 0;
209 imm_s = 0x3C;
210 imm_r = (value & 3) - 1;
211 break;
212 }
213
214 n = (width == 64) ? 1 : 0;
215 imm_s = ((imm_s_fixed | (set_bits - 1)) & imm_s_mask);
216 if ((lead_zero + set_bits) == width) {
217 imm_r = 0;
218 } else {
219 imm_r = (lead_zero > 0) ? (width - trail_zero) : lead_one;
220 }
221
222 // 3. If the sum of leading zeros, trailing zeros and set bits is
223 // equal to the bit width of the value, it can be encoded.
224 if (lead_zero + trail_zero + set_bits == width) {
225 break;
226 }
227
228 // 4. If the sum of leading ones, trailing ones and unset bits in the
229 // value is equal to the bit width of the value, it can be encoded.
230 if (lead_one + trail_one + (width - set_bits) == width) {
231 break;
232 }
233
234 // 5. If the most-significant half of the bitwise value is equal to
235 // the least-significant half, return to step 2 using the
236 // least-significant half of the value.
237 uint64_t mask = (UINT64_C(1) << (width >> 1)) - 1;
238 if ((value & mask) == ((value >> (width >> 1)) & mask)) {
239 width >>= 1;
240 set_bits >>= 1;
241 imm_s_fixed >>= 1;
242 continue;
243 }
244
245 // 6. Otherwise, the value can't be encoded.
246 return -1;
247 }
248
249 return (n << 12 | imm_r << 6 | imm_s);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100250}
251
252bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100253 return false; // (ModifiedImmediate(value) >= 0) || (ModifiedImmediate(~value) >= 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100254}
255
256bool Arm64Mir2Lir::InexpensiveConstantFloat(int32_t value) {
257 return EncodeImmSingle(value) >= 0;
258}
259
260bool Arm64Mir2Lir::InexpensiveConstantLong(int64_t value) {
261 return InexpensiveConstantInt(High32Bits(value)) && InexpensiveConstantInt(Low32Bits(value));
262}
263
264bool Arm64Mir2Lir::InexpensiveConstantDouble(int64_t value) {
265 return EncodeImmDouble(value) >= 0;
266}
267
268/*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100269 * Load a immediate using one single instruction when possible; otherwise
270 * use a pair of movz and movk instructions.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100271 *
272 * No additional register clobbering operation performed. Use this version when
273 * 1) r_dest is freshly returned from AllocTemp or
274 * 2) The codegen is under fixed register usage
275 */
276LIR* Arm64Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
277 LIR* res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100278
279 if (r_dest.IsFloat()) {
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100280 return LoadFPConstantValue(r_dest, value);
281 }
282
283 if (r_dest.Is64Bit()) {
284 return LoadConstantWide(r_dest, value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100285 }
286
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100287 // Loading SP/ZR with an immediate is not supported.
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100288 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
289 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100290
291 // Compute how many movk, movz instructions are needed to load the value.
292 uint16_t high_bits = High16Bits(value);
293 uint16_t low_bits = Low16Bits(value);
294
295 bool low_fast = ((uint16_t)(low_bits + 1) <= 1);
296 bool high_fast = ((uint16_t)(high_bits + 1) <= 1);
297
298 if (LIKELY(low_fast || high_fast)) {
299 // 1 instruction is enough to load the immediate.
300 if (LIKELY(low_bits == high_bits)) {
301 // Value is either 0 or -1: we can just use wzr.
302 ArmOpcode opcode = LIKELY(low_bits == 0) ? kA64Mov2rr : kA64Mvn2rr;
303 res = NewLIR2(opcode, r_dest.GetReg(), rwzr);
304 } else {
305 uint16_t uniform_bits, useful_bits;
306 int shift;
307
308 if (LIKELY(high_fast)) {
309 shift = 0;
310 uniform_bits = high_bits;
311 useful_bits = low_bits;
312 } else {
313 shift = 1;
314 uniform_bits = low_bits;
315 useful_bits = high_bits;
316 }
317
318 if (UNLIKELY(uniform_bits != 0)) {
319 res = NewLIR3(kA64Movn3rdM, r_dest.GetReg(), ~useful_bits, shift);
320 } else {
321 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), useful_bits, shift);
322 }
323 }
324 } else {
325 // movk, movz require 2 instructions. Try detecting logical immediates.
326 int log_imm = EncodeLogicalImmediate(/*is_wide=*/false, value);
327 if (log_imm >= 0) {
328 res = NewLIR3(kA64Orr3Rrl, r_dest.GetReg(), rwzr, log_imm);
329 } else {
330 // Use 2 instructions.
331 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), low_bits, 0);
332 NewLIR3(kA64Movk3rdM, r_dest.GetReg(), high_bits, 1);
333 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100334 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100335
Matteo Franchin43ec8732014-03-31 15:00:14 +0100336 return res;
337}
338
Matteo Franchinc41e6dc2014-06-13 19:16:28 +0100339// TODO: clean up the names. LoadConstantWide() should really be LoadConstantNoClobberWide().
340LIR* Arm64Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
341 // Maximum number of instructions to use for encoding the immediate.
342 const int max_num_ops = 2;
343
344 if (r_dest.IsFloat()) {
345 return LoadFPConstantValueWide(r_dest, value);
346 }
347
348 DCHECK(r_dest.Is64Bit());
349
350 // Loading SP/ZR with an immediate is not supported.
351 DCHECK(!A64_REG_IS_SP(r_dest.GetReg()));
352 DCHECK(!A64_REG_IS_ZR(r_dest.GetReg()));
353
354 if (LIKELY(value == INT64_C(0) || value == INT64_C(-1))) {
355 // value is either 0 or -1: we can just use xzr.
356 ArmOpcode opcode = LIKELY(value == 0) ? WIDE(kA64Mov2rr) : WIDE(kA64Mvn2rr);
357 return NewLIR2(opcode, r_dest.GetReg(), rxzr);
358 }
359
360 // At least one in value's halfwords is not 0x0, nor 0xffff: find out how many.
361 int num_0000_halfwords = 0;
362 int num_ffff_halfwords = 0;
363 uint64_t uvalue = static_cast<uint64_t>(value);
364 for (int shift = 0; shift < 64; shift += 16) {
365 uint16_t halfword = static_cast<uint16_t>(uvalue >> shift);
366 if (halfword == 0)
367 num_0000_halfwords++;
368 else if (halfword == UINT16_C(0xffff))
369 num_ffff_halfwords++;
370 }
371 int num_fast_halfwords = std::max(num_0000_halfwords, num_ffff_halfwords);
372
373 if (num_fast_halfwords < 3) {
374 // A single movz/movn is not enough. Try the logical immediate route.
375 int log_imm = EncodeLogicalImmediate(/*is_wide=*/true, value);
376 if (log_imm >= 0) {
377 return NewLIR3(WIDE(kA64Orr3Rrl), r_dest.GetReg(), rxzr, log_imm);
378 }
379 }
380
381 if (num_fast_halfwords >= 4 - max_num_ops) {
382 // We can encode the number using a movz/movn followed by one or more movk.
383 ArmOpcode op;
384 uint16_t background;
385 LIR* res = nullptr;
386
387 // Decide whether to use a movz or a movn.
388 if (num_0000_halfwords >= num_ffff_halfwords) {
389 op = WIDE(kA64Movz3rdM);
390 background = 0;
391 } else {
392 op = WIDE(kA64Movn3rdM);
393 background = 0xffff;
394 }
395
396 // Emit the first instruction (movz, movn).
397 int shift;
398 for (shift = 0; shift < 4; shift++) {
399 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
400 if (halfword != background) {
401 res = NewLIR3(op, r_dest.GetReg(), halfword ^ background, shift);
402 break;
403 }
404 }
405
406 // Emit the movk instructions.
407 for (shift++; shift < 4; shift++) {
408 uint16_t halfword = static_cast<uint16_t>(uvalue >> (shift << 4));
409 if (halfword != background) {
410 NewLIR3(WIDE(kA64Movk3rdM), r_dest.GetReg(), halfword, shift);
411 }
412 }
413 return res;
414 }
415
416 // Use the literal pool.
417 int32_t val_lo = Low32Bits(value);
418 int32_t val_hi = High32Bits(value);
419 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
420 if (data_target == NULL) {
421 data_target = AddWideData(&literal_list_, val_lo, val_hi);
422 }
423
424 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
425 LIR *res = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp),
426 r_dest.GetReg(), 0, 0, 0, 0, data_target);
427 AppendLIR(res);
428 return res;
429}
430
Matteo Franchin43ec8732014-03-31 15:00:14 +0100431LIR* Arm64Mir2Lir::OpUnconditionalBranch(LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100432 LIR* res = NewLIR1(kA64B1t, 0 /* offset to be patched during assembly */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100433 res->target = target;
434 return res;
435}
436
437LIR* Arm64Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100438 LIR* branch = NewLIR2(kA64B2ct, ArmConditionEncoding(cc),
439 0 /* offset to be patched */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100440 branch->target = target;
441 return branch;
442}
443
444LIR* Arm64Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100445 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100446 switch (op) {
447 case kOpBlx:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100448 opcode = kA64Blr1x;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100449 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100450 // TODO(Arm64): port kThumbBx.
451 // case kOpBx:
452 // opcode = kThumbBx;
453 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100454 default:
455 LOG(FATAL) << "Bad opcode " << op;
456 }
457 return NewLIR1(opcode, r_dest_src.GetReg());
458}
459
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100460LIR* Arm64Mir2Lir::OpRegRegShift(OpKind op, RegStorage r_dest_src1, RegStorage r_src2, int shift) {
461 ArmOpcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
462 CHECK_EQ(r_dest_src1.Is64Bit(), r_src2.Is64Bit());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100463 ArmOpcode opcode = kA64Brk1d;
464
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100465 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100466 case kOpCmn:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100467 opcode = kA64Cmn3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100468 break;
469 case kOpCmp:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100470 opcode = kA64Cmp3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100471 break;
472 case kOpMov:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100473 opcode = kA64Mov2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100474 break;
475 case kOpMvn:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100476 opcode = kA64Mvn2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100477 break;
478 case kOpNeg:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100479 opcode = kA64Neg3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100480 break;
481 case kOpTst:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100482 opcode = kA64Tst3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100483 break;
484 case kOpRev:
485 DCHECK_EQ(shift, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100486 // Binary, but rm is encoded twice.
Serban Constantinescu169489b2014-06-11 16:43:35 +0100487 return NewLIR2(kA64Rev2rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100488 break;
489 case kOpRevsh:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100490 // Binary, but rm is encoded twice.
Zheng Xua3fe7422014-07-09 14:03:15 +0800491 NewLIR2(kA64Rev162rr | wide, r_dest_src1.GetReg(), r_src2.GetReg());
492 // "sxth r1, r2" is "sbfm r1, r2, #0, #15"
493 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100494 break;
495 case kOp2Byte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100496 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
497 // "sbfx r1, r2, #imm1, #imm2" is "sbfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
498 // For now we use sbfm directly.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100499 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 7);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100500 case kOp2Short:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100501 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
502 // For now we use sbfm rather than its alias, sbfx.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100503 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100504 case kOp2Char:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100505 // "ubfx r1, r2, #imm1, #imm2" is "ubfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
506 // For now we use ubfm directly.
507 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100508 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100509 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100510 return OpRegRegRegShift(op, r_dest_src1, r_dest_src1, r_src2, shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100511 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100512
Matteo Franchin43ec8732014-03-31 15:00:14 +0100513 DCHECK(!IsPseudoLirOp(opcode));
514 if (EncodingMap[opcode].flags & IS_BINARY_OP) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100515 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100516 return NewLIR2(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100517 } else if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100518 ArmEncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100519 if (kind == kFmtShift) {
520 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100521 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100522 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100523
524 LOG(FATAL) << "Unexpected encoding operand count";
525 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100526}
527
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100528LIR* Arm64Mir2Lir::OpRegRegExtend(OpKind op, RegStorage r_dest_src1, RegStorage r_src2, int extend) {
529 ArmOpcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
530 ArmOpcode opcode = kA64Brk1d;
531
532 switch (op) {
533 case kOpCmn:
534 opcode = kA64Cmn3Rre;
535 break;
536 case kOpCmp:
537 opcode = kA64Cmp3Rre;
538 break;
539 default:
540 LOG(FATAL) << "Bad Opcode: " << opcode;
541 break;
542 }
543
544 DCHECK(!IsPseudoLirOp(opcode));
545 if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
546 ArmEncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
547 if (kind == kFmtExtend) {
548 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(), extend);
549 }
550 }
551
552 LOG(FATAL) << "Unexpected encoding operand count";
553 return NULL;
554}
555
Matteo Franchin43ec8732014-03-31 15:00:14 +0100556LIR* Arm64Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100557 /* RegReg operations with SP in first parameter need extended register instruction form.
558 * Only CMN and CMP instructions are implemented.
559 */
Zheng Xubaa7c882014-06-30 14:26:50 +0800560 if (r_dest_src1 == rs_sp) {
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100561 return OpRegRegExtend(op, r_dest_src1, r_src2, ENCODE_NO_EXTEND);
562 } else {
563 return OpRegRegShift(op, r_dest_src1, r_src2, ENCODE_NO_SHIFT);
564 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100565}
566
567LIR* Arm64Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type) {
568 UNIMPLEMENTED(FATAL);
569 return nullptr;
570}
571
572LIR* Arm64Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
573 UNIMPLEMENTED(FATAL);
574 return nullptr;
575}
576
577LIR* Arm64Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100578 LOG(FATAL) << "Unexpected use of OpCondRegReg for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100579 return NULL;
580}
581
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100582LIR* Arm64Mir2Lir::OpRegRegRegShift(OpKind op, RegStorage r_dest, RegStorage r_src1,
583 RegStorage r_src2, int shift) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100584 ArmOpcode opcode = kA64Brk1d;
585
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100586 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100587 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100588 opcode = kA64Add4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100589 break;
590 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100591 opcode = kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100592 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100593 // case kOpRsub:
594 // opcode = kA64RsubWWW;
595 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100596 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100597 opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100598 break;
599 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100600 opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100601 break;
602 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100603 opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100604 break;
605 case kOpMul:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100606 opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100607 break;
608 case kOpDiv:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100609 opcode = kA64Sdiv3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100610 break;
611 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100612 opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100613 break;
614 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100615 opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100616 break;
617 case kOpLsl:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100618 opcode = kA64Lsl3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100619 break;
620 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100621 opcode = kA64Lsr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100622 break;
623 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100624 opcode = kA64Asr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100625 break;
626 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100627 opcode = kA64Ror3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100628 break;
629 default:
630 LOG(FATAL) << "Bad opcode: " << op;
631 break;
632 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100633
634 // The instructions above belong to two kinds:
635 // - 4-operands instructions, where the last operand is a shift/extend immediate,
636 // - 3-operands instructions with no shift/extend.
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100637 ArmOpcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
638 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
639 CHECK_EQ(r_dest.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100640 if (EncodingMap[opcode].flags & IS_QUAD_OP) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100641 DCHECK(!IsExtendEncoding(shift));
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100642 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100643 } else {
644 DCHECK(EncodingMap[opcode].flags & IS_TERTIARY_OP);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100645 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100646 return NewLIR3(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100647 }
648}
649
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700650LIR* Arm64Mir2Lir::OpRegRegRegExtend(OpKind op, RegStorage r_dest, RegStorage r_src1,
651 RegStorage r_src2, A64RegExtEncodings ext, uint8_t amount) {
652 ArmOpcode opcode = kA64Brk1d;
653
654 switch (op) {
655 case kOpAdd:
656 opcode = kA64Add4RRre;
657 break;
658 case kOpSub:
659 opcode = kA64Sub4RRre;
660 break;
661 default:
662 LOG(FATAL) << "Unimplemented opcode: " << op;
663 break;
664 }
665 ArmOpcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
666
667 if (r_dest.Is64Bit()) {
668 CHECK(r_src1.Is64Bit());
669
670 // dest determines whether the op is wide or not. Up-convert src2 when necessary.
671 // Note: this is not according to aarch64 specifications, but our encoding.
672 if (!r_src2.Is64Bit()) {
673 r_src2 = As64BitReg(r_src2);
674 }
675 } else {
676 CHECK(!r_src1.Is64Bit());
677 CHECK(!r_src2.Is64Bit());
678 }
679
680 // Sanity checks.
681 // 1) Amount is in the range 0..4
682 CHECK_LE(amount, 4);
683
684 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(),
685 EncodeExtend(ext, amount));
686}
687
Matteo Franchin43ec8732014-03-31 15:00:14 +0100688LIR* Arm64Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100689 return OpRegRegRegShift(op, r_dest, r_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100690}
691
692LIR* Arm64Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800693 return OpRegRegImm64(op, r_dest, r_src1, static_cast<int64_t>(value));
694}
695
696LIR* Arm64Mir2Lir::OpRegRegImm64(OpKind op, RegStorage r_dest, RegStorage r_src1, int64_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100697 LIR* res;
698 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100699 int64_t abs_value = (neg) ? -value : value;
700 ArmOpcode opcode = kA64Brk1d;
701 ArmOpcode alt_opcode = kA64Brk1d;
702 int32_t log_imm = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100703 bool is_wide = r_dest.Is64Bit();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100704 ArmOpcode wide = (is_wide) ? WIDE(0) : UNWIDE(0);
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700705 int info = 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100706
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100707 switch (op) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100708 case kOpLsl: {
709 // "lsl w1, w2, #imm" is an alias of "ubfm w1, w2, #(-imm MOD 32), #(31-imm)"
Zheng Xu2d41a652014-06-09 11:05:31 +0800710 // and "lsl x1, x2, #imm" of "ubfm x1, x2, #(-imm MOD 64), #(63-imm)".
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100711 // For now, we just use ubfm directly.
Zheng Xu2d41a652014-06-09 11:05:31 +0800712 int max_value = (is_wide) ? 63 : 31;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100713 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest.GetReg(), r_src1.GetReg(),
Zheng Xu2d41a652014-06-09 11:05:31 +0800714 (-value) & max_value, max_value - value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100715 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100716 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100717 return NewLIR3(kA64Lsr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100718 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100719 return NewLIR3(kA64Asr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100720 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100721 // "ror r1, r2, #imm" is an alias of "extr r1, r2, r2, #imm".
722 // For now, we just use extr directly.
723 return NewLIR4(kA64Extr4rrrd | wide, r_dest.GetReg(), r_src1.GetReg(), r_src1.GetReg(),
724 value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100725 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100726 neg = !neg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 // Note: intentional fallthrough
728 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100729 // Add and sub below read/write sp rather than xzr.
730 if (abs_value < 0x1000) {
731 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
732 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value, 0);
733 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
734 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
735 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value >> 12, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100736 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100737 log_imm = -1;
Vladimir Marko903989d2014-07-01 17:21:18 +0100738 alt_opcode = (op == kOpAdd) ? kA64Add4RRre : kA64Sub4RRre;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700739 info = EncodeExtend(is_wide ? kA64Uxtx : kA64Uxtw, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100740 }
741 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100742 // case kOpRsub:
743 // opcode = kThumb2RsubRRI8M;
744 // alt_opcode = kThumb2RsubRRR;
745 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100746 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100747 log_imm = -1;
748 alt_opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100749 break;
750 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100751 log_imm = -1;
752 alt_opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100753 break;
754 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100755 log_imm = EncodeLogicalImmediate(is_wide, value);
756 opcode = kA64Orr3Rrl;
757 alt_opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100758 break;
759 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100760 log_imm = EncodeLogicalImmediate(is_wide, value);
761 opcode = kA64And3Rrl;
762 alt_opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100763 break;
764 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100765 log_imm = EncodeLogicalImmediate(is_wide, value);
766 opcode = kA64Eor3Rrl;
767 alt_opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100768 break;
769 case kOpMul:
770 // TUNING: power of 2, shift & add
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100771 log_imm = -1;
772 alt_opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100773 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100774 default:
775 LOG(FATAL) << "Bad opcode: " << op;
776 }
777
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100778 if (log_imm >= 0) {
779 return NewLIR3(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), log_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100780 } else {
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700781 RegStorage r_scratch;
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700782 if (is_wide) {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800783 r_scratch = AllocTempWide();
784 LoadConstantWide(r_scratch, value);
785 } else {
786 r_scratch = AllocTemp();
787 LoadConstant(r_scratch, value);
788 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100789 if (EncodingMap[alt_opcode].flags & IS_QUAD_OP)
Andreas Gampe9f975bf2014-06-18 17:45:32 -0700790 res = NewLIR4(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg(), info);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100791 else
Zheng Xue2eb29e2014-06-12 10:22:33 +0800792 res = NewLIR3(alt_opcode | wide, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100793 FreeTemp(r_scratch);
794 return res;
795 }
796}
797
Matteo Franchin43ec8732014-03-31 15:00:14 +0100798LIR* Arm64Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100799 return OpRegImm64(op, r_dest_src1, static_cast<int64_t>(value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100800}
801
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100802LIR* Arm64Mir2Lir::OpRegImm64(OpKind op, RegStorage r_dest_src1, int64_t value) {
803 ArmOpcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100804 ArmOpcode opcode = kA64Brk1d;
805 ArmOpcode neg_opcode = kA64Brk1d;
806 bool shift;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100807 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100808 uint64_t abs_value = (neg) ? -value : value;
809
810 if (LIKELY(abs_value < 0x1000)) {
811 // abs_value is a 12-bit immediate.
812 shift = false;
813 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
814 // abs_value is a shifted 12-bit immediate.
815 shift = true;
816 abs_value >>= 12;
Zheng Xue2eb29e2014-06-12 10:22:33 +0800817 } else if (LIKELY(abs_value < 0x1000000 && (op == kOpAdd || op == kOpSub))) {
818 // Note: It is better to use two ADD/SUB instead of loading a number to a temp register.
819 // This works for both normal registers and SP.
820 // For a frame size == 0x2468, it will be encoded as:
821 // sub sp, #0x2000
822 // sub sp, #0x468
823 if (neg) {
824 op = (op == kOpAdd) ? kOpSub : kOpAdd;
825 }
826 OpRegImm64(op, r_dest_src1, abs_value & (~INT64_C(0xfff)));
827 return OpRegImm64(op, r_dest_src1, abs_value & 0xfff);
828 } else if (LIKELY(A64_REG_IS_SP(r_dest_src1.GetReg()) && (op == kOpAdd || op == kOpSub))) {
829 // Note: "sub sp, sp, Xm" is not correct on arm64.
830 // We need special instructions for SP.
831 // Also operation on 32-bit SP should be avoided.
832 DCHECK(IS_WIDE(wide));
833 RegStorage r_tmp = AllocTempWide();
834 OpRegRegImm(kOpAdd, r_tmp, r_dest_src1, 0);
835 OpRegImm64(op, r_tmp, value);
836 return OpRegRegImm(kOpAdd, r_dest_src1, r_tmp, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100837 } else {
Zheng Xue2eb29e2014-06-12 10:22:33 +0800838 RegStorage r_tmp;
839 LIR* res;
840 if (IS_WIDE(wide)) {
841 r_tmp = AllocTempWide();
842 res = LoadConstantWide(r_tmp, value);
843 } else {
844 r_tmp = AllocTemp();
845 res = LoadConstant(r_tmp, value);
846 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100847 OpRegReg(op, r_dest_src1, r_tmp);
848 FreeTemp(r_tmp);
849 return res;
850 }
851
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100852 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100853 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100854 neg_opcode = kA64Sub4RRdT;
855 opcode = kA64Add4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100856 break;
857 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100858 neg_opcode = kA64Add4RRdT;
859 opcode = kA64Sub4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100860 break;
861 case kOpCmp:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100862 neg_opcode = kA64Cmn3RdT;
863 opcode = kA64Cmp3RdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100864 break;
865 default:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100866 LOG(FATAL) << "Bad op-kind in OpRegImm: " << op;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100867 break;
868 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100869
870 if (UNLIKELY(neg))
871 opcode = neg_opcode;
872
873 if (EncodingMap[opcode].flags & IS_QUAD_OP)
874 return NewLIR4(opcode | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), abs_value,
875 (shift) ? 1 : 0);
876 else
877 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), abs_value, (shift) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100878}
879
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100880int Arm64Mir2Lir::EncodeShift(int shift_type, int amount) {
Matteo Franchinc61b3c92014-06-18 11:52:47 +0100881 return ((shift_type & 0x3) << 7) | (amount & 0x3f);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100882}
883
884int Arm64Mir2Lir::EncodeExtend(int extend_type, int amount) {
885 return (1 << 6) | ((extend_type & 0x7) << 3) | (amount & 0x7);
886}
887
888bool Arm64Mir2Lir::IsExtendEncoding(int encoded_value) {
889 return ((1 << 6) & encoded_value) != 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100890}
891
892LIR* Arm64Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100893 int scale, OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100894 LIR* load;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100895 int expected_scale = 0;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100896 ArmOpcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -0700897 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100898
899 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
900 // register offset load (rather than doing the sign extension in a separate instruction).
901 if (r_index.Is32Bit()) {
902 // Assemble: ``sxtw xN, wN''.
903 r_index = As64BitReg(r_index);
904 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
905 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100906
907 if (r_dest.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100908 if (r_dest.IsDouble()) {
909 DCHECK(size == k64 || size == kDouble);
910 expected_scale = 3;
911 opcode = FWIDE(kA64Ldr4fXxG);
912 } else {
913 DCHECK(r_dest.IsSingle());
914 DCHECK(size == k32 || size == kSingle);
915 expected_scale = 2;
916 opcode = kA64Ldr4fXxG;
917 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100918
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100919 DCHECK(scale == 0 || scale == expected_scale);
920 return NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
921 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100922 }
923
924 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100925 case kDouble:
926 case kWord:
927 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +0000928 r_dest = Check64BitReg(r_dest);
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100929 opcode = WIDE(kA64Ldr4rXxG);
930 expected_scale = 3;
931 break;
Matteo Franchin255e0142014-07-04 13:50:41 +0100932 case kSingle: // Intentional fall-through.
933 case k32: // Intentional fall-through.
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100934 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +0000935 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100936 opcode = kA64Ldr4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100937 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100938 break;
939 case kUnsignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700940 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100941 opcode = kA64Ldrh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100942 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100943 break;
944 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700945 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100946 opcode = kA64Ldrsh4rXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100947 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100948 break;
949 case kUnsignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700950 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100951 opcode = kA64Ldrb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100952 break;
953 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -0700954 r_dest = Check32BitReg(r_dest);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100955 opcode = kA64Ldrsb3rXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100956 break;
957 default:
958 LOG(FATAL) << "Bad size: " << size;
959 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100960
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100961 if (UNLIKELY(expected_scale == 0)) {
962 // This is a tertiary op (e.g. ldrb, ldrsb), it does not not support scale.
963 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100964 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100965 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100966 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100967 DCHECK(scale == 0 || scale == expected_scale);
968 load = NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100969 (scale != 0) ? 1 : 0);
970 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100971
972 return load;
973}
974
Matteo Franchin255e0142014-07-04 13:50:41 +0100975LIR* Arm64Mir2Lir::LoadRefIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
976 int scale) {
977 return LoadBaseIndexed(r_base, r_index, As32BitReg(r_dest), scale, kReference);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000978}
979
Matteo Franchin43ec8732014-03-31 15:00:14 +0100980LIR* Arm64Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100981 int scale, OpSize size) {
982 LIR* store;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100983 int expected_scale = 0;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100984 ArmOpcode opcode = kA64Brk1d;
Andreas Gampe4b537a82014-06-30 22:24:53 -0700985 r_base = Check64BitReg(r_base);
Serban Constantinescu63fe93d2014-06-30 17:10:28 +0100986
987 // TODO(Arm64): The sign extension of r_index should be carried out by using an extended
988 // register offset store (rather than doing the sign extension in a separate instruction).
989 if (r_index.Is32Bit()) {
990 // Assemble: ``sxtw xN, wN''.
991 r_index = As64BitReg(r_index);
992 NewLIR4(WIDE(kA64Sbfm4rrdd), r_index.GetReg(), r_index.GetReg(), 0, 31);
993 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100994
995 if (r_src.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100996 if (r_src.IsDouble()) {
997 DCHECK(size == k64 || size == kDouble);
998 expected_scale = 3;
999 opcode = FWIDE(kA64Str4fXxG);
1000 } else {
1001 DCHECK(r_src.IsSingle());
1002 DCHECK(size == k32 || size == kSingle);
1003 expected_scale = 2;
1004 opcode = kA64Str4fXxG;
1005 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001006
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001007 DCHECK(scale == 0 || scale == expected_scale);
1008 return NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1009 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001010 }
1011
1012 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001013 case kDouble: // Intentional fall-trough.
1014 case kWord: // Intentional fall-trough.
1015 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001016 r_src = Check64BitReg(r_src);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001017 opcode = WIDE(kA64Str4rXxG);
1018 expected_scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001019 break;
1020 case kSingle: // Intentional fall-trough.
1021 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001022 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001023 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001024 opcode = kA64Str4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001025 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001026 break;
1027 case kUnsignedHalf:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001028 case kSignedHalf:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001030 opcode = kA64Strh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001031 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001032 break;
1033 case kUnsignedByte:
Matteo Franchin43ec8732014-03-31 15:00:14 +01001034 case kSignedByte:
Andreas Gampe4b537a82014-06-30 22:24:53 -07001035 r_src = Check32BitReg(r_src);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001036 opcode = kA64Strb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001037 break;
1038 default:
1039 LOG(FATAL) << "Bad size: " << size;
1040 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001041
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001042 if (UNLIKELY(expected_scale == 0)) {
1043 // This is a tertiary op (e.g. strb), it does not not support scale.
1044 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001045 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001046 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001047 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001048 store = NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
1049 (scale != 0) ? 1 : 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001050 }
Matteo Franchin43ec8732014-03-31 15:00:14 +01001051
1052 return store;
1053}
1054
Matteo Franchin255e0142014-07-04 13:50:41 +01001055LIR* Arm64Mir2Lir::StoreRefIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
1056 int scale) {
1057 return StoreBaseIndexed(r_base, r_index, As32BitReg(r_src), scale, kReference);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001058}
1059
Matteo Franchin43ec8732014-03-31 15:00:14 +01001060/*
1061 * Load value from base + displacement. Optionally perform null check
1062 * on base (which must have an associated s_reg and MIR). If not
1063 * performing null check, incoming MIR can be null.
1064 */
1065LIR* Arm64Mir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001066 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001067 LIR* load = NULL;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001068 ArmOpcode opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001069 ArmOpcode alt_opcode = kA64Brk1d;
1070 int scale = 0;
1071
Matteo Franchin43ec8732014-03-31 15:00:14 +01001072 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001073 case kDouble: // Intentional fall-through.
1074 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001075 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001076 r_dest = Check64BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001077 scale = 3;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001078 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001079 DCHECK(r_dest.IsDouble());
1080 opcode = FWIDE(kA64Ldr3fXD);
1081 alt_opcode = FWIDE(kA64Ldur3fXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001082 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +01001083 opcode = WIDE(kA64Ldr3rXD);
1084 alt_opcode = WIDE(kA64Ldur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001085 }
1086 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001087 case kSingle: // Intentional fall-through.
1088 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001089 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001090 r_dest = Check32BitReg(r_dest);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001091 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001092 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001093 DCHECK(r_dest.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001094 opcode = kA64Ldr3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001095 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001096 opcode = kA64Ldr3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001097 }
1098 break;
1099 case kUnsignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001100 scale = 1;
1101 opcode = kA64Ldrh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001102 break;
1103 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001104 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001105 opcode = kA64Ldrsh3rXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001106 break;
1107 case kUnsignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001108 opcode = kA64Ldrb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001109 break;
1110 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001111 opcode = kA64Ldrsb3rXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001112 break;
1113 default:
1114 LOG(FATAL) << "Bad size: " << size;
1115 }
1116
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001117 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1118 int scaled_disp = displacement >> scale;
1119 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1120 // Can use scaled load.
1121 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), scaled_disp);
1122 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1123 // Can use unscaled load.
1124 load = NewLIR3(alt_opcode, r_dest.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001125 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001126 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001127 // TODO: cleaner support for index/displacement registers? Not a reference, but must match width.
1128 RegStorage r_scratch = AllocTempWide();
1129 LoadConstantWide(r_scratch, displacement);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001130 load = LoadBaseIndexed(r_base, r_scratch, r_dest, 0, size);
1131 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001132 }
1133
1134 // TODO: in future may need to differentiate Dalvik accesses w/ spills
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001135 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Zheng Xubaa7c882014-06-30 14:26:50 +08001136 DCHECK(r_base == rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001137 AnnotateDalvikRegAccess(load, displacement >> 2, true /* is_load */, r_dest.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001138 }
1139 return load;
1140}
1141
Andreas Gampe3c12c512014-06-24 18:46:29 +00001142LIR* Arm64Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1143 OpSize size, VolatileKind is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +01001144 // LoadBaseDisp() will emit correct insn for atomic load on arm64
1145 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001146
1147 LIR* load = LoadBaseDispBody(r_base, displacement, r_dest, size);
1148
1149 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001150 // TODO: This should generate an acquire load instead of the barrier.
1151 GenMemBarrier(kLoadAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001152 }
1153
1154 return load;
Vladimir Marko674744e2014-04-24 15:18:26 +01001155}
1156
Andreas Gampe3c12c512014-06-24 18:46:29 +00001157LIR* Arm64Mir2Lir::LoadRefDisp(RegStorage r_base, int displacement, RegStorage r_dest,
1158 VolatileKind is_volatile) {
1159 return LoadBaseDisp(r_base, displacement, As32BitReg(r_dest), kReference, is_volatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001160}
1161
Matteo Franchin43ec8732014-03-31 15:00:14 +01001162LIR* Arm64Mir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001163 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001164 LIR* store = NULL;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001165 ArmOpcode opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001166 ArmOpcode alt_opcode = kA64Brk1d;
1167 int scale = 0;
1168
Matteo Franchin43ec8732014-03-31 15:00:14 +01001169 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001170 case kDouble: // Intentional fall-through.
1171 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +01001172 case k64:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001173 r_src = Check64BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001174 scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001175 if (r_src.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001176 DCHECK(r_src.IsDouble());
1177 opcode = FWIDE(kA64Str3fXD);
1178 alt_opcode = FWIDE(kA64Stur3fXd);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001179 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001180 opcode = FWIDE(kA64Str3rXD);
1181 alt_opcode = FWIDE(kA64Stur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001182 }
1183 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001184 case kSingle: // Intentional fall-through.
1185 case k32: // Intentional fall-trough.
Matteo Franchin255e0142014-07-04 13:50:41 +01001186 case kReference:
Andreas Gampe3c12c512014-06-24 18:46:29 +00001187 r_src = Check32BitReg(r_src);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001188 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001189 if (r_src.IsFloat()) {
1190 DCHECK(r_src.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001191 opcode = kA64Str3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001192 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001193 opcode = kA64Str3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001194 }
1195 break;
1196 case kUnsignedHalf:
1197 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001198 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001199 opcode = kA64Strh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001200 break;
1201 case kUnsignedByte:
1202 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001203 opcode = kA64Strb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001204 break;
1205 default:
1206 LOG(FATAL) << "Bad size: " << size;
1207 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001208
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001209 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
1210 int scaled_disp = displacement >> scale;
1211 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
1212 // Can use scaled store.
1213 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), scaled_disp);
1214 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
1215 // Can use unscaled store.
1216 store = NewLIR3(alt_opcode, r_src.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001217 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001218 // Use long sequence.
buzbee33ae5582014-06-12 14:56:32 -07001219 RegStorage r_scratch = AllocTempWide();
1220 LoadConstantWide(r_scratch, displacement);
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001221 store = StoreBaseIndexed(r_base, r_scratch, r_src, 0, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001222 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001223 }
1224
Matteo Franchinbc6d1972014-05-13 12:33:28 +01001225 // TODO: In future, may need to differentiate Dalvik & spill accesses.
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001226 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Zheng Xubaa7c882014-06-30 14:26:50 +08001227 DCHECK(r_base == rs_sp);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001228 AnnotateDalvikRegAccess(store, displacement >> 2, false /* is_load */, r_src.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +01001229 }
1230 return store;
1231}
1232
Andreas Gampe3c12c512014-06-24 18:46:29 +00001233LIR* Arm64Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
1234 OpSize size, VolatileKind is_volatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001235 // TODO: This should generate a release store and no barriers.
Andreas Gampe3c12c512014-06-24 18:46:29 +00001236 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001237 // Ensure that prior accesses become visible to other threads first.
1238 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001239 }
1240
Vladimir Marko674744e2014-04-24 15:18:26 +01001241 // StoreBaseDisp() will emit correct insn for atomic store on arm64
1242 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
Andreas Gampe3c12c512014-06-24 18:46:29 +00001243
1244 LIR* store = StoreBaseDispBody(r_base, displacement, r_src, size);
1245
1246 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -07001247 // Preserve order with respect to any subsequent volatile loads.
1248 // We need StoreLoad, but that generally requires the most expensive barrier.
1249 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +00001250 }
1251
1252 return store;
Vladimir Marko674744e2014-04-24 15:18:26 +01001253}
1254
Andreas Gampe3c12c512014-06-24 18:46:29 +00001255LIR* Arm64Mir2Lir::StoreRefDisp(RegStorage r_base, int displacement, RegStorage r_src,
1256 VolatileKind is_volatile) {
1257 return StoreBaseDisp(r_base, displacement, As32BitReg(r_src), kReference, is_volatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001258}
1259
Matteo Franchin43ec8732014-03-31 15:00:14 +01001260LIR* Arm64Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001261 LOG(FATAL) << "Unexpected use of OpFpRegCopy for Arm64";
1262 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001263}
1264
Matteo Franchin43ec8732014-03-31 15:00:14 +01001265LIR* Arm64Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001266 LOG(FATAL) << "Unexpected use of OpMem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001267 return NULL;
1268}
1269
Andreas Gampe98430592014-07-27 19:44:50 -07001270LIR* Arm64Mir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) {
1271 return OpReg(op, r_tgt);
Matteo Franchin43ec8732014-03-31 15:00:14 +01001272}
1273
1274} // namespace art