Re-commit r247683: Replace Triple with a new TargetTuple in MCTargetDesc/* and relate...
[oota-llvm.git] / include / llvm / MC / MCSubtargetInfo.h
1 //==-- llvm/MC/MCSubtargetInfo.h - Subtarget Information ---------*- 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 describes the subtarget options of a Target machine.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MC_MCSUBTARGETINFO_H
15 #define LLVM_MC_MCSUBTARGETINFO_H
16
17 #include "llvm/ADT/TargetTuple.h"
18 #include "llvm/MC/MCInstrItineraries.h"
19 #include "llvm/MC/SubtargetFeature.h"
20 #include <string>
21
22 namespace llvm {
23
24 class StringRef;
25
26 //===----------------------------------------------------------------------===//
27 ///
28 /// MCSubtargetInfo - Generic base class for all target subtargets.
29 ///
30 class MCSubtargetInfo {
31   TargetTuple TheTargetTuple;                 // Target triple
32   std::string CPU;                            // CPU being targeted.
33   ArrayRef<SubtargetFeatureKV> ProcFeatures;  // Processor feature list
34   ArrayRef<SubtargetFeatureKV> ProcDesc;      // Processor descriptions
35
36   // Scheduler machine model
37   const SubtargetInfoKV *ProcSchedModels;
38   const MCWriteProcResEntry *WriteProcResTable;
39   const MCWriteLatencyEntry *WriteLatencyTable;
40   const MCReadAdvanceEntry *ReadAdvanceTable;
41   const MCSchedModel *CPUSchedModel;
42
43   const InstrStage *Stages;            // Instruction itinerary stages
44   const unsigned *OperandCycles;       // Itinerary operand cycles
45   const unsigned *ForwardingPaths;     // Forwarding paths
46   FeatureBitset FeatureBits;           // Feature bits for current CPU + FS
47
48   MCSubtargetInfo() = delete;
49   MCSubtargetInfo &operator=(MCSubtargetInfo &&) = delete;
50   MCSubtargetInfo &operator=(const MCSubtargetInfo &) = delete;
51
52 public:
53   MCSubtargetInfo(const MCSubtargetInfo &) = default;
54   MCSubtargetInfo(const TargetTuple &TT, StringRef CPU, StringRef FS,
55                   ArrayRef<SubtargetFeatureKV> PF,
56                   ArrayRef<SubtargetFeatureKV> PD,
57                   const SubtargetInfoKV *ProcSched,
58                   const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
59                   const MCReadAdvanceEntry *RA, const InstrStage *IS,
60                   const unsigned *OC, const unsigned *FP);
61
62   /// getTargetTuple - Return the target triple string.
63   const TargetTuple &getTargetTuple() const { return TheTargetTuple; }
64
65   /// getCPU - Return the CPU string.
66   StringRef getCPU() const {
67     return CPU;
68   }
69
70   /// getFeatureBits - Return the feature bits.
71   ///
72   const FeatureBitset& getFeatureBits() const {
73     return FeatureBits;
74   }
75
76   /// setFeatureBits - Set the feature bits.
77   ///
78   void setFeatureBits(const FeatureBitset &FeatureBits_) {
79     FeatureBits = FeatureBits_;
80   }
81
82 protected:
83   /// Initialize the scheduling model and feature bits.
84   ///
85   /// FIXME: Find a way to stick this in the constructor, since it should only
86   /// be called during initialization.
87   void InitMCProcessorInfo(StringRef CPU, StringRef FS);
88
89 public:
90   /// Set the features to the default for the given CPU.
91   void setDefaultFeatures(StringRef CPU);
92
93   /// ToggleFeature - Toggle a feature and returns the re-computed feature
94   /// bits. This version does not change the implied bits.
95   FeatureBitset ToggleFeature(uint64_t FB);
96
97   /// ToggleFeature - Toggle a feature and returns the re-computed feature
98   /// bits. This version does not change the implied bits.
99   FeatureBitset ToggleFeature(const FeatureBitset& FB);
100
101   /// ToggleFeature - Toggle a set of features and returns the re-computed
102   /// feature bits. This version will also change all implied bits.
103   FeatureBitset ToggleFeature(StringRef FS);
104
105   /// Apply a feature flag and return the re-computed feature bits, including
106   /// all feature bits implied by the flag.
107   FeatureBitset ApplyFeatureFlag(StringRef FS);
108
109   /// getSchedModelForCPU - Get the machine model of a CPU.
110   ///
111   const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
112
113   /// Get the machine model for this subtarget's CPU.
114   const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
115
116   /// Return an iterator at the first process resource consumed by the given
117   /// scheduling class.
118   const MCWriteProcResEntry *getWriteProcResBegin(
119     const MCSchedClassDesc *SC) const {
120     return &WriteProcResTable[SC->WriteProcResIdx];
121   }
122   const MCWriteProcResEntry *getWriteProcResEnd(
123     const MCSchedClassDesc *SC) const {
124     return getWriteProcResBegin(SC) + SC->NumWriteProcResEntries;
125   }
126
127   const MCWriteLatencyEntry *getWriteLatencyEntry(const MCSchedClassDesc *SC,
128                                                   unsigned DefIdx) const {
129     assert(DefIdx < SC->NumWriteLatencyEntries &&
130            "MachineModel does not specify a WriteResource for DefIdx");
131
132     return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
133   }
134
135   int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
136                            unsigned WriteResID) const {
137     // TODO: The number of read advance entries in a class can be significant
138     // (~50). Consider compressing the WriteID into a dense ID of those that are
139     // used by ReadAdvance and representing them as a bitset.
140     for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
141            *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
142       if (I->UseIdx < UseIdx)
143         continue;
144       if (I->UseIdx > UseIdx)
145         break;
146       // Find the first WriteResIdx match, which has the highest cycle count.
147       if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
148         return I->Cycles;
149       }
150     }
151     return 0;
152   }
153
154   /// getInstrItineraryForCPU - Get scheduling itinerary of a CPU.
155   ///
156   InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
157
158   /// Initialize an InstrItineraryData instance.
159   void initInstrItins(InstrItineraryData &InstrItins) const;
160
161   /// Check whether the CPU string is valid.
162   bool isCPUStringValid(StringRef CPU) const {
163     auto Found = std::find_if(ProcDesc.begin(), ProcDesc.end(),
164                               [=](const SubtargetFeatureKV &KV) {
165                                 return CPU == KV.Key; 
166                               });
167     return Found != ProcDesc.end();
168   }
169 };
170
171 } // End llvm namespace
172
173 #endif