[llvm] [InstCombine] Handle commuted pattern for `((X s/ C1) << C2) + X` (PR #121737)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 6 02:31:31 PST 2025


================
@@ -1326,6 +1326,18 @@ Instruction *InstCombinerImpl::foldAddLikeCommutative(Value *LHS, Value *RHS,
     R->setHasNoUnsignedWrap(NUWOut);
     return R;
   }
+
+  // ((X s/ C1) << C2) + X => X s% -C1 where -C1 is 1 << C2
+  const APInt *C1, *C2;
+  if (match(LHS, m_Shl(m_SDiv(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))) {
+    APInt one(C2->getBitWidth(), 1);
+    APInt minusC1 = -(*C1);
+    if (minusC1 == (one << *C2)) {
+      Constant *NewRHS = ConstantInt::get(RHS->getType(), minusC1);
----------------
nikic wrote:

```suggestion
    APInt One(C2->getBitWidth(), 1);
    APInt MinusC1 = -(*C1);
    if (MinusC1 == (One << *C2)) {
      Constant *NewRHS = ConstantInt::get(RHS->getType(), MinusC1);
```
While you are here, might as well fix the coding style...

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


More information about the llvm-commits mailing list