Skip to content

Commit 23f1047

Browse files
committed
[InstCombine] Fold (icmp pred (trunc nuw/nsw X), C) -> (icmp pred X, (zext/sext C))
This is valid as long as the sign of the wrap flag doesn't differ from the sign of the `pred`. Proofs: https://alive2.llvm.org/ce/z/35NsrR NB: The online Alive2 hasn't been updated with `trunc nuw/nsw` support, so the proofs must be reproduced locally. Closes #87935
1 parent ebbf484 commit 23f1047

File tree

2 files changed

+91
-44
lines changed

2 files changed

+91
-44
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,19 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
14091409
const APInt &C) {
14101410
ICmpInst::Predicate Pred = Cmp.getPredicate();
14111411
Value *X = Trunc->getOperand(0);
1412+
Type *SrcTy = X->getType();
1413+
unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1414+
SrcBits = SrcTy->getScalarSizeInBits();
1415+
1416+
// Match (icmp pred (trunc nuw/nsw X), C)
1417+
// Which we can convert to (icmp pred X, (sext/zext C))
1418+
if (shouldChangeType(Trunc->getType(), SrcTy)) {
1419+
if (Trunc->hasNoSignedWrap())
1420+
return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
1421+
if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
1422+
return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
1423+
}
1424+
14121425
if (C.isOne() && C.getBitWidth() > 1) {
14131426
// icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
14141427
Value *V = nullptr;
@@ -1417,10 +1430,6 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
14171430
ConstantInt::get(V->getType(), 1));
14181431
}
14191432

1420-
Type *SrcTy = X->getType();
1421-
unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1422-
SrcBits = SrcTy->getScalarSizeInBits();
1423-
14241433
// TODO: Handle any shifted constant by subtracting trailing zeros.
14251434
// TODO: Handle non-equality predicates.
14261435
Value *Y;

llvm/test/Transforms/InstCombine/icmp-trunc.ll

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ define i1 @shl1_trunc_sgt4(i32 %a) {
558558

559559
define i1 @eq_nuw(i32 %x) {
560560
; DL64-LABEL: @eq_nuw(
561-
; DL64-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 255
562-
; DL64-NEXT: [[R:%.*]] = icmp eq i32 [[TMP1]], 123
561+
; DL64-NEXT: [[R:%.*]] = icmp eq i32 [[X:%.*]], 123
563562
; DL64-NEXT: ret i1 [[R]]
564563
;
565564
; DL8-LABEL: @eq_nuw(
@@ -573,22 +572,32 @@ define i1 @eq_nuw(i32 %x) {
573572
}
574573

575574
define i1 @ult_nuw(i32 %x) {
576-
; CHECK-LABEL: @ult_nuw(
577-
; CHECK-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
578-
; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[T]], 45
579-
; CHECK-NEXT: ret i1 [[R]]
575+
; DL64-LABEL: @ult_nuw(
576+
; DL64-NEXT: [[R:%.*]] = icmp ult i32 [[X:%.*]], 45
577+
; DL64-NEXT: ret i1 [[R]]
578+
;
579+
; DL8-LABEL: @ult_nuw(
580+
; DL8-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
581+
; DL8-NEXT: [[R:%.*]] = icmp ult i8 [[T]], 45
582+
; DL8-NEXT: ret i1 [[R]]
580583
;
581584
%t = trunc nuw i32 %x to i8
582585
%r = icmp ult i8 %t, 45
583586
ret i1 %r
584587
}
585588

586589
define i1 @ule_nuw(i32 %x) {
587-
; CHECK-LABEL: @ule_nuw(
588-
; CHECK-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
589-
; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[T]], 46
590-
; CHECK-NEXT: call void @use(i8 [[T]])
591-
; CHECK-NEXT: ret i1 [[R]]
590+
; DL64-LABEL: @ule_nuw(
591+
; DL64-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
592+
; DL64-NEXT: [[R:%.*]] = icmp ult i32 [[X]], 46
593+
; DL64-NEXT: call void @use(i8 [[T]])
594+
; DL64-NEXT: ret i1 [[R]]
595+
;
596+
; DL8-LABEL: @ule_nuw(
597+
; DL8-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
598+
; DL8-NEXT: [[R:%.*]] = icmp ult i8 [[T]], 46
599+
; DL8-NEXT: call void @use(i8 [[T]])
600+
; DL8-NEXT: ret i1 [[R]]
592601
;
593602
%t = trunc nuw i32 %x to i8
594603
%r = icmp ule i8 %t, 45
@@ -597,22 +606,32 @@ define i1 @ule_nuw(i32 %x) {
597606
}
598607

599608
define i1 @ugt_nuw(i32 %x) {
600-
; CHECK-LABEL: @ugt_nuw(
601-
; CHECK-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
602-
; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[T]], 12
603-
; CHECK-NEXT: ret i1 [[R]]
609+
; DL64-LABEL: @ugt_nuw(
610+
; DL64-NEXT: [[R:%.*]] = icmp ugt i32 [[X:%.*]], 12
611+
; DL64-NEXT: ret i1 [[R]]
612+
;
613+
; DL8-LABEL: @ugt_nuw(
614+
; DL8-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
615+
; DL8-NEXT: [[R:%.*]] = icmp ugt i8 [[T]], 12
616+
; DL8-NEXT: ret i1 [[R]]
604617
;
605618
%t = trunc nuw i32 %x to i8
606619
%r = icmp ugt i8 %t, 12
607620
ret i1 %r
608621
}
609622

610623
define i1 @uge_nuw(i32 %x) {
611-
; CHECK-LABEL: @uge_nuw(
612-
; CHECK-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
613-
; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[T]], 98
614-
; CHECK-NEXT: call void @use(i8 [[T]])
615-
; CHECK-NEXT: ret i1 [[R]]
624+
; DL64-LABEL: @uge_nuw(
625+
; DL64-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
626+
; DL64-NEXT: [[R:%.*]] = icmp ugt i32 [[X]], 98
627+
; DL64-NEXT: call void @use(i8 [[T]])
628+
; DL64-NEXT: ret i1 [[R]]
629+
;
630+
; DL8-LABEL: @uge_nuw(
631+
; DL8-NEXT: [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
632+
; DL8-NEXT: [[R:%.*]] = icmp ugt i8 [[T]], 98
633+
; DL8-NEXT: call void @use(i8 [[T]])
634+
; DL8-NEXT: ret i1 [[R]]
616635
;
617636
%t = trunc nuw i32 %x to i8
618637
%r = icmp uge i8 %t, 99
@@ -646,8 +665,7 @@ define i1 @sgt_nuw_fail(i32 %x) {
646665

647666
define i1 @ne_nsw(i32 %x) {
648667
; DL64-LABEL: @ne_nsw(
649-
; DL64-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 255
650-
; DL64-NEXT: [[R:%.*]] = icmp ne i32 [[TMP1]], 133
668+
; DL64-NEXT: [[R:%.*]] = icmp ne i32 [[X:%.*]], -123
651669
; DL64-NEXT: ret i1 [[R]]
652670
;
653671
; DL8-LABEL: @ne_nsw(
@@ -661,22 +679,32 @@ define i1 @ne_nsw(i32 %x) {
661679
}
662680

663681
define i1 @slt_nsw(i32 %x) {
664-
; CHECK-LABEL: @slt_nsw(
665-
; CHECK-NEXT: [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
666-
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[T]], 45
667-
; CHECK-NEXT: ret i1 [[R]]
682+
; DL64-LABEL: @slt_nsw(
683+
; DL64-NEXT: [[R:%.*]] = icmp slt i32 [[X:%.*]], 45
684+
; DL64-NEXT: ret i1 [[R]]
685+
;
686+
; DL8-LABEL: @slt_nsw(
687+
; DL8-NEXT: [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
688+
; DL8-NEXT: [[R:%.*]] = icmp slt i8 [[T]], 45
689+
; DL8-NEXT: ret i1 [[R]]
668690
;
669691
%t = trunc nsw i32 %x to i8
670692
%r = icmp slt i8 %t, 45
671693
ret i1 %r
672694
}
673695

674696
define i1 @sle_nsw(i16 %x) {
675-
; CHECK-LABEL: @sle_nsw(
676-
; CHECK-NEXT: [[T:%.*]] = trunc nsw i16 [[X:%.*]] to i8
677-
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[T]], 46
678-
; CHECK-NEXT: call void @use(i8 [[T]])
679-
; CHECK-NEXT: ret i1 [[R]]
697+
; DL64-LABEL: @sle_nsw(
698+
; DL64-NEXT: [[T:%.*]] = trunc nsw i16 [[X:%.*]] to i8
699+
; DL64-NEXT: [[R:%.*]] = icmp slt i16 [[X]], 46
700+
; DL64-NEXT: call void @use(i8 [[T]])
701+
; DL64-NEXT: ret i1 [[R]]
702+
;
703+
; DL8-LABEL: @sle_nsw(
704+
; DL8-NEXT: [[T:%.*]] = trunc nsw i16 [[X:%.*]] to i8
705+
; DL8-NEXT: [[R:%.*]] = icmp slt i8 [[T]], 46
706+
; DL8-NEXT: call void @use(i8 [[T]])
707+
; DL8-NEXT: ret i1 [[R]]
680708
;
681709
%t = trunc nsw i16 %x to i8
682710
%r = icmp sle i8 %t, 45
@@ -685,22 +713,32 @@ define i1 @sle_nsw(i16 %x) {
685713
}
686714

687715
define i1 @sgt_nsw(i32 %x) {
688-
; CHECK-LABEL: @sgt_nsw(
689-
; CHECK-NEXT: [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
690-
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[T]], -12
691-
; CHECK-NEXT: ret i1 [[R]]
716+
; DL64-LABEL: @sgt_nsw(
717+
; DL64-NEXT: [[R:%.*]] = icmp sgt i32 [[X:%.*]], -12
718+
; DL64-NEXT: ret i1 [[R]]
719+
;
720+
; DL8-LABEL: @sgt_nsw(
721+
; DL8-NEXT: [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
722+
; DL8-NEXT: [[R:%.*]] = icmp sgt i8 [[T]], -12
723+
; DL8-NEXT: ret i1 [[R]]
692724
;
693725
%t = trunc nsw i32 %x to i8
694726
%r = icmp sgt i8 %t, -12
695727
ret i1 %r
696728
}
697729

698730
define i1 @sge_nsw(i64 %x) {
699-
; CHECK-LABEL: @sge_nsw(
700-
; CHECK-NEXT: [[T:%.*]] = trunc nsw i64 [[X:%.*]] to i8
701-
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[T]], 98
702-
; CHECK-NEXT: call void @use(i8 [[T]])
703-
; CHECK-NEXT: ret i1 [[R]]
731+
; DL64-LABEL: @sge_nsw(
732+
; DL64-NEXT: [[T:%.*]] = trunc nsw i64 [[X:%.*]] to i8
733+
; DL64-NEXT: [[R:%.*]] = icmp sgt i64 [[X]], 98
734+
; DL64-NEXT: call void @use(i8 [[T]])
735+
; DL64-NEXT: ret i1 [[R]]
736+
;
737+
; DL8-LABEL: @sge_nsw(
738+
; DL8-NEXT: [[T:%.*]] = trunc nsw i64 [[X:%.*]] to i8
739+
; DL8-NEXT: [[R:%.*]] = icmp sgt i8 [[T]], 98
740+
; DL8-NEXT: call void @use(i8 [[T]])
741+
; DL8-NEXT: ret i1 [[R]]
704742
;
705743
%t = trunc nsw i64 %x to i8
706744
%r = icmp sge i8 %t, 99

0 commit comments

Comments
 (0)