]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gccint/passes-and-files-of-the-compiler/tree-ssa-passes.rst
sphinx: copy files from texi2rst-generated repository
[thirdparty/gcc.git] / gcc / doc / gccint / passes-and-files-of-the-compiler / tree-ssa-passes.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. _tree-ssa-passes:
7
8 Tree SSA passes
9 ***************
10
11 The following briefly describes the Tree optimization passes that are
12 run after gimplification and what source files they are located in.
13
14 * Remove useless statements
15
16 This pass is an extremely simple sweep across the gimple code in which
17 we identify obviously dead code and remove it. Here we do things like
18 simplify ``if`` statements with constant conditions, remove
19 exception handling constructs surrounding code that obviously cannot
20 throw, remove lexical bindings that contain no variables, and other
21 assorted simplistic cleanups. The idea is to get rid of the obvious
22 stuff quickly rather than wait until later when it's more work to get
23 rid of it. This pass is located in :samp:`tree-cfg.cc` and described by
24 ``pass_remove_useless_stmts``.
25
26 * OpenMP lowering
27
28 If OpenMP generation (:option:`-fopenmp`) is enabled, this pass lowers
29 OpenMP constructs into GIMPLE.
30
31 Lowering of OpenMP constructs involves creating replacement
32 expressions for local variables that have been mapped using data
33 sharing clauses, exposing the control flow of most synchronization
34 directives and adding region markers to facilitate the creation of the
35 control flow graph. The pass is located in :samp:`omp-low.cc` and is
36 described by ``pass_lower_omp``.
37
38 * OpenMP expansion
39
40 If OpenMP generation (:option:`-fopenmp`) is enabled, this pass expands
41 parallel regions into their own functions to be invoked by the thread
42 library. The pass is located in :samp:`omp-low.cc` and is described by
43 ``pass_expand_omp``.
44
45 * Lower control flow
46
47 This pass flattens ``if`` statements (``COND_EXPR``)
48 and moves lexical bindings (``BIND_EXPR``) out of line. After
49 this pass, all ``if`` statements will have exactly two ``goto``
50 statements in its ``then`` and ``else`` arms. Lexical binding
51 information for each statement will be found in ``TREE_BLOCK`` rather
52 than being inferred from its position under a ``BIND_EXPR``. This
53 pass is found in :samp:`gimple-low.cc` and is described by
54 ``pass_lower_cf``.
55
56 * Lower exception handling control flow
57
58 This pass decomposes high-level exception handling constructs
59 (``TRY_FINALLY_EXPR`` and ``TRY_CATCH_EXPR``) into a form
60 that explicitly represents the control flow involved. After this
61 pass, ``lookup_stmt_eh_region`` will return a non-negative
62 number for any statement that may have EH control flow semantics;
63 examine ``tree_can_throw_internal`` or ``tree_can_throw_external``
64 for exact semantics. Exact control flow may be extracted from
65 ``foreach_reachable_handler``. The EH region nesting tree is defined
66 in :samp:`except.h` and built in :samp:`except.cc`. The lowering pass
67 itself is in :samp:`tree-eh.cc` and is described by ``pass_lower_eh``.
68
69 * Build the control flow graph
70
71 This pass decomposes a function into basic blocks and creates all of
72 the edges that connect them. It is located in :samp:`tree-cfg.cc` and
73 is described by ``pass_build_cfg``.
74
75 * Find all referenced variables
76
77 This pass walks the entire function and collects an array of all
78 variables referenced in the function, ``referenced_vars``. The
79 index at which a variable is found in the array is used as a UID
80 for the variable within this function. This data is needed by the
81 SSA rewriting routines. The pass is located in :samp:`tree-dfa.cc`
82 and is described by ``pass_referenced_vars``.
83
84 * Enter static single assignment form
85
86 This pass rewrites the function such that it is in SSA form. After
87 this pass, all ``is_gimple_reg`` variables will be referenced by
88 ``SSA_NAME``, and all occurrences of other variables will be
89 annotated with ``VDEFS`` and ``VUSES`` ; PHI nodes will have
90 been inserted as necessary for each basic block. This pass is
91 located in :samp:`tree-ssa.cc` and is described by ``pass_build_ssa``.
92
93 * Warn for uninitialized variables
94
95 This pass scans the function for uses of ``SSA_NAME`` s that
96 are fed by default definition. For non-parameter variables, such
97 uses are uninitialized. The pass is run twice, before and after
98 optimization (if turned on). In the first pass we only warn for uses that are
99 positively uninitialized; in the second pass we warn for uses that
100 are possibly uninitialized. The pass is located in :samp:`tree-ssa.cc`
101 and is defined by ``pass_early_warn_uninitialized`` and
102 ``pass_late_warn_uninitialized``.
103
104 * Dead code elimination
105
106 This pass scans the function for statements without side effects whose
107 result is unused. It does not do memory life analysis, so any value
108 that is stored in memory is considered used. The pass is run multiple
109 times throughout the optimization process. It is located in
110 :samp:`tree-ssa-dce.cc` and is described by ``pass_dce``.
111
112 * Dominator optimizations
113
114 This pass performs trivial dominator-based copy and constant propagation,
115 expression simplification, and jump threading. It is run multiple times
116 throughout the optimization process. It is located in :samp:`tree-ssa-dom.cc`
117 and is described by ``pass_dominator``.
118
119 * Forward propagation of single-use variables
120
121 This pass attempts to remove redundant computation by substituting
122 variables that are used once into the expression that uses them and
123 seeing if the result can be simplified. It is located in
124 :samp:`tree-ssa-forwprop.cc` and is described by ``pass_forwprop``.
125
126 * Copy Renaming
127
128 This pass attempts to change the name of compiler temporaries involved in
129 copy operations such that SSA->normal can coalesce the copy away. When compiler
130 temporaries are copies of user variables, it also renames the compiler
131 temporary to the user variable resulting in better use of user symbols. It is
132 located in :samp:`tree-ssa-copyrename.c` and is described by
133 ``pass_copyrename``.
134
135 * PHI node optimizations
136
137 This pass recognizes forms of PHI inputs that can be represented as
138 conditional expressions and rewrites them into straight line code.
139 It is located in :samp:`tree-ssa-phiopt.cc` and is described by
140 ``pass_phiopt``.
141
142 * May-alias optimization
143
144 This pass performs a flow sensitive SSA-based points-to analysis.
145 The resulting may-alias, must-alias, and escape analysis information
146 is used to promote variables from in-memory addressable objects to
147 non-aliased variables that can be renamed into SSA form. We also
148 update the ``VDEF`` / ``VUSE`` memory tags for non-renameable
149 aggregates so that we get fewer false kills. The pass is located
150 in :samp:`tree-ssa-alias.cc` and is described by ``pass_may_alias``.
151
152 Interprocedural points-to information is located in
153 :samp:`tree-ssa-structalias.cc` and described by ``pass_ipa_pta``.
154
155 * Profiling
156
157 This pass instruments the function in order to collect runtime block
158 and value profiling data. Such data may be fed back into the compiler
159 on a subsequent run so as to allow optimization based on expected
160 execution frequencies. The pass is located in :samp:`tree-profile.cc` and
161 is described by ``pass_ipa_tree_profile``.
162
163 * Static profile estimation
164
165 This pass implements series of heuristics to guess propababilities
166 of branches. The resulting predictions are turned into edge profile
167 by propagating branches across the control flow graphs.
168 The pass is located in :samp:`tree-profile.cc` and is described by
169 ``pass_profile``.
170
171 * Lower complex arithmetic
172
173 This pass rewrites complex arithmetic operations into their component
174 scalar arithmetic operations. The pass is located in :samp:`tree-complex.cc`
175 and is described by ``pass_lower_complex``.
176
177 * Scalar replacement of aggregates
178
179 This pass rewrites suitable non-aliased local aggregate variables into
180 a set of scalar variables. The resulting scalar variables are
181 rewritten into SSA form, which allows subsequent optimization passes
182 to do a significantly better job with them. The pass is located in
183 :samp:`tree-sra.cc` and is described by ``pass_sra``.
184
185 * Dead store elimination
186
187 This pass eliminates stores to memory that are subsequently overwritten
188 by another store, without any intervening loads. The pass is located
189 in :samp:`tree-ssa-dse.cc` and is described by ``pass_dse``.
190
191 * Tail recursion elimination
192
193 This pass transforms tail recursion into a loop. It is located in
194 :samp:`tree-tailcall.cc` and is described by ``pass_tail_recursion``.
195
196 * Forward store motion
197
198 This pass sinks stores and assignments down the flowgraph closer to their
199 use point. The pass is located in :samp:`tree-ssa-sink.cc` and is
200 described by ``pass_sink_code``.
201
202 * Partial redundancy elimination
203
204 This pass eliminates partially redundant computations, as well as
205 performing load motion. The pass is located in :samp:`tree-ssa-pre.cc`
206 and is described by ``pass_pre``.
207
208 Just before partial redundancy elimination, if
209 :option:`-funsafe-math-optimizations` is on, GCC tries to convert
210 divisions to multiplications by the reciprocal. The pass is located
211 in :samp:`tree-ssa-math-opts.cc` and is described by
212 ``pass_cse_reciprocal``.
213
214 * Full redundancy elimination
215
216 This is a simpler form of PRE that only eliminates redundancies that
217 occur on all paths. It is located in :samp:`tree-ssa-pre.cc` and
218 described by ``pass_fre``.
219
220 * Loop optimization
221
222 The main driver of the pass is placed in :samp:`tree-ssa-loop.cc`
223 and described by ``pass_loop``.
224
225 The optimizations performed by this pass are:
226
227 Loop invariant motion. This pass moves only invariants that
228 would be hard to handle on RTL level (function calls, operations that expand to
229 nontrivial sequences of insns). With :option:`-funswitch-loops` it also moves
230 operands of conditions that are invariant out of the loop, so that we can use
231 just trivial invariantness analysis in loop unswitching. The pass also includes
232 store motion. The pass is implemented in :samp:`tree-ssa-loop-im.cc`.
233
234 Canonical induction variable creation. This pass creates a simple counter
235 for number of iterations of the loop and replaces the exit condition of the
236 loop using it, in case when a complicated analysis is necessary to determine
237 the number of iterations. Later optimizations then may determine the number
238 easily. The pass is implemented in :samp:`tree-ssa-loop-ivcanon.cc`.
239
240 Induction variable optimizations. This pass performs standard induction
241 variable optimizations, including strength reduction, induction variable
242 merging and induction variable elimination. The pass is implemented in
243 :samp:`tree-ssa-loop-ivopts.cc`.
244
245 Loop unswitching. This pass moves the conditional jumps that are invariant
246 out of the loops. To achieve this, a duplicate of the loop is created for
247 each possible outcome of conditional jump(s). The pass is implemented in
248 :samp:`tree-ssa-loop-unswitch.cc`.
249
250 Loop splitting. If a loop contains a conditional statement that is
251 always true for one part of the iteration space and false for the other
252 this pass splits the loop into two, one dealing with one side the other
253 only with the other, thereby removing one inner-loop conditional. The
254 pass is implemented in :samp:`tree-ssa-loop-split.cc`.
255
256 The optimizations also use various utility functions contained in
257 :samp:`tree-ssa-loop-manip.cc`, :samp:`cfgloop.cc`, :samp:`cfgloopanal.cc` and
258 :samp:`cfgloopmanip.cc`.
259
260 Vectorization. This pass transforms loops to operate on vector types
261 instead of scalar types. Data parallelism across loop iterations is exploited
262 to group data elements from consecutive iterations into a vector and operate
263 on them in parallel. Depending on available target support the loop is
264 conceptually unrolled by a factor ``VF`` (vectorization factor), which is
265 the number of elements operated upon in parallel in each iteration, and the
266 ``VF`` copies of each scalar operation are fused to form a vector operation.
267 Additional loop transformations such as peeling and versioning may take place
268 to align the number of iterations, and to align the memory accesses in the
269 loop.
270 The pass is implemented in :samp:`tree-vectorizer.cc` (the main driver),
271 :samp:`tree-vect-loop.cc` and :samp:`tree-vect-loop-manip.cc` (loop specific parts
272 and general loop utilities), :samp:`tree-vect-slp` (loop-aware SLP
273 functionality), :samp:`tree-vect-stmts.cc`, :samp:`tree-vect-data-refs.cc` and
274 :samp:`tree-vect-slp-patterns.cc` containing the SLP pattern matcher.
275 Analysis of data references is in :samp:`tree-data-ref.cc`.
276
277 SLP Vectorization. This pass performs vectorization of straight-line code. The
278 pass is implemented in :samp:`tree-vectorizer.cc` (the main driver),
279 :samp:`tree-vect-slp.cc`, :samp:`tree-vect-stmts.cc` and
280 :samp:`tree-vect-data-refs.cc`.
281
282 Autoparallelization. This pass splits the loop iteration space to run
283 into several threads. The pass is implemented in :samp:`tree-parloops.cc`.
284
285 Graphite is a loop transformation framework based on the polyhedral
286 model. Graphite stands for Gimple Represented as Polyhedra. The
287 internals of this infrastructure are documented in
288 https://gcc.gnu.org/wiki/Graphite. The passes working on
289 this representation are implemented in the various :samp:`graphite-*`
290 files.
291
292 * Tree level if-conversion for vectorizer
293
294 This pass applies if-conversion to simple loops to help vectorizer.
295 We identify if convertible loops, if-convert statements and merge
296 basic blocks in one big block. The idea is to present loop in such
297 form so that vectorizer can have one to one mapping between statements
298 and available vector operations. This pass is located in
299 :samp:`tree-if-conv.cc` and is described by ``pass_if_conversion``.
300
301 * Conditional constant propagation
302
303 This pass relaxes a lattice of values in order to identify those
304 that must be constant even in the presence of conditional branches.
305 The pass is located in :samp:`tree-ssa-ccp.cc` and is described
306 by ``pass_ccp``.
307
308 A related pass that works on memory loads and stores, and not just
309 register values, is located in :samp:`tree-ssa-ccp.cc` and described by
310 ``pass_store_ccp``.
311
312 * Conditional copy propagation
313
314 This is similar to constant propagation but the lattice of values is
315 the 'copy-of' relation. It eliminates redundant copies from the
316 code. The pass is located in :samp:`tree-ssa-copy.cc` and described by
317 ``pass_copy_prop``.
318
319 A related pass that works on memory copies, and not just register
320 copies, is located in :samp:`tree-ssa-copy.cc` and described by
321 ``pass_store_copy_prop``.
322
323 * Value range propagation
324
325 This transformation is similar to constant propagation but
326 instead of propagating single constant values, it propagates
327 known value ranges. The implementation is based on Patterson's
328 range propagation algorithm (Accurate Static Branch Prediction by
329 Value Range Propagation, J. R. C. Patterson, PLDI '95). In
330 contrast to Patterson's algorithm, this implementation does not
331 propagate branch probabilities nor it uses more than a single
332 range per SSA name. This means that the current implementation
333 cannot be used for branch prediction (though adapting it would
334 not be difficult). The pass is located in :samp:`tree-vrp.cc` and is
335 described by ``pass_vrp``.
336
337 * Folding built-in functions
338
339 This pass simplifies built-in functions, as applicable, with constant
340 arguments or with inferable string lengths. It is located in
341 :samp:`tree-ssa-ccp.cc` and is described by ``pass_fold_builtins``.
342
343 * Split critical edges
344
345 This pass identifies critical edges and inserts empty basic blocks
346 such that the edge is no longer critical. The pass is located in
347 :samp:`tree-cfg.cc` and is described by ``pass_split_crit_edges``.
348
349 * Control dependence dead code elimination
350
351 This pass is a stronger form of dead code elimination that can
352 eliminate unnecessary control flow statements. It is located
353 in :samp:`tree-ssa-dce.cc` and is described by ``pass_cd_dce``.
354
355 * Tail call elimination
356
357 This pass identifies function calls that may be rewritten into
358 jumps. No code transformation is actually applied here, but the
359 data and control flow problem is solved. The code transformation
360 requires target support, and so is delayed until RTL. In the
361 meantime ``CALL_EXPR_TAILCALL`` is set indicating the possibility.
362 The pass is located in :samp:`tree-tailcall.cc` and is described by
363 ``pass_tail_calls``. The RTL transformation is handled by
364 ``fixup_tail_calls`` in :samp:`calls.cc`.
365
366 * Warn for function return without value
367
368 For non-void functions, this pass locates return statements that do
369 not specify a value and issues a warning. Such a statement may have
370 been injected by falling off the end of the function. This pass is
371 run last so that we have as much time as possible to prove that the
372 statement is not reachable. It is located in :samp:`tree-cfg.cc` and
373 is described by ``pass_warn_function_return``.
374
375 * Leave static single assignment form
376
377 This pass rewrites the function such that it is in normal form. At
378 the same time, we eliminate as many single-use temporaries as possible,
379 so the intermediate language is no longer GIMPLE, but GENERIC. The
380 pass is located in :samp:`tree-outof-ssa.cc` and is described by
381 ``pass_del_ssa``.
382
383 * Merge PHI nodes that feed into one another
384
385 This is part of the CFG cleanup passes. It attempts to join PHI nodes
386 from a forwarder CFG block into another block with PHI nodes. The
387 pass is located in :samp:`tree-cfgcleanup.cc` and is described by
388 ``pass_merge_phi``.
389
390 * Return value optimization
391
392 If a function always returns the same local variable, and that local
393 variable is an aggregate type, then the variable is replaced with the
394 return value for the function (i.e., the function's DECL_RESULT). This
395 is equivalent to the C++ named return value optimization applied to
396 GIMPLE. The pass is located in :samp:`tree-nrv.cc` and is described by
397 ``pass_nrv``.
398
399 * Return slot optimization
400
401 If a function returns a memory object and is called as ``var =
402 foo()``, this pass tries to change the call so that the address of
403 ``var`` is sent to the caller to avoid an extra memory copy. This
404 pass is located in ``tree-nrv.cc`` and is described by
405 ``pass_return_slot``.
406
407 * Optimize calls to ``__builtin_object_size``
408
409 This is a propagation pass similar to CCP that tries to remove calls
410 to ``__builtin_object_size`` when the size of the object can be
411 computed at compile-time. This pass is located in
412 :samp:`tree-object-size.cc` and is described by
413 ``pass_object_sizes``.
414
415 * Loop invariant motion
416
417 This pass removes expensive loop-invariant computations out of loops.
418 The pass is located in :samp:`tree-ssa-loop.cc` and described by
419 ``pass_lim``.
420
421 * Loop nest optimizations
422
423 This is a family of loop transformations that works on loop nests. It
424 includes loop interchange, scaling, skewing and reversal and they are
425 all geared to the optimization of data locality in array traversals
426 and the removal of dependencies that hamper optimizations such as loop
427 parallelization and vectorization. The pass is located in
428 :samp:`tree-loop-linear.c` and described by
429 ``pass_linear_transform``.
430
431 * Removal of empty loops
432
433 This pass removes loops with no code in them. The pass is located in
434 :samp:`tree-ssa-loop-ivcanon.cc` and described by
435 ``pass_empty_loop``.
436
437 * Unrolling of small loops
438
439 This pass completely unrolls loops with few iterations. The pass
440 is located in :samp:`tree-ssa-loop-ivcanon.cc` and described by
441 ``pass_complete_unroll``.
442
443 * Predictive commoning
444
445 This pass makes the code reuse the computations from the previous
446 iterations of the loops, especially loads and stores to memory.
447 It does so by storing the values of these computations to a bank
448 of temporary variables that are rotated at the end of loop. To avoid
449 the need for this rotation, the loop is then unrolled and the copies
450 of the loop body are rewritten to use the appropriate version of
451 the temporary variable. This pass is located in :samp:`tree-predcom.cc`
452 and described by ``pass_predcom``.
453
454 * Array prefetching
455
456 This pass issues prefetch instructions for array references inside
457 loops. The pass is located in :samp:`tree-ssa-loop-prefetch.cc` and
458 described by ``pass_loop_prefetch``.
459
460 * Reassociation
461
462 This pass rewrites arithmetic expressions to enable optimizations that
463 operate on them, like redundancy elimination and vectorization. The
464 pass is located in :samp:`tree-ssa-reassoc.cc` and described by
465 ``pass_reassoc``.
466
467 * Optimization of ``stdarg`` functions
468
469 This pass tries to avoid the saving of register arguments into the
470 stack on entry to ``stdarg`` functions. If the function doesn't
471 use any ``va_start`` macros, no registers need to be saved. If
472 ``va_start`` macros are used, the ``va_list`` variables don't
473 escape the function, it is only necessary to save registers that will
474 be used in ``va_arg`` macros. For instance, if ``va_arg`` is
475 only used with integral types in the function, floating point
476 registers don't need to be saved. This pass is located in
477 ``tree-stdarg.cc`` and described by ``pass_stdarg``.