1 //===- RegisterInfoEmitter.cpp - Generate a Register File Desc. -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting a description of a target
11 // register file for a code generator. It uses instances of the Register,
12 // RegisterAliases, and RegisterClass classes to gather this information.
14 //===----------------------------------------------------------------------===//
16 #include "RegisterInfoEmitter.h"
17 #include "CodeGenTarget.h"
18 #include "CodeGenRegisters.h"
19 #include "llvm/TableGen/Record.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/Support/Format.h"
28 // runEnums - Print out enum values for all of the registers.
30 RegisterInfoEmitter::runEnums(raw_ostream &OS,
31 CodeGenTarget &Target, CodeGenRegBank &Bank) {
32 const std::vector<CodeGenRegister*> &Registers = Bank.getRegisters();
34 std::string Namespace = Registers[0]->TheDef->getValueAsString("Namespace");
36 EmitSourceFileHeader("Target Register Enum Values", OS);
38 OS << "\n#ifdef GET_REGINFO_ENUM\n";
39 OS << "#undef GET_REGINFO_ENUM\n";
41 OS << "namespace llvm {\n\n";
43 OS << "class MCRegisterClass;\n"
44 << "extern const MCRegisterClass " << Namespace
45 << "MCRegisterClasses[];\n\n";
47 if (!Namespace.empty())
48 OS << "namespace " << Namespace << " {\n";
49 OS << "enum {\n NoRegister,\n";
51 for (unsigned i = 0, e = Registers.size(); i != e; ++i)
52 OS << " " << Registers[i]->getName() << " = " <<
53 Registers[i]->EnumValue << ",\n";
54 assert(Registers.size() == Registers[Registers.size()-1]->EnumValue &&
55 "Register enum value mismatch!");
56 OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
58 if (!Namespace.empty())
61 ArrayRef<CodeGenRegisterClass*> RegisterClasses = Bank.getRegClasses();
62 if (!RegisterClasses.empty()) {
63 OS << "\n// Register classes\n";
64 if (!Namespace.empty())
65 OS << "namespace " << Namespace << " {\n";
67 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
69 OS << " " << RegisterClasses[i]->getName() << "RegClassID";
73 if (!Namespace.empty())
77 const std::vector<Record*> RegAltNameIndices = Target.getRegAltNameIndices();
78 // If the only definition is the default NoRegAltName, we don't need to
80 if (RegAltNameIndices.size() > 1) {
81 OS << "\n// Register alternate name indices\n";
82 if (!Namespace.empty())
83 OS << "namespace " << Namespace << " {\n";
85 for (unsigned i = 0, e = RegAltNameIndices.size(); i != e; ++i)
86 OS << " " << RegAltNameIndices[i]->getName() << ",\t// " << i << "\n";
87 OS << " NUM_TARGET_REG_ALT_NAMES = " << RegAltNameIndices.size() << "\n";
89 if (!Namespace.empty())
94 OS << "} // End llvm namespace \n";
95 OS << "#endif // GET_REGINFO_ENUM\n\n";
99 RegisterInfoEmitter::EmitRegMapping(raw_ostream &OS,
100 const std::vector<CodeGenRegister*> &Regs,
103 // Collect all information about dwarf register numbers
104 typedef std::map<Record*, std::vector<int64_t>, LessRecord> DwarfRegNumsMapTy;
105 DwarfRegNumsMapTy DwarfRegNums;
107 // First, just pull all provided information to the map
108 unsigned maxLength = 0;
109 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
110 Record *Reg = Regs[i]->TheDef;
111 std::vector<int64_t> RegNums = Reg->getValueAsListOfInts("DwarfNumbers");
112 maxLength = std::max((size_t)maxLength, RegNums.size());
113 if (DwarfRegNums.count(Reg))
114 errs() << "Warning: DWARF numbers for register " << getQualifiedName(Reg)
115 << "specified multiple times\n";
116 DwarfRegNums[Reg] = RegNums;
122 // Now we know maximal length of number list. Append -1's, where needed
123 for (DwarfRegNumsMapTy::iterator
124 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I)
125 for (unsigned i = I->second.size(), e = maxLength; i != e; ++i)
126 I->second.push_back(-1);
128 // Emit reverse information about the dwarf register numbers.
129 for (unsigned j = 0; j < 2; ++j) {
132 OS << "DwarfFlavour";
137 << " llvm_unreachable(\"Unknown DWARF flavour\");\n";
139 for (unsigned i = 0, e = maxLength; i != e; ++i) {
140 OS << " case " << i << ":\n";
141 for (DwarfRegNumsMapTy::iterator
142 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
143 int DwarfRegNo = I->second[i];
149 OS << "mapDwarfRegToLLVMReg(" << DwarfRegNo << ", "
150 << getQualifiedName(I->first) << ", ";
162 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
163 Record *Reg = Regs[i]->TheDef;
164 const RecordVal *V = Reg->getValue("DwarfAlias");
165 if (!V || !V->getValue())
168 DefInit *DI = dynamic_cast<DefInit*>(V->getValue());
169 Record *Alias = DI->getDef();
170 DwarfRegNums[Reg] = DwarfRegNums[Alias];
173 // Emit information about the dwarf register numbers.
174 for (unsigned j = 0; j < 2; ++j) {
177 OS << "DwarfFlavour";
182 << " llvm_unreachable(\"Unknown DWARF flavour\");\n";
184 for (unsigned i = 0, e = maxLength; i != e; ++i) {
185 OS << " case " << i << ":\n";
186 // Sort by name to get a stable order.
187 for (DwarfRegNumsMapTy::iterator
188 I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
189 int RegNo = I->second[i];
190 if (RegNo == -1) // -1 is the default value, don't emit a mapping.
196 OS << "mapLLVMRegToDwarfReg(" << getQualifiedName(I->first) << ", "
210 // Print a BitVector as a sequence of hex numbers using a little-endian mapping.
211 // Width is the number of bits per hex number.
212 static void printBitVectorAsHex(raw_ostream &OS,
213 const BitVector &Bits,
215 assert(Width <= 32 && "Width too large");
216 unsigned Digits = (Width + 3) / 4;
217 for (unsigned i = 0, e = Bits.size(); i < e; i += Width) {
219 for (unsigned j = 0; j != Width && i + j != e; ++j)
220 Value |= Bits.test(i + j) << j;
221 OS << format("0x%0*x, ", Digits, Value);
225 // Helper to emit a set of bits into a constant byte array.
226 class BitVectorEmitter {
229 void add(unsigned v) {
230 if (v >= Values.size())
231 Values.resize(((v/8)+1)*8); // Round up to the next byte.
235 void print(raw_ostream &OS) {
236 printBitVectorAsHex(OS, Values, 8);
241 // runMCDesc - Print out MC register descriptions.
244 RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
245 CodeGenRegBank &RegBank) {
246 EmitSourceFileHeader("MC Register Information", OS);
248 OS << "\n#ifdef GET_REGINFO_MC_DESC\n";
249 OS << "#undef GET_REGINFO_MC_DESC\n";
251 std::map<const CodeGenRegister*, CodeGenRegister::Set> Overlaps;
252 RegBank.computeOverlaps(Overlaps);
254 OS << "namespace llvm {\n\n";
256 const std::string &TargetName = Target.getName();
258 const std::vector<CodeGenRegister*> &Regs = RegBank.getRegisters();
260 OS << "extern const unsigned " << TargetName << "RegOverlaps[] = {\n";
262 // Emit an overlap list for all registers.
263 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
264 const CodeGenRegister *Reg = Regs[i];
265 const CodeGenRegister::Set &O = Overlaps[Reg];
266 // Move Reg to the front so TRI::getAliasSet can share the list.
267 OS << " /* " << Reg->getName() << "_Overlaps */ "
268 << getQualifiedName(Reg->TheDef) << ", ";
269 for (CodeGenRegister::Set::const_iterator I = O.begin(), E = O.end();
272 OS << getQualifiedName((*I)->TheDef) << ", ";
277 OS << "extern const unsigned " << TargetName << "SubRegsSet[] = {\n";
278 // Emit the empty sub-registers list
279 OS << " /* Empty_SubRegsSet */ 0,\n";
280 // Loop over all of the registers which have sub-registers, emitting the
281 // sub-registers list to memory.
282 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
283 const CodeGenRegister &Reg = *Regs[i];
284 if (Reg.getSubRegs().empty())
286 // getSubRegs() orders by SubRegIndex. We want a topological order.
287 SetVector<CodeGenRegister*> SR;
288 Reg.addSubRegsPreOrder(SR, RegBank);
289 OS << " /* " << Reg.getName() << "_SubRegsSet */ ";
290 for (unsigned j = 0, je = SR.size(); j != je; ++j)
291 OS << getQualifiedName(SR[j]->TheDef) << ", ";
296 OS << "extern const unsigned " << TargetName << "SuperRegsSet[] = {\n";
297 // Emit the empty super-registers list
298 OS << " /* Empty_SuperRegsSet */ 0,\n";
299 // Loop over all of the registers which have super-registers, emitting the
300 // super-registers list to memory.
301 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
302 const CodeGenRegister &Reg = *Regs[i];
303 const CodeGenRegister::SuperRegList &SR = Reg.getSuperRegs();
306 OS << " /* " << Reg.getName() << "_SuperRegsSet */ ";
307 for (unsigned j = 0, je = SR.size(); j != je; ++j)
308 OS << getQualifiedName(SR[j]->TheDef) << ", ";
313 OS << "extern const MCRegisterDesc " << TargetName
314 << "RegDesc[] = { // Descriptors\n";
315 OS << " { \"NOREG\", -1, -1, -1 },\n";
317 // Now that register alias and sub-registers sets have been emitted, emit the
318 // register descriptors now.
319 unsigned OverlapsIndex = 0;
320 unsigned SubRegIndex = 1; // skip 1 for empty set
321 unsigned SuperRegIndex = 1; // skip 1 for empty set
322 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
323 const CodeGenRegister *Reg = Regs[i];
325 OS << Reg->getName() << "\", /* " << Reg->getName() << "_Overlaps */ "
326 << OverlapsIndex << ", ";
327 OverlapsIndex += Overlaps[Reg].size() + 1;
328 if (!Reg->getSubRegs().empty()) {
329 OS << "/* " << Reg->getName() << "_SubRegsSet */ " << SubRegIndex
331 // FIXME not very nice to recalculate this
332 SetVector<CodeGenRegister*> SR;
333 Reg->addSubRegsPreOrder(SR, RegBank);
334 SubRegIndex += SR.size() + 1;
336 OS << "/* Empty_SubRegsSet */ 0, ";
337 if (!Reg->getSuperRegs().empty()) {
338 OS << "/* " << Reg->getName() << "_SuperRegsSet */ " << SuperRegIndex;
339 SuperRegIndex += Reg->getSuperRegs().size() + 1;
341 OS << "/* Empty_SuperRegsSet */ 0";
344 OS << "};\n\n"; // End of register descriptors...
346 ArrayRef<CodeGenRegisterClass*> RegisterClasses = RegBank.getRegClasses();
348 // Loop over all of the register classes... emitting each one.
349 OS << "namespace { // Register classes...\n";
351 // Emit the register enum value arrays for each RegisterClass
352 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
353 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
354 ArrayRef<Record*> Order = RC.getOrder();
356 // Give the register class a legal C name if it's anonymous.
357 std::string Name = RC.getName();
359 // Emit the register list now.
360 OS << " // " << Name << " Register Class...\n"
361 << " const unsigned " << Name
363 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
364 Record *Reg = Order[i];
365 OS << getQualifiedName(Reg) << ", ";
369 OS << " // " << Name << " Bit set.\n"
370 << " const unsigned char " << Name
372 BitVectorEmitter BVE;
373 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
374 Record *Reg = Order[i];
375 BVE.add(Target.getRegBank().getReg(Reg)->EnumValue);
383 OS << "extern const MCRegisterClass " << TargetName
384 << "MCRegisterClasses[] = {\n";
386 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
387 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
388 OS << " { " << RC.getQualifiedName() + "RegClassID" << ", "
389 << '\"' << RC.getName() << "\", "
390 << RC.SpillSize/8 << ", "
391 << RC.SpillAlignment/8 << ", "
392 << RC.CopyCost << ", "
393 << RC.Allocatable << ", "
394 << RC.getName() << ", " << RC.getName() << "Bits, "
395 << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits) },\n";
400 // MCRegisterInfo initialization routine.
401 OS << "static inline void Init" << TargetName
402 << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
403 << "unsigned DwarfFlavour = 0, unsigned EHFlavour = 0) {\n";
404 OS << " RI->InitMCRegisterInfo(" << TargetName << "RegDesc, "
405 << Regs.size()+1 << ", RA, " << TargetName << "MCRegisterClasses, "
406 << RegisterClasses.size() << ", " << TargetName << "RegOverlaps, "
407 << TargetName << "SubRegsSet, " << TargetName << "SuperRegsSet);\n\n";
409 EmitRegMapping(OS, Regs, false);
414 OS << "} // End llvm namespace \n";
415 OS << "#endif // GET_REGINFO_MC_DESC\n\n";
419 RegisterInfoEmitter::runTargetHeader(raw_ostream &OS, CodeGenTarget &Target,
420 CodeGenRegBank &RegBank) {
421 EmitSourceFileHeader("Register Information Header Fragment", OS);
423 OS << "\n#ifdef GET_REGINFO_HEADER\n";
424 OS << "#undef GET_REGINFO_HEADER\n";
426 const std::string &TargetName = Target.getName();
427 std::string ClassName = TargetName + "GenRegisterInfo";
429 OS << "#include \"llvm/Target/TargetRegisterInfo.h\"\n";
430 OS << "#include <string>\n\n";
432 OS << "namespace llvm {\n\n";
434 OS << "struct " << ClassName << " : public TargetRegisterInfo {\n"
435 << " explicit " << ClassName
436 << "(unsigned RA, unsigned D = 0, unsigned E = 0);\n"
437 << " virtual bool needsStackRealignment(const MachineFunction &) const\n"
438 << " { return false; }\n"
439 << " unsigned getSubReg(unsigned RegNo, unsigned Index) const;\n"
440 << " unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const;\n"
441 << " unsigned composeSubRegIndices(unsigned, unsigned) const;\n"
442 << " const TargetRegisterClass *"
443 "getSubClassWithSubReg(const TargetRegisterClass*, unsigned) const;\n"
444 << " const TargetRegisterClass *getMatchingSuperRegClass("
445 "const TargetRegisterClass*, const TargetRegisterClass*, "
449 ArrayRef<CodeGenSubRegIndex*> SubRegIndices = RegBank.getSubRegIndices();
450 if (!SubRegIndices.empty()) {
451 OS << "\n// Subregister indices\n";
452 std::string Namespace =
453 SubRegIndices[0]->getNamespace();
454 if (!Namespace.empty())
455 OS << "namespace " << Namespace << " {\n";
456 OS << "enum {\n NoSubRegister,\n";
457 for (unsigned i = 0, e = RegBank.getNumNamedIndices(); i != e; ++i)
458 OS << " " << SubRegIndices[i]->getName() << ",\t// " << i+1 << "\n";
459 OS << " NUM_TARGET_NAMED_SUBREGS\n};\n";
460 if (!Namespace.empty())
464 ArrayRef<CodeGenRegisterClass*> RegisterClasses = RegBank.getRegClasses();
466 if (!RegisterClasses.empty()) {
467 OS << "namespace " << RegisterClasses[0]->Namespace
468 << " { // Register classes\n";
470 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
471 const CodeGenRegisterClass &RC = *RegisterClasses[i];
472 const std::string &Name = RC.getName();
474 // Output the register class definition.
475 OS << " struct " << Name << "Class : public TargetRegisterClass {\n"
476 << " " << Name << "Class();\n";
477 if (!RC.AltOrderSelect.empty())
478 OS << " ArrayRef<unsigned> "
479 "getRawAllocationOrder(const MachineFunction&) const;\n";
482 // Output the extern for the instance.
483 OS << " extern " << Name << "Class\t" << Name << "RegClass;\n";
484 // Output the extern for the pointer to the instance (should remove).
485 OS << " static const TargetRegisterClass * const " << Name
486 << "RegisterClass = &" << Name << "RegClass;\n";
488 OS << "} // end of namespace " << TargetName << "\n\n";
490 OS << "} // End llvm namespace \n";
491 OS << "#endif // GET_REGINFO_HEADER\n\n";
495 // runTargetDesc - Output the target register and register file descriptions.
498 RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
499 CodeGenRegBank &RegBank){
500 EmitSourceFileHeader("Target Register and Register Classes Information", OS);
502 OS << "\n#ifdef GET_REGINFO_TARGET_DESC\n";
503 OS << "#undef GET_REGINFO_TARGET_DESC\n";
505 OS << "namespace llvm {\n\n";
507 // Get access to MCRegisterClass data.
508 OS << "extern const MCRegisterClass " << Target.getName()
509 << "MCRegisterClasses[];\n";
511 // Start out by emitting each of the register classes.
512 ArrayRef<CodeGenRegisterClass*> RegisterClasses = RegBank.getRegClasses();
514 // Collect all registers belonging to any allocatable class.
515 std::set<Record*> AllocatableRegs;
517 // Collect allocatable registers.
518 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
519 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
520 ArrayRef<Record*> Order = RC.getOrder();
523 AllocatableRegs.insert(Order.begin(), Order.end());
526 OS << "namespace { // Register classes...\n";
528 // Emit the ValueType arrays for each RegisterClass
529 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
530 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
532 // Give the register class a legal C name if it's anonymous.
533 std::string Name = RC.getName() + "VTs";
535 // Emit the register list now.
537 << " Register Class Value Types...\n"
538 << " const MVT::SimpleValueType " << Name
540 for (unsigned i = 0, e = RC.VTs.size(); i != e; ++i)
541 OS << getEnumName(RC.VTs[i]) << ", ";
542 OS << "MVT::Other\n };\n\n";
544 OS << "} // end anonymous namespace\n\n";
546 // Now that all of the structs have been emitted, emit the instances.
547 if (!RegisterClasses.empty()) {
548 OS << "namespace " << RegisterClasses[0]->Namespace
549 << " { // Register class instances\n";
550 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
551 OS << " " << RegisterClasses[i]->getName() << "Class\t"
552 << RegisterClasses[i]->getName() << "RegClass;\n";
554 std::map<unsigned, std::set<unsigned> > SuperRegClassMap;
556 OS << "\n static const TargetRegisterClass* const "
557 << "NullRegClasses[] = { NULL };\n\n";
559 unsigned NumSubRegIndices = RegBank.getSubRegIndices().size();
561 if (NumSubRegIndices) {
562 // Compute the super-register classes for each RegisterClass
563 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
564 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
565 for (DenseMap<Record*,Record*>::const_iterator
566 i = RC.SubRegClasses.begin(),
567 e = RC.SubRegClasses.end(); i != e; ++i) {
568 // Find the register class number of i->second for SuperRegClassMap.
569 const CodeGenRegisterClass *RC2 = RegBank.getRegClass(i->second);
570 assert(RC2 && "Invalid register class in SubRegClasses");
571 SuperRegClassMap[RC2->EnumValue].insert(rc);
575 // Emit the super-register classes for each RegisterClass
576 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
577 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
579 // Give the register class a legal C name if it's anonymous.
580 std::string Name = RC.getName();
583 << " Super-register Classes...\n"
584 << " static const TargetRegisterClass* const "
585 << Name << "SuperRegClasses[] = {\n ";
588 std::map<unsigned, std::set<unsigned> >::iterator I =
589 SuperRegClassMap.find(rc);
590 if (I != SuperRegClassMap.end()) {
591 for (std::set<unsigned>::iterator II = I->second.begin(),
592 EE = I->second.end(); II != EE; ++II) {
593 const CodeGenRegisterClass &RC2 = *RegisterClasses[*II];
596 OS << "&" << RC2.getQualifiedName() << "RegClass";
601 OS << (!Empty ? ", " : "") << "NULL";
606 // Emit the sub-classes array for each RegisterClass
607 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
608 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
610 // Give the register class a legal C name if it's anonymous.
611 std::string Name = RC.getName();
613 OS << " static const unsigned " << Name << "SubclassMask[] = { ";
614 printBitVectorAsHex(OS, RC.getSubClasses(), 32);
618 // Emit NULL terminated super-class lists.
619 for (unsigned rc = 0, e = RegisterClasses.size(); rc != e; ++rc) {
620 const CodeGenRegisterClass &RC = *RegisterClasses[rc];
621 ArrayRef<CodeGenRegisterClass*> Supers = RC.getSuperClasses();
623 // Skip classes without supers. We can reuse NullRegClasses.
627 OS << " static const TargetRegisterClass* const "
628 << RC.getName() << "Superclasses[] = {\n";
629 for (unsigned i = 0; i != Supers.size(); ++i)
630 OS << " &" << Supers[i]->getQualifiedName() << "RegClass,\n";
631 OS << " NULL\n };\n\n";
635 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) {
636 const CodeGenRegisterClass &RC = *RegisterClasses[i];
637 OS << RC.getName() << "Class::" << RC.getName()
638 << "Class() : TargetRegisterClass(&"
639 << Target.getName() << "MCRegisterClasses["
640 << RC.getName() + "RegClassID" << "], "
641 << RC.getName() + "VTs" << ", "
642 << RC.getName() + "SubclassMask" << ", ";
643 if (RC.getSuperClasses().empty())
644 OS << "NullRegClasses, ";
646 OS << RC.getName() + "Superclasses, ";
647 OS << (NumSubRegIndices ? RC.getName() + "Super" : std::string("Null"))
650 if (!RC.AltOrderSelect.empty()) {
651 OS << "\nstatic inline unsigned " << RC.getName()
652 << "AltOrderSelect(const MachineFunction &MF) {"
653 << RC.AltOrderSelect << "}\n\nArrayRef<unsigned> "
654 << RC.getName() << "Class::"
655 << "getRawAllocationOrder(const MachineFunction &MF) const {\n";
656 for (unsigned oi = 1 , oe = RC.getNumOrders(); oi != oe; ++oi) {
657 ArrayRef<Record*> Elems = RC.getOrder(oi);
658 if (!Elems.empty()) {
659 OS << " static const unsigned AltOrder" << oi << "[] = {";
660 for (unsigned elem = 0; elem != Elems.size(); ++elem)
661 OS << (elem ? ", " : " ") << getQualifiedName(Elems[elem]);
665 OS << " const MCRegisterClass &MCR = " << Target.getName()
666 << "MCRegisterClasses[" << RC.getQualifiedName() + "RegClassID];\n"
667 << " static const ArrayRef<unsigned> Order[] = {\n"
668 << " makeArrayRef(MCR.begin(), MCR.getNumRegs()";
669 for (unsigned oi = 1, oe = RC.getNumOrders(); oi != oe; ++oi)
670 if (RC.getOrder(oi).empty())
671 OS << "),\n ArrayRef<unsigned>(";
673 OS << "),\n makeArrayRef(AltOrder" << oi;
674 OS << ")\n };\n const unsigned Select = " << RC.getName()
675 << "AltOrderSelect(MF);\n assert(Select < " << RC.getNumOrders()
676 << ");\n return Order[Select];\n}\n";
683 OS << "\nnamespace {\n";
684 OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
685 for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i)
686 OS << " &" << RegisterClasses[i]->getQualifiedName()
689 OS << "}\n"; // End of anonymous namespace...
691 // Emit extra information about registers.
692 const std::string &TargetName = Target.getName();
693 OS << "\n static const TargetRegisterInfoDesc "
694 << TargetName << "RegInfoDesc[] = "
695 << "{ // Extra Descriptors\n";
696 OS << " { 0, 0 },\n";
698 const std::vector<CodeGenRegister*> &Regs = RegBank.getRegisters();
699 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
700 const CodeGenRegister &Reg = *Regs[i];
702 OS << Reg.CostPerUse << ", "
703 << int(AllocatableRegs.count(Reg.TheDef)) << " },\n";
705 OS << " };\n"; // End of register descriptors...
708 // Calculate the mapping of subregister+index pairs to physical registers.
709 // This will also create further anonymous indexes.
710 unsigned NamedIndices = RegBank.getNumNamedIndices();
712 // Emit SubRegIndex names, skipping 0
713 ArrayRef<CodeGenSubRegIndex*> SubRegIndices = RegBank.getSubRegIndices();
714 OS << "\n static const char *const " << TargetName
715 << "SubRegIndexTable[] = { \"";
716 for (unsigned i = 0, e = SubRegIndices.size(); i != e; ++i) {
717 OS << SubRegIndices[i]->getName();
723 // Emit names of the anonymus subreg indexes.
724 if (SubRegIndices.size() > NamedIndices) {
726 for (unsigned i = NamedIndices, e = SubRegIndices.size(); i != e; ++i) {
727 OS << "\n " << SubRegIndices[i]->getName() << " = " << i+1;
735 std::string ClassName = Target.getName() + "GenRegisterInfo";
737 // Emit the subregister + index mapping function based on the information
739 OS << "unsigned " << ClassName
740 << "::getSubReg(unsigned RegNo, unsigned Index) const {\n"
741 << " switch (RegNo) {\n"
742 << " default:\n return 0;\n";
743 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
744 const CodeGenRegister::SubRegMap &SRM = Regs[i]->getSubRegs();
747 OS << " case " << getQualifiedName(Regs[i]->TheDef) << ":\n";
748 OS << " switch (Index) {\n";
749 OS << " default: return 0;\n";
750 for (CodeGenRegister::SubRegMap::const_iterator ii = SRM.begin(),
751 ie = SRM.end(); ii != ie; ++ii)
752 OS << " case " << ii->first->getQualifiedName()
753 << ": return " << getQualifiedName(ii->second->TheDef) << ";\n";
754 OS << " };\n" << " break;\n";
757 OS << " return 0;\n";
760 OS << "unsigned " << ClassName
761 << "::getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const {\n"
762 << " switch (RegNo) {\n"
763 << " default:\n return 0;\n";
764 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
765 const CodeGenRegister::SubRegMap &SRM = Regs[i]->getSubRegs();
768 OS << " case " << getQualifiedName(Regs[i]->TheDef) << ":\n";
769 for (CodeGenRegister::SubRegMap::const_iterator ii = SRM.begin(),
770 ie = SRM.end(); ii != ie; ++ii)
771 OS << " if (SubRegNo == " << getQualifiedName(ii->second->TheDef)
772 << ") return " << ii->first->getQualifiedName() << ";\n";
773 OS << " return 0;\n";
776 OS << " return 0;\n";
779 // Emit composeSubRegIndices
780 OS << "unsigned " << ClassName
781 << "::composeSubRegIndices(unsigned IdxA, unsigned IdxB) const {\n"
782 << " switch (IdxA) {\n"
783 << " default:\n return IdxB;\n";
784 for (unsigned i = 0, e = SubRegIndices.size(); i != e; ++i) {
786 for (unsigned j = 0; j != e; ++j) {
787 if (CodeGenSubRegIndex *Comp =
788 SubRegIndices[i]->compose(SubRegIndices[j])) {
790 OS << " case " << SubRegIndices[i]->getQualifiedName()
791 << ": switch(IdxB) {\n default: return IdxB;\n";
794 OS << " case " << SubRegIndices[j]->getQualifiedName()
795 << ": return " << Comp->getQualifiedName() << ";\n";
803 // Emit getSubClassWithSubReg.
804 OS << "const TargetRegisterClass *" << ClassName
805 << "::getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx)"
807 if (SubRegIndices.empty()) {
808 OS << " assert(Idx == 0 && \"Target has no sub-registers\");\n"
811 // Use the smallest type that can hold a regclass ID with room for a
813 if (RegisterClasses.size() < UINT8_MAX)
814 OS << " static const uint8_t Table[";
815 else if (RegisterClasses.size() < UINT16_MAX)
816 OS << " static const uint16_t Table[";
818 throw "Too many register classes.";
819 OS << RegisterClasses.size() << "][" << SubRegIndices.size() << "] = {\n";
820 for (unsigned rci = 0, rce = RegisterClasses.size(); rci != rce; ++rci) {
821 const CodeGenRegisterClass &RC = *RegisterClasses[rci];
822 OS << " {\t// " << RC.getName() << "\n";
823 for (unsigned sri = 0, sre = SubRegIndices.size(); sri != sre; ++sri) {
824 CodeGenSubRegIndex *Idx = SubRegIndices[sri];
825 if (CodeGenRegisterClass *SRC = RC.getSubClassWithSubReg(Idx))
826 OS << " " << SRC->EnumValue + 1 << ",\t// " << Idx->getName()
827 << " -> " << SRC->getName() << "\n";
829 OS << " 0,\t// " << Idx->getName() << "\n";
833 OS << " };\n assert(RC && \"Missing regclass\");\n"
834 << " if (!Idx) return RC;\n --Idx;\n"
835 << " assert(Idx < " << SubRegIndices.size() << " && \"Bad subreg\");\n"
836 << " unsigned TV = Table[RC->getID()][Idx];\n"
837 << " return TV ? getRegClass(TV - 1) : 0;\n";
841 // Emit getMatchingSuperRegClass.
842 OS << "const TargetRegisterClass *" << ClassName
843 << "::getMatchingSuperRegClass(const TargetRegisterClass *A,"
844 " const TargetRegisterClass *B, unsigned Idx) const {\n";
845 if (SubRegIndices.empty()) {
846 OS << " llvm_unreachable(\"Target has no sub-registers\");\n";
848 // We need to find the largest sub-class of A such that every register has
849 // an Idx sub-register in B. Map (B, Idx) to a bit-vector of
850 // super-register classes that map into B. Then compute the largest common
851 // sub-class with A by taking advantage of the register class ordering,
852 // like getCommonSubClass().
854 // Bitvector table is NumRCs x NumSubIndexes x BVWords, where BVWords is
855 // the number of 32-bit words required to represent all register classes.
856 const unsigned BVWords = (RegisterClasses.size()+31)/32;
857 BitVector BV(RegisterClasses.size());
859 OS << " static const unsigned Table[" << RegisterClasses.size()
860 << "][" << SubRegIndices.size() << "][" << BVWords << "] = {\n";
861 for (unsigned rci = 0, rce = RegisterClasses.size(); rci != rce; ++rci) {
862 const CodeGenRegisterClass &RC = *RegisterClasses[rci];
863 OS << " {\t// " << RC.getName() << "\n";
864 for (unsigned sri = 0, sre = SubRegIndices.size(); sri != sre; ++sri) {
865 CodeGenSubRegIndex *Idx = SubRegIndices[sri];
867 RC.getSuperRegClasses(Idx, BV);
869 printBitVectorAsHex(OS, BV, 32);
870 OS << "},\t// " << Idx->getName() << '\n';
874 OS << " };\n assert(A && B && \"Missing regclass\");\n"
876 << " assert(Idx < " << SubRegIndices.size() << " && \"Bad subreg\");\n"
877 << " const unsigned *TV = Table[B->getID()][Idx];\n"
878 << " const unsigned *SC = A->getSubClassMask();\n"
879 << " for (unsigned i = 0; i != " << BVWords << "; ++i)\n"
880 << " if (unsigned Common = TV[i] & SC[i])\n"
881 << " return getRegClass(32*i + CountTrailingZeros_32(Common));\n"
886 // Emit the constructor of the class...
887 OS << "extern const MCRegisterDesc " << TargetName << "RegDesc[];\n";
888 OS << "extern const unsigned " << TargetName << "RegOverlaps[];\n";
889 OS << "extern const unsigned " << TargetName << "SubRegsSet[];\n";
890 OS << "extern const unsigned " << TargetName << "SuperRegsSet[];\n";
892 OS << ClassName << "::" << ClassName
893 << "(unsigned RA, unsigned DwarfFlavour, unsigned EHFlavour)\n"
894 << " : TargetRegisterInfo(" << TargetName << "RegInfoDesc"
895 << ", RegisterClasses, RegisterClasses+" << RegisterClasses.size() <<",\n"
896 << " " << TargetName << "SubRegIndexTable) {\n"
897 << " InitMCRegisterInfo(" << TargetName << "RegDesc, "
898 << Regs.size()+1 << ", RA, " << TargetName << "MCRegisterClasses, "
899 << RegisterClasses.size() << ", " << TargetName << "RegOverlaps, "
900 << TargetName << "SubRegsSet, " << TargetName << "SuperRegsSet);\n\n";
902 EmitRegMapping(OS, Regs, true);
907 // Emit CalleeSavedRegs information.
908 std::vector<Record*> CSRSets =
909 Records.getAllDerivedDefinitions("CalleeSavedRegs");
910 for (unsigned i = 0, e = CSRSets.size(); i != e; ++i) {
911 Record *CSRSet = CSRSets[i];
912 const SetTheory::RecVec *Regs = RegBank.getSets().expand(CSRSet);
913 assert(Regs && "Cannot expand CalleeSavedRegs instance");
915 // Emit the *_SaveList list of callee-saved registers.
916 OS << "static const unsigned " << CSRSet->getName()
917 << "_SaveList[] = { ";
918 for (unsigned r = 0, re = Regs->size(); r != re; ++r)
919 OS << getQualifiedName((*Regs)[r]) << ", ";
922 // Emit the *_RegMask bit mask of call-preserved registers.
923 OS << "static const uint32_t " << CSRSet->getName()
924 << "_RegMask[] = { ";
925 printBitVectorAsHex(OS, RegBank.computeCoveredRegisters(*Regs), 32);
930 OS << "} // End llvm namespace \n";
931 OS << "#endif // GET_REGINFO_TARGET_DESC\n\n";
934 void RegisterInfoEmitter::run(raw_ostream &OS) {
935 CodeGenTarget Target(Records);
936 CodeGenRegBank &RegBank = Target.getRegBank();
937 RegBank.computeDerivedInfo();
939 runEnums(OS, Target, RegBank);
940 runMCDesc(OS, Target, RegBank);
941 runTargetHeader(OS, Target, RegBank);
942 runTargetDesc(OS, Target, RegBank);