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

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 22:01:09 PDT 2026


================
@@ -963,6 +963,69 @@ 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_c_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())
----------------
hazarathayya wrote:

Actually, m_OneUse only covers the add/sub instructions. so we still need Mul->hasOneUse() check to ensure multiplication itself isn't duplicated.

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


More information about the llvm-commits mailing list