[PATCH] D138814: [InstCombine] Combine a/lshr of add -> uadd.with.overflow

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 30 04:19:07 PST 2022


lebedev.ri added a comment.

Nice! This is the proper place for it.
I'm not sure i understand why you can't just zext the overflow bit
and RAUW all the original users with that?



================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp:864
+
+  // Check that the bitwidth of the operation is a power of two, and that
+  // the shift amount is 1/2 of it.
----------------
Is it not sufficient to just check that the width is 2x the shift amount?
Do we need the power-of-two check?


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp:880-891
+  // Make sure that `Op0` is only used by `I` and `HalfWidth`-truncates.
+  SmallVector<TruncInst *, 2> Truncs;
+  for (User *Usr : Op0->users()) {
+    if (Usr == &I)
+      continue;
+
+    TruncInst *Trunc = dyn_cast<TruncInst>(Usr);
----------------
This is rather unusual in InstCombine.
Would it not be sufficient to just zext the result?


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp:917-918
+  // Replace the use of `Op0` by `I` with `Overflow`.
+  if (IsAShr)
+    return new SExtInst(Overflow, Ty);
+  return new ZExtInst(Overflow, Ty);
----------------
`mul i16 255, 2` is `i16 510`, which is non-negative,
so the signbit of the original result is never set.
IOW, just zero-extend.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D138814



More information about the llvm-commits mailing list