[llvm] [NVPTX] Add commutativity to SETP instructions to enable MachineCSE of inverted predicates (PR #191890)

Alex MacLean via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 14:06:17 PDT 2026


================
@@ -82,10 +83,65 @@ def BranchFlag : OperandWithDefaultOps<i1, (ops (i1 0))> {
   let PrintMethod = "printNegatedPredicate";
 }
 
-// Compare modes
-// These must match the enum in NVPTX.h
-def CmpEQ : PatLeaf<(i32 0)>;
-def CmpNE : PatLeaf<(i32 1)>;
+// SETP compare modes
+
+// Sentinel meaning "no valid inverse for this mode".
+defvar CmpModeInvalidInt = 0xFF;
+
+class CmpModeRec<int val, string name,
+                 int intInv, int fpInv,
+                 string fcmp, string icmp, string ityp> {
+  // Numeric encoding carried by the setp machine operand.
+  bits<8> Value           = val;
+  // Enumerator spelling in NVPTX::PTXCmpMode.
+  string  Name            = name;
+  // Inverse predicate under integer-setp semantics
+  bits<8> IntInverseValue = intInv;
+  // Inverse predicate under scalar-float-setp semantics
+  bits<8> FPInverseValue  = fpInv;
+  // Print mnemonic for float compares.
+  string  FCmpPrintStr    = fcmp;
+  // Print mnemonic for int compares.
+  string  ICmpPrintStr    = icmp;
+  // Signedness suffix for the integer setp instruction name (b/s/u);
+  string  ITypePrintStr   = ityp;
+}
+
+//                                    val  name          intInv             fpInv fcmp     icmp   ityp
+def CmpModeEQ_Rec         : CmpModeRec<0,  "EQ",         1,                 7,    "eq",    "eq",   "b">;
+def CmpModeNE_Rec         : CmpModeRec<1,  "NE",         0,                 6,    "ne",    "ne",   "b">;
----------------
AlexMaclean wrote:

FWIW, I personally think the previous implementation where there was a static switch that clearly mapped each opcode do the inverted opcode was actually a lot easier to understand than this grid of numbers. Maybe we can stick with that approach for now and revisit adding SearchableTables for CmpMode in a future NFC?

https://github.com/llvm/llvm-project/pull/191890


More information about the llvm-commits mailing list