1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
2 
3 // Verify while loop is recognized after unroll pragma.
while_test(int * List,int Length)4 void while_test(int *List, int Length) {
5   // CHECK: define {{.*}} @_Z10while_test
6   int i = 0;
7 
8 #pragma unroll
9   while (i < Length) {
10     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
11     List[i] = i * 2;
12     i++;
13   }
14 }
15 
16 // Verify do loop is recognized after multi-option pragma clang loop directive.
do_test(int * List,int Length)17 void do_test(int *List, int Length) {
18   int i = 0;
19 
20 #pragma nounroll
21   do {
22     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
23     List[i] = i * 2;
24     i++;
25   } while (i < Length);
26 }
27 
28 // Verify for loop is recognized after unroll pragma.
for_test(int * List,int Length)29 void for_test(int *List, int Length) {
30 #pragma unroll 8
31   for (int i = 0; i < Length; i++) {
32     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
33     List[i] = i * 2;
34   }
35 }
36 
37 // Verify c++11 for range loop is recognized after unroll pragma.
for_range_test()38 void for_range_test() {
39   double List[100];
40 
41 #pragma unroll(4)
42   for (int i : List) {
43     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
44     List[i] = i;
45   }
46 }
47 
48 #define UNROLLCOUNT 8
49 
50 // Verify defines are correctly resolved in unroll pragmas.
for_define_test(int * List,int Length,int Value)51 void for_define_test(int *List, int Length, int Value) {
52 #pragma unroll(UNROLLCOUNT)
53   for (int i = 0; i < Length; i++) {
54     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
55     List[i] = i * Value;
56   }
57 }
58 
59 // Verify metadata is generated when template is used.
60 template <typename A>
for_template_test(A * List,int Length,A Value)61 void for_template_test(A *List, int Length, A Value) {
62 #pragma unroll 8
63   for (int i = 0; i < Length; i++) {
64     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
65     List[i] = i * Value;
66   }
67 }
68 
69 // Verify define is resolved correctly when template is used.
70 template <typename A>
for_template_define_test(A * List,int Length,A Value)71 void for_template_define_test(A *List, int Length, A Value) {
72 #pragma unroll(UNROLLCOUNT)
73   for (int i = 0; i < Length; i++) {
74     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_7:.*]]
75     List[i] = i * Value;
76   }
77 }
78 
79 #undef UNROLLCOUNT
80 
81 // Use templates defined above. Test verifies metadata is generated correctly.
template_test(double * List,int Length)82 void template_test(double *List, int Length) {
83   double Value = 10;
84 
85   for_template_test<double>(List, Length, Value);
86   for_template_define_test<double>(List, Length, Value);
87 }
88 
89 // CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[UNROLL_FULL:.*]]}
90 // CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
91 // CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]]}
92 // CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
93 // CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[UNROLL_8:.*]]}
94 // CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
95 // CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[UNROLL_4:.*]]}
96 // CHECK: ![[UNROLL_4]] = !{!"llvm.loop.unroll.count", i32 4}
97 // CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_8:.*]]}
98 // CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[UNROLL_8:.*]]}
99 // CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[UNROLL_8:.*]]}
100