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