1 /*
2 * Copyright (C) 2011 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 #include <algorithm>
18
19 #include "instruction_set_features.h"
20
21 #include <algorithm>
22 #include <ostream>
23
24 #include "android-base/strings.h"
25
26 #include "base/casts.h"
27 #include "base/utils.h"
28
29 #include "arm/instruction_set_features_arm.h"
30 #include "arm64/instruction_set_features_arm64.h"
31 #include "x86/instruction_set_features_x86.h"
32 #include "x86_64/instruction_set_features_x86_64.h"
33
34 namespace art {
35
FromVariant(InstructionSet isa,const std::string & variant,std::string * error_msg)36 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromVariant(
37 InstructionSet isa, const std::string& variant, std::string* error_msg) {
38 switch (isa) {
39 case InstructionSet::kArm:
40 case InstructionSet::kThumb2:
41 return ArmInstructionSetFeatures::FromVariant(variant, error_msg);
42 case InstructionSet::kArm64:
43 return Arm64InstructionSetFeatures::FromVariant(variant, error_msg);
44 case InstructionSet::kX86:
45 return X86InstructionSetFeatures::FromVariant(variant, error_msg);
46 case InstructionSet::kX86_64:
47 return X86_64InstructionSetFeatures::FromVariant(variant, error_msg);
48
49 default:
50 break;
51 }
52 UNIMPLEMENTED(FATAL) << isa;
53 UNREACHABLE();
54 }
55
FromBitmap(InstructionSet isa,uint32_t bitmap)56 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromBitmap(InstructionSet isa,
57 uint32_t bitmap) {
58 std::unique_ptr<const InstructionSetFeatures> result;
59 switch (isa) {
60 case InstructionSet::kArm:
61 case InstructionSet::kThumb2:
62 result = ArmInstructionSetFeatures::FromBitmap(bitmap);
63 break;
64 case InstructionSet::kArm64:
65 result = Arm64InstructionSetFeatures::FromBitmap(bitmap);
66 break;
67 case InstructionSet::kX86:
68 result = X86InstructionSetFeatures::FromBitmap(bitmap);
69 break;
70 case InstructionSet::kX86_64:
71 result = X86_64InstructionSetFeatures::FromBitmap(bitmap);
72 break;
73
74 default:
75 UNIMPLEMENTED(FATAL) << isa;
76 UNREACHABLE();
77 }
78 CHECK_EQ(bitmap, result->AsBitmap());
79 return result;
80 }
81
FromCppDefines()82 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCppDefines() {
83 switch (kRuntimeISA) {
84 case InstructionSet::kArm:
85 case InstructionSet::kThumb2:
86 return ArmInstructionSetFeatures::FromCppDefines();
87 case InstructionSet::kArm64:
88 return Arm64InstructionSetFeatures::FromCppDefines();
89 case InstructionSet::kX86:
90 return X86InstructionSetFeatures::FromCppDefines();
91 case InstructionSet::kX86_64:
92 return X86_64InstructionSetFeatures::FromCppDefines();
93
94 default:
95 break;
96 }
97 UNIMPLEMENTED(FATAL) << kRuntimeISA;
98 UNREACHABLE();
99 }
100
FromRuntimeDetection()101 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromRuntimeDetection() {
102 switch (kRuntimeISA) {
103 #ifdef ART_TARGET_ANDROID
104 case InstructionSet::kArm64:
105 return Arm64InstructionSetFeatures::FromHwcap();
106 #endif
107 default:
108 return nullptr;
109 }
110 }
111
FromCpuInfo()112 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCpuInfo() {
113 switch (kRuntimeISA) {
114 case InstructionSet::kArm:
115 case InstructionSet::kThumb2:
116 return ArmInstructionSetFeatures::FromCpuInfo();
117 case InstructionSet::kArm64:
118 return Arm64InstructionSetFeatures::FromCpuInfo();
119 case InstructionSet::kX86:
120 return X86InstructionSetFeatures::FromCpuInfo();
121 case InstructionSet::kX86_64:
122 return X86_64InstructionSetFeatures::FromCpuInfo();
123
124 default:
125 break;
126 }
127 UNIMPLEMENTED(FATAL) << kRuntimeISA;
128 UNREACHABLE();
129 }
130
FromHwcap()131 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromHwcap() {
132 switch (kRuntimeISA) {
133 case InstructionSet::kArm:
134 case InstructionSet::kThumb2:
135 return ArmInstructionSetFeatures::FromHwcap();
136 case InstructionSet::kArm64:
137 return Arm64InstructionSetFeatures::FromHwcap();
138 case InstructionSet::kX86:
139 return X86InstructionSetFeatures::FromHwcap();
140 case InstructionSet::kX86_64:
141 return X86_64InstructionSetFeatures::FromHwcap();
142
143 default:
144 break;
145 }
146 UNIMPLEMENTED(FATAL) << kRuntimeISA;
147 UNREACHABLE();
148 }
149
FromAssembly()150 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromAssembly() {
151 switch (kRuntimeISA) {
152 case InstructionSet::kArm:
153 case InstructionSet::kThumb2:
154 return ArmInstructionSetFeatures::FromAssembly();
155 case InstructionSet::kArm64:
156 return Arm64InstructionSetFeatures::FromAssembly();
157 case InstructionSet::kX86:
158 return X86InstructionSetFeatures::FromAssembly();
159 case InstructionSet::kX86_64:
160 return X86_64InstructionSetFeatures::FromAssembly();
161
162 default:
163 break;
164 }
165 UNIMPLEMENTED(FATAL) << kRuntimeISA;
166 UNREACHABLE();
167 }
168
AddFeaturesFromString(const std::string & feature_list,std::string * error_msg) const169 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddFeaturesFromString(
170 const std::string& feature_list, /* out */ std::string* error_msg) const {
171 std::vector<std::string> features;
172 Split(feature_list, ',', &features);
173 std::transform(std::begin(features), std::end(features), std::begin(features),
174 [](const std::string &s) { return android::base::Trim(s); });
175 auto empty_strings_begin = std::copy_if(std::begin(features), std::end(features),
176 std::begin(features),
177 [](const std::string& s) { return !s.empty(); });
178 features.erase(empty_strings_begin, std::end(features));
179 if (features.empty()) {
180 *error_msg = "No instruction set features specified";
181 return nullptr;
182 }
183
184 bool use_default = false;
185 bool use_runtime_detection = false;
186 for (const std::string& feature : features) {
187 if (feature == "default") {
188 if (features.size() > 1) {
189 *error_msg = "Specific instruction set feature(s) cannot be used when 'default' is used.";
190 return nullptr;
191 }
192 use_default = true;
193 features.pop_back();
194 break;
195 } else if (feature == "runtime") {
196 if (features.size() > 1) {
197 *error_msg = "Specific instruction set feature(s) cannot be used when 'runtime' is used.";
198 return nullptr;
199 }
200 use_runtime_detection = true;
201 features.pop_back();
202 break;
203 }
204 }
205 // Expectation: "default" and "runtime" are standalone, no other feature names.
206 // But an empty features vector after processing can also come along if the
207 // handled feature names are the only ones in the list. So
208 // logically, we check "default or runtime => features.empty."
209 DCHECK((!use_default && !use_runtime_detection) || features.empty());
210
211 std::unique_ptr<const InstructionSetFeatures> runtime_detected_features;
212 if (use_runtime_detection) {
213 runtime_detected_features = FromRuntimeDetection();
214 }
215
216 if (runtime_detected_features != nullptr) {
217 return AddRuntimeDetectedFeatures(runtime_detected_features.get());
218 } else {
219 return AddFeaturesFromSplitString(features, error_msg);
220 }
221 }
222
AsArmInstructionSetFeatures() const223 const ArmInstructionSetFeatures* InstructionSetFeatures::AsArmInstructionSetFeatures() const {
224 DCHECK_EQ(InstructionSet::kArm, GetInstructionSet());
225 return down_cast<const ArmInstructionSetFeatures*>(this);
226 }
227
AsArm64InstructionSetFeatures() const228 const Arm64InstructionSetFeatures* InstructionSetFeatures::AsArm64InstructionSetFeatures() const {
229 DCHECK_EQ(InstructionSet::kArm64, GetInstructionSet());
230 return down_cast<const Arm64InstructionSetFeatures*>(this);
231 }
232
AsX86InstructionSetFeatures() const233 const X86InstructionSetFeatures* InstructionSetFeatures::AsX86InstructionSetFeatures() const {
234 DCHECK(InstructionSet::kX86 == GetInstructionSet() ||
235 InstructionSet::kX86_64 == GetInstructionSet());
236 return down_cast<const X86InstructionSetFeatures*>(this);
237 }
238
AsX86_64InstructionSetFeatures() const239 const X86_64InstructionSetFeatures* InstructionSetFeatures::AsX86_64InstructionSetFeatures() const {
240 DCHECK_EQ(InstructionSet::kX86_64, GetInstructionSet());
241 return down_cast<const X86_64InstructionSetFeatures*>(this);
242 }
243
FindVariantInArray(const char * const variants[],size_t num_variants,const std::string & variant)244 bool InstructionSetFeatures::FindVariantInArray(const char* const variants[], size_t num_variants,
245 const std::string& variant) {
246 const char* const * begin = variants;
247 const char* const * end = begin + num_variants;
248 return std::find(begin, end, variant) != end;
249 }
250
AddRuntimeDetectedFeatures(const InstructionSetFeatures * features ATTRIBUTE_UNUSED) const251 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddRuntimeDetectedFeatures(
252 const InstructionSetFeatures *features ATTRIBUTE_UNUSED) const {
253 UNIMPLEMENTED(FATAL) << kRuntimeISA;
254 UNREACHABLE();
255 }
256
operator <<(std::ostream & os,const InstructionSetFeatures & rhs)257 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs) {
258 os << "ISA: " << rhs.GetInstructionSet() << " Feature string: " << rhs.GetFeatureString();
259 return os;
260 }
261
262 } // namespace art
263