2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / df-scan.c
blob3a8e9ca7ec494c265e811f05a4acff15dc915a51
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
3 Originally contributed by Michael P. Hayes
4 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
5 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
6 and Kenneth Zadeck (zadeck@naturalbridge.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "alloc-pool.h"
35 #include "flags.h"
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "sbitmap.h"
39 #include "bitmap.h"
40 #include "dumpfile.h"
41 #include "tree.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "df.h"
45 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
48 typedef struct df_mw_hardreg *df_mw_hardreg_ptr;
51 #ifndef HAVE_epilogue
52 #define HAVE_epilogue 0
53 #endif
54 #ifndef HAVE_prologue
55 #define HAVE_prologue 0
56 #endif
57 #ifndef HAVE_sibcall_epilogue
58 #define HAVE_sibcall_epilogue 0
59 #endif
61 #ifndef EPILOGUE_USES
62 #define EPILOGUE_USES(REGNO) 0
63 #endif
65 /* The following two macros free the vecs that hold either the refs or
66 the mw refs. They are a little tricky because the vec has 0
67 elements is special and is not to be freed. */
68 #define df_scan_free_ref_vec(V) \
69 do { \
70 if (V && *V) \
71 free (V); \
72 } while (0)
74 #define df_scan_free_mws_vec(V) \
75 do { \
76 if (V && *V) \
77 free (V); \
78 } while (0)
80 /* The set of hard registers in eliminables[i].from. */
82 static HARD_REG_SET elim_reg_set;
84 /* Initialize ur_in and ur_out as if all hard registers were partially
85 available. */
87 struct df_collection_rec
89 vec<df_ref, va_stack> def_vec;
90 vec<df_ref, va_stack> use_vec;
91 vec<df_ref, va_stack> eq_use_vec;
92 vec<df_mw_hardreg_ptr, va_stack> mw_vec;
95 static df_ref df_null_ref_rec[1];
96 static struct df_mw_hardreg * df_null_mw_rec[1];
98 static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
99 rtx, rtx *,
100 basic_block, struct df_insn_info *,
101 enum df_ref_type, int ref_flags);
102 static void df_def_record_1 (struct df_collection_rec *, rtx *,
103 basic_block, struct df_insn_info *,
104 int ref_flags);
105 static void df_defs_record (struct df_collection_rec *, rtx,
106 basic_block, struct df_insn_info *,
107 int ref_flags);
108 static void df_uses_record (struct df_collection_rec *,
109 rtx *, enum df_ref_type,
110 basic_block, struct df_insn_info *,
111 int ref_flags);
113 static void df_install_ref_incremental (df_ref);
114 static df_ref df_ref_create_structure (enum df_ref_class,
115 struct df_collection_rec *, rtx, rtx *,
116 basic_block, struct df_insn_info *,
117 enum df_ref_type, int ref_flags);
118 static void df_insn_refs_collect (struct df_collection_rec*,
119 basic_block, struct df_insn_info *);
120 static void df_canonize_collection_rec (struct df_collection_rec *);
122 static void df_get_regular_block_artificial_uses (bitmap);
123 static void df_get_eh_block_artificial_uses (bitmap);
125 static void df_record_entry_block_defs (bitmap);
126 static void df_record_exit_block_uses (bitmap);
127 static void df_get_exit_block_use_set (bitmap);
128 static void df_get_entry_block_def_set (bitmap);
129 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
130 static void df_ref_chain_delete_du_chain (df_ref *);
131 static void df_ref_chain_delete (df_ref *);
133 static void df_refs_add_to_chains (struct df_collection_rec *,
134 basic_block, rtx);
136 static bool df_insn_refs_verify (struct df_collection_rec *, basic_block, rtx, bool);
137 static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
138 static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
139 static void df_install_ref (df_ref, struct df_reg_info *,
140 struct df_ref_info *, bool);
142 static int df_ref_compare (const void *, const void *);
143 static int df_mw_compare (const void *, const void *);
145 static void df_insn_info_delete (unsigned int);
147 /* Indexed by hardware reg number, is true if that register is ever
148 used in the current function.
150 In df-scan.c, this is set up to record the hard regs used
151 explicitly. Reload adds in the hard regs used for holding pseudo
152 regs. Final uses it to generate the code in the function prologue
153 and epilogue to save and restore registers as needed. */
155 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
157 /*----------------------------------------------------------------------------
158 SCANNING DATAFLOW PROBLEM
160 There are several ways in which scanning looks just like the other
161 dataflow problems. It shares the all the mechanisms for local info
162 as well as basic block info. Where it differs is when and how often
163 it gets run. It also has no need for the iterative solver.
164 ----------------------------------------------------------------------------*/
166 /* Problem data for the scanning dataflow function. */
167 struct df_scan_problem_data
169 alloc_pool ref_base_pool;
170 alloc_pool ref_artificial_pool;
171 alloc_pool ref_regular_pool;
172 alloc_pool insn_pool;
173 alloc_pool reg_pool;
174 alloc_pool mw_reg_pool;
175 bitmap_obstack reg_bitmaps;
176 bitmap_obstack insn_bitmaps;
179 typedef struct df_scan_bb_info *df_scan_bb_info_t;
182 /* Internal function to shut down the scanning problem. */
183 static void
184 df_scan_free_internal (void)
186 struct df_scan_problem_data *problem_data
187 = (struct df_scan_problem_data *) df_scan->problem_data;
188 unsigned int i;
189 basic_block bb;
191 /* The vectors that hold the refs are not pool allocated because
192 they come in many sizes. This makes them impossible to delete
193 all at once. */
194 for (i = 0; i < DF_INSN_SIZE(); i++)
196 struct df_insn_info *insn_info = DF_INSN_UID_GET(i);
197 /* Skip the insns that have no insn_info or have been
198 deleted. */
199 if (insn_info)
201 df_scan_free_ref_vec (insn_info->defs);
202 df_scan_free_ref_vec (insn_info->uses);
203 df_scan_free_ref_vec (insn_info->eq_uses);
204 df_scan_free_mws_vec (insn_info->mw_hardregs);
208 FOR_ALL_BB (bb)
210 unsigned int bb_index = bb->index;
211 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
212 if (bb_info)
214 df_scan_free_ref_vec (bb_info->artificial_defs);
215 df_scan_free_ref_vec (bb_info->artificial_uses);
219 free (df->def_info.refs);
220 free (df->def_info.begin);
221 free (df->def_info.count);
222 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
224 free (df->use_info.refs);
225 free (df->use_info.begin);
226 free (df->use_info.count);
227 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
229 free (df->def_regs);
230 df->def_regs = NULL;
231 free (df->use_regs);
232 df->use_regs = NULL;
233 free (df->eq_use_regs);
234 df->eq_use_regs = NULL;
235 df->regs_size = 0;
236 DF_REG_SIZE(df) = 0;
238 free (df->insns);
239 df->insns = NULL;
240 DF_INSN_SIZE () = 0;
242 free (df_scan->block_info);
243 df_scan->block_info = NULL;
244 df_scan->block_info_size = 0;
246 bitmap_clear (&df->hardware_regs_used);
247 bitmap_clear (&df->regular_block_artificial_uses);
248 bitmap_clear (&df->eh_block_artificial_uses);
249 BITMAP_FREE (df->entry_block_defs);
250 BITMAP_FREE (df->exit_block_uses);
251 bitmap_clear (&df->insns_to_delete);
252 bitmap_clear (&df->insns_to_rescan);
253 bitmap_clear (&df->insns_to_notes_rescan);
255 free_alloc_pool (problem_data->ref_base_pool);
256 free_alloc_pool (problem_data->ref_artificial_pool);
257 free_alloc_pool (problem_data->ref_regular_pool);
258 free_alloc_pool (problem_data->insn_pool);
259 free_alloc_pool (problem_data->reg_pool);
260 free_alloc_pool (problem_data->mw_reg_pool);
261 bitmap_obstack_release (&problem_data->reg_bitmaps);
262 bitmap_obstack_release (&problem_data->insn_bitmaps);
263 free (df_scan->problem_data);
267 /* Free basic block info. */
269 static void
270 df_scan_free_bb_info (basic_block bb, void *vbb_info)
272 struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
273 unsigned int bb_index = bb->index;
275 /* See if bb_info is initialized. */
276 if (bb_info->artificial_defs)
278 rtx insn;
279 FOR_BB_INSNS (bb, insn)
281 if (INSN_P (insn))
282 df_insn_info_delete (INSN_UID (insn));
285 if (bb_index < df_scan->block_info_size)
286 bb_info = df_scan_get_bb_info (bb_index);
288 /* Get rid of any artificial uses or defs. */
289 if (bb_info->artificial_defs)
291 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
292 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
293 df_ref_chain_delete (bb_info->artificial_defs);
294 df_ref_chain_delete (bb_info->artificial_uses);
295 bb_info->artificial_defs = NULL;
296 bb_info->artificial_uses = NULL;
302 /* Allocate the problem data for the scanning problem. This should be
303 called when the problem is created or when the entire function is to
304 be rescanned. */
305 void
306 df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
308 struct df_scan_problem_data *problem_data;
309 unsigned int insn_num = get_max_uid () + 1;
310 unsigned int block_size = 512;
311 basic_block bb;
313 /* Given the number of pools, this is really faster than tearing
314 everything apart. */
315 if (df_scan->problem_data)
316 df_scan_free_internal ();
318 problem_data = XNEW (struct df_scan_problem_data);
319 df_scan->problem_data = problem_data;
320 df_scan->computed = true;
322 problem_data->ref_base_pool
323 = create_alloc_pool ("df_scan ref base",
324 sizeof (struct df_base_ref), block_size);
325 problem_data->ref_artificial_pool
326 = create_alloc_pool ("df_scan ref artificial",
327 sizeof (struct df_artificial_ref), block_size);
328 problem_data->ref_regular_pool
329 = create_alloc_pool ("df_scan ref regular",
330 sizeof (struct df_regular_ref), block_size);
331 problem_data->insn_pool
332 = create_alloc_pool ("df_scan insn",
333 sizeof (struct df_insn_info), block_size);
334 problem_data->reg_pool
335 = create_alloc_pool ("df_scan reg",
336 sizeof (struct df_reg_info), block_size);
337 problem_data->mw_reg_pool
338 = create_alloc_pool ("df_scan mw_reg",
339 sizeof (struct df_mw_hardreg), block_size / 16);
341 bitmap_obstack_initialize (&problem_data->reg_bitmaps);
342 bitmap_obstack_initialize (&problem_data->insn_bitmaps);
344 insn_num += insn_num / 4;
345 df_grow_reg_info ();
347 df_grow_insn_info ();
348 df_grow_bb_info (df_scan);
350 FOR_ALL_BB (bb)
352 unsigned int bb_index = bb->index;
353 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
354 bb_info->artificial_defs = NULL;
355 bb_info->artificial_uses = NULL;
358 bitmap_initialize (&df->hardware_regs_used, &problem_data->reg_bitmaps);
359 bitmap_initialize (&df->regular_block_artificial_uses, &problem_data->reg_bitmaps);
360 bitmap_initialize (&df->eh_block_artificial_uses, &problem_data->reg_bitmaps);
361 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
362 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
363 bitmap_initialize (&df->insns_to_delete, &problem_data->insn_bitmaps);
364 bitmap_initialize (&df->insns_to_rescan, &problem_data->insn_bitmaps);
365 bitmap_initialize (&df->insns_to_notes_rescan, &problem_data->insn_bitmaps);
366 df_scan->optional_p = false;
370 /* Free all of the data associated with the scan problem. */
372 static void
373 df_scan_free (void)
375 if (df_scan->problem_data)
376 df_scan_free_internal ();
378 if (df->blocks_to_analyze)
380 BITMAP_FREE (df->blocks_to_analyze);
381 df->blocks_to_analyze = NULL;
384 free (df_scan);
387 /* Dump the preamble for DF_SCAN dump. */
388 static void
389 df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
391 int i;
392 int dcount = 0;
393 int ucount = 0;
394 int ecount = 0;
395 int icount = 0;
396 int ccount = 0;
397 basic_block bb;
398 rtx insn;
400 fprintf (file, ";; invalidated by call \t");
401 df_print_regset (file, regs_invalidated_by_call_regset);
402 fprintf (file, ";; hardware regs used \t");
403 df_print_regset (file, &df->hardware_regs_used);
404 fprintf (file, ";; regular block artificial uses \t");
405 df_print_regset (file, &df->regular_block_artificial_uses);
406 fprintf (file, ";; eh block artificial uses \t");
407 df_print_regset (file, &df->eh_block_artificial_uses);
408 fprintf (file, ";; entry block defs \t");
409 df_print_regset (file, df->entry_block_defs);
410 fprintf (file, ";; exit block uses \t");
411 df_print_regset (file, df->exit_block_uses);
412 fprintf (file, ";; regs ever live \t");
413 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
414 if (df_regs_ever_live_p (i))
415 fprintf (file, " %d[%s]", i, reg_names[i]);
416 fprintf (file, "\n;; ref usage \t");
418 for (i = 0; i < (int)df->regs_inited; i++)
419 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
421 const char * sep = "";
423 fprintf (file, "r%d={", i);
424 if (DF_REG_DEF_COUNT (i))
426 fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
427 sep = ",";
428 dcount += DF_REG_DEF_COUNT (i);
430 if (DF_REG_USE_COUNT (i))
432 fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
433 sep = ",";
434 ucount += DF_REG_USE_COUNT (i);
436 if (DF_REG_EQ_USE_COUNT (i))
438 fprintf (file, "%s%de", sep, DF_REG_EQ_USE_COUNT (i));
439 ecount += DF_REG_EQ_USE_COUNT (i);
441 fprintf (file, "} ");
444 FOR_EACH_BB (bb)
445 FOR_BB_INSNS (bb, insn)
446 if (INSN_P (insn))
448 if (CALL_P (insn))
449 ccount++;
450 else
451 icount++;
454 fprintf (file, "\n;; total ref usage %d{%dd,%du,%de}"
455 " in %d{%d regular + %d call} insns.\n",
456 dcount + ucount + ecount, dcount, ucount, ecount,
457 icount + ccount, icount, ccount);
460 /* Dump the bb_info for a given basic block. */
461 static void
462 df_scan_start_block (basic_block bb, FILE *file)
464 struct df_scan_bb_info *bb_info
465 = df_scan_get_bb_info (bb->index);
467 if (bb_info)
469 fprintf (file, ";; bb %d artificial_defs: ", bb->index);
470 df_refs_chain_dump (bb_info->artificial_defs, true, file);
471 fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
472 df_refs_chain_dump (bb_info->artificial_uses, true, file);
473 fprintf (file, "\n");
475 #if 0
477 rtx insn;
478 FOR_BB_INSNS (bb, insn)
479 if (INSN_P (insn))
480 df_insn_debug (insn, false, file);
482 #endif
485 static struct df_problem problem_SCAN =
487 DF_SCAN, /* Problem id. */
488 DF_NONE, /* Direction. */
489 df_scan_alloc, /* Allocate the problem specific data. */
490 NULL, /* Reset global information. */
491 df_scan_free_bb_info, /* Free basic block info. */
492 NULL, /* Local compute function. */
493 NULL, /* Init the solution specific data. */
494 NULL, /* Iterative solver. */
495 NULL, /* Confluence operator 0. */
496 NULL, /* Confluence operator n. */
497 NULL, /* Transfer function. */
498 NULL, /* Finalize function. */
499 df_scan_free, /* Free all of the problem information. */
500 NULL, /* Remove this problem from the stack of dataflow problems. */
501 df_scan_start_dump, /* Debugging. */
502 df_scan_start_block, /* Debugging start block. */
503 NULL, /* Debugging end block. */
504 NULL, /* Debugging start insn. */
505 NULL, /* Debugging end insn. */
506 NULL, /* Incremental solution verify start. */
507 NULL, /* Incremental solution verify end. */
508 NULL, /* Dependent problem. */
509 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
510 TV_DF_SCAN, /* Timing variable. */
511 false /* Reset blocks on dropping out of blocks_to_analyze. */
515 /* Create a new DATAFLOW instance and add it to an existing instance
516 of DF. The returned structure is what is used to get at the
517 solution. */
519 void
520 df_scan_add_problem (void)
522 df_add_problem (&problem_SCAN);
526 /*----------------------------------------------------------------------------
527 Storage Allocation Utilities
528 ----------------------------------------------------------------------------*/
531 /* First, grow the reg_info information. If the current size is less than
532 the number of pseudos, grow to 25% more than the number of
533 pseudos.
535 Second, assure that all of the slots up to max_reg_num have been
536 filled with reg_info structures. */
538 void
539 df_grow_reg_info (void)
541 unsigned int max_reg = max_reg_num ();
542 unsigned int new_size = max_reg;
543 struct df_scan_problem_data *problem_data
544 = (struct df_scan_problem_data *) df_scan->problem_data;
545 unsigned int i;
547 if (df->regs_size < new_size)
549 new_size += new_size / 4;
550 df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
551 df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
552 df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
553 new_size);
554 df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
555 df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
556 df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
557 df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
558 df->regs_size = new_size;
561 for (i = df->regs_inited; i < max_reg; i++)
563 struct df_reg_info *reg_info;
565 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
566 memset (reg_info, 0, sizeof (struct df_reg_info));
567 df->def_regs[i] = reg_info;
568 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
569 memset (reg_info, 0, sizeof (struct df_reg_info));
570 df->use_regs[i] = reg_info;
571 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
572 memset (reg_info, 0, sizeof (struct df_reg_info));
573 df->eq_use_regs[i] = reg_info;
574 df->def_info.begin[i] = 0;
575 df->def_info.count[i] = 0;
576 df->use_info.begin[i] = 0;
577 df->use_info.count[i] = 0;
580 df->regs_inited = max_reg;
584 /* Grow the ref information. */
586 static void
587 df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
589 if (ref_info->refs_size < new_size)
591 ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
592 memset (ref_info->refs + ref_info->refs_size, 0,
593 (new_size - ref_info->refs_size) *sizeof (df_ref));
594 ref_info->refs_size = new_size;
599 /* Check and grow the ref information if necessary. This routine
600 guarantees total_size + BITMAP_ADDEND amount of entries in refs
601 array. It updates ref_info->refs_size only and does not change
602 ref_info->total_size. */
604 static void
605 df_check_and_grow_ref_info (struct df_ref_info *ref_info,
606 unsigned bitmap_addend)
608 if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
610 int new_size = ref_info->total_size + bitmap_addend;
611 new_size += ref_info->total_size / 4;
612 df_grow_ref_info (ref_info, new_size);
617 /* Grow the ref information. If the current size is less than the
618 number of instructions, grow to 25% more than the number of
619 instructions. */
621 void
622 df_grow_insn_info (void)
624 unsigned int new_size = get_max_uid () + 1;
625 if (DF_INSN_SIZE () < new_size)
627 new_size += new_size / 4;
628 df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
629 memset (df->insns + df->insns_size, 0,
630 (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
631 DF_INSN_SIZE () = new_size;
638 /*----------------------------------------------------------------------------
639 PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
640 ----------------------------------------------------------------------------*/
642 /* Rescan all of the block_to_analyze or all of the blocks in the
643 function if df_set_blocks if blocks_to_analyze is NULL; */
645 void
646 df_scan_blocks (void)
648 basic_block bb;
650 df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
651 df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
653 df_get_regular_block_artificial_uses (&df->regular_block_artificial_uses);
654 df_get_eh_block_artificial_uses (&df->eh_block_artificial_uses);
656 bitmap_ior_into (&df->eh_block_artificial_uses,
657 &df->regular_block_artificial_uses);
659 /* ENTRY and EXIT blocks have special defs/uses. */
660 df_get_entry_block_def_set (df->entry_block_defs);
661 df_record_entry_block_defs (df->entry_block_defs);
662 df_get_exit_block_use_set (df->exit_block_uses);
663 df_record_exit_block_uses (df->exit_block_uses);
664 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
665 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
667 /* Regular blocks */
668 FOR_EACH_BB (bb)
670 unsigned int bb_index = bb->index;
671 df_bb_refs_record (bb_index, true);
675 /* Create new refs under address LOC within INSN. This function is
676 only used externally. REF_FLAGS must be either 0 or DF_REF_IN_NOTE,
677 depending on whether LOC is inside PATTERN (INSN) or a note. */
679 void
680 df_uses_create (rtx *loc, rtx insn, int ref_flags)
682 gcc_assert (!(ref_flags & ~DF_REF_IN_NOTE));
683 df_uses_record (NULL, loc, DF_REF_REG_USE,
684 BLOCK_FOR_INSN (insn),
685 DF_INSN_INFO_GET (insn),
686 ref_flags);
689 /* Create a new ref of type DF_REF_TYPE for register REG at address
690 LOC within INSN of BB. This function is only used externally. */
692 df_ref
693 df_ref_create (rtx reg, rtx *loc, rtx insn,
694 basic_block bb,
695 enum df_ref_type ref_type,
696 int ref_flags)
698 enum df_ref_class cl;
700 df_grow_reg_info ();
702 /* You cannot hack artificial refs. */
703 gcc_assert (insn);
705 if (loc)
706 cl = DF_REF_REGULAR;
707 else
708 cl = DF_REF_BASE;
710 return df_ref_create_structure (cl, NULL, reg, loc, bb,
711 DF_INSN_INFO_GET (insn),
712 ref_type, ref_flags);
715 static void
716 df_install_ref_incremental (df_ref ref)
718 struct df_reg_info **reg_info;
719 struct df_ref_info *ref_info;
720 df_ref *ref_rec;
721 df_ref **ref_rec_ptr;
722 unsigned int count = 0;
723 bool add_to_table;
725 rtx insn = DF_REF_INSN (ref);
726 basic_block bb = BLOCK_FOR_INSN (insn);
728 if (DF_REF_REG_DEF_P (ref))
730 reg_info = df->def_regs;
731 ref_info = &df->def_info;
732 ref_rec_ptr = &DF_INSN_DEFS (insn);
733 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
735 else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
737 reg_info = df->eq_use_regs;
738 ref_info = &df->use_info;
739 ref_rec_ptr = &DF_INSN_EQ_USES (insn);
740 switch (ref_info->ref_order)
742 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
743 case DF_REF_ORDER_BY_REG_WITH_NOTES:
744 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
745 add_to_table = true;
746 break;
747 default:
748 add_to_table = false;
749 break;
752 else
754 reg_info = df->use_regs;
755 ref_info = &df->use_info;
756 ref_rec_ptr = &DF_INSN_USES (insn);
757 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
760 /* Do not add if ref is not in the right blocks. */
761 if (add_to_table && df->analyze_subset)
762 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
764 df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
766 if (add_to_table)
767 switch (ref_info->ref_order)
769 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
770 case DF_REF_ORDER_BY_REG_WITH_NOTES:
771 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
772 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
773 break;
774 default:
775 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
776 break;
779 ref_rec = *ref_rec_ptr;
780 while (*ref_rec)
782 count++;
783 ref_rec++;
786 ref_rec = *ref_rec_ptr;
787 if (count)
789 ref_rec = XRESIZEVEC (df_ref, ref_rec, count+2);
790 *ref_rec_ptr = ref_rec;
791 ref_rec[count] = ref;
792 ref_rec[count+1] = NULL;
793 qsort (ref_rec, count + 1, sizeof (df_ref), df_ref_compare);
795 else
797 df_ref *ref_rec = XNEWVEC (df_ref, 2);
798 ref_rec[0] = ref;
799 ref_rec[1] = NULL;
800 *ref_rec_ptr = ref_rec;
803 #if 0
804 if (dump_file)
806 fprintf (dump_file, "adding ref ");
807 df_ref_debug (ref, dump_file);
809 #endif
810 /* By adding the ref directly, df_insn_rescan my not find any
811 differences even though the block will have changed. So we need
812 to mark the block dirty ourselves. */
813 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
814 df_set_bb_dirty (bb);
819 /*----------------------------------------------------------------------------
820 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
821 ----------------------------------------------------------------------------*/
823 static void
824 df_free_ref (df_ref ref)
826 struct df_scan_problem_data *problem_data
827 = (struct df_scan_problem_data *) df_scan->problem_data;
829 switch (DF_REF_CLASS (ref))
831 case DF_REF_BASE:
832 pool_free (problem_data->ref_base_pool, ref);
833 break;
835 case DF_REF_ARTIFICIAL:
836 pool_free (problem_data->ref_artificial_pool, ref);
837 break;
839 case DF_REF_REGULAR:
840 pool_free (problem_data->ref_regular_pool, ref);
841 break;
846 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
847 Also delete the def-use or use-def chain if it exists. */
849 static void
850 df_reg_chain_unlink (df_ref ref)
852 df_ref next = DF_REF_NEXT_REG (ref);
853 df_ref prev = DF_REF_PREV_REG (ref);
854 int id = DF_REF_ID (ref);
855 struct df_reg_info *reg_info;
856 df_ref *refs = NULL;
858 if (DF_REF_REG_DEF_P (ref))
860 int regno = DF_REF_REGNO (ref);
861 reg_info = DF_REG_DEF_GET (regno);
862 refs = df->def_info.refs;
864 else
866 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
868 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
869 switch (df->use_info.ref_order)
871 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
872 case DF_REF_ORDER_BY_REG_WITH_NOTES:
873 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
874 refs = df->use_info.refs;
875 break;
876 default:
877 break;
880 else
882 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
883 refs = df->use_info.refs;
887 if (refs)
889 if (df->analyze_subset)
891 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
892 refs[id] = NULL;
894 else
895 refs[id] = NULL;
898 /* Delete any def-use or use-def chains that start here. It is
899 possible that there is trash in this field. This happens for
900 insns that have been deleted when rescanning has been deferred
901 and the chain problem has also been deleted. The chain tear down
902 code skips deleted insns. */
903 if (df_chain && DF_REF_CHAIN (ref))
904 df_chain_unlink (ref);
906 reg_info->n_refs--;
907 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
909 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
910 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
913 /* Unlink from the reg chain. If there is no prev, this is the
914 first of the list. If not, just join the next and prev. */
915 if (prev)
916 DF_REF_NEXT_REG (prev) = next;
917 else
919 gcc_assert (reg_info->reg_chain == ref);
920 reg_info->reg_chain = next;
922 if (next)
923 DF_REF_PREV_REG (next) = prev;
925 df_free_ref (ref);
929 /* Remove REF from VEC. */
931 static void
932 df_ref_compress_rec (df_ref **vec_ptr, df_ref ref)
934 df_ref *vec = *vec_ptr;
936 if (vec[1])
938 while (*vec && *vec != ref)
939 vec++;
941 while (*vec)
943 *vec = *(vec+1);
944 vec++;
947 else
949 free (vec);
950 *vec_ptr = df_null_ref_rec;
955 /* Unlink REF from all def-use/use-def chains, etc. */
957 void
958 df_ref_remove (df_ref ref)
960 #if 0
961 if (dump_file)
963 fprintf (dump_file, "removing ref ");
964 df_ref_debug (ref, dump_file);
966 #endif
968 if (DF_REF_REG_DEF_P (ref))
970 if (DF_REF_IS_ARTIFICIAL (ref))
972 struct df_scan_bb_info *bb_info
973 = df_scan_get_bb_info (DF_REF_BBNO (ref));
974 df_ref_compress_rec (&bb_info->artificial_defs, ref);
976 else
978 unsigned int uid = DF_REF_INSN_UID (ref);
979 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
980 df_ref_compress_rec (&insn_rec->defs, ref);
983 else
985 if (DF_REF_IS_ARTIFICIAL (ref))
987 struct df_scan_bb_info *bb_info
988 = df_scan_get_bb_info (DF_REF_BBNO (ref));
989 df_ref_compress_rec (&bb_info->artificial_uses, ref);
991 else
993 unsigned int uid = DF_REF_INSN_UID (ref);
994 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
996 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
997 df_ref_compress_rec (&insn_rec->eq_uses, ref);
998 else
999 df_ref_compress_rec (&insn_rec->uses, ref);
1003 /* By deleting the ref directly, df_insn_rescan my not find any
1004 differences even though the block will have changed. So we need
1005 to mark the block dirty ourselves. */
1006 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
1007 df_set_bb_dirty (DF_REF_BB (ref));
1008 df_reg_chain_unlink (ref);
1012 /* Create the insn record for INSN. If there was one there, zero it
1013 out. */
1015 struct df_insn_info *
1016 df_insn_create_insn_record (rtx insn)
1018 struct df_scan_problem_data *problem_data
1019 = (struct df_scan_problem_data *) df_scan->problem_data;
1020 struct df_insn_info *insn_rec;
1022 df_grow_insn_info ();
1023 insn_rec = DF_INSN_INFO_GET (insn);
1024 if (!insn_rec)
1026 insn_rec = (struct df_insn_info *) pool_alloc (problem_data->insn_pool);
1027 DF_INSN_INFO_SET (insn, insn_rec);
1029 memset (insn_rec, 0, sizeof (struct df_insn_info));
1030 insn_rec->insn = insn;
1031 return insn_rec;
1035 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
1037 static void
1038 df_ref_chain_delete_du_chain (df_ref *ref_rec)
1040 while (*ref_rec)
1042 df_ref ref = *ref_rec;
1043 /* CHAIN is allocated by DF_CHAIN. So make sure to
1044 pass df_scan instance for the problem. */
1045 if (DF_REF_CHAIN (ref))
1046 df_chain_unlink (ref);
1047 ref_rec++;
1052 /* Delete all refs in the ref chain. */
1054 static void
1055 df_ref_chain_delete (df_ref *ref_rec)
1057 df_ref *start = ref_rec;
1058 while (*ref_rec)
1060 df_reg_chain_unlink (*ref_rec);
1061 ref_rec++;
1064 /* If the list is empty, it has a special shared element that is not
1065 to be deleted. */
1066 if (*start)
1067 free (start);
1071 /* Delete the hardreg chain. */
1073 static void
1074 df_mw_hardreg_chain_delete (struct df_mw_hardreg **hardregs)
1076 struct df_scan_problem_data *problem_data;
1078 if (!hardregs)
1079 return;
1081 problem_data = (struct df_scan_problem_data *) df_scan->problem_data;
1083 while (*hardregs)
1085 pool_free (problem_data->mw_reg_pool, *hardregs);
1086 hardregs++;
1091 /* Delete all of the refs information from the insn with UID.
1092 Internal helper for df_insn_delete, df_insn_rescan, and other
1093 df-scan routines that don't have to work in deferred mode
1094 and do not have to mark basic blocks for re-processing. */
1096 static void
1097 df_insn_info_delete (unsigned int uid)
1099 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1101 bitmap_clear_bit (&df->insns_to_delete, uid);
1102 bitmap_clear_bit (&df->insns_to_rescan, uid);
1103 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1104 if (insn_info)
1106 struct df_scan_problem_data *problem_data
1107 = (struct df_scan_problem_data *) df_scan->problem_data;
1109 /* In general, notes do not have the insn_info fields
1110 initialized. However, combine deletes insns by changing them
1111 to notes. How clever. So we cannot just check if it is a
1112 valid insn before short circuiting this code, we need to see
1113 if we actually initialized it. */
1114 if (insn_info->defs)
1116 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1118 if (df_chain)
1120 df_ref_chain_delete_du_chain (insn_info->defs);
1121 df_ref_chain_delete_du_chain (insn_info->uses);
1122 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1125 df_ref_chain_delete (insn_info->defs);
1126 df_ref_chain_delete (insn_info->uses);
1127 df_ref_chain_delete (insn_info->eq_uses);
1128 df_scan_free_mws_vec (insn_info->mw_hardregs);
1130 pool_free (problem_data->insn_pool, insn_info);
1131 DF_INSN_UID_SET (uid, NULL);
1135 /* Delete all of the refs information from INSN, either right now
1136 or marked for later in deferred mode. */
1138 void
1139 df_insn_delete (rtx insn)
1141 unsigned int uid;
1142 basic_block bb;
1144 gcc_checking_assert (INSN_P (insn));
1146 if (!df)
1147 return;
1149 uid = INSN_UID (insn);
1150 bb = BLOCK_FOR_INSN (insn);
1152 /* ??? bb can be NULL after pass_free_cfg. At that point, DF should
1153 not exist anymore (as mentioned in df-core.c: "The only requirement
1154 [for DF] is that there be a correct control flow graph." Clearly
1155 that isn't the case after pass_free_cfg. But DF is freed much later
1156 because some back-ends want to use DF info even though the CFG is
1157 already gone. It's not clear to me whether that is safe, actually.
1158 In any case, we expect BB to be non-NULL at least up to register
1159 allocation, so disallow a non-NULL BB up to there. Not perfect
1160 but better than nothing... */
1161 gcc_checking_assert (bb != NULL || reload_completed);
1163 df_grow_bb_info (df_scan);
1164 df_grow_reg_info ();
1166 /* The block must be marked as dirty now, rather than later as in
1167 df_insn_rescan and df_notes_rescan because it may not be there at
1168 rescanning time and the mark would blow up.
1169 DEBUG_INSNs do not make a block's data flow solution dirty (at
1170 worst the LUIDs are no longer contiguous). */
1171 if (bb != NULL && NONDEBUG_INSN_P (insn))
1172 df_set_bb_dirty (bb);
1174 /* The client has deferred rescanning. */
1175 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1177 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1178 if (insn_info)
1180 bitmap_clear_bit (&df->insns_to_rescan, uid);
1181 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1182 bitmap_set_bit (&df->insns_to_delete, uid);
1184 if (dump_file)
1185 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1186 return;
1189 if (dump_file)
1190 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1192 df_insn_info_delete (uid);
1196 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1198 static void
1199 df_free_collection_rec (struct df_collection_rec *collection_rec)
1201 unsigned int ix;
1202 struct df_scan_problem_data *problem_data
1203 = (struct df_scan_problem_data *) df_scan->problem_data;
1204 df_ref ref;
1205 struct df_mw_hardreg *mw;
1207 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
1208 df_free_ref (ref);
1209 FOR_EACH_VEC_ELT (collection_rec->use_vec, ix, ref)
1210 df_free_ref (ref);
1211 FOR_EACH_VEC_ELT (collection_rec->eq_use_vec, ix, ref)
1212 df_free_ref (ref);
1213 FOR_EACH_VEC_ELT (collection_rec->mw_vec, ix, mw)
1214 pool_free (problem_data->mw_reg_pool, mw);
1216 collection_rec->def_vec.release ();
1217 collection_rec->use_vec.release ();
1218 collection_rec->eq_use_vec.release ();
1219 collection_rec->mw_vec.release ();
1222 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1224 bool
1225 df_insn_rescan (rtx insn)
1227 unsigned int uid = INSN_UID (insn);
1228 struct df_insn_info *insn_info = NULL;
1229 basic_block bb = BLOCK_FOR_INSN (insn);
1230 struct df_collection_rec collection_rec;
1232 if ((!df) || (!INSN_P (insn)))
1233 return false;
1235 if (!bb)
1237 if (dump_file)
1238 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1239 return false;
1242 /* The client has disabled rescanning and plans to do it itself. */
1243 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1244 return false;
1246 df_grow_bb_info (df_scan);
1247 df_grow_reg_info ();
1249 insn_info = DF_INSN_UID_SAFE_GET (uid);
1251 /* The client has deferred rescanning. */
1252 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1254 if (!insn_info)
1256 insn_info = df_insn_create_insn_record (insn);
1257 insn_info->defs = df_null_ref_rec;
1258 insn_info->uses = df_null_ref_rec;
1259 insn_info->eq_uses = df_null_ref_rec;
1260 insn_info->mw_hardregs = df_null_mw_rec;
1262 if (dump_file)
1263 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1265 bitmap_clear_bit (&df->insns_to_delete, uid);
1266 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1267 bitmap_set_bit (&df->insns_to_rescan, INSN_UID (insn));
1268 return false;
1271 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
1272 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
1273 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
1274 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
1276 bitmap_clear_bit (&df->insns_to_delete, uid);
1277 bitmap_clear_bit (&df->insns_to_rescan, uid);
1278 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1279 if (insn_info)
1281 int luid;
1282 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1283 /* If there's no change, return false. */
1284 if (the_same)
1286 df_free_collection_rec (&collection_rec);
1287 if (dump_file)
1288 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1289 return false;
1291 if (dump_file)
1292 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1294 /* There's change - we need to delete the existing info.
1295 Since the insn isn't moved, we can salvage its LUID. */
1296 luid = DF_INSN_LUID (insn);
1297 df_insn_info_delete (uid);
1298 df_insn_create_insn_record (insn);
1299 DF_INSN_LUID (insn) = luid;
1301 else
1303 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1304 df_insn_refs_collect (&collection_rec, bb, insn_info);
1305 if (dump_file)
1306 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1309 df_refs_add_to_chains (&collection_rec, bb, insn);
1310 if (!DEBUG_INSN_P (insn))
1311 df_set_bb_dirty (bb);
1313 collection_rec.def_vec.release ();
1314 collection_rec.use_vec.release ();
1315 collection_rec.eq_use_vec.release ();
1316 collection_rec.mw_vec.release ();
1318 return true;
1321 /* Same as df_insn_rescan, but don't mark the basic block as
1322 dirty. */
1324 bool
1325 df_insn_rescan_debug_internal (rtx insn)
1327 unsigned int uid = INSN_UID (insn);
1328 struct df_insn_info *insn_info;
1330 gcc_assert (DEBUG_INSN_P (insn)
1331 && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1333 if (!df)
1334 return false;
1336 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1337 if (!insn_info)
1338 return false;
1340 if (dump_file)
1341 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1343 bitmap_clear_bit (&df->insns_to_delete, uid);
1344 bitmap_clear_bit (&df->insns_to_rescan, uid);
1345 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1347 if (!insn_info->defs)
1348 return false;
1350 if (insn_info->defs == df_null_ref_rec
1351 && insn_info->uses == df_null_ref_rec
1352 && insn_info->eq_uses == df_null_ref_rec
1353 && insn_info->mw_hardregs == df_null_mw_rec)
1354 return false;
1356 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1358 if (df_chain)
1360 df_ref_chain_delete_du_chain (insn_info->defs);
1361 df_ref_chain_delete_du_chain (insn_info->uses);
1362 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1365 df_ref_chain_delete (insn_info->defs);
1366 df_ref_chain_delete (insn_info->uses);
1367 df_ref_chain_delete (insn_info->eq_uses);
1368 df_scan_free_mws_vec (insn_info->mw_hardregs);
1370 insn_info->defs = df_null_ref_rec;
1371 insn_info->uses = df_null_ref_rec;
1372 insn_info->eq_uses = df_null_ref_rec;
1373 insn_info->mw_hardregs = df_null_mw_rec;
1375 return true;
1379 /* Rescan all of the insns in the function. Note that the artificial
1380 uses and defs are not touched. This function will destroy def-use
1381 or use-def chains. */
1383 void
1384 df_insn_rescan_all (void)
1386 bool no_insn_rescan = false;
1387 bool defer_insn_rescan = false;
1388 basic_block bb;
1389 bitmap_iterator bi;
1390 unsigned int uid;
1391 bitmap_head tmp;
1393 bitmap_initialize (&tmp, &df_bitmap_obstack);
1395 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1397 df_clear_flags (DF_NO_INSN_RESCAN);
1398 no_insn_rescan = true;
1401 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1403 df_clear_flags (DF_DEFER_INSN_RESCAN);
1404 defer_insn_rescan = true;
1407 bitmap_copy (&tmp, &df->insns_to_delete);
1408 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1410 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1411 if (insn_info)
1412 df_insn_info_delete (uid);
1415 bitmap_clear (&tmp);
1416 bitmap_clear (&df->insns_to_delete);
1417 bitmap_clear (&df->insns_to_rescan);
1418 bitmap_clear (&df->insns_to_notes_rescan);
1420 FOR_EACH_BB (bb)
1422 rtx insn;
1423 FOR_BB_INSNS (bb, insn)
1425 df_insn_rescan (insn);
1429 if (no_insn_rescan)
1430 df_set_flags (DF_NO_INSN_RESCAN);
1431 if (defer_insn_rescan)
1432 df_set_flags (DF_DEFER_INSN_RESCAN);
1436 /* Process all of the deferred rescans or deletions. */
1438 void
1439 df_process_deferred_rescans (void)
1441 bool no_insn_rescan = false;
1442 bool defer_insn_rescan = false;
1443 bitmap_iterator bi;
1444 unsigned int uid;
1445 bitmap_head tmp;
1447 bitmap_initialize (&tmp, &df_bitmap_obstack);
1449 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1451 df_clear_flags (DF_NO_INSN_RESCAN);
1452 no_insn_rescan = true;
1455 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1457 df_clear_flags (DF_DEFER_INSN_RESCAN);
1458 defer_insn_rescan = true;
1461 if (dump_file)
1462 fprintf (dump_file, "starting the processing of deferred insns\n");
1464 bitmap_copy (&tmp, &df->insns_to_delete);
1465 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1467 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1468 if (insn_info)
1469 df_insn_info_delete (uid);
1472 bitmap_copy (&tmp, &df->insns_to_rescan);
1473 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1475 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1476 if (insn_info)
1477 df_insn_rescan (insn_info->insn);
1480 bitmap_copy (&tmp, &df->insns_to_notes_rescan);
1481 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1483 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1484 if (insn_info)
1485 df_notes_rescan (insn_info->insn);
1488 if (dump_file)
1489 fprintf (dump_file, "ending the processing of deferred insns\n");
1491 bitmap_clear (&tmp);
1492 bitmap_clear (&df->insns_to_delete);
1493 bitmap_clear (&df->insns_to_rescan);
1494 bitmap_clear (&df->insns_to_notes_rescan);
1496 if (no_insn_rescan)
1497 df_set_flags (DF_NO_INSN_RESCAN);
1498 if (defer_insn_rescan)
1499 df_set_flags (DF_DEFER_INSN_RESCAN);
1501 /* If someone changed regs_ever_live during this pass, fix up the
1502 entry and exit blocks. */
1503 if (df->redo_entry_and_exit)
1505 df_update_entry_exit_and_calls ();
1506 df->redo_entry_and_exit = false;
1511 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1512 the uses if INCLUDE_USES. Include the eq_uses if
1513 INCLUDE_EQ_USES. */
1515 static unsigned int
1516 df_count_refs (bool include_defs, bool include_uses,
1517 bool include_eq_uses)
1519 unsigned int regno;
1520 int size = 0;
1521 unsigned int m = df->regs_inited;
1523 for (regno = 0; regno < m; regno++)
1525 if (include_defs)
1526 size += DF_REG_DEF_COUNT (regno);
1527 if (include_uses)
1528 size += DF_REG_USE_COUNT (regno);
1529 if (include_eq_uses)
1530 size += DF_REG_EQ_USE_COUNT (regno);
1532 return size;
1536 /* Take build ref table for either the uses or defs from the reg-use
1537 or reg-def chains. This version processes the refs in reg order
1538 which is likely to be best if processing the whole function. */
1540 static void
1541 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1542 bool include_defs,
1543 bool include_uses,
1544 bool include_eq_uses)
1546 unsigned int m = df->regs_inited;
1547 unsigned int regno;
1548 unsigned int offset = 0;
1549 unsigned int start;
1551 if (df->changeable_flags & DF_NO_HARD_REGS)
1553 start = FIRST_PSEUDO_REGISTER;
1554 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1555 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1557 else
1558 start = 0;
1560 ref_info->total_size
1561 = df_count_refs (include_defs, include_uses, include_eq_uses);
1563 df_check_and_grow_ref_info (ref_info, 1);
1565 for (regno = start; regno < m; regno++)
1567 int count = 0;
1568 ref_info->begin[regno] = offset;
1569 if (include_defs)
1571 df_ref ref = DF_REG_DEF_CHAIN (regno);
1572 while (ref)
1574 ref_info->refs[offset] = ref;
1575 DF_REF_ID (ref) = offset++;
1576 count++;
1577 ref = DF_REF_NEXT_REG (ref);
1578 gcc_checking_assert (offset < ref_info->refs_size);
1581 if (include_uses)
1583 df_ref ref = DF_REG_USE_CHAIN (regno);
1584 while (ref)
1586 ref_info->refs[offset] = ref;
1587 DF_REF_ID (ref) = offset++;
1588 count++;
1589 ref = DF_REF_NEXT_REG (ref);
1590 gcc_checking_assert (offset < ref_info->refs_size);
1593 if (include_eq_uses)
1595 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1596 while (ref)
1598 ref_info->refs[offset] = ref;
1599 DF_REF_ID (ref) = offset++;
1600 count++;
1601 ref = DF_REF_NEXT_REG (ref);
1602 gcc_checking_assert (offset < ref_info->refs_size);
1605 ref_info->count[regno] = count;
1608 /* The bitmap size is not decremented when refs are deleted. So
1609 reset it now that we have squished out all of the empty
1610 slots. */
1611 ref_info->table_size = offset;
1615 /* Take build ref table for either the uses or defs from the reg-use
1616 or reg-def chains. This version processes the refs in insn order
1617 which is likely to be best if processing some segment of the
1618 function. */
1620 static void
1621 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1622 bool include_defs,
1623 bool include_uses,
1624 bool include_eq_uses)
1626 bitmap_iterator bi;
1627 unsigned int bb_index;
1628 unsigned int m = df->regs_inited;
1629 unsigned int offset = 0;
1630 unsigned int r;
1631 unsigned int start
1632 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1634 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1635 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1637 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1638 df_check_and_grow_ref_info (ref_info, 1);
1640 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1642 basic_block bb = BASIC_BLOCK (bb_index);
1643 rtx insn;
1644 df_ref *ref_rec;
1646 if (include_defs)
1647 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1649 unsigned int regno = DF_REF_REGNO (*ref_rec);
1650 ref_info->count[regno]++;
1652 if (include_uses)
1653 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1655 unsigned int regno = DF_REF_REGNO (*ref_rec);
1656 ref_info->count[regno]++;
1659 FOR_BB_INSNS (bb, insn)
1661 if (INSN_P (insn))
1663 unsigned int uid = INSN_UID (insn);
1665 if (include_defs)
1666 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1668 unsigned int regno = DF_REF_REGNO (*ref_rec);
1669 ref_info->count[regno]++;
1671 if (include_uses)
1672 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1674 unsigned int regno = DF_REF_REGNO (*ref_rec);
1675 ref_info->count[regno]++;
1677 if (include_eq_uses)
1678 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1680 unsigned int regno = DF_REF_REGNO (*ref_rec);
1681 ref_info->count[regno]++;
1687 for (r = start; r < m; r++)
1689 ref_info->begin[r] = offset;
1690 offset += ref_info->count[r];
1691 ref_info->count[r] = 0;
1694 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1696 basic_block bb = BASIC_BLOCK (bb_index);
1697 rtx insn;
1698 df_ref *ref_rec;
1700 if (include_defs)
1701 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1703 df_ref ref = *ref_rec;
1704 unsigned int regno = DF_REF_REGNO (ref);
1705 if (regno >= start)
1707 unsigned int id
1708 = ref_info->begin[regno] + ref_info->count[regno]++;
1709 DF_REF_ID (ref) = id;
1710 ref_info->refs[id] = ref;
1713 if (include_uses)
1714 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1716 df_ref ref = *ref_rec;
1717 unsigned int regno = DF_REF_REGNO (ref);
1718 if (regno >= start)
1720 unsigned int id
1721 = ref_info->begin[regno] + ref_info->count[regno]++;
1722 DF_REF_ID (ref) = id;
1723 ref_info->refs[id] = ref;
1727 FOR_BB_INSNS (bb, insn)
1729 if (INSN_P (insn))
1731 unsigned int uid = INSN_UID (insn);
1733 if (include_defs)
1734 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1736 df_ref ref = *ref_rec;
1737 unsigned int regno = DF_REF_REGNO (ref);
1738 if (regno >= start)
1740 unsigned int id
1741 = ref_info->begin[regno] + ref_info->count[regno]++;
1742 DF_REF_ID (ref) = id;
1743 ref_info->refs[id] = ref;
1746 if (include_uses)
1747 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1749 df_ref ref = *ref_rec;
1750 unsigned int regno = DF_REF_REGNO (ref);
1751 if (regno >= start)
1753 unsigned int id
1754 = ref_info->begin[regno] + ref_info->count[regno]++;
1755 DF_REF_ID (ref) = id;
1756 ref_info->refs[id] = ref;
1759 if (include_eq_uses)
1760 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1762 df_ref ref = *ref_rec;
1763 unsigned int regno = DF_REF_REGNO (ref);
1764 if (regno >= start)
1766 unsigned int id
1767 = ref_info->begin[regno] + ref_info->count[regno]++;
1768 DF_REF_ID (ref) = id;
1769 ref_info->refs[id] = ref;
1776 /* The bitmap size is not decremented when refs are deleted. So
1777 reset it now that we have squished out all of the empty
1778 slots. */
1780 ref_info->table_size = offset;
1783 /* Take build ref table for either the uses or defs from the reg-use
1784 or reg-def chains. */
1786 static void
1787 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1788 bool include_defs,
1789 bool include_uses,
1790 bool include_eq_uses)
1792 if (df->analyze_subset)
1793 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1794 include_uses, include_eq_uses);
1795 else
1796 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1797 include_uses, include_eq_uses);
1801 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1802 static unsigned int
1803 df_add_refs_to_table (unsigned int offset,
1804 struct df_ref_info *ref_info,
1805 df_ref *ref_vec)
1807 while (*ref_vec)
1809 df_ref ref = *ref_vec;
1810 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1811 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1813 ref_info->refs[offset] = ref;
1814 DF_REF_ID (*ref_vec) = offset++;
1816 ref_vec++;
1818 return offset;
1822 /* Count the number of refs in all of the insns of BB. Include the
1823 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1824 eq_uses if INCLUDE_EQ_USES. */
1826 static unsigned int
1827 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1828 struct df_ref_info *ref_info,
1829 bool include_defs, bool include_uses,
1830 bool include_eq_uses)
1832 rtx insn;
1834 if (include_defs)
1835 offset = df_add_refs_to_table (offset, ref_info,
1836 df_get_artificial_defs (bb->index));
1837 if (include_uses)
1838 offset = df_add_refs_to_table (offset, ref_info,
1839 df_get_artificial_uses (bb->index));
1841 FOR_BB_INSNS (bb, insn)
1842 if (INSN_P (insn))
1844 unsigned int uid = INSN_UID (insn);
1845 if (include_defs)
1846 offset = df_add_refs_to_table (offset, ref_info,
1847 DF_INSN_UID_DEFS (uid));
1848 if (include_uses)
1849 offset = df_add_refs_to_table (offset, ref_info,
1850 DF_INSN_UID_USES (uid));
1851 if (include_eq_uses)
1852 offset = df_add_refs_to_table (offset, ref_info,
1853 DF_INSN_UID_EQ_USES (uid));
1855 return offset;
1859 /* Organize the refs by insn into the table in REF_INFO. If
1860 blocks_to_analyze is defined, use that set, otherwise the entire
1861 program. Include the defs if INCLUDE_DEFS. Include the uses if
1862 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1864 static void
1865 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1866 bool include_defs, bool include_uses,
1867 bool include_eq_uses)
1869 basic_block bb;
1870 unsigned int offset = 0;
1872 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1873 df_check_and_grow_ref_info (ref_info, 1);
1874 if (df->blocks_to_analyze)
1876 bitmap_iterator bi;
1877 unsigned int index;
1879 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1881 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1882 include_defs, include_uses,
1883 include_eq_uses);
1886 ref_info->table_size = offset;
1888 else
1890 FOR_ALL_BB (bb)
1891 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1892 include_defs, include_uses,
1893 include_eq_uses);
1894 ref_info->table_size = offset;
1899 /* If the use refs in DF are not organized, reorganize them. */
1901 void
1902 df_maybe_reorganize_use_refs (enum df_ref_order order)
1904 if (order == df->use_info.ref_order)
1905 return;
1907 switch (order)
1909 case DF_REF_ORDER_BY_REG:
1910 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1911 break;
1913 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1914 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1915 break;
1917 case DF_REF_ORDER_BY_INSN:
1918 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1919 break;
1921 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1922 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1923 break;
1925 case DF_REF_ORDER_NO_TABLE:
1926 free (df->use_info.refs);
1927 df->use_info.refs = NULL;
1928 df->use_info.refs_size = 0;
1929 break;
1931 case DF_REF_ORDER_UNORDERED:
1932 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1933 gcc_unreachable ();
1934 break;
1937 df->use_info.ref_order = order;
1941 /* If the def refs in DF are not organized, reorganize them. */
1943 void
1944 df_maybe_reorganize_def_refs (enum df_ref_order order)
1946 if (order == df->def_info.ref_order)
1947 return;
1949 switch (order)
1951 case DF_REF_ORDER_BY_REG:
1952 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1953 break;
1955 case DF_REF_ORDER_BY_INSN:
1956 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1957 break;
1959 case DF_REF_ORDER_NO_TABLE:
1960 free (df->def_info.refs);
1961 df->def_info.refs = NULL;
1962 df->def_info.refs_size = 0;
1963 break;
1965 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1966 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1967 case DF_REF_ORDER_UNORDERED:
1968 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1969 gcc_unreachable ();
1970 break;
1973 df->def_info.ref_order = order;
1977 /* Change all of the basic block references in INSN to use the insn's
1978 current basic block. This function is called from routines that move
1979 instructions from one block to another. */
1981 void
1982 df_insn_change_bb (rtx insn, basic_block new_bb)
1984 basic_block old_bb = BLOCK_FOR_INSN (insn);
1985 struct df_insn_info *insn_info;
1986 unsigned int uid = INSN_UID (insn);
1988 if (old_bb == new_bb)
1989 return;
1991 set_block_for_insn (insn, new_bb);
1993 if (!df)
1994 return;
1996 if (dump_file)
1997 fprintf (dump_file, "changing bb of uid %d\n", uid);
1999 insn_info = DF_INSN_UID_SAFE_GET (uid);
2000 if (insn_info == NULL)
2002 if (dump_file)
2003 fprintf (dump_file, " unscanned insn\n");
2004 df_insn_rescan (insn);
2005 return;
2008 if (!INSN_P (insn))
2009 return;
2011 df_set_bb_dirty (new_bb);
2012 if (old_bb)
2014 if (dump_file)
2015 fprintf (dump_file, " from %d to %d\n",
2016 old_bb->index, new_bb->index);
2017 df_set_bb_dirty (old_bb);
2019 else
2020 if (dump_file)
2021 fprintf (dump_file, " to %d\n", new_bb->index);
2025 /* Helper function for df_ref_change_reg_with_loc. */
2027 static void
2028 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
2029 struct df_reg_info *new_df,
2030 int new_regno, rtx loc)
2032 df_ref the_ref = old_df->reg_chain;
2034 while (the_ref)
2036 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
2037 && DF_REF_LOC (the_ref)
2038 && (*DF_REF_LOC (the_ref) == loc))
2040 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
2041 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
2042 df_ref *ref_vec, *ref_vec_t;
2043 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
2044 unsigned int count = 0;
2046 DF_REF_REGNO (the_ref) = new_regno;
2047 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
2049 /* Pull the_ref out of the old regno chain. */
2050 if (prev_ref)
2051 DF_REF_NEXT_REG (prev_ref) = next_ref;
2052 else
2053 old_df->reg_chain = next_ref;
2054 if (next_ref)
2055 DF_REF_PREV_REG (next_ref) = prev_ref;
2056 old_df->n_refs--;
2058 /* Put the ref into the new regno chain. */
2059 DF_REF_PREV_REG (the_ref) = NULL;
2060 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
2061 if (new_df->reg_chain)
2062 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
2063 new_df->reg_chain = the_ref;
2064 new_df->n_refs++;
2065 if (DF_REF_BB (the_ref))
2066 df_set_bb_dirty (DF_REF_BB (the_ref));
2068 /* Need to sort the record again that the ref was in because
2069 the regno is a sorting key. First, find the right
2070 record. */
2071 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
2072 ref_vec = insn_info->eq_uses;
2073 else
2074 ref_vec = insn_info->uses;
2075 if (dump_file)
2076 fprintf (dump_file, "changing reg in insn %d\n",
2077 DF_REF_INSN_UID (the_ref));
2079 ref_vec_t = ref_vec;
2081 /* Find the length. */
2082 while (*ref_vec_t)
2084 count++;
2085 ref_vec_t++;
2087 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
2089 the_ref = next_ref;
2091 else
2092 the_ref = DF_REF_NEXT_REG (the_ref);
2097 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2098 NEW_REGNO. Refs that do not match LOC are not changed which means
2099 that artificial refs are not changed since they have no loc. This
2100 call is to support the SET_REGNO macro. */
2102 void
2103 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2105 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2106 return;
2108 df_grow_reg_info ();
2110 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2111 DF_REG_DEF_GET (new_regno), new_regno, loc);
2112 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2113 DF_REG_USE_GET (new_regno), new_regno, loc);
2114 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2115 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2119 /* Delete the mw_hardregs that point into the eq_notes. */
2121 static unsigned int
2122 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2124 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2125 unsigned int deleted = 0;
2126 unsigned int count = 0;
2127 struct df_scan_problem_data *problem_data
2128 = (struct df_scan_problem_data *) df_scan->problem_data;
2130 if (!*mw_vec)
2131 return 0;
2133 while (*mw_vec)
2135 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2137 struct df_mw_hardreg **temp_vec = mw_vec;
2139 pool_free (problem_data->mw_reg_pool, *mw_vec);
2140 temp_vec = mw_vec;
2141 /* Shove the remaining ones down one to fill the gap. While
2142 this looks n**2, it is highly unusual to have any mw regs
2143 in eq_notes and the chances of more than one are almost
2144 non existent. */
2145 while (*temp_vec)
2147 *temp_vec = *(temp_vec + 1);
2148 temp_vec++;
2150 deleted++;
2152 else
2154 mw_vec++;
2155 count++;
2159 if (count == 0)
2161 df_scan_free_mws_vec (insn_info->mw_hardregs);
2162 insn_info->mw_hardregs = df_null_mw_rec;
2163 return 0;
2165 return deleted;
2169 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2171 void
2172 df_notes_rescan (rtx insn)
2174 struct df_insn_info *insn_info;
2175 unsigned int uid = INSN_UID (insn);
2177 if (!df)
2178 return;
2180 /* The client has disabled rescanning and plans to do it itself. */
2181 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2182 return;
2184 /* Do nothing if the insn hasn't been emitted yet. */
2185 if (!BLOCK_FOR_INSN (insn))
2186 return;
2188 df_grow_bb_info (df_scan);
2189 df_grow_reg_info ();
2191 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2193 /* The client has deferred rescanning. */
2194 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2196 if (!insn_info)
2198 insn_info = df_insn_create_insn_record (insn);
2199 insn_info->defs = df_null_ref_rec;
2200 insn_info->uses = df_null_ref_rec;
2201 insn_info->eq_uses = df_null_ref_rec;
2202 insn_info->mw_hardregs = df_null_mw_rec;
2205 bitmap_clear_bit (&df->insns_to_delete, uid);
2206 /* If the insn is set to be rescanned, it does not need to also
2207 be notes rescanned. */
2208 if (!bitmap_bit_p (&df->insns_to_rescan, uid))
2209 bitmap_set_bit (&df->insns_to_notes_rescan, INSN_UID (insn));
2210 return;
2213 bitmap_clear_bit (&df->insns_to_delete, uid);
2214 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
2216 if (insn_info)
2218 basic_block bb = BLOCK_FOR_INSN (insn);
2219 rtx note;
2220 struct df_collection_rec collection_rec;
2221 unsigned int num_deleted;
2222 unsigned int mw_len;
2224 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2225 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
2226 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
2228 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2229 df_ref_chain_delete (insn_info->eq_uses);
2230 insn_info->eq_uses = NULL;
2232 /* Process REG_EQUIV/REG_EQUAL notes */
2233 for (note = REG_NOTES (insn); note;
2234 note = XEXP (note, 1))
2236 switch (REG_NOTE_KIND (note))
2238 case REG_EQUIV:
2239 case REG_EQUAL:
2240 df_uses_record (&collection_rec,
2241 &XEXP (note, 0), DF_REF_REG_USE,
2242 bb, insn_info, DF_REF_IN_NOTE);
2243 default:
2244 break;
2248 /* Find some place to put any new mw_hardregs. */
2249 df_canonize_collection_rec (&collection_rec);
2250 mw_len = collection_rec.mw_vec.length ();
2251 if (mw_len)
2253 unsigned int count = 0;
2254 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2255 while (*mw_rec)
2257 count++;
2258 mw_rec++;
2261 if (count)
2263 /* Append to the end of the existing record after
2264 expanding it if necessary. */
2265 if (mw_len > num_deleted)
2267 insn_info->mw_hardregs =
2268 XRESIZEVEC (struct df_mw_hardreg *,
2269 insn_info->mw_hardregs,
2270 count + 1 + mw_len);
2272 memcpy (&insn_info->mw_hardregs[count],
2273 collection_rec.mw_vec.address (),
2274 mw_len * sizeof (struct df_mw_hardreg *));
2275 insn_info->mw_hardregs[count + mw_len] = NULL;
2276 qsort (insn_info->mw_hardregs, count + mw_len,
2277 sizeof (struct df_mw_hardreg *), df_mw_compare);
2279 else
2281 /* No vector there. */
2282 insn_info->mw_hardregs
2283 = XNEWVEC (struct df_mw_hardreg*, 1 + mw_len);
2284 memcpy (insn_info->mw_hardregs,
2285 collection_rec.mw_vec.address (),
2286 mw_len * sizeof (struct df_mw_hardreg *));
2287 insn_info->mw_hardregs[mw_len] = NULL;
2290 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2291 ignore it. */
2292 collection_rec.mw_vec.release ();
2293 df_refs_add_to_chains (&collection_rec, bb, insn);
2294 collection_rec.eq_use_vec.release ();
2296 else
2297 df_insn_rescan (insn);
2302 /*----------------------------------------------------------------------------
2303 Hard core instruction scanning code. No external interfaces here,
2304 just a lot of routines that look inside insns.
2305 ----------------------------------------------------------------------------*/
2308 /* Return true if the contents of two df_ref's are identical.
2309 It ignores DF_REF_MARKER. */
2311 static bool
2312 df_ref_equal_p (df_ref ref1, df_ref ref2)
2314 if (!ref2)
2315 return false;
2317 if (ref1 == ref2)
2318 return true;
2320 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2321 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2322 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2323 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2324 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2325 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2326 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2327 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2328 return false;
2330 switch (DF_REF_CLASS (ref1))
2332 case DF_REF_ARTIFICIAL:
2333 case DF_REF_BASE:
2334 return true;
2336 case DF_REF_REGULAR:
2337 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2339 default:
2340 gcc_unreachable ();
2342 return false;
2346 /* Compare REF1 and REF2 for sorting. This is only called from places
2347 where all of the refs are of the same type, in the same insn, and
2348 have the same bb. So these fields are not checked. */
2350 static int
2351 df_ref_compare (const void *r1, const void *r2)
2353 const df_ref ref1 = *(const df_ref *)r1;
2354 const df_ref ref2 = *(const df_ref *)r2;
2356 if (ref1 == ref2)
2357 return 0;
2359 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2360 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2362 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2363 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2365 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2366 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2368 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2369 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2371 /* Cannot look at the LOC field on artificial refs. */
2372 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2373 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2374 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2376 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2378 /* If two refs are identical except that one of them has is from
2379 a mw and one is not, we need to have the one with the mw
2380 first. */
2381 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2382 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2383 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2384 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2385 return -1;
2386 else
2387 return 1;
2390 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2393 static void
2394 df_swap_refs (vec<df_ref, va_stack> *ref_vec, int i, int j)
2396 df_ref tmp = (*ref_vec)[i];
2397 (*ref_vec)[i] = (*ref_vec)[j];
2398 (*ref_vec)[j] = tmp;
2401 /* Sort and compress a set of refs. */
2403 static void
2404 df_sort_and_compress_refs (vec<df_ref, va_stack> *ref_vec)
2406 unsigned int count;
2407 unsigned int i;
2408 unsigned int dist = 0;
2410 count = ref_vec->length ();
2412 /* If there are 1 or 0 elements, there is nothing to do. */
2413 if (count < 2)
2414 return;
2415 else if (count == 2)
2417 df_ref r0 = (*ref_vec)[0];
2418 df_ref r1 = (*ref_vec)[1];
2419 if (df_ref_compare (&r0, &r1) > 0)
2420 df_swap_refs (ref_vec, 0, 1);
2422 else
2424 for (i = 0; i < count - 1; i++)
2426 df_ref r0 = (*ref_vec)[i];
2427 df_ref r1 = (*ref_vec)[i + 1];
2428 if (df_ref_compare (&r0, &r1) >= 0)
2429 break;
2431 /* If the array is already strictly ordered,
2432 which is the most common case for large COUNT case
2433 (which happens for CALL INSNs),
2434 no need to sort and filter out duplicate.
2435 Simply return the count.
2436 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2437 of DF_REF_COMPARE. */
2438 if (i == count - 1)
2439 return;
2440 ref_vec->qsort (df_ref_compare);
2443 for (i=0; i<count-dist; i++)
2445 /* Find the next ref that is not equal to the current ref. */
2446 while (i + dist + 1 < count
2447 && df_ref_equal_p ((*ref_vec)[i],
2448 (*ref_vec)[i + dist + 1]))
2450 df_free_ref ((*ref_vec)[i + dist + 1]);
2451 dist++;
2453 /* Copy it down to the next position. */
2454 if (dist && i + dist + 1 < count)
2455 (*ref_vec)[i + 1] = (*ref_vec)[i + dist + 1];
2458 count -= dist;
2459 ref_vec->truncate (count);
2463 /* Return true if the contents of two df_ref's are identical.
2464 It ignores DF_REF_MARKER. */
2466 static bool
2467 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2469 if (!mw2)
2470 return false;
2471 return (mw1 == mw2) ||
2472 (mw1->mw_reg == mw2->mw_reg
2473 && mw1->type == mw2->type
2474 && mw1->flags == mw2->flags
2475 && mw1->start_regno == mw2->start_regno
2476 && mw1->end_regno == mw2->end_regno);
2480 /* Compare MW1 and MW2 for sorting. */
2482 static int
2483 df_mw_compare (const void *m1, const void *m2)
2485 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2486 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2488 if (mw1 == mw2)
2489 return 0;
2491 if (mw1->type != mw2->type)
2492 return mw1->type - mw2->type;
2494 if (mw1->flags != mw2->flags)
2495 return mw1->flags - mw2->flags;
2497 if (mw1->start_regno != mw2->start_regno)
2498 return mw1->start_regno - mw2->start_regno;
2500 if (mw1->end_regno != mw2->end_regno)
2501 return mw1->end_regno - mw2->end_regno;
2503 if (mw1->mw_reg != mw2->mw_reg)
2504 return mw1->mw_order - mw2->mw_order;
2506 return 0;
2510 /* Sort and compress a set of refs. */
2512 static void
2513 df_sort_and_compress_mws (vec<df_mw_hardreg_ptr, va_stack> *mw_vec)
2515 unsigned int count;
2516 struct df_scan_problem_data *problem_data
2517 = (struct df_scan_problem_data *) df_scan->problem_data;
2518 unsigned int i;
2519 unsigned int dist = 0;
2521 count = mw_vec->length ();
2522 if (count < 2)
2523 return;
2524 else if (count == 2)
2526 struct df_mw_hardreg *m0 = (*mw_vec)[0];
2527 struct df_mw_hardreg *m1 = (*mw_vec)[1];
2528 if (df_mw_compare (&m0, &m1) > 0)
2530 struct df_mw_hardreg *tmp = (*mw_vec)[0];
2531 (*mw_vec)[0] = (*mw_vec)[1];
2532 (*mw_vec)[1] = tmp;
2535 else
2536 mw_vec->qsort (df_mw_compare);
2538 for (i=0; i<count-dist; i++)
2540 /* Find the next ref that is not equal to the current ref. */
2541 while (i + dist + 1 < count
2542 && df_mw_equal_p ((*mw_vec)[i], (*mw_vec)[i + dist + 1]))
2544 pool_free (problem_data->mw_reg_pool,
2545 (*mw_vec)[i + dist + 1]);
2546 dist++;
2548 /* Copy it down to the next position. */
2549 if (dist && i + dist + 1 < count)
2550 (*mw_vec)[i + 1] = (*mw_vec)[i + dist + 1];
2553 count -= dist;
2554 mw_vec->truncate (count);
2558 /* Sort and remove duplicates from the COLLECTION_REC. */
2560 static void
2561 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2563 df_sort_and_compress_refs (&collection_rec->def_vec);
2564 df_sort_and_compress_refs (&collection_rec->use_vec);
2565 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2566 df_sort_and_compress_mws (&collection_rec->mw_vec);
2570 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2572 static void
2573 df_install_ref (df_ref this_ref,
2574 struct df_reg_info *reg_info,
2575 struct df_ref_info *ref_info,
2576 bool add_to_table)
2578 unsigned int regno = DF_REF_REGNO (this_ref);
2579 /* Add the ref to the reg_{def,use,eq_use} chain. */
2580 df_ref head = reg_info->reg_chain;
2582 reg_info->reg_chain = this_ref;
2583 reg_info->n_refs++;
2585 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2587 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2588 df->hard_regs_live_count[regno]++;
2591 gcc_checking_assert (DF_REF_NEXT_REG (this_ref) == NULL
2592 && DF_REF_PREV_REG (this_ref) == NULL);
2594 DF_REF_NEXT_REG (this_ref) = head;
2596 /* We cannot actually link to the head of the chain. */
2597 DF_REF_PREV_REG (this_ref) = NULL;
2599 if (head)
2600 DF_REF_PREV_REG (head) = this_ref;
2602 if (add_to_table)
2604 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2605 df_check_and_grow_ref_info (ref_info, 1);
2606 DF_REF_ID (this_ref) = ref_info->table_size;
2607 /* Add the ref to the big array of defs. */
2608 ref_info->refs[ref_info->table_size] = this_ref;
2609 ref_info->table_size++;
2611 else
2612 DF_REF_ID (this_ref) = -1;
2614 ref_info->total_size++;
2618 /* This function takes one of the groups of refs (defs, uses or
2619 eq_uses) and installs the entire group into the insn. It also adds
2620 each of these refs into the appropriate chains. */
2622 static df_ref *
2623 df_install_refs (basic_block bb,
2624 vec<df_ref, va_stack> old_vec,
2625 struct df_reg_info **reg_info,
2626 struct df_ref_info *ref_info,
2627 bool is_notes)
2629 unsigned int count;
2631 count = old_vec.length ();
2632 if (count)
2634 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2635 bool add_to_table;
2636 df_ref this_ref;
2637 unsigned int ix;
2639 switch (ref_info->ref_order)
2641 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2642 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2643 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2644 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2645 add_to_table = true;
2646 break;
2647 case DF_REF_ORDER_UNORDERED:
2648 case DF_REF_ORDER_BY_REG:
2649 case DF_REF_ORDER_BY_INSN:
2650 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2651 add_to_table = !is_notes;
2652 break;
2653 default:
2654 add_to_table = false;
2655 break;
2658 /* Do not add if ref is not in the right blocks. */
2659 if (add_to_table && df->analyze_subset)
2660 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2662 FOR_EACH_VEC_ELT (old_vec, ix, this_ref)
2664 new_vec[ix] = this_ref;
2665 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2666 ref_info, add_to_table);
2669 new_vec[count] = NULL;
2670 return new_vec;
2672 else
2673 return df_null_ref_rec;
2677 /* This function takes the mws installs the entire group into the
2678 insn. */
2680 static struct df_mw_hardreg **
2681 df_install_mws (vec<df_mw_hardreg_ptr, va_stack> old_vec)
2683 unsigned int count;
2685 count = old_vec.length ();
2686 if (count)
2688 struct df_mw_hardreg **new_vec
2689 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2690 memcpy (new_vec, old_vec.address (),
2691 sizeof (struct df_mw_hardreg*) * count);
2692 new_vec[count] = NULL;
2693 return new_vec;
2695 else
2696 return df_null_mw_rec;
2700 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2701 chains and update other necessary information. */
2703 static void
2704 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2705 basic_block bb, rtx insn)
2707 if (insn)
2709 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2710 /* If there is a vector in the collection rec, add it to the
2711 insn. A null rec is a signal that the caller will handle the
2712 chain specially. */
2713 if (collection_rec->def_vec.exists ())
2715 df_scan_free_ref_vec (insn_rec->defs);
2716 insn_rec->defs
2717 = df_install_refs (bb, collection_rec->def_vec,
2718 df->def_regs,
2719 &df->def_info, false);
2721 if (collection_rec->use_vec.exists ())
2723 df_scan_free_ref_vec (insn_rec->uses);
2724 insn_rec->uses
2725 = df_install_refs (bb, collection_rec->use_vec,
2726 df->use_regs,
2727 &df->use_info, false);
2729 if (collection_rec->eq_use_vec.exists ())
2731 df_scan_free_ref_vec (insn_rec->eq_uses);
2732 insn_rec->eq_uses
2733 = df_install_refs (bb, collection_rec->eq_use_vec,
2734 df->eq_use_regs,
2735 &df->use_info, true);
2737 if (collection_rec->mw_vec.exists ())
2739 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2740 insn_rec->mw_hardregs
2741 = df_install_mws (collection_rec->mw_vec);
2744 else
2746 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2748 df_scan_free_ref_vec (bb_info->artificial_defs);
2749 bb_info->artificial_defs
2750 = df_install_refs (bb, collection_rec->def_vec,
2751 df->def_regs,
2752 &df->def_info, false);
2753 df_scan_free_ref_vec (bb_info->artificial_uses);
2754 bb_info->artificial_uses
2755 = df_install_refs (bb, collection_rec->use_vec,
2756 df->use_regs,
2757 &df->use_info, false);
2762 /* Allocate a ref and initialize its fields. */
2764 static df_ref
2765 df_ref_create_structure (enum df_ref_class cl,
2766 struct df_collection_rec *collection_rec,
2767 rtx reg, rtx *loc,
2768 basic_block bb, struct df_insn_info *info,
2769 enum df_ref_type ref_type,
2770 int ref_flags)
2772 df_ref this_ref = NULL;
2773 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2774 struct df_scan_problem_data *problem_data
2775 = (struct df_scan_problem_data *) df_scan->problem_data;
2777 switch (cl)
2779 case DF_REF_BASE:
2780 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2781 gcc_checking_assert (loc == NULL);
2782 break;
2784 case DF_REF_ARTIFICIAL:
2785 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2786 this_ref->artificial_ref.bb = bb;
2787 gcc_checking_assert (loc == NULL);
2788 break;
2790 case DF_REF_REGULAR:
2791 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2792 this_ref->regular_ref.loc = loc;
2793 gcc_checking_assert (loc);
2794 break;
2797 DF_REF_CLASS (this_ref) = cl;
2798 DF_REF_ID (this_ref) = -1;
2799 DF_REF_REG (this_ref) = reg;
2800 DF_REF_REGNO (this_ref) = regno;
2801 DF_REF_TYPE (this_ref) = ref_type;
2802 DF_REF_INSN_INFO (this_ref) = info;
2803 DF_REF_CHAIN (this_ref) = NULL;
2804 DF_REF_FLAGS (this_ref) = ref_flags;
2805 DF_REF_NEXT_REG (this_ref) = NULL;
2806 DF_REF_PREV_REG (this_ref) = NULL;
2807 DF_REF_ORDER (this_ref) = df->ref_order++;
2809 /* We need to clear this bit because fwprop, and in the future
2810 possibly other optimizations sometimes create new refs using ond
2811 refs as the model. */
2812 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2814 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2815 if (regno < FIRST_PSEUDO_REGISTER
2816 && !DF_REF_IS_ARTIFICIAL (this_ref)
2817 && !DEBUG_INSN_P (DF_REF_INSN (this_ref)))
2819 if (DF_REF_REG_DEF_P (this_ref))
2821 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2822 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2824 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2825 && (regno == FRAME_POINTER_REGNUM
2826 || regno == ARG_POINTER_REGNUM)))
2827 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2830 if (collection_rec)
2832 if (DF_REF_REG_DEF_P (this_ref))
2833 collection_rec->def_vec.safe_push (this_ref);
2834 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2835 collection_rec->eq_use_vec.safe_push (this_ref);
2836 else
2837 collection_rec->use_vec.safe_push (this_ref);
2839 else
2840 df_install_ref_incremental (this_ref);
2842 return this_ref;
2846 /* Create new references of type DF_REF_TYPE for each part of register REG
2847 at address LOC within INSN of BB. */
2850 static void
2851 df_ref_record (enum df_ref_class cl,
2852 struct df_collection_rec *collection_rec,
2853 rtx reg, rtx *loc,
2854 basic_block bb, struct df_insn_info *insn_info,
2855 enum df_ref_type ref_type,
2856 int ref_flags)
2858 unsigned int regno;
2860 gcc_checking_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2862 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2863 if (regno < FIRST_PSEUDO_REGISTER)
2865 struct df_mw_hardreg *hardreg = NULL;
2866 struct df_scan_problem_data *problem_data
2867 = (struct df_scan_problem_data *) df_scan->problem_data;
2868 unsigned int i;
2869 unsigned int endregno;
2870 df_ref ref;
2872 if (GET_CODE (reg) == SUBREG)
2874 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2875 SUBREG_BYTE (reg), GET_MODE (reg));
2876 endregno = regno + subreg_nregs (reg);
2878 else
2879 endregno = END_HARD_REGNO (reg);
2881 /* If this is a multiword hardreg, we create some extra
2882 datastructures that will enable us to easily build REG_DEAD
2883 and REG_UNUSED notes. */
2884 if (collection_rec
2885 && (endregno != regno + 1) && insn_info)
2887 /* Sets to a subreg of a multiword register are partial.
2888 Sets to a non-subreg of a multiword register are not. */
2889 if (GET_CODE (reg) == SUBREG)
2890 ref_flags |= DF_REF_PARTIAL;
2891 ref_flags |= DF_REF_MW_HARDREG;
2893 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2894 hardreg->type = ref_type;
2895 hardreg->flags = ref_flags;
2896 hardreg->mw_reg = reg;
2897 hardreg->start_regno = regno;
2898 hardreg->end_regno = endregno - 1;
2899 hardreg->mw_order = df->ref_order++;
2900 collection_rec->mw_vec.safe_push (hardreg);
2903 for (i = regno; i < endregno; i++)
2905 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2906 bb, insn_info, ref_type, ref_flags);
2908 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2911 else
2913 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2914 ref_type, ref_flags);
2919 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2920 covered by the outer mode is smaller than that covered by the inner mode,
2921 is a read-modify-write operation.
2922 This function returns true iff the SUBREG X is such a SUBREG. */
2924 bool
2925 df_read_modify_subreg_p (rtx x)
2927 unsigned int isize, osize;
2928 if (GET_CODE (x) != SUBREG)
2929 return false;
2930 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2931 osize = GET_MODE_SIZE (GET_MODE (x));
2932 return isize > osize
2933 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2937 /* Process all the registers defined in the rtx pointed by LOC.
2938 Autoincrement/decrement definitions will be picked up by df_uses_record.
2939 Any change here has to be matched in df_find_hard_reg_defs_1. */
2941 static void
2942 df_def_record_1 (struct df_collection_rec *collection_rec,
2943 rtx *loc, basic_block bb, struct df_insn_info *insn_info,
2944 int flags)
2946 rtx dst = *loc;
2948 /* It is legal to have a set destination be a parallel. */
2949 if (GET_CODE (dst) == PARALLEL)
2951 int i;
2952 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2954 rtx temp = XVECEXP (dst, 0, i);
2955 gcc_assert (GET_CODE (temp) == EXPR_LIST);
2956 df_def_record_1 (collection_rec, &XEXP (temp, 0),
2957 bb, insn_info, flags);
2959 return;
2962 if (GET_CODE (dst) == STRICT_LOW_PART)
2964 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2966 loc = &XEXP (dst, 0);
2967 dst = *loc;
2970 if (GET_CODE (dst) == ZERO_EXTRACT)
2972 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
2974 loc = &XEXP (dst, 0);
2975 dst = *loc;
2978 /* At this point if we do not have a reg or a subreg, just return. */
2979 if (REG_P (dst))
2981 df_ref_record (DF_REF_REGULAR, collection_rec,
2982 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
2984 /* We want to keep sp alive everywhere - by making all
2985 writes to sp also use of sp. */
2986 if (REGNO (dst) == STACK_POINTER_REGNUM)
2987 df_ref_record (DF_REF_BASE, collection_rec,
2988 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags);
2990 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
2992 if (df_read_modify_subreg_p (dst))
2993 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
2995 flags |= DF_REF_SUBREG;
2997 df_ref_record (DF_REF_REGULAR, collection_rec,
2998 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
3003 /* Process all the registers defined in the pattern rtx, X. Any change
3004 here has to be matched in df_find_hard_reg_defs. */
3006 static void
3007 df_defs_record (struct df_collection_rec *collection_rec,
3008 rtx x, basic_block bb, struct df_insn_info *insn_info,
3009 int flags)
3011 RTX_CODE code = GET_CODE (x);
3012 int i;
3014 switch (code)
3016 case SET:
3017 df_def_record_1 (collection_rec, &SET_DEST (x), bb, insn_info, flags);
3018 break;
3020 case CLOBBER:
3021 flags |= DF_REF_MUST_CLOBBER;
3022 df_def_record_1 (collection_rec, &XEXP (x, 0), bb, insn_info, flags);
3023 break;
3025 case COND_EXEC:
3026 df_defs_record (collection_rec, COND_EXEC_CODE (x),
3027 bb, insn_info, DF_REF_CONDITIONAL);
3028 break;
3030 case PARALLEL:
3031 for (i = 0; i < XVECLEN (x, 0); i++)
3032 df_defs_record (collection_rec, XVECEXP (x, 0, i),
3033 bb, insn_info, flags);
3034 break;
3035 default:
3036 /* No DEFs to record in other cases */
3037 break;
3041 /* Set bits in *DEFS for hard registers found in the rtx DST, which is the
3042 destination of a set or clobber. This has to match the logic in
3043 df_defs_record_1. */
3045 static void
3046 df_find_hard_reg_defs_1 (rtx dst, HARD_REG_SET *defs)
3048 /* It is legal to have a set destination be a parallel. */
3049 if (GET_CODE (dst) == PARALLEL)
3051 int i;
3052 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
3054 rtx temp = XVECEXP (dst, 0, i);
3055 gcc_assert (GET_CODE (temp) == EXPR_LIST);
3056 df_find_hard_reg_defs_1 (XEXP (temp, 0), defs);
3058 return;
3061 if (GET_CODE (dst) == STRICT_LOW_PART)
3062 dst = XEXP (dst, 0);
3064 if (GET_CODE (dst) == ZERO_EXTRACT)
3065 dst = XEXP (dst, 0);
3067 /* At this point if we do not have a reg or a subreg, just return. */
3068 if (REG_P (dst) && HARD_REGISTER_P (dst))
3069 SET_HARD_REG_BIT (*defs, REGNO (dst));
3070 else if (GET_CODE (dst) == SUBREG
3071 && REG_P (SUBREG_REG (dst)) && HARD_REGISTER_P (dst))
3072 SET_HARD_REG_BIT (*defs, REGNO (SUBREG_REG (dst)));
3075 /* Set bits in *DEFS for hard registers defined in the pattern X. This
3076 has to match the logic in df_defs_record. */
3078 static void
3079 df_find_hard_reg_defs (rtx x, HARD_REG_SET *defs)
3081 RTX_CODE code = GET_CODE (x);
3082 int i;
3084 switch (code)
3086 case SET:
3087 df_find_hard_reg_defs_1 (SET_DEST (x), defs);
3088 break;
3090 case CLOBBER:
3091 df_find_hard_reg_defs_1 (XEXP (x, 0), defs);
3092 break;
3094 case COND_EXEC:
3095 df_find_hard_reg_defs (COND_EXEC_CODE (x), defs);
3096 break;
3098 case PARALLEL:
3099 for (i = 0; i < XVECLEN (x, 0); i++)
3100 df_find_hard_reg_defs (XVECEXP (x, 0, i), defs);
3101 break;
3102 default:
3103 /* No DEFs to record in other cases */
3104 break;
3109 /* Process all the registers used in the rtx at address LOC. */
3111 static void
3112 df_uses_record (struct df_collection_rec *collection_rec,
3113 rtx *loc, enum df_ref_type ref_type,
3114 basic_block bb, struct df_insn_info *insn_info,
3115 int flags)
3117 RTX_CODE code;
3118 rtx x;
3120 retry:
3121 x = *loc;
3122 if (!x)
3123 return;
3124 code = GET_CODE (x);
3125 switch (code)
3127 case LABEL_REF:
3128 case SYMBOL_REF:
3129 case CONST:
3130 CASE_CONST_ANY:
3131 case PC:
3132 case CC0:
3133 case ADDR_VEC:
3134 case ADDR_DIFF_VEC:
3135 return;
3137 case CLOBBER:
3138 /* If we are clobbering a MEM, mark any registers inside the address
3139 as being used. */
3140 if (MEM_P (XEXP (x, 0)))
3141 df_uses_record (collection_rec,
3142 &XEXP (XEXP (x, 0), 0),
3143 DF_REF_REG_MEM_STORE,
3144 bb, insn_info,
3145 flags);
3147 /* If we're clobbering a REG then we have a def so ignore. */
3148 return;
3150 case MEM:
3151 df_uses_record (collection_rec,
3152 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3153 bb, insn_info, flags & DF_REF_IN_NOTE);
3154 return;
3156 case SUBREG:
3157 /* While we're here, optimize this case. */
3158 flags |= DF_REF_PARTIAL;
3159 /* In case the SUBREG is not of a REG, do not optimize. */
3160 if (!REG_P (SUBREG_REG (x)))
3162 loc = &SUBREG_REG (x);
3163 df_uses_record (collection_rec, loc, ref_type, bb, insn_info, flags);
3164 return;
3166 /* ... Fall through ... */
3168 case REG:
3169 df_ref_record (DF_REF_REGULAR, collection_rec,
3170 x, loc, bb, insn_info,
3171 ref_type, flags);
3172 return;
3174 case SIGN_EXTRACT:
3175 case ZERO_EXTRACT:
3177 df_uses_record (collection_rec,
3178 &XEXP (x, 1), ref_type, bb, insn_info, flags);
3179 df_uses_record (collection_rec,
3180 &XEXP (x, 2), ref_type, bb, insn_info, flags);
3182 /* If the parameters to the zero or sign extract are
3183 constants, strip them off and recurse, otherwise there is
3184 no information that we can gain from this operation. */
3185 if (code == ZERO_EXTRACT)
3186 flags |= DF_REF_ZERO_EXTRACT;
3187 else
3188 flags |= DF_REF_SIGN_EXTRACT;
3190 df_uses_record (collection_rec,
3191 &XEXP (x, 0), ref_type, bb, insn_info, flags);
3192 return;
3194 break;
3196 case SET:
3198 rtx dst = SET_DEST (x);
3199 gcc_assert (!(flags & DF_REF_IN_NOTE));
3200 df_uses_record (collection_rec,
3201 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags);
3203 switch (GET_CODE (dst))
3205 case SUBREG:
3206 if (df_read_modify_subreg_p (dst))
3208 df_uses_record (collection_rec, &SUBREG_REG (dst),
3209 DF_REF_REG_USE, bb, insn_info,
3210 flags | DF_REF_READ_WRITE | DF_REF_SUBREG);
3211 break;
3213 /* Fall through. */
3214 case REG:
3215 case PARALLEL:
3216 case SCRATCH:
3217 case PC:
3218 case CC0:
3219 break;
3220 case MEM:
3221 df_uses_record (collection_rec, &XEXP (dst, 0),
3222 DF_REF_REG_MEM_STORE, bb, insn_info, flags);
3223 break;
3224 case STRICT_LOW_PART:
3226 rtx *temp = &XEXP (dst, 0);
3227 /* A strict_low_part uses the whole REG and not just the
3228 SUBREG. */
3229 dst = XEXP (dst, 0);
3230 df_uses_record (collection_rec,
3231 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3232 DF_REF_REG_USE, bb, insn_info,
3233 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART);
3235 break;
3236 case ZERO_EXTRACT:
3238 df_uses_record (collection_rec, &XEXP (dst, 1),
3239 DF_REF_REG_USE, bb, insn_info, flags);
3240 df_uses_record (collection_rec, &XEXP (dst, 2),
3241 DF_REF_REG_USE, bb, insn_info, flags);
3242 if (GET_CODE (XEXP (dst,0)) == MEM)
3243 df_uses_record (collection_rec, &XEXP (dst, 0),
3244 DF_REF_REG_USE, bb, insn_info,
3245 flags);
3246 else
3247 df_uses_record (collection_rec, &XEXP (dst, 0),
3248 DF_REF_REG_USE, bb, insn_info,
3249 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT);
3251 break;
3253 default:
3254 gcc_unreachable ();
3256 return;
3259 case RETURN:
3260 case SIMPLE_RETURN:
3261 break;
3263 case ASM_OPERANDS:
3264 case UNSPEC_VOLATILE:
3265 case TRAP_IF:
3266 case ASM_INPUT:
3268 /* Traditional and volatile asm instructions must be
3269 considered to use and clobber all hard registers, all
3270 pseudo-registers and all of memory. So must TRAP_IF and
3271 UNSPEC_VOLATILE operations.
3273 Consider for instance a volatile asm that changes the fpu
3274 rounding mode. An insn should not be moved across this
3275 even if it only uses pseudo-regs because it might give an
3276 incorrectly rounded result.
3278 However, flow.c's liveness computation did *not* do this,
3279 giving the reasoning as " ?!? Unfortunately, marking all
3280 hard registers as live causes massive problems for the
3281 register allocator and marking all pseudos as live creates
3282 mountains of uninitialized variable warnings."
3284 In order to maintain the status quo with regard to liveness
3285 and uses, we do what flow.c did and just mark any regs we
3286 can find in ASM_OPERANDS as used. In global asm insns are
3287 scanned and regs_asm_clobbered is filled out.
3289 For all ASM_OPERANDS, we must traverse the vector of input
3290 operands. We can not just fall through here since then we
3291 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3292 which do not indicate traditional asms unlike their normal
3293 usage. */
3294 if (code == ASM_OPERANDS)
3296 int j;
3298 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3299 df_uses_record (collection_rec, &ASM_OPERANDS_INPUT (x, j),
3300 DF_REF_REG_USE, bb, insn_info, flags);
3301 return;
3303 break;
3306 case VAR_LOCATION:
3307 df_uses_record (collection_rec,
3308 &PAT_VAR_LOCATION_LOC (x),
3309 DF_REF_REG_USE, bb, insn_info, flags);
3310 return;
3312 case PRE_DEC:
3313 case POST_DEC:
3314 case PRE_INC:
3315 case POST_INC:
3316 case PRE_MODIFY:
3317 case POST_MODIFY:
3318 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3319 /* Catch the def of the register being modified. */
3320 df_ref_record (DF_REF_REGULAR, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3321 bb, insn_info,
3322 DF_REF_REG_DEF,
3323 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY);
3325 /* ... Fall through to handle uses ... */
3327 default:
3328 break;
3331 /* Recursively scan the operands of this expression. */
3333 const char *fmt = GET_RTX_FORMAT (code);
3334 int i;
3336 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3338 if (fmt[i] == 'e')
3340 /* Tail recursive case: save a function call level. */
3341 if (i == 0)
3343 loc = &XEXP (x, 0);
3344 goto retry;
3346 df_uses_record (collection_rec, &XEXP (x, i), ref_type,
3347 bb, insn_info, flags);
3349 else if (fmt[i] == 'E')
3351 int j;
3352 for (j = 0; j < XVECLEN (x, i); j++)
3353 df_uses_record (collection_rec,
3354 &XVECEXP (x, i, j), ref_type,
3355 bb, insn_info, flags);
3360 return;
3364 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3366 static void
3367 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3369 unsigned int ix;
3370 df_ref ref;
3372 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
3374 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3376 df_ref use;
3378 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3379 DF_REF_LOC (ref), DF_REF_BB (ref),
3380 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3381 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL);
3382 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3388 /* Get call's extra defs and uses (track caller-saved registers). */
3390 static void
3391 df_get_call_refs (struct df_collection_rec *collection_rec,
3392 basic_block bb,
3393 struct df_insn_info *insn_info,
3394 int flags)
3396 rtx note;
3397 bool is_sibling_call;
3398 unsigned int i;
3399 HARD_REG_SET defs_generated;
3401 CLEAR_HARD_REG_SET (defs_generated);
3402 df_find_hard_reg_defs (PATTERN (insn_info->insn), &defs_generated);
3403 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3405 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3407 if (i == STACK_POINTER_REGNUM)
3408 /* The stack ptr is used (honorarily) by a CALL insn. */
3409 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3410 NULL, bb, insn_info, DF_REF_REG_USE,
3411 DF_REF_CALL_STACK_USAGE | flags);
3412 else if (global_regs[i])
3414 /* Calls to const functions cannot access any global registers and
3415 calls to pure functions cannot set them. All other calls may
3416 reference any of the global registers, so they are recorded as
3417 used. */
3418 if (!RTL_CONST_CALL_P (insn_info->insn))
3420 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3421 NULL, bb, insn_info, DF_REF_REG_USE, flags);
3422 if (!RTL_PURE_CALL_P (insn_info->insn))
3423 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3424 NULL, bb, insn_info, DF_REF_REG_DEF, flags);
3427 else if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
3428 /* no clobbers for regs that are the result of the call */
3429 && !TEST_HARD_REG_BIT (defs_generated, i)
3430 && (!is_sibling_call
3431 || !bitmap_bit_p (df->exit_block_uses, i)
3432 || refers_to_regno_p (i, i+1,
3433 crtl->return_rtx, NULL)))
3434 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3435 NULL, bb, insn_info, DF_REF_REG_DEF,
3436 DF_REF_MAY_CLOBBER | flags);
3439 /* Record the registers used to pass arguments, and explicitly
3440 noted as clobbered. */
3441 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3442 note = XEXP (note, 1))
3444 if (GET_CODE (XEXP (note, 0)) == USE)
3445 df_uses_record (collection_rec, &XEXP (XEXP (note, 0), 0),
3446 DF_REF_REG_USE, bb, insn_info, flags);
3447 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3449 if (REG_P (XEXP (XEXP (note, 0), 0)))
3451 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3452 if (!TEST_HARD_REG_BIT (defs_generated, regno))
3453 df_defs_record (collection_rec, XEXP (note, 0), bb,
3454 insn_info, flags);
3456 else
3457 df_uses_record (collection_rec, &XEXP (note, 0),
3458 DF_REF_REG_USE, bb, insn_info, flags);
3462 return;
3465 /* Collect all refs in the INSN. This function is free of any
3466 side-effect - it will create and return a lists of df_ref's in the
3467 COLLECTION_REC without putting those refs into existing ref chains
3468 and reg chains. */
3470 static void
3471 df_insn_refs_collect (struct df_collection_rec *collection_rec,
3472 basic_block bb, struct df_insn_info *insn_info)
3474 rtx note;
3475 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3477 /* Clear out the collection record. */
3478 collection_rec->def_vec.truncate (0);
3479 collection_rec->use_vec.truncate (0);
3480 collection_rec->eq_use_vec.truncate (0);
3481 collection_rec->mw_vec.truncate (0);
3483 /* Process REG_EQUIV/REG_EQUAL notes. */
3484 for (note = REG_NOTES (insn_info->insn); note;
3485 note = XEXP (note, 1))
3487 switch (REG_NOTE_KIND (note))
3489 case REG_EQUIV:
3490 case REG_EQUAL:
3491 df_uses_record (collection_rec,
3492 &XEXP (note, 0), DF_REF_REG_USE,
3493 bb, insn_info, DF_REF_IN_NOTE);
3494 break;
3495 case REG_NON_LOCAL_GOTO:
3496 /* The frame ptr is used by a non-local goto. */
3497 df_ref_record (DF_REF_BASE, collection_rec,
3498 regno_reg_rtx[FRAME_POINTER_REGNUM],
3499 NULL, bb, insn_info,
3500 DF_REF_REG_USE, 0);
3501 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3502 df_ref_record (DF_REF_BASE, collection_rec,
3503 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3504 NULL, bb, insn_info,
3505 DF_REF_REG_USE, 0);
3506 #endif
3507 break;
3508 default:
3509 break;
3513 /* For CALL_INSNs, first record DF_REF_BASE register defs, as well as
3514 uses from CALL_INSN_FUNCTION_USAGE. */
3515 if (CALL_P (insn_info->insn))
3516 df_get_call_refs (collection_rec, bb, insn_info,
3517 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3519 /* Record other defs. These should be mostly for DF_REF_REGULAR, so
3520 that a qsort on the defs is unnecessary in most cases. */
3521 df_defs_record (collection_rec,
3522 PATTERN (insn_info->insn), bb, insn_info, 0);
3524 /* Record the register uses. */
3525 df_uses_record (collection_rec,
3526 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0);
3528 /* DF_REF_CONDITIONAL needs corresponding USES. */
3529 if (is_cond_exec)
3530 df_get_conditional_uses (collection_rec);
3532 df_canonize_collection_rec (collection_rec);
3535 /* Recompute the luids for the insns in BB. */
3537 void
3538 df_recompute_luids (basic_block bb)
3540 rtx insn;
3541 int luid = 0;
3543 df_grow_insn_info ();
3545 /* Scan the block an insn at a time from beginning to end. */
3546 FOR_BB_INSNS (bb, insn)
3548 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3549 /* Inserting labels does not always trigger the incremental
3550 rescanning. */
3551 if (!insn_info)
3553 gcc_assert (!INSN_P (insn));
3554 insn_info = df_insn_create_insn_record (insn);
3557 DF_INSN_INFO_LUID (insn_info) = luid;
3558 if (INSN_P (insn))
3559 luid++;
3564 /* Collect all artificial refs at the block level for BB and add them
3565 to COLLECTION_REC. */
3567 static void
3568 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3570 collection_rec->def_vec.truncate (0);
3571 collection_rec->use_vec.truncate (0);
3572 collection_rec->eq_use_vec.truncate (0);
3573 collection_rec->mw_vec.truncate (0);
3575 if (bb->index == ENTRY_BLOCK)
3577 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3578 return;
3580 else if (bb->index == EXIT_BLOCK)
3582 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3583 return;
3586 #ifdef EH_RETURN_DATA_REGNO
3587 if (bb_has_eh_pred (bb))
3589 unsigned int i;
3590 /* Mark the registers that will contain data for the handler. */
3591 for (i = 0; ; ++i)
3593 unsigned regno = EH_RETURN_DATA_REGNO (i);
3594 if (regno == INVALID_REGNUM)
3595 break;
3596 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3597 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3600 #endif
3602 /* Add the hard_frame_pointer if this block is the target of a
3603 non-local goto. */
3604 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3605 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3606 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3608 /* Add the artificial uses. */
3609 if (bb->index >= NUM_FIXED_BLOCKS)
3611 bitmap_iterator bi;
3612 unsigned int regno;
3613 bitmap au = bb_has_eh_pred (bb)
3614 ? &df->eh_block_artificial_uses
3615 : &df->regular_block_artificial_uses;
3617 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3619 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3620 bb, NULL, DF_REF_REG_USE, 0);
3624 df_canonize_collection_rec (collection_rec);
3628 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3630 void
3631 df_bb_refs_record (int bb_index, bool scan_insns)
3633 basic_block bb = BASIC_BLOCK (bb_index);
3634 rtx insn;
3635 int luid = 0;
3636 struct df_collection_rec collection_rec;
3638 if (!df)
3639 return;
3641 df_grow_bb_info (df_scan);
3642 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
3643 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
3644 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
3645 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
3647 if (scan_insns)
3648 /* Scan the block an insn at a time from beginning to end. */
3649 FOR_BB_INSNS (bb, insn)
3651 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3652 gcc_assert (!insn_info);
3654 insn_info = df_insn_create_insn_record (insn);
3655 if (INSN_P (insn))
3657 /* Record refs within INSN. */
3658 DF_INSN_INFO_LUID (insn_info) = luid++;
3659 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3660 df_refs_add_to_chains (&collection_rec, bb, insn);
3662 DF_INSN_INFO_LUID (insn_info) = luid;
3665 /* Other block level artificial refs */
3666 df_bb_refs_collect (&collection_rec, bb);
3667 df_refs_add_to_chains (&collection_rec, bb, NULL);
3669 collection_rec.def_vec.release ();
3670 collection_rec.use_vec.release ();
3671 collection_rec.eq_use_vec.release ();
3672 collection_rec.mw_vec.release ();
3674 /* Now that the block has been processed, set the block as dirty so
3675 LR and LIVE will get it processed. */
3676 df_set_bb_dirty (bb);
3680 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3681 block. */
3683 static void
3684 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3686 #ifdef EH_USES
3687 unsigned int i;
3688 #endif
3690 bitmap_clear (regular_block_artificial_uses);
3692 if (reload_completed)
3694 if (frame_pointer_needed)
3695 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3697 else
3698 /* Before reload, there are a few registers that must be forced
3699 live everywhere -- which might not already be the case for
3700 blocks within infinite loops. */
3702 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3704 /* Any reference to any pseudo before reload is a potential
3705 reference of the frame pointer. */
3706 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3708 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3709 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3710 #endif
3712 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3713 /* Pseudos with argument area equivalences may require
3714 reloading via the argument pointer. */
3715 if (fixed_regs[ARG_POINTER_REGNUM])
3716 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3717 #endif
3719 /* Any constant, or pseudo with constant equivalences, may
3720 require reloading from memory using the pic register. */
3721 if (picreg != INVALID_REGNUM
3722 && fixed_regs[picreg])
3723 bitmap_set_bit (regular_block_artificial_uses, picreg);
3725 /* The all-important stack pointer must always be live. */
3726 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3728 #ifdef EH_USES
3729 /* EH_USES registers are used:
3730 1) at all insns that might throw (calls or with -fnon-call-exceptions
3731 trapping insns)
3732 2) in all EH edges
3733 3) to support backtraces and/or debugging, anywhere between their
3734 initialization and where they the saved registers are restored
3735 from them, including the cases where we don't reach the epilogue
3736 (noreturn call or infinite loop). */
3737 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3738 if (EH_USES (i))
3739 bitmap_set_bit (regular_block_artificial_uses, i);
3740 #endif
3744 /* Get the artificial use set for an eh block. */
3746 static void
3747 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3749 bitmap_clear (eh_block_artificial_uses);
3751 /* The following code (down through the arg_pointer setting APPEARS
3752 to be necessary because there is nothing that actually
3753 describes what the exception handling code may actually need
3754 to keep alive. */
3755 if (reload_completed)
3757 if (frame_pointer_needed)
3759 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3760 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3761 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3762 #endif
3764 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3765 if (fixed_regs[ARG_POINTER_REGNUM])
3766 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3767 #endif
3773 /*----------------------------------------------------------------------------
3774 Specialized hard register scanning functions.
3775 ----------------------------------------------------------------------------*/
3778 /* Mark a register in SET. Hard registers in large modes get all
3779 of their component registers set as well. */
3781 static void
3782 df_mark_reg (rtx reg, void *vset)
3784 bitmap set = (bitmap) vset;
3785 int regno = REGNO (reg);
3787 gcc_assert (GET_MODE (reg) != BLKmode);
3789 if (regno < FIRST_PSEUDO_REGISTER)
3791 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3792 bitmap_set_range (set, regno, n);
3794 else
3795 bitmap_set_bit (set, regno);
3799 /* Set the bit for regs that are considered being defined at the entry. */
3801 static void
3802 df_get_entry_block_def_set (bitmap entry_block_defs)
3804 rtx r;
3805 int i;
3807 bitmap_clear (entry_block_defs);
3809 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3811 if (global_regs[i])
3812 bitmap_set_bit (entry_block_defs, i);
3813 if (FUNCTION_ARG_REGNO_P (i))
3814 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3817 /* The always important stack pointer. */
3818 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3820 /* Once the prologue has been generated, all of these registers
3821 should just show up in the first regular block. */
3822 if (HAVE_prologue && epilogue_completed)
3824 /* Defs for the callee saved registers are inserted so that the
3825 pushes have some defining location. */
3826 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3827 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3828 bitmap_set_bit (entry_block_defs, i);
3831 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3832 if (r && REG_P (r))
3833 bitmap_set_bit (entry_block_defs, REGNO (r));
3835 /* If the function has an incoming STATIC_CHAIN, it has to show up
3836 in the entry def set. */
3837 r = targetm.calls.static_chain (current_function_decl, true);
3838 if (r && REG_P (r))
3839 bitmap_set_bit (entry_block_defs, REGNO (r));
3841 if ((!reload_completed) || frame_pointer_needed)
3843 /* Any reference to any pseudo before reload is a potential
3844 reference of the frame pointer. */
3845 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3846 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3847 /* If they are different, also mark the hard frame pointer as live. */
3848 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3849 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3850 #endif
3853 /* These registers are live everywhere. */
3854 if (!reload_completed)
3856 #ifdef PIC_OFFSET_TABLE_REGNUM
3857 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3858 #endif
3860 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3861 /* Pseudos with argument area equivalences may require
3862 reloading via the argument pointer. */
3863 if (fixed_regs[ARG_POINTER_REGNUM])
3864 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3865 #endif
3867 #ifdef PIC_OFFSET_TABLE_REGNUM
3868 /* Any constant, or pseudo with constant equivalences, may
3869 require reloading from memory using the pic register. */
3870 if (picreg != INVALID_REGNUM
3871 && fixed_regs[picreg])
3872 bitmap_set_bit (entry_block_defs, picreg);
3873 #endif
3876 #ifdef INCOMING_RETURN_ADDR_RTX
3877 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3878 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3879 #endif
3881 targetm.extra_live_on_entry (entry_block_defs);
3885 /* Return the (conservative) set of hard registers that are defined on
3886 entry to the function.
3887 It uses df->entry_block_defs to determine which register
3888 reference to include. */
3890 static void
3891 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3892 bitmap entry_block_defs)
3894 unsigned int i;
3895 bitmap_iterator bi;
3897 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3899 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3900 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0);
3903 df_canonize_collection_rec (collection_rec);
3907 /* Record the (conservative) set of hard registers that are defined on
3908 entry to the function. */
3910 static void
3911 df_record_entry_block_defs (bitmap entry_block_defs)
3913 struct df_collection_rec collection_rec;
3914 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3915 vec_stack_alloc (df_ref, collection_rec.def_vec, FIRST_PSEUDO_REGISTER);
3916 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3918 /* Process bb_refs chain */
3919 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3920 collection_rec.def_vec.release ();
3924 /* Update the defs in the entry block. */
3926 void
3927 df_update_entry_block_defs (void)
3929 bitmap_head refs;
3930 bool changed = false;
3932 bitmap_initialize (&refs, &df_bitmap_obstack);
3933 df_get_entry_block_def_set (&refs);
3934 if (df->entry_block_defs)
3936 if (!bitmap_equal_p (df->entry_block_defs, &refs))
3938 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3939 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3940 df_ref_chain_delete (bb_info->artificial_defs);
3941 bb_info->artificial_defs = NULL;
3942 changed = true;
3945 else
3947 struct df_scan_problem_data *problem_data
3948 = (struct df_scan_problem_data *) df_scan->problem_data;
3949 gcc_unreachable ();
3950 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3951 changed = true;
3954 if (changed)
3956 df_record_entry_block_defs (&refs);
3957 bitmap_copy (df->entry_block_defs, &refs);
3958 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
3960 bitmap_clear (&refs);
3964 /* Set the bit for regs that are considered being used at the exit. */
3966 static void
3967 df_get_exit_block_use_set (bitmap exit_block_uses)
3969 unsigned int i;
3970 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3972 bitmap_clear (exit_block_uses);
3974 /* Stack pointer is always live at the exit. */
3975 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
3977 /* Mark the frame pointer if needed at the end of the function.
3978 If we end up eliminating it, it will be removed from the live
3979 list of each basic block by reload. */
3981 if ((!reload_completed) || frame_pointer_needed)
3983 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
3984 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3985 /* If they are different, also mark the hard frame pointer as live. */
3986 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3987 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
3988 #endif
3991 /* Many architectures have a GP register even without flag_pic.
3992 Assume the pic register is not in use, or will be handled by
3993 other means, if it is not fixed. */
3994 if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
3995 && picreg != INVALID_REGNUM
3996 && fixed_regs[picreg])
3997 bitmap_set_bit (exit_block_uses, picreg);
3999 /* Mark all global registers, and all registers used by the
4000 epilogue as being live at the end of the function since they
4001 may be referenced by our caller. */
4002 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4003 if (global_regs[i] || EPILOGUE_USES (i))
4004 bitmap_set_bit (exit_block_uses, i);
4006 if (HAVE_epilogue && epilogue_completed)
4008 /* Mark all call-saved registers that we actually used. */
4009 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4010 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
4011 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
4012 bitmap_set_bit (exit_block_uses, i);
4015 #ifdef EH_RETURN_DATA_REGNO
4016 /* Mark the registers that will contain data for the handler. */
4017 if (reload_completed && crtl->calls_eh_return)
4018 for (i = 0; ; ++i)
4020 unsigned regno = EH_RETURN_DATA_REGNO (i);
4021 if (regno == INVALID_REGNUM)
4022 break;
4023 bitmap_set_bit (exit_block_uses, regno);
4025 #endif
4027 #ifdef EH_RETURN_STACKADJ_RTX
4028 if ((!HAVE_epilogue || ! epilogue_completed)
4029 && crtl->calls_eh_return)
4031 rtx tmp = EH_RETURN_STACKADJ_RTX;
4032 if (tmp && REG_P (tmp))
4033 df_mark_reg (tmp, exit_block_uses);
4035 #endif
4037 #ifdef EH_RETURN_HANDLER_RTX
4038 if ((!HAVE_epilogue || ! epilogue_completed)
4039 && crtl->calls_eh_return)
4041 rtx tmp = EH_RETURN_HANDLER_RTX;
4042 if (tmp && REG_P (tmp))
4043 df_mark_reg (tmp, exit_block_uses);
4045 #endif
4047 /* Mark function return value. */
4048 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4052 /* Return the refs of hard registers that are used in the exit block.
4053 It uses df->exit_block_uses to determine register to include. */
4055 static void
4056 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4058 unsigned int i;
4059 bitmap_iterator bi;
4061 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4062 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4063 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4065 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4066 /* It is deliberate that this is not put in the exit block uses but
4067 I do not know why. */
4068 if (reload_completed
4069 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4070 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4071 && fixed_regs[ARG_POINTER_REGNUM])
4072 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4073 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4074 #endif
4076 df_canonize_collection_rec (collection_rec);
4080 /* Record the set of hard registers that are used in the exit block.
4081 It uses df->exit_block_uses to determine which bit to include. */
4083 static void
4084 df_record_exit_block_uses (bitmap exit_block_uses)
4086 struct df_collection_rec collection_rec;
4087 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4088 vec_stack_alloc (df_ref, collection_rec.use_vec, FIRST_PSEUDO_REGISTER);
4089 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4091 /* Process bb_refs chain */
4092 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4093 collection_rec.use_vec.release ();
4097 /* Update the uses in the exit block. */
4099 void
4100 df_update_exit_block_uses (void)
4102 bitmap_head refs;
4103 bool changed = false;
4105 bitmap_initialize (&refs, &df_bitmap_obstack);
4106 df_get_exit_block_use_set (&refs);
4107 if (df->exit_block_uses)
4109 if (!bitmap_equal_p (df->exit_block_uses, &refs))
4111 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4112 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4113 df_ref_chain_delete (bb_info->artificial_uses);
4114 bb_info->artificial_uses = NULL;
4115 changed = true;
4118 else
4120 struct df_scan_problem_data *problem_data
4121 = (struct df_scan_problem_data *) df_scan->problem_data;
4122 gcc_unreachable ();
4123 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4124 changed = true;
4127 if (changed)
4129 df_record_exit_block_uses (&refs);
4130 bitmap_copy (df->exit_block_uses,& refs);
4131 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4133 bitmap_clear (&refs);
4136 static bool initialized = false;
4139 /* Initialize some platform specific structures. */
4141 void
4142 df_hard_reg_init (void)
4144 #ifdef ELIMINABLE_REGS
4145 int i;
4146 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4147 #endif
4148 if (initialized)
4149 return;
4151 /* Record which registers will be eliminated. We use this in
4152 mark_used_regs. */
4153 CLEAR_HARD_REG_SET (elim_reg_set);
4155 #ifdef ELIMINABLE_REGS
4156 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4157 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4158 #else
4159 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4160 #endif
4162 initialized = true;
4166 /* Recompute the parts of scanning that are based on regs_ever_live
4167 because something changed in that array. */
4169 void
4170 df_update_entry_exit_and_calls (void)
4172 basic_block bb;
4174 df_update_entry_block_defs ();
4175 df_update_exit_block_uses ();
4177 /* The call insns need to be rescanned because there may be changes
4178 in the set of registers clobbered across the call. */
4179 FOR_EACH_BB (bb)
4181 rtx insn;
4182 FOR_BB_INSNS (bb, insn)
4184 if (INSN_P (insn) && CALL_P (insn))
4185 df_insn_rescan (insn);
4191 /* Return true if hard REG is actually used in the some instruction.
4192 There are a fair number of conditions that affect the setting of
4193 this array. See the comment in df.h for df->hard_regs_live_count
4194 for the conditions that this array is set. */
4196 bool
4197 df_hard_reg_used_p (unsigned int reg)
4199 return df->hard_regs_live_count[reg] != 0;
4203 /* A count of the number of times REG is actually used in the some
4204 instruction. There are a fair number of conditions that affect the
4205 setting of this array. See the comment in df.h for
4206 df->hard_regs_live_count for the conditions that this array is
4207 set. */
4210 unsigned int
4211 df_hard_reg_used_count (unsigned int reg)
4213 return df->hard_regs_live_count[reg];
4217 /* Get the value of regs_ever_live[REGNO]. */
4219 bool
4220 df_regs_ever_live_p (unsigned int regno)
4222 return regs_ever_live[regno];
4226 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4227 to change, schedule that change for the next update. */
4229 void
4230 df_set_regs_ever_live (unsigned int regno, bool value)
4232 if (regs_ever_live[regno] == value)
4233 return;
4235 regs_ever_live[regno] = value;
4236 if (df)
4237 df->redo_entry_and_exit = true;
4241 /* Compute "regs_ever_live" information from the underlying df
4242 information. Set the vector to all false if RESET. */
4244 void
4245 df_compute_regs_ever_live (bool reset)
4247 unsigned int i;
4248 bool changed = df->redo_entry_and_exit;
4250 if (reset)
4251 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4253 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4254 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4256 regs_ever_live[i] = true;
4257 changed = true;
4259 if (changed)
4260 df_update_entry_exit_and_calls ();
4261 df->redo_entry_and_exit = false;
4265 /*----------------------------------------------------------------------------
4266 Dataflow ref information verification functions.
4268 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4269 df_reg_chain_verify_unmarked (refs)
4270 df_refs_verify (vec<stack, va_df_ref>, ref*, bool)
4271 df_mws_verify (mw*, mw*, bool)
4272 df_insn_refs_verify (collection_rec, bb, insn, bool)
4273 df_bb_refs_verify (bb, refs, bool)
4274 df_bb_verify (bb)
4275 df_exit_block_bitmap_verify (bool)
4276 df_entry_block_bitmap_verify (bool)
4277 df_scan_verify ()
4278 ----------------------------------------------------------------------------*/
4281 /* Mark all refs in the reg chain. Verify that all of the registers
4282 are in the correct chain. */
4284 static unsigned int
4285 df_reg_chain_mark (df_ref refs, unsigned int regno,
4286 bool is_def, bool is_eq_use)
4288 unsigned int count = 0;
4289 df_ref ref;
4290 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4292 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4294 /* If there are no def-use or use-def chains, make sure that all
4295 of the chains are clear. */
4296 if (!df_chain)
4297 gcc_assert (!DF_REF_CHAIN (ref));
4299 /* Check to make sure the ref is in the correct chain. */
4300 gcc_assert (DF_REF_REGNO (ref) == regno);
4301 if (is_def)
4302 gcc_assert (DF_REF_REG_DEF_P (ref));
4303 else
4304 gcc_assert (!DF_REF_REG_DEF_P (ref));
4306 if (is_eq_use)
4307 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4308 else
4309 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4311 if (DF_REF_NEXT_REG (ref))
4312 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4313 count++;
4314 DF_REF_REG_MARK (ref);
4316 return count;
4320 /* Verify that all of the registers in the chain are unmarked. */
4322 static void
4323 df_reg_chain_verify_unmarked (df_ref refs)
4325 df_ref ref;
4326 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4327 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4331 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4333 static bool
4334 df_refs_verify (vec<df_ref, va_stack> new_rec, df_ref *old_rec,
4335 bool abort_if_fail)
4337 unsigned int ix;
4338 df_ref new_ref;
4340 FOR_EACH_VEC_ELT (new_rec, ix, new_ref)
4342 if (*old_rec == NULL || !df_ref_equal_p (new_ref, *old_rec))
4344 if (abort_if_fail)
4345 gcc_assert (0);
4346 else
4347 return false;
4350 /* Abort if fail is called from the function level verifier. If
4351 that is the context, mark this reg as being seem. */
4352 if (abort_if_fail)
4354 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4355 DF_REF_REG_UNMARK (*old_rec);
4358 old_rec++;
4361 if (abort_if_fail)
4362 gcc_assert (*old_rec == NULL);
4363 else
4364 return *old_rec == NULL;
4365 return false;
4369 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4371 static bool
4372 df_mws_verify (vec<df_mw_hardreg_ptr, va_stack> new_rec,
4373 struct df_mw_hardreg **old_rec,
4374 bool abort_if_fail)
4376 unsigned int ix;
4377 struct df_mw_hardreg *new_reg;
4379 FOR_EACH_VEC_ELT (new_rec, ix, new_reg)
4381 if (*old_rec == NULL || !df_mw_equal_p (new_reg, *old_rec))
4383 if (abort_if_fail)
4384 gcc_assert (0);
4385 else
4386 return false;
4388 old_rec++;
4391 if (abort_if_fail)
4392 gcc_assert (*old_rec == NULL);
4393 else
4394 return *old_rec == NULL;
4395 return false;
4399 /* Return true if the existing insn refs information is complete and
4400 correct. Otherwise (i.e. if there's any missing or extra refs),
4401 return the correct df_ref chain in REFS_RETURN.
4403 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4404 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4405 verification mode instead of the whole function, so unmark
4406 everything.
4408 If ABORT_IF_FAIL is set, this function never returns false. */
4410 static bool
4411 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4412 basic_block bb,
4413 rtx insn,
4414 bool abort_if_fail)
4416 bool ret1, ret2, ret3, ret4;
4417 unsigned int uid = INSN_UID (insn);
4418 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4420 df_insn_refs_collect (collection_rec, bb, insn_info);
4422 if (!DF_INSN_UID_DEFS (uid))
4424 /* The insn_rec was created but it was never filled out. */
4425 if (abort_if_fail)
4426 gcc_assert (0);
4427 else
4428 return false;
4431 /* Unfortunately we cannot opt out early if one of these is not
4432 right because the marks will not get cleared. */
4433 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4434 abort_if_fail);
4435 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4436 abort_if_fail);
4437 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4438 abort_if_fail);
4439 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4440 abort_if_fail);
4441 return (ret1 && ret2 && ret3 && ret4);
4445 /* Return true if all refs in the basic block are correct and complete.
4446 Due to df_ref_chain_verify, it will cause all refs
4447 that are verified to have DF_REF_MARK bit set. */
4449 static bool
4450 df_bb_verify (basic_block bb)
4452 rtx insn;
4453 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4454 struct df_collection_rec collection_rec;
4456 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4457 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
4458 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
4459 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
4460 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
4462 gcc_assert (bb_info);
4464 /* Scan the block, one insn at a time, from beginning to end. */
4465 FOR_BB_INSNS_REVERSE (bb, insn)
4467 if (!INSN_P (insn))
4468 continue;
4469 df_insn_refs_verify (&collection_rec, bb, insn, true);
4470 df_free_collection_rec (&collection_rec);
4473 /* Do the artificial defs and uses. */
4474 df_bb_refs_collect (&collection_rec, bb);
4475 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4476 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4477 df_free_collection_rec (&collection_rec);
4479 return true;
4483 /* Returns true if the entry block has correct and complete df_ref set.
4484 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4486 static bool
4487 df_entry_block_bitmap_verify (bool abort_if_fail)
4489 bitmap_head entry_block_defs;
4490 bool is_eq;
4492 bitmap_initialize (&entry_block_defs, &df_bitmap_obstack);
4493 df_get_entry_block_def_set (&entry_block_defs);
4495 is_eq = bitmap_equal_p (&entry_block_defs, df->entry_block_defs);
4497 if (!is_eq && abort_if_fail)
4499 fprintf (stderr, "entry_block_defs = ");
4500 df_print_regset (stderr, &entry_block_defs);
4501 fprintf (stderr, "df->entry_block_defs = ");
4502 df_print_regset (stderr, df->entry_block_defs);
4503 gcc_assert (0);
4506 bitmap_clear (&entry_block_defs);
4508 return is_eq;
4512 /* Returns true if the exit block has correct and complete df_ref set.
4513 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4515 static bool
4516 df_exit_block_bitmap_verify (bool abort_if_fail)
4518 bitmap_head exit_block_uses;
4519 bool is_eq;
4521 bitmap_initialize (&exit_block_uses, &df_bitmap_obstack);
4522 df_get_exit_block_use_set (&exit_block_uses);
4524 is_eq = bitmap_equal_p (&exit_block_uses, df->exit_block_uses);
4526 if (!is_eq && abort_if_fail)
4528 fprintf (stderr, "exit_block_uses = ");
4529 df_print_regset (stderr, &exit_block_uses);
4530 fprintf (stderr, "df->exit_block_uses = ");
4531 df_print_regset (stderr, df->exit_block_uses);
4532 gcc_assert (0);
4535 bitmap_clear (&exit_block_uses);
4537 return is_eq;
4541 /* Return true if df_ref information for all insns in all blocks are
4542 correct and complete. */
4544 void
4545 df_scan_verify (void)
4547 unsigned int i;
4548 basic_block bb;
4549 bitmap_head regular_block_artificial_uses;
4550 bitmap_head eh_block_artificial_uses;
4552 if (!df)
4553 return;
4555 /* Verification is a 4 step process. */
4557 /* (1) All of the refs are marked by going through the reg chains. */
4558 for (i = 0; i < DF_REG_SIZE (df); i++)
4560 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4561 == DF_REG_DEF_COUNT(i));
4562 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4563 == DF_REG_USE_COUNT(i));
4564 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4565 == DF_REG_EQ_USE_COUNT(i));
4568 /* (2) There are various bitmaps whose value may change over the
4569 course of the compilation. This step recomputes them to make
4570 sure that they have not slipped out of date. */
4571 bitmap_initialize (&regular_block_artificial_uses, &df_bitmap_obstack);
4572 bitmap_initialize (&eh_block_artificial_uses, &df_bitmap_obstack);
4574 df_get_regular_block_artificial_uses (&regular_block_artificial_uses);
4575 df_get_eh_block_artificial_uses (&eh_block_artificial_uses);
4577 bitmap_ior_into (&eh_block_artificial_uses,
4578 &regular_block_artificial_uses);
4580 /* Check artificial_uses bitmaps didn't change. */
4581 gcc_assert (bitmap_equal_p (&regular_block_artificial_uses,
4582 &df->regular_block_artificial_uses));
4583 gcc_assert (bitmap_equal_p (&eh_block_artificial_uses,
4584 &df->eh_block_artificial_uses));
4586 bitmap_clear (&regular_block_artificial_uses);
4587 bitmap_clear (&eh_block_artificial_uses);
4589 /* Verify entry block and exit block. These only verify the bitmaps,
4590 the refs are verified in df_bb_verify. */
4591 df_entry_block_bitmap_verify (true);
4592 df_exit_block_bitmap_verify (true);
4594 /* (3) All of the insns in all of the blocks are traversed and the
4595 marks are cleared both in the artificial refs attached to the
4596 blocks and the real refs inside the insns. It is a failure to
4597 clear a mark that has not been set as this means that the ref in
4598 the block or insn was not in the reg chain. */
4600 FOR_ALL_BB (bb)
4601 df_bb_verify (bb);
4603 /* (4) See if all reg chains are traversed a second time. This time
4604 a check is made that the marks are clear. A set mark would be a
4605 from a reg that is not in any insn or basic block. */
4607 for (i = 0; i < DF_REG_SIZE (df); i++)
4609 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4610 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4611 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));