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"
22 #include <initializer_list>
28 template <typename T
> class ArrayRef
;
32 const unsigned MAX_SUBTARGET_FEATURES
= 192;
33 /// Container class for subtarget features.
34 /// This is convenient because std::bitset does not have a constructor
35 /// with an initializer list of set bits.
36 class FeatureBitset
: public std::bitset
<MAX_SUBTARGET_FEATURES
> {
38 // Cannot inherit constructors because it's not supported by VC++..
39 FeatureBitset() = default;
41 FeatureBitset(const bitset
<MAX_SUBTARGET_FEATURES
>& B
) : bitset(B
) {}
43 FeatureBitset(std::initializer_list
<unsigned> Init
) {
49 //===----------------------------------------------------------------------===//
51 /// Used to provide key value pairs for feature and CPU bit flags.
52 struct SubtargetFeatureKV
{
53 const char *Key
; ///< K-V key string
54 const char *Desc
; ///< Help descriptor
55 unsigned Value
; ///< K-V integer value
56 FeatureBitset Implies
; ///< K-V bit mask
58 /// Compare routine for std::lower_bound
59 bool operator<(StringRef S
) const {
60 return StringRef(Key
) < S
;
63 /// Compare routine for std::is_sorted.
64 bool operator<(const SubtargetFeatureKV
&Other
) const {
65 return StringRef(Key
) < StringRef(Other
.Key
);
69 //===----------------------------------------------------------------------===//
71 /// Used to provide key value pairs for CPU and arbitrary pointers.
72 struct SubtargetInfoKV
{
73 const char *Key
; ///< K-V key string
74 const void *Value
; ///< K-V pointer value
76 /// Compare routine for std::lower_bound
77 bool operator<(StringRef S
) const {
78 return StringRef(Key
) < S
;
82 //===----------------------------------------------------------------------===//
84 /// Manages the enabling and disabling of subtarget specific features.
86 /// Features are encoded as a string of the form
87 /// "+attr1,+attr2,-attr3,...,+attrN"
88 /// A comma separates each feature from the next (all lowercase.)
89 /// Each of the remaining features is prefixed with + or - indicating whether
90 /// that feature should be enabled or disabled contrary to the cpu
92 class SubtargetFeatures
{
93 std::vector
<std::string
> Features
; ///< Subtarget features as a vector
96 explicit SubtargetFeatures(StringRef Initial
= "");
98 /// Returns features as a string.
99 std::string
getString() const;
102 void AddFeature(StringRef String
, bool Enable
= true);
104 /// Toggles a feature and update the feature bits.
105 static void ToggleFeature(FeatureBitset
&Bits
, StringRef String
,
106 ArrayRef
<SubtargetFeatureKV
> FeatureTable
);
108 /// Applies the feature flag and update the feature bits.
109 static void ApplyFeatureFlag(FeatureBitset
&Bits
, StringRef Feature
,
110 ArrayRef
<SubtargetFeatureKV
> FeatureTable
);
112 /// Returns feature bits of a CPU.
113 FeatureBitset
getFeatureBits(StringRef CPU
,
114 ArrayRef
<SubtargetFeatureKV
> CPUTable
,
115 ArrayRef
<SubtargetFeatureKV
> FeatureTable
);
117 /// Returns the vector of individual subtarget features.
118 const std::vector
<std::string
> &getFeatures() const { return Features
; }
120 /// Prints feature string.
121 void print(raw_ostream
&OS
) const;
123 // Dumps feature info.
126 /// Adds the default features for the specified target triple.
127 void getDefaultSubtargetFeatures(const Triple
& Triple
);
130 } // end namespace llvm
132 #endif // LLVM_MC_SUBTARGETFEATURE_H