OSDN Git Service

[InstSimplify] add nuw %x, -1 -> -1 fold.
authorRoman Lebedev <lebedev.ri@gmail.com>
Fri, 8 Jun 2018 15:44:47 +0000 (15:44 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Fri, 8 Jun 2018 15:44:47 +0000 (15:44 +0000)
Summary:
`%ret = add nuw i8 %x, C`
From [[ https://llvm.org/docs/LangRef.html#add-instruction | langref ]]:
    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 D47428/rL334127 folds `shl i32 1, %NBits`) to `shl nuw i32 -1, %NBits`
2. Then, InstSimplify D47883/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 D47428/rL334127 fold could happen.
Thus we now end up with the opposite constant,
and it is all good: https://rise4fun.com/Alive/OA9

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

Reviewers: spatel, craig.topper

Reviewed By: spatel

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D47908

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334298 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 0b150f8..4eddde9 100644 (file)
@@ -561,6 +561,10 @@ static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
       match(Op0, m_Xor(m_Value(Y), m_SignMask())))
     return Y;
 
+  // add nuw %x, -1  ->  -1, because %x can only be 0.
+  if (isNUW && match(Op1, m_AllOnes()))
+    return Op1; // Which is -1.
+
   /// i1 add -> xor.
   if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
     if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
index b67854c..99ec450 100644 (file)
@@ -39,7 +39,7 @@ define i32 @shl_add_nsw(i32 %NBits) {
 
 define i32 @shl_add_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_add_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl i32 1, %NBits
   %ret = add nuw i32 %setbit, -1
@@ -48,7 +48,7 @@ define i32 @shl_add_nuw(i32 %NBits) {
 
 define i32 @shl_add_nsw_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_add_nsw_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl i32 1, %NBits
   %ret = add nuw nsw i32 %setbit, -1
@@ -81,7 +81,7 @@ define i32 @shl_nsw_add_nsw(i32 %NBits) {
 
 define i32 @shl_nsw_add_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nsw_add_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nsw i32 1, %NBits
   %ret = add nuw i32 %setbit, -1
@@ -90,7 +90,7 @@ define i32 @shl_nsw_add_nuw(i32 %NBits) {
 
 define i32 @shl_nsw_add_nsw_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nsw_add_nsw_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nsw i32 1, %NBits
   %ret = add nuw nsw i32 %setbit, -1
@@ -123,7 +123,7 @@ define i32 @shl_nuw_add_nsw(i32 %NBits) {
 
 define i32 @shl_nuw_add_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nuw_add_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nuw i32 1, %NBits
   %ret = add nuw i32 %setbit, -1
@@ -132,7 +132,7 @@ define i32 @shl_nuw_add_nuw(i32 %NBits) {
 
 define i32 @shl_nuw_add_nsw_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nuw_add_nsw_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nuw i32 1, %NBits
   %ret = add nuw nsw i32 %setbit, -1
@@ -165,7 +165,7 @@ define i32 @shl_nsw_nuw_add_nsw(i32 %NBits) {
 
 define i32 @shl_nsw_nuw_add_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nsw_nuw_add_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nuw nsw i32 1, %NBits
   %ret = add nuw i32 %setbit, -1
@@ -174,7 +174,7 @@ define i32 @shl_nsw_nuw_add_nuw(i32 %NBits) {
 
 define i32 @shl_nsw_nuw_add_nsw_nuw(i32 %NBits) {
 ; CHECK-LABEL: @shl_nsw_nuw_add_nsw_nuw(
-; CHECK-NEXT:    ret i32 0
+; CHECK-NEXT:    ret i32 -1
 ;
   %setbit = shl nuw nsw i32 1, %NBits
   %ret = add nuw nsw i32 %setbit, -1
index 68db8ce..53393c1 100644 (file)
@@ -7,8 +7,7 @@
 
 define i8 @add_nuw (i8 %x) {
 ; CHECK-LABEL: @add_nuw(
-; CHECK-NEXT:    [[RET:%.*]] = add nuw i8 [[X:%.*]], -1
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 -1
 ;
   %ret = add nuw i8 %x, -1
   ; nuw here means that %x can only be 0
@@ -17,8 +16,7 @@ define i8 @add_nuw (i8 %x) {
 
 define i8 @add_nuw_nsw (i8 %x) {
 ; CHECK-LABEL: @add_nuw_nsw(
-; CHECK-NEXT:    [[RET:%.*]] = add nuw nsw i8 [[X:%.*]], -1
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 -1
 ;
   %ret = add nuw nsw i8 %x, -1
   ; nuw here means that %x can only be 0
@@ -27,8 +25,7 @@ define i8 @add_nuw_nsw (i8 %x) {
 
 define i8 @add_nuw_commute (i8 %x) {
 ; CHECK-LABEL: @add_nuw_commute(
-; CHECK-NEXT:    [[RET:%.*]] = add nuw i8 -1, [[X:%.*]]
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 -1
 ;
   %ret = add nuw i8 -1, %x ; swapped
   ; nuw here means that %x can only be 0
@@ -60,8 +57,7 @@ define i8 @knownbits_allones(i8 %x, i8 %y) {
 
 define <2 x i8> @add_vec(<2 x i8> %x) {
 ; CHECK-LABEL: @add_vec(
-; CHECK-NEXT:    [[RET:%.*]] = add nuw <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
-; CHECK-NEXT:    ret <2 x i8> [[RET]]
+; CHECK-NEXT:    ret <2 x i8> <i8 -1, i8 -1>
 ;
   %ret = add nuw <2 x i8> %x, <i8 -1, i8 -1>
   ret <2 x i8> %ret
@@ -69,8 +65,7 @@ define <2 x i8> @add_vec(<2 x i8> %x) {
 
 define <3 x i8> @add_vec_undef(<3 x i8> %x) {
 ; CHECK-LABEL: @add_vec_undef(
-; CHECK-NEXT:    [[RET:%.*]] = add nuw <3 x i8> [[X:%.*]], <i8 -1, i8 undef, i8 -1>
-; CHECK-NEXT:    ret <3 x i8> [[RET]]
+; CHECK-NEXT:    ret <3 x i8> <i8 -1, i8 undef, i8 -1>
 ;
   %ret = add nuw <3 x i8> %x, <i8 -1, i8 undef, i8 -1>
   ret <3 x i8> %ret