blob: cc949c5275c04dafc873f18afa3cb36460460202 [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
Artem Serovaf4e42a2016-08-08 15:11:24 +010025// TODO(VIXL): Make VIXL compile with -Wshadow.
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-Wshadow"
28#include "aarch64/disasm-aarch64.h"
29#include "aarch64/macro-assembler-aarch64.h"
30#include "aarch64/simulator-aarch64.h"
31#pragma GCC diagnostic pop
Andreas Gampe878d58c2015-01-15 23:24:00 -080032
33namespace art {
34namespace arm64 {
35namespace helpers {
36
Andreas Gampe878d58c2015-01-15 23:24:00 -080037// Convenience helpers to ease conversion to and from VIXL operands.
38static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
39 "Unexpected values for register codes.");
40
41static inline int VIXLRegCodeFromART(int code) {
42 if (code == SP) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010043 return vixl::aarch64::kSPRegInternalCode;
Andreas Gampe878d58c2015-01-15 23:24:00 -080044 }
45 if (code == XZR) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010046 return vixl::aarch64::kZeroRegCode;
Andreas Gampe878d58c2015-01-15 23:24:00 -080047 }
48 return code;
49}
50
51static inline int ARTRegCodeFromVIXL(int code) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010052 if (code == vixl::aarch64::kSPRegInternalCode) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080053 return SP;
54 }
Scott Wakeling97c72b72016-06-24 16:19:36 +010055 if (code == vixl::aarch64::kZeroRegCode) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080056 return XZR;
57 }
58 return code;
59}
60
Scott Wakeling97c72b72016-06-24 16:19:36 +010061static inline vixl::aarch64::Register XRegisterFrom(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::GetXRegFromCode(VIXLRegCodeFromART(location.reg()));
Andreas Gampe878d58c2015-01-15 23:24:00 -080064}
65
Scott Wakeling97c72b72016-06-24 16:19:36 +010066static inline vixl::aarch64::Register WRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010067 DCHECK(location.IsRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010068 return vixl::aarch64::Register::GetWRegFromCode(VIXLRegCodeFromART(location.reg()));
Andreas Gampe878d58c2015-01-15 23:24:00 -080069}
70
Scott Wakeling97c72b72016-06-24 16:19:36 +010071static inline vixl::aarch64::Register RegisterFrom(Location location, Primitive::Type type) {
Roland Levillain3a448e42016-04-01 18:37:46 +010072 DCHECK(type != Primitive::kPrimVoid && !Primitive::IsFloatingPointType(type)) << type;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073 return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location);
74}
75
Scott Wakeling97c72b72016-06-24 16:19:36 +010076static inline vixl::aarch64::Register OutputRegister(HInstruction* instr) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080077 return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
78}
79
Scott Wakeling97c72b72016-06-24 16:19:36 +010080static inline vixl::aarch64::Register InputRegisterAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080081 return RegisterFrom(instr->GetLocations()->InAt(input_index),
82 instr->InputAt(input_index)->GetType());
83}
84
Scott Wakeling97c72b72016-06-24 16:19:36 +010085static inline vixl::aarch64::FPRegister DRegisterFrom(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::GetDRegFromCode(location.reg());
Andreas Gampe878d58c2015-01-15 23:24:00 -080088}
89
Scott Wakeling97c72b72016-06-24 16:19:36 +010090static inline vixl::aarch64::FPRegister SRegisterFrom(Location location) {
Roland Levillain3a448e42016-04-01 18:37:46 +010091 DCHECK(location.IsFpuRegister()) << location;
Scott Wakeling97c72b72016-06-24 16:19:36 +010092 return vixl::aarch64::FPRegister::GetSRegFromCode(location.reg());
Andreas Gampe878d58c2015-01-15 23:24:00 -080093}
94
Scott Wakeling97c72b72016-06-24 16:19:36 +010095static inline vixl::aarch64::FPRegister FPRegisterFrom(Location location, Primitive::Type type) {
Roland Levillain3a448e42016-04-01 18:37:46 +010096 DCHECK(Primitive::IsFloatingPointType(type)) << type;
Andreas Gampe878d58c2015-01-15 23:24:00 -080097 return type == Primitive::kPrimDouble ? DRegisterFrom(location) : SRegisterFrom(location);
98}
99
Scott Wakeling97c72b72016-06-24 16:19:36 +0100100static inline vixl::aarch64::FPRegister OutputFPRegister(HInstruction* instr) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800101 return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
102}
103
Scott Wakeling97c72b72016-06-24 16:19:36 +0100104static inline vixl::aarch64::FPRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800105 return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
106 instr->InputAt(input_index)->GetType());
107}
108
Scott Wakeling97c72b72016-06-24 16:19:36 +0100109static inline vixl::aarch64::CPURegister CPURegisterFrom(Location location, Primitive::Type type) {
110 return Primitive::IsFloatingPointType(type)
111 ? vixl::aarch64::CPURegister(FPRegisterFrom(location, type))
112 : vixl::aarch64::CPURegister(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800113}
114
Scott Wakeling97c72b72016-06-24 16:19:36 +0100115static inline vixl::aarch64::CPURegister OutputCPURegister(HInstruction* instr) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000116 return Primitive::IsFloatingPointType(instr->GetType())
Scott Wakeling97c72b72016-06-24 16:19:36 +0100117 ? static_cast<vixl::aarch64::CPURegister>(OutputFPRegister(instr))
118 : static_cast<vixl::aarch64::CPURegister>(OutputRegister(instr));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800119}
120
Scott Wakeling97c72b72016-06-24 16:19:36 +0100121static inline vixl::aarch64::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000122 return Primitive::IsFloatingPointType(instr->InputAt(index)->GetType())
Scott Wakeling97c72b72016-06-24 16:19:36 +0100123 ? static_cast<vixl::aarch64::CPURegister>(InputFPRegisterAt(instr, index))
124 : static_cast<vixl::aarch64::CPURegister>(InputRegisterAt(instr, index));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800125}
126
127static inline int64_t Int64ConstantFrom(Location location) {
128 HConstant* instr = location.GetConstant();
Nicolas Geoffrayde0eb6f2015-03-04 10:28:04 +0000129 if (instr->IsIntConstant()) {
130 return instr->AsIntConstant()->GetValue();
131 } else if (instr->IsNullConstant()) {
132 return 0;
133 } else {
Roland Levillain3a448e42016-04-01 18:37:46 +0100134 DCHECK(instr->IsLongConstant()) << instr->DebugName();
Nicolas Geoffrayde0eb6f2015-03-04 10:28:04 +0000135 return instr->AsLongConstant()->GetValue();
136 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800137}
138
Scott Wakeling97c72b72016-06-24 16:19:36 +0100139static inline vixl::aarch64::Operand OperandFrom(Location location, Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800140 if (location.IsRegister()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100141 return vixl::aarch64::Operand(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800142 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100143 return vixl::aarch64::Operand(Int64ConstantFrom(location));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800144 }
145}
146
Scott Wakeling97c72b72016-06-24 16:19:36 +0100147static inline vixl::aarch64::Operand InputOperandAt(HInstruction* instr, int input_index) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800148 return OperandFrom(instr->GetLocations()->InAt(input_index),
149 instr->InputAt(input_index)->GetType());
150}
151
Scott Wakeling97c72b72016-06-24 16:19:36 +0100152static inline vixl::aarch64::MemOperand StackOperandFrom(Location location) {
153 return vixl::aarch64::MemOperand(vixl::aarch64::sp, location.GetStackIndex());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800154}
155
Scott Wakeling97c72b72016-06-24 16:19:36 +0100156static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
157 size_t offset = 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800158 // A heap reference must be 32bit, so fit in a W register.
159 DCHECK(base.IsW());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100160 return vixl::aarch64::MemOperand(base.X(), offset);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800161}
162
Scott Wakeling97c72b72016-06-24 16:19:36 +0100163static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
164 const vixl::aarch64::Register& regoffset,
165 vixl::aarch64::Shift shift = vixl::aarch64::LSL,
166 unsigned shift_amount = 0) {
Alexandre Rames82000b02015-07-07 11:34:16 +0100167 // A heap reference must be 32bit, so fit in a W register.
168 DCHECK(base.IsW());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100169 return vixl::aarch64::MemOperand(base.X(), regoffset, shift, shift_amount);
Alexandre Rames82000b02015-07-07 11:34:16 +0100170}
171
Scott Wakeling97c72b72016-06-24 16:19:36 +0100172static inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
173 Offset offset) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800174 return HeapOperand(base, offset.SizeValue());
175}
176
Scott Wakeling97c72b72016-06-24 16:19:36 +0100177static inline vixl::aarch64::MemOperand HeapOperandFrom(Location location, Offset offset) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800178 return HeapOperand(RegisterFrom(location, Primitive::kPrimNot), offset);
179}
180
Scott Wakeling97c72b72016-06-24 16:19:36 +0100181static inline Location LocationFrom(const vixl::aarch64::Register& reg) {
182 return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.GetCode()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800183}
184
Scott Wakeling97c72b72016-06-24 16:19:36 +0100185static inline Location LocationFrom(const vixl::aarch64::FPRegister& fpreg) {
186 return Location::FpuRegisterLocation(fpreg.GetCode());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800187}
188
Scott Wakeling97c72b72016-06-24 16:19:36 +0100189static inline vixl::aarch64::Operand OperandFromMemOperand(
190 const vixl::aarch64::MemOperand& mem_op) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800191 if (mem_op.IsImmediateOffset()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 return vixl::aarch64::Operand(mem_op.GetOffset());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800193 } else {
194 DCHECK(mem_op.IsRegisterOffset());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100195 if (mem_op.GetExtend() != vixl::aarch64::NO_EXTEND) {
196 return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
197 mem_op.GetExtend(),
198 mem_op.GetShiftAmount());
199 } else if (mem_op.GetShift() != vixl::aarch64::NO_SHIFT) {
200 return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
201 mem_op.GetShift(),
202 mem_op.GetShiftAmount());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800203 } else {
204 LOG(FATAL) << "Should not reach here";
205 UNREACHABLE();
206 }
207 }
208}
209
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000210static bool CanEncodeConstantAsImmediate(HConstant* constant, HInstruction* instr) {
Roland Levillain22c49222016-03-18 14:04:28 +0000211 DCHECK(constant->IsIntConstant() || constant->IsLongConstant() || constant->IsNullConstant())
212 << constant->DebugName();
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000213
214 // For single uses we let VIXL handle the constant generation since it will
215 // use registers that are not managed by the register allocator (wip0, wip1).
Vladimir Marko46817b82016-03-29 12:21:58 +0100216 if (constant->GetUses().HasExactlyOneElement()) {
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000217 return true;
218 }
219
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000220 // Our code generator ensures shift distances are within an encodable range.
221 if (instr->IsRor()) {
222 return true;
223 }
224
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000225 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
226
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100227 if (instr->IsAnd() || instr->IsOr() || instr->IsXor()) {
228 // Uses logical operations.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100229 return vixl::aarch64::Assembler::IsImmLogical(value, vixl::aarch64::kXRegSize);
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100230 } else if (instr->IsNeg()) {
231 // Uses mov -immediate.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100232 return vixl::aarch64::Assembler::IsImmMovn(value, vixl::aarch64::kXRegSize);
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100233 } else {
234 DCHECK(instr->IsAdd() ||
Artem Serov328429f2016-07-06 16:23:04 +0100235 instr->IsIntermediateAddress() ||
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100236 instr->IsBoundsCheck() ||
237 instr->IsCompare() ||
238 instr->IsCondition() ||
Roland Levillain22c49222016-03-18 14:04:28 +0000239 instr->IsSub())
240 << instr->DebugName();
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000241 // Uses aliases of ADD/SUB instructions.
Alexandre Ramesb69fbfb2015-10-16 09:08:46 +0100242 // If `value` does not fit but `-value` does, VIXL will automatically use
243 // the 'opposite' instruction.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100244 return vixl::aarch64::Assembler::IsImmAddSub(value)
245 || vixl::aarch64::Assembler::IsImmAddSub(-value);
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000246 }
247}
248
249static inline Location ARM64EncodableConstantOrRegister(HInstruction* constant,
250 HInstruction* instr) {
251 if (constant->IsConstant()
252 && CanEncodeConstantAsImmediate(constant->AsConstant(), instr)) {
253 return Location::ConstantLocation(constant->AsConstant());
254 }
255
256 return Location::RequiresRegister();
257}
258
Zheng Xuda403092015-04-24 17:35:39 +0800259// Check if registers in art register set have the same register code in vixl. If the register
260// codes are same, we can initialize vixl register list simply by the register masks. Currently,
261// only SP/WSP and ZXR/WZR codes are different between art and vixl.
262// Note: This function is only used for debug checks.
263static inline bool ArtVixlRegCodeCoherentForRegSet(uint32_t art_core_registers,
264 size_t num_core,
265 uint32_t art_fpu_registers,
266 size_t num_fpu) {
267 // The register masks won't work if the number of register is larger than 32.
268 DCHECK_GE(sizeof(art_core_registers) * 8, num_core);
269 DCHECK_GE(sizeof(art_fpu_registers) * 8, num_fpu);
270 for (size_t art_reg_code = 0; art_reg_code < num_core; ++art_reg_code) {
271 if (RegisterSet::Contains(art_core_registers, art_reg_code)) {
272 if (art_reg_code != static_cast<size_t>(VIXLRegCodeFromART(art_reg_code))) {
273 return false;
274 }
275 }
276 }
277 // There is no register code translation for float registers.
278 return true;
279}
280
Scott Wakeling97c72b72016-06-24 16:19:36 +0100281static inline vixl::aarch64::Shift ShiftFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000282 switch (op_kind) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100283 case HArm64DataProcWithShifterOp::kASR: return vixl::aarch64::ASR;
284 case HArm64DataProcWithShifterOp::kLSL: return vixl::aarch64::LSL;
285 case HArm64DataProcWithShifterOp::kLSR: return vixl::aarch64::LSR;
Alexandre Rames8626b742015-11-25 16:28:08 +0000286 default:
287 LOG(FATAL) << "Unexpected op kind " << op_kind;
288 UNREACHABLE();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100289 return vixl::aarch64::NO_SHIFT;
Alexandre Rames8626b742015-11-25 16:28:08 +0000290 }
291}
292
Scott Wakeling97c72b72016-06-24 16:19:36 +0100293static inline vixl::aarch64::Extend ExtendFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000294 switch (op_kind) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100295 case HArm64DataProcWithShifterOp::kUXTB: return vixl::aarch64::UXTB;
296 case HArm64DataProcWithShifterOp::kUXTH: return vixl::aarch64::UXTH;
297 case HArm64DataProcWithShifterOp::kUXTW: return vixl::aarch64::UXTW;
298 case HArm64DataProcWithShifterOp::kSXTB: return vixl::aarch64::SXTB;
299 case HArm64DataProcWithShifterOp::kSXTH: return vixl::aarch64::SXTH;
300 case HArm64DataProcWithShifterOp::kSXTW: return vixl::aarch64::SXTW;
Alexandre Rames8626b742015-11-25 16:28:08 +0000301 default:
302 LOG(FATAL) << "Unexpected op kind " << op_kind;
303 UNREACHABLE();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100304 return vixl::aarch64::NO_EXTEND;
Alexandre Rames8626b742015-11-25 16:28:08 +0000305 }
306}
307
308static inline bool CanFitInShifterOperand(HInstruction* instruction) {
309 if (instruction->IsTypeConversion()) {
310 HTypeConversion* conversion = instruction->AsTypeConversion();
311 Primitive::Type result_type = conversion->GetResultType();
312 Primitive::Type input_type = conversion->GetInputType();
313 // We don't expect to see the same type as input and result.
314 return Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type) &&
315 (result_type != input_type);
316 } else {
317 return (instruction->IsShl() && instruction->AsShl()->InputAt(1)->IsIntConstant()) ||
318 (instruction->IsShr() && instruction->AsShr()->InputAt(1)->IsIntConstant()) ||
319 (instruction->IsUShr() && instruction->AsUShr()->InputAt(1)->IsIntConstant());
320 }
321}
322
323static inline bool HasShifterOperand(HInstruction* instr) {
324 // `neg` instructions are an alias of `sub` using the zero register as the
325 // first register input.
326 bool res = instr->IsAdd() || instr->IsAnd() || instr->IsNeg() ||
327 instr->IsOr() || instr->IsSub() || instr->IsXor();
328 return res;
329}
330
331static inline bool ShifterOperandSupportsExtension(HInstruction* instruction) {
332 DCHECK(HasShifterOperand(instruction));
333 // Although the `neg` instruction is an alias of the `sub` instruction, `HNeg`
334 // does *not* support extension. This is because the `extended register` form
335 // of the `sub` instruction interprets the left register with code 31 as the
336 // stack pointer and not the zero register. (So does the `immediate` form.) In
337 // the other form `shifted register, the register with code 31 is interpreted
338 // as the zero register.
339 return instruction->IsAdd() || instruction->IsSub();
340}
341
Andreas Gampe878d58c2015-01-15 23:24:00 -0800342} // namespace helpers
343} // namespace arm64
344} // namespace art
345
346#endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_