Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 1 | ========================== |
| 2 | Auto-Vectorization in LLVM |
| 3 | ========================== |
| 4 | |
Sean Silva | 99e12f9 | 2012-12-20 22:42:20 +0000 | [diff] [blame] | 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | LLVM has two vectorizers: The :ref:`Loop Vectorizer <loop-vectorizer>`, |
| 9 | which operates on Loops, and the :ref:`Basic Block Vectorizer |
| 10 | <bb-vectorizer>`, which optimizes straight-line code. These vectorizers |
| 11 | focus on different optimization opportunities and use different techniques. |
| 12 | The BB vectorizer merges multiple scalars that are found in the code into |
| 13 | vectors while the Loop Vectorizer widens instructions in the original loop |
| 14 | to operate on multiple consecutive loop iterations. |
| 15 | |
| 16 | .. _loop-vectorizer: |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 17 | |
| 18 | The Loop Vectorizer |
| 19 | =================== |
| 20 | |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 21 | Usage |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 22 | ----- |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 23 | |
Sean Silva | 13ed79c | 2012-12-20 02:23:25 +0000 | [diff] [blame] | 24 | LLVM's Loop Vectorizer is now available and will be useful for many people. |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 25 | It is not enabled by default, but can be enabled through clang using the |
| 26 | command line flag: |
| 27 | |
| 28 | .. code-block:: console |
| 29 | |
Nadav Rotem | 8f4a6cc | 2012-12-19 18:02:36 +0000 | [diff] [blame] | 30 | $ clang -fvectorize -O3 file.c |
| 31 | |
| 32 | If the ``-fvectorize`` flag is used then the loop vectorizer will be enabled |
| 33 | when running with ``-O3``, ``-O2``. When ``-Os`` is used, the loop vectorizer |
| 34 | will only vectorize loops that do not require a major increase in code size. |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 35 | |
| 36 | We plan to enable the Loop Vectorizer by default as part of the LLVM 3.3 release. |
| 37 | |
Nadav Rotem | 7daadf2 | 2013-01-04 17:49:45 +0000 | [diff] [blame] | 38 | Command line flags |
| 39 | ^^^^^^^^^^^^^^^^^^ |
| 40 | |
| 41 | The loop vectorizer uses a cost model to decide on the optimal vectorization factor |
| 42 | and unroll factor. However, users of the vectorizer can force the vectorizer to use |
| 43 | specific values. Both 'clang' and 'opt' support the flags below. |
| 44 | |
| 45 | Users can control the vectorization SIMD width using the command line flag "-force-vector-width". |
| 46 | |
| 47 | .. code-block:: console |
| 48 | |
| 49 | $ clang -mllvm -force-vector-width=8 ... |
| 50 | $ opt -loop-vectorize -force-vector-width=8 ... |
| 51 | |
| 52 | Users can control the unroll factor using the command line flag "-force-vector-unroll" |
| 53 | |
| 54 | .. code-block:: console |
| 55 | |
| 56 | $ clang -mllvm -force-vector-unroll=2 ... |
| 57 | $ opt -loop-vectorize -force-vector-unroll=2 ... |
| 58 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 59 | Features |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 60 | -------- |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 61 | |
| 62 | The LLVM Loop Vectorizer has a number of features that allow it to vectorize |
| 63 | complex loops. |
| 64 | |
| 65 | Loops with unknown trip count |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 66 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 67 | |
| 68 | The Loop Vectorizer supports loops with an unknown trip count. |
| 69 | In the loop below, the iteration ``start`` and ``finish`` points are unknown, |
| 70 | and the Loop Vectorizer has a mechanism to vectorize loops that do not start |
Sean Silva | 13ed79c | 2012-12-20 02:23:25 +0000 | [diff] [blame] | 71 | at zero. In this example, 'n' may not be a multiple of the vector width, and |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 72 | the vectorizer has to execute the last few iterations as scalar code. Keeping |
| 73 | a scalar copy of the loop increases the code size. |
| 74 | |
| 75 | .. code-block:: c++ |
| 76 | |
| 77 | void bar(float *A, float* B, float K, int start, int end) { |
Sean Silva | 8c44a47 | 2012-12-20 22:47:41 +0000 | [diff] [blame] | 78 | for (int i = start; i < end; ++i) |
| 79 | A[i] *= B[i] + K; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | Runtime Checks of Pointers |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 83 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 84 | |
| 85 | In the example below, if the pointers A and B point to consecutive addresses, |
| 86 | then it is illegal to vectorize the code because some elements of A will be |
| 87 | written before they are read from array B. |
| 88 | |
| 89 | Some programmers use the 'restrict' keyword to notify the compiler that the |
| 90 | pointers are disjointed, but in our example, the Loop Vectorizer has no way of |
| 91 | knowing that the pointers A and B are unique. The Loop Vectorizer handles this |
| 92 | loop by placing code that checks, at runtime, if the arrays A and B point to |
| 93 | disjointed memory locations. If arrays A and B overlap, then the scalar version |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 94 | of the loop is executed. |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 95 | |
| 96 | .. code-block:: c++ |
| 97 | |
| 98 | void bar(float *A, float* B, float K, int n) { |
Sean Silva | 8c44a47 | 2012-12-20 22:47:41 +0000 | [diff] [blame] | 99 | for (int i = 0; i < n; ++i) |
| 100 | A[i] *= B[i] + K; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | Reductions |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 105 | ^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 106 | |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 107 | In this example the ``sum`` variable is used by consecutive iterations of |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 108 | the loop. Normally, this would prevent vectorization, but the vectorizer can |
Sean Silva | 13ed79c | 2012-12-20 02:23:25 +0000 | [diff] [blame] | 109 | detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 110 | of integers, and at the end of the loop the elements of the array are added |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 111 | together to create the correct result. We support a number of different |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 112 | reduction operations, such as addition, multiplication, XOR, AND and OR. |
| 113 | |
| 114 | .. code-block:: c++ |
| 115 | |
| 116 | int foo(int *A, int *B, int n) { |
| 117 | unsigned sum = 0; |
| 118 | for (int i = 0; i < n; ++i) |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 119 | sum += A[i] + 5; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 120 | return sum; |
| 121 | } |
| 122 | |
Nadav Rotem | 9f20781 | 2013-01-08 17:46:30 +0000 | [diff] [blame] | 123 | We support floating point reduction operations when `-ffast-math` is used. |
| 124 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 125 | Inductions |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 126 | ^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 127 | |
| 128 | In this example the value of the induction variable ``i`` is saved into an |
| 129 | array. The Loop Vectorizer knows to vectorize induction variables. |
| 130 | |
| 131 | .. code-block:: c++ |
| 132 | |
| 133 | void bar(float *A, float* B, float K, int n) { |
Sean Silva | 8c44a47 | 2012-12-20 22:47:41 +0000 | [diff] [blame] | 134 | for (int i = 0; i < n; ++i) |
| 135 | A[i] = i; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | If Conversion |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 139 | ^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 140 | |
| 141 | The Loop Vectorizer is able to "flatten" the IF statement in the code and |
| 142 | generate a single stream of instructions. The Loop Vectorizer supports any |
| 143 | control flow in the innermost loop. The innermost loop may contain complex |
| 144 | nesting of IFs, ELSEs and even GOTOs. |
| 145 | |
| 146 | .. code-block:: c++ |
| 147 | |
| 148 | int foo(int *A, int *B, int n) { |
| 149 | unsigned sum = 0; |
| 150 | for (int i = 0; i < n; ++i) |
| 151 | if (A[i] > B[i]) |
| 152 | sum += A[i] + 5; |
| 153 | return sum; |
| 154 | } |
| 155 | |
| 156 | Pointer Induction Variables |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 157 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 158 | |
| 159 | This example uses the "accumulate" function of the standard c++ library. This |
| 160 | loop uses C++ iterators, which are pointers, and not integer indices. |
| 161 | The Loop Vectorizer detects pointer induction variables and can vectorize |
| 162 | this loop. This feature is important because many C++ programs use iterators. |
| 163 | |
| 164 | .. code-block:: c++ |
| 165 | |
| 166 | int baz(int *A, int n) { |
| 167 | return std::accumulate(A, A + n, 0); |
| 168 | } |
| 169 | |
| 170 | Reverse Iterators |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 171 | ^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 172 | |
| 173 | The Loop Vectorizer can vectorize loops that count backwards. |
| 174 | |
| 175 | .. code-block:: c++ |
| 176 | |
| 177 | int foo(int *A, int *B, int n) { |
| 178 | for (int i = n; i > 0; --i) |
| 179 | A[i] +=1; |
| 180 | } |
| 181 | |
| 182 | Scatter / Gather |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 183 | ^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 184 | |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 185 | The Loop Vectorizer can vectorize code that becomes a sequence of scalar instructions |
| 186 | that scatter/gathers memory. |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 187 | |
| 188 | .. code-block:: c++ |
| 189 | |
| 190 | int foo(int *A, int *B, int n, int k) { |
Sean Silva | 8c44a47 | 2012-12-20 22:47:41 +0000 | [diff] [blame] | 191 | for (int i = 0; i < n; ++i) |
Sean Silva | e140b2e | 2012-12-20 22:49:13 +0000 | [diff] [blame] | 192 | A[i*7] += B[i*k]; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Nadav Rotem | af14a3f | 2012-12-19 07:36:35 +0000 | [diff] [blame] | 195 | Vectorization of Mixed Types |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 196 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 197 | |
| 198 | The Loop Vectorizer can vectorize programs with mixed types. The Vectorizer |
| 199 | cost model can estimate the cost of the type conversion and decide if |
| 200 | vectorization is profitable. |
| 201 | |
| 202 | .. code-block:: c++ |
| 203 | |
| 204 | int foo(int *A, char *B, int n, int k) { |
Sean Silva | 8c44a47 | 2012-12-20 22:47:41 +0000 | [diff] [blame] | 205 | for (int i = 0; i < n; ++i) |
Sean Silva | e140b2e | 2012-12-20 22:49:13 +0000 | [diff] [blame] | 206 | A[i] += 4 * B[i]; |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Renato Golin | f2ea19e | 2013-02-23 13:25:41 +0000 | [diff] [blame] | 209 | Global Structures Alias Analysis |
| 210 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 211 | |
| 212 | Access to global structures can also be vectorized, with alias analysis being |
| 213 | used to make sure accesses don't alias. Run-time checks can also be added on |
| 214 | pointer access to structure members. |
| 215 | |
| 216 | Many variations are supported, but some that rely on undefined behaviour being |
| 217 | ignored (as other compilers do) are still being left un-vectorized. |
| 218 | |
| 219 | .. code-block:: c++ |
| 220 | |
| 221 | struct { int A[100], K, B[100]; } Foo; |
| 222 | |
| 223 | int foo() { |
| 224 | for (int i = 0; i < 100; ++i) |
| 225 | Foo.A[i] = Foo.B[i] + 100; |
| 226 | } |
| 227 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 228 | Vectorization of function calls |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 229 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 230 | |
| 231 | The Loop Vectorize can vectorize intrinsic math functions. |
| 232 | See the table below for a list of these functions. |
| 233 | |
| 234 | +-----+-----+---------+ |
| 235 | | pow | exp | exp2 | |
| 236 | +-----+-----+---------+ |
| 237 | | sin | cos | sqrt | |
| 238 | +-----+-----+---------+ |
| 239 | | log |log2 | log10 | |
| 240 | +-----+-----+---------+ |
| 241 | |fabs |floor| ceil | |
| 242 | +-----+-----+---------+ |
| 243 | |fma |trunc|nearbyint| |
| 244 | +-----+-----+---------+ |
Nadav Rotem | 7375d35 | 2012-12-26 06:03:35 +0000 | [diff] [blame] | 245 | | | | fmuladd | |
| 246 | +-----+-----+---------+ |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 247 | |
Benjamin Kramer | a87d512 | 2013-02-28 19:33:46 +0000 | [diff] [blame] | 248 | The loop vectorizer knows about special instructions on the target and will |
| 249 | vectorize a loop containing a function call that maps to the instructions. For |
| 250 | example, the loop below will be vectorized on Intel x86 if the SSE4.1 roundps |
| 251 | instruction is available. |
| 252 | |
| 253 | .. code-block:: c++ |
| 254 | |
| 255 | void foo(float *f) { |
| 256 | for (int i = 0; i != 1024; ++i) |
| 257 | f[i] = floorf(f[i]); |
| 258 | } |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 259 | |
| 260 | Partial unrolling during vectorization |
| 261 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 262 | |
| 263 | Modern processors feature multiple execution units, and only programs that contain a |
Nadav Rotem | 7ea18a7 | 2013-01-03 01:56:33 +0000 | [diff] [blame] | 264 | high degree of parallelism can fully utilize the entire width of the machine. |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 265 | The Loop Vectorizer increases the instruction level parallelism (ILP) by |
| 266 | performing partial-unrolling of loops. |
| 267 | |
| 268 | In the example below the entire array is accumulated into the variable 'sum'. |
Nadav Rotem | 7ea18a7 | 2013-01-03 01:56:33 +0000 | [diff] [blame] | 269 | This is inefficient because only a single execution port can be used by the processor. |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 270 | By unrolling the code the Loop Vectorizer allows two or more execution ports |
Nadav Rotem | 7ea18a7 | 2013-01-03 01:56:33 +0000 | [diff] [blame] | 271 | to be used simultaneously. |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 272 | |
| 273 | .. code-block:: c++ |
| 274 | |
| 275 | int foo(int *A, int *B, int n) { |
| 276 | unsigned sum = 0; |
| 277 | for (int i = 0; i < n; ++i) |
| 278 | sum += A[i]; |
| 279 | return sum; |
| 280 | } |
| 281 | |
Nadav Rotem | 7daadf2 | 2013-01-04 17:49:45 +0000 | [diff] [blame] | 282 | The Loop Vectorizer uses a cost model to decide when it is profitable to unroll loops. |
| 283 | The decision to unroll the loop depends on the register pressure and the generated code size. |
Nadav Rotem | a616d68 | 2013-01-03 01:47:02 +0000 | [diff] [blame] | 284 | |
Nadav Rotem | 15bdbbe | 2012-12-19 08:28:24 +0000 | [diff] [blame] | 285 | Performance |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 286 | ----------- |
Nadav Rotem | 15bdbbe | 2012-12-19 08:28:24 +0000 | [diff] [blame] | 287 | |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 288 | This section shows the the execution time of Clang on a simple benchmark: |
Nadav Rotem | 90c8b4b | 2012-12-19 08:43:05 +0000 | [diff] [blame] | 289 | `gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`_. |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 290 | This benchmarks is a collection of loops from the GCC autovectorization |
Nadav Rotem | 8f4a6cc | 2012-12-19 18:02:36 +0000 | [diff] [blame] | 291 | `page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>`_ by Dorit Nuzman. |
Nadav Rotem | 15bdbbe | 2012-12-19 08:28:24 +0000 | [diff] [blame] | 292 | |
Nadav Rotem | 12da396 | 2012-12-20 00:03:36 +0000 | [diff] [blame] | 293 | The chart below compares GCC-4.7, ICC-13, and Clang-SVN with and without loop vectorization at -O3, tuned for "corei7-avx", running on a Sandybridge iMac. |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 294 | The Y-axis shows the time in msec. Lower is better. The last column shows the geomean of all the kernels. |
Nadav Rotem | 15bdbbe | 2012-12-19 08:28:24 +0000 | [diff] [blame] | 295 | |
| 296 | .. image:: gcc-loops.png |
| 297 | |
Nadav Rotem | 014e19c | 2013-01-04 19:00:42 +0000 | [diff] [blame] | 298 | And Linpack-pc with the same configuration. Result is Mflops, higher is better. |
| 299 | |
| 300 | .. image:: linpack-pc.png |
| 301 | |
Sean Silva | 99e12f9 | 2012-12-20 22:42:20 +0000 | [diff] [blame] | 302 | .. _bb-vectorizer: |
| 303 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 304 | The Basic Block Vectorizer |
| 305 | ========================== |
| 306 | |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 307 | Usage |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 308 | ------ |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 309 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 310 | The Basic Block Vectorizer is not enabled by default, but it can be enabled |
| 311 | through clang using the command line flag: |
| 312 | |
| 313 | .. code-block:: console |
| 314 | |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 315 | $ clang -fslp-vectorize file.c |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 316 | |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 317 | Details |
Sean Silva | 08fd088 | 2012-12-20 02:40:45 +0000 | [diff] [blame] | 318 | ------- |
Nadav Rotem | 0328f5e | 2012-12-19 18:04:44 +0000 | [diff] [blame] | 319 | |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 320 | The goal of basic-block vectorization (a.k.a. superword-level parallelism) is |
| 321 | to combine similar independent instructions within simple control-flow regions |
| 322 | into vector instructions. Memory accesses, arithemetic operations, comparison |
| 323 | operations and some math functions can all be vectorized using this technique |
Sean Silva | 287e7d2 | 2012-12-20 22:59:36 +0000 | [diff] [blame] | 324 | (subject to the capabilities of the target architecture). |
Nadav Rotem | c4efbb8 | 2012-12-19 07:22:24 +0000 | [diff] [blame] | 325 | |
| 326 | For example, the following function performs very similar operations on its |
| 327 | inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these |
| 328 | into vector operations. |
| 329 | |
| 330 | .. code-block:: c++ |
| 331 | |
| 332 | int foo(int a1, int a2, int b1, int b2) { |
| 333 | int r1 = a1*(a1 + b1)/b1 + 50*b1/a1; |
| 334 | int r2 = a2*(a2 + b2)/b2 + 50*b2/a2; |
| 335 | return r1 + r2; |
| 336 | } |
| 337 | |
| 338 | |