blob: 5f49d3894ae6e6ca9093dab4517de83f32440b87 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/MC/SubtargetFeature.h - CPU characteristics ---------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_MC_SUBTARGETFEATURE_H
18#define LLVM_MC_SUBTARGETFEATURE_H
19
20#include "llvm/ADT/StringRef.h"
21#include <bitset>
22#include <initializer_list>
23#include <string>
24#include <vector>
25
26namespace llvm {
27
28template <typename T> class ArrayRef;
29class raw_ostream;
30class Triple;
31
32const 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.
36class FeatureBitset : public std::bitset<MAX_SUBTARGET_FEATURES> {
37public:
38 // Cannot inherit constructors because it's not supported by VC++..
39 FeatureBitset() = default;
40
41 FeatureBitset(const bitset<MAX_SUBTARGET_FEATURES>& B) : bitset(B) {}
42
43 FeatureBitset(std::initializer_list<unsigned> Init) {
44 for (auto I : Init)
45 set(I);
46 }
47};
48
49//===----------------------------------------------------------------------===//
50
51/// Used to provide key value pairs for feature and CPU bit flags.
52struct SubtargetFeatureKV {
53 const char *Key; ///< K-V key string
54 const char *Desc; ///< Help descriptor
55 FeatureBitset Value; ///< K-V integer value
56 FeatureBitset Implies; ///< K-V bit mask
57
58 /// Compare routine for std::lower_bound
59 bool operator<(StringRef S) const {
60 return StringRef(Key) < S;
61 }
62
63 /// Compare routine for std::is_sorted.
64 bool operator<(const SubtargetFeatureKV &Other) const {
65 return StringRef(Key) < StringRef(Other.Key);
66 }
67};
68
69//===----------------------------------------------------------------------===//
70
71/// Used to provide key value pairs for CPU and arbitrary pointers.
72struct SubtargetInfoKV {
73 const char *Key; ///< K-V key string
74 const void *Value; ///< K-V pointer value
75
76 /// Compare routine for std::lower_bound
77 bool operator<(StringRef S) const {
78 return StringRef(Key) < S;
79 }
80};
81
82//===----------------------------------------------------------------------===//
83
84/// Manages the enabling and disabling of subtarget specific features.
85///
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
91/// specification.
92class SubtargetFeatures {
93 std::vector<std::string> Features; ///< Subtarget features as a vector
94
95public:
96 explicit SubtargetFeatures(StringRef Initial = "");
97
98 /// Returns features as a string.
99 std::string getString() const;
100
101 /// Adds Features.
102 void AddFeature(StringRef String, bool Enable = true);
103
104 /// Toggles a feature and update the feature bits.
105 static void ToggleFeature(FeatureBitset &Bits, StringRef String,
106 ArrayRef<SubtargetFeatureKV> FeatureTable);
107
108 /// Applies the feature flag and update the feature bits.
109 static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
110 ArrayRef<SubtargetFeatureKV> FeatureTable);
111
112 /// Returns feature bits of a CPU.
113 FeatureBitset getFeatureBits(StringRef CPU,
114 ArrayRef<SubtargetFeatureKV> CPUTable,
115 ArrayRef<SubtargetFeatureKV> FeatureTable);
116
117 /// Returns the vector of individual subtarget features.
118 const std::vector<std::string> &getFeatures() const { return Features; }
119
120 /// Prints feature string.
121 void print(raw_ostream &OS) const;
122
123 // Dumps feature info.
124 void dump() const;
125
126 /// Adds the default features for the specified target triple.
127 void getDefaultSubtargetFeatures(const Triple& Triple);
128};
129
130} // end namespace llvm
131
132#endif // LLVM_MC_SUBTARGETFEATURE_H