1 //===- llvm/MC/SubtargetFeature.h - CPU characteristics ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// \file Defines and manages user or tool specified CPU characteristics.
10 /// The intent is to be able to package specific features that should or should
11 /// not be used on a specific target processor. A tool, such as llc, could, as
12 /// as example, gather chip info from the command line, a long with features
13 /// that should be used on that chip.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_MC_SUBTARGETFEATURE_H
18 #define LLVM_MC_SUBTARGETFEATURE_H
20 #include "llvm/ADT/StringRef.h"
23 #include <initializer_list>
32 const unsigned MAX_SUBTARGET_WORDS
= 3;
33 const unsigned MAX_SUBTARGET_FEATURES
= MAX_SUBTARGET_WORDS
* 64;
35 /// Container class for subtarget features.
36 /// This is convenient because std::bitset does not have a constructor
37 /// with an initializer list of set bits.
38 class FeatureBitset
: public std::bitset
<MAX_SUBTARGET_FEATURES
> {
40 // Cannot inherit constructors because it's not supported by VC++..
41 FeatureBitset() = default;
43 FeatureBitset(const bitset
<MAX_SUBTARGET_FEATURES
>& B
) : bitset(B
) {}
45 FeatureBitset(std::initializer_list
<unsigned> Init
) {
50 bool operator < (const FeatureBitset
&Other
) const {
51 for (unsigned I
= 0, E
= size(); I
!= E
; ++I
) {
52 bool LHS
= test(I
), RHS
= Other
.test(I
);
60 /// Class used to store the subtarget bits in the tables created by tablegen.
61 /// The std::initializer_list constructor of FeatureBitset can't be done at
62 /// compile time and requires a static constructor to run at startup.
63 class FeatureBitArray
{
64 std::array
<uint64_t, MAX_SUBTARGET_WORDS
> Bits
;
67 constexpr FeatureBitArray(const std::array
<uint64_t, MAX_SUBTARGET_WORDS
> &B
)
70 FeatureBitset
getAsBitset() const {
73 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
)
74 Result
|= FeatureBitset(Bits
[i
]) << (64 * i
);
80 //===----------------------------------------------------------------------===//
82 /// Manages the enabling and disabling of subtarget specific features.
84 /// Features are encoded as a string of the form
85 /// "+attr1,+attr2,-attr3,...,+attrN"
86 /// A comma separates each feature from the next (all lowercase.)
87 /// Each of the remaining features is prefixed with + or - indicating whether
88 /// that feature should be enabled or disabled contrary to the cpu
90 class SubtargetFeatures
{
91 std::vector
<std::string
> Features
; ///< Subtarget features as a vector
94 explicit SubtargetFeatures(StringRef Initial
= "");
96 /// Returns features as a string.
97 std::string
getString() const;
100 void AddFeature(StringRef String
, bool Enable
= true);
102 /// Returns the vector of individual subtarget features.
103 const std::vector
<std::string
> &getFeatures() const { return Features
; }
105 /// Prints feature string.
106 void print(raw_ostream
&OS
) const;
108 // Dumps feature info.
111 /// Adds the default features for the specified target triple.
112 void getDefaultSubtargetFeatures(const Triple
& Triple
);
114 /// Determine if a feature has a flag; '+' or '-'
115 static bool hasFlag(StringRef Feature
) {
116 assert(!Feature
.empty() && "Empty string");
117 // Get first character
118 char Ch
= Feature
[0];
119 // Check if first character is '+' or '-' flag
120 return Ch
== '+' || Ch
=='-';
123 /// Return string stripped of flag.
124 static std::string
StripFlag(StringRef Feature
) {
125 return hasFlag(Feature
) ? Feature
.substr(1) : Feature
;
128 /// Return true if enable flag; '+'.
129 static inline bool isEnabled(StringRef Feature
) {
130 assert(!Feature
.empty() && "Empty string");
131 // Get first character
132 char Ch
= Feature
[0];
133 // Check if first character is '+' for enabled
137 /// Splits a string of comma separated items in to a vector of strings.
138 static void Split(std::vector
<std::string
> &V
, StringRef S
);
141 } // end namespace llvm
143 #endif // LLVM_MC_SUBTARGETFEATURE_H