Skip to content

Commit 4986a79

Browse files
authored
[TableGen] Emit llvm::is_contained for CheckOpcode predicate (llvm#134057)
When the list is large, using `llvm::is_contained` is of higher performance than a sequence of comparisons. When the list is small, the `llvm::is_contained` can be inlined and unrolled, which has the same effect as using a sequence of comparisons. And the generated code is more readable.
1 parent 4fe0d74 commit 4986a79

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

llvm/test/TableGen/MacroFusion.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def TestBothFusionPredicate: Fusion<"test-both-fusion-predicate", "HasBothFusion
4242
[BothFusionPredicate]>;
4343

4444
def TestFusion: SimpleFusion<"test-fusion", "HasTestFusion", "Test Fusion",
45-
CheckOpcode<[Inst0]>,
45+
CheckOpcode<[Inst0, Inst1]>,
4646
CheckAll<[
4747
CheckOpcode<[Inst1]>,
4848
CheckRegOperand<0, X0>
@@ -162,7 +162,7 @@ def TestSingleFusion: SingleFusion<"test-single-fusion", "HasTestSingleFusion",
162162
// CHECK-PREDICATOR-NEXT: return true;
163163
// CHECK-PREDICATOR-NEXT: {
164164
// CHECK-PREDICATOR-NEXT: const MachineInstr *MI = FirstMI;
165-
// CHECK-PREDICATOR-NEXT: if (( MI->getOpcode() != Test::Inst0 ))
165+
// CHECK-PREDICATOR-NEXT: if (!llvm::is_contained({Test::Inst0, Test::Inst1}, MI->getOpcode()))
166166
// CHECK-PREDICATOR-NEXT: return false;
167167
// CHECK-PREDICATOR-NEXT: }
168168
// CHECK-PREDICATOR-NEXT: if (!SecondMI.getOperand(0).getReg().isVirtual()) {

llvm/utils/TableGen/Common/PredicateExpander.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
143143
void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
144144
ArrayRef<const Record *> Opcodes) {
145145
assert(!Opcodes.empty() && "Expected at least one opcode to check!");
146-
bool First = true;
147146

148147
if (Opcodes.size() == 1) {
149148
OS << "( ";
@@ -152,19 +151,15 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
152151
return;
153152
}
154153

155-
OS << '(';
156-
++Indent;
157-
for (const Record *Rec : Opcodes) {
158-
OS << '\n' << Indent;
159-
if (!First)
160-
OS << (shouldNegate() ? "&& " : "|| ");
161-
162-
expandCheckOpcode(OS, Rec);
163-
First = false;
164-
}
165-
166-
--Indent;
167-
OS << '\n' << Indent << ')';
154+
if (shouldNegate())
155+
OS << '!';
156+
OS << "llvm::is_contained(";
157+
ListSeparator Sep;
158+
OS << '{';
159+
for (const Record *Inst : Opcodes)
160+
OS << Sep << Inst->getValueAsString("Namespace") << "::" << Inst->getName();
161+
OS << '}';
162+
OS << ", MI" << (isByRef() ? "." : "->") << "getOpcode())";
168163
}
169164

170165
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,

0 commit comments

Comments
 (0)