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