[llvm] [Reassociate] Distribute multiply over add to enable factorization (PR #178201)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 18 07:51:20 PDT 2026


================
@@ -961,6 +961,65 @@ static BinaryOperator *convertOrWithNoCommonBitsToAdd(Instruction *Or) {
   return New;
 }
 
+/// Return true if Mul is of the form (X+Y)*C or (X-Y)*C where C is a
+/// constant, and there exists a sibling instruction of the form X*C' or Y*C'
+/// in the same expression — indicating that distribution followed by
+/// factoring will reduce the instruction count.
+static bool ShouldBreakUpDistribution(Instruction *Mul) {
+  Value *A, *B;
+  if (!match(Mul, m_Mul(m_OneUse(m_CombineOr(m_Add(m_Value(A), m_Value(B)),
+                                             m_Sub(m_Value(A), m_Value(B)))),
+                        m_ImmConstant())))
+    return false;
+
+  if (!Mul->hasOneUse())
+    return false;
----------------
dtcxzyw wrote:

Use `m_OneUse(m_Mul(` instead.

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


More information about the llvm-commits mailing list