| 12
 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
 
 | ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal  -attributor-annotate-decl-cs  -S < %s | FileCheck %s --check-prefixes=CHECK,TUNIT
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal  -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,CGSCC
; FIXME: amdgpu doesn't claim malloc is a thing, so the test is somewhat
; useless except the __kmpc_alloc_shared part which now also covers the important
; part this test was initially designed for, make sure the "is freed" check is
; not sufficient on a GPU.
target triple = "amdgcn-amd-amdhsa"
target datalayout = "A5"
declare noalias ptr @malloc(i64)
declare void @nocapture_func_frees_pointer(ptr nocapture)
declare void @func_throws(...)
declare void @sync_func(ptr %p)
declare void @sync_will_return(ptr %p) willreturn nounwind
declare void @no_sync_func(ptr nocapture %p) nofree nosync willreturn
declare void @nofree_func(ptr nocapture %p) nofree  nosync willreturn
declare void @usei8(ptr %p)
declare void @foo(ptr %p)
declare void @foo_nounw(ptr %p) nounwind nofree
declare i32 @no_return_call() noreturn
declare void @free(ptr nocapture)
declare void @llvm.lifetime.start.p0(i64, ptr nocapture) nounwind
;.
; CHECK: @[[G:[a-zA-Z0-9_$"\\.-]+]] = internal global ptr undef, align 4
; CHECK: @[[GTL:[a-zA-Z0-9_$"\\.-]+]] = internal thread_local global ptr undef, align 4
;.
define void @nofree_arg_only(ptr %p1, ptr %p2) {
; CHECK-LABEL: define {{[^@]+}}@nofree_arg_only
; CHECK-SAME: (ptr nocapture nofree [[P1:%.*]], ptr nocapture [[P2:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    tail call void @free(ptr nocapture [[P2]])
; CHECK-NEXT:    tail call void @nofree_func(ptr nocapture nofree [[P1]])
; CHECK-NEXT:    ret void
;
bb:
  tail call void @free(ptr %p2)
  tail call void @nofree_func(ptr %p1)
  ret void
}
; TEST 1 - negative, pointer freed in another function.
define void @test1() {
; CHECK-LABEL: define {{[^@]+}}@test1() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @nocapture_func_frees_pointer(ptr noalias nocapture [[I]])
; CHECK-NEXT:    tail call void (...) @func_throws()
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @nocapture_func_frees_pointer(ptr %i)
  tail call void (...) @func_throws()
  tail call void @free(ptr %i)
  ret void
}
; TEST 2 - negative, call to a sync function.
define void @test2() {
; CHECK-LABEL: define {{[^@]+}}@test2() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @sync_func(ptr [[I]])
; CHECK-NEXT:    tail call void @free(ptr nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
; TEST 3 - 1 malloc, 1 free
define void @test3() {
; CHECK-LABEL: define {{[^@]+}}@test3() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
define void @test3a(ptr %p) {
; CHECK-LABEL: define {{[^@]+}}@test3a
; CHECK-SAME: (ptr nocapture [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @nofree_arg_only(ptr noalias nocapture nofree [[I]], ptr nocapture [[P]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @nofree_arg_only(ptr %i, ptr %p)
  tail call void @free(ptr %i)
  ret void
}
declare noalias ptr @aligned_alloc(i64, i64)
define void @test3b(ptr %p) {
; CHECK-LABEL: define {{[^@]+}}@test3b
; CHECK-SAME: (ptr nocapture [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @aligned_alloc(i64 noundef 32, i64 noundef 128)
; CHECK-NEXT:    tail call void @nofree_arg_only(ptr noalias nocapture nofree [[I]], ptr nocapture [[P]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @aligned_alloc(i64 32, i64 128)
  tail call void @nofree_arg_only(ptr %i, ptr %p)
  tail call void @free(ptr %i)
  ret void
}
; leave alone non-constant alignments.
define void @test3c(i64 %alignment) {
; CHECK-LABEL: define {{[^@]+}}@test3c
; CHECK-SAME: (i64 [[ALIGNMENT:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @aligned_alloc(i64 [[ALIGNMENT]], i64 noundef 128)
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @aligned_alloc(i64 %alignment, i64 128)
  tail call void @free(ptr %i)
  ret void
}
declare noalias ptr @calloc(i64, i64)
define void @test0() {
; CHECK-LABEL: define {{[^@]+}}@test0() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @calloc(i64 noundef 2, i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @calloc(i64 2, i64 4)
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
; TEST 4
define void @test4() {
; CHECK-LABEL: define {{[^@]+}}@test4() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @nofree_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @nofree_func(ptr %i)
  ret void
}
; TEST 5 - not all exit paths have a call to free, but all uses of malloc
; are in nofree functions and are not captured
define void @test5(i32 %arg, ptr %p) {
; CHECK-LABEL: define {{[^@]+}}@test5
; CHECK-SAME: (i32 [[ARG:%.*]], ptr nocapture [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    [[I1:%.*]] = icmp eq i32 [[ARG]], 0
; CHECK-NEXT:    br i1 [[I1]], label [[BB3:%.*]], label [[BB2:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    tail call void @nofree_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    br label [[BB4:%.*]]
; CHECK:       bb3:
; CHECK-NEXT:    tail call void @nofree_arg_only(ptr noalias nocapture nofree [[I]], ptr nocapture [[P]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    br label [[BB4]]
; CHECK:       bb4:
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  %i1 = icmp eq i32 %arg, 0
  br i1 %i1, label %bb3, label %bb2
bb2:
  tail call void @nofree_func(ptr %i)
  br label %bb4
bb3:
  tail call void @nofree_arg_only(ptr %i, ptr %p)
  tail call void @free(ptr %i)
  br label %bb4
bb4:
  ret void
}
; TEST 6 - all exit paths have a call to free
define void @test6(i32 %arg) {
; CHECK-LABEL: define {{[^@]+}}@test6
; CHECK-SAME: (i32 [[ARG:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    [[I1:%.*]] = icmp eq i32 [[ARG]], 0
; CHECK-NEXT:    br i1 [[I1]], label [[BB3:%.*]], label [[BB2:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    tail call void @nofree_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    br label [[BB4:%.*]]
; CHECK:       bb3:
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    br label [[BB4]]
; CHECK:       bb4:
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  %i1 = icmp eq i32 %arg, 0
  br i1 %i1, label %bb3, label %bb2
bb2:
  tail call void @nofree_func(ptr %i)
  tail call void @free(ptr %i)
  br label %bb4
bb3:
  tail call void @free(ptr %i)
  br label %bb4
bb4:
  ret void
}
; TEST 7 - free is dead.
define void @test7() {
; CHECK-LABEL: define {{[^@]+}}@test7() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    [[I1:%.*]] = tail call i32 @no_return_call() #[[ATTR3:[0-9]+]]
; CHECK-NEXT:    unreachable
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  %i1 = tail call i32 @no_return_call()
  tail call void @free(ptr %i)
  ret void
}
; TEST 8 - Negative: bitcast pointer used in capture function
define void @test8() {
; CHECK-LABEL: define {{[^@]+}}@test8() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @foo(ptr nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    tail call void @free(ptr nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @foo(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
; TEST 9 - FIXME: malloc should be converted.
define void @test9() {
; CHECK-LABEL: define {{[^@]+}}@test9() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @foo_nounw(ptr nofree nonnull align 4 dereferenceable(4) [[I]]) #[[ATTR6:[0-9]+]]
; CHECK-NEXT:    tail call void @free(ptr nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @foo_nounw(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
; TEST 10 - 1 malloc, 1 free
define i32 @test10() {
; CHECK-LABEL: define {{[^@]+}}@test10() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret i32 [[I2]]
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @free(ptr %i)
  ret i32 %i2
}
define i32 @test_lifetime() {
; CHECK-LABEL: define {{[^@]+}}@test_lifetime() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    call void @llvm.lifetime.start.p0(i64 noundef 4, ptr noalias nocapture nofree nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret i32 [[I2]]
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @no_sync_func(ptr %i)
  call void @llvm.lifetime.start.p0(i64 4, ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @free(ptr %i)
  ret i32 %i2
}
; TEST 11
define void @test11() {
; CHECK-LABEL: define {{[^@]+}}@test11() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    tail call void @sync_will_return(ptr [[I]]) #[[ATTR6]]
; CHECK-NEXT:    tail call void @free(ptr nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  tail call void @sync_will_return(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
; TEST 12
define i32 @irreducible_cfg(i32 %arg) {
; CHECK-LABEL: define {{[^@]+}}@irreducible_cfg
; CHECK-SAME: (i32 [[ARG:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = icmp eq i32 [[ARG]], 1
; CHECK-NEXT:    br i1 [[I2]], label [[BB3:%.*]], label [[BB5:%.*]]
; CHECK:       bb3:
; CHECK-NEXT:    [[I4:%.*]] = add nsw i32 [[ARG]], 5
; CHECK-NEXT:    br label [[BB11:%.*]]
; CHECK:       bb5:
; CHECK-NEXT:    br label [[BB6:%.*]]
; CHECK:       bb6:
; CHECK-NEXT:    [[DOT0:%.*]] = phi i32 [ [[I12:%.*]], [[BB11]] ], [ 1, [[BB5]] ]
; CHECK-NEXT:    [[I7:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    [[I8:%.*]] = add nsw i32 [[I7]], -1
; CHECK-NEXT:    store i32 [[I8]], ptr [[I]], align 4
; CHECK-NEXT:    [[I9:%.*]] = icmp ne i32 [[I7]], 0
; CHECK-NEXT:    br i1 [[I9]], label [[BB10:%.*]], label [[BB13:%.*]]
; CHECK:       bb10:
; CHECK-NEXT:    br label [[BB11]]
; CHECK:       bb11:
; CHECK-NEXT:    [[DOT1:%.*]] = phi i32 [ [[I4]], [[BB3]] ], [ [[DOT0]], [[BB10]] ]
; CHECK-NEXT:    [[I12]] = add nsw i32 [[DOT1]], 1
; CHECK-NEXT:    br label [[BB6]]
; CHECK:       bb13:
; CHECK-NEXT:    call void @free(ptr noalias nocapture noundef nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    [[I16:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    ret i32 [[I16]]
;
bb:
  %i = call noalias ptr @malloc(i64 4)
  store i32 10, ptr %i, align 4
  %i2 = icmp eq i32 %arg, 1
  br i1 %i2, label %bb3, label %bb5
bb3:
  %i4 = add nsw i32 %arg, 5
  br label %bb11
bb5:
  br label %bb6
bb6:
  %.0 = phi i32 [ %i12, %bb11 ], [ 1, %bb5 ]
  %i7 = load i32, ptr %i, align 4
  %i8 = add nsw i32 %i7, -1
  store i32 %i8, ptr %i, align 4
  %i9 = icmp ne i32 %i7, 0
  br i1 %i9, label %bb10, label %bb13
bb10:
  br label %bb11
bb11:
  %.1 = phi i32 [ %i4, %bb3 ], [ %.0, %bb10 ]
  %i12 = add nsw i32 %.1, 1
  br label %bb6
bb13:
  %i14 = load i32, ptr %i, align 4
  call void @free(ptr %i)
  %i16 = load i32, ptr %i, align 4
  ret i32 %i16
}
define i32 @malloc_in_loop(i32 %arg) {
; CHECK-LABEL: define {{[^@]+}}@malloc_in_loop
; CHECK-SAME: (i32 [[ARG:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = alloca i32, align 4
; CHECK-NEXT:    [[I1:%.*]] = alloca ptr, align 8
; CHECK-NEXT:    [[I11:%.*]] = alloca i8, i32 0, align 8
; CHECK-NEXT:    store i32 [[ARG]], ptr [[I]], align 4
; CHECK-NEXT:    br label [[BB2:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    [[I3:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    [[I4:%.*]] = add nsw i32 [[I3]], -1
; CHECK-NEXT:    store i32 [[I4]], ptr [[I]], align 4
; CHECK-NEXT:    [[I5:%.*]] = icmp sgt i32 [[I4]], 0
; CHECK-NEXT:    br i1 [[I5]], label [[BB6:%.*]], label [[BB9:%.*]]
; CHECK:       bb6:
; CHECK-NEXT:    [[I7:%.*]] = call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    br label [[BB2]]
; CHECK:       bb9:
; CHECK-NEXT:    ret i32 5
;
bb:
  %i = alloca i32, align 4
  %i1 = alloca ptr, align 8
  store i32 %arg, ptr %i, align 4
  br label %bb2
bb2:
  %i3 = load i32, ptr %i, align 4
  %i4 = add nsw i32 %i3, -1
  store i32 %i4, ptr %i, align 4
  %i5 = icmp sgt i32 %i4, 0
  br i1 %i5, label %bb6, label %bb9
bb6:
  %i7 = call noalias ptr @malloc(i64 4)
  store i32 1, ptr %i7, align 8
  br label %bb2
bb9:
  ret i32 5
}
; Malloc/Calloc too large
define i32 @test13() {
; CHECK-LABEL: define {{[^@]+}}@test13() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 256)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret i32 [[I2]]
;
bb:
  %i = tail call noalias ptr @malloc(i64 256)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @free(ptr %i)
  ret i32 %i2
}
define i32 @test_sle() {
; CHECK-LABEL: define {{[^@]+}}@test_sle() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef -1)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret i32 [[I2]]
;
bb:
  %i = tail call noalias ptr @malloc(i64 -1)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @free(ptr %i)
  ret i32 %i2
}
define i32 @test_overflow() {
; CHECK-LABEL: define {{[^@]+}}@test_overflow() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @calloc(i64 noundef 65537, i64 noundef 65537)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    store i32 10, ptr [[I]], align 4
; CHECK-NEXT:    [[I2:%.*]] = load i32, ptr [[I]], align 4
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture nonnull align 4 dereferenceable(4) [[I]])
; CHECK-NEXT:    ret i32 [[I2]]
;
bb:
  %i = tail call noalias ptr @calloc(i64 65537, i64 65537)
  tail call void @no_sync_func(ptr %i)
  store i32 10, ptr %i, align 4
  %i2 = load i32, ptr %i, align 4
  tail call void @free(ptr %i)
  ret i32 %i2
}
define void @test14() {
; CHECK-LABEL: define {{[^@]+}}@test14() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @calloc(i64 noundef 64, i64 noundef 4)
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @calloc(i64 64, i64 4)
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
define void @test15(i64 %S) {
; CHECK-LABEL: define {{[^@]+}}@test15
; CHECK-SAME: (i64 [[S:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 [[S]])
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 %S)
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
define void @test16a(i8 %v, ptr %P) {
; CHECK-LABEL: define {{[^@]+}}@test16a
; CHECK-SAME: (i8 [[V:%.*]], ptr nocapture nofree readnone [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    store i8 [[V]], ptr [[I]], align 1
; CHECK-NEXT:    tail call void @no_sync_func(ptr noalias nocapture nofree noundef nonnull dereferenceable(1) [[I]])
; CHECK-NEXT:    tail call void @free(ptr noalias nocapture noundef nonnull dereferenceable(1) [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  store i8 %v, ptr %i, align 1
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr nonnull dereferenceable(1) %i)
  ret void
}
define void @test16b(i8 %v, ptr %P) {
; CHECK-LABEL: define {{[^@]+}}@test16b
; CHECK-SAME: (i8 [[V:%.*]], ptr nocapture nofree writeonly [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    store ptr [[I]], ptr [[P]], align 8
; CHECK-NEXT:    tail call void @no_sync_func(ptr nocapture nofree [[I]])
; CHECK-NEXT:    tail call void @free(ptr nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  store ptr %i, ptr %P, align 8
  tail call void @no_sync_func(ptr %i)
  tail call void @free(ptr %i)
  ret void
}
define void @test16c(i8 %v, ptr %P) {
; CHECK-LABEL: define {{[^@]+}}@test16c
; CHECK-SAME: (i8 [[V:%.*]], ptr nocapture nofree writeonly [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    store ptr [[I]], ptr [[P]], align 8
; CHECK-NEXT:    tail call void @no_sync_func(ptr nocapture nofree [[I]]) #[[ATTR6]]
; CHECK-NEXT:    tail call void @free(ptr nocapture [[I]])
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  store ptr %i, ptr %P, align 8
  tail call void @no_sync_func(ptr %i) nounwind
  tail call void @free(ptr %i)
  ret void
}
define void @test16d(i8 %v, ptr %P) {
; CHECK-LABEL: define {{[^@]+}}@test16d
; CHECK-SAME: (i8 [[V:%.*]], ptr nocapture nofree writeonly [[P:%.*]]) {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @malloc(i64 noundef 4)
; CHECK-NEXT:    store ptr [[I]], ptr [[P]], align 8
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @malloc(i64 4)
  store ptr %i, ptr %P, align 8
  ret void
}
declare ptr @__kmpc_alloc_shared(i64)
declare void @__kmpc_free_shared(ptr nocapture, i64)
define void @test17() {
; CHECK-LABEL: define {{[^@]+}}@test17() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I_H2S:%.*]] = alloca i8, i64 4, align 1, addrspace(5)
; CHECK-NEXT:    [[MALLOC_CAST:%.*]] = addrspacecast ptr addrspace(5) [[I_H2S]] to ptr
; CHECK-NEXT:    tail call void @usei8(ptr noalias nocapture nofree [[MALLOC_CAST]]) #[[ATTR7:[0-9]+]]
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 4)
  tail call void @usei8(ptr nocapture nofree %i) nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 4)
  ret void
}
define void @test17b() {
; CHECK-LABEL: define {{[^@]+}}@test17b() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @__kmpc_alloc_shared(i64 noundef 4)
; CHECK-NEXT:    tail call void @usei8(ptr nofree [[I]]) #[[ATTR7]]
; CHECK-NEXT:    tail call void @__kmpc_free_shared(ptr nocapture [[I]], i64 noundef 4)
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 4)
  tail call void @usei8(ptr nofree %i) nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 4)
  ret void
}
define void @move_alloca() {
; CHECK-LABEL: define {{[^@]+}}@move_alloca() {
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[I_H2S:%.*]] = alloca i8, i64 4, align 1, addrspace(5)
; CHECK-NEXT:    br label [[NOT_ENTRY:%.*]]
; CHECK:       not_entry:
; CHECK-NEXT:    [[MALLOC_CAST:%.*]] = addrspacecast ptr addrspace(5) [[I_H2S]] to ptr
; CHECK-NEXT:    tail call void @usei8(ptr noalias nocapture nofree [[MALLOC_CAST]]) #[[ATTR7]]
; CHECK-NEXT:    ret void
;
entry:
  br label %not_entry
not_entry:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 4)
  tail call void @usei8(ptr nocapture nofree %i) nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 4)
  ret void
}
@G = internal global ptr undef, align 4
define void @test16e(i8 %v) norecurse {
; CHECK: Function Attrs: norecurse
; CHECK-LABEL: define {{[^@]+}}@test16e
; CHECK-SAME: (i8 [[V:%.*]]) #[[ATTR5:[0-9]+]] {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I:%.*]] = tail call noalias ptr @__kmpc_alloc_shared(i64 noundef 4)
; CHECK-NEXT:    store ptr [[I]], ptr @G, align 8
; CHECK-NEXT:    call void @usei8(ptr nocapture nofree [[I]]) #[[ATTR8:[0-9]+]]
; CHECK-NEXT:    tail call void @__kmpc_free_shared(ptr noalias nocapture [[I]], i64 noundef 4)
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 4)
  store ptr %i, ptr @G, align 8
  %i1 = load ptr, ptr @G, align 8
  call void @usei8(ptr nocapture nofree %i1) nocallback nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 4)
  ret void
}
@Gtl = internal thread_local global ptr undef, align 4
define void @test16f(i8 %v) norecurse {
; CHECK: Function Attrs: norecurse
; CHECK-LABEL: define {{[^@]+}}@test16f
; CHECK-SAME: (i8 [[V:%.*]]) #[[ATTR5]] {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I_H2S:%.*]] = alloca i8, i64 4, align 1, addrspace(5)
; CHECK-NEXT:    [[MALLOC_CAST:%.*]] = addrspacecast ptr addrspace(5) [[I_H2S]] to ptr
; CHECK-NEXT:    store ptr [[MALLOC_CAST]], ptr @Gtl, align 8
; CHECK-NEXT:    call void @usei8(ptr nocapture nofree [[MALLOC_CAST]]) #[[ATTR8]]
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 4)
  store ptr %i, ptr @Gtl, align 8
  %i1 = load ptr, ptr @Gtl, align 8
  call void @usei8(ptr nocapture nofree %i1) nocallback nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 4)
  ret void
}
define void @convert_large_kmpc_alloc_shared() {
; CHECK-LABEL: define {{[^@]+}}@convert_large_kmpc_alloc_shared() {
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I_H2S:%.*]] = alloca i8, i64 256, align 1, addrspace(5)
; CHECK-NEXT:    [[MALLOC_CAST:%.*]] = addrspacecast ptr addrspace(5) [[I_H2S]] to ptr
; CHECK-NEXT:    tail call void @usei8(ptr noalias nocapture nofree [[MALLOC_CAST]]) #[[ATTR7]]
; CHECK-NEXT:    ret void
;
bb:
  %i = tail call noalias ptr @__kmpc_alloc_shared(i64 256)
  tail call void @usei8(ptr nocapture nofree %i) nosync nounwind willreturn
  tail call void @__kmpc_free_shared(ptr %i, i64 256)
  ret void
}
;.
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind willreturn }
; CHECK: attributes #[[ATTR1:[0-9]+]] = { nofree nosync willreturn }
; CHECK: attributes #[[ATTR2:[0-9]+]] = { nofree nounwind }
; CHECK: attributes #[[ATTR3]] = { noreturn }
; CHECK: attributes #[[ATTR4:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
; CHECK: attributes #[[ATTR5]] = { norecurse }
; CHECK: attributes #[[ATTR6]] = { nounwind }
; CHECK: attributes #[[ATTR7]] = { nosync nounwind willreturn }
; CHECK: attributes #[[ATTR8]] = { nocallback nosync nounwind willreturn }
;.
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; CGSCC: {{.*}}
; TUNIT: {{.*}}
 |