Skip to content

Commit

Permalink
[InstCombine] drop poison flags when simplifying 'shl' based on deman…
Browse files Browse the repository at this point in the history
…ded 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 1b24f35 and leads to the miscompile
shown in:
https://llvm.org/PR50341
  • Loading branch information
rotateright committed May 14, 2021
1 parent 339d0c1 commit e82db87
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/shl-demand.ll
Expand Up @@ -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]]
;
Expand Down

0 comments on commit e82db87

Please sign in to comment.