[llvm] r334298 - [InstSimplify] add nuw %x, -1 -> -1 fold.

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 8 08:44:47 PDT 2018


Author: lebedevri
Date: Fri Jun  8 08:44:47 2018
New Revision: 334298

URL: http://llvm.org/viewvc/llvm-project?rev=334298&view=rev
Log:
[InstSimplify] add nuw %x, -1 -> -1 fold.

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

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

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=334298&r1=334297&r2=334298&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Fri Jun  8 08:44:47 2018
@@ -561,6 +561,10 @@ static Value *SimplifyAddInst(Value *Op0
       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))

Modified: llvm/trunk/test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll?rev=334298&r1=334297&r2=334298&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/set-lowbits-mask-canonicalize.ll Fri Jun  8 08:44:47 2018
@@ -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 %NBi
 
 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 %NBi
 
 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

Modified: llvm/trunk/test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll?rev=334298&r1=334297&r2=334298&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/constantfold-add-nuw-allones-to-allones.ll Fri Jun  8 08:44:47 2018
@@ -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 %
 
 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




More information about the llvm-commits mailing list