Skip to content

Commit c3bacf0

Browse files
committed
[LVI] Handle nonnull at callsite
1 parent bcc2746 commit c3bacf0

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,12 +780,25 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
780780
}
781781

782782
if (BBLV.isOverdefined()) {
783-
// Check whether we're checking at the terminator, and the pointer has
784-
// been dereferenced in this block.
785-
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
786-
if (PTy && BB->getTerminator() == BBI &&
787-
isNonNullAtEndOfBlock(Val, BB))
788-
BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
783+
if (PointerType *PTy = dyn_cast<PointerType>(Val->getType())) {
784+
// Check whether we're checking at the terminator, and the pointer has
785+
// been dereferenced in this block.
786+
if (BB->getTerminator() == BBI && isNonNullAtEndOfBlock(Val, BB))
787+
BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
788+
else {
789+
for (Use &U : Val->uses()) {
790+
if (auto *CB = dyn_cast<CallBase>(U.getUser())) {
791+
if (CB->isArgOperand(&U) &&
792+
CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
793+
/*AllowUndefOrPoison=*/false) &&
794+
isValidAssumeForContext(CB, BBI)) {
795+
BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
796+
break;
797+
}
798+
}
799+
}
800+
}
801+
}
789802
}
790803
}
791804

llvm/test/Transforms/CorrelatedValuePropagation/non-null.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ define i1 @test_known_nonnull_at_callsite(ptr %src) {
349349
; CHECK-LABEL: @test_known_nonnull_at_callsite(
350350
; CHECK-NEXT: entry:
351351
; CHECK-NEXT: call void @callee(ptr noundef nonnull [[SRC:%.*]])
352-
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
353-
; CHECK-NEXT: ret i1 [[NONNULL]]
352+
; CHECK-NEXT: ret i1 false
354353
;
355354
entry:
356355
call void @callee(ptr noundef nonnull %src)
@@ -362,8 +361,7 @@ define i1 @test_known_nonnull_mixed(ptr %src) {
362361
; CHECK-LABEL: @test_known_nonnull_mixed(
363362
; CHECK-NEXT: entry:
364363
; CHECK-NEXT: call void @callee2(ptr nonnull [[SRC:%.*]])
365-
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
366-
; CHECK-NEXT: ret i1 [[NONNULL]]
364+
; CHECK-NEXT: ret i1 false
367365
;
368366
entry:
369367
call void @callee2(ptr nonnull %src)
@@ -375,8 +373,7 @@ define i1 @test_known_nonnull_at_callsite_dereferenceable(ptr %src) {
375373
; CHECK-LABEL: @test_known_nonnull_at_callsite_dereferenceable(
376374
; CHECK-NEXT: entry:
377375
; CHECK-NEXT: call void @callee(ptr dereferenceable(1) [[SRC:%.*]])
378-
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
379-
; CHECK-NEXT: ret i1 [[NONNULL]]
376+
; CHECK-NEXT: ret i1 false
380377
;
381378
entry:
382379
call void @callee(ptr dereferenceable(1) %src)

0 commit comments

Comments
 (0)