[PATCH] D154807: [InstCombine] Transform `(add (shl (neg X), Cnt))` -> `(sub (shl X, Cnt))`

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 25 14:18:18 PDT 2023


nikic added a comment.

We have a general Negator infrastructure, which we could use to fold `add X, Y` to `sub X, -Y` if the latter is free. However, given how we currently use it to fold `sub X, Y` to `add X, -Y`, actually trying to do that would probably result in many new and exiting infinite combine loops, so I'm happy to take the one-off pattern...



================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1167
+  if (!MatchShlOfNeg(0) && !MatchShlOfNeg(1))
+    return nullptr;
+
----------------
Can this function be reduced to something along the lines of this?
```
if (match(I, m_c_Add(m_OneUse(m_Shl(m_OneUse(m_Neg(m_Value(X))), m_Value(Cnt))),
                     m_Value(X))) {
  Value *NewShl = Builder.CreateShl(X, Cnt);
  return BinaryOperator::CreateSub(Other, NewShl);
}
```


================
Comment at: llvm/test/Transforms/InstCombine/add-shift.ll:62
   ret <2 x i8> %r
 }
----------------
This only covers one of the one-use conditions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154807/new/

https://reviews.llvm.org/D154807



More information about the llvm-commits mailing list