blob: ab5014f32babaa9c6fb2ab8257f1572dca848117 [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"
20
21namespace art {
22
Matteo Franchine45fb9e2014-05-06 10:10:30 +010023/* This file contains codegen for the A64 ISA. */
Matteo Franchin43ec8732014-03-31 15:00:14 +010024
Matteo Franchine45fb9e2014-05-06 10:10:30 +010025static int32_t EncodeImmSingle(uint32_t bits) {
26 /*
27 * Valid values will have the form:
28 *
29 * aBbb.bbbc.defg.h000.0000.0000.0000.0000
30 *
31 * where B = not(b). In other words, if b == 1, then B == 0 and viceversa.
32 */
33
34 // bits[19..0] are cleared.
35 if ((bits & 0x0007ffff) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010036 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010037
38 // bits[29..25] are all set or all cleared.
39 uint32_t b_pattern = (bits >> 16) & 0x3e00;
40 if (b_pattern != 0 && b_pattern != 0x3e00)
41 return -1;
42
43 // bit[30] and bit[29] are opposite.
44 if (((bits ^ (bits << 1)) & 0x40000000) == 0)
45 return -1;
46
47 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
48 // bit7: a000.0000
49 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
50 // bit6: 0b00.0000
51 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
52 // bit5_to_0: 00cd.efgh
53 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
54 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010055}
56
Matteo Franchine45fb9e2014-05-06 10:10:30 +010057static int32_t EncodeImmDouble(uint64_t bits) {
58 /*
59 * Valid values will have the form:
60 *
61 * aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
62 * 0000.0000.0000.0000.0000.0000.0000.0000
63 *
64 * where B = not(b).
65 */
66
67 // bits[47..0] are cleared.
68 if ((bits & UINT64_C(0xffffffffffff)) != 0)
Matteo Franchin43ec8732014-03-31 15:00:14 +010069 return -1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +010070
71 // bits[61..54] are all set or all cleared.
72 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
73 if (b_pattern != 0 && b_pattern != 0x3fc0)
74 return -1;
75
76 // bit[62] and bit[61] are opposite.
77 if (((bits ^ (bits << 1)) & UINT64_C(0x4000000000000000)) == 0)
78 return -1;
79
80 // bit7: a000.0000
81 uint32_t bit7 = ((bits >> 63) & 0x1) << 7;
82 // bit6: 0b00.0000
83 uint32_t bit6 = ((bits >> 61) & 0x1) << 6;
84 // bit5_to_0: 00cd.efgh
85 uint32_t bit5_to_0 = (bits >> 48) & 0x3f;
86 return (bit7 | bit6 | bit5_to_0);
Matteo Franchin43ec8732014-03-31 15:00:14 +010087}
88
Matteo Franchine45fb9e2014-05-06 10:10:30 +010089LIR* Arm64Mir2Lir::LoadFPConstantValue(int r_dest, int32_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +010090 DCHECK(RegStorage::IsSingle(r_dest));
91 if (value == 0) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010092 return NewLIR2(kA64Fmov2sw, r_dest, rwzr);
Matteo Franchin43ec8732014-03-31 15:00:14 +010093 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010094 int32_t encoded_imm = EncodeImmSingle((uint32_t)value);
Matteo Franchin43ec8732014-03-31 15:00:14 +010095 if (encoded_imm >= 0) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +010096 return NewLIR2(kA64Fmov2fI, r_dest, encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +010097 }
98 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +010099
Matteo Franchin43ec8732014-03-31 15:00:14 +0100100 LIR* data_target = ScanLiteralPool(literal_list_, value, 0);
101 if (data_target == NULL) {
102 data_target = AddWordData(&literal_list_, value);
103 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100104
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100105 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100106 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, kA64Ldr2fp,
107 r_dest, 0, 0, 0, 0, data_target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100108 AppendLIR(load_pc_rel);
109 return load_pc_rel;
110}
111
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100112LIR* Arm64Mir2Lir::LoadFPConstantValueWide(int r_dest, int64_t value) {
113 DCHECK(RegStorage::IsDouble(r_dest));
114 if (value == 0) {
115 return NewLIR2(kA64Fmov2Sx, r_dest, rwzr);
116 } else {
117 int32_t encoded_imm = EncodeImmDouble(value);
118 if (encoded_imm >= 0) {
119 return NewLIR2(FWIDE(kA64Fmov2fI), r_dest, encoded_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100120 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100121 }
122
123 // No short form - load from the literal pool.
124 int32_t val_lo = Low32Bits(value);
125 int32_t val_hi = High32Bits(value);
126 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
127 if (data_target == NULL) {
128 data_target = AddWideData(&literal_list_, val_lo, val_hi);
129 }
130
131 DCHECK(RegStorage::IsFloat(r_dest));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100132 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100133 LIR* load_pc_rel = RawLIR(current_dalvik_offset_, FWIDE(kA64Ldr2fp),
134 r_dest, 0, 0, 0, 0, data_target);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100135 AppendLIR(load_pc_rel);
136 return load_pc_rel;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100137}
138
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100139static int CountLeadingZeros(bool is_wide, uint64_t value) {
140 return (is_wide) ? __builtin_clzl(value) : __builtin_clz((uint32_t)value);
141}
Matteo Franchin43ec8732014-03-31 15:00:14 +0100142
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100143static int CountTrailingZeros(bool is_wide, uint64_t value) {
144 return (is_wide) ? __builtin_ctzl(value) : __builtin_ctz((uint32_t)value);
145}
146
147static int CountSetBits(bool is_wide, uint64_t value) {
148 return ((is_wide) ?
149 __builtin_popcountl(value) : __builtin_popcount((uint32_t)value));
150}
151
152/**
153 * @brief Try encoding an immediate in the form required by logical instructions.
154 *
155 * @param is_wide Whether @p value is a 64-bit (as opposed to 32-bit) value.
156 * @param value An integer to be encoded. This is interpreted as 64-bit if @p is_wide is true and as
157 * 32-bit if @p is_wide is false.
158 * @return A non-negative integer containing the encoded immediate or -1 if the encoding failed.
159 * @note This is the inverse of Arm64Mir2Lir::DecodeLogicalImmediate().
160 */
161int Arm64Mir2Lir::EncodeLogicalImmediate(bool is_wide, uint64_t value) {
162 unsigned n, imm_s, imm_r;
163
164 // Logical immediates are encoded using parameters n, imm_s and imm_r using
165 // the following table:
166 //
167 // N imms immr size S R
168 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
169 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
170 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
171 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
172 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
173 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
174 // (s bits must not be all set)
175 //
176 // A pattern is constructed of size bits, where the least significant S+1
177 // bits are set. The pattern is rotated right by R, and repeated across a
178 // 32 or 64-bit value, depending on destination register width.
179 //
180 // To test if an arbitary immediate can be encoded using this scheme, an
181 // iterative algorithm is used.
182 //
183
184 // 1. If the value has all set or all clear bits, it can't be encoded.
185 if (value == 0 || value == ~UINT64_C(0) ||
186 (!is_wide && (uint32_t)value == ~UINT32_C(0))) {
187 return -1;
188 }
189
190 unsigned lead_zero = CountLeadingZeros(is_wide, value);
191 unsigned lead_one = CountLeadingZeros(is_wide, ~value);
192 unsigned trail_zero = CountTrailingZeros(is_wide, value);
193 unsigned trail_one = CountTrailingZeros(is_wide, ~value);
194 unsigned set_bits = CountSetBits(is_wide, value);
195
196 // The fixed bits in the immediate s field.
197 // If width == 64 (X reg), start at 0xFFFFFF80.
198 // If width == 32 (W reg), start at 0xFFFFFFC0, as the iteration for 64-bit
199 // widths won't be executed.
200 unsigned width = (is_wide) ? 64 : 32;
201 int imm_s_fixed = (is_wide) ? -128 : -64;
202 int imm_s_mask = 0x3f;
203
204 for (;;) {
205 // 2. If the value is two bits wide, it can be encoded.
206 if (width == 2) {
207 n = 0;
208 imm_s = 0x3C;
209 imm_r = (value & 3) - 1;
210 break;
211 }
212
213 n = (width == 64) ? 1 : 0;
214 imm_s = ((imm_s_fixed | (set_bits - 1)) & imm_s_mask);
215 if ((lead_zero + set_bits) == width) {
216 imm_r = 0;
217 } else {
218 imm_r = (lead_zero > 0) ? (width - trail_zero) : lead_one;
219 }
220
221 // 3. If the sum of leading zeros, trailing zeros and set bits is
222 // equal to the bit width of the value, it can be encoded.
223 if (lead_zero + trail_zero + set_bits == width) {
224 break;
225 }
226
227 // 4. If the sum of leading ones, trailing ones and unset bits in the
228 // value is equal to the bit width of the value, it can be encoded.
229 if (lead_one + trail_one + (width - set_bits) == width) {
230 break;
231 }
232
233 // 5. If the most-significant half of the bitwise value is equal to
234 // the least-significant half, return to step 2 using the
235 // least-significant half of the value.
236 uint64_t mask = (UINT64_C(1) << (width >> 1)) - 1;
237 if ((value & mask) == ((value >> (width >> 1)) & mask)) {
238 width >>= 1;
239 set_bits >>= 1;
240 imm_s_fixed >>= 1;
241 continue;
242 }
243
244 // 6. Otherwise, the value can't be encoded.
245 return -1;
246 }
247
248 return (n << 12 | imm_r << 6 | imm_s);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100249}
250
251bool Arm64Mir2Lir::InexpensiveConstantInt(int32_t value) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100252 return false; // (ModifiedImmediate(value) >= 0) || (ModifiedImmediate(~value) >= 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100253}
254
255bool Arm64Mir2Lir::InexpensiveConstantFloat(int32_t value) {
256 return EncodeImmSingle(value) >= 0;
257}
258
259bool Arm64Mir2Lir::InexpensiveConstantLong(int64_t value) {
260 return InexpensiveConstantInt(High32Bits(value)) && InexpensiveConstantInt(Low32Bits(value));
261}
262
263bool Arm64Mir2Lir::InexpensiveConstantDouble(int64_t value) {
264 return EncodeImmDouble(value) >= 0;
265}
266
267/*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100268 * Load a immediate using one single instruction when possible; otherwise
269 * use a pair of movz and movk instructions.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100270 *
271 * No additional register clobbering operation performed. Use this version when
272 * 1) r_dest is freshly returned from AllocTemp or
273 * 2) The codegen is under fixed register usage
274 */
275LIR* Arm64Mir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
276 LIR* res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100277
278 if (r_dest.IsFloat()) {
279 return LoadFPConstantValue(r_dest.GetReg(), value);
280 }
281
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100282 // Loading SP/ZR with an immediate is not supported.
283 DCHECK_NE(r_dest.GetReg(), rwsp);
284 DCHECK_NE(r_dest.GetReg(), rwzr);
285
286 // Compute how many movk, movz instructions are needed to load the value.
287 uint16_t high_bits = High16Bits(value);
288 uint16_t low_bits = Low16Bits(value);
289
290 bool low_fast = ((uint16_t)(low_bits + 1) <= 1);
291 bool high_fast = ((uint16_t)(high_bits + 1) <= 1);
292
293 if (LIKELY(low_fast || high_fast)) {
294 // 1 instruction is enough to load the immediate.
295 if (LIKELY(low_bits == high_bits)) {
296 // Value is either 0 or -1: we can just use wzr.
297 ArmOpcode opcode = LIKELY(low_bits == 0) ? kA64Mov2rr : kA64Mvn2rr;
298 res = NewLIR2(opcode, r_dest.GetReg(), rwzr);
299 } else {
300 uint16_t uniform_bits, useful_bits;
301 int shift;
302
303 if (LIKELY(high_fast)) {
304 shift = 0;
305 uniform_bits = high_bits;
306 useful_bits = low_bits;
307 } else {
308 shift = 1;
309 uniform_bits = low_bits;
310 useful_bits = high_bits;
311 }
312
313 if (UNLIKELY(uniform_bits != 0)) {
314 res = NewLIR3(kA64Movn3rdM, r_dest.GetReg(), ~useful_bits, shift);
315 } else {
316 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), useful_bits, shift);
317 }
318 }
319 } else {
320 // movk, movz require 2 instructions. Try detecting logical immediates.
321 int log_imm = EncodeLogicalImmediate(/*is_wide=*/false, value);
322 if (log_imm >= 0) {
323 res = NewLIR3(kA64Orr3Rrl, r_dest.GetReg(), rwzr, log_imm);
324 } else {
325 // Use 2 instructions.
326 res = NewLIR3(kA64Movz3rdM, r_dest.GetReg(), low_bits, 0);
327 NewLIR3(kA64Movk3rdM, r_dest.GetReg(), high_bits, 1);
328 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100329 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100330
Matteo Franchin43ec8732014-03-31 15:00:14 +0100331 return res;
332}
333
334LIR* Arm64Mir2Lir::OpUnconditionalBranch(LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100335 LIR* res = NewLIR1(kA64B1t, 0 /* offset to be patched during assembly */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100336 res->target = target;
337 return res;
338}
339
340LIR* Arm64Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100341 LIR* branch = NewLIR2(kA64B2ct, ArmConditionEncoding(cc),
342 0 /* offset to be patched */);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100343 branch->target = target;
344 return branch;
345}
346
347LIR* Arm64Mir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100348 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100349 switch (op) {
350 case kOpBlx:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100351 opcode = kA64Blr1x;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100352 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100353 // TODO(Arm64): port kThumbBx.
354 // case kOpBx:
355 // opcode = kThumbBx;
356 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100357 default:
358 LOG(FATAL) << "Bad opcode " << op;
359 }
360 return NewLIR1(opcode, r_dest_src.GetReg());
361}
362
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100363LIR* Arm64Mir2Lir::OpRegRegShift(OpKind op, RegStorage r_dest_src1, RegStorage r_src2, int shift) {
364 ArmOpcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
365 CHECK_EQ(r_dest_src1.Is64Bit(), r_src2.Is64Bit());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100366 ArmOpcode opcode = kA64Brk1d;
367
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100368 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100369 case kOpCmn:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100370 opcode = kA64Cmn3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100371 break;
372 case kOpCmp:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100373 opcode = kA64Cmp3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100374 break;
375 case kOpMov:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100376 opcode = kA64Mov2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100377 break;
378 case kOpMvn:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100379 opcode = kA64Mvn2rr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100380 break;
381 case kOpNeg:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100382 opcode = kA64Neg3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100383 break;
384 case kOpTst:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100385 opcode = kA64Tst3rro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100386 break;
387 case kOpRev:
388 DCHECK_EQ(shift, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100389 // Binary, but rm is encoded twice.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100390 return NewLIR3(kA64Rev2rr | wide, r_dest_src1.GetReg(), r_src2.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100391 break;
392 case kOpRevsh:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100393 // Binary, but rm is encoded twice.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100394 return NewLIR3(kA64Rev162rr | wide, r_dest_src1.GetReg(), r_src2.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100395 break;
396 case kOp2Byte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100397 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
398 // "sbfx r1, r2, #imm1, #imm2" is "sbfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
399 // For now we use sbfm directly.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100400 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 7);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100401 case kOp2Short:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100402 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
403 // For now we use sbfm rather than its alias, sbfx.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100404 return NewLIR4(kA64Sbfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100405 case kOp2Char:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100406 // "ubfx r1, r2, #imm1, #imm2" is "ubfm r1, r2, #imm1, #(imm1 + imm2 - 1)".
407 // For now we use ubfm directly.
408 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100409 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest_src1.GetReg(), r_src2.GetReg(), 0, 15);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100410 default:
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100411 return OpRegRegRegShift(op, r_dest_src1, r_dest_src1, r_src2, shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100412 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100413
Matteo Franchin43ec8732014-03-31 15:00:14 +0100414 DCHECK(!IsPseudoLirOp(opcode));
415 if (EncodingMap[opcode].flags & IS_BINARY_OP) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100416 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100417 return NewLIR2(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100418 } else if (EncodingMap[opcode].flags & IS_TERTIARY_OP) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100419 ArmEncodingKind kind = EncodingMap[opcode].field_loc[2].kind;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100420 if (kind == kFmtShift) {
421 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100422 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100423 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100424
425 LOG(FATAL) << "Unexpected encoding operand count";
426 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100427}
428
429LIR* Arm64Mir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100430 return OpRegRegShift(op, r_dest_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100431}
432
433LIR* Arm64Mir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type) {
434 UNIMPLEMENTED(FATAL);
435 return nullptr;
436}
437
438LIR* Arm64Mir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
439 UNIMPLEMENTED(FATAL);
440 return nullptr;
441}
442
443LIR* Arm64Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100444 LOG(FATAL) << "Unexpected use of OpCondRegReg for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +0100445 return NULL;
446}
447
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100448LIR* Arm64Mir2Lir::OpRegRegRegShift(OpKind op, RegStorage r_dest, RegStorage r_src1,
449 RegStorage r_src2, int shift) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100450 ArmOpcode opcode = kA64Brk1d;
451
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100452 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100453 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100454 opcode = kA64Add4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100455 break;
456 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100457 opcode = kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100458 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100459 // case kOpRsub:
460 // opcode = kA64RsubWWW;
461 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100462 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100463 opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100464 break;
465 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100466 opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100467 break;
468 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100469 opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100470 break;
471 case kOpMul:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100472 opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100473 break;
474 case kOpDiv:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100475 opcode = kA64Sdiv3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100476 break;
477 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100478 opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100479 break;
480 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100481 opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100482 break;
483 case kOpLsl:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100484 opcode = kA64Lsl3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100485 break;
486 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100487 opcode = kA64Lsr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100488 break;
489 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100490 opcode = kA64Asr3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100491 break;
492 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100493 opcode = kA64Ror3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100494 break;
495 default:
496 LOG(FATAL) << "Bad opcode: " << op;
497 break;
498 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100499
500 // The instructions above belong to two kinds:
501 // - 4-operands instructions, where the last operand is a shift/extend immediate,
502 // - 3-operands instructions with no shift/extend.
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100503 ArmOpcode widened_opcode = r_dest.Is64Bit() ? WIDE(opcode) : opcode;
504 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
505 CHECK_EQ(r_dest.Is64Bit(), r_src2.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100506 if (EncodingMap[opcode].flags & IS_QUAD_OP) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100507 DCHECK(!IsExtendEncoding(shift));
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100508 return NewLIR4(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg(), shift);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100509 } else {
510 DCHECK(EncodingMap[opcode].flags & IS_TERTIARY_OP);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100511 DCHECK_EQ(shift, ENCODE_NO_SHIFT);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100512 return NewLIR3(widened_opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100513 }
514}
515
516LIR* Arm64Mir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100517 return OpRegRegRegShift(op, r_dest, r_src1, r_src2, ENCODE_NO_SHIFT);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100518}
519
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100520// Should be taking an int64_t value ?
Matteo Franchin43ec8732014-03-31 15:00:14 +0100521LIR* Arm64Mir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
522 LIR* res;
523 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100524 int64_t abs_value = (neg) ? -value : value;
525 ArmOpcode opcode = kA64Brk1d;
526 ArmOpcode alt_opcode = kA64Brk1d;
527 int32_t log_imm = -1;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100528 bool is_wide = r_dest.Is64Bit();
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100529 CHECK_EQ(r_dest.Is64Bit(), r_src1.Is64Bit());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100530 ArmOpcode wide = (is_wide) ? WIDE(0) : UNWIDE(0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100531
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100532 switch (op) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100533 case kOpLsl: {
534 // "lsl w1, w2, #imm" is an alias of "ubfm w1, w2, #(-imm MOD 32), #(31-imm)"
Zheng Xu2d41a652014-06-09 11:05:31 +0800535 // and "lsl x1, x2, #imm" of "ubfm x1, x2, #(-imm MOD 64), #(63-imm)".
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100536 // For now, we just use ubfm directly.
Zheng Xu2d41a652014-06-09 11:05:31 +0800537 int max_value = (is_wide) ? 63 : 31;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100538 return NewLIR4(kA64Ubfm4rrdd | wide, r_dest.GetReg(), r_src1.GetReg(),
Zheng Xu2d41a652014-06-09 11:05:31 +0800539 (-value) & max_value, max_value - value);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100540 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100541 case kOpLsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100542 return NewLIR3(kA64Lsr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100543 case kOpAsr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100544 return NewLIR3(kA64Asr3rrd | wide, r_dest.GetReg(), r_src1.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100545 case kOpRor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100546 // "ror r1, r2, #imm" is an alias of "extr r1, r2, r2, #imm".
547 // For now, we just use extr directly.
548 return NewLIR4(kA64Extr4rrrd | wide, r_dest.GetReg(), r_src1.GetReg(), r_src1.GetReg(),
549 value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100550 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100551 neg = !neg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100552 // Note: intentional fallthrough
553 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100554 // Add and sub below read/write sp rather than xzr.
555 if (abs_value < 0x1000) {
556 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
557 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value, 0);
558 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
559 opcode = (neg) ? kA64Add4RRdT : kA64Sub4RRdT;
560 return NewLIR4(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), abs_value >> 12, 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100561 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100562 log_imm = -1;
563 alt_opcode = (neg) ? kA64Add4rrro : kA64Sub4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100564 }
565 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100566 // case kOpRsub:
567 // opcode = kThumb2RsubRRI8M;
568 // alt_opcode = kThumb2RsubRRR;
569 // break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100570 case kOpAdc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100571 log_imm = -1;
572 alt_opcode = kA64Adc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100573 break;
574 case kOpSbc:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100575 log_imm = -1;
576 alt_opcode = kA64Sbc3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100577 break;
578 case kOpOr:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100579 log_imm = EncodeLogicalImmediate(is_wide, value);
580 opcode = kA64Orr3Rrl;
581 alt_opcode = kA64Orr4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100582 break;
583 case kOpAnd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100584 log_imm = EncodeLogicalImmediate(is_wide, value);
585 opcode = kA64And3Rrl;
586 alt_opcode = kA64And4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100587 break;
588 case kOpXor:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100589 log_imm = EncodeLogicalImmediate(is_wide, value);
590 opcode = kA64Eor3Rrl;
591 alt_opcode = kA64Eor4rrro;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100592 break;
593 case kOpMul:
594 // TUNING: power of 2, shift & add
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100595 log_imm = -1;
596 alt_opcode = kA64Mul3rrr;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100597 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100598 default:
599 LOG(FATAL) << "Bad opcode: " << op;
600 }
601
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100602 if (log_imm >= 0) {
603 return NewLIR3(opcode | wide, r_dest.GetReg(), r_src1.GetReg(), log_imm);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100604 } else {
605 RegStorage r_scratch = AllocTemp();
606 LoadConstant(r_scratch, value);
607 if (EncodingMap[alt_opcode].flags & IS_QUAD_OP)
608 res = NewLIR4(alt_opcode, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg(), 0);
609 else
610 res = NewLIR3(alt_opcode, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
611 FreeTemp(r_scratch);
612 return res;
613 }
614}
615
Matteo Franchin43ec8732014-03-31 15:00:14 +0100616LIR* Arm64Mir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100617 return OpRegImm64(op, r_dest_src1, static_cast<int64_t>(value));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100618}
619
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100620LIR* Arm64Mir2Lir::OpRegImm64(OpKind op, RegStorage r_dest_src1, int64_t value) {
621 ArmOpcode wide = (r_dest_src1.Is64Bit()) ? WIDE(0) : UNWIDE(0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100622 ArmOpcode opcode = kA64Brk1d;
623 ArmOpcode neg_opcode = kA64Brk1d;
624 bool shift;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100625 bool neg = (value < 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100626 uint64_t abs_value = (neg) ? -value : value;
627
628 if (LIKELY(abs_value < 0x1000)) {
629 // abs_value is a 12-bit immediate.
630 shift = false;
631 } else if ((abs_value & UINT64_C(0xfff)) == 0 && ((abs_value >> 12) < 0x1000)) {
632 // abs_value is a shifted 12-bit immediate.
633 shift = true;
634 abs_value >>= 12;
635 } else {
636 RegStorage r_tmp = AllocTemp();
637 LIR* res = LoadConstant(r_tmp, value);
638 OpRegReg(op, r_dest_src1, r_tmp);
639 FreeTemp(r_tmp);
640 return res;
641 }
642
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100643 switch (op) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100644 case kOpAdd:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100645 neg_opcode = kA64Sub4RRdT;
646 opcode = kA64Add4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100647 break;
648 case kOpSub:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100649 neg_opcode = kA64Add4RRdT;
650 opcode = kA64Sub4RRdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100651 break;
652 case kOpCmp:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100653 neg_opcode = kA64Cmn3RdT;
654 opcode = kA64Cmp3RdT;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100655 break;
656 default:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100657 LOG(FATAL) << "Bad op-kind in OpRegImm: " << op;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100658 break;
659 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100660
661 if (UNLIKELY(neg))
662 opcode = neg_opcode;
663
664 if (EncodingMap[opcode].flags & IS_QUAD_OP)
665 return NewLIR4(opcode | wide, r_dest_src1.GetReg(), r_dest_src1.GetReg(), abs_value,
666 (shift) ? 1 : 0);
667 else
668 return NewLIR3(opcode | wide, r_dest_src1.GetReg(), abs_value, (shift) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100669}
670
671LIR* Arm64Mir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100672 if (r_dest.IsFloat()) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100673 return LoadFPConstantValueWide(r_dest.GetReg(), value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100674 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100675 // TODO(Arm64): check whether we can load the immediate with a short form.
676 // e.g. via movz, movk or via logical immediate.
677
Matteo Franchin43ec8732014-03-31 15:00:14 +0100678 // No short form - load from the literal pool.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100679 int32_t val_lo = Low32Bits(value);
680 int32_t val_hi = High32Bits(value);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100681 LIR* data_target = ScanLiteralPoolWide(literal_list_, val_lo, val_hi);
682 if (data_target == NULL) {
683 data_target = AddWideData(&literal_list_, val_lo, val_hi);
684 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100685
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100686 ScopedMemRefType mem_ref_type(this, ResourceMask::kLiteral);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100687 LIR* res = RawLIR(current_dalvik_offset_, WIDE(kA64Ldr2rp),
688 r_dest.GetReg(), 0, 0, 0, 0, data_target);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100689 AppendLIR(res);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100690 return res;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100691 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100692}
693
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100694int Arm64Mir2Lir::EncodeShift(int shift_type, int amount) {
695 return ((shift_type & 0x3) << 7) | (amount & 0x1f);
696}
697
698int Arm64Mir2Lir::EncodeExtend(int extend_type, int amount) {
699 return (1 << 6) | ((extend_type & 0x7) << 3) | (amount & 0x7);
700}
701
702bool Arm64Mir2Lir::IsExtendEncoding(int encoded_value) {
703 return ((1 << 6) & encoded_value) != 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100704}
705
706LIR* Arm64Mir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100707 int scale, OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100708 LIR* load;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100709 int expected_scale = 0;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100710 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100711
712 if (r_dest.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100713 if (r_dest.IsDouble()) {
714 DCHECK(size == k64 || size == kDouble);
715 expected_scale = 3;
716 opcode = FWIDE(kA64Ldr4fXxG);
717 } else {
718 DCHECK(r_dest.IsSingle());
719 DCHECK(size == k32 || size == kSingle);
720 expected_scale = 2;
721 opcode = kA64Ldr4fXxG;
722 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100723
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100724 DCHECK(scale == 0 || scale == expected_scale);
725 return NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
726 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 }
728
729 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100730 case kDouble:
731 case kWord:
732 case k64:
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100733 opcode = WIDE(kA64Ldr4rXxG);
734 expected_scale = 3;
735 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100736 case kSingle:
Matteo Franchin43ec8732014-03-31 15:00:14 +0100737 case k32:
Matteo Franchin43ec8732014-03-31 15:00:14 +0100738 case kReference:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100739 opcode = kA64Ldr4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100740 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100741 break;
742 case kUnsignedHalf:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100743 opcode = kA64Ldrh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100744 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100745 break;
746 case kSignedHalf:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100747 opcode = kA64Ldrsh4rXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100748 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100749 break;
750 case kUnsignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100751 opcode = kA64Ldrb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100752 break;
753 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100754 opcode = kA64Ldrsb3rXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100755 break;
756 default:
757 LOG(FATAL) << "Bad size: " << size;
758 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100759
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100760 if (UNLIKELY(expected_scale == 0)) {
761 // This is a tertiary op (e.g. ldrb, ldrsb), it does not not support scale.
762 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100763 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100764 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100765 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100766 DCHECK(scale == 0 || scale == expected_scale);
767 load = NewLIR4(opcode, r_dest.GetReg(), r_base.GetReg(), r_index.GetReg(),
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100768 (scale != 0) ? 1 : 0);
769 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100770
771 return load;
772}
773
774LIR* Arm64Mir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100775 int scale, OpSize size) {
776 LIR* store;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100777 int expected_scale = 0;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100778 ArmOpcode opcode = kA64Brk1d;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100779
780 if (r_src.IsFloat()) {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100781 if (r_src.IsDouble()) {
782 DCHECK(size == k64 || size == kDouble);
783 expected_scale = 3;
784 opcode = FWIDE(kA64Str4fXxG);
785 } else {
786 DCHECK(r_src.IsSingle());
787 DCHECK(size == k32 || size == kSingle);
788 expected_scale = 2;
789 opcode = kA64Str4fXxG;
790 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100791
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100792 DCHECK(scale == 0 || scale == expected_scale);
793 return NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
794 (scale != 0) ? 1 : 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100795 }
796
797 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100798 case kDouble: // Intentional fall-trough.
799 case kWord: // Intentional fall-trough.
800 case k64:
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100801 opcode = WIDE(kA64Str4rXxG);
802 expected_scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100803 break;
804 case kSingle: // Intentional fall-trough.
805 case k32: // Intentional fall-trough.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100806 case kReference:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100807 opcode = kA64Str4rXxG;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100808 expected_scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100809 break;
810 case kUnsignedHalf:
Matteo Franchin43ec8732014-03-31 15:00:14 +0100811 case kSignedHalf:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100812 opcode = kA64Strh4wXxd;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100813 expected_scale = 1;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100814 break;
815 case kUnsignedByte:
Matteo Franchin43ec8732014-03-31 15:00:14 +0100816 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100817 opcode = kA64Strb3wXx;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100818 break;
819 default:
820 LOG(FATAL) << "Bad size: " << size;
821 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100822
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100823 if (UNLIKELY(expected_scale == 0)) {
824 // This is a tertiary op (e.g. strb), it does not not support scale.
825 DCHECK_NE(EncodingMap[UNWIDE(opcode)].flags & IS_TERTIARY_OP, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100826 DCHECK_EQ(scale, 0);
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100827 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100828 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100829 store = NewLIR4(opcode, r_src.GetReg(), r_base.GetReg(), r_index.GetReg(),
830 (scale != 0) ? 1 : 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100831 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100832
833 return store;
834}
835
836/*
837 * Load value from base + displacement. Optionally perform null check
838 * on base (which must have an associated s_reg and MIR). If not
839 * performing null check, incoming MIR can be null.
840 */
841LIR* Arm64Mir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100842 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100843 LIR* load = NULL;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100844 ArmOpcode opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100845 ArmOpcode alt_opcode = kA64Brk1d;
846 int scale = 0;
847
Matteo Franchin43ec8732014-03-31 15:00:14 +0100848 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100849 case kDouble: // Intentional fall-through.
850 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100851 case k64:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100852 scale = 3;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100853 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100854 DCHECK(r_dest.IsDouble());
855 opcode = FWIDE(kA64Ldr3fXD);
856 alt_opcode = FWIDE(kA64Ldur3fXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100857 } else {
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100858 opcode = WIDE(kA64Ldr3rXD);
859 alt_opcode = WIDE(kA64Ldur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100860 }
861 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100862 case kSingle: // Intentional fall-through.
863 case k32: // Intentional fall-trough.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100864 case kReference:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100865 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100866 if (r_dest.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100867 DCHECK(r_dest.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100868 opcode = kA64Ldr3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100869 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100870 opcode = kA64Ldr3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100871 }
872 break;
873 case kUnsignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100874 scale = 1;
875 opcode = kA64Ldrh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100876 break;
877 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100878 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100879 opcode = kA64Ldrsh3rXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100880 break;
881 case kUnsignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100882 opcode = kA64Ldrb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100883 break;
884 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100885 opcode = kA64Ldrsb3rXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100886 break;
887 default:
888 LOG(FATAL) << "Bad size: " << size;
889 }
890
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100891 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
892 int scaled_disp = displacement >> scale;
893 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
894 // Can use scaled load.
895 load = NewLIR3(opcode, r_dest.GetReg(), r_base.GetReg(), scaled_disp);
896 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
897 // Can use unscaled load.
898 load = NewLIR3(alt_opcode, r_dest.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100899 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100900 // Use long sequence.
901 RegStorage r_scratch = AllocTemp();
902 LoadConstant(r_scratch, displacement);
903 load = LoadBaseIndexed(r_base, r_scratch, r_dest, 0, size);
904 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100905 }
906
907 // TODO: in future may need to differentiate Dalvik accesses w/ spills
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100908 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
909 DCHECK(r_base == rs_rA64_SP);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100910 AnnotateDalvikRegAccess(load, displacement >> 2, true /* is_load */, r_dest.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100911 }
912 return load;
913}
914
Vladimir Marko674744e2014-04-24 15:18:26 +0100915LIR* Arm64Mir2Lir::LoadBaseDispVolatile(RegStorage r_base, int displacement, RegStorage r_dest,
916 OpSize size) {
917 // LoadBaseDisp() will emit correct insn for atomic load on arm64
918 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
919 return LoadBaseDisp(r_base, displacement, r_dest, size);
920}
921
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100922LIR* Arm64Mir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
923 OpSize size) {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100924 return LoadBaseDispBody(r_base, displacement, r_dest, size);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100925}
926
Matteo Franchin43ec8732014-03-31 15:00:14 +0100927
928LIR* Arm64Mir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement, RegStorage r_src,
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100929 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100930 LIR* store = NULL;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100931 ArmOpcode opcode = kA64Brk1d;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100932 ArmOpcode alt_opcode = kA64Brk1d;
933 int scale = 0;
934
Matteo Franchin43ec8732014-03-31 15:00:14 +0100935 switch (size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100936 case kDouble: // Intentional fall-through.
937 case kWord: // Intentional fall-through.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100938 case k64:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100939 scale = 3;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100940 if (r_src.IsFloat()) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100941 DCHECK(r_src.IsDouble());
942 opcode = FWIDE(kA64Str3fXD);
943 alt_opcode = FWIDE(kA64Stur3fXd);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100944 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100945 opcode = FWIDE(kA64Str3rXD);
946 alt_opcode = FWIDE(kA64Stur3rXd);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100947 }
948 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100949 case kSingle: // Intentional fall-through.
950 case k32: // Intentional fall-trough.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100951 case kReference:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100952 scale = 2;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100953 if (r_src.IsFloat()) {
954 DCHECK(r_src.IsSingle());
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100955 opcode = kA64Str3fXD;
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100956 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100957 opcode = kA64Str3rXD;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100958 }
959 break;
960 case kUnsignedHalf:
961 case kSignedHalf:
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100962 scale = 1;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100963 opcode = kA64Strh3wXF;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100964 break;
965 case kUnsignedByte:
966 case kSignedByte:
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100967 opcode = kA64Strb3wXd;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100968 break;
969 default:
970 LOG(FATAL) << "Bad size: " << size;
971 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100972
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100973 bool displacement_is_aligned = (displacement & ((1 << scale) - 1)) == 0;
974 int scaled_disp = displacement >> scale;
975 if (displacement_is_aligned && scaled_disp >= 0 && scaled_disp < 4096) {
976 // Can use scaled store.
977 store = NewLIR3(opcode, r_src.GetReg(), r_base.GetReg(), scaled_disp);
978 } else if (alt_opcode != kA64Brk1d && IS_SIGNED_IMM9(displacement)) {
979 // Can use unscaled store.
980 store = NewLIR3(alt_opcode, r_src.GetReg(), r_base.GetReg(), displacement);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100981 } else {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100982 // Use long sequence.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100983 RegStorage r_scratch = AllocTemp();
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100984 LoadConstant(r_scratch, displacement);
985 store = StoreBaseIndexed(r_base, r_scratch, r_src, 0, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100986 FreeTemp(r_scratch);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100987 }
988
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100989 // TODO: In future, may need to differentiate Dalvik & spill accesses.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100990 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
991 DCHECK(r_base == rs_rA64_SP);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100992 AnnotateDalvikRegAccess(store, displacement >> 2, false /* is_load */, r_src.Is64Bit());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100993 }
994 return store;
995}
996
Vladimir Marko674744e2014-04-24 15:18:26 +0100997LIR* Arm64Mir2Lir::StoreBaseDispVolatile(RegStorage r_base, int displacement, RegStorage r_src,
998 OpSize size) {
999 // StoreBaseDisp() will emit correct insn for atomic store on arm64
1000 // assuming r_dest is correctly prepared using RegClassForFieldLoadStore().
1001 return StoreBaseDisp(r_base, displacement, r_src, size);
1002}
1003
Matteo Franchin43ec8732014-03-31 15:00:14 +01001004LIR* Arm64Mir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
Vladimir Marko674744e2014-04-24 15:18:26 +01001005 OpSize size) {
Matteo Franchin43ec8732014-03-31 15:00:14 +01001006 return StoreBaseDispBody(r_base, displacement, r_src, size);
1007}
1008
Matteo Franchin43ec8732014-03-31 15:00:14 +01001009LIR* Arm64Mir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001010 LOG(FATAL) << "Unexpected use of OpFpRegCopy for Arm64";
1011 return NULL;
Matteo Franchin43ec8732014-03-31 15:00:14 +01001012}
1013
Andreas Gampe2f244e92014-05-08 03:35:25 -07001014LIR* Arm64Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<4> thread_offset) {
1015 UNIMPLEMENTED(FATAL) << "Should not be used.";
1016 return nullptr;
1017}
1018
1019LIR* Arm64Mir2Lir::OpThreadMem(OpKind op, ThreadOffset<8> thread_offset) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001020 LOG(FATAL) << "Unexpected use of OpThreadMem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001021 return NULL;
1022}
1023
1024LIR* Arm64Mir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001025 LOG(FATAL) << "Unexpected use of OpMem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001026 return NULL;
1027}
1028
1029LIR* Arm64Mir2Lir::StoreBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001030 int displacement, RegStorage r_src, OpSize size) {
1031 LOG(FATAL) << "Unexpected use of StoreBaseIndexedDisp for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001032 return NULL;
1033}
1034
1035LIR* Arm64Mir2Lir::OpRegMem(OpKind op, RegStorage r_dest, RegStorage r_base, int offset) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001036 LOG(FATAL) << "Unexpected use of OpRegMem for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001037 return NULL;
1038}
1039
1040LIR* Arm64Mir2Lir::LoadBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +01001041 int displacement, RegStorage r_dest, OpSize size) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +01001042 LOG(FATAL) << "Unexpected use of LoadBaseIndexedDisp for Arm64";
Matteo Franchin43ec8732014-03-31 15:00:14 +01001043 return NULL;
1044}
1045
1046} // namespace art