Analysis and Optimization of GIMPLE Trees
*****************************************

GCC uses three main intermediate languages to represent the program during compilation: GENERIC, GIMPLE and RTL.  GENERIC is a language-independent representation generated by each front end.  It is used to serve as an interface between the parser and optimizer. GENERIC is a common representation that is able to represent programs written in all the languages supported by GCC.

GIMPLE and RTL are used to optimize the program.  GIMPLE is used for target and language independent optimizations (e.g., inlining, constant propagation, tail call elimination, redundancy elimination, etc).  Much like GENERIC, GIMPLE is a language independent, tree based representation.  However, it differs from GENERIC in that the GIMPLE grammar is more restrictive: expressions contain no more than 3 operands (except function calls), it has no control flow structures and expressions with side-effects are only allowed on the right hand side of assignments.  See the chapter describing GENERIC and GIMPLE for more details.

This chapter describes the data structures and functions used in the GIMPLE optimizers (also known as ``tree optimizers'' or ``middle end'').  In particular, it focuses on all the macros, data structures, functions and programming constructs needed to implement optimization passes for GIMPLE.

GENERIC
=======

The purpose of GENERIC is simply to provide a language-independent way of representing an entire function in trees.  To this end, it was necessary to add a few new tree codes to the back end, but most everything was already there.  If you can express it with the codes in gcc/tree.def, it's GENERIC.

Early on, there was a great deal of debate about how to think about statements in a tree IL.  In GENERIC, a statement is defined as any expression whose value, if any, is ignored.  A statement will always have TREE_SIDE_EFFECTS set (or it will be discarded), but a non-statement expression may also have side effects.  A CALL_EXPR, for instance.

It would be possible for some local optimizations to work on the GENERIC form of a function; indeed, the adapted tree inliner works fine on GENERIC, but the current compiler performs inlining after lowering to GIMPLE (a restricted form described in the next section). Indeed, currently the frontends perform this lowering before handing off to tree_rest_of_compilation, but this seems inelegant.

If necessary, a front end can use some language-dependent tree codes in its GENERIC representation, so long as it provides a hook for converting them to GIMPLE and doesn't expect them to work with any (hypothetical) optimizers that run before the conversion to GIMPLE. The intermediate representation used while parsing C and C++ looks very little like GENERIC, but the C and C++ gimplifier hooks are perfectly happy to take it as input and spit out GIMPLE.

GIMPLE
======

GIMPLE is a simplified subset of GENERIC for use in optimization.  The particular subset chosen (and the name) was heavily influenced by the SIMPLE IL used by the McCAT compiler project at McGill University (http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html), though we have made some different choices.  For one thing, SIMPLE doesn't support goto; a production compiler can't afford that kind of restriction.

GIMPLE retains much of the structure of the parse trees: lexical scopes are represented as containers, rather than markers.  However, expressions are broken down into a 3-address form, using temporary variables to hold intermediate values.  Also, control structures are lowered to gotos.

In GIMPLE no container node is ever used for its value; if a COND_EXPR or BIND_EXPR has a value, it is stored into a temporary within the controlled blocks, and that temporary is used in place of the container.

The compiler pass which lowers GENERIC to GIMPLE is referred to as the gimplifier.  The gimplifier works recursively, replacing complex statements with sequences of simple statements.

Interfaces
----------

The tree representation of a function is stored in DECL_SAVED_TREE.  It is lowered to GIMPLE by a call to gimplify_function_tree.

If a front end wants to include language-specific tree codes in the tree representation which it provides to the back end, it must provide a definition of LANG_HOOKS_GIMPLIFY_EXPR which knows how to convert the front end trees to GIMPLE.  Usually such a hook will involve much of the same code for expanding front end trees to RTL.  This function can return fully lowered GIMPLE, or it can return GENERIC trees and let the main gimplifier lower them the rest of the way; this is often simpler.

The C and C++ front ends currently convert directly from front end trees to GIMPLE, and hand that off to the back end rather than first converting to GENERIC.  Their gimplifier hooks know about all the _STMT nodes and how to convert them to GENERIC forms.  There was some work done on a genericization pass which would run first, but the existence of STMT_EXPR meant that in order to convert all of the C statements into GENERIC equivalents would involve walking the entire tree anyway, so it was simpler to lower all the way.  This might change in the future if someone writes an optimization pass which would work better with higher-level trees, but currently the optimizers all expect GIMPLE.

A front end which wants to use the tree optimizers (and already has some sort of whole-function tree representation) only needs to provide a definition of LANG_HOOKS_GIMPLIFY_EXPR, call gimplify_function_tree to lower to GIMPLE, and then hand off to tree_rest_of_compilation to compile and output the function.

You can tell the compiler to dump a C-like representation of the GIMPLE form with the flag -fdump-tree-gimple.

Temporaries
-----------

When gimplification encounters a subexpression which is too complex, it creates a new temporary variable to hold the value of the subexpression, and adds a new statement to initialize it before the current statement. These special temporaries are known as expression temporaries, and are allocated using get_formal_tmp_var.  The compiler tries to always evaluate identical expressions into the same temporary, to simplify elimination of redundant calculations.

We can only use expression temporaries when we know that it will not be reevaluated before its value is used, and that it will not be otherwise modified (These restrictions are derived from those in Morgan 4.8.). Other temporaries can be allocated using get_initialized_tmp_var or create_tmp_var.

Currently, an expression like a = b + 5 is not reduced any further.  We tried converting it to something like

       T1 = b + 5;
       a = T1;

but this bloated the representation for minimal benefit.  However, a variable which must live in memory cannot appear in an expression; its value is explicitly loaded into a temporary first.  Similarly, storing the value of an expression to a memory variable goes through a temporary.

Expressions
-----------

In general, expressions in GIMPLE consist of an operation and the appropriate number of simple operands; these operands must either be a GIMPLE rvalue (is_gimple_val), i.e. a constant or a register variable.  More complex operands are factored out into temporaries, so that

       a = b + c + d

becomes

       T1 = b + c;
       a = T1 + d;

The same rule holds for arguments to a CALL_EXPR.

The target of an assignment is usually a variable, but can also be an INDIRECT_REF or a compound lvalue as described below.

Compound Expressions
....................

The left-hand side of a C comma expression is simply moved into a separate statement.

Compound Lvalues
................

Currently compound lvalues involving array and structure field references are not broken down; an expression like a.b[2] = 42 is not reduced any further (though complex array subscripts are).  This restriction is a workaround for limitations in later optimizers; if we were to convert this to

       T1 = &a.b;
       T1[2] = 42;

alias analysis would not remember that the reference to T1[2] came by way of a.b, so it would think that the assignment could alias another member of a; this broke struct-alias-1.c.  Future optimizer improvements may make this limitation unnecessary.

Conditional Expressions
.......................

A C ?: expression is converted into an if statement with each branch assigning to the same temporary.  So,

       a = b ? c : d;

becomes

       if (b)
         T1 = c;
       else
         T1 = d;
       a = T1;

Tree level if-conversion pass re-introduces ?: expression, if appropriate. It is used to vectorize loops with conditions using vector conditional operations.

Note that in GIMPLE, if statements are also represented using COND_EXPR, as described below.

Logical Operators
.................

Except when they appear in the condition operand of a COND_EXPR, logical `and' and `or' operators are simplified as follows: a = b && c becomes

       T1 = (bool)b;
       if (T1)
         T1 = (bool)c;
       a = T1;

Note that T1 in this example cannot be an expression temporary, because it has two different assignments.

Statements
----------

Most statements will be assignment statements, represented by MODIFY_EXPR.  A CALL_EXPR whose value is ignored can also be a statement.  No other C expressions can appear at statement level; a reference to a volatile object is converted into a MODIFY_EXPR. In GIMPLE form, type of MODIFY_EXPR is not meaningful.  Instead, use type of LHS or RHS.

There are also several varieties of complex statements.

Blocks
......

Block scopes and the variables they declare in GENERIC and GIMPLE are expressed using the BIND_EXPR code, which in previous versions of GCC was primarily used for the C statement-expression extension.

Variables in a block are collected into BIND_EXPR_VARS in declaration order.  Any runtime initialization is moved out of DECL_INITIAL and into a statement in the controlled block.  When gimplifying from C or C++, this initialization replaces the DECL_STMT.

Variable-length arrays (VLAs) complicate this process, as their size often refers to variables initialized earlier in the block.  To handle this, we currently split the block at that point, and move the VLA into a new, inner BIND_EXPR.  This strategy may change in the future.

DECL_SAVED_TREE for a GIMPLE function will always be a BIND_EXPR which contains declarations for the temporary variables used in the function.

A C++ program will usually contain more BIND_EXPRs than there are syntactic blocks in the source code, since several C++ constructs have implicit scopes associated with them.  On the other hand, although the C++ front end uses pseudo-scopes to handle cleanups for objects with destructors, these don't translate into the GIMPLE form; multiple declarations at the same level use the same BIND_EXPR.

Statement Sequences
...................

Multiple statements at the same nesting level are collected into a STATEMENT_LIST.  Statement lists are modified and traversed using the interface in tree-iterator.h.

Empty Statements
................

Whenever possible, statements with no effect are discarded.  But if they are nested within another construct which cannot be discarded for some reason, they are instead replaced with an empty statement, generated by build_empty_stmt.  Initially, all empty statements were shared, after the pattern of the Java front end, but this caused a lot of trouble in practice.

An empty statement is represented as (void)0.

Loops
.....

At one time loops were expressed in GIMPLE using LOOP_EXPR, but now they are lowered to explicit gotos.

Selection Statements
....................

A simple selection statement, such as the C if statement, is expressed in GIMPLE using a void COND_EXPR.  If only one branch is used, the other is filled with an empty statement.

Normally, the condition expression is reduced to a simple comparison.  If it is a shortcut (&& or ||) expression, however, we try to break up the if into multiple ifs so that the implied shortcut is taken directly, much like the transformation done by do_jump in the RTL expander.

A SWITCH_EXPR in GIMPLE contains the condition and a TREE_VEC of CASE_LABEL_EXPRs describing the case values and corresponding LABEL_DECLs to jump to.  The body of the switch is moved after the SWITCH_EXPR.

Jumps
.....

Other jumps are expressed by either GOTO_EXPR or RETURN_EXPR.

The operand of a GOTO_EXPR must be either a label or a variable containing the address to jump to.

The operand of a RETURN_EXPR is either NULL_TREE or a MODIFY_EXPR which sets the return value.  It would be nice to move the MODIFY_EXPR into a separate statement, but the special return semantics in expand_return make that difficult.  It may still happen in the future, perhaps by moving most of that logic into expand_assignment.

Cleanups
........

Destructors for local C++ objects and similar dynamic cleanups are represented in GIMPLE by a TRY_FINALLY_EXPR.  When the controlled block exits, the cleanup is run.

TRY_FINALLY_EXPR complicates the flow graph, since the cleanup needs to appear on every edge out of the controlled block; this reduces the freedom to move code across these edges.  Therefore, the EH lowering pass which runs before most of the optimization passes eliminates these expressions by explicitly adding the cleanup to each edge.

Exception Handling
..................

Other exception handling constructs are represented using TRY_CATCH_EXPR.  The handler operand of a TRY_CATCH_EXPR can be a normal statement to be executed if the controlled block throws an exception, or it can have one of two special forms:

 1. A CATCH_EXPR executes its handler if the thrown exception matches one of the allowed types.  Multiple handlers can be expressed by a sequence of CATCH_EXPR statements.

 2. An EH_FILTER_EXPR executes its handler if the thrown exception does not match one of the allowed types.

Currently throwing an exception is not directly represented in GIMPLE, since it is implemented by calling a function.  At some point in the future we will want to add some way to express that the call will throw an exception of a known type.

Just before running the optimizers, the compiler lowers the high-level EH constructs above into a set of gotos, magic labels, and EH regions.  Continuing to unwind at the end of a cleanup is represented with a RESX_EXPR.

GIMPLE Example
--------------

     struct A { A(); ~A(); };

     int i;
     int g();
     void f()
     {
       A a;
       int j = (--i, i ? 0 : 1);

       for (int x = 42; x > 0; --x)
         {
           i += g()*4 + 32;
         }
     }

becomes

     void f()
     {
       int i.0;
       int T.1;
       int iftmp.2;
       int T.3;
       int T.4;
       int T.5;
       int T.6;

       {
         struct A a;
         int j;

         __comp_ctor (&a);
         try
           {
             i.0 = i;
             T.1 = i.0 - 1;
             i = T.1;
             i.0 = i;
             if (i.0 == 0)
               iftmp.2 = 1;
             else
               iftmp.2 = 0;
             j = iftmp.2;
             {
               int x;

               x = 42;
               goto test;
               loop:;

               T.3 = g ();
               T.4 = T.3 * 4;
               i.0 = i;
               T.5 = T.4 + i.0;
               T.6 = T.5 + 32;
               i = T.6;
               x = x - 1;

               test:;
               if (x > 0)
                 goto loop;
               else
                 goto break_;
               break_:;
             }
           }
         finally
           {
             __comp_dtor (&a);
           }
       }
     }

Rough GIMPLE Grammar
--------------------

        function     : FUNCTION_DECL
                             DECL_SAVED_TREE -> compound-stmt

        compound-stmt: STATEMENT_LIST
                             members -> stmt

        stmt         : block
                     | if-stmt
                     | switch-stmt
                     | goto-stmt
                     | return-stmt
                     | resx-stmt
                     | label-stmt
                     | try-stmt
                     | modify-stmt
                     | call-stmt

        block        : BIND_EXPR
                             BIND_EXPR_VARS -> chain of DECLs
                             BIND_EXPR_BLOCK -> BLOCK
                             BIND_EXPR_BODY -> compound-stmt

        if-stmt      : COND_EXPR
                             op0 -> condition
                             op1 -> compound-stmt
                             op2 -> compound-stmt

        switch-stmt  : SWITCH_EXPR
                             op0 -> val
                             op1 -> NULL
                             op2 -> TREE_VEC of CASE_LABEL_EXPRs
                                 The CASE_LABEL_EXPRs are sorted by CASE_LOW,
                                 and default is last.

        goto-stmt    : GOTO_EXPR
                             op0 -> LABEL_DECL | val

        return-stmt  : RETURN_EXPR
                             op0 -> return-value

        return-value : NULL
                     | RESULT_DECL
                     | MODIFY_EXPR
                             op0 -> RESULT_DECL
                             op1 -> lhs

        resx-stmt    : RESX_EXPR

        label-stmt   : LABEL_EXPR
                             op0 -> LABEL_DECL

        try-stmt     : TRY_CATCH_EXPR
                             op0 -> compound-stmt
                             op1 -> handler
                     | TRY_FINALLY_EXPR
                             op0 -> compound-stmt
                             op1 -> compound-stmt

        handler      : catch-seq
                     | EH_FILTER_EXPR
                     | compound-stmt

        catch-seq    : STATEMENT_LIST
                             members -> CATCH_EXPR

        modify-stmt  : MODIFY_EXPR
                             op0 -> lhs
                             op1 -> rhs

        call-stmt    : CALL_EXPR
                             op0 -> val | OBJ_TYPE_REF
                             op1 -> call-arg-list

        call-arg-list: TREE_LIST
                             members -> lhs | CONST

        addr-expr-arg: ID
                     | compref

        addressable  : addr-expr-arg
                     | indirectref

        with-size-arg: addressable
                     | call-stmt

        indirectref  : INDIRECT_REF
                             op0 -> val

        lhs          : addressable
                     | bitfieldref
                     | WITH_SIZE_EXPR
                             op0 -> with-size-arg
                             op1 -> val

        min-lval     : ID
                     | indirectref

        bitfieldref  : BIT_FIELD_REF
                             op0 -> inner-compref
                             op1 -> CONST
                             op2 -> var

        compref      : inner-compref
                     | REALPART_EXPR
                             op0 -> inner-compref
                     | IMAGPART_EXPR
                             op0 -> inner-compref

        inner-compref: min-lval
                     | COMPONENT_REF
                             op0 -> inner-compref
                             op1 -> FIELD_DECL
                             op2 -> val
                     | ARRAY_REF
                             op0 -> inner-compref
                             op1 -> val
                             op2 -> val
                             op3 -> val
                     | ARRAY_RANGE_REF
                             op0 -> inner-compref
                             op1 -> val
                             op2 -> val
                             op3 -> val
                     | VIEW_CONVERT_EXPR
                             op0 -> inner-compref

        condition    : val
                     | RELOP
                             op0 -> val
                             op1 -> val

        val          : ID
                     | CONST

        rhs          : lhs
                     | CONST
                     | call-stmt
                     | ADDR_EXPR
                             op0 -> addr-expr-arg
                     | UNOP
                             op0 -> val
                     | BINOP
                             op0 -> val
                             op1 -> val
                     | RELOP
                             op0 -> val
                             op1 -> val

Annotations
===========

The optimizers need to associate attributes with statements and variables during the optimization process.  For instance, we need to know what basic block does a statement belong to or whether a variable has aliases.  All these attributes are stored in data structures called annotations which are then linked to the field ann in struct tree_common.

Presently, we define annotations for statements (stmt_ann_t), variables (var_ann_t) and SSA names (ssa_name_ann_t). Annotations are defined and documented in tree-flow.h.

Statement Operands
==================

Almost every GIMPLE statement will contain a reference to a variable or memory location.  Since statements come in different shapes and sizes, their operands are going to be located at various spots inside the statement's tree.  To facilitate access to the statement's operands, they are organized into arrays associated inside each statement's annotation.  Each element in an operand array is a pointer to a VAR_DECL, PARM_DECL or SSA_NAME tree node. This provides a very convenient way of examining and replacing operands.

Data flow analysis and optimization is done on all tree nodes representing variables.  Any node for which SSA_VAR_P returns nonzero is considered when scanning statement operands.  However, not all SSA_VAR_P variables are processed in the same way.  For the purposes of optimization, we need to distinguish between references to local scalar variables and references to globals, statics, structures, arrays, aliased variables, etc.  The reason is simple, the compiler can gather complete data flow information for a local scalar.  On the other hand, a global variable may be modified by a function call, it may not be possible to keep track of all the elements of an array or the fields of a structure, etc.

The operand scanner gathers two kinds of operands: real and virtual.  An operand for which is_gimple_reg returns true is considered real, otherwise it is a virtual operand.  We also distinguish between uses and definitions.  An operand is used if its value is loaded by the statement (e.g., the operand at the RHS of an assignment).  If the statement assigns a new value to the operand, the operand is considered a definition (e.g., the operand at the LHS of an assignment).

Virtual and real operands also have very different data flow properties.  Real operands are unambiguous references to the full object that they represent.  For instance, given

     {
       int a, b;
       a = b
     }

Since a and b are non-aliased locals, the statement a = b will have one real definition and one real use because variable b is completely modified with the contents of variable a.  Real definition are also known as killing definitions.  Similarly, the use of a reads all its bits.

In contrast, virtual operands are used with variables that can have a partial or ambiguous reference.  This includes structures, arrays, globals, and aliased variables.  In these cases, we have two types of definitions.  For globals, structures, and arrays, we can determine from a statement whether a variable of these types has a killing definition. If the variable does, then the statement is marked as having a must definition of that variable.  However, if a statement is only defining a part of the variable (i.e. a field in a structure), or if we know that a statement might define the variable but we cannot say for sure, then we mark that statement as having a may definition.  For instance, given

     {
       int a, b, *p;

       if (...)
         p = &a;
       else
         p = &b;
       *p = 5;
       return *p;
     }

The assignment *p = 5 may be a definition of a or b.  If we cannot determine statically where p is pointing to at the time of the store operation, we create virtual definitions to mark that statement as a potential definition site for a and b.  Memory loads are similarly marked with virtual use operands.  Virtual operands are shown in tree dumps right before the statement that contains them.  To request a tree dump with virtual operands, use the -vops option to -fdump-tree:

     {
       int a, b, *p;

       if (...)
         p = &a;
       else
         p = &b;
       # a = V_MAY_DEF <a>
       # b = V_MAY_DEF <b>
       *p = 5;

       # VUSE <a>
       # VUSE <b>
       return *p;
     }

Notice that V_MAY_DEF operands have two copies of the referenced variable.  This indicates that this is not a killing definition of that variable.  In this case we refer to it as a may definition or aliased store.  The presence of the second copy of the variable in the V_MAY_DEF operand will become important when the function is converted into SSA form.  This will be used to link all the non-killing definitions to prevent optimizations from making incorrect assumptions about them.

Operands are collected by tree-ssa-operands.c.  They are stored inside each statement's annotation and can be accessed with DEF_OPS, USE_OPS, V_MAY_DEF_OPS, V_MUST_DEF_OPS and VUSE_OPS.  The following are all the accessor macros available to access USE operands.  To access all the other operand arrays, just change the name accordingly.  Note that this interface to the operands is deprecated, and is slated for removal in a future version of gcc.  The preferred interface is the operand iterator interface.  Unless you need to discover the number of operands of a given type on a statement, you are strongly urged not to use this interface.

 -- Macro: USE_OPS (ANN)
     Returns the array of operands used by the statement with annotation ANN.

 -- Macro: STMT_USE_OPS (STMT)
     Alternate version of USE_OPS that takes the statement STMT as input.

 -- Macro: NUM_USES (OPS)
     Return the number of USE operands in array OPS.

 -- Macro: USE_OP_PTR (OPS, I)
     Return a pointer to the Ith operand in array OPS.

 -- Macro: USE_OP (OPS, I)
     Return the Ith operand in array OPS.

The following function shows how to print all the operands of a given statement:

     void
     print_ops (tree stmt)
     {
       vuse_optype vuses;
       v_may_def_optype v_may_defs;
       v_must_def_optype v_must_defs;
       def_optype defs;
       use_optype uses;
       stmt_ann_t ann;
       size_t i;

       get_stmt_operands (stmt);
       ann = stmt_ann (stmt);

       defs = DEF_OPS (ann);
       for (i = 0; i < NUM_DEFS (defs); i++)
         print_generic_expr (stderr, DEF_OP (defs, i), 0);

       uses = USE_OPS (ann);
       for (i = 0; i < NUM_USES (uses); i++)
         print_generic_expr (stderr, USE_OP (uses, i), 0);

       v_may_defs = V_MAY_DEF_OPS (ann);
       for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
         {
           print_generic_expr (stderr, V_MAY_DEF_OP (v_may_defs, i), 0);
           print_generic_expr (stderr, V_MAY_DEF_RESULT (v_may_defs, i), 0);
         }

       v_must_defs = V_MUST_DEF_OPS (ann);
       for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
         print_generic_expr (stderr, V_MUST_DEF_OP (v_must_defs, i), 0);

       vuses = VUSE_OPS (ann);
       for (i = 0; i < NUM_VUSES (vuses); i++)
         print_generic_expr (stderr, VUSE_OP (vuses, i), 0);
     }

To collect the operands, you first need to call get_stmt_operands.  Since that is a potentially expensive operation, statements are only scanned if they have been marked modified by a call to modify_stmt.  So, if your pass replaces operands in a statement, make sure to call modify_stmt.

Operand Iterators
-----------------

There is an alternative to iterating over the operands in a statement. It is especially useful when you wish to perform the same operation on more than one type of operand.  The previous example could be rewritten as follows:

     void
     print_ops (tree stmt)
     {
       ssa_op_iter;
       tree var;

       get_stmt_operands (stmt);
       FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
         print_generic_expr (stderr, var, 0);
     }

 1. Determine whether you are need to see the operand pointers, or just the trees, and choose the appropriate macro:

          Need            Macro:
          ----            -------
          use_operand_p   FOR_EACH_SSA_USE_OPERAND
          def_operand_p   FOR_EACH_SSA_DEF_OPERAND
          tree            FOR_EACH_SSA_TREE_OPERAND

 2. You need to declare a variable of the type you are interested in, and an ssa_op_iter structure which serves as the loop controlling variable.

 3. Determine which operands you wish to use, and specify the flags of those you are interested in.  They are documented in tree-ssa-operands.h:

          #define SSA_OP_USE              0x01    /* Real USE operands.  */
          #define SSA_OP_DEF              0x02    /* Real DEF operands.  */
          #define SSA_OP_VUSE             0x04    /* VUSE operands.  */
          #define SSA_OP_VMAYUSE          0x08    /* USE portion of V_MAY_DEFS.  */
          #define SSA_OP_VMAYDEF          0x10    /* DEF portion of V_MAY_DEFS.  */
          #define SSA_OP_VMUSTDEF         0x20    /* V_MUST_DEF definitions.  */

          /* These are commonly grouped operand flags.  */
          #define SSA_OP_VIRTUAL_USES     (SSA_OP_VUSE | SSA_OP_VMAYUSE)
          #define SSA_OP_VIRTUAL_DEFS     (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF)
          #define SSA_OP_ALL_USES         (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
          #define SSA_OP_ALL_DEFS         (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
          #define SSA_OP_ALL_OPERANDS     (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)

So if you want to look at the use pointers for all the USE and VUSE operands, you would do something like:

       use_operand_p use_p;
       ssa_op_iter iter;

       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
         {
           process_use_ptr (use_p);
         }

The _TREE_ macro is basically the same as the USE and DEF macros, only with the use or def dereferenced via USE_FROM_PTR (use_p) and DEF_FROM_PTR (def_p).  Since we aren't using operand pointers, use and defs flags can be mixed.

       tree var;
       ssa_op_iter iter;

       FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE | SSA_OP_VMUSTDEF)
         {
            print_generic_expr (stderr, var, TDF_SLIM);
         }

V_MAY_DEFs are broken into two flags, one for the DEF portion (SSA_OP_VMAYDEF) and one for the USE portion (SSA_OP_VMAYUSE).  If all you want to look at are the V_MAY_DEFs together, there is a fourth iterator macro for this, which returns both a def_operand_p and a use_operand_p for each V_MAY_DEF in the statement.  Note that you don't need any flags for this one.

       use_operand_p use_p;
       def_operand_p def_p;
       ssa_op_iter iter;

       FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
         {
           my_code;
         }

V_MUST_DEFs are broken into two flags, one for the DEF portion (SSA_OP_VMUSTDEF) and one for the kill portion (SSA_OP_VMUSTDEFKILL).  If all you want to look at are the V_MUST_DEFs together, there is a fourth iterator macro for this, which returns both a def_operand_p and a use_operand_p for each V_MUST_DEF in the statement.  Note that you don't need any flags for this one.

       use_operand_p kill_p;
       def_operand_p def_p;
       ssa_op_iter iter;

       FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, kill_p, stmt, iter)
         {
           my_code;
         }

There are many examples in the code as well, as well as the documentation in tree-ssa-operands.h.

Static Single Assignment
========================

Most of the tree optimizers rely on the data flow information provided by the Static Single Assignment (SSA) form.  We implement the SSA form as described in R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck.  Efficiently Computing Static Single Assignment Form and the Control Dependence Graph.  ACM Transactions on Programming Languages and Systems, 13(4):451-490, October 1991.

The SSA form is based on the premise that program variables are assigned in exactly one location in the program.  Multiple assignments to the same variable create new versions of that variable.  Naturally, actual programs are seldom in SSA form initially because variables tend to be assigned multiple times.  The compiler modifies the program representation so that every time a variable is assigned in the code, a new version of the variable is created.  Different versions of the same variable are distinguished by subscripting the variable name with its version number.  Variables used in the right-hand side of expressions are renamed so that their version number matches that of the most recent assignment.

We represent variable versions using SSA_NAME nodes.  The renaming process in tree-ssa.c wraps every real and virtual operand with an SSA_NAME node which contains the version number and the statement that created the SSA_NAME.  Only definitions and virtual definitions may create new SSA_NAME nodes.

Sometimes, flow of control makes it impossible to determine what is the most recent version of a variable.  In these cases, the compiler inserts an artificial definition for that variable called PHI function or PHI node.  This new definition merges all the incoming versions of the variable to create a new name for it.  For instance,

     if (...)
       a_1 = 5;
     else if (...)
       a_2 = 2;
     else
       a_3 = 13;

     # a_4 = PHI <a_1, a_2, a_3>
     return a_4;

Since it is not possible to determine which of the three branches will be taken at runtime, we don't know which of a_1, a_2 or a_3 to use at the return statement.  So, the SSA renamer creates a new version a_4 which is assigned the result of ``merging'' a_1, a_2 and a_3. Hence, PHI nodes mean ``one of these operands.  I don't know which''.

The following macros can be used to examine PHI nodes

 -- Macro: PHI_RESULT (PHI)
     Returns the SSA_NAME created by PHI node PHI (i.e., PHI's LHS).

 -- Macro: PHI_NUM_ARGS (PHI)
     Returns the number of arguments in PHI.  This number is exactly the number of incoming edges to the basic block holding PHI.

 -- Macro: PHI_ARG_ELT (PHI, I)
     Returns a tuple representing the Ith argument of PHI. Each element of this tuple contains an SSA_NAME VAR and the incoming edge through which VAR flows.

 -- Macro: PHI_ARG_EDGE (PHI, I)
     Returns the incoming edge for the Ith argument of PHI.

 -- Macro: PHI_ARG_DEF (PHI, I)
     Returns the SSA_NAME for the Ith argument of PHI.

Preserving the SSA form
-----------------------

Some optimization passes make changes to the function that invalidate the SSA property.  This can happen when a pass has added new variables or changed the program so that variables that were previously aliased aren't anymore.

Whenever something like this happens, the affected variables must be renamed into SSA form again.  To do this, you should mark the new variables in the global bitmap vars_to_rename.  Once your pass has finished, the pass manager will invoke the SSA renamer to put the program into SSA once more.

Examining SSA_NAME nodes
------------------------

The following macros can be used to examine SSA_NAME nodes

 -- Macro: SSA_NAME_DEF_STMT (VAR)
     Returns the statement S that creates the SSA_NAME VAR.  If S is an empty statement (i.e., IS_EMPTY_STMT (S) returns true), it means that the first reference to this variable is a USE or a VUSE.

 -- Macro: SSA_NAME_VERSION (VAR)
     Returns the version number of the SSA_NAME object VAR.

Walking use-def chains
----------------------

 -- Tree SSA function: void walk_use_def_chains (VAR, FN, DATA)

     Walks use-def chains starting at the SSA_NAME node VAR. Calls function FN at each reaching definition found.  Function FN takes three arguments: VAR, its defining statement (DEF_STMT) and a generic pointer to whatever state information that FN may want to maintain (DATA).  Function FN is able to stop the walk by returning true, otherwise in order to continue the walk, FN should return false.

     Note, that if DEF_STMT is a PHI node, the semantics are slightly different.  For each argument ARG of the PHI node, this function will:

      1. Walk the use-def chains for ARG.

      2. Call FN (ARG, PHI, DATA).

     Note how the first argument to FN is no longer the original variable VAR, but the PHI argument currently being examined. If FN wants to get at VAR, it should call PHI_RESULT (PHI).

Walking the dominator tree
--------------------------

 -- Tree SSA function: void walk_dominator_tree (WALK_DATA, BB)

     This function walks the dominator tree for the current CFG calling a set of callback functions defined in STRUCT DOM_WALK_DATA in domwalk.h.  The call back functions you need to define give you hooks to execute custom code at various points during traversal:

      1. Once to initialize any local data needed while processing BB and its children.  This local data is pushed into an internal stack which is automatically pushed and popped as the walker traverses the dominator tree.

      2. Once before traversing all the statements in the BB.

      3. Once for every statement inside BB.

      4. Once after traversing all the statements and before recursing into BB's dominator children.

      5. It then recurses into all the dominator children of BB.

      6. After recursing into all the dominator children of BB it can, optionally, traverse every statement in BB again (i.e., repeating steps 2 and 3).

      7. Once after walking the statements in BB and BB's dominator children.  At this stage, the block local data stack is popped.

Alias analysis
==============

Alias analysis proceeds in 3 main phases:

 1. Points-to and escape analysis.

     This phase walks the use-def chains in the SSA web looking for three things:

       * Assignments of the form P_i = &VAR

       * Assignments of the form P_i = malloc()

       * Pointers and ADDR_EXPR that escape the current function.

     The concept of `escaping' is the same one used in the Java world. When a pointer or an ADDR_EXPR escapes, it means that it has been exposed outside of the current function.  So, assignment to global variables, function arguments and returning a pointer are all escape sites.

     This is where we are currently limited.  Since not everything is renamed into SSA, we lose track of escape properties when a pointer is stashed inside a field in a structure, for instance. In those cases, we are assuming that the pointer does escape.

     We use escape analysis to determine whether a variable is call-clobbered.  Simply put, if an ADDR_EXPR escapes, then the variable is call-clobbered.  If a pointer P_i escapes, then all the variables pointed-to by P_i (and its memory tag) also escape.

 2. Compute flow-sensitive aliases

     We have two classes of memory tags.  Memory tags associated with the pointed-to data type of the pointers in the program.  These tags are called ``type memory tag'' (TMT).  The other class are those associated with SSA_NAMEs, called ``name memory tag'' (NMT). The basic idea is that when adding operands for an INDIRECT_REF *P_i, we will first check whether P_i has a name tag, if it does we use it, because that will have more precise aliasing information.  Otherwise, we use the standard type tag.

     In this phase, we go through all the pointers we found in points-to analysis and create alias sets for the name memory tags associated with each pointer P_i.  If P_i escapes, we mark call-clobbered the variables it points to and its tag.

 3. Compute flow-insensitive aliases

     This pass will compare the alias set of every type memory tag and every addressable variable found in the program.  Given a type memory tag TMT and an addressable variable V.  If the alias sets of TMT and V conflict (as computed by may_alias_p), then V is marked as an alias tag and added to the alias set of TMT.

For instance, consider the following function:

     foo (int i)
     {
       int *p, *q, a, b;

       if (i > 10)
         p = &a;
       else
         q = &b;

       *p = 3;
       *q = 5;
       a = b + 2;
       return *p;
     }

After aliasing analysis has finished, the type memory tag for pointer p will have two aliases, namely variables a and b. Every time pointer p is dereferenced, we want to mark the operation as a potential reference to a and b.

     foo (int i)
     {
       int *p, a, b;

       if (i_2 > 10)
         p_4 = &a;
       else
         p_6 = &b;
       # p_1 = PHI <p_4(1), p_6(2)>;

       # a_7 = V_MAY_DEF <a_3>;
       # b_8 = V_MAY_DEF <b_5>;
       *p_1 = 3;

       # a_9 = V_MAY_DEF <a_7>
       # VUSE <b_8>
       a_9 = b_8 + 2;

       # VUSE <a_9>;
       # VUSE <b_8>;
       return *p_1;
     }

In certain cases, the list of may aliases for a pointer may grow too large.  This may cause an explosion in the number of virtual operands inserted in the code.  Resulting in increased memory consumption and compilation time.

When the number of virtual operands needed to represent aliased loads and stores grows too large (configurable with --param max-aliased-vops), alias sets are grouped to avoid severe compile-time slow downs and memory consumption.  The alias grouping heuristic proceeds as follows:

 1. Sort the list of pointers in decreasing number of contributed virtual operands.

 2. Take the first pointer from the list and reverse the role of the memory tag and its aliases.  Usually, whenever an aliased variable Vi is found to alias with a memory tag T, we add Vi to the may-aliases set for T.  Meaning that after alias analysis, we will have:

          may-aliases(T) = { V1, V2, V3, ..., Vn }

     This means that every statement that references T, will get n virtual operands for each of the Vi tags.  But, when alias grouping is enabled, we make T an alias tag and add it to the alias set of all the Vi variables:

          may-aliases(V1) = { T }
          may-aliases(V2) = { T }
          ...
          may-aliases(Vn) = { T }

     This has two effects: (a) statements referencing T will only get a single virtual operand, and, (b) all the variables Vi will now appear to alias each other.  So, we lose alias precision to improve compile time.  But, in theory, a program with such a high level of aliasing should not be very optimizable in the first place.

 3. Since variables may be in the alias set of more than one memory tag, the grouping done in step (2) needs to be extended to all the memory tags that have a non-empty intersection with the may-aliases set of tag T.  For instance, if we originally had these may-aliases sets:

          may-aliases(T) = { V1, V2, V3 }
          may-aliases(R) = { V2, V4 }

     In step (2) we would have reverted the aliases for T as:

          may-aliases(V1) = { T }
          may-aliases(V2) = { T }
          may-aliases(V3) = { T }

     But note that now V2 is no longer aliased with R.  We could add R to may-aliases(V2), but we are in the process of grouping aliases to reduce virtual operands so what we do is add V4 to the grouping to obtain:

          may-aliases(V1) = { T }
          may-aliases(V2) = { T }
          may-aliases(V3) = { T }
          may-aliases(V4) = { T }

 4. If the total number of virtual operands due to aliasing is still above the threshold set by max-alias-vops, go back to (2).