Skip to content

[SCCP] Propagate non-null pointers #106090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2024
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
34 changes: 33 additions & 1 deletion llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
return markConstant(ValueState[V], V, C);
}

bool markNotConstant(ValueLatticeElement &IV, Value *V, Constant *C);

bool markNotNull(ValueLatticeElement &IV, Value *V) {
return markNotConstant(IV, V, Constant::getNullValue(V->getType()));
}

/// markConstantRange - Mark the object as constant range with \p CR. If the
/// object is not a constant range with the range \p CR, add it to the
/// instruction work list so that the users of the instruction are updated
Expand Down Expand Up @@ -820,7 +826,11 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
return;
}
}
// Assume nothing about the incoming arguments without range.
if (A->hasNonNullAttr()) {
markNotNull(ValueState[A], A);
return;
}
// Assume nothing about the incoming arguments without attributes.
markOverdefined(A);
}

Expand Down Expand Up @@ -905,6 +915,15 @@ bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V,
return true;
}

bool SCCPInstVisitor::markNotConstant(ValueLatticeElement &IV, Value *V,
Constant *C) {
if (!IV.markNotConstant(C))
return false;
LLVM_DEBUG(dbgs() << "markNotConstant: " << *C << ": " << *V << '\n');
pushToWorkList(IV, V);
return true;
}

bool SCCPInstVisitor::markConstantRange(ValueLatticeElement &IV, Value *V,
const ConstantRange &CR) {
if (!IV.markConstantRange(CR))
Expand Down Expand Up @@ -1574,6 +1593,19 @@ void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
if (ValueState[&I].isOverdefined())
return (void)markOverdefined(&I);

const ValueLatticeElement &PtrState = getValueState(I.getPointerOperand());
if (PtrState.isUnknownOrUndef())
return;

// gep inbounds/nuw of non-null is non-null.
if (PtrState.isNotConstant() && PtrState.getNotConstant()->isNullValue()) {
if (I.hasNoUnsignedWrap() ||
(I.isInBounds() &&
!NullPointerIsDefined(I.getFunction(), I.getAddressSpace())))
return (void)markNotNull(ValueState[&I], &I);
return (void)markOverdefined(&I);
}

SmallVector<Constant *, 8> Operands;
Operands.reserve(I.getNumOperands());

Expand Down
21 changes: 7 additions & 14 deletions llvm/test/Transforms/SCCP/pointer-nonnull.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ define i1 @test_no_attr(ptr %p) {
define i1 @test_nonnull(ptr nonnull %p) {
; CHECK-LABEL: define i1 @test_nonnull(
; CHECK-SAME: ptr nonnull [[P:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%cmp = icmp ne ptr %p, null
ret i1 %cmp
Expand All @@ -24,8 +23,7 @@ define i1 @test_nonnull(ptr nonnull %p) {
define i1 @test_nonnull_eq(ptr nonnull %p) {
; CHECK-LABEL: define i1 @test_nonnull_eq(
; CHECK-SAME: ptr nonnull [[P:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[P]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 false
;
%cmp = icmp eq ptr %p, null
ret i1 %cmp
Expand All @@ -34,8 +32,7 @@ define i1 @test_nonnull_eq(ptr nonnull %p) {
define i1 @test_dereferenceable(ptr dereferenceable(4) %p) {
; CHECK-LABEL: define i1 @test_dereferenceable(
; CHECK-SAME: ptr dereferenceable(4) [[P:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%cmp = icmp ne ptr %p, null
ret i1 %cmp
Expand All @@ -57,8 +54,7 @@ define i1 @test_gep_nuw(ptr nonnull %p, i64 %x) {
; CHECK-LABEL: define i1 @test_gep_nuw(
; CHECK-SAME: ptr nonnull [[P:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[X]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[GEP]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%gep = getelementptr nuw i8, ptr %p, i64 %x
%cmp = icmp ne ptr %gep, null
Expand All @@ -69,8 +65,7 @@ define i1 @test_gep_inbounds(ptr nonnull %p, i64 %x) {
; CHECK-LABEL: define i1 @test_gep_inbounds(
; CHECK-SAME: ptr nonnull [[P:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 [[X]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[GEP]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%gep = getelementptr inbounds i8, ptr %p, i64 %x
%cmp = icmp ne ptr %gep, null
Expand All @@ -94,8 +89,7 @@ define i1 @test_select(i1 %c, ptr nonnull %p, i64 %x) {
; CHECK-SAME: i1 [[C:%.*]], ptr nonnull [[P:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[X]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C]], ptr [[P]], ptr [[GEP]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[SEL]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%gep = getelementptr nuw i8, ptr %p, i64 %x
%sel = select i1 %c, ptr %p, ptr %gep
Expand Down Expand Up @@ -127,8 +121,7 @@ define i1 @test_phi(i1 %c, ptr nonnull %p, i64 %x) {
; CHECK-NEXT: br label %[[JOIN]]
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[P]], %[[ENTRY]] ], [ [[GEP]], %[[IF]] ]
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[PHI]], null
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
entry:
br i1 %c, label %if, label %join
Expand Down
Loading