[llvm] e82db87 - [InstCombine] drop poison flags when simplifying 'shl' based on demanded bits

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri May 14 10:54:23 PDT 2021


Author: Sanjay Patel
Date: 2021-05-14T13:54:13-04:00
New Revision: e82db87fb102f01b0895b074e56568025c659575

URL: https://github.com/llvm/llvm-project/commit/e82db87fb102f01b0895b074e56568025c659575
DIFF: https://github.com/llvm/llvm-project/commit/e82db87fb102f01b0895b074e56568025c659575.diff

LOG: [InstCombine] drop poison flags when simplifying 'shl' based on demanded bits

As with other transforms in demanded bits, we must be careful not to
wrongly propagate nsw/nuw if we are reducing values leading up to the shift.

This bug was introduced with 1b24f35f843c and leads to the miscompile
shown in:
https://llvm.org/PR50341

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
    llvm/test/Transforms/InstCombine/shl-demand.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 2b9c1f2ad3dfa..8957598925570 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -575,8 +575,11 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
       // demanding those bits from the pre-shifted operand either.
       if (unsigned CTLZ = DemandedMask.countLeadingZeros()) {
         APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ));
-        if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1))
+        if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1)) {
+          // We can't guarantee that nsw/nuw hold after simplifying the operand.
+          I->dropPoisonGeneratingFlags();
           return I;
+        }
       }
       computeKnownBits(I, Known, Depth, CxtI);
     }

diff  --git a/llvm/test/Transforms/InstCombine/shl-demand.ll b/llvm/test/Transforms/InstCombine/shl-demand.ll
index 245c703319070..11c2de8e58d75 100644
--- a/llvm/test/Transforms/InstCombine/shl-demand.ll
+++ b/llvm/test/Transforms/InstCombine/shl-demand.ll
@@ -89,7 +89,7 @@ define i32 @set_shl_mask(i32 %x, i32 %y) {
 
 define i8 @must_drop_poison(i32 %x, i32 %y)  {
 ; CHECK-LABEL: @must_drop_poison(
-; CHECK-NEXT:    [[S:%.*]] = shl nuw nsw i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[S:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[S]] to i8
 ; CHECK-NEXT:    ret i8 [[T]]
 ;


        


More information about the llvm-commits mailing list