xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/captured-statements.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm %s -o %t
2f4a2713aSLionel Sambuc // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-GLOBALS
3f4a2713aSLionel Sambuc // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-1
4f4a2713aSLionel Sambuc // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-2
5f4a2713aSLionel Sambuc // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-3
6f4a2713aSLionel Sambuc 
7*0a6a1f1dSLionel Sambuc typedef __INTPTR_TYPE__ intptr_t;
8*0a6a1f1dSLionel Sambuc 
9f4a2713aSLionel Sambuc int foo();
10f4a2713aSLionel Sambuc int global;
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc // Single statement
test1()13f4a2713aSLionel Sambuc void test1() {
14f4a2713aSLionel Sambuc   int i = 0;
15f4a2713aSLionel Sambuc   #pragma clang __debug captured
16f4a2713aSLionel Sambuc   {
17f4a2713aSLionel Sambuc     i++;
18f4a2713aSLionel Sambuc   }
19f4a2713aSLionel Sambuc   // CHECK-1: %struct.anon = type { i32* }
20f4a2713aSLionel Sambuc   //
21f4a2713aSLionel Sambuc   // CHECK-1: test1
22f4a2713aSLionel Sambuc   // CHECK-1: alloca %struct.anon
23f4a2713aSLionel Sambuc   // CHECK-1: getelementptr inbounds %struct.anon*
24f4a2713aSLionel Sambuc   // CHECK-1: store i32* %i
25f4a2713aSLionel Sambuc   // CHECK-1: call void @[[HelperName:__captured_stmt[0-9]+]]
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc // CHECK-1: define internal void @[[HelperName]](%struct.anon
29f4a2713aSLionel Sambuc // CHECK-1:   getelementptr inbounds %struct.anon{{.*}}, i32 0, i32 0
30f4a2713aSLionel Sambuc // CHECK-1:   load i32**
31f4a2713aSLionel Sambuc // CHECK-1:   load i32*
32f4a2713aSLionel Sambuc // CHECK-1:   add nsw i32
33f4a2713aSLionel Sambuc // CHECK-1:   store i32
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc // Compound statement with local variable
test2(int x)36f4a2713aSLionel Sambuc void test2(int x) {
37f4a2713aSLionel Sambuc   #pragma clang __debug captured
38f4a2713aSLionel Sambuc   {
39f4a2713aSLionel Sambuc     int i;
40f4a2713aSLionel Sambuc     for (i = 0; i < x; i++)
41f4a2713aSLionel Sambuc       foo();
42f4a2713aSLionel Sambuc   }
43f4a2713aSLionel Sambuc   // CHECK-2: test2
44f4a2713aSLionel Sambuc   // CHECK-2-NOT: %i
45f4a2713aSLionel Sambuc   // CHECK-2: call void @[[HelperName:__captured_stmt[0-9]+]]
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc // CHECK-2: define internal void @[[HelperName]]
49f4a2713aSLionel Sambuc // CHECK-2-NOT: }
50f4a2713aSLionel Sambuc // CHECK-2:   %i = alloca i32
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc // Capture array
test3(int size)53*0a6a1f1dSLionel Sambuc void test3(int size) {
54f4a2713aSLionel Sambuc   int arr[] = {1, 2, 3, 4, 5};
55*0a6a1f1dSLionel Sambuc   int vla_arr[size];
56f4a2713aSLionel Sambuc   #pragma clang __debug captured
57f4a2713aSLionel Sambuc   {
58*0a6a1f1dSLionel Sambuc     arr[2] = vla_arr[size - 1];
59f4a2713aSLionel Sambuc   }
60f4a2713aSLionel Sambuc   // CHECK-3: test3
61f4a2713aSLionel Sambuc   // CHECK-3: alloca [5 x i32]
62f4a2713aSLionel Sambuc   // CHECK-3: call void @__captured_stmt
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc // Capture VLA array
test4(intptr_t size,intptr_t vla_arr[size])66*0a6a1f1dSLionel Sambuc void test4(intptr_t size, intptr_t vla_arr[size]) {
67*0a6a1f1dSLionel Sambuc   #pragma clang __debug captured
68*0a6a1f1dSLionel Sambuc   {
69*0a6a1f1dSLionel Sambuc     vla_arr[0] = 1;
70*0a6a1f1dSLionel Sambuc   }
71*0a6a1f1dSLionel Sambuc   // CHECK-3: test4([[INTPTR_T:i.+]] {{.*}}[[SIZE_ARG:%.+]], [[INTPTR_T]]*
72*0a6a1f1dSLionel Sambuc   // CHECK-3: store [[INTPTR_T]] {{.*}}[[SIZE_ARG]], [[INTPTR_T]]* [[SIZE_ADDR:%.+]],
73*0a6a1f1dSLionel Sambuc   // CHECK-3: [[SIZE:%.+]] = load [[INTPTR_T]]* [[SIZE_ADDR]],
74*0a6a1f1dSLionel Sambuc   // CHECK-3: [[REF:%.+]] = getelementptr inbounds
75*0a6a1f1dSLionel Sambuc   // CHECK-3: store [[INTPTR_T]] [[SIZE]], [[INTPTR_T]]* [[REF]]
76*0a6a1f1dSLionel Sambuc   // CHECK-3: call void @__captured_stmt
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc 
dont_capture_global()79f4a2713aSLionel Sambuc void dont_capture_global() {
80f4a2713aSLionel Sambuc   static int s;
81f4a2713aSLionel Sambuc   extern int e;
82f4a2713aSLionel Sambuc   #pragma clang __debug captured
83f4a2713aSLionel Sambuc   {
84f4a2713aSLionel Sambuc     global++;
85f4a2713aSLionel Sambuc     s++;
86f4a2713aSLionel Sambuc     e++;
87f4a2713aSLionel Sambuc   }
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   // CHECK-GLOBALS: %[[Capture:struct\.anon[\.0-9]*]] = type {}
90f4a2713aSLionel Sambuc   // CHECK-GLOBALS: call void @__captured_stmt[[HelperName:[0-9]+]](%[[Capture]]
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc // CHECK-GLOBALS: define internal void @__captured_stmt[[HelperName]]
94f4a2713aSLionel Sambuc // CHECK-GLOBALS-NOT: ret
95f4a2713aSLionel Sambuc // CHECK-GLOBALS:   load i32* @global
96f4a2713aSLionel Sambuc // CHECK-GLOBALS:   load i32* @
97f4a2713aSLionel Sambuc // CHECK-GLOBALS:   load i32* @e
98