blob: 10d83439fdc0dcca312a670a31e7454233f183f8 [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"
Serban Constantinescu82e52ce2015-03-26 16:50:57 +000024#include "vixl/a64/disasm-a64.h"
25#include "vixl/a64/macro-assembler-a64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026
27namespace art {
28namespace arm64 {
29namespace helpers {
30
Andreas Gampe878d58c2015-01-15 23:24:00 -080031// Convenience helpers to ease conversion to and from VIXL operands.
32static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
33 "Unexpected values for register codes.");
34
35static inline int VIXLRegCodeFromART(int code) {
36 if (code == SP) {
37 return vixl::kSPRegInternalCode;
38 }
39 if (code == XZR) {
40 return vixl::kZeroRegCode;
41 }
42 return code;
43}
44
45static inline int ARTRegCodeFromVIXL(int code) {
46 if (code == vixl::kSPRegInternalCode) {
47 return SP;
48 }
49 if (code == vixl::kZeroRegCode) {
50 return XZR;
51 }
52 return code;
53}
54
55static inline vixl::Register XRegisterFrom(Location location) {
56 DCHECK(location.IsRegister());
57 return vixl::Register::XRegFromCode(VIXLRegCodeFromART(location.reg()));
58}
59
60static inline vixl::Register WRegisterFrom(Location location) {
61 DCHECK(location.IsRegister());
62 return vixl::Register::WRegFromCode(VIXLRegCodeFromART(location.reg()));
63}
64
65static inline vixl::Register RegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +000066 DCHECK(type != Primitive::kPrimVoid && !Primitive::IsFloatingPointType(type));
Andreas Gampe878d58c2015-01-15 23:24:00 -080067 return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location);
68}
69
70static inline vixl::Register OutputRegister(HInstruction* instr) {
71 return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
72}
73
74static inline vixl::Register InputRegisterAt(HInstruction* instr, int input_index) {
75 return RegisterFrom(instr->GetLocations()->InAt(input_index),
76 instr->InputAt(input_index)->GetType());
77}
78
79static inline vixl::FPRegister DRegisterFrom(Location location) {
80 DCHECK(location.IsFpuRegister());
81 return vixl::FPRegister::DRegFromCode(location.reg());
82}
83
84static inline vixl::FPRegister SRegisterFrom(Location location) {
85 DCHECK(location.IsFpuRegister());
86 return vixl::FPRegister::SRegFromCode(location.reg());
87}
88
89static inline vixl::FPRegister FPRegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +000090 DCHECK(Primitive::IsFloatingPointType(type));
Andreas Gampe878d58c2015-01-15 23:24:00 -080091 return type == Primitive::kPrimDouble ? DRegisterFrom(location) : SRegisterFrom(location);
92}
93
94static inline vixl::FPRegister OutputFPRegister(HInstruction* instr) {
95 return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
96}
97
98static inline vixl::FPRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
99 return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
100 instr->InputAt(input_index)->GetType());
101}
102
103static inline vixl::CPURegister CPURegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000104 return Primitive::IsFloatingPointType(type) ? vixl::CPURegister(FPRegisterFrom(location, type))
105 : vixl::CPURegister(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800106}
107
108static inline vixl::CPURegister OutputCPURegister(HInstruction* instr) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000109 return Primitive::IsFloatingPointType(instr->GetType())
110 ? static_cast<vixl::CPURegister>(OutputFPRegister(instr))
111 : static_cast<vixl::CPURegister>(OutputRegister(instr));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800112}
113
114static inline vixl::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000115 return Primitive::IsFloatingPointType(instr->InputAt(index)->GetType())
Andreas Gampe878d58c2015-01-15 23:24:00 -0800116 ? static_cast<vixl::CPURegister>(InputFPRegisterAt(instr, index))
117 : static_cast<vixl::CPURegister>(InputRegisterAt(instr, index));
118}
119
120static inline int64_t Int64ConstantFrom(Location location) {
121 HConstant* instr = location.GetConstant();
Nicolas Geoffrayde0eb6f2015-03-04 10:28:04 +0000122 if (instr->IsIntConstant()) {
123 return instr->AsIntConstant()->GetValue();
124 } else if (instr->IsNullConstant()) {
125 return 0;
126 } else {
127 DCHECK(instr->IsLongConstant());
128 return instr->AsLongConstant()->GetValue();
129 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800130}
131
132static inline vixl::Operand OperandFrom(Location location, Primitive::Type type) {
133 if (location.IsRegister()) {
134 return vixl::Operand(RegisterFrom(location, type));
135 } else {
136 return vixl::Operand(Int64ConstantFrom(location));
137 }
138}
139
140static inline vixl::Operand InputOperandAt(HInstruction* instr, int input_index) {
141 return OperandFrom(instr->GetLocations()->InAt(input_index),
142 instr->InputAt(input_index)->GetType());
143}
144
145static inline vixl::MemOperand StackOperandFrom(Location location) {
146 return vixl::MemOperand(vixl::sp, location.GetStackIndex());
147}
148
149static inline vixl::MemOperand HeapOperand(const vixl::Register& base, size_t offset = 0) {
150 // A heap reference must be 32bit, so fit in a W register.
151 DCHECK(base.IsW());
152 return vixl::MemOperand(base.X(), offset);
153}
154
Alexandre Rames82000b02015-07-07 11:34:16 +0100155static inline vixl::MemOperand HeapOperand(const vixl::Register& base,
156 const vixl::Register& regoffset,
157 vixl::Shift shift = vixl::LSL,
158 unsigned shift_amount = 0) {
159 // A heap reference must be 32bit, so fit in a W register.
160 DCHECK(base.IsW());
161 return vixl::MemOperand(base.X(), regoffset, shift, shift_amount);
162}
163
Andreas Gampe878d58c2015-01-15 23:24:00 -0800164static inline vixl::MemOperand HeapOperand(const vixl::Register& base, Offset offset) {
165 return HeapOperand(base, offset.SizeValue());
166}
167
168static inline vixl::MemOperand HeapOperandFrom(Location location, Offset offset) {
169 return HeapOperand(RegisterFrom(location, Primitive::kPrimNot), offset);
170}
171
172static inline Location LocationFrom(const vixl::Register& reg) {
173 return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.code()));
174}
175
176static inline Location LocationFrom(const vixl::FPRegister& fpreg) {
177 return Location::FpuRegisterLocation(fpreg.code());
178}
179
180static inline vixl::Operand OperandFromMemOperand(const vixl::MemOperand& mem_op) {
181 if (mem_op.IsImmediateOffset()) {
182 return vixl::Operand(mem_op.offset());
183 } else {
184 DCHECK(mem_op.IsRegisterOffset());
185 if (mem_op.extend() != vixl::NO_EXTEND) {
186 return vixl::Operand(mem_op.regoffset(), mem_op.extend(), mem_op.shift_amount());
187 } else if (mem_op.shift() != vixl::NO_SHIFT) {
188 return vixl::Operand(mem_op.regoffset(), mem_op.shift(), mem_op.shift_amount());
189 } else {
190 LOG(FATAL) << "Should not reach here";
191 UNREACHABLE();
192 }
193 }
194}
195
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000196static bool CanEncodeConstantAsImmediate(HConstant* constant, HInstruction* instr) {
197 DCHECK(constant->IsIntConstant() || constant->IsLongConstant() || constant->IsNullConstant());
198
199 // For single uses we let VIXL handle the constant generation since it will
200 // use registers that are not managed by the register allocator (wip0, wip1).
201 if (constant->GetUses().HasOnlyOneUse()) {
202 return true;
203 }
204
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000205 // Our code generator ensures shift distances are within an encodable range.
206 if (instr->IsRor()) {
207 return true;
208 }
209
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000210 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
211
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100212 if (instr->IsAnd() || instr->IsOr() || instr->IsXor()) {
213 // Uses logical operations.
214 return vixl::Assembler::IsImmLogical(value, vixl::kXRegSize);
215 } else if (instr->IsNeg()) {
216 // Uses mov -immediate.
217 return vixl::Assembler::IsImmMovn(value, vixl::kXRegSize);
218 } else {
219 DCHECK(instr->IsAdd() ||
220 instr->IsArm64IntermediateAddress() ||
221 instr->IsBoundsCheck() ||
222 instr->IsCompare() ||
223 instr->IsCondition() ||
224 instr->IsSub());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000225 // Uses aliases of ADD/SUB instructions.
Alexandre Ramesb69fbfb2015-10-16 09:08:46 +0100226 // If `value` does not fit but `-value` does, VIXL will automatically use
227 // the 'opposite' instruction.
228 return vixl::Assembler::IsImmAddSub(value) || vixl::Assembler::IsImmAddSub(-value);
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +0000229 }
230}
231
232static inline Location ARM64EncodableConstantOrRegister(HInstruction* constant,
233 HInstruction* instr) {
234 if (constant->IsConstant()
235 && CanEncodeConstantAsImmediate(constant->AsConstant(), instr)) {
236 return Location::ConstantLocation(constant->AsConstant());
237 }
238
239 return Location::RequiresRegister();
240}
241
Zheng Xuda403092015-04-24 17:35:39 +0800242// Check if registers in art register set have the same register code in vixl. If the register
243// codes are same, we can initialize vixl register list simply by the register masks. Currently,
244// only SP/WSP and ZXR/WZR codes are different between art and vixl.
245// Note: This function is only used for debug checks.
246static inline bool ArtVixlRegCodeCoherentForRegSet(uint32_t art_core_registers,
247 size_t num_core,
248 uint32_t art_fpu_registers,
249 size_t num_fpu) {
250 // The register masks won't work if the number of register is larger than 32.
251 DCHECK_GE(sizeof(art_core_registers) * 8, num_core);
252 DCHECK_GE(sizeof(art_fpu_registers) * 8, num_fpu);
253 for (size_t art_reg_code = 0; art_reg_code < num_core; ++art_reg_code) {
254 if (RegisterSet::Contains(art_core_registers, art_reg_code)) {
255 if (art_reg_code != static_cast<size_t>(VIXLRegCodeFromART(art_reg_code))) {
256 return false;
257 }
258 }
259 }
260 // There is no register code translation for float registers.
261 return true;
262}
263
Alexandre Rames8626b742015-11-25 16:28:08 +0000264static inline vixl::Shift ShiftFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
265 switch (op_kind) {
266 case HArm64DataProcWithShifterOp::kASR: return vixl::ASR;
267 case HArm64DataProcWithShifterOp::kLSL: return vixl::LSL;
268 case HArm64DataProcWithShifterOp::kLSR: return vixl::LSR;
269 default:
270 LOG(FATAL) << "Unexpected op kind " << op_kind;
271 UNREACHABLE();
272 return vixl::NO_SHIFT;
273 }
274}
275
276static inline vixl::Extend ExtendFromOpKind(HArm64DataProcWithShifterOp::OpKind op_kind) {
277 switch (op_kind) {
278 case HArm64DataProcWithShifterOp::kUXTB: return vixl::UXTB;
279 case HArm64DataProcWithShifterOp::kUXTH: return vixl::UXTH;
280 case HArm64DataProcWithShifterOp::kUXTW: return vixl::UXTW;
281 case HArm64DataProcWithShifterOp::kSXTB: return vixl::SXTB;
282 case HArm64DataProcWithShifterOp::kSXTH: return vixl::SXTH;
283 case HArm64DataProcWithShifterOp::kSXTW: return vixl::SXTW;
284 default:
285 LOG(FATAL) << "Unexpected op kind " << op_kind;
286 UNREACHABLE();
287 return vixl::NO_EXTEND;
288 }
289}
290
291static inline bool CanFitInShifterOperand(HInstruction* instruction) {
292 if (instruction->IsTypeConversion()) {
293 HTypeConversion* conversion = instruction->AsTypeConversion();
294 Primitive::Type result_type = conversion->GetResultType();
295 Primitive::Type input_type = conversion->GetInputType();
296 // We don't expect to see the same type as input and result.
297 return Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type) &&
298 (result_type != input_type);
299 } else {
300 return (instruction->IsShl() && instruction->AsShl()->InputAt(1)->IsIntConstant()) ||
301 (instruction->IsShr() && instruction->AsShr()->InputAt(1)->IsIntConstant()) ||
302 (instruction->IsUShr() && instruction->AsUShr()->InputAt(1)->IsIntConstant());
303 }
304}
305
306static inline bool HasShifterOperand(HInstruction* instr) {
307 // `neg` instructions are an alias of `sub` using the zero register as the
308 // first register input.
309 bool res = instr->IsAdd() || instr->IsAnd() || instr->IsNeg() ||
310 instr->IsOr() || instr->IsSub() || instr->IsXor();
311 return res;
312}
313
314static inline bool ShifterOperandSupportsExtension(HInstruction* instruction) {
315 DCHECK(HasShifterOperand(instruction));
316 // Although the `neg` instruction is an alias of the `sub` instruction, `HNeg`
317 // does *not* support extension. This is because the `extended register` form
318 // of the `sub` instruction interprets the left register with code 31 as the
319 // stack pointer and not the zero register. (So does the `immediate` form.) In
320 // the other form `shifted register, the register with code 31 is interpreted
321 // as the zero register.
322 return instruction->IsAdd() || instruction->IsSub();
323}
324
Andreas Gampe878d58c2015-01-15 23:24:00 -0800325} // namespace helpers
326} // namespace arm64
327} // namespace art
328
329#endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_