1; RUN: opt %loadPolly -polly-scops \
2; RUN: -analyze < %s | FileCheck %s --check-prefix=SCOP
3
4; RUN: opt %loadPolly -polly-codegen-ppcg \
5; RUN: -S -polly-acc-codegen-managed-memory \
6; RUN: -polly-acc-rewrite-managed-memory < %s | FileCheck %s --check-prefix=HOST-IR
7;
8; REQUIRES: pollyacc
9;
10; Check that we can correctly rewrite `malloc` to `polly_mallocManaged`, and
11; `free` to `polly_freeManaged` with the `polly-acc-rewrite-managed-memory`
12; pass.
13;
14; #include <memory.h>
15;
16; static const int N = 100;
17; int* f(int *ToFree) {
18;     free(ToFree);
19;     int *A = (int *)malloc(sizeof(int) * N);
20;     for(int i = 0; i < N; i++) {
21;         A[i] = 42;
22;     }
23;     return A;
24;
25; }
26
27; SCOP:      Function: f
28; SCOP-NEXT: Region: %for.body---%for.end
29; SCOP-NEXT: Max Loop Depth:  1
30
31; SCOP:      Arrays {
32; SCOP-NEXT:     i32 MemRef_call[*]; // Element size 4
33; SCOP-NEXT: }
34
35; // Check that polly_mallocManaged is declared and used correctly.
36; HOST-IR: %call = tail call i8* @polly_mallocManaged(i64 400)
37; HOST-IR: declare i8* @polly_mallocManaged(i64)
38
39; // Check that polly_freeManaged is declared and used correctly.
40; HOST-IR  %toFreeBitcast = bitcast i32* %toFree to i8*
41; HOST-IR  call void @polly_freeManaged(i8* %toFreeBitcast)
42; HOST-IR: declare void @polly_freeManaged(i8*)
43
44; // Check that we remove the original malloc,free
45; HOST-IR-NOT: declare i8* @malloc(i64)
46; HOST-IR-NOT: declare void @free(i8*)
47
48target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
49target triple = "x86_64-apple-macosx10.12.0"
50
51define i32* @f(i32 *%toFree) {
52entry:
53  %toFreeBitcast = bitcast i32* %toFree to i8*
54  call void @free(i8* %toFreeBitcast)
55  br label %entry.split
56
57entry.split:                                      ; preds = %entry
58  %call = tail call i8* @malloc(i64 400)
59  %tmp = bitcast i8* %call to i32*
60  br label %for.body
61
62for.body:                                         ; preds = %entry.split, %for.body
63  %indvars.iv1 = phi i64 [ 0, %entry.split ], [ %indvars.iv.next, %for.body ]
64  %arrayidx = getelementptr inbounds i32, i32* %tmp, i64 %indvars.iv1
65  store i32 42, i32* %arrayidx, align 4, !tbaa !3
66  %indvars.iv.next = add nuw nsw i64 %indvars.iv1, 1
67  %exitcond = icmp eq i64 %indvars.iv.next, 100
68  br i1 %exitcond, label %for.end, label %for.body
69
70for.end:                                          ; preds = %for.body
71  ret i32* %tmp
72}
73
74; Function Attrs: argmemonly nounwind
75declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #0
76
77declare i8* @malloc(i64)
78declare void @free(i8*)
79
80; Function Attrs: argmemonly nounwind
81declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #0
82
83attributes #0 = { argmemonly nounwind }
84
85!llvm.module.flags = !{!0, !1}
86!llvm.ident = !{!2}
87
88!0 = !{i32 1, !"wchar_size", i32 4}
89!1 = !{i32 7, !"PIC Level", i32 2}
90!2 = !{!"clang version 6.0.0"}
91!3 = !{!4, !4, i64 0}
92!4 = !{!"int", !5, i64 0}
93!5 = !{!"omnipotent char", !6, i64 0}
94!6 = !{!"Simple C/C++ TBAA"}
95