Skip to content

Commit 5033431

Browse files
committed
[M68k][TableGen](1/8) TableGen related changes
- Add a new TableGen backend: CodeBeads - Add support to generate logical operand information For the first item, it is currently a workaround of M68k's (complex) instruction encoding. A typical architecture, especially CISC one like X86, normally uses `MCInstrDesc::TSFlags` to carry instruction encoding info. However, at the early days of M68k backend development, we found it difficult to fit every possible encoding into the 64-bit `MCInstrDesc::TSFlags`. Therefore CodeBeads was invented to provide an alternative, arbitrary length container for instruciton encoding info. However, in the long term we incline not to use a new TG backend for less common pattern like what we encountered in M68k. A bug has been created to host to discussion on migrating from CodeBeads to more concise solution: https://bugs.llvm.org/show_bug.cgi?id=48792 The second item was also served for similar purpose. It created utility functions that tell you the index of a `MachineOperand` in a `MachineInst` given a logical operand index. In normal cases a logical operand is the same as `MachineOperand`, but for operands using complex addressing mode a logical operand might be consisting of multiple `MachineOperand`. The TableGen-ed `getLogicalOperandIdx`, for instance, can give you the mapping between these two concepts. Nevertheless, we hope to remove this feature in the future if possible. Since it's not really useful for the targets supported by LLVM now either. Authors: myhsu, m4yers, glaubitz Differential Revision: https://reviews.llvm.org/D88385
1 parent a3fee39 commit 5033431

File tree

6 files changed

+344
-0
lines changed

6 files changed

+344
-0
lines changed

llvm/include/llvm/Target/Target.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,14 @@ class Instruction : InstructionEncoding {
639639
/// by TableGen.
640640
bit UseNamedOperandTable = false;
641641

642+
/// Should generate helper functions that help you to map a logical operand's
643+
/// index to the underlying MIOperand's index.
644+
/// In most architectures logical operand indicies are equal to
645+
/// MIOperand indicies, but for some CISC architectures, a logical operand
646+
/// might be consist of multiple MIOperand (e.g. a logical operand that
647+
/// uses complex address mode).
648+
bit UseLogicalOperandMappings = false;
649+
642650
/// Should FastISel ignore this instruction. For certain ISAs, they have
643651
/// instructions which map to the same ISD Opcode, value type operands and
644652
/// instruction selection predicates. FastISel cannot handle such cases, but

llvm/utils/TableGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_tablegen(llvm-tblgen LLVM
88
AsmWriterInst.cpp
99
Attributes.cpp
1010
CallingConvEmitter.cpp
11+
CodeBeadsGen.cpp
1112
CodeEmitterGen.cpp
1213
CodeGenDAGPatterns.cpp
1314
CodeGenHwModes.cpp

llvm/utils/TableGen/CodeBeadsGen.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//===---------- CodeBeadsGen.cpp - Code Beads Generator -------------------===//
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+
// CodeBeads are data fields carrying auxiliary information for instructions.
9+
//
10+
// Under the hood it's simply implemented by a `bits` field (with arbitrary
11+
// length) in each TG instruction description, where this TG backend will
12+
// generate a helper function to access it.
13+
//
14+
// This is especially useful for expressing variable length encoding
15+
// instructions and complex addressing modes. Since in those cases each
16+
// instruction is usually associated with large amount of information like
17+
// addressing mode details used on a specific operand. Instead of retreating to
18+
// ad-hoc methods to figure out these information when encoding an instruction,
19+
// CodeBeads provide a clean table for the instruction encoder to lookup.
20+
//===----------------------------------------------------------------------===//
21+
22+
#include "CodeGenTarget.h"
23+
#include "llvm/ADT/StringExtras.h"
24+
#include "llvm/Support/Debug.h"
25+
#include "llvm/TableGen/Error.h"
26+
#include "llvm/TableGen/Record.h"
27+
#include "llvm/TableGen/TableGenBackend.h"
28+
#include <map>
29+
#include <string>
30+
#include <vector>
31+
using namespace llvm;
32+
33+
namespace {
34+
35+
class CodeBeadsGen {
36+
RecordKeeper &Records;
37+
38+
public:
39+
CodeBeadsGen(RecordKeeper &R) : Records(R) {}
40+
void run(raw_ostream &OS);
41+
};
42+
43+
void CodeBeadsGen::run(raw_ostream &OS) {
44+
CodeGenTarget Target(Records);
45+
std::vector<Record *> Insts = Records.getAllDerivedDefinitions("Instruction");
46+
47+
// For little-endian instruction bit encodings, reverse the bit order
48+
Target.reverseBitsForLittleEndianEncoding();
49+
50+
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
51+
Target.getInstructionsByEnumValue();
52+
53+
// Emit function declaration
54+
OS << "const uint8_t *llvm::" << Target.getInstNamespace();
55+
OS << "::getMCInstrBeads(unsigned Opcode) {\n";
56+
57+
// First, get the maximum bit length among all beads. And do some
58+
// simple validation
59+
unsigned MaxBitLength = 0;
60+
61+
for (const CodeGenInstruction *CGI : NumberedInstructions) {
62+
Record *R = CGI->TheDef;
63+
if (!R->getValue("Beads"))
64+
continue;
65+
66+
BitsInit *BI = R->getValueAsBitsInit("Beads");
67+
if (!BI->isComplete()) {
68+
PrintFatalError(R->getLoc(), "Record `" + R->getName() +
69+
"', bit field 'Beads' is not complete");
70+
}
71+
72+
MaxBitLength = std::max(MaxBitLength, BI->getNumBits());
73+
}
74+
75+
// Number of bytes
76+
unsigned Parts = MaxBitLength / 8;
77+
78+
// Emit instruction base values
79+
OS << " static const uint8_t InstBits[][" << Parts << "] = {\n";
80+
for (const CodeGenInstruction *CGI : NumberedInstructions) {
81+
Record *R = CGI->TheDef;
82+
83+
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
84+
!R->getValue("Beads")) {
85+
OS << "\t{ 0x0 },\t// ";
86+
if (R->getValueAsBit("isPseudo"))
87+
OS << "(Pseudo) ";
88+
OS << R->getName() << "\n";
89+
continue;
90+
}
91+
92+
BitsInit *BI = R->getValueAsBitsInit("Beads");
93+
94+
// Convert to byte array:
95+
// [dcba] -> [a][b][c][d]
96+
OS << "\t{";
97+
for (unsigned p = 0; p < Parts; ++p) {
98+
unsigned Right = 8 * p;
99+
unsigned Left = Right + 8;
100+
101+
uint8_t Value = 0;
102+
for (unsigned i = Right; i != Left; ++i) {
103+
unsigned Shift = i % 8;
104+
if (auto *B = dyn_cast<BitInit>(BI->getBit(i))) {
105+
Value |= (static_cast<uint8_t>(B->getValue()) << Shift);
106+
} else {
107+
PrintFatalError(R->getLoc(), "Record `" + R->getName() +
108+
"', bit 'Beads[" + Twine(i) +
109+
"]' is not defined");
110+
}
111+
}
112+
113+
if (p)
114+
OS << ',';
115+
OS << " 0x";
116+
OS.write_hex(Value);
117+
OS << "";
118+
}
119+
OS << " }," << '\t' << "// " << R->getName() << "\n";
120+
}
121+
OS << "\t{ 0x0 }\n };\n";
122+
123+
// Emit initial function code
124+
OS << " return InstBits[Opcode];\n"
125+
<< "}\n\n";
126+
}
127+
128+
} // End anonymous namespace
129+
130+
namespace llvm {
131+
132+
void EmitCodeBeads(RecordKeeper &RK, raw_ostream &OS) {
133+
emitSourceFileHeader("Machine Code Beads", OS);
134+
CodeBeadsGen(RK).run(OS);
135+
}
136+
137+
} // namespace llvm

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "SequenceToOffsetTable.h"
2020
#include "TableGenBackends.h"
2121
#include "llvm/ADT/ArrayRef.h"
22+
#include "llvm/ADT/STLExtras.h"
2223
#include "llvm/ADT/StringExtras.h"
2324
#include "llvm/Support/Casting.h"
2425
#include "llvm/Support/raw_ostream.h"
@@ -27,6 +28,7 @@
2728
#include "llvm/TableGen/TableGenBackend.h"
2829
#include <cassert>
2930
#include <cstdint>
31+
#include <iterator>
3032
#include <map>
3133
#include <string>
3234
#include <utility>
@@ -87,6 +89,13 @@ class InstrInfoEmitter {
8789
void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target,
8890
ArrayRef<const CodeGenInstruction*> NumberedInstructions);
8991

92+
void emitLogicalOperandSizeMappings(
93+
raw_ostream &OS, StringRef Namespace,
94+
ArrayRef<const CodeGenInstruction *> NumberedInstructions);
95+
void emitLogicalOperandTypeMappings(
96+
raw_ostream &OS, StringRef Namespace,
97+
ArrayRef<const CodeGenInstruction *> NumberedInstructions);
98+
9099
// Operand information.
91100
void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs);
92101
std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst);
@@ -442,6 +451,182 @@ void InstrInfoEmitter::emitOperandTypeMappings(
442451
OS << "#endif // GET_INSTRINFO_OPERAND_TYPE\n\n";
443452
}
444453

454+
void InstrInfoEmitter::emitLogicalOperandSizeMappings(
455+
raw_ostream &OS, StringRef Namespace,
456+
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
457+
std::map<std::vector<unsigned>, unsigned> LogicalOpSizeMap;
458+
459+
std::map<unsigned, std::vector<std::string>> InstMap;
460+
461+
size_t LogicalOpListSize = 0U;
462+
std::vector<unsigned> LogicalOpList;
463+
for (const auto *Inst : NumberedInstructions) {
464+
if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings"))
465+
continue;
466+
467+
LogicalOpList.clear();
468+
llvm::transform(Inst->Operands, std::back_inserter(LogicalOpList),
469+
[](const CGIOperandList::OperandInfo &Op) -> unsigned {
470+
auto *MIOI = Op.MIOperandInfo;
471+
if (!MIOI || MIOI->getNumArgs() == 0)
472+
return 1;
473+
return MIOI->getNumArgs();
474+
});
475+
LogicalOpListSize = std::max(LogicalOpList.size(), LogicalOpListSize);
476+
477+
auto I =
478+
LogicalOpSizeMap.insert({LogicalOpList, LogicalOpSizeMap.size()}).first;
479+
InstMap[I->second].push_back(
480+
(Namespace + "::" + Inst->TheDef->getName()).str());
481+
}
482+
483+
OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n";
484+
OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n";
485+
OS << "namespace llvm {\n";
486+
OS << "namespace " << Namespace << " {\n";
487+
OS << "LLVM_READONLY static unsigned\n";
488+
OS << "getLogicalOperandSize(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
489+
if (!InstMap.empty()) {
490+
std::vector<const std::vector<unsigned> *> LogicalOpSizeList(
491+
LogicalOpSizeMap.size());
492+
for (auto &P : LogicalOpSizeMap) {
493+
LogicalOpSizeList[P.second] = &P.first;
494+
}
495+
OS << " static const unsigned SizeMap[][" << LogicalOpListSize
496+
<< "] = {\n";
497+
for (int r = 0, rs = LogicalOpSizeList.size(); r < rs; ++r) {
498+
const auto &Row = *LogicalOpSizeList[r];
499+
OS << " {";
500+
int i;
501+
for (i = 0; i < static_cast<int>(Row.size()); ++i) {
502+
OS << Row[i] << ", ";
503+
}
504+
for (; i < static_cast<int>(LogicalOpListSize); ++i) {
505+
OS << "0, ";
506+
}
507+
OS << "}, ";
508+
OS << "\n";
509+
}
510+
OS << " };\n";
511+
512+
OS << " switch (Opcode) {\n";
513+
OS << " default: return LogicalOpIdx;\n";
514+
for (auto &P : InstMap) {
515+
auto OpMapIdx = P.first;
516+
const auto &Insts = P.second;
517+
for (const auto &Inst : Insts) {
518+
OS << " case " << Inst << ":\n";
519+
}
520+
OS << " return SizeMap[" << OpMapIdx << "][LogicalOpIdx];\n";
521+
}
522+
OS << " }\n";
523+
} else {
524+
OS << " return LogicalOpIdx;\n";
525+
}
526+
OS << "}\n";
527+
528+
OS << "LLVM_READONLY static inline unsigned\n";
529+
OS << "getLogicalOperandIdx(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
530+
OS << " auto S = 0U;\n";
531+
OS << " for (auto i = 0U; i < LogicalOpIdx; ++i)\n";
532+
OS << " S += getLogicalOperandSize(Opcode, i);\n";
533+
OS << " return S;\n";
534+
OS << "}\n";
535+
536+
OS << "} // end namespace " << Namespace << "\n";
537+
OS << "} // end namespace llvm\n";
538+
OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n\n";
539+
}
540+
541+
void InstrInfoEmitter::emitLogicalOperandTypeMappings(
542+
raw_ostream &OS, StringRef Namespace,
543+
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
544+
std::map<std::vector<std::string>, unsigned> LogicalOpTypeMap;
545+
546+
std::map<unsigned, std::vector<std::string>> InstMap;
547+
548+
size_t OpTypeListSize = 0U;
549+
std::vector<std::string> LogicalOpTypeList;
550+
for (const auto *Inst : NumberedInstructions) {
551+
if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings"))
552+
continue;
553+
554+
LogicalOpTypeList.clear();
555+
for (const auto &Op : Inst->Operands) {
556+
auto *OpR = Op.Rec;
557+
if ((OpR->isSubClassOf("Operand") ||
558+
OpR->isSubClassOf("RegisterOperand") ||
559+
OpR->isSubClassOf("RegisterClass")) &&
560+
!OpR->isAnonymous()) {
561+
LogicalOpTypeList.push_back(
562+
(Namespace + "::OpTypes::" + Op.Rec->getName()).str());
563+
} else {
564+
LogicalOpTypeList.push_back("-1");
565+
}
566+
}
567+
OpTypeListSize = std::max(LogicalOpTypeList.size(), OpTypeListSize);
568+
569+
auto I =
570+
LogicalOpTypeMap.insert({LogicalOpTypeList, LogicalOpTypeMap.size()})
571+
.first;
572+
InstMap[I->second].push_back(
573+
(Namespace + "::" + Inst->TheDef->getName()).str());
574+
}
575+
576+
OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n";
577+
OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n";
578+
OS << "namespace llvm {\n";
579+
OS << "namespace " << Namespace << " {\n";
580+
OS << "LLVM_READONLY static int\n";
581+
OS << "getLogicalOperandType(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
582+
if (!InstMap.empty()) {
583+
std::vector<const std::vector<std::string> *> LogicalOpTypeList(
584+
LogicalOpTypeMap.size());
585+
for (auto &P : LogicalOpTypeMap) {
586+
LogicalOpTypeList[P.second] = &P.first;
587+
}
588+
OS << " static const int TypeMap[][" << OpTypeListSize << "] = {\n";
589+
for (int r = 0, rs = LogicalOpTypeList.size(); r < rs; ++r) {
590+
const auto &Row = *LogicalOpTypeList[r];
591+
OS << " {";
592+
int i, s = Row.size();
593+
for (i = 0; i < s; ++i) {
594+
if (i > 0)
595+
OS << ", ";
596+
OS << Row[i];
597+
}
598+
for (; i < static_cast<int>(OpTypeListSize); ++i) {
599+
if (i > 0)
600+
OS << ", ";
601+
OS << "-1";
602+
}
603+
OS << "}";
604+
if (r != rs - 1)
605+
OS << ",";
606+
OS << "\n";
607+
}
608+
OS << " };\n";
609+
610+
OS << " switch (Opcode) {\n";
611+
OS << " default: return -1;\n";
612+
for (auto &P : InstMap) {
613+
auto OpMapIdx = P.first;
614+
const auto &Insts = P.second;
615+
for (const auto &Inst : Insts) {
616+
OS << " case " << Inst << ":\n";
617+
}
618+
OS << " return TypeMap[" << OpMapIdx << "][LogicalOpIdx];\n";
619+
}
620+
OS << " }\n";
621+
} else {
622+
OS << " return -1;\n";
623+
}
624+
OS << "}\n";
625+
OS << "} // end namespace " << Namespace << "\n";
626+
OS << "} // end namespace llvm\n";
627+
OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n\n";
628+
}
629+
445630
void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS,
446631
StringRef TargetName) {
447632
RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
@@ -726,6 +911,12 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
726911
Records.startTimer("Emit operand type mappings");
727912
emitOperandTypeMappings(OS, Target, NumberedInstructions);
728913

914+
Records.startTimer("Emit logical operand size mappings");
915+
emitLogicalOperandSizeMappings(OS, TargetName, NumberedInstructions);
916+
917+
Records.startTimer("Emit logical operand type mappings");
918+
emitLogicalOperandTypeMappings(OS, TargetName, NumberedInstructions);
919+
729920
Records.startTimer("Emit helper methods");
730921
emitMCIIHelperMethods(OS, TargetName);
731922
}

0 commit comments

Comments
 (0)