blob: fc9565ceafad4a38982a4564684b159c3ed8429e [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"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010021#include <array>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022#include <bitset>
23#include <initializer_list>
24#include <string>
25#include <vector>
26
27namespace llvm {
28
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029class raw_ostream;
30class Triple;
31
Andrew Walbran3d2c1972020-04-07 12:24:26 +010032const unsigned MAX_SUBTARGET_WORDS = 3;
33const unsigned MAX_SUBTARGET_FEATURES = MAX_SUBTARGET_WORDS * 64;
34
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035/// 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.
38class FeatureBitset : public std::bitset<MAX_SUBTARGET_FEATURES> {
39public:
40 // Cannot inherit constructors because it's not supported by VC++..
41 FeatureBitset() = default;
42
43 FeatureBitset(const bitset<MAX_SUBTARGET_FEATURES>& B) : bitset(B) {}
44
45 FeatureBitset(std::initializer_list<unsigned> Init) {
46 for (auto I : Init)
47 set(I);
48 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049
Andrew Walbran3d2c1972020-04-07 12:24:26 +010050 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);
53 if (LHS != RHS)
54 return LHS < RHS;
55 }
56 return false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 }
58};
59
Andrew Walbran3d2c1972020-04-07 12:24:26 +010060/// 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.
63class FeatureBitArray {
64 std::array<uint64_t, MAX_SUBTARGET_WORDS> Bits;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065
Andrew Walbran3d2c1972020-04-07 12:24:26 +010066public:
67 constexpr FeatureBitArray(const std::array<uint64_t, MAX_SUBTARGET_WORDS> &B)
68 : Bits(B) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069
Andrew Walbran3d2c1972020-04-07 12:24:26 +010070 FeatureBitset getAsBitset() const {
71 FeatureBitset Result;
72
73 for (unsigned i = 0, e = Bits.size(); i != e; ++i)
74 Result |= FeatureBitset(Bits[i]) << (64 * i);
75
76 return Result;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077 }
78};
79
80//===----------------------------------------------------------------------===//
81
82/// Manages the enabling and disabling of subtarget specific features.
83///
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
89/// specification.
90class SubtargetFeatures {
91 std::vector<std::string> Features; ///< Subtarget features as a vector
92
93public:
94 explicit SubtargetFeatures(StringRef Initial = "");
95
96 /// Returns features as a string.
97 std::string getString() const;
98
99 /// Adds Features.
100 void AddFeature(StringRef String, bool Enable = true);
101
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 /// Returns the vector of individual subtarget features.
103 const std::vector<std::string> &getFeatures() const { return Features; }
104
105 /// Prints feature string.
106 void print(raw_ostream &OS) const;
107
108 // Dumps feature info.
109 void dump() const;
110
111 /// Adds the default features for the specified target triple.
112 void getDefaultSubtargetFeatures(const Triple& Triple);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100113
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 =='-';
121 }
122
123 /// Return string stripped of flag.
124 static std::string StripFlag(StringRef Feature) {
125 return hasFlag(Feature) ? Feature.substr(1) : Feature;
126 }
127
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
134 return Ch == '+';
135 }
136
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);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100139};
140
141} // end namespace llvm
142
143#endif // LLVM_MC_SUBTARGETFEATURE_H