1//===- RegisterBankEmitter.cpp - Generate a Register Bank Desc. -*- C++ -*-===//
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//
9// This tablegen backend is responsible for emitting a description of a target
10// register bank for a code generator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Common/CodeGenRegisters.h"
15#include "Common/CodeGenTarget.h"
16#include "Common/InfoByHwMode.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/MathExtras.h"
19#include "llvm/TableGen/CodeGenHelpers.h"
20#include "llvm/TableGen/Error.h"
21#include "llvm/TableGen/Record.h"
22#include "llvm/TableGen/TGTimer.h"
23#include "llvm/TableGen/TableGenBackend.h"
24
25#define DEBUG_TYPE "register-bank-emitter"
26
27using namespace llvm;
28
29namespace {
30class RegisterBank {
31
32 /// A vector of register classes that are included in the register bank.
33 using RegisterClassesTy = std::vector<const CodeGenRegisterClass *>;
34
35private:
36 const Record &TheDef;
37
38 /// The register classes that are covered by the register bank.
39 RegisterClassesTy RCs;
40
41 /// The register class with the largest register size.
42 std::vector<const CodeGenRegisterClass *> RCsWithLargestRegSize;
43
44public:
45 RegisterBank(const Record &TheDef, unsigned NumModeIds)
46 : TheDef(TheDef), RCsWithLargestRegSize(NumModeIds) {}
47
48 /// Get the human-readable name for the bank.
49 StringRef getName() const { return TheDef.getValueAsString(FieldName: "Name"); }
50
51 /// Get the name of the enumerator in the ID enumeration.
52 std::string getEnumeratorName() const {
53 return (TheDef.getName() + "ID").str();
54 }
55
56 /// Get the name of the array holding the register class coverage data;
57 std::string getCoverageArrayName() const {
58 return (TheDef.getName() + "CoverageData").str();
59 }
60
61 /// Get the name of the global instance variable.
62 StringRef getInstanceVarName() const { return TheDef.getName(); }
63
64 const Record &getDef() const { return TheDef; }
65
66 /// Get the register classes listed in the RegisterBank.RegisterClasses field.
67 std::vector<const CodeGenRegisterClass *>
68 getExplicitlySpecifiedRegisterClasses(
69 const CodeGenRegBank &RegisterClassHierarchy) const {
70 std::vector<const CodeGenRegisterClass *> RCs;
71 for (const auto *RCDef : getDef().getValueAsListOfDefs(FieldName: "RegisterClasses"))
72 RCs.push_back(x: RegisterClassHierarchy.getRegClass(RCDef));
73 return RCs;
74 }
75
76 /// Add a register class to the bank without duplicates.
77 void addRegisterClass(const CodeGenRegisterClass *RC) {
78 if (llvm::is_contained(Range&: RCs, Element: RC))
79 return;
80
81 // FIXME? We really want the register size rather than the spill size
82 // since the spill size may be bigger on some targets with
83 // limited load/store instructions. However, we don't store the
84 // register size anywhere (we could sum the sizes of the subregisters
85 // but there may be additional bits too) and we can't derive it from
86 // the VT's reliably due to Untyped.
87 unsigned NumModeIds = RCsWithLargestRegSize.size();
88 for (unsigned M = 0; M < NumModeIds; ++M) {
89 if (RCsWithLargestRegSize[M] == nullptr)
90 RCsWithLargestRegSize[M] = RC;
91 else if (RCsWithLargestRegSize[M]->RSI.get(Mode: M).SpillSize <
92 RC->RSI.get(Mode: M).SpillSize)
93 RCsWithLargestRegSize[M] = RC;
94 assert(RCsWithLargestRegSize[M] && "RC was nullptr?");
95 }
96
97 RCs.emplace_back(args&: RC);
98 }
99
100 const CodeGenRegisterClass *getRCWithLargestRegSize(unsigned HwMode) const {
101 return RCsWithLargestRegSize[HwMode];
102 }
103
104 iterator_range<RegisterClassesTy::const_iterator> register_classes() const {
105 return RCs;
106 }
107};
108
109class RegisterBankEmitter {
110private:
111 const CodeGenTarget Target;
112 const RecordKeeper &Records;
113
114 void emitHeader(raw_ostream &OS, StringRef TargetName,
115 ArrayRef<RegisterBank> Banks);
116 void emitBaseClassDefinition(raw_ostream &OS, StringRef TargetName,
117 ArrayRef<RegisterBank> Banks);
118 void emitBaseClassImplementation(raw_ostream &OS, StringRef TargetName,
119 ArrayRef<RegisterBank> Banks);
120
121public:
122 RegisterBankEmitter(const RecordKeeper &R) : Target(R), Records(R) {}
123
124 void run(raw_ostream &OS);
125};
126
127} // end anonymous namespace
128
129/// Emit code to declare the ID enumeration and external global instance
130/// variables.
131void RegisterBankEmitter::emitHeader(raw_ostream &OS, StringRef TargetName,
132 ArrayRef<RegisterBank> Banks) {
133 IfDefEmitter IfDef(OS, "GET_REGBANK_DECLARATIONS");
134 NamespaceEmitter NS(OS, ("llvm::" + TargetName).str());
135
136 // <Target>RegisterBankInfo.h
137 OS << "enum : unsigned {\n";
138
139 OS << " InvalidRegBankID = ~0u,\n";
140 unsigned ID = 0;
141 for (const auto &Bank : Banks)
142 OS << " " << Bank.getEnumeratorName() << " = " << ID++ << ",\n";
143 OS << " NumRegisterBanks,\n"
144 << "};\n";
145}
146
147/// Emit declarations of the <Target>GenRegisterBankInfo class.
148void RegisterBankEmitter::emitBaseClassDefinition(
149 raw_ostream &OS, StringRef TargetName, ArrayRef<RegisterBank> Banks) {
150 IfDefEmitter IfDef(OS, "GET_TARGET_REGBANK_CLASS");
151
152 OS << "private:\n"
153 << " static const RegisterBank *RegBanks[];\n"
154 << " static const unsigned Sizes[];\n\n"
155 << "public:\n"
156 << " const RegisterBank &getRegBankFromRegClass(const "
157 "TargetRegisterClass &RC, LLT Ty) const override;\n"
158 << "protected:\n"
159 << " " << TargetName << "GenRegisterBankInfo(unsigned HwMode = 0);\n"
160 << "\n";
161}
162
163/// Visit each register class belonging to the given register bank.
164///
165/// A class belongs to the bank iff any of these apply:
166/// * It is explicitly specified
167/// * It is a subclass of a class that is a member.
168/// * It is a class containing subregisters of the registers of a class that
169/// is a member. This is known as a subreg-class.
170///
171/// This function must be called for each explicitly specified register class.
172///
173/// \param RC The register class to search.
174/// \param Kind A debug string containing the path the visitor took to reach RC.
175/// \param VisitFn The action to take for each class visited. It may be called
176/// multiple times for a given class if there are multiple paths
177/// to the class.
178static void visitRegisterBankClasses(
179 const CodeGenRegBank &RegisterClassHierarchy,
180 const CodeGenRegisterClass *RC, const Twine &Kind,
181 std::function<void(const CodeGenRegisterClass *, StringRef)> VisitFn,
182 DenseSet<const CodeGenRegisterClass *> &VisitedRCs) {
183
184 // Make sure we only visit each class once to avoid infinite loops.
185 if (!VisitedRCs.insert(V: RC).second)
186 return;
187
188 // Visit each explicitly named class.
189 VisitFn(RC, Kind.str());
190
191 for (const auto &PossibleSubclass : RegisterClassHierarchy.getRegClasses()) {
192 std::string TmpKind =
193 (Kind + " (" + PossibleSubclass.getName() + ")").str();
194
195 // Visit each subclass of an explicitly named class.
196 if (RC != &PossibleSubclass && RC->hasSubClass(RC: &PossibleSubclass))
197 visitRegisterBankClasses(RegisterClassHierarchy, RC: &PossibleSubclass,
198 Kind: TmpKind + " " + RC->getName() + " subclass",
199 VisitFn, VisitedRCs);
200
201 // Visit each class that contains only subregisters of RC with a common
202 // subregister-index.
203 //
204 // More precisely, PossibleSubclass is a subreg-class iff Reg:SubIdx is in
205 // PossibleSubclass for all registers Reg from RC using any
206 // subregister-index SubReg
207 if (!PossibleSubclass.hasSuperRegClass(RC))
208 continue;
209
210 for (const auto &SubIdx : RegisterClassHierarchy.getSubRegIndices()) {
211 if (PossibleSubclass.hasSuperRegClass(SubIdx: &SubIdx, RC)) {
212 std::string TmpKind2 = (Twine(TmpKind) + " " + RC->getName() +
213 " class-with-subregs: " + RC->getName())
214 .str();
215 VisitFn(&PossibleSubclass, TmpKind2);
216 }
217 }
218 }
219}
220
221void RegisterBankEmitter::emitBaseClassImplementation(
222 raw_ostream &OS, StringRef TargetName, ArrayRef<RegisterBank> Banks) {
223 const CodeGenRegBank &RegisterClassHierarchy = Target.getRegBank();
224 const CodeGenHwModes &CGH = Target.getHwModes();
225
226 IfDefEmitter IfDef(OS, "GET_TARGET_REGBANK_IMPL");
227 NamespaceEmitter LlvmNS(OS, "llvm");
228
229 {
230 NamespaceEmitter TargetNS(OS, TargetName);
231 for (const auto &Bank : Banks) {
232 std::vector<std::vector<const CodeGenRegisterClass *>> RCsGroupedByWord(
233 (RegisterClassHierarchy.getRegClasses().size() + 31) / 32);
234
235 for (const auto &RC : Bank.register_classes())
236 RCsGroupedByWord[RC->EnumValue / 32].push_back(x: RC);
237
238 OS << "const uint32_t " << Bank.getCoverageArrayName() << "[] = {\n";
239 unsigned LowestIdxInWord = 0;
240 for (const auto &RCs : RCsGroupedByWord) {
241 OS << " // " << LowestIdxInWord << "-" << (LowestIdxInWord + 31)
242 << "\n";
243 for (const auto &RC : RCs) {
244 OS << " (1u << (" << RC->getQualifiedIdName() << " - "
245 << LowestIdxInWord << ")) |\n";
246 }
247 OS << " 0,\n";
248 LowestIdxInWord += 32;
249 }
250 OS << "};\n";
251 }
252 OS << "\n";
253
254 for (const auto &Bank : Banks) {
255 std::string QualifiedBankID =
256 (TargetName + "::" + Bank.getEnumeratorName()).str();
257 OS << "constexpr RegisterBank " << Bank.getInstanceVarName()
258 << "(/* ID */ " << QualifiedBankID << ", /* Name */ \""
259 << Bank.getName() << "\", " << "/* CoveredRegClasses */ "
260 << Bank.getCoverageArrayName() << ", /* NumRegClasses */ "
261 << RegisterClassHierarchy.getRegClasses().size() << ");\n";
262 }
263 } // End target namespace.
264
265 OS << "\nconst RegisterBank *" << TargetName
266 << "GenRegisterBankInfo::RegBanks[] = {\n";
267 for (const auto &Bank : Banks)
268 OS << " &" << TargetName << "::" << Bank.getInstanceVarName() << ",\n";
269 OS << "};\n\n";
270
271 unsigned NumModeIds = CGH.getNumModeIds();
272 OS << "const unsigned " << TargetName << "GenRegisterBankInfo::Sizes[] = {\n";
273 for (unsigned M = 0; M < NumModeIds; ++M) {
274 OS << " // Mode = " << M << " ("
275 << CGH.getModeName(Id: M, /*IncludeDefault=*/true) << ")\n";
276 for (const auto &Bank : Banks) {
277 const CodeGenRegisterClass &RC = *Bank.getRCWithLargestRegSize(HwMode: M);
278 unsigned Size = RC.RSI.get(Mode: M).SpillSize;
279 OS << " " << Size << ",\n";
280 }
281 }
282 OS << "};\n\n";
283
284 OS << TargetName << "GenRegisterBankInfo::" << TargetName
285 << "GenRegisterBankInfo(unsigned HwMode)\n"
286 << " : RegisterBankInfo(RegBanks, " << TargetName
287 << "::NumRegisterBanks, Sizes, HwMode) {\n"
288 << " // Assert that RegBank indices match their ID's\n"
289 << "#ifndef NDEBUG\n"
290 << " for (auto RB : enumerate(RegBanks))\n"
291 << " assert(RB.index() == RB.value()->getID() && \"Index != ID\");\n"
292 << "#endif // NDEBUG\n"
293 << "}\n";
294
295 uint32_t NumRegBanks = Banks.size();
296 uint32_t BitSize = NextPowerOf2(A: Log2_32(Value: NumRegBanks));
297 uint32_t ElemsPerWord = 32 / BitSize;
298 uint32_t BitMask = (1 << BitSize) - 1;
299 bool HasAmbigousOrMissingEntry = false;
300 struct Entry {
301 std::string RCIdName;
302 std::string RBIdName;
303 };
304 SmallVector<Entry, 0> Entries;
305 for (const auto &Bank : Banks) {
306 for (const auto *RC : Bank.register_classes()) {
307 if (RC->EnumValue >= Entries.size())
308 Entries.resize(N: RC->EnumValue + 1);
309 Entry &E = Entries[RC->EnumValue];
310 E.RCIdName = RC->getIdName();
311 if (!E.RBIdName.empty()) {
312 HasAmbigousOrMissingEntry = true;
313 E.RBIdName = "InvalidRegBankID";
314 } else {
315 E.RBIdName = (TargetName + "::" + Bank.getEnumeratorName()).str();
316 }
317 }
318 }
319 for (auto &E : Entries) {
320 if (E.RBIdName.empty()) {
321 HasAmbigousOrMissingEntry = true;
322 E.RBIdName = "InvalidRegBankID";
323 }
324 }
325 OS << "\nconst RegisterBank &\n"
326 << TargetName
327 << "GenRegisterBankInfo::getRegBankFromRegClass"
328 "(const TargetRegisterClass &RC, LLT) const {\n";
329 if (HasAmbigousOrMissingEntry) {
330 OS << " constexpr uint32_t InvalidRegBankID = uint32_t("
331 << TargetName + "::InvalidRegBankID) & " << BitMask << ";\n";
332 }
333 unsigned TableSize =
334 Entries.size() / ElemsPerWord + ((Entries.size() % ElemsPerWord) > 0);
335 OS << " static const uint32_t RegClass2RegBank[" << TableSize << "] = {\n";
336 uint32_t Shift = 32 - BitSize;
337 bool First = true;
338 std::string TrailingComment;
339 for (auto &E : Entries) {
340 Shift += BitSize;
341 if (Shift == 32) {
342 Shift = 0;
343 if (First)
344 First = false;
345 else
346 OS << ',' << TrailingComment << '\n';
347 } else {
348 OS << " |" << TrailingComment << '\n';
349 }
350 OS << " ("
351 << (E.RBIdName.empty()
352 ? "InvalidRegBankID"
353 : Twine("uint32_t(").concat(Suffix: E.RBIdName).concat(Suffix: ")").str())
354 << " << " << Shift << ')';
355 if (!E.RCIdName.empty())
356 TrailingComment = " // " + E.RCIdName;
357 else
358 TrailingComment = "";
359 }
360 OS << TrailingComment
361 << "\n };\n"
362 " const unsigned RegClassID = RC.getID();\n"
363 " if (LLVM_LIKELY(RegClassID < "
364 << Entries.size()
365 << ")) {\n"
366 " unsigned RegBankID = (RegClass2RegBank[RegClassID / "
367 << ElemsPerWord << "] >> ((RegClassID % " << ElemsPerWord << ") * "
368 << BitSize << ")) & " << BitMask << ";\n";
369 if (HasAmbigousOrMissingEntry) {
370 OS << " if (RegBankID != InvalidRegBankID)\n"
371 " return getRegBank(RegBankID);\n";
372 } else {
373 OS << " return getRegBank(RegBankID);\n";
374 }
375 OS << " }\n"
376 " llvm_unreachable(llvm::Twine(\"Target needs to handle register "
377 "class ID "
378 "0x\").concat(llvm::Twine::utohexstr(RegClassID)).str().c_str());\n"
379 "}\n";
380}
381
382void RegisterBankEmitter::run(raw_ostream &OS) {
383 StringRef TargetName = Target.getName();
384 const CodeGenRegBank &RegisterClassHierarchy = Target.getRegBank();
385 const CodeGenHwModes &CGH = Target.getHwModes();
386
387 TGTimer &Timer = Records.getTimer();
388 Timer.startTimer(Name: "Analyze records");
389 std::vector<RegisterBank> Banks;
390 for (const auto &V : Records.getAllDerivedDefinitions(ClassName: "RegisterBank")) {
391 DenseSet<const CodeGenRegisterClass *> VisitedRCs;
392 RegisterBank Bank(*V, CGH.getNumModeIds());
393
394 for (const CodeGenRegisterClass *RC :
395 Bank.getExplicitlySpecifiedRegisterClasses(RegisterClassHierarchy)) {
396 visitRegisterBankClasses(
397 RegisterClassHierarchy, RC, Kind: "explicit",
398 VisitFn: [&Bank](const CodeGenRegisterClass *RC, StringRef Kind) {
399 LLVM_DEBUG(dbgs()
400 << "Added " << RC->getName() << "(" << Kind << ")\n");
401 Bank.addRegisterClass(RC);
402 },
403 VisitedRCs);
404 }
405
406 Banks.push_back(x: Bank);
407 }
408
409 if (Banks.empty())
410 PrintFatalError(Msg: "No register banks defined");
411
412 // Warn about ambiguous MIR caused by register bank/class name clashes.
413 Timer.startTimer(Name: "Warn ambiguous");
414 for (const auto &Class : RegisterClassHierarchy.getRegClasses()) {
415 for (const auto &Bank : Banks) {
416 if (Bank.getName().lower() == StringRef(Class.getName()).lower()) {
417 PrintWarning(WarningLoc: Bank.getDef().getLoc(), Msg: "Register bank names should be "
418 "distinct from register classes "
419 "to avoid ambiguous MIR");
420 PrintNote(NoteLoc: Bank.getDef().getLoc(), Msg: "RegisterBank was declared here");
421 PrintNote(NoteLoc: Class.getDef()->getLoc(), Msg: "RegisterClass was declared here");
422 }
423 }
424 }
425
426 Timer.startTimer(Name: "Emit output");
427 emitSourceFileHeader(Desc: "Register Bank Source Fragments", OS);
428 emitHeader(OS, TargetName, Banks);
429 emitBaseClassDefinition(OS, TargetName, Banks);
430 emitBaseClassImplementation(OS, TargetName, Banks);
431}
432
433static TableGen::Emitter::OptClass<RegisterBankEmitter>
434 X("gen-register-bank", "Generate registers bank descriptions");
435