[llvm] LICM: hoist BO assoc when BinOp is in RHS (PR #107072)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 06:16:45 PDT 2024


================
@@ -2825,30 +2823,45 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
   if (Opcode != Instruction::Add && Opcode != Instruction::Mul)
     return false;
 
-  auto *BO0 = dyn_cast<BinaryOperator>(BO->getOperand(0));
+  bool BinOpInRHS = isa<BinaryOperator>(BO->getOperand(1));
+  auto *BO0 = dyn_cast<BinaryOperator>(BO->getOperand(BinOpInRHS));
   if (!BO0 || BO0->getOpcode() != Opcode || !BO0->isAssociative() ||
       BO0->hasNUsesOrMore(3))
     return false;
 
   Value *LV = BO0->getOperand(0);
   Value *C1 = BO0->getOperand(1);
-  Value *C2 = BO->getOperand(1);
+  Value *C2 = BO->getOperand(!BinOpInRHS);
 
-  if (L.isLoopInvariant(LV) && !L.isLoopInvariant(C1)) {
-    assert(BO0->isCommutative() && "Associativity implies commutativity");
+  if (!L.isLoopInvariant(C2))
----------------
nikic wrote:

I don't understand the changes here, shouldn't the previous logic work?

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


More information about the llvm-commits mailing list