blob: af0ee4e19746a09c343336a4ff3fd045bdebf15e [file] [log] [blame]
Andreas Gampe878d58c2015-01-15 23:24:00 -08001/*
2 * Copyright (C) 2015 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#ifndef ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
18#define ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
19
Alexandre Rames8626b742015-11-25 16:28:08 +000020#include "code_generator.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080021#include "locations.h"
22#include "nodes.h"
23#include "utils/arm64/assembler_arm64.h"
Scott Wakeling97c72b72016-06-24 16:19:36 +010024
25#include "a64/disasm-a64.h"
26#include "a64/macro-assembler-a64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027
28namespace art {
29namespace arm64 {
30namespace helpers {
31
Andreas Gampe878d58c2015-01-15 23:24:00 -080032// Convenience helpers to ease conversion to and from VIXL operands.
33static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
34 "Unexpected values for register codes.");
35
36static inline int VIXLRegCodeFromART(int code) {
37 if (code == SP) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010038 return vixl::aarch64::kSPRegInternalCode;
Andreas Gampe878d58c2015-01-15 23:24:00 -080039 }
40 if (code == XZR) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010041 return vixl::aarch64::kZeroRegCode;
Andreas Gampe878d58c2015-01-15 23:24:00 -080042 }
43 return code;
44}
45
46static inline int ARTRegCodeFromVIXL(int code) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010047 if (code == vixl::aarch64::kSPRegInternalCode) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080048 return SP;
49 }
Scott Wakeling97c72b72016-06-24 16:19:36 +010050 if (code == vixl::aarch64::kZeroRegCode) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080051 return XZR;
52 }
53 return code;
54}
55
Scott Wakeling97c72b72016-06-24 16:19:36 +010056static inline vixl::aarch64::Register XRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010057 DCHECK(location.IsRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010058 return vixl::aarch64::Register::GetXRegFromCode(VIXLRegCodeFromART(location.reg()));
Andreas Gampe878d58c2015-01-15 23:24:00 -080059}
60
Scott Wakeling97c72b72016-06-24 16:19:36 +010061static inline vixl::aarch64::Register WRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010062 DCHECK(location.IsRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010063 return vixl::aarch64::Register::GetWRegFromCode(VIXLRegCodeFromART(location.reg()));
Andreas Gampe878d58c2015-01-15 23:24:00 -080064}
65
Scott Wakeling97c72b72016-06-24 16:19:36 +010066static inline vixl::aarch64::Register RegisterFrom(Location location, Primitive::Type type) {
Roland Levillain3a448e42016-04-01 18:37:46 +010067 DCHECK(type != Primitive::kPrimVoid && !Primitive::IsFloatingPointType(type)) << type;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068 return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location);
69}
70
Scott Wakeling97c72b72016-06-24 16:19:36 +010071static inline vixl::aarch64::Register OutputRegister(HInstruction* instr) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080072 return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
73}
74
Scott Wakeling97c72b72016-06-24 16:19:36 +010075static inline vixl::aarch64::Register InputRegisterAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080076 return RegisterFrom(instr->GetLocations()->InAt(input_index),
77 instr->InputAt(input_index)->GetType());
78}
79
Scott Wakeling97c72b72016-06-24 16:19:36 +010080static inline vixl::aarch64::FPRegister DRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010081 DCHECK(location.IsFpuRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010082 return vixl::aarch64::FPRegister::GetDRegFromCode(location.reg());
Andreas Gampe878d58c2015-01-15 23:24:00 -080083}
84
Scott Wakeling97c72b72016-06-24 16:19:36 +010085static inline vixl::aarch64::FPRegister SRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010086 DCHECK(location.IsFpuRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010087 return vixl::aarch64::FPRegister::GetSRegFromCode(location.reg());
Andreas Gampe878d58c2015-01-15 23:24:00 -080088}
89
Scott Wakeling97c72b72016-06-24 16:19:36 +010090static inline vixl::aarch64::FPRegister FPRegisterFrom(Location location, Primitive::Type type) {
Roland Levillain3a448e42016-04-01 18:37:46 +010091 DCHECK(Primitive::IsFloatingPointType(type)) << type;
Andreas Gampe878d58c2015-01-15 23:24:00 -080092 return type == Primitive::kPrimDouble ? DRegisterFrom(location) : SRegisterFrom(location);
93}
94
Scott Wakeling97c72b72016-06-24 16:19:36 +010095static inline vixl::aarch64::FPRegister OutputFPRegister(HInstruction* instr) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080096 return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
97}
98
Scott Wakeling97c72b72016-06-24 16:19:36 +010099static inline vixl::aarch64::FPRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800100 return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
101 instr->InputAt(input_index)->GetType());
102}
103
Scott Wakeling97c72b72016-06-24 16:19:36 +0100104static inline vixl::aarch64::CPURegister CPURegisterFrom(Location location, Primitive::Type type) {
105 return Primitive::IsFloatingPointType(type)
106 ? vixl::aarch64::CPURegister(FPRegisterFrom(location, type))
107 : vixl::aarch64::CPURegister(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800108}
109
Scott Wakeling97c72b72016-06-24 16:19:36 +0100110static inline vixl::aarch64::CPURegister OutputCPURegister(HInstruction* instr) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000111 return Primitive::IsFloatingPointType(instr->GetType())
Scott Wakeling97c72b72016-06-24 16:19:36 +0100112 ? static_cast<vixl::aarch64::CPURegister>(OutputFPRegister(instr))
113 : static_cast<vixl::aarch64::CPURegister>(OutputRegister(instr));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800114}
115
Scott Wakeling97c72b72016-06-24 16:19:36 +0100116static inline vixl::aarch64::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000117 return Primitive::IsFloatingPointType(instr->InputAt(index)->GetType())
Scott Wakeling97c72b72016-06-24 16:19:36 +0100118 ? static_cast<vixl::aarch64::CPURegister>(InputFPRegisterAt(instr, index))
119 : static_cast<vixl::aarch64::CPURegister>(InputRegisterAt(instr, index));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120}
121
122static inline int64_t Int64ConstantFrom(Location location) {
123 HConstant* instr = location.GetConstant();
Nicolas Geoffrayde0eb6f2015-03-04 10:28:04 +0000124 if (instr->IsIntConstant()) {
125 return instr->AsIntConstant()->GetValue();
126 } else if (instr->IsNullConstant()) {
127 return 0;
128 } else {
Roland Levillain3a448e42016-04-01 18:37:46 +0100129 DCHECK(instr->IsLongConstant()) << instr->DebugName();
Nicolas Geoffrayde0eb6f2015-03-04 10:28:04 +0000130 return instr->AsLongConstant()->GetValue();
131 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800132}
133
Scott Wakeling97c72b72016-06-24 16:19:36 +0100134static inline vixl::aarch64::Operand OperandFrom(Location location, Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800135 if (location.IsRegister()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100136 return vixl::aarch64::Operand(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800137 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100138 return vixl::aarch64::Operand(Int64ConstantFrom(location));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800139 }
140}
141
Scott Wakeling97c72b72016-06-24 16:19:36 +0100142static inline vixl::aarch64::Operand InputOperandAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800143 return OperandFrom(instr->GetLocations()->InAt(input_index),
144 instr->InputAt(input_index)->GetType());
145}
146
Scott Wakeling97c72b72016-06-24 16:19:36 +0100147static inline vixl::aarch64::MemOperand StackOperandFrom(Location location) {
148 return vixl::aarch64::MemOperand(vixl::aarch64::sp, location.GetStackIndex());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800149}
150
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
152 size_t offset = 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800153 // A heap reference must be 32bit, so fit in a W register.
154 DCHECK(base.IsW());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100155 return vixl::aarch64::MemOperand(base.X(), offset);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800156}
157
Scott Wakeling97c72b72016-06-24 16:19:36 +0100158static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
159 const vixl::aarch64::Register& regoffset,
160 vixl::aarch64::Shift shift = vixl::aarch64::LSL,
161 unsigned shift_amount = 0) {
Alexandre Rames82000b02015-07-07 11:34:16 +0100162 // A heap reference must be 32bit, so fit in a W register.
163 DCHECK(base.IsW());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 return vixl::aarch64::MemOperand(base.X(), regoffset, shift, shift_amount);
Alexandre Rames82000b02015-07-07 11:34:16 +0100165}
166
Scott Wakeling97c72b72016-06-24 16:19:36 +0100167static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
168 Offset offset) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800169 return HeapOperand(base, offset.SizeValue());
170}
171
Scott Wakeling97c72b72016-06-24 16:19:36 +0100172static inline vixl::aarch64::MemOperand HeapOperandFrom(Location location, Offset offset) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800173 return HeapOperand(RegisterFrom(location, Primitive::kPrimNot), offset);
174}
175
Scott Wakeling97c72b72016-06-24 16:19:36 +0100176static inline Location LocationFrom(const vixl::aarch64::Register& reg) {
177 return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.GetCode()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800178}
179
Scott Wakeling97c72b72016-06-24 16:19:36 +0100180static inline Location LocationFrom(const vixl::aarch64::FPRegister& fpreg) {
181 return Location::FpuRegisterLocation(fpreg.GetCode());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800182}
183
Scott Wakeling97c72b72016-06-24 16:19:36 +0100184static inline vixl::aarch64::Operand OperandFromMemOperand(
185 const vixl::aarch64::MemOperand& mem_op) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800186 if (mem_op.IsImmediateOffset()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 return vixl::aarch64::Operand(mem_op.GetOffset());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800188 } else {
189 DCHECK(mem_op.IsRegisterOffset());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100190 if (mem_op.GetExtend() != vixl::aarch64::NO_EXTEND) {
191 return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
192 mem_op.GetExtend(),
193 mem_op.GetShiftAmount());
194 } else if (mem_op.GetShift() != vixl::aarch64::NO_SHIFT) {
195 return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
196 mem_op.GetShift(),
197 mem_op.GetShiftAmount());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800198 } else {
199 LOG(FATAL) << "Should not reach here";
200 UNREACHABLE();
201 }
202 }
203}
204
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000205static bool CanEncodeConstantAsImmediate(HConstant* constant, HInstruction* instr) {
Roland Levillain22c49222016-03-18 14:04:28 +0000206 DCHECK(constant->IsIntConstant() || constant->IsLongConstant() || constant->IsNullConstant())
207 << constant->DebugName();
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000208
209 // For single uses we let VIXL handle the constant generation since it will
210 // use registers that are not managed by the register allocator (wip0, wip1).
Vladimir Marko46817b82016-03-29 12:21:58 +0100211 if (constant->GetUses().HasExactlyOneElement()) {
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000212 return true;
213 }
214
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000215 // Our code generator ensures shift distances are within an encodable range.
216 if (instr->IsRor()) {
217 return true;
218 }
219
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000220 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
221
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100222 if (instr->IsAnd() || instr->IsOr() || instr->IsXor()) {
223 // Uses logical operations.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100224 return vixl::aarch64::Assembler::IsImmLogical(value, vixl::aarch64::kXRegSize);
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100225 } else if (instr->IsNeg()) {
226 // Uses mov -immediate.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100227 return vixl::aarch64::Assembler::IsImmMovn(value, vixl::aarch64::kXRegSize);
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100228 } else {
229 DCHECK(instr->IsAdd() ||
Artem Serov328429f2016-07-06 16:23:04 +0100230 instr->IsIntermediateAddress() ||
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100231 instr->IsBoundsCheck() ||
232 instr->IsCompare() ||
233 instr->IsCondition() ||
Roland Levillain22c49222016-03-18 14:04:28 +0000234 instr->IsSub())
235 << instr->DebugName();
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000236 // Uses aliases of ADD/SUB instructions.
Alexandre Ramesb69fbfb2015-10-16 09:08:46 +0100237 // If `value` does not fit but `-value` does, VIXL will automatically use
238 // the 'opposite' instruction.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100239 return vixl::aarch64::Assembler::IsImmAddSub(value)
240 || vixl::aarch64::Assembler::IsImmAddSub(-value);
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000241 }
242}
243
244static inline Location ARM64EncodableConstantOrRegister(HInstruction* constant,
245 HInstruction* instr) {
246 if (constant->IsConstant()
247 && CanEncodeConstantAsImmediate(constant->AsConstant(), instr)) {
248 return Location::ConstantLocation(constant->AsConstant());
249 }
250
251 return Location::RequiresRegister();
252}
253
Zheng Xuda403092015-04-24 17:35:39 +0800254// Check if registers in art register set have the same register code in vixl. If the register
255// codes are same, we can initialize vixl register list simply by the register masks. Currently,
256// only SP/WSP and ZXR/WZR codes are different between art and vixl.
257// Note: This function is only used for debug checks.
258static inline bool ArtVixlRegCodeCoherentForRegSet(uint32_t art_core_registers,
259 size_t num_core,
260 uint32_t art_fpu_registers,
261 size_t num_fpu) {
262 // The register masks won't work if the number of register is larger than 32.
263 DCHECK_GE(sizeof(art_core_registers) * 8, num_core);
264 DCHECK_GE(sizeof(art_fpu_registers) * 8, num_fpu);
265 for (size_t art_reg_code = 0; art_reg_code < num_core; ++art_reg_code) {
266 if (RegisterSet::Contains(art_core_registers, art_reg_code)) {
267 if (art_reg_code != static_cast<size_t>(VIXLRegCodeFromART(art_reg_code))) {
268 return false;
269 }
270 }
271 }
272 // There is no register code translation for float registers.
273 return true;
274}
275
Scott Wakeling97c72b72016-06-24 16:19:36 +0100276static inline vixl::aarch64::Shift ShiftFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000277 switch (op_kind) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100278 case HArm64DataProcWithShifterOp::kASR: return vixl::aarch64::ASR;
279 case HArm64DataProcWithShifterOp::kLSL: return vixl::aarch64::LSL;
280 case HArm64DataProcWithShifterOp::kLSR: return vixl::aarch64::LSR;
Alexandre Rames8626b742015-11-25 16:28:08 +0000281 default:
282 LOG(FATAL) << "Unexpected op kind " << op_kind;
283 UNREACHABLE();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100284 return vixl::aarch64::NO_SHIFT;
Alexandre Rames8626b742015-11-25 16:28:08 +0000285 }
286}
287
Scott Wakeling97c72b72016-06-24 16:19:36 +0100288static inline vixl::aarch64::Extend ExtendFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000289 switch (op_kind) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100290 case HArm64DataProcWithShifterOp::kUXTB: return vixl::aarch64::UXTB;
291 case HArm64DataProcWithShifterOp::kUXTH: return vixl::aarch64::UXTH;
292 case HArm64DataProcWithShifterOp::kUXTW: return vixl::aarch64::UXTW;
293 case HArm64DataProcWithShifterOp::kSXTB: return vixl::aarch64::SXTB;
294 case HArm64DataProcWithShifterOp::kSXTH: return vixl::aarch64::SXTH;
295 case HArm64DataProcWithShifterOp::kSXTW: return vixl::aarch64::SXTW;
Alexandre Rames8626b742015-11-25 16:28:08 +0000296 default:
297 LOG(FATAL) << "Unexpected op kind " << op_kind;
298 UNREACHABLE();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100299 return vixl::aarch64::NO_EXTEND;
Alexandre Rames8626b742015-11-25 16:28:08 +0000300 }
301}
302
303static inline bool CanFitInShifterOperand(HInstruction* instruction) {
304 if (instruction->IsTypeConversion()) {
305 HTypeConversion* conversion = instruction->AsTypeConversion();
306 Primitive::Type result_type = conversion->GetResultType();
307 Primitive::Type input_type = conversion->GetInputType();
308 // We don't expect to see the same type as input and result.
309 return Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type) &&
310 (result_type != input_type);
311 } else {
312 return (instruction->IsShl() && instruction->AsShl()->InputAt(1)->IsIntConstant()) ||
313 (instruction->IsShr() && instruction->AsShr()->InputAt(1)->IsIntConstant()) ||
314 (instruction->IsUShr() && instruction->AsUShr()->InputAt(1)->IsIntConstant());
315 }
316}
317
318static inline bool HasShifterOperand(HInstruction* instr) {
319 // `neg` instructions are an alias of `sub` using the zero register as the
320 // first register input.
321 bool res = instr->IsAdd() || instr->IsAnd() || instr->IsNeg() ||
322 instr->IsOr() || instr->IsSub() || instr->IsXor();
323 return res;
324}
325
326static inline bool ShifterOperandSupportsExtension(HInstruction* instruction) {
327 DCHECK(HasShifterOperand(instruction));
328 // Although the `neg` instruction is an alias of the `sub` instruction, `HNeg`
329 // does *not* support extension. This is because the `extended register` form
330 // of the `sub` instruction interprets the left register with code 31 as the
331 // stack pointer and not the zero register. (So does the `immediate` form.) In
332 // the other form `shifted register, the register with code 31 is interpreted
333 // as the zero register.
334 return instruction->IsAdd() || instruction->IsSub();
335}
336
Andreas Gampe878d58c2015-01-15 23:24:00 -0800337} // namespace helpers
338} // namespace arm64
339} // namespace art
340
341#endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_