* doc/rtl.texi: Fix a typo.
[official-gcc.git] / gcc / df-core.c
blob1d4aff2315feddfc76d6dc7c3cbb44c8465b6451
1 /* Allocation for dataflow support routines.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Originally contributed by Michael P. Hayes
5 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
6 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
7 and Kenneth Zadeck (zadeck@naturalbridge.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to the Free
23 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA.
28 OVERVIEW:
30 The files in this collection (df*.c,df.h) provide a general framework
31 for solving dataflow problems. The global dataflow is performed using
32 a good implementation of iterative dataflow analysis.
34 The file df-problems.c provides problem instance for the most common
35 dataflow problems: reaching defs, upward exposed uses, live variables,
36 uninitialized variables, def-use chains, and use-def chains. However,
37 the interface allows other dataflow problems to be defined as well.
40 USAGE:
42 Here is an example of using the dataflow routines.
44 struct df *df;
46 df = df_init (init_flags);
48 df_add_problem (df, problem, flags);
50 df_set_blocks (df, blocks);
52 df_rescan_blocks (df, blocks);
54 df_analyze (df);
56 df_dump (df, stderr);
58 df_finish (df);
62 DF_INIT simply creates a poor man's object (df) that needs to be
63 passed to all the dataflow routines. df_finish destroys this object
64 and frees up any allocated memory.
66 There are three flags that can be passed to df_init, each of these
67 flags controls the scanning of the rtl:
69 DF_HARD_REGS means that the scanning is to build information about
70 both pseudo registers and hardware registers. Without this
71 information, the problems will be solved only on pseudo registers.
72 DF_EQUIV_NOTES marks the uses present in EQUIV/EQUAL notes.
73 DF_SUBREGS return subregs rather than the inner reg.
76 DF_ADD_PROBLEM adds a problem, defined by an instance to struct
77 df_problem, to the set of problems solved in this instance of df. All
78 calls to add a problem for a given instance of df must occur before
79 the first call to DF_RESCAN_BLOCKS, DF_SET_BLOCKS or DF_ANALYZE.
81 For all of the problems defined in df-problems.c, there are
82 convenience functions named DF_*_ADD_PROBLEM.
85 Problems can be dependent on other problems. For instance, solving
86 def-use or use-def chains is dependent on solving reaching
87 definitions. As long as these dependencies are listed in the problem
88 definition, the order of adding the problems is not material.
89 Otherwise, the problems will be solved in the order of calls to
90 df_add_problem. Note that it is not necessary to have a problem. In
91 that case, df will just be used to do the scanning.
95 DF_SET_BLOCKS is an optional call used to define a region of the
96 function on which the analysis will be performed. The normal case is
97 to analyze the entire function and no call to df_set_blocks is made.
99 When a subset is given, the analysis behaves as if the function only
100 contains those blocks and any edges that occur directly between the
101 blocks in the set. Care should be taken to call df_set_blocks right
102 before the call to analyze in order to eliminate the possibility that
103 optimizations that reorder blocks invalidate the bitvector.
107 DF_RESCAN_BLOCKS is an optional call that causes the scanner to be
108 (re)run over the set of blocks passed in. If blocks is NULL, the entire
109 function (or all of the blocks defined in df_set_blocks) is rescanned.
110 If blocks contains blocks that were not defined in the call to
111 df_set_blocks, these blocks are added to the set of blocks.
114 DF_ANALYZE causes all of the defined problems to be (re)solved. It
115 does not cause blocks to be (re)scanned at the rtl level unless no
116 prior call is made to df_rescan_blocks.
119 DF_DUMP can then be called to dump the information produce to some
120 file.
124 DF_FINISH causes all of the datastructures to be cleaned up and freed.
125 The df_instance is also freed and its pointer should be NULLed.
130 Scanning produces a `struct df_ref' data structure (ref) is allocated
131 for every register reference (def or use) and this records the insn
132 and bb the ref is found within. The refs are linked together in
133 chains of uses and defs for each insn and for each register. Each ref
134 also has a chain field that links all the use refs for a def or all
135 the def refs for a use. This is used to create use-def or def-use
136 chains.
138 Different optimizations have different needs. Ultimately, only
139 register allocation and schedulers should be using the bitmaps
140 produced for the live register and uninitialized register problems.
141 The rest of the backend should be upgraded to using and maintaining
142 the linked information such as def use or use def chains.
146 PHILOSOPHY:
148 While incremental bitmaps are not worthwhile to maintain, incremental
149 chains may be perfectly reasonable. The fastest way to build chains
150 from scratch or after significant modifications is to build reaching
151 definitions (RD) and build the chains from this.
153 However, general algorithms for maintaining use-def or def-use chains
154 are not practical. The amount of work to recompute the chain any
155 chain after an arbitrary change is large. However, with a modest
156 amount of work it is generally possible to have the application that
157 uses the chains keep them up to date. The high level knowledge of
158 what is really happening is essential to crafting efficient
159 incremental algorithms.
161 As for the bit vector problems, there is no interface to give a set of
162 blocks over with to resolve the iteration. In general, restarting a
163 dataflow iteration is difficult and expensive. Again, the best way to
164 keep the dataflow infomation up to data (if this is really what is
165 needed) it to formulate a problem specific solution.
167 There are fine grained calls for creating and deleting references from
168 instructions in df-scan.c. However, these are not currently connected
169 to the engine that resolves the dataflow equations.
172 DATA STRUCTURES:
174 The basic object is a DF_REF (reference) and this may either be a
175 DEF (definition) or a USE of a register.
177 These are linked into a variety of lists; namely reg-def, reg-use,
178 insn-def, insn-use, def-use, and use-def lists. For example, the
179 reg-def lists contain all the locations that define a given register
180 while the insn-use lists contain all the locations that use a
181 register.
183 Note that the reg-def and reg-use chains are generally short for
184 pseudos and long for the hard registers.
186 ACCESSING REFS:
188 There are 4 ways to obtain access to refs:
190 1) References are divided into two categories, REAL and ARTIFICIAL.
192 REAL refs are associated with instructions. They are linked into
193 either in the insn's defs list (accessed by the DF_INSN_DEFS or
194 DF_INSN_UID_DEFS macros) or the insn's uses list (accessed by the
195 DF_INSN_USES or DF_INSN_UID_USES macros). These macros produce a
196 ref (or NULL), the rest of the list can be obtained by traversal of
197 the NEXT_REF field (accessed by the DF_REF_NEXT_REF macro.) There
198 is no significance to the ordering of the uses or refs in an
199 instruction.
201 ARTIFICIAL refs are associated with basic blocks. The heads of
202 these lists can be accessed by calling get_artificial_defs or
203 get_artificial_uses for the particular basic block. Artificial
204 defs and uses are only there if DF_HARD_REGS was specified when the
205 df instance was created.
207 Artificial defs and uses occur both at the beginning and ends of blocks.
209 For blocks that area at the destination of eh edges, the
210 artificial uses and defs occur at the beginning. The defs relate
211 to the registers specified in EH_RETURN_DATA_REGNO and the uses
212 relate to the registers specified in ED_USES. Logically these
213 defs and uses should really occur along the eh edge, but there is
214 no convenient way to do this. Artificial edges that occur at the
215 beginning of the block have the DF_REF_AT_TOP flag set.
217 Artificial uses occur at the end of all blocks. These arise from
218 the hard registers that are always live, such as the stack
219 register and are put there to keep the code from forgetting about
220 them.
222 Artificial defs occur at the end of the entry block. These arise
223 from registers that are live at entry to the function.
225 2) All of the uses and defs associated with each pseudo or hard
226 register are linked in a bidirectional chain. These are called
227 reg-use or reg_def chains.
229 The first use (or def) for a register can be obtained using the
230 DF_REG_USE_GET macro (or DF_REG_DEF_GET macro). Subsequent uses
231 for the same regno can be obtained by following the next_reg field
232 of the ref.
234 In previous versions of this code, these chains were ordered. It
235 has not been practical to continue this practice.
237 3) If def-use or use-def chains are built, these can be traversed to
238 get to other refs.
240 4) An array of all of the uses (and an array of all of the defs) can
241 be built. These arrays are indexed by the value in the id
242 structure. These arrays are only lazily kept up to date, and that
243 process can be expensive. To have these arrays built, call
244 df_reorganize_refs. Note that the values in the id field of a ref
245 may change across calls to df_analyze or df_reorganize refs.
247 If the only use of this array is to find all of the refs, it is
248 better to traverse all of the registers and then traverse all of
249 reg-use or reg-def chains.
253 NOTES:
255 Embedded addressing side-effects, such as POST_INC or PRE_INC, generate
256 both a use and a def. These are both marked read/write to show that they
257 are dependent. For example, (set (reg 40) (mem (post_inc (reg 42))))
258 will generate a use of reg 42 followed by a def of reg 42 (both marked
259 read/write). Similarly, (set (reg 40) (mem (pre_dec (reg 41))))
260 generates a use of reg 41 then a def of reg 41 (both marked read/write),
261 even though reg 41 is decremented before it is used for the memory
262 address in this second example.
264 A set to a REG inside a ZERO_EXTRACT, or a set to a non-paradoxical SUBREG
265 for which the number of word_mode units covered by the outer mode is
266 smaller than that covered by the inner mode, invokes a read-modify-write.
267 operation. We generate both a use and a def and again mark them
268 read/write.
270 Paradoxical subreg writes do not leave a trace of the old content, so they
271 are write-only operations.
275 #include "config.h"
276 #include "system.h"
277 #include "coretypes.h"
278 #include "tm.h"
279 #include "rtl.h"
280 #include "tm_p.h"
281 #include "insn-config.h"
282 #include "recog.h"
283 #include "function.h"
284 #include "regs.h"
285 #include "output.h"
286 #include "alloc-pool.h"
287 #include "flags.h"
288 #include "hard-reg-set.h"
289 #include "basic-block.h"
290 #include "sbitmap.h"
291 #include "bitmap.h"
292 #include "timevar.h"
293 #include "df.h"
294 #include "tree-pass.h"
296 static struct df *ddf = NULL;
297 struct df *shared_df = NULL;
299 static void *df_get_bb_info (struct dataflow *, unsigned int);
300 static void df_set_bb_info (struct dataflow *, unsigned int, void *);
301 /*----------------------------------------------------------------------------
302 Functions to create, destroy and manipulate an instance of df.
303 ----------------------------------------------------------------------------*/
306 /* Initialize dataflow analysis and allocate and initialize dataflow
307 memory. */
309 struct df *
310 df_init (int flags)
312 struct df *df = XCNEW (struct df);
314 /* This is executed once per compilation to initialize platform
315 specific data structures. */
316 df_hard_reg_init ();
318 /* All df instance must define the scanning problem. */
319 df_scan_add_problem (df, flags);
320 ddf = df;
321 return df;
324 /* Add PROBLEM to the DF instance. */
326 struct dataflow *
327 df_add_problem (struct df *df, struct df_problem *problem, int flags)
329 struct dataflow *dflow;
331 /* First try to add the dependent problem. */
332 if (problem->dependent_problem_fun)
333 (problem->dependent_problem_fun) (df, 0);
335 /* Check to see if this problem has already been defined. If it
336 has, just return that instance, if not, add it to the end of the
337 vector. */
338 dflow = df->problems_by_index[problem->id];
339 if (dflow)
340 return dflow;
342 /* Make a new one and add it to the end. */
343 dflow = XCNEW (struct dataflow);
344 dflow->flags = flags;
345 dflow->df = df;
346 dflow->problem = problem;
347 df->problems_in_order[df->num_problems_defined++] = dflow;
348 df->problems_by_index[dflow->problem->id] = dflow;
350 return dflow;
354 /* Set the MASK flags in the DFLOW problem. The old flags are
355 returned. If a flag is not allowed to be changed this will fail if
356 checking is enabled. */
357 int
358 df_set_flags (struct dataflow *dflow, int mask)
360 int old_flags = dflow->flags;
362 gcc_assert (!(mask & (~dflow->problem->changeable_flags)));
364 dflow->flags |= mask;
366 return old_flags;
369 /* Clear the MASK flags in the DFLOW problem. The old flags are
370 returned. If a flag is not allowed to be changed this will fail if
371 checking is enabled. */
372 int
373 df_clear_flags (struct dataflow *dflow, int mask)
375 int old_flags = dflow->flags;
377 gcc_assert (!(mask & (~dflow->problem->changeable_flags)));
379 dflow->flags &= !mask;
381 return old_flags;
384 /* Set the blocks that are to be considered for analysis. If this is
385 not called or is called with null, the entire function in
386 analyzed. */
388 void
389 df_set_blocks (struct df *df, bitmap blocks)
391 if (blocks)
393 if (df->blocks_to_analyze)
395 int p;
396 bitmap diff = BITMAP_ALLOC (NULL);
397 bitmap_and_compl (diff, df->blocks_to_analyze, blocks);
398 for (p = df->num_problems_defined - 1; p >= 0 ;p--)
400 struct dataflow *dflow = df->problems_in_order[p];
401 if (dflow->problem->reset_fun)
402 dflow->problem->reset_fun (dflow, df->blocks_to_analyze);
403 else if (dflow->problem->free_bb_fun)
405 bitmap_iterator bi;
406 unsigned int bb_index;
408 EXECUTE_IF_SET_IN_BITMAP (diff, 0, bb_index, bi)
410 basic_block bb = BASIC_BLOCK (bb_index);
411 if (bb)
413 dflow->problem->free_bb_fun
414 (dflow, bb, df_get_bb_info (dflow, bb_index));
415 df_set_bb_info (dflow, bb_index, NULL);
421 BITMAP_FREE (diff);
423 else
425 /* If we have not actually run scanning before, do not try
426 to clear anything. */
427 struct dataflow *scan_dflow = df->problems_by_index [DF_SCAN];
428 if (scan_dflow->problem_data)
430 bitmap blocks_to_reset = NULL;
431 int p;
432 for (p = df->num_problems_defined - 1; p >= 0 ;p--)
434 struct dataflow *dflow = df->problems_in_order[p];
435 if (dflow->problem->reset_fun)
437 if (!blocks_to_reset)
439 basic_block bb;
440 blocks_to_reset = BITMAP_ALLOC (NULL);
441 FOR_ALL_BB(bb)
443 bitmap_set_bit (blocks_to_reset, bb->index);
446 dflow->problem->reset_fun (dflow, blocks_to_reset);
449 if (blocks_to_reset)
450 BITMAP_FREE (blocks_to_reset);
452 df->blocks_to_analyze = BITMAP_ALLOC (NULL);
454 bitmap_copy (df->blocks_to_analyze, blocks);
456 else
458 if (df->blocks_to_analyze)
460 BITMAP_FREE (df->blocks_to_analyze);
461 df->blocks_to_analyze = NULL;
467 /* Free all of the per basic block dataflow from all of the problems.
468 This is typically called before a basic block is deleted and the
469 problem will be reanalyzed. */
471 void
472 df_delete_basic_block (struct df *df, int bb_index)
474 basic_block bb = BASIC_BLOCK (bb_index);
475 int i;
477 for (i = 0; i < df->num_problems_defined; i++)
479 struct dataflow *dflow = df->problems_in_order[i];
480 if (dflow->problem->free_bb_fun)
481 dflow->problem->free_bb_fun
482 (dflow, bb, df_get_bb_info (dflow, bb_index));
487 /* Free all the dataflow info and the DF structure. This should be
488 called from the df_finish macro which also NULLs the parm. */
490 void
491 df_finish1 (struct df *df)
493 int i;
495 for (i = 0; i < df->num_problems_defined; i++)
496 df->problems_in_order[i]->problem->free_fun (df->problems_in_order[i]);
498 free (df);
502 /*----------------------------------------------------------------------------
503 The general data flow analysis engine.
504 ----------------------------------------------------------------------------*/
507 /* Hybrid search algorithm from "Implementation Techniques for
508 Efficient Data-Flow Analysis of Large Programs". */
510 static void
511 df_hybrid_search_forward (basic_block bb,
512 struct dataflow *dataflow,
513 bool single_pass)
515 int result_changed;
516 int i = bb->index;
517 edge e;
518 edge_iterator ei;
520 SET_BIT (dataflow->visited, bb->index);
521 gcc_assert (TEST_BIT (dataflow->pending, bb->index));
522 RESET_BIT (dataflow->pending, i);
524 /* Calculate <conf_op> of predecessor_outs. */
525 if (EDGE_COUNT (bb->preds) > 0)
526 FOR_EACH_EDGE (e, ei, bb->preds)
528 if (!TEST_BIT (dataflow->considered, e->src->index))
529 continue;
531 dataflow->problem->con_fun_n (dataflow, e);
533 else if (dataflow->problem->con_fun_0)
534 dataflow->problem->con_fun_0 (dataflow, bb);
536 result_changed = dataflow->problem->trans_fun (dataflow, i);
538 if (!result_changed || single_pass)
539 return;
541 FOR_EACH_EDGE (e, ei, bb->succs)
543 if (e->dest->index == i)
544 continue;
545 if (!TEST_BIT (dataflow->considered, e->dest->index))
546 continue;
547 SET_BIT (dataflow->pending, e->dest->index);
550 FOR_EACH_EDGE (e, ei, bb->succs)
552 if (e->dest->index == i)
553 continue;
555 if (!TEST_BIT (dataflow->considered, e->dest->index))
556 continue;
557 if (!TEST_BIT (dataflow->visited, e->dest->index))
558 df_hybrid_search_forward (e->dest, dataflow, single_pass);
562 static void
563 df_hybrid_search_backward (basic_block bb,
564 struct dataflow *dataflow,
565 bool single_pass)
567 int result_changed;
568 int i = bb->index;
569 edge e;
570 edge_iterator ei;
572 SET_BIT (dataflow->visited, bb->index);
573 gcc_assert (TEST_BIT (dataflow->pending, bb->index));
574 RESET_BIT (dataflow->pending, i);
576 /* Calculate <conf_op> of predecessor_outs. */
577 if (EDGE_COUNT (bb->succs) > 0)
578 FOR_EACH_EDGE (e, ei, bb->succs)
580 if (!TEST_BIT (dataflow->considered, e->dest->index))
581 continue;
583 dataflow->problem->con_fun_n (dataflow, e);
585 else if (dataflow->problem->con_fun_0)
586 dataflow->problem->con_fun_0 (dataflow, bb);
588 result_changed = dataflow->problem->trans_fun (dataflow, i);
590 if (!result_changed || single_pass)
591 return;
593 FOR_EACH_EDGE (e, ei, bb->preds)
595 if (e->src->index == i)
596 continue;
598 if (!TEST_BIT (dataflow->considered, e->src->index))
599 continue;
601 SET_BIT (dataflow->pending, e->src->index);
604 FOR_EACH_EDGE (e, ei, bb->preds)
606 if (e->src->index == i)
607 continue;
609 if (!TEST_BIT (dataflow->considered, e->src->index))
610 continue;
612 if (!TEST_BIT (dataflow->visited, e->src->index))
613 df_hybrid_search_backward (e->src, dataflow, single_pass);
618 /* This function will perform iterative bitvector dataflow described
619 by DATAFLOW, producing the in and out sets. Only the part of the
620 cfg induced by blocks in DATAFLOW->order is taken into account.
622 SINGLE_PASS is true if you just want to make one pass over the
623 blocks. */
625 void
626 df_iterative_dataflow (struct dataflow *dataflow,
627 bitmap blocks_to_consider, bitmap blocks_to_init,
628 int *blocks_in_postorder, int n_blocks,
629 bool single_pass)
631 unsigned int idx;
632 int i;
633 sbitmap visited = sbitmap_alloc (last_basic_block);
634 sbitmap pending = sbitmap_alloc (last_basic_block);
635 sbitmap considered = sbitmap_alloc (last_basic_block);
636 bitmap_iterator bi;
638 dataflow->visited = visited;
639 dataflow->pending = pending;
640 dataflow->considered = considered;
642 sbitmap_zero (visited);
643 sbitmap_zero (pending);
644 sbitmap_zero (considered);
646 gcc_assert (dataflow->problem->dir);
648 EXECUTE_IF_SET_IN_BITMAP (blocks_to_consider, 0, idx, bi)
650 SET_BIT (considered, idx);
653 for (i = 0; i < n_blocks; i++)
655 idx = blocks_in_postorder[i];
656 SET_BIT (pending, idx);
659 dataflow->problem->init_fun (dataflow, blocks_to_init);
661 while (1)
664 /* For forward problems, you want to pass in reverse postorder
665 and for backward problems you want postorder. This has been
666 shown to be as good as you can do by several people, the
667 first being Mathew Hecht in his phd dissertation.
669 The nodes are passed into this function in postorder. */
671 if (dataflow->problem->dir == DF_FORWARD)
673 for (i = n_blocks - 1 ; i >= 0 ; i--)
675 idx = blocks_in_postorder[i];
677 if (TEST_BIT (pending, idx) && !TEST_BIT (visited, idx))
678 df_hybrid_search_forward (BASIC_BLOCK (idx), dataflow, single_pass);
681 else
683 for (i = 0; i < n_blocks; i++)
685 idx = blocks_in_postorder[i];
687 if (TEST_BIT (pending, idx) && !TEST_BIT (visited, idx))
688 df_hybrid_search_backward (BASIC_BLOCK (idx), dataflow, single_pass);
692 if (sbitmap_first_set_bit (pending) == -1)
693 break;
695 sbitmap_zero (visited);
698 sbitmap_free (pending);
699 sbitmap_free (visited);
700 sbitmap_free (considered);
704 /* Remove the entries not in BLOCKS from the LIST of length LEN, preserving
705 the order of the remaining entries. Returns the length of the resulting
706 list. */
708 static unsigned
709 df_prune_to_subcfg (int list[], unsigned len, bitmap blocks)
711 unsigned act, last;
713 for (act = 0, last = 0; act < len; act++)
714 if (bitmap_bit_p (blocks, list[act]))
715 list[last++] = list[act];
717 return last;
721 /* Execute dataflow analysis on a single dataflow problem.
723 There are three sets of blocks passed in:
725 BLOCKS_TO_CONSIDER are the blocks whose solution can either be
726 examined or will be computed. For calls from DF_ANALYZE, this is
727 the set of blocks that has been passed to DF_SET_BLOCKS. For calls
728 from DF_ANALYZE_SIMPLE_CHANGE_SOME_BLOCKS, this is the set of
729 blocks in the fringe (the set of blocks passed in plus the set of
730 immed preds and succs of those blocks).
732 BLOCKS_TO_INIT are the blocks whose solution will be changed by
733 this iteration. For calls from DF_ANALYZE, this is the set of
734 blocks that has been passed to DF_SET_BLOCKS. For calls from
735 DF_ANALYZE_SIMPLE_CHANGE_SOME_BLOCKS, this is the set of blocks
736 passed in.
738 BLOCKS_TO_SCAN are the set of blocks that need to be rescanned.
739 For calls from DF_ANALYZE, this is the accumulated set of blocks
740 that has been passed to DF_RESCAN_BLOCKS since the last call to
741 DF_ANALYZE. For calls from DF_ANALYZE_SIMPLE_CHANGE_SOME_BLOCKS,
742 this is the set of blocks passed in.
744 blocks_to_consider blocks_to_init blocks_to_scan
745 full redo all all all
746 partial redo all all sub
747 small fixup fringe sub sub
750 void
751 df_analyze_problem (struct dataflow *dflow,
752 bitmap blocks_to_consider,
753 bitmap blocks_to_init,
754 bitmap blocks_to_scan,
755 int *postorder, int n_blocks, bool single_pass)
757 /* (Re)Allocate the datastructures necessary to solve the problem. */
758 if (dflow->problem->alloc_fun)
759 dflow->problem->alloc_fun (dflow, blocks_to_scan, blocks_to_init);
761 /* Set up the problem and compute the local information. This
762 function is passed both the blocks_to_consider and the
763 blocks_to_scan because the RD and RU problems require the entire
764 function to be rescanned if they are going to be updated. */
765 if (dflow->problem->local_compute_fun)
766 dflow->problem->local_compute_fun (dflow, blocks_to_consider, blocks_to_scan);
768 /* Solve the equations. */
769 if (dflow->problem->dataflow_fun)
770 dflow->problem->dataflow_fun (dflow, blocks_to_consider, blocks_to_init,
771 postorder, n_blocks, single_pass);
773 /* Massage the solution. */
774 if (dflow->problem->finalize_fun)
775 dflow->problem->finalize_fun (dflow, blocks_to_consider);
779 /* Analyze dataflow info for the basic blocks specified by the bitmap
780 BLOCKS, or for the whole CFG if BLOCKS is zero. */
782 void
783 df_analyze (struct df *df)
785 int *postorder = XNEWVEC (int, last_basic_block);
786 bitmap current_all_blocks = BITMAP_ALLOC (NULL);
787 int n_blocks;
788 int i;
789 bool everything;
791 n_blocks = post_order_compute (postorder, true);
793 if (n_blocks != n_basic_blocks)
794 delete_unreachable_blocks ();
796 for (i = 0; i < n_blocks; i++)
797 bitmap_set_bit (current_all_blocks, postorder[i]);
799 /* No one called df_rescan_blocks, so do it. */
800 if (!df->blocks_to_scan)
801 df_rescan_blocks (df, NULL);
803 /* Make sure that we have pruned any unreachable blocks from these
804 sets. */
805 bitmap_and_into (df->blocks_to_scan, current_all_blocks);
807 if (df->blocks_to_analyze)
809 everything = false;
810 bitmap_and_into (df->blocks_to_analyze, current_all_blocks);
811 n_blocks = df_prune_to_subcfg (postorder, n_blocks, df->blocks_to_analyze);
812 BITMAP_FREE (current_all_blocks);
814 else
816 everything = true;
817 df->blocks_to_analyze = current_all_blocks;
818 current_all_blocks = NULL;
821 /* Skip over the DF_SCAN problem. */
822 for (i = 1; i < df->num_problems_defined; i++)
823 df_analyze_problem (df->problems_in_order[i],
824 df->blocks_to_analyze, df->blocks_to_analyze,
825 df->blocks_to_scan,
826 postorder, n_blocks, false);
828 if (everything)
830 BITMAP_FREE (df->blocks_to_analyze);
831 df->blocks_to_analyze = NULL;
834 BITMAP_FREE (df->blocks_to_scan);
835 df->blocks_to_scan = NULL;
836 free (postorder);
841 /*----------------------------------------------------------------------------
842 Functions to support limited incremental change.
843 ----------------------------------------------------------------------------*/
846 /* Get basic block info. */
848 static void *
849 df_get_bb_info (struct dataflow *dflow, unsigned int index)
851 return (struct df_scan_bb_info *) dflow->block_info[index];
855 /* Set basic block info. */
857 static void
858 df_set_bb_info (struct dataflow *dflow, unsigned int index,
859 void *bb_info)
861 dflow->block_info[index] = bb_info;
865 /* Called from the rtl_compact_blocks to reorganize the problems basic
866 block info. */
868 void
869 df_compact_blocks (struct df *df)
871 int i, p;
872 basic_block bb;
873 void **problem_temps;
874 int size = last_basic_block *sizeof (void *);
875 problem_temps = xmalloc (size);
877 for (p = 0; p < df->num_problems_defined; p++)
879 struct dataflow *dflow = df->problems_in_order[p];
880 if (dflow->problem->free_bb_fun)
882 df_grow_bb_info (dflow);
883 memcpy (problem_temps, dflow->block_info, size);
885 /* Copy the bb info from the problem tmps to the proper
886 place in the block_info vector. Null out the copied
887 item. */
888 i = NUM_FIXED_BLOCKS;
889 FOR_EACH_BB (bb)
891 df_set_bb_info (dflow, i, problem_temps[bb->index]);
892 problem_temps[bb->index] = NULL;
893 i++;
895 memset (dflow->block_info + i, 0,
896 (last_basic_block - i) *sizeof (void *));
898 /* Free any block infos that were not copied (and NULLed).
899 These are from orphaned blocks. */
900 for (i = NUM_FIXED_BLOCKS; i < last_basic_block; i++)
902 basic_block bb = BASIC_BLOCK (i);
903 if (problem_temps[i] && bb)
904 dflow->problem->free_bb_fun
905 (dflow, bb, problem_temps[i]);
910 free (problem_temps);
912 i = NUM_FIXED_BLOCKS;
913 FOR_EACH_BB (bb)
915 SET_BASIC_BLOCK (i, bb);
916 bb->index = i;
917 i++;
920 gcc_assert (i == n_basic_blocks);
922 for (; i < last_basic_block; i++)
923 SET_BASIC_BLOCK (i, NULL);
927 /* Shove NEW_BLOCK in at OLD_INDEX. Called from if-cvt to hack a
928 block. There is no excuse for people to do this kind of thing. */
930 void
931 df_bb_replace (struct df *df, int old_index, basic_block new_block)
933 int p;
935 for (p = 0; p < df->num_problems_defined; p++)
937 struct dataflow *dflow = df->problems_in_order[p];
938 if (dflow->block_info)
940 void *temp;
942 df_grow_bb_info (dflow);
944 /* The old switcheroo. */
946 temp = df_get_bb_info (dflow, old_index);
947 df_set_bb_info (dflow, old_index,
948 df_get_bb_info (dflow, new_block->index));
949 df_set_bb_info (dflow, new_block->index, temp);
953 SET_BASIC_BLOCK (old_index, new_block);
954 new_block->index = old_index;
957 /*----------------------------------------------------------------------------
958 PUBLIC INTERFACES TO QUERY INFORMATION.
959 ----------------------------------------------------------------------------*/
962 /* Return last use of REGNO within BB. */
964 struct df_ref *
965 df_bb_regno_last_use_find (struct df *df, basic_block bb, unsigned int regno)
967 rtx insn;
968 struct df_ref *use;
969 unsigned int uid;
971 FOR_BB_INSNS_REVERSE (bb, insn)
973 if (!INSN_P (insn))
974 continue;
976 uid = INSN_UID (insn);
977 for (use = DF_INSN_UID_GET (df, uid)->uses; use; use = use->next_ref)
978 if (DF_REF_REGNO (use) == regno)
979 return use;
981 return NULL;
985 /* Return first def of REGNO within BB. */
987 struct df_ref *
988 df_bb_regno_first_def_find (struct df *df, basic_block bb, unsigned int regno)
990 rtx insn;
991 struct df_ref *def;
992 unsigned int uid;
994 FOR_BB_INSNS (bb, insn)
996 if (!INSN_P (insn))
997 continue;
999 uid = INSN_UID (insn);
1000 for (def = DF_INSN_UID_GET (df, uid)->defs; def; def = def->next_ref)
1001 if (DF_REF_REGNO (def) == regno)
1002 return def;
1004 return NULL;
1008 /* Return last def of REGNO within BB. */
1010 struct df_ref *
1011 df_bb_regno_last_def_find (struct df *df, basic_block bb, unsigned int regno)
1013 rtx insn;
1014 struct df_ref *def;
1015 unsigned int uid;
1017 FOR_BB_INSNS_REVERSE (bb, insn)
1019 if (!INSN_P (insn))
1020 continue;
1022 uid = INSN_UID (insn);
1023 for (def = DF_INSN_UID_GET (df, uid)->defs; def; def = def->next_ref)
1024 if (DF_REF_REGNO (def) == regno)
1025 return def;
1028 return NULL;
1031 /* Return true if INSN defines REGNO. */
1033 bool
1034 df_insn_regno_def_p (struct df *df, rtx insn, unsigned int regno)
1036 unsigned int uid;
1037 struct df_ref *def;
1039 uid = INSN_UID (insn);
1040 for (def = DF_INSN_UID_GET (df, uid)->defs; def; def = def->next_ref)
1041 if (DF_REF_REGNO (def) == regno)
1042 return true;
1044 return false;
1048 /* Finds the reference corresponding to the definition of REG in INSN.
1049 DF is the dataflow object. */
1051 struct df_ref *
1052 df_find_def (struct df *df, rtx insn, rtx reg)
1054 unsigned int uid;
1055 struct df_ref *def;
1057 if (GET_CODE (reg) == SUBREG)
1058 reg = SUBREG_REG (reg);
1059 gcc_assert (REG_P (reg));
1061 uid = INSN_UID (insn);
1062 for (def = DF_INSN_UID_GET (df, uid)->defs; def; def = def->next_ref)
1063 if (rtx_equal_p (DF_REF_REAL_REG (def), reg))
1064 return def;
1066 return NULL;
1070 /* Return true if REG is defined in INSN, zero otherwise. */
1072 bool
1073 df_reg_defined (struct df *df, rtx insn, rtx reg)
1075 return df_find_def (df, insn, reg) != NULL;
1079 /* Finds the reference corresponding to the use of REG in INSN.
1080 DF is the dataflow object. */
1082 struct df_ref *
1083 df_find_use (struct df *df, rtx insn, rtx reg)
1085 unsigned int uid;
1086 struct df_ref *use;
1088 if (GET_CODE (reg) == SUBREG)
1089 reg = SUBREG_REG (reg);
1090 gcc_assert (REG_P (reg));
1092 uid = INSN_UID (insn);
1093 for (use = DF_INSN_UID_GET (df, uid)->uses; use; use = use->next_ref)
1094 if (rtx_equal_p (DF_REF_REAL_REG (use), reg))
1095 return use;
1097 return NULL;
1101 /* Return true if REG is referenced in INSN, zero otherwise. */
1103 bool
1104 df_reg_used (struct df *df, rtx insn, rtx reg)
1106 return df_find_use (df, insn, reg) != NULL;
1110 /*----------------------------------------------------------------------------
1111 Debugging and printing functions.
1112 ----------------------------------------------------------------------------*/
1114 /* Dump dataflow info. */
1115 void
1116 df_dump (struct df *df, FILE *file)
1118 int i;
1120 if (!df || !file)
1121 return;
1123 fprintf (file, "\n\n%s\n", current_function_name ());
1124 fprintf (file, "\nDataflow summary:\n");
1125 fprintf (file, "def_info->bitmap_size = %d, use_info->bitmap_size = %d\n",
1126 df->def_info.bitmap_size, df->use_info.bitmap_size);
1128 for (i = 0; i < df->num_problems_defined; i++)
1129 df->problems_in_order[i]->problem->dump_fun (df->problems_in_order[i], file);
1131 fprintf (file, "\n");
1135 void
1136 df_refs_chain_dump (struct df_ref *ref, bool follow_chain, FILE *file)
1138 fprintf (file, "{ ");
1139 while (ref)
1141 fprintf (file, "%c%d(%d) ",
1142 DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
1143 DF_REF_ID (ref),
1144 DF_REF_REGNO (ref));
1145 if (follow_chain)
1146 df_chain_dump (DF_REF_CHAIN (ref), file);
1147 ref = ref->next_ref;
1149 fprintf (file, "}");
1153 /* Dump either a ref-def or reg-use chain. */
1155 void
1156 df_regs_chain_dump (struct df *df ATTRIBUTE_UNUSED, struct df_ref *ref, FILE *file)
1158 fprintf (file, "{ ");
1159 while (ref)
1161 fprintf (file, "%c%d(%d) ",
1162 DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
1163 DF_REF_ID (ref),
1164 DF_REF_REGNO (ref));
1165 ref = ref->next_reg;
1167 fprintf (file, "}");
1171 static void
1172 df_mws_dump (struct df_mw_hardreg *mws, FILE *file)
1174 while (mws)
1176 struct df_link *regs = mws->regs;
1177 fprintf (file, "%c%d(",
1178 (mws->type == DF_REF_REG_DEF) ? 'd' : 'u',
1179 DF_REF_REGNO (regs->ref));
1180 while (regs)
1182 fprintf (file, "%d ", DF_REF_REGNO (regs->ref));
1183 regs = regs->next;
1186 fprintf (file, ") ");
1187 mws = mws->next;
1192 static void
1193 df_insn_uid_debug (struct df *df, unsigned int uid,
1194 bool follow_chain, FILE *file)
1196 int bbi;
1198 if (DF_INSN_UID_DEFS (df, uid))
1199 bbi = DF_REF_BBNO (DF_INSN_UID_DEFS (df, uid));
1200 else if (DF_INSN_UID_USES(df, uid))
1201 bbi = DF_REF_BBNO (DF_INSN_UID_USES (df, uid));
1202 else
1203 bbi = -1;
1205 fprintf (file, "insn %d bb %d luid %d",
1206 uid, bbi, DF_INSN_UID_LUID (df, uid));
1208 if (DF_INSN_UID_DEFS (df, uid))
1210 fprintf (file, " defs ");
1211 df_refs_chain_dump (DF_INSN_UID_DEFS (df, uid), follow_chain, file);
1214 if (DF_INSN_UID_USES (df, uid))
1216 fprintf (file, " uses ");
1217 df_refs_chain_dump (DF_INSN_UID_USES (df, uid), follow_chain, file);
1220 if (DF_INSN_UID_MWS (df, uid))
1222 fprintf (file, " mws ");
1223 df_mws_dump (DF_INSN_UID_MWS (df, uid), file);
1225 fprintf (file, "\n");
1229 void
1230 df_insn_debug (struct df *df, rtx insn, bool follow_chain, FILE *file)
1232 df_insn_uid_debug (df, INSN_UID (insn), follow_chain, file);
1235 void
1236 df_insn_debug_regno (struct df *df, rtx insn, FILE *file)
1238 unsigned int uid;
1239 int bbi;
1241 uid = INSN_UID (insn);
1242 if (DF_INSN_UID_DEFS (df, uid))
1243 bbi = DF_REF_BBNO (DF_INSN_UID_DEFS (df, uid));
1244 else if (DF_INSN_UID_USES(df, uid))
1245 bbi = DF_REF_BBNO (DF_INSN_UID_USES (df, uid));
1246 else
1247 bbi = -1;
1249 fprintf (file, "insn %d bb %d luid %d defs ",
1250 uid, bbi, DF_INSN_LUID (df, insn));
1251 df_regs_chain_dump (df, DF_INSN_UID_DEFS (df, uid), file);
1253 fprintf (file, " uses ");
1254 df_regs_chain_dump (df, DF_INSN_UID_USES (df, uid), file);
1255 fprintf (file, "\n");
1258 void
1259 df_regno_debug (struct df *df, unsigned int regno, FILE *file)
1261 fprintf (file, "reg %d defs ", regno);
1262 df_regs_chain_dump (df, DF_REG_DEF_GET (df, regno)->reg_chain, file);
1263 fprintf (file, " uses ");
1264 df_regs_chain_dump (df, DF_REG_USE_GET (df, regno)->reg_chain, file);
1265 fprintf (file, "\n");
1269 void
1270 df_ref_debug (struct df_ref *ref, FILE *file)
1272 fprintf (file, "%c%d ",
1273 DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
1274 DF_REF_ID (ref));
1275 fprintf (file, "reg %d bb %d insn %d flag %x chain ",
1276 DF_REF_REGNO (ref),
1277 DF_REF_BBNO (ref),
1278 DF_REF_INSN (ref) ? INSN_UID (DF_REF_INSN (ref)) : -1,
1279 DF_REF_FLAGS (ref));
1280 df_chain_dump (DF_REF_CHAIN (ref), file);
1281 fprintf (file, "\n");
1284 /* Functions for debugging from GDB. */
1286 void
1287 debug_df_insn (rtx insn)
1289 df_insn_debug (ddf, insn, true, stderr);
1290 debug_rtx (insn);
1294 void
1295 debug_df_reg (rtx reg)
1297 df_regno_debug (ddf, REGNO (reg), stderr);
1301 void
1302 debug_df_regno (unsigned int regno)
1304 df_regno_debug (ddf, regno, stderr);
1308 void
1309 debug_df_ref (struct df_ref *ref)
1311 df_ref_debug (ref, stderr);
1315 void
1316 debug_df_defno (unsigned int defno)
1318 df_ref_debug (DF_DEFS_GET (ddf, defno), stderr);
1322 void
1323 debug_df_useno (unsigned int defno)
1325 df_ref_debug (DF_USES_GET (ddf, defno), stderr);
1329 void
1330 debug_df_chain (struct df_link *link)
1332 df_chain_dump (link, stderr);
1333 fputc ('\n', stderr);