OSDN Git Service

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