1 /*
2  * Copyright (C) 2016 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_ARM_H_
18 #define ART_COMPILER_OPTIMIZING_COMMON_ARM_H_
19 
20 #include "instruction_simplifier_shared.h"
21 #include "locations.h"
22 #include "nodes.h"
23 #include "utils/arm/constants_arm.h"
24 
25 // TODO(VIXL): Make VIXL compile with -Wshadow.
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Wshadow"
28 #include "aarch32/macro-assembler-aarch32.h"
29 #pragma GCC diagnostic pop
30 
31 namespace art {
32 
33 using helpers::HasShifterOperand;
34 
35 namespace arm {
36 namespace helpers {
37 
38 static_assert(vixl::aarch32::kSpCode == SP, "vixl::aarch32::kSpCode must equal ART's SP");
39 
HighRegisterFrom(Location location)40 inline vixl::aarch32::Register HighRegisterFrom(Location location) {
41   DCHECK(location.IsRegisterPair()) << location;
42   return vixl::aarch32::Register(location.AsRegisterPairHigh<vixl::aarch32::Register>());
43 }
44 
HighDRegisterFrom(Location location)45 inline vixl::aarch32::DRegister HighDRegisterFrom(Location location) {
46   DCHECK(location.IsFpuRegisterPair()) << location;
47   return vixl::aarch32::DRegister(location.AsFpuRegisterPairHigh<vixl::aarch32::DRegister>());
48 }
49 
LowRegisterFrom(Location location)50 inline vixl::aarch32::Register LowRegisterFrom(Location location) {
51   DCHECK(location.IsRegisterPair()) << location;
52   return vixl::aarch32::Register(location.AsRegisterPairLow<vixl::aarch32::Register>());
53 }
54 
LowSRegisterFrom(Location location)55 inline vixl::aarch32::SRegister LowSRegisterFrom(Location location) {
56   DCHECK(location.IsFpuRegisterPair()) << location;
57   return vixl::aarch32::SRegister(location.AsFpuRegisterPairLow<vixl::aarch32::SRegister>());
58 }
59 
HighSRegisterFrom(Location location)60 inline vixl::aarch32::SRegister HighSRegisterFrom(Location location) {
61   DCHECK(location.IsFpuRegisterPair()) << location;
62   return vixl::aarch32::SRegister(location.AsFpuRegisterPairHigh<vixl::aarch32::SRegister>());
63 }
64 
RegisterFrom(Location location)65 inline vixl::aarch32::Register RegisterFrom(Location location) {
66   DCHECK(location.IsRegister()) << location;
67   return vixl::aarch32::Register(location.reg());
68 }
69 
RegisterFrom(Location location,DataType::Type type)70 inline vixl::aarch32::Register RegisterFrom(Location location, DataType::Type type) {
71   DCHECK(type != DataType::Type::kVoid && !DataType::IsFloatingPointType(type)) << type;
72   return RegisterFrom(location);
73 }
74 
DRegisterFrom(Location location)75 inline vixl::aarch32::DRegister DRegisterFrom(Location location) {
76   DCHECK(location.IsFpuRegisterPair()) << location;
77   int reg_code = location.low();
78   DCHECK_EQ(reg_code % 2, 0) << reg_code;
79   return vixl::aarch32::DRegister(reg_code / 2);
80 }
81 
SRegisterFrom(Location location)82 inline vixl::aarch32::SRegister SRegisterFrom(Location location) {
83   DCHECK(location.IsFpuRegister()) << location;
84   return vixl::aarch32::SRegister(location.reg());
85 }
86 
OutputSRegister(HInstruction * instr)87 inline vixl::aarch32::SRegister OutputSRegister(HInstruction* instr) {
88   DataType::Type type = instr->GetType();
89   DCHECK_EQ(type, DataType::Type::kFloat32) << type;
90   return SRegisterFrom(instr->GetLocations()->Out());
91 }
92 
OutputDRegister(HInstruction * instr)93 inline vixl::aarch32::DRegister OutputDRegister(HInstruction* instr) {
94   DataType::Type type = instr->GetType();
95   DCHECK_EQ(type, DataType::Type::kFloat64) << type;
96   return DRegisterFrom(instr->GetLocations()->Out());
97 }
98 
OutputVRegister(HInstruction * instr)99 inline vixl::aarch32::VRegister OutputVRegister(HInstruction* instr) {
100   DataType::Type type = instr->GetType();
101   if (type == DataType::Type::kFloat32) {
102     return OutputSRegister(instr);
103   } else {
104     return OutputDRegister(instr);
105   }
106 }
107 
InputSRegisterAt(HInstruction * instr,int input_index)108 inline vixl::aarch32::SRegister InputSRegisterAt(HInstruction* instr, int input_index) {
109   DataType::Type type = instr->InputAt(input_index)->GetType();
110   DCHECK_EQ(type, DataType::Type::kFloat32) << type;
111   return SRegisterFrom(instr->GetLocations()->InAt(input_index));
112 }
113 
InputDRegisterAt(HInstruction * instr,int input_index)114 inline vixl::aarch32::DRegister InputDRegisterAt(HInstruction* instr, int input_index) {
115   DataType::Type type = instr->InputAt(input_index)->GetType();
116   DCHECK_EQ(type, DataType::Type::kFloat64) << type;
117   return DRegisterFrom(instr->GetLocations()->InAt(input_index));
118 }
119 
InputVRegisterAt(HInstruction * instr,int input_index)120 inline vixl::aarch32::VRegister InputVRegisterAt(HInstruction* instr, int input_index) {
121   DataType::Type type = instr->InputAt(input_index)->GetType();
122   if (type == DataType::Type::kFloat32) {
123     return InputSRegisterAt(instr, input_index);
124   } else {
125     DCHECK_EQ(type, DataType::Type::kFloat64);
126     return InputDRegisterAt(instr, input_index);
127   }
128 }
129 
InputVRegister(HInstruction * instr)130 inline vixl::aarch32::VRegister InputVRegister(HInstruction* instr) {
131   DCHECK_EQ(instr->InputCount(), 1u);
132   return InputVRegisterAt(instr, 0);
133 }
134 
OutputRegister(HInstruction * instr)135 inline vixl::aarch32::Register OutputRegister(HInstruction* instr) {
136   return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
137 }
138 
InputRegisterAt(HInstruction * instr,int input_index)139 inline vixl::aarch32::Register InputRegisterAt(HInstruction* instr, int input_index) {
140   return RegisterFrom(instr->GetLocations()->InAt(input_index),
141                       instr->InputAt(input_index)->GetType());
142 }
143 
InputRegister(HInstruction * instr)144 inline vixl::aarch32::Register InputRegister(HInstruction* instr) {
145   DCHECK_EQ(instr->InputCount(), 1u);
146   return InputRegisterAt(instr, 0);
147 }
148 
DRegisterFromS(vixl::aarch32::SRegister s)149 inline vixl::aarch32::DRegister DRegisterFromS(vixl::aarch32::SRegister s) {
150   vixl::aarch32::DRegister d = vixl::aarch32::DRegister(s.GetCode() / 2);
151   DCHECK(s.Is(d.GetLane(0)) || s.Is(d.GetLane(1)));
152   return d;
153 }
154 
Int32ConstantFrom(HInstruction * instr)155 inline int32_t Int32ConstantFrom(HInstruction* instr) {
156   if (instr->IsIntConstant()) {
157     return instr->AsIntConstant()->GetValue();
158   } else if (instr->IsNullConstant()) {
159     return 0;
160   } else {
161     DCHECK(instr->IsLongConstant()) << instr->DebugName();
162     const int64_t ret = instr->AsLongConstant()->GetValue();
163     DCHECK_GE(ret, std::numeric_limits<int32_t>::min());
164     DCHECK_LE(ret, std::numeric_limits<int32_t>::max());
165     return ret;
166   }
167 }
168 
Int32ConstantFrom(Location location)169 inline int32_t Int32ConstantFrom(Location location) {
170   return Int32ConstantFrom(location.GetConstant());
171 }
172 
Int64ConstantFrom(Location location)173 inline int64_t Int64ConstantFrom(Location location) {
174   HConstant* instr = location.GetConstant();
175   if (instr->IsIntConstant()) {
176     return instr->AsIntConstant()->GetValue();
177   } else if (instr->IsNullConstant()) {
178     return 0;
179   } else {
180     DCHECK(instr->IsLongConstant()) << instr->DebugName();
181     return instr->AsLongConstant()->GetValue();
182   }
183 }
184 
Uint64ConstantFrom(HInstruction * instr)185 inline uint64_t Uint64ConstantFrom(HInstruction* instr) {
186   DCHECK(instr->IsConstant()) << instr->DebugName();
187   return instr->AsConstant()->GetValueAsUint64();
188 }
189 
OperandFrom(Location location,DataType::Type type)190 inline vixl::aarch32::Operand OperandFrom(Location location, DataType::Type type) {
191   if (location.IsRegister()) {
192     return vixl::aarch32::Operand(RegisterFrom(location, type));
193   } else {
194     return vixl::aarch32::Operand(Int32ConstantFrom(location));
195   }
196 }
197 
InputOperandAt(HInstruction * instr,int input_index)198 inline vixl::aarch32::Operand InputOperandAt(HInstruction* instr, int input_index) {
199   return OperandFrom(instr->GetLocations()->InAt(input_index),
200                      instr->InputAt(input_index)->GetType());
201 }
202 
LocationFrom(const vixl::aarch32::Register & reg)203 inline Location LocationFrom(const vixl::aarch32::Register& reg) {
204   return Location::RegisterLocation(reg.GetCode());
205 }
206 
LocationFrom(const vixl::aarch32::SRegister & reg)207 inline Location LocationFrom(const vixl::aarch32::SRegister& reg) {
208   return Location::FpuRegisterLocation(reg.GetCode());
209 }
210 
LocationFrom(const vixl::aarch32::Register & low,const vixl::aarch32::Register & high)211 inline Location LocationFrom(const vixl::aarch32::Register& low,
212                              const vixl::aarch32::Register& high) {
213   return Location::RegisterPairLocation(low.GetCode(), high.GetCode());
214 }
215 
LocationFrom(const vixl::aarch32::SRegister & low,const vixl::aarch32::SRegister & high)216 inline Location LocationFrom(const vixl::aarch32::SRegister& low,
217                              const vixl::aarch32::SRegister& high) {
218   return Location::FpuRegisterPairLocation(low.GetCode(), high.GetCode());
219 }
220 
221 }  // namespace helpers
222 }  // namespace arm
223 }  // namespace art
224 
225 #endif  // ART_COMPILER_OPTIMIZING_COMMON_ARM_H_
226