Skip to content

Commit d6ad551

Browse files
authored
[IPSCCP] Infer nonnull return attribute (#106553)
Similarly to the existing range attribute inference, also infer the nonnull attribute on function return values. I think in practice FunctionAttrs will handle nearly all cases, the main one I think it doesn't is cases involving branch conditions. But as we already have the information here, we may as well materialize it.
1 parent 89e6a28 commit d6ad551

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

llvm/lib/Transforms/IPO/SCCP.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ static bool runIPSCCP(
296296
F->addRangeRetAttr(CR);
297297
continue;
298298
}
299+
// Infer nonnull return attribute.
300+
if (F->getReturnType()->isPointerTy() && ReturnValue.isNotConstant() &&
301+
ReturnValue.getNotConstant()->isNullValue() &&
302+
!F->hasRetAttribute(Attribute::NonNull)) {
303+
F->addRetAttr(Attribute::NonNull);
304+
continue;
305+
}
299306
if (F->getReturnType()->isVoidTy())
300307
continue;
301308
if (SCCPSolver::isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())

llvm/test/Transforms/PGOProfile/memprof.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16
8181
target triple = "x86_64-unknown-linux-gnu"
8282

8383
; Function Attrs: mustprogress noinline optnone uwtable
84-
; ALL-LABEL: define dso_local noundef ptr @_Z3foov()
84+
; ALL-LABEL: define dso_local noundef{{.*}}ptr @_Z3foov()
8585
; There should be some PGO metadata
8686
; PGO: !prof
8787
define dso_local noundef ptr @_Z3foov() #0 !dbg !10 {
@@ -96,7 +96,7 @@ entry:
9696
declare noundef nonnull ptr @_Znam(i64 noundef) #1
9797

9898
; Function Attrs: mustprogress noinline optnone uwtable
99-
; ALL-LABEL: define dso_local noundef ptr @_Z4foo2v()
99+
; ALL-LABEL: define dso_local noundef{{.*}}ptr @_Z4foo2v()
100100
define dso_local noundef ptr @_Z4foo2v() #0 !dbg !15 {
101101
entry:
102102
; MEMPROF: call {{.*}} @_Z3foov{{.*}} !callsite ![[C2:[0-9]+]]

llvm/test/Transforms/SCCP/pointer-nonnull.ll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ define i1 @ip_test_nonnull_caller(ptr %p) {
232232
}
233233

234234
define ptr @ret_nonnull_pointer(ptr nonnull %p) {
235-
; CHECK-LABEL: define ptr @ret_nonnull_pointer(
236-
; CHECK-SAME: ptr nonnull [[P:%.*]]) {
237-
; CHECK-NEXT: ret ptr [[P]]
235+
; SCCP-LABEL: define ptr @ret_nonnull_pointer(
236+
; SCCP-SAME: ptr nonnull [[P:%.*]]) {
237+
; SCCP-NEXT: ret ptr [[P]]
238+
;
239+
; IPSCCP-LABEL: define nonnull ptr @ret_nonnull_pointer(
240+
; IPSCCP-SAME: ptr nonnull [[P:%.*]]) {
241+
; IPSCCP-NEXT: ret ptr [[P]]
238242
;
239243
ret ptr %p
240244
}

0 commit comments

Comments
 (0)