From 9de33dbf7b04ea94d71c8c786f381040a89413be Mon Sep 17 00:00:00 2001 From: Wang Pengcheng Date: Wed, 2 Apr 2025 17:42:10 +0800 Subject: [PATCH 1/3] [TableGen] Emit `llvm::is_contained` for `CheckOpcode` predicate 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. --- llvm/test/TableGen/MacroFusion.td | 6 ++--- .../TableGen/Common/PredicateExpander.cpp | 23 ++++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/llvm/test/TableGen/MacroFusion.td b/llvm/test/TableGen/MacroFusion.td index 6cf22f5447150..73a8bf263ff5b 100644 --- a/llvm/test/TableGen/MacroFusion.td +++ b/llvm/test/TableGen/MacroFusion.td @@ -34,7 +34,7 @@ let Namespace = "Test" in { def Inst0 : TestInst<0>; def Inst1 : TestInst<1>; let isCommutable = true in -def Inst2 : TestInst<2>; +def Inst2 : TestInst<1>; def BothFusionPredicate: BothFusionPredicateWithMCInstPredicate>; def TestBothFusionPredicate: Fusion<"test-both-fusion-predicate", "HasBothFusionPredicate", @@ -42,7 +42,7 @@ def TestBothFusionPredicate: Fusion<"test-both-fusion-predicate", "HasBothFusion [BothFusionPredicate]>; def TestFusion: SimpleFusion<"test-fusion", "HasTestFusion", "Test Fusion", - CheckOpcode<[Inst0]>, + CheckOpcode<[Inst0, Inst1]>, CheckAll<[ CheckOpcode<[Inst1]>, CheckRegOperand<0, X0> @@ -162,7 +162,7 @@ def TestSingleFusion: SingleFusion<"test-single-fusion", "HasTestSingleFusion", // CHECK-PREDICATOR-NEXT: return true; // CHECK-PREDICATOR-NEXT: { // CHECK-PREDICATOR-NEXT: const MachineInstr *MI = FirstMI; -// CHECK-PREDICATOR-NEXT: if (( MI->getOpcode() != Test::Inst0 )) +// CHECK-PREDICATOR-NEXT: if (!llvm::is_contained({Test::Inst0, Test::Inst1}, MI->getOpcode())) // CHECK-PREDICATOR-NEXT: return false; // CHECK-PREDICATOR-NEXT: } // CHECK-PREDICATOR-NEXT: if (!SecondMI.getOperand(0).getReg().isVirtual()) { diff --git a/llvm/utils/TableGen/Common/PredicateExpander.cpp b/llvm/utils/TableGen/Common/PredicateExpander.cpp index e54df89937c4a..f3d88cfab3fb9 100644 --- a/llvm/utils/TableGen/Common/PredicateExpander.cpp +++ b/llvm/utils/TableGen/Common/PredicateExpander.cpp @@ -143,7 +143,6 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) { void PredicateExpander::expandCheckOpcode(raw_ostream &OS, ArrayRef Opcodes) { assert(!Opcodes.empty() && "Expected at least one opcode to check!"); - bool First = true; if (Opcodes.size() == 1) { OS << "( "; @@ -152,19 +151,15 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS, return; } - OS << '('; - ++Indent; - for (const Record *Rec : Opcodes) { - OS << '\n' << Indent; - if (!First) - OS << (shouldNegate() ? "&& " : "|| "); - - expandCheckOpcode(OS, Rec); - First = false; - } - - --Indent; - OS << '\n' << Indent << ')'; + if (shouldNegate()) + OS << "!"; + OS << "llvm::is_contained("; + ListSeparator Sep; + OS << "{"; + for (const Record *Inst : Opcodes) + OS << Sep << Inst->getValueAsString("Namespace") << "::" << Inst->getName(); + OS << "}"; + OS << ", MI" << (isByRef() ? "." : "->") << "getOpcode())"; } void PredicateExpander::expandCheckPseudo(raw_ostream &OS, From 56a915841447c72a99a853ee5765b6c3261bf7ba Mon Sep 17 00:00:00 2001 From: Wang Pengcheng Date: Wed, 2 Apr 2025 21:09:55 +0800 Subject: [PATCH 2/3] Use single quote --- llvm/utils/TableGen/Common/PredicateExpander.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/utils/TableGen/Common/PredicateExpander.cpp b/llvm/utils/TableGen/Common/PredicateExpander.cpp index f3d88cfab3fb9..09d953801600d 100644 --- a/llvm/utils/TableGen/Common/PredicateExpander.cpp +++ b/llvm/utils/TableGen/Common/PredicateExpander.cpp @@ -152,13 +152,13 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS, } if (shouldNegate()) - OS << "!"; + OS << '!'; OS << "llvm::is_contained("; ListSeparator Sep; - OS << "{"; + OS << '{'; for (const Record *Inst : Opcodes) OS << Sep << Inst->getValueAsString("Namespace") << "::" << Inst->getName(); - OS << "}"; + OS << '}'; OS << ", MI" << (isByRef() ? "." : "->") << "getOpcode())"; } From 0c35cb3cb971a26f0d3ee79e75bd9e6ff9a82c64 Mon Sep 17 00:00:00 2001 From: Wang Pengcheng Date: Wed, 2 Apr 2025 21:11:01 +0800 Subject: [PATCH 3/3] fix test type --- llvm/test/TableGen/MacroFusion.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/TableGen/MacroFusion.td b/llvm/test/TableGen/MacroFusion.td index 73a8bf263ff5b..66cff7ec4ef4c 100644 --- a/llvm/test/TableGen/MacroFusion.td +++ b/llvm/test/TableGen/MacroFusion.td @@ -34,7 +34,7 @@ let Namespace = "Test" in { def Inst0 : TestInst<0>; def Inst1 : TestInst<1>; let isCommutable = true in -def Inst2 : TestInst<1>; +def Inst2 : TestInst<2>; def BothFusionPredicate: BothFusionPredicateWithMCInstPredicate>; def TestBothFusionPredicate: Fusion<"test-both-fusion-predicate", "HasBothFusionPredicate",