1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes --check-attributes
2; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal  -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=15 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
3; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal  -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=17 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
4; RUN: opt -attributor-cgscc -enable-new-pm=0 -attributor-manifest-internal  -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
5; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal  -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM
6
7
8target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
9
10declare nonnull i8* @ret_nonnull()
11declare void @llvm.assume(i1)
12
13; Return a pointer trivially nonnull (call return attribute)
14define i8* @test1() {
15; CHECK-LABEL: define {{[^@]+}}@test1()
16; CHECK-NEXT:    [[RET:%.*]] = call nonnull i8* @ret_nonnull()
17; CHECK-NEXT:    ret i8* [[RET]]
18;
19  %ret = call i8* @ret_nonnull()
20  ret i8* %ret
21}
22
23; Return a pointer trivially nonnull (argument attribute)
24define i8* @test2(i8* nonnull %p) {
25; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
26; IS__TUNIT____-LABEL: define {{[^@]+}}@test2
27; IS__TUNIT____-SAME: (i8* nofree nonnull readnone returned "no-capture-maybe-returned" [[P:%.*]])
28; IS__TUNIT____-NEXT:    ret i8* [[P]]
29;
30; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
31; IS__CGSCC____-LABEL: define {{[^@]+}}@test2
32; IS__CGSCC____-SAME: (i8* nofree nonnull readnone returned "no-capture-maybe-returned" [[P:%.*]])
33; IS__CGSCC____-NEXT:    ret i8* [[P]]
34;
35  ret i8* %p
36}
37
38define i8* @test2A(i1 %c, i8* %ret) {
39; ATTRIBUTOR: define nonnull i8* @test2A(i1 %c, i8* nofree nonnull readnone returned %ret)
40; NOT_CGSCC_OPM: Function Attrs: nounwind willreturn
41; NOT_CGSCC_OPM-LABEL: define {{[^@]+}}@test2A
42; NOT_CGSCC_OPM-SAME: (i1 [[C:%.*]], i8* nofree nonnull readnone returned "no-capture-maybe-returned" [[RET:%.*]])
43; NOT_CGSCC_OPM-NEXT:    br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
44; NOT_CGSCC_OPM:       A:
45; NOT_CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR11:#.*]] [ "nonnull"(i8* [[RET]]) ]
46; NOT_CGSCC_OPM-NEXT:    ret i8* [[RET]]
47; NOT_CGSCC_OPM:       B:
48; NOT_CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR11]] [ "nonnull"(i8* [[RET]]) ]
49; NOT_CGSCC_OPM-NEXT:    ret i8* [[RET]]
50;
51; IS__CGSCC_OPM: Function Attrs: nounwind willreturn
52; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@test2A
53; IS__CGSCC_OPM-SAME: (i1 [[C:%.*]], i8* nofree nonnull readnone returned "no-capture-maybe-returned" [[RET:%.*]])
54; IS__CGSCC_OPM-NEXT:    br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
55; IS__CGSCC_OPM:       A:
56; IS__CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR12:#.*]] [ "nonnull"(i8* [[RET]]) ]
57; IS__CGSCC_OPM-NEXT:    ret i8* [[RET]]
58; IS__CGSCC_OPM:       B:
59; IS__CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR12]] [ "nonnull"(i8* [[RET]]) ]
60; IS__CGSCC_OPM-NEXT:    ret i8* [[RET]]
61;
62  br i1 %c, label %A, label %B
63A:
64  call void @llvm.assume(i1 true) [ "nonnull"(i8* %ret) ]
65  ret i8* %ret
66B:
67  call void @llvm.assume(i1 true) [ "nonnull"(i8* %ret) ]
68  ret i8* %ret
69}
70
71define i8* @test2B(i1 %c, i8* %ret) {
72; ATTRIBUTOR: define nonnull dereferenceable(4) i8* @test2B(i1 %c, i8* nofree nonnull readnone returned dereferenceable(4) %ret)
73; NOT_CGSCC_OPM: Function Attrs: nounwind willreturn
74; NOT_CGSCC_OPM-LABEL: define {{[^@]+}}@test2B
75; NOT_CGSCC_OPM-SAME: (i1 [[C:%.*]], i8* nofree nonnull readnone returned dereferenceable(4) "no-capture-maybe-returned" [[RET:%.*]])
76; NOT_CGSCC_OPM-NEXT:    br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
77; NOT_CGSCC_OPM:       A:
78; NOT_CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR11]] [ "dereferenceable"(i8* [[RET]], i32 4) ]
79; NOT_CGSCC_OPM-NEXT:    ret i8* [[RET]]
80; NOT_CGSCC_OPM:       B:
81; NOT_CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR11]] [ "dereferenceable"(i8* [[RET]], i32 4) ]
82; NOT_CGSCC_OPM-NEXT:    ret i8* [[RET]]
83;
84; IS__CGSCC_OPM: Function Attrs: nounwind willreturn
85; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@test2B
86; IS__CGSCC_OPM-SAME: (i1 [[C:%.*]], i8* nofree nonnull readnone returned dereferenceable(4) "no-capture-maybe-returned" [[RET:%.*]])
87; IS__CGSCC_OPM-NEXT:    br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
88; IS__CGSCC_OPM:       A:
89; IS__CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR12]] [ "dereferenceable"(i8* [[RET]], i32 4) ]
90; IS__CGSCC_OPM-NEXT:    ret i8* [[RET]]
91; IS__CGSCC_OPM:       B:
92; IS__CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR12]] [ "dereferenceable"(i8* [[RET]], i32 4) ]
93; IS__CGSCC_OPM-NEXT:    ret i8* [[RET]]
94;
95  br i1 %c, label %A, label %B
96A:
97  call void @llvm.assume(i1 true) [ "dereferenceable"(i8* %ret, i32 4) ]
98  ret i8* %ret
99B:
100  call void @llvm.assume(i1 true) [ "dereferenceable"(i8* %ret, i32 4) ]
101  ret i8* %ret
102}
103
104; Given an SCC where one of the functions can not be marked nonnull,
105; can we still mark the other one which is trivially nonnull
106define i8* @scc_binder(i1 %c) {
107; CHECK-LABEL: define {{[^@]+}}@scc_binder
108; CHECK-SAME: (i1 [[C:%.*]])
109; CHECK-NEXT:    br i1 [[C]], label [[REC:%.*]], label [[END:%.*]]
110; CHECK:       rec:
111; CHECK-NEXT:    [[TMP1:%.*]] = call i8* @test3(i1 [[C]])
112; CHECK-NEXT:    br label [[END]]
113; CHECK:       end:
114; CHECK-NEXT:    ret i8* null
115;
116  br i1 %c, label %rec, label %end
117rec:
118  call i8* @test3(i1 %c)
119  br label %end
120end:
121  ret i8* null
122}
123
124define i8* @test3(i1 %c) {
125; CHECK-LABEL: define {{[^@]+}}@test3
126; CHECK-SAME: (i1 [[C:%.*]])
127; CHECK-NEXT:    [[TMP1:%.*]] = call i8* @scc_binder(i1 [[C]])
128; CHECK-NEXT:    [[RET:%.*]] = call nonnull i8* @ret_nonnull()
129; CHECK-NEXT:    ret i8* [[RET]]
130;
131  call i8* @scc_binder(i1 %c)
132  %ret = call i8* @ret_nonnull()
133  ret i8* %ret
134}
135
136; Given a mutual recursive set of functions, we can mark them
137; nonnull if neither can ever return null.  (In this case, they
138; just never return period.)
139define i8* @test4_helper() {
140; NOT_CGSCC_NPM: Function Attrs: nofree noreturn nosync nounwind readnone
141; NOT_CGSCC_NPM-LABEL: define {{[^@]+}}@test4_helper()
142; NOT_CGSCC_NPM-NEXT:    unreachable
143;
144; IS__CGSCC_NPM: Function Attrs: nofree norecurse noreturn nosync nounwind readnone willreturn
145; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@test4_helper()
146; IS__CGSCC_NPM-NEXT:    unreachable
147;
148  %ret = call i8* @test4()
149  ret i8* %ret
150}
151
152define i8* @test4() {
153; NOT_CGSCC_NPM: Function Attrs: nofree noreturn nosync nounwind readnone
154; NOT_CGSCC_NPM-LABEL: define {{[^@]+}}@test4()
155; NOT_CGSCC_NPM-NEXT:    unreachable
156;
157; IS__CGSCC_NPM: Function Attrs: nofree norecurse noreturn nosync nounwind readnone willreturn
158; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@test4()
159; IS__CGSCC_NPM-NEXT:    unreachable
160;
161  %ret = call i8* @test4_helper()
162  ret i8* %ret
163}
164
165; Given a mutual recursive set of functions which *can* return null
166; make sure we haven't marked them as nonnull.
167define i8* @test5_helper(i1 %c) {
168; NOT_CGSCC_NPM: Function Attrs: nofree nosync nounwind readnone willreturn
169; NOT_CGSCC_NPM-LABEL: define {{[^@]+}}@test5_helper
170; NOT_CGSCC_NPM-SAME: (i1 [[C:%.*]])
171; NOT_CGSCC_NPM-NEXT:    br i1 [[C]], label [[REC:%.*]], label [[END:%.*]]
172; NOT_CGSCC_NPM:       rec:
173; NOT_CGSCC_NPM-NEXT:    br label [[END]]
174; NOT_CGSCC_NPM:       end:
175; NOT_CGSCC_NPM-NEXT:    ret i8* null
176;
177; IS__CGSCC_NPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
178; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@test5_helper
179; IS__CGSCC_NPM-SAME: (i1 [[C:%.*]])
180; IS__CGSCC_NPM-NEXT:    br i1 [[C]], label [[REC:%.*]], label [[END:%.*]]
181; IS__CGSCC_NPM:       rec:
182; IS__CGSCC_NPM-NEXT:    br label [[END]]
183; IS__CGSCC_NPM:       end:
184; IS__CGSCC_NPM-NEXT:    ret i8* null
185;
186  br i1 %c, label %rec, label %end
187rec:
188  %ret = call i8* @test5(i1 %c)
189  br label %end
190end:
191  ret i8* null
192}
193
194define i8* @test5(i1 %c) {
195; NOT_CGSCC_NPM: Function Attrs: nofree nosync nounwind readnone willreturn
196; NOT_CGSCC_NPM-LABEL: define {{[^@]+}}@test5
197; NOT_CGSCC_NPM-SAME: (i1 [[C:%.*]])
198; NOT_CGSCC_NPM-NEXT:    ret i8* null
199;
200; IS__CGSCC_NPM: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
201; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@test5
202; IS__CGSCC_NPM-SAME: (i1 [[C:%.*]])
203; IS__CGSCC_NPM-NEXT:    ret i8* null
204;
205  %ret = call i8* @test5_helper(i1 %c)
206  ret i8* %ret
207}
208
209; Local analysis, but going through a self recursive phi
210define i8* @test6a() {
211; CHECK: Function Attrs: noreturn
212; CHECK-LABEL: define {{[^@]+}}@test6a()
213; CHECK-NEXT:  entry:
214; CHECK-NEXT:    [[RET:%.*]] = call i8* @ret_nonnull()
215; CHECK-NEXT:    br label [[LOOP:%.*]]
216; CHECK:       loop:
217; CHECK-NEXT:    unreachable
218; CHECK:       exit:
219; CHECK-NEXT:    unreachable
220;
221entry:
222  %ret = call i8* @ret_nonnull()
223  br label %loop
224loop:
225  %phi = phi i8* [%ret, %entry], [%phi, %loop]
226  br i1 undef, label %loop, label %exit
227exit:
228  ret i8* %phi
229}
230
231define i8* @test6b(i1 %c) {
232; CHECK-LABEL: define {{[^@]+}}@test6b
233; CHECK-SAME: (i1 [[C:%.*]])
234; CHECK-NEXT:  entry:
235; CHECK-NEXT:    [[RET:%.*]] = call nonnull i8* @ret_nonnull()
236; CHECK-NEXT:    br label [[LOOP:%.*]]
237; CHECK:       loop:
238; CHECK-NEXT:    [[PHI:%.*]] = phi i8* [ [[RET]], [[ENTRY:%.*]] ], [ [[PHI]], [[LOOP]] ]
239; CHECK-NEXT:    br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
240; CHECK:       exit:
241; CHECK-NEXT:    ret i8* [[PHI]]
242;
243entry:
244  %ret = call i8* @ret_nonnull()
245  br label %loop
246loop:
247  %phi = phi i8* [%ret, %entry], [%phi, %loop]
248  br i1 %c, label %loop, label %exit
249exit:
250  ret i8* %phi
251}
252
253define i8* @test7(i8* %a) {
254; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
255; IS__TUNIT____-LABEL: define {{[^@]+}}@test7
256; IS__TUNIT____-SAME: (i8* nofree readnone returned "no-capture-maybe-returned" [[A:%.*]])
257; IS__TUNIT____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 0
258; IS__TUNIT____-NEXT:    ret i8* [[B]]
259;
260; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
261; IS__CGSCC____-LABEL: define {{[^@]+}}@test7
262; IS__CGSCC____-SAME: (i8* nofree readnone returned "no-capture-maybe-returned" [[A:%.*]])
263; IS__CGSCC____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 0
264; IS__CGSCC____-NEXT:    ret i8* [[B]]
265;
266  %b = getelementptr inbounds i8, i8* %a, i64 0
267  ret i8* %b
268}
269
270define i8* @test8(i8* %a) {
271; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
272; IS__TUNIT____-LABEL: define {{[^@]+}}@test8
273; IS__TUNIT____-SAME: (i8* nofree readnone "no-capture-maybe-returned" [[A:%.*]])
274; IS__TUNIT____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 1
275; IS__TUNIT____-NEXT:    ret i8* [[B]]
276;
277; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
278; IS__CGSCC____-LABEL: define {{[^@]+}}@test8
279; IS__CGSCC____-SAME: (i8* nofree readnone "no-capture-maybe-returned" [[A:%.*]])
280; IS__CGSCC____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 1
281; IS__CGSCC____-NEXT:    ret i8* [[B]]
282;
283  %b = getelementptr inbounds i8, i8* %a, i64 1
284  ret i8* %b
285}
286
287define i8* @test9(i8* %a, i64 %n) {
288; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
289; IS__TUNIT____-LABEL: define {{[^@]+}}@test9
290; IS__TUNIT____-SAME: (i8* nofree readnone "no-capture-maybe-returned" [[A:%.*]], i64 [[N:%.*]])
291; IS__TUNIT____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 [[N]]
292; IS__TUNIT____-NEXT:    ret i8* [[B]]
293;
294; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
295; IS__CGSCC____-LABEL: define {{[^@]+}}@test9
296; IS__CGSCC____-SAME: (i8* nofree readnone "no-capture-maybe-returned" [[A:%.*]], i64 [[N:%.*]])
297; IS__CGSCC____-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 [[N]]
298; IS__CGSCC____-NEXT:    ret i8* [[B]]
299;
300  %b = getelementptr inbounds i8, i8* %a, i64 %n
301  ret i8* %b
302}
303
304; ATTRIBUTOR_OPM: define i8* @test10
305; ATTRIBUTOR_NPM: define nonnull i8* @test10
306define i8* @test10(i8* %a, i64 %n) {
307; CHECK: Function Attrs: nounwind willreturn
308; CHECK-LABEL: define {{[^@]+}}@test10
309; CHECK-SAME: (i8* nofree readnone "no-capture-maybe-returned" [[A:%.*]], i64 [[N:%.*]])
310; CHECK-NEXT:    call void @llvm.assume(i1 true)
311; CHECK-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[A]], i64 [[N]]
312; CHECK-NEXT:    ret i8* [[B]]
313;
314  %cmp = icmp ne i64 %n, 0
315  call void @llvm.assume(i1 %cmp)
316  %b = getelementptr inbounds i8, i8* %a, i64 %n
317  ret i8* %b
318}
319
320; TEST 11
321; char* test11(char *p) {
322;   return p? p: nonnull();
323; }
324; FIXME: missing nonnull
325define i8* @test11(i8*) local_unnamed_addr {
326; CHECK-LABEL: define {{[^@]+}}@test11
327; CHECK-SAME: (i8* [[TMP0:%.*]]) local_unnamed_addr
328; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8* [[TMP0]], null
329; CHECK-NEXT:    br i1 [[TMP2]], label [[TMP3:%.*]], label [[TMP5:%.*]]
330; CHECK:       3:
331; CHECK-NEXT:    [[TMP4:%.*]] = tail call i8* @ret_nonnull()
332; CHECK-NEXT:    br label [[TMP5]]
333; CHECK:       5:
334; CHECK-NEXT:    [[TMP6:%.*]] = phi i8* [ [[TMP4]], [[TMP3]] ], [ [[TMP0]], [[TMP1:%.*]] ]
335; CHECK-NEXT:    ret i8* [[TMP6]]
336;
337  %2 = icmp eq i8* %0, null
338  br i1 %2, label %3, label %5
339
340; <label>:3:                                      ; preds = %1
341  %4 = tail call i8* @ret_nonnull()
342  br label %5
343
344; <label>:5:                                      ; preds = %3, %1
345  %6 = phi i8* [ %4, %3 ], [ %0, %1 ]
346  ret i8* %6
347}
348
349; TEST 12
350; Simple CallSite Test
351declare void @test12_helper(i8*)
352define void @test12(i8* nonnull %a) {
353; CHECK-LABEL: define {{[^@]+}}@test12
354; CHECK-SAME: (i8* nonnull [[A:%.*]])
355; CHECK-NEXT:    tail call void @test12_helper(i8* nonnull [[A]])
356; CHECK-NEXT:    ret void
357;
358  tail call void @test12_helper(i8* %a)
359  ret void
360}
361
362; TEST 13
363; Simple Argument Tests
364declare i8* @unknown()
365define void @test13_helper() {
366; CHECK-LABEL: define {{[^@]+}}@test13_helper()
367; CHECK-NEXT:    [[NONNULLPTR:%.*]] = tail call nonnull i8* @ret_nonnull()
368; CHECK-NEXT:    [[MAYBENULLPTR:%.*]] = tail call i8* @unknown()
369; CHECK-NEXT:    tail call void @test13(i8* noalias nocapture nofree nonnull readnone [[NONNULLPTR]], i8* noalias nocapture nofree nonnull readnone [[NONNULLPTR]], i8* noalias nocapture nofree readnone [[MAYBENULLPTR]])
370; CHECK-NEXT:    tail call void @test13(i8* noalias nocapture nofree nonnull readnone [[NONNULLPTR]], i8* noalias nocapture nofree readnone [[MAYBENULLPTR]], i8* noalias nocapture nofree nonnull readnone [[NONNULLPTR]])
371; CHECK-NEXT:    ret void
372;
373  %nonnullptr = tail call i8* @ret_nonnull()
374  %maybenullptr = tail call i8* @unknown()
375  tail call void @test13(i8* %nonnullptr, i8* %nonnullptr, i8* %maybenullptr)
376  tail call void @test13(i8* %nonnullptr, i8* %maybenullptr, i8* %nonnullptr)
377  ret void
378}
379define internal void @test13(i8* %a, i8* %b, i8* %c) {
380; IS__TUNIT____: Function Attrs: nounwind
381; IS__TUNIT____-LABEL: define {{[^@]+}}@test13
382; IS__TUNIT____-SAME: (i8* noalias nocapture nofree nonnull readnone [[A:%.*]], i8* noalias nocapture nofree readnone [[B:%.*]], i8* noalias nocapture nofree readnone [[C:%.*]])
383; IS__TUNIT____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[A]])
384; IS__TUNIT____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[B]])
385; IS__TUNIT____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[C]])
386; IS__TUNIT____-NEXT:    ret void
387;
388; IS__CGSCC____: Function Attrs: nounwind
389; IS__CGSCC____-LABEL: define {{[^@]+}}@test13
390; IS__CGSCC____-SAME: (i8* nocapture nofree readnone [[A:%.*]], i8* nocapture nofree readnone [[B:%.*]], i8* nocapture nofree readnone [[C:%.*]])
391; IS__CGSCC____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[A]])
392; IS__CGSCC____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[B]])
393; IS__CGSCC____-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[C]])
394; IS__CGSCC____-NEXT:    ret void
395;
396  call void @use_i8_ptr(i8* %a)
397  call void @use_i8_ptr(i8* %b)
398  call void @use_i8_ptr(i8* %c)
399  ret void
400}
401
402declare nonnull i8* @nonnull()
403
404; TEST 14
405; Complex propagation
406; Argument of f1, f2, f3 can be marked with nonnull.
407
408; * Argument
409; 1. In f1:bb6, %arg can be marked with nonnull because of the comparison in bb1
410; 2. Because f2 is internal function, f2(i32* %arg) -> @f2(i32* nonnull %arg)
411; 3. In f1:bb4 %tmp5 is nonnull and f3 is internal function.
412;    Then, f3(i32* %arg) -> @f3(i32* nonnull %arg)
413; 4. We get nonnull in whole f1 call sites so f1(i32* %arg) -> @f1(i32* nonnull %arg)
414
415
416define internal i32* @f1(i32* %arg) {
417; FIXME: missing nonnull It should be nonnull @f1(i32* nonnull readonly %arg)
418; CHECK: Function Attrs: argmemonly nofree nosync nounwind readonly
419; CHECK-LABEL: define {{[^@]+}}@f1
420; CHECK-SAME: (i32* nofree readonly [[ARG:%.*]])
421; CHECK-NEXT:  bb:
422; CHECK-NEXT:    [[TMP:%.*]] = icmp eq i32* [[ARG]], null
423; CHECK-NEXT:    br i1 [[TMP]], label [[BB9:%.*]], label [[BB1:%.*]]
424; CHECK:       bb1:
425; CHECK-NEXT:    [[TMP2:%.*]] = load i32, i32* [[ARG]], align 4
426; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0
427; CHECK-NEXT:    br i1 [[TMP3]], label [[BB6:%.*]], label [[BB4:%.*]]
428; CHECK:       bb4:
429; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr inbounds i32, i32* [[ARG]], i64 1
430; CHECK-NEXT:    [[TMP5B:%.*]] = tail call i32* @f3(i32* nofree nonnull readonly [[TMP5]])
431; CHECK-NEXT:    [[TMP5C:%.*]] = getelementptr inbounds i32, i32* [[TMP5B]], i64 -1
432; CHECK-NEXT:    br label [[BB9]]
433; CHECK:       bb6:
434; CHECK-NEXT:    [[TMP7:%.*]] = tail call nonnull i32* @f2(i32* nofree nonnull readonly align 4 dereferenceable(4) [[ARG]])
435; CHECK-NEXT:    ret i32* [[TMP7]]
436; CHECK:       bb9:
437; CHECK-NEXT:    [[TMP10:%.*]] = phi i32* [ [[TMP5C]], [[BB4]] ], [ inttoptr (i64 4 to i32*), [[BB:%.*]] ]
438; CHECK-NEXT:    ret i32* [[TMP10]]
439;
440
441bb:
442  %tmp = icmp eq i32* %arg, null
443  br i1 %tmp, label %bb9, label %bb1
444
445bb1:                                              ; preds = %bb
446  %tmp2 = load i32, i32* %arg, align 4
447  %tmp3 = icmp eq i32 %tmp2, 0
448  br i1 %tmp3, label %bb6, label %bb4
449
450bb4:                                              ; preds = %bb1
451  %tmp5 = getelementptr inbounds i32, i32* %arg, i64 1
452  %tmp5b = tail call i32* @f3(i32* %tmp5)
453  %tmp5c = getelementptr inbounds i32, i32* %tmp5b, i64 -1
454  br label %bb9
455
456bb6:                                              ; preds = %bb1
457  %tmp7 = tail call i32* @f2(i32* %arg)
458  ret i32* %tmp7
459
460bb9:                                              ; preds = %bb4, %bb
461  %tmp10 = phi i32* [ %tmp5c, %bb4 ], [ inttoptr (i64 4 to i32*), %bb ]
462  ret i32* %tmp10
463}
464
465define internal i32* @f2(i32* %arg) {
466; CHECK: Function Attrs: argmemonly nofree nosync nounwind readonly
467; CHECK-LABEL: define {{[^@]+}}@f2
468; CHECK-SAME: (i32* nofree nonnull readonly align 4 dereferenceable(4) [[ARG:%.*]])
469; CHECK-NEXT:  bb:
470; CHECK-NEXT:    [[TMP:%.*]] = tail call nonnull i32* @f1(i32* nofree nonnull readonly align 4 dereferenceable(4) [[ARG]])
471; CHECK-NEXT:    ret i32* [[TMP]]
472;
473bb:
474  %tmp = tail call i32* @f1(i32* %arg)
475  ret i32* %tmp
476}
477
478define dso_local noalias i32* @f3(i32* %arg) {
479; FIXME: missing nonnull. It should be nonnull @f3(i32* nonnull readonly %arg)
480; CHECK: Function Attrs: argmemonly nofree nosync nounwind readonly
481; CHECK-LABEL: define {{[^@]+}}@f3
482; CHECK-SAME: (i32* nofree readonly [[ARG:%.*]])
483; CHECK-NEXT:  bb:
484; CHECK-NEXT:    [[TMP:%.*]] = call nonnull i32* @f1(i32* nofree readonly [[ARG]])
485; CHECK-NEXT:    ret i32* [[TMP]]
486;
487bb:
488; FIXME: missing nonnull. It should be @f1(i32* nonnull readonly %arg)
489  %tmp = call i32* @f1(i32* %arg)
490  ret i32* %tmp
491}
492
493; TEST 15
494define void @f15(i8* %arg) {
495; CHECK-LABEL: define {{[^@]+}}@f15
496; CHECK-SAME: (i8* nonnull dereferenceable(4) [[ARG:%.*]])
497; CHECK-NEXT:    tail call void @use1(i8* nonnull dereferenceable(4) [[ARG]])
498; CHECK-NEXT:    ret void
499;
500
501  tail call void @use1(i8* dereferenceable(4) %arg)
502  ret void
503}
504
505declare void @fun0() #1
506declare void @fun1(i8*) #1
507declare void @fun2(i8*, i8*) #1
508declare void @fun3(i8*, i8*, i8*) #1
509; TEST 16 simple path test
510; if(..)
511;   fun2(nonnull %a, nonnull %b)
512; else
513;   fun2(nonnull %a, %b)
514; We can say that %a is nonnull but %b is not.
515define void @f16(i8* %a, i8 * %b, i8 %c) {
516; CHECK: Function Attrs: nounwind willreturn
517; CHECK-LABEL: define {{[^@]+}}@f16
518; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* [[B:%.*]], i8 [[C:%.*]])
519; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[C]], 0
520; CHECK-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
521; CHECK:       if.then:
522; CHECK-NEXT:    tail call void @fun2(i8* nonnull [[A]], i8* nonnull [[B]])
523; CHECK-NEXT:    ret void
524; CHECK:       if.else:
525; CHECK-NEXT:    tail call void @fun2(i8* nonnull [[A]], i8* [[B]])
526; CHECK-NEXT:    ret void
527;
528  %cmp = icmp eq i8 %c, 0
529  br i1 %cmp, label %if.then, label %if.else
530if.then:
531  tail call void @fun2(i8* nonnull %a, i8* nonnull %b)
532  ret void
533if.else:
534  tail call void @fun2(i8* nonnull %a, i8* %b)
535  ret void
536}
537; TEST 17 explore child BB test
538; if(..)
539;    ... (willreturn & nounwind)
540; else
541;    ... (willreturn & nounwind)
542; fun1(nonnull %a)
543; We can say that %a is nonnull
544define void @f17(i8* %a, i8 %c) {
545; CHECK: Function Attrs: nounwind willreturn
546; CHECK-LABEL: define {{[^@]+}}@f17
547; CHECK-SAME: (i8* nonnull [[A:%.*]], i8 [[C:%.*]])
548; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[C]], 0
549; CHECK-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
550; CHECK:       if.then:
551; CHECK-NEXT:    tail call void @fun0()
552; CHECK-NEXT:    br label [[CONT:%.*]]
553; CHECK:       if.else:
554; CHECK-NEXT:    tail call void @fun0()
555; CHECK-NEXT:    br label [[CONT]]
556; CHECK:       cont:
557; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[A]])
558; CHECK-NEXT:    ret void
559;
560  %cmp = icmp eq i8 %c, 0
561  br i1 %cmp, label %if.then, label %if.else
562if.then:
563  tail call void @fun0()
564  br label %cont
565if.else:
566  tail call void @fun0()
567  br label %cont
568cont:
569  tail call void @fun1(i8* nonnull %a)
570  ret void
571}
572; TEST 18 More complex test
573; if(..)
574;    ... (willreturn & nounwind)
575; else
576;    ... (willreturn & nounwind)
577; if(..)
578;    ... (willreturn & nounwind)
579; else
580;    ... (willreturn & nounwind)
581; fun1(nonnull %a)
582
583define void @f18(i8* %a, i8* %b, i8 %c) {
584; CHECK: Function Attrs: nounwind willreturn
585; CHECK-LABEL: define {{[^@]+}}@f18
586; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* [[B:%.*]], i8 [[C:%.*]])
587; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[C]], 0
588; CHECK-NEXT:    br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
589; CHECK:       if.then:
590; CHECK-NEXT:    tail call void @fun0()
591; CHECK-NEXT:    br label [[CONT:%.*]]
592; CHECK:       if.else:
593; CHECK-NEXT:    tail call void @fun0()
594; CHECK-NEXT:    br label [[CONT]]
595; CHECK:       cont:
596; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i8 [[C]], 1
597; CHECK-NEXT:    br i1 [[CMP2]], label [[CONT_THEN:%.*]], label [[CONT_ELSE:%.*]]
598; CHECK:       cont.then:
599; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[B]])
600; CHECK-NEXT:    br label [[CONT2:%.*]]
601; CHECK:       cont.else:
602; CHECK-NEXT:    tail call void @fun0()
603; CHECK-NEXT:    br label [[CONT2]]
604; CHECK:       cont2:
605; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[A]])
606; CHECK-NEXT:    ret void
607;
608  %cmp1 = icmp eq i8 %c, 0
609  br i1 %cmp1, label %if.then, label %if.else
610if.then:
611  tail call void @fun0()
612  br label %cont
613if.else:
614  tail call void @fun0()
615  br label %cont
616cont:
617  %cmp2 = icmp eq i8 %c, 1
618  br i1 %cmp2, label %cont.then, label %cont.else
619cont.then:
620  tail call void @fun1(i8* nonnull %b)
621  br label %cont2
622cont.else:
623  tail call void @fun0()
624  br label %cont2
625cont2:
626  tail call void @fun1(i8* nonnull %a)
627  ret void
628}
629
630; TEST 19: Loop
631
632define void @f19(i8* %a, i8* %b, i8 %c) {
633; CHECK: Function Attrs: nounwind
634; CHECK-LABEL: define {{[^@]+}}@f19
635; CHECK-SAME: (i8* [[A:%.*]], i8* nonnull [[B:%.*]], i8 [[C:%.*]])
636; CHECK-NEXT:    br label [[LOOP_HEADER:%.*]]
637; CHECK:       loop.header:
638; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i8 [[C]], 0
639; CHECK-NEXT:    br i1 [[CMP2]], label [[LOOP_BODY:%.*]], label [[LOOP_EXIT:%.*]]
640; CHECK:       loop.body:
641; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[B]])
642; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[A]])
643; CHECK-NEXT:    br label [[LOOP_HEADER]]
644; CHECK:       loop.exit:
645; CHECK-NEXT:    tail call void @fun1(i8* nonnull [[B]])
646; CHECK-NEXT:    ret void
647;
648  br label %loop.header
649loop.header:
650  %cmp2 = icmp eq i8 %c, 0
651  br i1 %cmp2, label %loop.body, label %loop.exit
652loop.body:
653  tail call void @fun1(i8* nonnull %b)
654  tail call void @fun1(i8* nonnull %a)
655  br label %loop.header
656loop.exit:
657  tail call void @fun1(i8* nonnull %b)
658  ret void
659}
660
661; Test propagation of nonnull callsite args back to caller.
662
663declare void @use1(i8* %x)
664declare void @use2(i8* %x, i8* %y);
665declare void @use3(i8* %x, i8* %y, i8* %z);
666
667declare void @use1nonnull(i8* nonnull %x);
668declare void @use2nonnull(i8* nonnull %x, i8* nonnull %y);
669declare void @use3nonnull(i8* nonnull %x, i8* nonnull %y, i8* nonnull %z);
670
671declare i8 @use1safecall(i8* %x) readonly nounwind ; readonly+nounwind guarantees that execution continues to successor
672
673; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.
674
675define void @parent1(i8* %a, i8* %b, i8* %c) {
676; CHECK-LABEL: define {{[^@]+}}@parent1
677; CHECK-SAME: (i8* [[A:%.*]], i8* [[B:%.*]], i8* [[C:%.*]])
678; CHECK-NEXT:    call void @use3(i8* [[C]], i8* [[A]], i8* [[B]])
679; CHECK-NEXT:    call void @use3nonnull(i8* nonnull [[B]], i8* nonnull [[C]], i8* nonnull [[A]])
680; CHECK-NEXT:    ret void
681;
682  call void @use3(i8* %c, i8* %a, i8* %b)
683  call void @use3nonnull(i8* %b, i8* %c, i8* %a)
684  ret void
685}
686
687; Extend non-null to parent for all arguments.
688
689define void @parent2(i8* %a, i8* %b, i8* %c) {
690; CHECK-LABEL: define {{[^@]+}}@parent2
691; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* nonnull [[B:%.*]], i8* nonnull [[C:%.*]])
692; CHECK-NEXT:    call void @use3nonnull(i8* nonnull [[B]], i8* nonnull [[C]], i8* nonnull [[A]])
693; CHECK-NEXT:    call void @use3(i8* nonnull [[C]], i8* nonnull [[A]], i8* nonnull [[B]])
694; CHECK-NEXT:    ret void
695;
696
697  call void @use3nonnull(i8* %b, i8* %c, i8* %a)
698  call void @use3(i8* %c, i8* %a, i8* %b)
699  ret void
700}
701
702; Extend non-null to parent for 1st argument.
703
704define void @parent3(i8* %a, i8* %b, i8* %c) {
705; CHECK-LABEL: define {{[^@]+}}@parent3
706; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* [[B:%.*]], i8* [[C:%.*]])
707; CHECK-NEXT:    call void @use1nonnull(i8* nonnull [[A]])
708; CHECK-NEXT:    call void @use3(i8* [[C]], i8* [[B]], i8* nonnull [[A]])
709; CHECK-NEXT:    ret void
710;
711
712
713  call void @use1nonnull(i8* %a)
714  call void @use3(i8* %c, i8* %b, i8* %a)
715  ret void
716}
717
718; Extend non-null to parent for last 2 arguments.
719
720define void @parent4(i8* %a, i8* %b, i8* %c) {
721; CHECK-LABEL: define {{[^@]+}}@parent4
722; CHECK-SAME: (i8* [[A:%.*]], i8* nonnull [[B:%.*]], i8* nonnull [[C:%.*]])
723; CHECK-NEXT:    call void @use2nonnull(i8* nonnull [[C]], i8* nonnull [[B]])
724; CHECK-NEXT:    call void @use2(i8* [[A]], i8* nonnull [[C]])
725; CHECK-NEXT:    call void @use1(i8* nonnull [[B]])
726; CHECK-NEXT:    ret void
727;
728
729
730
731  call void @use2nonnull(i8* %c, i8* %b)
732  call void @use2(i8* %a, i8* %c)
733  call void @use1(i8* %b)
734  ret void
735}
736
737; The callsite must execute in order for the attribute to transfer to the parent.
738; It appears benign to extend non-null to the parent in this case, but we can't do that
739; because it would incorrectly propagate the wrong information to its callers.
740
741define void @parent5(i8* %a, i1 %a_is_notnull) {
742; CHECK-LABEL: define {{[^@]+}}@parent5
743; CHECK-SAME: (i8* [[A:%.*]], i1 [[A_IS_NOTNULL:%.*]])
744; CHECK-NEXT:    br i1 [[A_IS_NOTNULL]], label [[T:%.*]], label [[F:%.*]]
745; CHECK:       t:
746; CHECK-NEXT:    call void @use1nonnull(i8* nonnull [[A]])
747; CHECK-NEXT:    ret void
748; CHECK:       f:
749; CHECK-NEXT:    ret void
750;
751
752  br i1 %a_is_notnull, label %t, label %f
753t:
754  call void @use1nonnull(i8* %a)
755  ret void
756f:
757  ret void
758}
759
760; The callsite must execute in order for the attribute to transfer to the parent.
761; The volatile load can't trap, so we can guarantee that we'll get to the call.
762
763define i8 @parent6(i8* %a, i8* %b) {
764; CHECK-LABEL: define {{[^@]+}}@parent6
765; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* [[B:%.*]])
766; CHECK-NEXT:    [[C:%.*]] = load volatile i8, i8* [[B]], align 1
767; CHECK-NEXT:    call void @use1nonnull(i8* nonnull [[A]])
768; CHECK-NEXT:    ret i8 [[C]]
769;
770
771  %c = load volatile i8, i8* %b
772  call void @use1nonnull(i8* %a)
773  ret i8 %c
774}
775
776; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.
777
778define i8 @parent7(i8* %a) {
779; CHECK-LABEL: define {{[^@]+}}@parent7
780; CHECK-SAME: (i8* nonnull [[A:%.*]])
781; CHECK-NEXT:    [[RET:%.*]] = call i8 @use1safecall(i8* nonnull readonly [[A]])
782; CHECK-NEXT:    call void @use1nonnull(i8* nonnull [[A]])
783; CHECK-NEXT:    ret i8 [[RET]]
784;
785
786
787  %ret = call i8 @use1safecall(i8* %a)
788  call void @use1nonnull(i8* %a)
789  ret i8 %ret
790}
791
792; Make sure that an invoke works similarly to a call.
793
794declare i32 @esfp(...)
795
796define i1 @parent8(i8* %a, i8* %bogus1, i8* %b) personality i8* bitcast (i32 (...)* @esfp to i8*){
797; CHECK: Function Attrs: nounwind
798; CHECK-LABEL: define {{[^@]+}}@parent8
799; CHECK-SAME: (i8* nonnull [[A:%.*]], i8* nocapture nofree readnone [[BOGUS1:%.*]], i8* nonnull [[B:%.*]]) [[ATTR4:#.*]] personality i8* bitcast (i32 (...)* @esfp to i8*)
800; CHECK-NEXT:  entry:
801; CHECK-NEXT:    invoke void @use2nonnull(i8* nonnull [[A]], i8* nonnull [[B]])
802; CHECK-NEXT:    to label [[CONT:%.*]] unwind label [[EXC:%.*]]
803; CHECK:       cont:
804; CHECK-NEXT:    ret i1 false
805; CHECK:       exc:
806; CHECK-NEXT:    [[LP:%.*]] = landingpad { i8*, i32 }
807; CHECK-NEXT:    filter [0 x i8*] zeroinitializer
808; CHECK-NEXT:    unreachable
809;
810
811entry:
812  invoke void @use2nonnull(i8* %a, i8* %b)
813  to label %cont unwind label %exc
814
815cont:
816  %null_check = icmp eq i8* %b, null
817  ret i1 %null_check
818
819exc:
820  %lp = landingpad { i8*, i32 }
821  filter [0 x i8*] zeroinitializer
822  unreachable
823}
824
825define i32* @gep1(i32* %p) {
826; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
827; IS__TUNIT____-LABEL: define {{[^@]+}}@gep1
828; IS__TUNIT____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
829; IS__TUNIT____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32* [[P]], i32 1
830; IS__TUNIT____-NEXT:    ret i32* [[Q]]
831;
832; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
833; IS__CGSCC____-LABEL: define {{[^@]+}}@gep1
834; IS__CGSCC____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
835; IS__CGSCC____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32* [[P]], i32 1
836; IS__CGSCC____-NEXT:    ret i32* [[Q]]
837;
838  %q = getelementptr inbounds i32, i32* %p, i32 1
839  ret i32* %q
840}
841
842define i32* @gep1_no_null_opt(i32* %p) #0 {
843; Should't be able to derive nonnull based on gep.
844; IS__TUNIT____: Function Attrs: nofree nosync nounwind null_pointer_is_valid readnone willreturn
845; IS__TUNIT____-LABEL: define {{[^@]+}}@gep1_no_null_opt
846; IS__TUNIT____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
847; IS__TUNIT____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32* [[P]], i32 1
848; IS__TUNIT____-NEXT:    ret i32* [[Q]]
849;
850; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind null_pointer_is_valid readnone willreturn
851; IS__CGSCC____-LABEL: define {{[^@]+}}@gep1_no_null_opt
852; IS__CGSCC____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
853; IS__CGSCC____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32* [[P]], i32 1
854; IS__CGSCC____-NEXT:    ret i32* [[Q]]
855;
856  %q = getelementptr inbounds i32, i32* %p, i32 1
857  ret i32* %q
858}
859
860define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
861; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
862; IS__TUNIT____-LABEL: define {{[^@]+}}@gep2
863; IS__TUNIT____-SAME: (i32 addrspace(3)* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
864; IS__TUNIT____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32 addrspace(3)* [[P]], i32 1
865; IS__TUNIT____-NEXT:    ret i32 addrspace(3)* [[Q]]
866;
867; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
868; IS__CGSCC____-LABEL: define {{[^@]+}}@gep2
869; IS__CGSCC____-SAME: (i32 addrspace(3)* nofree readnone "no-capture-maybe-returned" [[P:%.*]])
870; IS__CGSCC____-NEXT:    [[Q:%.*]] = getelementptr inbounds i32, i32 addrspace(3)* [[P]], i32 1
871; IS__CGSCC____-NEXT:    ret i32 addrspace(3)* [[Q]]
872;
873  %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
874  ret i32 addrspace(3)* %q
875}
876
877; FIXME: We should propagate dereferenceable here but *not* nonnull
878define i32 addrspace(3)* @as(i32 addrspace(3)* dereferenceable(4) %p) {
879; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
880; IS__TUNIT____-LABEL: define {{[^@]+}}@as
881; IS__TUNIT____-SAME: (i32 addrspace(3)* nofree nonnull readnone returned dereferenceable(4) "no-capture-maybe-returned" [[P:%.*]])
882; IS__TUNIT____-NEXT:    ret i32 addrspace(3)* [[P]]
883;
884; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
885; IS__CGSCC____-LABEL: define {{[^@]+}}@as
886; IS__CGSCC____-SAME: (i32 addrspace(3)* nofree nonnull readnone returned dereferenceable(4) "no-capture-maybe-returned" [[P:%.*]])
887; IS__CGSCC____-NEXT:    ret i32 addrspace(3)* [[P]]
888;
889  ret i32 addrspace(3)* %p
890}
891
892; CHECK-NOT: @g2()
893define internal i32* @g2() {
894; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
895; IS__CGSCC____-LABEL: define {{[^@]+}}@g2()
896; IS__CGSCC____-NEXT:    ret i32* inttoptr (i64 4 to i32*)
897;
898  ret i32* inttoptr (i64 4 to i32*)
899}
900
901define i32* @g1() {
902; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
903; IS__TUNIT____-LABEL: define {{[^@]+}}@g1()
904; IS__TUNIT____-NEXT:    ret i32* inttoptr (i64 4 to i32*)
905;
906; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
907; IS__CGSCC____-LABEL: define {{[^@]+}}@g1()
908; IS__CGSCC____-NEXT:    ret i32* inttoptr (i64 4 to i32*)
909;
910  %c = call i32* @g2()
911  ret i32* %c
912}
913
914declare void @use_i32_ptr(i32* readnone nocapture) nounwind
915define internal void @called_by_weak(i32* %a) {
916; IS__TUNIT____: Function Attrs: nounwind
917; IS__TUNIT____-LABEL: define {{[^@]+}}@called_by_weak
918; IS__TUNIT____-SAME: (i32* noalias nocapture nonnull readnone [[A:%.*]])
919; IS__TUNIT____-NEXT:    call void @use_i32_ptr(i32* noalias nocapture nonnull readnone [[A]])
920; IS__TUNIT____-NEXT:    ret void
921;
922; IS__CGSCC____: Function Attrs: nounwind
923; IS__CGSCC____-LABEL: define {{[^@]+}}@called_by_weak
924; IS__CGSCC____-SAME: (i32* nocapture nonnull readnone [[A:%.*]])
925; IS__CGSCC____-NEXT:    call void @use_i32_ptr(i32* noalias nocapture nonnull readnone [[A]])
926; IS__CGSCC____-NEXT:    ret void
927;
928  call void @use_i32_ptr(i32* %a)
929  ret void
930}
931
932; Check we do not annotate the function interface of this weak function.
933define weak_odr void @weak_caller(i32* nonnull %a) {
934; CHECK-LABEL: define {{[^@]+}}@weak_caller
935; CHECK-SAME: (i32* nonnull [[A:%.*]])
936; CHECK-NEXT:    call void @called_by_weak(i32* noalias nocapture nonnull readnone [[A]])
937; CHECK-NEXT:    ret void
938;
939  call void @called_by_weak(i32* %a)
940  ret void
941}
942
943; Expect nonnull
944define internal void @control(i32* dereferenceable(4) %a) {
945; IS__TUNIT____: Function Attrs: nounwind
946; IS__TUNIT____-LABEL: define {{[^@]+}}@control
947; IS__TUNIT____-SAME: (i32* noalias nocapture nonnull readnone align 16 dereferenceable(8) [[A:%.*]])
948; IS__TUNIT____-NEXT:    call void @use_i32_ptr(i32* noalias nocapture nonnull readnone align 16 dereferenceable(8) [[A]])
949; IS__TUNIT____-NEXT:    ret void
950;
951; IS__CGSCC____: Function Attrs: nounwind
952; IS__CGSCC____-LABEL: define {{[^@]+}}@control
953; IS__CGSCC____-SAME: (i32* nocapture nonnull readnone align 16 dereferenceable(8) [[A:%.*]])
954; IS__CGSCC____-NEXT:    call void @use_i32_ptr(i32* noalias nocapture nonnull readnone align 16 dereferenceable(8) [[A]])
955; IS__CGSCC____-NEXT:    ret void
956;
957  call void @use_i32_ptr(i32* %a)
958  ret void
959}
960; Avoid nonnull as we do not touch naked functions
961define internal void @naked(i32* dereferenceable(4) %a) naked {
962; CHECK: Function Attrs: naked
963; CHECK-LABEL: define {{[^@]+}}@naked
964; CHECK-SAME: (i32* dereferenceable(4) [[A:%.*]])
965; CHECK-NEXT:    call void @use_i32_ptr(i32* [[A]])
966; CHECK-NEXT:    ret void
967;
968  call void @use_i32_ptr(i32* %a)
969  ret void
970}
971; Avoid nonnull as we do not touch optnone
972define internal void @optnone(i32* dereferenceable(4) %a) optnone noinline {
973; CHECK: Function Attrs: noinline optnone
974; CHECK-LABEL: define {{[^@]+}}@optnone
975; CHECK-SAME: (i32* dereferenceable(4) [[A:%.*]])
976; CHECK-NEXT:    call void @use_i32_ptr(i32* [[A]])
977; CHECK-NEXT:    ret void
978;
979  call void @use_i32_ptr(i32* %a)
980  ret void
981}
982define void @make_live(i32* nonnull dereferenceable(8) %a) {
983; CHECK-LABEL: define {{[^@]+}}@make_live
984; CHECK-SAME: (i32* nonnull align 16 dereferenceable(8) [[A:%.*]])
985; CHECK-NEXT:    call void @naked(i32* nonnull align 16 dereferenceable(8) [[A]])
986; CHECK-NEXT:    call void @control(i32* noalias nocapture nonnull readnone align 16 dereferenceable(8) [[A]])
987; CHECK-NEXT:    call void @optnone(i32* nonnull align 16 dereferenceable(8) [[A]])
988; CHECK-NEXT:    ret void
989;
990  call void @naked(i32* nonnull dereferenceable(8) align 16 %a)
991  call void @control(i32* nonnull dereferenceable(8) align 16 %a)
992  call void @optnone(i32* nonnull dereferenceable(8) align 16 %a)
993  ret void
994}
995
996
997;int f(int *u, int n){
998;  for(int i = 0;i<n;i++){
999;    h(u);
1000;  }
1001;  return g(nonnull u);
1002;}
1003declare void @h(i32*) willreturn nounwind
1004declare i32 @g(i32*) willreturn nounwind
1005define i32 @nonnull_exec_ctx_1(i32* %a, i32 %b) {
1006;
1007; IS________OPM: Function Attrs: nounwind
1008; IS________OPM-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1
1009; IS________OPM-SAME: (i32* [[A:%.*]], i32 [[B:%.*]])
1010; IS________OPM-NEXT:  en:
1011; IS________OPM-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1012; IS________OPM-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1013; IS________OPM:       ex:
1014; IS________OPM-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1015; IS________OPM-NEXT:    ret i32 [[TMP5]]
1016; IS________OPM:       hd:
1017; IS________OPM-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD]] ], [ 0, [[EN:%.*]] ]
1018; IS________OPM-NEXT:    tail call void @h(i32* [[A]])
1019; IS________OPM-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1020; IS________OPM-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1021; IS________OPM-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1022;
1023; IS________NPM: Function Attrs: nounwind willreturn
1024; IS________NPM-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1
1025; IS________NPM-SAME: (i32* [[A:%.*]], i32 [[B:%.*]])
1026; IS________NPM-NEXT:  en:
1027; IS________NPM-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1028; IS________NPM-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1029; IS________NPM:       ex:
1030; IS________NPM-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1031; IS________NPM-NEXT:    ret i32 [[TMP5]]
1032; IS________NPM:       hd:
1033; IS________NPM-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD]] ], [ 0, [[EN:%.*]] ]
1034; IS________NPM-NEXT:    tail call void @h(i32* [[A]])
1035; IS________NPM-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1036; IS________NPM-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1037; IS________NPM-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1038;
1039en:
1040  %tmp3 = icmp eq i32 %b, 0
1041  br i1 %tmp3, label %ex, label %hd
1042
1043ex:
1044  %tmp5 = tail call i32 @g(i32* nonnull %a)
1045  ret i32 %tmp5
1046
1047hd:
1048  %tmp7 = phi i32 [ %tmp8, %hd ], [ 0, %en ]
1049  tail call void @h(i32* %a)
1050  %tmp8 = add nuw i32 %tmp7, 1
1051  %tmp9 = icmp eq i32 %tmp8, %b
1052  br i1 %tmp9, label %ex, label %hd
1053}
1054
1055define i32 @nonnull_exec_ctx_1b(i32* %a, i32 %b) {
1056;
1057; IS________OPM: Function Attrs: nounwind
1058; IS________OPM-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1b
1059; IS________OPM-SAME: (i32* [[A:%.*]], i32 [[B:%.*]])
1060; IS________OPM-NEXT:  en:
1061; IS________OPM-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1062; IS________OPM-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1063; IS________OPM:       ex:
1064; IS________OPM-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1065; IS________OPM-NEXT:    ret i32 [[TMP5]]
1066; IS________OPM:       hd:
1067; IS________OPM-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD2:%.*]] ], [ 0, [[EN:%.*]] ]
1068; IS________OPM-NEXT:    tail call void @h(i32* [[A]])
1069; IS________OPM-NEXT:    br label [[HD2]]
1070; IS________OPM:       hd2:
1071; IS________OPM-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1072; IS________OPM-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1073; IS________OPM-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1074;
1075; IS________NPM: Function Attrs: nounwind willreturn
1076; IS________NPM-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1b
1077; IS________NPM-SAME: (i32* [[A:%.*]], i32 [[B:%.*]])
1078; IS________NPM-NEXT:  en:
1079; IS________NPM-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1080; IS________NPM-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1081; IS________NPM:       ex:
1082; IS________NPM-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1083; IS________NPM-NEXT:    ret i32 [[TMP5]]
1084; IS________NPM:       hd:
1085; IS________NPM-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD2:%.*]] ], [ 0, [[EN:%.*]] ]
1086; IS________NPM-NEXT:    tail call void @h(i32* [[A]])
1087; IS________NPM-NEXT:    br label [[HD2]]
1088; IS________NPM:       hd2:
1089; IS________NPM-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1090; IS________NPM-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1091; IS________NPM-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1092;
1093en:
1094  %tmp3 = icmp eq i32 %b, 0
1095  br i1 %tmp3, label %ex, label %hd
1096
1097ex:
1098  %tmp5 = tail call i32 @g(i32* nonnull %a)
1099  ret i32 %tmp5
1100
1101hd:
1102  %tmp7 = phi i32 [ %tmp8, %hd2 ], [ 0, %en ]
1103  tail call void @h(i32* %a)
1104  br label %hd2
1105
1106hd2:
1107  %tmp8 = add nuw i32 %tmp7, 1
1108  %tmp9 = icmp eq i32 %tmp8, %b
1109  br i1 %tmp9, label %ex, label %hd
1110}
1111
1112define i32 @nonnull_exec_ctx_2(i32* %a, i32 %b) willreturn nounwind {
1113;
1114; CHECK: Function Attrs: nounwind willreturn
1115; CHECK-LABEL: define {{[^@]+}}@nonnull_exec_ctx_2
1116; CHECK-SAME: (i32* nonnull [[A:%.*]], i32 [[B:%.*]])
1117; CHECK-NEXT:  en:
1118; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1119; CHECK-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1120; CHECK:       ex:
1121; CHECK-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1122; CHECK-NEXT:    ret i32 [[TMP5]]
1123; CHECK:       hd:
1124; CHECK-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD]] ], [ 0, [[EN:%.*]] ]
1125; CHECK-NEXT:    tail call void @h(i32* nonnull [[A]])
1126; CHECK-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1127; CHECK-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1128; CHECK-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1129;
1130en:
1131  %tmp3 = icmp eq i32 %b, 0
1132  br i1 %tmp3, label %ex, label %hd
1133
1134ex:
1135  %tmp5 = tail call i32 @g(i32* nonnull %a)
1136  ret i32 %tmp5
1137
1138hd:
1139  %tmp7 = phi i32 [ %tmp8, %hd ], [ 0, %en ]
1140  tail call void @h(i32* %a)
1141  %tmp8 = add nuw i32 %tmp7, 1
1142  %tmp9 = icmp eq i32 %tmp8, %b
1143  br i1 %tmp9, label %ex, label %hd
1144}
1145
1146define i32 @nonnull_exec_ctx_2b(i32* %a, i32 %b) willreturn nounwind {
1147;
1148; CHECK: Function Attrs: nounwind willreturn
1149; CHECK-LABEL: define {{[^@]+}}@nonnull_exec_ctx_2b
1150; CHECK-SAME: (i32* nonnull [[A:%.*]], i32 [[B:%.*]])
1151; CHECK-NEXT:  en:
1152; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B]], 0
1153; CHECK-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
1154; CHECK:       ex:
1155; CHECK-NEXT:    [[TMP5:%.*]] = tail call i32 @g(i32* nonnull [[A]])
1156; CHECK-NEXT:    ret i32 [[TMP5]]
1157; CHECK:       hd:
1158; CHECK-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD2:%.*]] ], [ 0, [[EN:%.*]] ]
1159; CHECK-NEXT:    tail call void @h(i32* nonnull [[A]])
1160; CHECK-NEXT:    br label [[HD2]]
1161; CHECK:       hd2:
1162; CHECK-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
1163; CHECK-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
1164; CHECK-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
1165;
1166en:
1167  %tmp3 = icmp eq i32 %b, 0
1168  br i1 %tmp3, label %ex, label %hd
1169
1170ex:
1171  %tmp5 = tail call i32 @g(i32* nonnull %a)
1172  ret i32 %tmp5
1173
1174hd:
1175  %tmp7 = phi i32 [ %tmp8, %hd2 ], [ 0, %en ]
1176  tail call void @h(i32* %a)
1177  br label %hd2
1178
1179hd2:
1180  %tmp8 = add nuw i32 %tmp7, 1
1181  %tmp9 = icmp eq i32 %tmp8, %b
1182  br i1 %tmp9, label %ex, label %hd
1183}
1184
1185; Original from PR43833
1186declare void @sink(i32*)
1187
1188; FIXME: the sink argument should be marked nonnull as in @PR43833_simple.
1189define void @PR43833(i32* %0, i32 %1) {
1190; CHECK-LABEL: define {{[^@]+}}@PR43833
1191; CHECK-SAME: (i32* [[TMP0:%.*]], i32 [[TMP1:%.*]])
1192; CHECK-NEXT:    [[TMP3:%.*]] = icmp sgt i32 [[TMP1]], 1
1193; CHECK-NEXT:    br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]]
1194; CHECK:       4:
1195; CHECK-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP1]] to i64
1196; CHECK-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], i64 [[TMP5]]
1197; CHECK-NEXT:    br label [[TMP8:%.*]]
1198; CHECK:       7:
1199; CHECK-NEXT:    ret void
1200; CHECK:       8:
1201; CHECK-NEXT:    [[TMP9:%.*]] = phi i32 [ 1, [[TMP4]] ], [ [[TMP10:%.*]], [[TMP8]] ]
1202; CHECK-NEXT:    tail call void @sink(i32* [[TMP6]])
1203; CHECK-NEXT:    [[TMP10]] = add nuw nsw i32 [[TMP9]], 1
1204; CHECK-NEXT:    [[TMP11:%.*]] = icmp eq i32 [[TMP10]], [[TMP1]]
1205; CHECK-NEXT:    br i1 [[TMP11]], label [[TMP7]], label [[TMP8]]
1206;
1207  %3 = icmp sgt i32 %1, 1
1208  br i1 %3, label %4, label %7
1209
12104:                                                ; preds = %2
1211  %5 = zext i32 %1 to i64
1212  %6 = getelementptr inbounds i32, i32* %0, i64 %5
1213  br label %8
1214
12157:                                                ; preds = %8, %2
1216  ret void
1217
12188:                                                ; preds = %8, %4
1219  %9 = phi i32 [ 1, %4 ], [ %10, %8 ]
1220  tail call void @sink(i32* %6)
1221  %10 = add nuw nsw i32 %9, 1
1222  %11 = icmp eq i32 %10, %1
1223  br i1 %11, label %7, label %8
1224}
1225
1226; Adjusted from PR43833
1227define void @PR43833_simple(i32* %0, i32 %1) {
1228; IS________OPM-LABEL: define {{[^@]+}}@PR43833_simple
1229; IS________OPM-SAME: (i32* [[TMP0:%.*]], i32 [[TMP1:%.*]])
1230; IS________OPM-NEXT:    [[TMP3:%.*]] = icmp ne i32 [[TMP1]], 0
1231; IS________OPM-NEXT:    br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]]
1232; IS________OPM:       4:
1233; IS________OPM-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP1]] to i64
1234; IS________OPM-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], i64 [[TMP5]]
1235; IS________OPM-NEXT:    br label [[TMP8:%.*]]
1236; IS________OPM:       7:
1237; IS________OPM-NEXT:    ret void
1238; IS________OPM:       8:
1239; IS________OPM-NEXT:    [[TMP9:%.*]] = phi i32 [ 1, [[TMP4]] ], [ [[TMP10:%.*]], [[TMP8]] ]
1240; IS________OPM-NEXT:    tail call void @sink(i32* [[TMP6]])
1241; IS________OPM-NEXT:    [[TMP10]] = add nuw nsw i32 [[TMP9]], 1
1242; IS________OPM-NEXT:    [[TMP11:%.*]] = icmp eq i32 [[TMP10]], [[TMP1]]
1243; IS________OPM-NEXT:    br i1 [[TMP11]], label [[TMP7]], label [[TMP8]]
1244;
1245; IS________NPM-LABEL: define {{[^@]+}}@PR43833_simple
1246; IS________NPM-SAME: (i32* [[TMP0:%.*]], i32 [[TMP1:%.*]])
1247; IS________NPM-NEXT:    [[TMP3:%.*]] = icmp ne i32 [[TMP1]], 0
1248; IS________NPM-NEXT:    br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]]
1249; IS________NPM:       4:
1250; IS________NPM-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP1]] to i64
1251; IS________NPM-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], i64 [[TMP5]]
1252; IS________NPM-NEXT:    br label [[TMP8:%.*]]
1253; IS________NPM:       7:
1254; IS________NPM-NEXT:    ret void
1255; IS________NPM:       8:
1256; IS________NPM-NEXT:    [[TMP9:%.*]] = phi i32 [ 1, [[TMP4]] ], [ [[TMP10:%.*]], [[TMP8]] ]
1257; IS________NPM-NEXT:    tail call void @sink(i32* nonnull [[TMP6]])
1258; IS________NPM-NEXT:    [[TMP10]] = add nuw nsw i32 [[TMP9]], 1
1259; IS________NPM-NEXT:    [[TMP11:%.*]] = icmp eq i32 [[TMP10]], [[TMP1]]
1260; IS________NPM-NEXT:    br i1 [[TMP11]], label [[TMP7]], label [[TMP8]]
1261;
1262  %3 = icmp ne i32 %1, 0
1263  br i1 %3, label %4, label %7
1264
12654:                                                ; preds = %2
1266  %5 = zext i32 %1 to i64
1267  %6 = getelementptr inbounds i32, i32* %0, i64 %5
1268  br label %8
1269
12707:                                                ; preds = %8, %2
1271  ret void
1272
12738:                                                ; preds = %8, %4
1274  %9 = phi i32 [ 1, %4 ], [ %10, %8 ]
1275  tail call void @sink(i32* %6)
1276  %10 = add nuw nsw i32 %9, 1
1277  %11 = icmp eq i32 %10, %1
1278  br i1 %11, label %7, label %8
1279}
1280
1281declare i8* @strrchr(i8* %0, i32 %1) nofree nounwind readonly
1282
1283; We should not mark the return of @strrchr as `nonnull`, it may well be NULL!
1284define i8* @mybasename(i8* nofree readonly %str) {
1285; CHECK: Function Attrs: nofree nounwind readonly
1286; CHECK-LABEL: define {{[^@]+}}@mybasename
1287; CHECK-SAME: (i8* nofree readonly [[STR:%.*]])
1288; CHECK-NEXT:    [[CALL:%.*]] = call i8* @strrchr(i8* nofree readonly [[STR]], i32 47)
1289; CHECK-NEXT:    [[TOBOOL:%.*]] = icmp ne i8* [[CALL]], null
1290; CHECK-NEXT:    [[ADD_PTR:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 1
1291; CHECK-NEXT:    [[COND:%.*]] = select i1 [[TOBOOL]], i8* [[ADD_PTR]], i8* [[STR]]
1292; CHECK-NEXT:    ret i8* [[COND]]
1293;
1294  %call = call i8* @strrchr(i8* %str, i32 47)
1295  %tobool = icmp ne i8* %call, null
1296  %add.ptr = getelementptr inbounds i8, i8* %call, i64 1
1297  %cond = select i1 %tobool, i8* %add.ptr, i8* %str
1298  ret i8* %cond
1299}
1300
1301define void @nonnull_assume_pos(i8* %arg) {
1302; ATTRIBUTOR-LABEL: define {{[^@]+}}@nonnull_assume_pos
1303; ATTRIBUTOR-SAME: (i8* nocapture nofree nonnull readnone [[ARG:%.*]])
1304; ATTRIBUTOR-NEXT:    call void @llvm.assume(i1 true) #11 [ "nonnull"(i8* [[ARG]]) ]
1305; ATTRIBUTOR-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1306; ATTRIBUTOR-NEXT:    [[TMP1:%.*]] = call i8* @unknown()
1307; ATTRIBUTOR-NEXT:    ret void
1308;
1309; NOT_CGSCC_OPM-LABEL: define {{[^@]+}}@nonnull_assume_pos
1310; NOT_CGSCC_OPM-SAME: (i8* nocapture nofree nonnull readnone [[ARG:%.*]])
1311; NOT_CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR11]] [ "nonnull"(i8* [[ARG]]) ]
1312; NOT_CGSCC_OPM-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1313; NOT_CGSCC_OPM-NEXT:    [[TMP1:%.*]] = call i8* @unknown()
1314; NOT_CGSCC_OPM-NEXT:    ret void
1315;
1316; IS__CGSCC_OPM-LABEL: define {{[^@]+}}@nonnull_assume_pos
1317; IS__CGSCC_OPM-SAME: (i8* nocapture nofree nonnull readnone [[ARG:%.*]])
1318; IS__CGSCC_OPM-NEXT:    call void @llvm.assume(i1 true) [[ATTR12]] [ "nonnull"(i8* [[ARG]]) ]
1319; IS__CGSCC_OPM-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1320; IS__CGSCC_OPM-NEXT:    [[TMP1:%.*]] = call i8* @unknown()
1321; IS__CGSCC_OPM-NEXT:    ret void
1322;
1323  call void @llvm.assume(i1 true) ["nonnull"(i8* %arg)]
1324  call void @use_i8_ptr(i8* %arg)
1325  call i8* @unknown()
1326  ret void
1327}
1328define void @nonnull_assume_neg(i8* %arg) {
1329; ATTRIBUTOR-LABEL: define {{[^@]+}}@nonnull_assume_neg
1330; ATTRIBUTOR-SAME: (i8* nocapture nofree readnone [[ARG:%.*]])
1331; ATTRIBUTOR-NEXT:    [[TMP1:%.*]] = call i8* @unknown()
1332; ATTRIBUTOR-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[ARG]])
1333; ATTRIBUTOR-NEXT:    call void @llvm.assume(i1 true) [ "nonnull"(i8* [[ARG]]) ]
1334; ATTRIBUTOR-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1335; ATTRIBUTOR-NEXT:    [[TMP2:%.*]] = call i8* @unknown()
1336; ATTRIBUTOR-NEXT:    call void @use_i8_ptr_ret(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1337; ATTRIBUTOR-NEXT:    call void @llvm.assume(i1 true) [ "nonnull"(i8* [[ARG]]) ]
1338; ATTRIBUTOR-NEXT:    call void @use_i8_ptr_ret(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1339; ATTRIBUTOR-NEXT:    ret void
1340;
1341; CHECK-LABEL: define {{[^@]+}}@nonnull_assume_neg
1342; CHECK-SAME: (i8* nocapture nofree readnone [[ARG:%.*]])
1343; CHECK-NEXT:    [[TMP1:%.*]] = call i8* @unknown()
1344; CHECK-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree readnone [[ARG]])
1345; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "nonnull"(i8* [[ARG]]) ]
1346; CHECK-NEXT:    call void @use_i8_ptr(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1347; CHECK-NEXT:    [[TMP2:%.*]] = call i8* @unknown()
1348; CHECK-NEXT:    call void @use_i8_ptr_ret(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1349; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "nonnull"(i8* [[ARG]]) ]
1350; CHECK-NEXT:    call void @use_i8_ptr_ret(i8* noalias nocapture nofree nonnull readnone [[ARG]])
1351; CHECK-NEXT:    ret void
1352;
1353  call i8* @unknown()
1354  call void @use_i8_ptr(i8* %arg)
1355  call void @llvm.assume(i1 true) ["nonnull"(i8* %arg)]
1356  call void @use_i8_ptr(i8* %arg)
1357  call i8* @unknown()
1358  call void @use_i8_ptr_ret(i8* %arg)
1359  call void @llvm.assume(i1 true) ["nonnull"(i8* %arg)]
1360  call void @use_i8_ptr_ret(i8* %arg)
1361  ret void
1362}
1363declare void @use_i8_ptr(i8* nofree nocapture readnone) nounwind
1364declare void @use_i8_ptr_ret(i8* nofree nocapture readnone) nounwind willreturn
1365
1366define i8* @nonnull_function_ptr_1() {
1367; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
1368; IS__TUNIT____-LABEL: define {{[^@]+}}@nonnull_function_ptr_1()
1369; IS__TUNIT____-NEXT:    [[BC:%.*]] = bitcast i8* ()* @nonnull_function_ptr_1 to i8*
1370; IS__TUNIT____-NEXT:    ret i8* [[BC]]
1371;
1372; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1373; IS__CGSCC____-LABEL: define {{[^@]+}}@nonnull_function_ptr_1()
1374; IS__CGSCC____-NEXT:    [[BC:%.*]] = bitcast i8* ()* @nonnull_function_ptr_1 to i8*
1375; IS__CGSCC____-NEXT:    ret i8* [[BC]]
1376;
1377  %bc = bitcast i8*()* @nonnull_function_ptr_1 to i8*
1378  ret i8* %bc
1379}
1380
1381declare i8* @function_decl()
1382define i8* @nonnull_function_ptr_2() {
1383; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
1384; IS__TUNIT____-LABEL: define {{[^@]+}}@nonnull_function_ptr_2()
1385; IS__TUNIT____-NEXT:    [[BC:%.*]] = bitcast i8* ()* @function_decl to i8*
1386; IS__TUNIT____-NEXT:    ret i8* [[BC]]
1387;
1388; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
1389; IS__CGSCC____-LABEL: define {{[^@]+}}@nonnull_function_ptr_2()
1390; IS__CGSCC____-NEXT:    [[BC:%.*]] = bitcast i8* ()* @function_decl to i8*
1391; IS__CGSCC____-NEXT:    ret i8* [[BC]]
1392;
1393  %bc = bitcast i8*()* @function_decl to i8*
1394  ret i8* %bc
1395}
1396
1397attributes #0 = { null_pointer_is_valid }
1398attributes #1 = { nounwind willreturn}
1399