[llvm] [SLP] Normalize copyable operand order via majority voting (PR #191631)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 13 09:49:19 PDT 2026


================
@@ -11931,12 +11931,76 @@ class InstructionsCompatibilityAnalysis {
     if (S.areInstructionsWithCopyableElements()) {
       MainOp = S.getMainOp();
       MainOpcode = S.getOpcode();
+      const bool IsCommutative =
+          isCommutative(MainOp) && MainOp->getNumOperands() == 2;
       Operands.assign(MainOp->getNumOperands(),
                       BoUpSLP::ValueList(VL.size(), nullptr));
+      // Build operands and simultaneously count (ID0, ID1) pair
+      // frequencies for commutative operand normalization. Pairs and
+      // their inverses are tracked under a canonical key so that
+      // (Load, Add) and (Add, Load) contribute to the same bucket.
+      struct PairInfo {
+        unsigned FwdCount = 0;
+        unsigned RevCount = 0;
+        unsigned MinID = 0;
+        unsigned MaxID = 0;
+      };
+      SmallMapVector<std::pair<unsigned, unsigned>, PairInfo, 8> PairCounts;
+      unsigned MajID0 = 0, MajID1 = 0;
       for (auto [Idx, V] : enumerate(VL)) {
         SmallVector<Value *> OperandsForValue = getOperands(S, V);
         for (auto [OperandIdx, Operand] : enumerate(OperandsForValue))
           Operands[OperandIdx][Idx] = Operand;
+        if (!IsCommutative || S.isCopyableElement(V) || isa<PoisonValue>(V))
+          continue;
+        unsigned ID0 = OperandsForValue[0]->getValueID();
+        unsigned ID1 = OperandsForValue[1]->getValueID();
----------------
alexey-bataev wrote:

We're not tied directly to some particular values here, just using them for sorting/choosing better candidates, so it should be fine here

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


More information about the llvm-commits mailing list