Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,12 @@ LazyValueInfoImpl::solveBlockValueImpl(Value *Val, BasicBlock *BB) {
return getFromRangeMetadata(BBI);
}

static void AddNonNullPointer(Value *Ptr, NonNullPointerSet &PtrSet) {
static void AddNonNullPointer(Value *Ptr, NonNullPointerSet &PtrSet,
bool IsDereferenced = true) {
// TODO: Use NullPointerIsDefined instead.
if (Ptr->getType()->getPointerAddressSpace() == 0)
PtrSet.insert(getUnderlyingObject(Ptr));
PtrSet.insert(IsDereferenced ? getUnderlyingObject(Ptr)
: Ptr->stripInBoundsOffsets());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a followup, it may be worth checking whether always using stripInBoundsOffset() has a positive effect on llvm-opt-benchmark. The lookup always uses stripInBoundsOffset(), so currently there is mismatch between getUnderlyingObject on one side an stripInBoundsOffset on the other and it may give better results to always use only stripInBoundsOffset.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worse than getUnderlyingObject.

}

static void AddNonNullPointersByInstruction(
Expand All @@ -644,6 +646,13 @@ static void AddNonNullPointersByInstruction(
AddNonNullPointer(MI->getRawDest(), PtrSet);
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
AddNonNullPointer(MTI->getRawSource(), PtrSet);
} else if (auto *CB = dyn_cast<CallBase>(I)) {
for (auto &U : CB->args()) {
if (U->getType()->isPointerTy() &&
CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
/*AllowUndefOrPoison=*/false))
AddNonNullPointer(U.get(), PtrSet, /*IsDereferenced=*/false);
}
}
}

Expand Down
101 changes: 101 additions & 0 deletions llvm/test/Transforms/CorrelatedValuePropagation/non-null.ll
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,105 @@ define i1 @test_store_same_block(ptr %arg) {
ret i1 %cmp
}


define i1 @test_known_nonnull_at_callsite(ptr %src) {
; CHECK-LABEL: @test_known_nonnull_at_callsite(
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @callee(ptr noundef nonnull [[SRC:%.*]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 false
;
entry:
call void @callee(ptr noundef nonnull %src)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

define i1 @test_known_nonnull_mixed(ptr %src) {
; CHECK-LABEL: @test_known_nonnull_mixed(
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @callee2(ptr nonnull [[SRC:%.*]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 false
;
entry:
call void @callee2(ptr nonnull %src)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

define i1 @test_known_nonnull_at_callsite_dereferenceable(ptr %src) {
; CHECK-LABEL: @test_known_nonnull_at_callsite_dereferenceable(
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @callee(ptr dereferenceable(1) [[SRC:%.*]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 false
;
entry:
call void @callee(ptr dereferenceable(1) %src)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

define i1 @test_known_nonnull_at_callsite_gep_inbounds(ptr %src, i64 %x) {
; CHECK-LABEL: @test_known_nonnull_at_callsite_gep_inbounds(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[SRC:%.*]], i64 [[X:%.*]]
; CHECK-NEXT: call void @callee(ptr noundef nonnull [[GEP]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 false
;
entry:
%gep = getelementptr inbounds i8, ptr %src, i64 %x
call void @callee(ptr noundef nonnull %gep)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

; Negative tests

define i1 @test_known_nonnull_at_callsite_without_noundef(ptr %src) {
; CHECK-LABEL: @test_known_nonnull_at_callsite_without_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @callee(ptr nonnull [[SRC:%.*]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 [[NONNULL]]
;
entry:
call void @callee(ptr nonnull %src)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

define i1 @test_known_nonnull_at_callsite_dereferenceable_null_is_defined(ptr %src) #0 {
; CHECK-LABEL: @test_known_nonnull_at_callsite_dereferenceable_null_is_defined(
; CHECK-NEXT: entry:
; CHECK-NEXT: call void @callee(ptr dereferenceable(1) [[SRC:%.*]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 [[NONNULL]]
;
entry:
call void @callee(ptr dereferenceable(1) %src)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

define i1 @test_known_nonnull_at_callsite_gep_without_inbounds(ptr %src, i64 %x) {
; CHECK-LABEL: @test_known_nonnull_at_callsite_gep_without_inbounds(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[SRC:%.*]], i64 [[X:%.*]]
; CHECK-NEXT: call void @callee(ptr noundef nonnull [[GEP]])
; CHECK-NEXT: [[NONNULL:%.*]] = icmp eq ptr [[SRC]], null
; CHECK-NEXT: ret i1 [[NONNULL]]
;
entry:
%gep = getelementptr i8, ptr %src, i64 %x
call void @callee(ptr noundef nonnull %gep)
%nonnull = icmp eq ptr %src, null
ret i1 %nonnull
}

declare void @callee(ptr)
declare void @callee2(ptr noundef)

attributes #0 = { null_pointer_is_valid }