[PATCH] D145073: [InstCombine] fold signed absolute diff patterns

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 3 10:25:09 PST 2023


spatel marked an inline comment as done.
spatel added inline comments.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp:980
+    // The remaining subtract is not "nuw" any more, and it is "nsw" only if
+    // it was already "nsw" or if this is the only use.
+    TI->setHasNoUnsignedWrap(false);
----------------
goldstein.w.n wrote:
> Why does one-use allow it to be `nsw`?
If there's one use of the subtract (no other use than the use we are about to replace), then we know that the sub is "nsw" in this context even if it was "nuw" before:
https://alive2.llvm.org/ce/z/cmSzrG

That's what we are verifying in the `abs_diff_signed_sgt_nuw` test.

But if there's another use, then we can't add "nsw" to the existing instruction because it may not be safe in the other user's context:
https://alive2.llvm.org/ce/z/mio8xf

This is more complicated than the usual no-wrap propagation. I can add text like this to the code comment if this helped to explain it.


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

https://reviews.llvm.org/D145073



More information about the llvm-commits mailing list