OSDN Git Service

* doc/install.texi: Document that dejagnu 1.4.4 is required.
[pf3gnuchains/gcc-fork.git] / gcc / doc / cfg.texi
1 @c -*-texinfo-*-
2 @c Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
5
6 @c ---------------------------------------------------------------------
7 @c Control Flow Graph
8 @c ---------------------------------------------------------------------
9
10 @node Control Flow
11 @chapter Control Flow Graph
12 @cindex CFG, Control Flow Graph
13 @findex basic-block.h
14
15 A control flow graph (CFG) is a data structure built on top of the
16 intermediate code representation (the RTL or @code{tree} 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}.
23
24 @menu
25 * Basic Blocks::           The definition and representation of basic blocks.
26 * Edges::                  Types of edges and their representation.
27 * Profile information::    Representation of frequencies and probabilities.
28 * Maintaining the CFG::    Keeping the control flow graph and up to date.
29 * Liveness information::   Using and maintaining liveness information.
30 @end menu
31
32
33 @node Basic Blocks
34 @section Basic Blocks
35
36 @cindex basic block
37 @findex basic_block
38 A basic block is a straight-line sequence of code with only one entry
39 point and only one exit.  In GCC, basic blocks are represented using
40 the @code{basic_block} data type.
41
42 @findex next_bb, prev_bb, FOR_EACH_BB
43 Two pointer members of the @code{basic_block} structure are the
44 pointers @code{next_bb} and @code{prev_bb}.  These are used to keep
45 doubly linked chain of basic blocks in the same order as the
46 underlying instruction stream.  The chain of basic blocks is updated
47 transparently by the provided API for manipulating the CFG.  The macro
48 @code{FOR_EACH_BB} can be used to visit all the basic blocks in
49 lexicographical order.  Dominator traversals are also possible using
50 @code{walk_dominator_tree}.
51
52 @findex BASIC_BLOCK
53 The @code{BASIC_BLOCK} array contains all basic blocks in an
54 unspecified order.  Each @code{basic_block} structure has a field
55 that holds a unique integer identifier @code{index} that is the
56 index of the block in the @code{BASIC_BLOCK} array.
57 The total number of basic blocks in the function is
58 @code{n_basic_blocks}.  Both the basic block indices and
59 the total number of basic blocks may vary during the compilation
60 process, as passes reorder, create, duplicate, and destroy basic
61 blocks.  The index for any block should never be greater than
62 @code{last_basic_block}.
63
64 @findex ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR
65 Special basic blocks represent possible entry and exit points of a
66 function.  These blocks are called @code{ENTRY_BLOCK_PTR} and
67 @code{EXIT_BLOCK_PTR}.  These blocks do not contain any code, and are
68 not elements of the @code{BASIC_BLOCK} array.  Therefore they have
69 been assigned unique, negative index numbers.
70
71 Each @code{basic_block} also contains pointers to the first
72 instruction (the @dfn{head}) and the last instruction (the @dfn{tail})
73 or @dfn{end} of the instruction stream contained in a basic block.  In
74 fact, since the @code{basic_block} data type is used to represent
75 blocks in both major intermediate representations of GCC (@code{tree}
76 and RTL), there are pointers to the head and end of a basic block for
77 both representations.
78
79 @findex NOTE_INSN_BASIC_BLOCK, CODE_LABEL, notes
80 For RTL, these pointers are @code{rtx head, end}.  In the RTL function
81 representation, the head pointer always points either to a
82 @code{NOTE_INSN_BASIC_BLOCK} or to a @code{CODE_LABEL}, if present.
83 In the RTL representation of a function, the instruction stream
84 contains not only the ``real'' instructions, but also @dfn{notes}.
85 Any function that moves or duplicates the basic blocks needs
86 to take care of updating of these notes.  Many of these notes expect
87 that the instruction stream consists of linear regions, making such
88 updates difficult.   The @code{NOTE_INSN_BASIC_BLOCK} note is the only
89 kind of note that may appear in the instruction stream contained in a
90 basic block.  The instruction stream of a basic block always follows a
91 @code{NOTE_INSN_BASIC_BLOCK},  but zero or more @code{CODE_LABEL}
92 nodes can precede the block note.   A basic block ends by control flow
93 instruction or last instruction before following @code{CODE_LABEL} or
94 @code{NOTE_INSN_BASIC_BLOCK}.  A @code{CODE_LABEL} cannot appear in
95 the instruction stream of a basic block.
96
97 @findex can_fallthru
98 @cindex table jump
99 In addition to notes, the jump table vectors are also represented as
100 ``pseudo-instructions'' inside the insn stream.  These vectors never
101 appear in the basic block and should always be placed just after the
102 table jump instructions referencing them.  After removing the
103 table-jump it is often difficult to eliminate the code computing the
104 address and referencing the vector, so cleaning up these vectors is
105 postponed until after liveness analysis.   Thus the jump table vectors
106 may appear in the insn stream unreferenced and without any purpose.
107 Before any edge is made @dfn{fall-thru}, the existence of such
108 construct in the way needs to be checked by calling
109 @code{can_fallthru} function.
110
111 @cindex block statement iterators
112 For the @code{tree} representation, the head and end of the basic block
113 are being pointed to by the @code{stmt_list} field, but this special
114 @code{tree} should never be referenced directly.  Instead, at the tree
115 level abstract containers and iterators are used to access statements
116 and expressions in basic blocks.  These iterators are called
117 @dfn{block statement iterators} (BSIs).  Grep for @code{^bsi}
118 in the various @file{tree-*} files.
119 The following snippet will pretty-print all the statements of the
120 program in the GIMPLE representation.
121
122 @example
123 FOR_EACH_BB (bb)
124   @{
125      block_stmt_iterator si;
126
127      for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
128        @{
129           tree stmt = bsi_stmt (si);
130           print_generic_stmt (stderr, stmt, 0);
131        @}
132   @}
133 @end example
134
135
136 @node Edges
137 @section Edges
138
139 @cindex edge in the flow graph
140 @findex edge
141 Edges represent possible control flow transfers from the end of some
142 basic block A to the head of another basic block B.  We say that A is
143 a predecessor of B, and B is a successor of A.  Edges are represented
144 in GCC with the @code{edge} data type.  Each @code{edge} acts as a
145 link between two basic blocks: the @code{src} member of an edge
146 points to the predecessor basic block of the @code{dest} basic block.
147 The members @code{pred} and @code{succ} of the @code{basic_block} data
148 type point to single linked lists of edges to the predecessors and
149 successorts of the block.  The edges are linked via the
150 @code{succ_next} and @code{pred_next} members of the @code{edge} data
151 type.
152
153 @findex fall-thru
154 There are various reasons why control flow may transfer from one block
155 to another.  One possibility is that some instruction, for example a
156 @code{CODE_LABEL}, in a linearized instruction stream just always
157 starts a new basic block.  In this case a @dfn{fall-thru} edge links
158 the basic block to the first following basic block.  But there are
159 several other reasons why edges may be created.  The @code{flags}
160 field of the @code{edge} data type is used to store information
161 about the type of edge we are dealing with.  Each edge is of one of
162 the following types:
163
164 @table @emph
165 @item jump
166 No type flags are set for edges corresponding to jump instructions.
167 These edges are used for unconditional or conditional jumps and in
168 RTL also for table jumps.  They are the easiest to manipulate as they
169 may be freely redirected when the flow graph is not in SSA form.
170
171 @item fall-thru
172 @findex EDGE_FALLTHRU, force_nonfallthru
173 Fall-thru edges are present in case where the basic block may continue
174 execution to the following one without branching.  These edges have
175 the @code{EDGE_FALLTHRU} flag set. Unlike other types of edges, these
176 edges must come into the basic block immediately following in the
177 instruction stream.  The function @code{force_nonfallthru} is
178 available to insert an unconditional jump in the case that redirection
179 is needed.  Note that this may require creation of a new basic block.
180
181 @item exception handling
182 @cindex exception handling
183 @findex EDGE_ABNORMAL, EDGE_EH
184 Exception handling edges represent possible control transfers from a
185 trapping instruction to an exception handler.  The definition of
186 ``trapping'' varies.  In C++, only function calls can throw, but for
187 Java, exceptions like division by zero or segmentation fault are
188 defined and thus each instruction possibly throwing this kind of
189 exception needs to be handled as control flow instruction.  Exception
190 edges have the @code{EDGE_ABNORMAL} and @code{EDGE_EH} flags set.
191
192 @findex purge_dead_edges
193 When updating the instruction stream it is easy to change possibly
194 trapping instruction to non-trapping, by simply removing the exception
195 edge. The opposite conversion is difficult, but should not happen
196 anyway.  The edges can be eliminated via @code{purge_dead_edges} call.
197
198 @findex REG_EH_REGION, EDGE_ABNORMAL_CALL
199 In the RTL representation, the destination of an exception edge is
200 specified by @code{REG_EH_REGION} note attached to the insn.
201 In case of a trapping call the @code{EDGE_ABNORMAL_CALL} flag is set
202 too.  In the @code{tree} representation, this extra flag is not set.
203
204 @findex may_trap_p, tree_could_trap_p
205 In the RTL representation, the predicate @code{may_trap_p} may be used
206 to check whether instruction still may trap or not.  For the tree
207 representation, the @code{tree_could_trap_p} predicate is available,
208 but this predicate only checks for possible memory traps, as in
209 dereferencing an invalid pointer location.
210
211
212 @item sibling calls
213 @cindex sibling call
214 @findex EDGE_ABNORMAL, EDGE_SIBCALL
215 Sibling calls or tail calls terminate the function in a non-standard
216 way and thus an edge to the exit must be present.
217 @code{EDGE_SIBCALL} and @code{EDGE_ABNORMAL} are set in such case.
218 These edges only exist in the RTL representation.
219
220 @item computed jumps
221 @cindex computed jump
222 @findex EDGE_ABNORMAL
223 Computed jumps contain edges to all labels in the function referenced
224 from the code.  All those edges have @code{EDGE_ABNORMAL} flag set.
225 The edges used to represent computed jumps often cause compile time
226 performance problems, since functions consisting of many taken labels
227 and many computed jumps may have @emph{very} dense flow graphs, so
228 these edges need to be handled with special care.  During the earlier
229 stages of the compilation process, GCC tries to avoid such dense flow
230 graphs by factoring computed jumps.  For example, given the following
231 series of jumps, 
232
233 @example
234   goto *x;
235   [ ... ]
236
237   goto *x;
238   [ ... ]
239
240   goto *x;
241   [ ... ]
242 @end example
243
244 @noindent
245 factoring the computed jumps results in the following code sequence
246 which has a much simpler flow graph:
247
248 @example
249   goto y;
250   [ ... ]
251
252   goto y;
253   [ ... ]
254
255   goto y;
256   [ ... ]
257
258 y:
259   goto *x;
260 @end example
261
262 However, the classic problem with this transformation is that it has a
263 runtime cost in there resulting code: An extra jump.  Therefore, the
264 computed jumps are un-factored in the later passes of the compiler.
265 Be aware of that when you work on passes in that area.  There have
266 been numerous examples already where the compile time for code with
267 unfactored computed jumps caused some serious headaches.
268
269 @item nonlocal goto handlers
270 @cindex nonlocal goto handler
271 @findex EDGE_ABNORMAL, EDGE_ABNORMAL_CALL
272 GCC allows nested functions to return into caller using a @code{goto}
273 to a label passed to as an argument to the callee.  The labels passed
274 to nested functions contain special code to cleanup after function
275 call.  Such sections of code are referred to as ``nonlocal goto
276 receivers''.  If a function contains such nonlocal goto receivers, an
277 edge from the call to the label is created with the
278 @code{EDGE_ABNORMAL} and @code{EDGE_ABNORMAL_CALL} flags set.
279
280 @item function entry points
281 @cindex function entry point, alternate function entry point
282 @findex LABEL_ALTERNATE_NAME
283 By definition, execution of function starts at basic block 0, so there
284 is always an edge from the @code{ENTRY_BLOCK_PTR} to basic block 0.
285 There is no @code{tree} representation for alternate entry points at
286 this moment.  In RTL, alternate entry points are specified by
287 @code{CODE_LABEL} with @code{LABEL_ALTERNATE_NAME} defined.  This
288 feature is currently used for multiple entry point prologues and is
289 limited to post-reload passes only.  This can be used by back-ends to
290 emit alternate prologues for functions called from different contexts.
291 In future full support for multiple entry functions defined by Fortran
292 90 needs to be implemented.
293
294 @item function exits
295 In the pre-reload representation a function terminates after the last
296 instruction in the insn chain and no explicit return instructions are
297 used.  This corresponds to the fall-thru edge into exit block.  After
298 reload, optimal RTL epilogues are used that use explicit (conditional)
299 return instructions that are represented by edges with no flags set.
300
301 @end table
302
303
304 @node Profile information
305 @section Profile information
306
307 @cindex profile representation
308 In many cases a compiler must make a choice whether to trade speed in
309 one part of code for speed in another, or to trade code size for code
310 speed.  In such cases it is useful to know information about how often
311 some given block will be executed.  That is the purpose for
312 maintaining profile within the flow graph.
313 GCC can handle profile information obtained through @dfn{profile
314 feedback}, but it can also  estimate branch probabilities based on
315 statics and heuristics.
316
317 @cindex profile feedback
318 The feedback based profile is produced by compiling the program with
319 instrumentation, executing it on a train run and reading the numbers
320 of executions of basic blocks and edges back to the compiler while
321 re-compiling the program to produce the final executable.  This method
322 provides very accurate information about where a program spends most
323 of its time on the train run.  Whether it matches the average run of
324 course depends on the choice of train data set, but several studies
325 have shown that the behavior of a program usually changes just
326 marginally over different data sets.
327
328 @cindex Static profile estimation
329 @cindex branch prediction
330 @findex predict.def
331 When profile feedback is not available, the compiler may be asked to
332 attempt to predict the behavior of each branch in the program using a
333 set of heuristics (see @file{predict.def} for details) and compute
334 estimated frequencies of each basic block by propagating the
335 probabilities over the graph.
336
337 @findex frequency, count, BB_FREQ_BASE
338 Each @code{basic_block} contains two integer fields to represent
339 profile information: @code{frequency} and @code{count}.  The
340 @code{frequency} is an estimation how often is basic block executed
341 within a function.  It is represented as an integer scaled in the
342 range from 0 to @code{BB_FREQ_BASE}.  The most frequently executed
343 basic block in function is initially set to @code{BB_FREQ_BASE} and
344 the rest of frequencies are scaled accordingly.  During optimization,
345 the frequency of the most frequent basic block can both decrease (for
346 instance by loop unrolling) or grow (for instance by cross-jumping
347 optimization), so scaling sometimes has to be performed multiple
348 times.
349
350 @findex gcov_type
351 The @code{count} contains hard-counted numbers of execution measured
352 during training runs and is nonzero only when profile feedback is
353 available.  This value is represented as the host's widest integer
354 (typically a 64 bit integer) of the special type @code{gcov_type}.
355
356 Most optimization passes can use only the frequency information of a
357 basic block, but a few passes may want to know hard execution counts.
358 The frequencies should always match the counts after scaling, however
359 during updating of the profile information numerical error may
360 accumulate into quite large errors.
361
362 @findex REG_BR_PROB_BASE, EDGE_FREQUENCY
363 Each edge also contains a branch probability field: an integer in the
364 range from 0 to @code{REG_BR_PROB_BASE}.  It represents probability of
365 passing control from the end of the @code{src} basic block to the
366 @code{dest} basic block, i.e. the probability that control will flow
367 along this edge.   The @code{EDGE_FREQUENCY} macro is available to
368 compute how frequently a given edge is taken. There is a @code{count}
369 field for each edge as well, representing same information as for a
370 basic block.
371
372 The basic block frequencies are not represented in the instruction
373 stream, but in the RTL representation the edge frequencies are
374 represented for conditional jumps (via the @code{REG_BR_PROB}
375 macro) since they are used when instructions are output to the
376 assembly file and the flow graph is no longer maintained.
377
378 @cindex reverse probability
379 The probability that control flow arrives via a given edge to its
380 destination basic block is called @dfn{reverse probability} and is not
381 directly represented, but it may be easily computed from frequencies
382 of basic blocks.
383
384 @findex redirect_edge_and_branch
385 Updating profile information is a delicate task that can unfortunately
386 not be easily integrated with the CFG manipulation API.  Many of the
387 functions and hooks to modify the CFG, such as
388 @code{redirect_edge_and_branch}, do not have enough information to
389 easily update the profile, so updating it is in the majority of cases
390 left up to the caller.  It is difficult to uncover bugs in the profile
391 updating code, because they manifest themselves only by producing
392 worse code, and checking profile consistency is not possible because
393 of numeric error accumulation.  Hence special attention needs to be
394 given to this issue in each pass that modifies the CFG.
395
396 @findex REG_BR_PROB_BASE, BB_FREQ_BASE, count
397 It is important to point out that @code{REG_BR_PROB_BASE} and
398 @code{BB_FREQ_BASE} are both set low enough to be possible to compute
399 second power of any frequency or probability in the flow graph, it is
400 not possible to even square the @code{count} field, as modern CPUs are
401 fast enough to execute $2^32$ operations quickly.
402
403
404 @node Maintaining the CFG
405 @section Maintaining the CFG
406 @findex cfghooks.h
407
408 An important task of each compiler pass is to keep both the control
409 flow graph and all profile information up-to-date.  Reconstruction of
410 the control flow graph after each pass is not an option, since it may be
411 very expensive and lost profile information cannot be reconstructed at
412 all.
413
414 GCC has two major intermediate representations, and both use the
415 @code{basic_block} and @code{edge} data types to represent control
416 flow.  Both representations share as much of the CFG maintenance code
417 as possible.  For each representation, a set of @dfn{hooks} is defined
418 so that each representation can provide its own implementation of CFG
419 manipulation routines when necessary.  These hooks are defined in
420 @file{cfghooks.h}.  There are hooks for almost all common CFG
421 manipulations, including block splitting and merging, edge redirection
422 and creating and deleting basic blocks.  These hooks should provide
423 everything you need to maintain and manipulate the CFG in both the RTL
424 and @code{tree} representation.
425
426 At the moment, the basic block boundaries are maintained transparently
427 when modifying instructions, so there rarely is a need to move them
428 manually (such as in case someone wants to output instruction outside
429 basic block explicitly).
430 Often the CFG may be better viewed as integral part of instruction
431 chain, than structure built on the top of it.  However, in principle
432 the control flow graph for the @code{tree} representation is
433 @emph{not} an integral part of the representation, in that a function
434 tree may be expanded without first building a  flow graph for the
435 @code{tree} representation at all.  This happens when compiling
436 without any @code{tree} optimization enabled.  When the @code{tree}
437 optimizations are enabled and the instruction stream is rewritten in
438 SSA form, the CFG is very tightly coupled with the instruction stream.
439 In particular, statement insertion and removal has to be done with
440 care.  In fact, the whole @code{tree} representation can not be easily
441 used or maintained without proper maintenance of the CFG
442 simultaneously.
443
444 @findex BLOCK_FOR_INSN, bb_for_stmt
445 In the RTL representation, each instruction has a
446 @code{BLOCK_FOR_INSN} value that represents pointer to the basic block
447 that contains the instruction.  In the @code{tree} representation, the
448 function @code{bb_for_stmt} returns a pointer to the basic block
449 containing the queried statement.
450
451 @cindex block statement iterators
452 When changes need to be applied to a function in its @code{tree}
453 representation, @dfn{block statement iterators} should be used.  These
454 iterators provide an integrated abstraction of the flow graph and the
455 instruction stream.  Block statement iterators iterators are
456 constructed using the @code{block_stmt_iterator} data structure and
457 several modifier are available, including the following:
458
459 @table @code
460 @item bsi_start
461 @findex bsi_start
462 This function initializes a @code{block_stmt_iterator} that points to
463 the first non-empty statement in a basic block.
464
465 @item bsi_last
466 @findex bsi_last
467 This function initializes a @code{block_stmt_iterator} that points to
468 the last statement in a basic block.
469
470 @item bsi_end_p
471 @findex bsi_end_p
472 This predicate is @code{true} if a @code{block_stmt_iterator}
473 represents the end of a basic block.
474
475 @item bsi_next
476 @findex bsi_next
477 This function takes a @code{block_stmt_iterator} and makes it point to
478 its successor.
479
480 @item bsi_prev
481 @item bsi_prev
482 This function takes a @code{block_stmt_iterator} and makes it point to
483 its predecessor.
484
485 @item bsi_insert_after
486 @findex bsi_insert_after
487 This function inserts a statement after the @code{block_stmt_iterator}
488 passed in.  The final parameter determines whether the statement
489 iterator is updated to point to the newly inserted statement, or left
490 pointing to the original statement.
491
492 @item bsi_insert_before
493 @findex bsi_insert_before
494 This function inserts a statement before the @code{block_stmt_iterator}
495 passed in.  The final parameter determines whether the statement
496 iterator is updated to point to the newly inserted statement, or left
497 pointing to the original  statement.
498
499 @item bsi_remove
500 This function removes the @code{block_stmt_iterator} passed in and
501 rechains the remaining statements in a basic block, if any.
502
503 @end table
504
505 @findex BB_HEAD, BB_END
506 In the RTL representation, the macros @code{BB_HEAD} and @code{BB_END}
507 may be used to get the head and end @code{rtx} of a basic block.  No
508 abstract iterators are defined for traversing the insn chain, but you
509 can just use @code{NEXT_INSN} and @code{PREV_INSN} instead.  See
510 @xref{Insns}.
511
512 @findex purge_dead_edges
513 Usually a code manipulating pass simplifies the instruction stream and
514 the flow of control, possibly eliminating some edges.  This may for
515 example happen when a conditional jump is replaced with an
516 unconditional jump, but also when simplifying possibly trapping
517 instruction to non-trapping while compiling Java.  Updating of edges
518 is not transparent and each optimization pass is required to do so
519 manually.  However only few cases occur in practice.  The pass may
520 call @code{purge_dead_edges} on a given basic block to remove
521 superfluous edges, if any.
522
523 @findex redirect_edge_and_branch, redirect_jump
524 Another common scenario is redirection of branch instructions, but
525 this is best modeled as redirection of edges in the control flow graph
526 and thus use of @code{redirect_edge_and_branch} is preferred over more
527 low level functions, such as @code{redirect_jump} that operate on RTL
528 chain only.  The CFG hooks defined in @file{cfghooks.h} should provide
529 the complete API required for manipulating and maintaining the CFG.
530
531 @findex find_sub_basic_blocks, split_block
532 It is also possible that a pass has to insert control flow instruction
533 into the middle of a basic block, thus creating an entry point in the
534 middle of the basic block, which is impossible by definition: The
535 block must be split to make sure it only has one entry point, i.e. the
536 head of the basic block.  In the RTL representation, the
537 @code{find_sub_basic_blocks} may be used to split existing basic block
538 and add necessary edges.  The CFG hook @code{split_block} may be used
539 when an instruction in the middle of a basic block has to become the
540 target of a jump or branch instruction.
541
542 @findex insert_insn_on_edge, commit_edge_insertions
543 @findex bsi_insert_on_edge, bsi_commit_edge_inserts
544 @cindex edge splitting
545 For a global optimizer, a common operation is to split edges in the
546 flow graph and insert instructions on them.  In the RTL
547 representation, this can be easily done using the
548 @code{insert_insn_on_edge} function that emits an instruction
549 ``on the edge'', caching it for a later @code{commit_edge_insertions}
550 call that will take care of moving the inserted instructions off the
551 edge into the instruction stream contained in a basic block.  This
552 includes the creation of new basic blocks where needed.  In the
553 @code{tree} representation, the equivalent functions are
554 @code{bsi_insert_on_edge} which inserts a block statement
555 iterator on an edge, and @code{bsi_commit_edge_inserts} which flushes
556 the instruction to actual instruction stream.
557
558 While debugging the optimization pass, an @code{verify_flow_info}
559 function may be useful to find bugs in the control flow graph updating
560 code.
561
562 Note that at present, the representation of control flow in the
563 @code{tree} representation is discarded before expanding to RTL.
564 Long term the CFG should be maintained and ``expanded'' to the
565 RTL representation along with the function @code{tree} itself.
566
567
568 @node Liveness information
569 @section Liveness information
570 @cindex Liveness representation
571 Liveness information is useful to determine whether some register is
572 ``live'' at given point of program, i.e. that it contains a value that
573 may be used at a later point in the program.  This information is
574 used, for instance, during register allocation, as the pseudo
575 registers only need to be assigned to a unique hard register or to a
576 stack slot if they are live.  The hard registers and stack slots may
577 be freely reused for other values when a register is dead.
578
579 @findex REG_DEAD, REG_UNUSED
580 The liveness information is stored partly in the RTL instruction
581 stream and partly in the flow graph.  Local information is stored in
582 the instruction stream: 
583 Each instruction may contain @code{REG_DEAD} notes representing that
584 the value of a given register is no longer needed, or
585 @code{REG_UNUSED} notes representing that the value computed by the
586 instruction is never used.  The second is useful for instructions
587 computing multiple values at once.
588
589 @findex global_live_at_start, global_live_at_end
590 Global liveness information is stored in the control flow graph.
591 Each basic block contains two bitmaps, @code{global_live_at_start} and
592 @code{global_live_at_end} representing liveness of each register at
593 the entry and exit of the basic block.  The file @code{flow.c}
594 contains functions to compute liveness of each register at any given
595 place in the instruction stream using this information.
596
597 @findex BB_DIRTY, clear_bb_flags, update_life_info_in_dirty_blocks
598 Liveness is expensive to compute and thus it is desirable to keep it
599 up to date during code modifying passes.  This can be easily
600 accomplished using the @code{flags} field of a basic block.  Functions
601 modifying the instruction stream automatically set the @code{BB_DIRTY}
602 flag of a modifies basic block, so the pass may simply
603 use@code{clear_bb_flags} before doing any modifications and then ask
604 the data flow module to have liveness updated via the
605 @code{update_life_info_in_dirty_blocks} function.
606
607 This scheme works reliably as long as no control flow graph
608 transformations are done.  The task of updating liveness after control
609 flow graph changes is more difficult as normal iterative data flow
610 analysis may produce invalid results or get into an infinite cycle
611 when the initial solution is not below the desired one.  Only simple
612 transformations, like splitting basic blocks or inserting on edges,
613 are safe, as functions to implement them already know how to update
614 liveness information locally.