Skip to content

Commit

Permalink
[InstCombine] Fix incorrect nneg inference on shift amount
Browse files Browse the repository at this point in the history
Whether this is valid depends on the bit widths of the involved
integers.

Fixes #72927.
  • Loading branch information
nikic committed Nov 21, 2023
1 parent a1652fd commit ac75171
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,9 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {

if (!Zext.hasNonNeg()) {
// If this zero extend is only used by a shift, add nneg flag.
if (Zext.hasOneUse() && SrcTy->getScalarSizeInBits() > 2 &&
if (Zext.hasOneUse() &&
SrcTy->getScalarSizeInBits() >
Log2_64_Ceil(DestTy->getScalarSizeInBits()) &&
match(Zext.user_back(), m_Shift(m_Value(), m_Specific(&Zext)))) {
Zext.setNonNeg();
return &Zext;
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/InstCombine/shift.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2208,8 +2208,8 @@ define i128 @shift_zext_nneg(i8 %arg) {

define i129 @shift_zext_not_nneg(i8 %arg) {
; CHECK-LABEL: @shift_zext_not_nneg(
; CHECK-NEXT: [[EXT:%.*]] = zext nneg i8 [[ARG:%.*]] to i129
; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i129 1, [[EXT]]
; CHECK-NEXT: [[EXT:%.*]] = zext i8 [[ARG:%.*]] to i129
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i129 1, [[EXT]]
; CHECK-NEXT: ret i129 [[SHL]]
;
%ext = zext i8 %arg to i129
Expand Down

0 comments on commit ac75171

Please sign in to comment.