1//===--- InfoByHwMode.cpp -------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8// Classes that implement data parameterized by HW modes for instruction
9// selection. Currently it is ValueTypeByHwMode (parameterized ValueType),
10// and RegSizeInfoByHwMode (parameterized register/spill size and alignment
11// data).
12//===----------------------------------------------------------------------===//
13
14#include "InfoByHwMode.h"
15#include "CodeGenRegisters.h"
16#include "CodeGenTarget.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
20#include "llvm/TableGen/Error.h"
21#include "llvm/TableGen/Record.h"
22#include <string>
23
24using namespace llvm;
25
26std::string llvm::getModeName(unsigned Mode) {
27 if (Mode == DefaultMode)
28 return "*";
29 return (Twine('m') + Twine(Mode)).str();
30}
31
32ValueTypeByHwMode::ValueTypeByHwMode(const Record *R, const CodeGenHwModes &CGH)
33 : InfoByHwMode<llvm::MVT>(R) {
34 const HwModeSelect &MS = CGH.getHwModeSelect(R);
35 for (auto [ModeID, VT] : MS.Items) {
36 assert(VT && VT->isSubClassOf("ValueType"));
37 if (!Map.try_emplace(k: ModeID, args: MVT(llvm::getValueType(Rec: VT))).second)
38 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "duplicate ValueType entry for HwMode " +
39 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
40 VT->getName());
41 }
42 if (R->isSubClassOf(Name: "PtrValueType"))
43 PtrAddrSpace = R->getValueAsInt(FieldName: "AddrSpace");
44}
45
46ValueTypeByHwMode::ValueTypeByHwMode(const Record *R, MVT T)
47 : ValueTypeByHwMode(T) {
48 if (R->isSubClassOf(Name: "PtrValueType"))
49 PtrAddrSpace = R->getValueAsInt(FieldName: "AddrSpace");
50}
51
52bool ValueTypeByHwMode::operator==(const ValueTypeByHwMode &T) const {
53 assert(isValid() && T.isValid() && "Invalid type in assignment");
54 bool Simple = isSimple();
55 if (Simple != T.isSimple())
56 return false;
57 if (Simple)
58 return getSimple() == T.getSimple();
59
60 return Map == T.Map;
61}
62
63bool ValueTypeByHwMode::operator<(const ValueTypeByHwMode &T) const {
64 assert(isValid() && T.isValid() && "Invalid type in comparison");
65 // Default order for maps.
66 return Map < T.Map;
67}
68
69StringRef ValueTypeByHwMode::getMVTName(MVT T) {
70 StringRef N = llvm::getEnumName(T: T.SimpleTy);
71 N.consume_front(Prefix: "MVT::");
72 return N;
73}
74
75void ValueTypeByHwMode::writeToStream(raw_ostream &OS) const {
76 if (isSimple()) {
77 OS << getMVTName(T: getSimple());
78 return;
79 }
80
81 OS << '{';
82 ListSeparator LS(",");
83 for (const auto &[Mode, VT] : Map)
84 OS << LS << '(' << getModeName(Mode) << ':' << getMVTName(T: VT) << ')';
85 OS << '}';
86}
87
88LLVM_DUMP_METHOD
89void ValueTypeByHwMode::dump() const { dbgs() << *this << '\n'; }
90
91ValueTypeByHwMode llvm::getValueTypeByHwMode(const Record *Rec,
92 const CodeGenHwModes &CGH) {
93#ifndef NDEBUG
94 if (!Rec->isSubClassOf("ValueType"))
95 Rec->dump();
96#endif
97 assert(Rec->isSubClassOf("ValueType") &&
98 "Record must be derived from ValueType");
99 if (Rec->isSubClassOf(Name: "HwModeSelect"))
100 return ValueTypeByHwMode(Rec, CGH);
101 return ValueTypeByHwMode(Rec, llvm::getValueType(Rec));
102}
103
104RegSizeInfo::RegSizeInfo(const Record *R) {
105 RegSize = R->getValueAsInt(FieldName: "RegSize");
106 SpillSize = R->getValueAsInt(FieldName: "SpillSize");
107 SpillAlignment = R->getValueAsInt(FieldName: "SpillAlignment");
108}
109
110bool RegSizeInfo::operator<(const RegSizeInfo &I) const {
111 return std::tie(args: RegSize, args: SpillSize, args: SpillAlignment) <
112 std::tie(args: I.RegSize, args: I.SpillSize, args: I.SpillAlignment);
113}
114
115bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const {
116 return RegSize <= I.RegSize && SpillAlignment &&
117 I.SpillAlignment % SpillAlignment == 0 && SpillSize <= I.SpillSize;
118}
119
120void RegSizeInfo::writeToStream(raw_ostream &OS) const {
121 OS << "[R=" << RegSize << ",S=" << SpillSize << ",A=" << SpillAlignment
122 << ']';
123}
124
125RegSizeInfoByHwMode::RegSizeInfoByHwMode(const Record *R,
126 const CodeGenHwModes &CGH)
127 : InfoByHwMode<llvm::RegSizeInfo>(R) {
128 const HwModeSelect &MS = CGH.getHwModeSelect(R);
129 for (auto [ModeID, RegInfo] : MS.Items) {
130 assert(RegInfo && RegInfo->isSubClassOf("RegInfo"));
131 if (!Map.try_emplace(k: ModeID, args: RegSizeInfo(RegInfo)).second)
132 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "duplicate RegInfo entry for HwMode " +
133 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
134 RegInfo->getName());
135 }
136}
137
138bool RegSizeInfoByHwMode::operator<(const RegSizeInfoByHwMode &I) const {
139 unsigned M0 = Map.begin()->first;
140 return get(Mode: M0) < I.get(Mode: M0);
141}
142
143bool RegSizeInfoByHwMode::operator==(const RegSizeInfoByHwMode &I) const {
144 unsigned M0 = Map.begin()->first;
145 return get(Mode: M0) == I.get(Mode: M0);
146}
147
148bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const {
149 unsigned M0 = Map.begin()->first;
150 return get(Mode: M0).isSubClassOf(I: I.get(Mode: M0));
151}
152
153bool RegSizeInfoByHwMode::hasStricterSpillThan(
154 const RegSizeInfoByHwMode &I) const {
155 unsigned M0 = Map.begin()->first;
156 const RegSizeInfo &A0 = get(Mode: M0);
157 const RegSizeInfo &B0 = I.get(Mode: M0);
158 return std::tie(args: A0.SpillSize, args: A0.SpillAlignment) >
159 std::tie(args: B0.SpillSize, args: B0.SpillAlignment);
160}
161
162void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
163 OS << '{';
164 ListSeparator LS(",");
165 for (const auto &[Mode, Info] : Map)
166 OS << LS << '(' << getModeName(Mode) << ':' << Info << ')';
167 OS << '}';
168}
169
170RegClassByHwMode::RegClassByHwMode(const Record *R,
171 const CodeGenRegBank &RegBank)
172 : InfoByHwMode<const llvm::CodeGenRegisterClass *>(R) {
173 const CodeGenHwModes &CGH = RegBank.getHwModes();
174 const HwModeSelect &MS = CGH.getHwModeSelect(R);
175
176 for (auto [ModeID, RegClassRec] : MS.Items) {
177 assert(RegClassRec && RegClassRec->isSubClassOf("RegisterClass") &&
178 "Register class must subclass RegisterClass");
179 const CodeGenRegisterClass *RegClass = RegBank.getRegClass(RegClassRec);
180 if (!Map.try_emplace(k: ModeID, args&: RegClass).second)
181 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "duplicate RegisterClass entry for HwMode " +
182 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
183 RegClass->getName());
184 }
185}
186
187SubRegRange::SubRegRange(const Record *R) {
188 Size = R->getValueAsInt(FieldName: "Size");
189 Offset = R->getValueAsInt(FieldName: "Offset");
190}
191
192SubRegRangeByHwMode::SubRegRangeByHwMode(const Record *R,
193 const CodeGenHwModes &CGH)
194 : InfoByHwMode<llvm::SubRegRange>(R) {
195 const HwModeSelect &MS = CGH.getHwModeSelect(R);
196 for (auto [ModeID, Range] : MS.Items) {
197 assert(Range && Range->isSubClassOf("SubRegRange"));
198 if (!Map.try_emplace(k: ModeID, args: SubRegRange(Range)).second)
199 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "duplicate SubRegRange entry for HwMode " +
200 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
201 Range->getName());
202 }
203}
204
205EncodingInfoByHwMode::EncodingInfoByHwMode(const Record *R,
206 const CodeGenHwModes &CGH)
207 : InfoByHwMode<const llvm::Record *>(R) {
208 const HwModeSelect &MS = CGH.getHwModeSelect(R);
209 for (auto [ModeID, Encoding] : MS.Items) {
210 assert(Encoding && Encoding->isSubClassOf("InstructionEncoding") &&
211 "Encoding must subclass InstructionEncoding");
212 if (!Map.try_emplace(k: ModeID, args&: Encoding).second)
213 PrintFatalError(ErrorLoc: R->getLoc(),
214 Msg: "duplicate InstructionEncoding entry for HwMode " +
215 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
216 Encoding->getName());
217 }
218}
219
220RegisterByHwMode::RegisterByHwMode(const Record *R, CodeGenRegBank &RegBank)
221 : InfoByHwMode<const llvm::CodeGenRegister *>(R) {
222 const CodeGenHwModes &CGH = RegBank.getHwModes();
223 const HwModeSelect &MS = CGH.getHwModeSelect(R);
224 const Record *RCDef = R->getValueAsDef(FieldName: "RegClass");
225 Namespace = RegBank.getRegClasses().front().Namespace;
226 std::optional<RegClassByHwMode> RegClassByMode;
227 if (RCDef->isSubClassOf(Name: "RegClassByHwMode"))
228 RegClassByMode = RegClassByHwMode(RCDef, RegBank);
229 for (auto [ModeID, RegRecord] : MS.Items) {
230 assert(RegRecord && RegRecord->isSubClassOf("Register") &&
231 "Register value must subclass Register");
232 CodeGenRegister *Reg = RegBank.getReg(RegRecord);
233 const CodeGenRegisterClass *RC =
234 RegClassByMode ? RegClassByMode->get(Mode: ModeID)
235 : RegBank.getRegClass(RCDef, Loc: R->getLoc());
236 if (!RC->contains(Reg))
237 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "Register " + Reg->getName() +
238 " for HwMode " +
239 CGH.getModeName(Id: ModeID, IncludeDefault: true) +
240 " is not a member of register class " +
241 RC->getName());
242 if (!Map.try_emplace(k: ModeID, args&: Reg).second)
243 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "duplicate Register for HwMode " +
244 CGH.getModeName(Id: ModeID, IncludeDefault: true) + ": " +
245 Reg->getName());
246 }
247}
248
249void RegisterByHwMode::emitResolverCall(raw_ostream &OS,
250 const Twine &HwMode) const {
251 OS << Namespace << "::RegisterByHwMode::get" << Def->getName() << "("
252 << HwMode << ")";
253}
254
255raw_ostream &llvm::operator<<(raw_ostream &OS, const ValueTypeByHwMode &T) {
256 T.writeToStream(OS);
257 return OS;
258}
259
260raw_ostream &llvm::operator<<(raw_ostream &OS, const RegSizeInfo &T) {
261 T.writeToStream(OS);
262 return OS;
263}
264
265raw_ostream &llvm::operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T) {
266 T.writeToStream(OS);
267 return OS;
268}
269