File: nonnull.ll

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (781 lines) | stat: -rw-r--r-- 22,398 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=function-attrs -enable-nonnull-arg-prop %s | FileCheck %s --check-prefixes=FNATTR

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

declare nonnull ptr @ret_nonnull()

; Return a pointer trivially nonnull (call return attribute)
define ptr @test1() {
; FNATTR: define nonnull ptr @test1
  %ret = call ptr @ret_nonnull()
  ret ptr %ret
}

; Return a pointer trivially nonnull (argument attribute)
define ptr @test2(ptr nonnull %p) {
; FNATTR: define nonnull ptr @test2
  ret ptr %p
}

; Given an SCC where one of the functions can not be marked nonnull,
; can we still mark the other one which is trivially nonnull
define ptr @scc_binder(i1 %c) {
; FNATTR: define ptr @scc_binder
  br i1 %c, label %rec, label %end
rec:
  call ptr @test3(i1 %c)
  br label %end
end:
  ret ptr null
}

define ptr @test3(i1 %c) {
; FNATTR: define nonnull ptr @test3
  call ptr @scc_binder(i1 %c)
  %ret = call ptr @ret_nonnull()
  ret ptr %ret
}

; Given a mutual recursive set of functions, we can mark them
; nonnull if neither can ever return null.  (In this case, they
; just never return period.)
define ptr @test4_helper() {
; FNATTR: define noalias nonnull ptr @test4_helper
  %ret = call ptr @test4()
  ret ptr %ret
}

define ptr @test4() {
; FNATTR: define noalias nonnull ptr @test4
  %ret = call ptr @test4_helper()
  ret ptr %ret
}

; Given a mutual recursive set of functions which *can* return null
; make sure we haven't marked them as nonnull.
define ptr @test5_helper(i1 %c) {
; FNATTR: define noalias ptr @test5_helper
  br i1 %c, label %rec, label %end
rec:
  %ret = call ptr @test5(i1 %c)
  br label %end
end:
  ret ptr null
}

define ptr @test5(i1 %c) {
; FNATTR: define noalias ptr @test5
  %ret = call ptr @test5_helper(i1 %c)
  ret ptr %ret
}

; Local analysis, but going through a self recursive phi
define ptr @test6a() {
entry:
  %ret = call ptr @ret_nonnull()
  br label %loop
loop:
  %phi = phi ptr [%ret, %entry], [%phi, %loop]
  br i1 undef, label %loop, label %exit
exit:
  ret ptr %phi
}

define ptr @test6b(i1 %c) {
entry:
  %ret = call ptr @ret_nonnull()
  br label %loop
loop:
  %phi = phi ptr [%ret, %entry], [%phi, %loop]
  br i1 %c, label %loop, label %exit
exit:
  ret ptr %phi
}

; FNATTR: define ptr @test7
define ptr @test7(ptr %a) {
  ret ptr %a
}

; FNATTR: define nonnull ptr @test8
define ptr @test8(ptr %a) {
  %b = getelementptr inbounds i8, ptr %a, i64 1
  ret ptr %b
}

; FNATTR: define ptr @test9
define ptr @test9(ptr %a, i64 %n) {
  %b = getelementptr inbounds i8, ptr %a, i64 %n
  ret ptr %b
}

declare void @llvm.assume(i1)
; FNATTR: define ptr @test10
; FIXME: missing nonnull
define ptr @test10(ptr %a, i64 %n) {
  %cmp = icmp ne i64 %n, 0
  call void @llvm.assume(i1 %cmp)
  %b = getelementptr inbounds i8, ptr %a, i64 %n
  ret ptr %b
}

; TEST 11
; char* test11(char *p) {
;   return p? p: nonnull();
; }
; FNATTR: define ptr @test11
; FIXME: missing nonnull
define ptr @test11(ptr) local_unnamed_addr {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %5

; <label>:3:                                      ; preds = %1
  %4 = tail call ptr @ret_nonnull()
  br label %5

; <label>:5:                                      ; preds = %3, %1
  %6 = phi ptr [ %4, %3 ], [ %0, %1 ]
  ret ptr %6
}

; TEST 12
; Simple CallSite Test
declare void @test12_helper(ptr)
define void @test12(ptr nonnull %a) {
  tail call void @test12_helper(ptr %a)
  ret void
}

; TEST 13
; Simple Argument Tests
declare ptr @unknown()
define void @test13_helper() {
  %nonnullptr = tail call ptr @ret_nonnull()
  %maybenullptr = tail call ptr @unknown()
  tail call void @test13(ptr %nonnullptr, ptr %nonnullptr, ptr %maybenullptr)
  tail call void @test13(ptr %nonnullptr, ptr %maybenullptr, ptr %nonnullptr)
  ret void
}
define internal void @test13(ptr %a, ptr %b, ptr %c) {
  ret void
}

declare nonnull ptr @nonnull()

; TEST 14
; Complex propagation
; Argument of f1, f2, f3 can be marked with nonnull.

; * Argument
; 1. In f1:bb6, %arg can be marked with nonnull because of the comparison in bb1
; 2. Because f2 is internal function, f2(ptr %arg) -> @f2(ptr nonnull %arg)
; 3. In f1:bb4 %tmp5 is nonnull and f3 is internal function.
;    Then, f3(ptr %arg) -> @f3(ptr nonnull %arg)
; 4. We get nonnull in whole f1 call sites so f1(ptr %arg) -> @f1(ptr nonnull %arg)


define internal ptr @f1(ptr %arg) {
; FIXME: missing nonnull It should be nonnull @f1(ptr nonnull readonly %arg)

bb:
  %tmp = icmp eq ptr %arg, null
  br i1 %tmp, label %bb9, label %bb1

bb1:                                              ; preds = %bb
  %tmp2 = load i32, ptr %arg, align 4
  %tmp3 = icmp eq i32 %tmp2, 0
  br i1 %tmp3, label %bb6, label %bb4

bb4:                                              ; preds = %bb1
  %tmp5 = getelementptr inbounds i32, ptr %arg, i64 1
  %tmp5b = tail call ptr @f3(ptr %tmp5)
  %tmp5c = getelementptr inbounds i32, ptr %tmp5b, i64 -1
  br label %bb9

bb6:                                              ; preds = %bb1
; FIXME: missing nonnull. It should be @f2(ptr nonnull %arg)
  %tmp7 = tail call ptr @f2(ptr %arg)
  ret ptr %tmp7

bb9:                                              ; preds = %bb4, %bb
  %tmp10 = phi ptr [ %tmp5c, %bb4 ], [ inttoptr (i64 4 to ptr), %bb ]
  ret ptr %tmp10
}

define internal ptr @f2(ptr %arg) {
; FIXME: missing nonnull. It should be nonnull @f2(ptr nonnull %arg)
bb:

; FIXME: missing nonnull. It should be @f1(ptr nonnull readonly %arg)
  %tmp = tail call ptr @f1(ptr %arg)
  ret ptr %tmp
}

define dso_local noalias ptr @f3(ptr %arg) {
; FIXME: missing nonnull. It should be nonnull @f3(ptr nonnull readonly %arg)
bb:
; FIXME: missing nonnull. It should be @f1(ptr nonnull readonly %arg)
  %tmp = call ptr @f1(ptr %arg)
  ret ptr %tmp
}

; TEST 15
define void @f15(ptr %arg) {

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

declare void @fun0() #1
declare void @fun1(ptr) #1
declare void @fun2(ptr, ptr) #1
declare void @fun3(ptr, ptr, ptr) #1
; TEST 16 simple path test
; if(..)
;   fun2(nonnull %a, nonnull %b)
; else
;   fun2(nonnull %a, %b)
; We can say that %a is nonnull but %b is not.
define void @f16(ptr %a, ptr %b, i8 %c) {
; FIXME: missing nonnull on %a
  %cmp = icmp eq i8 %c, 0
  br i1 %cmp, label %if.then, label %if.else
if.then:
  tail call void @fun2(ptr nonnull %a, ptr nonnull %b)
  ret void
if.else:
  tail call void @fun2(ptr nonnull %a, ptr %b)
  ret void
}
; TEST 17 explore child BB test
; if(..)
;    ... (willreturn & nounwind)
; else
;    ... (willreturn & nounwind)
; fun1(nonnull %a)
; We can say that %a is nonnull
define void @f17(ptr %a, i8 %c) {
  %cmp = icmp eq i8 %c, 0
  br i1 %cmp, label %if.then, label %if.else
if.then:
  tail call void @fun0()
  br label %cont
if.else:
  tail call void @fun0()
  br label %cont
cont:
  tail call void @fun1(ptr nonnull %a)
  ret void
}
; TEST 18 More complex test
; if(..)
;    ... (willreturn & nounwind)
; else
;    ... (willreturn & nounwind)
; if(..)
;    ... (willreturn & nounwind)
; else
;    ... (willreturn & nounwind)
; fun1(nonnull %a)

define void @f18(ptr %a, ptr %b, i8 %c) {
  %cmp1 = icmp eq i8 %c, 0
  br i1 %cmp1, label %if.then, label %if.else
if.then:
  tail call void @fun0()
  br label %cont
if.else:
  tail call void @fun0()
  br label %cont
cont:
  %cmp2 = icmp eq i8 %c, 1
  br i1 %cmp2, label %cont.then, label %cont.else
cont.then:
  tail call void @fun1(ptr nonnull %b)
  br label %cont2
cont.else:
  tail call void @fun0()
  br label %cont2
cont2:
  tail call void @fun1(ptr nonnull %a)
  ret void
}

; TEST 19: Loop

define void @f19(ptr %a, ptr %b, i8 %c) {
; FIXME: missing nonnull on %b
  br label %loop.header
loop.header:
  %cmp2 = icmp eq i8 %c, 0
  br i1 %cmp2, label %loop.body, label %loop.exit
loop.body:
  tail call void @fun1(ptr nonnull %b)
  tail call void @fun1(ptr nonnull %a)
  br label %loop.header
loop.exit:
  tail call void @fun1(ptr nonnull %b)
  ret void
}

; Test propagation of nonnull callsite args back to caller.

declare void @use1(ptr %x)
declare void @use2(ptr %x, ptr %y);
declare void @use3(ptr %x, ptr %y, ptr %z);

declare void @use1nonnull(ptr nonnull noundef %x);
declare void @use1nonnull_without_noundef(ptr nonnull %x);
declare void @use2nonnull(ptr nonnull noundef %x, ptr nonnull noundef %y);
declare void @use3nonnull(ptr nonnull noundef %x, ptr nonnull noundef %y, ptr nonnull noundef %z);

declare i8 @use1safecall(ptr %x) nounwind willreturn ; nounwind+willreturn guarantees that execution continues to successor

; Without noundef, nonnull cannot be propagated to the parent

define void @parent_poison(ptr %a) {
; FNATTR-LABEL: @parent_poison(ptr %a)
  call void @use1nonnull_without_noundef(ptr %a)
  ret void
}

; Can't extend non-null to parent for any argument because the 2nd call is not guaranteed to execute.

define void @parent1(ptr %a, ptr %b, ptr %c) {
; FNATTR-LABEL: @parent1(ptr %a, ptr %b, ptr %c)
; FNATTR-NEXT:    call void @use3(ptr %c, ptr %a, ptr %b)
; FNATTR-NEXT:    call void @use3nonnull(ptr %b, ptr %c, ptr %a)
; FNATTR-NEXT:    ret void
  call void @use3(ptr %c, ptr %a, ptr %b)
  call void @use3nonnull(ptr %b, ptr %c, ptr %a)
  ret void
}

; Extend non-null to parent for all arguments.

define void @parent2(ptr %a, ptr %b, ptr %c) {
; FNATTR-LABEL: @parent2(ptr nonnull %a, ptr nonnull %b, ptr nonnull %c)
; FNATTR-NEXT:    call void @use3nonnull(ptr %b, ptr %c, ptr %a)
; FNATTR-NEXT:    call void @use3(ptr %c, ptr %a, ptr %b)


; FNATTR-NEXT:    ret void
  call void @use3nonnull(ptr %b, ptr %c, ptr %a)
  call void @use3(ptr %c, ptr %a, ptr %b)
  ret void
}

; Extend non-null to parent for 1st argument.

define void @parent3(ptr %a, ptr %b, ptr %c) {
; FNATTR-LABEL: @parent3(ptr nonnull %a, ptr %b, ptr %c)
; FNATTR-NEXT:    call void @use1nonnull(ptr %a)
; FNATTR-NEXT:    call void @use3(ptr %c, ptr %b, ptr %a)


; FNATTR-NEXT:  ret void

  call void @use1nonnull(ptr %a)
  call void @use3(ptr %c, ptr %b, ptr %a)
  ret void
}

; Extend non-null to parent for last 2 arguments.

define void @parent4(ptr %a, ptr %b, ptr %c) {
; CHECK-LABEL: @parent4(ptr %a, ptr nonnull %b, ptr nonnull %c)
; CHECK-NEXT:    call void @use2nonnull(ptr %c, ptr %b)
; CHECK-NEXT:    call void @use2(ptr %a, ptr %c)
; CHECK-NEXT:    call void @use1(ptr %b)


; FNATTR: ret void

  call void @use2nonnull(ptr %c, ptr %b)
  call void @use2(ptr %a, ptr %c)
  call void @use1(ptr %b)
  ret void
}

; The callsite must execute in order for the attribute to transfer to the parent.
; It appears benign to extend non-null to the parent in this case, but we can't do that
; because it would incorrectly propagate the wrong information to its callers.

define void @parent5(ptr %a, i1 %a_is_notnull) {
; FNATTR: @parent5(ptr %a, i1 %a_is_notnull)
; FNATTR-NEXT:    br i1 %a_is_notnull, label %t, label %f
; FNATTR:       t:
; FNATTR-NEXT:    call void @use1nonnull(ptr %a)
; FNATTR-NEXT:    ret void
; FNATTR:       f:
; FNATTR-NEXT:    ret void

  br i1 %a_is_notnull, label %t, label %f
t:
  call void @use1nonnull(ptr %a)
  ret void
f:
  ret void
}

; The callsite must execute in order for the attribute to transfer to the parent.
; The volatile load can't trap, so we can guarantee that we'll get to the call.

define i8 @parent6(ptr %a, ptr %b) {
; FNATTR-LABEL: @parent6(ptr nonnull %a, ptr %b)
; FNATTR-NEXT:    [[C:%.*]] = load volatile i8, ptr %b
; FNATTR-NEXT:    call void @use1nonnull(ptr %a)
; FNATTR-NEXT:    ret i8 [[C]]

  %c = load volatile i8, ptr %b
  call void @use1nonnull(ptr %a)
  ret i8 %c
}

; The nonnull callsite is guaranteed to execute, so the argument must be nonnull throughout the parent.

define i8 @parent7(ptr %a) {
; FNATTR-LABEL: @parent7(ptr nonnull %a)
; FNATTR-NEXT:    [[RET:%.*]] = call i8 @use1safecall(ptr %a)
; FNATTR-NEXT:    call void @use1nonnull(ptr %a)



; FNATTR-NEXT: ret i8 [[RET]]

  %ret = call i8 @use1safecall(ptr %a)
  call void @use1nonnull(ptr %a)
  ret i8 %ret
}

; Make sure that an invoke works similarly to a call.

declare i32 @esfp(...)

define i1 @parent8(ptr %a, ptr %bogus1, ptr %b) personality ptr @esfp{
; FNATTR-LABEL: @parent8(ptr nonnull %a, ptr nocapture readnone %bogus1, ptr nonnull %b)
; FNATTR-NEXT:  entry:
; FNATTR-NEXT:    invoke void @use2nonnull(ptr %a, ptr %b)
; FNATTR-NEXT:    to label %cont unwind label %exc
; FNATTR:       cont:
; FNATTR-NEXT:    [[NULL_CHECK:%.*]] = icmp eq ptr %b, null
; FNATTR-NEXT:    ret i1 [[NULL_CHECK]]
; FNATTR:       exc:
; FNATTR-NEXT:    [[LP:%.*]] = landingpad { ptr, i32 }
; FNATTR-NEXT:    filter [0 x ptr] zeroinitializer
; FNATTR-NEXT:    unreachable

entry:
  invoke void @use2nonnull(ptr %a, ptr %b)
  to label %cont unwind label %exc

cont:
  %null_check = icmp eq ptr %b, null
  ret i1 %null_check

exc:
  %lp = landingpad { ptr, i32 }
  filter [0 x ptr] zeroinitializer
  unreachable
}

; FNATTR: define nonnull ptr @gep1(
define ptr @gep1(ptr %p) {
  %q = getelementptr inbounds i32, ptr %p, i32 1
  ret ptr %q
}

define ptr @gep1_no_null_opt(ptr %p) #0 {
; Should't be able to derive nonnull based on gep.
; FNATTR: define ptr @gep1_no_null_opt(
  %q = getelementptr inbounds i32, ptr %p, i32 1
  ret ptr %q
}

; FNATTR: define ptr addrspace(3) @gep2(
define ptr addrspace(3) @gep2(ptr addrspace(3) %p) {
  %q = getelementptr inbounds i32, ptr addrspace(3) %p, i32 1
  ret ptr addrspace(3) %q
}

; FNATTR:     define ptr addrspace(3) @as(ptr addrspace(3) readnone returned dereferenceable(4) %p)
; FIXME: We should propagate dereferenceable here but *not* nonnull
define ptr addrspace(3) @as(ptr addrspace(3) dereferenceable(4) %p) {
  ret ptr addrspace(3) %p
}

; FNATTR: define internal nonnull ptr @g2()
define internal ptr @g2() {
  ret ptr inttoptr (i64 4 to ptr)
}

define  ptr @g1() {
 %c = call ptr @g2()
  ret ptr %c
}

declare void @use_i32_ptr(ptr) readnone nounwind
define internal void @called_by_weak(ptr %a) {
  call void @use_i32_ptr(ptr %a)
  ret void
}

; Check we do not annotate the function interface of this weak function.
define weak_odr void @weak_caller(ptr nonnull %a) {
  call void @called_by_weak(ptr %a)
  ret void
}

; Expect nonnull
define internal void @control(ptr dereferenceable(4) %a) {
  call void @use_i32_ptr(ptr %a)
  ret void
}
; Avoid nonnull as we do not touch naked functions
define internal void @naked(ptr dereferenceable(4) %a) naked {
  call void @use_i32_ptr(ptr %a)
  ret void
}
; Avoid nonnull as we do not touch optnone
define internal void @optnone(ptr dereferenceable(4) %a) optnone noinline {
  call void @use_i32_ptr(ptr %a)
  ret void
}
define void @make_live(ptr nonnull dereferenceable(8) %a) {
  call void @naked(ptr nonnull dereferenceable(8) align 16 %a)
  call void @control(ptr nonnull dereferenceable(8) align 16 %a)
  call void @optnone(ptr nonnull dereferenceable(8) align 16 %a)
  ret void
}

;int f(int *u, int n){
;  for(int i = 0;i<n;i++){
;    h(u);
;  }
;  return g(nonnull u);
;}
declare void @h(ptr) willreturn nounwind
declare i32 @g(ptr) willreturn nounwind
define i32 @nonnull_exec_ctx_1(ptr %a, i32 %b) {
; FNATTR-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1
; FNATTR-SAME: (ptr [[A:%.*]], i32 [[B:%.*]])
; FNATTR-NEXT:  en:
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B:%.*]], 0
; FNATTR-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
; FNATTR:       ex:
; FNATTR-NEXT:    [[TMP5:%.*]] = tail call i32 @g(ptr nonnull [[A:%.*]])
; FNATTR-NEXT:    ret i32 [[TMP5]]
; FNATTR:       hd:
; FNATTR-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD]] ], [ 0, [[EN:%.*]] ]
; FNATTR-NEXT:    tail call void @h(ptr [[A]])
; FNATTR-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
; FNATTR-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
; FNATTR-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
;
;
en:
  %tmp3 = icmp eq i32 %b, 0
  br i1 %tmp3, label %ex, label %hd

ex:
  %tmp5 = tail call i32 @g(ptr nonnull %a)
  ret i32 %tmp5

hd:
  %tmp7 = phi i32 [ %tmp8, %hd ], [ 0, %en ]
  tail call void @h(ptr %a)
  %tmp8 = add nuw i32 %tmp7, 1
  %tmp9 = icmp eq i32 %tmp8, %b
  br i1 %tmp9, label %ex, label %hd
}

define i32 @nonnull_exec_ctx_1b(ptr %a, i32 %b) {
; FNATTR-LABEL: define {{[^@]+}}@nonnull_exec_ctx_1b
; FNATTR-SAME: (ptr [[A:%.*]], i32 [[B:%.*]])
; FNATTR-NEXT:  en:
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B:%.*]], 0
; FNATTR-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
; FNATTR:       ex:
; FNATTR-NEXT:    [[TMP5:%.*]] = tail call i32 @g(ptr nonnull [[A:%.*]])
; FNATTR-NEXT:    ret i32 [[TMP5]]
; FNATTR:       hd:
; FNATTR-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD2:%.*]] ], [ 0, [[EN:%.*]] ]
; FNATTR-NEXT:    tail call void @h(ptr [[A]])
; FNATTR-NEXT:    br label [[HD2]]
; FNATTR:       hd2:
; FNATTR-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
; FNATTR-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
; FNATTR-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
;
;
en:
  %tmp3 = icmp eq i32 %b, 0
  br i1 %tmp3, label %ex, label %hd

ex:
  %tmp5 = tail call i32 @g(ptr nonnull %a)
  ret i32 %tmp5

hd:
  %tmp7 = phi i32 [ %tmp8, %hd2 ], [ 0, %en ]
  tail call void @h(ptr %a)
  br label %hd2

hd2:
  %tmp8 = add nuw i32 %tmp7, 1
  %tmp9 = icmp eq i32 %tmp8, %b
  br i1 %tmp9, label %ex, label %hd
}

define i32 @nonnull_exec_ctx_2(ptr %a, i32 %b) willreturn nounwind {
; FNATTR-LABEL: define {{[^@]+}}@nonnull_exec_ctx_2
; FNATTR-SAME: (ptr [[A:%.*]], i32 [[B:%.*]])
; FNATTR-NEXT:  en:
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B:%.*]], 0
; FNATTR-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
; FNATTR:       ex:
; FNATTR-NEXT:    [[TMP5:%.*]] = tail call i32 @g(ptr nonnull [[A:%.*]])
; FNATTR-NEXT:    ret i32 [[TMP5]]
; FNATTR:       hd:
; FNATTR-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD]] ], [ 0, [[EN:%.*]] ]
; FNATTR-NEXT:    tail call void @h(ptr [[A]])
; FNATTR-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
; FNATTR-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
; FNATTR-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
;
;
en:
  %tmp3 = icmp eq i32 %b, 0
  br i1 %tmp3, label %ex, label %hd

ex:
  %tmp5 = tail call i32 @g(ptr nonnull %a)
  ret i32 %tmp5

hd:
  %tmp7 = phi i32 [ %tmp8, %hd ], [ 0, %en ]
  tail call void @h(ptr %a)
  %tmp8 = add nuw i32 %tmp7, 1
  %tmp9 = icmp eq i32 %tmp8, %b
  br i1 %tmp9, label %ex, label %hd
}

define i32 @nonnull_exec_ctx_2b(ptr %a, i32 %b) willreturn nounwind {
; FNATTR-LABEL: define {{[^@]+}}@nonnull_exec_ctx_2b
; FNATTR-SAME: (ptr [[A:%.*]], i32 [[B:%.*]])
; FNATTR-NEXT:  en:
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[B:%.*]], 0
; FNATTR-NEXT:    br i1 [[TMP3]], label [[EX:%.*]], label [[HD:%.*]]
; FNATTR:       ex:
; FNATTR-NEXT:    [[TMP5:%.*]] = tail call i32 @g(ptr nonnull [[A:%.*]])
; FNATTR-NEXT:    ret i32 [[TMP5]]
; FNATTR:       hd:
; FNATTR-NEXT:    [[TMP7:%.*]] = phi i32 [ [[TMP8:%.*]], [[HD2:%.*]] ], [ 0, [[EN:%.*]] ]
; FNATTR-NEXT:    tail call void @h(ptr [[A]])
; FNATTR-NEXT:    br label [[HD2]]
; FNATTR:       hd2:
; FNATTR-NEXT:    [[TMP8]] = add nuw i32 [[TMP7]], 1
; FNATTR-NEXT:    [[TMP9:%.*]] = icmp eq i32 [[TMP8]], [[B]]
; FNATTR-NEXT:    br i1 [[TMP9]], label [[EX]], label [[HD]]
;
;
en:
  %tmp3 = icmp eq i32 %b, 0
  br i1 %tmp3, label %ex, label %hd

ex:
  %tmp5 = tail call i32 @g(ptr nonnull %a)
  ret i32 %tmp5

hd:
  %tmp7 = phi i32 [ %tmp8, %hd2 ], [ 0, %en ]
  tail call void @h(ptr %a)
  br label %hd2

hd2:
  %tmp8 = add nuw i32 %tmp7, 1
  %tmp9 = icmp eq i32 %tmp8, %b
  br i1 %tmp9, label %ex, label %hd
}

; Original from PR43833
declare void @sink(ptr)

; FIXME: the sink argument should be marked nonnull as in @PR43833_simple.
define void @PR43833(ptr %0, i32 %1) {
; FNATTR-LABEL: @PR43833(
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp sgt i32 [[TMP1:%.*]], 1
; FNATTR-NEXT:    br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]]
; FNATTR:       4:
; FNATTR-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP1]] to i64
; FNATTR-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[TMP0:%.*]], i64 [[TMP5]]
; FNATTR-NEXT:    br label [[TMP8:%.*]]
; FNATTR:       7:
; FNATTR-NEXT:    ret void
; FNATTR:       8:
; FNATTR-NEXT:    [[TMP9:%.*]] = phi i32 [ 1, [[TMP4]] ], [ [[TMP10:%.*]], [[TMP8]] ]
; FNATTR-NEXT:    tail call void @sink(ptr [[TMP6]])
; FNATTR-NEXT:    [[TMP10]] = add nuw nsw i32 [[TMP9]], 1
; FNATTR-NEXT:    [[TMP11:%.*]] = icmp eq i32 [[TMP10]], [[TMP1]]
; FNATTR-NEXT:    br i1 [[TMP11]], label [[TMP7]], label [[TMP8]]
;
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = zext i32 %1 to i64
  %6 = getelementptr inbounds i32, ptr %0, i64 %5
  br label %8

7:                                                ; preds = %8, %2
  ret void

8:                                                ; preds = %8, %4
  %9 = phi i32 [ 1, %4 ], [ %10, %8 ]
  tail call void @sink(ptr %6)
  %10 = add nuw nsw i32 %9, 1
  %11 = icmp eq i32 %10, %1
  br i1 %11, label %7, label %8
}

; Adjusted from PR43833
define void @PR43833_simple(ptr %0, i32 %1) {
; FNATTR-LABEL: @PR43833_simple(
; FNATTR-NEXT:    [[TMP3:%.*]] = icmp ne i32 [[TMP1:%.*]], 0
; FNATTR-NEXT:    br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]]
; FNATTR:       4:
; FNATTR-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP1]] to i64
; FNATTR-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[TMP0:%.*]], i64 [[TMP5]]
; FNATTR-NEXT:    br label [[TMP8:%.*]]
; FNATTR:       7:
; FNATTR-NEXT:    ret void
; FNATTR:       8:
; FNATTR-NEXT:    [[TMP9:%.*]] = phi i32 [ 1, [[TMP4]] ], [ [[TMP10:%.*]], [[TMP8]] ]
; FNATTR-NEXT:    tail call void @sink(ptr [[TMP6]])
; FNATTR-NEXT:    [[TMP10]] = add nuw nsw i32 [[TMP9]], 1
; FNATTR-NEXT:    [[TMP11:%.*]] = icmp eq i32 [[TMP10]], [[TMP1]]
; FNATTR-NEXT:    br i1 [[TMP11]], label [[TMP7]], label [[TMP8]]
;
;
  %3 = icmp ne i32 %1, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = zext i32 %1 to i64
  %6 = getelementptr inbounds i32, ptr %0, i64 %5
  br label %8

7:                                                ; preds = %8, %2
  ret void

8:                                                ; preds = %8, %4
  %9 = phi i32 [ 1, %4 ], [ %10, %8 ]
  tail call void @sink(ptr %6)
  %10 = add nuw nsw i32 %9, 1
  %11 = icmp eq i32 %10, %1
  br i1 %11, label %7, label %8
}

attributes #0 = { null_pointer_is_valid }
attributes #1 = { nounwind willreturn}