[RISCV] Simplify usage of SplatPat_simm5_plus1. NFC (#125340)
[llvm-project.git] / clang / test / CodeGenCXX / pragma-gcc-unroll.cpp
blob85f10fcdff14dd9f708055177a87d11a02c06497
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
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
6 // Verify while loop is recognized after unroll pragma.
7 void while_test(int *List, int Length) {
8 // CHECK: define {{.*}} @_Z10while_test
9 int i = 0;
11 #pragma GCC unroll
12 while (i < Length) {
13 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
14 List[i] = i * 2;
15 i++;
19 // Verify do loop is recognized after multi-option pragma clang loop directive.
20 void do_test(int *List, int Length) {
21 // CHECK: define {{.*}} @_Z7do_test
22 int i = 0;
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);
32 // Verify for loop is recognized after unroll pragma.
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;
42 // Verify c++11 for range loop is recognized after unroll pragma.
43 void for_range_test() {
44 // CHECK: define {{.*}} @_Z14for_range_test
45 double List[100];
47 #pragma GCC unroll(4)
48 for (int i : List) {
49 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
50 List[i] = i;
54 #define UNROLLCOUNT 8
56 // Verify defines are correctly resolved in unroll pragmas.
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;
66 // Verify metadata is generated when template is used.
67 template <typename A>
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;
77 // Verify define is resolved correctly when template is used.
78 template <typename A>
79 void for_template_define_test(A *List, int Length, A Value) {
80 // CHECK: define {{.*}} @_Z24for_template_define_test
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;
89 #undef UNROLLCOUNT
91 // Use templates defined above. Test verifies metadata is generated correctly.
92 void template_test(double *List, int Length) {
93 double Value = 10;
95 for_template_test<double>(List, Length, Value);
96 for_template_define_test<double>(List, Length, Value);
99 void for_unroll_zero_test(int *List, int Length) {
100 // CHECK: define {{.*}} @_Z20for_unroll_zero_testPii
101 #pragma GCC unroll 0
102 for (int i = 0; i < Length; i++) {
103 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_14:.*]]
104 List[i] = i * 2;
108 void while_unroll_zero_test(int *List, int Length) {
109 // CHECK: define {{.*}} @_Z22while_unroll_zero_testPii
110 int i = 0;
111 #pragma GCC unroll(0)
112 while (i < Length) {
113 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_15:.*]]
114 List[i] = i * 2;
115 i++;
119 using size_t = unsigned long long;
121 template <bool Flag>
122 int value_dependent(int n) {
123 // CHECK: define {{.*}} @_Z15value_dependentILb1EEii
124 constexpr int N = 100;
125 auto init = [=]() { return Flag ? n : 0UL; };
126 auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
127 auto iter = [=](size_t ix) {
128 return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
130 #pragma GCC unroll Flag ? 1 : N
131 for (size_t ix = init(); cond(ix); ix = iter(ix)) {
132 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]
133 n *= n;
135 #pragma GCC unroll Flag ? 0 : N
136 for (size_t ix = init(); cond(ix); ix = iter(ix)) {
137 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]
138 n *= n;
140 return n;
143 void test_value_dependent(int n) {
144 value_dependent<true>(n);
147 // CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], [[MP:![0-9]+]], ![[UNROLL_ENABLE:.*]]}
148 // CHECK: ![[UNROLL_ENABLE]] = !{!"llvm.loop.unroll.enable"}
149 // CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]]}
150 // CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
151 // CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], [[MP]], ![[UNROLL_8:.*]]}
152 // CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
153 // CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[UNROLL_4:.*]]}
154 // CHECK: ![[UNROLL_4]] = !{!"llvm.loop.unroll.count", i32 4}
155 // CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_8:.*]]}
156 // CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[UNROLL_8:.*]]}
157 // CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[UNROLL_8:.*]]}
158 // CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], [[MP]], ![[UNROLL_DISABLE:.*]]}
159 // CHECK: ![[LOOP_15]] = distinct !{![[LOOP_15]], [[MP]], ![[UNROLL_DISABLE:.*]]}
160 // CHECK: ![[LOOP_16]] = distinct !{![[LOOP_16]], [[MP]], ![[UNROLL_DISABLE:.*]]}
161 // CHECK: ![[LOOP_17]] = distinct !{![[LOOP_17]], [[MP]], ![[UNROLL_DISABLE:.*]]}