Skip to content

Commit 1a793a8

Browse files
authored
[LLVM][TableGen] Change X86InstrMapping to use const RecordKeeper (#109066)
Change X86InstrMappingEmitter to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent 5f02558 commit 1a793a8

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

llvm/utils/TableGen/X86InstrMappingEmitter.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ using namespace X86Disassembler;
2626
namespace {
2727

2828
class X86InstrMappingEmitter {
29-
RecordKeeper &Records;
30-
CodeGenTarget Target;
29+
const RecordKeeper &Records;
30+
const CodeGenTarget Target;
3131

3232
// Hold all pontentially compressible EVEX instructions
3333
std::vector<const CodeGenInstruction *> PreCompressionInsts;
@@ -44,7 +44,7 @@ class X86InstrMappingEmitter {
4444
PredicateInstMap PredicateInsts;
4545

4646
public:
47-
X86InstrMappingEmitter(RecordKeeper &R) : Records(R), Target(R) {}
47+
X86InstrMappingEmitter(const RecordKeeper &R) : Records(R), Target(R) {}
4848

4949
// run - Output X86 EVEX compression tables.
5050
void run(raw_ostream &OS);
@@ -63,8 +63,8 @@ class X86InstrMappingEmitter {
6363
void printClassDef(raw_ostream &OS);
6464
// Prints the given table as a C++ array of type X86TableEntry under the guard
6565
// \p Macro.
66-
void printTable(const std::vector<Entry> &Table, StringRef Name,
67-
StringRef Macro, raw_ostream &OS);
66+
void printTable(ArrayRef<Entry> Table, StringRef Name, StringRef Macro,
67+
raw_ostream &OS);
6868
};
6969

7070
void X86InstrMappingEmitter::printClassDef(raw_ostream &OS) {
@@ -90,9 +90,8 @@ static void printMacroEnd(StringRef Macro, raw_ostream &OS) {
9090
OS << "#endif // " << Macro << "\n\n";
9191
}
9292

93-
void X86InstrMappingEmitter::printTable(const std::vector<Entry> &Table,
94-
StringRef Name, StringRef Macro,
95-
raw_ostream &OS) {
93+
void X86InstrMappingEmitter::printTable(ArrayRef<Entry> Table, StringRef Name,
94+
StringRef Macro, raw_ostream &OS) {
9695
printMacroBegin(Macro, OS);
9796

9897
OS << "static const X86TableEntry " << Name << "[] = {\n";
@@ -220,7 +219,7 @@ void X86InstrMappingEmitter::emitCompressEVEXTable(
220219
assert(NewRec && "Instruction not found!");
221220
NewInst = &Target.getInstruction(NewRec);
222221
} else if (Name.ends_with("_EVEX")) {
223-
if (auto *NewRec = Records.getDef(Name.drop_back(5)))
222+
if (const auto *NewRec = Records.getDef(Name.drop_back(5)))
224223
NewInst = &Target.getInstruction(NewRec);
225224
} else if (Name.ends_with("_ND"))
226225
// Leave it to ND2NONND table.
@@ -319,7 +318,7 @@ void X86InstrMappingEmitter::emitND2NonNDTable(
319318
if (!isInteresting(Rec) || NoCompressSet.find(Name) != NoCompressSet.end())
320319
continue;
321320
if (ManualMap.find(Name) != ManualMap.end()) {
322-
auto *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
321+
const auto *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
323322
assert(NewRec && "Instruction not found!");
324323
auto &NewInst = Target.getInstruction(NewRec);
325324
Table.push_back(std::pair(Inst, &NewInst));
@@ -328,10 +327,10 @@ void X86InstrMappingEmitter::emitND2NonNDTable(
328327

329328
if (!Name.ends_with("_ND"))
330329
continue;
331-
auto *NewRec = Records.getDef(Name.drop_back(3));
330+
const auto *NewRec = Records.getDef(Name.drop_back(3));
332331
if (!NewRec)
333332
continue;
334-
auto &NewInst = Target.getInstruction(NewRec);
333+
const auto &NewInst = Target.getInstruction(NewRec);
335334
if (isRegisterOperand(NewInst.Operands[0].Rec))
336335
Table.push_back(std::pair(Inst, &NewInst));
337336
}
@@ -353,15 +352,15 @@ void X86InstrMappingEmitter::emitSSE2AVXTable(
353352
if (!isInteresting(Rec))
354353
continue;
355354
if (ManualMap.find(Name) != ManualMap.end()) {
356-
auto *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
355+
const auto *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
357356
assert(NewRec && "Instruction not found!");
358-
auto &NewInst = Target.getInstruction(NewRec);
357+
const auto &NewInst = Target.getInstruction(NewRec);
359358
Table.push_back(std::pair(Inst, &NewInst));
360359
continue;
361360
}
362361

363362
std::string NewName = ("V" + Name).str();
364-
auto *AVXRec = Records.getDef(NewName);
363+
const auto *AVXRec = Records.getDef(NewName);
365364
if (!AVXRec)
366365
continue;
367366
auto &AVXInst = Target.getInstruction(AVXRec);

llvm/utils/TableGen/X86RecognizableInstr.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,13 @@ RecognizableInstr::RecognizableInstr(DisassemblerTables &tables,
154154
UID(uid), Spec(&tables.specForUID(uid)) {
155155
// Check for 64-bit inst which does not require REX
156156
// FIXME: Is there some better way to check for In64BitMode?
157-
std::vector<Record *> Predicates = Rec->getValueAsListOfDefs("Predicates");
158-
for (unsigned i = 0, e = Predicates.size(); i != e; ++i) {
159-
if (Predicates[i]->getName().contains("Not64Bit") ||
160-
Predicates[i]->getName().contains("In32Bit")) {
157+
for (const Record *Predicate : Rec->getValueAsListOfConstDefs("Predicates")) {
158+
if (Predicate->getName().contains("Not64Bit") ||
159+
Predicate->getName().contains("In32Bit")) {
161160
Is32Bit = true;
162161
break;
163162
}
164-
if (Predicates[i]->getName().contains("In64Bit")) {
163+
if (Predicate->getName().contains("In64Bit")) {
165164
Is64Bit = true;
166165
break;
167166
}

0 commit comments

Comments
 (0)