1 //===- llvm/MC/SubtargetFeature.h - CPU characteristics ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file Defines and manages user or tool specified CPU characteristics.
11 /// The intent is to be able to package specific features that should or should
12 /// not be used on a specific target processor. A tool, such as llc, could, as
13 /// as example, gather chip info from the command line, a long with features
14 /// that should be used on that chip.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_MC_SUBTARGETFEATURE_H
19 #define LLVM_MC_SUBTARGETFEATURE_H
21 #include "llvm/ADT/StringRef.h"
23 #include <initializer_list>
29 template <typename T> class ArrayRef;
33 const unsigned MAX_SUBTARGET_FEATURES = 192;
34 /// Container class for subtarget features.
35 /// This is convenient because std::bitset does not have a constructor
36 /// with an initializer list of set bits.
37 class FeatureBitset : public std::bitset<MAX_SUBTARGET_FEATURES> {
39 // Cannot inherit constructors because it's not supported by VC++..
40 FeatureBitset() = default;
42 FeatureBitset(const bitset<MAX_SUBTARGET_FEATURES>& B) : bitset(B) {}
44 FeatureBitset(std::initializer_list<unsigned> Init) {
50 //===----------------------------------------------------------------------===//
52 /// Used to provide key value pairs for feature and CPU bit flags.
53 struct SubtargetFeatureKV {
54 const char *Key; ///< K-V key string
55 const char *Desc; ///< Help descriptor
56 FeatureBitset Value; ///< K-V integer value
57 FeatureBitset Implies; ///< K-V bit mask
59 /// Compare routine for std::lower_bound
60 bool operator<(StringRef S) const {
61 return StringRef(Key) < S;
64 /// Compare routine for std::is_sorted.
65 bool operator<(const SubtargetFeatureKV &Other) const {
66 return StringRef(Key) < StringRef(Other.Key);
70 //===----------------------------------------------------------------------===//
72 /// Used to provide key value pairs for CPU and arbitrary pointers.
73 struct SubtargetInfoKV {
74 const char *Key; ///< K-V key string
75 const void *Value; ///< K-V pointer value
77 /// Compare routine for std::lower_bound
78 bool operator<(StringRef S) const {
79 return StringRef(Key) < S;
83 //===----------------------------------------------------------------------===//
85 /// Manages the enabling and disabling of subtarget specific features.
87 /// Features are encoded as a string of the form
88 /// "+attr1,+attr2,-attr3,...,+attrN"
89 /// A comma separates each feature from the next (all lowercase.)
90 /// Each of the remaining features is prefixed with + or - indicating whether
91 /// that feature should be enabled or disabled contrary to the cpu
93 class SubtargetFeatures {
94 std::vector<std::string> Features; ///< Subtarget features as a vector
97 explicit SubtargetFeatures(StringRef Initial = "");
99 /// Returns features as a string.
100 std::string getString() const;
103 void AddFeature(StringRef String, bool Enable = true);
105 /// Toggles a feature and update the feature bits.
106 static void ToggleFeature(FeatureBitset &Bits, StringRef String,
107 ArrayRef<SubtargetFeatureKV> FeatureTable);
109 /// Applies the feature flag and update the feature bits.
110 static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
111 ArrayRef<SubtargetFeatureKV> FeatureTable);
113 /// Returns feature bits of a CPU.
114 FeatureBitset getFeatureBits(StringRef CPU,
115 ArrayRef<SubtargetFeatureKV> CPUTable,
116 ArrayRef<SubtargetFeatureKV> FeatureTable);
118 /// Returns the vector of individual subtarget features.
119 const std::vector<std::string> &getFeatures() const { return Features; }
121 /// Prints feature string.
122 void print(raw_ostream &OS) const;
124 // Dumps feature info.
127 /// Adds the default features for the specified target triple.
128 void getDefaultSubtargetFeatures(const Triple& Triple);
131 } // end namespace llvm
133 #endif // LLVM_MC_SUBTARGETFEATURE_H