[llvm] [NaryReassociate] Teach NaryReassociate about UniformityAnalysis (PR #175167)

Pankaj Dwivedi via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 12 03:07:41 PST 2026


================
@@ -653,16 +731,35 @@ Value *NaryReassociatePass::tryReassociateMinOrMax(Instruction *I,
   const SCEV *BExpr = SE->getSCEV(B);
   const SCEV *RHSExpr = SE->getSCEV(RHS);
 
-  if (BExpr != RHSExpr) {
-    // Try (A op RHS) op B
-    if (auto *NewMinMax = tryCombination(A, AExpr, RHS, RHSExpr, B, BExpr))
-      return NewMinMax;
-  }
-
-  if (AExpr != RHSExpr) {
-    // Try (RHS op B) op A
-    if (auto *NewMinMax = tryCombination(RHS, RHSExpr, B, BExpr, A, AExpr))
-      return NewMinMax;
+  // Similar to binary ops, prefer grouping uniform values together when
+  // uniformity analysis is available.
+  // For I = minmax(minmax(A, B), RHS), we can form:
+  //   - minmax(minmax(A, RHS), B): groups A and RHS
+  //   - minmax(minmax(B, RHS), A): groups B and RHS
+  if (UI && UI->isUniform(B) && UI->isUniform(RHS) && !UI->isUniform(A)) {
+    LLVM_DEBUG(dbgs() << "NARY: Preferring uniform grouping for minmax " << *I
+                      << "\n");
+    // Try (B op RHS) op A first - groups uniform B with uniform RHS
+    if (AExpr != RHSExpr) {
+      if (auto *NewMinMax = tryCombination(RHS, RHSExpr, B, BExpr, A, AExpr))
+        return NewMinMax;
+    }
+    if (BExpr != RHSExpr) {
+      if (auto *NewMinMax = tryCombination(A, AExpr, RHS, RHSExpr, B, BExpr))
+        return NewMinMax;
+    }
----------------
PankajDwivedi-25 wrote:

Thanks, I have refactor it in the recent commit.

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


More information about the llvm-commits mailing list