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
161 #include "insn-config.h"
163 #include "function.h"
166 #include "hard-reg-set.h"
167 #include "basic-block.h"
172 #define FOR_ALL_BBS(BB, CODE) \
175 for (node_ = 0; node_ < n_basic_blocks; node_++) \
176 {(BB) = BASIC_BLOCK (node_); CODE;};} while (0)
178 #define FOR_EACH_BB_IN_BITMAP(BITMAP, MIN, BB, CODE) \
180 unsigned int node_; \
181 EXECUTE_IF_SET_IN_BITMAP (BITMAP, MIN, node_, \
182 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
184 #define FOR_EACH_BB_IN_BITMAP_REV(BITMAP, MIN, BB, CODE) \
186 unsigned int node_; \
187 EXECUTE_IF_SET_IN_BITMAP_REV (BITMAP, node_, \
188 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
190 #define FOR_EACH_BB_IN_SBITMAP(BITMAP, MIN, BB, CODE) \
192 unsigned int node_; \
193 EXECUTE_IF_SET_IN_SBITMAP (BITMAP, MIN, node_, \
194 {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)
196 #define obstack_chunk_alloc xmalloc
197 #define obstack_chunk_free free
199 static struct obstack df_ref_obstack;
200 static struct df *ddf;
202 static void df_reg_table_realloc PARAMS((struct df *, int));
204 static void df_def_table_realloc PARAMS((struct df *, int));
206 static void df_insn_table_realloc PARAMS((struct df *, int));
207 static void df_bitmaps_alloc PARAMS((struct df *, int));
208 static void df_bitmaps_free PARAMS((struct df *, int));
209 static void df_free PARAMS((struct df *));
210 static void df_alloc PARAMS((struct df *, int));
212 static rtx df_reg_clobber_gen PARAMS((unsigned int));
213 static rtx df_reg_use_gen PARAMS((unsigned int));
215 static inline struct df_link *df_link_create PARAMS((struct ref *,
217 static struct df_link *df_ref_unlink PARAMS((struct df_link **, struct ref *));
218 static void df_def_unlink PARAMS((struct df *, struct ref *));
219 static void df_use_unlink PARAMS((struct df *, struct ref *));
220 static void df_insn_refs_unlink PARAMS ((struct df *, basic_block, rtx));
222 static void df_bb_refs_unlink PARAMS ((struct df *, basic_block));
223 static void df_refs_unlink PARAMS ((struct df *, bitmap));
226 static struct ref *df_ref_create PARAMS((struct df *,
227 rtx, rtx *, basic_block, rtx,
229 static void df_ref_record_1 PARAMS((struct df *, rtx, rtx *,
230 basic_block, rtx, enum df_ref_type));
231 static void df_ref_record PARAMS((struct df *, rtx, rtx *,
232 basic_block bb, rtx, enum df_ref_type));
233 static void df_def_record_1 PARAMS((struct df *, rtx, basic_block, rtx));
234 static void df_defs_record PARAMS((struct df *, rtx, basic_block, rtx));
235 static void df_uses_record PARAMS((struct df *, rtx *,
236 enum df_ref_type, basic_block, rtx));
237 static void df_insn_refs_record PARAMS((struct df *, basic_block, rtx));
238 static void df_bb_refs_record PARAMS((struct df *, basic_block));
239 static void df_refs_record PARAMS((struct df *, bitmap));
241 static int df_visit_next_rc PARAMS ((struct df *, sbitmap));
242 static int df_visit_next_rts PARAMS ((struct df *, sbitmap));
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_rd_global_compute PARAMS((struct df *, bitmap));
252 static void df_ru_global_compute PARAMS((struct df *, bitmap));
253 static void df_lr_global_compute PARAMS((struct df *, bitmap));
254 static void df_bb_rd_local_compute PARAMS((struct df *, basic_block));
255 static void df_rd_local_compute PARAMS((struct df *, bitmap));
256 static void df_bb_ru_local_compute PARAMS((struct df *, basic_block));
257 static void df_ru_local_compute PARAMS((struct df *, bitmap));
258 static void df_bb_lr_local_compute PARAMS((struct df *, basic_block));
259 static void df_lr_local_compute PARAMS((struct df *, bitmap));
260 static void df_bb_reg_info_compute PARAMS((struct df *, basic_block, bitmap));
261 static void df_reg_info_compute PARAMS((struct df *, bitmap));
263 static int df_bb_luids_set PARAMS((struct df *df, basic_block));
264 static int df_luids_set PARAMS((struct df *df, bitmap));
266 static int df_modified_p PARAMS ((struct df *, bitmap));
267 static int df_refs_queue PARAMS ((struct df *));
268 static int df_refs_process PARAMS ((struct df *));
269 static int df_bb_refs_update PARAMS ((struct df *, basic_block));
270 static int df_refs_update PARAMS ((struct df *));
271 static void df_analyse_1 PARAMS((struct df *, bitmap, int, int));
273 static void df_insns_modify PARAMS((struct df *, basic_block,
275 static int df_rtx_mem_replace PARAMS ((rtx *, void *));
276 static int df_rtx_reg_replace PARAMS ((rtx *, void *));
277 void df_refs_reg_replace PARAMS ((struct df *, bitmap,
278 struct df_link *, rtx, rtx));
280 static int df_def_dominates_all_uses_p PARAMS((struct df *, struct ref *def));
281 static int df_def_dominates_uses_p PARAMS((struct df *,
282 struct ref *def, bitmap));
283 static struct ref *df_bb_regno_last_use_find PARAMS((struct df *, basic_block,
285 static struct ref *df_bb_regno_first_def_find PARAMS((struct df *, basic_block,
287 static struct ref *df_bb_insn_regno_last_use_find PARAMS((struct df *,
290 static struct ref *df_bb_insn_regno_first_def_find PARAMS((struct df *,
294 static void df_chain_dump PARAMS((struct df_link *, FILE *file));
295 static void df_chain_dump_regno PARAMS((struct df_link *, FILE *file));
296 static void df_regno_debug PARAMS ((struct df *, unsigned int, FILE *));
297 static void df_ref_debug PARAMS ((struct df *, struct ref *, FILE *));
300 /* Local memory allocation/deallocation routines. */
303 /* Increase the insn info table by SIZE more elements. */
305 df_insn_table_realloc (df, size)
309 /* Make table 25 percent larger by default. */
311 size = df->insn_size / 4;
313 size += df->insn_size;
315 df->insns = (struct insn_info *)
316 xrealloc (df->insns, size * sizeof (struct insn_info));
318 memset (df->insns + df->insn_size, 0,
319 (size - df->insn_size) * sizeof (struct insn_info));
321 df->insn_size = size;
323 if (! df->insns_modified)
325 df->insns_modified = BITMAP_XMALLOC ();
326 bitmap_zero (df->insns_modified);
331 /* Increase the reg info table by SIZE more elements. */
333 df_reg_table_realloc (df, size)
337 /* Make table 25 percent larger by default. */
339 size = df->reg_size / 4;
341 size += df->reg_size;
343 df->regs = (struct reg_info *)
344 xrealloc (df->regs, size * sizeof (struct reg_info));
346 /* Zero the new entries. */
347 memset (df->regs + df->reg_size, 0,
348 (size - df->reg_size) * sizeof (struct reg_info));
355 /* Not currently used. */
357 df_def_table_realloc (df, size)
364 /* Make table 25 percent larger by default. */
366 size = df->def_size / 4;
368 df->def_size += size;
369 df->defs = xrealloc (df->defs,
370 df->def_size * sizeof (*df->defs));
372 /* Allocate a new block of memory and link into list of blocks
373 that will need to be freed later. */
375 refs = xmalloc (size * sizeof (*refs));
377 /* Link all the new refs together, overloading the chain field. */
378 for (i = 0; i < size - 1; i++)
379 refs[i].chain = (struct df_link *)(refs + i + 1);
380 refs[size - 1].chain = 0;
386 /* Allocate bitmaps for each basic block. */
388 df_bitmaps_alloc (df, flags)
395 /* Free the bitmaps if they need resizing. */
396 if ((flags & DF_LR) && df->n_regs < (unsigned int)max_reg_num ())
397 dflags |= DF_LR | DF_RU;
398 if ((flags & DF_RU) && df->n_uses < df->use_id)
400 if ((flags & DF_RD) && df->n_defs < df->def_id)
404 df_bitmaps_free (df, dflags);
406 df->n_defs = df->def_id;
407 df->n_uses = df->use_id;
409 for (i = 0; i < df->n_bbs; i++)
411 basic_block bb = BASIC_BLOCK (i);
412 struct bb_info *bb_info = DF_BB_INFO (df, bb);
414 if (flags & DF_RD && ! bb_info->rd_in)
416 /* Allocate bitmaps for reaching definitions. */
417 bb_info->rd_kill = BITMAP_XMALLOC ();
418 bitmap_zero (bb_info->rd_kill);
419 bb_info->rd_gen = BITMAP_XMALLOC ();
420 bitmap_zero (bb_info->rd_gen);
421 bb_info->rd_in = BITMAP_XMALLOC ();
422 bb_info->rd_out = BITMAP_XMALLOC ();
423 bb_info->rd_valid = 0;
426 if (flags & DF_RU && ! bb_info->ru_in)
428 /* Allocate bitmaps for upward exposed uses. */
429 bb_info->ru_kill = BITMAP_XMALLOC ();
430 bitmap_zero (bb_info->ru_kill);
431 /* Note the lack of symmetry. */
432 bb_info->ru_gen = BITMAP_XMALLOC ();
433 bitmap_zero (bb_info->ru_gen);
434 bb_info->ru_in = BITMAP_XMALLOC ();
435 bb_info->ru_out = BITMAP_XMALLOC ();
436 bb_info->ru_valid = 0;
439 if (flags & DF_LR && ! bb_info->lr_in)
441 /* Allocate bitmaps for live variables. */
442 bb_info->lr_def = BITMAP_XMALLOC ();
443 bitmap_zero (bb_info->lr_def);
444 bb_info->lr_use = BITMAP_XMALLOC ();
445 bitmap_zero (bb_info->lr_use);
446 bb_info->lr_in = BITMAP_XMALLOC ();
447 bb_info->lr_out = BITMAP_XMALLOC ();
448 bb_info->lr_valid = 0;
454 /* Free bitmaps for each basic block. */
456 df_bitmaps_free (df, flags)
457 struct df *df ATTRIBUTE_UNUSED;
462 for (i = 0; i < df->n_bbs; i++)
464 basic_block bb = BASIC_BLOCK (i);
465 struct bb_info *bb_info = DF_BB_INFO (df, bb);
470 if ((flags & DF_RD) && bb_info->rd_in)
472 /* Free bitmaps for reaching definitions. */
473 BITMAP_XFREE (bb_info->rd_kill);
474 bb_info->rd_kill = NULL;
475 BITMAP_XFREE (bb_info->rd_gen);
476 bb_info->rd_gen = NULL;
477 BITMAP_XFREE (bb_info->rd_in);
478 bb_info->rd_in = NULL;
479 BITMAP_XFREE (bb_info->rd_out);
480 bb_info->rd_out = NULL;
483 if ((flags & DF_RU) && bb_info->ru_in)
485 /* Free bitmaps for upward exposed uses. */
486 BITMAP_XFREE (bb_info->ru_kill);
487 bb_info->ru_kill = NULL;
488 BITMAP_XFREE (bb_info->ru_gen);
489 bb_info->ru_gen = NULL;
490 BITMAP_XFREE (bb_info->ru_in);
491 bb_info->ru_in = NULL;
492 BITMAP_XFREE (bb_info->ru_out);
493 bb_info->ru_out = NULL;
496 if ((flags & DF_LR) && bb_info->lr_in)
498 /* Free bitmaps for live variables. */
499 BITMAP_XFREE (bb_info->lr_def);
500 bb_info->lr_def = NULL;
501 BITMAP_XFREE (bb_info->lr_use);
502 bb_info->lr_use = NULL;
503 BITMAP_XFREE (bb_info->lr_in);
504 bb_info->lr_in = NULL;
505 BITMAP_XFREE (bb_info->lr_out);
506 bb_info->lr_out = NULL;
509 df->flags &= ~(flags & (DF_RD | DF_RU | DF_LR));
513 /* Allocate and initialise dataflow memory. */
515 df_alloc (df, n_regs)
522 gcc_obstack_init (&df_ref_obstack);
524 /* Perhaps we should use LUIDs to save memory for the insn_refs
525 table. This is only a small saving; a few pointers. */
526 n_insns = get_max_uid () + 1;
530 /* Approximate number of defs by number of insns. */
531 df->def_size = n_insns;
532 df->defs = xmalloc (df->def_size * sizeof (*df->defs));
536 /* Approximate number of uses by twice number of insns. */
537 df->use_size = n_insns * 2;
538 df->uses = xmalloc (df->use_size * sizeof (*df->uses));
541 df->n_bbs = n_basic_blocks;
543 /* Allocate temporary working array used during local dataflow analysis. */
544 df->reg_def_last = xmalloc (df->n_regs * sizeof (struct ref *));
546 df_insn_table_realloc (df, n_insns);
548 df_reg_table_realloc (df, df->n_regs);
550 df->bbs_modified = BITMAP_XMALLOC ();
551 bitmap_zero (df->bbs_modified);
555 df->bbs = xcalloc (df->n_bbs, sizeof (struct bb_info));
557 df->all_blocks = BITMAP_XMALLOC ();
558 for (i = 0; i < n_basic_blocks; i++)
559 bitmap_set_bit (df->all_blocks, i);
563 /* Free all the dataflow info. */
568 df_bitmaps_free (df, DF_ALL);
596 if (df->bbs_modified)
597 BITMAP_XFREE (df->bbs_modified);
598 df->bbs_modified = 0;
600 if (df->insns_modified)
601 BITMAP_XFREE (df->insns_modified);
602 df->insns_modified = 0;
604 BITMAP_XFREE (df->all_blocks);
607 obstack_free (&df_ref_obstack, NULL);
610 /* Local miscellaneous routines. */
612 /* Return a USE for register REGNO. */
613 static rtx df_reg_use_gen (regno)
619 reg = regno >= FIRST_PSEUDO_REGISTER
620 ? regno_reg_rtx[regno] : gen_rtx_REG (reg_raw_mode[regno], regno);
622 use = gen_rtx_USE (GET_MODE (reg), reg);
627 /* Return a CLOBBER for register REGNO. */
628 static rtx df_reg_clobber_gen (regno)
634 reg = regno >= FIRST_PSEUDO_REGISTER
635 ? regno_reg_rtx[regno] : gen_rtx_REG (reg_raw_mode[regno], regno);
637 use = gen_rtx_CLOBBER (GET_MODE (reg), reg);
641 /* Local chain manipulation routines. */
643 /* Create a link in a def-use or use-def chain. */
644 static inline struct df_link *
645 df_link_create (ref, next)
647 struct df_link *next;
649 struct df_link *link;
651 link = (struct df_link *) obstack_alloc (&df_ref_obstack,
659 /* Add REF to chain head pointed to by PHEAD. */
660 static struct df_link *
661 df_ref_unlink (phead, ref)
662 struct df_link **phead;
665 struct df_link *link = *phead;
671 /* Only a single ref. It must be the one we want.
672 If not, the def-use and use-def chains are likely to
674 if (link->ref != ref)
676 /* Now have an empty chain. */
681 /* Multiple refs. One of them must be us. */
682 if (link->ref == ref)
687 for (; link->next; link = link->next)
689 if (link->next->ref == ref)
691 /* Unlink from list. */
692 link->next = link->next->next;
703 /* Unlink REF from all def-use/use-def chains, etc. */
705 df_ref_remove (df, ref)
709 if (DF_REF_REG_DEF_P (ref))
711 df_def_unlink (df, ref);
712 df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].defs, ref);
716 df_use_unlink (df, ref);
717 df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].uses, ref);
723 /* Unlink DEF from use-def and reg-def chains. */
725 df_def_unlink (df, def)
726 struct df *df ATTRIBUTE_UNUSED;
729 struct df_link *du_link;
730 unsigned int dregno = DF_REF_REGNO (def);
732 /* Follow def-use chain to find all the uses of this def. */
733 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
735 struct ref *use = du_link->ref;
737 /* Unlink this def from the use-def chain. */
738 df_ref_unlink (&DF_REF_CHAIN (use), def);
740 DF_REF_CHAIN (def) = 0;
742 /* Unlink def from reg-def chain. */
743 df_ref_unlink (&df->regs[dregno].defs, def);
745 df->defs[DF_REF_ID (def)] = 0;
749 /* Unlink use from def-use and reg-use chains. */
751 df_use_unlink (df, use)
752 struct df *df ATTRIBUTE_UNUSED;
755 struct df_link *ud_link;
756 unsigned int uregno = DF_REF_REGNO (use);
758 /* Follow use-def chain to find all the defs of this use. */
759 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
761 struct ref *def = ud_link->ref;
763 /* Unlink this use from the def-use chain. */
764 df_ref_unlink (&DF_REF_CHAIN (def), use);
766 DF_REF_CHAIN (use) = 0;
768 /* Unlink use from reg-use chain. */
769 df_ref_unlink (&df->regs[uregno].uses, use);
771 df->uses[DF_REF_ID (use)] = 0;
774 /* Local routines for recording refs. */
777 /* Create a new ref of type DF_REF_TYPE for register REG at address
778 LOC within INSN of BB. */
780 df_ref_create (df, reg, loc, bb, insn, ref_type)
786 enum df_ref_type ref_type;
788 struct ref *this_ref;
791 this_ref = (struct ref *) obstack_alloc (&df_ref_obstack,
793 DF_REF_REG (this_ref) = reg;
794 DF_REF_LOC (this_ref) = loc;
795 DF_REF_BB (this_ref) = bb;
796 DF_REF_INSN (this_ref) = insn;
797 DF_REF_CHAIN (this_ref) = 0;
798 DF_REF_TYPE (this_ref) = ref_type;
799 uid = INSN_UID (insn);
801 if (ref_type == DF_REF_REG_DEF)
803 if (df->def_id >= df->def_size)
805 /* Make table 25 percent larger. */
806 df->def_size += (df->def_size / 4);
807 df->defs = xrealloc (df->defs,
808 df->def_size * sizeof (*df->defs));
810 DF_REF_ID (this_ref) = df->def_id;
811 df->defs[df->def_id++] = this_ref;
815 if (df->use_id >= df->use_size)
817 /* Make table 25 percent larger. */
818 df->use_size += (df->use_size / 4);
819 df->uses = xrealloc (df->uses,
820 df->use_size * sizeof (*df->uses));
822 DF_REF_ID (this_ref) = df->use_id;
823 df->uses[df->use_id++] = this_ref;
829 /* Create a new reference of type DF_REF_TYPE for a single register REG,
830 used inside the LOC rtx of INSN. */
832 df_ref_record_1 (df, reg, loc, bb, insn, ref_type)
838 enum df_ref_type ref_type;
840 df_ref_create (df, reg, loc, bb, insn, ref_type);
844 /* Create new references of type DF_REF_TYPE for each part of register REG
845 at address LOC within INSN of BB. */
847 df_ref_record (df, reg, loc, bb, insn, ref_type)
853 enum df_ref_type ref_type;
857 if (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG)
860 /* For the reg allocator we are interested in some SUBREG rtx's, but not
861 all. Notably only those representing a word extraction from a multi-word
862 reg. As written in the docu those should have the form
863 (subreg:SI (reg:M A) N), with size(SImode) > size(Mmode).
864 XXX Is that true? We could also use the global word_mode variable. */
865 if (GET_CODE (reg) == SUBREG
866 && (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (word_mode)
867 || GET_MODE_SIZE (GET_MODE (reg))
868 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (reg)))))
870 loc = &SUBREG_REG (reg);
874 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
875 if (regno < FIRST_PSEUDO_REGISTER)
880 if (! (df->flags & DF_HARD_REGS))
883 /* GET_MODE (reg) is correct here. We don't want to go into a SUBREG
884 for the mode, because we only want to add references to regs, which
885 are really referenced. E.g. a (subreg:SI (reg:DI 0) 0) does _not_
886 reference the whole reg 0 in DI mode (which would also include
887 reg 1, at least, if 0 and 1 are SImode registers). */
888 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
890 for (i = regno; i < endregno; i++)
891 df_ref_record_1 (df, gen_rtx_REG (reg_raw_mode[i], i),
892 loc, bb, insn, ref_type);
896 df_ref_record_1 (df, reg, loc, bb, insn, ref_type);
901 /* Process all the registers defined in the rtx, X. */
903 df_def_record_1 (df, x, bb, insn)
909 rtx *loc = &SET_DEST (x);
912 /* Some targets place small structures in registers for
913 return values of functions. */
914 if (GET_CODE (dst) == PARALLEL && GET_MODE (dst) == BLKmode)
918 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
919 df_def_record_1 (df, XVECEXP (dst, 0, i), bb, insn);
923 /* May be, we should flag the use of strict_low_part somehow. Might be
924 handy for the reg allocator. */
926 while (GET_CODE (dst) == STRICT_LOW_PART
927 || GET_CODE (dst) == ZERO_EXTRACT
928 || GET_CODE (dst) == SIGN_EXTRACT)
930 loc = &XEXP (dst, 0);
933 /* For the reg allocator we are interested in exact register references.
934 This means, we want to know, if only a part of a register is
937 if (GET_CODE (dst) == SUBREG)
939 loc = &XEXP (dst, 0);
944 while (GET_CODE (dst) == SUBREG
945 || GET_CODE (dst) == ZERO_EXTRACT
946 || GET_CODE (dst) == SIGN_EXTRACT
947 || GET_CODE (dst) == STRICT_LOW_PART)
949 loc = &XEXP (dst, 0);
954 if (GET_CODE (dst) == REG
955 || (GET_CODE (dst) == SUBREG && GET_CODE (SUBREG_REG (dst)) == REG))
956 df_ref_record (df, dst, loc, bb, insn, DF_REF_REG_DEF);
960 /* Process all the registers defined in the pattern rtx, X. */
962 df_defs_record (df, x, bb, insn)
968 RTX_CODE code = GET_CODE (x);
970 if (code == SET || code == CLOBBER)
972 /* Mark the single def within the pattern. */
973 df_def_record_1 (df, x, bb, insn);
975 else if (code == PARALLEL)
979 /* Mark the multiple defs within the pattern. */
980 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
982 code = GET_CODE (XVECEXP (x, 0, i));
983 if (code == SET || code == CLOBBER)
984 df_def_record_1 (df, XVECEXP (x, 0, i), bb, insn);
990 /* Process all the registers used in the rtx at address LOC. */
992 df_uses_record (df, loc, ref_type, bb, insn)
995 enum df_ref_type ref_type;
1004 code = GET_CODE (x);
1018 /* If we are clobbering a MEM, mark any registers inside the address
1020 if (GET_CODE (XEXP (x, 0)) == MEM)
1021 df_uses_record (df, &XEXP (XEXP (x, 0), 0),
1022 DF_REF_REG_MEM_STORE, bb, insn);
1024 /* If we're clobbering a REG then we have a def so ignore. */
1028 df_uses_record (df, &XEXP (x, 0), DF_REF_REG_MEM_LOAD, bb, insn);
1032 /* While we're here, optimize this case. */
1033 #if defined(HANDLE_SUBREG)
1035 /* In case the SUBREG is not of a register, don't optimize. */
1036 if (GET_CODE (SUBREG_REG (x)) != REG)
1038 loc = &SUBREG_REG (x);
1039 df_uses_record (df, loc, ref_type, bb, insn);
1043 loc = &SUBREG_REG (x);
1045 if (GET_CODE (x) != REG)
1047 df_uses_record (df, loc, ref_type, bb, insn);
1052 /* ... Fall through ... */
1055 /* See a register (or subreg) other than being set. */
1056 df_ref_record (df, x, loc, bb, insn, ref_type);
1061 rtx dst = SET_DEST (x);
1064 /* If storing into MEM, don't show it as being used. But do
1065 show the address as being used. */
1066 if (GET_CODE (dst) == MEM)
1068 df_uses_record (df, &XEXP (dst, 0),
1069 DF_REF_REG_MEM_STORE,
1071 df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn);
1075 #if 1 && defined(HANDLE_SUBREG)
1076 /* Look for sets that perform a read-modify-write. */
1077 while (GET_CODE (dst) == STRICT_LOW_PART
1078 || GET_CODE (dst) == ZERO_EXTRACT
1079 || GET_CODE (dst) == SIGN_EXTRACT)
1081 if (GET_CODE (dst) == STRICT_LOW_PART)
1083 dst = XEXP (dst, 0);
1084 if (GET_CODE (dst) != SUBREG)
1086 /* A strict_low_part uses the whole reg not only the subreg. */
1087 df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb, insn);
1091 df_uses_record (df, &XEXP (dst, 0), DF_REF_REG_USE, bb, insn);
1092 dst = XEXP (dst, 0);
1095 if (GET_CODE (dst) == SUBREG)
1097 /* Paradoxical or too small subreg's are read-mod-write. */
1098 if (GET_MODE_SIZE (GET_MODE (dst)) < GET_MODE_SIZE (word_mode)
1099 || GET_MODE_SIZE (GET_MODE (dst))
1100 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1103 /* In the original code also some SUBREG rtx's were considered
1104 read-modify-write (those with
1105 REG_SIZE(SUBREG_REG(dst)) > REG_SIZE(dst) )
1106 e.g. a (subreg:QI (reg:SI A) 0). I can't see this. The only
1107 reason for a read cycle for reg A would be to somehow preserve
1108 the bits outside of the subreg:QI. But for this a strict_low_part
1109 was necessary anyway, and this we handled already. */
1111 while (GET_CODE (dst) == STRICT_LOW_PART
1112 || GET_CODE (dst) == ZERO_EXTRACT
1113 || GET_CODE (dst) == SIGN_EXTRACT
1114 || GET_CODE (dst) == SUBREG)
1116 /* A SUBREG of a smaller size does not use the old value. */
1117 if (GET_CODE (dst) != SUBREG
1118 || (REG_SIZE (SUBREG_REG (dst)) > REG_SIZE (dst)))
1120 dst = XEXP (dst, 0);
1124 if ((GET_CODE (dst) == PARALLEL && GET_MODE (dst) == BLKmode)
1125 || GET_CODE (dst) == REG || GET_CODE (dst) == SUBREG)
1127 #if 1 || !defined(HANDLE_SUBREG)
1129 df_uses_record (df, &SET_DEST (x), DF_REF_REG_USE, bb, insn);
1131 df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn);
1141 case UNSPEC_VOLATILE:
1145 /* Traditional and volatile asm instructions must be considered to use
1146 and clobber all hard registers, all pseudo-registers and all of
1147 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1149 Consider for instance a volatile asm that changes the fpu rounding
1150 mode. An insn should not be moved across this even if it only uses
1151 pseudo-regs because it might give an incorrectly rounded result.
1153 For now, just mark any regs we can find in ASM_OPERANDS as
1156 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1157 We can not just fall through here since then we would be confused
1158 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1159 traditional asms unlike their normal usage. */
1160 if (code == ASM_OPERANDS)
1164 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1165 df_uses_record (df, &ASM_OPERANDS_INPUT (x, j),
1166 DF_REF_REG_USE, bb, insn);
1178 /* Catch the def of the register being modified. */
1179 df_ref_record (df, XEXP (x, 0), &XEXP (x, 0), bb, insn, DF_REF_REG_DEF);
1181 /* ... Fall through to handle uses ... */
1187 /* Recursively scan the operands of this expression. */
1189 const char *fmt = GET_RTX_FORMAT (code);
1192 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1196 /* Tail recursive case: save a function call level. */
1202 df_uses_record (df, &XEXP (x, i), ref_type, bb, insn);
1204 else if (fmt[i] == 'E')
1207 for (j = 0; j < XVECLEN (x, i); j++)
1208 df_uses_record (df, &XVECEXP (x, i, j), ref_type,
1216 /* Record all the df within INSN of basic block BB. */
1218 df_insn_refs_record (df, bb, insn)
1229 /* Record register defs */
1230 df_defs_record (df, PATTERN (insn), bb, insn);
1232 if (df->flags & DF_EQUIV_NOTES)
1233 for (note = REG_NOTES (insn); note;
1234 note = XEXP (note, 1))
1236 switch (REG_NOTE_KIND (note))
1240 df_uses_record (df, &XEXP (note, 0), DF_REF_REG_USE,
1247 if (GET_CODE (insn) == CALL_INSN)
1252 /* Record the registers used to pass arguments. */
1253 for (note = CALL_INSN_FUNCTION_USAGE (insn); note;
1254 note = XEXP (note, 1))
1256 if (GET_CODE (XEXP (note, 0)) == USE)
1257 df_uses_record (df, &SET_DEST (XEXP (note, 0)), DF_REF_REG_USE,
1261 /* The stack ptr is used (honorarily) by a CALL insn. */
1262 x = df_reg_use_gen (STACK_POINTER_REGNUM);
1263 df_uses_record (df, &SET_DEST (x), DF_REF_REG_USE, bb, insn);
1265 if (df->flags & DF_HARD_REGS)
1267 /* Calls may also reference any of the global registers,
1268 so they are recorded as used. */
1269 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1272 x = df_reg_use_gen (i);
1273 df_uses_record (df, &SET_DEST (x),
1274 DF_REF_REG_USE, bb, insn);
1279 /* Record the register uses. */
1280 df_uses_record (df, &PATTERN (insn),
1281 DF_REF_REG_USE, bb, insn);
1284 if (GET_CODE (insn) == CALL_INSN)
1288 if (df->flags & DF_HARD_REGS)
1290 /* Kill all registers invalidated by a call. */
1291 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1292 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1294 rtx reg_clob = df_reg_clobber_gen (i);
1295 df_defs_record (df, reg_clob, bb, insn);
1299 /* There may be extra registers to be clobbered. */
1300 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1302 note = XEXP (note, 1))
1303 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1304 df_defs_record (df, XEXP (note, 0), bb, insn);
1310 /* Record all the refs within the basic block BB. */
1312 df_bb_refs_record (df, bb)
1318 /* Scan the block an insn at a time from beginning to end. */
1319 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1323 /* Record defs within INSN. */
1324 df_insn_refs_record (df, bb, insn);
1326 if (insn == bb->end)
1332 /* Record all the refs in the basic blocks specified by BLOCKS. */
1334 df_refs_record (df, blocks)
1340 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1342 df_bb_refs_record (df, bb);
1346 /* Dataflow analysis routines. */
1349 /* Create reg-def chains for basic block BB. These are a list of
1350 definitions for each register. */
1352 df_bb_reg_def_chain_create (df, bb)
1358 /* Perhaps the defs should be sorted using a depth first search
1359 of the CFG (or possibly a breadth first search). We currently
1360 scan the basic blocks in reverse order so that the first defs
1361 apprear at the start of the chain. */
1363 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1364 insn = PREV_INSN (insn))
1366 struct df_link *link;
1367 unsigned int uid = INSN_UID (insn);
1369 if (! INSN_P (insn))
1372 for (link = df->insns[uid].defs; link; link = link->next)
1374 struct ref *def = link->ref;
1375 unsigned int dregno = DF_REF_REGNO (def);
1377 df->regs[dregno].defs
1378 = df_link_create (def, df->regs[dregno].defs);
1384 /* Create reg-def chains for each basic block within BLOCKS. These
1385 are a list of definitions for each register. */
1387 df_reg_def_chain_create (df, blocks)
1393 FOR_EACH_BB_IN_BITMAP/*_REV*/ (blocks, 0, bb,
1395 df_bb_reg_def_chain_create (df, bb);
1400 /* Create reg-use chains for basic block BB. These are a list of uses
1401 for each register. */
1403 df_bb_reg_use_chain_create (df, bb)
1409 /* Scan in forward order so that the last uses appear at the
1410 start of the chain. */
1412 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1413 insn = NEXT_INSN (insn))
1415 struct df_link *link;
1416 unsigned int uid = INSN_UID (insn);
1418 if (! INSN_P (insn))
1421 for (link = df->insns[uid].uses; link; link = link->next)
1423 struct ref *use = link->ref;
1424 unsigned int uregno = DF_REF_REGNO (use);
1426 df->regs[uregno].uses
1427 = df_link_create (use, df->regs[uregno].uses);
1433 /* Create reg-use chains for each basic block within BLOCKS. These
1434 are a list of uses for each register. */
1436 df_reg_use_chain_create (df, blocks)
1442 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1444 df_bb_reg_use_chain_create (df, bb);
1449 /* Create def-use chains from reaching use bitmaps for basic block BB. */
1451 df_bb_du_chain_create (df, bb, ru)
1456 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1459 bitmap_copy (ru, bb_info->ru_out);
1461 /* For each def in BB create a linked list (chain) of uses
1462 reached from the def. */
1463 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1464 insn = PREV_INSN (insn))
1466 struct df_link *def_link;
1467 struct df_link *use_link;
1468 unsigned int uid = INSN_UID (insn);
1470 if (! INSN_P (insn))
1473 /* For each def in insn... */
1474 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1476 struct ref *def = def_link->ref;
1477 unsigned int dregno = DF_REF_REGNO (def);
1479 DF_REF_CHAIN (def) = 0;
1481 /* While the reg-use chains are not essential, it
1482 is _much_ faster to search these short lists rather
1483 than all the reaching uses, especially for large functions. */
1484 for (use_link = df->regs[dregno].uses; use_link;
1485 use_link = use_link->next)
1487 struct ref *use = use_link->ref;
1489 if (bitmap_bit_p (ru, DF_REF_ID (use)))
1492 = df_link_create (use, DF_REF_CHAIN (def));
1494 bitmap_clear_bit (ru, DF_REF_ID (use));
1499 /* For each use in insn... */
1500 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1502 struct ref *use = use_link->ref;
1503 bitmap_set_bit (ru, DF_REF_ID (use));
1509 /* Create def-use chains from reaching use bitmaps for basic blocks
1512 df_du_chain_create (df, blocks)
1519 ru = BITMAP_XMALLOC ();
1521 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1523 df_bb_du_chain_create (df, bb, ru);
1530 /* Create use-def chains from reaching def bitmaps for basic block BB. */
1532 df_bb_ud_chain_create (df, bb)
1536 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1537 struct ref **reg_def_last = df->reg_def_last;
1540 memset (reg_def_last, 0, df->n_regs * sizeof (struct ref *));
1542 /* For each use in BB create a linked list (chain) of defs
1543 that reach the use. */
1544 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1545 insn = NEXT_INSN (insn))
1547 unsigned int uid = INSN_UID (insn);
1548 struct df_link *use_link;
1549 struct df_link *def_link;
1551 if (! INSN_P (insn))
1554 /* For each use in insn... */
1555 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1557 struct ref *use = use_link->ref;
1558 unsigned int regno = DF_REF_REGNO (use);
1560 DF_REF_CHAIN (use) = 0;
1562 /* Has regno been defined in this BB yet? If so, use
1563 the last def as the single entry for the use-def
1564 chain for this use. Otherwise, we need to add all
1565 the defs using this regno that reach the start of
1567 if (reg_def_last[regno])
1570 = df_link_create (reg_def_last[regno], 0);
1574 /* While the reg-def chains are not essential, it is
1575 _much_ faster to search these short lists rather than
1576 all the reaching defs, especially for large
1578 for (def_link = df->regs[regno].defs; def_link;
1579 def_link = def_link->next)
1581 struct ref *def = def_link->ref;
1583 if (bitmap_bit_p (bb_info->rd_in, DF_REF_ID (def)))
1586 = df_link_create (def, DF_REF_CHAIN (use));
1593 /* For each def in insn...record the last def of each reg. */
1594 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1596 struct ref *def = def_link->ref;
1597 int dregno = DF_REF_REGNO (def);
1599 reg_def_last[dregno] = def;
1605 /* Create use-def chains from reaching def bitmaps for basic blocks
1608 df_ud_chain_create (df, blocks)
1614 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1616 df_bb_ud_chain_create (df, bb);
1621 /* Use reverse completion order, and the worklist, to figure out what block
1625 df_visit_next_rc (df, blocks)
1626 struct df *df ATTRIBUTE_UNUSED;
1630 for (i = 0; i < n_basic_blocks; i++)
1631 if (TEST_BIT (blocks, df->rc_order[i]))
1632 return df->rc_order[i];
1633 return sbitmap_first_set_bit (blocks);
1636 /* Use reverse topsort order, and the worklist, to figure out what block
1640 df_visit_next_rts (df, blocks)
1641 struct df *df ATTRIBUTE_UNUSED;
1645 for (i = 0; i < n_basic_blocks; i++)
1646 if (TEST_BIT (blocks, df->rts_order[i]))
1647 return df->rts_order[i];
1648 return sbitmap_first_set_bit (blocks);
1652 /* Calculate reaching defs for each basic block in BLOCKS, i.e., the
1653 defs that are live at the start of a basic block. */
1655 df_rd_global_compute (df, blocks)
1656 struct df *df ATTRIBUTE_UNUSED;
1663 worklist = sbitmap_alloc (n_basic_blocks);
1664 sbitmap_zero (worklist);
1666 /* Copy the blocklist to the worklist */
1667 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
1669 SET_BIT (worklist, i);
1672 /* We assume that only the basic blocks in WORKLIST have been
1674 FOR_EACH_BB_IN_SBITMAP (worklist, 0, bb,
1676 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1678 bitmap_copy (bb_info->rd_out, bb_info->rd_gen);
1681 while ((i = df_visit_next_rc (df, worklist)) >= 0)
1683 struct bb_info *bb_info;
1687 /* Remove this block from the worklist. */
1688 RESET_BIT (worklist, i);
1691 bb = BASIC_BLOCK (i);
1692 bb_info = DF_BB_INFO (df, bb);
1694 /* Calculate union of predecessor outs. */
1695 bitmap_zero (bb_info->rd_in);
1696 for (e = bb->pred; e != 0; e = e->pred_next)
1698 struct bb_info *pred_refs = DF_BB_INFO (df, e->src);
1700 if (e->src == ENTRY_BLOCK_PTR)
1703 bitmap_a_or_b (bb_info->rd_in, bb_info->rd_in,
1707 /* RD_OUT is the set of defs that are live at the end of the
1708 BB. These are the defs that are either generated by defs
1709 (RD_GEN) within the BB or are live at the start (RD_IN)
1710 and are not killed by other defs (RD_KILL). */
1711 changed = bitmap_union_of_diff (bb_info->rd_out, bb_info->rd_gen,
1712 bb_info->rd_in, bb_info->rd_kill);
1716 /* Add each of this block's successors to the worklist. */
1717 for (e = bb->succ; e != 0; e = e->succ_next)
1719 if (e->dest == EXIT_BLOCK_PTR)
1722 SET_BIT (worklist, e->dest->index);
1726 sbitmap_free (worklist);
1730 /* Calculate reaching uses for each basic block within BLOCKS, i.e.,
1731 the uses that are live at the start of a basic block. */
1733 df_ru_global_compute (df, blocks)
1734 struct df *df ATTRIBUTE_UNUSED;
1741 worklist = sbitmap_alloc (n_basic_blocks);
1742 sbitmap_zero (worklist);
1744 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
1746 SET_BIT (worklist, i);
1749 /* We assume that only the basic blocks in WORKLIST have been
1751 FOR_EACH_BB_IN_SBITMAP (worklist, 0, bb,
1753 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1755 bitmap_copy (bb_info->ru_in, bb_info->ru_gen);
1759 while ((i = df_visit_next_rts (df, worklist)) >= 0)
1761 struct bb_info *bb_info;
1765 /* Remove this block from the worklist. */
1766 RESET_BIT (worklist, i);
1768 bb = BASIC_BLOCK (i);
1769 bb_info = DF_BB_INFO (df, bb);
1771 /* Calculate union of successor ins. */
1772 bitmap_zero (bb_info->ru_out);
1773 for (e = bb->succ; e != 0; e = e->succ_next)
1775 struct bb_info *succ_refs = DF_BB_INFO (df, e->dest);
1777 if (e->dest == EXIT_BLOCK_PTR)
1780 bitmap_a_or_b (bb_info->ru_out, bb_info->ru_out,
1784 /* RU_IN is the set of uses that are live at the start of the
1785 BB. These are the uses that are either generated within the
1786 BB (RU_GEN) or are live at the end (RU_OUT) and are not uses
1787 killed by defs within the BB (RU_KILL). */
1788 changed = bitmap_union_of_diff (bb_info->ru_in, bb_info->ru_gen,
1789 bb_info->ru_out, bb_info->ru_kill);
1793 /* Add each of this block's predecessors to the worklist. */
1794 for (e = bb->pred; e != 0; e = e->pred_next)
1796 if (e->src == ENTRY_BLOCK_PTR)
1799 SET_BIT (worklist, e->src->index);
1804 sbitmap_free (worklist);
1808 /* Calculate live registers for each basic block within BLOCKS. */
1810 df_lr_global_compute (df, blocks)
1811 struct df *df ATTRIBUTE_UNUSED;
1818 worklist = BITMAP_XMALLOC ();
1819 bitmap_copy (worklist, blocks);
1821 /* We assume that only the basic blocks in WORKLIST have been
1823 FOR_EACH_BB_IN_BITMAP (worklist, 0, bb,
1825 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1827 bitmap_copy (bb_info->lr_in, bb_info->lr_use);
1830 while ((i = bitmap_last_set_bit (worklist)) >= 0)
1832 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1836 /* Remove this block from the worklist. */
1837 bitmap_clear_bit (worklist, i);
1839 bb = BASIC_BLOCK (i);
1840 bb_info = DF_BB_INFO (df, bb);
1842 /* Calculate union of successor ins. */
1843 bitmap_zero (bb_info->lr_out);
1844 for (e = bb->succ; e != 0; e = e->succ_next)
1846 struct bb_info *succ_refs = DF_BB_INFO (df, e->dest);
1848 if (e->dest == EXIT_BLOCK_PTR)
1851 bitmap_a_or_b (bb_info->lr_out, bb_info->lr_out,
1855 /* LR_IN is the set of uses that are live at the start of the
1856 BB. These are the uses that are either generated by uses
1857 (LR_USE) within the BB or are live at the end (LR_OUT)
1858 and are not killed by other uses (LR_DEF). */
1859 changed = bitmap_union_of_diff (bb_info->lr_in, bb_info->lr_use,
1860 bb_info->lr_out, bb_info->lr_def);
1864 /* Add each of this block's predecessors to the worklist. */
1865 for (e = bb->pred; e != 0; e = e->pred_next)
1867 if (e->src == ENTRY_BLOCK_PTR)
1870 bitmap_set_bit (worklist, e->src->index);
1874 BITMAP_XFREE (worklist);
1878 /* Compute local reaching def info for basic block BB. */
1880 df_bb_rd_local_compute (df, bb)
1884 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1887 for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
1888 insn = NEXT_INSN (insn))
1890 unsigned int uid = INSN_UID (insn);
1891 struct df_link *def_link;
1893 if (! INSN_P (insn))
1896 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1898 struct ref *def = def_link->ref;
1899 unsigned int regno = DF_REF_REGNO (def);
1900 struct df_link *def2_link;
1902 for (def2_link = df->regs[regno].defs; def2_link;
1903 def2_link = def2_link->next)
1905 struct ref *def2 = def2_link->ref;
1907 /* Add all defs of this reg to the set of kills. This
1908 is greedy since many of these defs will not actually
1909 be killed by this BB but it keeps things a lot
1911 bitmap_set_bit (bb_info->rd_kill, DF_REF_ID (def2));
1913 /* Zap from the set of gens for this BB. */
1914 bitmap_clear_bit (bb_info->rd_gen, DF_REF_ID (def2));
1917 bitmap_set_bit (bb_info->rd_gen, DF_REF_ID (def));
1921 bb_info->rd_valid = 1;
1925 /* Compute local reaching def info for each basic block within BLOCKS. */
1927 df_rd_local_compute (df, blocks)
1933 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
1935 df_bb_rd_local_compute (df, bb);
1940 /* Compute local reaching use (upward exposed use) info for basic
1943 df_bb_ru_local_compute (df, bb)
1947 /* This is much more tricky than computing reaching defs. With
1948 reaching defs, defs get killed by other defs. With upwards
1949 exposed uses, these get killed by defs with the same regno. */
1951 struct bb_info *bb_info = DF_BB_INFO (df, bb);
1954 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
1955 insn = PREV_INSN (insn))
1957 unsigned int uid = INSN_UID (insn);
1958 struct df_link *def_link;
1959 struct df_link *use_link;
1961 if (! INSN_P (insn))
1964 for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
1966 struct ref *def = def_link->ref;
1967 unsigned int dregno = DF_REF_REGNO (def);
1969 for (use_link = df->regs[dregno].uses; use_link;
1970 use_link = use_link->next)
1972 struct ref *use = use_link->ref;
1974 /* Add all uses of this reg to the set of kills. This
1975 is greedy since many of these uses will not actually
1976 be killed by this BB but it keeps things a lot
1978 bitmap_set_bit (bb_info->ru_kill, DF_REF_ID (use));
1980 /* Zap from the set of gens for this BB. */
1981 bitmap_clear_bit (bb_info->ru_gen, DF_REF_ID (use));
1985 for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
1987 struct ref *use = use_link->ref;
1988 /* Add use to set of gens in this BB. */
1989 bitmap_set_bit (bb_info->ru_gen, DF_REF_ID (use));
1992 bb_info->ru_valid = 1;
1996 /* Compute local reaching use (upward exposed use) info for each basic
1997 block within BLOCKS. */
1999 df_ru_local_compute (df, blocks)
2005 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2007 df_bb_ru_local_compute (df, bb);
2012 /* Compute local live variable info for basic block BB. */
2014 df_bb_lr_local_compute (df, bb)
2018 struct bb_info *bb_info = DF_BB_INFO (df, bb);
2021 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
2022 insn = PREV_INSN (insn))
2024 unsigned int uid = INSN_UID (insn);
2025 struct df_link *link;
2027 if (! INSN_P (insn))
2030 for (link = df->insns[uid].defs; link; link = link->next)
2032 struct ref *def = link->ref;
2033 unsigned int dregno = DF_REF_REGNO (def);
2035 /* Add def to set of defs in this BB. */
2036 bitmap_set_bit (bb_info->lr_def, dregno);
2038 bitmap_clear_bit (bb_info->lr_use, dregno);
2041 for (link = df->insns[uid].uses; link; link = link->next)
2043 struct ref *use = link->ref;
2044 /* Add use to set of uses in this BB. */
2045 bitmap_set_bit (bb_info->lr_use, DF_REF_REGNO (use));
2048 bb_info->lr_valid = 1;
2052 /* Compute local live variable info for each basic block within BLOCKS. */
2054 df_lr_local_compute (df, blocks)
2060 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2062 df_bb_lr_local_compute (df, bb);
2067 /* Compute register info: lifetime, bb, and number of defs and uses
2068 for basic block BB. */
2070 df_bb_reg_info_compute (df, bb, live)
2075 struct reg_info *reg_info = df->regs;
2076 struct bb_info *bb_info = DF_BB_INFO (df, bb);
2079 bitmap_copy (live, bb_info->lr_out);
2081 for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
2082 insn = PREV_INSN (insn))
2084 unsigned int uid = INSN_UID (insn);
2086 struct df_link *link;
2088 if (! INSN_P (insn))
2091 for (link = df->insns[uid].defs; link; link = link->next)
2093 struct ref *def = link->ref;
2094 unsigned int dregno = DF_REF_REGNO (def);
2096 /* Kill this register. */
2097 bitmap_clear_bit (live, dregno);
2098 reg_info[dregno].n_defs++;
2101 for (link = df->insns[uid].uses; link; link = link->next)
2103 struct ref *use = link->ref;
2104 unsigned int uregno = DF_REF_REGNO (use);
2106 /* This register is now live. */
2107 bitmap_set_bit (live, uregno);
2108 reg_info[uregno].n_uses++;
2111 /* Increment lifetimes of all live registers. */
2112 EXECUTE_IF_SET_IN_BITMAP (live, 0, regno,
2114 reg_info[regno].lifetime++;
2120 /* Compute register info: lifetime, bb, and number of defs and uses. */
2122 df_reg_info_compute (df, blocks)
2129 live = BITMAP_XMALLOC ();
2131 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2133 df_bb_reg_info_compute (df, bb, live);
2136 BITMAP_XFREE (live);
2140 /* Assign LUIDs for BB. */
2142 df_bb_luids_set (df, bb)
2149 /* The LUIDs are monotonically increasing for each basic block. */
2151 for (insn = bb->head; ; insn = NEXT_INSN (insn))
2154 DF_INSN_LUID (df, insn) = luid++;
2155 DF_INSN_LUID (df, insn) = luid;
2157 if (insn == bb->end)
2164 /* Assign LUIDs for each basic block within BLOCKS. */
2166 df_luids_set (df, blocks)
2173 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2175 total += df_bb_luids_set (df, bb);
2181 /* Perform dataflow analysis using existing DF structure for blocks
2182 within BLOCKS. If BLOCKS is zero, use all basic blocks in the CFG. */
2184 df_analyse_1 (df, blocks, flags, update)
2195 if (flags & DF_UD_CHAIN)
2196 aflags |= DF_RD | DF_RD_CHAIN;
2198 if (flags & DF_DU_CHAIN)
2202 aflags |= DF_RU_CHAIN;
2204 if (flags & DF_REG_INFO)
2208 blocks = df->all_blocks;
2213 df_refs_update (df);
2214 /* More fine grained incremental dataflow analysis would be
2215 nice. For now recompute the whole shebang for the
2218 df_refs_unlink (df, blocks);
2220 /* All the def-use, use-def chains can be potentially
2221 modified by changes in one block. The size of the
2222 bitmaps can also change. */
2226 /* Scan the function for all register defs and uses. */
2228 df_refs_record (df, blocks);
2230 /* Link all the new defs and uses to the insns. */
2231 df_refs_process (df);
2234 /* Allocate the bitmaps now the total number of defs and uses are
2235 known. If the number of defs or uses have changed, then
2236 these bitmaps need to be reallocated. */
2237 df_bitmaps_alloc (df, aflags);
2239 /* Set the LUIDs for each specified basic block. */
2240 df_luids_set (df, blocks);
2242 /* Recreate reg-def and reg-use chains from scratch so that first
2243 def is at the head of the reg-def chain and the last use is at
2244 the head of the reg-use chain. This is only important for
2245 regs local to a basic block as it speeds up searching. */
2246 if (aflags & DF_RD_CHAIN)
2248 df_reg_def_chain_create (df, blocks);
2251 if (aflags & DF_RU_CHAIN)
2253 df_reg_use_chain_create (df, blocks);
2256 df->dfs_order = xmalloc (sizeof(int) * n_basic_blocks);
2257 df->rc_order = xmalloc (sizeof(int) * n_basic_blocks);
2258 df->rts_order = xmalloc (sizeof(int) * n_basic_blocks);
2260 flow_depth_first_order_compute (df->dfs_order, df->rc_order);
2261 flow_reverse_top_sort_order_compute (df->rts_order);
2264 /* Compute the sets of gens and kills for the defs of each bb. */
2265 df_rd_local_compute (df, df->flags & DF_RD ? blocks : df->all_blocks);
2267 /* Compute the global reaching definitions. */
2268 df_rd_global_compute (df, df->all_blocks);
2271 if (aflags & DF_UD_CHAIN)
2273 /* Create use-def chains. */
2274 df_ud_chain_create (df, df->all_blocks);
2276 if (! (flags & DF_RD))
2282 /* Compute the sets of gens and kills for the upwards exposed
2284 df_ru_local_compute (df, df->flags & DF_RU ? blocks : df->all_blocks);
2286 /* Compute the global reaching uses. */
2287 df_ru_global_compute (df, df->all_blocks);
2290 if (aflags & DF_DU_CHAIN)
2292 /* Create def-use chains. */
2293 df_du_chain_create (df, df->all_blocks);
2295 if (! (flags & DF_RU))
2299 /* Free up bitmaps that are no longer required. */
2301 df_bitmaps_free (df, dflags);
2305 /* Compute the sets of defs and uses of live variables. */
2306 df_lr_local_compute (df, df->flags & DF_LR ? blocks : df->all_blocks);
2308 /* Compute the global live variables. */
2309 df_lr_global_compute (df, df->all_blocks);
2312 if (aflags & DF_REG_INFO)
2314 df_reg_info_compute (df, df->all_blocks);
2316 free (df->dfs_order);
2317 free (df->rc_order);
2318 free (df->rts_order);
2322 /* Initialise dataflow analysis. */
2328 df = xcalloc (1, sizeof (struct df));
2330 /* Squirrel away a global for debugging. */
2337 /* Start queuing refs. */
2342 df->def_id_save = df->def_id;
2343 df->use_id_save = df->use_id;
2344 /* ???? Perhaps we should save current obstack state so that we can
2350 /* Process queued refs. */
2352 df_refs_process (df)
2357 /* Build new insn-def chains. */
2358 for (i = df->def_id_save; i != df->def_id; i++)
2360 struct ref *def = df->defs[i];
2361 unsigned int uid = DF_REF_INSN_UID (def);
2363 /* Add def to head of def list for INSN. */
2365 = df_link_create (def, df->insns[uid].defs);
2368 /* Build new insn-use chains. */
2369 for (i = df->use_id_save; i != df->use_id; i++)
2371 struct ref *use = df->uses[i];
2372 unsigned int uid = DF_REF_INSN_UID (use);
2374 /* Add use to head of use list for INSN. */
2376 = df_link_create (use, df->insns[uid].uses);
2382 /* Update refs for basic block BB. */
2384 df_bb_refs_update (df, bb)
2391 /* While we have to scan the chain of insns for this BB, we don't
2392 need to allocate and queue a long chain of BB/INSN pairs. Using
2393 a bitmap for insns_modified saves memory and avoids queuing
2396 for (insn = bb->head; ; insn = NEXT_INSN (insn))
2400 uid = INSN_UID (insn);
2402 if (bitmap_bit_p (df->insns_modified, uid))
2404 /* Delete any allocated refs of this insn. MPH, FIXME. */
2405 df_insn_refs_unlink (df, bb, insn);
2407 /* Scan the insn for refs. */
2408 df_insn_refs_record (df, bb, insn);
2411 bitmap_clear_bit (df->insns_modified, uid);
2414 if (insn == bb->end)
2421 /* Process all the modified/deleted insns that were queued. */
2429 if ((unsigned int)max_reg_num () >= df->reg_size)
2430 df_reg_table_realloc (df, 0);
2434 FOR_EACH_BB_IN_BITMAP (df->bbs_modified, 0, bb,
2436 count += df_bb_refs_update (df, bb);
2439 df_refs_process (df);
2444 /* Return non-zero if any of the requested blocks in the bitmap
2445 BLOCKS have been modified. */
2447 df_modified_p (df, blocks)
2454 for (j = 0; j < df->n_bbs; j++)
2455 if (bitmap_bit_p (df->bbs_modified, j)
2456 && (! blocks || (blocks == (bitmap) -1) || bitmap_bit_p (blocks, j)))
2466 /* Analyse dataflow info for the basic blocks specified by the bitmap
2467 BLOCKS, or for the whole CFG if BLOCKS is zero, or just for the
2468 modified blocks if BLOCKS is -1. */
2470 df_analyse (df, blocks, flags)
2477 /* We could deal with additional basic blocks being created by
2478 rescanning everything again. */
2479 if (df->n_bbs && df->n_bbs != (unsigned int)n_basic_blocks)
2482 update = df_modified_p (df, blocks);
2483 if (update || (flags != df->flags))
2489 /* Recompute everything from scratch. */
2492 /* Allocate and initialise data structures. */
2493 df_alloc (df, max_reg_num ());
2494 df_analyse_1 (df, 0, flags, 0);
2499 if (blocks == (bitmap) -1)
2500 blocks = df->bbs_modified;
2505 df_analyse_1 (df, blocks, flags, 1);
2506 bitmap_zero (df->bbs_modified);
2513 /* Free all the dataflow info and the DF structure. */
2523 /* Unlink INSN from its reference information. */
2525 df_insn_refs_unlink (df, bb, insn)
2527 basic_block bb ATTRIBUTE_UNUSED;
2530 struct df_link *link;
2533 uid = INSN_UID (insn);
2535 /* Unlink all refs defined by this insn. */
2536 for (link = df->insns[uid].defs; link; link = link->next)
2537 df_def_unlink (df, link->ref);
2539 /* Unlink all refs used by this insn. */
2540 for (link = df->insns[uid].uses; link; link = link->next)
2541 df_use_unlink (df, link->ref);
2543 df->insns[uid].defs = 0;
2544 df->insns[uid].uses = 0;
2549 /* Unlink all the insns within BB from their reference information. */
2551 df_bb_refs_unlink (df, bb)
2557 /* Scan the block an insn at a time from beginning to end. */
2558 for (insn = bb->head; ; insn = NEXT_INSN (insn))
2562 /* Unlink refs for INSN. */
2563 df_insn_refs_unlink (df, bb, insn);
2565 if (insn == bb->end)
2571 /* Unlink all the refs in the basic blocks specified by BLOCKS.
2572 Not currently used. */
2574 df_refs_unlink (df, blocks)
2582 FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
2584 df_bb_refs_unlink (df, bb);
2591 df_bb_refs_unlink (df, bb);
2597 /* Functions to modify insns. */
2600 /* Delete INSN and all its reference information. */
2602 df_insn_delete (df, bb, insn)
2604 basic_block bb ATTRIBUTE_UNUSED;
2607 /* If the insn is a jump, we should perhaps call delete_insn to
2608 handle the JUMP_LABEL? */
2610 /* We should not be deleting the NOTE_INSN_BASIC_BLOCK or label. */
2611 if (insn == bb->head)
2614 /* Delete the insn. */
2617 df_insn_modify (df, bb, insn);
2619 return NEXT_INSN (insn);
2623 /* Mark that INSN within BB may have changed (created/modified/deleted).
2624 This may be called multiple times for the same insn. There is no
2625 harm calling this function if the insn wasn't changed; it will just
2626 slow down the rescanning of refs. */
2628 df_insn_modify (df, bb, insn)
2635 uid = INSN_UID (insn);
2637 if (uid >= df->insn_size)
2638 df_insn_table_realloc (df, 0);
2640 bitmap_set_bit (df->bbs_modified, bb->index);
2641 bitmap_set_bit (df->insns_modified, uid);
2644 /* For incremental updating on the fly, perhaps we could make a copy
2645 of all the refs of the original insn and turn them into
2646 anti-refs. When df_refs_update finds these anti-refs, it annihilates
2647 the original refs. If validate_change fails then these anti-refs
2648 will just get ignored. */
2654 typedef struct replace_args
2663 /* Replace mem pointed to by PX with its associated pseudo register.
2664 DATA is actually a pointer to a structure describing the
2665 instruction currently being scanned and the MEM we are currently
2668 df_rtx_mem_replace (px, data)
2672 replace_args *args = (replace_args *) data;
2675 if (mem == NULL_RTX)
2678 switch (GET_CODE (mem))
2684 /* We're not interested in the MEM associated with a
2685 CONST_DOUBLE, so there's no need to traverse into one. */
2689 /* This is not a MEM. */
2693 if (!rtx_equal_p (args->match, mem))
2694 /* This is not the MEM we are currently replacing. */
2697 /* Actually replace the MEM. */
2698 validate_change (args->insn, px, args->replacement, 1);
2706 df_insn_mem_replace (df, bb, insn, mem, reg)
2717 args.replacement = reg;
2720 /* Seach and replace all matching mems within insn. */
2721 for_each_rtx (&insn, df_rtx_mem_replace, &args);
2724 df_insn_modify (df, bb, insn);
2726 /* ???? FIXME. We may have a new def or one or more new uses of REG
2727 in INSN. REG should be a new pseudo so it won't affect the
2728 dataflow information that we currently have. We should add
2729 the new uses and defs to INSN and then recreate the chains
2730 when df_analyse is called. */
2731 return args.modified;
2735 /* Replace one register with another. Called through for_each_rtx; PX
2736 points to the rtx being scanned. DATA is actually a pointer to a
2737 structure of arguments. */
2739 df_rtx_reg_replace (px, data)
2744 replace_args *args = (replace_args *) data;
2749 if (x == args->match)
2751 validate_change (args->insn, px, args->replacement, 1);
2759 /* Replace the reg within every ref on CHAIN that is within the set
2760 BLOCKS of basic blocks with NEWREG. Also update the regs within
2763 df_refs_reg_replace (df, blocks, chain, oldreg, newreg)
2766 struct df_link *chain;
2770 struct df_link *link;
2774 blocks = df->all_blocks;
2776 args.match = oldreg;
2777 args.replacement = newreg;
2780 for (link = chain; link; link = link->next)
2782 struct ref *ref = link->ref;
2783 rtx insn = DF_REF_INSN (ref);
2785 if (! INSN_P (insn))
2788 if (bitmap_bit_p (blocks, DF_REF_BBNO (ref)))
2790 df_ref_reg_replace (df, ref, oldreg, newreg);
2792 /* Replace occurrences of the reg within the REG_NOTES. */
2793 if ((! link->next || DF_REF_INSN (ref)
2794 != DF_REF_INSN (link->next->ref))
2795 && REG_NOTES (insn))
2798 for_each_rtx (®_NOTES (insn), df_rtx_reg_replace, &args);
2803 /* Temporary check to ensure that we have a grip on which
2804 regs should be replaced. */
2811 /* Replace all occurrences of register OLDREG with register NEWREG in
2812 blocks defined by bitmap BLOCKS. This also replaces occurrences of
2813 OLDREG in the REG_NOTES but only for insns containing OLDREG. This
2814 routine expects the reg-use and reg-def chains to be valid. */
2816 df_reg_replace (df, blocks, oldreg, newreg)
2822 unsigned int oldregno = REGNO (oldreg);
2824 df_refs_reg_replace (df, blocks, df->regs[oldregno].defs, oldreg, newreg);
2825 df_refs_reg_replace (df, blocks, df->regs[oldregno].uses, oldreg, newreg);
2830 /* Try replacing the reg within REF with NEWREG. Do not modify
2831 def-use/use-def chains. */
2833 df_ref_reg_replace (df, ref, oldreg, newreg)
2839 /* Check that insn was deleted by being converted into a NOTE. If
2840 so ignore this insn. */
2841 if (! INSN_P (DF_REF_INSN (ref)))
2844 if (oldreg && oldreg != DF_REF_REG (ref))
2847 if (! validate_change (DF_REF_INSN (ref), DF_REF_LOC (ref), newreg, 1))
2850 df_insn_modify (df, DF_REF_BB (ref), DF_REF_INSN (ref));
2856 df_bb_def_use_swap (df, bb, def_insn, use_insn, regno)
2867 struct df_link *link;
2869 def = df_bb_insn_regno_first_def_find (df, bb, def_insn, regno);
2873 use = df_bb_insn_regno_last_use_find (df, bb, use_insn, regno);
2877 /* The USE no longer exists. */
2878 use_uid = INSN_UID (use_insn);
2879 df_use_unlink (df, use);
2880 df_ref_unlink (&df->insns[use_uid].uses, use);
2882 /* The DEF requires shifting so remove it from DEF_INSN
2883 and add it to USE_INSN by reusing LINK. */
2884 def_uid = INSN_UID (def_insn);
2885 link = df_ref_unlink (&df->insns[def_uid].defs, def);
2887 link->next = df->insns[use_uid].defs;
2888 df->insns[use_uid].defs = link;
2891 link = df_ref_unlink (&df->regs[regno].defs, def);
2893 link->next = df->regs[regno].defs;
2894 df->insns[regno].defs = link;
2897 DF_REF_INSN (def) = use_insn;
2902 /* Record df between FIRST_INSN and LAST_INSN inclusive. All new
2903 insns must be processed by this routine. */
2905 df_insns_modify (df, bb, first_insn, last_insn)
2913 for (insn = first_insn; ; insn = NEXT_INSN (insn))
2917 /* A non-const call should not have slipped through the net. If
2918 it does, we need to create a new basic block. Ouch. The
2919 same applies for a label. */
2920 if ((GET_CODE (insn) == CALL_INSN
2921 && ! CONST_OR_PURE_CALL_P (insn))
2922 || GET_CODE (insn) == CODE_LABEL)
2925 uid = INSN_UID (insn);
2927 if (uid >= df->insn_size)
2928 df_insn_table_realloc (df, 0);
2930 df_insn_modify (df, bb, insn);
2932 if (insn == last_insn)
2938 /* Emit PATTERN before INSN within BB. */
2940 df_pattern_emit_before (df, pattern, bb, insn)
2941 struct df *df ATTRIBUTE_UNUSED;
2947 rtx prev_insn = PREV_INSN (insn);
2949 /* We should not be inserting before the start of the block. */
2950 if (insn == bb->head)
2952 ret_insn = emit_insn_before (pattern, insn);
2953 if (ret_insn == insn)
2956 df_insns_modify (df, bb, NEXT_INSN (prev_insn), ret_insn);
2961 /* Emit PATTERN after INSN within BB. */
2963 df_pattern_emit_after (df, pattern, bb, insn)
2971 ret_insn = emit_insn_after (pattern, insn);
2972 if (ret_insn == insn)
2975 df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
2980 /* Emit jump PATTERN after INSN within BB. */
2982 df_jump_pattern_emit_after (df, pattern, bb, insn)
2990 ret_insn = emit_jump_insn_after (pattern, insn);
2991 if (ret_insn == insn)
2994 df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
2999 /* Move INSN within BB before BEFORE_INSN within BEFORE_BB.
3001 This function should only be used to move loop invariant insns
3002 out of a loop where it has been proven that the def-use info
3003 will still be valid. */
3005 df_insn_move_before (df, bb, insn, before_bb, before_insn)
3009 basic_block before_bb;
3012 struct df_link *link;
3016 return df_pattern_emit_before (df, insn, before_bb, before_insn);
3018 uid = INSN_UID (insn);
3020 /* Change bb for all df defined and used by this insn. */
3021 for (link = df->insns[uid].defs; link; link = link->next)
3022 DF_REF_BB (link->ref) = before_bb;
3023 for (link = df->insns[uid].uses; link; link = link->next)
3024 DF_REF_BB (link->ref) = before_bb;
3026 /* The lifetimes of the registers used in this insn will be reduced
3027 while the lifetimes of the registers defined in this insn
3028 are likely to be increased. */
3030 /* ???? Perhaps all the insns moved should be stored on a list
3031 which df_analyse removes when it recalculates data flow. */
3033 return emit_insn_before (insn, before_insn);
3036 /* Functions to query dataflow information. */
3040 df_insn_regno_def_p (df, bb, insn, regno)
3042 basic_block bb ATTRIBUTE_UNUSED;
3047 struct df_link *link;
3049 uid = INSN_UID (insn);
3051 for (link = df->insns[uid].defs; link; link = link->next)
3053 struct ref *def = link->ref;
3055 if (DF_REF_REGNO (def) == regno)
3064 df_def_dominates_all_uses_p (df, def)
3065 struct df *df ATTRIBUTE_UNUSED;
3068 struct df_link *du_link;
3070 /* Follow def-use chain to find all the uses of this def. */
3071 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
3073 struct ref *use = du_link->ref;
3074 struct df_link *ud_link;
3076 /* Follow use-def chain to check all the defs for this use. */
3077 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
3078 if (ud_link->ref != def)
3086 df_insn_dominates_all_uses_p (df, bb, insn)
3088 basic_block bb ATTRIBUTE_UNUSED;
3092 struct df_link *link;
3094 uid = INSN_UID (insn);
3096 for (link = df->insns[uid].defs; link; link = link->next)
3098 struct ref *def = link->ref;
3100 if (! df_def_dominates_all_uses_p (df, def))
3108 /* Return non-zero if all DF dominates all the uses within the bitmap
3111 df_def_dominates_uses_p (df, def, blocks)
3112 struct df *df ATTRIBUTE_UNUSED;
3116 struct df_link *du_link;
3118 /* Follow def-use chain to find all the uses of this def. */
3119 for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
3121 struct ref *use = du_link->ref;
3122 struct df_link *ud_link;
3124 /* Only worry about the uses within BLOCKS. For example,
3125 consider a register defined within a loop that is live at the
3127 if (bitmap_bit_p (blocks, DF_REF_BBNO (use)))
3129 /* Follow use-def chain to check all the defs for this use. */
3130 for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
3131 if (ud_link->ref != def)
3139 /* Return non-zero if all the defs of INSN within BB dominates
3140 all the corresponding uses. */
3142 df_insn_dominates_uses_p (df, bb, insn, blocks)
3144 basic_block bb ATTRIBUTE_UNUSED;
3149 struct df_link *link;
3151 uid = INSN_UID (insn);
3153 for (link = df->insns[uid].defs; link; link = link->next)
3155 struct ref *def = link->ref;
3157 /* Only consider the defs within BLOCKS. */
3158 if (bitmap_bit_p (blocks, DF_REF_BBNO (def))
3159 && ! df_def_dominates_uses_p (df, def, blocks))
3166 /* Return the basic block that REG referenced in or NULL if referenced
3167 in multiple basic blocks. */
3169 df_regno_bb (df, regno)
3173 struct df_link *defs = df->regs[regno].defs;
3174 struct df_link *uses = df->regs[regno].uses;
3175 struct ref *def = defs ? defs->ref : 0;
3176 struct ref *use = uses ? uses->ref : 0;
3177 basic_block bb_def = def ? DF_REF_BB (def) : 0;
3178 basic_block bb_use = use ? DF_REF_BB (use) : 0;
3180 /* Compare blocks of first def and last use. ???? FIXME. What if
3181 the reg-def and reg-use lists are not correctly ordered. */
3182 return bb_def == bb_use ? bb_def : 0;
3186 /* Return non-zero if REG used in multiple basic blocks. */
3188 df_reg_global_p (df, reg)
3192 return df_regno_bb (df, REGNO (reg)) != 0;
3196 /* Return total lifetime (in insns) of REG. */
3198 df_reg_lifetime (df, reg)
3202 return df->regs[REGNO (reg)].lifetime;
3206 /* Return non-zero if REG live at start of BB. */
3208 df_bb_reg_live_start_p (df, bb, reg)
3209 struct df *df ATTRIBUTE_UNUSED;
3213 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3215 #ifdef ENABLE_CHECKING
3216 if (! bb_info->lr_in)
3220 return bitmap_bit_p (bb_info->lr_in, REGNO (reg));
3224 /* Return non-zero if REG live at end of BB. */
3226 df_bb_reg_live_end_p (df, bb, reg)
3227 struct df *df ATTRIBUTE_UNUSED;
3231 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3233 #ifdef ENABLE_CHECKING
3234 if (! bb_info->lr_in)
3238 return bitmap_bit_p (bb_info->lr_out, REGNO (reg));
3242 /* Return -1 if life of REG1 before life of REG2, 1 if life of REG1
3243 after life of REG2, or 0, if the lives overlap. */
3245 df_bb_regs_lives_compare (df, bb, reg1, reg2)
3251 unsigned int regno1 = REGNO (reg1);
3252 unsigned int regno2 = REGNO (reg2);
3259 /* The regs must be local to BB. */
3260 if (df_regno_bb (df, regno1) != bb
3261 || df_regno_bb (df, regno2) != bb)
3264 def2 = df_bb_regno_first_def_find (df, bb, regno2);
3265 use1 = df_bb_regno_last_use_find (df, bb, regno1);
3267 if (DF_INSN_LUID (df, DF_REF_INSN (def2))
3268 > DF_INSN_LUID (df, DF_REF_INSN (use1)))
3271 def1 = df_bb_regno_first_def_find (df, bb, regno1);
3272 use2 = df_bb_regno_last_use_find (df, bb, regno2);
3274 if (DF_INSN_LUID (df, DF_REF_INSN (def1))
3275 > DF_INSN_LUID (df, DF_REF_INSN (use2)))
3282 /* Return last use of REGNO within BB. */
3284 df_bb_regno_last_use_find (df, bb, regno)
3286 basic_block bb ATTRIBUTE_UNUSED;
3289 struct df_link *link;
3291 /* This assumes that the reg-use list is ordered such that for any
3292 BB, the last use is found first. However, since the BBs are not
3293 ordered, the first use in the chain is not necessarily the last
3294 use in the function. */
3295 for (link = df->regs[regno].uses; link; link = link->next)
3297 struct ref *use = link->ref;
3299 if (DF_REF_BB (use) == bb)
3306 /* Return first def of REGNO within BB. */
3308 df_bb_regno_first_def_find (df, bb, regno)
3310 basic_block bb ATTRIBUTE_UNUSED;
3313 struct df_link *link;
3315 /* This assumes that the reg-def list is ordered such that for any
3316 BB, the first def is found first. However, since the BBs are not
3317 ordered, the first def in the chain is not necessarily the first
3318 def in the function. */
3319 for (link = df->regs[regno].defs; link; link = link->next)
3321 struct ref *def = link->ref;
3323 if (DF_REF_BB (def) == bb)
3330 /* Return first use of REGNO inside INSN within BB. */
3332 df_bb_insn_regno_last_use_find (df, bb, insn, regno)
3334 basic_block bb ATTRIBUTE_UNUSED;
3339 struct df_link *link;
3341 uid = INSN_UID (insn);
3343 for (link = df->insns[uid].uses; link; link = link->next)
3345 struct ref *use = link->ref;
3347 if (DF_REF_REGNO (use) == regno)
3355 /* Return first def of REGNO inside INSN within BB. */
3357 df_bb_insn_regno_first_def_find (df, bb, insn, regno)
3359 basic_block bb ATTRIBUTE_UNUSED;
3364 struct df_link *link;
3366 uid = INSN_UID (insn);
3368 for (link = df->insns[uid].defs; link; link = link->next)
3370 struct ref *def = link->ref;
3372 if (DF_REF_REGNO (def) == regno)
3380 /* Return insn using REG if the BB contains only a single
3381 use and def of REG. */
3383 df_bb_single_def_use_insn_find (df, bb, insn, reg)
3391 struct df_link *du_link;
3393 def = df_bb_insn_regno_first_def_find (df, bb, insn, REGNO (reg));
3398 du_link = DF_REF_CHAIN (def);
3405 /* Check if def is dead. */
3409 /* Check for multiple uses. */
3413 return DF_REF_INSN (use);
3416 /* Functions for debugging/dumping dataflow information. */
3419 /* Dump a def-use or use-def chain for REF to FILE. */
3421 df_chain_dump (link, file)
3422 struct df_link *link;
3425 fprintf (file, "{ ");
3426 for (; link; link = link->next)
3428 fprintf (file, "%c%d ",
3429 DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
3430 DF_REF_ID (link->ref));
3432 fprintf (file, "}");
3436 df_chain_dump_regno (link, file)
3437 struct df_link *link;
3440 fprintf (file, "{ ");
3441 for (; link; link = link->next)
3443 fprintf (file, "%c%d(%d) ",
3444 DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
3445 DF_REF_ID (link->ref),
3446 DF_REF_REGNO (link->ref));
3448 fprintf (file, "}");
3451 /* Dump dataflow info. */
3453 df_dump (df, flags, file)
3464 fprintf (file, "\nDataflow summary:\n");
3465 fprintf (file, "n_regs = %d, n_defs = %d, n_uses = %d, n_bbs = %d\n",
3466 df->n_regs, df->n_defs, df->n_uses, df->n_bbs);
3470 fprintf (file, "Reaching defs:\n");
3471 for (i = 0; i < df->n_bbs; i++)
3473 basic_block bb = BASIC_BLOCK (i);
3474 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3476 if (! bb_info->rd_in)
3479 fprintf (file, "bb %d in \t", i);
3480 dump_bitmap (file, bb_info->rd_in);
3481 fprintf (file, "bb %d gen \t", i);
3482 dump_bitmap (file, bb_info->rd_gen);
3483 fprintf (file, "bb %d kill\t", i);
3484 dump_bitmap (file, bb_info->rd_kill);
3485 fprintf (file, "bb %d out \t", i);
3486 dump_bitmap (file, bb_info->rd_out);
3490 if (flags & DF_UD_CHAIN)
3492 fprintf (file, "Use-def chains:\n");
3493 for (j = 0; j < df->n_defs; j++)
3497 fprintf (file, "d%d bb %d luid %d insn %d reg %d ",
3498 j, DF_REF_BBNO (df->defs[j]),
3499 DF_INSN_LUID (df, DF_REF_INSN (df->defs[j])),
3500 DF_REF_INSN_UID (df->defs[j]),
3501 DF_REF_REGNO (df->defs[j]));
3502 df_chain_dump (DF_REF_CHAIN (df->defs[j]), file);
3503 fprintf (file, "\n");
3510 fprintf (file, "Reaching uses:\n");
3511 for (i = 0; i < df->n_bbs; i++)
3513 basic_block bb = BASIC_BLOCK (i);
3514 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3516 if (! bb_info->ru_in)
3519 fprintf (file, "bb %d in \t", i);
3520 dump_bitmap (file, bb_info->ru_in);
3521 fprintf (file, "bb %d gen \t", i);
3522 dump_bitmap (file, bb_info->ru_gen);
3523 fprintf (file, "bb %d kill\t", i);
3524 dump_bitmap (file, bb_info->ru_kill);
3525 fprintf (file, "bb %d out \t", i);
3526 dump_bitmap (file, bb_info->ru_out);
3530 if (flags & DF_DU_CHAIN)
3532 fprintf (file, "Def-use chains:\n");
3533 for (j = 0; j < df->n_uses; j++)
3537 fprintf (file, "u%d bb %d luid %d insn %d reg %d ",
3538 j, DF_REF_BBNO (df->uses[j]),
3539 DF_INSN_LUID (df, DF_REF_INSN (df->uses[j])),
3540 DF_REF_INSN_UID (df->uses[j]),
3541 DF_REF_REGNO (df->uses[j]));
3542 df_chain_dump (DF_REF_CHAIN (df->uses[j]), file);
3543 fprintf (file, "\n");
3550 fprintf (file, "Live regs:\n");
3551 for (i = 0; i < df->n_bbs; i++)
3553 basic_block bb = BASIC_BLOCK (i);
3554 struct bb_info *bb_info = DF_BB_INFO (df, bb);
3556 if (! bb_info->lr_in)
3559 fprintf (file, "bb %d in \t", i);
3560 dump_bitmap (file, bb_info->lr_in);
3561 fprintf (file, "bb %d use \t", i);
3562 dump_bitmap (file, bb_info->lr_use);
3563 fprintf (file, "bb %d def \t", i);
3564 dump_bitmap (file, bb_info->lr_def);
3565 fprintf (file, "bb %d out \t", i);
3566 dump_bitmap (file, bb_info->lr_out);
3570 if (flags & (DF_REG_INFO | DF_RD_CHAIN | DF_RU_CHAIN))
3572 struct reg_info *reg_info = df->regs;
3574 fprintf (file, "Register info:\n");
3575 for (j = 0; j < df->n_regs; j++)
3577 if (((flags & DF_REG_INFO)
3578 && (reg_info[j].n_uses || reg_info[j].n_defs))
3579 || ((flags & DF_RD_CHAIN) && reg_info[j].defs)
3580 || ((flags & DF_RU_CHAIN) && reg_info[j].uses))
3582 fprintf (file, "reg %d", j);
3583 if ((flags & DF_RD_CHAIN) && (flags & DF_RU_CHAIN))
3585 basic_block bb = df_regno_bb (df, j);
3588 fprintf (file, " bb %d", bb->index);
3590 fprintf (file, " bb ?");
3592 if (flags & DF_REG_INFO)
3594 fprintf (file, " life %d", reg_info[j].lifetime);
3597 if ((flags & DF_REG_INFO) || (flags & DF_RD_CHAIN))
3599 fprintf (file, " defs ");
3600 if (flags & DF_REG_INFO)
3601 fprintf (file, "%d ", reg_info[j].n_defs);
3602 if (flags & DF_RD_CHAIN)
3603 df_chain_dump (reg_info[j].defs, file);
3606 if ((flags & DF_REG_INFO) || (flags & DF_RU_CHAIN))
3608 fprintf (file, " uses ");
3609 if (flags & DF_REG_INFO)
3610 fprintf (file, "%d ", reg_info[j].n_uses);
3611 if (flags & DF_RU_CHAIN)
3612 df_chain_dump (reg_info[j].uses, file);
3615 fprintf (file, "\n");
3619 fprintf (file, "\n");
3624 df_insn_debug (df, insn, file)
3632 uid = INSN_UID (insn);
3633 if (uid >= df->insn_size)
3636 if (df->insns[uid].defs)
3637 bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
3638 else if (df->insns[uid].uses)
3639 bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
3643 fprintf (file, "insn %d bb %d luid %d defs ",
3644 uid, bbi, DF_INSN_LUID (df, insn));
3645 df_chain_dump (df->insns[uid].defs, file);
3646 fprintf (file, " uses ");
3647 df_chain_dump (df->insns[uid].uses, file);
3648 fprintf (file, "\n");
3652 df_insn_debug_regno (df, insn, file)
3660 uid = INSN_UID (insn);
3661 if (uid >= df->insn_size)
3664 if (df->insns[uid].defs)
3665 bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
3666 else if (df->insns[uid].uses)
3667 bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
3671 fprintf (file, "insn %d bb %d luid %d defs ",
3672 uid, bbi, DF_INSN_LUID (df, insn));
3673 df_chain_dump_regno (df->insns[uid].defs, file);
3674 fprintf (file, " uses ");
3675 df_chain_dump_regno (df->insns[uid].uses, file);
3676 fprintf (file, "\n");
3680 df_regno_debug (df, regno, file)
3685 if (regno >= df->reg_size)
3688 fprintf (file, "reg %d life %d defs ",
3689 regno, df->regs[regno].lifetime);
3690 df_chain_dump (df->regs[regno].defs, file);
3691 fprintf (file, " uses ");
3692 df_chain_dump (df->regs[regno].uses, file);
3693 fprintf (file, "\n");
3698 df_ref_debug (df, ref, file)
3703 fprintf (file, "%c%d ",
3704 DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
3706 fprintf (file, "reg %d bb %d luid %d insn %d chain ",
3709 DF_INSN_LUID (df, DF_REF_INSN (ref)),
3710 INSN_UID (DF_REF_INSN (ref)));
3711 df_chain_dump (DF_REF_CHAIN (ref), file);
3712 fprintf (file, "\n");
3717 debug_df_insn (insn)
3720 df_insn_debug (ddf, insn, stderr);
3729 df_regno_debug (ddf, REGNO (reg), stderr);
3734 debug_df_regno (regno)
3737 df_regno_debug (ddf, regno, stderr);
3745 df_ref_debug (ddf, ref, stderr);
3750 debug_df_defno (defno)
3753 df_ref_debug (ddf, ddf->defs[defno], stderr);
3758 debug_df_useno (defno)
3761 df_ref_debug (ddf, ddf->uses[defno], stderr);
3766 debug_df_chain (link)
3767 struct df_link *link;
3769 df_chain_dump (link, stderr);
3770 fputc ('\n', stderr);