re PR tree-optimization/37021 (Fortran Complex reduction / multiplication not vectorized)
[official-gcc.git] / gcc / df-scan.c
blobfdfa93190ce0c77f1a858fab83725f81b4cddb59
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... */
1162 gcc_checking_assert (bb != NULL || reload_completed);
1164 df_grow_bb_info (df_scan);
1165 df_grow_reg_info ();
1167 /* The block must be marked as dirty now, rather than later as in
1168 df_insn_rescan and df_notes_rescan because it may not be there at
1169 rescanning time and the mark would blow up.
1170 DEBUG_INSNs do not make a block's data flow solution dirty (at
1171 worst the LUIDs are no longer contiguous). */
1172 if (bb != NULL && NONDEBUG_INSN_P (insn))
1173 df_set_bb_dirty (bb);
1175 /* The client has deferred rescanning. */
1176 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1178 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1179 if (insn_info)
1181 bitmap_clear_bit (&df->insns_to_rescan, uid);
1182 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1183 bitmap_set_bit (&df->insns_to_delete, uid);
1185 if (dump_file)
1186 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1187 return;
1190 if (dump_file)
1191 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1193 df_insn_info_delete (uid);
1197 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1199 static void
1200 df_free_collection_rec (struct df_collection_rec *collection_rec)
1202 unsigned int ix;
1203 struct df_scan_problem_data *problem_data
1204 = (struct df_scan_problem_data *) df_scan->problem_data;
1205 df_ref ref;
1206 struct df_mw_hardreg *mw;
1208 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
1209 df_free_ref (ref);
1210 FOR_EACH_VEC_ELT (collection_rec->use_vec, ix, ref)
1211 df_free_ref (ref);
1212 FOR_EACH_VEC_ELT (collection_rec->eq_use_vec, ix, ref)
1213 df_free_ref (ref);
1214 FOR_EACH_VEC_ELT (collection_rec->mw_vec, ix, mw)
1215 pool_free (problem_data->mw_reg_pool, mw);
1217 collection_rec->def_vec.release ();
1218 collection_rec->use_vec.release ();
1219 collection_rec->eq_use_vec.release ();
1220 collection_rec->mw_vec.release ();
1223 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1225 bool
1226 df_insn_rescan (rtx insn)
1228 unsigned int uid = INSN_UID (insn);
1229 struct df_insn_info *insn_info = NULL;
1230 basic_block bb = BLOCK_FOR_INSN (insn);
1231 struct df_collection_rec collection_rec;
1233 if ((!df) || (!INSN_P (insn)))
1234 return false;
1236 if (!bb)
1238 if (dump_file)
1239 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1240 return false;
1243 /* The client has disabled rescanning and plans to do it itself. */
1244 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1245 return false;
1247 df_grow_bb_info (df_scan);
1248 df_grow_reg_info ();
1250 insn_info = DF_INSN_UID_SAFE_GET (uid);
1252 /* The client has deferred rescanning. */
1253 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1255 if (!insn_info)
1257 insn_info = df_insn_create_insn_record (insn);
1258 insn_info->defs = df_null_ref_rec;
1259 insn_info->uses = df_null_ref_rec;
1260 insn_info->eq_uses = df_null_ref_rec;
1261 insn_info->mw_hardregs = df_null_mw_rec;
1263 if (dump_file)
1264 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1266 bitmap_clear_bit (&df->insns_to_delete, uid);
1267 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1268 bitmap_set_bit (&df->insns_to_rescan, INSN_UID (insn));
1269 return false;
1272 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
1273 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
1274 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
1275 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
1277 bitmap_clear_bit (&df->insns_to_delete, uid);
1278 bitmap_clear_bit (&df->insns_to_rescan, uid);
1279 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1280 if (insn_info)
1282 int luid;
1283 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1284 /* If there's no change, return false. */
1285 if (the_same)
1287 df_free_collection_rec (&collection_rec);
1288 if (dump_file)
1289 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1290 return false;
1292 if (dump_file)
1293 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1295 /* There's change - we need to delete the existing info.
1296 Since the insn isn't moved, we can salvage its LUID. */
1297 luid = DF_INSN_LUID (insn);
1298 df_insn_info_delete (uid);
1299 df_insn_create_insn_record (insn);
1300 DF_INSN_LUID (insn) = luid;
1302 else
1304 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1305 df_insn_refs_collect (&collection_rec, bb, insn_info);
1306 if (dump_file)
1307 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1310 df_refs_add_to_chains (&collection_rec, bb, insn);
1311 if (!DEBUG_INSN_P (insn))
1312 df_set_bb_dirty (bb);
1314 collection_rec.def_vec.release ();
1315 collection_rec.use_vec.release ();
1316 collection_rec.eq_use_vec.release ();
1317 collection_rec.mw_vec.release ();
1319 return true;
1322 /* Same as df_insn_rescan, but don't mark the basic block as
1323 dirty. */
1325 bool
1326 df_insn_rescan_debug_internal (rtx insn)
1328 unsigned int uid = INSN_UID (insn);
1329 struct df_insn_info *insn_info;
1331 gcc_assert (DEBUG_INSN_P (insn)
1332 && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1334 if (!df)
1335 return false;
1337 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1338 if (!insn_info)
1339 return false;
1341 if (dump_file)
1342 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1344 bitmap_clear_bit (&df->insns_to_delete, uid);
1345 bitmap_clear_bit (&df->insns_to_rescan, uid);
1346 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1348 if (!insn_info->defs)
1349 return false;
1351 if (insn_info->defs == df_null_ref_rec
1352 && insn_info->uses == df_null_ref_rec
1353 && insn_info->eq_uses == df_null_ref_rec
1354 && insn_info->mw_hardregs == df_null_mw_rec)
1355 return false;
1357 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1359 if (df_chain)
1361 df_ref_chain_delete_du_chain (insn_info->defs);
1362 df_ref_chain_delete_du_chain (insn_info->uses);
1363 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1366 df_ref_chain_delete (insn_info->defs);
1367 df_ref_chain_delete (insn_info->uses);
1368 df_ref_chain_delete (insn_info->eq_uses);
1369 df_scan_free_mws_vec (insn_info->mw_hardregs);
1371 insn_info->defs = df_null_ref_rec;
1372 insn_info->uses = df_null_ref_rec;
1373 insn_info->eq_uses = df_null_ref_rec;
1374 insn_info->mw_hardregs = df_null_mw_rec;
1376 return true;
1380 /* Rescan all of the insns in the function. Note that the artificial
1381 uses and defs are not touched. This function will destroy def-use
1382 or use-def chains. */
1384 void
1385 df_insn_rescan_all (void)
1387 bool no_insn_rescan = false;
1388 bool defer_insn_rescan = false;
1389 basic_block bb;
1390 bitmap_iterator bi;
1391 unsigned int uid;
1392 bitmap_head tmp;
1394 bitmap_initialize (&tmp, &df_bitmap_obstack);
1396 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1398 df_clear_flags (DF_NO_INSN_RESCAN);
1399 no_insn_rescan = true;
1402 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1404 df_clear_flags (DF_DEFER_INSN_RESCAN);
1405 defer_insn_rescan = true;
1408 bitmap_copy (&tmp, &df->insns_to_delete);
1409 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1411 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1412 if (insn_info)
1413 df_insn_info_delete (uid);
1416 bitmap_clear (&tmp);
1417 bitmap_clear (&df->insns_to_delete);
1418 bitmap_clear (&df->insns_to_rescan);
1419 bitmap_clear (&df->insns_to_notes_rescan);
1421 FOR_EACH_BB (bb)
1423 rtx insn;
1424 FOR_BB_INSNS (bb, insn)
1426 df_insn_rescan (insn);
1430 if (no_insn_rescan)
1431 df_set_flags (DF_NO_INSN_RESCAN);
1432 if (defer_insn_rescan)
1433 df_set_flags (DF_DEFER_INSN_RESCAN);
1437 /* Process all of the deferred rescans or deletions. */
1439 void
1440 df_process_deferred_rescans (void)
1442 bool no_insn_rescan = false;
1443 bool defer_insn_rescan = false;
1444 bitmap_iterator bi;
1445 unsigned int uid;
1446 bitmap_head tmp;
1448 bitmap_initialize (&tmp, &df_bitmap_obstack);
1450 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1452 df_clear_flags (DF_NO_INSN_RESCAN);
1453 no_insn_rescan = true;
1456 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1458 df_clear_flags (DF_DEFER_INSN_RESCAN);
1459 defer_insn_rescan = true;
1462 if (dump_file)
1463 fprintf (dump_file, "starting the processing of deferred insns\n");
1465 bitmap_copy (&tmp, &df->insns_to_delete);
1466 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1468 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1469 if (insn_info)
1470 df_insn_info_delete (uid);
1473 bitmap_copy (&tmp, &df->insns_to_rescan);
1474 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1476 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1477 if (insn_info)
1478 df_insn_rescan (insn_info->insn);
1481 bitmap_copy (&tmp, &df->insns_to_notes_rescan);
1482 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1484 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1485 if (insn_info)
1486 df_notes_rescan (insn_info->insn);
1489 if (dump_file)
1490 fprintf (dump_file, "ending the processing of deferred insns\n");
1492 bitmap_clear (&tmp);
1493 bitmap_clear (&df->insns_to_delete);
1494 bitmap_clear (&df->insns_to_rescan);
1495 bitmap_clear (&df->insns_to_notes_rescan);
1497 if (no_insn_rescan)
1498 df_set_flags (DF_NO_INSN_RESCAN);
1499 if (defer_insn_rescan)
1500 df_set_flags (DF_DEFER_INSN_RESCAN);
1502 /* If someone changed regs_ever_live during this pass, fix up the
1503 entry and exit blocks. */
1504 if (df->redo_entry_and_exit)
1506 df_update_entry_exit_and_calls ();
1507 df->redo_entry_and_exit = false;
1512 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1513 the uses if INCLUDE_USES. Include the eq_uses if
1514 INCLUDE_EQ_USES. */
1516 static unsigned int
1517 df_count_refs (bool include_defs, bool include_uses,
1518 bool include_eq_uses)
1520 unsigned int regno;
1521 int size = 0;
1522 unsigned int m = df->regs_inited;
1524 for (regno = 0; regno < m; regno++)
1526 if (include_defs)
1527 size += DF_REG_DEF_COUNT (regno);
1528 if (include_uses)
1529 size += DF_REG_USE_COUNT (regno);
1530 if (include_eq_uses)
1531 size += DF_REG_EQ_USE_COUNT (regno);
1533 return size;
1537 /* Take build ref table for either the uses or defs from the reg-use
1538 or reg-def chains. This version processes the refs in reg order
1539 which is likely to be best if processing the whole function. */
1541 static void
1542 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1543 bool include_defs,
1544 bool include_uses,
1545 bool include_eq_uses)
1547 unsigned int m = df->regs_inited;
1548 unsigned int regno;
1549 unsigned int offset = 0;
1550 unsigned int start;
1552 if (df->changeable_flags & DF_NO_HARD_REGS)
1554 start = FIRST_PSEUDO_REGISTER;
1555 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1556 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1558 else
1559 start = 0;
1561 ref_info->total_size
1562 = df_count_refs (include_defs, include_uses, include_eq_uses);
1564 df_check_and_grow_ref_info (ref_info, 1);
1566 for (regno = start; regno < m; regno++)
1568 int count = 0;
1569 ref_info->begin[regno] = offset;
1570 if (include_defs)
1572 df_ref ref = DF_REG_DEF_CHAIN (regno);
1573 while (ref)
1575 ref_info->refs[offset] = ref;
1576 DF_REF_ID (ref) = offset++;
1577 count++;
1578 ref = DF_REF_NEXT_REG (ref);
1579 gcc_checking_assert (offset < ref_info->refs_size);
1582 if (include_uses)
1584 df_ref ref = DF_REG_USE_CHAIN (regno);
1585 while (ref)
1587 ref_info->refs[offset] = ref;
1588 DF_REF_ID (ref) = offset++;
1589 count++;
1590 ref = DF_REF_NEXT_REG (ref);
1591 gcc_checking_assert (offset < ref_info->refs_size);
1594 if (include_eq_uses)
1596 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1597 while (ref)
1599 ref_info->refs[offset] = ref;
1600 DF_REF_ID (ref) = offset++;
1601 count++;
1602 ref = DF_REF_NEXT_REG (ref);
1603 gcc_checking_assert (offset < ref_info->refs_size);
1606 ref_info->count[regno] = count;
1609 /* The bitmap size is not decremented when refs are deleted. So
1610 reset it now that we have squished out all of the empty
1611 slots. */
1612 ref_info->table_size = offset;
1616 /* Take build ref table for either the uses or defs from the reg-use
1617 or reg-def chains. This version processes the refs in insn order
1618 which is likely to be best if processing some segment of the
1619 function. */
1621 static void
1622 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1623 bool include_defs,
1624 bool include_uses,
1625 bool include_eq_uses)
1627 bitmap_iterator bi;
1628 unsigned int bb_index;
1629 unsigned int m = df->regs_inited;
1630 unsigned int offset = 0;
1631 unsigned int r;
1632 unsigned int start
1633 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1635 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1636 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1638 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1639 df_check_and_grow_ref_info (ref_info, 1);
1641 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1643 basic_block bb = BASIC_BLOCK (bb_index);
1644 rtx insn;
1645 df_ref *ref_rec;
1647 if (include_defs)
1648 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1650 unsigned int regno = DF_REF_REGNO (*ref_rec);
1651 ref_info->count[regno]++;
1653 if (include_uses)
1654 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1656 unsigned int regno = DF_REF_REGNO (*ref_rec);
1657 ref_info->count[regno]++;
1660 FOR_BB_INSNS (bb, insn)
1662 if (INSN_P (insn))
1664 unsigned int uid = INSN_UID (insn);
1666 if (include_defs)
1667 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1669 unsigned int regno = DF_REF_REGNO (*ref_rec);
1670 ref_info->count[regno]++;
1672 if (include_uses)
1673 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1675 unsigned int regno = DF_REF_REGNO (*ref_rec);
1676 ref_info->count[regno]++;
1678 if (include_eq_uses)
1679 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1681 unsigned int regno = DF_REF_REGNO (*ref_rec);
1682 ref_info->count[regno]++;
1688 for (r = start; r < m; r++)
1690 ref_info->begin[r] = offset;
1691 offset += ref_info->count[r];
1692 ref_info->count[r] = 0;
1695 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1697 basic_block bb = BASIC_BLOCK (bb_index);
1698 rtx insn;
1699 df_ref *ref_rec;
1701 if (include_defs)
1702 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1704 df_ref ref = *ref_rec;
1705 unsigned int regno = DF_REF_REGNO (ref);
1706 if (regno >= start)
1708 unsigned int id
1709 = ref_info->begin[regno] + ref_info->count[regno]++;
1710 DF_REF_ID (ref) = id;
1711 ref_info->refs[id] = ref;
1714 if (include_uses)
1715 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1717 df_ref ref = *ref_rec;
1718 unsigned int regno = DF_REF_REGNO (ref);
1719 if (regno >= start)
1721 unsigned int id
1722 = ref_info->begin[regno] + ref_info->count[regno]++;
1723 DF_REF_ID (ref) = id;
1724 ref_info->refs[id] = ref;
1728 FOR_BB_INSNS (bb, insn)
1730 if (INSN_P (insn))
1732 unsigned int uid = INSN_UID (insn);
1734 if (include_defs)
1735 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1737 df_ref ref = *ref_rec;
1738 unsigned int regno = DF_REF_REGNO (ref);
1739 if (regno >= start)
1741 unsigned int id
1742 = ref_info->begin[regno] + ref_info->count[regno]++;
1743 DF_REF_ID (ref) = id;
1744 ref_info->refs[id] = ref;
1747 if (include_uses)
1748 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1750 df_ref ref = *ref_rec;
1751 unsigned int regno = DF_REF_REGNO (ref);
1752 if (regno >= start)
1754 unsigned int id
1755 = ref_info->begin[regno] + ref_info->count[regno]++;
1756 DF_REF_ID (ref) = id;
1757 ref_info->refs[id] = ref;
1760 if (include_eq_uses)
1761 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1763 df_ref ref = *ref_rec;
1764 unsigned int regno = DF_REF_REGNO (ref);
1765 if (regno >= start)
1767 unsigned int id
1768 = ref_info->begin[regno] + ref_info->count[regno]++;
1769 DF_REF_ID (ref) = id;
1770 ref_info->refs[id] = ref;
1777 /* The bitmap size is not decremented when refs are deleted. So
1778 reset it now that we have squished out all of the empty
1779 slots. */
1781 ref_info->table_size = offset;
1784 /* Take build ref table for either the uses or defs from the reg-use
1785 or reg-def chains. */
1787 static void
1788 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1789 bool include_defs,
1790 bool include_uses,
1791 bool include_eq_uses)
1793 if (df->analyze_subset)
1794 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1795 include_uses, include_eq_uses);
1796 else
1797 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1798 include_uses, include_eq_uses);
1802 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1803 static unsigned int
1804 df_add_refs_to_table (unsigned int offset,
1805 struct df_ref_info *ref_info,
1806 df_ref *ref_vec)
1808 while (*ref_vec)
1810 df_ref ref = *ref_vec;
1811 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1812 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1814 ref_info->refs[offset] = ref;
1815 DF_REF_ID (*ref_vec) = offset++;
1817 ref_vec++;
1819 return offset;
1823 /* Count the number of refs in all of the insns of BB. Include the
1824 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1825 eq_uses if INCLUDE_EQ_USES. */
1827 static unsigned int
1828 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1829 struct df_ref_info *ref_info,
1830 bool include_defs, bool include_uses,
1831 bool include_eq_uses)
1833 rtx insn;
1835 if (include_defs)
1836 offset = df_add_refs_to_table (offset, ref_info,
1837 df_get_artificial_defs (bb->index));
1838 if (include_uses)
1839 offset = df_add_refs_to_table (offset, ref_info,
1840 df_get_artificial_uses (bb->index));
1842 FOR_BB_INSNS (bb, insn)
1843 if (INSN_P (insn))
1845 unsigned int uid = INSN_UID (insn);
1846 if (include_defs)
1847 offset = df_add_refs_to_table (offset, ref_info,
1848 DF_INSN_UID_DEFS (uid));
1849 if (include_uses)
1850 offset = df_add_refs_to_table (offset, ref_info,
1851 DF_INSN_UID_USES (uid));
1852 if (include_eq_uses)
1853 offset = df_add_refs_to_table (offset, ref_info,
1854 DF_INSN_UID_EQ_USES (uid));
1856 return offset;
1860 /* Organize the refs by insn into the table in REF_INFO. If
1861 blocks_to_analyze is defined, use that set, otherwise the entire
1862 program. Include the defs if INCLUDE_DEFS. Include the uses if
1863 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1865 static void
1866 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1867 bool include_defs, bool include_uses,
1868 bool include_eq_uses)
1870 basic_block bb;
1871 unsigned int offset = 0;
1873 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1874 df_check_and_grow_ref_info (ref_info, 1);
1875 if (df->blocks_to_analyze)
1877 bitmap_iterator bi;
1878 unsigned int index;
1880 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1882 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1883 include_defs, include_uses,
1884 include_eq_uses);
1887 ref_info->table_size = offset;
1889 else
1891 FOR_ALL_BB (bb)
1892 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1893 include_defs, include_uses,
1894 include_eq_uses);
1895 ref_info->table_size = offset;
1900 /* If the use refs in DF are not organized, reorganize them. */
1902 void
1903 df_maybe_reorganize_use_refs (enum df_ref_order order)
1905 if (order == df->use_info.ref_order)
1906 return;
1908 switch (order)
1910 case DF_REF_ORDER_BY_REG:
1911 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1912 break;
1914 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1915 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1916 break;
1918 case DF_REF_ORDER_BY_INSN:
1919 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1920 break;
1922 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1923 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1924 break;
1926 case DF_REF_ORDER_NO_TABLE:
1927 free (df->use_info.refs);
1928 df->use_info.refs = NULL;
1929 df->use_info.refs_size = 0;
1930 break;
1932 case DF_REF_ORDER_UNORDERED:
1933 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1934 gcc_unreachable ();
1935 break;
1938 df->use_info.ref_order = order;
1942 /* If the def refs in DF are not organized, reorganize them. */
1944 void
1945 df_maybe_reorganize_def_refs (enum df_ref_order order)
1947 if (order == df->def_info.ref_order)
1948 return;
1950 switch (order)
1952 case DF_REF_ORDER_BY_REG:
1953 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1954 break;
1956 case DF_REF_ORDER_BY_INSN:
1957 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1958 break;
1960 case DF_REF_ORDER_NO_TABLE:
1961 free (df->def_info.refs);
1962 df->def_info.refs = NULL;
1963 df->def_info.refs_size = 0;
1964 break;
1966 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1967 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1968 case DF_REF_ORDER_UNORDERED:
1969 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1970 gcc_unreachable ();
1971 break;
1974 df->def_info.ref_order = order;
1978 /* Change all of the basic block references in INSN to use the insn's
1979 current basic block. This function is called from routines that move
1980 instructions from one block to another. */
1982 void
1983 df_insn_change_bb (rtx insn, basic_block new_bb)
1985 basic_block old_bb = BLOCK_FOR_INSN (insn);
1986 struct df_insn_info *insn_info;
1987 unsigned int uid = INSN_UID (insn);
1989 if (old_bb == new_bb)
1990 return;
1992 set_block_for_insn (insn, new_bb);
1994 if (!df)
1995 return;
1997 if (dump_file)
1998 fprintf (dump_file, "changing bb of uid %d\n", uid);
2000 insn_info = DF_INSN_UID_SAFE_GET (uid);
2001 if (insn_info == NULL)
2003 if (dump_file)
2004 fprintf (dump_file, " unscanned insn\n");
2005 df_insn_rescan (insn);
2006 return;
2009 if (!INSN_P (insn))
2010 return;
2012 df_set_bb_dirty (new_bb);
2013 if (old_bb)
2015 if (dump_file)
2016 fprintf (dump_file, " from %d to %d\n",
2017 old_bb->index, new_bb->index);
2018 df_set_bb_dirty (old_bb);
2020 else
2021 if (dump_file)
2022 fprintf (dump_file, " to %d\n", new_bb->index);
2026 /* Helper function for df_ref_change_reg_with_loc. */
2028 static void
2029 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
2030 struct df_reg_info *new_df,
2031 int new_regno, rtx loc)
2033 df_ref the_ref = old_df->reg_chain;
2035 while (the_ref)
2037 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
2038 && DF_REF_LOC (the_ref)
2039 && (*DF_REF_LOC (the_ref) == loc))
2041 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
2042 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
2043 df_ref *ref_vec, *ref_vec_t;
2044 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
2045 unsigned int count = 0;
2047 DF_REF_REGNO (the_ref) = new_regno;
2048 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
2050 /* Pull the_ref out of the old regno chain. */
2051 if (prev_ref)
2052 DF_REF_NEXT_REG (prev_ref) = next_ref;
2053 else
2054 old_df->reg_chain = next_ref;
2055 if (next_ref)
2056 DF_REF_PREV_REG (next_ref) = prev_ref;
2057 old_df->n_refs--;
2059 /* Put the ref into the new regno chain. */
2060 DF_REF_PREV_REG (the_ref) = NULL;
2061 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
2062 if (new_df->reg_chain)
2063 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
2064 new_df->reg_chain = the_ref;
2065 new_df->n_refs++;
2066 if (DF_REF_BB (the_ref))
2067 df_set_bb_dirty (DF_REF_BB (the_ref));
2069 /* Need to sort the record again that the ref was in because
2070 the regno is a sorting key. First, find the right
2071 record. */
2072 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
2073 ref_vec = insn_info->eq_uses;
2074 else
2075 ref_vec = insn_info->uses;
2076 if (dump_file)
2077 fprintf (dump_file, "changing reg in insn %d\n",
2078 DF_REF_INSN_UID (the_ref));
2080 ref_vec_t = ref_vec;
2082 /* Find the length. */
2083 while (*ref_vec_t)
2085 count++;
2086 ref_vec_t++;
2088 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
2090 the_ref = next_ref;
2092 else
2093 the_ref = DF_REF_NEXT_REG (the_ref);
2098 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2099 NEW_REGNO. Refs that do not match LOC are not changed which means
2100 that artificial refs are not changed since they have no loc. This
2101 call is to support the SET_REGNO macro. */
2103 void
2104 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2106 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2107 return;
2109 df_grow_reg_info ();
2111 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2112 DF_REG_DEF_GET (new_regno), new_regno, loc);
2113 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2114 DF_REG_USE_GET (new_regno), new_regno, loc);
2115 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2116 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2120 /* Delete the mw_hardregs that point into the eq_notes. */
2122 static unsigned int
2123 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2125 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2126 unsigned int deleted = 0;
2127 unsigned int count = 0;
2128 struct df_scan_problem_data *problem_data
2129 = (struct df_scan_problem_data *) df_scan->problem_data;
2131 if (!*mw_vec)
2132 return 0;
2134 while (*mw_vec)
2136 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2138 struct df_mw_hardreg **temp_vec = mw_vec;
2140 pool_free (problem_data->mw_reg_pool, *mw_vec);
2141 temp_vec = mw_vec;
2142 /* Shove the remaining ones down one to fill the gap. While
2143 this looks n**2, it is highly unusual to have any mw regs
2144 in eq_notes and the chances of more than one are almost
2145 non existent. */
2146 while (*temp_vec)
2148 *temp_vec = *(temp_vec + 1);
2149 temp_vec++;
2151 deleted++;
2153 else
2155 mw_vec++;
2156 count++;
2160 if (count == 0)
2162 df_scan_free_mws_vec (insn_info->mw_hardregs);
2163 insn_info->mw_hardregs = df_null_mw_rec;
2164 return 0;
2166 return deleted;
2170 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2172 void
2173 df_notes_rescan (rtx insn)
2175 struct df_insn_info *insn_info;
2176 unsigned int uid = INSN_UID (insn);
2178 if (!df)
2179 return;
2181 /* The client has disabled rescanning and plans to do it itself. */
2182 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2183 return;
2185 /* Do nothing if the insn hasn't been emitted yet. */
2186 if (!BLOCK_FOR_INSN (insn))
2187 return;
2189 df_grow_bb_info (df_scan);
2190 df_grow_reg_info ();
2192 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2194 /* The client has deferred rescanning. */
2195 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2197 if (!insn_info)
2199 insn_info = df_insn_create_insn_record (insn);
2200 insn_info->defs = df_null_ref_rec;
2201 insn_info->uses = df_null_ref_rec;
2202 insn_info->eq_uses = df_null_ref_rec;
2203 insn_info->mw_hardregs = df_null_mw_rec;
2206 bitmap_clear_bit (&df->insns_to_delete, uid);
2207 /* If the insn is set to be rescanned, it does not need to also
2208 be notes rescanned. */
2209 if (!bitmap_bit_p (&df->insns_to_rescan, uid))
2210 bitmap_set_bit (&df->insns_to_notes_rescan, INSN_UID (insn));
2211 return;
2214 bitmap_clear_bit (&df->insns_to_delete, uid);
2215 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
2217 if (insn_info)
2219 basic_block bb = BLOCK_FOR_INSN (insn);
2220 rtx note;
2221 struct df_collection_rec collection_rec;
2222 unsigned int num_deleted;
2223 unsigned int mw_len;
2225 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2226 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
2227 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
2229 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2230 df_ref_chain_delete (insn_info->eq_uses);
2231 insn_info->eq_uses = NULL;
2233 /* Process REG_EQUIV/REG_EQUAL notes */
2234 for (note = REG_NOTES (insn); note;
2235 note = XEXP (note, 1))
2237 switch (REG_NOTE_KIND (note))
2239 case REG_EQUIV:
2240 case REG_EQUAL:
2241 df_uses_record (&collection_rec,
2242 &XEXP (note, 0), DF_REF_REG_USE,
2243 bb, insn_info, DF_REF_IN_NOTE);
2244 default:
2245 break;
2249 /* Find some place to put any new mw_hardregs. */
2250 df_canonize_collection_rec (&collection_rec);
2251 mw_len = collection_rec.mw_vec.length ();
2252 if (mw_len)
2254 unsigned int count = 0;
2255 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2256 while (*mw_rec)
2258 count++;
2259 mw_rec++;
2262 if (count)
2264 /* Append to the end of the existing record after
2265 expanding it if necessary. */
2266 if (mw_len > num_deleted)
2268 insn_info->mw_hardregs =
2269 XRESIZEVEC (struct df_mw_hardreg *,
2270 insn_info->mw_hardregs,
2271 count + 1 + mw_len);
2273 memcpy (&insn_info->mw_hardregs[count],
2274 collection_rec.mw_vec.address (),
2275 mw_len * sizeof (struct df_mw_hardreg *));
2276 insn_info->mw_hardregs[count + mw_len] = NULL;
2277 qsort (insn_info->mw_hardregs, count + mw_len,
2278 sizeof (struct df_mw_hardreg *), df_mw_compare);
2280 else
2282 /* No vector there. */
2283 insn_info->mw_hardregs
2284 = XNEWVEC (struct df_mw_hardreg*, 1 + mw_len);
2285 memcpy (insn_info->mw_hardregs,
2286 collection_rec.mw_vec.address (),
2287 mw_len * sizeof (struct df_mw_hardreg *));
2288 insn_info->mw_hardregs[mw_len] = NULL;
2291 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2292 ignore it. */
2293 collection_rec.mw_vec.release ();
2294 df_refs_add_to_chains (&collection_rec, bb, insn);
2295 collection_rec.eq_use_vec.release ();
2297 else
2298 df_insn_rescan (insn);
2303 /*----------------------------------------------------------------------------
2304 Hard core instruction scanning code. No external interfaces here,
2305 just a lot of routines that look inside insns.
2306 ----------------------------------------------------------------------------*/
2309 /* Return true if the contents of two df_ref's are identical.
2310 It ignores DF_REF_MARKER. */
2312 static bool
2313 df_ref_equal_p (df_ref ref1, df_ref ref2)
2315 if (!ref2)
2316 return false;
2318 if (ref1 == ref2)
2319 return true;
2321 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2322 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2323 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2324 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2325 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2326 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2327 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2328 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2329 return false;
2331 switch (DF_REF_CLASS (ref1))
2333 case DF_REF_ARTIFICIAL:
2334 case DF_REF_BASE:
2335 return true;
2337 case DF_REF_REGULAR:
2338 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2340 default:
2341 gcc_unreachable ();
2343 return false;
2347 /* Compare REF1 and REF2 for sorting. This is only called from places
2348 where all of the refs are of the same type, in the same insn, and
2349 have the same bb. So these fields are not checked. */
2351 static int
2352 df_ref_compare (const void *r1, const void *r2)
2354 const df_ref ref1 = *(const df_ref *)r1;
2355 const df_ref ref2 = *(const df_ref *)r2;
2357 if (ref1 == ref2)
2358 return 0;
2360 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2361 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2363 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2364 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2366 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2367 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2369 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2370 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2372 /* Cannot look at the LOC field on artificial refs. */
2373 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2374 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2375 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2377 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2379 /* If two refs are identical except that one of them has is from
2380 a mw and one is not, we need to have the one with the mw
2381 first. */
2382 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2383 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2384 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2385 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2386 return -1;
2387 else
2388 return 1;
2391 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2394 static void
2395 df_swap_refs (vec<df_ref, va_stack> *ref_vec, int i, int j)
2397 df_ref tmp = (*ref_vec)[i];
2398 (*ref_vec)[i] = (*ref_vec)[j];
2399 (*ref_vec)[j] = tmp;
2402 /* Sort and compress a set of refs. */
2404 static void
2405 df_sort_and_compress_refs (vec<df_ref, va_stack> *ref_vec)
2407 unsigned int count;
2408 unsigned int i;
2409 unsigned int dist = 0;
2411 count = ref_vec->length ();
2413 /* If there are 1 or 0 elements, there is nothing to do. */
2414 if (count < 2)
2415 return;
2416 else if (count == 2)
2418 df_ref r0 = (*ref_vec)[0];
2419 df_ref r1 = (*ref_vec)[1];
2420 if (df_ref_compare (&r0, &r1) > 0)
2421 df_swap_refs (ref_vec, 0, 1);
2423 else
2425 for (i = 0; i < count - 1; i++)
2427 df_ref r0 = (*ref_vec)[i];
2428 df_ref r1 = (*ref_vec)[i + 1];
2429 if (df_ref_compare (&r0, &r1) >= 0)
2430 break;
2432 /* If the array is already strictly ordered,
2433 which is the most common case for large COUNT case
2434 (which happens for CALL INSNs),
2435 no need to sort and filter out duplicate.
2436 Simply return the count.
2437 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2438 of DF_REF_COMPARE. */
2439 if (i == count - 1)
2440 return;
2441 ref_vec->qsort (df_ref_compare);
2444 for (i=0; i<count-dist; i++)
2446 /* Find the next ref that is not equal to the current ref. */
2447 while (i + dist + 1 < count
2448 && df_ref_equal_p ((*ref_vec)[i],
2449 (*ref_vec)[i + dist + 1]))
2451 df_free_ref ((*ref_vec)[i + dist + 1]);
2452 dist++;
2454 /* Copy it down to the next position. */
2455 if (dist && i + dist + 1 < count)
2456 (*ref_vec)[i + 1] = (*ref_vec)[i + dist + 1];
2459 count -= dist;
2460 ref_vec->truncate (count);
2464 /* Return true if the contents of two df_ref's are identical.
2465 It ignores DF_REF_MARKER. */
2467 static bool
2468 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2470 if (!mw2)
2471 return false;
2472 return (mw1 == mw2) ||
2473 (mw1->mw_reg == mw2->mw_reg
2474 && mw1->type == mw2->type
2475 && mw1->flags == mw2->flags
2476 && mw1->start_regno == mw2->start_regno
2477 && mw1->end_regno == mw2->end_regno);
2481 /* Compare MW1 and MW2 for sorting. */
2483 static int
2484 df_mw_compare (const void *m1, const void *m2)
2486 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2487 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2489 if (mw1 == mw2)
2490 return 0;
2492 if (mw1->type != mw2->type)
2493 return mw1->type - mw2->type;
2495 if (mw1->flags != mw2->flags)
2496 return mw1->flags - mw2->flags;
2498 if (mw1->start_regno != mw2->start_regno)
2499 return mw1->start_regno - mw2->start_regno;
2501 if (mw1->end_regno != mw2->end_regno)
2502 return mw1->end_regno - mw2->end_regno;
2504 if (mw1->mw_reg != mw2->mw_reg)
2505 return mw1->mw_order - mw2->mw_order;
2507 return 0;
2511 /* Sort and compress a set of refs. */
2513 static void
2514 df_sort_and_compress_mws (vec<df_mw_hardreg_ptr, va_stack> *mw_vec)
2516 unsigned int count;
2517 struct df_scan_problem_data *problem_data
2518 = (struct df_scan_problem_data *) df_scan->problem_data;
2519 unsigned int i;
2520 unsigned int dist = 0;
2522 count = mw_vec->length ();
2523 if (count < 2)
2524 return;
2525 else if (count == 2)
2527 struct df_mw_hardreg *m0 = (*mw_vec)[0];
2528 struct df_mw_hardreg *m1 = (*mw_vec)[1];
2529 if (df_mw_compare (&m0, &m1) > 0)
2531 struct df_mw_hardreg *tmp = (*mw_vec)[0];
2532 (*mw_vec)[0] = (*mw_vec)[1];
2533 (*mw_vec)[1] = tmp;
2536 else
2537 mw_vec->qsort (df_mw_compare);
2539 for (i=0; i<count-dist; i++)
2541 /* Find the next ref that is not equal to the current ref. */
2542 while (i + dist + 1 < count
2543 && df_mw_equal_p ((*mw_vec)[i], (*mw_vec)[i + dist + 1]))
2545 pool_free (problem_data->mw_reg_pool,
2546 (*mw_vec)[i + dist + 1]);
2547 dist++;
2549 /* Copy it down to the next position. */
2550 if (dist && i + dist + 1 < count)
2551 (*mw_vec)[i + 1] = (*mw_vec)[i + dist + 1];
2554 count -= dist;
2555 mw_vec->truncate (count);
2559 /* Sort and remove duplicates from the COLLECTION_REC. */
2561 static void
2562 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2564 df_sort_and_compress_refs (&collection_rec->def_vec);
2565 df_sort_and_compress_refs (&collection_rec->use_vec);
2566 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2567 df_sort_and_compress_mws (&collection_rec->mw_vec);
2571 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2573 static void
2574 df_install_ref (df_ref this_ref,
2575 struct df_reg_info *reg_info,
2576 struct df_ref_info *ref_info,
2577 bool add_to_table)
2579 unsigned int regno = DF_REF_REGNO (this_ref);
2580 /* Add the ref to the reg_{def,use,eq_use} chain. */
2581 df_ref head = reg_info->reg_chain;
2583 reg_info->reg_chain = this_ref;
2584 reg_info->n_refs++;
2586 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2588 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2589 df->hard_regs_live_count[regno]++;
2592 gcc_checking_assert (DF_REF_NEXT_REG (this_ref) == NULL
2593 && DF_REF_PREV_REG (this_ref) == NULL);
2595 DF_REF_NEXT_REG (this_ref) = head;
2597 /* We cannot actually link to the head of the chain. */
2598 DF_REF_PREV_REG (this_ref) = NULL;
2600 if (head)
2601 DF_REF_PREV_REG (head) = this_ref;
2603 if (add_to_table)
2605 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2606 df_check_and_grow_ref_info (ref_info, 1);
2607 DF_REF_ID (this_ref) = ref_info->table_size;
2608 /* Add the ref to the big array of defs. */
2609 ref_info->refs[ref_info->table_size] = this_ref;
2610 ref_info->table_size++;
2612 else
2613 DF_REF_ID (this_ref) = -1;
2615 ref_info->total_size++;
2619 /* This function takes one of the groups of refs (defs, uses or
2620 eq_uses) and installs the entire group into the insn. It also adds
2621 each of these refs into the appropriate chains. */
2623 static df_ref *
2624 df_install_refs (basic_block bb,
2625 vec<df_ref, va_stack> old_vec,
2626 struct df_reg_info **reg_info,
2627 struct df_ref_info *ref_info,
2628 bool is_notes)
2630 unsigned int count;
2632 count = old_vec.length ();
2633 if (count)
2635 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2636 bool add_to_table;
2637 df_ref this_ref;
2638 unsigned int ix;
2640 switch (ref_info->ref_order)
2642 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2643 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2644 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2645 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2646 add_to_table = true;
2647 break;
2648 case DF_REF_ORDER_UNORDERED:
2649 case DF_REF_ORDER_BY_REG:
2650 case DF_REF_ORDER_BY_INSN:
2651 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2652 add_to_table = !is_notes;
2653 break;
2654 default:
2655 add_to_table = false;
2656 break;
2659 /* Do not add if ref is not in the right blocks. */
2660 if (add_to_table && df->analyze_subset)
2661 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2663 FOR_EACH_VEC_ELT (old_vec, ix, this_ref)
2665 new_vec[ix] = this_ref;
2666 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2667 ref_info, add_to_table);
2670 new_vec[count] = NULL;
2671 return new_vec;
2673 else
2674 return df_null_ref_rec;
2678 /* This function takes the mws installs the entire group into the
2679 insn. */
2681 static struct df_mw_hardreg **
2682 df_install_mws (vec<df_mw_hardreg_ptr, va_stack> old_vec)
2684 unsigned int count;
2686 count = old_vec.length ();
2687 if (count)
2689 struct df_mw_hardreg **new_vec
2690 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2691 memcpy (new_vec, old_vec.address (),
2692 sizeof (struct df_mw_hardreg*) * count);
2693 new_vec[count] = NULL;
2694 return new_vec;
2696 else
2697 return df_null_mw_rec;
2701 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2702 chains and update other necessary information. */
2704 static void
2705 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2706 basic_block bb, rtx insn)
2708 if (insn)
2710 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2711 /* If there is a vector in the collection rec, add it to the
2712 insn. A null rec is a signal that the caller will handle the
2713 chain specially. */
2714 if (collection_rec->def_vec.exists ())
2716 df_scan_free_ref_vec (insn_rec->defs);
2717 insn_rec->defs
2718 = df_install_refs (bb, collection_rec->def_vec,
2719 df->def_regs,
2720 &df->def_info, false);
2722 if (collection_rec->use_vec.exists ())
2724 df_scan_free_ref_vec (insn_rec->uses);
2725 insn_rec->uses
2726 = df_install_refs (bb, collection_rec->use_vec,
2727 df->use_regs,
2728 &df->use_info, false);
2730 if (collection_rec->eq_use_vec.exists ())
2732 df_scan_free_ref_vec (insn_rec->eq_uses);
2733 insn_rec->eq_uses
2734 = df_install_refs (bb, collection_rec->eq_use_vec,
2735 df->eq_use_regs,
2736 &df->use_info, true);
2738 if (collection_rec->mw_vec.exists ())
2740 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2741 insn_rec->mw_hardregs
2742 = df_install_mws (collection_rec->mw_vec);
2745 else
2747 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2749 df_scan_free_ref_vec (bb_info->artificial_defs);
2750 bb_info->artificial_defs
2751 = df_install_refs (bb, collection_rec->def_vec,
2752 df->def_regs,
2753 &df->def_info, false);
2754 df_scan_free_ref_vec (bb_info->artificial_uses);
2755 bb_info->artificial_uses
2756 = df_install_refs (bb, collection_rec->use_vec,
2757 df->use_regs,
2758 &df->use_info, false);
2763 /* Allocate a ref and initialize its fields. */
2765 static df_ref
2766 df_ref_create_structure (enum df_ref_class cl,
2767 struct df_collection_rec *collection_rec,
2768 rtx reg, rtx *loc,
2769 basic_block bb, struct df_insn_info *info,
2770 enum df_ref_type ref_type,
2771 int ref_flags)
2773 df_ref this_ref = NULL;
2774 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2775 struct df_scan_problem_data *problem_data
2776 = (struct df_scan_problem_data *) df_scan->problem_data;
2778 switch (cl)
2780 case DF_REF_BASE:
2781 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2782 gcc_checking_assert (loc == NULL);
2783 break;
2785 case DF_REF_ARTIFICIAL:
2786 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2787 this_ref->artificial_ref.bb = bb;
2788 gcc_checking_assert (loc == NULL);
2789 break;
2791 case DF_REF_REGULAR:
2792 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2793 this_ref->regular_ref.loc = loc;
2794 gcc_checking_assert (loc);
2795 break;
2798 DF_REF_CLASS (this_ref) = cl;
2799 DF_REF_ID (this_ref) = -1;
2800 DF_REF_REG (this_ref) = reg;
2801 DF_REF_REGNO (this_ref) = regno;
2802 DF_REF_TYPE (this_ref) = ref_type;
2803 DF_REF_INSN_INFO (this_ref) = info;
2804 DF_REF_CHAIN (this_ref) = NULL;
2805 DF_REF_FLAGS (this_ref) = ref_flags;
2806 DF_REF_NEXT_REG (this_ref) = NULL;
2807 DF_REF_PREV_REG (this_ref) = NULL;
2808 DF_REF_ORDER (this_ref) = df->ref_order++;
2810 /* We need to clear this bit because fwprop, and in the future
2811 possibly other optimizations sometimes create new refs using ond
2812 refs as the model. */
2813 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2815 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2816 if (regno < FIRST_PSEUDO_REGISTER
2817 && !DF_REF_IS_ARTIFICIAL (this_ref)
2818 && !DEBUG_INSN_P (DF_REF_INSN (this_ref)))
2820 if (DF_REF_REG_DEF_P (this_ref))
2822 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2823 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2825 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2826 && (regno == FRAME_POINTER_REGNUM
2827 || regno == ARG_POINTER_REGNUM)))
2828 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2831 if (collection_rec)
2833 if (DF_REF_REG_DEF_P (this_ref))
2834 collection_rec->def_vec.safe_push (this_ref);
2835 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2836 collection_rec->eq_use_vec.safe_push (this_ref);
2837 else
2838 collection_rec->use_vec.safe_push (this_ref);
2840 else
2841 df_install_ref_incremental (this_ref);
2843 return this_ref;
2847 /* Create new references of type DF_REF_TYPE for each part of register REG
2848 at address LOC within INSN of BB. */
2851 static void
2852 df_ref_record (enum df_ref_class cl,
2853 struct df_collection_rec *collection_rec,
2854 rtx reg, rtx *loc,
2855 basic_block bb, struct df_insn_info *insn_info,
2856 enum df_ref_type ref_type,
2857 int ref_flags)
2859 unsigned int regno;
2861 gcc_checking_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2863 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2864 if (regno < FIRST_PSEUDO_REGISTER)
2866 struct df_mw_hardreg *hardreg = NULL;
2867 struct df_scan_problem_data *problem_data
2868 = (struct df_scan_problem_data *) df_scan->problem_data;
2869 unsigned int i;
2870 unsigned int endregno;
2871 df_ref ref;
2873 if (GET_CODE (reg) == SUBREG)
2875 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2876 SUBREG_BYTE (reg), GET_MODE (reg));
2877 endregno = regno + subreg_nregs (reg);
2879 else
2880 endregno = END_HARD_REGNO (reg);
2882 /* If this is a multiword hardreg, we create some extra
2883 datastructures that will enable us to easily build REG_DEAD
2884 and REG_UNUSED notes. */
2885 if (collection_rec
2886 && (endregno != regno + 1) && insn_info)
2888 /* Sets to a subreg of a multiword register are partial.
2889 Sets to a non-subreg of a multiword register are not. */
2890 if (GET_CODE (reg) == SUBREG)
2891 ref_flags |= DF_REF_PARTIAL;
2892 ref_flags |= DF_REF_MW_HARDREG;
2894 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2895 hardreg->type = ref_type;
2896 hardreg->flags = ref_flags;
2897 hardreg->mw_reg = reg;
2898 hardreg->start_regno = regno;
2899 hardreg->end_regno = endregno - 1;
2900 hardreg->mw_order = df->ref_order++;
2901 collection_rec->mw_vec.safe_push (hardreg);
2904 for (i = regno; i < endregno; i++)
2906 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2907 bb, insn_info, ref_type, ref_flags);
2909 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2912 else
2914 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2915 ref_type, ref_flags);
2920 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2921 covered by the outer mode is smaller than that covered by the inner mode,
2922 is a read-modify-write operation.
2923 This function returns true iff the SUBREG X is such a SUBREG. */
2925 bool
2926 df_read_modify_subreg_p (rtx x)
2928 unsigned int isize, osize;
2929 if (GET_CODE (x) != SUBREG)
2930 return false;
2931 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2932 osize = GET_MODE_SIZE (GET_MODE (x));
2933 return isize > osize
2934 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2938 /* Process all the registers defined in the rtx pointed by LOC.
2939 Autoincrement/decrement definitions will be picked up by df_uses_record.
2940 Any change here has to be matched in df_find_hard_reg_defs_1. */
2942 static void
2943 df_def_record_1 (struct df_collection_rec *collection_rec,
2944 rtx *loc, basic_block bb, struct df_insn_info *insn_info,
2945 int flags)
2947 rtx dst = *loc;
2949 /* It is legal to have a set destination be a parallel. */
2950 if (GET_CODE (dst) == PARALLEL)
2952 int i;
2953 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2955 rtx temp = XVECEXP (dst, 0, i);
2956 gcc_assert (GET_CODE (temp) == EXPR_LIST);
2957 df_def_record_1 (collection_rec, &XEXP (temp, 0),
2958 bb, insn_info, flags);
2960 return;
2963 if (GET_CODE (dst) == STRICT_LOW_PART)
2965 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2967 loc = &XEXP (dst, 0);
2968 dst = *loc;
2971 if (GET_CODE (dst) == ZERO_EXTRACT)
2973 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
2975 loc = &XEXP (dst, 0);
2976 dst = *loc;
2979 /* At this point if we do not have a reg or a subreg, just return. */
2980 if (REG_P (dst))
2982 df_ref_record (DF_REF_REGULAR, collection_rec,
2983 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
2985 /* We want to keep sp alive everywhere - by making all
2986 writes to sp also use of sp. */
2987 if (REGNO (dst) == STACK_POINTER_REGNUM)
2988 df_ref_record (DF_REF_BASE, collection_rec,
2989 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags);
2991 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
2993 if (df_read_modify_subreg_p (dst))
2994 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
2996 flags |= DF_REF_SUBREG;
2998 df_ref_record (DF_REF_REGULAR, collection_rec,
2999 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
3004 /* Process all the registers defined in the pattern rtx, X. Any change
3005 here has to be matched in df_find_hard_reg_defs. */
3007 static void
3008 df_defs_record (struct df_collection_rec *collection_rec,
3009 rtx x, basic_block bb, struct df_insn_info *insn_info,
3010 int flags)
3012 RTX_CODE code = GET_CODE (x);
3013 int i;
3015 switch (code)
3017 case SET:
3018 df_def_record_1 (collection_rec, &SET_DEST (x), bb, insn_info, flags);
3019 break;
3021 case CLOBBER:
3022 flags |= DF_REF_MUST_CLOBBER;
3023 df_def_record_1 (collection_rec, &XEXP (x, 0), bb, insn_info, flags);
3024 break;
3026 case COND_EXEC:
3027 df_defs_record (collection_rec, COND_EXEC_CODE (x),
3028 bb, insn_info, DF_REF_CONDITIONAL);
3029 break;
3031 case PARALLEL:
3032 for (i = 0; i < XVECLEN (x, 0); i++)
3033 df_defs_record (collection_rec, XVECEXP (x, 0, i),
3034 bb, insn_info, flags);
3035 break;
3036 default:
3037 /* No DEFs to record in other cases */
3038 break;
3042 /* Set bits in *DEFS for hard registers found in the rtx DST, which is the
3043 destination of a set or clobber. This has to match the logic in
3044 df_defs_record_1. */
3046 static void
3047 df_find_hard_reg_defs_1 (rtx dst, HARD_REG_SET *defs)
3049 /* It is legal to have a set destination be a parallel. */
3050 if (GET_CODE (dst) == PARALLEL)
3052 int i;
3053 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
3055 rtx temp = XVECEXP (dst, 0, i);
3056 gcc_assert (GET_CODE (temp) == EXPR_LIST);
3057 df_find_hard_reg_defs_1 (XEXP (temp, 0), defs);
3059 return;
3062 if (GET_CODE (dst) == STRICT_LOW_PART)
3063 dst = XEXP (dst, 0);
3065 if (GET_CODE (dst) == ZERO_EXTRACT)
3066 dst = XEXP (dst, 0);
3068 /* At this point if we do not have a reg or a subreg, just return. */
3069 if (REG_P (dst) && HARD_REGISTER_P (dst))
3070 SET_HARD_REG_BIT (*defs, REGNO (dst));
3071 else if (GET_CODE (dst) == SUBREG
3072 && REG_P (SUBREG_REG (dst)) && HARD_REGISTER_P (dst))
3073 SET_HARD_REG_BIT (*defs, REGNO (SUBREG_REG (dst)));
3076 /* Set bits in *DEFS for hard registers defined in the pattern X. This
3077 has to match the logic in df_defs_record. */
3079 static void
3080 df_find_hard_reg_defs (rtx x, HARD_REG_SET *defs)
3082 RTX_CODE code = GET_CODE (x);
3083 int i;
3085 switch (code)
3087 case SET:
3088 df_find_hard_reg_defs_1 (SET_DEST (x), defs);
3089 break;
3091 case CLOBBER:
3092 df_find_hard_reg_defs_1 (XEXP (x, 0), defs);
3093 break;
3095 case COND_EXEC:
3096 df_find_hard_reg_defs (COND_EXEC_CODE (x), defs);
3097 break;
3099 case PARALLEL:
3100 for (i = 0; i < XVECLEN (x, 0); i++)
3101 df_find_hard_reg_defs (XVECEXP (x, 0, i), defs);
3102 break;
3103 default:
3104 /* No DEFs to record in other cases */
3105 break;
3110 /* Process all the registers used in the rtx at address LOC. */
3112 static void
3113 df_uses_record (struct df_collection_rec *collection_rec,
3114 rtx *loc, enum df_ref_type ref_type,
3115 basic_block bb, struct df_insn_info *insn_info,
3116 int flags)
3118 RTX_CODE code;
3119 rtx x;
3121 retry:
3122 x = *loc;
3123 if (!x)
3124 return;
3125 code = GET_CODE (x);
3126 switch (code)
3128 case LABEL_REF:
3129 case SYMBOL_REF:
3130 case CONST:
3131 CASE_CONST_ANY:
3132 case PC:
3133 case CC0:
3134 case ADDR_VEC:
3135 case ADDR_DIFF_VEC:
3136 return;
3138 case CLOBBER:
3139 /* If we are clobbering a MEM, mark any registers inside the address
3140 as being used. */
3141 if (MEM_P (XEXP (x, 0)))
3142 df_uses_record (collection_rec,
3143 &XEXP (XEXP (x, 0), 0),
3144 DF_REF_REG_MEM_STORE,
3145 bb, insn_info,
3146 flags);
3148 /* If we're clobbering a REG then we have a def so ignore. */
3149 return;
3151 case MEM:
3152 df_uses_record (collection_rec,
3153 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3154 bb, insn_info, flags & DF_REF_IN_NOTE);
3155 return;
3157 case SUBREG:
3158 /* While we're here, optimize this case. */
3159 flags |= DF_REF_PARTIAL;
3160 /* In case the SUBREG is not of a REG, do not optimize. */
3161 if (!REG_P (SUBREG_REG (x)))
3163 loc = &SUBREG_REG (x);
3164 df_uses_record (collection_rec, loc, ref_type, bb, insn_info, flags);
3165 return;
3167 /* ... Fall through ... */
3169 case REG:
3170 df_ref_record (DF_REF_REGULAR, collection_rec,
3171 x, loc, bb, insn_info,
3172 ref_type, flags);
3173 return;
3175 case SIGN_EXTRACT:
3176 case ZERO_EXTRACT:
3178 df_uses_record (collection_rec,
3179 &XEXP (x, 1), ref_type, bb, insn_info, flags);
3180 df_uses_record (collection_rec,
3181 &XEXP (x, 2), ref_type, bb, insn_info, flags);
3183 /* If the parameters to the zero or sign extract are
3184 constants, strip them off and recurse, otherwise there is
3185 no information that we can gain from this operation. */
3186 if (code == ZERO_EXTRACT)
3187 flags |= DF_REF_ZERO_EXTRACT;
3188 else
3189 flags |= DF_REF_SIGN_EXTRACT;
3191 df_uses_record (collection_rec,
3192 &XEXP (x, 0), ref_type, bb, insn_info, flags);
3193 return;
3195 break;
3197 case SET:
3199 rtx dst = SET_DEST (x);
3200 gcc_assert (!(flags & DF_REF_IN_NOTE));
3201 df_uses_record (collection_rec,
3202 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags);
3204 switch (GET_CODE (dst))
3206 case SUBREG:
3207 if (df_read_modify_subreg_p (dst))
3209 df_uses_record (collection_rec, &SUBREG_REG (dst),
3210 DF_REF_REG_USE, bb, insn_info,
3211 flags | DF_REF_READ_WRITE | DF_REF_SUBREG);
3212 break;
3214 /* Fall through. */
3215 case REG:
3216 case PARALLEL:
3217 case SCRATCH:
3218 case PC:
3219 case CC0:
3220 break;
3221 case MEM:
3222 df_uses_record (collection_rec, &XEXP (dst, 0),
3223 DF_REF_REG_MEM_STORE, bb, insn_info, flags);
3224 break;
3225 case STRICT_LOW_PART:
3227 rtx *temp = &XEXP (dst, 0);
3228 /* A strict_low_part uses the whole REG and not just the
3229 SUBREG. */
3230 dst = XEXP (dst, 0);
3231 df_uses_record (collection_rec,
3232 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3233 DF_REF_REG_USE, bb, insn_info,
3234 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART);
3236 break;
3237 case ZERO_EXTRACT:
3239 df_uses_record (collection_rec, &XEXP (dst, 1),
3240 DF_REF_REG_USE, bb, insn_info, flags);
3241 df_uses_record (collection_rec, &XEXP (dst, 2),
3242 DF_REF_REG_USE, bb, insn_info, flags);
3243 if (GET_CODE (XEXP (dst,0)) == MEM)
3244 df_uses_record (collection_rec, &XEXP (dst, 0),
3245 DF_REF_REG_USE, bb, insn_info,
3246 flags);
3247 else
3248 df_uses_record (collection_rec, &XEXP (dst, 0),
3249 DF_REF_REG_USE, bb, insn_info,
3250 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT);
3252 break;
3254 default:
3255 gcc_unreachable ();
3257 return;
3260 case RETURN:
3261 case SIMPLE_RETURN:
3262 break;
3264 case ASM_OPERANDS:
3265 case UNSPEC_VOLATILE:
3266 case TRAP_IF:
3267 case ASM_INPUT:
3269 /* Traditional and volatile asm instructions must be
3270 considered to use and clobber all hard registers, all
3271 pseudo-registers and all of memory. So must TRAP_IF and
3272 UNSPEC_VOLATILE operations.
3274 Consider for instance a volatile asm that changes the fpu
3275 rounding mode. An insn should not be moved across this
3276 even if it only uses pseudo-regs because it might give an
3277 incorrectly rounded result.
3279 However, flow.c's liveness computation did *not* do this,
3280 giving the reasoning as " ?!? Unfortunately, marking all
3281 hard registers as live causes massive problems for the
3282 register allocator and marking all pseudos as live creates
3283 mountains of uninitialized variable warnings."
3285 In order to maintain the status quo with regard to liveness
3286 and uses, we do what flow.c did and just mark any regs we
3287 can find in ASM_OPERANDS as used. In global asm insns are
3288 scanned and regs_asm_clobbered is filled out.
3290 For all ASM_OPERANDS, we must traverse the vector of input
3291 operands. We can not just fall through here since then we
3292 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3293 which do not indicate traditional asms unlike their normal
3294 usage. */
3295 if (code == ASM_OPERANDS)
3297 int j;
3299 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3300 df_uses_record (collection_rec, &ASM_OPERANDS_INPUT (x, j),
3301 DF_REF_REG_USE, bb, insn_info, flags);
3302 return;
3304 break;
3307 case VAR_LOCATION:
3308 df_uses_record (collection_rec,
3309 &PAT_VAR_LOCATION_LOC (x),
3310 DF_REF_REG_USE, bb, insn_info, flags);
3311 return;
3313 case PRE_DEC:
3314 case POST_DEC:
3315 case PRE_INC:
3316 case POST_INC:
3317 case PRE_MODIFY:
3318 case POST_MODIFY:
3319 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3320 /* Catch the def of the register being modified. */
3321 df_ref_record (DF_REF_REGULAR, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3322 bb, insn_info,
3323 DF_REF_REG_DEF,
3324 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY);
3326 /* ... Fall through to handle uses ... */
3328 default:
3329 break;
3332 /* Recursively scan the operands of this expression. */
3334 const char *fmt = GET_RTX_FORMAT (code);
3335 int i;
3337 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3339 if (fmt[i] == 'e')
3341 /* Tail recursive case: save a function call level. */
3342 if (i == 0)
3344 loc = &XEXP (x, 0);
3345 goto retry;
3347 df_uses_record (collection_rec, &XEXP (x, i), ref_type,
3348 bb, insn_info, flags);
3350 else if (fmt[i] == 'E')
3352 int j;
3353 for (j = 0; j < XVECLEN (x, i); j++)
3354 df_uses_record (collection_rec,
3355 &XVECEXP (x, i, j), ref_type,
3356 bb, insn_info, flags);
3361 return;
3365 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3367 static void
3368 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3370 unsigned int ix;
3371 df_ref ref;
3373 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
3375 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3377 df_ref use;
3379 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3380 DF_REF_LOC (ref), DF_REF_BB (ref),
3381 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3382 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL);
3383 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3389 /* Get call's extra defs and uses (track caller-saved registers). */
3391 static void
3392 df_get_call_refs (struct df_collection_rec *collection_rec,
3393 basic_block bb,
3394 struct df_insn_info *insn_info,
3395 int flags)
3397 rtx note;
3398 bool is_sibling_call;
3399 unsigned int i;
3400 HARD_REG_SET defs_generated;
3402 CLEAR_HARD_REG_SET (defs_generated);
3403 df_find_hard_reg_defs (PATTERN (insn_info->insn), &defs_generated);
3404 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3406 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3408 if (i == STACK_POINTER_REGNUM)
3409 /* The stack ptr is used (honorarily) by a CALL insn. */
3410 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3411 NULL, bb, insn_info, DF_REF_REG_USE,
3412 DF_REF_CALL_STACK_USAGE | flags);
3413 else if (global_regs[i])
3415 /* Calls to const functions cannot access any global registers and
3416 calls to pure functions cannot set them. All other calls may
3417 reference any of the global registers, so they are recorded as
3418 used. */
3419 if (!RTL_CONST_CALL_P (insn_info->insn))
3421 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3422 NULL, bb, insn_info, DF_REF_REG_USE, flags);
3423 if (!RTL_PURE_CALL_P (insn_info->insn))
3424 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3425 NULL, bb, insn_info, DF_REF_REG_DEF, flags);
3428 else if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
3429 /* no clobbers for regs that are the result of the call */
3430 && !TEST_HARD_REG_BIT (defs_generated, i)
3431 && (!is_sibling_call
3432 || !bitmap_bit_p (df->exit_block_uses, i)
3433 || refers_to_regno_p (i, i+1,
3434 crtl->return_rtx, NULL)))
3435 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3436 NULL, bb, insn_info, DF_REF_REG_DEF,
3437 DF_REF_MAY_CLOBBER | flags);
3440 /* Record the registers used to pass arguments, and explicitly
3441 noted as clobbered. */
3442 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3443 note = XEXP (note, 1))
3445 if (GET_CODE (XEXP (note, 0)) == USE)
3446 df_uses_record (collection_rec, &XEXP (XEXP (note, 0), 0),
3447 DF_REF_REG_USE, bb, insn_info, flags);
3448 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3450 if (REG_P (XEXP (XEXP (note, 0), 0)))
3452 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3453 if (!TEST_HARD_REG_BIT (defs_generated, regno))
3454 df_defs_record (collection_rec, XEXP (note, 0), bb,
3455 insn_info, flags);
3457 else
3458 df_uses_record (collection_rec, &XEXP (note, 0),
3459 DF_REF_REG_USE, bb, insn_info, flags);
3463 return;
3466 /* Collect all refs in the INSN. This function is free of any
3467 side-effect - it will create and return a lists of df_ref's in the
3468 COLLECTION_REC without putting those refs into existing ref chains
3469 and reg chains. */
3471 static void
3472 df_insn_refs_collect (struct df_collection_rec *collection_rec,
3473 basic_block bb, struct df_insn_info *insn_info)
3475 rtx note;
3476 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3478 /* Clear out the collection record. */
3479 collection_rec->def_vec.truncate (0);
3480 collection_rec->use_vec.truncate (0);
3481 collection_rec->eq_use_vec.truncate (0);
3482 collection_rec->mw_vec.truncate (0);
3484 /* Process REG_EQUIV/REG_EQUAL notes. */
3485 for (note = REG_NOTES (insn_info->insn); note;
3486 note = XEXP (note, 1))
3488 switch (REG_NOTE_KIND (note))
3490 case REG_EQUIV:
3491 case REG_EQUAL:
3492 df_uses_record (collection_rec,
3493 &XEXP (note, 0), DF_REF_REG_USE,
3494 bb, insn_info, DF_REF_IN_NOTE);
3495 break;
3496 case REG_NON_LOCAL_GOTO:
3497 /* The frame ptr is used by a non-local goto. */
3498 df_ref_record (DF_REF_BASE, collection_rec,
3499 regno_reg_rtx[FRAME_POINTER_REGNUM],
3500 NULL, bb, insn_info,
3501 DF_REF_REG_USE, 0);
3502 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3503 df_ref_record (DF_REF_BASE, collection_rec,
3504 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3505 NULL, bb, insn_info,
3506 DF_REF_REG_USE, 0);
3507 #endif
3508 break;
3509 default:
3510 break;
3514 /* For CALL_INSNs, first record DF_REF_BASE register defs, as well as
3515 uses from CALL_INSN_FUNCTION_USAGE. */
3516 if (CALL_P (insn_info->insn))
3517 df_get_call_refs (collection_rec, bb, insn_info,
3518 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3520 /* Record other defs. These should be mostly for DF_REF_REGULAR, so
3521 that a qsort on the defs is unnecessary in most cases. */
3522 df_defs_record (collection_rec,
3523 PATTERN (insn_info->insn), bb, insn_info, 0);
3525 /* Record the register uses. */
3526 df_uses_record (collection_rec,
3527 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0);
3529 /* DF_REF_CONDITIONAL needs corresponding USES. */
3530 if (is_cond_exec)
3531 df_get_conditional_uses (collection_rec);
3533 df_canonize_collection_rec (collection_rec);
3536 /* Recompute the luids for the insns in BB. */
3538 void
3539 df_recompute_luids (basic_block bb)
3541 rtx insn;
3542 int luid = 0;
3544 df_grow_insn_info ();
3546 /* Scan the block an insn at a time from beginning to end. */
3547 FOR_BB_INSNS (bb, insn)
3549 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3550 /* Inserting labels does not always trigger the incremental
3551 rescanning. */
3552 if (!insn_info)
3554 gcc_assert (!INSN_P (insn));
3555 insn_info = df_insn_create_insn_record (insn);
3558 DF_INSN_INFO_LUID (insn_info) = luid;
3559 if (INSN_P (insn))
3560 luid++;
3565 /* Collect all artificial refs at the block level for BB and add them
3566 to COLLECTION_REC. */
3568 static void
3569 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3571 collection_rec->def_vec.truncate (0);
3572 collection_rec->use_vec.truncate (0);
3573 collection_rec->eq_use_vec.truncate (0);
3574 collection_rec->mw_vec.truncate (0);
3576 if (bb->index == ENTRY_BLOCK)
3578 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3579 return;
3581 else if (bb->index == EXIT_BLOCK)
3583 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3584 return;
3587 #ifdef EH_RETURN_DATA_REGNO
3588 if (bb_has_eh_pred (bb))
3590 unsigned int i;
3591 /* Mark the registers that will contain data for the handler. */
3592 for (i = 0; ; ++i)
3594 unsigned regno = EH_RETURN_DATA_REGNO (i);
3595 if (regno == INVALID_REGNUM)
3596 break;
3597 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3598 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3601 #endif
3603 /* Add the hard_frame_pointer if this block is the target of a
3604 non-local goto. */
3605 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3606 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3607 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3609 /* Add the artificial uses. */
3610 if (bb->index >= NUM_FIXED_BLOCKS)
3612 bitmap_iterator bi;
3613 unsigned int regno;
3614 bitmap au = bb_has_eh_pred (bb)
3615 ? &df->eh_block_artificial_uses
3616 : &df->regular_block_artificial_uses;
3618 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3620 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3621 bb, NULL, DF_REF_REG_USE, 0);
3625 df_canonize_collection_rec (collection_rec);
3629 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3631 void
3632 df_bb_refs_record (int bb_index, bool scan_insns)
3634 basic_block bb = BASIC_BLOCK (bb_index);
3635 rtx insn;
3636 int luid = 0;
3637 struct df_collection_rec collection_rec;
3639 if (!df)
3640 return;
3642 df_grow_bb_info (df_scan);
3643 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
3644 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
3645 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
3646 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
3648 if (scan_insns)
3649 /* Scan the block an insn at a time from beginning to end. */
3650 FOR_BB_INSNS (bb, insn)
3652 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3653 gcc_assert (!insn_info);
3655 insn_info = df_insn_create_insn_record (insn);
3656 if (INSN_P (insn))
3658 /* Record refs within INSN. */
3659 DF_INSN_INFO_LUID (insn_info) = luid++;
3660 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3661 df_refs_add_to_chains (&collection_rec, bb, insn);
3663 DF_INSN_INFO_LUID (insn_info) = luid;
3666 /* Other block level artificial refs */
3667 df_bb_refs_collect (&collection_rec, bb);
3668 df_refs_add_to_chains (&collection_rec, bb, NULL);
3670 collection_rec.def_vec.release ();
3671 collection_rec.use_vec.release ();
3672 collection_rec.eq_use_vec.release ();
3673 collection_rec.mw_vec.release ();
3675 /* Now that the block has been processed, set the block as dirty so
3676 LR and LIVE will get it processed. */
3677 df_set_bb_dirty (bb);
3681 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3682 block. */
3684 static void
3685 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3687 #ifdef EH_USES
3688 unsigned int i;
3689 #endif
3691 bitmap_clear (regular_block_artificial_uses);
3693 if (reload_completed)
3695 if (frame_pointer_needed)
3696 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3698 else
3699 /* Before reload, there are a few registers that must be forced
3700 live everywhere -- which might not already be the case for
3701 blocks within infinite loops. */
3703 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3705 /* Any reference to any pseudo before reload is a potential
3706 reference of the frame pointer. */
3707 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3709 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3710 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3711 #endif
3713 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3714 /* Pseudos with argument area equivalences may require
3715 reloading via the argument pointer. */
3716 if (fixed_regs[ARG_POINTER_REGNUM])
3717 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3718 #endif
3720 /* Any constant, or pseudo with constant equivalences, may
3721 require reloading from memory using the pic register. */
3722 if (picreg != INVALID_REGNUM
3723 && fixed_regs[picreg])
3724 bitmap_set_bit (regular_block_artificial_uses, picreg);
3726 /* The all-important stack pointer must always be live. */
3727 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3729 #ifdef EH_USES
3730 /* EH_USES registers are used:
3731 1) at all insns that might throw (calls or with -fnon-call-exceptions
3732 trapping insns)
3733 2) in all EH edges
3734 3) to support backtraces and/or debugging, anywhere between their
3735 initialization and where they the saved registers are restored
3736 from them, including the cases where we don't reach the epilogue
3737 (noreturn call or infinite loop). */
3738 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3739 if (EH_USES (i))
3740 bitmap_set_bit (regular_block_artificial_uses, i);
3741 #endif
3745 /* Get the artificial use set for an eh block. */
3747 static void
3748 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3750 bitmap_clear (eh_block_artificial_uses);
3752 /* The following code (down through the arg_pointer setting APPEARS
3753 to be necessary because there is nothing that actually
3754 describes what the exception handling code may actually need
3755 to keep alive. */
3756 if (reload_completed)
3758 if (frame_pointer_needed)
3760 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3761 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3762 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3763 #endif
3765 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3766 if (fixed_regs[ARG_POINTER_REGNUM])
3767 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3768 #endif
3774 /*----------------------------------------------------------------------------
3775 Specialized hard register scanning functions.
3776 ----------------------------------------------------------------------------*/
3779 /* Mark a register in SET. Hard registers in large modes get all
3780 of their component registers set as well. */
3782 static void
3783 df_mark_reg (rtx reg, void *vset)
3785 bitmap set = (bitmap) vset;
3786 int regno = REGNO (reg);
3788 gcc_assert (GET_MODE (reg) != BLKmode);
3790 if (regno < FIRST_PSEUDO_REGISTER)
3792 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3793 bitmap_set_range (set, regno, n);
3795 else
3796 bitmap_set_bit (set, regno);
3800 /* Set the bit for regs that are considered being defined at the entry. */
3802 static void
3803 df_get_entry_block_def_set (bitmap entry_block_defs)
3805 rtx r;
3806 int i;
3808 bitmap_clear (entry_block_defs);
3810 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3812 if (global_regs[i])
3813 bitmap_set_bit (entry_block_defs, i);
3814 if (FUNCTION_ARG_REGNO_P (i))
3815 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3818 /* The always important stack pointer. */
3819 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3821 /* Once the prologue has been generated, all of these registers
3822 should just show up in the first regular block. */
3823 if (HAVE_prologue && epilogue_completed)
3825 /* Defs for the callee saved registers are inserted so that the
3826 pushes have some defining location. */
3827 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3828 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3829 bitmap_set_bit (entry_block_defs, i);
3832 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3833 if (r && REG_P (r))
3834 bitmap_set_bit (entry_block_defs, REGNO (r));
3836 /* If the function has an incoming STATIC_CHAIN, it has to show up
3837 in the entry def set. */
3838 r = targetm.calls.static_chain (current_function_decl, true);
3839 if (r && REG_P (r))
3840 bitmap_set_bit (entry_block_defs, REGNO (r));
3842 if ((!reload_completed) || frame_pointer_needed)
3844 /* Any reference to any pseudo before reload is a potential
3845 reference of the frame pointer. */
3846 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3847 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3848 /* If they are different, also mark the hard frame pointer as live. */
3849 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3850 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3851 #endif
3854 /* These registers are live everywhere. */
3855 if (!reload_completed)
3857 #ifdef PIC_OFFSET_TABLE_REGNUM
3858 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3859 #endif
3861 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3862 /* Pseudos with argument area equivalences may require
3863 reloading via the argument pointer. */
3864 if (fixed_regs[ARG_POINTER_REGNUM])
3865 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3866 #endif
3868 #ifdef PIC_OFFSET_TABLE_REGNUM
3869 /* Any constant, or pseudo with constant equivalences, may
3870 require reloading from memory using the pic register. */
3871 if (picreg != INVALID_REGNUM
3872 && fixed_regs[picreg])
3873 bitmap_set_bit (entry_block_defs, picreg);
3874 #endif
3877 #ifdef INCOMING_RETURN_ADDR_RTX
3878 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3879 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3880 #endif
3882 targetm.extra_live_on_entry (entry_block_defs);
3886 /* Return the (conservative) set of hard registers that are defined on
3887 entry to the function.
3888 It uses df->entry_block_defs to determine which register
3889 reference to include. */
3891 static void
3892 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3893 bitmap entry_block_defs)
3895 unsigned int i;
3896 bitmap_iterator bi;
3898 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3900 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3901 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0);
3904 df_canonize_collection_rec (collection_rec);
3908 /* Record the (conservative) set of hard registers that are defined on
3909 entry to the function. */
3911 static void
3912 df_record_entry_block_defs (bitmap entry_block_defs)
3914 struct df_collection_rec collection_rec;
3915 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3916 vec_stack_alloc (df_ref, collection_rec.def_vec, FIRST_PSEUDO_REGISTER);
3917 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3919 /* Process bb_refs chain */
3920 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3921 collection_rec.def_vec.release ();
3925 /* Update the defs in the entry block. */
3927 void
3928 df_update_entry_block_defs (void)
3930 bitmap_head refs;
3931 bool changed = false;
3933 bitmap_initialize (&refs, &df_bitmap_obstack);
3934 df_get_entry_block_def_set (&refs);
3935 if (df->entry_block_defs)
3937 if (!bitmap_equal_p (df->entry_block_defs, &refs))
3939 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3940 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3941 df_ref_chain_delete (bb_info->artificial_defs);
3942 bb_info->artificial_defs = NULL;
3943 changed = true;
3946 else
3948 struct df_scan_problem_data *problem_data
3949 = (struct df_scan_problem_data *) df_scan->problem_data;
3950 gcc_unreachable ();
3951 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3952 changed = true;
3955 if (changed)
3957 df_record_entry_block_defs (&refs);
3958 bitmap_copy (df->entry_block_defs, &refs);
3959 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
3961 bitmap_clear (&refs);
3965 /* Set the bit for regs that are considered being used at the exit. */
3967 static void
3968 df_get_exit_block_use_set (bitmap exit_block_uses)
3970 unsigned int i;
3971 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3973 bitmap_clear (exit_block_uses);
3975 /* Stack pointer is always live at the exit. */
3976 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
3978 /* Mark the frame pointer if needed at the end of the function.
3979 If we end up eliminating it, it will be removed from the live
3980 list of each basic block by reload. */
3982 if ((!reload_completed) || frame_pointer_needed)
3984 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
3985 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3986 /* If they are different, also mark the hard frame pointer as live. */
3987 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3988 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
3989 #endif
3992 /* Many architectures have a GP register even without flag_pic.
3993 Assume the pic register is not in use, or will be handled by
3994 other means, if it is not fixed. */
3995 if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
3996 && picreg != INVALID_REGNUM
3997 && fixed_regs[picreg])
3998 bitmap_set_bit (exit_block_uses, picreg);
4000 /* Mark all global registers, and all registers used by the
4001 epilogue as being live at the end of the function since they
4002 may be referenced by our caller. */
4003 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4004 if (global_regs[i] || EPILOGUE_USES (i))
4005 bitmap_set_bit (exit_block_uses, i);
4007 if (HAVE_epilogue && epilogue_completed)
4009 /* Mark all call-saved registers that we actually used. */
4010 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4011 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
4012 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
4013 bitmap_set_bit (exit_block_uses, i);
4016 #ifdef EH_RETURN_DATA_REGNO
4017 /* Mark the registers that will contain data for the handler. */
4018 if (reload_completed && crtl->calls_eh_return)
4019 for (i = 0; ; ++i)
4021 unsigned regno = EH_RETURN_DATA_REGNO (i);
4022 if (regno == INVALID_REGNUM)
4023 break;
4024 bitmap_set_bit (exit_block_uses, regno);
4026 #endif
4028 #ifdef EH_RETURN_STACKADJ_RTX
4029 if ((!HAVE_epilogue || ! epilogue_completed)
4030 && crtl->calls_eh_return)
4032 rtx tmp = EH_RETURN_STACKADJ_RTX;
4033 if (tmp && REG_P (tmp))
4034 df_mark_reg (tmp, exit_block_uses);
4036 #endif
4038 #ifdef EH_RETURN_HANDLER_RTX
4039 if ((!HAVE_epilogue || ! epilogue_completed)
4040 && crtl->calls_eh_return)
4042 rtx tmp = EH_RETURN_HANDLER_RTX;
4043 if (tmp && REG_P (tmp))
4044 df_mark_reg (tmp, exit_block_uses);
4046 #endif
4048 /* Mark function return value. */
4049 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4053 /* Return the refs of hard registers that are used in the exit block.
4054 It uses df->exit_block_uses to determine register to include. */
4056 static void
4057 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4059 unsigned int i;
4060 bitmap_iterator bi;
4062 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4063 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4064 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4066 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4067 /* It is deliberate that this is not put in the exit block uses but
4068 I do not know why. */
4069 if (reload_completed
4070 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4071 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4072 && fixed_regs[ARG_POINTER_REGNUM])
4073 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4074 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4075 #endif
4077 df_canonize_collection_rec (collection_rec);
4081 /* Record the set of hard registers that are used in the exit block.
4082 It uses df->exit_block_uses to determine which bit to include. */
4084 static void
4085 df_record_exit_block_uses (bitmap exit_block_uses)
4087 struct df_collection_rec collection_rec;
4088 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4089 vec_stack_alloc (df_ref, collection_rec.use_vec, FIRST_PSEUDO_REGISTER);
4090 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4092 /* Process bb_refs chain */
4093 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4094 collection_rec.use_vec.release ();
4098 /* Update the uses in the exit block. */
4100 void
4101 df_update_exit_block_uses (void)
4103 bitmap_head refs;
4104 bool changed = false;
4106 bitmap_initialize (&refs, &df_bitmap_obstack);
4107 df_get_exit_block_use_set (&refs);
4108 if (df->exit_block_uses)
4110 if (!bitmap_equal_p (df->exit_block_uses, &refs))
4112 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4113 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4114 df_ref_chain_delete (bb_info->artificial_uses);
4115 bb_info->artificial_uses = NULL;
4116 changed = true;
4119 else
4121 struct df_scan_problem_data *problem_data
4122 = (struct df_scan_problem_data *) df_scan->problem_data;
4123 gcc_unreachable ();
4124 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4125 changed = true;
4128 if (changed)
4130 df_record_exit_block_uses (&refs);
4131 bitmap_copy (df->exit_block_uses,& refs);
4132 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4134 bitmap_clear (&refs);
4137 static bool initialized = false;
4140 /* Initialize some platform specific structures. */
4142 void
4143 df_hard_reg_init (void)
4145 #ifdef ELIMINABLE_REGS
4146 int i;
4147 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4148 #endif
4149 if (initialized)
4150 return;
4152 /* Record which registers will be eliminated. We use this in
4153 mark_used_regs. */
4154 CLEAR_HARD_REG_SET (elim_reg_set);
4156 #ifdef ELIMINABLE_REGS
4157 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4158 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4159 #else
4160 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4161 #endif
4163 initialized = true;
4167 /* Recompute the parts of scanning that are based on regs_ever_live
4168 because something changed in that array. */
4170 void
4171 df_update_entry_exit_and_calls (void)
4173 basic_block bb;
4175 df_update_entry_block_defs ();
4176 df_update_exit_block_uses ();
4178 /* The call insns need to be rescanned because there may be changes
4179 in the set of registers clobbered across the call. */
4180 FOR_EACH_BB (bb)
4182 rtx insn;
4183 FOR_BB_INSNS (bb, insn)
4185 if (INSN_P (insn) && CALL_P (insn))
4186 df_insn_rescan (insn);
4192 /* Return true if hard REG is actually used in the some instruction.
4193 There are a fair number of conditions that affect the setting of
4194 this array. See the comment in df.h for df->hard_regs_live_count
4195 for the conditions that this array is set. */
4197 bool
4198 df_hard_reg_used_p (unsigned int reg)
4200 return df->hard_regs_live_count[reg] != 0;
4204 /* A count of the number of times REG is actually used in the some
4205 instruction. There are a fair number of conditions that affect the
4206 setting of this array. See the comment in df.h for
4207 df->hard_regs_live_count for the conditions that this array is
4208 set. */
4211 unsigned int
4212 df_hard_reg_used_count (unsigned int reg)
4214 return df->hard_regs_live_count[reg];
4218 /* Get the value of regs_ever_live[REGNO]. */
4220 bool
4221 df_regs_ever_live_p (unsigned int regno)
4223 return regs_ever_live[regno];
4227 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4228 to change, schedule that change for the next update. */
4230 void
4231 df_set_regs_ever_live (unsigned int regno, bool value)
4233 if (regs_ever_live[regno] == value)
4234 return;
4236 regs_ever_live[regno] = value;
4237 if (df)
4238 df->redo_entry_and_exit = true;
4242 /* Compute "regs_ever_live" information from the underlying df
4243 information. Set the vector to all false if RESET. */
4245 void
4246 df_compute_regs_ever_live (bool reset)
4248 unsigned int i;
4249 bool changed = df->redo_entry_and_exit;
4251 if (reset)
4252 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4254 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4255 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4257 regs_ever_live[i] = true;
4258 changed = true;
4260 if (changed)
4261 df_update_entry_exit_and_calls ();
4262 df->redo_entry_and_exit = false;
4266 /*----------------------------------------------------------------------------
4267 Dataflow ref information verification functions.
4269 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4270 df_reg_chain_verify_unmarked (refs)
4271 df_refs_verify (vec<stack, va_df_ref>, ref*, bool)
4272 df_mws_verify (mw*, mw*, bool)
4273 df_insn_refs_verify (collection_rec, bb, insn, bool)
4274 df_bb_refs_verify (bb, refs, bool)
4275 df_bb_verify (bb)
4276 df_exit_block_bitmap_verify (bool)
4277 df_entry_block_bitmap_verify (bool)
4278 df_scan_verify ()
4279 ----------------------------------------------------------------------------*/
4282 /* Mark all refs in the reg chain. Verify that all of the registers
4283 are in the correct chain. */
4285 static unsigned int
4286 df_reg_chain_mark (df_ref refs, unsigned int regno,
4287 bool is_def, bool is_eq_use)
4289 unsigned int count = 0;
4290 df_ref ref;
4291 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4293 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4295 /* If there are no def-use or use-def chains, make sure that all
4296 of the chains are clear. */
4297 if (!df_chain)
4298 gcc_assert (!DF_REF_CHAIN (ref));
4300 /* Check to make sure the ref is in the correct chain. */
4301 gcc_assert (DF_REF_REGNO (ref) == regno);
4302 if (is_def)
4303 gcc_assert (DF_REF_REG_DEF_P (ref));
4304 else
4305 gcc_assert (!DF_REF_REG_DEF_P (ref));
4307 if (is_eq_use)
4308 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4309 else
4310 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4312 if (DF_REF_NEXT_REG (ref))
4313 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4314 count++;
4315 DF_REF_REG_MARK (ref);
4317 return count;
4321 /* Verify that all of the registers in the chain are unmarked. */
4323 static void
4324 df_reg_chain_verify_unmarked (df_ref refs)
4326 df_ref ref;
4327 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4328 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4332 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4334 static bool
4335 df_refs_verify (vec<df_ref, va_stack> new_rec, df_ref *old_rec,
4336 bool abort_if_fail)
4338 unsigned int ix;
4339 df_ref new_ref;
4341 FOR_EACH_VEC_ELT (new_rec, ix, new_ref)
4343 if (*old_rec == NULL || !df_ref_equal_p (new_ref, *old_rec))
4345 if (abort_if_fail)
4346 gcc_assert (0);
4347 else
4348 return false;
4351 /* Abort if fail is called from the function level verifier. If
4352 that is the context, mark this reg as being seem. */
4353 if (abort_if_fail)
4355 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4356 DF_REF_REG_UNMARK (*old_rec);
4359 old_rec++;
4362 if (abort_if_fail)
4363 gcc_assert (*old_rec == NULL);
4364 else
4365 return *old_rec == NULL;
4366 return false;
4370 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4372 static bool
4373 df_mws_verify (vec<df_mw_hardreg_ptr, va_stack> new_rec,
4374 struct df_mw_hardreg **old_rec,
4375 bool abort_if_fail)
4377 unsigned int ix;
4378 struct df_mw_hardreg *new_reg;
4380 FOR_EACH_VEC_ELT (new_rec, ix, new_reg)
4382 if (*old_rec == NULL || !df_mw_equal_p (new_reg, *old_rec))
4384 if (abort_if_fail)
4385 gcc_assert (0);
4386 else
4387 return false;
4389 old_rec++;
4392 if (abort_if_fail)
4393 gcc_assert (*old_rec == NULL);
4394 else
4395 return *old_rec == NULL;
4396 return false;
4400 /* Return true if the existing insn refs information is complete and
4401 correct. Otherwise (i.e. if there's any missing or extra refs),
4402 return the correct df_ref chain in REFS_RETURN.
4404 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4405 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4406 verification mode instead of the whole function, so unmark
4407 everything.
4409 If ABORT_IF_FAIL is set, this function never returns false. */
4411 static bool
4412 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4413 basic_block bb,
4414 rtx insn,
4415 bool abort_if_fail)
4417 bool ret1, ret2, ret3, ret4;
4418 unsigned int uid = INSN_UID (insn);
4419 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4421 df_insn_refs_collect (collection_rec, bb, insn_info);
4423 if (!DF_INSN_UID_DEFS (uid))
4425 /* The insn_rec was created but it was never filled out. */
4426 if (abort_if_fail)
4427 gcc_assert (0);
4428 else
4429 return false;
4432 /* Unfortunately we cannot opt out early if one of these is not
4433 right because the marks will not get cleared. */
4434 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4435 abort_if_fail);
4436 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4437 abort_if_fail);
4438 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4439 abort_if_fail);
4440 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4441 abort_if_fail);
4442 return (ret1 && ret2 && ret3 && ret4);
4446 /* Return true if all refs in the basic block are correct and complete.
4447 Due to df_ref_chain_verify, it will cause all refs
4448 that are verified to have DF_REF_MARK bit set. */
4450 static bool
4451 df_bb_verify (basic_block bb)
4453 rtx insn;
4454 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4455 struct df_collection_rec collection_rec;
4457 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4458 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
4459 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
4460 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
4461 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
4463 gcc_assert (bb_info);
4465 /* Scan the block, one insn at a time, from beginning to end. */
4466 FOR_BB_INSNS_REVERSE (bb, insn)
4468 if (!INSN_P (insn))
4469 continue;
4470 df_insn_refs_verify (&collection_rec, bb, insn, true);
4471 df_free_collection_rec (&collection_rec);
4474 /* Do the artificial defs and uses. */
4475 df_bb_refs_collect (&collection_rec, bb);
4476 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4477 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4478 df_free_collection_rec (&collection_rec);
4480 return true;
4484 /* Returns true if the entry block has correct and complete df_ref set.
4485 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4487 static bool
4488 df_entry_block_bitmap_verify (bool abort_if_fail)
4490 bitmap_head entry_block_defs;
4491 bool is_eq;
4493 bitmap_initialize (&entry_block_defs, &df_bitmap_obstack);
4494 df_get_entry_block_def_set (&entry_block_defs);
4496 is_eq = bitmap_equal_p (&entry_block_defs, df->entry_block_defs);
4498 if (!is_eq && abort_if_fail)
4500 fprintf (stderr, "entry_block_defs = ");
4501 df_print_regset (stderr, &entry_block_defs);
4502 fprintf (stderr, "df->entry_block_defs = ");
4503 df_print_regset (stderr, df->entry_block_defs);
4504 gcc_assert (0);
4507 bitmap_clear (&entry_block_defs);
4509 return is_eq;
4513 /* Returns true if the exit block has correct and complete df_ref set.
4514 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4516 static bool
4517 df_exit_block_bitmap_verify (bool abort_if_fail)
4519 bitmap_head exit_block_uses;
4520 bool is_eq;
4522 bitmap_initialize (&exit_block_uses, &df_bitmap_obstack);
4523 df_get_exit_block_use_set (&exit_block_uses);
4525 is_eq = bitmap_equal_p (&exit_block_uses, df->exit_block_uses);
4527 if (!is_eq && abort_if_fail)
4529 fprintf (stderr, "exit_block_uses = ");
4530 df_print_regset (stderr, &exit_block_uses);
4531 fprintf (stderr, "df->exit_block_uses = ");
4532 df_print_regset (stderr, df->exit_block_uses);
4533 gcc_assert (0);
4536 bitmap_clear (&exit_block_uses);
4538 return is_eq;
4542 /* Return true if df_ref information for all insns in all blocks are
4543 correct and complete. */
4545 void
4546 df_scan_verify (void)
4548 unsigned int i;
4549 basic_block bb;
4550 bitmap_head regular_block_artificial_uses;
4551 bitmap_head eh_block_artificial_uses;
4553 if (!df)
4554 return;
4556 /* Verification is a 4 step process. */
4558 /* (1) All of the refs are marked by going through the reg chains. */
4559 for (i = 0; i < DF_REG_SIZE (df); i++)
4561 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4562 == DF_REG_DEF_COUNT(i));
4563 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4564 == DF_REG_USE_COUNT(i));
4565 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4566 == DF_REG_EQ_USE_COUNT(i));
4569 /* (2) There are various bitmaps whose value may change over the
4570 course of the compilation. This step recomputes them to make
4571 sure that they have not slipped out of date. */
4572 bitmap_initialize (&regular_block_artificial_uses, &df_bitmap_obstack);
4573 bitmap_initialize (&eh_block_artificial_uses, &df_bitmap_obstack);
4575 df_get_regular_block_artificial_uses (&regular_block_artificial_uses);
4576 df_get_eh_block_artificial_uses (&eh_block_artificial_uses);
4578 bitmap_ior_into (&eh_block_artificial_uses,
4579 &regular_block_artificial_uses);
4581 /* Check artificial_uses bitmaps didn't change. */
4582 gcc_assert (bitmap_equal_p (&regular_block_artificial_uses,
4583 &df->regular_block_artificial_uses));
4584 gcc_assert (bitmap_equal_p (&eh_block_artificial_uses,
4585 &df->eh_block_artificial_uses));
4587 bitmap_clear (&regular_block_artificial_uses);
4588 bitmap_clear (&eh_block_artificial_uses);
4590 /* Verify entry block and exit block. These only verify the bitmaps,
4591 the refs are verified in df_bb_verify. */
4592 df_entry_block_bitmap_verify (true);
4593 df_exit_block_bitmap_verify (true);
4595 /* (3) All of the insns in all of the blocks are traversed and the
4596 marks are cleared both in the artificial refs attached to the
4597 blocks and the real refs inside the insns. It is a failure to
4598 clear a mark that has not been set as this means that the ref in
4599 the block or insn was not in the reg chain. */
4601 FOR_ALL_BB (bb)
4602 df_bb_verify (bb);
4604 /* (4) See if all reg chains are traversed a second time. This time
4605 a check is made that the marks are clear. A set mark would be a
4606 from a reg that is not in any insn or basic block. */
4608 for (i = 0; i < DF_REG_SIZE (df); i++)
4610 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4611 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4612 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));