1; RUN: opt < %s -functionattrs -S | FileCheck %s --check-prefixes=CHECK,FNATTR
2; RUN: opt < %s -aa-pipeline=basic-aa -passes='cgscc(function-attrs)' -S | FileCheck %s --check-prefixes=CHECK,FNATTR
3; RUN: opt < %s -attributor -attributor-disable=false -S -attributor-annotate-decl-cs | FileCheck %s --check-prefixes=CHECK,ATTRIBUTOR
4; RUN: opt < %s -aa-pipeline=basic-aa -passes='attributor' -attributor-disable=false -S -attributor-annotate-decl-cs | FileCheck %s --check-prefixes=CHECK,ATTRIBUTOR
5
6@x = global i32 0
7
8declare void @test1_1(i8* %x1_1, i8* readonly %y1_1, ...)
9
10; NOTE: readonly for %y1_2 would be OK here but not for the similar situation in test13.
11;
12; FNATTR: define void @test1_2(i8* %x1_2, i8* readonly %y1_2, i8* %z1_2)
13; ATTRIBUTOR: define void @test1_2(i8* %x1_2, i8* %y1_2, i8* %z1_2)
14define void @test1_2(i8* %x1_2, i8* %y1_2, i8* %z1_2) {
15  call void (i8*, i8*, ...) @test1_1(i8* %x1_2, i8* %y1_2, i8* %z1_2)
16  store i32 0, i32* @x
17  ret void
18}
19
20; FNATTR: define i8* @test2(i8* readnone returned %p)
21; ATTRIBUTOR: define i8* @test2(i8* nofree readnone returned %p)
22define i8* @test2(i8* %p) {
23  store i32 0, i32* @x
24  ret i8* %p
25}
26
27; FNATTR: define i1 @test3(i8* readnone %p, i8* readnone %q)
28; ATTRIBUTOR: define i1 @test3(i8* nofree readnone %p, i8* nofree readnone %q)
29define i1 @test3(i8* %p, i8* %q) {
30  %A = icmp ult i8* %p, %q
31  ret i1 %A
32}
33
34declare void @test4_1(i8* nocapture) readonly
35
36; CHECK: define void @test4_2(i8* nocapture readonly %p)
37define void @test4_2(i8* %p) {
38  call void @test4_1(i8* %p)
39  ret void
40}
41
42; FNATTR: define void @test5(i8** nocapture %p, i8* %q)
43; ATTRIBUTOR: define void @test5(i8** nocapture nofree nonnull writeonly dereferenceable(8) %p, i8* nofree writeonly %q)
44; Missed optz'n: we could make %q readnone, but don't break test6!
45define void @test5(i8** %p, i8* %q) {
46  store i8* %q, i8** %p
47  ret void
48}
49
50declare void @test6_1()
51; FNATTR: define void @test6_2(i8** nocapture %p, i8* %q)
52; ATTRIBUTOR: define void @test6_2(i8** nocapture nonnull writeonly dereferenceable(8) %p, i8* %q)
53; This is not a missed optz'n.
54define void @test6_2(i8** %p, i8* %q) {
55  store i8* %q, i8** %p
56  call void @test6_1()
57  ret void
58}
59
60; FNATTR: define void @test7_1(i32* inalloca nocapture %a)
61; ATTRIBUTOR: define void @test7_1(i32* inalloca nocapture nofree writeonly %a)
62; inalloca parameters are always considered written
63define void @test7_1(i32* inalloca %a) {
64  ret void
65}
66
67; FNATTR: define i32* @test8_1(i32* readnone returned %p)
68; ATTRIBUTOR: define i32* @test8_1(i32* nofree readnone returned %p)
69define i32* @test8_1(i32* %p) {
70entry:
71  ret i32* %p
72}
73
74; FNATTR: define void @test8_2(i32* %p)
75; ATTRIBUTOR: define void @test8_2(i32* nocapture nofree writeonly %p)
76define void @test8_2(i32* %p) {
77entry:
78  %call = call i32* @test8_1(i32* %p)
79  store i32 10, i32* %call, align 4
80  ret void
81}
82
83; CHECK: declare void @llvm.masked.scatter
84declare void @llvm.masked.scatter.v4i32.v4p0i32(<4 x i32>%val, <4 x i32*>, i32, <4 x i1>)
85
86; CHECK-NOT: readnone
87; CHECK-NOT: readonly
88; CHECK: define void @test9
89define void @test9(<4 x i32*> %ptrs, <4 x i32>%val) {
90  call void @llvm.masked.scatter.v4i32.v4p0i32(<4 x i32>%val, <4 x i32*> %ptrs, i32 4, <4 x i1><i1 true, i1 false, i1 true, i1 false>)
91  ret void
92}
93
94; CHECK: declare <4 x i32> @llvm.masked.gather
95declare <4 x i32> @llvm.masked.gather.v4i32.v4p0i32(<4 x i32*>, i32, <4 x i1>, <4 x i32>)
96; CHECK: readonly
97; CHECK: define <4 x i32> @test10
98define <4 x i32> @test10(<4 x i32*> %ptrs) {
99  %res = call <4 x i32> @llvm.masked.gather.v4i32.v4p0i32(<4 x i32*> %ptrs, i32 4, <4 x i1><i1 true, i1 false, i1 true, i1 false>, <4 x i32>undef)
100  ret <4 x i32> %res
101}
102
103; CHECK: declare <4 x i32> @test11_1
104declare <4 x i32> @test11_1(<4 x i32*>) argmemonly nounwind readonly
105; CHECK: readonly
106; CHECK-NOT: readnone
107; CHECK: define <4 x i32> @test11_2
108define <4 x i32> @test11_2(<4 x i32*> %ptrs) {
109  %res = call <4 x i32> @test11_1(<4 x i32*> %ptrs)
110  ret <4 x i32> %res
111}
112
113declare <4 x i32> @test12_1(<4 x i32*>) argmemonly nounwind
114; CHECK-NOT: readnone
115; CHECK: define <4 x i32> @test12_2
116define <4 x i32> @test12_2(<4 x i32*> %ptrs) {
117  %res = call <4 x i32> @test12_1(<4 x i32*> %ptrs)
118  ret <4 x i32> %res
119}
120
121; CHECK: define i32 @volatile_load(
122; CHECK-NOT: readonly
123; CHECK: ret
124define i32 @volatile_load(i32* %p) {
125  %load = load volatile i32, i32* %p
126  ret i32 %load
127}
128
129declare void @escape_readnone_ptr(i8** %addr, i8* readnone %ptr)
130declare void @escape_readonly_ptr(i8** %addr, i8* readonly %ptr)
131
132; The argument pointer %escaped_then_written cannot be marked readnone/only even
133; though the only direct use, in @escape_readnone_ptr/@escape_readonly_ptr,
134; is marked as readnone/only. However, the functions can write the pointer into
135; %addr, causing the store to write to %escaped_then_written.
136;
137; FIXME: This test currently exposes a bug in functionattrs!
138;
139; FNATTR: define void @unsound_readnone(i8* nocapture readnone %ignored, i8* readnone %escaped_then_written)
140; FNATTR: define void @unsound_readonly(i8* nocapture readnone %ignored, i8* readonly %escaped_then_written)
141;
142; ATTRIBUTOR: define void @unsound_readnone(i8* nocapture nofree readnone %ignored, i8* %escaped_then_written)
143; ATTRIBUTOR: define void @unsound_readonly(i8* nocapture nofree readnone %ignored, i8* %escaped_then_written)
144define void @unsound_readnone(i8* %ignored, i8* %escaped_then_written) {
145  %addr = alloca i8*
146  call void @escape_readnone_ptr(i8** %addr, i8* %escaped_then_written)
147  %addr.ld = load i8*, i8** %addr
148  store i8 0, i8* %addr.ld
149  ret void
150}
151
152define void @unsound_readonly(i8* %ignored, i8* %escaped_then_written) {
153  %addr = alloca i8*
154  call void @escape_readonly_ptr(i8** %addr, i8* %escaped_then_written)
155  %addr.ld = load i8*, i8** %addr
156  store i8 0, i8* %addr.ld
157  ret void
158}
159