fd09638268619bee9e356df9bd21722b45efd0ea
[oota-llvm.git] / include / llvm / MC / SubtargetFeature.h
1 //===-- llvm/MC/SubtargetFeature.h - CPU characteristics --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This 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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_MC_SUBTARGETFEATURE_H
19 #define LLVM_MC_SUBTARGETFEATURE_H
20
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Support/DataTypes.h"
24 #include <bitset>
25
26 namespace llvm {
27   class raw_ostream;
28   class StringRef;
29
30 // A container class for subtarget features.
31 // This is convenient because std::bitset does not have a constructor
32 // with an initializer list of set bits.
33 const unsigned MAX_SUBTARGET_FEATURES = 64;
34 class FeatureBitset : public std::bitset<MAX_SUBTARGET_FEATURES> {
35 public:
36   // Cannot inherit constructors because it's not supported by VC++..
37   FeatureBitset() : bitset() {}
38
39   FeatureBitset(const bitset<MAX_SUBTARGET_FEATURES>& B) : bitset(B) {}
40
41   FeatureBitset(std::initializer_list<unsigned> Init) : bitset() {
42     for (auto I = Init.begin() , E = Init.end(); I != E; ++I)
43       set(*I);
44   }
45 };
46
47 //===----------------------------------------------------------------------===//
48 ///
49 /// SubtargetFeatureKV - Used to provide key value pairs for feature and
50 /// CPU bit flags.
51 //
52 struct 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
64 //===----------------------------------------------------------------------===//
65 ///
66 /// SubtargetInfoKV - Used to provide key value pairs for CPU and arbitrary
67 /// pointers.
68 //
69 struct SubtargetInfoKV {
70   const char *Key;                      // K-V key string
71   const void *Value;                    // K-V pointer value
72
73   // Compare routine for std::lower_bound
74   bool operator<(StringRef S) const {
75     return StringRef(Key) < S;
76   }
77 };
78
79 //===----------------------------------------------------------------------===//
80 ///
81 /// SubtargetFeatures - Manages the enabling and disabling of subtarget
82 /// specific features.  Features are encoded as a string of the form
83 ///   "+attr1,+attr2,-attr3,...,+attrN"
84 /// A comma separates each feature from the next (all lowercase.)
85 /// Each of the remaining features is prefixed with + or - indicating whether
86 /// that feature should be enabled or disabled contrary to the cpu
87 /// specification.
88 ///
89
90 class SubtargetFeatures {
91   std::vector<std::string> Features;    // Subtarget features as a vector
92 public:
93   explicit SubtargetFeatures(StringRef Initial = "");
94
95   /// Features string accessors.
96   std::string getString() const;
97
98   /// Adding Features.
99   void AddFeature(StringRef String);
100
101   /// ToggleFeature - Toggle a feature and returns the newly updated feature
102   /// bits.
103   FeatureBitset ToggleFeature(FeatureBitset Bits, StringRef String,
104                          ArrayRef<SubtargetFeatureKV> FeatureTable);
105
106   /// Get feature bits of a CPU.
107   FeatureBitset getFeatureBits(StringRef CPU,
108                           ArrayRef<SubtargetFeatureKV> CPUTable,
109                           ArrayRef<SubtargetFeatureKV> FeatureTable);
110
111   /// Print feature string.
112   void print(raw_ostream &OS) const;
113
114   // Dump feature info.
115   void dump() const;
116
117   /// Adds the default features for the specified target triple.
118   void getDefaultSubtargetFeatures(const Triple& Triple);
119 };
120
121 } // End namespace llvm
122
123 #endif