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