[PATCH] D47908: [InstSimplify] add nuw %x, -1 -> -1 fold.

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 7 14:46:05 PDT 2018


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, craig.topper.

`%ret = add nuw i8 %x, C`
>From langref <https://llvm.org/docs/LangRef.html#add-instruction>:

  nuw and nsw stand for “No Unsigned Wrap” and “No Signed Wrap”,
  respectively. If the nuw and/or nsw keywords are present,
  the result value of the add is a poison value if unsigned
  and/or signed overflow, respectively, occurs.


So if `C` is `-1`, `%x` can only be `0`, and the result is always `-1`.

I'm not sure we want to use `KnownBits`/`LVI` here, because there is
exactly one possible value (all bits set, `-1`), so some other pass
should take care of replacing the known-all-ones with constant `-1`.

The `test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll` change *is* confusing.
What happening is, before this: (omitting `nuw` for simplicity)

1. First, InstCombine https://reviews.llvm.org/D47428/https://reviews.llvm.org/rL334127 folds `shl i32 1, %NBits`) to `shl nuw i32 -1, %NBits`
2. Then, InstSimplify https://reviews.llvm.org/D47883/https://reviews.llvm.org/rL334222 folds `shl nuw i32 -1, %NBits` to `-1`,
3. `-1` is inverted to `0`.

But now:

1. *This* InstSimplify fold `%ret = add nuw i32 %setbit, -1` -> `-1` happens first, before InstCombine https://reviews.llvm.org/D47428/https://reviews.llvm.org/rL334127 fold could happen.
2. `0` is inverted to `-1`.

Thus we now end up with the opposite constant, and it is all good.

https://rise4fun.com/Alive/sldC
Was mentioned in https://reviews.llvm.org/D47428 review.
Follow-up for https://reviews.llvm.org/D47883.


Repository:
  rL LLVM

https://reviews.llvm.org/D47908

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll
  test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47908.150408.patch
Type: text/x-patch
Size: 4573 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180607/23576427/attachment.bin>


More information about the llvm-commits mailing list