[llvm] LICM: hoist BO assoc when (C1 op LV) op C2 (PR #106999)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 02:41:31 PDT 2024


================
@@ -2830,11 +2834,15 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
       BO0->hasNUsesOrMore(3))
     return false;
 
-  // Transform: "(LV op C1) op C2" ==> "LV op (C1 op C2)"
   Value *LV = BO0->getOperand(0);
   Value *C1 = BO0->getOperand(1);
   Value *C2 = BO->getOperand(1);
 
+  if (L.isLoopInvariant(LV) && !L.isLoopInvariant(C1)) {
+    assert(BO->isCommutative() && BO0->isCommutative() &&
+           "Associativity implies commutativity");
----------------
nikic wrote:

```suggestion
    assert(BO0->isCommutative() && "Associativity implies commutativity");
```
We're only relying on the commutativity of BO0 here, so I think it would be more explicit to only check it. But I'm okay keeping it as-is as well.

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


More information about the llvm-commits mailing list