Skip to content

Commit 29f3a35

Browse files
authored
[InstCombine] Do not keep samesign when speculatively executing icmps (#127007)
Closes #126974.
1 parent dab9156 commit 29f3a35

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5637,6 +5637,11 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
56375637
return false;
56385638
return std::nullopt;
56395639
};
5640+
// Remove samesign here since it is illegal to keep it when we speculatively
5641+
// execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
5642+
// cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
5643+
// -46, -32)`. `X` is allowed to be non-negative here.
5644+
Pred = static_cast<CmpInst::Predicate>(Pred);
56405645
auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
56415646
auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
56425647
if (!CmpXZ.has_value() && !CmpYZ.has_value())

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,4 +804,28 @@ end:
804804
ret void
805805
}
806806

807+
define i1 @pr126974(i8 %x) {
808+
; CHECK-LABEL: @pr126974(
809+
; CHECK-NEXT: entry:
810+
; CHECK-NEXT: [[COND:%.*]] = icmp sgt i8 [[X:%.*]], -2
811+
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
812+
; CHECK: if.then:
813+
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X]], -1
814+
; CHECK-NEXT: ret i1 [[CMP]]
815+
; CHECK: if.else:
816+
; CHECK-NEXT: ret i1 false
817+
;
818+
entry:
819+
%cond = icmp sgt i8 %x, -2
820+
br i1 %cond, label %if.then, label %if.else
821+
822+
if.then:
823+
%umax = call i8 @llvm.umax.i8(i8 %x, i8 -46)
824+
%cmp = icmp samesign ult i8 %umax, -32
825+
ret i1 %cmp
826+
827+
if.else:
828+
ret i1 false
829+
}
830+
807831
declare i32 @llvm.umax.i32(i32, i32)

0 commit comments

Comments
 (0)