Skip to content

Commit 6b7e41c

Browse files
committed
Added some checks to preserve the logic of existing code exactly.
Added more tests.
1 parent 1ad7ff9 commit 6b7e41c

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,10 +2969,11 @@ Instruction *InstCombinerImpl::foldICmpAddLikeConstant(ICmpInst &Cmp,
29692969
// If the add does not wrap, we can always adjust the compare by subtracting
29702970
// the constants. Equality comparisons are handled elsewhere. SGE/SLE/UGE/ULE
29712971
// are canonicalized to SGT/SLT/UGT/ULT.
2972-
if (AddLike->getOpcode() == Instruction::Or ||
2973-
(AddLike->hasNoSignedWrap() &&
2972+
if (((AddLike->getOpcode() == Instruction::Or ||
2973+
AddLike->hasNoSignedWrap()) &&
29742974
(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) ||
2975-
(AddLike->hasNoUnsignedWrap() &&
2975+
((AddLike->getOpcode() == Instruction::Or ||
2976+
AddLike->hasNoUnsignedWrap()) &&
29762977
(Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT))) {
29772978
bool Overflow;
29782979
APInt NewC =

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5138,12 +5138,62 @@ entry:
51385138
ret i1 %cmp
51395139
}
51405140

5141-
define i1 @icmp_disjoint_or(i32 %x) {
5142-
; CHECK-LABEL: @icmp_disjoint_or(
5141+
define i1 @icmp_disjoint_or_sgt(i32 %x) {
5142+
; CHECK-LABEL: @icmp_disjoint_or_sgt(
51435143
; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[X:%.*]], 35
51445144
; CHECK-NEXT: ret i1 [[C]]
51455145
;
51465146
%or_ = or disjoint i32 %x, 6
51475147
%C = icmp sgt i32 %or_, 41
51485148
ret i1 %C
51495149
}
5150+
5151+
define i1 @icmp_disjoint_or_slt(i32 %x) {
5152+
; CHECK-LABEL: @icmp_disjoint_or_slt(
5153+
; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[X:%.*]], 35
5154+
; CHECK-NEXT: ret i1 [[C]]
5155+
;
5156+
%or_ = or disjoint i32 %x, 6
5157+
%C = icmp slt i32 %or_, 41
5158+
ret i1 %C
5159+
}
5160+
5161+
define i1 @icmp_disjoint_or_ult(i32 %x) {
5162+
; CHECK-LABEL: @icmp_disjoint_or_ult(
5163+
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X:%.*]], 35
5164+
; CHECK-NEXT: ret i1 [[C]]
5165+
;
5166+
%or_ = or disjoint i32 %x, 6
5167+
%C = icmp ult i32 %or_, 41
5168+
ret i1 %C
5169+
}
5170+
5171+
define i1 @icmp_disjoint_or_ugt(i32 %x) {
5172+
; CHECK-LABEL: @icmp_disjoint_or_ugt(
5173+
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X:%.*]], 35
5174+
; CHECK-NEXT: ret i1 [[C]]
5175+
;
5176+
%or_ = or disjoint i32 %x, 6
5177+
%C = icmp ugt i32 %or_, 41
5178+
ret i1 %C
5179+
}
5180+
5181+
define i1 @icmp_disjoint_or_eq(i32 %x) {
5182+
; CHECK-LABEL: @icmp_disjoint_or_eq(
5183+
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
5184+
; CHECK-NEXT: ret i1 [[C]]
5185+
;
5186+
%or_ = or disjoint i32 %x, 5
5187+
%C = icmp eq i32 %or_, 5
5188+
ret i1 %C
5189+
}
5190+
5191+
define i1 @icmp_disjoint_or_be(i32 %x) {
5192+
; CHECK-LABEL: @icmp_disjoint_or_be(
5193+
; CHECK-NEXT: [[C:%.*]] = icmp ne i32 [[X:%.*]], 0
5194+
; CHECK-NEXT: ret i1 [[C]]
5195+
;
5196+
%or_ = or disjoint i32 %x, 5
5197+
%C = icmp ne i32 %or_, 5
5198+
ret i1 %C
5199+
}

0 commit comments

Comments
 (0)