Skip to content

Commit a880c8e

Browse files
authored
[NFC][TableGen] Add accessors for various instruction subclasses (#146615)
- Add various instruction subclass/sub-slice accessors to `CodeGenTarget`. - Delete unused `inst_begin` and `inst_end` iterators. - Rename `Instructions` to `InstructionMap` and `getInstructions` to `getInstructionMap` to better represent their meaning. - Use these new accessors in InstrInfoEmitter
1 parent d457621 commit a880c8e

File tree

4 files changed

+63
-56
lines changed

4 files changed

+63
-56
lines changed

llvm/utils/TableGen/Common/CodeGenSchedule.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@ struct InstRegexOp : public SetTheory::Operator {
7777

7878
void apply(SetTheory &ST, const DagInit *Expr, SetTheory::RecSet &Elts,
7979
ArrayRef<SMLoc> Loc) override {
80-
ArrayRef<const CodeGenInstruction *> Instructions =
81-
Target.getInstructionsByEnumValue();
82-
83-
unsigned NumGeneric = Target.getNumFixedInstructions();
84-
unsigned NumPseudos = Target.getNumPseudoInstructions();
85-
auto Generics = Instructions.slice(0, NumGeneric);
86-
auto Pseudos = Instructions.slice(NumGeneric, NumPseudos);
87-
auto NonPseudos = Instructions.slice(NumGeneric + NumPseudos);
80+
ArrayRef<const CodeGenInstruction *> Generics =
81+
Target.getGenericInstructionsByEnumValue();
82+
ArrayRef<const CodeGenInstruction *> Pseudos =
83+
Target.getTargetPseudoInstructionsByEnumValue();
84+
ArrayRef<const CodeGenInstruction *> NonPseudos =
85+
Target.getTargetNonPseudoInstructionsByEnumValue();
8886

8987
for (const Init *Arg : Expr->getArgs()) {
9088
const StringInit *SI = dyn_cast<StringInit>(Arg);
@@ -145,7 +143,7 @@ struct InstRegexOp : public SetTheory::Operator {
145143
}
146144

147145
// Target instructions are split into two ranges: pseudo instructions
148-
// first, than non-pseudos. Each range is in lexicographical order
146+
// first, then non-pseudos. Each range is in lexicographical order
149147
// sorted by name. Find the sub-ranges that start with our prefix.
150148
struct Comp {
151149
bool operator()(const CodeGenInstruction *LHS, StringRef RHS) {

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ void CodeGenTarget::ReadInstructions() const {
211211

212212
// Parse the instructions defined in the .td file.
213213
for (const Record *R : Insts) {
214-
auto &Inst = Instructions[R];
215-
Inst = std::make_unique<CodeGenInstruction>(R);
216-
if (Inst->isVariableLengthEncoding())
217-
HasVariableLengthEncodings = true;
214+
auto [II, _] =
215+
InstructionMap.try_emplace(R, std::make_unique<CodeGenInstruction>(R));
216+
HasVariableLengthEncodings |= II->second->isVariableLengthEncoding();
218217
}
219218
}
220219

@@ -242,9 +241,9 @@ unsigned CodeGenTarget::getNumFixedInstructions() {
242241
/// Return all of the instructions defined by the target, ordered by
243242
/// their enum value.
244243
void CodeGenTarget::ComputeInstrsByEnum() const {
245-
const auto &Insts = getInstructions();
244+
const auto &InstMap = getInstructionMap();
246245
for (const char *Name : FixedInstrs) {
247-
const CodeGenInstruction *Instr = GetInstByName(Name, Insts, Records);
246+
const CodeGenInstruction *Instr = GetInstByName(Name, InstMap, Records);
248247
assert(Instr && "Missing target independent instruction");
249248
assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
250249
InstrsByEnum.push_back(Instr);
@@ -253,23 +252,24 @@ void CodeGenTarget::ComputeInstrsByEnum() const {
253252
assert(EndOfPredefines == getNumFixedInstructions() &&
254253
"Missing generic opcode");
255254

256-
for (const auto &I : Insts) {
257-
const CodeGenInstruction *CGI = I.second.get();
255+
for (const auto &[_, CGIUp] : InstMap) {
256+
const CodeGenInstruction *CGI = CGIUp.get();
258257
if (CGI->Namespace != "TargetOpcode") {
259258
InstrsByEnum.push_back(CGI);
260-
if (CGI->TheDef->getValueAsBit("isPseudo"))
261-
++NumPseudoInstructions;
259+
NumPseudoInstructions += CGI->TheDef->getValueAsBit("isPseudo");
262260
}
263261
}
264262

265-
assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
263+
assert(InstrsByEnum.size() == InstMap.size() && "Missing predefined instr");
266264

267265
// All of the instructions are now in random order based on the map iteration.
268266
llvm::sort(
269267
InstrsByEnum.begin() + EndOfPredefines, InstrsByEnum.end(),
270268
[](const CodeGenInstruction *Rec1, const CodeGenInstruction *Rec2) {
271-
const auto &D1 = *Rec1->TheDef;
272-
const auto &D2 = *Rec2->TheDef;
269+
const Record &D1 = *Rec1->TheDef;
270+
const Record &D2 = *Rec2->TheDef;
271+
// Sort all pseudo instructions before non-pseudo ones, and sort by name
272+
// within.
273273
return std::tuple(!D1.getValueAsBit("isPseudo"), D1.getName()) <
274274
std::tuple(!D2.getValueAsBit("isPseudo"), D2.getName());
275275
});

llvm/utils/TableGen/Common/CodeGenTarget.h

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CodeGenTarget {
5959
const Record *TargetRec;
6060

6161
mutable DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>>
62-
Instructions;
62+
InstructionMap;
6363
mutable std::unique_ptr<CodeGenRegBank> RegBank;
6464
mutable ArrayRef<const Record *> RegAltNameIndices;
6565
mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
@@ -154,31 +154,22 @@ class CodeGenTarget {
154154

155155
private:
156156
DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &
157-
getInstructions() const {
158-
if (Instructions.empty())
157+
getInstructionMap() const {
158+
if (InstructionMap.empty())
159159
ReadInstructions();
160-
return Instructions;
160+
return InstructionMap;
161161
}
162162

163163
public:
164164
CodeGenInstruction &getInstruction(const Record *InstRec) const {
165-
if (Instructions.empty())
166-
ReadInstructions();
167-
auto I = Instructions.find(InstRec);
168-
assert(I != Instructions.end() && "Not an instruction");
165+
auto I = getInstructionMap().find(InstRec);
166+
assert(I != InstructionMap.end() && "Not an instruction");
169167
return *I->second;
170168
}
171169

172170
/// Returns the number of predefined instructions.
173171
static unsigned getNumFixedInstructions();
174172

175-
/// Returns the number of pseudo instructions.
176-
unsigned getNumPseudoInstructions() const {
177-
if (InstrsByEnum.empty())
178-
ComputeInstrsByEnum();
179-
return NumPseudoInstructions;
180-
}
181-
182173
/// Return all of the instructions defined by the target, ordered by their
183174
/// enum value.
184175
/// The following order of instructions is also guaranteed:
@@ -191,19 +182,34 @@ class CodeGenTarget {
191182
return InstrsByEnum;
192183
}
193184

185+
// Functions that return various slices of `getInstructionsByEnumValue`.
186+
ArrayRef<const CodeGenInstruction *>
187+
getGenericInstructionsByEnumValue() const {
188+
return getInstructionsByEnumValue().take_front(getNumFixedInstructions());
189+
}
190+
191+
ArrayRef<const CodeGenInstruction *>
192+
getTargetInstructionsByEnumValue() const {
193+
return getInstructionsByEnumValue().drop_front(getNumFixedInstructions());
194+
}
195+
196+
ArrayRef<const CodeGenInstruction *>
197+
getTargetPseudoInstructionsByEnumValue() const {
198+
return getTargetInstructionsByEnumValue().take_front(NumPseudoInstructions);
199+
}
200+
201+
ArrayRef<const CodeGenInstruction *>
202+
getTargetNonPseudoInstructionsByEnumValue() const {
203+
return getTargetInstructionsByEnumValue().drop_front(NumPseudoInstructions);
204+
}
205+
194206
/// Return the integer enum value corresponding to this instruction record.
195207
unsigned getInstrIntValue(const Record *R) const {
196208
if (InstrsByEnum.empty())
197209
ComputeInstrsByEnum();
198210
return getInstruction(R).EnumVal;
199211
}
200212

201-
typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;
202-
inst_iterator inst_begin() const {
203-
return getInstructionsByEnumValue().begin();
204-
}
205-
inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
206-
207213
/// Return whether instructions have variable length encodings on this target.
208214
bool hasVariableLengthEncodings() const { return HasVariableLengthEncodings; }
209215

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ class InstrInfoEmitter {
9191
ArrayRef<const CodeGenInstruction *> NumberedInstructions);
9292
void emitOperandNameMappings(
9393
raw_ostream &OS, const CodeGenTarget &Target,
94-
ArrayRef<const CodeGenInstruction *> NumberedInstructions);
94+
ArrayRef<const CodeGenInstruction *> TargetInstructions);
9595
void emitLogicalOperandSizeMappings(
9696
raw_ostream &OS, StringRef Namespace,
97-
ArrayRef<const CodeGenInstruction *> NumberedInstructions);
97+
ArrayRef<const CodeGenInstruction *> TargetInstructions);
9898

9999
// Operand information.
100100
unsigned CollectOperandInfo(OperandInfoListTy &OperandInfoList,
@@ -234,9 +234,12 @@ void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
234234
/// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
235235
/// for looking up the operand index for an instruction, given a value from
236236
/// OpName enum
237+
///
238+
/// Fixed/Predefined instructions do not have UseNamedOperandTable enabled, so
239+
/// we can just skip them. Hence accept just the TargetInstructions.
237240
void InstrInfoEmitter::emitOperandNameMappings(
238241
raw_ostream &OS, const CodeGenTarget &Target,
239-
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
242+
ArrayRef<const CodeGenInstruction *> TargetInstructions) {
240243
StringRef Namespace = Target.getInstNamespace();
241244

242245
/// To facilitate assigning OpName enum values in the sorted alphabetical
@@ -260,9 +263,7 @@ void InstrInfoEmitter::emitOperandNameMappings(
260263

261264
// Fixed/Predefined instructions do not have UseNamedOperandTable enabled, so
262265
// we can just skip them.
263-
const unsigned NumFixedInsts = Target.getNumFixedInstructions();
264-
for (const CodeGenInstruction *Inst :
265-
NumberedInstructions.drop_front(NumFixedInsts)) {
266+
for (const CodeGenInstruction *Inst : TargetInstructions) {
266267
if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable"))
267268
continue;
268269
std::map<unsigned, unsigned> OpList;
@@ -476,19 +477,18 @@ void InstrInfoEmitter::emitOperandTypeMappings(
476477
OS << "#endif // GET_INSTRINFO_MEM_OPERAND_SIZE\n\n";
477478
}
478479

480+
// Fixed/Predefined instructions do not have UseLogicalOperandMappings
481+
// enabled, so we can just skip them. Hence accept TargetInstructions.
479482
void InstrInfoEmitter::emitLogicalOperandSizeMappings(
480483
raw_ostream &OS, StringRef Namespace,
481-
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
484+
ArrayRef<const CodeGenInstruction *> TargetInstructions) {
482485
std::map<std::vector<unsigned>, unsigned> LogicalOpSizeMap;
483486
std::map<unsigned, std::vector<std::string>> InstMap;
484487

485488
size_t LogicalOpListSize = 0U;
486489
std::vector<unsigned> LogicalOpList;
487490

488-
// Fixed/Predefined instructions do not have UseLogicalOperandMappings
489-
// enabled, so we can just skip them.
490-
const unsigned NumFixedInsts = CDP.getTargetInfo().getNumFixedInstructions();
491-
for (const auto *Inst : NumberedInstructions.drop_front(NumFixedInsts)) {
491+
for (const auto *Inst : TargetInstructions) {
492492
if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings"))
493493
continue;
494494

@@ -1057,17 +1057,20 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
10571057

10581058
OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n";
10591059

1060+
ArrayRef<const CodeGenInstruction *> TargetInstructions =
1061+
Target.getTargetInstructionsByEnumValue();
1062+
10601063
if (HasUseNamedOperandTable) {
10611064
Timer.startTimer("Emit operand name mappings");
1062-
emitOperandNameMappings(OS, Target, NumberedInstructions);
1065+
emitOperandNameMappings(OS, Target, TargetInstructions);
10631066
}
10641067

10651068
Timer.startTimer("Emit operand type mappings");
10661069
emitOperandTypeMappings(OS, Target, NumberedInstructions);
10671070

10681071
if (HasUseLogicalOperandMappings) {
10691072
Timer.startTimer("Emit logical operand size mappings");
1070-
emitLogicalOperandSizeMappings(OS, TargetName, NumberedInstructions);
1073+
emitLogicalOperandSizeMappings(OS, TargetName, TargetInstructions);
10711074
}
10721075

10731076
Timer.startTimer("Emit helper methods");

0 commit comments

Comments
 (0)