* config/xtensa/lib1funcs.asm (__mulsi3): Use symbolic name for ACCLO.
[official-gcc.git] / gcc / doc / tree-ssa.texi
blob4679912fa9aacbc4ae0d2132385952ebde3c441c
1 @c Copyright (c) 2004, 2005 Free Software Foundation, Inc.
2 @c Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @c ---------------------------------------------------------------------
7 @c Tree SSA
8 @c ---------------------------------------------------------------------
10 @node Tree SSA
11 @chapter Analysis and Optimization of GIMPLE Trees
12 @cindex Tree SSA
13 @cindex Optimization infrastructure for GIMPLE
15 GCC uses three main intermediate languages to represent the program
16 during compilation: GENERIC, GIMPLE and RTL@.  GENERIC is a
17 language-independent representation generated by each front end.  It
18 is used to serve as an interface between the parser and optimizer.
19 GENERIC is a common representation that is able to represent programs
20 written in all the languages supported by GCC@.
22 GIMPLE and RTL are used to optimize the program.  GIMPLE is used for
23 target and language independent optimizations (e.g., inlining,
24 constant propagation, tail call elimination, redundancy elimination,
25 etc).  Much like GENERIC, GIMPLE is a language independent, tree based
26 representation.  However, it differs from GENERIC in that the GIMPLE
27 grammar is more restrictive: expressions contain no more than 3
28 operands (except function calls), it has no control flow structures
29 and expressions with side-effects are only allowed on the right hand
30 side of assignments.  See the chapter describing GENERIC and GIMPLE
31 for more details.
33 This chapter describes the data structures and functions used in the
34 GIMPLE optimizers (also known as ``tree optimizers'' or ``middle
35 end'').  In particular, it focuses on all the macros, data structures,
36 functions and programming constructs needed to implement optimization
37 passes for GIMPLE@.
39 @menu
40 * GENERIC::             A high-level language-independent representation.
41 * GIMPLE::              A lower-level factored tree representation.
42 * Annotations::         Attributes for statements and variables.
43 * Statement Operands::  Variables referenced by GIMPLE statements.
44 * SSA::                 Static Single Assignment representation.
45 * Alias analysis::      Representing aliased loads and stores.
46 @end menu
48 @node GENERIC
49 @section GENERIC
50 @cindex GENERIC
52 The purpose of GENERIC is simply to provide a language-independent way of
53 representing an entire function in trees.  To this end, it was necessary to
54 add a few new tree codes to the back end, but most everything was already
55 there.  If you can express it with the codes in @code{gcc/tree.def}, it's
56 GENERIC@.
58 Early on, there was a great deal of debate about how to think about
59 statements in a tree IL@.  In GENERIC, a statement is defined as any
60 expression whose value, if any, is ignored.  A statement will always
61 have @code{TREE_SIDE_EFFECTS} set (or it will be discarded), but a
62 non-statement expression may also have side effects.  A
63 @code{CALL_EXPR}, for instance.
65 It would be possible for some local optimizations to work on the
66 GENERIC form of a function; indeed, the adapted tree inliner works
67 fine on GENERIC, but the current compiler performs inlining after
68 lowering to GIMPLE (a restricted form described in the next section).
69 Indeed, currently the frontends perform this lowering before handing
70 off to @code{tree_rest_of_compilation}, but this seems inelegant.
72 If necessary, a front end can use some language-dependent tree codes
73 in its GENERIC representation, so long as it provides a hook for
74 converting them to GIMPLE and doesn't expect them to work with any
75 (hypothetical) optimizers that run before the conversion to GIMPLE@.
76 The intermediate representation used while parsing C and C++ looks
77 very little like GENERIC, but the C and C++ gimplifier hooks are
78 perfectly happy to take it as input and spit out GIMPLE@.
80 @node GIMPLE
81 @section GIMPLE
82 @cindex GIMPLE
84 GIMPLE is a simplified subset of GENERIC for use in optimization.  The
85 particular subset chosen (and the name) was heavily influenced by the
86 SIMPLE IL used by the McCAT compiler project at McGill University
87 (@uref{http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html}),
88 though we have made some different choices.  For one thing, SIMPLE
89 doesn't support @code{goto}; a production compiler can't afford that
90 kind of restriction.
92 GIMPLE retains much of the structure of the parse trees: lexical
93 scopes are represented as containers, rather than markers.  However,
94 expressions are broken down into a 3-address form, using temporary
95 variables to hold intermediate values.  Also, control structures are
96 lowered to gotos.
98 In GIMPLE no container node is ever used for its value; if a
99 @code{COND_EXPR} or @code{BIND_EXPR} has a value, it is stored into a
100 temporary within the controlled blocks, and that temporary is used in
101 place of the container.
103 The compiler pass which lowers GENERIC to GIMPLE is referred to as the
104 @samp{gimplifier}.  The gimplifier works recursively, replacing complex
105 statements with sequences of simple statements.
107 @c Currently, the only way to
108 @c tell whether or not an expression is in GIMPLE form is by recursively
109 @c examining it; in the future there will probably be a flag to help avoid
110 @c redundant work.  FIXME FIXME
112 @menu
113 * Interfaces::
114 * Temporaries::
115 * GIMPLE Expressions::
116 * Statements::
117 * GIMPLE Example::
118 * Rough GIMPLE Grammar::
119 @end menu
121 @node Interfaces
122 @subsection Interfaces
123 @cindex gimplification
125 The tree representation of a function is stored in
126 @code{DECL_SAVED_TREE}.  It is lowered to GIMPLE by a call to
127 @code{gimplify_function_tree}.
129 If a front end wants to include language-specific tree codes in the tree
130 representation which it provides to the back end, it must provide a
131 definition of @code{LANG_HOOKS_GIMPLIFY_EXPR} which knows how to
132 convert the front end trees to GIMPLE@.  Usually such a hook will involve
133 much of the same code for expanding front end trees to RTL@.  This function
134 can return fully lowered GIMPLE, or it can return GENERIC trees and let the
135 main gimplifier lower them the rest of the way; this is often simpler.
137 The C and C++ front ends currently convert directly from front end
138 trees to GIMPLE, and hand that off to the back end rather than first
139 converting to GENERIC@.  Their gimplifier hooks know about all the
140 @code{_STMT} nodes and how to convert them to GENERIC forms.  There
141 was some work done on a genericization pass which would run first, but
142 the existence of @code{STMT_EXPR} meant that in order to convert all
143 of the C statements into GENERIC equivalents would involve walking the
144 entire tree anyway, so it was simpler to lower all the way.  This
145 might change in the future if someone writes an optimization pass
146 which would work better with higher-level trees, but currently the
147 optimizers all expect GIMPLE@.
149 A front end which wants to use the tree optimizers (and already has
150 some sort of whole-function tree representation) only needs to provide
151 a definition of @code{LANG_HOOKS_GIMPLIFY_EXPR}, call
152 @code{gimplify_function_tree} to lower to GIMPLE, and then hand off to
153 @code{tree_rest_of_compilation} to compile and output the function.
155 You can tell the compiler to dump a C-like representation of the GIMPLE
156 form with the flag @option{-fdump-tree-gimple}.
158 @node Temporaries
159 @subsection Temporaries
160 @cindex Temporaries
162 When gimplification encounters a subexpression which is too complex, it
163 creates a new temporary variable to hold the value of the subexpression,
164 and adds a new statement to initialize it before the current statement.
165 These special temporaries are known as @samp{expression temporaries}, and are
166 allocated using @code{get_formal_tmp_var}.  The compiler tries to
167 always evaluate identical expressions into the same temporary, to simplify
168 elimination of redundant calculations.
170 We can only use expression temporaries when we know that it will not be
171 reevaluated before its value is used, and that it will not be otherwise
172 modified@footnote{These restrictions are derived from those in Morgan 4.8.}.
173 Other temporaries can be allocated using
174 @code{get_initialized_tmp_var} or @code{create_tmp_var}.
176 Currently, an expression like @code{a = b + 5} is not reduced any
177 further.  We tried converting it to something like
178 @smallexample
179   T1 = b + 5;
180   a = T1;
181 @end smallexample
182 but this bloated the representation for minimal benefit.  However, a
183 variable which must live in memory cannot appear in an expression; its
184 value is explicitly loaded into a temporary first.  Similarly, storing
185 the value of an expression to a memory variable goes through a
186 temporary.
188 @node GIMPLE Expressions
189 @subsection Expressions
190 @cindex GIMPLE Expressions
192 In general, expressions in GIMPLE consist of an operation and the
193 appropriate number of simple operands; these operands must either be a
194 GIMPLE rvalue (@code{is_gimple_val}), i.e.@: a constant or a register
195 variable.  More complex operands are factored out into temporaries, so
196 that
197 @smallexample
198   a = b + c + d
199 @end smallexample
200 becomes
201 @smallexample
202   T1 = b + c;
203   a = T1 + d;
204 @end smallexample
206 The same rule holds for arguments to a @code{CALL_EXPR}.
208 The target of an assignment is usually a variable, but can also be an
209 @code{INDIRECT_REF} or a compound lvalue as described below.
211 @menu
212 * Compound Expressions::
213 * Compound Lvalues::
214 * Conditional Expressions::
215 * Logical Operators::
216 @end menu
218 @node Compound Expressions
219 @subsubsection Compound Expressions
220 @cindex Compound Expressions
222 The left-hand side of a C comma expression is simply moved into a separate
223 statement.
225 @node Compound Lvalues
226 @subsubsection Compound Lvalues
227 @cindex Compound Lvalues
229 Currently compound lvalues involving array and structure field references
230 are not broken down; an expression like @code{a.b[2] = 42} is not reduced
231 any further (though complex array subscripts are).  This restriction is a
232 workaround for limitations in later optimizers; if we were to convert this
235 @smallexample
236   T1 = &a.b;
237   T1[2] = 42;
238 @end smallexample
240 alias analysis would not remember that the reference to @code{T1[2]} came
241 by way of @code{a.b}, so it would think that the assignment could alias
242 another member of @code{a}; this broke @code{struct-alias-1.c}.  Future
243 optimizer improvements may make this limitation unnecessary.
245 @node Conditional Expressions
246 @subsubsection Conditional Expressions
247 @cindex Conditional Expressions
249 A C @code{?:} expression is converted into an @code{if} statement with
250 each branch assigning to the same temporary.  So,
252 @smallexample
253   a = b ? c : d;
254 @end smallexample
255 becomes
256 @smallexample
257   if (b)
258     T1 = c;
259   else
260     T1 = d;
261   a = T1;
262 @end smallexample
264 Tree level if-conversion pass re-introduces @code{?:} expression, if appropriate.
265 It is used to vectorize loops with conditions using vector conditional operations.
267 Note that in GIMPLE, @code{if} statements are also represented using
268 @code{COND_EXPR}, as described below.
270 @node Logical Operators
271 @subsubsection Logical Operators
272 @cindex Logical Operators
274 Except when they appear in the condition operand of a @code{COND_EXPR},
275 logical `and' and `or' operators are simplified as follows:
276 @code{a = b && c} becomes
278 @smallexample
279   T1 = (bool)b;
280   if (T1)
281     T1 = (bool)c;
282   a = T1;
283 @end smallexample
285 Note that @code{T1} in this example cannot be an expression temporary,
286 because it has two different assignments.
288 @node Statements
289 @subsection Statements
290 @cindex Statements
292 Most statements will be assignment statements, represented by
293 @code{MODIFY_EXPR}.  A @code{CALL_EXPR} whose value is ignored can
294 also be a statement.  No other C expressions can appear at statement level;
295 a reference to a volatile object is converted into a @code{MODIFY_EXPR}.
296 In GIMPLE form, type of @code{MODIFY_EXPR} is not meaningful.  Instead, use type
297 of LHS or RHS@.
299 There are also several varieties of complex statements.
301 @menu
302 * Blocks::
303 * Statement Sequences::
304 * Empty Statements::
305 * Loops::
306 * Selection Statements::
307 * Jumps::
308 * Cleanups::
309 * GIMPLE Exception Handling::
310 @end menu
312 @node Blocks
313 @subsubsection Blocks
314 @cindex Blocks
316 Block scopes and the variables they declare in GENERIC and GIMPLE are
317 expressed using the @code{BIND_EXPR} code, which in previous versions of
318 GCC was primarily used for the C statement-expression extension.
320 Variables in a block are collected into @code{BIND_EXPR_VARS} in
321 declaration order.  Any runtime initialization is moved out of
322 @code{DECL_INITIAL} and into a statement in the controlled block.  When
323 gimplifying from C or C++, this initialization replaces the
324 @code{DECL_STMT}.
326 Variable-length arrays (VLAs) complicate this process, as their size often
327 refers to variables initialized earlier in the block.  To handle this, we
328 currently split the block at that point, and move the VLA into a new, inner
329 @code{BIND_EXPR}.  This strategy may change in the future.
331 @code{DECL_SAVED_TREE} for a GIMPLE function will always be a
332 @code{BIND_EXPR} which contains declarations for the temporary variables
333 used in the function.
335 A C++ program will usually contain more @code{BIND_EXPR}s than there are
336 syntactic blocks in the source code, since several C++ constructs have
337 implicit scopes associated with them.  On the other hand, although the C++
338 front end uses pseudo-scopes to handle cleanups for objects with
339 destructors, these don't translate into the GIMPLE form; multiple
340 declarations at the same level use the same @code{BIND_EXPR}.
342 @node Statement Sequences
343 @subsubsection Statement Sequences
344 @cindex Statement Sequences
346 Multiple statements at the same nesting level are collected into a
347 @code{STATEMENT_LIST}.  Statement lists are modified and traversed
348 using the interface in @samp{tree-iterator.h}.
350 @node Empty Statements
351 @subsubsection Empty Statements
352 @cindex Empty Statements
354 Whenever possible, statements with no effect are discarded.  But if they
355 are nested within another construct which cannot be discarded for some
356 reason, they are instead replaced with an empty statement, generated by
357 @code{build_empty_stmt}.  Initially, all empty statements were shared,
358 after the pattern of the Java front end, but this caused a lot of trouble in
359 practice.
361 An empty statement is represented as @code{(void)0}.
363 @node Loops
364 @subsubsection Loops
365 @cindex Loops
367 At one time loops were expressed in GIMPLE using @code{LOOP_EXPR}, but
368 now they are lowered to explicit gotos.
370 @node Selection Statements
371 @subsubsection Selection Statements
372 @cindex Selection Statements
374 A simple selection statement, such as the C @code{if} statement, is
375 expressed in GIMPLE using a void @code{COND_EXPR}.  If only one branch is
376 used, the other is filled with an empty statement.
378 Normally, the condition expression is reduced to a simple comparison.  If
379 it is a shortcut (@code{&&} or @code{||}) expression, however, we try to
380 break up the @code{if} into multiple @code{if}s so that the implied shortcut
381 is taken directly, much like the transformation done by @code{do_jump} in
382 the RTL expander.
384 A @code{SWITCH_EXPR} in GIMPLE contains the condition and a
385 @code{TREE_VEC} of @code{CASE_LABEL_EXPR}s describing the case values
386 and corresponding @code{LABEL_DECL}s to jump to.  The body of the
387 @code{switch} is moved after the @code{SWITCH_EXPR}.
389 @node Jumps
390 @subsubsection Jumps
391 @cindex Jumps
393 Other jumps are expressed by either @code{GOTO_EXPR} or @code{RETURN_EXPR}.
395 The operand of a @code{GOTO_EXPR} must be either a label or a variable
396 containing the address to jump to.
398 The operand of a @code{RETURN_EXPR} is either @code{NULL_TREE} or a
399 @code{MODIFY_EXPR} which sets the return value.  It would be nice to
400 move the @code{MODIFY_EXPR} into a separate statement, but the special
401 return semantics in @code{expand_return} make that difficult.  It may
402 still happen in the future, perhaps by moving most of that logic into
403 @code{expand_assignment}.
405 @node Cleanups
406 @subsubsection Cleanups
407 @cindex Cleanups
409 Destructors for local C++ objects and similar dynamic cleanups are
410 represented in GIMPLE by a @code{TRY_FINALLY_EXPR}.  When the controlled
411 block exits, the cleanup is run.
413 @code{TRY_FINALLY_EXPR} complicates the flow graph, since the cleanup
414 needs to appear on every edge out of the controlled block; this
415 reduces the freedom to move code across these edges.  Therefore, the
416 EH lowering pass which runs before most of the optimization passes
417 eliminates these expressions by explicitly adding the cleanup to each
418 edge.
420 @node GIMPLE Exception Handling
421 @subsubsection Exception Handling
422 @cindex GIMPLE Exception Handling
424 Other exception handling constructs are represented using
425 @code{TRY_CATCH_EXPR}.  The handler operand of a @code{TRY_CATCH_EXPR}
426 can be a normal statement to be executed if the controlled block throws an
427 exception, or it can have one of two special forms:
429 @enumerate
430 @item A @code{CATCH_EXPR} executes its handler if the thrown exception
431   matches one of the allowed types.  Multiple handlers can be
432   expressed by a sequence of @code{CATCH_EXPR} statements.
433 @item An @code{EH_FILTER_EXPR} executes its handler if the thrown
434   exception does not match one of the allowed types.
435 @end enumerate
437 Currently throwing an exception is not directly represented in GIMPLE,
438 since it is implemented by calling a function.  At some point in the future
439 we will want to add some way to express that the call will throw an
440 exception of a known type.
442 Just before running the optimizers, the compiler lowers the high-level
443 EH constructs above into a set of @samp{goto}s, magic labels, and EH
444 regions.  Continuing to unwind at the end of a cleanup is represented
445 with a @code{RESX_EXPR}.
447 @node GIMPLE Example
448 @subsection GIMPLE Example
449 @cindex GIMPLE Example
451 @smallexample
452 struct A @{ A(); ~A(); @};
454 int i;
455 int g();
456 void f()
458   A a;
459   int j = (--i, i ? 0 : 1);
461   for (int x = 42; x > 0; --x)
462     @{
463       i += g()*4 + 32;
464     @}
466 @end smallexample
468 becomes
470 @smallexample
471 void f()
473   int i.0;
474   int T.1;
475   int iftmp.2;
476   int T.3;
477   int T.4;
478   int T.5;
479   int T.6;
481   @{
482     struct A a;
483     int j;
485     __comp_ctor (&a);
486     try
487       @{
488         i.0 = i;
489         T.1 = i.0 - 1;
490         i = T.1;
491         i.0 = i;
492         if (i.0 == 0)
493           iftmp.2 = 1;
494         else
495           iftmp.2 = 0;
496         j = iftmp.2;
497         @{
498           int x;
500           x = 42;
501           goto test;
502           loop:;
504           T.3 = g ();
505           T.4 = T.3 * 4;
506           i.0 = i;
507           T.5 = T.4 + i.0;
508           T.6 = T.5 + 32;
509           i = T.6;
510           x = x - 1;
512           test:;
513           if (x > 0)
514             goto loop;
515           else
516             goto break_;
517           break_:;
518         @}
519       @}
520     finally
521       @{
522         __comp_dtor (&a);
523       @}
524   @}
526 @end smallexample
528 @node Rough GIMPLE Grammar
529 @subsection Rough GIMPLE Grammar
530 @cindex Rough GIMPLE Grammar
532 @smallexample
533    function     : FUNCTION_DECL
534                         DECL_SAVED_TREE -> compound-stmt
536    compound-stmt: STATEMENT_LIST
537                         members -> stmt
539    stmt         : block
540                 | if-stmt
541                 | switch-stmt
542                 | goto-stmt
543                 | return-stmt
544                 | resx-stmt
545                 | label-stmt
546                 | try-stmt
547                 | modify-stmt
548                 | call-stmt
550    block        : BIND_EXPR
551                         BIND_EXPR_VARS -> chain of DECLs
552                         BIND_EXPR_BLOCK -> BLOCK
553                         BIND_EXPR_BODY -> compound-stmt
555    if-stmt      : COND_EXPR
556                         op0 -> condition
557                         op1 -> compound-stmt
558                         op2 -> compound-stmt
560    switch-stmt  : SWITCH_EXPR
561                         op0 -> val
562                         op1 -> NULL
563                         op2 -> TREE_VEC of CASE_LABEL_EXPRs
564                             The CASE_LABEL_EXPRs are sorted by CASE_LOW,
565                             and default is last.
567    goto-stmt    : GOTO_EXPR
568                         op0 -> LABEL_DECL | val
570    return-stmt  : RETURN_EXPR
571                         op0 -> return-value
573    return-value : NULL
574                 | RESULT_DECL
575                 | MODIFY_EXPR
576                         op0 -> RESULT_DECL
577                         op1 -> lhs
579    resx-stmt    : RESX_EXPR
581    label-stmt   : LABEL_EXPR
582                         op0 -> LABEL_DECL
584    try-stmt     : TRY_CATCH_EXPR
585                         op0 -> compound-stmt
586                         op1 -> handler
587                 | TRY_FINALLY_EXPR
588                         op0 -> compound-stmt
589                         op1 -> compound-stmt
591    handler      : catch-seq
592                 | EH_FILTER_EXPR
593                 | compound-stmt
595    catch-seq    : STATEMENT_LIST
596                         members -> CATCH_EXPR
598    modify-stmt  : MODIFY_EXPR
599                         op0 -> lhs
600                         op1 -> rhs
602    call-stmt    : CALL_EXPR
603                         op0 -> val | OBJ_TYPE_REF
604                         op1 -> call-arg-list
606    call-arg-list: TREE_LIST
607                         members -> lhs | CONST
609    addr-expr-arg: ID
610                 | compref
612    addressable  : addr-expr-arg
613                 | indirectref
615    with-size-arg: addressable
616                 | call-stmt
618    indirectref  : INDIRECT_REF
619                         op0 -> val
621    lhs          : addressable
622                 | bitfieldref
623                 | WITH_SIZE_EXPR
624                         op0 -> with-size-arg
625                         op1 -> val
627    min-lval     : ID
628                 | indirectref
630    bitfieldref  : BIT_FIELD_REF
631                         op0 -> inner-compref
632                         op1 -> CONST
633                         op2 -> var
635    compref      : inner-compref
636                 | REALPART_EXPR
637                         op0 -> inner-compref
638                 | IMAGPART_EXPR
639                         op0 -> inner-compref
641    inner-compref: min-lval
642                 | COMPONENT_REF
643                         op0 -> inner-compref
644                         op1 -> FIELD_DECL
645                         op2 -> val
646                 | ARRAY_REF
647                         op0 -> inner-compref
648                         op1 -> val
649                         op2 -> val
650                         op3 -> val
651                 | ARRAY_RANGE_REF
652                         op0 -> inner-compref
653                         op1 -> val
654                         op2 -> val
655                         op3 -> val
656                 | VIEW_CONVERT_EXPR
657                         op0 -> inner-compref
659    condition    : val
660                 | RELOP
661                         op0 -> val
662                         op1 -> val
664    val          : ID
665                 | CONST
667    rhs          : lhs
668                 | CONST
669                 | call-stmt
670                 | ADDR_EXPR
671                         op0 -> addr-expr-arg
672                 | UNOP
673                         op0 -> val
674                 | BINOP
675                         op0 -> val
676                         op1 -> val
677                 | RELOP
678                         op0 -> val
679                         op1 -> val
680 @end smallexample
682 @node Annotations
683 @section Annotations
684 @cindex annotations
686 The optimizers need to associate attributes with statements and
687 variables during the optimization process.  For instance, we need to
688 know what basic block does a statement belong to or whether a variable
689 has aliases.  All these attributes are stored in data structures
690 called annotations which are then linked to the field @code{ann} in
691 @code{struct tree_common}.
693 Presently, we define annotations for statements (@code{stmt_ann_t}),
694 variables (@code{var_ann_t}) and SSA names (@code{ssa_name_ann_t}).
695 Annotations are defined and documented in @file{tree-flow.h}.
698 @node Statement Operands
699 @section Statement Operands
700 @cindex operands
701 @cindex virtual operands
702 @cindex real operands
703 @findex get_stmt_operands
704 @findex modify_stmt
706 Almost every GIMPLE statement will contain a reference to a variable
707 or memory location.  Since statements come in different shapes and
708 sizes, their operands are going to be located at various spots inside
709 the statement's tree.  To facilitate access to the statement's
710 operands, they are organized into arrays associated inside each
711 statement's annotation.  Each element in an operand array is a pointer
712 to a @code{VAR_DECL}, @code{PARM_DECL} or @code{SSA_NAME} tree node.
713 This provides a very convenient way of examining and replacing
714 operands.
716 Data flow analysis and optimization is done on all tree nodes
717 representing variables.  Any node for which @code{SSA_VAR_P} returns
718 nonzero is considered when scanning statement operands.  However, not
719 all @code{SSA_VAR_P} variables are processed in the same way.  For the
720 purposes of optimization, we need to distinguish between references to
721 local scalar variables and references to globals, statics, structures,
722 arrays, aliased variables, etc.  The reason is simple, the compiler
723 can gather complete data flow information for a local scalar.  On the
724 other hand, a global variable may be modified by a function call, it
725 may not be possible to keep track of all the elements of an array or
726 the fields of a structure, etc.
728 The operand scanner gathers two kinds of operands: @dfn{real} and
729 @dfn{virtual}.  An operand for which @code{is_gimple_reg} returns true
730 is considered real, otherwise it is a virtual operand.  We also
731 distinguish between uses and definitions.  An operand is used if its
732 value is loaded by the statement (e.g., the operand at the RHS of an
733 assignment).  If the statement assigns a new value to the operand, the
734 operand is considered a definition (e.g., the operand at the LHS of
735 an assignment).
737 Virtual and real operands also have very different data flow
738 properties.  Real operands are unambiguous references to the
739 full object that they represent.  For instance, given
741 @smallexample
743   int a, b;
744   a = b
746 @end smallexample
748 Since @code{a} and @code{b} are non-aliased locals, the statement
749 @code{a = b} will have one real definition and one real use because
750 variable @code{b} is completely modified with the contents of
751 variable @code{a}.  Real definition are also known as @dfn{killing
752 definitions}.  Similarly, the use of @code{a} reads all its bits.
754 In contrast, virtual operands are used with variables that can have
755 a partial or ambiguous reference.  This includes structures, arrays,
756 globals, and aliased variables.  In these cases, we have two types of
757 definitions.  For globals, structures, and arrays, we can determine from
758 a statement whether a variable of these types has a killing definition.
759 If the variable does, then the statement is marked as having a
760 @dfn{must definition} of that variable.  However, if a statement is only
761 defining a part of the variable (i.e.@: a field in a structure), or if we
762 know that a statement might define the variable but we cannot say for sure,
763 then we mark that statement as having a @dfn{may definition}.  For
764 instance, given
766 @smallexample
768   int a, b, *p;
770   if (...)
771     p = &a;
772   else
773     p = &b;
774   *p = 5;
775   return *p;
777 @end smallexample
779 The assignment @code{*p = 5} may be a definition of @code{a} or
780 @code{b}.  If we cannot determine statically where @code{p} is
781 pointing to at the time of the store operation, we create virtual
782 definitions to mark that statement as a potential definition site for
783 @code{a} and @code{b}.  Memory loads are similarly marked with virtual
784 use operands.  Virtual operands are shown in tree dumps right before
785 the statement that contains them.  To request a tree dump with virtual
786 operands, use the @option{-vops} option to @option{-fdump-tree}:
788 @smallexample
790   int a, b, *p;
792   if (...)
793     p = &a;
794   else
795     p = &b;
796   # a = V_MAY_DEF <a>
797   # b = V_MAY_DEF <b>
798   *p = 5;
800   # VUSE <a>
801   # VUSE <b>
802   return *p;
804 @end smallexample
806 Notice that @code{V_MAY_DEF} operands have two copies of the referenced
807 variable.  This indicates that this is not a killing definition of
808 that variable.  In this case we refer to it as a @dfn{may definition}
809 or @dfn{aliased store}.  The presence of the second copy of the
810 variable in the @code{V_MAY_DEF} operand will become important when the
811 function is converted into SSA form.  This will be used to link all
812 the non-killing definitions to prevent optimizations from making
813 incorrect assumptions about them.
815 Operands are collected by @file{tree-ssa-operands.c}.  They are stored
816 inside each statement's annotation and can be accessed with
817 @code{DEF_OPS}, @code{USE_OPS}, @code{V_MAY_DEF_OPS},
818 @code{V_MUST_DEF_OPS} and @code{VUSE_OPS}.  The following are all the
819 accessor macros available to access USE operands.  To access all the
820 other operand arrays, just change the name accordingly.  Note that
821 this interface to the operands is deprecated, and is slated for
822 removal in a future version of gcc.  The preferred interface is the
823 operand iterator interface.  Unless you need to discover the number of
824 operands of a given type on a statement, you are strongly urged not to
825 use this interface.
827 @defmac USE_OPS (@var{ann})
828 Returns the array of operands used by the statement with annotation
829 @var{ann}.
830 @end defmac
832 @defmac STMT_USE_OPS (@var{stmt})
833 Alternate version of USE_OPS that takes the statement @var{stmt} as
834 input.
835 @end defmac
837 @defmac NUM_USES (@var{ops})
838 Return the number of USE operands in array @var{ops}.
839 @end defmac
841 @defmac USE_OP_PTR (@var{ops}, @var{i})
842 Return a pointer to the @var{i}th operand in array @var{ops}.
843 @end defmac
845 @defmac USE_OP (@var{ops}, @var{i})
846 Return the @var{i}th operand in array @var{ops}.
847 @end defmac
849 The following function shows how to print all the operands of a given
850 statement:
852 @smallexample
853 void
854 print_ops (tree stmt)
856   vuse_optype vuses;
857   v_may_def_optype v_may_defs;
858   v_must_def_optype v_must_defs;
859   def_optype defs;
860   use_optype uses;
861   stmt_ann_t ann;
862   size_t i;
864   get_stmt_operands (stmt);
865   ann = stmt_ann (stmt);
867   defs = DEF_OPS (ann);
868   for (i = 0; i < NUM_DEFS (defs); i++)
869     print_generic_expr (stderr, DEF_OP (defs, i), 0);
871   uses = USE_OPS (ann);
872   for (i = 0; i < NUM_USES (uses); i++)
873     print_generic_expr (stderr, USE_OP (uses, i), 0);
875   v_may_defs = V_MAY_DEF_OPS (ann);
876   for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
877     @{
878       print_generic_expr (stderr, V_MAY_DEF_OP (v_may_defs, i), 0);
879       print_generic_expr (stderr, V_MAY_DEF_RESULT (v_may_defs, i), 0);
880     @}
882   v_must_defs = V_MUST_DEF_OPS (ann);
883   for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
884     print_generic_expr (stderr, V_MUST_DEF_OP (v_must_defs, i), 0);
886   vuses = VUSE_OPS (ann);
887   for (i = 0; i < NUM_VUSES (vuses); i++)
888     print_generic_expr (stderr, VUSE_OP (vuses, i), 0);
890 @end smallexample
892 To collect the operands, you first need to call
893 @code{get_stmt_operands}.  Since that is a potentially expensive
894 operation, statements are only scanned if they have been marked
895 modified by a call to @code{modify_stmt}.  So, if your pass replaces
896 operands in a statement, make sure to call @code{modify_stmt}.
898 @subsection Operand Iterators
899 @cindex Operand Iterators
901 There is an alternative to iterating over the operands in a statement.
902 It is especially useful when you wish to perform the same operation on
903 more than one type of operand.  The previous example could be
904 rewritten as follows:
906 @smallexample
907 void
908 print_ops (tree stmt)
910   ssa_op_iter;
911   tree var;
913   get_stmt_operands (stmt);
914   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
915     print_generic_expr (stderr, var, 0);
917 @end smallexample
920 @enumerate
921 @item Determine whether you are need to see the operand pointers, or just the
922     trees, and choose the appropriate macro:
924 @smallexample
925 Need            Macro:
926 ----            -------
927 use_operand_p   FOR_EACH_SSA_USE_OPERAND
928 def_operand_p   FOR_EACH_SSA_DEF_OPERAND
929 tree            FOR_EACH_SSA_TREE_OPERAND
930 @end smallexample
932 @item You need to declare a variable of the type you are interested
933     in, and an ssa_op_iter structure which serves as the loop
934     controlling variable.
936 @item Determine which operands you wish to use, and specify the flags of
937     those you are interested in.  They are documented in
938     @file{tree-ssa-operands.h}:
940 @smallexample
941 #define SSA_OP_USE              0x01    /* @r{Real USE operands.}  */
942 #define SSA_OP_DEF              0x02    /* @r{Real DEF operands.}  */
943 #define SSA_OP_VUSE             0x04    /* @r{VUSE operands.}  */
944 #define SSA_OP_VMAYUSE          0x08    /* @r{USE portion of V_MAY_DEFS.}  */
945 #define SSA_OP_VMAYDEF          0x10    /* @r{DEF portion of V_MAY_DEFS.}  */
946 #define SSA_OP_VMUSTDEF         0x20    /* @r{V_MUST_DEF definitions.}  */
948 /* @r{These are commonly grouped operand flags.}  */
949 #define SSA_OP_VIRTUAL_USES     (SSA_OP_VUSE | SSA_OP_VMAYUSE)
950 #define SSA_OP_VIRTUAL_DEFS     (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF)
951 #define SSA_OP_ALL_USES         (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
952 #define SSA_OP_ALL_DEFS         (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
953 #define SSA_OP_ALL_OPERANDS     (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
954 @end smallexample
955 @end enumerate
957 So if you want to look at the use pointers for all the @code{USE} and
958 @code{VUSE} operands, you would do something like:
960 @smallexample
961   use_operand_p use_p;
962   ssa_op_iter iter;
964   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
965     @{
966       process_use_ptr (use_p);
967     @}
968 @end smallexample
970 The @code{_TREE_} macro is basically the same as the @code{USE} and
971 @code{DEF} macros, only with the use or def dereferenced via
972 @code{USE_FROM_PTR (use_p)} and @code{DEF_FROM_PTR (def_p)}.  Since we
973 aren't using operand pointers, use and defs flags can be mixed.
975 @smallexample
976   tree var;
977   ssa_op_iter iter;
979   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE | SSA_OP_VMUSTDEF)
980     @{
981        print_generic_expr (stderr, var, TDF_SLIM);
982     @}
983 @end smallexample
985 @code{V_MAY_DEF}s are broken into two flags, one for the
986 @code{DEF} portion (@code{SSA_OP_VMAYDEF}) and one for the USE portion
987 (@code{SSA_OP_VMAYUSE}).  If all you want to look at are the
988 @code{V_MAY_DEF}s together, there is a fourth iterator macro for this,
989 which returns both a def_operand_p and a use_operand_p for each
990 @code{V_MAY_DEF} in the statement.  Note that you don't need any flags for
991 this one.
993 @smallexample
994   use_operand_p use_p;
995   def_operand_p def_p;
996   ssa_op_iter iter;
998   FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
999     @{
1000       my_code;
1001     @}
1002 @end smallexample
1004 @code{V_MUST_DEF}s are broken into two flags, one for the
1005 @code{DEF} portion (@code{SSA_OP_VMUSTDEF}) and one for the kill portion
1006 (@code{SSA_OP_VMUSTDEFKILL}).  If all you want to look at are the
1007 @code{V_MUST_DEF}s together, there is a fourth iterator macro for this,
1008 which returns both a def_operand_p and a use_operand_p for each
1009 @code{V_MUST_DEF} in the statement.  Note that you don't need any flags for
1010 this one.
1012 @smallexample
1013   use_operand_p kill_p;
1014   def_operand_p def_p;
1015   ssa_op_iter iter;
1017   FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, kill_p, stmt, iter)
1018     @{
1019       my_code;
1020     @}
1021 @end smallexample
1024 There are many examples in the code as well, as well as the
1025 documentation in @file{tree-ssa-operands.h}.
1028 @node SSA
1029 @section Static Single Assignment
1030 @cindex SSA
1031 @cindex static single assignment
1033 Most of the tree optimizers rely on the data flow information provided
1034 by the Static Single Assignment (SSA) form.  We implement the SSA form
1035 as described in @cite{R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and
1036 K. Zadeck.  Efficiently Computing Static Single Assignment Form and the
1037 Control Dependence Graph.  ACM Transactions on Programming Languages
1038 and Systems, 13(4):451-490, October 1991}.
1040 The SSA form is based on the premise that program variables are
1041 assigned in exactly one location in the program.  Multiple assignments
1042 to the same variable create new versions of that variable.  Naturally,
1043 actual programs are seldom in SSA form initially because variables
1044 tend to be assigned multiple times.  The compiler modifies the program
1045 representation so that every time a variable is assigned in the code,
1046 a new version of the variable is created.  Different versions of the
1047 same variable are distinguished by subscripting the variable name with
1048 its version number.  Variables used in the right-hand side of
1049 expressions are renamed so that their version number matches that of
1050 the most recent assignment.
1052 We represent variable versions using @code{SSA_NAME} nodes.  The
1053 renaming process in @file{tree-ssa.c} wraps every real and
1054 virtual operand with an @code{SSA_NAME} node which contains
1055 the version number and the statement that created the
1056 @code{SSA_NAME}.  Only definitions and virtual definitions may
1057 create new @code{SSA_NAME} nodes.
1059 Sometimes, flow of control makes it impossible to determine what is the
1060 most recent version of a variable.  In these cases, the compiler
1061 inserts an artificial definition for that variable called
1062 @dfn{PHI function} or @dfn{PHI node}.  This new definition merges
1063 all the incoming versions of the variable to create a new name
1064 for it.  For instance,
1066 @smallexample
1067 if (...)
1068   a_1 = 5;
1069 else if (...)
1070   a_2 = 2;
1071 else
1072   a_3 = 13;
1074 # a_4 = PHI <a_1, a_2, a_3>
1075 return a_4;
1076 @end smallexample
1078 Since it is not possible to determine which of the three branches
1079 will be taken at runtime, we don't know which of @code{a_1},
1080 @code{a_2} or @code{a_3} to use at the return statement.  So, the
1081 SSA renamer creates a new version @code{a_4} which is assigned
1082 the result of ``merging'' @code{a_1}, @code{a_2} and @code{a_3}.
1083 Hence, PHI nodes mean ``one of these operands.  I don't know
1084 which''.
1086 The following macros can be used to examine PHI nodes
1088 @defmac PHI_RESULT (@var{phi})
1089 Returns the @code{SSA_NAME} created by PHI node @var{phi} (i.e.,
1090 @var{phi}'s LHS)@.
1091 @end defmac
1093 @defmac PHI_NUM_ARGS (@var{phi})
1094 Returns the number of arguments in @var{phi}.  This number is exactly
1095 the number of incoming edges to the basic block holding @var{phi}@.
1096 @end defmac
1098 @defmac PHI_ARG_ELT (@var{phi}, @var{i})
1099 Returns a tuple representing the @var{i}th argument of @var{phi}@.
1100 Each element of this tuple contains an @code{SSA_NAME} @var{var} and
1101 the incoming edge through which @var{var} flows.
1102 @end defmac
1104 @defmac PHI_ARG_EDGE (@var{phi}, @var{i})
1105 Returns the incoming edge for the @var{i}th argument of @var{phi}.
1106 @end defmac
1108 @defmac PHI_ARG_DEF (@var{phi}, @var{i})
1109 Returns the @code{SSA_NAME} for the @var{i}th argument of @var{phi}.
1110 @end defmac
1113 @subsection Preserving the SSA form
1114 @findex vars_to_rename
1115 @cindex preserving SSA form
1116 Some optimization passes make changes to the function that
1117 invalidate the SSA property.  This can happen when a pass has
1118 added new variables or changed the program so that variables that
1119 were previously aliased aren't anymore.
1121 Whenever something like this happens, the affected variables must
1122 be renamed into SSA form again.  To do this, you should mark the
1123 new variables in the global bitmap @code{vars_to_rename}.  Once
1124 your pass has finished, the pass manager will invoke the SSA
1125 renamer to put the program into SSA once more.
1127 @subsection Examining @code{SSA_NAME} nodes
1128 @cindex examining SSA_NAMEs
1130 The following macros can be used to examine @code{SSA_NAME} nodes
1132 @defmac SSA_NAME_DEF_STMT (@var{var})
1133 Returns the statement @var{s} that creates the @code{SSA_NAME}
1134 @var{var}.  If @var{s} is an empty statement (i.e., @code{IS_EMPTY_STMT
1135 (@var{s})} returns @code{true}), it means that the first reference to
1136 this variable is a USE or a VUSE@.
1137 @end defmac
1139 @defmac SSA_NAME_VERSION (@var{var})
1140 Returns the version number of the @code{SSA_NAME} object @var{var}.
1141 @end defmac
1144 @subsection Walking use-def chains
1146 @deftypefn {Tree SSA function} void walk_use_def_chains (@var{var}, @var{fn}, @var{data})
1148 Walks use-def chains starting at the @code{SSA_NAME} node @var{var}.
1149 Calls function @var{fn} at each reaching definition found.  Function
1150 @var{FN} takes three arguments: @var{var}, its defining statement
1151 (@var{def_stmt}) and a generic pointer to whatever state information
1152 that @var{fn} may want to maintain (@var{data}).  Function @var{fn} is
1153 able to stop the walk by returning @code{true}, otherwise in order to
1154 continue the walk, @var{fn} should return @code{false}.
1156 Note, that if @var{def_stmt} is a @code{PHI} node, the semantics are
1157 slightly different.  For each argument @var{arg} of the PHI node, this
1158 function will:
1160 @enumerate
1161 @item   Walk the use-def chains for @var{arg}.
1162 @item   Call @code{FN (@var{arg}, @var{phi}, @var{data})}.
1163 @end enumerate
1165 Note how the first argument to @var{fn} is no longer the original
1166 variable @var{var}, but the PHI argument currently being examined.
1167 If @var{fn} wants to get at @var{var}, it should call
1168 @code{PHI_RESULT} (@var{phi}).
1169 @end deftypefn
1171 @subsection Walking the dominator tree
1173 @deftypefn {Tree SSA function} void walk_dominator_tree (@var{walk_data}, @var{bb})
1175 This function walks the dominator tree for the current CFG calling a
1176 set of callback functions defined in @var{struct dom_walk_data} in
1177 @file{domwalk.h}.  The call back functions you need to define give you
1178 hooks to execute custom code at various points during traversal:
1180 @enumerate
1181 @item Once to initialize any local data needed while processing
1182       @var{bb} and its children.  This local data is pushed into an
1183       internal stack which is automatically pushed and popped as the
1184       walker traverses the dominator tree.
1186 @item Once before traversing all the statements in the @var{bb}.
1188 @item Once for every statement inside @var{bb}.
1190 @item Once after traversing all the statements and before recursing
1191       into @var{bb}'s dominator children.
1193 @item It then recurses into all the dominator children of @var{bb}.
1195 @item After recursing into all the dominator children of @var{bb} it
1196       can, optionally, traverse every statement in @var{bb} again
1197       (i.e., repeating steps 2 and 3).
1199 @item Once after walking the statements in @var{bb} and @var{bb}'s
1200       dominator children.  At this stage, the block local data stack
1201       is popped.
1202 @end enumerate
1203 @end deftypefn
1205 @node Alias analysis
1206 @section Alias analysis
1207 @cindex alias
1208 @cindex flow-sensitive alias analysis
1209 @cindex flow-insensitive alias analysis
1211 Alias analysis proceeds in 4 main phases:
1213 @enumerate
1214 @item   Structural alias analysis.
1216 This phase walks the types for structure variables, and determines which
1217 of the fields can overlap using offset and size of each field.  For each
1218 field, a ``subvariable'' called a ``Structure field tag'' (SFT)@ is
1219 created, which represents that field as a separate variable.  All
1220 accesses that could possibly overlap with a given field will have
1221 virtual operands for the SFT of that field.
1223 @smallexample
1224 struct foo
1226   int a;
1227   int b;
1229 struct foo temp;
1230 int bar (void)
1232   int tmp1, tmp2, tmp3;
1233   SFT.0_2 = V_MUST_DEF <SFT.0_1>
1234   temp.a = 5;
1235   SFT.1_4 = V_MUST_DEF <SFT.1_3>
1236   temp.b = 6;
1237   
1238   VUSE <SFT.1_4>
1239   tmp1_5 = temp.b;
1240   VUSE <SFT.0_2>
1241   tmp2_6 = temp.a;
1243   tmp3_7 = tmp1_5 + tmp2_6;
1244   return tmp3_7;
1246 @end smallexample
1248 If you copy the type tag for a variable for some reason, you probably
1249 also want to copy the subvariables for that variable.
1251 @item   Points-to and escape analysis.
1253 This phase walks the use-def chains in the SSA web looking for
1254 three things:
1256         @itemize @bullet
1257         @item   Assignments of the form @code{P_i = &VAR}
1258         @item   Assignments of the form P_i = malloc()
1259         @item   Pointers and ADDR_EXPR that escape the current function.
1260         @end itemize
1262 The concept of `escaping' is the same one used in the Java world.
1263 When a pointer or an ADDR_EXPR escapes, it means that it has been
1264 exposed outside of the current function.  So, assignment to
1265 global variables, function arguments and returning a pointer are
1266 all escape sites.
1268 This is where we are currently limited.  Since not everything is
1269 renamed into SSA, we lose track of escape properties when a
1270 pointer is stashed inside a field in a structure, for instance.
1271 In those cases, we are assuming that the pointer does escape.
1273 We use escape analysis to determine whether a variable is
1274 call-clobbered.  Simply put, if an ADDR_EXPR escapes, then the
1275 variable is call-clobbered.  If a pointer P_i escapes, then all
1276 the variables pointed-to by P_i (and its memory tag) also escape.
1278 @item   Compute flow-sensitive aliases
1280 We have two classes of memory tags.  Memory tags associated with
1281 the pointed-to data type of the pointers in the program.  These
1282 tags are called ``type memory tag'' (TMT)@.  The other class are
1283 those associated with SSA_NAMEs, called ``name memory tag'' (NMT)@.
1284 The basic idea is that when adding operands for an INDIRECT_REF
1285 *P_i, we will first check whether P_i has a name tag, if it does
1286 we use it, because that will have more precise aliasing
1287 information.  Otherwise, we use the standard type tag.
1289 In this phase, we go through all the pointers we found in
1290 points-to analysis and create alias sets for the name memory tags
1291 associated with each pointer P_i.  If P_i escapes, we mark
1292 call-clobbered the variables it points to and its tag.
1295 @item   Compute flow-insensitive aliases
1297 This pass will compare the alias set of every type memory tag and
1298 every addressable variable found in the program.  Given a type
1299 memory tag TMT and an addressable variable V@.  If the alias sets
1300 of TMT and V conflict (as computed by may_alias_p), then V is
1301 marked as an alias tag and added to the alias set of TMT@.
1302 @end enumerate
1304 For instance, consider the following function:
1306 @smallexample
1307 foo (int i)
1309   int *p, *q, a, b;
1311   if (i > 10)
1312     p = &a;
1313   else
1314     q = &b;
1316   *p = 3;
1317   *q = 5;
1318   a = b + 2;
1319   return *p;
1321 @end smallexample
1323 After aliasing analysis has finished, the type memory tag for
1324 pointer @code{p} will have two aliases, namely variables @code{a} and
1325 @code{b}.
1326 Every time pointer @code{p} is dereferenced, we want to mark the
1327 operation as a potential reference to @code{a} and @code{b}.
1329 @smallexample
1330 foo (int i)
1332   int *p, a, b;
1334   if (i_2 > 10)
1335     p_4 = &a;
1336   else
1337     p_6 = &b;
1338   # p_1 = PHI <p_4(1), p_6(2)>;
1340   # a_7 = V_MAY_DEF <a_3>;
1341   # b_8 = V_MAY_DEF <b_5>;
1342   *p_1 = 3;
1344   # a_9 = V_MAY_DEF <a_7>
1345   # VUSE <b_8>
1346   a_9 = b_8 + 2;
1348   # VUSE <a_9>;
1349   # VUSE <b_8>;
1350   return *p_1;
1352 @end smallexample
1354 In certain cases, the list of may aliases for a pointer may grow
1355 too large.  This may cause an explosion in the number of virtual
1356 operands inserted in the code.  Resulting in increased memory
1357 consumption and compilation time.
1359 When the number of virtual operands needed to represent aliased
1360 loads and stores grows too large (configurable with @option{--param
1361 max-aliased-vops}), alias sets are grouped to avoid severe
1362 compile-time slow downs and memory consumption.  The alias
1363 grouping heuristic proceeds as follows:
1365 @enumerate
1366 @item Sort the list of pointers in decreasing number of contributed
1367 virtual operands.
1369 @item Take the first pointer from the list and reverse the role
1370 of the memory tag and its aliases.  Usually, whenever an
1371 aliased variable Vi is found to alias with a memory tag
1372 T, we add Vi to the may-aliases set for T@.  Meaning that
1373 after alias analysis, we will have:
1375 @smallexample
1376 may-aliases(T) = @{ V1, V2, V3, ..., Vn @}
1377 @end smallexample
1379 This means that every statement that references T, will get
1380 @code{n} virtual operands for each of the Vi tags.  But, when
1381 alias grouping is enabled, we make T an alias tag and add it
1382 to the alias set of all the Vi variables:
1384 @smallexample
1385 may-aliases(V1) = @{ T @}
1386 may-aliases(V2) = @{ T @}
1388 may-aliases(Vn) = @{ T @}
1389 @end smallexample
1391 This has two effects: (a) statements referencing T will only get
1392 a single virtual operand, and, (b) all the variables Vi will now
1393 appear to alias each other.  So, we lose alias precision to
1394 improve compile time.  But, in theory, a program with such a high
1395 level of aliasing should not be very optimizable in the first
1396 place.
1398 @item Since variables may be in the alias set of more than one
1399 memory tag, the grouping done in step (2) needs to be extended
1400 to all the memory tags that have a non-empty intersection with
1401 the may-aliases set of tag T@.  For instance, if we originally
1402 had these may-aliases sets:
1404 @smallexample
1405 may-aliases(T) = @{ V1, V2, V3 @}
1406 may-aliases(R) = @{ V2, V4 @}
1407 @end smallexample
1409 In step (2) we would have reverted the aliases for T as:
1411 @smallexample
1412 may-aliases(V1) = @{ T @}
1413 may-aliases(V2) = @{ T @}
1414 may-aliases(V3) = @{ T @}
1415 @end smallexample
1417 But note that now V2 is no longer aliased with R@.  We could
1418 add R to may-aliases(V2), but we are in the process of
1419 grouping aliases to reduce virtual operands so what we do is
1420 add V4 to the grouping to obtain:
1422 @smallexample
1423 may-aliases(V1) = @{ T @}
1424 may-aliases(V2) = @{ T @}
1425 may-aliases(V3) = @{ T @}
1426 may-aliases(V4) = @{ T @}
1427 @end smallexample
1429 @item If the total number of virtual operands due to aliasing is
1430 still above the threshold set by max-alias-vops, go back to (2).
1431 @end enumerate