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

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 17 08:13:02 PDT 2026


================
@@ -2569,6 +2655,26 @@ PreservedAnalyses ReassociatePass::run(Function &F, FunctionAnalysisManager &) {
 
   MadeChange = false;
 
+  // Pre-process: Distribute multiplications to enable reassociation
+  for (BasicBlock *BI : RPOT) {
+    for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;) {
+      Instruction *Inst = &*II;
+      ++II; // Advance before modification
+
+      if (auto *Mul = dyn_cast<BinaryOperator>(Inst)) {
+        if (Mul->getOpcode() == Instruction::Mul) {
----------------
hazarathayya wrote:

I have removed this and also the trydistributemul() fn call. 
In the new implementation ShouldBreakUpDistribution would check for users of X, Y in (X+Y)*Const before. so only distributes when profitable.
ShouldBreakUpDistribution may fire on unrelated sibling muls in other expression trees. A follow-up can tighten this by scanning the parent instruction's operands instead of the global user list.

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


More information about the llvm-commits mailing list