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