1 /* Dataflow support routines.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz,
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 This file provides some dataflow routines for computing reaching defs,
28 upward exposed uses, live variables, def-use chains, and use-def
29 chains. The global dataflow is performed using simple iterative
30 methods with a worklist and could be sped up by ordering the blocks
31 with a depth first search order.
33 A `struct ref' data structure (ref) is allocated for every register
34 reference (def or use) and this records the insn and bb the ref is
35 found within. The refs are linked together in chains of uses and defs
36 for each insn and for each register. Each ref also has a chain field
37 that links all the use refs for a def or all the def refs for a use.
38 This is used to create use-def or def-use chains.
43 Here's an example of using the dataflow routines.
49 df_analyze (df, 0, DF_ALL);
51 df_dump (df, DF_ALL, stderr);
56 df_init simply creates a poor man's object (df) that needs to be
57 passed to all the dataflow routines. df_finish destroys this
58 object and frees up any allocated memory. DF_ALL says to analyze
61 df_analyze performs the following:
63 1. Records defs and uses by scanning the insns in each basic block
64 or by scanning the insns queued by df_insn_modify.
65 2. Links defs and uses into insn-def and insn-use chains.
66 3. Links defs and uses into reg-def and reg-use chains.
67 4. Assigns LUIDs to each insn (for modified blocks).
68 5. Calculates local reaching definitions.
69 6. Calculates global reaching definitions.
70 7. Creates use-def chains.
71 8. Calculates local reaching uses (upwards exposed uses).
72 9. Calculates global reaching uses.
73 10. Creates def-use chains.
74 11. Calculates local live registers.
75 12. Calculates global live registers.
76 13. Calculates register lifetimes and determines local registers.
81 Note that the dataflow information is not updated for every newly
82 deleted or created insn. If the dataflow information requires
83 updating then all the changed, new, or deleted insns needs to be
84 marked with df_insn_modify (or df_insns_modify) either directly or
85 indirectly (say through calling df_insn_delete). df_insn_modify
86 marks all the modified insns to get processed the next time df_analyze
89 Beware that tinkering with insns may invalidate the dataflow information.
90 The philosophy behind these routines is that once the dataflow
91 information has been gathered, the user should store what they require
92 before they tinker with any insn. Once a reg is replaced, for example,
93 then the reg-def/reg-use chains will point to the wrong place. Once a
94 whole lot of changes have been made, df_analyze can be called again
95 to update the dataflow information. Currently, this is not very smart
96 with regard to propagating changes to the dataflow so it should not
102 The basic object is a REF (reference) and this may either be a DEF
103 (definition) or a USE of a register.
105 These are linked into a variety of lists; namely reg-def, reg-use,
106 insn-def, insn-use, def-use, and use-def lists. For example,
107 the reg-def lists contain all the refs that define a given register
108 while the insn-use lists contain all the refs used by an insn.
110 Note that the reg-def and reg-use chains are generally short (except for
111 the hard registers) and thus it is much faster to search these chains
112 rather than searching the def or use bitmaps.
114 If the insns are in SSA form then the reg-def and use-def lists
115 should only contain the single defining ref.
120 1) Incremental dataflow analysis.
122 Note that if a loop invariant insn is hoisted (or sunk), we do not
123 need to change the def-use or use-def chains. All we have to do is to
124 change the bb field for all the associated defs and uses and to
125 renumber the LUIDs for the original and new basic blocks of the insn.
127 When shadowing loop mems we create new uses and defs for new pseudos
128 so we do not affect the existing dataflow information.
130 My current strategy is to queue up all modified, created, or deleted
131 insns so when df_analyze is called we can easily determine all the new
132 or deleted refs. Currently the global dataflow information is
133 recomputed from scratch but this could be propagated more efficiently.
135 2) Reduced memory requirements.
137 We could operate a pool of ref structures. When a ref is deleted it
138 gets returned to the pool (say by linking on to a chain of free refs).
139 This will require a pair of bitmaps for defs and uses so that we can
140 tell which ones have been changed. Alternatively, we could
141 periodically squeeze the def and use tables and associated bitmaps and
142 renumber the def and use ids.
144 3) Ordering of reg-def and reg-use lists.
146 Should the first entry in the def list be the first def (within a BB)?
147 Similarly, should the first entry in the use list be the last use
150 4) Working with a sub-CFG.
152 Often the whole CFG does not need to be analyzed, for example,
153 when optimizing a loop, only certain registers are of interest.
154 Perhaps there should be a bitmap argument to df_analyze to specify
155 which registers should be analyzed?
160 Embedded addressing side-effects, such as POST_INC or PRE_INC, generate
161 both a use and a def. These are both marked read/write to show that they
162 are dependent. For example, (set (reg 40) (mem (post_inc (reg 42))))
163 will generate a use of reg 42 followed by a def of reg 42 (both marked
164 read/write). Similarly, (set (reg 40) (mem (pre_dec (reg 41))))
165 generates a use of reg 41 then a def of reg 41 (both marked read/write),
166 even though reg 41 is decremented before it is used for the memory
167 address in this second example.
169 A set to a REG inside a ZERO_EXTRACT, or a set to a non-paradoxical SUBREG
170 for which the number of word_mode units covered by the outer mode is
171 smaller than that covered by the inner mode, invokes a read-modify-write.
172 operation. We generate both a use and a def and again mark them
174 Paradoxical subreg writes don't leave a trace of the old content, so they
175 are write-only operations. */
179 #include "coretypes.h"
183 #include "insn-config.h"
185 #include "function.h"
187 #include "alloc-pool.h"
188 #include "hard-reg-set.h"
189 #include "basic-block.h"
194 #define FOR_EACH_BB_IN_BITMAP(BITMAP, MIN, BB, CODE) \
197 unsigned int node_; \
198 bitmap_iterator bi; \
199 EXECUTE_IF_SET_IN_BITMAP (BITMAP, MIN, node_, bi) \
201 (BB) = BASIC_BLOCK (node_); \
207 static alloc_pool df_ref_pool
;
208 static alloc_pool df_link_pool
;
209 static struct df
*ddf
;
211 static void df_reg_table_realloc (struct df
*, int);
212 static void df_insn_table_realloc (struct df
*, unsigned int);
213 static void df_bb_table_realloc (struct df
*, unsigned int);
214 static void df_bitmaps_alloc (struct df
*, bitmap
, int);
215 static void df_bitmaps_free (struct df
*, int);
216 static void df_free (struct df
*);
217 static void df_alloc (struct df
*, int);
219 static rtx
df_reg_use_gen (unsigned int);
221 static inline struct df_link
*df_link_create (struct ref
*, struct df_link
*);
222 static struct df_link
*df_ref_unlink (struct df_link
**, struct ref
*);
223 static void df_def_unlink (struct df
*, struct ref
*);
224 static void df_use_unlink (struct df
*, struct ref
*);
225 static void df_insn_refs_unlink (struct df
*, basic_block
, rtx
);
227 static void df_bb_refs_unlink (struct df
*, basic_block
);
228 static void df_refs_unlink (struct df
*, bitmap
);
231 static struct ref
*df_ref_create (struct df
*, rtx
, rtx
*, rtx
,
232 enum df_ref_type
, enum df_ref_flags
);
233 static void df_ref_record_1 (struct df
*, rtx
, rtx
*, rtx
, enum df_ref_type
,
235 static void df_ref_record (struct df
*, rtx
, rtx
*, rtx
, enum df_ref_type
,
237 static void df_def_record_1 (struct df
*, rtx
, basic_block
, rtx
);
238 static void df_defs_record (struct df
*, rtx
, basic_block
, rtx
);
239 static void df_uses_record (struct df
*, rtx
*, enum df_ref_type
,
240 basic_block
, rtx
, enum df_ref_flags
);
241 static void df_insn_refs_record (struct df
*, basic_block
, rtx
);
242 static void df_bb_refs_record (struct df
*, basic_block
);
243 static void df_refs_record (struct df
*, bitmap
);
245 static void df_bb_reg_def_chain_create (struct df
*, basic_block
);
246 static void df_reg_def_chain_create (struct df
*, bitmap
, bool);
247 static void df_bb_reg_use_chain_create (struct df
*, basic_block
);
248 static void df_reg_use_chain_create (struct df
*, bitmap
, bool);
249 static void df_bb_du_chain_create (struct df
*, basic_block
, bitmap
);
250 static void df_du_chain_create (struct df
*, bitmap
);
251 static void df_bb_ud_chain_create (struct df
*, basic_block
);
252 static void df_ud_chain_create (struct df
*, bitmap
);
253 static void df_bb_rd_local_compute (struct df
*, basic_block
, bitmap
);
254 static void df_rd_local_compute (struct df
*, bitmap
);
255 static void df_bb_ru_local_compute (struct df
*, basic_block
);
256 static void df_ru_local_compute (struct df
*, bitmap
);
257 static void df_bb_lr_local_compute (struct df
*, basic_block
);
258 static void df_lr_local_compute (struct df
*, bitmap
);
259 static void df_bb_reg_info_compute (struct df
*, basic_block
, bitmap
);
260 static void df_reg_info_compute (struct df
*, bitmap
);
262 static int df_bb_luids_set (struct df
*df
, basic_block
);
263 static int df_luids_set (struct df
*df
, bitmap
);
265 static int df_modified_p (struct df
*, bitmap
);
266 static int df_refs_queue (struct df
*);
267 static int df_refs_process (struct df
*);
268 static int df_bb_refs_update (struct df
*, basic_block
);
269 static int df_refs_update (struct df
*, bitmap
);
270 static void df_analyze_1 (struct df
*, bitmap
, int, int);
272 static void df_insns_modify (struct df
*, basic_block
, rtx
, rtx
);
273 static int df_rtx_mem_replace (rtx
*, void *);
274 static int df_rtx_reg_replace (rtx
*, void *);
275 void df_refs_reg_replace (struct df
*, bitmap
, struct df_link
*, rtx
, rtx
);
277 static int df_def_dominates_all_uses_p (struct df
*, struct ref
*def
);
278 static int df_def_dominates_uses_p (struct df
*, struct ref
*def
, bitmap
);
279 static struct ref
*df_bb_insn_regno_last_use_find (struct df
*, basic_block
,
281 static struct ref
*df_bb_insn_regno_first_def_find (struct df
*, basic_block
,
284 static void df_chain_dump (struct df_link
*, FILE *file
);
285 static void df_chain_dump_regno (struct df_link
*, FILE *file
);
286 static void df_regno_debug (struct df
*, unsigned int, FILE *);
287 static void df_ref_debug (struct df
*, struct ref
*, FILE *);
288 static void df_rd_transfer_function (int, int *, void *, void *, void *,
290 static void df_ru_transfer_function (int, int *, void *, void *, void *,
292 static void df_lr_transfer_function (int, int *, void *, void *, void *,
294 static void hybrid_search (basic_block
, struct dataflow
*,
295 sbitmap
, sbitmap
, sbitmap
);
298 /* Local memory allocation/deallocation routines. */
301 /* Increase the insn info table to have space for at least SIZE + 1
304 df_insn_table_realloc (struct df
*df
, unsigned int size
)
307 if (size
<= df
->insn_size
)
310 /* Make the table a little larger than requested, so we do not need
311 to enlarge it so often. */
312 size
+= df
->insn_size
/ 4;
314 df
->insns
= xrealloc (df
->insns
, size
* sizeof (struct insn_info
));
316 memset (df
->insns
+ df
->insn_size
, 0,
317 (size
- df
->insn_size
) * sizeof (struct insn_info
));
319 df
->insn_size
= size
;
321 if (! df
->insns_modified
)
323 df
->insns_modified
= BITMAP_ALLOC (NULL
);
324 bitmap_zero (df
->insns_modified
);
328 /* Increase the bb info table to have space for at least SIZE + 1
332 df_bb_table_realloc (struct df
*df
, unsigned int size
)
335 if (size
<= df
->n_bbs
)
338 /* Make the table a little larger than requested, so we do not need
339 to enlarge it so often. */
340 size
+= df
->n_bbs
/ 4;
342 df
->bbs
= xrealloc (df
->bbs
, size
* sizeof (struct bb_info
));
344 memset (df
->bbs
+ df
->n_bbs
, 0, (size
- df
->n_bbs
) * sizeof (struct bb_info
));
349 /* Increase the reg info table by SIZE more elements. */
351 df_reg_table_realloc (struct df
*df
, int size
)
353 /* Make table 25 percent larger by default. */
355 size
= df
->reg_size
/ 4;
357 size
+= df
->reg_size
;
358 if (size
< max_reg_num ())
359 size
= max_reg_num ();
361 df
->regs
= xrealloc (df
->regs
, size
* sizeof (struct reg_info
));
362 df
->reg_def_last
= xrealloc (df
->reg_def_last
,
363 size
* sizeof (struct ref
*));
365 /* Zero the new entries. */
366 memset (df
->regs
+ df
->reg_size
, 0,
367 (size
- df
->reg_size
) * sizeof (struct reg_info
));
373 /* Allocate bitmaps for each basic block. */
376 df_bitmaps_alloc (struct df
*df
, bitmap blocks
, int flags
)
380 df
->n_defs
= df
->def_id
;
381 df
->n_uses
= df
->use_id
;
384 blocks
= df
->all_blocks
;
386 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
388 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
394 /* Allocate bitmaps for reaching definitions. */
395 bb_info
->rd_kill
= BITMAP_ALLOC (NULL
);
396 bb_info
->rd_gen
= BITMAP_ALLOC (NULL
);
397 bb_info
->rd_in
= BITMAP_ALLOC (NULL
);
398 bb_info
->rd_out
= BITMAP_ALLOC (NULL
);
402 bitmap_clear (bb_info
->rd_kill
);
403 bitmap_clear (bb_info
->rd_gen
);
404 bitmap_clear (bb_info
->rd_in
);
405 bitmap_clear (bb_info
->rd_out
);
413 /* Allocate bitmaps for upward exposed uses. */
414 bb_info
->ru_kill
= BITMAP_ALLOC (NULL
);
415 bb_info
->ru_gen
= BITMAP_ALLOC (NULL
);
416 bb_info
->ru_in
= BITMAP_ALLOC (NULL
);
417 bb_info
->ru_out
= BITMAP_ALLOC (NULL
);
421 bitmap_clear (bb_info
->ru_kill
);
422 bitmap_clear (bb_info
->ru_gen
);
423 bitmap_clear (bb_info
->ru_in
);
424 bitmap_clear (bb_info
->ru_out
);
432 /* Allocate bitmaps for live variables. */
433 bb_info
->lr_def
= BITMAP_ALLOC (NULL
);
434 bb_info
->lr_use
= BITMAP_ALLOC (NULL
);
435 bb_info
->lr_in
= BITMAP_ALLOC (NULL
);
436 bb_info
->lr_out
= BITMAP_ALLOC (NULL
);
440 bitmap_clear (bb_info
->lr_def
);
441 bitmap_clear (bb_info
->lr_use
);
442 bitmap_clear (bb_info
->lr_in
);
443 bitmap_clear (bb_info
->lr_out
);
450 /* Free bitmaps for each basic block. */
452 df_bitmaps_free (struct df
*df
, int flags
)
458 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
463 if ((flags
& DF_RD
) && bb_info
->rd_in
)
465 /* Free bitmaps for reaching definitions. */
466 BITMAP_FREE (bb_info
->rd_kill
);
467 bb_info
->rd_kill
= NULL
;
468 BITMAP_FREE (bb_info
->rd_gen
);
469 bb_info
->rd_gen
= NULL
;
470 BITMAP_FREE (bb_info
->rd_in
);
471 bb_info
->rd_in
= NULL
;
472 BITMAP_FREE (bb_info
->rd_out
);
473 bb_info
->rd_out
= NULL
;
476 if ((flags
& DF_RU
) && bb_info
->ru_in
)
478 /* Free bitmaps for upward exposed uses. */
479 BITMAP_FREE (bb_info
->ru_kill
);
480 bb_info
->ru_kill
= NULL
;
481 BITMAP_FREE (bb_info
->ru_gen
);
482 bb_info
->ru_gen
= NULL
;
483 BITMAP_FREE (bb_info
->ru_in
);
484 bb_info
->ru_in
= NULL
;
485 BITMAP_FREE (bb_info
->ru_out
);
486 bb_info
->ru_out
= NULL
;
489 if ((flags
& DF_LR
) && bb_info
->lr_in
)
491 /* Free bitmaps for live variables. */
492 BITMAP_FREE (bb_info
->lr_def
);
493 bb_info
->lr_def
= NULL
;
494 BITMAP_FREE (bb_info
->lr_use
);
495 bb_info
->lr_use
= NULL
;
496 BITMAP_FREE (bb_info
->lr_in
);
497 bb_info
->lr_in
= NULL
;
498 BITMAP_FREE (bb_info
->lr_out
);
499 bb_info
->lr_out
= NULL
;
502 df
->flags
&= ~(flags
& (DF_RD
| DF_RU
| DF_LR
));
506 /* Allocate and initialize dataflow memory. */
508 df_alloc (struct df
*df
, int n_regs
)
513 df_link_pool
= create_alloc_pool ("df_link pool", sizeof (struct df_link
),
515 df_ref_pool
= create_alloc_pool ("df_ref pool", sizeof (struct ref
), 100);
517 /* Perhaps we should use LUIDs to save memory for the insn_refs
518 table. This is only a small saving; a few pointers. */
519 n_insns
= get_max_uid () + 1;
523 /* Approximate number of defs by number of insns. */
524 df
->def_size
= n_insns
;
525 df
->defs
= xmalloc (df
->def_size
* sizeof (*df
->defs
));
529 /* Approximate number of uses by twice number of insns. */
530 df
->use_size
= n_insns
* 2;
531 df
->uses
= xmalloc (df
->use_size
* sizeof (*df
->uses
));
534 df
->n_bbs
= last_basic_block
;
536 /* Allocate temporary working array used during local dataflow analysis. */
537 df_insn_table_realloc (df
, n_insns
);
539 df_reg_table_realloc (df
, df
->n_regs
);
541 df
->bbs_modified
= BITMAP_ALLOC (NULL
);
542 bitmap_zero (df
->bbs_modified
);
546 df
->bbs
= xcalloc (last_basic_block
, sizeof (struct bb_info
));
548 df
->all_blocks
= BITMAP_ALLOC (NULL
);
550 bitmap_set_bit (df
->all_blocks
, bb
->index
);
554 /* Free all the dataflow info. */
556 df_free (struct df
*df
)
558 df_bitmaps_free (df
, DF_ALL
);
586 BITMAP_FREE (df
->bbs_modified
);
587 df
->bbs_modified
= 0;
589 BITMAP_FREE (df
->insns_modified
);
590 df
->insns_modified
= 0;
592 BITMAP_FREE (df
->all_blocks
);
595 free_alloc_pool (df_ref_pool
);
596 free_alloc_pool (df_link_pool
);
599 /* Local miscellaneous routines. */
601 /* Return a USE for register REGNO. */
602 static rtx
df_reg_use_gen (unsigned int regno
)
607 reg
= regno_reg_rtx
[regno
];
609 use
= gen_rtx_USE (GET_MODE (reg
), reg
);
613 /* Local chain manipulation routines. */
615 /* Create a link in a def-use or use-def chain. */
616 static inline struct df_link
*
617 df_link_create (struct ref
*ref
, struct df_link
*next
)
619 struct df_link
*link
;
621 link
= pool_alloc (df_link_pool
);
627 /* Releases members of the CHAIN. */
630 free_reg_ref_chain (struct df_link
**chain
)
632 struct df_link
*act
, *next
;
634 for (act
= *chain
; act
; act
= next
)
637 pool_free (df_link_pool
, act
);
643 /* Add REF to chain head pointed to by PHEAD. */
644 static struct df_link
*
645 df_ref_unlink (struct df_link
**phead
, struct ref
*ref
)
647 struct df_link
*link
= *phead
;
653 /* Only a single ref. It must be the one we want.
654 If not, the def-use and use-def chains are likely to
656 gcc_assert (link
->ref
== ref
);
658 /* Now have an empty chain. */
663 /* Multiple refs. One of them must be us. */
664 if (link
->ref
== ref
)
669 for (; link
->next
; link
= link
->next
)
671 if (link
->next
->ref
== ref
)
673 /* Unlink from list. */
674 link
->next
= link
->next
->next
;
685 /* Unlink REF from all def-use/use-def chains, etc. */
687 df_ref_remove (struct df
*df
, struct ref
*ref
)
689 if (DF_REF_REG_DEF_P (ref
))
691 df_def_unlink (df
, ref
);
692 df_ref_unlink (&df
->insns
[DF_REF_INSN_UID (ref
)].defs
, ref
);
696 df_use_unlink (df
, ref
);
697 df_ref_unlink (&df
->insns
[DF_REF_INSN_UID (ref
)].uses
, ref
);
703 /* Unlink DEF from use-def and reg-def chains. */
705 df_def_unlink (struct df
*df ATTRIBUTE_UNUSED
, struct ref
*def
)
707 struct df_link
*du_link
;
708 unsigned int dregno
= DF_REF_REGNO (def
);
710 /* Follow def-use chain to find all the uses of this def. */
711 for (du_link
= DF_REF_CHAIN (def
); du_link
; du_link
= du_link
->next
)
713 struct ref
*use
= du_link
->ref
;
715 /* Unlink this def from the use-def chain. */
716 df_ref_unlink (&DF_REF_CHAIN (use
), def
);
718 DF_REF_CHAIN (def
) = 0;
720 /* Unlink def from reg-def chain. */
721 df_ref_unlink (&df
->regs
[dregno
].defs
, def
);
723 df
->defs
[DF_REF_ID (def
)] = 0;
727 /* Unlink use from def-use and reg-use chains. */
729 df_use_unlink (struct df
*df ATTRIBUTE_UNUSED
, struct ref
*use
)
731 struct df_link
*ud_link
;
732 unsigned int uregno
= DF_REF_REGNO (use
);
734 /* Follow use-def chain to find all the defs of this use. */
735 for (ud_link
= DF_REF_CHAIN (use
); ud_link
; ud_link
= ud_link
->next
)
737 struct ref
*def
= ud_link
->ref
;
739 /* Unlink this use from the def-use chain. */
740 df_ref_unlink (&DF_REF_CHAIN (def
), use
);
742 DF_REF_CHAIN (use
) = 0;
744 /* Unlink use from reg-use chain. */
745 df_ref_unlink (&df
->regs
[uregno
].uses
, use
);
747 df
->uses
[DF_REF_ID (use
)] = 0;
750 /* Local routines for recording refs. */
753 /* Create a new ref of type DF_REF_TYPE for register REG at address
754 LOC within INSN of BB. */
756 df_ref_create (struct df
*df
, rtx reg
, rtx
*loc
, rtx insn
,
757 enum df_ref_type ref_type
, enum df_ref_flags ref_flags
)
759 struct ref
*this_ref
;
761 this_ref
= pool_alloc (df_ref_pool
);
762 DF_REF_REG (this_ref
) = reg
;
763 DF_REF_LOC (this_ref
) = loc
;
764 DF_REF_INSN (this_ref
) = insn
;
765 DF_REF_CHAIN (this_ref
) = 0;
766 DF_REF_TYPE (this_ref
) = ref_type
;
767 DF_REF_FLAGS (this_ref
) = ref_flags
;
768 DF_REF_DATA (this_ref
) = NULL
;
770 if (ref_type
== DF_REF_REG_DEF
)
772 if (df
->def_id
>= df
->def_size
)
774 /* Make table 25 percent larger. */
775 df
->def_size
+= (df
->def_size
/ 4);
776 df
->defs
= xrealloc (df
->defs
,
777 df
->def_size
* sizeof (*df
->defs
));
779 DF_REF_ID (this_ref
) = df
->def_id
;
780 df
->defs
[df
->def_id
++] = this_ref
;
784 if (df
->use_id
>= df
->use_size
)
786 /* Make table 25 percent larger. */
787 df
->use_size
+= (df
->use_size
/ 4);
788 df
->uses
= xrealloc (df
->uses
,
789 df
->use_size
* sizeof (*df
->uses
));
791 DF_REF_ID (this_ref
) = df
->use_id
;
792 df
->uses
[df
->use_id
++] = this_ref
;
798 /* Create a new reference of type DF_REF_TYPE for a single register REG,
799 used inside the LOC rtx of INSN. */
801 df_ref_record_1 (struct df
*df
, rtx reg
, rtx
*loc
, rtx insn
,
802 enum df_ref_type ref_type
, enum df_ref_flags ref_flags
)
804 df_ref_create (df
, reg
, loc
, insn
, ref_type
, ref_flags
);
808 /* Create new references of type DF_REF_TYPE for each part of register REG
809 at address LOC within INSN of BB. */
811 df_ref_record (struct df
*df
, rtx reg
, rtx
*loc
, rtx insn
,
812 enum df_ref_type ref_type
, enum df_ref_flags ref_flags
)
816 gcc_assert (REG_P (reg
) || GET_CODE (reg
) == SUBREG
);
818 /* For the reg allocator we are interested in some SUBREG rtx's, but not
819 all. Notably only those representing a word extraction from a multi-word
820 reg. As written in the docu those should have the form
821 (subreg:SI (reg:M A) N), with size(SImode) > size(Mmode).
822 XXX Is that true? We could also use the global word_mode variable. */
823 if ((df
->flags
& DF_SUBREGS
) == 0
824 && GET_CODE (reg
) == SUBREG
825 && (GET_MODE_SIZE (GET_MODE (reg
)) < GET_MODE_SIZE (word_mode
)
826 || GET_MODE_SIZE (GET_MODE (reg
))
827 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (reg
)))))
829 loc
= &SUBREG_REG (reg
);
831 ref_flags
|= DF_REF_STRIPPED
;
834 regno
= REGNO (GET_CODE (reg
) == SUBREG
? SUBREG_REG (reg
) : reg
);
835 if (regno
< FIRST_PSEUDO_REGISTER
)
840 if (! (df
->flags
& DF_HARD_REGS
))
843 /* GET_MODE (reg) is correct here. We do not want to go into a SUBREG
844 for the mode, because we only want to add references to regs, which
845 are really referenced. E.g., a (subreg:SI (reg:DI 0) 0) does _not_
846 reference the whole reg 0 in DI mode (which would also include
847 reg 1, at least, if 0 and 1 are SImode registers). */
848 endregno
= hard_regno_nregs
[regno
][GET_MODE (reg
)];
849 if (GET_CODE (reg
) == SUBREG
)
850 regno
+= subreg_regno_offset (regno
, GET_MODE (SUBREG_REG (reg
)),
851 SUBREG_BYTE (reg
), GET_MODE (reg
));
854 for (i
= regno
; i
< endregno
; i
++)
855 df_ref_record_1 (df
, regno_reg_rtx
[i
],
856 loc
, insn
, ref_type
, ref_flags
);
860 df_ref_record_1 (df
, reg
, loc
, insn
, ref_type
, ref_flags
);
865 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
866 covered by the outer mode is smaller than that covered by the inner mode,
867 is a read-modify-write operation.
868 This function returns true iff the SUBREG X is such a SUBREG. */
870 read_modify_subreg_p (rtx x
)
872 unsigned int isize
, osize
;
873 if (GET_CODE (x
) != SUBREG
)
875 isize
= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x
)));
876 osize
= GET_MODE_SIZE (GET_MODE (x
));
877 return (isize
> osize
&& isize
> UNITS_PER_WORD
);
881 /* Process all the registers defined in the rtx, X. */
883 df_def_record_1 (struct df
*df
, rtx x
, basic_block bb
, rtx insn
)
887 enum df_ref_flags flags
= 0;
889 /* We may recursively call ourselves on EXPR_LIST when dealing with PARALLEL
891 if (GET_CODE (x
) == EXPR_LIST
|| GET_CODE (x
) == CLOBBER
)
897 /* Some targets place small structures in registers for
898 return values of functions. */
899 if (GET_CODE (dst
) == PARALLEL
&& GET_MODE (dst
) == BLKmode
)
903 for (i
= XVECLEN (dst
, 0) - 1; i
>= 0; i
--)
905 rtx temp
= XVECEXP (dst
, 0, i
);
906 if (GET_CODE (temp
) == EXPR_LIST
|| GET_CODE (temp
) == CLOBBER
907 || GET_CODE (temp
) == SET
)
908 df_def_record_1 (df
, temp
, bb
, insn
);
913 /* Maybe, we should flag the use of STRICT_LOW_PART somehow. It might
914 be handy for the reg allocator. */
915 while (GET_CODE (dst
) == STRICT_LOW_PART
916 || GET_CODE (dst
) == ZERO_EXTRACT
917 || read_modify_subreg_p (dst
))
919 /* Strict low part always contains SUBREG, but we do not want to make
920 it appear outside, as whole register is always considered. */
921 if (GET_CODE (dst
) == STRICT_LOW_PART
)
923 loc
= &XEXP (dst
, 0);
926 loc
= &XEXP (dst
, 0);
928 flags
|= DF_REF_READ_WRITE
;
932 || (GET_CODE (dst
) == SUBREG
&& REG_P (SUBREG_REG (dst
))))
933 df_ref_record (df
, dst
, loc
, insn
, DF_REF_REG_DEF
, flags
);
937 /* Process all the registers defined in the pattern rtx, X. */
939 df_defs_record (struct df
*df
, rtx x
, basic_block bb
, rtx insn
)
941 RTX_CODE code
= GET_CODE (x
);
943 if (code
== SET
|| code
== CLOBBER
)
945 /* Mark the single def within the pattern. */
946 df_def_record_1 (df
, x
, bb
, insn
);
948 else if (code
== PARALLEL
)
952 /* Mark the multiple defs within the pattern. */
953 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
955 code
= GET_CODE (XVECEXP (x
, 0, i
));
956 if (code
== SET
|| code
== CLOBBER
)
957 df_def_record_1 (df
, XVECEXP (x
, 0, i
), bb
, insn
);
963 /* Process all the registers used in the rtx at address LOC. */
965 df_uses_record (struct df
*df
, rtx
*loc
, enum df_ref_type ref_type
,
966 basic_block bb
, rtx insn
, enum df_ref_flags flags
)
990 /* If we are clobbering a MEM, mark any registers inside the address
992 if (MEM_P (XEXP (x
, 0)))
993 df_uses_record (df
, &XEXP (XEXP (x
, 0), 0),
994 DF_REF_REG_MEM_STORE
, bb
, insn
, flags
);
996 /* If we're clobbering a REG then we have a def so ignore. */
1000 df_uses_record (df
, &XEXP (x
, 0), DF_REF_REG_MEM_LOAD
, bb
, insn
, 0);
1004 /* While we're here, optimize this case. */
1006 /* In case the SUBREG is not of a REG, do not optimize. */
1007 if (!REG_P (SUBREG_REG (x
)))
1009 loc
= &SUBREG_REG (x
);
1010 df_uses_record (df
, loc
, ref_type
, bb
, insn
, flags
);
1013 /* ... Fall through ... */
1016 df_ref_record (df
, x
, loc
, insn
, ref_type
, flags
);
1021 rtx dst
= SET_DEST (x
);
1023 df_uses_record (df
, &SET_SRC (x
), DF_REF_REG_USE
, bb
, insn
, 0);
1025 switch (GET_CODE (dst
))
1028 if (read_modify_subreg_p (dst
))
1030 df_uses_record (df
, &SUBREG_REG (dst
), DF_REF_REG_USE
, bb
,
1031 insn
, DF_REF_READ_WRITE
);
1042 df_uses_record (df
, &XEXP (dst
, 0),
1043 DF_REF_REG_MEM_STORE
,
1046 case STRICT_LOW_PART
:
1047 /* A strict_low_part uses the whole REG and not just the
1049 dst
= XEXP (dst
, 0);
1050 gcc_assert (GET_CODE (dst
) == SUBREG
);
1051 df_uses_record (df
, &SUBREG_REG (dst
), DF_REF_REG_USE
, bb
,
1052 insn
, DF_REF_READ_WRITE
);
1056 df_uses_record (df
, &XEXP (dst
, 0), DF_REF_REG_USE
, bb
, insn
,
1058 df_uses_record (df
, &XEXP (dst
, 1), DF_REF_REG_USE
, bb
, insn
, 0);
1059 df_uses_record (df
, &XEXP (dst
, 2), DF_REF_REG_USE
, bb
, insn
, 0);
1060 dst
= XEXP (dst
, 0);
1072 case UNSPEC_VOLATILE
:
1076 /* Traditional and volatile asm instructions must be considered to use
1077 and clobber all hard registers, all pseudo-registers and all of
1078 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
1080 Consider for instance a volatile asm that changes the fpu rounding
1081 mode. An insn should not be moved across this even if it only uses
1082 pseudo-regs because it might give an incorrectly rounded result.
1084 For now, just mark any regs we can find in ASM_OPERANDS as
1087 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
1088 We can not just fall through here since then we would be confused
1089 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
1090 traditional asms unlike their normal usage. */
1091 if (code
== ASM_OPERANDS
)
1095 for (j
= 0; j
< ASM_OPERANDS_INPUT_LENGTH (x
); j
++)
1096 df_uses_record (df
, &ASM_OPERANDS_INPUT (x
, j
),
1097 DF_REF_REG_USE
, bb
, insn
, 0);
1109 /* Catch the def of the register being modified. */
1110 df_ref_record (df
, XEXP (x
, 0), &XEXP (x
, 0), insn
, DF_REF_REG_DEF
, DF_REF_READ_WRITE
);
1112 /* ... Fall through to handle uses ... */
1118 /* Recursively scan the operands of this expression. */
1120 const char *fmt
= GET_RTX_FORMAT (code
);
1123 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1127 /* Tail recursive case: save a function call level. */
1133 df_uses_record (df
, &XEXP (x
, i
), ref_type
, bb
, insn
, flags
);
1135 else if (fmt
[i
] == 'E')
1138 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1139 df_uses_record (df
, &XVECEXP (x
, i
, j
), ref_type
,
1147 /* Record all the df within INSN of basic block BB. */
1149 df_insn_refs_record (struct df
*df
, basic_block bb
, rtx insn
)
1157 /* Record register defs. */
1158 df_defs_record (df
, PATTERN (insn
), bb
, insn
);
1160 if (df
->flags
& DF_EQUIV_NOTES
)
1161 for (note
= REG_NOTES (insn
); note
;
1162 note
= XEXP (note
, 1))
1164 switch (REG_NOTE_KIND (note
))
1168 df_uses_record (df
, &XEXP (note
, 0), DF_REF_REG_USE
,
1180 /* Record the registers used to pass arguments. */
1181 for (note
= CALL_INSN_FUNCTION_USAGE (insn
); note
;
1182 note
= XEXP (note
, 1))
1184 if (GET_CODE (XEXP (note
, 0)) == USE
)
1185 df_uses_record (df
, &XEXP (XEXP (note
, 0), 0), DF_REF_REG_USE
,
1189 /* The stack ptr is used (honorarily) by a CALL insn. */
1190 x
= df_reg_use_gen (STACK_POINTER_REGNUM
);
1191 df_uses_record (df
, &XEXP (x
, 0), DF_REF_REG_USE
, bb
, insn
, 0);
1193 if (df
->flags
& DF_HARD_REGS
)
1195 /* Calls may also reference any of the global registers,
1196 so they are recorded as used. */
1197 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1200 x
= df_reg_use_gen (i
);
1201 df_uses_record (df
, &XEXP (x
, 0),
1202 DF_REF_REG_USE
, bb
, insn
, 0);
1207 /* Record the register uses. */
1208 df_uses_record (df
, &PATTERN (insn
),
1209 DF_REF_REG_USE
, bb
, insn
, 0);
1215 /* We do not record hard registers clobbered by the call,
1216 since there are awfully many of them and "defs" created
1217 through them are not interesting (since no use can be legally
1218 reached by them). So we must just make sure we include them when
1219 computing kill bitmaps. */
1221 /* There may be extra registers to be clobbered. */
1222 for (note
= CALL_INSN_FUNCTION_USAGE (insn
);
1224 note
= XEXP (note
, 1))
1225 if (GET_CODE (XEXP (note
, 0)) == CLOBBER
)
1226 df_defs_record (df
, XEXP (note
, 0), bb
, insn
);
1232 /* Record all the refs within the basic block BB. */
1234 df_bb_refs_record (struct df
*df
, basic_block bb
)
1238 /* Scan the block an insn at a time from beginning to end. */
1239 FOR_BB_INSNS (bb
, insn
)
1243 /* Record defs within INSN. */
1244 df_insn_refs_record (df
, bb
, insn
);
1250 /* Record all the refs in the basic blocks specified by BLOCKS. */
1252 df_refs_record (struct df
*df
, bitmap blocks
)
1256 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1258 df_bb_refs_record (df
, bb
);
1262 /* Dataflow analysis routines. */
1264 /* Create reg-def chains for basic block BB. These are a list of
1265 definitions for each register. */
1268 df_bb_reg_def_chain_create (struct df
*df
, basic_block bb
)
1272 /* Perhaps the defs should be sorted using a depth first search
1273 of the CFG (or possibly a breadth first search). */
1275 FOR_BB_INSNS_REVERSE (bb
, insn
)
1277 struct df_link
*link
;
1278 unsigned int uid
= INSN_UID (insn
);
1280 if (! INSN_P (insn
))
1283 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
1285 struct ref
*def
= link
->ref
;
1286 unsigned int dregno
= DF_REF_REGNO (def
);
1288 /* Do not add ref's to the chain twice, i.e., only add new
1289 refs. XXX the same could be done by testing if the
1290 current insn is a modified (or a new) one. This would be
1292 if (DF_REF_ID (def
) < df
->def_id_save
)
1295 df
->regs
[dregno
].defs
= df_link_create (def
, df
->regs
[dregno
].defs
);
1301 /* Create reg-def chains for each basic block within BLOCKS. These
1302 are a list of definitions for each register. If REDO is true, add
1303 all defs, otherwise just add the new defs. */
1306 df_reg_def_chain_create (struct df
*df
, bitmap blocks
, bool redo
)
1309 #ifdef ENABLE_CHECKING
1312 unsigned old_def_id_save
= df
->def_id_save
;
1316 #ifdef ENABLE_CHECKING
1317 for (regno
= 0; regno
< df
->n_regs
; regno
++)
1318 gcc_assert (!df
->regs
[regno
].defs
);
1321 /* Pretend that all defs are new. */
1322 df
->def_id_save
= 0;
1325 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1327 df_bb_reg_def_chain_create (df
, bb
);
1330 df
->def_id_save
= old_def_id_save
;
1333 /* Remove all reg-def chains stored in the dataflow object DF. */
1336 df_reg_def_chain_clean (struct df
*df
)
1340 for (regno
= 0; regno
< df
->n_regs
; regno
++)
1341 free_reg_ref_chain (&df
->regs
[regno
].defs
);
1344 /* Create reg-use chains for basic block BB. These are a list of uses
1345 for each register. */
1348 df_bb_reg_use_chain_create (struct df
*df
, basic_block bb
)
1352 /* Scan in forward order so that the last uses appear at the start
1355 FOR_BB_INSNS (bb
, insn
)
1357 struct df_link
*link
;
1358 unsigned int uid
= INSN_UID (insn
);
1360 if (! INSN_P (insn
))
1363 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
1365 struct ref
*use
= link
->ref
;
1366 unsigned int uregno
= DF_REF_REGNO (use
);
1368 /* Do not add ref's to the chain twice, i.e., only add new
1369 refs. XXX the same could be done by testing if the
1370 current insn is a modified (or a new) one. This would be
1372 if (DF_REF_ID (use
) < df
->use_id_save
)
1375 df
->regs
[uregno
].uses
1376 = df_link_create (use
, df
->regs
[uregno
].uses
);
1382 /* Create reg-use chains for each basic block within BLOCKS. These
1383 are a list of uses for each register. If REDO is true, remove the
1384 old reg-use chains first, otherwise just add new uses to them. */
1387 df_reg_use_chain_create (struct df
*df
, bitmap blocks
, bool redo
)
1390 #ifdef ENABLE_CHECKING
1393 unsigned old_use_id_save
= df
->use_id_save
;
1397 #ifdef ENABLE_CHECKING
1398 for (regno
= 0; regno
< df
->n_regs
; regno
++)
1399 gcc_assert (!df
->regs
[regno
].uses
);
1402 /* Pretend that all uses are new. */
1403 df
->use_id_save
= 0;
1406 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1408 df_bb_reg_use_chain_create (df
, bb
);
1411 df
->use_id_save
= old_use_id_save
;
1414 /* Remove all reg-use chains stored in the dataflow object DF. */
1417 df_reg_use_chain_clean (struct df
*df
)
1421 for (regno
= 0; regno
< df
->n_regs
; regno
++)
1422 free_reg_ref_chain (&df
->regs
[regno
].uses
);
1425 /* Create def-use chains from reaching use bitmaps for basic block BB. */
1427 df_bb_du_chain_create (struct df
*df
, basic_block bb
, bitmap ru
)
1429 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1432 bitmap_copy (ru
, bb_info
->ru_out
);
1434 /* For each def in BB create a linked list (chain) of uses
1435 reached from the def. */
1436 FOR_BB_INSNS_REVERSE (bb
, insn
)
1438 struct df_link
*def_link
;
1439 struct df_link
*use_link
;
1440 unsigned int uid
= INSN_UID (insn
);
1442 if (! INSN_P (insn
))
1445 /* For each def in insn... */
1446 for (def_link
= df
->insns
[uid
].defs
; def_link
; def_link
= def_link
->next
)
1448 struct ref
*def
= def_link
->ref
;
1449 unsigned int dregno
= DF_REF_REGNO (def
);
1451 DF_REF_CHAIN (def
) = 0;
1453 /* While the reg-use chains are not essential, it
1454 is _much_ faster to search these short lists rather
1455 than all the reaching uses, especially for large functions. */
1456 for (use_link
= df
->regs
[dregno
].uses
; use_link
;
1457 use_link
= use_link
->next
)
1459 struct ref
*use
= use_link
->ref
;
1461 if (bitmap_bit_p (ru
, DF_REF_ID (use
)))
1464 = df_link_create (use
, DF_REF_CHAIN (def
));
1466 bitmap_clear_bit (ru
, DF_REF_ID (use
));
1471 /* For each use in insn... */
1472 for (use_link
= df
->insns
[uid
].uses
; use_link
; use_link
= use_link
->next
)
1474 struct ref
*use
= use_link
->ref
;
1475 bitmap_set_bit (ru
, DF_REF_ID (use
));
1481 /* Create def-use chains from reaching use bitmaps for basic blocks
1484 df_du_chain_create (struct df
*df
, bitmap blocks
)
1489 ru
= BITMAP_ALLOC (NULL
);
1491 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1493 df_bb_du_chain_create (df
, bb
, ru
);
1500 /* Create use-def chains from reaching def bitmaps for basic block BB. */
1502 df_bb_ud_chain_create (struct df
*df
, basic_block bb
)
1504 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1505 struct ref
**reg_def_last
= df
->reg_def_last
;
1508 memset (reg_def_last
, 0, df
->n_regs
* sizeof (struct ref
*));
1510 /* For each use in BB create a linked list (chain) of defs
1511 that reach the use. */
1512 FOR_BB_INSNS (bb
, insn
)
1514 unsigned int uid
= INSN_UID (insn
);
1515 struct df_link
*use_link
;
1516 struct df_link
*def_link
;
1518 if (! INSN_P (insn
))
1521 /* For each use in insn... */
1522 for (use_link
= df
->insns
[uid
].uses
; use_link
; use_link
= use_link
->next
)
1524 struct ref
*use
= use_link
->ref
;
1525 unsigned int regno
= DF_REF_REGNO (use
);
1527 DF_REF_CHAIN (use
) = 0;
1529 /* Has regno been defined in this BB yet? If so, use
1530 the last def as the single entry for the use-def
1531 chain for this use. Otherwise, we need to add all
1532 the defs using this regno that reach the start of
1534 if (reg_def_last
[regno
])
1537 = df_link_create (reg_def_last
[regno
], 0);
1541 /* While the reg-def chains are not essential, it is
1542 _much_ faster to search these short lists rather than
1543 all the reaching defs, especially for large
1545 for (def_link
= df
->regs
[regno
].defs
; def_link
;
1546 def_link
= def_link
->next
)
1548 struct ref
*def
= def_link
->ref
;
1550 if (bitmap_bit_p (bb_info
->rd_in
, DF_REF_ID (def
)))
1553 = df_link_create (def
, DF_REF_CHAIN (use
));
1560 /* For each def in insn... record the last def of each reg. */
1561 for (def_link
= df
->insns
[uid
].defs
; def_link
; def_link
= def_link
->next
)
1563 struct ref
*def
= def_link
->ref
;
1564 int dregno
= DF_REF_REGNO (def
);
1566 reg_def_last
[dregno
] = def
;
1572 /* Create use-def chains from reaching def bitmaps for basic blocks
1575 df_ud_chain_create (struct df
*df
, bitmap blocks
)
1579 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1581 df_bb_ud_chain_create (df
, bb
);
1588 df_rd_transfer_function (int bb ATTRIBUTE_UNUSED
, int *changed
, void *in
,
1589 void *out
, void *gen
, void *kill
,
1590 void *data ATTRIBUTE_UNUSED
)
1592 *changed
= bitmap_ior_and_compl (out
, gen
, in
, kill
);
1597 df_ru_transfer_function (int bb ATTRIBUTE_UNUSED
, int *changed
, void *in
,
1598 void *out
, void *gen
, void *kill
,
1599 void *data ATTRIBUTE_UNUSED
)
1601 *changed
= bitmap_ior_and_compl (in
, gen
, out
, kill
);
1606 df_lr_transfer_function (int bb ATTRIBUTE_UNUSED
, int *changed
, void *in
,
1607 void *out
, void *use
, void *def
,
1608 void *data ATTRIBUTE_UNUSED
)
1610 *changed
= bitmap_ior_and_compl (in
, use
, out
, def
);
1614 /* Compute local reaching def info for basic block BB. */
1616 df_bb_rd_local_compute (struct df
*df
, basic_block bb
, bitmap call_killed_defs
)
1618 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1620 bitmap seen
= BITMAP_ALLOC (NULL
);
1621 bool call_seen
= false;
1623 FOR_BB_INSNS_REVERSE (bb
, insn
)
1625 unsigned int uid
= INSN_UID (insn
);
1626 struct df_link
*def_link
;
1628 if (! INSN_P (insn
))
1631 for (def_link
= df
->insns
[uid
].defs
; def_link
; def_link
= def_link
->next
)
1633 struct ref
*def
= def_link
->ref
;
1634 unsigned int regno
= DF_REF_REGNO (def
);
1635 struct df_link
*def2_link
;
1637 if (bitmap_bit_p (seen
, regno
)
1639 && regno
< FIRST_PSEUDO_REGISTER
1640 && TEST_HARD_REG_BIT (regs_invalidated_by_call
, regno
)))
1643 for (def2_link
= df
->regs
[regno
].defs
; def2_link
;
1644 def2_link
= def2_link
->next
)
1646 struct ref
*def2
= def2_link
->ref
;
1648 /* Add all defs of this reg to the set of kills. This
1649 is greedy since many of these defs will not actually
1650 be killed by this BB but it keeps things a lot
1652 bitmap_set_bit (bb_info
->rd_kill
, DF_REF_ID (def2
));
1655 bitmap_set_bit (bb_info
->rd_gen
, DF_REF_ID (def
));
1656 bitmap_set_bit (seen
, regno
);
1659 if (CALL_P (insn
) && (df
->flags
& DF_HARD_REGS
))
1661 bitmap_ior_into (bb_info
->rd_kill
, call_killed_defs
);
1670 /* Compute local reaching def info for each basic block within BLOCKS. */
1672 df_rd_local_compute (struct df
*df
, bitmap blocks
)
1675 bitmap killed_by_call
= NULL
;
1677 struct df_link
*def_link
;
1679 if (df
->flags
& DF_HARD_REGS
)
1681 killed_by_call
= BITMAP_ALLOC (NULL
);
1682 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1684 if (!TEST_HARD_REG_BIT (regs_invalidated_by_call
, regno
))
1687 for (def_link
= df
->regs
[regno
].defs
;
1689 def_link
= def_link
->next
)
1690 bitmap_set_bit (killed_by_call
, DF_REF_ID (def_link
->ref
));
1694 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1696 df_bb_rd_local_compute (df
, bb
, killed_by_call
);
1699 if (df
->flags
& DF_HARD_REGS
)
1700 BITMAP_FREE (killed_by_call
);
1704 /* Compute local reaching use (upward exposed use) info for basic
1707 df_bb_ru_local_compute (struct df
*df
, basic_block bb
)
1709 /* This is much more tricky than computing reaching defs. With
1710 reaching defs, defs get killed by other defs. With upwards
1711 exposed uses, these get killed by defs with the same regno. */
1713 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1717 FOR_BB_INSNS_REVERSE (bb
, insn
)
1719 unsigned int uid
= INSN_UID (insn
);
1720 struct df_link
*def_link
;
1721 struct df_link
*use_link
;
1723 if (! INSN_P (insn
))
1726 for (def_link
= df
->insns
[uid
].defs
; def_link
; def_link
= def_link
->next
)
1728 struct ref
*def
= def_link
->ref
;
1729 unsigned int dregno
= DF_REF_REGNO (def
);
1731 for (use_link
= df
->regs
[dregno
].uses
; use_link
;
1732 use_link
= use_link
->next
)
1734 struct ref
*use
= use_link
->ref
;
1736 /* Add all uses of this reg to the set of kills. This
1737 is greedy since many of these uses will not actually
1738 be killed by this BB but it keeps things a lot
1740 bitmap_set_bit (bb_info
->ru_kill
, DF_REF_ID (use
));
1742 /* Zap from the set of gens for this BB. */
1743 bitmap_clear_bit (bb_info
->ru_gen
, DF_REF_ID (use
));
1747 for (use_link
= df
->insns
[uid
].uses
; use_link
; use_link
= use_link
->next
)
1749 struct ref
*use
= use_link
->ref
;
1750 /* Add use to set of gens in this BB. */
1751 bitmap_set_bit (bb_info
->ru_gen
, DF_REF_ID (use
));
1757 /* Compute local reaching use (upward exposed use) info for each basic
1758 block within BLOCKS. */
1760 df_ru_local_compute (struct df
*df
, bitmap blocks
)
1764 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1766 df_bb_ru_local_compute (df
, bb
);
1771 /* Compute local live variable info for basic block BB. */
1773 df_bb_lr_local_compute (struct df
*df
, basic_block bb
)
1775 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1778 FOR_BB_INSNS_REVERSE (bb
, insn
)
1780 unsigned int uid
= INSN_UID (insn
);
1781 struct df_link
*link
;
1783 if (! INSN_P (insn
))
1786 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
1788 struct ref
*def
= link
->ref
;
1789 unsigned int dregno
= DF_REF_REGNO (def
);
1791 /* Add def to set of defs in this BB. */
1792 bitmap_set_bit (bb_info
->lr_def
, dregno
);
1794 bitmap_clear_bit (bb_info
->lr_use
, dregno
);
1797 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
1799 struct ref
*use
= link
->ref
;
1800 /* Add use to set of uses in this BB. */
1801 bitmap_set_bit (bb_info
->lr_use
, DF_REF_REGNO (use
));
1807 /* Compute local live variable info for each basic block within BLOCKS. */
1809 df_lr_local_compute (struct df
*df
, bitmap blocks
)
1813 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1815 df_bb_lr_local_compute (df
, bb
);
1820 /* Compute register info: lifetime, bb, and number of defs and uses
1821 for basic block BB. */
1823 df_bb_reg_info_compute (struct df
*df
, basic_block bb
, bitmap live
)
1825 struct reg_info
*reg_info
= df
->regs
;
1826 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
1829 bitmap_copy (live
, bb_info
->lr_out
);
1831 FOR_BB_INSNS_REVERSE (bb
, insn
)
1833 unsigned int uid
= INSN_UID (insn
);
1835 struct df_link
*link
;
1838 if (! INSN_P (insn
))
1841 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
1843 struct ref
*def
= link
->ref
;
1844 unsigned int dregno
= DF_REF_REGNO (def
);
1846 /* Kill this register. */
1847 bitmap_clear_bit (live
, dregno
);
1848 reg_info
[dregno
].n_defs
++;
1851 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
1853 struct ref
*use
= link
->ref
;
1854 unsigned int uregno
= DF_REF_REGNO (use
);
1856 /* This register is now live. */
1857 bitmap_set_bit (live
, uregno
);
1858 reg_info
[uregno
].n_uses
++;
1861 /* Increment lifetimes of all live registers. */
1862 EXECUTE_IF_SET_IN_BITMAP (live
, 0, regno
, bi
)
1864 reg_info
[regno
].lifetime
++;
1870 /* Compute register info: lifetime, bb, and number of defs and uses. */
1872 df_reg_info_compute (struct df
*df
, bitmap blocks
)
1877 live
= BITMAP_ALLOC (NULL
);
1879 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1881 df_bb_reg_info_compute (df
, bb
, live
);
1888 /* Assign LUIDs for BB. */
1890 df_bb_luids_set (struct df
*df
, basic_block bb
)
1895 /* The LUIDs are monotonically increasing for each basic block. */
1897 FOR_BB_INSNS (bb
, insn
)
1900 DF_INSN_LUID (df
, insn
) = luid
++;
1901 DF_INSN_LUID (df
, insn
) = luid
;
1907 /* Assign LUIDs for each basic block within BLOCKS. */
1909 df_luids_set (struct df
*df
, bitmap blocks
)
1914 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
1916 total
+= df_bb_luids_set (df
, bb
);
1922 /* Perform dataflow analysis using existing DF structure for blocks
1923 within BLOCKS. If BLOCKS is zero, use all basic blocks in the CFG. */
1925 df_analyze_1 (struct df
*df
, bitmap blocks
, int flags
, int update
)
1930 struct dataflow dflow
;
1934 if (flags
& DF_UD_CHAIN
)
1935 aflags
|= DF_RD
| DF_RD_CHAIN
;
1937 if (flags
& DF_DU_CHAIN
)
1941 aflags
|= DF_RU_CHAIN
;
1943 if (flags
& DF_REG_INFO
)
1947 blocks
= df
->all_blocks
;
1952 df_refs_update (df
, NULL
);
1953 /* More fine grained incremental dataflow analysis would be
1954 nice. For now recompute the whole shebang for the
1957 df_refs_unlink (df
, blocks
);
1959 /* All the def-use, use-def chains can be potentially
1960 modified by changes in one block. The size of the
1961 bitmaps can also change. */
1965 /* Scan the function for all register defs and uses. */
1967 df_refs_record (df
, blocks
);
1969 /* Link all the new defs and uses to the insns. */
1970 df_refs_process (df
);
1973 /* Allocate the bitmaps now the total number of defs and uses are
1974 known. If the number of defs or uses have changed, then
1975 these bitmaps need to be reallocated. */
1976 df_bitmaps_alloc (df
, NULL
, aflags
);
1978 /* Set the LUIDs for each specified basic block. */
1979 df_luids_set (df
, blocks
);
1981 /* Recreate reg-def and reg-use chains from scratch so that first
1982 def is at the head of the reg-def chain and the last use is at
1983 the head of the reg-use chain. This is only important for
1984 regs local to a basic block as it speeds up searching. */
1985 if (aflags
& DF_RD_CHAIN
)
1987 df_reg_def_chain_create (df
, blocks
, false);
1990 if (aflags
& DF_RU_CHAIN
)
1992 df_reg_use_chain_create (df
, blocks
, false);
1995 df
->dfs_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
1996 df
->rc_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
1997 df
->rts_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
1999 flow_depth_first_order_compute (df
->dfs_order
, df
->rc_order
);
2000 flow_reverse_top_sort_order_compute (df
->rts_order
);
2003 /* Compute the sets of gens and kills for the defs of each bb. */
2004 dflow
.in
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2005 dflow
.out
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2006 dflow
.gen
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2007 dflow
.kill
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2009 df_rd_local_compute (df
, df
->flags
& DF_RD
? blocks
: df
->all_blocks
);
2012 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_in
;
2013 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_out
;
2014 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_gen
;
2015 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_kill
;
2018 dflow
.repr
= SR_BITMAP
;
2019 dflow
.dir
= DF_FORWARD
;
2020 dflow
.conf_op
= DF_UNION
;
2021 dflow
.transfun
= df_rd_transfer_function
;
2022 dflow
.n_blocks
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
2023 dflow
.order
= df
->rc_order
;
2026 iterative_dataflow (&dflow
);
2033 if (aflags
& DF_UD_CHAIN
)
2035 /* Create use-def chains. */
2036 df_ud_chain_create (df
, df
->all_blocks
);
2038 if (! (flags
& DF_RD
))
2044 /* Compute the sets of gens and kills for the upwards exposed
2046 dflow
.in
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2047 dflow
.out
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2048 dflow
.gen
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2049 dflow
.kill
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2051 df_ru_local_compute (df
, df
->flags
& DF_RU
? blocks
: df
->all_blocks
);
2055 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_in
;
2056 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_out
;
2057 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_gen
;
2058 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_kill
;
2061 dflow
.repr
= SR_BITMAP
;
2062 dflow
.dir
= DF_BACKWARD
;
2063 dflow
.conf_op
= DF_UNION
;
2064 dflow
.transfun
= df_ru_transfer_function
;
2065 dflow
.n_blocks
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
2066 dflow
.order
= df
->rts_order
;
2069 iterative_dataflow (&dflow
);
2076 if (aflags
& DF_DU_CHAIN
)
2078 /* Create def-use chains. */
2079 df_du_chain_create (df
, df
->all_blocks
);
2081 if (! (flags
& DF_RU
))
2085 /* Free up bitmaps that are no longer required. */
2087 df_bitmaps_free (df
, dflags
);
2091 /* Compute the sets of defs and uses of live variables. */
2092 dflow
.in
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2093 dflow
.out
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2094 dflow
.gen
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2095 dflow
.kill
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2097 df_lr_local_compute (df
, df
->flags
& DF_LR
? blocks
: df
->all_blocks
);
2101 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_in
;
2102 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_out
;
2103 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_use
;
2104 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_def
;
2107 dflow
.repr
= SR_BITMAP
;
2108 dflow
.dir
= DF_BACKWARD
;
2109 dflow
.conf_op
= DF_UNION
;
2110 dflow
.transfun
= df_lr_transfer_function
;
2111 dflow
.n_blocks
= n_basic_blocks
- NUM_FIXED_BLOCKS
;
2112 dflow
.order
= df
->rts_order
;
2115 iterative_dataflow (&dflow
);
2122 if (aflags
& DF_REG_INFO
)
2124 df_reg_info_compute (df
, df
->all_blocks
);
2127 free (df
->dfs_order
);
2128 free (df
->rc_order
);
2129 free (df
->rts_order
);
2133 /* Initialize dataflow analysis. */
2139 df
= xcalloc (1, sizeof (struct df
));
2141 /* Squirrel away a global for debugging. */
2148 /* Start queuing refs. */
2150 df_refs_queue (struct df
*df
)
2152 df
->def_id_save
= df
->def_id
;
2153 df
->use_id_save
= df
->use_id
;
2154 /* ???? Perhaps we should save current obstack state so that we can
2160 /* Process queued refs. */
2162 df_refs_process (struct df
*df
)
2166 /* Build new insn-def chains. */
2167 for (i
= df
->def_id_save
; i
!= df
->def_id
; i
++)
2169 struct ref
*def
= df
->defs
[i
];
2170 unsigned int uid
= DF_REF_INSN_UID (def
);
2172 /* Add def to head of def list for INSN. */
2174 = df_link_create (def
, df
->insns
[uid
].defs
);
2177 /* Build new insn-use chains. */
2178 for (i
= df
->use_id_save
; i
!= df
->use_id
; i
++)
2180 struct ref
*use
= df
->uses
[i
];
2181 unsigned int uid
= DF_REF_INSN_UID (use
);
2183 /* Add use to head of use list for INSN. */
2185 = df_link_create (use
, df
->insns
[uid
].uses
);
2191 /* Update refs for basic block BB. */
2193 df_bb_refs_update (struct df
*df
, basic_block bb
)
2198 /* While we have to scan the chain of insns for this BB, we do not
2199 need to allocate and queue a long chain of BB/INSN pairs. Using
2200 a bitmap for insns_modified saves memory and avoids queuing
2203 FOR_BB_INSNS (bb
, insn
)
2207 uid
= INSN_UID (insn
);
2209 if (bitmap_bit_p (df
->insns_modified
, uid
))
2211 /* Delete any allocated refs of this insn. MPH, FIXME. */
2212 df_insn_refs_unlink (df
, bb
, insn
);
2214 /* Scan the insn for refs. */
2215 df_insn_refs_record (df
, bb
, insn
);
2224 /* Process all the modified/deleted insns that were queued. */
2226 df_refs_update (struct df
*df
, bitmap blocks
)
2229 unsigned count
= 0, bbno
;
2231 df
->n_regs
= max_reg_num ();
2232 if (df
->n_regs
>= df
->reg_size
)
2233 df_reg_table_realloc (df
, 0);
2239 FOR_EACH_BB_IN_BITMAP (df
->bbs_modified
, 0, bb
,
2241 count
+= df_bb_refs_update (df
, bb
);
2248 EXECUTE_IF_AND_IN_BITMAP (df
->bbs_modified
, blocks
, 0, bbno
, bi
)
2250 count
+= df_bb_refs_update (df
, BASIC_BLOCK (bbno
));
2254 df_refs_process (df
);
2259 /* Return nonzero if any of the requested blocks in the bitmap
2260 BLOCKS have been modified. */
2262 df_modified_p (struct df
*df
, bitmap blocks
)
2271 if (bitmap_bit_p (df
->bbs_modified
, bb
->index
)
2272 && (! blocks
|| (blocks
== (bitmap
) -1) || bitmap_bit_p (blocks
, bb
->index
)))
2281 /* Analyze dataflow info for the basic blocks specified by the bitmap
2282 BLOCKS, or for the whole CFG if BLOCKS is zero, or just for the
2283 modified blocks if BLOCKS is -1. */
2286 df_analyze (struct df
*df
, bitmap blocks
, int flags
)
2290 /* We could deal with additional basic blocks being created by
2291 rescanning everything again. */
2292 gcc_assert (!df
->n_bbs
|| df
->n_bbs
== (unsigned int) last_basic_block
);
2294 update
= df_modified_p (df
, blocks
);
2295 if (update
|| (flags
!= df
->flags
))
2301 /* Recompute everything from scratch. */
2304 /* Allocate and initialize data structures. */
2305 df_alloc (df
, max_reg_num ());
2306 df_analyze_1 (df
, 0, flags
, 0);
2311 if (blocks
== (bitmap
) -1)
2312 blocks
= df
->bbs_modified
;
2314 gcc_assert (df
->n_bbs
);
2316 df_analyze_1 (df
, blocks
, flags
, 1);
2317 bitmap_zero (df
->bbs_modified
);
2318 bitmap_zero (df
->insns_modified
);
2324 /* Remove the entries not in BLOCKS from the LIST of length LEN, preserving
2325 the order of the remaining entries. Returns the length of the resulting
2329 prune_to_subcfg (int list
[], unsigned len
, bitmap blocks
)
2333 for (act
= 0, last
= 0; act
< len
; act
++)
2334 if (bitmap_bit_p (blocks
, list
[act
]))
2335 list
[last
++] = list
[act
];
2340 /* Alternative entry point to the analysis. Analyze just the part of the cfg
2341 graph induced by BLOCKS.
2343 TODO I am not quite sure how to avoid code duplication with df_analyze_1
2344 here, and simultaneously not make even greater chaos in it. We behave
2345 slightly differently in some details, especially in handling modified
2349 df_analyze_subcfg (struct df
*df
, bitmap blocks
, int flags
)
2353 struct dataflow dflow
;
2356 if (flags
& DF_UD_CHAIN
)
2357 flags
|= DF_RD
| DF_RD_CHAIN
;
2358 if (flags
& DF_DU_CHAIN
)
2361 flags
|= DF_RU_CHAIN
;
2362 if (flags
& DF_REG_INFO
)
2367 df_alloc (df
, max_reg_num ());
2369 /* Mark all insns as modified. */
2373 FOR_BB_INSNS (bb
, insn
)
2375 df_insn_modify (df
, bb
, insn
);
2382 df_reg_def_chain_clean (df
);
2383 df_reg_use_chain_clean (df
);
2385 df_refs_update (df
, blocks
);
2387 /* Clear the updated stuff from ``modified'' bitmaps. */
2388 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
2390 if (bitmap_bit_p (df
->bbs_modified
, bb
->index
))
2392 FOR_BB_INSNS (bb
, insn
)
2394 bitmap_clear_bit (df
->insns_modified
, INSN_UID (insn
));
2397 bitmap_clear_bit (df
->bbs_modified
, bb
->index
);
2401 /* Allocate the bitmaps now the total number of defs and uses are
2402 known. If the number of defs or uses have changed, then
2403 these bitmaps need to be reallocated. */
2404 df_bitmaps_alloc (df
, blocks
, flags
);
2406 /* Set the LUIDs for each specified basic block. */
2407 df_luids_set (df
, blocks
);
2409 /* Recreate reg-def and reg-use chains from scratch so that first
2410 def is at the head of the reg-def chain and the last use is at
2411 the head of the reg-use chain. This is only important for
2412 regs local to a basic block as it speeds up searching. */
2413 if (flags
& DF_RD_CHAIN
)
2415 df_reg_def_chain_create (df
, blocks
, true);
2418 if (flags
& DF_RU_CHAIN
)
2420 df_reg_use_chain_create (df
, blocks
, true);
2423 df
->dfs_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
2424 df
->rc_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
2425 df
->rts_order
= xmalloc (sizeof (int) * n_basic_blocks
- NUM_FIXED_BLOCKS
);
2427 flow_depth_first_order_compute (df
->dfs_order
, df
->rc_order
);
2428 flow_reverse_top_sort_order_compute (df
->rts_order
);
2430 n_blocks
= prune_to_subcfg (df
->dfs_order
, n_basic_blocks
- NUM_FIXED_BLOCKS
, blocks
);
2431 prune_to_subcfg (df
->rc_order
, n_basic_blocks
- NUM_FIXED_BLOCKS
, blocks
);
2432 prune_to_subcfg (df
->rts_order
, n_basic_blocks
- NUM_FIXED_BLOCKS
, blocks
);
2434 dflow
.in
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2435 dflow
.out
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2436 dflow
.gen
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2437 dflow
.kill
= xmalloc (sizeof (bitmap
) * last_basic_block
);
2441 /* Compute the sets of gens and kills for the defs of each bb. */
2442 df_rd_local_compute (df
, blocks
);
2444 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
2446 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_in
;
2447 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_out
;
2448 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_gen
;
2449 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->rd_kill
;
2452 dflow
.repr
= SR_BITMAP
;
2453 dflow
.dir
= DF_FORWARD
;
2454 dflow
.conf_op
= DF_UNION
;
2455 dflow
.transfun
= df_rd_transfer_function
;
2456 dflow
.n_blocks
= n_blocks
;
2457 dflow
.order
= df
->rc_order
;
2460 iterative_dataflow (&dflow
);
2463 if (flags
& DF_UD_CHAIN
)
2465 /* Create use-def chains. */
2466 df_ud_chain_create (df
, blocks
);
2471 /* Compute the sets of gens and kills for the upwards exposed
2473 df_ru_local_compute (df
, blocks
);
2475 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
2477 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_in
;
2478 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_out
;
2479 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_gen
;
2480 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->ru_kill
;
2483 dflow
.repr
= SR_BITMAP
;
2484 dflow
.dir
= DF_BACKWARD
;
2485 dflow
.conf_op
= DF_UNION
;
2486 dflow
.transfun
= df_ru_transfer_function
;
2487 dflow
.n_blocks
= n_blocks
;
2488 dflow
.order
= df
->rts_order
;
2491 iterative_dataflow (&dflow
);
2494 if (flags
& DF_DU_CHAIN
)
2496 /* Create def-use chains. */
2497 df_du_chain_create (df
, blocks
);
2502 /* Compute the sets of defs and uses of live variables. */
2503 df_lr_local_compute (df
, blocks
);
2507 dflow
.in
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_in
;
2508 dflow
.out
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_out
;
2509 dflow
.gen
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_use
;
2510 dflow
.kill
[bb
->index
] = DF_BB_INFO (df
, bb
)->lr_def
;
2513 dflow
.repr
= SR_BITMAP
;
2514 dflow
.dir
= DF_BACKWARD
;
2515 dflow
.conf_op
= DF_UNION
;
2516 dflow
.transfun
= df_lr_transfer_function
;
2517 dflow
.n_blocks
= n_blocks
;
2518 dflow
.order
= df
->rts_order
;
2521 iterative_dataflow (&dflow
);
2524 if (flags
& DF_REG_INFO
)
2526 df_reg_info_compute (df
, blocks
);
2534 free (df
->dfs_order
);
2535 free (df
->rc_order
);
2536 free (df
->rts_order
);
2539 /* Free all the dataflow info and the DF structure. */
2541 df_finish (struct df
*df
)
2547 /* Unlink INSN from its reference information. */
2549 df_insn_refs_unlink (struct df
*df
, basic_block bb ATTRIBUTE_UNUSED
, rtx insn
)
2551 struct df_link
*link
;
2554 uid
= INSN_UID (insn
);
2556 /* Unlink all refs defined by this insn. */
2557 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
2558 df_def_unlink (df
, link
->ref
);
2560 /* Unlink all refs used by this insn. */
2561 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
2562 df_use_unlink (df
, link
->ref
);
2564 df
->insns
[uid
].defs
= 0;
2565 df
->insns
[uid
].uses
= 0;
2570 /* Unlink all the insns within BB from their reference information. */
2572 df_bb_refs_unlink (struct df
*df
, basic_block bb
)
2576 /* Scan the block an insn at a time from beginning to end. */
2577 for (insn
= BB_HEAD (bb
); ; insn
= NEXT_INSN (insn
))
2581 /* Unlink refs for INSN. */
2582 df_insn_refs_unlink (df
, bb
, insn
);
2584 if (insn
== BB_END (bb
))
2590 /* Unlink all the refs in the basic blocks specified by BLOCKS.
2591 Not currently used. */
2593 df_refs_unlink (struct df
*df
, bitmap blocks
)
2599 FOR_EACH_BB_IN_BITMAP (blocks
, 0, bb
,
2601 df_bb_refs_unlink (df
, bb
);
2607 df_bb_refs_unlink (df
, bb
);
2612 /* Functions to modify insns. */
2615 /* Delete INSN and all its reference information. */
2617 df_insn_delete (struct df
*df
, basic_block bb ATTRIBUTE_UNUSED
, rtx insn
)
2619 /* If the insn is a jump, we should perhaps call delete_insn to
2620 handle the JUMP_LABEL? */
2622 /* We should not be deleting the NOTE_INSN_BASIC_BLOCK or label. */
2623 gcc_assert (insn
!= BB_HEAD (bb
));
2625 /* Delete the insn. */
2628 df_insn_modify (df
, bb
, insn
);
2630 return NEXT_INSN (insn
);
2633 /* Mark that basic block BB was modified. */
2636 df_bb_modify (struct df
*df
, basic_block bb
)
2638 if ((unsigned) bb
->index
>= df
->n_bbs
)
2639 df_bb_table_realloc (df
, df
->n_bbs
);
2641 bitmap_set_bit (df
->bbs_modified
, bb
->index
);
2644 /* Mark that INSN within BB may have changed (created/modified/deleted).
2645 This may be called multiple times for the same insn. There is no
2646 harm calling this function if the insn wasn't changed; it will just
2647 slow down the rescanning of refs. */
2649 df_insn_modify (struct df
*df
, basic_block bb
, rtx insn
)
2653 uid
= INSN_UID (insn
);
2654 if (uid
>= df
->insn_size
)
2655 df_insn_table_realloc (df
, uid
);
2657 df_bb_modify (df
, bb
);
2658 bitmap_set_bit (df
->insns_modified
, uid
);
2660 /* For incremental updating on the fly, perhaps we could make a copy
2661 of all the refs of the original insn and turn them into
2662 anti-refs. When df_refs_update finds these anti-refs, it annihilates
2663 the original refs. If validate_change fails then these anti-refs
2664 will just get ignored. */
2667 /* Check if INSN was marked as changed. Of course the correctness of
2668 the information depends on whether the instruction was really modified
2669 at the time df_insn_modify was called. */
2671 df_insn_modified_p (struct df
*df
, rtx insn
)
2675 uid
= INSN_UID (insn
);
2676 return (df
->insns_modified
2677 && uid
< df
->insn_size
2678 && bitmap_bit_p (df
->insns_modified
, uid
));
2681 typedef struct replace_args
2690 /* Replace mem pointed to by PX with its associated pseudo register.
2691 DATA is actually a pointer to a structure describing the
2692 instruction currently being scanned and the MEM we are currently
2695 df_rtx_mem_replace (rtx
*px
, void *data
)
2697 replace_args
*args
= (replace_args
*) data
;
2700 if (mem
== NULL_RTX
)
2703 switch (GET_CODE (mem
))
2709 /* We're not interested in the MEM associated with a
2710 CONST_DOUBLE, so there's no need to traverse into one. */
2714 /* This is not a MEM. */
2718 if (!rtx_equal_p (args
->match
, mem
))
2719 /* This is not the MEM we are currently replacing. */
2722 /* Actually replace the MEM. */
2723 validate_change (args
->insn
, px
, args
->replacement
, 1);
2731 df_insn_mem_replace (struct df
*df
, basic_block bb
, rtx insn
, rtx mem
, rtx reg
)
2737 args
.replacement
= reg
;
2740 /* Search and replace all matching mems within insn. */
2741 for_each_rtx (&insn
, df_rtx_mem_replace
, &args
);
2744 df_insn_modify (df
, bb
, insn
);
2746 /* ???? FIXME. We may have a new def or one or more new uses of REG
2747 in INSN. REG should be a new pseudo so it won't affect the
2748 dataflow information that we currently have. We should add
2749 the new uses and defs to INSN and then recreate the chains
2750 when df_analyze is called. */
2751 return args
.modified
;
2755 /* Replace one register with another. Called through for_each_rtx; PX
2756 points to the rtx being scanned. DATA is actually a pointer to a
2757 structure of arguments. */
2759 df_rtx_reg_replace (rtx
*px
, void *data
)
2762 replace_args
*args
= (replace_args
*) data
;
2767 if (x
== args
->match
)
2769 validate_change (args
->insn
, px
, args
->replacement
, 1);
2777 /* Replace the reg within every ref on CHAIN that is within the set
2778 BLOCKS of basic blocks with NEWREG. Also update the regs within
2781 df_refs_reg_replace (struct df
*df
, bitmap blocks
, struct df_link
*chain
, rtx oldreg
, rtx newreg
)
2783 struct df_link
*link
;
2787 blocks
= df
->all_blocks
;
2789 args
.match
= oldreg
;
2790 args
.replacement
= newreg
;
2793 for (link
= chain
; link
; link
= link
->next
)
2795 struct ref
*ref
= link
->ref
;
2796 rtx insn
= DF_REF_INSN (ref
);
2798 if (! INSN_P (insn
))
2801 gcc_assert (bitmap_bit_p (blocks
, DF_REF_BBNO (ref
)));
2803 df_ref_reg_replace (df
, ref
, oldreg
, newreg
);
2805 /* Replace occurrences of the reg within the REG_NOTES. */
2806 if ((! link
->next
|| DF_REF_INSN (ref
)
2807 != DF_REF_INSN (link
->next
->ref
))
2808 && REG_NOTES (insn
))
2811 for_each_rtx (®_NOTES (insn
), df_rtx_reg_replace
, &args
);
2817 /* Replace all occurrences of register OLDREG with register NEWREG in
2818 blocks defined by bitmap BLOCKS. This also replaces occurrences of
2819 OLDREG in the REG_NOTES but only for insns containing OLDREG. This
2820 routine expects the reg-use and reg-def chains to be valid. */
2822 df_reg_replace (struct df
*df
, bitmap blocks
, rtx oldreg
, rtx newreg
)
2824 unsigned int oldregno
= REGNO (oldreg
);
2826 df_refs_reg_replace (df
, blocks
, df
->regs
[oldregno
].defs
, oldreg
, newreg
);
2827 df_refs_reg_replace (df
, blocks
, df
->regs
[oldregno
].uses
, oldreg
, newreg
);
2832 /* Try replacing the reg within REF with NEWREG. Do not modify
2833 def-use/use-def chains. */
2835 df_ref_reg_replace (struct df
*df
, struct ref
*ref
, rtx oldreg
, rtx newreg
)
2837 /* Check that insn was deleted by being converted into a NOTE. If
2838 so ignore this insn. */
2839 if (! INSN_P (DF_REF_INSN (ref
)))
2842 gcc_assert (!oldreg
|| oldreg
== DF_REF_REG (ref
));
2844 if (! validate_change (DF_REF_INSN (ref
), DF_REF_LOC (ref
), newreg
, 1))
2847 df_insn_modify (df
, DF_REF_BB (ref
), DF_REF_INSN (ref
));
2853 df_bb_def_use_swap (struct df
*df
, basic_block bb
, rtx def_insn
, rtx use_insn
, unsigned int regno
)
2859 struct df_link
*link
;
2861 def
= df_bb_insn_regno_first_def_find (df
, bb
, def_insn
, regno
);
2865 use
= df_bb_insn_regno_last_use_find (df
, bb
, use_insn
, regno
);
2869 /* The USE no longer exists. */
2870 use_uid
= INSN_UID (use_insn
);
2871 df_use_unlink (df
, use
);
2872 df_ref_unlink (&df
->insns
[use_uid
].uses
, use
);
2874 /* The DEF requires shifting so remove it from DEF_INSN
2875 and add it to USE_INSN by reusing LINK. */
2876 def_uid
= INSN_UID (def_insn
);
2877 link
= df_ref_unlink (&df
->insns
[def_uid
].defs
, def
);
2879 link
->next
= df
->insns
[use_uid
].defs
;
2880 df
->insns
[use_uid
].defs
= link
;
2883 link
= df_ref_unlink (&df
->regs
[regno
].defs
, def
);
2885 link
->next
= df
->regs
[regno
].defs
;
2886 df
->insns
[regno
].defs
= link
;
2889 DF_REF_INSN (def
) = use_insn
;
2894 /* Record df between FIRST_INSN and LAST_INSN inclusive. All new
2895 insns must be processed by this routine. */
2897 df_insns_modify (struct df
*df
, basic_block bb
, rtx first_insn
, rtx last_insn
)
2901 for (insn
= first_insn
; ; insn
= NEXT_INSN (insn
))
2905 /* A non-const call should not have slipped through the net. If
2906 it does, we need to create a new basic block. Ouch. The
2907 same applies for a label. */
2908 gcc_assert ((!CALL_P (insn
) || CONST_OR_PURE_CALL_P (insn
))
2909 && !LABEL_P (insn
));
2911 uid
= INSN_UID (insn
);
2913 if (uid
>= df
->insn_size
)
2914 df_insn_table_realloc (df
, uid
);
2916 df_insn_modify (df
, bb
, insn
);
2918 if (insn
== last_insn
)
2924 /* Emit PATTERN before INSN within BB. */
2926 df_pattern_emit_before (struct df
*df
, rtx pattern
, basic_block bb
, rtx insn
)
2929 rtx prev_insn
= PREV_INSN (insn
);
2931 /* We should not be inserting before the start of the block. */
2932 gcc_assert (insn
!= BB_HEAD (bb
));
2933 ret_insn
= emit_insn_before (pattern
, insn
);
2934 if (ret_insn
== insn
)
2937 df_insns_modify (df
, bb
, NEXT_INSN (prev_insn
), ret_insn
);
2942 /* Emit PATTERN after INSN within BB. */
2944 df_pattern_emit_after (struct df
*df
, rtx pattern
, basic_block bb
, rtx insn
)
2948 ret_insn
= emit_insn_after (pattern
, insn
);
2949 if (ret_insn
== insn
)
2952 df_insns_modify (df
, bb
, NEXT_INSN (insn
), ret_insn
);
2957 /* Emit jump PATTERN after INSN within BB. */
2959 df_jump_pattern_emit_after (struct df
*df
, rtx pattern
, basic_block bb
, rtx insn
)
2963 ret_insn
= emit_jump_insn_after (pattern
, insn
);
2964 if (ret_insn
== insn
)
2967 df_insns_modify (df
, bb
, NEXT_INSN (insn
), ret_insn
);
2972 /* Move INSN within BB before BEFORE_INSN within BEFORE_BB.
2974 This function should only be used to move loop invariant insns
2975 out of a loop where it has been proven that the def-use info
2976 will still be valid. */
2978 df_insn_move_before (struct df
*df
, basic_block bb
, rtx insn
, basic_block before_bb
, rtx before_insn
)
2980 struct df_link
*link
;
2984 return df_pattern_emit_before (df
, insn
, before_bb
, before_insn
);
2986 uid
= INSN_UID (insn
);
2988 /* Change bb for all df defined and used by this insn. */
2989 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
2990 DF_REF_BB (link
->ref
) = before_bb
;
2991 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
2992 DF_REF_BB (link
->ref
) = before_bb
;
2994 /* The lifetimes of the registers used in this insn will be reduced
2995 while the lifetimes of the registers defined in this insn
2996 are likely to be increased. */
2998 /* ???? Perhaps all the insns moved should be stored on a list
2999 which df_analyze removes when it recalculates data flow. */
3001 return emit_insn_before (insn
, before_insn
);
3004 /* Functions to query dataflow information. */
3008 df_insn_regno_def_p (struct df
*df
, basic_block bb ATTRIBUTE_UNUSED
,
3009 rtx insn
, unsigned int regno
)
3012 struct df_link
*link
;
3014 uid
= INSN_UID (insn
);
3016 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
3018 struct ref
*def
= link
->ref
;
3020 if (DF_REF_REGNO (def
) == regno
)
3027 /* Finds the reference corresponding to the definition of REG in INSN.
3028 DF is the dataflow object. */
3031 df_find_def (struct df
*df
, rtx insn
, rtx reg
)
3033 struct df_link
*defs
;
3035 for (defs
= DF_INSN_DEFS (df
, insn
); defs
; defs
= defs
->next
)
3036 if (rtx_equal_p (DF_REF_REG (defs
->ref
), reg
))
3042 /* Return 1 if REG is referenced in INSN, zero otherwise. */
3045 df_reg_used (struct df
*df
, rtx insn
, rtx reg
)
3047 struct df_link
*uses
;
3049 for (uses
= DF_INSN_USES (df
, insn
); uses
; uses
= uses
->next
)
3050 if (rtx_equal_p (DF_REF_REG (uses
->ref
), reg
))
3057 df_def_dominates_all_uses_p (struct df
*df ATTRIBUTE_UNUSED
, struct ref
*def
)
3059 struct df_link
*du_link
;
3061 /* Follow def-use chain to find all the uses of this def. */
3062 for (du_link
= DF_REF_CHAIN (def
); du_link
; du_link
= du_link
->next
)
3064 struct ref
*use
= du_link
->ref
;
3065 struct df_link
*ud_link
;
3067 /* Follow use-def chain to check all the defs for this use. */
3068 for (ud_link
= DF_REF_CHAIN (use
); ud_link
; ud_link
= ud_link
->next
)
3069 if (ud_link
->ref
!= def
)
3077 df_insn_dominates_all_uses_p (struct df
*df
, basic_block bb ATTRIBUTE_UNUSED
,
3081 struct df_link
*link
;
3083 uid
= INSN_UID (insn
);
3085 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
3087 struct ref
*def
= link
->ref
;
3089 if (! df_def_dominates_all_uses_p (df
, def
))
3097 /* Return nonzero if all DF dominates all the uses within the bitmap
3100 df_def_dominates_uses_p (struct df
*df ATTRIBUTE_UNUSED
, struct ref
*def
,
3103 struct df_link
*du_link
;
3105 /* Follow def-use chain to find all the uses of this def. */
3106 for (du_link
= DF_REF_CHAIN (def
); du_link
; du_link
= du_link
->next
)
3108 struct ref
*use
= du_link
->ref
;
3109 struct df_link
*ud_link
;
3111 /* Only worry about the uses within BLOCKS. For example,
3112 consider a register defined within a loop that is live at the
3114 if (bitmap_bit_p (blocks
, DF_REF_BBNO (use
)))
3116 /* Follow use-def chain to check all the defs for this use. */
3117 for (ud_link
= DF_REF_CHAIN (use
); ud_link
; ud_link
= ud_link
->next
)
3118 if (ud_link
->ref
!= def
)
3126 /* Return nonzero if all the defs of INSN within BB dominates
3127 all the corresponding uses. */
3129 df_insn_dominates_uses_p (struct df
*df
, basic_block bb ATTRIBUTE_UNUSED
,
3130 rtx insn
, bitmap blocks
)
3133 struct df_link
*link
;
3135 uid
= INSN_UID (insn
);
3137 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
3139 struct ref
*def
= link
->ref
;
3141 /* Only consider the defs within BLOCKS. */
3142 if (bitmap_bit_p (blocks
, DF_REF_BBNO (def
))
3143 && ! df_def_dominates_uses_p (df
, def
, blocks
))
3150 /* Return the basic block that REG referenced in or NULL if referenced
3151 in multiple basic blocks. */
3153 df_regno_bb (struct df
*df
, unsigned int regno
)
3155 struct df_link
*defs
= df
->regs
[regno
].defs
;
3156 struct df_link
*uses
= df
->regs
[regno
].uses
;
3157 struct ref
*def
= defs
? defs
->ref
: 0;
3158 struct ref
*use
= uses
? uses
->ref
: 0;
3159 basic_block bb_def
= def
? DF_REF_BB (def
) : 0;
3160 basic_block bb_use
= use
? DF_REF_BB (use
) : 0;
3162 /* Compare blocks of first def and last use. ???? FIXME. What if
3163 the reg-def and reg-use lists are not correctly ordered. */
3164 return bb_def
== bb_use
? bb_def
: 0;
3168 /* Return nonzero if REG used in multiple basic blocks. */
3170 df_reg_global_p (struct df
*df
, rtx reg
)
3172 return df_regno_bb (df
, REGNO (reg
)) != 0;
3176 /* Return total lifetime (in insns) of REG. */
3178 df_reg_lifetime (struct df
*df
, rtx reg
)
3180 return df
->regs
[REGNO (reg
)].lifetime
;
3184 /* Return nonzero if REG live at start of BB. */
3186 df_bb_reg_live_start_p (struct df
*df
, basic_block bb
, rtx reg
)
3188 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
3190 gcc_assert (bb_info
->lr_in
);
3192 return bitmap_bit_p (bb_info
->lr_in
, REGNO (reg
));
3196 /* Return nonzero if REG live at end of BB. */
3198 df_bb_reg_live_end_p (struct df
*df
, basic_block bb
, rtx reg
)
3200 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
3202 gcc_assert (bb_info
->lr_in
);
3204 return bitmap_bit_p (bb_info
->lr_out
, REGNO (reg
));
3208 /* Return -1 if life of REG1 before life of REG2, 1 if life of REG1
3209 after life of REG2, or 0, if the lives overlap. */
3211 df_bb_regs_lives_compare (struct df
*df
, basic_block bb
, rtx reg1
, rtx reg2
)
3213 unsigned int regno1
= REGNO (reg1
);
3214 unsigned int regno2
= REGNO (reg2
);
3221 /* The regs must be local to BB. */
3222 gcc_assert (df_regno_bb (df
, regno1
) == bb
3223 && df_regno_bb (df
, regno2
) == bb
);
3225 def2
= df_bb_regno_first_def_find (df
, bb
, regno2
);
3226 use1
= df_bb_regno_last_use_find (df
, bb
, regno1
);
3228 if (DF_INSN_LUID (df
, DF_REF_INSN (def2
))
3229 > DF_INSN_LUID (df
, DF_REF_INSN (use1
)))
3232 def1
= df_bb_regno_first_def_find (df
, bb
, regno1
);
3233 use2
= df_bb_regno_last_use_find (df
, bb
, regno2
);
3235 if (DF_INSN_LUID (df
, DF_REF_INSN (def1
))
3236 > DF_INSN_LUID (df
, DF_REF_INSN (use2
)))
3243 /* Return true if the definition DEF, which is in the same basic
3244 block as USE, is available at USE. So DEF may as well be
3245 dead, in which case using it will extend its live range. */
3247 df_local_def_available_p (struct df
*df
, struct ref
*def
, struct ref
*use
)
3249 struct df_link
*link
;
3250 int def_luid
= DF_INSN_LUID (df
, DF_REF_INSN (def
));
3252 unsigned int regno
= REGNO (def
->reg
);
3255 /* The regs must be local to BB. */
3256 gcc_assert (DF_REF_BB (def
) == DF_REF_BB (use
));
3257 bb
= DF_REF_BB (def
);
3259 /* This assumes that the reg-def list is ordered such that for any
3260 BB, the first def is found first. However, since the BBs are not
3261 ordered, the first def in the chain is not necessarily the first
3262 def in the function. */
3263 for (link
= df
->regs
[regno
].defs
; link
; link
= link
->next
)
3265 struct ref
*this_def
= link
->ref
;
3266 if (DF_REF_BB (this_def
) == bb
)
3268 int this_luid
= DF_INSN_LUID (df
, DF_REF_INSN (this_def
));
3269 /* Do nothing with defs coming before DEF. */
3270 if (this_luid
> def_luid
)
3271 return this_luid
> DF_INSN_LUID (df
, DF_REF_INSN (use
));
3276 /* DEF was the last in its basic block. */
3280 /* DEF was the last in the function. */
3285 /* Return last use of REGNO within BB. */
3287 df_bb_regno_last_use_find (struct df
*df
, basic_block bb
, unsigned int regno
)
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 (struct df
*df
, basic_block bb
, unsigned int regno
)
3310 struct df_link
*link
;
3312 /* This assumes that the reg-def list is ordered such that for any
3313 BB, the first def is found first. However, since the BBs are not
3314 ordered, the first def in the chain is not necessarily the first
3315 def in the function. */
3316 for (link
= df
->regs
[regno
].defs
; link
; link
= link
->next
)
3318 struct ref
*def
= link
->ref
;
3320 if (DF_REF_BB (def
) == bb
)
3326 /* Return last def of REGNO within BB. */
3328 df_bb_regno_last_def_find (struct df
*df
, basic_block bb
, unsigned int regno
)
3330 struct df_link
*link
;
3331 struct ref
*last_def
= NULL
;
3334 /* This assumes that the reg-def list is ordered such that for any
3335 BB, the first def is found first. However, since the BBs are not
3336 ordered, the first def in the chain is not necessarily the first
3337 def in the function. */
3338 for (link
= df
->regs
[regno
].defs
; link
; link
= link
->next
)
3340 struct ref
*def
= link
->ref
;
3341 /* The first time in the desired block. */
3342 if (DF_REF_BB (def
) == bb
)
3344 /* The last def in the desired block. */
3352 /* Return last use of REGNO inside INSN within BB. */
3354 df_bb_insn_regno_last_use_find (struct df
*df
,
3355 basic_block bb ATTRIBUTE_UNUSED
, rtx insn
,
3359 struct df_link
*link
;
3361 uid
= INSN_UID (insn
);
3363 for (link
= df
->insns
[uid
].uses
; link
; link
= link
->next
)
3365 struct ref
*use
= link
->ref
;
3367 if (DF_REF_REGNO (use
) == regno
)
3375 /* Return first def of REGNO inside INSN within BB. */
3377 df_bb_insn_regno_first_def_find (struct df
*df
,
3378 basic_block bb ATTRIBUTE_UNUSED
, rtx insn
,
3382 struct df_link
*link
;
3384 uid
= INSN_UID (insn
);
3386 for (link
= df
->insns
[uid
].defs
; link
; link
= link
->next
)
3388 struct ref
*def
= link
->ref
;
3390 if (DF_REF_REGNO (def
) == regno
)
3398 /* Return insn using REG if the BB contains only a single
3399 use and def of REG. */
3401 df_bb_single_def_use_insn_find (struct df
*df
, basic_block bb
, rtx insn
, rtx reg
)
3405 struct df_link
*du_link
;
3407 def
= df_bb_insn_regno_first_def_find (df
, bb
, insn
, REGNO (reg
));
3411 du_link
= DF_REF_CHAIN (def
);
3418 /* Check if def is dead. */
3422 /* Check for multiple uses. */
3426 return DF_REF_INSN (use
);
3429 /* Functions for debugging/dumping dataflow information. */
3432 /* Dump a def-use or use-def chain for REF to FILE. */
3434 df_chain_dump (struct df_link
*link
, FILE *file
)
3436 fprintf (file
, "{ ");
3437 for (; link
; link
= link
->next
)
3439 fprintf (file
, "%c%d ",
3440 DF_REF_REG_DEF_P (link
->ref
) ? 'd' : 'u',
3441 DF_REF_ID (link
->ref
));
3443 fprintf (file
, "}");
3447 /* Dump a chain of refs with the associated regno. */
3449 df_chain_dump_regno (struct df_link
*link
, FILE *file
)
3451 fprintf (file
, "{ ");
3452 for (; link
; link
= link
->next
)
3454 fprintf (file
, "%c%d(%d) ",
3455 DF_REF_REG_DEF_P (link
->ref
) ? 'd' : 'u',
3456 DF_REF_ID (link
->ref
),
3457 DF_REF_REGNO (link
->ref
));
3459 fprintf (file
, "}");
3463 /* Dump dataflow info. */
3465 df_dump (struct df
*df
, int flags
, FILE *file
)
3473 fprintf (file
, "\nDataflow summary:\n");
3474 fprintf (file
, "n_regs = %d, n_defs = %d, n_uses = %d, n_bbs = %d\n",
3475 df
->n_regs
, df
->n_defs
, df
->n_uses
, df
->n_bbs
);
3481 fprintf (file
, "Reaching defs:\n");
3484 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
3486 if (! bb_info
->rd_in
)
3489 fprintf (file
, "bb %d in \t", bb
->index
);
3490 dump_bitmap (file
, bb_info
->rd_in
);
3491 fprintf (file
, "bb %d gen \t", bb
->index
);
3492 dump_bitmap (file
, bb_info
->rd_gen
);
3493 fprintf (file
, "bb %d kill\t", bb
->index
);
3494 dump_bitmap (file
, bb_info
->rd_kill
);
3495 fprintf (file
, "bb %d out \t", bb
->index
);
3496 dump_bitmap (file
, bb_info
->rd_out
);
3500 if (flags
& DF_UD_CHAIN
)
3502 fprintf (file
, "Use-def chains:\n");
3503 for (j
= 0; j
< df
->n_defs
; j
++)
3507 fprintf (file
, "d%d bb %d luid %d insn %d reg %d ",
3508 j
, DF_REF_BBNO (df
->defs
[j
]),
3509 DF_INSN_LUID (df
, DF_REF_INSN (df
->defs
[j
])),
3510 DF_REF_INSN_UID (df
->defs
[j
]),
3511 DF_REF_REGNO (df
->defs
[j
]));
3512 if (df
->defs
[j
]->flags
& DF_REF_READ_WRITE
)
3513 fprintf (file
, "read/write ");
3514 df_chain_dump (DF_REF_CHAIN (df
->defs
[j
]), file
);
3515 fprintf (file
, "\n");
3522 fprintf (file
, "Reaching uses:\n");
3525 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
3527 if (! bb_info
->ru_in
)
3530 fprintf (file
, "bb %d in \t", bb
->index
);
3531 dump_bitmap (file
, bb_info
->ru_in
);
3532 fprintf (file
, "bb %d gen \t", bb
->index
);
3533 dump_bitmap (file
, bb_info
->ru_gen
);
3534 fprintf (file
, "bb %d kill\t", bb
->index
);
3535 dump_bitmap (file
, bb_info
->ru_kill
);
3536 fprintf (file
, "bb %d out \t", bb
->index
);
3537 dump_bitmap (file
, bb_info
->ru_out
);
3541 if (flags
& DF_DU_CHAIN
)
3543 fprintf (file
, "Def-use chains:\n");
3544 for (j
= 0; j
< df
->n_uses
; j
++)
3548 fprintf (file
, "u%d bb %d luid %d insn %d reg %d ",
3549 j
, DF_REF_BBNO (df
->uses
[j
]),
3550 DF_INSN_LUID (df
, DF_REF_INSN (df
->uses
[j
])),
3551 DF_REF_INSN_UID (df
->uses
[j
]),
3552 DF_REF_REGNO (df
->uses
[j
]));
3553 if (df
->uses
[j
]->flags
& DF_REF_READ_WRITE
)
3554 fprintf (file
, "read/write ");
3555 df_chain_dump (DF_REF_CHAIN (df
->uses
[j
]), file
);
3556 fprintf (file
, "\n");
3563 fprintf (file
, "Live regs:\n");
3566 struct bb_info
*bb_info
= DF_BB_INFO (df
, bb
);
3568 if (! bb_info
->lr_in
)
3571 fprintf (file
, "bb %d in \t", bb
->index
);
3572 dump_bitmap (file
, bb_info
->lr_in
);
3573 fprintf (file
, "bb %d use \t", bb
->index
);
3574 dump_bitmap (file
, bb_info
->lr_use
);
3575 fprintf (file
, "bb %d def \t", bb
->index
);
3576 dump_bitmap (file
, bb_info
->lr_def
);
3577 fprintf (file
, "bb %d out \t", bb
->index
);
3578 dump_bitmap (file
, bb_info
->lr_out
);
3582 if (flags
& (DF_REG_INFO
| DF_RD_CHAIN
| DF_RU_CHAIN
))
3584 struct reg_info
*reg_info
= df
->regs
;
3586 fprintf (file
, "Register info:\n");
3587 for (j
= 0; j
< df
->n_regs
; j
++)
3589 if (((flags
& DF_REG_INFO
)
3590 && (reg_info
[j
].n_uses
|| reg_info
[j
].n_defs
))
3591 || ((flags
& DF_RD_CHAIN
) && reg_info
[j
].defs
)
3592 || ((flags
& DF_RU_CHAIN
) && reg_info
[j
].uses
))
3594 fprintf (file
, "reg %d", j
);
3595 if ((flags
& DF_RD_CHAIN
) && (flags
& DF_RU_CHAIN
))
3597 basic_block bb
= df_regno_bb (df
, j
);
3600 fprintf (file
, " bb %d", bb
->index
);
3602 fprintf (file
, " bb ?");
3604 if (flags
& DF_REG_INFO
)
3606 fprintf (file
, " life %d", reg_info
[j
].lifetime
);
3609 if ((flags
& DF_REG_INFO
) || (flags
& DF_RD_CHAIN
))
3611 fprintf (file
, " defs ");
3612 if (flags
& DF_REG_INFO
)
3613 fprintf (file
, "%d ", reg_info
[j
].n_defs
);
3614 if (flags
& DF_RD_CHAIN
)
3615 df_chain_dump (reg_info
[j
].defs
, file
);
3618 if ((flags
& DF_REG_INFO
) || (flags
& DF_RU_CHAIN
))
3620 fprintf (file
, " uses ");
3621 if (flags
& DF_REG_INFO
)
3622 fprintf (file
, "%d ", reg_info
[j
].n_uses
);
3623 if (flags
& DF_RU_CHAIN
)
3624 df_chain_dump (reg_info
[j
].uses
, file
);
3627 fprintf (file
, "\n");
3631 fprintf (file
, "\n");
3636 df_insn_debug (struct df
*df
, rtx insn
, FILE *file
)
3641 uid
= INSN_UID (insn
);
3642 if (uid
>= df
->insn_size
)
3645 if (df
->insns
[uid
].defs
)
3646 bbi
= DF_REF_BBNO (df
->insns
[uid
].defs
->ref
);
3647 else if (df
->insns
[uid
].uses
)
3648 bbi
= DF_REF_BBNO (df
->insns
[uid
].uses
->ref
);
3652 fprintf (file
, "insn %d bb %d luid %d defs ",
3653 uid
, bbi
, DF_INSN_LUID (df
, insn
));
3654 df_chain_dump (df
->insns
[uid
].defs
, file
);
3655 fprintf (file
, " uses ");
3656 df_chain_dump (df
->insns
[uid
].uses
, file
);
3657 fprintf (file
, "\n");
3662 df_insn_debug_regno (struct df
*df
, rtx insn
, FILE *file
)
3667 uid
= INSN_UID (insn
);
3668 if (uid
>= df
->insn_size
)
3671 if (df
->insns
[uid
].defs
)
3672 bbi
= DF_REF_BBNO (df
->insns
[uid
].defs
->ref
);
3673 else if (df
->insns
[uid
].uses
)
3674 bbi
= DF_REF_BBNO (df
->insns
[uid
].uses
->ref
);
3678 fprintf (file
, "insn %d bb %d luid %d defs ",
3679 uid
, bbi
, DF_INSN_LUID (df
, insn
));
3680 df_chain_dump_regno (df
->insns
[uid
].defs
, file
);
3681 fprintf (file
, " uses ");
3682 df_chain_dump_regno (df
->insns
[uid
].uses
, file
);
3683 fprintf (file
, "\n");
3688 df_regno_debug (struct df
*df
, unsigned int regno
, FILE *file
)
3690 if (regno
>= df
->reg_size
)
3693 fprintf (file
, "reg %d life %d defs ",
3694 regno
, df
->regs
[regno
].lifetime
);
3695 df_chain_dump (df
->regs
[regno
].defs
, file
);
3696 fprintf (file
, " uses ");
3697 df_chain_dump (df
->regs
[regno
].uses
, file
);
3698 fprintf (file
, "\n");
3703 df_ref_debug (struct df
*df
, struct ref
*ref
, FILE *file
)
3705 fprintf (file
, "%c%d ",
3706 DF_REF_REG_DEF_P (ref
) ? 'd' : 'u',
3708 fprintf (file
, "reg %d bb %d luid %d insn %d chain ",
3711 DF_INSN_LUID (df
, DF_REF_INSN (ref
)),
3712 INSN_UID (DF_REF_INSN (ref
)));
3713 df_chain_dump (DF_REF_CHAIN (ref
), file
);
3714 fprintf (file
, "\n");
3717 /* Functions for debugging from GDB. */
3720 debug_df_insn (rtx insn
)
3722 df_insn_debug (ddf
, insn
, stderr
);
3728 debug_df_reg (rtx reg
)
3730 df_regno_debug (ddf
, REGNO (reg
), stderr
);
3735 debug_df_regno (unsigned int regno
)
3737 df_regno_debug (ddf
, regno
, stderr
);
3742 debug_df_ref (struct ref
*ref
)
3744 df_ref_debug (ddf
, ref
, stderr
);
3749 debug_df_defno (unsigned int defno
)
3751 df_ref_debug (ddf
, ddf
->defs
[defno
], stderr
);
3756 debug_df_useno (unsigned int defno
)
3758 df_ref_debug (ddf
, ddf
->uses
[defno
], stderr
);
3763 debug_df_chain (struct df_link
*link
)
3765 df_chain_dump (link
, stderr
);
3766 fputc ('\n', stderr
);
3770 /* Perform the set operation OP1 OP OP2, using set representation REPR, and
3771 storing the result in OP1. */
3774 dataflow_set_a_op_b (enum set_representation repr
,
3775 enum df_confluence_op op
,
3776 void *op1
, void *op2
)
3784 sbitmap_a_or_b (op1
, op1
, op2
);
3787 case DF_INTERSECTION
:
3788 sbitmap_a_and_b (op1
, op1
, op2
);
3800 bitmap_ior_into (op1
, op2
);
3803 case DF_INTERSECTION
:
3804 bitmap_and_into (op1
, op2
);
3818 dataflow_set_copy (enum set_representation repr
, void *dest
, void *src
)
3823 sbitmap_copy (dest
, src
);
3827 bitmap_copy (dest
, src
);
3835 /* Hybrid search algorithm from "Implementation Techniques for
3836 Efficient Data-Flow Analysis of Large Programs". */
3839 hybrid_search (basic_block bb
, struct dataflow
*dataflow
,
3840 sbitmap visited
, sbitmap pending
, sbitmap considered
)
3847 SET_BIT (visited
, bb
->index
);
3848 gcc_assert (TEST_BIT (pending
, bb
->index
));
3849 RESET_BIT (pending
, i
);
3851 #define HS(E_ANTI, E_ANTI_BB, E_ANTI_START_BB, IN_SET, \
3852 E, E_BB, E_START_BB, OUT_SET) \
3855 /* Calculate <conf_op> of predecessor_outs. */ \
3856 bitmap_zero (IN_SET[i]); \
3857 FOR_EACH_EDGE (e, ei, bb->E_ANTI) \
3859 if (e->E_ANTI_BB == E_ANTI_START_BB) \
3861 if (!TEST_BIT (considered, e->E_ANTI_BB->index)) \
3864 dataflow_set_a_op_b (dataflow->repr, dataflow->conf_op, \
3866 OUT_SET[e->E_ANTI_BB->index]); \
3869 (*dataflow->transfun)(i, &changed, \
3870 dataflow->in[i], dataflow->out[i], \
3871 dataflow->gen[i], dataflow->kill[i], \
3877 FOR_EACH_EDGE (e, ei, bb->E) \
3879 if (e->E_BB == E_START_BB || e->E_BB->index == i) \
3882 if (!TEST_BIT (considered, e->E_BB->index)) \
3885 SET_BIT (pending, e->E_BB->index); \
3888 FOR_EACH_EDGE (e, ei, bb->E) \
3890 if (e->E_BB == E_START_BB || e->E_BB->index == i) \
3893 if (!TEST_BIT (considered, e->E_BB->index)) \
3896 if (!TEST_BIT (visited, e->E_BB->index)) \
3897 hybrid_search (e->E_BB, dataflow, visited, pending, considered); \
3901 if (dataflow
->dir
== DF_FORWARD
)
3902 HS (preds
, src
, ENTRY_BLOCK_PTR
, dataflow
->in
,
3903 succs
, dest
, EXIT_BLOCK_PTR
, dataflow
->out
);
3905 HS (succs
, dest
, EXIT_BLOCK_PTR
, dataflow
->out
,
3906 preds
, src
, ENTRY_BLOCK_PTR
, dataflow
->in
);
3909 /* This function will perform iterative bitvector dataflow described by
3910 DATAFLOW, producing the in and out sets. Only the part of the cfg
3911 induced by blocks in DATAFLOW->order is taken into account.
3913 For forward problems, you probably want to pass in rc_order. */
3916 iterative_dataflow (struct dataflow
*dataflow
)
3919 sbitmap visited
, pending
, considered
;
3921 pending
= sbitmap_alloc (last_basic_block
);
3922 visited
= sbitmap_alloc (last_basic_block
);
3923 considered
= sbitmap_alloc (last_basic_block
);
3924 sbitmap_zero (pending
);
3925 sbitmap_zero (visited
);
3926 sbitmap_zero (considered
);
3928 for (i
= 0; i
< dataflow
->n_blocks
; i
++)
3930 idx
= dataflow
->order
[i
];
3931 SET_BIT (pending
, idx
);
3932 SET_BIT (considered
, idx
);
3933 if (dataflow
->dir
== DF_FORWARD
)
3934 dataflow_set_copy (dataflow
->repr
,
3935 dataflow
->out
[idx
], dataflow
->gen
[idx
]);
3937 dataflow_set_copy (dataflow
->repr
,
3938 dataflow
->in
[idx
], dataflow
->gen
[idx
]);
3943 for (i
= 0; i
< dataflow
->n_blocks
; i
++)
3945 idx
= dataflow
->order
[i
];
3947 if (TEST_BIT (pending
, idx
) && !TEST_BIT (visited
, idx
))
3948 hybrid_search (BASIC_BLOCK (idx
), dataflow
,
3949 visited
, pending
, considered
);
3952 if (sbitmap_first_set_bit (pending
) == -1)
3955 sbitmap_zero (visited
);
3958 sbitmap_free (pending
);
3959 sbitmap_free (visited
);
3960 sbitmap_free (considered
);