Skip to content

Commit 3e6f8bd

Browse files
committed
Explicitly handle pointer comparisons
These don't get handled by the m_APInt code.
1 parent e3633bf commit 3e6f8bd

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,28 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
629629
static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
630630
KnownBits &Known, unsigned Depth,
631631
const SimplifyQuery &Q) {
632+
if (Cmp->getOperand(1)->getType()->isPointerTy()) {
633+
// Handle comparison of pointer to null explicitly, as it will not be
634+
// covered by the m_APInt() logic below.
635+
if (match(Cmp->getOperand(1), m_Zero())) {
636+
switch (Cmp->getPredicate()) {
637+
case ICmpInst::ICMP_EQ:
638+
Known.setAllZero();
639+
break;
640+
case ICmpInst::ICMP_SGE:
641+
case ICmpInst::ICMP_SGT:
642+
Known.makeNonNegative();
643+
break;
644+
case ICmpInst::ICMP_SLT:
645+
Known.makeNegative();
646+
break;
647+
default:
648+
break;
649+
}
650+
}
651+
return;
652+
}
653+
632654
unsigned BitWidth = Known.getBitWidth();
633655
auto m_V =
634656
m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V)));

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,10 +4016,9 @@ define i32 @abs_preserve(i32 %x) {
40164016
declare void @llvm.assume(i1)
40174017
define i1 @PR35794(ptr %a) {
40184018
; CHECK-LABEL: @PR35794(
4019-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt ptr [[A:%.*]], inttoptr (i64 -1 to ptr)
4020-
; CHECK-NEXT: [[MASKCOND:%.*]] = icmp eq ptr [[A]], null
4019+
; CHECK-NEXT: [[MASKCOND:%.*]] = icmp eq ptr [[A:%.*]], null
40214020
; CHECK-NEXT: tail call void @llvm.assume(i1 [[MASKCOND]])
4022-
; CHECK-NEXT: ret i1 [[CMP]]
4021+
; CHECK-NEXT: ret i1 true
40234022
;
40244023
%cmp = icmp sgt ptr %a, inttoptr (i64 -1 to ptr)
40254024
%maskcond = icmp eq ptr %a, null

0 commit comments

Comments
 (0)