xref: /dflybsd-src/contrib/gcc-4.7/gcc/doc/tree-ssa.texi (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino@c Copyright (c) 2004, 2005, 2007, 2008, 2010
2*e4b17023SJohn Marino@c Free Software Foundation, Inc.
3*e4b17023SJohn Marino@c This is part of the GCC manual.
4*e4b17023SJohn Marino@c For copying conditions, see the file gcc.texi.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino@c ---------------------------------------------------------------------
7*e4b17023SJohn Marino@c Tree SSA
8*e4b17023SJohn Marino@c ---------------------------------------------------------------------
9*e4b17023SJohn Marino
10*e4b17023SJohn Marino@node Tree SSA
11*e4b17023SJohn Marino@chapter Analysis and Optimization of GIMPLE tuples
12*e4b17023SJohn Marino@cindex Tree SSA
13*e4b17023SJohn Marino@cindex Optimization infrastructure for GIMPLE
14*e4b17023SJohn Marino
15*e4b17023SJohn MarinoGCC uses three main intermediate languages to represent the program
16*e4b17023SJohn Marinoduring compilation: GENERIC, GIMPLE and RTL@.  GENERIC is a
17*e4b17023SJohn Marinolanguage-independent representation generated by each front end.  It
18*e4b17023SJohn Marinois used to serve as an interface between the parser and optimizer.
19*e4b17023SJohn MarinoGENERIC is a common representation that is able to represent programs
20*e4b17023SJohn Marinowritten in all the languages supported by GCC@.
21*e4b17023SJohn Marino
22*e4b17023SJohn MarinoGIMPLE and RTL are used to optimize the program.  GIMPLE is used for
23*e4b17023SJohn Marinotarget and language independent optimizations (e.g., inlining,
24*e4b17023SJohn Marinoconstant propagation, tail call elimination, redundancy elimination,
25*e4b17023SJohn Marinoetc).  Much like GENERIC, GIMPLE is a language independent, tree based
26*e4b17023SJohn Marinorepresentation.  However, it differs from GENERIC in that the GIMPLE
27*e4b17023SJohn Marinogrammar is more restrictive: expressions contain no more than 3
28*e4b17023SJohn Marinooperands (except function calls), it has no control flow structures
29*e4b17023SJohn Marinoand expressions with side-effects are only allowed on the right hand
30*e4b17023SJohn Marinoside of assignments.  See the chapter describing GENERIC and GIMPLE
31*e4b17023SJohn Marinofor more details.
32*e4b17023SJohn Marino
33*e4b17023SJohn MarinoThis chapter describes the data structures and functions used in the
34*e4b17023SJohn MarinoGIMPLE optimizers (also known as ``tree optimizers'' or ``middle
35*e4b17023SJohn Marinoend'').  In particular, it focuses on all the macros, data structures,
36*e4b17023SJohn Marinofunctions and programming constructs needed to implement optimization
37*e4b17023SJohn Marinopasses for GIMPLE@.
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino@menu
40*e4b17023SJohn Marino* Annotations::         Attributes for variables.
41*e4b17023SJohn Marino* SSA Operands::        SSA names referenced by GIMPLE statements.
42*e4b17023SJohn Marino* SSA::                 Static Single Assignment representation.
43*e4b17023SJohn Marino* Alias analysis::      Representing aliased loads and stores.
44*e4b17023SJohn Marino* Memory model::        Memory model used by the middle-end.
45*e4b17023SJohn Marino@end menu
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino@node Annotations
48*e4b17023SJohn Marino@section Annotations
49*e4b17023SJohn Marino@cindex annotations
50*e4b17023SJohn Marino
51*e4b17023SJohn MarinoThe optimizers need to associate attributes with variables during the
52*e4b17023SJohn Marinooptimization process.  For instance, we need to know whether a
53*e4b17023SJohn Marinovariable has aliases.  All these attributes are stored in data
54*e4b17023SJohn Marinostructures called annotations which are then linked to the field
55*e4b17023SJohn Marino@code{ann} in @code{struct tree_common}.
56*e4b17023SJohn Marino
57*e4b17023SJohn MarinoPresently, we define annotations for variables (@code{var_ann_t}).
58*e4b17023SJohn MarinoAnnotations are defined and documented in @file{tree-flow.h}.
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino@node SSA Operands
62*e4b17023SJohn Marino@section SSA Operands
63*e4b17023SJohn Marino@cindex operands
64*e4b17023SJohn Marino@cindex virtual operands
65*e4b17023SJohn Marino@cindex real operands
66*e4b17023SJohn Marino@findex update_stmt
67*e4b17023SJohn Marino
68*e4b17023SJohn MarinoAlmost every GIMPLE statement will contain a reference to a variable
69*e4b17023SJohn Marinoor memory location.  Since statements come in different shapes and
70*e4b17023SJohn Marinosizes, their operands are going to be located at various spots inside
71*e4b17023SJohn Marinothe statement's tree.  To facilitate access to the statement's
72*e4b17023SJohn Marinooperands, they are organized into lists associated inside each
73*e4b17023SJohn Marinostatement's annotation.  Each element in an operand list is a pointer
74*e4b17023SJohn Marinoto a @code{VAR_DECL}, @code{PARM_DECL} or @code{SSA_NAME} tree node.
75*e4b17023SJohn MarinoThis provides a very convenient way of examining and replacing
76*e4b17023SJohn Marinooperands.
77*e4b17023SJohn Marino
78*e4b17023SJohn MarinoData flow analysis and optimization is done on all tree nodes
79*e4b17023SJohn Marinorepresenting variables.  Any node for which @code{SSA_VAR_P} returns
80*e4b17023SJohn Marinononzero is considered when scanning statement operands.  However, not
81*e4b17023SJohn Marinoall @code{SSA_VAR_P} variables are processed in the same way.  For the
82*e4b17023SJohn Marinopurposes of optimization, we need to distinguish between references to
83*e4b17023SJohn Marinolocal scalar variables and references to globals, statics, structures,
84*e4b17023SJohn Marinoarrays, aliased variables, etc.  The reason is simple, the compiler
85*e4b17023SJohn Marinocan gather complete data flow information for a local scalar.  On the
86*e4b17023SJohn Marinoother hand, a global variable may be modified by a function call, it
87*e4b17023SJohn Marinomay not be possible to keep track of all the elements of an array or
88*e4b17023SJohn Marinothe fields of a structure, etc.
89*e4b17023SJohn Marino
90*e4b17023SJohn MarinoThe operand scanner gathers two kinds of operands: @dfn{real} and
91*e4b17023SJohn Marino@dfn{virtual}.  An operand for which @code{is_gimple_reg} returns true
92*e4b17023SJohn Marinois considered real, otherwise it is a virtual operand.  We also
93*e4b17023SJohn Marinodistinguish between uses and definitions.  An operand is used if its
94*e4b17023SJohn Marinovalue is loaded by the statement (e.g., the operand at the RHS of an
95*e4b17023SJohn Marinoassignment).  If the statement assigns a new value to the operand, the
96*e4b17023SJohn Marinooperand is considered a definition (e.g., the operand at the LHS of
97*e4b17023SJohn Marinoan assignment).
98*e4b17023SJohn Marino
99*e4b17023SJohn MarinoVirtual and real operands also have very different data flow
100*e4b17023SJohn Marinoproperties.  Real operands are unambiguous references to the
101*e4b17023SJohn Marinofull object that they represent.  For instance, given
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino@smallexample
104*e4b17023SJohn Marino@{
105*e4b17023SJohn Marino  int a, b;
106*e4b17023SJohn Marino  a = b
107*e4b17023SJohn Marino@}
108*e4b17023SJohn Marino@end smallexample
109*e4b17023SJohn Marino
110*e4b17023SJohn MarinoSince @code{a} and @code{b} are non-aliased locals, the statement
111*e4b17023SJohn Marino@code{a = b} will have one real definition and one real use because
112*e4b17023SJohn Marinovariable @code{a} is completely modified with the contents of
113*e4b17023SJohn Marinovariable @code{b}.  Real definition are also known as @dfn{killing
114*e4b17023SJohn Marinodefinitions}.  Similarly, the use of @code{b} reads all its bits.
115*e4b17023SJohn Marino
116*e4b17023SJohn MarinoIn contrast, virtual operands are used with variables that can have
117*e4b17023SJohn Marinoa partial or ambiguous reference.  This includes structures, arrays,
118*e4b17023SJohn Marinoglobals, and aliased variables.  In these cases, we have two types of
119*e4b17023SJohn Marinodefinitions.  For globals, structures, and arrays, we can determine from
120*e4b17023SJohn Marinoa statement whether a variable of these types has a killing definition.
121*e4b17023SJohn MarinoIf the variable does, then the statement is marked as having a
122*e4b17023SJohn Marino@dfn{must definition} of that variable.  However, if a statement is only
123*e4b17023SJohn Marinodefining a part of the variable (i.e.@: a field in a structure), or if we
124*e4b17023SJohn Marinoknow that a statement might define the variable but we cannot say for sure,
125*e4b17023SJohn Marinothen we mark that statement as having a @dfn{may definition}.  For
126*e4b17023SJohn Marinoinstance, given
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino@smallexample
129*e4b17023SJohn Marino@{
130*e4b17023SJohn Marino  int a, b, *p;
131*e4b17023SJohn Marino
132*e4b17023SJohn Marino  if (@dots{})
133*e4b17023SJohn Marino    p = &a;
134*e4b17023SJohn Marino  else
135*e4b17023SJohn Marino    p = &b;
136*e4b17023SJohn Marino  *p = 5;
137*e4b17023SJohn Marino  return *p;
138*e4b17023SJohn Marino@}
139*e4b17023SJohn Marino@end smallexample
140*e4b17023SJohn Marino
141*e4b17023SJohn MarinoThe assignment @code{*p = 5} may be a definition of @code{a} or
142*e4b17023SJohn Marino@code{b}.  If we cannot determine statically where @code{p} is
143*e4b17023SJohn Marinopointing to at the time of the store operation, we create virtual
144*e4b17023SJohn Marinodefinitions to mark that statement as a potential definition site for
145*e4b17023SJohn Marino@code{a} and @code{b}.  Memory loads are similarly marked with virtual
146*e4b17023SJohn Marinouse operands.  Virtual operands are shown in tree dumps right before
147*e4b17023SJohn Marinothe statement that contains them.  To request a tree dump with virtual
148*e4b17023SJohn Marinooperands, use the @option{-vops} option to @option{-fdump-tree}:
149*e4b17023SJohn Marino
150*e4b17023SJohn Marino@smallexample
151*e4b17023SJohn Marino@{
152*e4b17023SJohn Marino  int a, b, *p;
153*e4b17023SJohn Marino
154*e4b17023SJohn Marino  if (@dots{})
155*e4b17023SJohn Marino    p = &a;
156*e4b17023SJohn Marino  else
157*e4b17023SJohn Marino    p = &b;
158*e4b17023SJohn Marino  # a = VDEF <a>
159*e4b17023SJohn Marino  # b = VDEF <b>
160*e4b17023SJohn Marino  *p = 5;
161*e4b17023SJohn Marino
162*e4b17023SJohn Marino  # VUSE <a>
163*e4b17023SJohn Marino  # VUSE <b>
164*e4b17023SJohn Marino  return *p;
165*e4b17023SJohn Marino@}
166*e4b17023SJohn Marino@end smallexample
167*e4b17023SJohn Marino
168*e4b17023SJohn MarinoNotice that @code{VDEF} operands have two copies of the referenced
169*e4b17023SJohn Marinovariable.  This indicates that this is not a killing definition of
170*e4b17023SJohn Marinothat variable.  In this case we refer to it as a @dfn{may definition}
171*e4b17023SJohn Marinoor @dfn{aliased store}.  The presence of the second copy of the
172*e4b17023SJohn Marinovariable in the @code{VDEF} operand will become important when the
173*e4b17023SJohn Marinofunction is converted into SSA form.  This will be used to link all
174*e4b17023SJohn Marinothe non-killing definitions to prevent optimizations from making
175*e4b17023SJohn Marinoincorrect assumptions about them.
176*e4b17023SJohn Marino
177*e4b17023SJohn MarinoOperands are updated as soon as the statement is finished via a call
178*e4b17023SJohn Marinoto @code{update_stmt}.  If statement elements are changed via
179*e4b17023SJohn Marino@code{SET_USE} or @code{SET_DEF}, then no further action is required
180*e4b17023SJohn Marino(i.e., those macros take care of updating the statement).  If changes
181*e4b17023SJohn Marinoare made by manipulating the statement's tree directly, then a call
182*e4b17023SJohn Marinomust be made to @code{update_stmt} when complete.  Calling one of the
183*e4b17023SJohn Marino@code{bsi_insert} routines or @code{bsi_replace} performs an implicit
184*e4b17023SJohn Marinocall to @code{update_stmt}.
185*e4b17023SJohn Marino
186*e4b17023SJohn Marino@subsection Operand Iterators And Access Routines
187*e4b17023SJohn Marino@cindex Operand Iterators
188*e4b17023SJohn Marino@cindex Operand Access Routines
189*e4b17023SJohn Marino
190*e4b17023SJohn MarinoOperands are collected by @file{tree-ssa-operands.c}.  They are stored
191*e4b17023SJohn Marinoinside each statement's annotation and can be accessed through either the
192*e4b17023SJohn Marinooperand iterators or an access routine.
193*e4b17023SJohn Marino
194*e4b17023SJohn MarinoThe following access routines are available for examining operands:
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino@enumerate
197*e4b17023SJohn Marino@item @code{SINGLE_SSA_@{USE,DEF,TREE@}_OPERAND}: These accessors will return
198*e4b17023SJohn MarinoNULL unless there is exactly one operand matching the specified flags.  If
199*e4b17023SJohn Marinothere is exactly one operand, the operand is returned as either a @code{tree},
200*e4b17023SJohn Marino@code{def_operand_p}, or @code{use_operand_p}.
201*e4b17023SJohn Marino
202*e4b17023SJohn Marino@smallexample
203*e4b17023SJohn Marinotree t = SINGLE_SSA_TREE_OPERAND (stmt, flags);
204*e4b17023SJohn Marinouse_operand_p u = SINGLE_SSA_USE_OPERAND (stmt, SSA_ALL_VIRTUAL_USES);
205*e4b17023SJohn Marinodef_operand_p d = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_ALL_DEFS);
206*e4b17023SJohn Marino@end smallexample
207*e4b17023SJohn Marino
208*e4b17023SJohn Marino@item @code{ZERO_SSA_OPERANDS}: This macro returns true if there are no
209*e4b17023SJohn Marinooperands matching the specified flags.
210*e4b17023SJohn Marino
211*e4b17023SJohn Marino@smallexample
212*e4b17023SJohn Marinoif (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
213*e4b17023SJohn Marino  return;
214*e4b17023SJohn Marino@end smallexample
215*e4b17023SJohn Marino
216*e4b17023SJohn Marino@item @code{NUM_SSA_OPERANDS}: This macro Returns the number of operands
217*e4b17023SJohn Marinomatching 'flags'.  This actually executes a loop to perform the count, so
218*e4b17023SJohn Marinoonly use this if it is really needed.
219*e4b17023SJohn Marino
220*e4b17023SJohn Marino@smallexample
221*e4b17023SJohn Marinoint count = NUM_SSA_OPERANDS (stmt, flags)
222*e4b17023SJohn Marino@end smallexample
223*e4b17023SJohn Marino@end enumerate
224*e4b17023SJohn Marino
225*e4b17023SJohn Marino
226*e4b17023SJohn MarinoIf you wish to iterate over some or all operands, use the
227*e4b17023SJohn Marino@code{FOR_EACH_SSA_@{USE,DEF,TREE@}_OPERAND} iterator.  For example, to print
228*e4b17023SJohn Marinoall the operands for a statement:
229*e4b17023SJohn Marino
230*e4b17023SJohn Marino@smallexample
231*e4b17023SJohn Marinovoid
232*e4b17023SJohn Marinoprint_ops (tree stmt)
233*e4b17023SJohn Marino@{
234*e4b17023SJohn Marino  ssa_op_iter;
235*e4b17023SJohn Marino  tree var;
236*e4b17023SJohn Marino
237*e4b17023SJohn Marino  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
238*e4b17023SJohn Marino    print_generic_expr (stderr, var, TDF_SLIM);
239*e4b17023SJohn Marino@}
240*e4b17023SJohn Marino@end smallexample
241*e4b17023SJohn Marino
242*e4b17023SJohn Marino
243*e4b17023SJohn MarinoHow to choose the appropriate iterator:
244*e4b17023SJohn Marino
245*e4b17023SJohn Marino@enumerate
246*e4b17023SJohn Marino@item Determine whether you are need to see the operand pointers, or just the
247*e4b17023SJohn Marinotrees, and choose the appropriate macro:
248*e4b17023SJohn Marino
249*e4b17023SJohn Marino@smallexample
250*e4b17023SJohn MarinoNeed            Macro:
251*e4b17023SJohn Marino----            -------
252*e4b17023SJohn Marinouse_operand_p   FOR_EACH_SSA_USE_OPERAND
253*e4b17023SJohn Marinodef_operand_p   FOR_EACH_SSA_DEF_OPERAND
254*e4b17023SJohn Marinotree            FOR_EACH_SSA_TREE_OPERAND
255*e4b17023SJohn Marino@end smallexample
256*e4b17023SJohn Marino
257*e4b17023SJohn Marino@item You need to declare a variable of the type you are interested
258*e4b17023SJohn Marinoin, and an ssa_op_iter structure which serves as the loop controlling
259*e4b17023SJohn Marinovariable.
260*e4b17023SJohn Marino
261*e4b17023SJohn Marino@item Determine which operands you wish to use, and specify the flags of
262*e4b17023SJohn Marinothose you are interested in.  They are documented in
263*e4b17023SJohn Marino@file{tree-ssa-operands.h}:
264*e4b17023SJohn Marino
265*e4b17023SJohn Marino@smallexample
266*e4b17023SJohn Marino#define SSA_OP_USE              0x01    /* @r{Real USE operands.}  */
267*e4b17023SJohn Marino#define SSA_OP_DEF              0x02    /* @r{Real DEF operands.}  */
268*e4b17023SJohn Marino#define SSA_OP_VUSE             0x04    /* @r{VUSE operands.}  */
269*e4b17023SJohn Marino#define SSA_OP_VMAYUSE          0x08    /* @r{USE portion of VDEFS.}  */
270*e4b17023SJohn Marino#define SSA_OP_VDEF             0x10    /* @r{DEF portion of VDEFS.}  */
271*e4b17023SJohn Marino
272*e4b17023SJohn Marino/* @r{These are commonly grouped operand flags.}  */
273*e4b17023SJohn Marino#define SSA_OP_VIRTUAL_USES     (SSA_OP_VUSE | SSA_OP_VMAYUSE)
274*e4b17023SJohn Marino#define SSA_OP_VIRTUAL_DEFS     (SSA_OP_VDEF)
275*e4b17023SJohn Marino#define SSA_OP_ALL_USES         (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
276*e4b17023SJohn Marino#define SSA_OP_ALL_DEFS         (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
277*e4b17023SJohn Marino#define SSA_OP_ALL_OPERANDS     (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
278*e4b17023SJohn Marino@end smallexample
279*e4b17023SJohn Marino@end enumerate
280*e4b17023SJohn Marino
281*e4b17023SJohn MarinoSo if you want to look at the use pointers for all the @code{USE} and
282*e4b17023SJohn Marino@code{VUSE} operands, you would do something like:
283*e4b17023SJohn Marino
284*e4b17023SJohn Marino@smallexample
285*e4b17023SJohn Marino  use_operand_p use_p;
286*e4b17023SJohn Marino  ssa_op_iter iter;
287*e4b17023SJohn Marino
288*e4b17023SJohn Marino  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
289*e4b17023SJohn Marino    @{
290*e4b17023SJohn Marino      process_use_ptr (use_p);
291*e4b17023SJohn Marino    @}
292*e4b17023SJohn Marino@end smallexample
293*e4b17023SJohn Marino
294*e4b17023SJohn MarinoThe @code{TREE} macro is basically the same as the @code{USE} and
295*e4b17023SJohn Marino@code{DEF} macros, only with the use or def dereferenced via
296*e4b17023SJohn Marino@code{USE_FROM_PTR (use_p)} and @code{DEF_FROM_PTR (def_p)}.  Since we
297*e4b17023SJohn Marinoaren't using operand pointers, use and defs flags can be mixed.
298*e4b17023SJohn Marino
299*e4b17023SJohn Marino@smallexample
300*e4b17023SJohn Marino  tree var;
301*e4b17023SJohn Marino  ssa_op_iter iter;
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE)
304*e4b17023SJohn Marino    @{
305*e4b17023SJohn Marino       print_generic_expr (stderr, var, TDF_SLIM);
306*e4b17023SJohn Marino    @}
307*e4b17023SJohn Marino@end smallexample
308*e4b17023SJohn Marino
309*e4b17023SJohn Marino@code{VDEF}s are broken into two flags, one for the
310*e4b17023SJohn Marino@code{DEF} portion (@code{SSA_OP_VDEF}) and one for the USE portion
311*e4b17023SJohn Marino(@code{SSA_OP_VMAYUSE}).  If all you want to look at are the
312*e4b17023SJohn Marino@code{VDEF}s together, there is a fourth iterator macro for this,
313*e4b17023SJohn Marinowhich returns both a def_operand_p and a use_operand_p for each
314*e4b17023SJohn Marino@code{VDEF} in the statement.  Note that you don't need any flags for
315*e4b17023SJohn Marinothis one.
316*e4b17023SJohn Marino
317*e4b17023SJohn Marino@smallexample
318*e4b17023SJohn Marino  use_operand_p use_p;
319*e4b17023SJohn Marino  def_operand_p def_p;
320*e4b17023SJohn Marino  ssa_op_iter iter;
321*e4b17023SJohn Marino
322*e4b17023SJohn Marino  FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
323*e4b17023SJohn Marino    @{
324*e4b17023SJohn Marino      my_code;
325*e4b17023SJohn Marino    @}
326*e4b17023SJohn Marino@end smallexample
327*e4b17023SJohn Marino
328*e4b17023SJohn MarinoThere are many examples in the code as well, as well as the
329*e4b17023SJohn Marinodocumentation in @file{tree-ssa-operands.h}.
330*e4b17023SJohn Marino
331*e4b17023SJohn MarinoThere are also a couple of variants on the stmt iterators regarding PHI
332*e4b17023SJohn Marinonodes.
333*e4b17023SJohn Marino
334*e4b17023SJohn Marino@code{FOR_EACH_PHI_ARG} Works exactly like
335*e4b17023SJohn Marino@code{FOR_EACH_SSA_USE_OPERAND}, except it works over @code{PHI} arguments
336*e4b17023SJohn Marinoinstead of statement operands.
337*e4b17023SJohn Marino
338*e4b17023SJohn Marino@smallexample
339*e4b17023SJohn Marino/* Look at every virtual PHI use.  */
340*e4b17023SJohn MarinoFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_VIRTUAL_USES)
341*e4b17023SJohn Marino@{
342*e4b17023SJohn Marino   my_code;
343*e4b17023SJohn Marino@}
344*e4b17023SJohn Marino
345*e4b17023SJohn Marino/* Look at every real PHI use.  */
346*e4b17023SJohn MarinoFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_USES)
347*e4b17023SJohn Marino  my_code;
348*e4b17023SJohn Marino
349*e4b17023SJohn Marino/* Look at every PHI use.  */
350*e4b17023SJohn MarinoFOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_ALL_USES)
351*e4b17023SJohn Marino  my_code;
352*e4b17023SJohn Marino@end smallexample
353*e4b17023SJohn Marino
354*e4b17023SJohn Marino@code{FOR_EACH_PHI_OR_STMT_@{USE,DEF@}} works exactly like
355*e4b17023SJohn Marino@code{FOR_EACH_SSA_@{USE,DEF@}_OPERAND}, except it will function on
356*e4b17023SJohn Marinoeither a statement or a @code{PHI} node.  These should be used when it is
357*e4b17023SJohn Marinoappropriate but they are not quite as efficient as the individual
358*e4b17023SJohn Marino@code{FOR_EACH_PHI} and @code{FOR_EACH_SSA} routines.
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino@smallexample
361*e4b17023SJohn MarinoFOR_EACH_PHI_OR_STMT_USE (use_operand_p, stmt, iter, flags)
362*e4b17023SJohn Marino  @{
363*e4b17023SJohn Marino     my_code;
364*e4b17023SJohn Marino  @}
365*e4b17023SJohn Marino
366*e4b17023SJohn MarinoFOR_EACH_PHI_OR_STMT_DEF (def_operand_p, phi, iter, flags)
367*e4b17023SJohn Marino  @{
368*e4b17023SJohn Marino     my_code;
369*e4b17023SJohn Marino  @}
370*e4b17023SJohn Marino@end smallexample
371*e4b17023SJohn Marino
372*e4b17023SJohn Marino@subsection Immediate Uses
373*e4b17023SJohn Marino@cindex Immediate Uses
374*e4b17023SJohn Marino
375*e4b17023SJohn MarinoImmediate use information is now always available.  Using the immediate use
376*e4b17023SJohn Marinoiterators, you may examine every use of any @code{SSA_NAME}. For instance,
377*e4b17023SJohn Marinoto change each use of @code{ssa_var} to @code{ssa_var2} and call fold_stmt on
378*e4b17023SJohn Marinoeach stmt after that is done:
379*e4b17023SJohn Marino
380*e4b17023SJohn Marino@smallexample
381*e4b17023SJohn Marino  use_operand_p imm_use_p;
382*e4b17023SJohn Marino  imm_use_iterator iterator;
383*e4b17023SJohn Marino  tree ssa_var, stmt;
384*e4b17023SJohn Marino
385*e4b17023SJohn Marino
386*e4b17023SJohn Marino  FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
387*e4b17023SJohn Marino    @{
388*e4b17023SJohn Marino      FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
389*e4b17023SJohn Marino        SET_USE (imm_use_p, ssa_var_2);
390*e4b17023SJohn Marino      fold_stmt (stmt);
391*e4b17023SJohn Marino    @}
392*e4b17023SJohn Marino@end smallexample
393*e4b17023SJohn Marino
394*e4b17023SJohn MarinoThere are 2 iterators which can be used. @code{FOR_EACH_IMM_USE_FAST} is
395*e4b17023SJohn Marinoused when the immediate uses are not changed, i.e., you are looking at the
396*e4b17023SJohn Marinouses, but not setting them.
397*e4b17023SJohn Marino
398*e4b17023SJohn MarinoIf they do get changed, then care must be taken that things are not changed
399*e4b17023SJohn Marinounder the iterators, so use the @code{FOR_EACH_IMM_USE_STMT} and
400*e4b17023SJohn Marino@code{FOR_EACH_IMM_USE_ON_STMT} iterators.  They attempt to preserve the
401*e4b17023SJohn Marinosanity of the use list by moving all the uses for a statement into
402*e4b17023SJohn Marinoa controlled position, and then iterating over those uses.  Then the
403*e4b17023SJohn Marinooptimization can manipulate the stmt when all the uses have been
404*e4b17023SJohn Marinoprocessed.  This is a little slower than the FAST version since it adds a
405*e4b17023SJohn Marinoplaceholder element and must sort through the list a bit for each statement.
406*e4b17023SJohn MarinoThis placeholder element must be also be removed if the loop is
407*e4b17023SJohn Marinoterminated early.  The macro @code{BREAK_FROM_IMM_USE_SAFE} is provided
408*e4b17023SJohn Marinoto do this :
409*e4b17023SJohn Marino
410*e4b17023SJohn Marino@smallexample
411*e4b17023SJohn Marino  FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
412*e4b17023SJohn Marino    @{
413*e4b17023SJohn Marino      if (stmt == last_stmt)
414*e4b17023SJohn Marino        BREAK_FROM_SAFE_IMM_USE (iter);
415*e4b17023SJohn Marino
416*e4b17023SJohn Marino      FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
417*e4b17023SJohn Marino        SET_USE (imm_use_p, ssa_var_2);
418*e4b17023SJohn Marino      fold_stmt (stmt);
419*e4b17023SJohn Marino    @}
420*e4b17023SJohn Marino@end smallexample
421*e4b17023SJohn Marino
422*e4b17023SJohn MarinoThere are checks in @code{verify_ssa} which verify that the immediate use list
423*e4b17023SJohn Marinois up to date, as well as checking that an optimization didn't break from the
424*e4b17023SJohn Marinoloop without using this macro.  It is safe to simply 'break'; from a
425*e4b17023SJohn Marino@code{FOR_EACH_IMM_USE_FAST} traverse.
426*e4b17023SJohn Marino
427*e4b17023SJohn MarinoSome useful functions and macros:
428*e4b17023SJohn Marino@enumerate
429*e4b17023SJohn Marino@item  @code{has_zero_uses (ssa_var)} : Returns true if there are no uses of
430*e4b17023SJohn Marino@code{ssa_var}.
431*e4b17023SJohn Marino@item   @code{has_single_use (ssa_var)} : Returns true if there is only a
432*e4b17023SJohn Marinosingle use of @code{ssa_var}.
433*e4b17023SJohn Marino@item   @code{single_imm_use (ssa_var, use_operand_p *ptr, tree *stmt)} :
434*e4b17023SJohn MarinoReturns true if there is only a single use of @code{ssa_var}, and also returns
435*e4b17023SJohn Marinothe use pointer and statement it occurs in, in the second and third parameters.
436*e4b17023SJohn Marino@item   @code{num_imm_uses (ssa_var)} : Returns the number of immediate uses of
437*e4b17023SJohn Marino@code{ssa_var}. It is better not to use this if possible since it simply
438*e4b17023SJohn Marinoutilizes a loop to count the uses.
439*e4b17023SJohn Marino@item  @code{PHI_ARG_INDEX_FROM_USE (use_p)} : Given a use within a @code{PHI}
440*e4b17023SJohn Marinonode, return the index number for the use.  An assert is triggered if the use
441*e4b17023SJohn Marinoisn't located in a @code{PHI} node.
442*e4b17023SJohn Marino@item  @code{USE_STMT (use_p)} : Return the statement a use occurs in.
443*e4b17023SJohn Marino@end enumerate
444*e4b17023SJohn Marino
445*e4b17023SJohn MarinoNote that uses are not put into an immediate use list until their statement is
446*e4b17023SJohn Marinoactually inserted into the instruction stream via a @code{bsi_*} routine.
447*e4b17023SJohn Marino
448*e4b17023SJohn MarinoIt is also still possible to utilize lazy updating of statements, but this
449*e4b17023SJohn Marinoshould be used only when absolutely required.  Both alias analysis and the
450*e4b17023SJohn Marinodominator optimizations currently do this.
451*e4b17023SJohn Marino
452*e4b17023SJohn MarinoWhen lazy updating is being used, the immediate use information is out of date
453*e4b17023SJohn Marinoand cannot be used reliably.  Lazy updating is achieved by simply marking
454*e4b17023SJohn Marinostatements modified via calls to @code{mark_stmt_modified} instead of
455*e4b17023SJohn Marino@code{update_stmt}.  When lazy updating is no longer required, all the
456*e4b17023SJohn Marinomodified statements must have @code{update_stmt} called in order to bring them
457*e4b17023SJohn Marinoup to date.  This must be done before the optimization is finished, or
458*e4b17023SJohn Marino@code{verify_ssa} will trigger an abort.
459*e4b17023SJohn Marino
460*e4b17023SJohn MarinoThis is done with a simple loop over the instruction stream:
461*e4b17023SJohn Marino@smallexample
462*e4b17023SJohn Marino  block_stmt_iterator bsi;
463*e4b17023SJohn Marino  basic_block bb;
464*e4b17023SJohn Marino  FOR_EACH_BB (bb)
465*e4b17023SJohn Marino    @{
466*e4b17023SJohn Marino      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
467*e4b17023SJohn Marino        update_stmt_if_modified (bsi_stmt (bsi));
468*e4b17023SJohn Marino    @}
469*e4b17023SJohn Marino@end smallexample
470*e4b17023SJohn Marino
471*e4b17023SJohn Marino@node SSA
472*e4b17023SJohn Marino@section Static Single Assignment
473*e4b17023SJohn Marino@cindex SSA
474*e4b17023SJohn Marino@cindex static single assignment
475*e4b17023SJohn Marino
476*e4b17023SJohn MarinoMost of the tree optimizers rely on the data flow information provided
477*e4b17023SJohn Marinoby the Static Single Assignment (SSA) form.  We implement the SSA form
478*e4b17023SJohn Marinoas described in @cite{R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and
479*e4b17023SJohn MarinoK. Zadeck.  Efficiently Computing Static Single Assignment Form and the
480*e4b17023SJohn MarinoControl Dependence Graph.  ACM Transactions on Programming Languages
481*e4b17023SJohn Marinoand Systems, 13(4):451-490, October 1991}.
482*e4b17023SJohn Marino
483*e4b17023SJohn MarinoThe SSA form is based on the premise that program variables are
484*e4b17023SJohn Marinoassigned in exactly one location in the program.  Multiple assignments
485*e4b17023SJohn Marinoto the same variable create new versions of that variable.  Naturally,
486*e4b17023SJohn Marinoactual programs are seldom in SSA form initially because variables
487*e4b17023SJohn Marinotend to be assigned multiple times.  The compiler modifies the program
488*e4b17023SJohn Marinorepresentation so that every time a variable is assigned in the code,
489*e4b17023SJohn Marinoa new version of the variable is created.  Different versions of the
490*e4b17023SJohn Marinosame variable are distinguished by subscripting the variable name with
491*e4b17023SJohn Marinoits version number.  Variables used in the right-hand side of
492*e4b17023SJohn Marinoexpressions are renamed so that their version number matches that of
493*e4b17023SJohn Marinothe most recent assignment.
494*e4b17023SJohn Marino
495*e4b17023SJohn MarinoWe represent variable versions using @code{SSA_NAME} nodes.  The
496*e4b17023SJohn Marinorenaming process in @file{tree-ssa.c} wraps every real and
497*e4b17023SJohn Marinovirtual operand with an @code{SSA_NAME} node which contains
498*e4b17023SJohn Marinothe version number and the statement that created the
499*e4b17023SJohn Marino@code{SSA_NAME}.  Only definitions and virtual definitions may
500*e4b17023SJohn Marinocreate new @code{SSA_NAME} nodes.
501*e4b17023SJohn Marino
502*e4b17023SJohn Marino@cindex PHI nodes
503*e4b17023SJohn MarinoSometimes, flow of control makes it impossible to determine the
504*e4b17023SJohn Marinomost recent version of a variable.  In these cases, the compiler
505*e4b17023SJohn Marinoinserts an artificial definition for that variable called
506*e4b17023SJohn Marino@dfn{PHI function} or @dfn{PHI node}.  This new definition merges
507*e4b17023SJohn Marinoall the incoming versions of the variable to create a new name
508*e4b17023SJohn Marinofor it.  For instance,
509*e4b17023SJohn Marino
510*e4b17023SJohn Marino@smallexample
511*e4b17023SJohn Marinoif (@dots{})
512*e4b17023SJohn Marino  a_1 = 5;
513*e4b17023SJohn Marinoelse if (@dots{})
514*e4b17023SJohn Marino  a_2 = 2;
515*e4b17023SJohn Marinoelse
516*e4b17023SJohn Marino  a_3 = 13;
517*e4b17023SJohn Marino
518*e4b17023SJohn Marino# a_4 = PHI <a_1, a_2, a_3>
519*e4b17023SJohn Marinoreturn a_4;
520*e4b17023SJohn Marino@end smallexample
521*e4b17023SJohn Marino
522*e4b17023SJohn MarinoSince it is not possible to determine which of the three branches
523*e4b17023SJohn Marinowill be taken at runtime, we don't know which of @code{a_1},
524*e4b17023SJohn Marino@code{a_2} or @code{a_3} to use at the return statement.  So, the
525*e4b17023SJohn MarinoSSA renamer creates a new version @code{a_4} which is assigned
526*e4b17023SJohn Marinothe result of ``merging'' @code{a_1}, @code{a_2} and @code{a_3}.
527*e4b17023SJohn MarinoHence, PHI nodes mean ``one of these operands.  I don't know
528*e4b17023SJohn Marinowhich''.
529*e4b17023SJohn Marino
530*e4b17023SJohn MarinoThe following macros can be used to examine PHI nodes
531*e4b17023SJohn Marino
532*e4b17023SJohn Marino@defmac PHI_RESULT (@var{phi})
533*e4b17023SJohn MarinoReturns the @code{SSA_NAME} created by PHI node @var{phi} (i.e.,
534*e4b17023SJohn Marino@var{phi}'s LHS)@.
535*e4b17023SJohn Marino@end defmac
536*e4b17023SJohn Marino
537*e4b17023SJohn Marino@defmac PHI_NUM_ARGS (@var{phi})
538*e4b17023SJohn MarinoReturns the number of arguments in @var{phi}.  This number is exactly
539*e4b17023SJohn Marinothe number of incoming edges to the basic block holding @var{phi}@.
540*e4b17023SJohn Marino@end defmac
541*e4b17023SJohn Marino
542*e4b17023SJohn Marino@defmac PHI_ARG_ELT (@var{phi}, @var{i})
543*e4b17023SJohn MarinoReturns a tuple representing the @var{i}th argument of @var{phi}@.
544*e4b17023SJohn MarinoEach element of this tuple contains an @code{SSA_NAME} @var{var} and
545*e4b17023SJohn Marinothe incoming edge through which @var{var} flows.
546*e4b17023SJohn Marino@end defmac
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino@defmac PHI_ARG_EDGE (@var{phi}, @var{i})
549*e4b17023SJohn MarinoReturns the incoming edge for the @var{i}th argument of @var{phi}.
550*e4b17023SJohn Marino@end defmac
551*e4b17023SJohn Marino
552*e4b17023SJohn Marino@defmac PHI_ARG_DEF (@var{phi}, @var{i})
553*e4b17023SJohn MarinoReturns the @code{SSA_NAME} for the @var{i}th argument of @var{phi}.
554*e4b17023SJohn Marino@end defmac
555*e4b17023SJohn Marino
556*e4b17023SJohn Marino
557*e4b17023SJohn Marino@subsection Preserving the SSA form
558*e4b17023SJohn Marino@findex update_ssa
559*e4b17023SJohn Marino@cindex preserving SSA form
560*e4b17023SJohn MarinoSome optimization passes make changes to the function that
561*e4b17023SJohn Marinoinvalidate the SSA property.  This can happen when a pass has
562*e4b17023SJohn Marinoadded new symbols or changed the program so that variables that
563*e4b17023SJohn Marinowere previously aliased aren't anymore.  Whenever something like this
564*e4b17023SJohn Marinohappens, the affected symbols must be renamed into SSA form again.
565*e4b17023SJohn MarinoTransformations that emit new code or replicate existing statements
566*e4b17023SJohn Marinowill also need to update the SSA form@.
567*e4b17023SJohn Marino
568*e4b17023SJohn MarinoSince GCC implements two different SSA forms for register and virtual
569*e4b17023SJohn Marinovariables, keeping the SSA form up to date depends on whether you are
570*e4b17023SJohn Marinoupdating register or virtual names.  In both cases, the general idea
571*e4b17023SJohn Marinobehind incremental SSA updates is similar: when new SSA names are
572*e4b17023SJohn Marinocreated, they typically are meant to replace other existing names in
573*e4b17023SJohn Marinothe program@.
574*e4b17023SJohn Marino
575*e4b17023SJohn MarinoFor instance, given the following code:
576*e4b17023SJohn Marino
577*e4b17023SJohn Marino@smallexample
578*e4b17023SJohn Marino     1  L0:
579*e4b17023SJohn Marino     2  x_1 = PHI (0, x_5)
580*e4b17023SJohn Marino     3  if (x_1 < 10)
581*e4b17023SJohn Marino     4    if (x_1 > 7)
582*e4b17023SJohn Marino     5      y_2 = 0
583*e4b17023SJohn Marino     6    else
584*e4b17023SJohn Marino     7      y_3 = x_1 + x_7
585*e4b17023SJohn Marino     8    endif
586*e4b17023SJohn Marino     9    x_5 = x_1 + 1
587*e4b17023SJohn Marino     10   goto L0;
588*e4b17023SJohn Marino     11 endif
589*e4b17023SJohn Marino@end smallexample
590*e4b17023SJohn Marino
591*e4b17023SJohn MarinoSuppose that we insert new names @code{x_10} and @code{x_11} (lines
592*e4b17023SJohn Marino@code{4} and @code{8})@.
593*e4b17023SJohn Marino
594*e4b17023SJohn Marino@smallexample
595*e4b17023SJohn Marino     1  L0:
596*e4b17023SJohn Marino     2  x_1 = PHI (0, x_5)
597*e4b17023SJohn Marino     3  if (x_1 < 10)
598*e4b17023SJohn Marino     4    x_10 = @dots{}
599*e4b17023SJohn Marino     5    if (x_1 > 7)
600*e4b17023SJohn Marino     6      y_2 = 0
601*e4b17023SJohn Marino     7    else
602*e4b17023SJohn Marino     8      x_11 = @dots{}
603*e4b17023SJohn Marino     9      y_3 = x_1 + x_7
604*e4b17023SJohn Marino     10   endif
605*e4b17023SJohn Marino     11   x_5 = x_1 + 1
606*e4b17023SJohn Marino     12   goto L0;
607*e4b17023SJohn Marino     13 endif
608*e4b17023SJohn Marino@end smallexample
609*e4b17023SJohn Marino
610*e4b17023SJohn MarinoWe want to replace all the uses of @code{x_1} with the new definitions
611*e4b17023SJohn Marinoof @code{x_10} and @code{x_11}.  Note that the only uses that should
612*e4b17023SJohn Marinobe replaced are those at lines @code{5}, @code{9} and @code{11}.
613*e4b17023SJohn MarinoAlso, the use of @code{x_7} at line @code{9} should @emph{not} be
614*e4b17023SJohn Marinoreplaced (this is why we cannot just mark symbol @code{x} for
615*e4b17023SJohn Marinorenaming)@.
616*e4b17023SJohn Marino
617*e4b17023SJohn MarinoAdditionally, we may need to insert a PHI node at line @code{11}
618*e4b17023SJohn Marinobecause that is a merge point for @code{x_10} and @code{x_11}.  So the
619*e4b17023SJohn Marinouse of @code{x_1} at line @code{11} will be replaced with the new PHI
620*e4b17023SJohn Marinonode.  The insertion of PHI nodes is optional.  They are not strictly
621*e4b17023SJohn Marinonecessary to preserve the SSA form, and depending on what the caller
622*e4b17023SJohn Marinoinserted, they may not even be useful for the optimizers@.
623*e4b17023SJohn Marino
624*e4b17023SJohn MarinoUpdating the SSA form is a two step process.  First, the pass has to
625*e4b17023SJohn Marinoidentify which names need to be updated and/or which symbols need to
626*e4b17023SJohn Marinobe renamed into SSA form for the first time.  When new names are
627*e4b17023SJohn Marinointroduced to replace existing names in the program, the mapping
628*e4b17023SJohn Marinobetween the old and the new names are registered by calling
629*e4b17023SJohn Marino@code{register_new_name_mapping} (note that if your pass creates new
630*e4b17023SJohn Marinocode by duplicating basic blocks, the call to @code{tree_duplicate_bb}
631*e4b17023SJohn Marinowill set up the necessary mappings automatically).  On the other hand,
632*e4b17023SJohn Marinoif your pass exposes a new symbol that should be put in SSA form for
633*e4b17023SJohn Marinothe first time, the new symbol should be registered with
634*e4b17023SJohn Marino@code{mark_sym_for_renaming}.
635*e4b17023SJohn Marino
636*e4b17023SJohn MarinoAfter the replacement mappings have been registered and new symbols
637*e4b17023SJohn Marinomarked for renaming, a call to @code{update_ssa} makes the registered
638*e4b17023SJohn Marinochanges.  This can be done with an explicit call or by creating
639*e4b17023SJohn Marino@code{TODO} flags in the @code{tree_opt_pass} structure for your pass.
640*e4b17023SJohn MarinoThere are several @code{TODO} flags that control the behavior of
641*e4b17023SJohn Marino@code{update_ssa}:
642*e4b17023SJohn Marino
643*e4b17023SJohn Marino@itemize @bullet
644*e4b17023SJohn Marino@item @code{TODO_update_ssa}.  Update the SSA form inserting PHI nodes
645*e4b17023SJohn Marinofor newly exposed symbols and virtual names marked for updating.
646*e4b17023SJohn MarinoWhen updating real names, only insert PHI nodes for a real name
647*e4b17023SJohn Marino@code{O_j} in blocks reached by all the new and old definitions for
648*e4b17023SJohn Marino@code{O_j}.  If the iterated dominance frontier for @code{O_j}
649*e4b17023SJohn Marinois not pruned, we may end up inserting PHI nodes in blocks that
650*e4b17023SJohn Marinohave one or more edges with no incoming definition for
651*e4b17023SJohn Marino@code{O_j}.  This would lead to uninitialized warnings for
652*e4b17023SJohn Marino@code{O_j}'s symbol@.
653*e4b17023SJohn Marino
654*e4b17023SJohn Marino@item @code{TODO_update_ssa_no_phi}.  Update the SSA form without
655*e4b17023SJohn Marinoinserting any new PHI nodes at all.  This is used by passes that
656*e4b17023SJohn Marinohave either inserted all the PHI nodes themselves or passes that
657*e4b17023SJohn Marinoneed only to patch use-def and def-def chains for virtuals
658*e4b17023SJohn Marino(e.g., DCE)@.
659*e4b17023SJohn Marino
660*e4b17023SJohn Marino
661*e4b17023SJohn Marino@item @code{TODO_update_ssa_full_phi}.  Insert PHI nodes everywhere
662*e4b17023SJohn Marinothey are needed.  No pruning of the IDF is done.  This is used
663*e4b17023SJohn Marinoby passes that need the PHI nodes for @code{O_j} even if it
664*e4b17023SJohn Marinomeans that some arguments will come from the default definition
665*e4b17023SJohn Marinoof @code{O_j}'s symbol (e.g., @code{pass_linear_transform})@.
666*e4b17023SJohn Marino
667*e4b17023SJohn MarinoWARNING: If you need to use this flag, chances are that your
668*e4b17023SJohn Marinopass may be doing something wrong.  Inserting PHI nodes for an
669*e4b17023SJohn Marinoold name where not all edges carry a new replacement may lead to
670*e4b17023SJohn Marinosilent codegen errors or spurious uninitialized warnings@.
671*e4b17023SJohn Marino
672*e4b17023SJohn Marino@item @code{TODO_update_ssa_only_virtuals}.  Passes that update the
673*e4b17023SJohn MarinoSSA form on their own may want to delegate the updating of
674*e4b17023SJohn Marinovirtual names to the generic updater.  Since FUD chains are
675*e4b17023SJohn Marinoeasier to maintain, this simplifies the work they need to do.
676*e4b17023SJohn MarinoNOTE: If this flag is used, any OLD->NEW mappings for real names
677*e4b17023SJohn Marinoare explicitly destroyed and only the symbols marked for
678*e4b17023SJohn Marinorenaming are processed@.
679*e4b17023SJohn Marino@end itemize
680*e4b17023SJohn Marino
681*e4b17023SJohn Marino@subsection Preserving the virtual SSA form
682*e4b17023SJohn Marino@cindex preserving virtual SSA form
683*e4b17023SJohn Marino
684*e4b17023SJohn MarinoThe virtual SSA form is harder to preserve than the non-virtual SSA form
685*e4b17023SJohn Marinomainly because the set of virtual operands for a statement may change at
686*e4b17023SJohn Marinowhat some would consider unexpected times.  In general, statement
687*e4b17023SJohn Marinomodifications should be bracketed between calls to
688*e4b17023SJohn Marino@code{push_stmt_changes} and @code{pop_stmt_changes}.  For example,
689*e4b17023SJohn Marino
690*e4b17023SJohn Marino@smallexample
691*e4b17023SJohn Marino    munge_stmt (tree stmt)
692*e4b17023SJohn Marino    @{
693*e4b17023SJohn Marino       push_stmt_changes (&stmt);
694*e4b17023SJohn Marino       @dots{} rewrite STMT @dots{}
695*e4b17023SJohn Marino       pop_stmt_changes (&stmt);
696*e4b17023SJohn Marino    @}
697*e4b17023SJohn Marino@end smallexample
698*e4b17023SJohn Marino
699*e4b17023SJohn MarinoThe call to @code{push_stmt_changes} saves the current state of the
700*e4b17023SJohn Marinostatement operands and the call to @code{pop_stmt_changes} compares
701*e4b17023SJohn Marinothe saved state with the current one and does the appropriate symbol
702*e4b17023SJohn Marinomarking for the SSA renamer.
703*e4b17023SJohn Marino
704*e4b17023SJohn MarinoIt is possible to modify several statements at a time, provided that
705*e4b17023SJohn Marino@code{push_stmt_changes} and @code{pop_stmt_changes} are called in
706*e4b17023SJohn MarinoLIFO order, as when processing a stack of statements.
707*e4b17023SJohn Marino
708*e4b17023SJohn MarinoAdditionally, if the pass discovers that it did not need to make
709*e4b17023SJohn Marinochanges to the statement after calling @code{push_stmt_changes}, it
710*e4b17023SJohn Marinocan simply discard the topmost change buffer by calling
711*e4b17023SJohn Marino@code{discard_stmt_changes}.  This will avoid the expensive operand
712*e4b17023SJohn Marinore-scan operation and the buffer comparison that determines if symbols
713*e4b17023SJohn Marinoneed to be marked for renaming.
714*e4b17023SJohn Marino
715*e4b17023SJohn Marino@subsection Examining @code{SSA_NAME} nodes
716*e4b17023SJohn Marino@cindex examining SSA_NAMEs
717*e4b17023SJohn Marino
718*e4b17023SJohn MarinoThe following macros can be used to examine @code{SSA_NAME} nodes
719*e4b17023SJohn Marino
720*e4b17023SJohn Marino@defmac SSA_NAME_DEF_STMT (@var{var})
721*e4b17023SJohn MarinoReturns the statement @var{s} that creates the @code{SSA_NAME}
722*e4b17023SJohn Marino@var{var}.  If @var{s} is an empty statement (i.e., @code{IS_EMPTY_STMT
723*e4b17023SJohn Marino(@var{s})} returns @code{true}), it means that the first reference to
724*e4b17023SJohn Marinothis variable is a USE or a VUSE@.
725*e4b17023SJohn Marino@end defmac
726*e4b17023SJohn Marino
727*e4b17023SJohn Marino@defmac SSA_NAME_VERSION (@var{var})
728*e4b17023SJohn MarinoReturns the version number of the @code{SSA_NAME} object @var{var}.
729*e4b17023SJohn Marino@end defmac
730*e4b17023SJohn Marino
731*e4b17023SJohn Marino
732*e4b17023SJohn Marino@subsection Walking use-def chains
733*e4b17023SJohn Marino
734*e4b17023SJohn Marino@deftypefn {Tree SSA function} void walk_use_def_chains (@var{var}, @var{fn}, @var{data})
735*e4b17023SJohn Marino
736*e4b17023SJohn MarinoWalks use-def chains starting at the @code{SSA_NAME} node @var{var}.
737*e4b17023SJohn MarinoCalls function @var{fn} at each reaching definition found.  Function
738*e4b17023SJohn Marino@var{FN} takes three arguments: @var{var}, its defining statement
739*e4b17023SJohn Marino(@var{def_stmt}) and a generic pointer to whatever state information
740*e4b17023SJohn Marinothat @var{fn} may want to maintain (@var{data}).  Function @var{fn} is
741*e4b17023SJohn Marinoable to stop the walk by returning @code{true}, otherwise in order to
742*e4b17023SJohn Marinocontinue the walk, @var{fn} should return @code{false}.
743*e4b17023SJohn Marino
744*e4b17023SJohn MarinoNote, that if @var{def_stmt} is a @code{PHI} node, the semantics are
745*e4b17023SJohn Marinoslightly different.  For each argument @var{arg} of the PHI node, this
746*e4b17023SJohn Marinofunction will:
747*e4b17023SJohn Marino
748*e4b17023SJohn Marino@enumerate
749*e4b17023SJohn Marino@item Walk the use-def chains for @var{arg}.
750*e4b17023SJohn Marino@item Call @code{FN (@var{arg}, @var{phi}, @var{data})}.
751*e4b17023SJohn Marino@end enumerate
752*e4b17023SJohn Marino
753*e4b17023SJohn MarinoNote how the first argument to @var{fn} is no longer the original
754*e4b17023SJohn Marinovariable @var{var}, but the PHI argument currently being examined.
755*e4b17023SJohn MarinoIf @var{fn} wants to get at @var{var}, it should call
756*e4b17023SJohn Marino@code{PHI_RESULT} (@var{phi}).
757*e4b17023SJohn Marino@end deftypefn
758*e4b17023SJohn Marino
759*e4b17023SJohn Marino@subsection Walking the dominator tree
760*e4b17023SJohn Marino
761*e4b17023SJohn Marino@deftypefn {Tree SSA function} void walk_dominator_tree (@var{walk_data}, @var{bb})
762*e4b17023SJohn Marino
763*e4b17023SJohn MarinoThis function walks the dominator tree for the current CFG calling a
764*e4b17023SJohn Marinoset of callback functions defined in @var{struct dom_walk_data} in
765*e4b17023SJohn Marino@file{domwalk.h}.  The call back functions you need to define give you
766*e4b17023SJohn Marinohooks to execute custom code at various points during traversal:
767*e4b17023SJohn Marino
768*e4b17023SJohn Marino@enumerate
769*e4b17023SJohn Marino@item Once to initialize any local data needed while processing
770*e4b17023SJohn Marino@var{bb} and its children.  This local data is pushed into an
771*e4b17023SJohn Marinointernal stack which is automatically pushed and popped as the
772*e4b17023SJohn Marinowalker traverses the dominator tree.
773*e4b17023SJohn Marino
774*e4b17023SJohn Marino@item Once before traversing all the statements in the @var{bb}.
775*e4b17023SJohn Marino
776*e4b17023SJohn Marino@item Once for every statement inside @var{bb}.
777*e4b17023SJohn Marino
778*e4b17023SJohn Marino@item Once after traversing all the statements and before recursing
779*e4b17023SJohn Marinointo @var{bb}'s dominator children.
780*e4b17023SJohn Marino
781*e4b17023SJohn Marino@item It then recurses into all the dominator children of @var{bb}.
782*e4b17023SJohn Marino
783*e4b17023SJohn Marino@item After recursing into all the dominator children of @var{bb} it
784*e4b17023SJohn Marinocan, optionally, traverse every statement in @var{bb} again
785*e4b17023SJohn Marino(i.e., repeating steps 2 and 3).
786*e4b17023SJohn Marino
787*e4b17023SJohn Marino@item Once after walking the statements in @var{bb} and @var{bb}'s
788*e4b17023SJohn Marinodominator children.  At this stage, the block local data stack
789*e4b17023SJohn Marinois popped.
790*e4b17023SJohn Marino@end enumerate
791*e4b17023SJohn Marino@end deftypefn
792*e4b17023SJohn Marino
793*e4b17023SJohn Marino@node Alias analysis
794*e4b17023SJohn Marino@section Alias analysis
795*e4b17023SJohn Marino@cindex alias
796*e4b17023SJohn Marino@cindex flow-sensitive alias analysis
797*e4b17023SJohn Marino@cindex flow-insensitive alias analysis
798*e4b17023SJohn Marino
799*e4b17023SJohn MarinoAlias analysis in GIMPLE SSA form consists of two pieces.  First
800*e4b17023SJohn Marinothe virtual SSA web ties conflicting memory accesses and provides
801*e4b17023SJohn Marinoa SSA use-def chain and SSA immediate-use chains for walking
802*e4b17023SJohn Marinopossibly dependent memory accesses.  Second an alias-oracle can
803*e4b17023SJohn Marinobe queried to disambiguate explicit and implicit memory references.
804*e4b17023SJohn Marino
805*e4b17023SJohn Marino@enumerate
806*e4b17023SJohn Marino@item Memory SSA form.
807*e4b17023SJohn Marino
808*e4b17023SJohn MarinoAll statements that may use memory have exactly one accompanied use of
809*e4b17023SJohn Marinoa virtual SSA name that represents the state of memory at the
810*e4b17023SJohn Marinogiven point in the IL.
811*e4b17023SJohn Marino
812*e4b17023SJohn MarinoAll statements that may define memory have exactly one accompanied
813*e4b17023SJohn Marinodefinition of a virtual SSA name using the previous state of memory
814*e4b17023SJohn Marinoand defining the new state of memory after the given point in the IL.
815*e4b17023SJohn Marino
816*e4b17023SJohn Marino@smallexample
817*e4b17023SJohn Marinoint i;
818*e4b17023SJohn Marinoint foo (void)
819*e4b17023SJohn Marino@{
820*e4b17023SJohn Marino  # .MEM_3 = VDEF <.MEM_2(D)>
821*e4b17023SJohn Marino  i = 1;
822*e4b17023SJohn Marino  # VUSE <.MEM_3>
823*e4b17023SJohn Marino  return i;
824*e4b17023SJohn Marino@}
825*e4b17023SJohn Marino@end smallexample
826*e4b17023SJohn Marino
827*e4b17023SJohn MarinoThe virtual SSA names in this case are @code{.MEM_2(D)} and
828*e4b17023SJohn Marino@code{.MEM_3}.  The store to the global variable @code{i}
829*e4b17023SJohn Marinodefines @code{.MEM_3} invalidating @code{.MEM_2(D)}.  The
830*e4b17023SJohn Marinoload from @code{i} uses that new state @code{.MEM_3}.
831*e4b17023SJohn Marino
832*e4b17023SJohn MarinoThe virtual SSA web serves as constraints to SSA optimizers
833*e4b17023SJohn Marinopreventing illegitimate code-motion and optimization.  It
834*e4b17023SJohn Marinoalso provides a way to walk related memory statements.
835*e4b17023SJohn Marino
836*e4b17023SJohn Marino@item Points-to and escape analysis.
837*e4b17023SJohn Marino
838*e4b17023SJohn MarinoPoints-to analysis builds a set of constraints from the GIMPLE
839*e4b17023SJohn MarinoSSA IL representing all pointer operations and facts we do
840*e4b17023SJohn Marinoor do not know about pointers.  Solving this set of constraints
841*e4b17023SJohn Marinoyields a conservatively correct solution for each pointer
842*e4b17023SJohn Marinovariable in the program (though we are only interested in
843*e4b17023SJohn MarinoSSA name pointers) as to what it may possibly point to.
844*e4b17023SJohn Marino
845*e4b17023SJohn MarinoThis points-to solution for a given SSA name pointer is stored
846*e4b17023SJohn Marinoin the @code{pt_solution} sub-structure of the
847*e4b17023SJohn Marino@code{SSA_NAME_PTR_INFO} record.  The following accessor
848*e4b17023SJohn Marinofunctions are available:
849*e4b17023SJohn Marino
850*e4b17023SJohn Marino@itemize @bullet
851*e4b17023SJohn Marino@item @code{pt_solution_includes}
852*e4b17023SJohn Marino@item @code{pt_solutions_intersect}
853*e4b17023SJohn Marino@end itemize
854*e4b17023SJohn Marino
855*e4b17023SJohn MarinoPoints-to analysis also computes the solution for two special
856*e4b17023SJohn Marinoset of pointers, @code{ESCAPED} and @code{CALLUSED}.  Those
857*e4b17023SJohn Marinorepresent all memory that has escaped the scope of analysis
858*e4b17023SJohn Marinoor that is used by pure or nested const calls.
859*e4b17023SJohn Marino
860*e4b17023SJohn Marino@item Type-based alias analysis
861*e4b17023SJohn Marino
862*e4b17023SJohn MarinoType-based alias analysis is frontend dependent though generic
863*e4b17023SJohn Marinosupport is provided by the middle-end in @code{alias.c}.  TBAA
864*e4b17023SJohn Marinocode is used by both tree optimizers and RTL optimizers.
865*e4b17023SJohn Marino
866*e4b17023SJohn MarinoEvery language that wishes to perform language-specific alias analysis
867*e4b17023SJohn Marinoshould define a function that computes, given a @code{tree}
868*e4b17023SJohn Marinonode, an alias set for the node.  Nodes in different alias sets are not
869*e4b17023SJohn Marinoallowed to alias.  For an example, see the C front-end function
870*e4b17023SJohn Marino@code{c_get_alias_set}.
871*e4b17023SJohn Marino
872*e4b17023SJohn Marino@item Tree alias-oracle
873*e4b17023SJohn Marino
874*e4b17023SJohn MarinoThe tree alias-oracle provides means to disambiguate two memory
875*e4b17023SJohn Marinoreferences and memory references against statements.  The following
876*e4b17023SJohn Marinoqueries are available:
877*e4b17023SJohn Marino
878*e4b17023SJohn Marino@itemize @bullet
879*e4b17023SJohn Marino@item @code{refs_may_alias_p}
880*e4b17023SJohn Marino@item @code{ref_maybe_used_by_stmt_p}
881*e4b17023SJohn Marino@item @code{stmt_may_clobber_ref_p}
882*e4b17023SJohn Marino@end itemize
883*e4b17023SJohn Marino
884*e4b17023SJohn MarinoIn addition to those two kind of statement walkers are available
885*e4b17023SJohn Marinowalking statements related to a reference ref.
886*e4b17023SJohn Marino@code{walk_non_aliased_vuses} walks over dominating memory defining
887*e4b17023SJohn Marinostatements and calls back if the statement does not clobber ref
888*e4b17023SJohn Marinoproviding the non-aliased VUSE.  The walk stops at
889*e4b17023SJohn Marinothe first clobbering statement or if asked to.
890*e4b17023SJohn Marino@code{walk_aliased_vdefs} walks over dominating memory defining
891*e4b17023SJohn Marinostatements and calls back on each statement clobbering ref
892*e4b17023SJohn Marinoproviding its aliasing VDEF.  The walk stops if asked to.
893*e4b17023SJohn Marino
894*e4b17023SJohn Marino@end enumerate
895*e4b17023SJohn Marino
896*e4b17023SJohn Marino
897*e4b17023SJohn Marino@node Memory model
898*e4b17023SJohn Marino@section Memory model
899*e4b17023SJohn Marino@cindex memory model
900*e4b17023SJohn Marino
901*e4b17023SJohn MarinoThe memory model used by the middle-end models that of the C/C++
902*e4b17023SJohn Marinolanguages.  The middle-end has the notion of an effective type
903*e4b17023SJohn Marinoof a memory region which is used for type-based alias analysis.
904*e4b17023SJohn Marino
905*e4b17023SJohn MarinoThe following is a refinement of ISO C99 6.5/6, clarifying the block copy case
906*e4b17023SJohn Marinoto follow common sense and extending the concept of a dynamic effective
907*e4b17023SJohn Marinotype to objects with a declared type as required for C++.
908*e4b17023SJohn Marino
909*e4b17023SJohn Marino@smallexample
910*e4b17023SJohn MarinoThe effective type of an object for an access to its stored value is
911*e4b17023SJohn Marinothe declared type of the object or the effective type determined by
912*e4b17023SJohn Marinoa previous store to it.  If a value is stored into an object through
913*e4b17023SJohn Marinoan lvalue having a type that is not a character type, then the
914*e4b17023SJohn Marinotype of the lvalue becomes the effective type of the object for that
915*e4b17023SJohn Marinoaccess and for subsequent accesses that do not modify the stored value.
916*e4b17023SJohn MarinoIf a value is copied into an object using @code{memcpy} or @code{memmove},
917*e4b17023SJohn Marinoor is copied as an array of character type, then the effective type
918*e4b17023SJohn Marinoof the modified object for that access and for subsequent accesses that
919*e4b17023SJohn Marinodo not modify the value is undetermined.  For all other accesses to an
920*e4b17023SJohn Marinoobject, the effective type of the object is simply the type of the
921*e4b17023SJohn Marinolvalue used for the access.
922*e4b17023SJohn Marino@end smallexample
923*e4b17023SJohn Marino
924