OSDN Git Service

* config/arm/arm.c (arm_init_libfuncs): Clear mod optabs.
[pf3gnuchains/gcc-fork.git] / gcc / doc / tree-ssa.texi
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.
5
6 @c ---------------------------------------------------------------------
7 @c Tree SSA
8 @c ---------------------------------------------------------------------
9
10 @node Tree SSA
11 @chapter Analysis and Optimization of GIMPLE Trees
12 @cindex Tree SSA
13 @cindex Optimization infrastructure for GIMPLE
14
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@.
21
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.
32
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@.
38
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
47
48 @node GENERIC
49 @section GENERIC
50 @cindex GENERIC
51
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@.
57
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.
64
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.
71
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@.
79
80 @node GIMPLE
81 @section GIMPLE
82 @cindex GIMPLE
83
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 though we have made some different choices.  For one thing, SIMPLE
88 doesn't support @code{goto}; a production compiler can't afford that
89 kind of restriction.
90
91 GIMPLE retains much of the structure of the parse trees: lexical
92 scopes are represented as containers, rather than markers.  However,
93 expressions are broken down into a 3-address form, using temporary
94 variables to hold intermediate values.  Also, control structures are
95 lowered to gotos.
96
97 In GIMPLE no container node is ever used for its value; if a
98 @code{COND_EXPR} or @code{BIND_EXPR} has a value, it is stored into a
99 temporary within the controlled blocks, and that temporary is used in
100 place of the container.
101
102 The compiler pass which lowers GENERIC to GIMPLE is referred to as the
103 @samp{gimplifier}.  The gimplifier works recursively, replacing complex
104 statements with sequences of simple statements.
105
106 @c Currently, the only way to
107 @c tell whether or not an expression is in GIMPLE form is by recursively
108 @c examining it; in the future there will probably be a flag to help avoid
109 @c redundant work.  FIXME FIXME
110
111 @menu
112 * Interfaces::
113 * Temporaries::
114 * GIMPLE Expressions::
115 * Statements::
116 * GIMPLE Example::
117 * Rough GIMPLE Grammar::
118 @end menu
119
120 @node Interfaces
121 @subsection Interfaces
122 @cindex gimplification
123
124 The tree representation of a function is stored in
125 @code{DECL_SAVED_TREE}.  It is lowered to GIMPLE by a call to
126 @code{gimplify_function_tree}.
127
128 If a front end wants to include language-specific tree codes in the tree
129 representation which it provides to the back end, it must provide a
130 definition of @code{LANG_HOOKS_GIMPLIFY_EXPR} which knows how to
131 convert the front end trees to GIMPLE@.  Usually such a hook will involve
132 much of the same code for expanding front end trees to RTL@.  This function
133 can return fully lowered GIMPLE, or it can return GENERIC trees and let the
134 main gimplifier lower them the rest of the way; this is often simpler.
135
136 The C and C++ front ends currently convert directly from front end
137 trees to GIMPLE, and hand that off to the back end rather than first
138 converting to GENERIC@.  Their gimplifier hooks know about all the
139 @code{_STMT} nodes and how to convert them to GENERIC forms.  There
140 was some work done on a genericization pass which would run first, but
141 the existence of @code{STMT_EXPR} meant that in order to convert all
142 of the C statements into GENERIC equivalents would involve walking the
143 entire tree anyway, so it was simpler to lower all the way.  This
144 might change in the future if someone writes an optimization pass
145 which would work better with higher-level trees, but currently the
146 optimizers all expect GIMPLE@.
147
148 A front end which wants to use the tree optimizers (and already has
149 some sort of whole-function tree representation) only needs to provide
150 a definition of @code{LANG_HOOKS_GIMPLIFY_EXPR}, call
151 @code{gimplify_function_tree} to lower to GIMPLE, and then hand off to
152 @code{tree_rest_of_compilation} to compile and output the function.
153
154 You can tell the compiler to dump a C-like representation of the GIMPLE
155 form with the flag @option{-fdump-tree-gimple}.
156
157 @node Temporaries
158 @subsection Temporaries
159 @cindex Temporaries
160
161 When gimplification encounters a subexpression which is too complex, it
162 creates a new temporary variable to hold the value of the subexpression,
163 and adds a new statement to initialize it before the current statement.
164 These special temporaries are known as @samp{expression temporaries}, and are
165 allocated using @code{get_formal_tmp_var}.  The compiler tries to
166 always evaluate identical expressions into the same temporary, to simplify
167 elimination of redundant calculations.
168
169 We can only use expression temporaries when we know that it will not be
170 reevaluated before its value is used, and that it will not be otherwise
171 modified@footnote{These restrictions are derived from those in Morgan 4.8.}.
172 Other temporaries can be allocated using
173 @code{get_initialized_tmp_var} or @code{create_tmp_var}.
174
175 Currently, an expression like @code{a = b + 5} is not reduced any
176 further.  We tried converting it to something like
177 @smallexample
178   T1 = b + 5;
179   a = T1;
180 @end smallexample
181 but this bloated the representation for minimal benefit.  However, a
182 variable which must live in memory cannot appear in an expression; its
183 value is explicitly loaded into a temporary first.  Similarly, storing
184 the value of an expression to a memory variable goes through a
185 temporary.
186
187 @node GIMPLE Expressions
188 @subsection Expressions
189 @cindex GIMPLE Expressions
190
191 In general, expressions in GIMPLE consist of an operation and the
192 appropriate number of simple operands; these operands must either be a
193 GIMPLE rvalue (@code{is_gimple_val}), i.e.@: a constant or a register
194 variable.  More complex operands are factored out into temporaries, so
195 that
196 @smallexample
197   a = b + c + d
198 @end smallexample
199 becomes
200 @smallexample
201   T1 = b + c;
202   a = T1 + d;
203 @end smallexample
204
205 The same rule holds for arguments to a @code{CALL_EXPR}.
206
207 The target of an assignment is usually a variable, but can also be an
208 @code{INDIRECT_REF} or a compound lvalue as described below.
209
210 @menu
211 * Compound Expressions::
212 * Compound Lvalues::
213 * Conditional Expressions::
214 * Logical Operators::
215 @end menu
216
217 @node Compound Expressions
218 @subsubsection Compound Expressions
219 @cindex Compound Expressions
220
221 The left-hand side of a C comma expression is simply moved into a separate
222 statement.
223
224 @node Compound Lvalues
225 @subsubsection Compound Lvalues
226 @cindex Compound Lvalues
227
228 Currently compound lvalues involving array and structure field references
229 are not broken down; an expression like @code{a.b[2] = 42} is not reduced
230 any further (though complex array subscripts are).  This restriction is a
231 workaround for limitations in later optimizers; if we were to convert this
232 to
233
234 @smallexample
235   T1 = &a.b;
236   T1[2] = 42;
237 @end smallexample
238
239 alias analysis would not remember that the reference to @code{T1[2]} came
240 by way of @code{a.b}, so it would think that the assignment could alias
241 another member of @code{a}; this broke @code{struct-alias-1.c}.  Future
242 optimizer improvements may make this limitation unnecessary.
243
244 @node Conditional Expressions
245 @subsubsection Conditional Expressions
246 @cindex Conditional Expressions
247
248 A C @code{?:} expression is converted into an @code{if} statement with
249 each branch assigning to the same temporary.  So,
250
251 @smallexample
252   a = b ? c : d;
253 @end smallexample
254 becomes
255 @smallexample
256   if (b)
257     T1 = c;
258   else
259     T1 = d;
260   a = T1;
261 @end smallexample
262
263 Tree level if-conversion pass re-introduces @code{?:} expression, if appropriate.
264 It is used to vectorize loops with conditions using vector conditional operations.
265
266 Note that in GIMPLE, @code{if} statements are also represented using
267 @code{COND_EXPR}, as described below.
268
269 @node Logical Operators
270 @subsubsection Logical Operators
271 @cindex Logical Operators
272
273 Except when they appear in the condition operand of a @code{COND_EXPR},
274 logical `and' and `or' operators are simplified as follows:
275 @code{a = b && c} becomes
276
277 @smallexample
278   T1 = (bool)b;
279   if (T1)
280     T1 = (bool)c;
281   a = T1;
282 @end smallexample
283
284 Note that @code{T1} in this example cannot be an expression temporary,
285 because it has two different assignments.
286
287 @node Statements
288 @subsection Statements
289 @cindex Statements
290
291 Most statements will be assignment statements, represented by
292 @code{MODIFY_EXPR}.  A @code{CALL_EXPR} whose value is ignored can
293 also be a statement.  No other C expressions can appear at statement level;
294 a reference to a volatile object is converted into a @code{MODIFY_EXPR}.
295 In GIMPLE form, type of @code{MODIFY_EXPR} is not meaningful.  Instead, use type
296 of LHS or RHS@.
297
298 There are also several varieties of complex statements.
299
300 @menu
301 * Blocks::
302 * Statement Sequences::
303 * Empty Statements::
304 * Loops::
305 * Selection Statements::
306 * Jumps::
307 * Cleanups::
308 * GIMPLE Exception Handling::
309 @end menu
310
311 @node Blocks
312 @subsubsection Blocks
313 @cindex Blocks
314
315 Block scopes and the variables they declare in GENERIC and GIMPLE are
316 expressed using the @code{BIND_EXPR} code, which in previous versions of
317 GCC was primarily used for the C statement-expression extension.
318
319 Variables in a block are collected into @code{BIND_EXPR_VARS} in
320 declaration order.  Any runtime initialization is moved out of
321 @code{DECL_INITIAL} and into a statement in the controlled block.  When
322 gimplifying from C or C++, this initialization replaces the
323 @code{DECL_STMT}.
324
325 Variable-length arrays (VLAs) complicate this process, as their size often
326 refers to variables initialized earlier in the block.  To handle this, we
327 currently split the block at that point, and move the VLA into a new, inner
328 @code{BIND_EXPR}.  This strategy may change in the future.
329
330 @code{DECL_SAVED_TREE} for a GIMPLE function will always be a
331 @code{BIND_EXPR} which contains declarations for the temporary variables
332 used in the function.
333
334 A C++ program will usually contain more @code{BIND_EXPR}s than there are
335 syntactic blocks in the source code, since several C++ constructs have
336 implicit scopes associated with them.  On the other hand, although the C++
337 front end uses pseudo-scopes to handle cleanups for objects with
338 destructors, these don't translate into the GIMPLE form; multiple
339 declarations at the same level use the same @code{BIND_EXPR}.
340
341 @node Statement Sequences
342 @subsubsection Statement Sequences
343 @cindex Statement Sequences
344
345 Multiple statements at the same nesting level are collected into a
346 @code{STATEMENT_LIST}.  Statement lists are modified and traversed
347 using the interface in @samp{tree-iterator.h}.
348
349 @node Empty Statements
350 @subsubsection Empty Statements
351 @cindex Empty Statements
352
353 Whenever possible, statements with no effect are discarded.  But if they
354 are nested within another construct which cannot be discarded for some
355 reason, they are instead replaced with an empty statement, generated by
356 @code{build_empty_stmt}.  Initially, all empty statements were shared,
357 after the pattern of the Java front end, but this caused a lot of trouble in
358 practice.
359
360 An empty statement is represented as @code{(void)0}.
361
362 @node Loops
363 @subsubsection Loops
364 @cindex Loops
365
366 At one time loops were expressed in GIMPLE using @code{LOOP_EXPR}, but
367 now they are lowered to explicit gotos.
368
369 @node Selection Statements
370 @subsubsection Selection Statements
371 @cindex Selection Statements
372
373 A simple selection statement, such as the C @code{if} statement, is
374 expressed in GIMPLE using a void @code{COND_EXPR}.  If only one branch is
375 used, the other is filled with an empty statement.
376
377 Normally, the condition expression is reduced to a simple comparison.  If
378 it is a shortcut (@code{&&} or @code{||}) expression, however, we try to
379 break up the @code{if} into multiple @code{if}s so that the implied shortcut
380 is taken directly, much like the transformation done by @code{do_jump} in
381 the RTL expander.
382
383 A @code{SWITCH_EXPR} in GIMPLE contains the condition and a
384 @code{TREE_VEC} of @code{CASE_LABEL_EXPR}s describing the case values
385 and corresponding @code{LABEL_DECL}s to jump to.  The body of the
386 @code{switch} is moved after the @code{SWITCH_EXPR}.
387
388 @node Jumps
389 @subsubsection Jumps
390 @cindex Jumps
391
392 Other jumps are expressed by either @code{GOTO_EXPR} or @code{RETURN_EXPR}.
393
394 The operand of a @code{GOTO_EXPR} must be either a label or a variable
395 containing the address to jump to.
396
397 The operand of a @code{RETURN_EXPR} is either @code{NULL_TREE} or a
398 @code{MODIFY_EXPR} which sets the return value.  It would be nice to
399 move the @code{MODIFY_EXPR} into a separate statement, but the special
400 return semantics in @code{expand_return} make that difficult.  It may
401 still happen in the future, perhaps by moving most of that logic into
402 @code{expand_assignment}.
403
404 @node Cleanups
405 @subsubsection Cleanups
406 @cindex Cleanups
407
408 Destructors for local C++ objects and similar dynamic cleanups are
409 represented in GIMPLE by a @code{TRY_FINALLY_EXPR}.  When the controlled
410 block exits, the cleanup is run.
411
412 @code{TRY_FINALLY_EXPR} complicates the flow graph, since the cleanup
413 needs to appear on every edge out of the controlled block; this
414 reduces the freedom to move code across these edges.  Therefore, the
415 EH lowering pass which runs before most of the optimization passes
416 eliminates these expressions by explicitly adding the cleanup to each
417 edge.
418
419 @node GIMPLE Exception Handling
420 @subsubsection Exception Handling
421 @cindex GIMPLE Exception Handling
422
423 Other exception handling constructs are represented using
424 @code{TRY_CATCH_EXPR}.  The handler operand of a @code{TRY_CATCH_EXPR}
425 can be a normal statement to be executed if the controlled block throws an
426 exception, or it can have one of two special forms:
427
428 @enumerate
429 @item A @code{CATCH_EXPR} executes its handler if the thrown exception
430   matches one of the allowed types.  Multiple handlers can be
431   expressed by a sequence of @code{CATCH_EXPR} statements.
432 @item An @code{EH_FILTER_EXPR} executes its handler if the thrown
433   exception does not match one of the allowed types.
434 @end enumerate
435
436 Currently throwing an exception is not directly represented in GIMPLE,
437 since it is implemented by calling a function.  At some point in the future
438 we will want to add some way to express that the call will throw an
439 exception of a known type.
440
441 Just before running the optimizers, the compiler lowers the high-level
442 EH constructs above into a set of @samp{goto}s, magic labels, and EH
443 regions.  Continuing to unwind at the end of a cleanup is represented
444 with a @code{RESX_EXPR}.
445
446 @node GIMPLE Example
447 @subsection GIMPLE Example
448 @cindex GIMPLE Example
449
450 @smallexample
451 struct A @{ A(); ~A(); @};
452
453 int i;
454 int g();
455 void f()
456 @{
457   A a;
458   int j = (--i, i ? 0 : 1);
459
460   for (int x = 42; x > 0; --x)
461     @{
462       i += g()*4 + 32;
463     @}
464 @}
465 @end smallexample
466
467 becomes
468
469 @smallexample
470 void f()
471 @{
472   int i.0;
473   int T.1;
474   int iftmp.2;
475   int T.3;
476   int T.4;
477   int T.5;
478   int T.6;
479
480   @{
481     struct A a;
482     int j;
483
484     __comp_ctor (&a);
485     try
486       @{
487         i.0 = i;
488         T.1 = i.0 - 1;
489         i = T.1;
490         i.0 = i;
491         if (i.0 == 0)
492           iftmp.2 = 1;
493         else
494           iftmp.2 = 0;
495         j = iftmp.2;
496         @{
497           int x;
498
499           x = 42;
500           goto test;
501           loop:;
502
503           T.3 = g ();
504           T.4 = T.3 * 4;
505           i.0 = i;
506           T.5 = T.4 + i.0;
507           T.6 = T.5 + 32;
508           i = T.6;
509           x = x - 1;
510
511           test:;
512           if (x > 0)
513             goto loop;
514           else
515             goto break_;
516           break_:;
517         @}
518       @}
519     finally
520       @{
521         __comp_dtor (&a);
522       @}
523   @}
524 @}
525 @end smallexample
526
527 @node Rough GIMPLE Grammar
528 @subsection Rough GIMPLE Grammar
529 @cindex Rough GIMPLE Grammar
530
531 @smallexample
532    function     : FUNCTION_DECL
533                         DECL_SAVED_TREE -> compound-stmt
534
535    compound-stmt: STATEMENT_LIST
536                         members -> stmt
537
538    stmt         : block
539                 | if-stmt
540                 | switch-stmt
541                 | goto-stmt
542                 | return-stmt
543                 | resx-stmt
544                 | label-stmt
545                 | try-stmt
546                 | modify-stmt
547                 | call-stmt
548
549    block        : BIND_EXPR
550                         BIND_EXPR_VARS -> chain of DECLs
551                         BIND_EXPR_BLOCK -> BLOCK
552                         BIND_EXPR_BODY -> compound-stmt
553
554    if-stmt      : COND_EXPR
555                         op0 -> condition
556                         op1 -> compound-stmt
557                         op2 -> compound-stmt
558
559    switch-stmt  : SWITCH_EXPR
560                         op0 -> val
561                         op1 -> NULL
562                         op2 -> TREE_VEC of CASE_LABEL_EXPRs
563                             The CASE_LABEL_EXPRs are sorted by CASE_LOW,
564                             and default is last.
565
566    goto-stmt    : GOTO_EXPR
567                         op0 -> LABEL_DECL | val
568
569    return-stmt  : RETURN_EXPR
570                         op0 -> return-value
571
572    return-value : NULL
573                 | RESULT_DECL
574                 | MODIFY_EXPR
575                         op0 -> RESULT_DECL
576                         op1 -> lhs
577
578    resx-stmt    : RESX_EXPR
579
580    label-stmt   : LABEL_EXPR
581                         op0 -> LABEL_DECL
582
583    try-stmt     : TRY_CATCH_EXPR
584                         op0 -> compound-stmt
585                         op1 -> handler
586                 | TRY_FINALLY_EXPR
587                         op0 -> compound-stmt
588                         op1 -> compound-stmt
589
590    handler      : catch-seq
591                 | EH_FILTER_EXPR
592                 | compound-stmt
593
594    catch-seq    : STATEMENT_LIST
595                         members -> CATCH_EXPR
596
597    modify-stmt  : MODIFY_EXPR
598                         op0 -> lhs
599                         op1 -> rhs
600
601    call-stmt    : CALL_EXPR
602                         op0 -> val | OBJ_TYPE_REF
603                         op1 -> call-arg-list
604
605    call-arg-list: TREE_LIST
606                         members -> lhs | CONST
607
608    addr-expr-arg: ID
609                 | compref
610
611    addressable  : addr-expr-arg
612                 | indirectref
613
614    with-size-arg: addressable
615                 | call-stmt
616
617    indirectref  : INDIRECT_REF
618                         op0 -> val
619
620    lhs          : addressable
621                 | bitfieldref
622                 | WITH_SIZE_EXPR
623                         op0 -> with-size-arg
624                         op1 -> val
625
626    min-lval     : ID
627                 | indirectref
628
629    bitfieldref  : BIT_FIELD_REF
630                         op0 -> inner-compref
631                         op1 -> CONST
632                         op2 -> var
633
634    compref      : inner-compref
635                 | REALPART_EXPR
636                         op0 -> inner-compref
637                 | IMAGPART_EXPR
638                         op0 -> inner-compref
639
640    inner-compref: min-lval
641                 | COMPONENT_REF
642                         op0 -> inner-compref
643                         op1 -> FIELD_DECL
644                         op2 -> val
645                 | ARRAY_REF
646                         op0 -> inner-compref
647                         op1 -> val
648                         op2 -> val
649                         op3 -> val
650                 | ARRAY_RANGE_REF
651                         op0 -> inner-compref
652                         op1 -> val
653                         op2 -> val
654                         op3 -> val
655                 | VIEW_CONVERT_EXPR
656                         op0 -> inner-compref
657
658    condition    : val
659                 | RELOP
660                         op0 -> val
661                         op1 -> val
662
663    val          : ID
664                 | CONST
665
666    rhs          : lhs
667                 | CONST
668                 | call-stmt
669                 | ADDR_EXPR
670                         op0 -> addr-expr-arg
671                 | UNOP
672                         op0 -> val
673                 | BINOP
674                         op0 -> val
675                         op1 -> val
676                 | RELOP
677                         op0 -> val
678                         op1 -> val
679 @end smallexample
680
681 @node Annotations
682 @section Annotations
683 @cindex annotations
684
685 The optimizers need to associate attributes with statements and
686 variables during the optimization process.  For instance, we need to
687 know what basic block a statement belongs to or whether a variable
688 has aliases.  All these attributes are stored in data structures
689 called annotations which are then linked to the field @code{ann} in
690 @code{struct tree_common}.
691
692 Presently, we define annotations for statements (@code{stmt_ann_t}),
693 variables (@code{var_ann_t}) and SSA names (@code{ssa_name_ann_t}).
694 Annotations are defined and documented in @file{tree-flow.h}.
695
696
697 @node Statement Operands
698 @section Statement Operands
699 @cindex operands
700 @cindex virtual operands
701 @cindex real operands
702 @findex update_stmt
703
704 Almost every GIMPLE statement will contain a reference to a variable
705 or memory location.  Since statements come in different shapes and
706 sizes, their operands are going to be located at various spots inside
707 the statement's tree.  To facilitate access to the statement's
708 operands, they are organized into arrays associated inside each
709 statement's annotation.  Each element in an operand array is a pointer
710 to a @code{VAR_DECL}, @code{PARM_DECL} or @code{SSA_NAME} tree node.
711 This provides a very convenient way of examining and replacing
712 operands.
713
714 Data flow analysis and optimization is done on all tree nodes
715 representing variables.  Any node for which @code{SSA_VAR_P} returns
716 nonzero is considered when scanning statement operands.  However, not
717 all @code{SSA_VAR_P} variables are processed in the same way.  For the
718 purposes of optimization, we need to distinguish between references to
719 local scalar variables and references to globals, statics, structures,
720 arrays, aliased variables, etc.  The reason is simple, the compiler
721 can gather complete data flow information for a local scalar.  On the
722 other hand, a global variable may be modified by a function call, it
723 may not be possible to keep track of all the elements of an array or
724 the fields of a structure, etc.
725
726 The operand scanner gathers two kinds of operands: @dfn{real} and
727 @dfn{virtual}.  An operand for which @code{is_gimple_reg} returns true
728 is considered real, otherwise it is a virtual operand.  We also
729 distinguish between uses and definitions.  An operand is used if its
730 value is loaded by the statement (e.g., the operand at the RHS of an
731 assignment).  If the statement assigns a new value to the operand, the
732 operand is considered a definition (e.g., the operand at the LHS of
733 an assignment).
734
735 Virtual and real operands also have very different data flow
736 properties.  Real operands are unambiguous references to the
737 full object that they represent.  For instance, given
738
739 @smallexample
740 @{
741   int a, b;
742   a = b
743 @}
744 @end smallexample
745
746 Since @code{a} and @code{b} are non-aliased locals, the statement
747 @code{a = b} will have one real definition and one real use because
748 variable @code{b} is completely modified with the contents of
749 variable @code{a}.  Real definition are also known as @dfn{killing
750 definitions}.  Similarly, the use of @code{a} reads all its bits.
751
752 In contrast, virtual operands are used with variables that can have
753 a partial or ambiguous reference.  This includes structures, arrays,
754 globals, and aliased variables.  In these cases, we have two types of
755 definitions.  For globals, structures, and arrays, we can determine from
756 a statement whether a variable of these types has a killing definition.
757 If the variable does, then the statement is marked as having a
758 @dfn{must definition} of that variable.  However, if a statement is only
759 defining a part of the variable (i.e.@: a field in a structure), or if we
760 know that a statement might define the variable but we cannot say for sure,
761 then we mark that statement as having a @dfn{may definition}.  For
762 instance, given
763
764 @smallexample
765 @{
766   int a, b, *p;
767
768   if (...)
769     p = &a;
770   else
771     p = &b;
772   *p = 5;
773   return *p;
774 @}
775 @end smallexample
776
777 The assignment @code{*p = 5} may be a definition of @code{a} or
778 @code{b}.  If we cannot determine statically where @code{p} is
779 pointing to at the time of the store operation, we create virtual
780 definitions to mark that statement as a potential definition site for
781 @code{a} and @code{b}.  Memory loads are similarly marked with virtual
782 use operands.  Virtual operands are shown in tree dumps right before
783 the statement that contains them.  To request a tree dump with virtual
784 operands, use the @option{-vops} option to @option{-fdump-tree}:
785
786 @smallexample
787 @{
788   int a, b, *p;
789
790   if (...)
791     p = &a;
792   else
793     p = &b;
794   # a = V_MAY_DEF <a>
795   # b = V_MAY_DEF <b>
796   *p = 5;
797
798   # VUSE <a>
799   # VUSE <b>
800   return *p;
801 @}
802 @end smallexample
803
804 Notice that @code{V_MAY_DEF} operands have two copies of the referenced
805 variable.  This indicates that this is not a killing definition of
806 that variable.  In this case we refer to it as a @dfn{may definition}
807 or @dfn{aliased store}.  The presence of the second copy of the
808 variable in the @code{V_MAY_DEF} operand will become important when the
809 function is converted into SSA form.  This will be used to link all
810 the non-killing definitions to prevent optimizations from making
811 incorrect assumptions about them.
812
813 Operands are collected by @file{tree-ssa-operands.c}.  They are stored
814 inside each statement's annotation and can be accessed with
815 @code{DEF_OPS}, @code{USE_OPS}, @code{V_MAY_DEF_OPS},
816 @code{V_MUST_DEF_OPS} and @code{VUSE_OPS}.  The following are all the
817 accessor macros available to access USE operands.  To access all the
818 other operand arrays, just change the name accordingly.  Note that
819 this interface to the operands is deprecated, and is slated for
820 removal in a future version of gcc.  The preferred interface is the
821 operand iterator interface.  Unless you need to discover the number of
822 operands of a given type on a statement, you are strongly urged not to
823 use this interface.
824
825 @defmac USE_OPS (@var{ann})
826 Returns the array of operands used by the statement with annotation
827 @var{ann}.
828 @end defmac
829
830 @defmac STMT_USE_OPS (@var{stmt})
831 Alternate version of USE_OPS that takes the statement @var{stmt} as
832 input.
833 @end defmac
834
835 @defmac NUM_USES (@var{ops})
836 Return the number of USE operands in array @var{ops}.
837 @end defmac
838
839 @defmac USE_OP_PTR (@var{ops}, @var{i})
840 Return a pointer to the @var{i}th operand in array @var{ops}.
841 @end defmac
842
843 @defmac USE_OP (@var{ops}, @var{i})
844 Return the @var{i}th operand in array @var{ops}.
845 @end defmac
846
847 The following function shows how to print all the operands of a given
848 statement:
849
850 @smallexample
851 void
852 print_ops (tree stmt)
853 @{
854   vuse_optype vuses;
855   v_may_def_optype v_may_defs;
856   v_must_def_optype v_must_defs;
857   def_optype defs;
858   use_optype uses;
859   stmt_ann_t ann;
860   size_t i;
861
862   ann = stmt_ann (stmt);
863
864   defs = DEF_OPS (ann);
865   for (i = 0; i < NUM_DEFS (defs); i++)
866     print_generic_expr (stderr, DEF_OP (defs, i), 0);
867
868   uses = USE_OPS (ann);
869   for (i = 0; i < NUM_USES (uses); i++)
870     print_generic_expr (stderr, USE_OP (uses, i), 0);
871
872   v_may_defs = V_MAY_DEF_OPS (ann);
873   for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
874     @{
875       print_generic_expr (stderr, V_MAY_DEF_OP (v_may_defs, i), 0);
876       print_generic_expr (stderr, V_MAY_DEF_RESULT (v_may_defs, i), 0);
877     @}
878
879   v_must_defs = V_MUST_DEF_OPS (ann);
880   for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
881     print_generic_expr (stderr, V_MUST_DEF_OP (v_must_defs, i), 0);
882
883   vuses = VUSE_OPS (ann);
884   for (i = 0; i < NUM_VUSES (vuses); i++)
885     print_generic_expr (stderr, VUSE_OP (vuses, i), 0);
886 @}
887 @end smallexample
888
889 Operands are updated as soon as the statement is finished via a call
890 to @code{update_stmt}.  If statement elements are changed via
891 @code{SET_USE} or @code{SET_DEF}, then no further action is required
892 (ie, those macros take care of updating the statement).  If changes
893 are made by manipulating the statement's tree directly, then a call
894 must be made to @code{update_stmt} when complete.  Calling one of the
895 @code{bsi_insert} routines or @code{bsi_replace} performs an implicit
896 call to @code{update_stmt}.
897
898 @subsection Operand Iterators
899 @cindex Operand Iterators
900
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:
905
906 @smallexample
907 void
908 print_ops (tree stmt)
909 @{
910   ssa_op_iter;
911   tree var;
912
913   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
914     print_generic_expr (stderr, var, 0);
915 @}
916 @end smallexample
917
918
919 @enumerate
920 @item Determine whether you are need to see the operand pointers, or just the
921     trees, and choose the appropriate macro:
922
923 @smallexample
924 Need            Macro:
925 ----            -------
926 use_operand_p   FOR_EACH_SSA_USE_OPERAND
927 def_operand_p   FOR_EACH_SSA_DEF_OPERAND
928 tree            FOR_EACH_SSA_TREE_OPERAND
929 @end smallexample
930
931 @item You need to declare a variable of the type you are interested
932     in, and an ssa_op_iter structure which serves as the loop
933     controlling variable.
934
935 @item Determine which operands you wish to use, and specify the flags of
936     those you are interested in.  They are documented in
937     @file{tree-ssa-operands.h}:
938
939 @smallexample
940 #define SSA_OP_USE              0x01    /* @r{Real USE operands.}  */
941 #define SSA_OP_DEF              0x02    /* @r{Real DEF operands.}  */
942 #define SSA_OP_VUSE             0x04    /* @r{VUSE operands.}  */
943 #define SSA_OP_VMAYUSE          0x08    /* @r{USE portion of V_MAY_DEFS.}  */
944 #define SSA_OP_VMAYDEF          0x10    /* @r{DEF portion of V_MAY_DEFS.}  */
945 #define SSA_OP_VMUSTDEF         0x20    /* @r{V_MUST_DEF definitions.}  */
946
947 /* @r{These are commonly grouped operand flags.}  */
948 #define SSA_OP_VIRTUAL_USES     (SSA_OP_VUSE | SSA_OP_VMAYUSE)
949 #define SSA_OP_VIRTUAL_DEFS     (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF)
950 #define SSA_OP_ALL_USES         (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
951 #define SSA_OP_ALL_DEFS         (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
952 #define SSA_OP_ALL_OPERANDS     (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
953 @end smallexample
954 @end enumerate
955
956 So if you want to look at the use pointers for all the @code{USE} and
957 @code{VUSE} operands, you would do something like:
958
959 @smallexample
960   use_operand_p use_p;
961   ssa_op_iter iter;
962
963   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
964     @{
965       process_use_ptr (use_p);
966     @}
967 @end smallexample
968
969 The @code{_TREE_} macro is basically the same as the @code{USE} and
970 @code{DEF} macros, only with the use or def dereferenced via
971 @code{USE_FROM_PTR (use_p)} and @code{DEF_FROM_PTR (def_p)}.  Since we
972 aren't using operand pointers, use and defs flags can be mixed.
973
974 @smallexample
975   tree var;
976   ssa_op_iter iter;
977
978   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE | SSA_OP_VMUSTDEF)
979     @{
980        print_generic_expr (stderr, var, TDF_SLIM);
981     @}
982 @end smallexample
983
984 @code{V_MAY_DEF}s are broken into two flags, one for the
985 @code{DEF} portion (@code{SSA_OP_VMAYDEF}) and one for the USE portion
986 (@code{SSA_OP_VMAYUSE}).  If all you want to look at are the
987 @code{V_MAY_DEF}s together, there is a fourth iterator macro for this,
988 which returns both a def_operand_p and a use_operand_p for each
989 @code{V_MAY_DEF} in the statement.  Note that you don't need any flags for
990 this one.
991
992 @smallexample
993   use_operand_p use_p;
994   def_operand_p def_p;
995   ssa_op_iter iter;
996
997   FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
998     @{
999       my_code;
1000     @}
1001 @end smallexample
1002
1003 @code{V_MUST_DEF}s are broken into two flags, one for the
1004 @code{DEF} portion (@code{SSA_OP_VMUSTDEF}) and one for the kill portion
1005 (@code{SSA_OP_VMUSTDEFKILL}).  If all you want to look at are the
1006 @code{V_MUST_DEF}s together, there is a fourth iterator macro for this,
1007 which returns both a def_operand_p and a use_operand_p for each
1008 @code{V_MUST_DEF} in the statement.  Note that you don't need any flags for
1009 this one.
1010
1011 @smallexample
1012   use_operand_p kill_p;
1013   def_operand_p def_p;
1014   ssa_op_iter iter;
1015
1016   FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, kill_p, stmt, iter)
1017     @{
1018       my_code;
1019     @}
1020 @end smallexample
1021
1022
1023 There are many examples in the code as well, as well as the
1024 documentation in @file{tree-ssa-operands.h}.
1025
1026
1027 @subsection Immediate Uses
1028 @cindex Immediate Uses
1029
1030 Immediate use information is now always available.  Using the immediate use 
1031 iterators, you may examine every use of any @code{SSA_NAME}. For instance,
1032 to change each use of @code{ssa_var} to @code{ssa_var2}:
1033
1034 @smallexample
1035   use_operand_p imm_use_p;
1036   imm_use_iterator iterator;
1037   tree ssa_var
1038
1039   FOR_EACH_IMM_USE_SAFE (imm_use_p, iterator, ssa_var)
1040     SET_USE (imm_use_p, ssa_var_2);
1041 @end smallexample
1042
1043 There are 2 iterators which can be used. @code{FOR_EACH_IMM_USE_FAST} is used 
1044 when the immediate uses are not changed, ie. you are looking at the uses, but 
1045 not setting them.  
1046
1047 If they do get changed, then care must be taken that things are not changed 
1048 under the iterators, so use the @code{FOR_EACH_IMM_USE_SAFE} iterator.  It 
1049 attempts to preserve the sanity of the use list by moving an iterator element
1050 through the use list, preventing insertions and deletions in the list from
1051 resulting in invalid pointers.  This is a little slower since it adds a
1052 placeholder element and moves it through the list.  This element must be 
1053 also be removed if the loop is terminated early.  A macro 
1054 (@code{BREAK_FROM SAFE_IMM_USE} is provided for this:
1055
1056 @smallexample
1057   FOR_EACH_IMM_USE_SAFE (use_p, iter, var)
1058     @{
1059       if (var == last_var)
1060         BREAK_FROM_SAFE_IMM_USE (iter);
1061       else
1062         SET_USE (use_p, var2);
1063     @}
1064 @end smallexample
1065
1066 There are checks in @code{verify_ssa} which verify that the immediate use list
1067 is up to date, as well as checking that an optimization didn't break from the 
1068 loop without using this macro.  It is safe to simply 'break'; from a 
1069 @code{FOR_EACH_IMM_USE_FAST} traverse.
1070
1071 Some useful functions and macros:
1072 @enumerate
1073 @item  @code{has_zero_uses (ssa_var)} : Returns true if there are no uses of
1074 @code{ssa_var}.
1075 @item   @code{has_single_use (ssa_var)} : Returns true if there is only a 
1076 single use of @code{ssa_var}.
1077 @item   @code{single_imm_use (ssa_var, use_operand_p *ptr, tree *stmt)} :
1078 Returns true if there is only a single use of @code{ssa_var}, and also returns
1079 the use pointer and statement it occurs in in the second and third parameters.
1080 @item   @code{num_imm_uses (ssa_var)} : Returns the number of immediate uses of
1081 @code{ssa_var}. It is better not to use this if possible since it simply
1082 utilizes a loop to count the uses.
1083 @item  @code{PHI_ARG_INDEX_FROM_USE (use_p)} : Given a use within a @code{PHI}
1084 node, return the index number for the use.  An assert is triggered if the use
1085 isn't located in a @code{PHI} node.
1086 @item  @code{USE_STMT (use_p)} : Return the statement a use occurs in.
1087 @end enumerate
1088
1089 Note that uses are not put into an immediate use list until their statement is
1090 actually inserted into the instruction stream via a @code{bsi_*} routine.  
1091
1092 It is also still possible to utilize lazy updating of statements, but this 
1093 should be used only when absolutely required.  Both alias analysis and the 
1094 dominator optimizations currently do this.  
1095
1096 When lazy updating is being used, the immediate use information is out of date 
1097 and cannot be used reliably.  Lazy updating is achieved by simply marking
1098 statements modified via calls to @code{mark_stmt_modified} instead of 
1099 @code{update_stmt}.  When lazy updating is no longer required, all the 
1100 modified statements must have @code{update_stmt} called in order to bring them 
1101 up to date.  This must be done before the optimization is finished, or 
1102 @code{verify_ssa} will trigger an abort.
1103
1104 This is done with a simple loop over the instruction stream:
1105 @smallexample
1106   block_stmt_iterator bsi;
1107   basic_block bb;
1108   FOR_EACH_BB (bb)
1109     @{
1110       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1111         update_stmt_if_modified (bsi_stmt (bsi));
1112     @}
1113 @end smallexample
1114
1115 @node SSA
1116 @section Static Single Assignment
1117 @cindex SSA
1118 @cindex static single assignment
1119
1120 Most of the tree optimizers rely on the data flow information provided
1121 by the Static Single Assignment (SSA) form.  We implement the SSA form
1122 as described in @cite{R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and
1123 K. Zadeck.  Efficiently Computing Static Single Assignment Form and the
1124 Control Dependence Graph.  ACM Transactions on Programming Languages
1125 and Systems, 13(4):451-490, October 1991}.
1126
1127 The SSA form is based on the premise that program variables are
1128 assigned in exactly one location in the program.  Multiple assignments
1129 to the same variable create new versions of that variable.  Naturally,
1130 actual programs are seldom in SSA form initially because variables
1131 tend to be assigned multiple times.  The compiler modifies the program
1132 representation so that every time a variable is assigned in the code,
1133 a new version of the variable is created.  Different versions of the
1134 same variable are distinguished by subscripting the variable name with
1135 its version number.  Variables used in the right-hand side of
1136 expressions are renamed so that their version number matches that of
1137 the most recent assignment.
1138
1139 We represent variable versions using @code{SSA_NAME} nodes.  The
1140 renaming process in @file{tree-ssa.c} wraps every real and
1141 virtual operand with an @code{SSA_NAME} node which contains
1142 the version number and the statement that created the
1143 @code{SSA_NAME}.  Only definitions and virtual definitions may
1144 create new @code{SSA_NAME} nodes.
1145
1146 Sometimes, flow of control makes it impossible to determine what is the
1147 most recent version of a variable.  In these cases, the compiler
1148 inserts an artificial definition for that variable called
1149 @dfn{PHI function} or @dfn{PHI node}.  This new definition merges
1150 all the incoming versions of the variable to create a new name
1151 for it.  For instance,
1152
1153 @smallexample
1154 if (...)
1155   a_1 = 5;
1156 else if (...)
1157   a_2 = 2;
1158 else
1159   a_3 = 13;
1160
1161 # a_4 = PHI <a_1, a_2, a_3>
1162 return a_4;
1163 @end smallexample
1164
1165 Since it is not possible to determine which of the three branches
1166 will be taken at runtime, we don't know which of @code{a_1},
1167 @code{a_2} or @code{a_3} to use at the return statement.  So, the
1168 SSA renamer creates a new version @code{a_4} which is assigned
1169 the result of ``merging'' @code{a_1}, @code{a_2} and @code{a_3}.
1170 Hence, PHI nodes mean ``one of these operands.  I don't know
1171 which''.
1172
1173 The following macros can be used to examine PHI nodes
1174
1175 @defmac PHI_RESULT (@var{phi})
1176 Returns the @code{SSA_NAME} created by PHI node @var{phi} (i.e.,
1177 @var{phi}'s LHS)@.
1178 @end defmac
1179
1180 @defmac PHI_NUM_ARGS (@var{phi})
1181 Returns the number of arguments in @var{phi}.  This number is exactly
1182 the number of incoming edges to the basic block holding @var{phi}@.
1183 @end defmac
1184
1185 @defmac PHI_ARG_ELT (@var{phi}, @var{i})
1186 Returns a tuple representing the @var{i}th argument of @var{phi}@.
1187 Each element of this tuple contains an @code{SSA_NAME} @var{var} and
1188 the incoming edge through which @var{var} flows.
1189 @end defmac
1190
1191 @defmac PHI_ARG_EDGE (@var{phi}, @var{i})
1192 Returns the incoming edge for the @var{i}th argument of @var{phi}.
1193 @end defmac
1194
1195 @defmac PHI_ARG_DEF (@var{phi}, @var{i})
1196 Returns the @code{SSA_NAME} for the @var{i}th argument of @var{phi}.
1197 @end defmac
1198
1199
1200 @subsection Preserving the SSA form
1201 @findex update_ssa
1202 @cindex preserving SSA form
1203 Some optimization passes make changes to the function that
1204 invalidate the SSA property.  This can happen when a pass has
1205 added new symbols or changed the program so that variables that
1206 were previously aliased aren't anymore.  Whenever something like this
1207 happens, the affected symbols must be renamed into SSA form again.  
1208 Transformations that emit new code or replicate existing statements
1209 will also need to update the SSA form@.
1210
1211 Since GCC implements two different SSA forms for register and virtual
1212 variables, keeping the SSA form up to date depends on whether you are
1213 updating register or virtual names.  In both cases, the general idea
1214 behind incremental SSA updates is similar: when new SSA names are
1215 created, they typically are meant to replace other existing names in
1216 the program@.
1217
1218 For instance, given the following code:
1219
1220 @smallexample
1221      1  L0:
1222      2  x_1 = PHI (0, x_5)
1223      3  if (x_1 < 10)
1224      4    if (x_1 > 7)
1225      5      y_2 = 0
1226      6    else
1227      7      y_3 = x_1 + x_7
1228      8    endif
1229      9    x_5 = x_1 + 1
1230      10   goto L0;
1231      11 endif
1232 @end smallexample
1233
1234 Suppose that we insert new names @code{x_10} and @code{x_11} (lines
1235 @code{4} and @code{8})@.
1236
1237 @smallexample
1238      1  L0:
1239      2  x_1 = PHI (0, x_5)
1240      3  if (x_1 < 10)
1241      4    x_10 = ...
1242      5    if (x_1 > 7)
1243      6      y_2 = 0
1244      7    else
1245      8      x_11 = ...
1246      9      y_3 = x_1 + x_7
1247      10   endif
1248      11   x_5 = x_1 + 1
1249      12   goto L0;
1250      13 endif
1251 @end smallexample
1252
1253 We want to replace all the uses of @code{x_1} with the new definitions
1254 of @code{x_10} and @code{x_11}.  Note that the only uses that should
1255 be replaced are those at lines @code{5}, @code{9} and @code{11}.
1256 Also, the use of @code{x_7} at line @code{9} should @emph{not} be
1257 replaced (this is why we cannot just mark symbol @code{x} for
1258 renaming)@.
1259
1260 Additionally, we may need to insert a PHI node at line @code{11}
1261 because that is a merge point for @code{x_10} and @code{x_11}.  So the
1262 use of @code{x_1} at line @code{11} will be replaced with the new PHI
1263 node.  The insertion of PHI nodes is optional.  They are not strictly
1264 necessary to preserve the SSA form, and depending on what the caller
1265 inserted, they may not even be useful for the optimizers@.
1266
1267 Updating the SSA form is a two step process.  First, the pass has to
1268 identify which names need to be updated and/or which symbols need to
1269 be renamed into SSA form for the first time.  When new names are
1270 introduced to replace existing names in the program, the mapping
1271 between the old and the new names are registered by calling
1272 @code{register_new_name_mapping} (note that if your pass creates new
1273 code by duplicating basic blocks, the call to @code{tree_duplicate_bb}
1274 will set up the necessary mappings automatically).  On the other hand,
1275 if your pass exposes a new symbol that should be put in SSA form for
1276 the first time, the new symbol should be registered with
1277 @code{mark_sym_for_renaming}.
1278
1279 After the replacement mappings have been registered and new symbols
1280 marked for renaming, a call to @code{update_ssa} makes the registered
1281 changes.  This can be done with an explicit call or by creating
1282 @code{TODO} flags in the @code{tree_opt_pass} structure for your pass.
1283 There are several @code{TODO} flags that control the behaviour of
1284 @code{update_ssa}:
1285
1286 @itemize @bullet
1287 @item @code{TODO_update_ssa}.  Update the SSA form inserting PHI nodes
1288       for newly exposed symbols and virtual names marked for updating.
1289       When updating real names, only insert PHI nodes for a real name
1290       @code{O_j} in blocks reached by all the new and old definitions for
1291       @code{O_j}.  If the iterated dominance frontier for @code{O_j}
1292       is not pruned, we may end up inserting PHI nodes in blocks that
1293       have one or more edges with no incoming definition for
1294       @code{O_j}.  This would lead to uninitialized warnings for
1295       @code{O_j}'s symbol@.
1296
1297 @item @code{TODO_update_ssa_no_phi}.  Update the SSA form without
1298       inserting any new PHI nodes at all.  This is used by passes that
1299       have either inserted all the PHI nodes themselves or passes that
1300       need only to patch use-def and def-def chains for virtuals
1301       (e.g., DCE)@.
1302
1303
1304 @item @code{TODO_update_ssa_full_phi}.  Insert PHI nodes everywhere
1305       they are needed.  No prunning of the IDF is done.  This is used
1306       by passes that need the PHI nodes for @code{O_j} even if it
1307       means that some arguments will come from the default definition
1308       of @code{O_j}'s symbol (e.g., @code{pass_linear_transform})@.
1309
1310       WARNING: If you need to use this flag, chances are that your
1311       pass may be doing something wrong.  Inserting PHI nodes for an
1312       old name where not all edges carry a new replacement may lead to
1313       silent codegen errors or spurious uninitialized warnings@.
1314
1315 @item @code{TODO_update_ssa_only_virtuals}.  Passes that update the
1316       SSA form on their own may want to delegate the updating of
1317       virtual names to the generic updater.  Since FUD chains are
1318       easier to maintain, this simplifies the work they need to do.
1319       NOTE: If this flag is used, any OLD->NEW mappings for real names
1320       are explicitly destroyed and only the symbols marked for
1321       renaming are processed@.
1322 @end itemize
1323
1324
1325 @subsection Examining @code{SSA_NAME} nodes
1326 @cindex examining SSA_NAMEs
1327
1328 The following macros can be used to examine @code{SSA_NAME} nodes
1329
1330 @defmac SSA_NAME_DEF_STMT (@var{var})
1331 Returns the statement @var{s} that creates the @code{SSA_NAME}
1332 @var{var}.  If @var{s} is an empty statement (i.e., @code{IS_EMPTY_STMT
1333 (@var{s})} returns @code{true}), it means that the first reference to
1334 this variable is a USE or a VUSE@.
1335 @end defmac
1336
1337 @defmac SSA_NAME_VERSION (@var{var})
1338 Returns the version number of the @code{SSA_NAME} object @var{var}.
1339 @end defmac
1340
1341
1342 @subsection Walking use-def chains
1343
1344 @deftypefn {Tree SSA function} void walk_use_def_chains (@var{var}, @var{fn}, @var{data})
1345
1346 Walks use-def chains starting at the @code{SSA_NAME} node @var{var}.
1347 Calls function @var{fn} at each reaching definition found.  Function
1348 @var{FN} takes three arguments: @var{var}, its defining statement
1349 (@var{def_stmt}) and a generic pointer to whatever state information
1350 that @var{fn} may want to maintain (@var{data}).  Function @var{fn} is
1351 able to stop the walk by returning @code{true}, otherwise in order to
1352 continue the walk, @var{fn} should return @code{false}.
1353
1354 Note, that if @var{def_stmt} is a @code{PHI} node, the semantics are
1355 slightly different.  For each argument @var{arg} of the PHI node, this
1356 function will:
1357
1358 @enumerate
1359 @item   Walk the use-def chains for @var{arg}.
1360 @item   Call @code{FN (@var{arg}, @var{phi}, @var{data})}.
1361 @end enumerate
1362
1363 Note how the first argument to @var{fn} is no longer the original
1364 variable @var{var}, but the PHI argument currently being examined.
1365 If @var{fn} wants to get at @var{var}, it should call
1366 @code{PHI_RESULT} (@var{phi}).
1367 @end deftypefn
1368
1369 @subsection Walking the dominator tree
1370
1371 @deftypefn {Tree SSA function} void walk_dominator_tree (@var{walk_data}, @var{bb})
1372
1373 This function walks the dominator tree for the current CFG calling a
1374 set of callback functions defined in @var{struct dom_walk_data} in
1375 @file{domwalk.h}.  The call back functions you need to define give you
1376 hooks to execute custom code at various points during traversal:
1377
1378 @enumerate
1379 @item Once to initialize any local data needed while processing
1380       @var{bb} and its children.  This local data is pushed into an
1381       internal stack which is automatically pushed and popped as the
1382       walker traverses the dominator tree.
1383
1384 @item Once before traversing all the statements in the @var{bb}.
1385
1386 @item Once for every statement inside @var{bb}.
1387
1388 @item Once after traversing all the statements and before recursing
1389       into @var{bb}'s dominator children.
1390
1391 @item It then recurses into all the dominator children of @var{bb}.
1392
1393 @item After recursing into all the dominator children of @var{bb} it
1394       can, optionally, traverse every statement in @var{bb} again
1395       (i.e., repeating steps 2 and 3).
1396
1397 @item Once after walking the statements in @var{bb} and @var{bb}'s
1398       dominator children.  At this stage, the block local data stack
1399       is popped.
1400 @end enumerate
1401 @end deftypefn
1402
1403 @node Alias analysis
1404 @section Alias analysis
1405 @cindex alias
1406 @cindex flow-sensitive alias analysis
1407 @cindex flow-insensitive alias analysis
1408
1409 Alias analysis proceeds in 4 main phases:
1410
1411 @enumerate
1412 @item   Structural alias analysis.
1413
1414 This phase walks the types for structure variables, and determines which
1415 of the fields can overlap using offset and size of each field.  For each
1416 field, a ``subvariable'' called a ``Structure field tag'' (SFT)@ is
1417 created, which represents that field as a separate variable.  All
1418 accesses that could possibly overlap with a given field will have
1419 virtual operands for the SFT of that field.
1420
1421 @smallexample
1422 struct foo
1423 @{
1424   int a;
1425   int b;
1426 @}
1427 struct foo temp;
1428 int bar (void)
1429 @{
1430   int tmp1, tmp2, tmp3;
1431   SFT.0_2 = V_MUST_DEF <SFT.0_1>
1432   temp.a = 5;
1433   SFT.1_4 = V_MUST_DEF <SFT.1_3>
1434   temp.b = 6;
1435   
1436   VUSE <SFT.1_4>
1437   tmp1_5 = temp.b;
1438   VUSE <SFT.0_2>
1439   tmp2_6 = temp.a;
1440
1441   tmp3_7 = tmp1_5 + tmp2_6;
1442   return tmp3_7;
1443 @}
1444 @end smallexample
1445
1446 If you copy the type tag for a variable for some reason, you probably
1447 also want to copy the subvariables for that variable.
1448
1449 @item   Points-to and escape analysis.
1450
1451 This phase walks the use-def chains in the SSA web looking for
1452 three things:
1453
1454         @itemize @bullet
1455         @item   Assignments of the form @code{P_i = &VAR}
1456         @item   Assignments of the form P_i = malloc()
1457         @item   Pointers and ADDR_EXPR that escape the current function.
1458         @end itemize
1459
1460 The concept of `escaping' is the same one used in the Java world.
1461 When a pointer or an ADDR_EXPR escapes, it means that it has been
1462 exposed outside of the current function.  So, assignment to
1463 global variables, function arguments and returning a pointer are
1464 all escape sites.
1465
1466 This is where we are currently limited.  Since not everything is
1467 renamed into SSA, we lose track of escape properties when a
1468 pointer is stashed inside a field in a structure, for instance.
1469 In those cases, we are assuming that the pointer does escape.
1470
1471 We use escape analysis to determine whether a variable is
1472 call-clobbered.  Simply put, if an ADDR_EXPR escapes, then the
1473 variable is call-clobbered.  If a pointer P_i escapes, then all
1474 the variables pointed-to by P_i (and its memory tag) also escape.
1475
1476 @item   Compute flow-sensitive aliases
1477
1478 We have two classes of memory tags.  Memory tags associated with
1479 the pointed-to data type of the pointers in the program.  These
1480 tags are called ``type memory tag'' (TMT)@.  The other class are
1481 those associated with SSA_NAMEs, called ``name memory tag'' (NMT)@.
1482 The basic idea is that when adding operands for an INDIRECT_REF
1483 *P_i, we will first check whether P_i has a name tag, if it does
1484 we use it, because that will have more precise aliasing
1485 information.  Otherwise, we use the standard type tag.
1486
1487 In this phase, we go through all the pointers we found in
1488 points-to analysis and create alias sets for the name memory tags
1489 associated with each pointer P_i.  If P_i escapes, we mark
1490 call-clobbered the variables it points to and its tag.
1491
1492
1493 @item   Compute flow-insensitive aliases
1494
1495 This pass will compare the alias set of every type memory tag and
1496 every addressable variable found in the program.  Given a type
1497 memory tag TMT and an addressable variable V@.  If the alias sets
1498 of TMT and V conflict (as computed by may_alias_p), then V is
1499 marked as an alias tag and added to the alias set of TMT@.
1500 @end enumerate
1501
1502 For instance, consider the following function:
1503
1504 @smallexample
1505 foo (int i)
1506 @{
1507   int *p, *q, a, b;
1508
1509   if (i > 10)
1510     p = &a;
1511   else
1512     q = &b;
1513
1514   *p = 3;
1515   *q = 5;
1516   a = b + 2;
1517   return *p;
1518 @}
1519 @end smallexample
1520
1521 After aliasing analysis has finished, the type memory tag for
1522 pointer @code{p} will have two aliases, namely variables @code{a} and
1523 @code{b}.
1524 Every time pointer @code{p} is dereferenced, we want to mark the
1525 operation as a potential reference to @code{a} and @code{b}.
1526
1527 @smallexample
1528 foo (int i)
1529 @{
1530   int *p, a, b;
1531
1532   if (i_2 > 10)
1533     p_4 = &a;
1534   else
1535     p_6 = &b;
1536   # p_1 = PHI <p_4(1), p_6(2)>;
1537
1538   # a_7 = V_MAY_DEF <a_3>;
1539   # b_8 = V_MAY_DEF <b_5>;
1540   *p_1 = 3;
1541
1542   # a_9 = V_MAY_DEF <a_7>
1543   # VUSE <b_8>
1544   a_9 = b_8 + 2;
1545
1546   # VUSE <a_9>;
1547   # VUSE <b_8>;
1548   return *p_1;
1549 @}
1550 @end smallexample
1551
1552 In certain cases, the list of may aliases for a pointer may grow
1553 too large.  This may cause an explosion in the number of virtual
1554 operands inserted in the code.  Resulting in increased memory
1555 consumption and compilation time.
1556
1557 When the number of virtual operands needed to represent aliased
1558 loads and stores grows too large (configurable with @option{--param
1559 max-aliased-vops}), alias sets are grouped to avoid severe
1560 compile-time slow downs and memory consumption.  The alias
1561 grouping heuristic proceeds as follows:
1562
1563 @enumerate
1564 @item Sort the list of pointers in decreasing number of contributed
1565 virtual operands.
1566
1567 @item Take the first pointer from the list and reverse the role
1568 of the memory tag and its aliases.  Usually, whenever an
1569 aliased variable Vi is found to alias with a memory tag
1570 T, we add Vi to the may-aliases set for T@.  Meaning that
1571 after alias analysis, we will have:
1572
1573 @smallexample
1574 may-aliases(T) = @{ V1, V2, V3, ..., Vn @}
1575 @end smallexample
1576
1577 This means that every statement that references T, will get
1578 @code{n} virtual operands for each of the Vi tags.  But, when
1579 alias grouping is enabled, we make T an alias tag and add it
1580 to the alias set of all the Vi variables:
1581
1582 @smallexample
1583 may-aliases(V1) = @{ T @}
1584 may-aliases(V2) = @{ T @}
1585 ...
1586 may-aliases(Vn) = @{ T @}
1587 @end smallexample
1588
1589 This has two effects: (a) statements referencing T will only get
1590 a single virtual operand, and, (b) all the variables Vi will now
1591 appear to alias each other.  So, we lose alias precision to
1592 improve compile time.  But, in theory, a program with such a high
1593 level of aliasing should not be very optimizable in the first
1594 place.
1595
1596 @item Since variables may be in the alias set of more than one
1597 memory tag, the grouping done in step (2) needs to be extended
1598 to all the memory tags that have a non-empty intersection with
1599 the may-aliases set of tag T@.  For instance, if we originally
1600 had these may-aliases sets:
1601
1602 @smallexample
1603 may-aliases(T) = @{ V1, V2, V3 @}
1604 may-aliases(R) = @{ V2, V4 @}
1605 @end smallexample
1606
1607 In step (2) we would have reverted the aliases for T as:
1608
1609 @smallexample
1610 may-aliases(V1) = @{ T @}
1611 may-aliases(V2) = @{ T @}
1612 may-aliases(V3) = @{ T @}
1613 @end smallexample
1614
1615 But note that now V2 is no longer aliased with R@.  We could
1616 add R to may-aliases(V2), but we are in the process of
1617 grouping aliases to reduce virtual operands so what we do is
1618 add V4 to the grouping to obtain:
1619
1620 @smallexample
1621 may-aliases(V1) = @{ T @}
1622 may-aliases(V2) = @{ T @}
1623 may-aliases(V3) = @{ T @}
1624 may-aliases(V4) = @{ T @}
1625 @end smallexample
1626
1627 @item If the total number of virtual operands due to aliasing is
1628 still above the threshold set by max-alias-vops, go back to (2).
1629 @end enumerate