Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/MC/SubtargetFeature.h - CPU characteristics ---------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 21 | #include <array> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | #include <bitset> |
| 23 | #include <initializer_list> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 29 | class raw_ostream; |
| 30 | class Triple; |
| 31 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 32 | const unsigned MAX_SUBTARGET_WORDS = 3; |
| 33 | const unsigned MAX_SUBTARGET_FEATURES = MAX_SUBTARGET_WORDS * 64; |
| 34 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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> { |
| 39 | public: |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 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); |
| 53 | if (LHS != RHS) |
| 54 | return LHS < RHS; |
| 55 | } |
| 56 | return false; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 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; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 66 | public: |
| 67 | constexpr FeatureBitArray(const std::array<uint64_t, MAX_SUBTARGET_WORDS> &B) |
| 68 | : Bits(B) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 70 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | } |
| 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. |
| 90 | class SubtargetFeatures { |
| 91 | std::vector<std::string> Features; ///< Subtarget features as a vector |
| 92 | |
| 93 | public: |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | /// 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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 113 | |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // end namespace llvm |
| 142 | |
| 143 | #endif // LLVM_MC_SUBTARGETFEATURE_H |