xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/doc/tree-ssa.texi (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1*8feb0f0bSmrg@c Copyright (C) 2004-2020 Free Software Foundation, Inc.
21debfc3dSmrg@c This is part of the GCC manual.
31debfc3dSmrg@c For copying conditions, see the file gcc.texi.
41debfc3dSmrg
51debfc3dSmrg@c ---------------------------------------------------------------------
61debfc3dSmrg@c Tree SSA
71debfc3dSmrg@c ---------------------------------------------------------------------
81debfc3dSmrg
91debfc3dSmrg@node Tree SSA
101debfc3dSmrg@chapter Analysis and Optimization of GIMPLE tuples
111debfc3dSmrg@cindex Tree SSA
121debfc3dSmrg@cindex Optimization infrastructure for GIMPLE
131debfc3dSmrg
141debfc3dSmrgGCC uses three main intermediate languages to represent the program
151debfc3dSmrgduring compilation: GENERIC, GIMPLE and RTL@.  GENERIC is a
161debfc3dSmrglanguage-independent representation generated by each front end.  It
171debfc3dSmrgis used to serve as an interface between the parser and optimizer.
181debfc3dSmrgGENERIC is a common representation that is able to represent programs
191debfc3dSmrgwritten in all the languages supported by GCC@.
201debfc3dSmrg
211debfc3dSmrgGIMPLE and RTL are used to optimize the program.  GIMPLE is used for
221debfc3dSmrgtarget and language independent optimizations (e.g., inlining,
231debfc3dSmrgconstant propagation, tail call elimination, redundancy elimination,
241debfc3dSmrgetc).  Much like GENERIC, GIMPLE is a language independent, tree based
251debfc3dSmrgrepresentation.  However, it differs from GENERIC in that the GIMPLE
261debfc3dSmrggrammar is more restrictive: expressions contain no more than 3
271debfc3dSmrgoperands (except function calls), it has no control flow structures
28a2dc1f3fSmrgand expressions with side effects are only allowed on the right hand
291debfc3dSmrgside of assignments.  See the chapter describing GENERIC and GIMPLE
301debfc3dSmrgfor more details.
311debfc3dSmrg
321debfc3dSmrgThis chapter describes the data structures and functions used in the
331debfc3dSmrgGIMPLE optimizers (also known as ``tree optimizers'' or ``middle
341debfc3dSmrgend'').  In particular, it focuses on all the macros, data structures,
351debfc3dSmrgfunctions and programming constructs needed to implement optimization
361debfc3dSmrgpasses for GIMPLE@.
371debfc3dSmrg
381debfc3dSmrg@menu
391debfc3dSmrg* Annotations::         Attributes for variables.
401debfc3dSmrg* SSA Operands::        SSA names referenced by GIMPLE statements.
411debfc3dSmrg* SSA::                 Static Single Assignment representation.
421debfc3dSmrg* Alias analysis::      Representing aliased loads and stores.
431debfc3dSmrg* Memory model::        Memory model used by the middle-end.
441debfc3dSmrg@end menu
451debfc3dSmrg
461debfc3dSmrg@node Annotations
471debfc3dSmrg@section Annotations
481debfc3dSmrg@cindex annotations
491debfc3dSmrg
501debfc3dSmrgThe optimizers need to associate attributes with variables during the
511debfc3dSmrgoptimization process.  For instance, we need to know whether a
521debfc3dSmrgvariable has aliases.  All these attributes are stored in data
531debfc3dSmrgstructures called annotations which are then linked to the field
541debfc3dSmrg@code{ann} in @code{struct tree_common}.
551debfc3dSmrg
561debfc3dSmrg
571debfc3dSmrg@node SSA Operands
581debfc3dSmrg@section SSA Operands
591debfc3dSmrg@cindex operands
601debfc3dSmrg@cindex virtual operands
611debfc3dSmrg@cindex real operands
621debfc3dSmrg@findex update_stmt
631debfc3dSmrg
641debfc3dSmrgAlmost every GIMPLE statement will contain a reference to a variable
651debfc3dSmrgor memory location.  Since statements come in different shapes and
661debfc3dSmrgsizes, their operands are going to be located at various spots inside
671debfc3dSmrgthe statement's tree.  To facilitate access to the statement's
681debfc3dSmrgoperands, they are organized into lists associated inside each
691debfc3dSmrgstatement's annotation.  Each element in an operand list is a pointer
701debfc3dSmrgto a @code{VAR_DECL}, @code{PARM_DECL} or @code{SSA_NAME} tree node.
711debfc3dSmrgThis provides a very convenient way of examining and replacing
721debfc3dSmrgoperands.
731debfc3dSmrg
741debfc3dSmrgData flow analysis and optimization is done on all tree nodes
751debfc3dSmrgrepresenting variables.  Any node for which @code{SSA_VAR_P} returns
761debfc3dSmrgnonzero is considered when scanning statement operands.  However, not
771debfc3dSmrgall @code{SSA_VAR_P} variables are processed in the same way.  For the
781debfc3dSmrgpurposes of optimization, we need to distinguish between references to
791debfc3dSmrglocal scalar variables and references to globals, statics, structures,
801debfc3dSmrgarrays, aliased variables, etc.  The reason is simple, the compiler
811debfc3dSmrgcan gather complete data flow information for a local scalar.  On the
821debfc3dSmrgother hand, a global variable may be modified by a function call, it
831debfc3dSmrgmay not be possible to keep track of all the elements of an array or
841debfc3dSmrgthe fields of a structure, etc.
851debfc3dSmrg
861debfc3dSmrgThe operand scanner gathers two kinds of operands: @dfn{real} and
871debfc3dSmrg@dfn{virtual}.  An operand for which @code{is_gimple_reg} returns true
881debfc3dSmrgis considered real, otherwise it is a virtual operand.  We also
891debfc3dSmrgdistinguish between uses and definitions.  An operand is used if its
901debfc3dSmrgvalue is loaded by the statement (e.g., the operand at the RHS of an
911debfc3dSmrgassignment).  If the statement assigns a new value to the operand, the
921debfc3dSmrgoperand is considered a definition (e.g., the operand at the LHS of
931debfc3dSmrgan assignment).
941debfc3dSmrg
951debfc3dSmrgVirtual and real operands also have very different data flow
961debfc3dSmrgproperties.  Real operands are unambiguous references to the
971debfc3dSmrgfull object that they represent.  For instance, given
981debfc3dSmrg
991debfc3dSmrg@smallexample
1001debfc3dSmrg@{
1011debfc3dSmrg  int a, b;
1021debfc3dSmrg  a = b
1031debfc3dSmrg@}
1041debfc3dSmrg@end smallexample
1051debfc3dSmrg
1061debfc3dSmrgSince @code{a} and @code{b} are non-aliased locals, the statement
1071debfc3dSmrg@code{a = b} will have one real definition and one real use because
1081debfc3dSmrgvariable @code{a} is completely modified with the contents of
1091debfc3dSmrgvariable @code{b}.  Real definition are also known as @dfn{killing
1101debfc3dSmrgdefinitions}.  Similarly, the use of @code{b} reads all its bits.
1111debfc3dSmrg
1121debfc3dSmrgIn contrast, virtual operands are used with variables that can have
1131debfc3dSmrga partial or ambiguous reference.  This includes structures, arrays,
1141debfc3dSmrgglobals, and aliased variables.  In these cases, we have two types of
1151debfc3dSmrgdefinitions.  For globals, structures, and arrays, we can determine from
1161debfc3dSmrga statement whether a variable of these types has a killing definition.
1171debfc3dSmrgIf the variable does, then the statement is marked as having a
1181debfc3dSmrg@dfn{must definition} of that variable.  However, if a statement is only
1191debfc3dSmrgdefining a part of the variable (i.e.@: a field in a structure), or if we
1201debfc3dSmrgknow that a statement might define the variable but we cannot say for sure,
1211debfc3dSmrgthen we mark that statement as having a @dfn{may definition}.  For
1221debfc3dSmrginstance, given
1231debfc3dSmrg
1241debfc3dSmrg@smallexample
1251debfc3dSmrg@{
1261debfc3dSmrg  int a, b, *p;
1271debfc3dSmrg
1281debfc3dSmrg  if (@dots{})
1291debfc3dSmrg    p = &a;
1301debfc3dSmrg  else
1311debfc3dSmrg    p = &b;
1321debfc3dSmrg  *p = 5;
1331debfc3dSmrg  return *p;
1341debfc3dSmrg@}
1351debfc3dSmrg@end smallexample
1361debfc3dSmrg
1371debfc3dSmrgThe assignment @code{*p = 5} may be a definition of @code{a} or
1381debfc3dSmrg@code{b}.  If we cannot determine statically where @code{p} is
1391debfc3dSmrgpointing to at the time of the store operation, we create virtual
1401debfc3dSmrgdefinitions to mark that statement as a potential definition site for
1411debfc3dSmrg@code{a} and @code{b}.  Memory loads are similarly marked with virtual
1421debfc3dSmrguse operands.  Virtual operands are shown in tree dumps right before
1431debfc3dSmrgthe statement that contains them.  To request a tree dump with virtual
1441debfc3dSmrgoperands, use the @option{-vops} option to @option{-fdump-tree}:
1451debfc3dSmrg
1461debfc3dSmrg@smallexample
1471debfc3dSmrg@{
1481debfc3dSmrg  int a, b, *p;
1491debfc3dSmrg
1501debfc3dSmrg  if (@dots{})
1511debfc3dSmrg    p = &a;
1521debfc3dSmrg  else
1531debfc3dSmrg    p = &b;
1541debfc3dSmrg  # a = VDEF <a>
1551debfc3dSmrg  # b = VDEF <b>
1561debfc3dSmrg  *p = 5;
1571debfc3dSmrg
1581debfc3dSmrg  # VUSE <a>
1591debfc3dSmrg  # VUSE <b>
1601debfc3dSmrg  return *p;
1611debfc3dSmrg@}
1621debfc3dSmrg@end smallexample
1631debfc3dSmrg
1641debfc3dSmrgNotice that @code{VDEF} operands have two copies of the referenced
1651debfc3dSmrgvariable.  This indicates that this is not a killing definition of
1661debfc3dSmrgthat variable.  In this case we refer to it as a @dfn{may definition}
1671debfc3dSmrgor @dfn{aliased store}.  The presence of the second copy of the
1681debfc3dSmrgvariable in the @code{VDEF} operand will become important when the
1691debfc3dSmrgfunction is converted into SSA form.  This will be used to link all
1701debfc3dSmrgthe non-killing definitions to prevent optimizations from making
1711debfc3dSmrgincorrect assumptions about them.
1721debfc3dSmrg
1731debfc3dSmrgOperands are updated as soon as the statement is finished via a call
1741debfc3dSmrgto @code{update_stmt}.  If statement elements are changed via
1751debfc3dSmrg@code{SET_USE} or @code{SET_DEF}, then no further action is required
1761debfc3dSmrg(i.e., those macros take care of updating the statement).  If changes
1771debfc3dSmrgare made by manipulating the statement's tree directly, then a call
1781debfc3dSmrgmust be made to @code{update_stmt} when complete.  Calling one of the
1791debfc3dSmrg@code{bsi_insert} routines or @code{bsi_replace} performs an implicit
1801debfc3dSmrgcall to @code{update_stmt}.
1811debfc3dSmrg
1821debfc3dSmrg@subsection Operand Iterators And Access Routines
1831debfc3dSmrg@cindex Operand Iterators
1841debfc3dSmrg@cindex Operand Access Routines
1851debfc3dSmrg
1861debfc3dSmrgOperands are collected by @file{tree-ssa-operands.c}.  They are stored
1871debfc3dSmrginside each statement's annotation and can be accessed through either the
1881debfc3dSmrgoperand iterators or an access routine.
1891debfc3dSmrg
1901debfc3dSmrgThe following access routines are available for examining operands:
1911debfc3dSmrg
1921debfc3dSmrg@enumerate
1931debfc3dSmrg@item @code{SINGLE_SSA_@{USE,DEF,TREE@}_OPERAND}: These accessors will return
1941debfc3dSmrgNULL unless there is exactly one operand matching the specified flags.  If
1951debfc3dSmrgthere is exactly one operand, the operand is returned as either a @code{tree},
1961debfc3dSmrg@code{def_operand_p}, or @code{use_operand_p}.
1971debfc3dSmrg
1981debfc3dSmrg@smallexample
1991debfc3dSmrgtree t = SINGLE_SSA_TREE_OPERAND (stmt, flags);
2001debfc3dSmrguse_operand_p u = SINGLE_SSA_USE_OPERAND (stmt, SSA_ALL_VIRTUAL_USES);
2011debfc3dSmrgdef_operand_p d = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_ALL_DEFS);
2021debfc3dSmrg@end smallexample
2031debfc3dSmrg
2041debfc3dSmrg@item @code{ZERO_SSA_OPERANDS}: This macro returns true if there are no
2051debfc3dSmrgoperands matching the specified flags.
2061debfc3dSmrg
2071debfc3dSmrg@smallexample
2081debfc3dSmrgif (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2091debfc3dSmrg  return;
2101debfc3dSmrg@end smallexample
2111debfc3dSmrg
2121debfc3dSmrg@item @code{NUM_SSA_OPERANDS}: This macro Returns the number of operands
2131debfc3dSmrgmatching 'flags'.  This actually executes a loop to perform the count, so
2141debfc3dSmrgonly use this if it is really needed.
2151debfc3dSmrg
2161debfc3dSmrg@smallexample
2171debfc3dSmrgint count = NUM_SSA_OPERANDS (stmt, flags)
2181debfc3dSmrg@end smallexample
2191debfc3dSmrg@end enumerate
2201debfc3dSmrg
2211debfc3dSmrg
2221debfc3dSmrgIf you wish to iterate over some or all operands, use the
2231debfc3dSmrg@code{FOR_EACH_SSA_@{USE,DEF,TREE@}_OPERAND} iterator.  For example, to print
2241debfc3dSmrgall the operands for a statement:
2251debfc3dSmrg
2261debfc3dSmrg@smallexample
2271debfc3dSmrgvoid
2281debfc3dSmrgprint_ops (tree stmt)
2291debfc3dSmrg@{
2301debfc3dSmrg  ssa_op_iter;
2311debfc3dSmrg  tree var;
2321debfc3dSmrg
2331debfc3dSmrg  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
2341debfc3dSmrg    print_generic_expr (stderr, var, TDF_SLIM);
2351debfc3dSmrg@}
2361debfc3dSmrg@end smallexample
2371debfc3dSmrg
2381debfc3dSmrg
2391debfc3dSmrgHow to choose the appropriate iterator:
2401debfc3dSmrg
2411debfc3dSmrg@enumerate
2421debfc3dSmrg@item Determine whether you are need to see the operand pointers, or just the
2431debfc3dSmrgtrees, and choose the appropriate macro:
2441debfc3dSmrg
2451debfc3dSmrg@smallexample
2461debfc3dSmrgNeed            Macro:
2471debfc3dSmrg----            -------
2481debfc3dSmrguse_operand_p   FOR_EACH_SSA_USE_OPERAND
2491debfc3dSmrgdef_operand_p   FOR_EACH_SSA_DEF_OPERAND
2501debfc3dSmrgtree            FOR_EACH_SSA_TREE_OPERAND
2511debfc3dSmrg@end smallexample
2521debfc3dSmrg
2531debfc3dSmrg@item You need to declare a variable of the type you are interested
2541debfc3dSmrgin, and an ssa_op_iter structure which serves as the loop controlling
2551debfc3dSmrgvariable.
2561debfc3dSmrg
2571debfc3dSmrg@item Determine which operands you wish to use, and specify the flags of
2581debfc3dSmrgthose you are interested in.  They are documented in
2591debfc3dSmrg@file{tree-ssa-operands.h}:
2601debfc3dSmrg
2611debfc3dSmrg@smallexample
2621debfc3dSmrg#define SSA_OP_USE              0x01    /* @r{Real USE operands.}  */
2631debfc3dSmrg#define SSA_OP_DEF              0x02    /* @r{Real DEF operands.}  */
2641debfc3dSmrg#define SSA_OP_VUSE             0x04    /* @r{VUSE operands.}  */
2651debfc3dSmrg#define SSA_OP_VDEF             0x08    /* @r{VDEF operands.}  */
2661debfc3dSmrg
2671debfc3dSmrg/* @r{These are commonly grouped operand flags.}  */
2681debfc3dSmrg#define SSA_OP_VIRTUAL_USES	(SSA_OP_VUSE)
2691debfc3dSmrg#define SSA_OP_VIRTUAL_DEFS	(SSA_OP_VDEF)
2701debfc3dSmrg#define SSA_OP_ALL_VIRTUALS     (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
2711debfc3dSmrg#define SSA_OP_ALL_USES		(SSA_OP_VIRTUAL_USES | SSA_OP_USE)
2721debfc3dSmrg#define SSA_OP_ALL_DEFS		(SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
2731debfc3dSmrg#define SSA_OP_ALL_OPERANDS	(SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
2741debfc3dSmrg@end smallexample
2751debfc3dSmrg@end enumerate
2761debfc3dSmrg
2771debfc3dSmrgSo if you want to look at the use pointers for all the @code{USE} and
2781debfc3dSmrg@code{VUSE} operands, you would do something like:
2791debfc3dSmrg
2801debfc3dSmrg@smallexample
2811debfc3dSmrg  use_operand_p use_p;
2821debfc3dSmrg  ssa_op_iter iter;
2831debfc3dSmrg
2841debfc3dSmrg  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
2851debfc3dSmrg    @{
2861debfc3dSmrg      process_use_ptr (use_p);
2871debfc3dSmrg    @}
2881debfc3dSmrg@end smallexample
2891debfc3dSmrg
2901debfc3dSmrgThe @code{TREE} macro is basically the same as the @code{USE} and
2911debfc3dSmrg@code{DEF} macros, only with the use or def dereferenced via
2921debfc3dSmrg@code{USE_FROM_PTR (use_p)} and @code{DEF_FROM_PTR (def_p)}.  Since we
2931debfc3dSmrgaren't using operand pointers, use and defs flags can be mixed.
2941debfc3dSmrg
2951debfc3dSmrg@smallexample
2961debfc3dSmrg  tree var;
2971debfc3dSmrg  ssa_op_iter iter;
2981debfc3dSmrg
2991debfc3dSmrg  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE)
3001debfc3dSmrg    @{
3011debfc3dSmrg       print_generic_expr (stderr, var, TDF_SLIM);
3021debfc3dSmrg    @}
3031debfc3dSmrg@end smallexample
3041debfc3dSmrg
3051debfc3dSmrg@code{VDEF}s are broken into two flags, one for the
3061debfc3dSmrg@code{DEF} portion (@code{SSA_OP_VDEF}) and one for the USE portion
3071debfc3dSmrg(@code{SSA_OP_VUSE}).
3081debfc3dSmrg
3091debfc3dSmrgThere are many examples in the code, in addition to the documentation
3101debfc3dSmrgin @file{tree-ssa-operands.h} and @file{ssa-iterators.h}.
3111debfc3dSmrg
3121debfc3dSmrgThere are also a couple of variants on the stmt iterators regarding PHI
3131debfc3dSmrgnodes.
3141debfc3dSmrg
3151debfc3dSmrg@code{FOR_EACH_PHI_ARG} Works exactly like
3161debfc3dSmrg@code{FOR_EACH_SSA_USE_OPERAND}, except it works over @code{PHI} arguments
3171debfc3dSmrginstead of statement operands.
3181debfc3dSmrg
3191debfc3dSmrg@smallexample
3201debfc3dSmrg/* Look at every virtual PHI use.  */
3211debfc3dSmrgFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_VIRTUAL_USES)
3221debfc3dSmrg@{
3231debfc3dSmrg   my_code;
3241debfc3dSmrg@}
3251debfc3dSmrg
3261debfc3dSmrg/* Look at every real PHI use.  */
3271debfc3dSmrgFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_USES)
3281debfc3dSmrg  my_code;
3291debfc3dSmrg
3301debfc3dSmrg/* Look at every PHI use.  */
3311debfc3dSmrgFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_ALL_USES)
3321debfc3dSmrg  my_code;
3331debfc3dSmrg@end smallexample
3341debfc3dSmrg
3351debfc3dSmrg@code{FOR_EACH_PHI_OR_STMT_@{USE,DEF@}} works exactly like
3361debfc3dSmrg@code{FOR_EACH_SSA_@{USE,DEF@}_OPERAND}, except it will function on
3371debfc3dSmrgeither a statement or a @code{PHI} node.  These should be used when it is
3381debfc3dSmrgappropriate but they are not quite as efficient as the individual
3391debfc3dSmrg@code{FOR_EACH_PHI} and @code{FOR_EACH_SSA} routines.
3401debfc3dSmrg
3411debfc3dSmrg@smallexample
3421debfc3dSmrgFOR_EACH_PHI_OR_STMT_USE (use_operand_p, stmt, iter, flags)
3431debfc3dSmrg  @{
3441debfc3dSmrg     my_code;
3451debfc3dSmrg  @}
3461debfc3dSmrg
3471debfc3dSmrgFOR_EACH_PHI_OR_STMT_DEF (def_operand_p, phi, iter, flags)
3481debfc3dSmrg  @{
3491debfc3dSmrg     my_code;
3501debfc3dSmrg  @}
3511debfc3dSmrg@end smallexample
3521debfc3dSmrg
3531debfc3dSmrg@subsection Immediate Uses
3541debfc3dSmrg@cindex Immediate Uses
3551debfc3dSmrg
3561debfc3dSmrgImmediate use information is now always available.  Using the immediate use
3571debfc3dSmrgiterators, you may examine every use of any @code{SSA_NAME}. For instance,
3581debfc3dSmrgto change each use of @code{ssa_var} to @code{ssa_var2} and call fold_stmt on
3591debfc3dSmrgeach stmt after that is done:
3601debfc3dSmrg
3611debfc3dSmrg@smallexample
3621debfc3dSmrg  use_operand_p imm_use_p;
3631debfc3dSmrg  imm_use_iterator iterator;
3641debfc3dSmrg  tree ssa_var, stmt;
3651debfc3dSmrg
3661debfc3dSmrg
3671debfc3dSmrg  FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
3681debfc3dSmrg    @{
3691debfc3dSmrg      FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
3701debfc3dSmrg        SET_USE (imm_use_p, ssa_var_2);
3711debfc3dSmrg      fold_stmt (stmt);
3721debfc3dSmrg    @}
3731debfc3dSmrg@end smallexample
3741debfc3dSmrg
3751debfc3dSmrgThere are 2 iterators which can be used. @code{FOR_EACH_IMM_USE_FAST} is
3761debfc3dSmrgused when the immediate uses are not changed, i.e., you are looking at the
3771debfc3dSmrguses, but not setting them.
3781debfc3dSmrg
3791debfc3dSmrgIf they do get changed, then care must be taken that things are not changed
3801debfc3dSmrgunder the iterators, so use the @code{FOR_EACH_IMM_USE_STMT} and
3811debfc3dSmrg@code{FOR_EACH_IMM_USE_ON_STMT} iterators.  They attempt to preserve the
3821debfc3dSmrgsanity of the use list by moving all the uses for a statement into
3831debfc3dSmrga controlled position, and then iterating over those uses.  Then the
3841debfc3dSmrgoptimization can manipulate the stmt when all the uses have been
3851debfc3dSmrgprocessed.  This is a little slower than the FAST version since it adds a
3861debfc3dSmrgplaceholder element and must sort through the list a bit for each statement.
3871debfc3dSmrgThis placeholder element must be also be removed if the loop is
388*8feb0f0bSmrgterminated early.  The macro @code{BREAK_FROM_IMM_USE_STMT} is provided
3891debfc3dSmrgto do this :
3901debfc3dSmrg
3911debfc3dSmrg@smallexample
3921debfc3dSmrg  FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
3931debfc3dSmrg    @{
3941debfc3dSmrg      if (stmt == last_stmt)
395*8feb0f0bSmrg        BREAK_FROM_IMM_USE_STMT (iterator);
3961debfc3dSmrg
3971debfc3dSmrg      FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
3981debfc3dSmrg        SET_USE (imm_use_p, ssa_var_2);
3991debfc3dSmrg      fold_stmt (stmt);
4001debfc3dSmrg    @}
4011debfc3dSmrg@end smallexample
4021debfc3dSmrg
4031debfc3dSmrgThere are checks in @code{verify_ssa} which verify that the immediate use list
4041debfc3dSmrgis up to date, as well as checking that an optimization didn't break from the
4051debfc3dSmrgloop without using this macro.  It is safe to simply 'break'; from a
4061debfc3dSmrg@code{FOR_EACH_IMM_USE_FAST} traverse.
4071debfc3dSmrg
4081debfc3dSmrgSome useful functions and macros:
4091debfc3dSmrg@enumerate
4101debfc3dSmrg@item  @code{has_zero_uses (ssa_var)} : Returns true if there are no uses of
4111debfc3dSmrg@code{ssa_var}.
4121debfc3dSmrg@item   @code{has_single_use (ssa_var)} : Returns true if there is only a
4131debfc3dSmrgsingle use of @code{ssa_var}.
4141debfc3dSmrg@item   @code{single_imm_use (ssa_var, use_operand_p *ptr, tree *stmt)} :
4151debfc3dSmrgReturns true if there is only a single use of @code{ssa_var}, and also returns
4161debfc3dSmrgthe use pointer and statement it occurs in, in the second and third parameters.
4171debfc3dSmrg@item   @code{num_imm_uses (ssa_var)} : Returns the number of immediate uses of
4181debfc3dSmrg@code{ssa_var}. It is better not to use this if possible since it simply
4191debfc3dSmrgutilizes a loop to count the uses.
4201debfc3dSmrg@item  @code{PHI_ARG_INDEX_FROM_USE (use_p)} : Given a use within a @code{PHI}
4211debfc3dSmrgnode, return the index number for the use.  An assert is triggered if the use
4221debfc3dSmrgisn't located in a @code{PHI} node.
4231debfc3dSmrg@item  @code{USE_STMT (use_p)} : Return the statement a use occurs in.
4241debfc3dSmrg@end enumerate
4251debfc3dSmrg
4261debfc3dSmrgNote that uses are not put into an immediate use list until their statement is
4271debfc3dSmrgactually inserted into the instruction stream via a @code{bsi_*} routine.
4281debfc3dSmrg
4291debfc3dSmrgIt is also still possible to utilize lazy updating of statements, but this
4301debfc3dSmrgshould be used only when absolutely required.  Both alias analysis and the
4311debfc3dSmrgdominator optimizations currently do this.
4321debfc3dSmrg
4331debfc3dSmrgWhen lazy updating is being used, the immediate use information is out of date
4341debfc3dSmrgand cannot be used reliably.  Lazy updating is achieved by simply marking
4351debfc3dSmrgstatements modified via calls to @code{gimple_set_modified} instead of
4361debfc3dSmrg@code{update_stmt}.  When lazy updating is no longer required, all the
4371debfc3dSmrgmodified statements must have @code{update_stmt} called in order to bring them
4381debfc3dSmrgup to date.  This must be done before the optimization is finished, or
4391debfc3dSmrg@code{verify_ssa} will trigger an abort.
4401debfc3dSmrg
4411debfc3dSmrgThis is done with a simple loop over the instruction stream:
4421debfc3dSmrg@smallexample
4431debfc3dSmrg  block_stmt_iterator bsi;
4441debfc3dSmrg  basic_block bb;
4451debfc3dSmrg  FOR_EACH_BB (bb)
4461debfc3dSmrg    @{
4471debfc3dSmrg      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4481debfc3dSmrg        update_stmt_if_modified (bsi_stmt (bsi));
4491debfc3dSmrg    @}
4501debfc3dSmrg@end smallexample
4511debfc3dSmrg
4521debfc3dSmrg@node SSA
4531debfc3dSmrg@section Static Single Assignment
4541debfc3dSmrg@cindex SSA
4551debfc3dSmrg@cindex static single assignment
4561debfc3dSmrg
4571debfc3dSmrgMost of the tree optimizers rely on the data flow information provided
4581debfc3dSmrgby the Static Single Assignment (SSA) form.  We implement the SSA form
4591debfc3dSmrgas described in @cite{R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and
4601debfc3dSmrgK. Zadeck.  Efficiently Computing Static Single Assignment Form and the
4611debfc3dSmrgControl Dependence Graph.  ACM Transactions on Programming Languages
4621debfc3dSmrgand Systems, 13(4):451-490, October 1991}.
4631debfc3dSmrg
4641debfc3dSmrgThe SSA form is based on the premise that program variables are
4651debfc3dSmrgassigned in exactly one location in the program.  Multiple assignments
4661debfc3dSmrgto the same variable create new versions of that variable.  Naturally,
4671debfc3dSmrgactual programs are seldom in SSA form initially because variables
4681debfc3dSmrgtend to be assigned multiple times.  The compiler modifies the program
4691debfc3dSmrgrepresentation so that every time a variable is assigned in the code,
4701debfc3dSmrga new version of the variable is created.  Different versions of the
4711debfc3dSmrgsame variable are distinguished by subscripting the variable name with
4721debfc3dSmrgits version number.  Variables used in the right-hand side of
4731debfc3dSmrgexpressions are renamed so that their version number matches that of
4741debfc3dSmrgthe most recent assignment.
4751debfc3dSmrg
4761debfc3dSmrgWe represent variable versions using @code{SSA_NAME} nodes.  The
4771debfc3dSmrgrenaming process in @file{tree-ssa.c} wraps every real and
4781debfc3dSmrgvirtual operand with an @code{SSA_NAME} node which contains
4791debfc3dSmrgthe version number and the statement that created the
4801debfc3dSmrg@code{SSA_NAME}.  Only definitions and virtual definitions may
4811debfc3dSmrgcreate new @code{SSA_NAME} nodes.
4821debfc3dSmrg
4831debfc3dSmrg@cindex PHI nodes
4841debfc3dSmrgSometimes, flow of control makes it impossible to determine the
4851debfc3dSmrgmost recent version of a variable.  In these cases, the compiler
4861debfc3dSmrginserts an artificial definition for that variable called
4871debfc3dSmrg@dfn{PHI function} or @dfn{PHI node}.  This new definition merges
4881debfc3dSmrgall the incoming versions of the variable to create a new name
4891debfc3dSmrgfor it.  For instance,
4901debfc3dSmrg
4911debfc3dSmrg@smallexample
4921debfc3dSmrgif (@dots{})
4931debfc3dSmrg  a_1 = 5;
4941debfc3dSmrgelse if (@dots{})
4951debfc3dSmrg  a_2 = 2;
4961debfc3dSmrgelse
4971debfc3dSmrg  a_3 = 13;
4981debfc3dSmrg
4991debfc3dSmrg# a_4 = PHI <a_1, a_2, a_3>
5001debfc3dSmrgreturn a_4;
5011debfc3dSmrg@end smallexample
5021debfc3dSmrg
5031debfc3dSmrgSince it is not possible to determine which of the three branches
5041debfc3dSmrgwill be taken at runtime, we don't know which of @code{a_1},
5051debfc3dSmrg@code{a_2} or @code{a_3} to use at the return statement.  So, the
5061debfc3dSmrgSSA renamer creates a new version @code{a_4} which is assigned
5071debfc3dSmrgthe result of ``merging'' @code{a_1}, @code{a_2} and @code{a_3}.
5081debfc3dSmrgHence, PHI nodes mean ``one of these operands.  I don't know
5091debfc3dSmrgwhich''.
5101debfc3dSmrg
5111debfc3dSmrgThe following functions can be used to examine PHI nodes
5121debfc3dSmrg
5131debfc3dSmrg@defun gimple_phi_result (@var{phi})
5141debfc3dSmrgReturns the @code{SSA_NAME} created by PHI node @var{phi} (i.e.,
5151debfc3dSmrg@var{phi}'s LHS)@.
5161debfc3dSmrg@end defun
5171debfc3dSmrg
5181debfc3dSmrg@defun gimple_phi_num_args (@var{phi})
5191debfc3dSmrgReturns the number of arguments in @var{phi}.  This number is exactly
5201debfc3dSmrgthe number of incoming edges to the basic block holding @var{phi}@.
5211debfc3dSmrg@end defun
5221debfc3dSmrg
5231debfc3dSmrg@defun gimple_phi_arg (@var{phi}, @var{i})
5241debfc3dSmrgReturns @var{i}th argument of @var{phi}@.
5251debfc3dSmrg@end defun
5261debfc3dSmrg
5271debfc3dSmrg@defun gimple_phi_arg_edge (@var{phi}, @var{i})
5281debfc3dSmrgReturns the incoming edge for the @var{i}th argument of @var{phi}.
5291debfc3dSmrg@end defun
5301debfc3dSmrg
5311debfc3dSmrg@defun gimple_phi_arg_def (@var{phi}, @var{i})
5321debfc3dSmrgReturns the @code{SSA_NAME} for the @var{i}th argument of @var{phi}.
5331debfc3dSmrg@end defun
5341debfc3dSmrg
5351debfc3dSmrg
5361debfc3dSmrg@subsection Preserving the SSA form
5371debfc3dSmrg@findex update_ssa
5381debfc3dSmrg@cindex preserving SSA form
5391debfc3dSmrgSome optimization passes make changes to the function that
5401debfc3dSmrginvalidate the SSA property.  This can happen when a pass has
5411debfc3dSmrgadded new symbols or changed the program so that variables that
5421debfc3dSmrgwere previously aliased aren't anymore.  Whenever something like this
5431debfc3dSmrghappens, the affected symbols must be renamed into SSA form again.
5441debfc3dSmrgTransformations that emit new code or replicate existing statements
5451debfc3dSmrgwill also need to update the SSA form@.
5461debfc3dSmrg
5471debfc3dSmrgSince GCC implements two different SSA forms for register and virtual
5481debfc3dSmrgvariables, keeping the SSA form up to date depends on whether you are
5491debfc3dSmrgupdating register or virtual names.  In both cases, the general idea
5501debfc3dSmrgbehind incremental SSA updates is similar: when new SSA names are
5511debfc3dSmrgcreated, they typically are meant to replace other existing names in
5521debfc3dSmrgthe program@.
5531debfc3dSmrg
5541debfc3dSmrgFor instance, given the following code:
5551debfc3dSmrg
5561debfc3dSmrg@smallexample
5571debfc3dSmrg     1  L0:
5581debfc3dSmrg     2  x_1 = PHI (0, x_5)
5591debfc3dSmrg     3  if (x_1 < 10)
5601debfc3dSmrg     4    if (x_1 > 7)
5611debfc3dSmrg     5      y_2 = 0
5621debfc3dSmrg     6    else
5631debfc3dSmrg     7      y_3 = x_1 + x_7
5641debfc3dSmrg     8    endif
5651debfc3dSmrg     9    x_5 = x_1 + 1
5661debfc3dSmrg     10   goto L0;
5671debfc3dSmrg     11 endif
5681debfc3dSmrg@end smallexample
5691debfc3dSmrg
5701debfc3dSmrgSuppose that we insert new names @code{x_10} and @code{x_11} (lines
5711debfc3dSmrg@code{4} and @code{8})@.
5721debfc3dSmrg
5731debfc3dSmrg@smallexample
5741debfc3dSmrg     1  L0:
5751debfc3dSmrg     2  x_1 = PHI (0, x_5)
5761debfc3dSmrg     3  if (x_1 < 10)
5771debfc3dSmrg     4    x_10 = @dots{}
5781debfc3dSmrg     5    if (x_1 > 7)
5791debfc3dSmrg     6      y_2 = 0
5801debfc3dSmrg     7    else
5811debfc3dSmrg     8      x_11 = @dots{}
5821debfc3dSmrg     9      y_3 = x_1 + x_7
5831debfc3dSmrg     10   endif
5841debfc3dSmrg     11   x_5 = x_1 + 1
5851debfc3dSmrg     12   goto L0;
5861debfc3dSmrg     13 endif
5871debfc3dSmrg@end smallexample
5881debfc3dSmrg
5891debfc3dSmrgWe want to replace all the uses of @code{x_1} with the new definitions
5901debfc3dSmrgof @code{x_10} and @code{x_11}.  Note that the only uses that should
5911debfc3dSmrgbe replaced are those at lines @code{5}, @code{9} and @code{11}.
5921debfc3dSmrgAlso, the use of @code{x_7} at line @code{9} should @emph{not} be
5931debfc3dSmrgreplaced (this is why we cannot just mark symbol @code{x} for
5941debfc3dSmrgrenaming)@.
5951debfc3dSmrg
5961debfc3dSmrgAdditionally, we may need to insert a PHI node at line @code{11}
5971debfc3dSmrgbecause that is a merge point for @code{x_10} and @code{x_11}.  So the
5981debfc3dSmrguse of @code{x_1} at line @code{11} will be replaced with the new PHI
5991debfc3dSmrgnode.  The insertion of PHI nodes is optional.  They are not strictly
6001debfc3dSmrgnecessary to preserve the SSA form, and depending on what the caller
6011debfc3dSmrginserted, they may not even be useful for the optimizers@.
6021debfc3dSmrg
6031debfc3dSmrgUpdating the SSA form is a two step process.  First, the pass has to
6041debfc3dSmrgidentify which names need to be updated and/or which symbols need to
6051debfc3dSmrgbe renamed into SSA form for the first time.  When new names are
6061debfc3dSmrgintroduced to replace existing names in the program, the mapping
6071debfc3dSmrgbetween the old and the new names are registered by calling
6081debfc3dSmrg@code{register_new_name_mapping} (note that if your pass creates new
6091debfc3dSmrgcode by duplicating basic blocks, the call to @code{tree_duplicate_bb}
6101debfc3dSmrgwill set up the necessary mappings automatically).
6111debfc3dSmrg
6121debfc3dSmrgAfter the replacement mappings have been registered and new symbols
6131debfc3dSmrgmarked for renaming, a call to @code{update_ssa} makes the registered
6141debfc3dSmrgchanges.  This can be done with an explicit call or by creating
6151debfc3dSmrg@code{TODO} flags in the @code{tree_opt_pass} structure for your pass.
6161debfc3dSmrgThere are several @code{TODO} flags that control the behavior of
6171debfc3dSmrg@code{update_ssa}:
6181debfc3dSmrg
6191debfc3dSmrg@itemize @bullet
6201debfc3dSmrg@item @code{TODO_update_ssa}.  Update the SSA form inserting PHI nodes
6211debfc3dSmrgfor newly exposed symbols and virtual names marked for updating.
6221debfc3dSmrgWhen updating real names, only insert PHI nodes for a real name
6231debfc3dSmrg@code{O_j} in blocks reached by all the new and old definitions for
6241debfc3dSmrg@code{O_j}.  If the iterated dominance frontier for @code{O_j}
6251debfc3dSmrgis not pruned, we may end up inserting PHI nodes in blocks that
6261debfc3dSmrghave one or more edges with no incoming definition for
6271debfc3dSmrg@code{O_j}.  This would lead to uninitialized warnings for
6281debfc3dSmrg@code{O_j}'s symbol@.
6291debfc3dSmrg
6301debfc3dSmrg@item @code{TODO_update_ssa_no_phi}.  Update the SSA form without
6311debfc3dSmrginserting any new PHI nodes at all.  This is used by passes that
6321debfc3dSmrghave either inserted all the PHI nodes themselves or passes that
6331debfc3dSmrgneed only to patch use-def and def-def chains for virtuals
6341debfc3dSmrg(e.g., DCE)@.
6351debfc3dSmrg
6361debfc3dSmrg
6371debfc3dSmrg@item @code{TODO_update_ssa_full_phi}.  Insert PHI nodes everywhere
6381debfc3dSmrgthey are needed.  No pruning of the IDF is done.  This is used
6391debfc3dSmrgby passes that need the PHI nodes for @code{O_j} even if it
6401debfc3dSmrgmeans that some arguments will come from the default definition
6411debfc3dSmrgof @code{O_j}'s symbol (e.g., @code{pass_linear_transform})@.
6421debfc3dSmrg
6431debfc3dSmrgWARNING: If you need to use this flag, chances are that your
6441debfc3dSmrgpass may be doing something wrong.  Inserting PHI nodes for an
6451debfc3dSmrgold name where not all edges carry a new replacement may lead to
6461debfc3dSmrgsilent codegen errors or spurious uninitialized warnings@.
6471debfc3dSmrg
6481debfc3dSmrg@item @code{TODO_update_ssa_only_virtuals}.  Passes that update the
6491debfc3dSmrgSSA form on their own may want to delegate the updating of
6501debfc3dSmrgvirtual names to the generic updater.  Since FUD chains are
6511debfc3dSmrgeasier to maintain, this simplifies the work they need to do.
6521debfc3dSmrgNOTE: If this flag is used, any OLD->NEW mappings for real names
6531debfc3dSmrgare explicitly destroyed and only the symbols marked for
6541debfc3dSmrgrenaming are processed@.
6551debfc3dSmrg@end itemize
6561debfc3dSmrg
6571debfc3dSmrg@subsection Examining @code{SSA_NAME} nodes
6581debfc3dSmrg@cindex examining SSA_NAMEs
6591debfc3dSmrg
6601debfc3dSmrgThe following macros can be used to examine @code{SSA_NAME} nodes
6611debfc3dSmrg
6621debfc3dSmrg@defmac SSA_NAME_DEF_STMT (@var{var})
6631debfc3dSmrgReturns the statement @var{s} that creates the @code{SSA_NAME}
6641debfc3dSmrg@var{var}.  If @var{s} is an empty statement (i.e., @code{IS_EMPTY_STMT
6651debfc3dSmrg(@var{s})} returns @code{true}), it means that the first reference to
6661debfc3dSmrgthis variable is a USE or a VUSE@.
6671debfc3dSmrg@end defmac
6681debfc3dSmrg
6691debfc3dSmrg@defmac SSA_NAME_VERSION (@var{var})
6701debfc3dSmrgReturns the version number of the @code{SSA_NAME} object @var{var}.
6711debfc3dSmrg@end defmac
6721debfc3dSmrg
6731debfc3dSmrg
6741debfc3dSmrg@subsection Walking the dominator tree
6751debfc3dSmrg
6761debfc3dSmrg@deftypefn {Tree SSA function} void walk_dominator_tree (@var{walk_data}, @var{bb})
6771debfc3dSmrg
6781debfc3dSmrgThis function walks the dominator tree for the current CFG calling a
6791debfc3dSmrgset of callback functions defined in @var{struct dom_walk_data} in
6801debfc3dSmrg@file{domwalk.h}.  The call back functions you need to define give you
6811debfc3dSmrghooks to execute custom code at various points during traversal:
6821debfc3dSmrg
6831debfc3dSmrg@enumerate
6841debfc3dSmrg@item Once to initialize any local data needed while processing
6851debfc3dSmrg@var{bb} and its children.  This local data is pushed into an
6861debfc3dSmrginternal stack which is automatically pushed and popped as the
6871debfc3dSmrgwalker traverses the dominator tree.
6881debfc3dSmrg
6891debfc3dSmrg@item Once before traversing all the statements in the @var{bb}.
6901debfc3dSmrg
6911debfc3dSmrg@item Once for every statement inside @var{bb}.
6921debfc3dSmrg
6931debfc3dSmrg@item Once after traversing all the statements and before recursing
6941debfc3dSmrginto @var{bb}'s dominator children.
6951debfc3dSmrg
6961debfc3dSmrg@item It then recurses into all the dominator children of @var{bb}.
6971debfc3dSmrg
6981debfc3dSmrg@item After recursing into all the dominator children of @var{bb} it
6991debfc3dSmrgcan, optionally, traverse every statement in @var{bb} again
7001debfc3dSmrg(i.e., repeating steps 2 and 3).
7011debfc3dSmrg
7021debfc3dSmrg@item Once after walking the statements in @var{bb} and @var{bb}'s
7031debfc3dSmrgdominator children.  At this stage, the block local data stack
7041debfc3dSmrgis popped.
7051debfc3dSmrg@end enumerate
7061debfc3dSmrg@end deftypefn
7071debfc3dSmrg
7081debfc3dSmrg@node Alias analysis
7091debfc3dSmrg@section Alias analysis
7101debfc3dSmrg@cindex alias
7111debfc3dSmrg@cindex flow-sensitive alias analysis
7121debfc3dSmrg@cindex flow-insensitive alias analysis
7131debfc3dSmrg
7141debfc3dSmrgAlias analysis in GIMPLE SSA form consists of two pieces.  First
7151debfc3dSmrgthe virtual SSA web ties conflicting memory accesses and provides
7161debfc3dSmrga SSA use-def chain and SSA immediate-use chains for walking
7171debfc3dSmrgpossibly dependent memory accesses.  Second an alias-oracle can
7181debfc3dSmrgbe queried to disambiguate explicit and implicit memory references.
7191debfc3dSmrg
7201debfc3dSmrg@enumerate
7211debfc3dSmrg@item Memory SSA form.
7221debfc3dSmrg
7231debfc3dSmrgAll statements that may use memory have exactly one accompanied use of
7241debfc3dSmrga virtual SSA name that represents the state of memory at the
7251debfc3dSmrggiven point in the IL.
7261debfc3dSmrg
7271debfc3dSmrgAll statements that may define memory have exactly one accompanied
7281debfc3dSmrgdefinition of a virtual SSA name using the previous state of memory
7291debfc3dSmrgand defining the new state of memory after the given point in the IL.
7301debfc3dSmrg
7311debfc3dSmrg@smallexample
7321debfc3dSmrgint i;
7331debfc3dSmrgint foo (void)
7341debfc3dSmrg@{
7351debfc3dSmrg  # .MEM_3 = VDEF <.MEM_2(D)>
7361debfc3dSmrg  i = 1;
7371debfc3dSmrg  # VUSE <.MEM_3>
7381debfc3dSmrg  return i;
7391debfc3dSmrg@}
7401debfc3dSmrg@end smallexample
7411debfc3dSmrg
7421debfc3dSmrgThe virtual SSA names in this case are @code{.MEM_2(D)} and
7431debfc3dSmrg@code{.MEM_3}.  The store to the global variable @code{i}
7441debfc3dSmrgdefines @code{.MEM_3} invalidating @code{.MEM_2(D)}.  The
7451debfc3dSmrgload from @code{i} uses that new state @code{.MEM_3}.
7461debfc3dSmrg
7471debfc3dSmrgThe virtual SSA web serves as constraints to SSA optimizers
7481debfc3dSmrgpreventing illegitimate code-motion and optimization.  It
7491debfc3dSmrgalso provides a way to walk related memory statements.
7501debfc3dSmrg
7511debfc3dSmrg@item Points-to and escape analysis.
7521debfc3dSmrg
7531debfc3dSmrgPoints-to analysis builds a set of constraints from the GIMPLE
7541debfc3dSmrgSSA IL representing all pointer operations and facts we do
7551debfc3dSmrgor do not know about pointers.  Solving this set of constraints
7561debfc3dSmrgyields a conservatively correct solution for each pointer
7571debfc3dSmrgvariable in the program (though we are only interested in
7581debfc3dSmrgSSA name pointers) as to what it may possibly point to.
7591debfc3dSmrg
7601debfc3dSmrgThis points-to solution for a given SSA name pointer is stored
7611debfc3dSmrgin the @code{pt_solution} sub-structure of the
7621debfc3dSmrg@code{SSA_NAME_PTR_INFO} record.  The following accessor
7631debfc3dSmrgfunctions are available:
7641debfc3dSmrg
7651debfc3dSmrg@itemize @bullet
7661debfc3dSmrg@item @code{pt_solution_includes}
7671debfc3dSmrg@item @code{pt_solutions_intersect}
7681debfc3dSmrg@end itemize
7691debfc3dSmrg
7701debfc3dSmrgPoints-to analysis also computes the solution for two special
7711debfc3dSmrgset of pointers, @code{ESCAPED} and @code{CALLUSED}.  Those
7721debfc3dSmrgrepresent all memory that has escaped the scope of analysis
7731debfc3dSmrgor that is used by pure or nested const calls.
7741debfc3dSmrg
7751debfc3dSmrg@item Type-based alias analysis
7761debfc3dSmrg
7771debfc3dSmrgType-based alias analysis is frontend dependent though generic
7781debfc3dSmrgsupport is provided by the middle-end in @code{alias.c}.  TBAA
7791debfc3dSmrgcode is used by both tree optimizers and RTL optimizers.
7801debfc3dSmrg
7811debfc3dSmrgEvery language that wishes to perform language-specific alias analysis
7821debfc3dSmrgshould define a function that computes, given a @code{tree}
7831debfc3dSmrgnode, an alias set for the node.  Nodes in different alias sets are not
7841debfc3dSmrgallowed to alias.  For an example, see the C front-end function
7851debfc3dSmrg@code{c_get_alias_set}.
7861debfc3dSmrg
7871debfc3dSmrg@item Tree alias-oracle
7881debfc3dSmrg
7891debfc3dSmrgThe tree alias-oracle provides means to disambiguate two memory
7901debfc3dSmrgreferences and memory references against statements.  The following
7911debfc3dSmrgqueries are available:
7921debfc3dSmrg
7931debfc3dSmrg@itemize @bullet
7941debfc3dSmrg@item @code{refs_may_alias_p}
7951debfc3dSmrg@item @code{ref_maybe_used_by_stmt_p}
7961debfc3dSmrg@item @code{stmt_may_clobber_ref_p}
7971debfc3dSmrg@end itemize
7981debfc3dSmrg
7991debfc3dSmrgIn addition to those two kind of statement walkers are available
8001debfc3dSmrgwalking statements related to a reference ref.
8011debfc3dSmrg@code{walk_non_aliased_vuses} walks over dominating memory defining
8021debfc3dSmrgstatements and calls back if the statement does not clobber ref
8031debfc3dSmrgproviding the non-aliased VUSE.  The walk stops at
8041debfc3dSmrgthe first clobbering statement or if asked to.
8051debfc3dSmrg@code{walk_aliased_vdefs} walks over dominating memory defining
8061debfc3dSmrgstatements and calls back on each statement clobbering ref
8071debfc3dSmrgproviding its aliasing VDEF.  The walk stops if asked to.
8081debfc3dSmrg
8091debfc3dSmrg@end enumerate
8101debfc3dSmrg
8111debfc3dSmrg
8121debfc3dSmrg@node Memory model
8131debfc3dSmrg@section Memory model
8141debfc3dSmrg@cindex memory model
8151debfc3dSmrg
8161debfc3dSmrgThe memory model used by the middle-end models that of the C/C++
8171debfc3dSmrglanguages.  The middle-end has the notion of an effective type
8181debfc3dSmrgof a memory region which is used for type-based alias analysis.
8191debfc3dSmrg
8201debfc3dSmrgThe following is a refinement of ISO C99 6.5/6, clarifying the block copy case
8211debfc3dSmrgto follow common sense and extending the concept of a dynamic effective
8221debfc3dSmrgtype to objects with a declared type as required for C++.
8231debfc3dSmrg
8241debfc3dSmrg@smallexample
8251debfc3dSmrgThe effective type of an object for an access to its stored value is
8261debfc3dSmrgthe declared type of the object or the effective type determined by
8271debfc3dSmrga previous store to it.  If a value is stored into an object through
8281debfc3dSmrgan lvalue having a type that is not a character type, then the
8291debfc3dSmrgtype of the lvalue becomes the effective type of the object for that
8301debfc3dSmrgaccess and for subsequent accesses that do not modify the stored value.
8311debfc3dSmrgIf a value is copied into an object using @code{memcpy} or @code{memmove},
8321debfc3dSmrgor is copied as an array of character type, then the effective type
8331debfc3dSmrgof the modified object for that access and for subsequent accesses that
8341debfc3dSmrgdo not modify the value is undetermined.  For all other accesses to an
8351debfc3dSmrgobject, the effective type of the object is simply the type of the
8361debfc3dSmrglvalue used for the access.
8371debfc3dSmrg@end smallexample
8381debfc3dSmrg
839