Skip to content

Commit cebb236

Browse files
statham-armLukacma
authored andcommitted
[TableGen] List the indices of sub-operands (llvm#163723)
Some instances of the `Operand` class used in Tablegen instruction definitions expand to a cluster of multiple operands at the MC layer, such as complex addressing modes involving base + offset + shift, or clusters of operands describing conditional Arm instructions or predicated MVE instructions. There's currently no convenient way for C++ code to know the offset of one of those sub-operands from the start of the cluster: instead it just hard-codes magic numbers like `index+2`, which is hard to read and fragile. This patch adds an extra piece of output to `InstrInfoEmitter` to define those instruction offsets, based on the name of the `Operand` class instance in Tablegen, and the names assigned to the sub-operands in the `MIOperandInfo` field. For example, if target Foo were to define def Bar : Operand { let MIOperandInfo = (ops GPR:$first, i32imm:$second); // ... } then the new constants would be `Foo::SUBOP_Bar_first` and `Foo::SUBOP_Bar_second`, defined as 0 and 1 respectively. As an example, I've converted some magic numbers related to the MVE predication operand types (`vpred_n` and its superset `vpred_r`) to use the new named constants in place of the integer literals they previously used. This is more verbose, but also clearer, because it explains why the integer is chosen instead of what its value is.
1 parent 99836ec commit cebb236

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ static bool producesFalseLanesZero(MachineInstr &MI,
882882
continue;
883883
// Skip the lr predicate reg
884884
int PIdx = llvm::findFirstVPTPredOperandIdx(MI);
885-
if (PIdx != -1 && (int)MO.getOperandNo() == PIdx + 2)
885+
if (PIdx != -1 && MO.getOperandNo() == PIdx + ARM::SUBOP_vpred_n_tp_reg)
886886
continue;
887887

888888
// Check that this instruction will produce zeros in its false lanes:

llvm/lib/Target/ARM/MVETPAndVPTOptimisationsPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ bool MVETPAndVPTOptimisations::ConvertTailPredLoop(MachineLoop *ML,
534534
Register LR = LoopPhi->getOperand(0).getReg();
535535
for (MachineInstr *MI : MVEInstrs) {
536536
int Idx = findFirstVPTPredOperandIdx(*MI);
537-
MI->getOperand(Idx + 2).setReg(LR);
537+
MI->getOperand(Idx + ARM::SUBOP_vpred_n_tp_reg).setReg(LR);
538538
}
539539
}
540540

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/Format.h"
3030
#include "llvm/Support/SourceMgr.h"
3131
#include "llvm/Support/raw_ostream.h"
32+
#include "llvm/TableGen/CodeGenHelpers.h"
3233
#include "llvm/TableGen/Error.h"
3334
#include "llvm/TableGen/Record.h"
3435
#include "llvm/TableGen/TGTimer.h"
@@ -1135,6 +1136,22 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
11351136

11361137
OS << "\n};\n} // end namespace llvm\n";
11371138

1139+
{
1140+
NamespaceEmitter LlvmNS(OS, "llvm");
1141+
NamespaceEmitter TargetNS(OS, Target.getInstNamespace());
1142+
for (const Record *R : Records.getAllDerivedDefinitions("Operand")) {
1143+
if (R->isAnonymous())
1144+
continue;
1145+
if (const DagInit *D = R->getValueAsDag("MIOperandInfo")) {
1146+
for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) {
1147+
if (const StringInit *Name = D->getArgName(i))
1148+
OS << "constexpr unsigned SUBOP_" << R->getName() << "_"
1149+
<< Name->getValue() << " = " << i << ";\n";
1150+
}
1151+
}
1152+
}
1153+
}
1154+
11381155
OS << "#endif // GET_INSTRINFO_HEADER\n\n";
11391156

11401157
OS << "#ifdef GET_INSTRINFO_HELPER_DECLS\n";

0 commit comments

Comments
 (0)