Skip to content

Commit c18e121

Browse files
authored
[InstCombine] Simplify zext nneg i1 X to zero (#85043)
Alive2: https://alive2.llvm.org/ce/z/Wm6kCk
1 parent ceb744e commit c18e121

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,10 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
11211121
Value *Src = Zext.getOperand(0);
11221122
Type *SrcTy = Src->getType(), *DestTy = Zext.getType();
11231123

1124+
// zext nneg bool x -> 0
1125+
if (SrcTy->isIntOrIntVectorTy(1) && Zext.hasNonNeg())
1126+
return replaceInstUsesWith(Zext, Constant::getNullValue(Zext.getType()));
1127+
11241128
// Try to extend the entire expression tree to the wide destination type.
11251129
unsigned BitsToClear;
11261130
if (shouldChangeType(SrcTy, DestTy) &&

llvm/test/Transforms/InstCombine/zext.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,3 +836,34 @@ define i64 @zext_nneg_demanded_constant(i8 %a) nounwind {
836836
%c = and i64 %b, 254
837837
ret i64 %c
838838
}
839+
840+
define i32 @zext_nneg_i1(i1 %x) {
841+
; CHECK-LABEL: @zext_nneg_i1(
842+
; CHECK-NEXT: entry:
843+
; CHECK-NEXT: ret i32 0
844+
;
845+
entry:
846+
%res = zext nneg i1 %x to i32
847+
ret i32 %res
848+
}
849+
850+
define <2 x i32> @zext_nneg_i1_vec(<2 x i1> %x) {
851+
; CHECK-LABEL: @zext_nneg_i1_vec(
852+
; CHECK-NEXT: entry:
853+
; CHECK-NEXT: ret <2 x i32> zeroinitializer
854+
;
855+
entry:
856+
%res = zext nneg <2 x i1> %x to <2 x i32>
857+
ret <2 x i32> %res
858+
}
859+
860+
define i32 @zext_nneg_i2(i2 %x) {
861+
; CHECK-LABEL: @zext_nneg_i2(
862+
; CHECK-NEXT: entry:
863+
; CHECK-NEXT: [[RES:%.*]] = zext nneg i2 [[X:%.*]] to i32
864+
; CHECK-NEXT: ret i32 [[RES]]
865+
;
866+
entry:
867+
%res = zext nneg i2 %x to i32
868+
ret i32 %res
869+
}

0 commit comments

Comments
 (0)