Skip to content

Commit 3bf2295

Browse files
authored
[InstCombine] Drop samesign flag in foldAndOrOfICmpsWithConstEq (#112489)
In 5dbfca3 we assume that RHS is poison implies LHS is also poison. It doesn't hold after introducing samesign flag. This patch drops the `samesign` flag on RHS if the original expression is a logical and/or. Closes #112467.
1 parent 9df8d8d commit 3bf2295

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3369,8 +3369,14 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
33693369
// We can convert this case to bitwise and, because both operands are used
33703370
// on the LHS, and as such poison from both will propagate.
33713371
if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, IsAnd,
3372-
/*IsLogical*/ false, Builder, Q))
3372+
/*IsLogical=*/false, Builder, Q)) {
3373+
// If RHS is still used, we should drop samesign flag.
3374+
if (IsLogical && RHS->hasSameSign() && !RHS->use_empty()) {
3375+
RHS->setSameSign(false);
3376+
addToWorklist(RHS);
3377+
}
33733378
return V;
3379+
}
33743380

33753381
if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, IsAnd, Builder, *this))
33763382
return V;

llvm/test/Transforms/InstCombine/and-or-icmp-min-max.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,17 @@ define i1 @sge_and_max_logical(i8 %x, i8 %y) {
689689
ret i1 %r
690690
}
691691

692+
define i1 @sge_and_max_logical_samesign(i8 %x, i8 %y) {
693+
; CHECK-LABEL: @sge_and_max_logical_samesign(
694+
; CHECK-NEXT: [[CMPEQ:%.*]] = icmp eq i8 [[X:%.*]], 127
695+
; CHECK-NEXT: ret i1 [[CMPEQ]]
696+
;
697+
%cmp = icmp sge i8 %x, %y
698+
%cmpeq = icmp samesign eq i8 %x, 127
699+
%r = select i1 %cmp, i1 %cmpeq, i1 false
700+
ret i1 %r
701+
}
702+
692703
define i1 @sge_and_max_commute(i8 %x, i8 %y) {
693704
; CHECK-LABEL: @sge_and_max_commute(
694705
; CHECK-NEXT: [[CMPEQ:%.*]] = icmp eq i8 [[X:%.*]], 127

llvm/test/Transforms/InstCombine/and-or-icmp-nullptr.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,19 @@ define i1 @sgt_and_min_logical(ptr %x, ptr %y) {
592592
ret i1 %r
593593
}
594594

595+
define i1 @sgt_and_min_logical_samesign(ptr %x, ptr %y) {
596+
; CHECK-LABEL: @sgt_and_min_logical_samesign(
597+
; CHECK-NEXT: [[CMPEQ:%.*]] = icmp eq ptr [[X:%.*]], null
598+
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt ptr [[Y:%.*]], null
599+
; CHECK-NEXT: [[R:%.*]] = and i1 [[CMPEQ]], [[TMP1]]
600+
; CHECK-NEXT: ret i1 [[R]]
601+
;
602+
%cmp = icmp sgt ptr %x, %y
603+
%cmpeq = icmp samesign eq ptr %x, null
604+
%r = select i1 %cmp, i1 %cmpeq, i1 false
605+
ret i1 %r
606+
}
607+
595608
define i1 @sle_or_not_min(ptr %x, ptr %y) {
596609
; CHECK-LABEL: @sle_or_not_min(
597610
; CHECK-NEXT: [[CMPEQ:%.*]] = icmp ne ptr [[X:%.*]], null

0 commit comments

Comments
 (0)