Skip to content

Commit 99f869c

Browse files
committed
[Attributes] Remove nonnull from UB-implying attributes
From LangRef: > if the parameter or return pointer is null, poison value is > returned or passed instead. The nonnull attribute should be > combined with the noundef attribute to ensure a pointer is not > null or otherwise the behavior is undefined. Dropping noundef is sufficient to prevent UB. Including nonnull in this method just muddies the semantics.
1 parent 34dc4f2 commit 99f869c

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

llvm/lib/IR/Attributes.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,6 @@ AttributeList::removeParamUndefImplyingAttributes(LLVMContext &C,
13401340
unsigned ArgNo) const {
13411341
AttrBuilder B;
13421342
B.addAttribute(Attribute::NoUndef);
1343-
B.addAttribute(Attribute::NonNull);
13441343
B.addDereferenceableAttr(1);
13451344
B.addDereferenceableOrNullAttr(1);
13461345
return removeParamAttributes(C, ArgNo, B);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
; RUN: opt -deadargelim -S < %s | FileCheck %s
22

3-
; If caller is changed to pass in undef, noundef and related attributes
4-
; should be deleted.
3+
; If caller is changed to pass in undef, noundef, dereferenceable and other
4+
; attributes that imply immediate undefined behavior should be delete.
5+
; Other attributes like nonnull, which only imply poison, can be safely kept.
56

6-
7-
; CHECK: define i64 @bar(i64* %0, i64 %1)
7+
; CHECK: define i64 @bar(i64* nonnull %0, i64 %1)
88
define i64 @bar(i64* nonnull dereferenceable(8) %0, i64 %1) {
99
entry:
1010
%2 = add i64 %1, 8
1111
ret i64 %2
1212
}
1313

1414
define i64 @foo(i64* %p, i64 %v) {
15-
; CHECK: %retval = call i64 @bar(i64* undef, i64 %v)
15+
; CHECK: %retval = call i64 @bar(i64* nonnull undef, i64 %v)
1616
%retval = call i64 @bar(i64* nonnull dereferenceable(8) %p, i64 %v)
1717
ret i64 %retval
1818
}

llvm/test/Transforms/InstCombine/unused-nonnull.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ target triple = "x86_64-unknown-linux-gnu"
99

1010
define i32 @main(i32 %argc, i8** %argv) #0 {
1111
; CHECK-LABEL: define {{[^@]+}}@main
12-
; CHECK-SAME: (i32 [[ARGC:%.*]], i8** nocapture readnone [[ARGV:%.*]]) local_unnamed_addr #0
12+
; CHECK-SAME: (i32 [[ARGC:%.*]], i8** nocapture readnone [[ARGV:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
1313
; CHECK-NEXT: entry:
1414
; CHECK-NEXT: [[TMP0:%.*]] = icmp slt i32 [[ARGC]], 2
1515
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TMP0]], i32 0, i32 [[ARGC]]
@@ -37,7 +37,7 @@ done:
3737

3838
define i32 @compute(i8* noundef nonnull %ptr, i32 %x) #1 {
3939
; CHECK-LABEL: define {{[^@]+}}@compute
40-
; CHECK-SAME: (i8* nocapture readnone [[PTR:%.*]], i32 returned [[X:%.*]]) local_unnamed_addr #1
40+
; CHECK-SAME: (i8* nocapture nonnull readnone [[PTR:%.*]], i32 returned [[X:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] {
4141
; CHECK-NEXT: ret i32 [[X]]
4242
;
4343
ret i32 %x

0 commit comments

Comments
 (0)