1 /* Dataflow support routines.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz,
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 This file provides some dataflow routines for computing reaching defs,
27 upward exposed uses, live variables, def-use chains, and use-def
28 chains. The global dataflow is performed using simple iterative
29 methods with a worklist and could be sped up by ordering the blocks
30 with a depth first search order.
32 A `struct ref' data structure (ref) is allocated for every register
33 reference (def or use) and this records the insn and bb the ref is
34 found within. The refs are linked together in chains of uses and defs
35 for each insn and for each register. Each ref also has a chain field
36 that links all the use refs for a def or all the def refs for a use.
37 This is used to create use-def or def-use chains.
42 Here's an example of using the dataflow routines.
48 df_analyse (df, 0, DF_ALL);
50 df_dump (df, DF_ALL, stderr);
55 df_init simply creates a poor man's object (df) that needs to be
56 passed to all the dataflow routines. df_finish destroys this
57 object and frees up any allocated memory.
59 df_analyse performs the following:
61 1. Records defs and uses by scanning the insns in each basic block
62 or by scanning the insns queued by df_insn_modify.
63 2. Links defs and uses into insn-def and insn-use chains.
64 3. Links defs and uses into reg-def and reg-use chains.
65 4. Assigns LUIDs to each insn (for modified blocks).
66 5. Calculates local reaching definitions.
67 6. Calculates global reaching definitions.
68 7. Creates use-def chains.
69 8. Calculates local reaching uses (upwards exposed uses).
70 9. Calculates global reaching uses.
71 10. Creates def-use chains.
72 11. Calculates local live registers.
73 12. Calculates global live registers.
74 13. Calculates register lifetimes and determines local registers.
79 Note that the dataflow information is not updated for every newly
80 deleted or created insn. If the dataflow information requires
81 updating then all the changed, new, or deleted insns needs to be
82 marked with df_insn_modify (or df_insns_modify) either directly or
83 indirectly (say through calling df_insn_delete). df_insn_modify
84 marks all the modified insns to get processed the next time df_analyse
87 Beware that tinkering with insns may invalidate the dataflow information.
88 The philosophy behind these routines is that once the dataflow
89 information has been gathered, the user should store what they require
90 before they tinker with any insn. Once a reg is replaced, for example,
91 then the reg-def/reg-use chains will point to the wrong place. Once a
92 whole lot of changes have been made, df_analyse can be called again
93 to update the dataflow information. Currently, this is not very smart
94 with regard to propagating changes to the dataflow so it should not
100 The basic object is a REF (reference) and this may either be a DEF
101 (definition) or a USE of a register.
103 These are linked into a variety of lists; namely reg-def, reg-use,
104 insn-def, insn-use, def-use, and use-def lists. For example,
105 the reg-def lists contain all the refs that define a given register
106 while the insn-use lists contain all the refs used by an insn.
108 Note that the reg-def and reg-use chains are generally short (except for the
109 hard registers) and thus it is much faster to search these chains
110 rather than searching the def or use bitmaps.
112 If the insns are in SSA form then the reg-def and use-def lists
113 should only contain the single defining ref.
117 1) Incremental dataflow analysis.
119 Note that if a loop invariant insn is hoisted (or sunk), we do not
120 need to change the def-use or use-def chains. All we have to do is to
121 change the bb field for all the associated defs and uses and to
122 renumber the LUIDs for the original and new basic blocks of the insn.
124 When shadowing loop mems we create new uses and defs for new pseudos
125 so we do not affect the existing dataflow information.
127 My current strategy is to queue up all modified, created, or deleted
128 insns so when df_analyse is called we can easily determine all the new
129 or deleted refs. Currently the global dataflow information is
130 recomputed from scratch but this could be propagated more efficiently.
132 2) Improved global data flow computation using depth first search.
134 3) Reduced memory requirements.
136 We could operate a pool of ref structures. When a ref is deleted it
137 gets returned to the pool (say by linking on to a chain of free refs).
138 This will require a pair of bitmaps for defs and uses so that we can
139 tell which ones have been changed. Alternatively, we could
140 periodically squeeze the def and use tables and associated bitmaps and
141 renumber the def and use ids.
143 4) Ordering of reg-def and reg-use lists.
145 Should the first entry in the def list be the first def (within a BB)?
146 Similarly, should the first entry in the use list be the last use
149 5) Working with a sub-CFG.
151 Often the whole CFG does not need to be analysed, for example,
152 when optimising a loop, only certain registers are of interest.
153 Perhaps there should be a bitmap argument to df_analyse to specify
154 which registers should be analysed? */
156 #define HANDLE_SUBREG
162 #include "insn-config.h"
164 #include "function.h"
167 #include "hard-reg-set.h"
168 #include "basic-block.h"
174 #define FOR_ALL_BBS(BB, CODE) \
177 for (node_ = 0; node_ < n_basic_blocks; node_++) \
178 {(BB) = BASIC_BLOCK (node_); CODE;};} while (0)
180 #define FOR_EACH_BB_IN_BITMAP(BITMAP, MIN, BB, CODE) \
182 unsigned int node_; \
183 EXECUTE_IF_SET_IN_BITMAP (BITMAP, MIN, node_, \
184 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
186 #define FOR_EACH_BB_IN_BITMAP_REV(BITMAP, MIN, BB, CODE) \
188 unsigned int node_; \
189 EXECUTE_IF_SET_IN_BITMAP_REV (BITMAP, node_, \
190 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
192 #define FOR_EACH_BB_IN_SBITMAP(BITMAP, MIN, BB, CODE) \
194 unsigned int node_; \
195 EXECUTE_IF_SET_IN_SBITMAP (BITMAP, MIN, node_, \
196 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
198 #define obstack_chunk_alloc xmalloc
199 #define obstack_chunk_free free
201 static struct obstack df_ref_obstack;
202 static struct df *ddf;
204 static void df_reg_table_realloc PARAMS((struct df *, int));
206 static void df_def_table_realloc PARAMS((struct df *, int));
208 static void df_insn_table_realloc PARAMS((struct df *, int));
209 static void df_bitmaps_alloc PARAMS((struct df *, int));
210 static void df_bitmaps_free PARAMS((struct df *, int));
211 static void df_free PARAMS((struct df *));
212 static void df_alloc PARAMS((struct df *, int));
214 static rtx df_reg_clobber_gen PARAMS((unsigned int));
215 static rtx df_reg_use_gen PARAMS((unsigned int));
217 static inline struct df_link *df_link_create PARAMS((struct ref *,
219 static struct df_link *df_ref_unlink PARAMS((struct df_link **, struct ref *));
220 static void df_def_unlink PARAMS((struct df *, struct ref *));
221 static void df_use_unlink PARAMS((struct df *, struct ref *));
222 static void df_insn_refs_unlink PARAMS ((struct df *, basic_block, rtx));
224 static void df_bb_refs_unlink PARAMS ((struct df *, basic_block));
225 static void df_refs_unlink PARAMS ((struct df *, bitmap));
228 static struct ref *df_ref_create PARAMS((struct df *,
229 rtx, rtx *, basic_block, rtx,
231 static void df_ref_record_1 PARAMS((struct df *, rtx, rtx *,
232 basic_block, rtx, enum df_ref_type));
233 static void df_ref_record PARAMS((struct df *, rtx, rtx *,
234 basic_block bb, rtx, enum df_ref_type));
235 static void df_def_record_1 PARAMS((struct df *, rtx, basic_block, rtx));
236 static void df_defs_record PARAMS((struct df *, rtx, basic_block, rtx));
237 static void df_uses_record PARAMS((struct df *, rtx *,
238 enum df_ref_type, basic_block, rtx));
239 static void df_insn_refs_record PARAMS((struct df *, basic_block, rtx));
240 static void df_bb_refs_record PARAMS((struct df *, basic_block));
241 static void df_refs_record PARAMS((struct df *, bitmap));
243 static void df_bb_reg_def_chain_create PARAMS((struct df *, basic_block));
244 static void df_reg_def_chain_create PARAMS((struct df *, bitmap));
245 static void df_bb_reg_use_chain_create PARAMS((struct df *, basic_block));
246 static void df_reg_use_chain_create PARAMS((struct df *, bitmap));
247 static void df_bb_du_chain_create PARAMS((struct df *, basic_block, bitmap));
248 static void df_du_chain_create PARAMS((struct df *, bitmap));
249 static void df_bb_ud_chain_create PARAMS((struct df *, basic_block));
250 static void df_ud_chain_create PARAMS((struct df *, bitmap));
251 static void df_bb_rd_local_compute PARAMS((struct df *, basic_block));
252 static void df_rd_local_compute PARAMS((struct df *, bitmap));
253 static void df_bb_ru_local_compute PARAMS((struct df *, basic_block));
254 static void df_ru_local_compute PARAMS((struct df *, bitmap));
255 static void df_bb_lr_local_compute PARAMS((struct df *, basic_block));
256 static void df_lr_local_compute PARAMS((struct df *, bitmap));
257 static void df_bb_reg_info_compute PARAMS((struct df *, basic_block, bitmap));
258 static void df_reg_info_compute PARAMS((struct df *, bitmap));
260 static int df_bb_luids_set PARAMS((struct df *df, basic_block));
261 static int df_luids_set PARAMS((struct df *df, bitmap));
263 static int df_modified_p PARAMS ((struct df *, bitmap));
264 static int df_refs_queue PARAMS ((struct df *));
265 static int df_refs_process PARAMS ((struct df *));
266 static int df_bb_refs_update PARAMS ((struct df *, basic_block));
267 static int df_refs_update PARAMS ((struct df *));
268 static void df_analyse_1 PARAMS((struct df *, bitmap, int, int));
270 static void df_insns_modify PARAMS((struct df *, basic_block,
272 static int df_rtx_mem_replace PARAMS ((rtx *, void *));
273 static int df_rtx_reg_replace PARAMS ((rtx *, void *));
274 void df_refs_reg_replace PARAMS ((struct df *, bitmap,
275 struct df_link *, rtx, rtx));
277 static int df_def_dominates_all_uses_p PARAMS((struct df *, struct ref *def));
278 static int df_def_dominates_uses_p PARAMS((struct df *,
279 struct ref *def, bitmap));
280 static struct ref *df_bb_regno_last_use_find PARAMS((struct df *, basic_block,
282 static struct ref *df_bb_regno_first_def_find PARAMS((struct df *, basic_block,
284 static struct ref *df_bb_insn_regno_last_use_find PARAMS((struct df *,
287 static struct ref *df_bb_insn_regno_first_def_find PARAMS((struct df *,
291 static void df_chain_dump PARAMS((struct df_link *, FILE *file));
292 static void df_chain_dump_regno PARAMS((struct df_link *, FILE *file));
293 static void df_regno_debug PARAMS ((struct df *, unsigned int, FILE *));
294 static void df_ref_debug PARAMS ((struct df *, struct ref *, FILE *));
295 static void df_rd_transfer_function PARAMS ((int, int *, bitmap, bitmap,
296 bitmap, bitmap, void *));
297 static void df_ru_transfer_function PARAMS ((int, int *, bitmap, bitmap,
298 bitmap, bitmap, void *));
299 static void df_lr_transfer_function PARAMS ((int, int *, bitmap, bitmap,
300 bitmap, bitmap, void *));
303 /* Local memory allocation/deallocation routines. */
306 /* Increase the insn info table by SIZE more elements. */
308 df_insn_table_realloc (df, size)
312 /* Make table 25 percent larger by default. */
314 size = df->insn_size / 4;
316 size += df->insn_size;
318 df->insns = (struct insn_info *)
319 xrealloc (df->insns, size * sizeof (struct insn_info));
321 memset (df->insns + df->insn_size, 0,
322 (size - df->insn_size) * sizeof (struct insn_info));
324 df->insn_size = size;
326 if (! df->insns_modified)
328 df->insns_modified = BITMAP_XMALLOC ();
329 bitmap_zero (df->insns_modified);
334 /* Increase the reg info table by SIZE more elements. */
336 df_reg_table_realloc (df, size)
340 /* Make table 25 percent larger by default. */
342 size = df->reg_size / 4;
344 size += df->reg_size;
346 df->regs = (struct reg_info *)
347 xrealloc (df->regs, size * sizeof (struct reg_info));
349 /* Zero the new entries. */
350 memset (df->regs + df->reg_size, 0,
351 (size - df->reg_size) * sizeof (struct reg_info));
358 /* Not currently used. */
360 df_def_table_realloc (df, size)
367 /* Make table 25 percent larger by default. */
369 size = df->def_size / 4;
371 df->def_size += size;
372 df->defs = xrealloc (df->defs,
373 df->def_size * sizeof (*df->defs));
375 /* Allocate a new block of memory and link into list of blocks
376 that will need to be freed later. */
378 refs = xmalloc (size * sizeof (*refs));
380 /* Link all the new refs together, overloading the chain field. */
381 for (i = 0; i < size - 1; i++)
382 refs[i].chain = (struct df_link *)(refs + i + 1);
383 refs[size - 1].chain = 0;
389 /* Allocate bitmaps for each basic block. */
391 df_bitmaps_alloc (df, flags)
398 /* Free the bitmaps if they need resizing. */
399 if ((flags & DF_LR) && df->n_regs < (unsigned int)max_reg_num ())
400 dflags |= DF_LR | DF_RU;
401 if ((flags & DF_RU) && df->n_uses < df->use_id)
403 if ((flags & DF_RD) && df->n_defs < df->def_id)
407 df_bitmaps_free (df, dflags);
409 df->n_defs = df->def_id;
410 df->n_uses = df->use_id;
412 for (i = 0; i < df->n_bbs; i++)
414 basic_block bb = BASIC_BLOCK (i);
415 struct bb_info *bb_info = DF_BB_INFO (df, bb);
417 if (flags & DF_RD && ! bb_info->rd_in)
419 /* Allocate bitmaps for reaching definitions. */
420 bb_info->rd_kill = BITMAP_XMALLOC ();
421 bitmap_zero (bb_info->rd_kill);
422 bb_info->rd_gen = BITMAP_XMALLOC ();
423 bitmap_zero (bb_info->rd_gen);
424 bb_info->rd_in = BITMAP_XMALLOC ();
425 bb_info->rd_out = BITMAP_XMALLOC ();
426 bb_info->rd_valid = 0;
429 if (flags & DF_RU && ! bb_info->ru_in)
431 /* Allocate bitmaps for upward exposed uses. */
432 bb_info->ru_kill = BITMAP_XMALLOC ();
433 bitmap_zero (bb_info->ru_kill);
434 /* Note the lack of symmetry. */
435 bb_info->ru_gen = BITMAP_XMALLOC ();
436 bitmap_zero (bb_info->ru_gen);
437 bb_info->ru_in = BITMAP_XMALLOC ();
438 bb_info->ru_out = BITMAP_XMALLOC ();
439 bb_info->ru_valid = 0;
442 if (flags & DF_LR && ! bb_info->lr_in)
444 /* Allocate bitmaps for live variables. */
445 bb_info->lr_def = BITMAP_XMALLOC ();
446 bitmap_zero (bb_info->lr_def);
447 bb_info->lr_use = BITMAP_XMALLOC ();
448 bitmap_zero (bb_info->lr_use);
449 bb_info->lr_in = BITMAP_XMALLOC ();
450 bb_info->lr_out = BITMAP_XMALLOC ();
451 bb_info->lr_valid = 0;
457 /* Free bitmaps for each basic block. */
459 df_bitmaps_free (df, flags)
460 struct df *df ATTRIBUTE_UNUSED;
465 for (i = 0; i < df->n_bbs; i++)
467 basic_block bb = BASIC_BLOCK (i);
468 struct bb_info *bb_info = DF_BB_INFO (df, bb);
473 if ((flags & DF_RD) && bb_info->rd_in)
475 /* Free bitmaps for reaching definitions. */
476 BITMAP_XFREE (bb_info->rd_kill);
477 bb_info->rd_kill = NULL;
478 BITMAP_XFREE (bb_info->rd_gen);
479 bb_info->rd_gen = NULL;
480 BITMAP_XFREE (bb_info->rd_in);
481 bb_info->rd_in = NULL;
482 BITMAP_XFREE (bb_info->rd_out);
483 bb_info->rd_out = NULL;
486 if ((flags & DF_RU) && bb_info->ru_in)
488 /* Free bitmaps for upward exposed uses. */
489 BITMAP_XFREE (bb_info->ru_kill);
490 bb_info->ru_kill = NULL;
491 BITMAP_XFREE (bb_info->ru_gen);
492 bb_info->ru_gen = NULL;
493 BITMAP_XFREE (bb_info->ru_in);
494 bb_info->ru_in = NULL;
495 BITMAP_XFREE (bb_info->ru_out);
496 bb_info->ru_out = NULL;
499 if ((flags & DF_LR) && bb_info->lr_in)
501 /* Free bitmaps for live variables. */
502 BITMAP_XFREE (bb_info->lr_def);
503 bb_info->lr_def = NULL;
504 BITMAP_XFREE (bb_info->lr_use);
505 bb_info->lr_use = NULL;
506 BITMAP_XFREE (bb_info->lr_in);
507 bb_info->lr_in = NULL;
508 BITMAP_XFREE (bb_info->lr_out);
509 bb_info->lr_out = NULL;
512 df->flags &= ~(flags & (DF_RD | DF_RU | DF_LR));
516 /* Allocate and initialise dataflow memory. */
518 df_alloc (df, n_regs)
525 gcc_obstack_init (&df_ref_obstack);
527 /* Perhaps we should use LUIDs to save memory for the insn_refs
528 table. This is only a small saving; a few pointers. */
529 n_insns = get_max_uid () + 1;
533 /* Approximate number of defs by number of insns. */
534 df->def_size = n_insns;
535 df->defs = xmalloc (df->def_size * sizeof (*df->defs));
539 /* Approximate number of uses by twice number of insns. */
540 df->use_size = n_insns * 2;
541 df->uses = xmalloc (df->use_size * sizeof (*df->uses));
544 df->n_bbs = n_basic_blocks;
546 /* Allocate temporary working array used during local dataflow analysis. */
547 df->reg_def_last = xmalloc (df->n_regs * sizeof (struct ref *));
549 df_insn_table_realloc (df, n_insns);
551 df_reg_table_realloc (df, df->n_regs);
553 df->bbs_modified = BITMAP_XMALLOC ();
554 bitmap_zero (df->bbs_modified);
558 df->bbs = xcalloc (df->n_bbs, sizeof (struct bb_info));
560 df->all_blocks = BITMAP_XMALLOC ();
561 for (i = 0; i < n_basic_blocks; i++)
562 bitmap_set_bit (df->all_blocks, i);
566 /* Free all the dataflow info. */
571 df_bitmaps_free (df, DF_ALL);
599 if (df->bbs_modified)
600 BITMAP_XFREE (df->bbs_modified);
601 df->bbs_modified = 0;
603 if (df->insns_modified)
604 BITMAP_XFREE (df->insns_modified);
605 df->insns_modified = 0;
607 BITMAP_XFREE (df->all_blocks);
610 obstack_free (&df_ref_obstack, NULL);
613 /* Local miscellaneous routines. */
615 /* Return a USE for register REGNO. */
616 static rtx df_reg_use_gen (regno)
622 reg = regno >= FIRST_PSEUDO_REGISTER
623 ? regno_reg_rtx[regno] : gen_rtx_REG (reg_raw_mode[regno], regno);
625 use = gen_rtx_USE (GET_MODE (reg), reg);
630 /* Return a CLOBBER for register REGNO. */
631 static rtx df_reg_clobber_gen (regno)
637 reg = regno >= FIRST_PSEUDO_REGISTER
638 ? regno_reg_rtx[regno] : gen_rtx_REG (reg_raw_mode[regno], regno);
640 use = gen_rtx_CLOBBER (GET_MODE (reg), reg);
644 /* Local chain manipulation routines. */
646 /* Create a link in a def-use or use-def chain. */
647 static inline struct df_link *
648 df_link_create (ref, next)
650 struct df_link *next;
652 struct df_link *link;
654 link = (struct df_link *) obstack_alloc (&df_ref_obstack,
662 /* Add REF to chain head pointed to by PHEAD. */
663 static struct df_link *
664 df_ref_unlink (phead, ref)
665 struct df_link **phead;
668 struct df_link *link = *phead;
674 /* Only a single ref. It must be the one we want.
675 If not, the def-use and use-def chains are likely to
677 if (link->ref != ref)
679 /* Now have an empty chain. */
684 /* Multiple refs. One of them must be us. */
685 if (link->ref == ref)
690 for (; link->next; link = link->next)
692 if (link->next->ref == ref)
694 /* Unlink from list. */
695 link->next = link->next->next;
706 /* Unlink REF from all def-use/use-def chains, etc. */
708 df_ref_remove (df, ref)
712 if (DF_REF_REG_DEF_P (ref))
714 df_def_unlink (df, ref);
715 df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].defs, ref);
719 df_use_unlink (df, ref);
720 df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].uses, ref);
726 /* Unlink DEF from use-def and reg-def chains. */
728 df_def_unlink (df, def)
729 struct df *df ATTRIBUTE_UNUSED;
732 struct df_link *du_link;
733 unsigned int dregno = DF_REF_REGNO (def);
735 /* Follow def-use chain to find all the uses of this def. */
736 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
738 struct ref *use = du_link->ref;
740 /* Unlink this def from the use-def chain. */
741 df_ref_unlink (&DF_REF_CHAIN (use), def);
743 DF_REF_CHAIN (def) = 0;
745 /* Unlink def from reg-def chain. */
746 df_ref_unlink (&df->regs[dregno].defs, def);
748 df->defs[DF_REF_ID (def)] = 0;
752 /* Unlink use from def-use and reg-use chains. */
754 df_use_unlink (df, use)
755 struct df *df ATTRIBUTE_UNUSED;
758 struct df_link *ud_link;
759 unsigned int uregno = DF_REF_REGNO (use);
761 /* Follow use-def chain to find all the defs of this use. */
762 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
764 struct ref *def = ud_link->ref;
766 /* Unlink this use from the def-use chain. */
767 df_ref_unlink (&DF_REF_CHAIN (def), use);
769 DF_REF_CHAIN (use) = 0;
771 /* Unlink use from reg-use chain. */
772 df_ref_unlink (&df->regs[uregno].uses, use);
774 df->uses[DF_REF_ID (use)] = 0;
777 /* Local routines for recording refs. */
780 /* Create a new ref of type DF_REF_TYPE for register REG at address
781 LOC within INSN of BB. */
783 df_ref_create (df, reg, loc, bb, insn, ref_type)
789 enum df_ref_type ref_type;
791 struct ref *this_ref;
794 this_ref = (struct ref *) obstack_alloc (&df_ref_obstack,
796 DF_REF_REG (this_ref) = reg;
797 DF_REF_LOC (this_ref) = loc;
798 DF_REF_BB (this_ref) = bb;
799 DF_REF_INSN (this_ref) = insn;
800 DF_REF_CHAIN (this_ref) = 0;
801 DF_REF_TYPE (this_ref) = ref_type;
802 uid = INSN_UID (insn);
804 if (ref_type == DF_REF_REG_DEF)
806 if (df->def_id >= df->def_size)
808 /* Make table 25 percent larger. */
809 df->def_size += (df->def_size / 4);
810 df->defs = xrealloc (df->defs,
811 df->def_size * sizeof (*df->defs));
813 DF_REF_ID (this_ref) = df->def_id;
814 df->defs[df->def_id++] = this_ref;
818 if (df->use_id >= df->use_size)
820 /* Make table 25 percent larger. */
821 df->use_size += (df->use_size / 4);
822 df->uses = xrealloc (df->uses,
823 df->use_size * sizeof (*df->uses));
825 DF_REF_ID (this_ref) = df->use_id;
826 df->uses[df->use_id++] = this_ref;
832 /* Create a new reference of type DF_REF_TYPE for a single register REG,
833 used inside the LOC rtx of INSN. */
835 df_ref_record_1 (df, reg, loc, bb, insn, ref_type)
841 enum df_ref_type ref_type;
843 df_ref_create (df, reg, loc, bb, insn, ref_type);
847 /* Create new references of type DF_REF_TYPE for each part of register REG
848 at address LOC within INSN of BB. */
850 df_ref_record (df, reg, loc, bb, insn, ref_type)
856 enum df_ref_type ref_type;
860 if (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG)
863 /* For the reg allocator we are interested in some SUBREG rtx's, but not
864 all. Notably only those representing a word extraction from a multi-word
865 reg. As written in the docu those should have the form
866 (subreg:SI (reg:M A) N), with size(SImode) > size(Mmode).
867 XXX Is that true? We could also use the global word_mode variable. */
868 if (GET_CODE (reg) == SUBREG
869 && (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (word_mode)
870 || GET_MODE_SIZE (GET_MODE (reg))
871 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (reg)))))
873 loc = &SUBREG_REG (reg);
877 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
878 if (regno < FIRST_PSEUDO_REGISTER)
883 if (! (df->flags & DF_HARD_REGS))
886 /* GET_MODE (reg) is correct here. We don't want to go into a SUBREG
887 for the mode, because we only want to add references to regs, which
888 are really referenced. E.g. a (subreg:SI (reg:DI 0) 0) does _not_
889 reference the whole reg 0 in DI mode (which would also include
890 reg 1, at least, if 0 and 1 are SImode registers). */
891 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
893 for (i = regno; i < endregno; i++)
894 df_ref_record_1 (df, gen_rtx_REG (reg_raw_mode[i], i),
895 loc, bb, insn, ref_type);
899 df_ref_record_1 (df, reg, loc, bb, insn, ref_type);
903 /* Process all the registers defined in the rtx, X. */
905 df_def_record_1 (df, x, bb, insn)
911 rtx *loc = &SET_DEST (x);
914 /* Some targets place small structures in registers for
915 return values of functions. */
916 if (GET_CODE (dst) == PARALLEL && GET_MODE (dst) == BLKmode)
920 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
921 df_def_record_1 (df, XVECEXP (dst, 0, i), bb, insn);
925 /* May be, we should flag the use of strict_low_part somehow. Might be
926 handy for the reg allocator. */
928 while (GET_CODE (dst) == STRICT_LOW_PART
929 || GET_CODE (dst) == ZERO_EXTRACT
930 || GET_CODE (dst) == SIGN_EXTRACT)
932 loc = &XEXP (dst, 0);
935 /* For the reg allocator we are interested in exact register references.
936 This means, we want to know, if only a part of a register is
939 if (GET_CODE (dst) == SUBREG)
941 loc = &XEXP (dst, 0);
946 while (GET_CODE (dst) == SUBREG
947 || GET_CODE (dst) == ZERO_EXTRACT
948 || GET_CODE (dst) == SIGN_EXTRACT
949 || GET_CODE (dst) == STRICT_LOW_PART)
951 loc = &XEXP (dst, 0);
956 if (GET_CODE (dst) == REG
957 || (GET_CODE (dst) == SUBREG && GET_CODE (SUBREG_REG (dst)) == REG))
958 df_ref_record (df, dst, loc, bb, insn, DF_REF_REG_DEF);
962 /* Process all the registers defined in the pattern rtx, X. */
964 df_defs_record (df, x, bb, insn)
970 RTX_CODE code = GET_CODE (x);
972 if (code == SET || code == CLOBBER)
974 /* Mark the single def within the pattern. */
975 df_def_record_1 (df, x, bb, insn);
977 else if (code == PARALLEL)
981 /* Mark the multiple defs within the pattern. */
982 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
984 code = GET_CODE (XVECEXP (x, 0, i));
985 if (code == SET || code == CLOBBER)
986 df_def_record_1 (df, XVECEXP (x, 0, i), bb, insn);
992 /* Process all the registers used in the rtx at address LOC. */
994 df_uses_record (df, loc, ref_type, bb, insn)
997 enum df_ref_type ref_type;
1006 code = GET_CODE (x);
1020 /* If we are clobbering a MEM, mark any registers inside the address
1022 if (GET_CODE (XEXP (x, 0)) == MEM)
1023 df_uses_record (df, &XEXP (XEXP (x, 0), 0),
1024 DF_REF_REG_MEM_STORE, bb, insn);
1026 /* If we're clobbering a REG then we have a def so ignore. */
1030 df_uses_record (df, &XEXP (x, 0), DF_REF_REG_MEM_LOAD, bb, insn);
1034 /* While we're here, optimize this case. */
1035 #if defined(HANDLE_SUBREG)
1037 /* In case the SUBREG is not of a register, don't optimize. */
1038 if (GET_CODE (SUBREG_REG (x)) != REG)
1040 loc = &SUBREG_REG (x);
1041 df_uses_record (df, loc, ref_type, bb, insn);
1046 loc = &SUBREG_REG (x);
1048 if (GET_CODE (x) != REG)
1050 df_uses_record (df, loc, ref_type, bb, insn);
1054 /* ... Fall through ... */
1057 /* See a register (or subreg) other than being set. */
1058 df_ref_record (df, x, loc, bb, insn, ref_type);
1063 rtx dst = SET_DEST (x);
1066 /* If storing into MEM, don't show it as being used. But do
1067 show the address as being used. */
1068 if (GET_CODE (dst) == MEM)
1070 df_uses_record (df, &XEXP (dst, 0),
1071 DF_REF_REG_MEM_STORE,
1073 df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn);
1077 #if 1 && defined(HANDLE_SUBREG)
1078 /* Look for sets that perform a read-modify-write. */
1079 while (GET_CODE (dst) == STRICT_LOW_PART
1080 || GET_CODE (dst) == ZERO_EXTRACT
1081 || GET_CODE (dst) == SIGN_EXTRACT)
1083 if (GET_CODE (dst) == STRICT_LOW_PART)
1085 dst = XEXP (dst, 0);
1086 if (GET_CODE (dst) != SUBREG)
1088 /* A strict_low_part uses the whole reg not only the subreg. */
1089 df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb, insn);
1093 df_uses_record (df, &XEXP (dst, 0), DF_REF_REG_USE, bb, insn);
1094 dst = XEXP (dst, 0);
1097 if (GET_CODE (dst) == SUBREG)
1099 /* Paradoxical or too small subreg's are read-mod-write. */
1100 if (GET_MODE_SIZE (GET_MODE (dst)) < GET_MODE_SIZE (word_mode)
1101 || GET_MODE_SIZE (GET_MODE (dst))
1102 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1105 /* In the original code also some SUBREG rtx's were considered
1106 read-modify-write (those with
1107 REG_SIZE(SUBREG_REG(dst)) > REG_SIZE(dst) )
1108 e.g. a (subreg:QI (reg:SI A) 0). I can't see this. The only
1109 reason for a read cycle for reg A would be to somehow preserve
1110 the bits outside of the subreg:QI. But for this a strict_low_part
1111 was necessary anyway, and this we handled already. */
1113 while (GET_CODE (dst) == STRICT_LOW_PART
1114 || GET_CODE (dst) == ZERO_EXTRACT
1115 || GET_CODE (dst) == SIGN_EXTRACT
1116 || GET_CODE (dst) == SUBREG)
1118 /* A SUBREG of a smaller size does not use the old value. */
1119 if (GET_CODE (dst) != SUBREG
1120 || (REG_SIZE (SUBREG_REG (dst)) > REG_SIZE (dst)))
1122 dst = XEXP (dst, 0);
1126 if ((GET_CODE (dst) == PARALLEL && GET_MODE (dst) == BLKmode)
1127 || GET_CODE (dst) == REG || GET_CODE (dst) == SUBREG)
1129 #if 1 || !defined(HANDLE_SUBREG)
1131 df_uses_record (df, &SET_DEST (x), DF_REF_REG_USE, bb, insn);
1133 df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn);
1143 case UNSPEC_VOLATILE:
1147 /* Traditional and volatile asm instructions must be considered to use
1148 and clobber all hard registers, all pseudo-registers and all of
1149 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1151 Consider for instance a volatile asm that changes the fpu rounding
1152 mode. An insn should not be moved across this even if it only uses
1153 pseudo-regs because it might give an incorrectly rounded result.
1155 For now, just mark any regs we can find in ASM_OPERANDS as
1158 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1159 We can not just fall through here since then we would be confused
1160 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1161 traditional asms unlike their normal usage. */
1162 if (code == ASM_OPERANDS)
1166 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1167 df_uses_record (df, &ASM_OPERANDS_INPUT (x, j),
1168 DF_REF_REG_USE, bb, insn);
1180 /* Catch the def of the register being modified. */
1181 df_ref_record (df, XEXP (x, 0), &XEXP (x, 0), bb, insn, DF_REF_REG_DEF);
1183 /* ... Fall through to handle uses ... */
1189 /* Recursively scan the operands of this expression. */
1191 const char *fmt = GET_RTX_FORMAT (code);
1194 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1198 /* Tail recursive case: save a function call level. */
1204 df_uses_record (df, &XEXP (x, i), ref_type, bb, insn);
1206 else if (fmt[i] == 'E')
1209 for (j = 0; j < XVECLEN (x, i); j++)
1210 df_uses_record (df, &XVECEXP (x, i, j), ref_type,
1218 /* Record all the df within INSN of basic block BB. */
1220 df_insn_refs_record (df, bb, insn)
1231 /* Record register defs */
1232 df_defs_record (df, PATTERN (insn), bb, insn);
1234 if (df->flags & DF_EQUIV_NOTES)
1235 for (note = REG_NOTES (insn); note;
1236 note = XEXP (note, 1))
1238 switch (REG_NOTE_KIND (note))
1242 df_uses_record (df, &XEXP (note, 0), DF_REF_REG_USE,
1249 if (GET_CODE (insn) == CALL_INSN)
1254 /* Record the registers used to pass arguments. */
1255 for (note = CALL_INSN_FUNCTION_USAGE (insn); note;
1256 note = XEXP (note, 1))
1258 if (GET_CODE (XEXP (note, 0)) == USE)
1259 df_uses_record (df, &SET_DEST (XEXP (note, 0)), DF_REF_REG_USE,
1263 /* The stack ptr is used (honorarily) by a CALL insn. */
1264 x = df_reg_use_gen (STACK_POINTER_REGNUM);
1265 df_uses_record (df, &SET_DEST (x), DF_REF_REG_USE, bb, insn);
1267 if (df->flags & DF_HARD_REGS)
1269 /* Calls may also reference any of the global registers,
1270 so they are recorded as used. */
1271 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1274 x = df_reg_use_gen (i);
1275 df_uses_record (df, &SET_DEST (x),
1276 DF_REF_REG_USE, bb, insn);
1281 /* Record the register uses. */
1282 df_uses_record (df, &PATTERN (insn),
1283 DF_REF_REG_USE, bb, insn);
1286 if (GET_CODE (insn) == CALL_INSN)
1290 if (df->flags & DF_HARD_REGS)
1292 /* Kill all registers invalidated by a call. */
1293 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1294 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1296 rtx reg_clob = df_reg_clobber_gen (i);
1297 df_defs_record (df, reg_clob, bb, insn);
1301 /* There may be extra registers to be clobbered. */
1302 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1304 note = XEXP (note, 1))
1305 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1306 df_defs_record (df, XEXP (note, 0), bb, insn);
1312 /* Record all the refs within the basic block BB. */
1314 df_bb_refs_record (df, bb)
1320 /* Scan the block an insn at a time from beginning to end. */
1321 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1325 /* Record defs within INSN. */
1326 df_insn_refs_record (df, bb, insn);
1328 if (insn == bb->end)
1334 /* Record all the refs in the basic blocks specified by BLOCKS. */
1336 df_refs_record (df, blocks)
1342 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1344 df_bb_refs_record (df, bb);
1348 /* Dataflow analysis routines. */
1351 /* Create reg-def chains for basic block BB. These are a list of
1352 definitions for each register. */
1354 df_bb_reg_def_chain_create (df, bb)
1360 /* Perhaps the defs should be sorted using a depth first search
1361 of the CFG (or possibly a breadth first search). We currently
1362 scan the basic blocks in reverse order so that the first defs
1363 apprear at the start of the chain. */
1365 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1366 insn = PREV_INSN (insn))
1368 struct df_link *link;
1369 unsigned int uid = INSN_UID (insn);
1371 if (! INSN_P (insn))
1374 for (link = df->insns[uid].defs; link; link = link->next)
1376 struct ref *def = link->ref;
1377 unsigned int dregno = DF_REF_REGNO (def);
1379 df->regs[dregno].defs
1380 = df_link_create (def, df->regs[dregno].defs);
1386 /* Create reg-def chains for each basic block within BLOCKS. These
1387 are a list of definitions for each register. */
1389 df_reg_def_chain_create (df, blocks)
1395 FOR_EACH_BB_IN_BITMAP/*_REV*/ (blocks, 0, bb,
1397 df_bb_reg_def_chain_create (df, bb);
1402 /* Create reg-use chains for basic block BB. These are a list of uses
1403 for each register. */
1405 df_bb_reg_use_chain_create (df, bb)
1411 /* Scan in forward order so that the last uses appear at the
1412 start of the chain. */
1414 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1415 insn = NEXT_INSN (insn))
1417 struct df_link *link;
1418 unsigned int uid = INSN_UID (insn);
1420 if (! INSN_P (insn))
1423 for (link = df->insns[uid].uses; link; link = link->next)
1425 struct ref *use = link->ref;
1426 unsigned int uregno = DF_REF_REGNO (use);
1428 df->regs[uregno].uses
1429 = df_link_create (use, df->regs[uregno].uses);
1435 /* Create reg-use chains for each basic block within BLOCKS. These
1436 are a list of uses for each register. */
1438 df_reg_use_chain_create (df, blocks)
1444 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1446 df_bb_reg_use_chain_create (df, bb);
1451 /* Create def-use chains from reaching use bitmaps for basic block BB. */
1453 df_bb_du_chain_create (df, bb, ru)
1458 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1461 bitmap_copy (ru, bb_info->ru_out);
1463 /* For each def in BB create a linked list (chain) of uses
1464 reached from the def. */
1465 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1466 insn = PREV_INSN (insn))
1468 struct df_link *def_link;
1469 struct df_link *use_link;
1470 unsigned int uid = INSN_UID (insn);
1472 if (! INSN_P (insn))
1475 /* For each def in insn... */
1476 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1478 struct ref *def = def_link->ref;
1479 unsigned int dregno = DF_REF_REGNO (def);
1481 DF_REF_CHAIN (def) = 0;
1483 /* While the reg-use chains are not essential, it
1484 is _much_ faster to search these short lists rather
1485 than all the reaching uses, especially for large functions. */
1486 for (use_link = df->regs[dregno].uses; use_link;
1487 use_link = use_link->next)
1489 struct ref *use = use_link->ref;
1491 if (bitmap_bit_p (ru, DF_REF_ID (use)))
1494 = df_link_create (use, DF_REF_CHAIN (def));
1496 bitmap_clear_bit (ru, DF_REF_ID (use));
1501 /* For each use in insn... */
1502 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1504 struct ref *use = use_link->ref;
1505 bitmap_set_bit (ru, DF_REF_ID (use));
1511 /* Create def-use chains from reaching use bitmaps for basic blocks
1514 df_du_chain_create (df, blocks)
1521 ru = BITMAP_XMALLOC ();
1523 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1525 df_bb_du_chain_create (df, bb, ru);
1532 /* Create use-def chains from reaching def bitmaps for basic block BB. */
1534 df_bb_ud_chain_create (df, bb)
1538 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1539 struct ref **reg_def_last = df->reg_def_last;
1542 memset (reg_def_last, 0, df->n_regs * sizeof (struct ref *));
1544 /* For each use in BB create a linked list (chain) of defs
1545 that reach the use. */
1546 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1547 insn = NEXT_INSN (insn))
1549 unsigned int uid = INSN_UID (insn);
1550 struct df_link *use_link;
1551 struct df_link *def_link;
1553 if (! INSN_P (insn))
1556 /* For each use in insn... */
1557 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1559 struct ref *use = use_link->ref;
1560 unsigned int regno = DF_REF_REGNO (use);
1562 DF_REF_CHAIN (use) = 0;
1564 /* Has regno been defined in this BB yet? If so, use
1565 the last def as the single entry for the use-def
1566 chain for this use. Otherwise, we need to add all
1567 the defs using this regno that reach the start of
1569 if (reg_def_last[regno])
1572 = df_link_create (reg_def_last[regno], 0);
1576 /* While the reg-def chains are not essential, it is
1577 _much_ faster to search these short lists rather than
1578 all the reaching defs, especially for large
1580 for (def_link = df->regs[regno].defs; def_link;
1581 def_link = def_link->next)
1583 struct ref *def = def_link->ref;
1585 if (bitmap_bit_p (bb_info->rd_in, DF_REF_ID (def)))
1588 = df_link_create (def, DF_REF_CHAIN (use));
1595 /* For each def in insn...record the last def of each reg. */
1596 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1598 struct ref *def = def_link->ref;
1599 int dregno = DF_REF_REGNO (def);
1601 reg_def_last[dregno] = def;
1607 /* Create use-def chains from reaching def bitmaps for basic blocks
1610 df_ud_chain_create (df, blocks)
1616 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1618 df_bb_ud_chain_create (df, bb);
1625 df_rd_transfer_function (bb, changed, in, out, gen, kill, data)
1626 int bb ATTRIBUTE_UNUSED;
1628 bitmap in, out, gen, kill;
1629 void *data ATTRIBUTE_UNUSED;
1631 *changed = bitmap_union_of_diff (out, gen, in, kill);
1634 df_ru_transfer_function (bb, changed, in, out, gen, kill, data)
1635 int bb ATTRIBUTE_UNUSED;
1637 bitmap in, out, gen, kill;
1638 void *data ATTRIBUTE_UNUSED;
1640 *changed = bitmap_union_of_diff (in, gen, out, kill);
1644 df_lr_transfer_function (bb, changed, in, out, use, def, data)
1645 int bb ATTRIBUTE_UNUSED;
1647 bitmap in, out, use, def;
1648 void *data ATTRIBUTE_UNUSED;
1650 *changed = bitmap_union_of_diff (in, use, out, def);
1654 /* Compute local reaching def info for basic block BB. */
1656 df_bb_rd_local_compute (df, bb)
1660 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1663 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1664 insn = NEXT_INSN (insn))
1666 unsigned int uid = INSN_UID (insn);
1667 struct df_link *def_link;
1669 if (! INSN_P (insn))
1672 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1674 struct ref *def = def_link->ref;
1675 unsigned int regno = DF_REF_REGNO (def);
1676 struct df_link *def2_link;
1678 for (def2_link = df->regs[regno].defs; def2_link;
1679 def2_link = def2_link->next)
1681 struct ref *def2 = def2_link->ref;
1683 /* Add all defs of this reg to the set of kills. This
1684 is greedy since many of these defs will not actually
1685 be killed by this BB but it keeps things a lot
1687 bitmap_set_bit (bb_info->rd_kill, DF_REF_ID (def2));
1689 /* Zap from the set of gens for this BB. */
1690 bitmap_clear_bit (bb_info->rd_gen, DF_REF_ID (def2));
1693 bitmap_set_bit (bb_info->rd_gen, DF_REF_ID (def));
1697 bb_info->rd_valid = 1;
1701 /* Compute local reaching def info for each basic block within BLOCKS. */
1703 df_rd_local_compute (df, blocks)
1709 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1711 df_bb_rd_local_compute (df, bb);
1716 /* Compute local reaching use (upward exposed use) info for basic
1719 df_bb_ru_local_compute (df, bb)
1723 /* This is much more tricky than computing reaching defs. With
1724 reaching defs, defs get killed by other defs. With upwards
1725 exposed uses, these get killed by defs with the same regno. */
1727 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1731 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1732 insn = PREV_INSN (insn))
1734 unsigned int uid = INSN_UID (insn);
1735 struct df_link *def_link;
1736 struct df_link *use_link;
1738 if (! INSN_P (insn))
1741 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1743 struct ref *def = def_link->ref;
1744 unsigned int dregno = DF_REF_REGNO (def);
1746 for (use_link = df->regs[dregno].uses; use_link;
1747 use_link = use_link->next)
1749 struct ref *use = use_link->ref;
1751 /* Add all uses of this reg to the set of kills. This
1752 is greedy since many of these uses will not actually
1753 be killed by this BB but it keeps things a lot
1755 bitmap_set_bit (bb_info->ru_kill, DF_REF_ID (use));
1757 /* Zap from the set of gens for this BB. */
1758 bitmap_clear_bit (bb_info->ru_gen, DF_REF_ID (use));
1762 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1764 struct ref *use = use_link->ref;
1765 /* Add use to set of gens in this BB. */
1766 bitmap_set_bit (bb_info->ru_gen, DF_REF_ID (use));
1769 bb_info->ru_valid = 1;
1773 /* Compute local reaching use (upward exposed use) info for each basic
1774 block within BLOCKS. */
1776 df_ru_local_compute (df, blocks)
1782 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1784 df_bb_ru_local_compute (df, bb);
1789 /* Compute local live variable info for basic block BB. */
1791 df_bb_lr_local_compute (df, bb)
1795 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1798 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1799 insn = PREV_INSN (insn))
1801 unsigned int uid = INSN_UID (insn);
1802 struct df_link *link;
1804 if (! INSN_P (insn))
1807 for (link = df->insns[uid].defs; link; link = link->next)
1809 struct ref *def = link->ref;
1810 unsigned int dregno = DF_REF_REGNO (def);
1812 /* Add def to set of defs in this BB. */
1813 bitmap_set_bit (bb_info->lr_def, dregno);
1815 bitmap_clear_bit (bb_info->lr_use, dregno);
1818 for (link = df->insns[uid].uses; link; link = link->next)
1820 struct ref *use = link->ref;
1821 /* Add use to set of uses in this BB. */
1822 bitmap_set_bit (bb_info->lr_use, DF_REF_REGNO (use));
1825 bb_info->lr_valid = 1;
1829 /* Compute local live variable info for each basic block within BLOCKS. */
1831 df_lr_local_compute (df, blocks)
1837 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1839 df_bb_lr_local_compute (df, bb);
1844 /* Compute register info: lifetime, bb, and number of defs and uses
1845 for basic block BB. */
1847 df_bb_reg_info_compute (df, bb, live)
1852 struct reg_info *reg_info = df->regs;
1853 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1856 bitmap_copy (live, bb_info->lr_out);
1858 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1859 insn = PREV_INSN (insn))
1861 unsigned int uid = INSN_UID (insn);
1863 struct df_link *link;
1865 if (! INSN_P (insn))
1868 for (link = df->insns[uid].defs; link; link = link->next)
1870 struct ref *def = link->ref;
1871 unsigned int dregno = DF_REF_REGNO (def);
1873 /* Kill this register. */
1874 bitmap_clear_bit (live, dregno);
1875 reg_info[dregno].n_defs++;
1878 for (link = df->insns[uid].uses; link; link = link->next)
1880 struct ref *use = link->ref;
1881 unsigned int uregno = DF_REF_REGNO (use);
1883 /* This register is now live. */
1884 bitmap_set_bit (live, uregno);
1885 reg_info[uregno].n_uses++;
1888 /* Increment lifetimes of all live registers. */
1889 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno,
1891 reg_info[regno].lifetime++;
1897 /* Compute register info: lifetime, bb, and number of defs and uses. */
1899 df_reg_info_compute (df, blocks)
1906 live = BITMAP_XMALLOC ();
1908 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1910 df_bb_reg_info_compute (df, bb, live);
1913 BITMAP_XFREE (live);
1917 /* Assign LUIDs for BB. */
1919 df_bb_luids_set (df, bb)
1926 /* The LUIDs are monotonically increasing for each basic block. */
1928 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1931 DF_INSN_LUID (df, insn) = luid++;
1932 DF_INSN_LUID (df, insn) = luid;
1934 if (insn == bb->end)
1941 /* Assign LUIDs for each basic block within BLOCKS. */
1943 df_luids_set (df, blocks)
1950 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1952 total += df_bb_luids_set (df, bb);
1958 /* Perform dataflow analysis using existing DF structure for blocks
1959 within BLOCKS. If BLOCKS is zero, use all basic blocks in the CFG. */
1961 df_analyse_1 (df, blocks, flags, update)
1972 if (flags & DF_UD_CHAIN)
1973 aflags |= DF_RD | DF_RD_CHAIN;
1975 if (flags & DF_DU_CHAIN)
1979 aflags |= DF_RU_CHAIN;
1981 if (flags & DF_REG_INFO)
1985 blocks = df->all_blocks;
1990 df_refs_update (df);
1991 /* More fine grained incremental dataflow analysis would be
1992 nice. For now recompute the whole shebang for the
1995 df_refs_unlink (df, blocks);
1997 /* All the def-use, use-def chains can be potentially
1998 modified by changes in one block. The size of the
1999 bitmaps can also change. */
2003 /* Scan the function for all register defs and uses. */
2005 df_refs_record (df, blocks);
2007 /* Link all the new defs and uses to the insns. */
2008 df_refs_process (df);
2011 /* Allocate the bitmaps now the total number of defs and uses are
2012 known. If the number of defs or uses have changed, then
2013 these bitmaps need to be reallocated. */
2014 df_bitmaps_alloc (df, aflags);
2016 /* Set the LUIDs for each specified basic block. */
2017 df_luids_set (df, blocks);
2019 /* Recreate reg-def and reg-use chains from scratch so that first
2020 def is at the head of the reg-def chain and the last use is at
2021 the head of the reg-use chain. This is only important for
2022 regs local to a basic block as it speeds up searching. */
2023 if (aflags & DF_RD_CHAIN)
2025 df_reg_def_chain_create (df, blocks);
2028 if (aflags & DF_RU_CHAIN)
2030 df_reg_use_chain_create (df, blocks);
2033 df->dfs_order = xmalloc (sizeof(int) * n_basic_blocks);
2034 df->rc_order = xmalloc (sizeof(int) * n_basic_blocks);
2035 df->rts_order = xmalloc (sizeof(int) * n_basic_blocks);
2036 df->inverse_dfs_map = xmalloc (sizeof(int) * n_basic_blocks);
2037 df->inverse_rc_map = xmalloc (sizeof(int) * n_basic_blocks);
2038 df->inverse_rts_map = xmalloc (sizeof(int) * n_basic_blocks);
2040 flow_depth_first_order_compute (df->dfs_order, df->rc_order);
2041 flow_reverse_top_sort_order_compute (df->rts_order);
2042 for (i = 0; i < n_basic_blocks; i ++)
2044 df->inverse_dfs_map[df->dfs_order[i]] = i;
2045 df->inverse_rc_map[df->rc_order[i]] = i;
2046 df->inverse_rts_map[df->rts_order[i]] = i;
2050 /* Compute the sets of gens and kills for the defs of each bb. */
2051 df_rd_local_compute (df, df->flags & DF_RD ? blocks : df->all_blocks);
2054 bitmap *in = xmalloc (sizeof (bitmap) * n_basic_blocks);
2055 bitmap *out = xmalloc (sizeof (bitmap) * n_basic_blocks);
2056 bitmap *gen = xmalloc (sizeof (bitmap) * n_basic_blocks);
2057 bitmap *kill = xmalloc (sizeof (bitmap) * n_basic_blocks);
2058 for (i = 0; i < n_basic_blocks; i ++)
2060 in[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->rd_in;
2061 out[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->rd_out;
2062 gen[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->rd_gen;
2063 kill[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->rd_kill;
2065 iterative_dataflow_bitmap (in, out, gen, kill, df->all_blocks,
2066 FORWARD, UNION, df_rd_transfer_function,
2067 df->inverse_rc_map, NULL);
2075 if (aflags & DF_UD_CHAIN)
2077 /* Create use-def chains. */
2078 df_ud_chain_create (df, df->all_blocks);
2080 if (! (flags & DF_RD))
2086 /* Compute the sets of gens and kills for the upwards exposed
2088 df_ru_local_compute (df, df->flags & DF_RU ? blocks : df->all_blocks);
2091 bitmap *in = xmalloc (sizeof (bitmap) * n_basic_blocks);
2092 bitmap *out = xmalloc (sizeof (bitmap) * n_basic_blocks);
2093 bitmap *gen = xmalloc (sizeof (bitmap) * n_basic_blocks);
2094 bitmap *kill = xmalloc (sizeof (bitmap) * n_basic_blocks);
2095 for (i = 0; i < n_basic_blocks; i ++)
2097 in[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->ru_in;
2098 out[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->ru_out;
2099 gen[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->ru_gen;
2100 kill[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->ru_kill;
2102 iterative_dataflow_bitmap (in, out, gen, kill, df->all_blocks,
2103 BACKWARD, UNION, df_ru_transfer_function,
2104 df->inverse_rts_map, NULL);
2112 if (aflags & DF_DU_CHAIN)
2114 /* Create def-use chains. */
2115 df_du_chain_create (df, df->all_blocks);
2117 if (! (flags & DF_RU))
2121 /* Free up bitmaps that are no longer required. */
2123 df_bitmaps_free (df, dflags);
2127 /* Compute the sets of defs and uses of live variables. */
2128 df_lr_local_compute (df, df->flags & DF_LR ? blocks : df->all_blocks);
2131 bitmap *in = xmalloc (sizeof (bitmap) * n_basic_blocks);
2132 bitmap *out = xmalloc (sizeof (bitmap) * n_basic_blocks);
2133 bitmap *use = xmalloc (sizeof (bitmap) * n_basic_blocks);
2134 bitmap *def = xmalloc (sizeof (bitmap) * n_basic_blocks);
2135 for (i = 0; i < n_basic_blocks; i ++)
2137 in[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->lr_in;
2138 out[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->lr_out;
2139 use[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->lr_use;
2140 def[i] = DF_BB_INFO (df, BASIC_BLOCK (i))->lr_def;
2142 iterative_dataflow_bitmap (in, out, use, def, df->all_blocks,
2143 BACKWARD, UNION, df_lr_transfer_function,
2144 df->inverse_rts_map, NULL);
2152 if (aflags & DF_REG_INFO)
2154 df_reg_info_compute (df, df->all_blocks);
2156 free (df->dfs_order);
2157 free (df->rc_order);
2158 free (df->rts_order);
2159 free (df->inverse_rc_map);
2160 free (df->inverse_dfs_map);
2161 free (df->inverse_rts_map);
2165 /* Initialise dataflow analysis. */
2171 df = xcalloc (1, sizeof (struct df));
2173 /* Squirrel away a global for debugging. */
2180 /* Start queuing refs. */
2185 df->def_id_save = df->def_id;
2186 df->use_id_save = df->use_id;
2187 /* ???? Perhaps we should save current obstack state so that we can
2193 /* Process queued refs. */
2195 df_refs_process (df)
2200 /* Build new insn-def chains. */
2201 for (i = df->def_id_save; i != df->def_id; i++)
2203 struct ref *def = df->defs[i];
2204 unsigned int uid = DF_REF_INSN_UID (def);
2206 /* Add def to head of def list for INSN. */
2208 = df_link_create (def, df->insns[uid].defs);
2211 /* Build new insn-use chains. */
2212 for (i = df->use_id_save; i != df->use_id; i++)
2214 struct ref *use = df->uses[i];
2215 unsigned int uid = DF_REF_INSN_UID (use);
2217 /* Add use to head of use list for INSN. */
2219 = df_link_create (use, df->insns[uid].uses);
2225 /* Update refs for basic block BB. */
2227 df_bb_refs_update (df, bb)
2234 /* While we have to scan the chain of insns for this BB, we don't
2235 need to allocate and queue a long chain of BB/INSN pairs. Using
2236 a bitmap for insns_modified saves memory and avoids queuing
2239 for (insn = bb->head; ; insn = NEXT_INSN (insn))
2243 uid = INSN_UID (insn);
2245 if (bitmap_bit_p (df->insns_modified, uid))
2247 /* Delete any allocated refs of this insn. MPH, FIXME. */
2248 df_insn_refs_unlink (df, bb, insn);
2250 /* Scan the insn for refs. */
2251 df_insn_refs_record (df, bb, insn);
2254 bitmap_clear_bit (df->insns_modified, uid);
2257 if (insn == bb->end)
2264 /* Process all the modified/deleted insns that were queued. */
2272 if ((unsigned int)max_reg_num () >= df->reg_size)
2273 df_reg_table_realloc (df, 0);
2277 FOR_EACH_BB_IN_BITMAP (df->bbs_modified, 0, bb,
2279 count += df_bb_refs_update (df, bb);
2282 df_refs_process (df);
2287 /* Return non-zero if any of the requested blocks in the bitmap
2288 BLOCKS have been modified. */
2290 df_modified_p (df, blocks)
2297 for (j = 0; j < df->n_bbs; j++)
2298 if (bitmap_bit_p (df->bbs_modified, j)
2299 && (! blocks || (blocks == (bitmap) -1) || bitmap_bit_p (blocks, j)))
2309 /* Analyse dataflow info for the basic blocks specified by the bitmap
2310 BLOCKS, or for the whole CFG if BLOCKS is zero, or just for the
2311 modified blocks if BLOCKS is -1. */
2313 df_analyse (df, blocks, flags)
2320 /* We could deal with additional basic blocks being created by
2321 rescanning everything again. */
2322 if (df->n_bbs && df->n_bbs != (unsigned int)n_basic_blocks)
2325 update = df_modified_p (df, blocks);
2326 if (update || (flags != df->flags))
2332 /* Recompute everything from scratch. */
2335 /* Allocate and initialise data structures. */
2336 df_alloc (df, max_reg_num ());
2337 df_analyse_1 (df, 0, flags, 0);
2342 if (blocks == (bitmap) -1)
2343 blocks = df->bbs_modified;
2348 df_analyse_1 (df, blocks, flags, 1);
2349 bitmap_zero (df->bbs_modified);
2356 /* Free all the dataflow info and the DF structure. */
2366 /* Unlink INSN from its reference information. */
2368 df_insn_refs_unlink (df, bb, insn)
2370 basic_block bb ATTRIBUTE_UNUSED;
2373 struct df_link *link;
2376 uid = INSN_UID (insn);
2378 /* Unlink all refs defined by this insn. */
2379 for (link = df->insns[uid].defs; link; link = link->next)
2380 df_def_unlink (df, link->ref);
2382 /* Unlink all refs used by this insn. */
2383 for (link = df->insns[uid].uses; link; link = link->next)
2384 df_use_unlink (df, link->ref);
2386 df->insns[uid].defs = 0;
2387 df->insns[uid].uses = 0;
2392 /* Unlink all the insns within BB from their reference information. */
2394 df_bb_refs_unlink (df, bb)
2400 /* Scan the block an insn at a time from beginning to end. */
2401 for (insn = bb->head; ; insn = NEXT_INSN (insn))
2405 /* Unlink refs for INSN. */
2406 df_insn_refs_unlink (df, bb, insn);
2408 if (insn == bb->end)
2414 /* Unlink all the refs in the basic blocks specified by BLOCKS.
2415 Not currently used. */
2417 df_refs_unlink (df, blocks)
2425 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2427 df_bb_refs_unlink (df, bb);
2434 df_bb_refs_unlink (df, bb);
2440 /* Functions to modify insns. */
2443 /* Delete INSN and all its reference information. */
2445 df_insn_delete (df, bb, insn)
2447 basic_block bb ATTRIBUTE_UNUSED;
2450 /* If the insn is a jump, we should perhaps call delete_insn to
2451 handle the JUMP_LABEL? */
2453 /* We should not be deleting the NOTE_INSN_BASIC_BLOCK or label. */
2454 if (insn == bb->head)
2457 /* Delete the insn. */
2460 df_insn_modify (df, bb, insn);
2462 return NEXT_INSN (insn);
2466 /* Mark that INSN within BB may have changed (created/modified/deleted).
2467 This may be called multiple times for the same insn. There is no
2468 harm calling this function if the insn wasn't changed; it will just
2469 slow down the rescanning of refs. */
2471 df_insn_modify (df, bb, insn)
2478 uid = INSN_UID (insn);
2480 if (uid >= df->insn_size)
2481 df_insn_table_realloc (df, 0);
2483 bitmap_set_bit (df->bbs_modified, bb->index);
2484 bitmap_set_bit (df->insns_modified, uid);
2486 /* For incremental updating on the fly, perhaps we could make a copy
2487 of all the refs of the original insn and turn them into
2488 anti-refs. When df_refs_update finds these anti-refs, it annihilates
2489 the original refs. If validate_change fails then these anti-refs
2490 will just get ignored. */
2494 typedef struct replace_args
2503 /* Replace mem pointed to by PX with its associated pseudo register.
2504 DATA is actually a pointer to a structure describing the
2505 instruction currently being scanned and the MEM we are currently
2508 df_rtx_mem_replace (px, data)
2512 replace_args *args = (replace_args *) data;
2515 if (mem == NULL_RTX)
2518 switch (GET_CODE (mem))
2524 /* We're not interested in the MEM associated with a
2525 CONST_DOUBLE, so there's no need to traverse into one. */
2529 /* This is not a MEM. */
2533 if (!rtx_equal_p (args->match, mem))
2534 /* This is not the MEM we are currently replacing. */
2537 /* Actually replace the MEM. */
2538 validate_change (args->insn, px, args->replacement, 1);
2546 df_insn_mem_replace (df, bb, insn, mem, reg)
2557 args.replacement = reg;
2560 /* Seach and replace all matching mems within insn. */
2561 for_each_rtx (&insn, df_rtx_mem_replace, &args);
2564 df_insn_modify (df, bb, insn);
2566 /* ???? FIXME. We may have a new def or one or more new uses of REG
2567 in INSN. REG should be a new pseudo so it won't affect the
2568 dataflow information that we currently have. We should add
2569 the new uses and defs to INSN and then recreate the chains
2570 when df_analyse is called. */
2571 return args.modified;
2575 /* Replace one register with another. Called through for_each_rtx; PX
2576 points to the rtx being scanned. DATA is actually a pointer to a
2577 structure of arguments. */
2579 df_rtx_reg_replace (px, data)
2584 replace_args *args = (replace_args *) data;
2589 if (x == args->match)
2591 validate_change (args->insn, px, args->replacement, 1);
2599 /* Replace the reg within every ref on CHAIN that is within the set
2600 BLOCKS of basic blocks with NEWREG. Also update the regs within
2603 df_refs_reg_replace (df, blocks, chain, oldreg, newreg)
2606 struct df_link *chain;
2610 struct df_link *link;
2614 blocks = df->all_blocks;
2616 args.match = oldreg;
2617 args.replacement = newreg;
2620 for (link = chain; link; link = link->next)
2622 struct ref *ref = link->ref;
2623 rtx insn = DF_REF_INSN (ref);
2625 if (! INSN_P (insn))
2628 if (bitmap_bit_p (blocks, DF_REF_BBNO (ref)))
2630 df_ref_reg_replace (df, ref, oldreg, newreg);
2632 /* Replace occurrences of the reg within the REG_NOTES. */
2633 if ((! link->next || DF_REF_INSN (ref)
2634 != DF_REF_INSN (link->next->ref))
2635 && REG_NOTES (insn))
2638 for_each_rtx (®_NOTES (insn), df_rtx_reg_replace, &args);
2643 /* Temporary check to ensure that we have a grip on which
2644 regs should be replaced. */
2651 /* Replace all occurrences of register OLDREG with register NEWREG in
2652 blocks defined by bitmap BLOCKS. This also replaces occurrences of
2653 OLDREG in the REG_NOTES but only for insns containing OLDREG. This
2654 routine expects the reg-use and reg-def chains to be valid. */
2656 df_reg_replace (df, blocks, oldreg, newreg)
2662 unsigned int oldregno = REGNO (oldreg);
2664 df_refs_reg_replace (df, blocks, df->regs[oldregno].defs, oldreg, newreg);
2665 df_refs_reg_replace (df, blocks, df->regs[oldregno].uses, oldreg, newreg);
2670 /* Try replacing the reg within REF with NEWREG. Do not modify
2671 def-use/use-def chains. */
2673 df_ref_reg_replace (df, ref, oldreg, newreg)
2679 /* Check that insn was deleted by being converted into a NOTE. If
2680 so ignore this insn. */
2681 if (! INSN_P (DF_REF_INSN (ref)))
2684 if (oldreg && oldreg != DF_REF_REG (ref))
2687 if (! validate_change (DF_REF_INSN (ref), DF_REF_LOC (ref), newreg, 1))
2690 df_insn_modify (df, DF_REF_BB (ref), DF_REF_INSN (ref));
2696 df_bb_def_use_swap (df, bb, def_insn, use_insn, regno)
2707 struct df_link *link;
2709 def = df_bb_insn_regno_first_def_find (df, bb, def_insn, regno);
2713 use = df_bb_insn_regno_last_use_find (df, bb, use_insn, regno);
2717 /* The USE no longer exists. */
2718 use_uid = INSN_UID (use_insn);
2719 df_use_unlink (df, use);
2720 df_ref_unlink (&df->insns[use_uid].uses, use);
2722 /* The DEF requires shifting so remove it from DEF_INSN
2723 and add it to USE_INSN by reusing LINK. */
2724 def_uid = INSN_UID (def_insn);
2725 link = df_ref_unlink (&df->insns[def_uid].defs, def);
2727 link->next = df->insns[use_uid].defs;
2728 df->insns[use_uid].defs = link;
2731 link = df_ref_unlink (&df->regs[regno].defs, def);
2733 link->next = df->regs[regno].defs;
2734 df->insns[regno].defs = link;
2737 DF_REF_INSN (def) = use_insn;
2742 /* Record df between FIRST_INSN and LAST_INSN inclusive. All new
2743 insns must be processed by this routine. */
2745 df_insns_modify (df, bb, first_insn, last_insn)
2753 for (insn = first_insn; ; insn = NEXT_INSN (insn))
2757 /* A non-const call should not have slipped through the net. If
2758 it does, we need to create a new basic block. Ouch. The
2759 same applies for a label. */
2760 if ((GET_CODE (insn) == CALL_INSN
2761 && ! CONST_OR_PURE_CALL_P (insn))
2762 || GET_CODE (insn) == CODE_LABEL)
2765 uid = INSN_UID (insn);
2767 if (uid >= df->insn_size)
2768 df_insn_table_realloc (df, 0);
2770 df_insn_modify (df, bb, insn);
2772 if (insn == last_insn)
2778 /* Emit PATTERN before INSN within BB. */
2780 df_pattern_emit_before (df, pattern, bb, insn)
2781 struct df *df ATTRIBUTE_UNUSED;
2787 rtx prev_insn = PREV_INSN (insn);
2789 /* We should not be inserting before the start of the block. */
2790 if (insn == bb->head)
2792 ret_insn = emit_insn_before (pattern, insn);
2793 if (ret_insn == insn)
2796 df_insns_modify (df, bb, NEXT_INSN (prev_insn), ret_insn);
2801 /* Emit PATTERN after INSN within BB. */
2803 df_pattern_emit_after (df, pattern, bb, insn)
2811 ret_insn = emit_insn_after (pattern, insn);
2812 if (ret_insn == insn)
2815 df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
2820 /* Emit jump PATTERN after INSN within BB. */
2822 df_jump_pattern_emit_after (df, pattern, bb, insn)
2830 ret_insn = emit_jump_insn_after (pattern, insn);
2831 if (ret_insn == insn)
2834 df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
2839 /* Move INSN within BB before BEFORE_INSN within BEFORE_BB.
2841 This function should only be used to move loop invariant insns
2842 out of a loop where it has been proven that the def-use info
2843 will still be valid. */
2845 df_insn_move_before (df, bb, insn, before_bb, before_insn)
2849 basic_block before_bb;
2852 struct df_link *link;
2856 return df_pattern_emit_before (df, insn, before_bb, before_insn);
2858 uid = INSN_UID (insn);
2860 /* Change bb for all df defined and used by this insn. */
2861 for (link = df->insns[uid].defs; link; link = link->next)
2862 DF_REF_BB (link->ref) = before_bb;
2863 for (link = df->insns[uid].uses; link; link = link->next)
2864 DF_REF_BB (link->ref) = before_bb;
2866 /* The lifetimes of the registers used in this insn will be reduced
2867 while the lifetimes of the registers defined in this insn
2868 are likely to be increased. */
2870 /* ???? Perhaps all the insns moved should be stored on a list
2871 which df_analyse removes when it recalculates data flow. */
2873 return emit_insn_before (insn, before_insn);
2876 /* Functions to query dataflow information. */
2880 df_insn_regno_def_p (df, bb, insn, regno)
2882 basic_block bb ATTRIBUTE_UNUSED;
2887 struct df_link *link;
2889 uid = INSN_UID (insn);
2891 for (link = df->insns[uid].defs; link; link = link->next)
2893 struct ref *def = link->ref;
2895 if (DF_REF_REGNO (def) == regno)
2904 df_def_dominates_all_uses_p (df, def)
2905 struct df *df ATTRIBUTE_UNUSED;
2908 struct df_link *du_link;
2910 /* Follow def-use chain to find all the uses of this def. */
2911 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
2913 struct ref *use = du_link->ref;
2914 struct df_link *ud_link;
2916 /* Follow use-def chain to check all the defs for this use. */
2917 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
2918 if (ud_link->ref != def)
2926 df_insn_dominates_all_uses_p (df, bb, insn)
2928 basic_block bb ATTRIBUTE_UNUSED;
2932 struct df_link *link;
2934 uid = INSN_UID (insn);
2936 for (link = df->insns[uid].defs; link; link = link->next)
2938 struct ref *def = link->ref;
2940 if (! df_def_dominates_all_uses_p (df, def))
2948 /* Return non-zero if all DF dominates all the uses within the bitmap
2951 df_def_dominates_uses_p (df, def, blocks)
2952 struct df *df ATTRIBUTE_UNUSED;
2956 struct df_link *du_link;
2958 /* Follow def-use chain to find all the uses of this def. */
2959 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
2961 struct ref *use = du_link->ref;
2962 struct df_link *ud_link;
2964 /* Only worry about the uses within BLOCKS. For example,
2965 consider a register defined within a loop that is live at the
2967 if (bitmap_bit_p (blocks, DF_REF_BBNO (use)))
2969 /* Follow use-def chain to check all the defs for this use. */
2970 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
2971 if (ud_link->ref != def)
2979 /* Return non-zero if all the defs of INSN within BB dominates
2980 all the corresponding uses. */
2982 df_insn_dominates_uses_p (df, bb, insn, blocks)
2984 basic_block bb ATTRIBUTE_UNUSED;
2989 struct df_link *link;
2991 uid = INSN_UID (insn);
2993 for (link = df->insns[uid].defs; link; link = link->next)
2995 struct ref *def = link->ref;
2997 /* Only consider the defs within BLOCKS. */
2998 if (bitmap_bit_p (blocks, DF_REF_BBNO (def))
2999 && ! df_def_dominates_uses_p (df, def, blocks))
3006 /* Return the basic block that REG referenced in or NULL if referenced
3007 in multiple basic blocks. */
3009 df_regno_bb (df, regno)
3013 struct df_link *defs = df->regs[regno].defs;
3014 struct df_link *uses = df->regs[regno].uses;
3015 struct ref *def = defs ? defs->ref : 0;
3016 struct ref *use = uses ? uses->ref : 0;
3017 basic_block bb_def = def ? DF_REF_BB (def) : 0;
3018 basic_block bb_use = use ? DF_REF_BB (use) : 0;
3020 /* Compare blocks of first def and last use. ???? FIXME. What if
3021 the reg-def and reg-use lists are not correctly ordered. */
3022 return bb_def == bb_use ? bb_def : 0;
3026 /* Return non-zero if REG used in multiple basic blocks. */
3028 df_reg_global_p (df, reg)
3032 return df_regno_bb (df, REGNO (reg)) != 0;
3036 /* Return total lifetime (in insns) of REG. */
3038 df_reg_lifetime (df, reg)
3042 return df->regs[REGNO (reg)].lifetime;
3046 /* Return non-zero if REG live at start of BB. */
3048 df_bb_reg_live_start_p (df, bb, reg)
3049 struct df *df ATTRIBUTE_UNUSED;
3053 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3055 #ifdef ENABLE_CHECKING
3056 if (! bb_info->lr_in)
3060 return bitmap_bit_p (bb_info->lr_in, REGNO (reg));
3064 /* Return non-zero if REG live at end of BB. */
3066 df_bb_reg_live_end_p (df, bb, reg)
3067 struct df *df ATTRIBUTE_UNUSED;
3071 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3073 #ifdef ENABLE_CHECKING
3074 if (! bb_info->lr_in)
3078 return bitmap_bit_p (bb_info->lr_out, REGNO (reg));
3082 /* Return -1 if life of REG1 before life of REG2, 1 if life of REG1
3083 after life of REG2, or 0, if the lives overlap. */
3085 df_bb_regs_lives_compare (df, bb, reg1, reg2)
3091 unsigned int regno1 = REGNO (reg1);
3092 unsigned int regno2 = REGNO (reg2);
3099 /* The regs must be local to BB. */
3100 if (df_regno_bb (df, regno1) != bb
3101 || df_regno_bb (df, regno2) != bb)
3104 def2 = df_bb_regno_first_def_find (df, bb, regno2);
3105 use1 = df_bb_regno_last_use_find (df, bb, regno1);
3107 if (DF_INSN_LUID (df, DF_REF_INSN (def2))
3108 > DF_INSN_LUID (df, DF_REF_INSN (use1)))
3111 def1 = df_bb_regno_first_def_find (df, bb, regno1);
3112 use2 = df_bb_regno_last_use_find (df, bb, regno2);
3114 if (DF_INSN_LUID (df, DF_REF_INSN (def1))
3115 > DF_INSN_LUID (df, DF_REF_INSN (use2)))
3122 /* Return last use of REGNO within BB. */
3124 df_bb_regno_last_use_find (df, bb, regno)
3126 basic_block bb ATTRIBUTE_UNUSED;
3129 struct df_link *link;
3131 /* This assumes that the reg-use list is ordered such that for any
3132 BB, the last use is found first. However, since the BBs are not
3133 ordered, the first use in the chain is not necessarily the last
3134 use in the function. */
3135 for (link = df->regs[regno].uses; link; link = link->next)
3137 struct ref *use = link->ref;
3139 if (DF_REF_BB (use) == bb)
3146 /* Return first def of REGNO within BB. */
3148 df_bb_regno_first_def_find (df, bb, regno)
3150 basic_block bb ATTRIBUTE_UNUSED;
3153 struct df_link *link;
3155 /* This assumes that the reg-def list is ordered such that for any
3156 BB, the first def is found first. However, since the BBs are not
3157 ordered, the first def in the chain is not necessarily the first
3158 def in the function. */
3159 for (link = df->regs[regno].defs; link; link = link->next)
3161 struct ref *def = link->ref;
3163 if (DF_REF_BB (def) == bb)
3170 /* Return first use of REGNO inside INSN within BB. */
3172 df_bb_insn_regno_last_use_find (df, bb, insn, regno)
3174 basic_block bb ATTRIBUTE_UNUSED;
3179 struct df_link *link;
3181 uid = INSN_UID (insn);
3183 for (link = df->insns[uid].uses; link; link = link->next)
3185 struct ref *use = link->ref;
3187 if (DF_REF_REGNO (use) == regno)
3195 /* Return first def of REGNO inside INSN within BB. */
3197 df_bb_insn_regno_first_def_find (df, bb, insn, regno)
3199 basic_block bb ATTRIBUTE_UNUSED;
3204 struct df_link *link;
3206 uid = INSN_UID (insn);
3208 for (link = df->insns[uid].defs; link; link = link->next)
3210 struct ref *def = link->ref;
3212 if (DF_REF_REGNO (def) == regno)
3220 /* Return insn using REG if the BB contains only a single
3221 use and def of REG. */
3223 df_bb_single_def_use_insn_find (df, bb, insn, reg)
3231 struct df_link *du_link;
3233 def = df_bb_insn_regno_first_def_find (df, bb, insn, REGNO (reg));
3238 du_link = DF_REF_CHAIN (def);
3245 /* Check if def is dead. */
3249 /* Check for multiple uses. */
3253 return DF_REF_INSN (use);
3256 /* Functions for debugging/dumping dataflow information. */
3259 /* Dump a def-use or use-def chain for REF to FILE. */
3261 df_chain_dump (link, file)
3262 struct df_link *link;
3265 fprintf (file, "{ ");
3266 for (; link; link = link->next)
3268 fprintf (file, "%c%d ",
3269 DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
3270 DF_REF_ID (link->ref));
3272 fprintf (file, "}");
3276 df_chain_dump_regno (link, file)
3277 struct df_link *link;
3280 fprintf (file, "{ ");
3281 for (; link; link = link->next)
3283 fprintf (file, "%c%d(%d) ",
3284 DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
3285 DF_REF_ID (link->ref),
3286 DF_REF_REGNO (link->ref));
3288 fprintf (file, "}");
3291 /* Dump dataflow info. */
3293 df_dump (df, flags, file)
3304 fprintf (file, "\nDataflow summary:\n");
3305 fprintf (file, "n_regs = %d, n_defs = %d, n_uses = %d, n_bbs = %d\n",
3306 df->n_regs, df->n_defs, df->n_uses, df->n_bbs);
3310 fprintf (file, "Reaching defs:\n");
3311 for (i = 0; i < df->n_bbs; i++)
3313 basic_block bb = BASIC_BLOCK (i);
3314 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3316 if (! bb_info->rd_in)
3319 fprintf (file, "bb %d in \t", i);
3320 dump_bitmap (file, bb_info->rd_in);
3321 fprintf (file, "bb %d gen \t", i);
3322 dump_bitmap (file, bb_info->rd_gen);
3323 fprintf (file, "bb %d kill\t", i);
3324 dump_bitmap (file, bb_info->rd_kill);
3325 fprintf (file, "bb %d out \t", i);
3326 dump_bitmap (file, bb_info->rd_out);
3330 if (flags & DF_UD_CHAIN)
3332 fprintf (file, "Use-def chains:\n");
3333 for (j = 0; j < df->n_defs; j++)
3337 fprintf (file, "d%d bb %d luid %d insn %d reg %d ",
3338 j, DF_REF_BBNO (df->defs[j]),
3339 DF_INSN_LUID (df, DF_REF_INSN (df->defs[j])),
3340 DF_REF_INSN_UID (df->defs[j]),
3341 DF_REF_REGNO (df->defs[j]));
3342 df_chain_dump (DF_REF_CHAIN (df->defs[j]), file);
3343 fprintf (file, "\n");
3350 fprintf (file, "Reaching uses:\n");
3351 for (i = 0; i < df->n_bbs; i++)
3353 basic_block bb = BASIC_BLOCK (i);
3354 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3356 if (! bb_info->ru_in)
3359 fprintf (file, "bb %d in \t", i);
3360 dump_bitmap (file, bb_info->ru_in);
3361 fprintf (file, "bb %d gen \t", i);
3362 dump_bitmap (file, bb_info->ru_gen);
3363 fprintf (file, "bb %d kill\t", i);
3364 dump_bitmap (file, bb_info->ru_kill);
3365 fprintf (file, "bb %d out \t", i);
3366 dump_bitmap (file, bb_info->ru_out);
3370 if (flags & DF_DU_CHAIN)
3372 fprintf (file, "Def-use chains:\n");
3373 for (j = 0; j < df->n_uses; j++)
3377 fprintf (file, "u%d bb %d luid %d insn %d reg %d ",
3378 j, DF_REF_BBNO (df->uses[j]),
3379 DF_INSN_LUID (df, DF_REF_INSN (df->uses[j])),
3380 DF_REF_INSN_UID (df->uses[j]),
3381 DF_REF_REGNO (df->uses[j]));
3382 df_chain_dump (DF_REF_CHAIN (df->uses[j]), file);
3383 fprintf (file, "\n");
3390 fprintf (file, "Live regs:\n");
3391 for (i = 0; i < df->n_bbs; i++)
3393 basic_block bb = BASIC_BLOCK (i);
3394 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3396 if (! bb_info->lr_in)
3399 fprintf (file, "bb %d in \t", i);
3400 dump_bitmap (file, bb_info->lr_in);
3401 fprintf (file, "bb %d use \t", i);
3402 dump_bitmap (file, bb_info->lr_use);
3403 fprintf (file, "bb %d def \t", i);
3404 dump_bitmap (file, bb_info->lr_def);
3405 fprintf (file, "bb %d out \t", i);
3406 dump_bitmap (file, bb_info->lr_out);
3410 if (flags & (DF_REG_INFO | DF_RD_CHAIN | DF_RU_CHAIN))
3412 struct reg_info *reg_info = df->regs;
3414 fprintf (file, "Register info:\n");
3415 for (j = 0; j < df->n_regs; j++)
3417 if (((flags & DF_REG_INFO)
3418 && (reg_info[j].n_uses || reg_info[j].n_defs))
3419 || ((flags & DF_RD_CHAIN) && reg_info[j].defs)
3420 || ((flags & DF_RU_CHAIN) && reg_info[j].uses))
3422 fprintf (file, "reg %d", j);
3423 if ((flags & DF_RD_CHAIN) && (flags & DF_RU_CHAIN))
3425 basic_block bb = df_regno_bb (df, j);
3428 fprintf (file, " bb %d", bb->index);
3430 fprintf (file, " bb ?");
3432 if (flags & DF_REG_INFO)
3434 fprintf (file, " life %d", reg_info[j].lifetime);
3437 if ((flags & DF_REG_INFO) || (flags & DF_RD_CHAIN))
3439 fprintf (file, " defs ");
3440 if (flags & DF_REG_INFO)
3441 fprintf (file, "%d ", reg_info[j].n_defs);
3442 if (flags & DF_RD_CHAIN)
3443 df_chain_dump (reg_info[j].defs, file);
3446 if ((flags & DF_REG_INFO) || (flags & DF_RU_CHAIN))
3448 fprintf (file, " uses ");
3449 if (flags & DF_REG_INFO)
3450 fprintf (file, "%d ", reg_info[j].n_uses);
3451 if (flags & DF_RU_CHAIN)
3452 df_chain_dump (reg_info[j].uses, file);
3455 fprintf (file, "\n");
3459 fprintf (file, "\n");
3464 df_insn_debug (df, insn, file)
3472 uid = INSN_UID (insn);
3473 if (uid >= df->insn_size)
3476 if (df->insns[uid].defs)
3477 bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
3478 else if (df->insns[uid].uses)
3479 bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
3483 fprintf (file, "insn %d bb %d luid %d defs ",
3484 uid, bbi, DF_INSN_LUID (df, insn));
3485 df_chain_dump (df->insns[uid].defs, file);
3486 fprintf (file, " uses ");
3487 df_chain_dump (df->insns[uid].uses, file);
3488 fprintf (file, "\n");
3492 df_insn_debug_regno (df, insn, file)
3500 uid = INSN_UID (insn);
3501 if (uid >= df->insn_size)
3504 if (df->insns[uid].defs)
3505 bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
3506 else if (df->insns[uid].uses)
3507 bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
3511 fprintf (file, "insn %d bb %d luid %d defs ",
3512 uid, bbi, DF_INSN_LUID (df, insn));
3513 df_chain_dump_regno (df->insns[uid].defs, file);
3514 fprintf (file, " uses ");
3515 df_chain_dump_regno (df->insns[uid].uses, file);
3516 fprintf (file, "\n");
3520 df_regno_debug (df, regno, file)
3525 if (regno >= df->reg_size)
3528 fprintf (file, "reg %d life %d defs ",
3529 regno, df->regs[regno].lifetime);
3530 df_chain_dump (df->regs[regno].defs, file);
3531 fprintf (file, " uses ");
3532 df_chain_dump (df->regs[regno].uses, file);
3533 fprintf (file, "\n");
3538 df_ref_debug (df, ref, file)
3543 fprintf (file, "%c%d ",
3544 DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
3546 fprintf (file, "reg %d bb %d luid %d insn %d chain ",
3549 DF_INSN_LUID (df, DF_REF_INSN (ref)),
3550 INSN_UID (DF_REF_INSN (ref)));
3551 df_chain_dump (DF_REF_CHAIN (ref), file);
3552 fprintf (file, "\n");
3557 debug_df_insn (insn)
3560 df_insn_debug (ddf, insn, stderr);
3569 df_regno_debug (ddf, REGNO (reg), stderr);
3574 debug_df_regno (regno)
3577 df_regno_debug (ddf, regno, stderr);
3585 df_ref_debug (ddf, ref, stderr);
3590 debug_df_defno (defno)
3593 df_ref_debug (ddf, ddf->defs[defno], stderr);
3598 debug_df_useno (defno)
3601 df_ref_debug (ddf, ddf->uses[defno], stderr);
3606 debug_df_chain (link)
3607 struct df_link *link;
3609 df_chain_dump (link, stderr);
3610 fputc ('\n', stderr);
3615 in, out = Filled in by function.
3616 blocks = Blocks to analyze.
3617 dir = Dataflow direction.
3618 conf_op = Confluence operation.
3619 transfun = Transfer function.
3620 order = Order to iterate in. (Should map block numbers -> order)
3621 data = Whatever you want. It's passed to the transfer function.
3623 This function will perform iterative bitvector dataflow, producing
3624 the in and out sets. Even if you only want to perform it for a
3625 small number of blocks, the vectors for in and out must be large
3626 enough for *all* blocks, because changing one block might affect
3627 others. However, it'll only put what you say to analyze on the
3630 For forward problems, you probably want to pass in a mapping of
3631 block number to rc_order (like df->inverse_rc_map).
3635 iterative_dataflow_sbitmap (in, out, gen, kill, blocks,
3636 dir, conf_op, transfun, order, data)
3637 sbitmap *in, *out, *gen, *kill;
3639 enum df_flow_dir dir;
3640 enum df_confluence_op conf_op;
3641 transfer_function_sbitmap transfun;
3650 onqueue = sbitmap_alloc (n_basic_blocks);
3651 sbitmap_zero (onqueue);
3652 worklist = fibheap_new ();
3653 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
3655 fibheap_insert (worklist, order[i], (void *) i);
3656 SET_BIT (onqueue, i);
3659 sbitmap_copy (out[i], gen[i]);
3663 sbitmap_copy (in[i], gen[i]);
3667 while (!fibheap_empty (worklist))
3670 i = (int) fibheap_extract_min (worklist);
3672 bb = BASIC_BLOCK (i);
3673 RESET_BIT (onqueue, i);
3676 /* Calculate <conf_op> of predecessor_outs */
3677 for (e = bb->pred; e != 0; e = e->pred_next)
3679 if (e->src == ENTRY_BLOCK_PTR)
3681 sbitmap_zero (in[i]);
3684 sbitmap_copy (in[i], out[e->src->index]);
3688 sbitmap_zero (in[i]);
3689 for (e = bb->pred; e != 0; e = e->pred_next)
3691 if (e->src == ENTRY_BLOCK_PTR)
3696 sbitmap_a_or_b (in[i], in[i], out[e->src->index]);
3699 sbitmap_a_and_b (in[i], in[i], out[e->src->index]);
3706 /* Calculate <conf_op> of successor ins */
3707 sbitmap_zero (out[i]);
3708 for (e = bb->succ; e != 0; e = e->succ_next)
3710 if (e->dest == EXIT_BLOCK_PTR)
3715 sbitmap_a_or_b (out[i], out[i], in[e->dest->index]);
3718 sbitmap_a_and_b (out[i], out[i], in[e->dest->index]);
3724 (*transfun)(i, &changed, in[i], out[i], gen[i], kill[i], data);
3730 for (e = bb->succ; e != 0; e = e->succ_next)
3732 if (e->dest == EXIT_BLOCK_PTR)
3734 if (!TEST_BIT (onqueue, e->dest->index))
3736 SET_BIT (onqueue, e->dest->index);
3737 fibheap_insert (worklist,
3738 order[e->dest->index],
3739 (void *)e->dest->index);
3745 for (e = bb->pred; e != 0; e = e->pred_next)
3747 if (e->src == ENTRY_BLOCK_PTR)
3749 if (!TEST_BIT (onqueue, e->src->index))
3751 SET_BIT (onqueue, e->src->index);
3752 fibheap_insert (worklist,
3753 order[e->src->index],
3754 (void *)e->src->index);
3761 sbitmap_free (onqueue);
3762 fibheap_delete (worklist);
3765 /* Exactly the same as iterative_dataflow_sbitmap, except it works on
3768 iterative_dataflow_bitmap (in, out, gen, kill, blocks,
3769 dir, conf_op, transfun, order, data)
3770 bitmap *in, *out, *gen, *kill;
3772 enum df_flow_dir dir;
3773 enum df_confluence_op conf_op;
3774 transfer_function_bitmap transfun;
3784 onqueue = sbitmap_alloc (n_basic_blocks);
3785 sbitmap_zero (onqueue);
3786 worklist = fibheap_new ();
3787 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
3789 fibheap_insert (worklist, order[i], (void *) i);
3790 SET_BIT (onqueue, i);
3793 bitmap_copy (out[i], gen[i]);
3797 bitmap_copy (in[i], gen[i]);
3801 while (!fibheap_empty (worklist))
3804 i = (int) fibheap_extract_min (worklist);
3806 bb = BASIC_BLOCK (i);
3807 RESET_BIT (onqueue, i);
3811 /* Calculate <conf_op> of predecessor_outs */
3812 bitmap_zero (in[i]);
3813 for (e = bb->pred; e != 0; e = e->pred_next)
3815 if (e->src == ENTRY_BLOCK_PTR)
3820 bitmap_a_or_b (in[i], in[i], out[e->src->index]);
3823 bitmap_a_and_b (in[i], in[i], out[e->src->index]);
3830 /* Calculate <conf_op> of successor ins */
3831 bitmap_zero(out[i]);
3832 for (e = bb->succ; e != 0; e = e->succ_next)
3834 if (e->dest == EXIT_BLOCK_PTR)
3839 bitmap_a_or_b (out[i], out[i], in[e->dest->index]);
3842 bitmap_a_and_b (out[i], out[i], in[e->dest->index]);
3848 (*transfun)(i, &changed, in[i], out[i], gen[i], kill[i], data);
3854 for (e = bb->succ; e != 0; e = e->succ_next)
3856 if (e->dest == EXIT_BLOCK_PTR)
3858 if (!TEST_BIT (onqueue, e->dest->index))
3860 SET_BIT (onqueue, e->dest->index);
3861 fibheap_insert (worklist,
3862 order[e->dest->index],
3863 (void *)e->dest->index);
3869 for (e = bb->pred; e != 0; e = e->pred_next)
3871 if (e->src == ENTRY_BLOCK_PTR)
3873 if (!TEST_BIT (onqueue, e->src->index))
3875 SET_BIT (onqueue, e->src->index);
3876 fibheap_insert (worklist,
3877 order[e->src->index],
3878 (void *)e->src->index);
3885 sbitmap_free (onqueue);
3886 fibheap_delete (worklist);