443 changes: 429 additions & 14 deletions llvm/lib/Transforms/IPO/Attributor.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion llvm/test/Transforms/FunctionAttrs/arg_nocapture.ll
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ entry:
; Other arguments are possible here due to the no-return behavior.
;
; FIXME: no-return missing
; CHECK: define noalias nonnull i32* @srec16(i32* nocapture readnone %a)
; CHECK: define noalias nonnull dereferenceable(4294967295) i32* @srec16(i32* nocapture readnone %a)
define i32* @srec16(i32* %a) #0 {
entry:
%call = call i32* @srec16(i32* %a)
Expand Down
52 changes: 52 additions & 0 deletions llvm/test/Transforms/FunctionAttrs/dereferenceable.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
; RUN: opt -attributor --attributor-disable=false -S < %s | FileCheck %s --check-prefixes=ATTRIBUTOR


; TEST 1
; take mininimum of return values
;
define i32* @test1(i32* dereferenceable(4), double* dereferenceable(8), i1 zeroext) local_unnamed_addr {
; ATTRIBUTOR: define nonnull dereferenceable(4) i32* @test1(i32* nonnull dereferenceable(4), double* nonnull dereferenceable(8), i1 zeroext)
%4 = bitcast double* %1 to i32*
%5 = select i1 %2, i32* %0, i32* %4
ret i32* %5
}

; TEST 2
define i32* @test2(i32* dereferenceable_or_null(4), double* dereferenceable(8), i1 zeroext) local_unnamed_addr {
; ATTRIBUTOR: define dereferenceable_or_null(4) i32* @test2(i32* dereferenceable_or_null(4), double* nonnull dereferenceable(8), i1 zeroext)
%4 = bitcast double* %1 to i32*
%5 = select i1 %2, i32* %0, i32* %4
ret i32* %5
}

; TEST 3
; GEP inbounds
define i32* @test3_1(i32* dereferenceable(8)) local_unnamed_addr {
; ATTRIBUTOR: define nonnull dereferenceable(4) i32* @test3_1(i32* nonnull dereferenceable(8))
%ret = getelementptr inbounds i32, i32* %0, i64 1
ret i32* %ret
}

define i32* @test3_2(i32* dereferenceable_or_null(32)) local_unnamed_addr {
; FIXME: Argument should be mark dereferenceable because of GEP `inbounds`.
; ATTRIBUTOR: define nonnull dereferenceable(16) i32* @test3_2(i32* dereferenceable_or_null(32))
%ret = getelementptr inbounds i32, i32* %0, i64 4
ret i32* %ret
}

define i32* @test3_3(i32* dereferenceable(8), i32* dereferenceable(16), i1) local_unnamed_addr {
; ATTRIBUTOR: define nonnull dereferenceable(4) i32* @test3_3(i32* nonnull dereferenceable(8), i32* nonnull dereferenceable(16), i1) local_unnamed_addr
%ret1 = getelementptr inbounds i32, i32* %0, i64 1
%ret2 = getelementptr inbounds i32, i32* %1, i64 2
%ret = select i1 %2, i32* %ret1, i32* %ret2
ret i32* %ret
}

; TEST 4
; Better than known in IR.

define dereferenceable(4) i32* @test4(i32* dereferenceable(8)) local_unnamed_addr {
; ATTRIBUTOR: define nonnull dereferenceable(8) i32* @test4(i32* nonnull returned dereferenceable(8))
ret i32* %0
}

4 changes: 2 additions & 2 deletions llvm/test/Transforms/FunctionAttrs/noalias_returned.ll
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ declare i8* @baz(...) nounwind uwtable
; TEST 5

; Returning global pointer. Should not be noalias.
; CHECK: define nonnull i8** @getter()
; CHECK: define nonnull dereferenceable(8) i8** @getter()
define i8** @getter() {
ret i8** @G
}

; Returning global pointer. Should not be noalias.
; CHECK: define nonnull i8** @calle1()
; CHECK: define nonnull dereferenceable(8) i8** @calle1()
define i8** @calle1(){
%1 = call i8** @getter()
ret i8** %1
Expand Down
13 changes: 11 additions & 2 deletions llvm/test/Transforms/FunctionAttrs/nonnull.ll
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ define i8* @test3() {
; just never return period.)
define i8* @test4_helper() {
; FNATTR: define noalias nonnull i8* @test4_helper
; ATTRIBUTOR: define noalias nonnull i8* @test4_helper
; ATTRIBUTOR: define noalias nonnull dereferenceable(4294967295) i8* @test4_helper
%ret = call i8* @test4()
ret i8* %ret
}

define i8* @test4() {
; FNATTR: define noalias nonnull i8* @test4
; ATTRIBUTOR: define noalias nonnull i8* @test4
; ATTRIBUTOR: define noalias nonnull dereferenceable(4294967295) i8* @test4
%ret = call i8* @test4_helper()
ret i8* %ret
}
Expand Down Expand Up @@ -219,6 +219,15 @@ bb:
%tmp = call i32* @f1(i32* %arg)
ret i32* null
}

; TEST 15
define void @f15(i8* %arg) {
; ATTRIBUTOR: tail call void @use1(i8* nonnull dereferenceable(4) %arg)

tail call void @use1(i8* dereferenceable(4) %arg)
ret void
}

; Test propagation of nonnull callsite args back to caller.

declare void @use1(i8* %x)
Expand Down