blob: 7077f98b15ff9849f91e5c738f5267526729aa21 [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
20#include "locations.h"
21#include "nodes.h"
22#include "utils/arm64/assembler_arm64.h"
23#include "a64/disasm-a64.h"
24#include "a64/macro-assembler-a64.h"
25
26namespace art {
27namespace arm64 {
28namespace helpers {
29
30constexpr bool IsFPType(Primitive::Type type) {
31 return type == Primitive::kPrimFloat || type == Primitive::kPrimDouble;
32}
33
34static inline bool IsIntegralType(Primitive::Type type) {
35 switch (type) {
36 case Primitive::kPrimByte:
37 case Primitive::kPrimChar:
38 case Primitive::kPrimShort:
39 case Primitive::kPrimInt:
40 case Primitive::kPrimLong:
41 return true;
42 default:
43 return false;
44 }
45}
46
47constexpr bool Is64BitType(Primitive::Type type) {
48 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
49}
50
51// Convenience helpers to ease conversion to and from VIXL operands.
52static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
53 "Unexpected values for register codes.");
54
55static inline int VIXLRegCodeFromART(int code) {
56 if (code == SP) {
57 return vixl::kSPRegInternalCode;
58 }
59 if (code == XZR) {
60 return vixl::kZeroRegCode;
61 }
62 return code;
63}
64
65static inline int ARTRegCodeFromVIXL(int code) {
66 if (code == vixl::kSPRegInternalCode) {
67 return SP;
68 }
69 if (code == vixl::kZeroRegCode) {
70 return XZR;
71 }
72 return code;
73}
74
75static inline vixl::Register XRegisterFrom(Location location) {
76 DCHECK(location.IsRegister());
77 return vixl::Register::XRegFromCode(VIXLRegCodeFromART(location.reg()));
78}
79
80static inline vixl::Register WRegisterFrom(Location location) {
81 DCHECK(location.IsRegister());
82 return vixl::Register::WRegFromCode(VIXLRegCodeFromART(location.reg()));
83}
84
85static inline vixl::Register RegisterFrom(Location location, Primitive::Type type) {
86 DCHECK(type != Primitive::kPrimVoid && !IsFPType(type));
87 return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location);
88}
89
90static inline vixl::Register OutputRegister(HInstruction* instr) {
91 return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
92}
93
94static inline vixl::Register InputRegisterAt(HInstruction* instr, int input_index) {
95 return RegisterFrom(instr->GetLocations()->InAt(input_index),
96 instr->InputAt(input_index)->GetType());
97}
98
99static inline vixl::FPRegister DRegisterFrom(Location location) {
100 DCHECK(location.IsFpuRegister());
101 return vixl::FPRegister::DRegFromCode(location.reg());
102}
103
104static inline vixl::FPRegister SRegisterFrom(Location location) {
105 DCHECK(location.IsFpuRegister());
106 return vixl::FPRegister::SRegFromCode(location.reg());
107}
108
109static inline vixl::FPRegister FPRegisterFrom(Location location, Primitive::Type type) {
110 DCHECK(IsFPType(type));
111 return type == Primitive::kPrimDouble ? DRegisterFrom(location) : SRegisterFrom(location);
112}
113
114static inline vixl::FPRegister OutputFPRegister(HInstruction* instr) {
115 return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
116}
117
118static inline vixl::FPRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
119 return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
120 instr->InputAt(input_index)->GetType());
121}
122
123static inline vixl::CPURegister CPURegisterFrom(Location location, Primitive::Type type) {
124 return IsFPType(type) ? vixl::CPURegister(FPRegisterFrom(location, type))
125 : vixl::CPURegister(RegisterFrom(location, type));
126}
127
128static inline vixl::CPURegister OutputCPURegister(HInstruction* instr) {
129 return IsFPType(instr->GetType()) ? static_cast<vixl::CPURegister>(OutputFPRegister(instr))
130 : static_cast<vixl::CPURegister>(OutputRegister(instr));
131}
132
133static inline vixl::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
134 return IsFPType(instr->InputAt(index)->GetType())
135 ? static_cast<vixl::CPURegister>(InputFPRegisterAt(instr, index))
136 : static_cast<vixl::CPURegister>(InputRegisterAt(instr, index));
137}
138
139static inline int64_t Int64ConstantFrom(Location location) {
140 HConstant* instr = location.GetConstant();
141 return instr->IsIntConstant() ? instr->AsIntConstant()->GetValue()
142 : instr->AsLongConstant()->GetValue();
143}
144
145static inline vixl::Operand OperandFrom(Location location, Primitive::Type type) {
146 if (location.IsRegister()) {
147 return vixl::Operand(RegisterFrom(location, type));
148 } else {
149 return vixl::Operand(Int64ConstantFrom(location));
150 }
151}
152
153static inline vixl::Operand InputOperandAt(HInstruction* instr, int input_index) {
154 return OperandFrom(instr->GetLocations()->InAt(input_index),
155 instr->InputAt(input_index)->GetType());
156}
157
158static inline vixl::MemOperand StackOperandFrom(Location location) {
159 return vixl::MemOperand(vixl::sp, location.GetStackIndex());
160}
161
162static inline vixl::MemOperand HeapOperand(const vixl::Register& base, size_t offset = 0) {
163 // A heap reference must be 32bit, so fit in a W register.
164 DCHECK(base.IsW());
165 return vixl::MemOperand(base.X(), offset);
166}
167
168static inline vixl::MemOperand HeapOperand(const vixl::Register& base, Offset offset) {
169 return HeapOperand(base, offset.SizeValue());
170}
171
172static inline vixl::MemOperand HeapOperandFrom(Location location, Offset offset) {
173 return HeapOperand(RegisterFrom(location, Primitive::kPrimNot), offset);
174}
175
176static inline Location LocationFrom(const vixl::Register& reg) {
177 return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.code()));
178}
179
180static inline Location LocationFrom(const vixl::FPRegister& fpreg) {
181 return Location::FpuRegisterLocation(fpreg.code());
182}
183
184static inline vixl::Operand OperandFromMemOperand(const vixl::MemOperand& mem_op) {
185 if (mem_op.IsImmediateOffset()) {
186 return vixl::Operand(mem_op.offset());
187 } else {
188 DCHECK(mem_op.IsRegisterOffset());
189 if (mem_op.extend() != vixl::NO_EXTEND) {
190 return vixl::Operand(mem_op.regoffset(), mem_op.extend(), mem_op.shift_amount());
191 } else if (mem_op.shift() != vixl::NO_SHIFT) {
192 return vixl::Operand(mem_op.regoffset(), mem_op.shift(), mem_op.shift_amount());
193 } else {
194 LOG(FATAL) << "Should not reach here";
195 UNREACHABLE();
196 }
197 }
198}
199
200} // namespace helpers
201} // namespace arm64
202} // namespace art
203
204#endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_