1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
22*795d594fSAndroid Build Coastguard Worker #include "instruction_simplifier_shared.h"
23*795d594fSAndroid Build Coastguard Worker #include "locations.h"
24*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
25*795d594fSAndroid Build Coastguard Worker #include "utils/arm64/assembler_arm64.h"
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker // TODO(VIXL): Make VIXL compile cleanly with -Wshadow, -Wdeprecated-declarations.
28*795d594fSAndroid Build Coastguard Worker #pragma GCC diagnostic push
29*795d594fSAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wshadow"
30*795d594fSAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31*795d594fSAndroid Build Coastguard Worker #include "aarch64/disasm-aarch64.h"
32*795d594fSAndroid Build Coastguard Worker #include "aarch64/macro-assembler-aarch64.h"
33*795d594fSAndroid Build Coastguard Worker #include "aarch64/simulator-aarch64.h"
34*795d594fSAndroid Build Coastguard Worker #pragma GCC diagnostic pop
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker using helpers::CanFitInShifterOperand;
39*795d594fSAndroid Build Coastguard Worker using helpers::HasShifterOperand;
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker namespace arm64 {
42*795d594fSAndroid Build Coastguard Worker namespace helpers {
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker // Convenience helpers to ease conversion to and from VIXL operands.
45*795d594fSAndroid Build Coastguard Worker static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
46*795d594fSAndroid Build Coastguard Worker "Unexpected values for register codes.");
47*795d594fSAndroid Build Coastguard Worker
VIXLRegCodeFromART(int code)48*795d594fSAndroid Build Coastguard Worker inline int VIXLRegCodeFromART(int code) {
49*795d594fSAndroid Build Coastguard Worker if (code == SP) {
50*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::kSPRegInternalCode;
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker if (code == XZR) {
53*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::kZeroRegCode;
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker return code;
56*795d594fSAndroid Build Coastguard Worker }
57*795d594fSAndroid Build Coastguard Worker
ARTRegCodeFromVIXL(int code)58*795d594fSAndroid Build Coastguard Worker inline int ARTRegCodeFromVIXL(int code) {
59*795d594fSAndroid Build Coastguard Worker if (code == vixl::aarch64::kSPRegInternalCode) {
60*795d594fSAndroid Build Coastguard Worker return SP;
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker if (code == vixl::aarch64::kZeroRegCode) {
63*795d594fSAndroid Build Coastguard Worker return XZR;
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker return code;
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker
XRegisterFrom(Location location)68*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Register XRegisterFrom(Location location) {
69*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsRegister()) << location;
70*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::XRegister(VIXLRegCodeFromART(location.reg()));
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker
WRegisterFrom(Location location)73*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Register WRegisterFrom(Location location) {
74*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsRegister()) << location;
75*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::WRegister(VIXLRegCodeFromART(location.reg()));
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker
RegisterFrom(Location location,DataType::Type type)78*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Register RegisterFrom(Location location, DataType::Type type) {
79*795d594fSAndroid Build Coastguard Worker DCHECK(type != DataType::Type::kVoid && !DataType::IsFloatingPointType(type)) << type;
80*795d594fSAndroid Build Coastguard Worker return type == DataType::Type::kInt64 ? XRegisterFrom(location) : WRegisterFrom(location);
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker
OutputRegister(HInstruction * instr)83*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Register OutputRegister(HInstruction* instr) {
84*795d594fSAndroid Build Coastguard Worker return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
85*795d594fSAndroid Build Coastguard Worker }
86*795d594fSAndroid Build Coastguard Worker
InputRegisterAt(HInstruction * instr,int input_index)87*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Register InputRegisterAt(HInstruction* instr, int input_index) {
88*795d594fSAndroid Build Coastguard Worker return RegisterFrom(instr->GetLocations()->InAt(input_index),
89*795d594fSAndroid Build Coastguard Worker instr->InputAt(input_index)->GetType());
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
DRegisterFrom(Location location)92*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister DRegisterFrom(Location location) {
93*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
94*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::DRegister(location.reg());
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker
QRegisterFrom(Location location)97*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister QRegisterFrom(Location location) {
98*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
99*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::QRegister(location.reg());
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker
VRegisterFrom(Location location)102*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister VRegisterFrom(Location location) {
103*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
104*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::VRegister(location.reg());
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
ZRegisterFrom(Location location)107*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::ZRegister ZRegisterFrom(Location location) {
108*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
109*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::ZRegister(location.reg());
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker
SRegisterFrom(Location location)112*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister SRegisterFrom(Location location) {
113*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
114*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::SRegister(location.reg());
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker
HRegisterFrom(Location location)117*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister HRegisterFrom(Location location) {
118*795d594fSAndroid Build Coastguard Worker DCHECK(location.IsFpuRegister()) << location;
119*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::HRegister(location.reg());
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker
FPRegisterFrom(Location location,DataType::Type type)122*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister FPRegisterFrom(Location location, DataType::Type type) {
123*795d594fSAndroid Build Coastguard Worker DCHECK(DataType::IsFloatingPointType(type)) << type;
124*795d594fSAndroid Build Coastguard Worker return type == DataType::Type::kFloat64 ? DRegisterFrom(location) : SRegisterFrom(location);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker
OutputFPRegister(HInstruction * instr)127*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister OutputFPRegister(HInstruction* instr) {
128*795d594fSAndroid Build Coastguard Worker return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker
InputFPRegisterAt(HInstruction * instr,int input_index)131*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::VRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
132*795d594fSAndroid Build Coastguard Worker return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
133*795d594fSAndroid Build Coastguard Worker instr->InputAt(input_index)->GetType());
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker
CPURegisterFrom(Location location,DataType::Type type)136*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::CPURegister CPURegisterFrom(Location location, DataType::Type type) {
137*795d594fSAndroid Build Coastguard Worker return DataType::IsFloatingPointType(type)
138*795d594fSAndroid Build Coastguard Worker ? vixl::aarch64::CPURegister(FPRegisterFrom(location, type))
139*795d594fSAndroid Build Coastguard Worker : vixl::aarch64::CPURegister(RegisterFrom(location, type));
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker
OutputCPURegister(HInstruction * instr)142*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::CPURegister OutputCPURegister(HInstruction* instr) {
143*795d594fSAndroid Build Coastguard Worker return DataType::IsFloatingPointType(instr->GetType())
144*795d594fSAndroid Build Coastguard Worker ? static_cast<vixl::aarch64::CPURegister>(OutputFPRegister(instr))
145*795d594fSAndroid Build Coastguard Worker : static_cast<vixl::aarch64::CPURegister>(OutputRegister(instr));
146*795d594fSAndroid Build Coastguard Worker }
147*795d594fSAndroid Build Coastguard Worker
InputCPURegisterAt(HInstruction * instr,int index)148*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
149*795d594fSAndroid Build Coastguard Worker return DataType::IsFloatingPointType(instr->InputAt(index)->GetType())
150*795d594fSAndroid Build Coastguard Worker ? static_cast<vixl::aarch64::CPURegister>(InputFPRegisterAt(instr, index))
151*795d594fSAndroid Build Coastguard Worker : static_cast<vixl::aarch64::CPURegister>(InputRegisterAt(instr, index));
152*795d594fSAndroid Build Coastguard Worker }
153*795d594fSAndroid Build Coastguard Worker
InputCPURegisterOrZeroRegAt(HInstruction * instr,int index)154*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::CPURegister InputCPURegisterOrZeroRegAt(HInstruction* instr,
155*795d594fSAndroid Build Coastguard Worker int index) {
156*795d594fSAndroid Build Coastguard Worker HInstruction* input = instr->InputAt(index);
157*795d594fSAndroid Build Coastguard Worker DataType::Type input_type = input->GetType();
158*795d594fSAndroid Build Coastguard Worker if (IsZeroBitPattern(input)) {
159*795d594fSAndroid Build Coastguard Worker return (DataType::Size(input_type) >= vixl::aarch64::kXRegSizeInBytes)
160*795d594fSAndroid Build Coastguard Worker ? vixl::aarch64::Register(vixl::aarch64::xzr)
161*795d594fSAndroid Build Coastguard Worker : vixl::aarch64::Register(vixl::aarch64::wzr);
162*795d594fSAndroid Build Coastguard Worker }
163*795d594fSAndroid Build Coastguard Worker return InputCPURegisterAt(instr, index);
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker
Int64FromLocation(Location location)166*795d594fSAndroid Build Coastguard Worker inline int64_t Int64FromLocation(Location location) {
167*795d594fSAndroid Build Coastguard Worker return Int64FromConstant(location.GetConstant());
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker
OperandFrom(Location location,DataType::Type type)170*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Operand OperandFrom(Location location, DataType::Type type) {
171*795d594fSAndroid Build Coastguard Worker if (location.IsRegister()) {
172*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Operand(RegisterFrom(location, type));
173*795d594fSAndroid Build Coastguard Worker } else {
174*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Operand(Int64FromLocation(location));
175*795d594fSAndroid Build Coastguard Worker }
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker
InputOperandAt(HInstruction * instr,int input_index)178*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Operand InputOperandAt(HInstruction* instr, int input_index) {
179*795d594fSAndroid Build Coastguard Worker return OperandFrom(instr->GetLocations()->InAt(input_index),
180*795d594fSAndroid Build Coastguard Worker instr->InputAt(input_index)->GetType());
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
StackOperandFrom(Location location)183*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::MemOperand StackOperandFrom(Location location) {
184*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::MemOperand(vixl::aarch64::sp, location.GetStackIndex());
185*795d594fSAndroid Build Coastguard Worker }
186*795d594fSAndroid Build Coastguard Worker
SveStackOperandFrom(Location location)187*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::SVEMemOperand SveStackOperandFrom(Location location) {
188*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::SVEMemOperand(vixl::aarch64::sp, location.GetStackIndex());
189*795d594fSAndroid Build Coastguard Worker }
190*795d594fSAndroid Build Coastguard Worker
191*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
192*795d594fSAndroid Build Coastguard Worker size_t offset = 0) {
193*795d594fSAndroid Build Coastguard Worker // A heap reference must be 32bit, so fit in a W register.
194*795d594fSAndroid Build Coastguard Worker DCHECK(base.IsW());
195*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::MemOperand(base.X(), offset);
196*795d594fSAndroid Build Coastguard Worker }
197*795d594fSAndroid Build Coastguard Worker
198*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
199*795d594fSAndroid Build Coastguard Worker const vixl::aarch64::Register& regoffset,
200*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Shift shift = vixl::aarch64::LSL,
201*795d594fSAndroid Build Coastguard Worker unsigned shift_amount = 0) {
202*795d594fSAndroid Build Coastguard Worker // A heap reference must be 32bit, so fit in a W register.
203*795d594fSAndroid Build Coastguard Worker DCHECK(base.IsW());
204*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::MemOperand(base.X(), regoffset, shift, shift_amount);
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker
HeapOperand(const vixl::aarch64::Register & base,Offset offset)207*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::MemOperand HeapOperand(const vixl::aarch64::Register& base,
208*795d594fSAndroid Build Coastguard Worker Offset offset) {
209*795d594fSAndroid Build Coastguard Worker return HeapOperand(base, offset.SizeValue());
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker
HeapOperandFrom(Location location,Offset offset)212*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::MemOperand HeapOperandFrom(Location location, Offset offset) {
213*795d594fSAndroid Build Coastguard Worker return HeapOperand(RegisterFrom(location, DataType::Type::kReference), offset);
214*795d594fSAndroid Build Coastguard Worker }
215*795d594fSAndroid Build Coastguard Worker
LocationFrom(const vixl::aarch64::Register & reg)216*795d594fSAndroid Build Coastguard Worker inline Location LocationFrom(const vixl::aarch64::Register& reg) {
217*795d594fSAndroid Build Coastguard Worker return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.GetCode()));
218*795d594fSAndroid Build Coastguard Worker }
219*795d594fSAndroid Build Coastguard Worker
LocationFrom(const vixl::aarch64::VRegister & fpreg)220*795d594fSAndroid Build Coastguard Worker inline Location LocationFrom(const vixl::aarch64::VRegister& fpreg) {
221*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterLocation(fpreg.GetCode());
222*795d594fSAndroid Build Coastguard Worker }
223*795d594fSAndroid Build Coastguard Worker
LocationFrom(const vixl::aarch64::ZRegister & zreg)224*795d594fSAndroid Build Coastguard Worker inline Location LocationFrom(const vixl::aarch64::ZRegister& zreg) {
225*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterLocation(zreg.GetCode());
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker
OperandFromMemOperand(const vixl::aarch64::MemOperand & mem_op)228*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Operand OperandFromMemOperand(
229*795d594fSAndroid Build Coastguard Worker const vixl::aarch64::MemOperand& mem_op) {
230*795d594fSAndroid Build Coastguard Worker if (mem_op.IsImmediateOffset()) {
231*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Operand(mem_op.GetOffset());
232*795d594fSAndroid Build Coastguard Worker } else {
233*795d594fSAndroid Build Coastguard Worker DCHECK(mem_op.IsRegisterOffset());
234*795d594fSAndroid Build Coastguard Worker if (mem_op.GetExtend() != vixl::aarch64::NO_EXTEND) {
235*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
236*795d594fSAndroid Build Coastguard Worker mem_op.GetExtend(),
237*795d594fSAndroid Build Coastguard Worker mem_op.GetShiftAmount());
238*795d594fSAndroid Build Coastguard Worker } else if (mem_op.GetShift() != vixl::aarch64::NO_SHIFT) {
239*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Operand(mem_op.GetRegisterOffset(),
240*795d594fSAndroid Build Coastguard Worker mem_op.GetShift(),
241*795d594fSAndroid Build Coastguard Worker mem_op.GetShiftAmount());
242*795d594fSAndroid Build Coastguard Worker } else {
243*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Should not reach here";
244*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
245*795d594fSAndroid Build Coastguard Worker }
246*795d594fSAndroid Build Coastguard Worker }
247*795d594fSAndroid Build Coastguard Worker }
248*795d594fSAndroid Build Coastguard Worker
AddSubCanEncodeAsImmediate(int64_t value)249*795d594fSAndroid Build Coastguard Worker inline bool AddSubCanEncodeAsImmediate(int64_t value) {
250*795d594fSAndroid Build Coastguard Worker // If `value` does not fit but `-value` does, VIXL will automatically use
251*795d594fSAndroid Build Coastguard Worker // the 'opposite' instruction.
252*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Assembler::IsImmAddSub(value)
253*795d594fSAndroid Build Coastguard Worker || vixl::aarch64::Assembler::IsImmAddSub(-value);
254*795d594fSAndroid Build Coastguard Worker }
255*795d594fSAndroid Build Coastguard Worker
Arm64CanEncodeConstantAsImmediate(HConstant * constant,HInstruction * instr)256*795d594fSAndroid Build Coastguard Worker inline bool Arm64CanEncodeConstantAsImmediate(HConstant* constant, HInstruction* instr) {
257*795d594fSAndroid Build Coastguard Worker int64_t value = CodeGenerator::GetInt64ValueOf(constant);
258*795d594fSAndroid Build Coastguard Worker
259*795d594fSAndroid Build Coastguard Worker // TODO: Improve this when IsSIMDConstantEncodable method is implemented in VIXL.
260*795d594fSAndroid Build Coastguard Worker if (instr->IsVecReplicateScalar()) {
261*795d594fSAndroid Build Coastguard Worker if (constant->IsLongConstant()) {
262*795d594fSAndroid Build Coastguard Worker return false;
263*795d594fSAndroid Build Coastguard Worker } else if (constant->IsFloatConstant()) {
264*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Assembler::IsImmFP32(constant->AsFloatConstant()->GetValue());
265*795d594fSAndroid Build Coastguard Worker } else if (constant->IsDoubleConstant()) {
266*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Assembler::IsImmFP64(constant->AsDoubleConstant()->GetValue());
267*795d594fSAndroid Build Coastguard Worker }
268*795d594fSAndroid Build Coastguard Worker return IsUint<8>(value);
269*795d594fSAndroid Build Coastguard Worker }
270*795d594fSAndroid Build Coastguard Worker
271*795d594fSAndroid Build Coastguard Worker // Code generation for Min/Max:
272*795d594fSAndroid Build Coastguard Worker // Cmp left_op, right_op
273*795d594fSAndroid Build Coastguard Worker // Csel dst, left_op, right_op, cond
274*795d594fSAndroid Build Coastguard Worker if (instr->IsMin() || instr->IsMax()) {
275*795d594fSAndroid Build Coastguard Worker if (constant->GetUses().HasExactlyOneElement()) {
276*795d594fSAndroid Build Coastguard Worker // If value can be encoded as immediate for the Cmp, then let VIXL handle
277*795d594fSAndroid Build Coastguard Worker // the constant generation for the Csel.
278*795d594fSAndroid Build Coastguard Worker return AddSubCanEncodeAsImmediate(value);
279*795d594fSAndroid Build Coastguard Worker }
280*795d594fSAndroid Build Coastguard Worker // These values are encodable as immediates for Cmp and VIXL will use csinc and csinv
281*795d594fSAndroid Build Coastguard Worker // with the zr register as right_op, hence no constant generation is required.
282*795d594fSAndroid Build Coastguard Worker return constant->IsZeroBitPattern() || constant->IsOne() || constant->IsMinusOne();
283*795d594fSAndroid Build Coastguard Worker }
284*795d594fSAndroid Build Coastguard Worker
285*795d594fSAndroid Build Coastguard Worker // For single uses we let VIXL handle the constant generation since it will
286*795d594fSAndroid Build Coastguard Worker // use registers that are not managed by the register allocator (wip0, wip1).
287*795d594fSAndroid Build Coastguard Worker if (constant->GetUses().HasExactlyOneElement()) {
288*795d594fSAndroid Build Coastguard Worker return true;
289*795d594fSAndroid Build Coastguard Worker }
290*795d594fSAndroid Build Coastguard Worker
291*795d594fSAndroid Build Coastguard Worker // Our code generator ensures shift distances are within an encodable range.
292*795d594fSAndroid Build Coastguard Worker if (instr->IsRor()) {
293*795d594fSAndroid Build Coastguard Worker return true;
294*795d594fSAndroid Build Coastguard Worker }
295*795d594fSAndroid Build Coastguard Worker
296*795d594fSAndroid Build Coastguard Worker if (instr->IsAnd() || instr->IsOr() || instr->IsXor()) {
297*795d594fSAndroid Build Coastguard Worker // Uses logical operations.
298*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Assembler::IsImmLogical(value, vixl::aarch64::kXRegSize);
299*795d594fSAndroid Build Coastguard Worker } else if (instr->IsNeg()) {
300*795d594fSAndroid Build Coastguard Worker // Uses mov -immediate.
301*795d594fSAndroid Build Coastguard Worker return vixl::aarch64::Assembler::IsImmMovn(value, vixl::aarch64::kXRegSize);
302*795d594fSAndroid Build Coastguard Worker } else {
303*795d594fSAndroid Build Coastguard Worker DCHECK(instr->IsAdd() ||
304*795d594fSAndroid Build Coastguard Worker instr->IsIntermediateAddress() ||
305*795d594fSAndroid Build Coastguard Worker instr->IsBoundsCheck() ||
306*795d594fSAndroid Build Coastguard Worker instr->IsCompare() ||
307*795d594fSAndroid Build Coastguard Worker instr->IsCondition() ||
308*795d594fSAndroid Build Coastguard Worker instr->IsSub())
309*795d594fSAndroid Build Coastguard Worker << instr->DebugName();
310*795d594fSAndroid Build Coastguard Worker // Uses aliases of ADD/SUB instructions.
311*795d594fSAndroid Build Coastguard Worker return AddSubCanEncodeAsImmediate(value);
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker }
314*795d594fSAndroid Build Coastguard Worker
ARM64EncodableConstantOrRegister(HInstruction * constant,HInstruction * instr)315*795d594fSAndroid Build Coastguard Worker inline Location ARM64EncodableConstantOrRegister(HInstruction* constant, HInstruction* instr) {
316*795d594fSAndroid Build Coastguard Worker if (constant->IsConstant() && Arm64CanEncodeConstantAsImmediate(constant->AsConstant(), instr)) {
317*795d594fSAndroid Build Coastguard Worker return Location::ConstantLocation(constant);
318*795d594fSAndroid Build Coastguard Worker }
319*795d594fSAndroid Build Coastguard Worker
320*795d594fSAndroid Build Coastguard Worker return Location::RequiresRegister();
321*795d594fSAndroid Build Coastguard Worker }
322*795d594fSAndroid Build Coastguard Worker
323*795d594fSAndroid Build Coastguard Worker // Check if registers in art register set have the same register code in vixl. If the register
324*795d594fSAndroid Build Coastguard Worker // codes are same, we can initialize vixl register list simply by the register masks. Currently,
325*795d594fSAndroid Build Coastguard Worker // only SP/WSP and ZXR/WZR codes are different between art and vixl.
326*795d594fSAndroid Build Coastguard Worker // Note: This function is only used for debug checks.
ArtVixlRegCodeCoherentForRegSet(uint32_t art_core_registers,size_t num_core,uint32_t art_fpu_registers,size_t num_fpu)327*795d594fSAndroid Build Coastguard Worker inline bool ArtVixlRegCodeCoherentForRegSet(uint32_t art_core_registers,
328*795d594fSAndroid Build Coastguard Worker size_t num_core,
329*795d594fSAndroid Build Coastguard Worker uint32_t art_fpu_registers,
330*795d594fSAndroid Build Coastguard Worker size_t num_fpu) {
331*795d594fSAndroid Build Coastguard Worker // The register masks won't work if the number of register is larger than 32.
332*795d594fSAndroid Build Coastguard Worker DCHECK_GE(sizeof(art_core_registers) * 8, num_core);
333*795d594fSAndroid Build Coastguard Worker DCHECK_GE(sizeof(art_fpu_registers) * 8, num_fpu);
334*795d594fSAndroid Build Coastguard Worker for (size_t art_reg_code = 0; art_reg_code < num_core; ++art_reg_code) {
335*795d594fSAndroid Build Coastguard Worker if (RegisterSet::Contains(art_core_registers, art_reg_code)) {
336*795d594fSAndroid Build Coastguard Worker if (art_reg_code != static_cast<size_t>(VIXLRegCodeFromART(art_reg_code))) {
337*795d594fSAndroid Build Coastguard Worker return false;
338*795d594fSAndroid Build Coastguard Worker }
339*795d594fSAndroid Build Coastguard Worker }
340*795d594fSAndroid Build Coastguard Worker }
341*795d594fSAndroid Build Coastguard Worker // There is no register code translation for float registers.
342*795d594fSAndroid Build Coastguard Worker return true;
343*795d594fSAndroid Build Coastguard Worker }
344*795d594fSAndroid Build Coastguard Worker
ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind)345*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
346*795d594fSAndroid Build Coastguard Worker switch (op_kind) {
347*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kASR: return vixl::aarch64::ASR;
348*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kLSL: return vixl::aarch64::LSL;
349*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kLSR: return vixl::aarch64::LSR;
350*795d594fSAndroid Build Coastguard Worker default:
351*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unexpected op kind " << op_kind;
352*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
353*795d594fSAndroid Build Coastguard Worker }
354*795d594fSAndroid Build Coastguard Worker }
355*795d594fSAndroid Build Coastguard Worker
ExtendFromOpKind(HDataProcWithShifterOp::OpKind op_kind)356*795d594fSAndroid Build Coastguard Worker inline vixl::aarch64::Extend ExtendFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
357*795d594fSAndroid Build Coastguard Worker switch (op_kind) {
358*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kUXTB: return vixl::aarch64::UXTB;
359*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kUXTH: return vixl::aarch64::UXTH;
360*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kUXTW: return vixl::aarch64::UXTW;
361*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kSXTB: return vixl::aarch64::SXTB;
362*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kSXTH: return vixl::aarch64::SXTH;
363*795d594fSAndroid Build Coastguard Worker case HDataProcWithShifterOp::kSXTW: return vixl::aarch64::SXTW;
364*795d594fSAndroid Build Coastguard Worker default:
365*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unexpected op kind " << op_kind;
366*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
367*795d594fSAndroid Build Coastguard Worker }
368*795d594fSAndroid Build Coastguard Worker }
369*795d594fSAndroid Build Coastguard Worker
ShifterOperandSupportsExtension(HInstruction * instruction)370*795d594fSAndroid Build Coastguard Worker inline bool ShifterOperandSupportsExtension(HInstruction* instruction) {
371*795d594fSAndroid Build Coastguard Worker DCHECK(HasShifterOperand(instruction, InstructionSet::kArm64));
372*795d594fSAndroid Build Coastguard Worker // Although the `neg` instruction is an alias of the `sub` instruction, `HNeg`
373*795d594fSAndroid Build Coastguard Worker // does *not* support extension. This is because the `extended register` form
374*795d594fSAndroid Build Coastguard Worker // of the `sub` instruction interprets the left register with code 31 as the
375*795d594fSAndroid Build Coastguard Worker // stack pointer and not the zero register. (So does the `immediate` form.) In
376*795d594fSAndroid Build Coastguard Worker // the other form `shifted register, the register with code 31 is interpreted
377*795d594fSAndroid Build Coastguard Worker // as the zero register.
378*795d594fSAndroid Build Coastguard Worker return instruction->IsAdd() || instruction->IsSub();
379*795d594fSAndroid Build Coastguard Worker }
380*795d594fSAndroid Build Coastguard Worker
381*795d594fSAndroid Build Coastguard Worker } // namespace helpers
382*795d594fSAndroid Build Coastguard Worker } // namespace arm64
383*795d594fSAndroid Build Coastguard Worker } // namespace art
384*795d594fSAndroid Build Coastguard Worker
385*795d594fSAndroid Build Coastguard Worker #endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
386