2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / doc / cfg.texi
blobb759e36ff75260edd5f7c2eefbf32aeb94c105c4
1 @c -*-texinfo-*-
2 @c Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @c ---------------------------------------------------------------------
7 @c Control Flow Graph
8 @c ---------------------------------------------------------------------
10 @node Control Flow
11 @chapter Control Flow Graph
12 @cindex CFG, Control Flow Graph
13 @findex basic-block.h
15 A control flow graph (CFG) is a data structure built on top of the
16 intermediate code representation (the RTL or @code{GIMPLE} instruction
17 stream) abstracting the control flow behavior of a function that is
18 being compiled.  The CFG is a directed graph where the vertices
19 represent basic blocks and edges represent possible transfer of
20 control flow from one basic block to another.  The data structures
21 used to represent the control flow graph are defined in
22 @file{basic-block.h}.
24 In GCC, the representation of control flow is maintained throughout
25 the compilation process, from constructing the CFG early in 
26 @code{pass_build_cfg} to @code{pass_free_cfg} (see @file{passes.c}).
27 The CFG takes various different modes and may undergo extensive
28 manipulations, but the graph is always valid between its construction
29 and its release.  This way, transfer of information such as data flow,
30 a measured profile, or the loop tree, can be propagated through the
31 passes pipeline, and even from @code{GIMPLE} to @code{RTL}.
33 Often the CFG may be better viewed as integral part of instruction
34 chain, than structure built on the top of it.  Updating the compiler's
35 intermediate representation for instructions can not be easily done
36 without proper maintenance of the CFG simultaneously.
38 @menu
39 * Basic Blocks::           The definition and representation of basic blocks.
40 * Edges::                  Types of edges and their representation.
41 * Profile information::    Representation of frequencies and probabilities.
42 * Maintaining the CFG::    Keeping the control flow graph and up to date.
43 * Liveness information::   Using and maintaining liveness information.
44 @end menu
47 @node Basic Blocks
48 @section Basic Blocks
50 @cindex basic block
51 @findex basic_block
52 A basic block is a straight-line sequence of code with only one entry
53 point and only one exit.  In GCC, basic blocks are represented using
54 the @code{basic_block} data type.
56 @findex ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR
57 Special basic blocks represent possible entry and exit points of a
58 function.  These blocks are called @code{ENTRY_BLOCK_PTR} and
59 @code{EXIT_BLOCK_PTR}.  These blocks do not contain any code.
61 @findex BASIC_BLOCK
62 The @code{BASIC_BLOCK} array contains all basic blocks in an
63 unspecified order.  Each @code{basic_block} structure has a field
64 that holds a unique integer identifier @code{index} that is the
65 index of the block in the @code{BASIC_BLOCK} array.
66 The total number of basic blocks in the function is
67 @code{n_basic_blocks}.  Both the basic block indices and
68 the total number of basic blocks may vary during the compilation
69 process, as passes reorder, create, duplicate, and destroy basic
70 blocks.  The index for any block should never be greater than
71 @code{last_basic_block}.  The indices 0 and 1 are special codes
72 reserved for @code{ENTRY_BLOCK} and @code{EXIT_BLOCK}, the
73 indices of @code{ENTRY_BLOCK_PTR} and @code{EXIT_BLOCK_PTR}.
75 @findex next_bb, prev_bb, FOR_EACH_BB, FOR_ALL_BB
76 Two pointer members of the @code{basic_block} structure are the
77 pointers @code{next_bb} and @code{prev_bb}.  These are used to keep
78 doubly linked chain of basic blocks in the same order as the
79 underlying instruction stream.  The chain of basic blocks is updated
80 transparently by the provided API for manipulating the CFG@.  The macro
81 @code{FOR_EACH_BB} can be used to visit all the basic blocks in
82 lexicographical order, except @code{ENTRY_BLOCK} and @code{EXIT_BLOCK}.
83 The macro @code{FOR_ALL_BB} also visits all basic blocks in
84 lexicographical order, including @code{ENTRY_BLOCK} and @code{EXIT_BLOCK}.
86 @findex post_order_compute, inverted_post_order_compute, walk_dominator_tree
87 The functions @code{post_order_compute} and @code{inverted_post_order_compute}
88 can be used to compute topological orders of the CFG.  The orders are
89 stored as vectors of basic block indices.  The @code{BASIC_BLOCK} array
90 can be used to iterate each basic block by index.
91 Dominator traversals are also possible using
92 @code{walk_dominator_tree}.  Given two basic blocks A and B, block A
93 dominates block B if A is @emph{always} executed before B@.
95 Each @code{basic_block} also contains pointers to the first
96 instruction (the @dfn{head}) and the last instruction (the @dfn{tail})
97 or @dfn{end} of the instruction stream contained in a basic block.  In
98 fact, since the @code{basic_block} data type is used to represent
99 blocks in both major intermediate representations of GCC (@code{GIMPLE}
100 and RTL), there are pointers to the head and end of a basic block for
101 both representations, stored in intermediate representation specific
102 data in the @code{il} field of @code{struct basic_block_def}.
104 @findex CODE_LABEL
105 @findex NOTE_INSN_BASIC_BLOCK
106 For RTL, these pointers are @code{BB_HEAD} and @code{BB_END}.
108 @cindex insn notes, notes
109 @findex NOTE_INSN_BASIC_BLOCK
110 In the RTL representation of a function, the instruction stream
111 contains not only the ``real'' instructions, but also @dfn{notes}
112 or @dfn{insn notes} (to distinguish them from @dfn{reg notes}).
113 Any function that moves or duplicates the basic blocks needs
114 to take care of updating of these notes.  Many of these notes expect
115 that the instruction stream consists of linear regions, so updating
116 can sometimes be tedious.  All types of insn notes are defined
117 in @file{insn-notes.def}.
119 In the RTL function representation, the instructions contained in a
120 basic block always follow a @code{NOTE_INSN_BASIC_BLOCK}, but zero
121 or more @code{CODE_LABEL} nodes can precede the block note.
122 A basic block ends with a control flow instruction or with the last
123 instruction before the next @code{CODE_LABEL} or
124 @code{NOTE_INSN_BASIC_BLOCK}.
125 By definition, a @code{CODE_LABEL} cannot appear in the middle of
126 the instruction stream of a basic block.
128 @findex can_fallthru
129 @cindex table jump
130 In addition to notes, the jump table vectors are also represented as
131 ``pseudo-instructions'' inside the insn stream.  These vectors never
132 appear in the basic block and should always be placed just after the
133 table jump instructions referencing them.  After removing the
134 table-jump it is often difficult to eliminate the code computing the
135 address and referencing the vector, so cleaning up these vectors is
136 postponed until after liveness analysis.   Thus the jump table vectors
137 may appear in the insn stream unreferenced and without any purpose.
138 Before any edge is made @dfn{fall-thru}, the existence of such
139 construct in the way needs to be checked by calling
140 @code{can_fallthru} function.
142 @cindex GIMPLE statement iterators
143 For the @code{GIMPLE} representation, the PHI nodes and statements
144 contained in a basic block are in a @code{gimple_seq} pointed to by
145 the basic block intermediate language specific pointers.
146 Abstract containers and iterators are used to access the PHI nodes
147 and statements in a basic blocks.  These iterators are called
148 @dfn{GIMPLE statement iterators} (GSIs).  Grep for @code{^gsi}
149 in the various @file{gimple-*} and @file{tree-*} files.
150 The following snippet will pretty-print all PHI nodes the statements
151 of the current function in the GIMPLE representation.
153 @smallexample
154 basic_block bb;
156 FOR_EACH_BB (bb)
157   @{
158    gimple_stmt_iterator si;
160    for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
161      @{
162        gimple phi = gsi_stmt (si);
163        print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
164      @}
165    for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
166      @{
167        gimple stmt = gsi_stmt (si);
168        print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
169      @}
170   @}
171 @end smallexample
174 @node Edges
175 @section Edges
177 @cindex edge in the flow graph
178 @findex edge
179 Edges represent possible control flow transfers from the end of some
180 basic block A to the head of another basic block B@.  We say that A is
181 a predecessor of B, and B is a successor of A@.  Edges are represented
182 in GCC with the @code{edge} data type.  Each @code{edge} acts as a
183 link between two basic blocks: The @code{src} member of an edge
184 points to the predecessor basic block of the @code{dest} basic block.
185 The members @code{preds} and @code{succs} of the @code{basic_block} data
186 type point to type-safe vectors of edges to the predecessors and
187 successors of the block.
189 @cindex edge iterators
190 When walking the edges in an edge vector, @dfn{edge iterators} should
191 be used.  Edge iterators are constructed using the
192 @code{edge_iterator} data structure and several methods are available
193 to operate on them:
195 @ftable @code
196 @item ei_start
197 This function initializes an @code{edge_iterator} that points to the
198 first edge in a vector of edges.
200 @item ei_last
201 This function initializes an @code{edge_iterator} that points to the
202 last edge in a vector of edges.
204 @item ei_end_p
205 This predicate is @code{true} if an @code{edge_iterator} represents
206 the last edge in an edge vector.
208 @item ei_one_before_end_p
209 This predicate is @code{true} if an @code{edge_iterator} represents
210 the second last edge in an edge vector.
212 @item ei_next
213 This function takes a pointer to an @code{edge_iterator} and makes it
214 point to the next edge in the sequence.
216 @item ei_prev
217 This function takes a pointer to an @code{edge_iterator} and makes it
218 point to the previous edge in the sequence.
220 @item ei_edge
221 This function returns the @code{edge} currently pointed to by an
222 @code{edge_iterator}.
224 @item ei_safe_safe
225 This function returns the @code{edge} currently pointed to by an
226 @code{edge_iterator}, but returns @code{NULL} if the iterator is
227 pointing at the end of the sequence.  This function has been provided
228 for existing code makes the assumption that a @code{NULL} edge
229 indicates the end of the sequence.
231 @end ftable
233 The convenience macro @code{FOR_EACH_EDGE} can be used to visit all of
234 the edges in a sequence of predecessor or successor edges.  It must
235 not be used when an element might be removed during the traversal,
236 otherwise elements will be missed.  Here is an example of how to use
237 the macro:
239 @smallexample
240 edge e;
241 edge_iterator ei;
243 FOR_EACH_EDGE (e, ei, bb->succs)
244   @{
245      if (e->flags & EDGE_FALLTHRU)
246        break;
247   @}
248 @end smallexample
250 @findex fall-thru
251 There are various reasons why control flow may transfer from one block
252 to another.  One possibility is that some instruction, for example a
253 @code{CODE_LABEL}, in a linearized instruction stream just always
254 starts a new basic block.  In this case a @dfn{fall-thru} edge links
255 the basic block to the first following basic block.  But there are
256 several other reasons why edges may be created.  The @code{flags}
257 field of the @code{edge} data type is used to store information
258 about the type of edge we are dealing with.  Each edge is of one of
259 the following types:
261 @table @emph
262 @item jump
263 No type flags are set for edges corresponding to jump instructions.
264 These edges are used for unconditional or conditional jumps and in
265 RTL also for table jumps.  They are the easiest to manipulate as they
266 may be freely redirected when the flow graph is not in SSA form.
268 @item fall-thru
269 @findex EDGE_FALLTHRU, force_nonfallthru
270 Fall-thru edges are present in case where the basic block may continue
271 execution to the following one without branching.  These edges have
272 the @code{EDGE_FALLTHRU} flag set.  Unlike other types of edges, these
273 edges must come into the basic block immediately following in the
274 instruction stream.  The function @code{force_nonfallthru} is
275 available to insert an unconditional jump in the case that redirection
276 is needed.  Note that this may require creation of a new basic block.
278 @item exception handling
279 @cindex exception handling
280 @findex EDGE_ABNORMAL, EDGE_EH
281 Exception handling edges represent possible control transfers from a
282 trapping instruction to an exception handler.  The definition of
283 ``trapping'' varies.  In C++, only function calls can throw, but for
284 Java and Ada, exceptions like division by zero or segmentation fault are
285 defined and thus each instruction possibly throwing this kind of
286 exception needs to be handled as control flow instruction.  Exception
287 edges have the @code{EDGE_ABNORMAL} and @code{EDGE_EH} flags set.
289 @findex purge_dead_edges
290 When updating the instruction stream it is easy to change possibly
291 trapping instruction to non-trapping, by simply removing the exception
292 edge.  The opposite conversion is difficult, but should not happen
293 anyway.  The edges can be eliminated via @code{purge_dead_edges} call.
295 @findex REG_EH_REGION, EDGE_ABNORMAL_CALL
296 In the RTL representation, the destination of an exception edge is
297 specified by @code{REG_EH_REGION} note attached to the insn.
298 In case of a trapping call the @code{EDGE_ABNORMAL_CALL} flag is set
299 too.  In the @code{GIMPLE} representation, this extra flag is not set.
301 @findex may_trap_p, tree_could_trap_p
302 In the RTL representation, the predicate @code{may_trap_p} may be used
303 to check whether instruction still may trap or not.  For the tree
304 representation, the @code{tree_could_trap_p} predicate is available,
305 but this predicate only checks for possible memory traps, as in
306 dereferencing an invalid pointer location.
309 @item sibling calls
310 @cindex sibling call
311 @findex EDGE_ABNORMAL, EDGE_SIBCALL
312 Sibling calls or tail calls terminate the function in a non-standard
313 way and thus an edge to the exit must be present.
314 @code{EDGE_SIBCALL} and @code{EDGE_ABNORMAL} are set in such case.
315 These edges only exist in the RTL representation.
317 @item computed jumps
318 @cindex computed jump
319 @findex EDGE_ABNORMAL
320 Computed jumps contain edges to all labels in the function referenced
321 from the code.  All those edges have @code{EDGE_ABNORMAL} flag set.
322 The edges used to represent computed jumps often cause compile time
323 performance problems, since functions consisting of many taken labels
324 and many computed jumps may have @emph{very} dense flow graphs, so
325 these edges need to be handled with special care.  During the earlier
326 stages of the compilation process, GCC tries to avoid such dense flow
327 graphs by factoring computed jumps.  For example, given the following
328 series of jumps,
330 @smallexample
331   goto *x;
332   [ @dots{} ]
334   goto *x;
335   [ @dots{} ]
337   goto *x;
338   [ @dots{} ]
339 @end smallexample
341 @noindent
342 factoring the computed jumps results in the following code sequence
343 which has a much simpler flow graph:
345 @smallexample
346   goto y;
347   [ @dots{} ]
349   goto y;
350   [ @dots{} ]
352   goto y;
353   [ @dots{} ]
356   goto *x;
357 @end smallexample
359 @findex pass_duplicate_computed_gotos
360 However, the classic problem with this transformation is that it has a
361 runtime cost in there resulting code: An extra jump.  Therefore, the
362 computed jumps are un-factored in the later passes of the compiler
363 (in the pass called @code{pass_duplicate_computed_gotos}).
364 Be aware of that when you work on passes in that area.  There have
365 been numerous examples already where the compile time for code with
366 unfactored computed jumps caused some serious headaches.
368 @item nonlocal goto handlers
369 @cindex nonlocal goto handler
370 @findex EDGE_ABNORMAL, EDGE_ABNORMAL_CALL
371 GCC allows nested functions to return into caller using a @code{goto}
372 to a label passed to as an argument to the callee.  The labels passed
373 to nested functions contain special code to cleanup after function
374 call.  Such sections of code are referred to as ``nonlocal goto
375 receivers''.  If a function contains such nonlocal goto receivers, an
376 edge from the call to the label is created with the
377 @code{EDGE_ABNORMAL} and @code{EDGE_ABNORMAL_CALL} flags set.
379 @item function entry points
380 @cindex function entry point, alternate function entry point
381 @findex LABEL_ALTERNATE_NAME
382 By definition, execution of function starts at basic block 0, so there
383 is always an edge from the @code{ENTRY_BLOCK_PTR} to basic block 0.
384 There is no @code{GIMPLE} representation for alternate entry points at
385 this moment.  In RTL, alternate entry points are specified by
386 @code{CODE_LABEL} with @code{LABEL_ALTERNATE_NAME} defined.  This
387 feature is currently used for multiple entry point prologues and is
388 limited to post-reload passes only.  This can be used by back-ends to
389 emit alternate prologues for functions called from different contexts.
390 In future full support for multiple entry functions defined by Fortran
391 90 needs to be implemented.
393 @item function exits
394 In the pre-reload representation a function terminates after the last
395 instruction in the insn chain and no explicit return instructions are
396 used.  This corresponds to the fall-thru edge into exit block.  After
397 reload, optimal RTL epilogues are used that use explicit (conditional)
398 return instructions that are represented by edges with no flags set.
400 @end table
403 @node Profile information
404 @section Profile information
406 @cindex profile representation
407 In many cases a compiler must make a choice whether to trade speed in
408 one part of code for speed in another, or to trade code size for code
409 speed.  In such cases it is useful to know information about how often
410 some given block will be executed.  That is the purpose for
411 maintaining profile within the flow graph.
412 GCC can handle profile information obtained through @dfn{profile
413 feedback}, but it can also estimate branch probabilities based on
414 statics and heuristics.
416 @cindex profile feedback
417 The feedback based profile is produced by compiling the program with
418 instrumentation, executing it on a train run and reading the numbers
419 of executions of basic blocks and edges back to the compiler while
420 re-compiling the program to produce the final executable.  This method
421 provides very accurate information about where a program spends most
422 of its time on the train run.  Whether it matches the average run of
423 course depends on the choice of train data set, but several studies
424 have shown that the behavior of a program usually changes just
425 marginally over different data sets.
427 @cindex Static profile estimation
428 @cindex branch prediction
429 @findex predict.def
430 When profile feedback is not available, the compiler may be asked to
431 attempt to predict the behavior of each branch in the program using a
432 set of heuristics (see @file{predict.def} for details) and compute
433 estimated frequencies of each basic block by propagating the
434 probabilities over the graph.
436 @findex frequency, count, BB_FREQ_BASE
437 Each @code{basic_block} contains two integer fields to represent
438 profile information: @code{frequency} and @code{count}.  The
439 @code{frequency} is an estimation how often is basic block executed
440 within a function.  It is represented as an integer scaled in the
441 range from 0 to @code{BB_FREQ_BASE}.  The most frequently executed
442 basic block in function is initially set to @code{BB_FREQ_BASE} and
443 the rest of frequencies are scaled accordingly.  During optimization,
444 the frequency of the most frequent basic block can both decrease (for
445 instance by loop unrolling) or grow (for instance by cross-jumping
446 optimization), so scaling sometimes has to be performed multiple
447 times.
449 @findex gcov_type
450 The @code{count} contains hard-counted numbers of execution measured
451 during training runs and is nonzero only when profile feedback is
452 available.  This value is represented as the host's widest integer
453 (typically a 64 bit integer) of the special type @code{gcov_type}.
455 Most optimization passes can use only the frequency information of a
456 basic block, but a few passes may want to know hard execution counts.
457 The frequencies should always match the counts after scaling, however
458 during updating of the profile information numerical error may
459 accumulate into quite large errors.
461 @findex REG_BR_PROB_BASE, EDGE_FREQUENCY
462 Each edge also contains a branch probability field: an integer in the
463 range from 0 to @code{REG_BR_PROB_BASE}.  It represents probability of
464 passing control from the end of the @code{src} basic block to the
465 @code{dest} basic block, i.e.@: the probability that control will flow
466 along this edge.  The @code{EDGE_FREQUENCY} macro is available to
467 compute how frequently a given edge is taken.  There is a @code{count}
468 field for each edge as well, representing same information as for a
469 basic block.
471 The basic block frequencies are not represented in the instruction
472 stream, but in the RTL representation the edge frequencies are
473 represented for conditional jumps (via the @code{REG_BR_PROB}
474 macro) since they are used when instructions are output to the
475 assembly file and the flow graph is no longer maintained.
477 @cindex reverse probability
478 The probability that control flow arrives via a given edge to its
479 destination basic block is called @dfn{reverse probability} and is not
480 directly represented, but it may be easily computed from frequencies
481 of basic blocks.
483 @findex redirect_edge_and_branch
484 Updating profile information is a delicate task that can unfortunately
485 not be easily integrated with the CFG manipulation API@.  Many of the
486 functions and hooks to modify the CFG, such as
487 @code{redirect_edge_and_branch}, do not have enough information to
488 easily update the profile, so updating it is in the majority of cases
489 left up to the caller.  It is difficult to uncover bugs in the profile
490 updating code, because they manifest themselves only by producing
491 worse code, and checking profile consistency is not possible because
492 of numeric error accumulation.  Hence special attention needs to be
493 given to this issue in each pass that modifies the CFG@.
495 @findex REG_BR_PROB_BASE, BB_FREQ_BASE, count
496 It is important to point out that @code{REG_BR_PROB_BASE} and
497 @code{BB_FREQ_BASE} are both set low enough to be possible to compute
498 second power of any frequency or probability in the flow graph, it is
499 not possible to even square the @code{count} field, as modern CPUs are
500 fast enough to execute $2^32$ operations quickly.
503 @node Maintaining the CFG
504 @section Maintaining the CFG
505 @findex cfghooks.h
507 An important task of each compiler pass is to keep both the control
508 flow graph and all profile information up-to-date.  Reconstruction of
509 the control flow graph after each pass is not an option, since it may be
510 very expensive and lost profile information cannot be reconstructed at
511 all.
513 GCC has two major intermediate representations, and both use the
514 @code{basic_block} and @code{edge} data types to represent control
515 flow.  Both representations share as much of the CFG maintenance code
516 as possible.  For each representation, a set of @dfn{hooks} is defined
517 so that each representation can provide its own implementation of CFG
518 manipulation routines when necessary.  These hooks are defined in
519 @file{cfghooks.h}.  There are hooks for almost all common CFG
520 manipulations, including block splitting and merging, edge redirection
521 and creating and deleting basic blocks.  These hooks should provide
522 everything you need to maintain and manipulate the CFG in both the RTL
523 and @code{GIMPLE} representation.
525 At the moment, the basic block boundaries are maintained transparently
526 when modifying instructions, so there rarely is a need to move them
527 manually (such as in case someone wants to output instruction outside
528 basic block explicitly).
530 @findex BLOCK_FOR_INSN, gimple_bb
531 In the RTL representation, each instruction has a
532 @code{BLOCK_FOR_INSN} value that represents pointer to the basic block
533 that contains the instruction.  In the @code{GIMPLE} representation, the
534 function @code{gimple_bb} returns a pointer to the basic block
535 containing the queried statement.
537 @cindex GIMPLE statement iterators
538 When changes need to be applied to a function in its @code{GIMPLE}
539 representation, @dfn{GIMPLE statement iterators} should be used.  These
540 iterators provide an integrated abstraction of the flow graph and the
541 instruction stream.  Block statement iterators are constructed using
542 the @code{gimple_stmt_iterator} data structure and several modifier are
543 available, including the following:
545 @ftable @code
546 @item gsi_start
547 This function initializes a @code{gimple_stmt_iterator} that points to
548 the first non-empty statement in a basic block.
550 @item gsi_last
551 This function initializes a @code{gimple_stmt_iterator} that points to
552 the last statement in a basic block.
554 @item gsi_end_p
555 This predicate is @code{true} if a @code{gimple_stmt_iterator}
556 represents the end of a basic block.
558 @item gsi_next
559 This function takes a @code{gimple_stmt_iterator} and makes it point to
560 its successor.
562 @item gsi_prev
563 This function takes a @code{gimple_stmt_iterator} and makes it point to
564 its predecessor.
566 @item gsi_insert_after
567 This function inserts a statement after the @code{gimple_stmt_iterator}
568 passed in.  The final parameter determines whether the statement
569 iterator is updated to point to the newly inserted statement, or left
570 pointing to the original statement.
572 @item gsi_insert_before
573 This function inserts a statement before the @code{gimple_stmt_iterator}
574 passed in.  The final parameter determines whether the statement
575 iterator is updated to point to the newly inserted statement, or left
576 pointing to the original  statement.
578 @item gsi_remove
579 This function removes the @code{gimple_stmt_iterator} passed in and
580 rechains the remaining statements in a basic block, if any.
581 @end ftable
583 @findex BB_HEAD, BB_END
584 In the RTL representation, the macros @code{BB_HEAD} and @code{BB_END}
585 may be used to get the head and end @code{rtx} of a basic block.  No
586 abstract iterators are defined for traversing the insn chain, but you
587 can just use @code{NEXT_INSN} and @code{PREV_INSN} instead.  @xref{Insns}.
589 @findex purge_dead_edges
590 Usually a code manipulating pass simplifies the instruction stream and
591 the flow of control, possibly eliminating some edges.  This may for
592 example happen when a conditional jump is replaced with an
593 unconditional jump, but also when simplifying possibly trapping
594 instruction to non-trapping while compiling Java.  Updating of edges
595 is not transparent and each optimization pass is required to do so
596 manually.  However only few cases occur in practice.  The pass may
597 call @code{purge_dead_edges} on a given basic block to remove
598 superfluous edges, if any.
600 @findex redirect_edge_and_branch, redirect_jump
601 Another common scenario is redirection of branch instructions, but
602 this is best modeled as redirection of edges in the control flow graph
603 and thus use of @code{redirect_edge_and_branch} is preferred over more
604 low level functions, such as @code{redirect_jump} that operate on RTL
605 chain only.  The CFG hooks defined in @file{cfghooks.h} should provide
606 the complete API required for manipulating and maintaining the CFG@.
608 @findex split_block
609 It is also possible that a pass has to insert control flow instruction
610 into the middle of a basic block, thus creating an entry point in the
611 middle of the basic block, which is impossible by definition: The
612 block must be split to make sure it only has one entry point, i.e.@: the
613 head of the basic block.  The CFG hook @code{split_block} may be used
614 when an instruction in the middle of a basic block has to become the
615 target of a jump or branch instruction.
617 @findex insert_insn_on_edge
618 @findex commit_edge_insertions
619 @findex gsi_insert_on_edge
620 @findex gsi_commit_edge_inserts
621 @cindex edge splitting
622 For a global optimizer, a common operation is to split edges in the
623 flow graph and insert instructions on them.  In the RTL
624 representation, this can be easily done using the
625 @code{insert_insn_on_edge} function that emits an instruction
626 ``on the edge'', caching it for a later @code{commit_edge_insertions}
627 call that will take care of moving the inserted instructions off the
628 edge into the instruction stream contained in a basic block.  This
629 includes the creation of new basic blocks where needed.  In the
630 @code{GIMPLE} representation, the equivalent functions are
631 @code{gsi_insert_on_edge} which inserts a block statement
632 iterator on an edge, and @code{gsi_commit_edge_inserts} which flushes
633 the instruction to actual instruction stream.
635 @findex verify_flow_info
636 @cindex CFG verification
637 While debugging the optimization pass, the @code{verify_flow_info}
638 function may be useful to find bugs in the control flow graph updating
639 code.
642 @node Liveness information
643 @section Liveness information
644 @cindex Liveness representation
645 Liveness information is useful to determine whether some register is
646 ``live'' at given point of program, i.e.@: that it contains a value that
647 may be used at a later point in the program.  This information is
648 used, for instance, during register allocation, as the pseudo
649 registers only need to be assigned to a unique hard register or to a
650 stack slot if they are live.  The hard registers and stack slots may
651 be freely reused for other values when a register is dead.
653 Liveness information is available in the back end starting with
654 @code{pass_df_initialize} and ending with @code{pass_df_finish}.  Three
655 flavors of live analysis are available: With @code{LR}, it is possible
656 to determine at any point @code{P} in the function if the register may be
657 used on some path from @code{P} to the end of the function.  With
658 @code{UR}, it is possible to determine if there is a path from the
659 beginning of the function to @code{P} that defines the variable.
660 @code{LIVE} is the intersection of the @code{LR} and @code{UR} and a
661 variable is live at @code{P} if there is both an assignment that reaches
662 it from the beginning of the function and a use that can be reached on
663 some path from @code{P} to the end of the function.
665 In general @code{LIVE} is the most useful of the three.  The macros
666 @code{DF_[LR,UR,LIVE]_[IN,OUT]} can be used to access this information.
667 The macros take a basic block number and return a bitmap that is indexed
668 by the register number.  This information is only guaranteed to be up to
669 date after calls are made to @code{df_analyze}.  See the file
670 @code{df-core.c} for details on using the dataflow.
673 @findex REG_DEAD, REG_UNUSED
674 The liveness information is stored partly in the RTL instruction stream
675 and partly in the flow graph.  Local information is stored in the
676 instruction stream: Each instruction may contain @code{REG_DEAD} notes
677 representing that the value of a given register is no longer needed, or
678 @code{REG_UNUSED} notes representing that the value computed by the
679 instruction is never used.  The second is useful for instructions
680 computing multiple values at once.