aarch64.md (*adds_<optab><mode>_multp2): New pattern.
[official-gcc.git] / gcc / df-scan.c
blobe8ee1892ef873692cce6369349f8f379a38abb1b
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
3 Originally contributed by Michael P. Hayes
4 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
5 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
6 and Kenneth Zadeck (zadeck@naturalbridge.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "alloc-pool.h"
35 #include "flags.h"
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "sbitmap.h"
39 #include "bitmap.h"
40 #include "dumpfile.h"
41 #include "tree.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "df.h"
45 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
48 typedef struct df_mw_hardreg *df_mw_hardreg_ptr;
51 #ifndef HAVE_epilogue
52 #define HAVE_epilogue 0
53 #endif
54 #ifndef HAVE_prologue
55 #define HAVE_prologue 0
56 #endif
57 #ifndef HAVE_sibcall_epilogue
58 #define HAVE_sibcall_epilogue 0
59 #endif
61 #ifndef EPILOGUE_USES
62 #define EPILOGUE_USES(REGNO) 0
63 #endif
65 /* The following two macros free the vecs that hold either the refs or
66 the mw refs. They are a little tricky because the vec has 0
67 elements is special and is not to be freed. */
68 #define df_scan_free_ref_vec(V) \
69 do { \
70 if (V && *V) \
71 free (V); \
72 } while (0)
74 #define df_scan_free_mws_vec(V) \
75 do { \
76 if (V && *V) \
77 free (V); \
78 } while (0)
80 /* The set of hard registers in eliminables[i].from. */
82 static HARD_REG_SET elim_reg_set;
84 /* Initialize ur_in and ur_out as if all hard registers were partially
85 available. */
87 struct df_collection_rec
89 vec<df_ref, va_stack> def_vec;
90 vec<df_ref, va_stack> use_vec;
91 vec<df_ref, va_stack> eq_use_vec;
92 vec<df_mw_hardreg_ptr, va_stack> mw_vec;
95 static df_ref df_null_ref_rec[1];
96 static struct df_mw_hardreg * df_null_mw_rec[1];
98 static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
99 rtx, rtx *,
100 basic_block, struct df_insn_info *,
101 enum df_ref_type, int ref_flags);
102 static void df_def_record_1 (struct df_collection_rec *, rtx *,
103 basic_block, struct df_insn_info *,
104 int ref_flags);
105 static void df_defs_record (struct df_collection_rec *, rtx,
106 basic_block, struct df_insn_info *,
107 int ref_flags);
108 static void df_uses_record (struct df_collection_rec *,
109 rtx *, enum df_ref_type,
110 basic_block, struct df_insn_info *,
111 int ref_flags);
113 static void df_install_ref_incremental (df_ref);
114 static df_ref df_ref_create_structure (enum df_ref_class,
115 struct df_collection_rec *, rtx, rtx *,
116 basic_block, struct df_insn_info *,
117 enum df_ref_type, int ref_flags);
118 static void df_insn_refs_collect (struct df_collection_rec*,
119 basic_block, struct df_insn_info *);
120 static void df_canonize_collection_rec (struct df_collection_rec *);
122 static void df_get_regular_block_artificial_uses (bitmap);
123 static void df_get_eh_block_artificial_uses (bitmap);
125 static void df_record_entry_block_defs (bitmap);
126 static void df_record_exit_block_uses (bitmap);
127 static void df_get_exit_block_use_set (bitmap);
128 static void df_get_entry_block_def_set (bitmap);
129 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
130 static void df_ref_chain_delete_du_chain (df_ref *);
131 static void df_ref_chain_delete (df_ref *);
133 static void df_refs_add_to_chains (struct df_collection_rec *,
134 basic_block, rtx);
136 static bool df_insn_refs_verify (struct df_collection_rec *, basic_block, rtx, bool);
137 static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
138 static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
139 static void df_install_ref (df_ref, struct df_reg_info *,
140 struct df_ref_info *, bool);
142 static int df_ref_compare (const void *, const void *);
143 static int df_mw_compare (const void *, const void *);
145 static void df_insn_info_delete (unsigned int);
147 /* Indexed by hardware reg number, is true if that register is ever
148 used in the current function.
150 In df-scan.c, this is set up to record the hard regs used
151 explicitly. Reload adds in the hard regs used for holding pseudo
152 regs. Final uses it to generate the code in the function prologue
153 and epilogue to save and restore registers as needed. */
155 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
157 /*----------------------------------------------------------------------------
158 SCANNING DATAFLOW PROBLEM
160 There are several ways in which scanning looks just like the other
161 dataflow problems. It shares the all the mechanisms for local info
162 as well as basic block info. Where it differs is when and how often
163 it gets run. It also has no need for the iterative solver.
164 ----------------------------------------------------------------------------*/
166 /* Problem data for the scanning dataflow function. */
167 struct df_scan_problem_data
169 alloc_pool ref_base_pool;
170 alloc_pool ref_artificial_pool;
171 alloc_pool ref_regular_pool;
172 alloc_pool insn_pool;
173 alloc_pool reg_pool;
174 alloc_pool mw_reg_pool;
175 bitmap_obstack reg_bitmaps;
176 bitmap_obstack insn_bitmaps;
179 typedef struct df_scan_bb_info *df_scan_bb_info_t;
182 /* Internal function to shut down the scanning problem. */
183 static void
184 df_scan_free_internal (void)
186 struct df_scan_problem_data *problem_data
187 = (struct df_scan_problem_data *) df_scan->problem_data;
188 unsigned int i;
189 basic_block bb;
191 /* The vectors that hold the refs are not pool allocated because
192 they come in many sizes. This makes them impossible to delete
193 all at once. */
194 for (i = 0; i < DF_INSN_SIZE(); i++)
196 struct df_insn_info *insn_info = DF_INSN_UID_GET(i);
197 /* Skip the insns that have no insn_info or have been
198 deleted. */
199 if (insn_info)
201 df_scan_free_ref_vec (insn_info->defs);
202 df_scan_free_ref_vec (insn_info->uses);
203 df_scan_free_ref_vec (insn_info->eq_uses);
204 df_scan_free_mws_vec (insn_info->mw_hardregs);
208 FOR_ALL_BB (bb)
210 unsigned int bb_index = bb->index;
211 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
212 if (bb_info)
214 df_scan_free_ref_vec (bb_info->artificial_defs);
215 df_scan_free_ref_vec (bb_info->artificial_uses);
219 free (df->def_info.refs);
220 free (df->def_info.begin);
221 free (df->def_info.count);
222 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
224 free (df->use_info.refs);
225 free (df->use_info.begin);
226 free (df->use_info.count);
227 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
229 free (df->def_regs);
230 df->def_regs = NULL;
231 free (df->use_regs);
232 df->use_regs = NULL;
233 free (df->eq_use_regs);
234 df->eq_use_regs = NULL;
235 df->regs_size = 0;
236 DF_REG_SIZE(df) = 0;
238 free (df->insns);
239 df->insns = NULL;
240 DF_INSN_SIZE () = 0;
242 free (df_scan->block_info);
243 df_scan->block_info = NULL;
244 df_scan->block_info_size = 0;
246 bitmap_clear (&df->hardware_regs_used);
247 bitmap_clear (&df->regular_block_artificial_uses);
248 bitmap_clear (&df->eh_block_artificial_uses);
249 BITMAP_FREE (df->entry_block_defs);
250 BITMAP_FREE (df->exit_block_uses);
251 bitmap_clear (&df->insns_to_delete);
252 bitmap_clear (&df->insns_to_rescan);
253 bitmap_clear (&df->insns_to_notes_rescan);
255 free_alloc_pool (problem_data->ref_base_pool);
256 free_alloc_pool (problem_data->ref_artificial_pool);
257 free_alloc_pool (problem_data->ref_regular_pool);
258 free_alloc_pool (problem_data->insn_pool);
259 free_alloc_pool (problem_data->reg_pool);
260 free_alloc_pool (problem_data->mw_reg_pool);
261 bitmap_obstack_release (&problem_data->reg_bitmaps);
262 bitmap_obstack_release (&problem_data->insn_bitmaps);
263 free (df_scan->problem_data);
267 /* Free basic block info. */
269 static void
270 df_scan_free_bb_info (basic_block bb, void *vbb_info)
272 struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
273 unsigned int bb_index = bb->index;
275 /* See if bb_info is initialized. */
276 if (bb_info->artificial_defs)
278 rtx insn;
279 FOR_BB_INSNS (bb, insn)
281 if (INSN_P (insn))
282 df_insn_info_delete (INSN_UID (insn));
285 if (bb_index < df_scan->block_info_size)
286 bb_info = df_scan_get_bb_info (bb_index);
288 /* Get rid of any artificial uses or defs. */
289 if (bb_info->artificial_defs)
291 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
292 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
293 df_ref_chain_delete (bb_info->artificial_defs);
294 df_ref_chain_delete (bb_info->artificial_uses);
295 bb_info->artificial_defs = NULL;
296 bb_info->artificial_uses = NULL;
302 /* Allocate the problem data for the scanning problem. This should be
303 called when the problem is created or when the entire function is to
304 be rescanned. */
305 void
306 df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
308 struct df_scan_problem_data *problem_data;
309 unsigned int insn_num = get_max_uid () + 1;
310 unsigned int block_size = 512;
311 basic_block bb;
313 /* Given the number of pools, this is really faster than tearing
314 everything apart. */
315 if (df_scan->problem_data)
316 df_scan_free_internal ();
318 problem_data = XNEW (struct df_scan_problem_data);
319 df_scan->problem_data = problem_data;
320 df_scan->computed = true;
322 problem_data->ref_base_pool
323 = create_alloc_pool ("df_scan ref base",
324 sizeof (struct df_base_ref), block_size);
325 problem_data->ref_artificial_pool
326 = create_alloc_pool ("df_scan ref artificial",
327 sizeof (struct df_artificial_ref), block_size);
328 problem_data->ref_regular_pool
329 = create_alloc_pool ("df_scan ref regular",
330 sizeof (struct df_regular_ref), block_size);
331 problem_data->insn_pool
332 = create_alloc_pool ("df_scan insn",
333 sizeof (struct df_insn_info), block_size);
334 problem_data->reg_pool
335 = create_alloc_pool ("df_scan reg",
336 sizeof (struct df_reg_info), block_size);
337 problem_data->mw_reg_pool
338 = create_alloc_pool ("df_scan mw_reg",
339 sizeof (struct df_mw_hardreg), block_size / 16);
341 bitmap_obstack_initialize (&problem_data->reg_bitmaps);
342 bitmap_obstack_initialize (&problem_data->insn_bitmaps);
344 insn_num += insn_num / 4;
345 df_grow_reg_info ();
347 df_grow_insn_info ();
348 df_grow_bb_info (df_scan);
350 FOR_ALL_BB (bb)
352 unsigned int bb_index = bb->index;
353 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
354 bb_info->artificial_defs = NULL;
355 bb_info->artificial_uses = NULL;
358 bitmap_initialize (&df->hardware_regs_used, &problem_data->reg_bitmaps);
359 bitmap_initialize (&df->regular_block_artificial_uses, &problem_data->reg_bitmaps);
360 bitmap_initialize (&df->eh_block_artificial_uses, &problem_data->reg_bitmaps);
361 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
362 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
363 bitmap_initialize (&df->insns_to_delete, &problem_data->insn_bitmaps);
364 bitmap_initialize (&df->insns_to_rescan, &problem_data->insn_bitmaps);
365 bitmap_initialize (&df->insns_to_notes_rescan, &problem_data->insn_bitmaps);
366 df_scan->optional_p = false;
370 /* Free all of the data associated with the scan problem. */
372 static void
373 df_scan_free (void)
375 if (df_scan->problem_data)
376 df_scan_free_internal ();
378 if (df->blocks_to_analyze)
380 BITMAP_FREE (df->blocks_to_analyze);
381 df->blocks_to_analyze = NULL;
384 free (df_scan);
387 /* Dump the preamble for DF_SCAN dump. */
388 static void
389 df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
391 int i;
392 int dcount = 0;
393 int ucount = 0;
394 int ecount = 0;
395 int icount = 0;
396 int ccount = 0;
397 basic_block bb;
398 rtx insn;
400 fprintf (file, ";; invalidated by call \t");
401 df_print_regset (file, regs_invalidated_by_call_regset);
402 fprintf (file, ";; hardware regs used \t");
403 df_print_regset (file, &df->hardware_regs_used);
404 fprintf (file, ";; regular block artificial uses \t");
405 df_print_regset (file, &df->regular_block_artificial_uses);
406 fprintf (file, ";; eh block artificial uses \t");
407 df_print_regset (file, &df->eh_block_artificial_uses);
408 fprintf (file, ";; entry block defs \t");
409 df_print_regset (file, df->entry_block_defs);
410 fprintf (file, ";; exit block uses \t");
411 df_print_regset (file, df->exit_block_uses);
412 fprintf (file, ";; regs ever live \t");
413 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
414 if (df_regs_ever_live_p (i))
415 fprintf (file, " %d[%s]", i, reg_names[i]);
416 fprintf (file, "\n;; ref usage \t");
418 for (i = 0; i < (int)df->regs_inited; i++)
419 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
421 const char * sep = "";
423 fprintf (file, "r%d={", i);
424 if (DF_REG_DEF_COUNT (i))
426 fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
427 sep = ",";
428 dcount += DF_REG_DEF_COUNT (i);
430 if (DF_REG_USE_COUNT (i))
432 fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
433 sep = ",";
434 ucount += DF_REG_USE_COUNT (i);
436 if (DF_REG_EQ_USE_COUNT (i))
438 fprintf (file, "%s%de", sep, DF_REG_EQ_USE_COUNT (i));
439 ecount += DF_REG_EQ_USE_COUNT (i);
441 fprintf (file, "} ");
444 FOR_EACH_BB (bb)
445 FOR_BB_INSNS (bb, insn)
446 if (INSN_P (insn))
448 if (CALL_P (insn))
449 ccount++;
450 else
451 icount++;
454 fprintf (file, "\n;; total ref usage %d{%dd,%du,%de}"
455 " in %d{%d regular + %d call} insns.\n",
456 dcount + ucount + ecount, dcount, ucount, ecount,
457 icount + ccount, icount, ccount);
460 /* Dump the bb_info for a given basic block. */
461 static void
462 df_scan_start_block (basic_block bb, FILE *file)
464 struct df_scan_bb_info *bb_info
465 = df_scan_get_bb_info (bb->index);
467 if (bb_info)
469 fprintf (file, ";; bb %d artificial_defs: ", bb->index);
470 df_refs_chain_dump (bb_info->artificial_defs, true, file);
471 fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
472 df_refs_chain_dump (bb_info->artificial_uses, true, file);
473 fprintf (file, "\n");
475 #if 0
477 rtx insn;
478 FOR_BB_INSNS (bb, insn)
479 if (INSN_P (insn))
480 df_insn_debug (insn, false, file);
482 #endif
485 static struct df_problem problem_SCAN =
487 DF_SCAN, /* Problem id. */
488 DF_NONE, /* Direction. */
489 df_scan_alloc, /* Allocate the problem specific data. */
490 NULL, /* Reset global information. */
491 df_scan_free_bb_info, /* Free basic block info. */
492 NULL, /* Local compute function. */
493 NULL, /* Init the solution specific data. */
494 NULL, /* Iterative solver. */
495 NULL, /* Confluence operator 0. */
496 NULL, /* Confluence operator n. */
497 NULL, /* Transfer function. */
498 NULL, /* Finalize function. */
499 df_scan_free, /* Free all of the problem information. */
500 NULL, /* Remove this problem from the stack of dataflow problems. */
501 df_scan_start_dump, /* Debugging. */
502 df_scan_start_block, /* Debugging start block. */
503 NULL, /* Debugging end block. */
504 NULL, /* Debugging start insn. */
505 NULL, /* Debugging end insn. */
506 NULL, /* Incremental solution verify start. */
507 NULL, /* Incremental solution verify end. */
508 NULL, /* Dependent problem. */
509 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
510 TV_DF_SCAN, /* Timing variable. */
511 false /* Reset blocks on dropping out of blocks_to_analyze. */
515 /* Create a new DATAFLOW instance and add it to an existing instance
516 of DF. The returned structure is what is used to get at the
517 solution. */
519 void
520 df_scan_add_problem (void)
522 df_add_problem (&problem_SCAN);
526 /*----------------------------------------------------------------------------
527 Storage Allocation Utilities
528 ----------------------------------------------------------------------------*/
531 /* First, grow the reg_info information. If the current size is less than
532 the number of pseudos, grow to 25% more than the number of
533 pseudos.
535 Second, assure that all of the slots up to max_reg_num have been
536 filled with reg_info structures. */
538 void
539 df_grow_reg_info (void)
541 unsigned int max_reg = max_reg_num ();
542 unsigned int new_size = max_reg;
543 struct df_scan_problem_data *problem_data
544 = (struct df_scan_problem_data *) df_scan->problem_data;
545 unsigned int i;
547 if (df->regs_size < new_size)
549 new_size += new_size / 4;
550 df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
551 df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
552 df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
553 new_size);
554 df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
555 df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
556 df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
557 df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
558 df->regs_size = new_size;
561 for (i = df->regs_inited; i < max_reg; i++)
563 struct df_reg_info *reg_info;
565 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
566 memset (reg_info, 0, sizeof (struct df_reg_info));
567 df->def_regs[i] = reg_info;
568 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
569 memset (reg_info, 0, sizeof (struct df_reg_info));
570 df->use_regs[i] = reg_info;
571 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
572 memset (reg_info, 0, sizeof (struct df_reg_info));
573 df->eq_use_regs[i] = reg_info;
574 df->def_info.begin[i] = 0;
575 df->def_info.count[i] = 0;
576 df->use_info.begin[i] = 0;
577 df->use_info.count[i] = 0;
580 df->regs_inited = max_reg;
584 /* Grow the ref information. */
586 static void
587 df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
589 if (ref_info->refs_size < new_size)
591 ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
592 memset (ref_info->refs + ref_info->refs_size, 0,
593 (new_size - ref_info->refs_size) *sizeof (df_ref));
594 ref_info->refs_size = new_size;
599 /* Check and grow the ref information if necessary. This routine
600 guarantees total_size + BITMAP_ADDEND amount of entries in refs
601 array. It updates ref_info->refs_size only and does not change
602 ref_info->total_size. */
604 static void
605 df_check_and_grow_ref_info (struct df_ref_info *ref_info,
606 unsigned bitmap_addend)
608 if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
610 int new_size = ref_info->total_size + bitmap_addend;
611 new_size += ref_info->total_size / 4;
612 df_grow_ref_info (ref_info, new_size);
617 /* Grow the ref information. If the current size is less than the
618 number of instructions, grow to 25% more than the number of
619 instructions. */
621 void
622 df_grow_insn_info (void)
624 unsigned int new_size = get_max_uid () + 1;
625 if (DF_INSN_SIZE () < new_size)
627 new_size += new_size / 4;
628 df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
629 memset (df->insns + df->insns_size, 0,
630 (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
631 DF_INSN_SIZE () = new_size;
638 /*----------------------------------------------------------------------------
639 PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
640 ----------------------------------------------------------------------------*/
642 /* Rescan all of the block_to_analyze or all of the blocks in the
643 function if df_set_blocks if blocks_to_analyze is NULL; */
645 void
646 df_scan_blocks (void)
648 basic_block bb;
650 df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
651 df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
653 df_get_regular_block_artificial_uses (&df->regular_block_artificial_uses);
654 df_get_eh_block_artificial_uses (&df->eh_block_artificial_uses);
656 bitmap_ior_into (&df->eh_block_artificial_uses,
657 &df->regular_block_artificial_uses);
659 /* ENTRY and EXIT blocks have special defs/uses. */
660 df_get_entry_block_def_set (df->entry_block_defs);
661 df_record_entry_block_defs (df->entry_block_defs);
662 df_get_exit_block_use_set (df->exit_block_uses);
663 df_record_exit_block_uses (df->exit_block_uses);
664 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
665 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
667 /* Regular blocks */
668 FOR_EACH_BB (bb)
670 unsigned int bb_index = bb->index;
671 df_bb_refs_record (bb_index, true);
675 /* Create new refs under address LOC within INSN. This function is
676 only used externally. REF_FLAGS must be either 0 or DF_REF_IN_NOTE,
677 depending on whether LOC is inside PATTERN (INSN) or a note. */
679 void
680 df_uses_create (rtx *loc, rtx insn, int ref_flags)
682 gcc_assert (!(ref_flags & ~DF_REF_IN_NOTE));
683 df_uses_record (NULL, loc, DF_REF_REG_USE,
684 BLOCK_FOR_INSN (insn),
685 DF_INSN_INFO_GET (insn),
686 ref_flags);
689 /* Create a new ref of type DF_REF_TYPE for register REG at address
690 LOC within INSN of BB. This function is only used externally. */
692 df_ref
693 df_ref_create (rtx reg, rtx *loc, rtx insn,
694 basic_block bb,
695 enum df_ref_type ref_type,
696 int ref_flags)
698 enum df_ref_class cl;
700 df_grow_reg_info ();
702 /* You cannot hack artificial refs. */
703 gcc_assert (insn);
705 if (loc)
706 cl = DF_REF_REGULAR;
707 else
708 cl = DF_REF_BASE;
710 return df_ref_create_structure (cl, NULL, reg, loc, bb,
711 DF_INSN_INFO_GET (insn),
712 ref_type, ref_flags);
715 static void
716 df_install_ref_incremental (df_ref ref)
718 struct df_reg_info **reg_info;
719 struct df_ref_info *ref_info;
720 df_ref *ref_rec;
721 df_ref **ref_rec_ptr;
722 unsigned int count = 0;
723 bool add_to_table;
725 rtx insn = DF_REF_INSN (ref);
726 basic_block bb = BLOCK_FOR_INSN (insn);
728 if (DF_REF_REG_DEF_P (ref))
730 reg_info = df->def_regs;
731 ref_info = &df->def_info;
732 ref_rec_ptr = &DF_INSN_DEFS (insn);
733 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
735 else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
737 reg_info = df->eq_use_regs;
738 ref_info = &df->use_info;
739 ref_rec_ptr = &DF_INSN_EQ_USES (insn);
740 switch (ref_info->ref_order)
742 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
743 case DF_REF_ORDER_BY_REG_WITH_NOTES:
744 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
745 add_to_table = true;
746 break;
747 default:
748 add_to_table = false;
749 break;
752 else
754 reg_info = df->use_regs;
755 ref_info = &df->use_info;
756 ref_rec_ptr = &DF_INSN_USES (insn);
757 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
760 /* Do not add if ref is not in the right blocks. */
761 if (add_to_table && df->analyze_subset)
762 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
764 df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
766 if (add_to_table)
767 switch (ref_info->ref_order)
769 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
770 case DF_REF_ORDER_BY_REG_WITH_NOTES:
771 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
772 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
773 break;
774 default:
775 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
776 break;
779 ref_rec = *ref_rec_ptr;
780 while (*ref_rec)
782 count++;
783 ref_rec++;
786 ref_rec = *ref_rec_ptr;
787 if (count)
789 ref_rec = XRESIZEVEC (df_ref, ref_rec, count+2);
790 *ref_rec_ptr = ref_rec;
791 ref_rec[count] = ref;
792 ref_rec[count+1] = NULL;
793 qsort (ref_rec, count + 1, sizeof (df_ref), df_ref_compare);
795 else
797 df_ref *ref_rec = XNEWVEC (df_ref, 2);
798 ref_rec[0] = ref;
799 ref_rec[1] = NULL;
800 *ref_rec_ptr = ref_rec;
803 #if 0
804 if (dump_file)
806 fprintf (dump_file, "adding ref ");
807 df_ref_debug (ref, dump_file);
809 #endif
810 /* By adding the ref directly, df_insn_rescan my not find any
811 differences even though the block will have changed. So we need
812 to mark the block dirty ourselves. */
813 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
814 df_set_bb_dirty (bb);
819 /*----------------------------------------------------------------------------
820 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
821 ----------------------------------------------------------------------------*/
823 static void
824 df_free_ref (df_ref ref)
826 struct df_scan_problem_data *problem_data
827 = (struct df_scan_problem_data *) df_scan->problem_data;
829 switch (DF_REF_CLASS (ref))
831 case DF_REF_BASE:
832 pool_free (problem_data->ref_base_pool, ref);
833 break;
835 case DF_REF_ARTIFICIAL:
836 pool_free (problem_data->ref_artificial_pool, ref);
837 break;
839 case DF_REF_REGULAR:
840 pool_free (problem_data->ref_regular_pool, ref);
841 break;
846 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
847 Also delete the def-use or use-def chain if it exists. */
849 static void
850 df_reg_chain_unlink (df_ref ref)
852 df_ref next = DF_REF_NEXT_REG (ref);
853 df_ref prev = DF_REF_PREV_REG (ref);
854 int id = DF_REF_ID (ref);
855 struct df_reg_info *reg_info;
856 df_ref *refs = NULL;
858 if (DF_REF_REG_DEF_P (ref))
860 int regno = DF_REF_REGNO (ref);
861 reg_info = DF_REG_DEF_GET (regno);
862 refs = df->def_info.refs;
864 else
866 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
868 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
869 switch (df->use_info.ref_order)
871 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
872 case DF_REF_ORDER_BY_REG_WITH_NOTES:
873 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
874 refs = df->use_info.refs;
875 break;
876 default:
877 break;
880 else
882 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
883 refs = df->use_info.refs;
887 if (refs)
889 if (df->analyze_subset)
891 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
892 refs[id] = NULL;
894 else
895 refs[id] = NULL;
898 /* Delete any def-use or use-def chains that start here. It is
899 possible that there is trash in this field. This happens for
900 insns that have been deleted when rescanning has been deferred
901 and the chain problem has also been deleted. The chain tear down
902 code skips deleted insns. */
903 if (df_chain && DF_REF_CHAIN (ref))
904 df_chain_unlink (ref);
906 reg_info->n_refs--;
907 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
909 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
910 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
913 /* Unlink from the reg chain. If there is no prev, this is the
914 first of the list. If not, just join the next and prev. */
915 if (prev)
916 DF_REF_NEXT_REG (prev) = next;
917 else
919 gcc_assert (reg_info->reg_chain == ref);
920 reg_info->reg_chain = next;
922 if (next)
923 DF_REF_PREV_REG (next) = prev;
925 df_free_ref (ref);
929 /* Remove REF from VEC. */
931 static void
932 df_ref_compress_rec (df_ref **vec_ptr, df_ref ref)
934 df_ref *vec = *vec_ptr;
936 if (vec[1])
938 while (*vec && *vec != ref)
939 vec++;
941 while (*vec)
943 *vec = *(vec+1);
944 vec++;
947 else
949 free (vec);
950 *vec_ptr = df_null_ref_rec;
955 /* Unlink REF from all def-use/use-def chains, etc. */
957 void
958 df_ref_remove (df_ref ref)
960 #if 0
961 if (dump_file)
963 fprintf (dump_file, "removing ref ");
964 df_ref_debug (ref, dump_file);
966 #endif
968 if (DF_REF_REG_DEF_P (ref))
970 if (DF_REF_IS_ARTIFICIAL (ref))
972 struct df_scan_bb_info *bb_info
973 = df_scan_get_bb_info (DF_REF_BBNO (ref));
974 df_ref_compress_rec (&bb_info->artificial_defs, ref);
976 else
978 unsigned int uid = DF_REF_INSN_UID (ref);
979 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
980 df_ref_compress_rec (&insn_rec->defs, ref);
983 else
985 if (DF_REF_IS_ARTIFICIAL (ref))
987 struct df_scan_bb_info *bb_info
988 = df_scan_get_bb_info (DF_REF_BBNO (ref));
989 df_ref_compress_rec (&bb_info->artificial_uses, ref);
991 else
993 unsigned int uid = DF_REF_INSN_UID (ref);
994 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
996 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
997 df_ref_compress_rec (&insn_rec->eq_uses, ref);
998 else
999 df_ref_compress_rec (&insn_rec->uses, ref);
1003 /* By deleting the ref directly, df_insn_rescan my not find any
1004 differences even though the block will have changed. So we need
1005 to mark the block dirty ourselves. */
1006 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
1007 df_set_bb_dirty (DF_REF_BB (ref));
1008 df_reg_chain_unlink (ref);
1012 /* Create the insn record for INSN. If there was one there, zero it
1013 out. */
1015 struct df_insn_info *
1016 df_insn_create_insn_record (rtx insn)
1018 struct df_scan_problem_data *problem_data
1019 = (struct df_scan_problem_data *) df_scan->problem_data;
1020 struct df_insn_info *insn_rec;
1022 df_grow_insn_info ();
1023 insn_rec = DF_INSN_INFO_GET (insn);
1024 if (!insn_rec)
1026 insn_rec = (struct df_insn_info *) pool_alloc (problem_data->insn_pool);
1027 DF_INSN_INFO_SET (insn, insn_rec);
1029 memset (insn_rec, 0, sizeof (struct df_insn_info));
1030 insn_rec->insn = insn;
1031 return insn_rec;
1035 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
1037 static void
1038 df_ref_chain_delete_du_chain (df_ref *ref_rec)
1040 while (*ref_rec)
1042 df_ref ref = *ref_rec;
1043 /* CHAIN is allocated by DF_CHAIN. So make sure to
1044 pass df_scan instance for the problem. */
1045 if (DF_REF_CHAIN (ref))
1046 df_chain_unlink (ref);
1047 ref_rec++;
1052 /* Delete all refs in the ref chain. */
1054 static void
1055 df_ref_chain_delete (df_ref *ref_rec)
1057 df_ref *start = ref_rec;
1058 while (*ref_rec)
1060 df_reg_chain_unlink (*ref_rec);
1061 ref_rec++;
1064 /* If the list is empty, it has a special shared element that is not
1065 to be deleted. */
1066 if (*start)
1067 free (start);
1071 /* Delete the hardreg chain. */
1073 static void
1074 df_mw_hardreg_chain_delete (struct df_mw_hardreg **hardregs)
1076 struct df_scan_problem_data *problem_data;
1078 if (!hardregs)
1079 return;
1081 problem_data = (struct df_scan_problem_data *) df_scan->problem_data;
1083 while (*hardregs)
1085 pool_free (problem_data->mw_reg_pool, *hardregs);
1086 hardregs++;
1091 /* Delete all of the refs information from the insn with UID.
1092 Internal helper for df_insn_delete, df_insn_rescan, and other
1093 df-scan routines that don't have to work in deferred mode
1094 and do not have to mark basic blocks for re-processing. */
1096 static void
1097 df_insn_info_delete (unsigned int uid)
1099 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1101 bitmap_clear_bit (&df->insns_to_delete, uid);
1102 bitmap_clear_bit (&df->insns_to_rescan, uid);
1103 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1104 if (insn_info)
1106 struct df_scan_problem_data *problem_data
1107 = (struct df_scan_problem_data *) df_scan->problem_data;
1109 /* In general, notes do not have the insn_info fields
1110 initialized. However, combine deletes insns by changing them
1111 to notes. How clever. So we cannot just check if it is a
1112 valid insn before short circuiting this code, we need to see
1113 if we actually initialized it. */
1114 if (insn_info->defs)
1116 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1118 if (df_chain)
1120 df_ref_chain_delete_du_chain (insn_info->defs);
1121 df_ref_chain_delete_du_chain (insn_info->uses);
1122 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1125 df_ref_chain_delete (insn_info->defs);
1126 df_ref_chain_delete (insn_info->uses);
1127 df_ref_chain_delete (insn_info->eq_uses);
1128 df_scan_free_mws_vec (insn_info->mw_hardregs);
1130 pool_free (problem_data->insn_pool, insn_info);
1131 DF_INSN_UID_SET (uid, NULL);
1135 /* Delete all of the refs information from INSN, either right now
1136 or marked for later in deferred mode. */
1138 void
1139 df_insn_delete (rtx insn)
1141 unsigned int uid;
1142 basic_block bb;
1144 gcc_checking_assert (INSN_P (insn));
1146 if (!df)
1147 return;
1149 uid = INSN_UID (insn);
1150 bb = BLOCK_FOR_INSN (insn);
1152 /* ??? bb can be NULL after pass_free_cfg. At that point, DF should
1153 not exist anymore (as mentioned in df-core.c: "The only requirement
1154 [for DF] is that there be a correct control flow graph." Clearly
1155 that isn't the case after pass_free_cfg. But DF is freed much later
1156 because some back-ends want to use DF info even though the CFG is
1157 already gone. It's not clear to me whether that is safe, actually.
1158 In any case, we expect BB to be non-NULL at least up to register
1159 allocation, so disallow a non-NULL BB up to there. Not perfect
1160 but better than nothing... */
1161 /* ??? bb can also be NULL if lower-subreg.c:resolve_simple_mov emits
1162 an insn into a sequence and then does delete_insn on it. Not sure
1163 if that makes sense, but for now it means this assert cannot work.
1164 See PR56738.
1165 Disable for now but revisit before the end of GCC 4.9 stage1. */
1166 #if 0
1167 gcc_checking_assert (bb != NULL || reload_completed);
1168 #else
1169 if (bb == NULL)
1170 return;
1171 #endif
1173 df_grow_bb_info (df_scan);
1174 df_grow_reg_info ();
1176 /* The block must be marked as dirty now, rather than later as in
1177 df_insn_rescan and df_notes_rescan because it may not be there at
1178 rescanning time and the mark would blow up.
1179 DEBUG_INSNs do not make a block's data flow solution dirty (at
1180 worst the LUIDs are no longer contiguous). */
1181 if (bb != NULL && NONDEBUG_INSN_P (insn))
1182 df_set_bb_dirty (bb);
1184 /* The client has deferred rescanning. */
1185 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1187 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1188 if (insn_info)
1190 bitmap_clear_bit (&df->insns_to_rescan, uid);
1191 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1192 bitmap_set_bit (&df->insns_to_delete, uid);
1194 if (dump_file)
1195 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1196 return;
1199 if (dump_file)
1200 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1202 df_insn_info_delete (uid);
1206 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1208 static void
1209 df_free_collection_rec (struct df_collection_rec *collection_rec)
1211 unsigned int ix;
1212 struct df_scan_problem_data *problem_data
1213 = (struct df_scan_problem_data *) df_scan->problem_data;
1214 df_ref ref;
1215 struct df_mw_hardreg *mw;
1217 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
1218 df_free_ref (ref);
1219 FOR_EACH_VEC_ELT (collection_rec->use_vec, ix, ref)
1220 df_free_ref (ref);
1221 FOR_EACH_VEC_ELT (collection_rec->eq_use_vec, ix, ref)
1222 df_free_ref (ref);
1223 FOR_EACH_VEC_ELT (collection_rec->mw_vec, ix, mw)
1224 pool_free (problem_data->mw_reg_pool, mw);
1226 collection_rec->def_vec.release ();
1227 collection_rec->use_vec.release ();
1228 collection_rec->eq_use_vec.release ();
1229 collection_rec->mw_vec.release ();
1232 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1234 bool
1235 df_insn_rescan (rtx insn)
1237 unsigned int uid = INSN_UID (insn);
1238 struct df_insn_info *insn_info = NULL;
1239 basic_block bb = BLOCK_FOR_INSN (insn);
1240 struct df_collection_rec collection_rec;
1242 if ((!df) || (!INSN_P (insn)))
1243 return false;
1245 if (!bb)
1247 if (dump_file)
1248 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1249 return false;
1252 /* The client has disabled rescanning and plans to do it itself. */
1253 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1254 return false;
1256 df_grow_bb_info (df_scan);
1257 df_grow_reg_info ();
1259 insn_info = DF_INSN_UID_SAFE_GET (uid);
1261 /* The client has deferred rescanning. */
1262 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1264 if (!insn_info)
1266 insn_info = df_insn_create_insn_record (insn);
1267 insn_info->defs = df_null_ref_rec;
1268 insn_info->uses = df_null_ref_rec;
1269 insn_info->eq_uses = df_null_ref_rec;
1270 insn_info->mw_hardregs = df_null_mw_rec;
1272 if (dump_file)
1273 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1275 bitmap_clear_bit (&df->insns_to_delete, uid);
1276 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1277 bitmap_set_bit (&df->insns_to_rescan, INSN_UID (insn));
1278 return false;
1281 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
1282 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
1283 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
1284 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
1286 bitmap_clear_bit (&df->insns_to_delete, uid);
1287 bitmap_clear_bit (&df->insns_to_rescan, uid);
1288 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1289 if (insn_info)
1291 int luid;
1292 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1293 /* If there's no change, return false. */
1294 if (the_same)
1296 df_free_collection_rec (&collection_rec);
1297 if (dump_file)
1298 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1299 return false;
1301 if (dump_file)
1302 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1304 /* There's change - we need to delete the existing info.
1305 Since the insn isn't moved, we can salvage its LUID. */
1306 luid = DF_INSN_LUID (insn);
1307 df_insn_info_delete (uid);
1308 df_insn_create_insn_record (insn);
1309 DF_INSN_LUID (insn) = luid;
1311 else
1313 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1314 df_insn_refs_collect (&collection_rec, bb, insn_info);
1315 if (dump_file)
1316 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1319 df_refs_add_to_chains (&collection_rec, bb, insn);
1320 if (!DEBUG_INSN_P (insn))
1321 df_set_bb_dirty (bb);
1323 collection_rec.def_vec.release ();
1324 collection_rec.use_vec.release ();
1325 collection_rec.eq_use_vec.release ();
1326 collection_rec.mw_vec.release ();
1328 return true;
1331 /* Same as df_insn_rescan, but don't mark the basic block as
1332 dirty. */
1334 bool
1335 df_insn_rescan_debug_internal (rtx insn)
1337 unsigned int uid = INSN_UID (insn);
1338 struct df_insn_info *insn_info;
1340 gcc_assert (DEBUG_INSN_P (insn)
1341 && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1343 if (!df)
1344 return false;
1346 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1347 if (!insn_info)
1348 return false;
1350 if (dump_file)
1351 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1353 bitmap_clear_bit (&df->insns_to_delete, uid);
1354 bitmap_clear_bit (&df->insns_to_rescan, uid);
1355 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1357 if (!insn_info->defs)
1358 return false;
1360 if (insn_info->defs == df_null_ref_rec
1361 && insn_info->uses == df_null_ref_rec
1362 && insn_info->eq_uses == df_null_ref_rec
1363 && insn_info->mw_hardregs == df_null_mw_rec)
1364 return false;
1366 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1368 if (df_chain)
1370 df_ref_chain_delete_du_chain (insn_info->defs);
1371 df_ref_chain_delete_du_chain (insn_info->uses);
1372 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1375 df_ref_chain_delete (insn_info->defs);
1376 df_ref_chain_delete (insn_info->uses);
1377 df_ref_chain_delete (insn_info->eq_uses);
1378 df_scan_free_mws_vec (insn_info->mw_hardregs);
1380 insn_info->defs = df_null_ref_rec;
1381 insn_info->uses = df_null_ref_rec;
1382 insn_info->eq_uses = df_null_ref_rec;
1383 insn_info->mw_hardregs = df_null_mw_rec;
1385 return true;
1389 /* Rescan all of the insns in the function. Note that the artificial
1390 uses and defs are not touched. This function will destroy def-use
1391 or use-def chains. */
1393 void
1394 df_insn_rescan_all (void)
1396 bool no_insn_rescan = false;
1397 bool defer_insn_rescan = false;
1398 basic_block bb;
1399 bitmap_iterator bi;
1400 unsigned int uid;
1401 bitmap_head tmp;
1403 bitmap_initialize (&tmp, &df_bitmap_obstack);
1405 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1407 df_clear_flags (DF_NO_INSN_RESCAN);
1408 no_insn_rescan = true;
1411 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1413 df_clear_flags (DF_DEFER_INSN_RESCAN);
1414 defer_insn_rescan = true;
1417 bitmap_copy (&tmp, &df->insns_to_delete);
1418 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1420 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1421 if (insn_info)
1422 df_insn_info_delete (uid);
1425 bitmap_clear (&tmp);
1426 bitmap_clear (&df->insns_to_delete);
1427 bitmap_clear (&df->insns_to_rescan);
1428 bitmap_clear (&df->insns_to_notes_rescan);
1430 FOR_EACH_BB (bb)
1432 rtx insn;
1433 FOR_BB_INSNS (bb, insn)
1435 df_insn_rescan (insn);
1439 if (no_insn_rescan)
1440 df_set_flags (DF_NO_INSN_RESCAN);
1441 if (defer_insn_rescan)
1442 df_set_flags (DF_DEFER_INSN_RESCAN);
1446 /* Process all of the deferred rescans or deletions. */
1448 void
1449 df_process_deferred_rescans (void)
1451 bool no_insn_rescan = false;
1452 bool defer_insn_rescan = false;
1453 bitmap_iterator bi;
1454 unsigned int uid;
1455 bitmap_head tmp;
1457 bitmap_initialize (&tmp, &df_bitmap_obstack);
1459 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1461 df_clear_flags (DF_NO_INSN_RESCAN);
1462 no_insn_rescan = true;
1465 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1467 df_clear_flags (DF_DEFER_INSN_RESCAN);
1468 defer_insn_rescan = true;
1471 if (dump_file)
1472 fprintf (dump_file, "starting the processing of deferred insns\n");
1474 bitmap_copy (&tmp, &df->insns_to_delete);
1475 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1477 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1478 if (insn_info)
1479 df_insn_info_delete (uid);
1482 bitmap_copy (&tmp, &df->insns_to_rescan);
1483 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1485 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1486 if (insn_info)
1487 df_insn_rescan (insn_info->insn);
1490 bitmap_copy (&tmp, &df->insns_to_notes_rescan);
1491 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
1493 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1494 if (insn_info)
1495 df_notes_rescan (insn_info->insn);
1498 if (dump_file)
1499 fprintf (dump_file, "ending the processing of deferred insns\n");
1501 bitmap_clear (&tmp);
1502 bitmap_clear (&df->insns_to_delete);
1503 bitmap_clear (&df->insns_to_rescan);
1504 bitmap_clear (&df->insns_to_notes_rescan);
1506 if (no_insn_rescan)
1507 df_set_flags (DF_NO_INSN_RESCAN);
1508 if (defer_insn_rescan)
1509 df_set_flags (DF_DEFER_INSN_RESCAN);
1511 /* If someone changed regs_ever_live during this pass, fix up the
1512 entry and exit blocks. */
1513 if (df->redo_entry_and_exit)
1515 df_update_entry_exit_and_calls ();
1516 df->redo_entry_and_exit = false;
1521 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1522 the uses if INCLUDE_USES. Include the eq_uses if
1523 INCLUDE_EQ_USES. */
1525 static unsigned int
1526 df_count_refs (bool include_defs, bool include_uses,
1527 bool include_eq_uses)
1529 unsigned int regno;
1530 int size = 0;
1531 unsigned int m = df->regs_inited;
1533 for (regno = 0; regno < m; regno++)
1535 if (include_defs)
1536 size += DF_REG_DEF_COUNT (regno);
1537 if (include_uses)
1538 size += DF_REG_USE_COUNT (regno);
1539 if (include_eq_uses)
1540 size += DF_REG_EQ_USE_COUNT (regno);
1542 return size;
1546 /* Take build ref table for either the uses or defs from the reg-use
1547 or reg-def chains. This version processes the refs in reg order
1548 which is likely to be best if processing the whole function. */
1550 static void
1551 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1552 bool include_defs,
1553 bool include_uses,
1554 bool include_eq_uses)
1556 unsigned int m = df->regs_inited;
1557 unsigned int regno;
1558 unsigned int offset = 0;
1559 unsigned int start;
1561 if (df->changeable_flags & DF_NO_HARD_REGS)
1563 start = FIRST_PSEUDO_REGISTER;
1564 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1565 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1567 else
1568 start = 0;
1570 ref_info->total_size
1571 = df_count_refs (include_defs, include_uses, include_eq_uses);
1573 df_check_and_grow_ref_info (ref_info, 1);
1575 for (regno = start; regno < m; regno++)
1577 int count = 0;
1578 ref_info->begin[regno] = offset;
1579 if (include_defs)
1581 df_ref ref = DF_REG_DEF_CHAIN (regno);
1582 while (ref)
1584 ref_info->refs[offset] = ref;
1585 DF_REF_ID (ref) = offset++;
1586 count++;
1587 ref = DF_REF_NEXT_REG (ref);
1588 gcc_checking_assert (offset < ref_info->refs_size);
1591 if (include_uses)
1593 df_ref ref = DF_REG_USE_CHAIN (regno);
1594 while (ref)
1596 ref_info->refs[offset] = ref;
1597 DF_REF_ID (ref) = offset++;
1598 count++;
1599 ref = DF_REF_NEXT_REG (ref);
1600 gcc_checking_assert (offset < ref_info->refs_size);
1603 if (include_eq_uses)
1605 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1606 while (ref)
1608 ref_info->refs[offset] = ref;
1609 DF_REF_ID (ref) = offset++;
1610 count++;
1611 ref = DF_REF_NEXT_REG (ref);
1612 gcc_checking_assert (offset < ref_info->refs_size);
1615 ref_info->count[regno] = count;
1618 /* The bitmap size is not decremented when refs are deleted. So
1619 reset it now that we have squished out all of the empty
1620 slots. */
1621 ref_info->table_size = offset;
1625 /* Take build ref table for either the uses or defs from the reg-use
1626 or reg-def chains. This version processes the refs in insn order
1627 which is likely to be best if processing some segment of the
1628 function. */
1630 static void
1631 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1632 bool include_defs,
1633 bool include_uses,
1634 bool include_eq_uses)
1636 bitmap_iterator bi;
1637 unsigned int bb_index;
1638 unsigned int m = df->regs_inited;
1639 unsigned int offset = 0;
1640 unsigned int r;
1641 unsigned int start
1642 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1644 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1645 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1647 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1648 df_check_and_grow_ref_info (ref_info, 1);
1650 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1652 basic_block bb = BASIC_BLOCK (bb_index);
1653 rtx insn;
1654 df_ref *ref_rec;
1656 if (include_defs)
1657 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1659 unsigned int regno = DF_REF_REGNO (*ref_rec);
1660 ref_info->count[regno]++;
1662 if (include_uses)
1663 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1665 unsigned int regno = DF_REF_REGNO (*ref_rec);
1666 ref_info->count[regno]++;
1669 FOR_BB_INSNS (bb, insn)
1671 if (INSN_P (insn))
1673 unsigned int uid = INSN_UID (insn);
1675 if (include_defs)
1676 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1678 unsigned int regno = DF_REF_REGNO (*ref_rec);
1679 ref_info->count[regno]++;
1681 if (include_uses)
1682 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1684 unsigned int regno = DF_REF_REGNO (*ref_rec);
1685 ref_info->count[regno]++;
1687 if (include_eq_uses)
1688 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1690 unsigned int regno = DF_REF_REGNO (*ref_rec);
1691 ref_info->count[regno]++;
1697 for (r = start; r < m; r++)
1699 ref_info->begin[r] = offset;
1700 offset += ref_info->count[r];
1701 ref_info->count[r] = 0;
1704 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1706 basic_block bb = BASIC_BLOCK (bb_index);
1707 rtx insn;
1708 df_ref *ref_rec;
1710 if (include_defs)
1711 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1713 df_ref ref = *ref_rec;
1714 unsigned int regno = DF_REF_REGNO (ref);
1715 if (regno >= start)
1717 unsigned int id
1718 = ref_info->begin[regno] + ref_info->count[regno]++;
1719 DF_REF_ID (ref) = id;
1720 ref_info->refs[id] = ref;
1723 if (include_uses)
1724 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1726 df_ref ref = *ref_rec;
1727 unsigned int regno = DF_REF_REGNO (ref);
1728 if (regno >= start)
1730 unsigned int id
1731 = ref_info->begin[regno] + ref_info->count[regno]++;
1732 DF_REF_ID (ref) = id;
1733 ref_info->refs[id] = ref;
1737 FOR_BB_INSNS (bb, insn)
1739 if (INSN_P (insn))
1741 unsigned int uid = INSN_UID (insn);
1743 if (include_defs)
1744 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1746 df_ref ref = *ref_rec;
1747 unsigned int regno = DF_REF_REGNO (ref);
1748 if (regno >= start)
1750 unsigned int id
1751 = ref_info->begin[regno] + ref_info->count[regno]++;
1752 DF_REF_ID (ref) = id;
1753 ref_info->refs[id] = ref;
1756 if (include_uses)
1757 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1759 df_ref ref = *ref_rec;
1760 unsigned int regno = DF_REF_REGNO (ref);
1761 if (regno >= start)
1763 unsigned int id
1764 = ref_info->begin[regno] + ref_info->count[regno]++;
1765 DF_REF_ID (ref) = id;
1766 ref_info->refs[id] = ref;
1769 if (include_eq_uses)
1770 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1772 df_ref ref = *ref_rec;
1773 unsigned int regno = DF_REF_REGNO (ref);
1774 if (regno >= start)
1776 unsigned int id
1777 = ref_info->begin[regno] + ref_info->count[regno]++;
1778 DF_REF_ID (ref) = id;
1779 ref_info->refs[id] = ref;
1786 /* The bitmap size is not decremented when refs are deleted. So
1787 reset it now that we have squished out all of the empty
1788 slots. */
1790 ref_info->table_size = offset;
1793 /* Take build ref table for either the uses or defs from the reg-use
1794 or reg-def chains. */
1796 static void
1797 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1798 bool include_defs,
1799 bool include_uses,
1800 bool include_eq_uses)
1802 if (df->analyze_subset)
1803 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1804 include_uses, include_eq_uses);
1805 else
1806 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1807 include_uses, include_eq_uses);
1811 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1812 static unsigned int
1813 df_add_refs_to_table (unsigned int offset,
1814 struct df_ref_info *ref_info,
1815 df_ref *ref_vec)
1817 while (*ref_vec)
1819 df_ref ref = *ref_vec;
1820 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1821 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1823 ref_info->refs[offset] = ref;
1824 DF_REF_ID (*ref_vec) = offset++;
1826 ref_vec++;
1828 return offset;
1832 /* Count the number of refs in all of the insns of BB. Include the
1833 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1834 eq_uses if INCLUDE_EQ_USES. */
1836 static unsigned int
1837 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1838 struct df_ref_info *ref_info,
1839 bool include_defs, bool include_uses,
1840 bool include_eq_uses)
1842 rtx insn;
1844 if (include_defs)
1845 offset = df_add_refs_to_table (offset, ref_info,
1846 df_get_artificial_defs (bb->index));
1847 if (include_uses)
1848 offset = df_add_refs_to_table (offset, ref_info,
1849 df_get_artificial_uses (bb->index));
1851 FOR_BB_INSNS (bb, insn)
1852 if (INSN_P (insn))
1854 unsigned int uid = INSN_UID (insn);
1855 if (include_defs)
1856 offset = df_add_refs_to_table (offset, ref_info,
1857 DF_INSN_UID_DEFS (uid));
1858 if (include_uses)
1859 offset = df_add_refs_to_table (offset, ref_info,
1860 DF_INSN_UID_USES (uid));
1861 if (include_eq_uses)
1862 offset = df_add_refs_to_table (offset, ref_info,
1863 DF_INSN_UID_EQ_USES (uid));
1865 return offset;
1869 /* Organize the refs by insn into the table in REF_INFO. If
1870 blocks_to_analyze is defined, use that set, otherwise the entire
1871 program. Include the defs if INCLUDE_DEFS. Include the uses if
1872 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1874 static void
1875 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1876 bool include_defs, bool include_uses,
1877 bool include_eq_uses)
1879 basic_block bb;
1880 unsigned int offset = 0;
1882 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1883 df_check_and_grow_ref_info (ref_info, 1);
1884 if (df->blocks_to_analyze)
1886 bitmap_iterator bi;
1887 unsigned int index;
1889 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1891 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1892 include_defs, include_uses,
1893 include_eq_uses);
1896 ref_info->table_size = offset;
1898 else
1900 FOR_ALL_BB (bb)
1901 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1902 include_defs, include_uses,
1903 include_eq_uses);
1904 ref_info->table_size = offset;
1909 /* If the use refs in DF are not organized, reorganize them. */
1911 void
1912 df_maybe_reorganize_use_refs (enum df_ref_order order)
1914 if (order == df->use_info.ref_order)
1915 return;
1917 switch (order)
1919 case DF_REF_ORDER_BY_REG:
1920 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1921 break;
1923 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1924 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1925 break;
1927 case DF_REF_ORDER_BY_INSN:
1928 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1929 break;
1931 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1932 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1933 break;
1935 case DF_REF_ORDER_NO_TABLE:
1936 free (df->use_info.refs);
1937 df->use_info.refs = NULL;
1938 df->use_info.refs_size = 0;
1939 break;
1941 case DF_REF_ORDER_UNORDERED:
1942 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1943 gcc_unreachable ();
1944 break;
1947 df->use_info.ref_order = order;
1951 /* If the def refs in DF are not organized, reorganize them. */
1953 void
1954 df_maybe_reorganize_def_refs (enum df_ref_order order)
1956 if (order == df->def_info.ref_order)
1957 return;
1959 switch (order)
1961 case DF_REF_ORDER_BY_REG:
1962 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1963 break;
1965 case DF_REF_ORDER_BY_INSN:
1966 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1967 break;
1969 case DF_REF_ORDER_NO_TABLE:
1970 free (df->def_info.refs);
1971 df->def_info.refs = NULL;
1972 df->def_info.refs_size = 0;
1973 break;
1975 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1976 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1977 case DF_REF_ORDER_UNORDERED:
1978 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1979 gcc_unreachable ();
1980 break;
1983 df->def_info.ref_order = order;
1987 /* Change all of the basic block references in INSN to use the insn's
1988 current basic block. This function is called from routines that move
1989 instructions from one block to another. */
1991 void
1992 df_insn_change_bb (rtx insn, basic_block new_bb)
1994 basic_block old_bb = BLOCK_FOR_INSN (insn);
1995 struct df_insn_info *insn_info;
1996 unsigned int uid = INSN_UID (insn);
1998 if (old_bb == new_bb)
1999 return;
2001 set_block_for_insn (insn, new_bb);
2003 if (!df)
2004 return;
2006 if (dump_file)
2007 fprintf (dump_file, "changing bb of uid %d\n", uid);
2009 insn_info = DF_INSN_UID_SAFE_GET (uid);
2010 if (insn_info == NULL)
2012 if (dump_file)
2013 fprintf (dump_file, " unscanned insn\n");
2014 df_insn_rescan (insn);
2015 return;
2018 if (!INSN_P (insn))
2019 return;
2021 df_set_bb_dirty (new_bb);
2022 if (old_bb)
2024 if (dump_file)
2025 fprintf (dump_file, " from %d to %d\n",
2026 old_bb->index, new_bb->index);
2027 df_set_bb_dirty (old_bb);
2029 else
2030 if (dump_file)
2031 fprintf (dump_file, " to %d\n", new_bb->index);
2035 /* Helper function for df_ref_change_reg_with_loc. */
2037 static void
2038 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
2039 struct df_reg_info *new_df,
2040 int new_regno, rtx loc)
2042 df_ref the_ref = old_df->reg_chain;
2044 while (the_ref)
2046 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
2047 && DF_REF_LOC (the_ref)
2048 && (*DF_REF_LOC (the_ref) == loc))
2050 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
2051 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
2052 df_ref *ref_vec, *ref_vec_t;
2053 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
2054 unsigned int count = 0;
2056 DF_REF_REGNO (the_ref) = new_regno;
2057 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
2059 /* Pull the_ref out of the old regno chain. */
2060 if (prev_ref)
2061 DF_REF_NEXT_REG (prev_ref) = next_ref;
2062 else
2063 old_df->reg_chain = next_ref;
2064 if (next_ref)
2065 DF_REF_PREV_REG (next_ref) = prev_ref;
2066 old_df->n_refs--;
2068 /* Put the ref into the new regno chain. */
2069 DF_REF_PREV_REG (the_ref) = NULL;
2070 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
2071 if (new_df->reg_chain)
2072 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
2073 new_df->reg_chain = the_ref;
2074 new_df->n_refs++;
2075 if (DF_REF_BB (the_ref))
2076 df_set_bb_dirty (DF_REF_BB (the_ref));
2078 /* Need to sort the record again that the ref was in because
2079 the regno is a sorting key. First, find the right
2080 record. */
2081 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
2082 ref_vec = insn_info->eq_uses;
2083 else
2084 ref_vec = insn_info->uses;
2085 if (dump_file)
2086 fprintf (dump_file, "changing reg in insn %d\n",
2087 DF_REF_INSN_UID (the_ref));
2089 ref_vec_t = ref_vec;
2091 /* Find the length. */
2092 while (*ref_vec_t)
2094 count++;
2095 ref_vec_t++;
2097 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
2099 the_ref = next_ref;
2101 else
2102 the_ref = DF_REF_NEXT_REG (the_ref);
2107 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2108 NEW_REGNO. Refs that do not match LOC are not changed which means
2109 that artificial refs are not changed since they have no loc. This
2110 call is to support the SET_REGNO macro. */
2112 void
2113 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2115 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2116 return;
2118 df_grow_reg_info ();
2120 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2121 DF_REG_DEF_GET (new_regno), new_regno, loc);
2122 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2123 DF_REG_USE_GET (new_regno), new_regno, loc);
2124 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2125 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2129 /* Delete the mw_hardregs that point into the eq_notes. */
2131 static unsigned int
2132 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2134 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2135 unsigned int deleted = 0;
2136 unsigned int count = 0;
2137 struct df_scan_problem_data *problem_data
2138 = (struct df_scan_problem_data *) df_scan->problem_data;
2140 if (!*mw_vec)
2141 return 0;
2143 while (*mw_vec)
2145 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2147 struct df_mw_hardreg **temp_vec = mw_vec;
2149 pool_free (problem_data->mw_reg_pool, *mw_vec);
2150 temp_vec = mw_vec;
2151 /* Shove the remaining ones down one to fill the gap. While
2152 this looks n**2, it is highly unusual to have any mw regs
2153 in eq_notes and the chances of more than one are almost
2154 non existent. */
2155 while (*temp_vec)
2157 *temp_vec = *(temp_vec + 1);
2158 temp_vec++;
2160 deleted++;
2162 else
2164 mw_vec++;
2165 count++;
2169 if (count == 0)
2171 df_scan_free_mws_vec (insn_info->mw_hardregs);
2172 insn_info->mw_hardregs = df_null_mw_rec;
2173 return 0;
2175 return deleted;
2179 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2181 void
2182 df_notes_rescan (rtx insn)
2184 struct df_insn_info *insn_info;
2185 unsigned int uid = INSN_UID (insn);
2187 if (!df)
2188 return;
2190 /* The client has disabled rescanning and plans to do it itself. */
2191 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2192 return;
2194 /* Do nothing if the insn hasn't been emitted yet. */
2195 if (!BLOCK_FOR_INSN (insn))
2196 return;
2198 df_grow_bb_info (df_scan);
2199 df_grow_reg_info ();
2201 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2203 /* The client has deferred rescanning. */
2204 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2206 if (!insn_info)
2208 insn_info = df_insn_create_insn_record (insn);
2209 insn_info->defs = df_null_ref_rec;
2210 insn_info->uses = df_null_ref_rec;
2211 insn_info->eq_uses = df_null_ref_rec;
2212 insn_info->mw_hardregs = df_null_mw_rec;
2215 bitmap_clear_bit (&df->insns_to_delete, uid);
2216 /* If the insn is set to be rescanned, it does not need to also
2217 be notes rescanned. */
2218 if (!bitmap_bit_p (&df->insns_to_rescan, uid))
2219 bitmap_set_bit (&df->insns_to_notes_rescan, INSN_UID (insn));
2220 return;
2223 bitmap_clear_bit (&df->insns_to_delete, uid);
2224 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
2226 if (insn_info)
2228 basic_block bb = BLOCK_FOR_INSN (insn);
2229 rtx note;
2230 struct df_collection_rec collection_rec;
2231 unsigned int num_deleted;
2232 unsigned int mw_len;
2234 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2235 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
2236 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
2238 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2239 df_ref_chain_delete (insn_info->eq_uses);
2240 insn_info->eq_uses = NULL;
2242 /* Process REG_EQUIV/REG_EQUAL notes */
2243 for (note = REG_NOTES (insn); note;
2244 note = XEXP (note, 1))
2246 switch (REG_NOTE_KIND (note))
2248 case REG_EQUIV:
2249 case REG_EQUAL:
2250 df_uses_record (&collection_rec,
2251 &XEXP (note, 0), DF_REF_REG_USE,
2252 bb, insn_info, DF_REF_IN_NOTE);
2253 default:
2254 break;
2258 /* Find some place to put any new mw_hardregs. */
2259 df_canonize_collection_rec (&collection_rec);
2260 mw_len = collection_rec.mw_vec.length ();
2261 if (mw_len)
2263 unsigned int count = 0;
2264 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2265 while (*mw_rec)
2267 count++;
2268 mw_rec++;
2271 if (count)
2273 /* Append to the end of the existing record after
2274 expanding it if necessary. */
2275 if (mw_len > num_deleted)
2277 insn_info->mw_hardregs =
2278 XRESIZEVEC (struct df_mw_hardreg *,
2279 insn_info->mw_hardregs,
2280 count + 1 + mw_len);
2282 memcpy (&insn_info->mw_hardregs[count],
2283 collection_rec.mw_vec.address (),
2284 mw_len * sizeof (struct df_mw_hardreg *));
2285 insn_info->mw_hardregs[count + mw_len] = NULL;
2286 qsort (insn_info->mw_hardregs, count + mw_len,
2287 sizeof (struct df_mw_hardreg *), df_mw_compare);
2289 else
2291 /* No vector there. */
2292 insn_info->mw_hardregs
2293 = XNEWVEC (struct df_mw_hardreg*, 1 + mw_len);
2294 memcpy (insn_info->mw_hardregs,
2295 collection_rec.mw_vec.address (),
2296 mw_len * sizeof (struct df_mw_hardreg *));
2297 insn_info->mw_hardregs[mw_len] = NULL;
2300 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2301 ignore it. */
2302 collection_rec.mw_vec.release ();
2303 df_refs_add_to_chains (&collection_rec, bb, insn);
2304 collection_rec.eq_use_vec.release ();
2306 else
2307 df_insn_rescan (insn);
2312 /*----------------------------------------------------------------------------
2313 Hard core instruction scanning code. No external interfaces here,
2314 just a lot of routines that look inside insns.
2315 ----------------------------------------------------------------------------*/
2318 /* Return true if the contents of two df_ref's are identical.
2319 It ignores DF_REF_MARKER. */
2321 static bool
2322 df_ref_equal_p (df_ref ref1, df_ref ref2)
2324 if (!ref2)
2325 return false;
2327 if (ref1 == ref2)
2328 return true;
2330 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2331 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2332 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2333 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2334 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2335 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2336 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2337 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2338 return false;
2340 switch (DF_REF_CLASS (ref1))
2342 case DF_REF_ARTIFICIAL:
2343 case DF_REF_BASE:
2344 return true;
2346 case DF_REF_REGULAR:
2347 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2349 default:
2350 gcc_unreachable ();
2352 return false;
2356 /* Compare REF1 and REF2 for sorting. This is only called from places
2357 where all of the refs are of the same type, in the same insn, and
2358 have the same bb. So these fields are not checked. */
2360 static int
2361 df_ref_compare (const void *r1, const void *r2)
2363 const df_ref ref1 = *(const df_ref *)r1;
2364 const df_ref ref2 = *(const df_ref *)r2;
2366 if (ref1 == ref2)
2367 return 0;
2369 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2370 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2372 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2373 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2375 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2376 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2378 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2379 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2381 /* Cannot look at the LOC field on artificial refs. */
2382 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2383 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2384 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2386 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2388 /* If two refs are identical except that one of them has is from
2389 a mw and one is not, we need to have the one with the mw
2390 first. */
2391 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2392 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2393 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2394 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2395 return -1;
2396 else
2397 return 1;
2400 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2403 static void
2404 df_swap_refs (vec<df_ref, va_stack> *ref_vec, int i, int j)
2406 df_ref tmp = (*ref_vec)[i];
2407 (*ref_vec)[i] = (*ref_vec)[j];
2408 (*ref_vec)[j] = tmp;
2411 /* Sort and compress a set of refs. */
2413 static void
2414 df_sort_and_compress_refs (vec<df_ref, va_stack> *ref_vec)
2416 unsigned int count;
2417 unsigned int i;
2418 unsigned int dist = 0;
2420 count = ref_vec->length ();
2422 /* If there are 1 or 0 elements, there is nothing to do. */
2423 if (count < 2)
2424 return;
2425 else if (count == 2)
2427 df_ref r0 = (*ref_vec)[0];
2428 df_ref r1 = (*ref_vec)[1];
2429 if (df_ref_compare (&r0, &r1) > 0)
2430 df_swap_refs (ref_vec, 0, 1);
2432 else
2434 for (i = 0; i < count - 1; i++)
2436 df_ref r0 = (*ref_vec)[i];
2437 df_ref r1 = (*ref_vec)[i + 1];
2438 if (df_ref_compare (&r0, &r1) >= 0)
2439 break;
2441 /* If the array is already strictly ordered,
2442 which is the most common case for large COUNT case
2443 (which happens for CALL INSNs),
2444 no need to sort and filter out duplicate.
2445 Simply return the count.
2446 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2447 of DF_REF_COMPARE. */
2448 if (i == count - 1)
2449 return;
2450 ref_vec->qsort (df_ref_compare);
2453 for (i=0; i<count-dist; i++)
2455 /* Find the next ref that is not equal to the current ref. */
2456 while (i + dist + 1 < count
2457 && df_ref_equal_p ((*ref_vec)[i],
2458 (*ref_vec)[i + dist + 1]))
2460 df_free_ref ((*ref_vec)[i + dist + 1]);
2461 dist++;
2463 /* Copy it down to the next position. */
2464 if (dist && i + dist + 1 < count)
2465 (*ref_vec)[i + 1] = (*ref_vec)[i + dist + 1];
2468 count -= dist;
2469 ref_vec->truncate (count);
2473 /* Return true if the contents of two df_ref's are identical.
2474 It ignores DF_REF_MARKER. */
2476 static bool
2477 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2479 if (!mw2)
2480 return false;
2481 return (mw1 == mw2) ||
2482 (mw1->mw_reg == mw2->mw_reg
2483 && mw1->type == mw2->type
2484 && mw1->flags == mw2->flags
2485 && mw1->start_regno == mw2->start_regno
2486 && mw1->end_regno == mw2->end_regno);
2490 /* Compare MW1 and MW2 for sorting. */
2492 static int
2493 df_mw_compare (const void *m1, const void *m2)
2495 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2496 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2498 if (mw1 == mw2)
2499 return 0;
2501 if (mw1->type != mw2->type)
2502 return mw1->type - mw2->type;
2504 if (mw1->flags != mw2->flags)
2505 return mw1->flags - mw2->flags;
2507 if (mw1->start_regno != mw2->start_regno)
2508 return mw1->start_regno - mw2->start_regno;
2510 if (mw1->end_regno != mw2->end_regno)
2511 return mw1->end_regno - mw2->end_regno;
2513 if (mw1->mw_reg != mw2->mw_reg)
2514 return mw1->mw_order - mw2->mw_order;
2516 return 0;
2520 /* Sort and compress a set of refs. */
2522 static void
2523 df_sort_and_compress_mws (vec<df_mw_hardreg_ptr, va_stack> *mw_vec)
2525 unsigned int count;
2526 struct df_scan_problem_data *problem_data
2527 = (struct df_scan_problem_data *) df_scan->problem_data;
2528 unsigned int i;
2529 unsigned int dist = 0;
2531 count = mw_vec->length ();
2532 if (count < 2)
2533 return;
2534 else if (count == 2)
2536 struct df_mw_hardreg *m0 = (*mw_vec)[0];
2537 struct df_mw_hardreg *m1 = (*mw_vec)[1];
2538 if (df_mw_compare (&m0, &m1) > 0)
2540 struct df_mw_hardreg *tmp = (*mw_vec)[0];
2541 (*mw_vec)[0] = (*mw_vec)[1];
2542 (*mw_vec)[1] = tmp;
2545 else
2546 mw_vec->qsort (df_mw_compare);
2548 for (i=0; i<count-dist; i++)
2550 /* Find the next ref that is not equal to the current ref. */
2551 while (i + dist + 1 < count
2552 && df_mw_equal_p ((*mw_vec)[i], (*mw_vec)[i + dist + 1]))
2554 pool_free (problem_data->mw_reg_pool,
2555 (*mw_vec)[i + dist + 1]);
2556 dist++;
2558 /* Copy it down to the next position. */
2559 if (dist && i + dist + 1 < count)
2560 (*mw_vec)[i + 1] = (*mw_vec)[i + dist + 1];
2563 count -= dist;
2564 mw_vec->truncate (count);
2568 /* Sort and remove duplicates from the COLLECTION_REC. */
2570 static void
2571 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2573 df_sort_and_compress_refs (&collection_rec->def_vec);
2574 df_sort_and_compress_refs (&collection_rec->use_vec);
2575 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2576 df_sort_and_compress_mws (&collection_rec->mw_vec);
2580 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2582 static void
2583 df_install_ref (df_ref this_ref,
2584 struct df_reg_info *reg_info,
2585 struct df_ref_info *ref_info,
2586 bool add_to_table)
2588 unsigned int regno = DF_REF_REGNO (this_ref);
2589 /* Add the ref to the reg_{def,use,eq_use} chain. */
2590 df_ref head = reg_info->reg_chain;
2592 reg_info->reg_chain = this_ref;
2593 reg_info->n_refs++;
2595 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2597 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2598 df->hard_regs_live_count[regno]++;
2601 gcc_checking_assert (DF_REF_NEXT_REG (this_ref) == NULL
2602 && DF_REF_PREV_REG (this_ref) == NULL);
2604 DF_REF_NEXT_REG (this_ref) = head;
2606 /* We cannot actually link to the head of the chain. */
2607 DF_REF_PREV_REG (this_ref) = NULL;
2609 if (head)
2610 DF_REF_PREV_REG (head) = this_ref;
2612 if (add_to_table)
2614 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2615 df_check_and_grow_ref_info (ref_info, 1);
2616 DF_REF_ID (this_ref) = ref_info->table_size;
2617 /* Add the ref to the big array of defs. */
2618 ref_info->refs[ref_info->table_size] = this_ref;
2619 ref_info->table_size++;
2621 else
2622 DF_REF_ID (this_ref) = -1;
2624 ref_info->total_size++;
2628 /* This function takes one of the groups of refs (defs, uses or
2629 eq_uses) and installs the entire group into the insn. It also adds
2630 each of these refs into the appropriate chains. */
2632 static df_ref *
2633 df_install_refs (basic_block bb,
2634 vec<df_ref, va_stack> old_vec,
2635 struct df_reg_info **reg_info,
2636 struct df_ref_info *ref_info,
2637 bool is_notes)
2639 unsigned int count;
2641 count = old_vec.length ();
2642 if (count)
2644 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2645 bool add_to_table;
2646 df_ref this_ref;
2647 unsigned int ix;
2649 switch (ref_info->ref_order)
2651 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2652 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2653 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2654 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2655 add_to_table = true;
2656 break;
2657 case DF_REF_ORDER_UNORDERED:
2658 case DF_REF_ORDER_BY_REG:
2659 case DF_REF_ORDER_BY_INSN:
2660 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2661 add_to_table = !is_notes;
2662 break;
2663 default:
2664 add_to_table = false;
2665 break;
2668 /* Do not add if ref is not in the right blocks. */
2669 if (add_to_table && df->analyze_subset)
2670 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2672 FOR_EACH_VEC_ELT (old_vec, ix, this_ref)
2674 new_vec[ix] = this_ref;
2675 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2676 ref_info, add_to_table);
2679 new_vec[count] = NULL;
2680 return new_vec;
2682 else
2683 return df_null_ref_rec;
2687 /* This function takes the mws installs the entire group into the
2688 insn. */
2690 static struct df_mw_hardreg **
2691 df_install_mws (vec<df_mw_hardreg_ptr, va_stack> old_vec)
2693 unsigned int count;
2695 count = old_vec.length ();
2696 if (count)
2698 struct df_mw_hardreg **new_vec
2699 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2700 memcpy (new_vec, old_vec.address (),
2701 sizeof (struct df_mw_hardreg*) * count);
2702 new_vec[count] = NULL;
2703 return new_vec;
2705 else
2706 return df_null_mw_rec;
2710 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2711 chains and update other necessary information. */
2713 static void
2714 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2715 basic_block bb, rtx insn)
2717 if (insn)
2719 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2720 /* If there is a vector in the collection rec, add it to the
2721 insn. A null rec is a signal that the caller will handle the
2722 chain specially. */
2723 if (collection_rec->def_vec.exists ())
2725 df_scan_free_ref_vec (insn_rec->defs);
2726 insn_rec->defs
2727 = df_install_refs (bb, collection_rec->def_vec,
2728 df->def_regs,
2729 &df->def_info, false);
2731 if (collection_rec->use_vec.exists ())
2733 df_scan_free_ref_vec (insn_rec->uses);
2734 insn_rec->uses
2735 = df_install_refs (bb, collection_rec->use_vec,
2736 df->use_regs,
2737 &df->use_info, false);
2739 if (collection_rec->eq_use_vec.exists ())
2741 df_scan_free_ref_vec (insn_rec->eq_uses);
2742 insn_rec->eq_uses
2743 = df_install_refs (bb, collection_rec->eq_use_vec,
2744 df->eq_use_regs,
2745 &df->use_info, true);
2747 if (collection_rec->mw_vec.exists ())
2749 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2750 insn_rec->mw_hardregs
2751 = df_install_mws (collection_rec->mw_vec);
2754 else
2756 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2758 df_scan_free_ref_vec (bb_info->artificial_defs);
2759 bb_info->artificial_defs
2760 = df_install_refs (bb, collection_rec->def_vec,
2761 df->def_regs,
2762 &df->def_info, false);
2763 df_scan_free_ref_vec (bb_info->artificial_uses);
2764 bb_info->artificial_uses
2765 = df_install_refs (bb, collection_rec->use_vec,
2766 df->use_regs,
2767 &df->use_info, false);
2772 /* Allocate a ref and initialize its fields. */
2774 static df_ref
2775 df_ref_create_structure (enum df_ref_class cl,
2776 struct df_collection_rec *collection_rec,
2777 rtx reg, rtx *loc,
2778 basic_block bb, struct df_insn_info *info,
2779 enum df_ref_type ref_type,
2780 int ref_flags)
2782 df_ref this_ref = NULL;
2783 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2784 struct df_scan_problem_data *problem_data
2785 = (struct df_scan_problem_data *) df_scan->problem_data;
2787 switch (cl)
2789 case DF_REF_BASE:
2790 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2791 gcc_checking_assert (loc == NULL);
2792 break;
2794 case DF_REF_ARTIFICIAL:
2795 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2796 this_ref->artificial_ref.bb = bb;
2797 gcc_checking_assert (loc == NULL);
2798 break;
2800 case DF_REF_REGULAR:
2801 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2802 this_ref->regular_ref.loc = loc;
2803 gcc_checking_assert (loc);
2804 break;
2807 DF_REF_CLASS (this_ref) = cl;
2808 DF_REF_ID (this_ref) = -1;
2809 DF_REF_REG (this_ref) = reg;
2810 DF_REF_REGNO (this_ref) = regno;
2811 DF_REF_TYPE (this_ref) = ref_type;
2812 DF_REF_INSN_INFO (this_ref) = info;
2813 DF_REF_CHAIN (this_ref) = NULL;
2814 DF_REF_FLAGS (this_ref) = ref_flags;
2815 DF_REF_NEXT_REG (this_ref) = NULL;
2816 DF_REF_PREV_REG (this_ref) = NULL;
2817 DF_REF_ORDER (this_ref) = df->ref_order++;
2819 /* We need to clear this bit because fwprop, and in the future
2820 possibly other optimizations sometimes create new refs using ond
2821 refs as the model. */
2822 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2824 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2825 if (regno < FIRST_PSEUDO_REGISTER
2826 && !DF_REF_IS_ARTIFICIAL (this_ref)
2827 && !DEBUG_INSN_P (DF_REF_INSN (this_ref)))
2829 if (DF_REF_REG_DEF_P (this_ref))
2831 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2832 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2834 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2835 && (regno == FRAME_POINTER_REGNUM
2836 || regno == ARG_POINTER_REGNUM)))
2837 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2840 if (collection_rec)
2842 if (DF_REF_REG_DEF_P (this_ref))
2843 collection_rec->def_vec.safe_push (this_ref);
2844 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2845 collection_rec->eq_use_vec.safe_push (this_ref);
2846 else
2847 collection_rec->use_vec.safe_push (this_ref);
2849 else
2850 df_install_ref_incremental (this_ref);
2852 return this_ref;
2856 /* Create new references of type DF_REF_TYPE for each part of register REG
2857 at address LOC within INSN of BB. */
2860 static void
2861 df_ref_record (enum df_ref_class cl,
2862 struct df_collection_rec *collection_rec,
2863 rtx reg, rtx *loc,
2864 basic_block bb, struct df_insn_info *insn_info,
2865 enum df_ref_type ref_type,
2866 int ref_flags)
2868 unsigned int regno;
2870 gcc_checking_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2872 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2873 if (regno < FIRST_PSEUDO_REGISTER)
2875 struct df_mw_hardreg *hardreg = NULL;
2876 struct df_scan_problem_data *problem_data
2877 = (struct df_scan_problem_data *) df_scan->problem_data;
2878 unsigned int i;
2879 unsigned int endregno;
2880 df_ref ref;
2882 if (GET_CODE (reg) == SUBREG)
2884 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2885 SUBREG_BYTE (reg), GET_MODE (reg));
2886 endregno = regno + subreg_nregs (reg);
2888 else
2889 endregno = END_HARD_REGNO (reg);
2891 /* If this is a multiword hardreg, we create some extra
2892 datastructures that will enable us to easily build REG_DEAD
2893 and REG_UNUSED notes. */
2894 if (collection_rec
2895 && (endregno != regno + 1) && insn_info)
2897 /* Sets to a subreg of a multiword register are partial.
2898 Sets to a non-subreg of a multiword register are not. */
2899 if (GET_CODE (reg) == SUBREG)
2900 ref_flags |= DF_REF_PARTIAL;
2901 ref_flags |= DF_REF_MW_HARDREG;
2903 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2904 hardreg->type = ref_type;
2905 hardreg->flags = ref_flags;
2906 hardreg->mw_reg = reg;
2907 hardreg->start_regno = regno;
2908 hardreg->end_regno = endregno - 1;
2909 hardreg->mw_order = df->ref_order++;
2910 collection_rec->mw_vec.safe_push (hardreg);
2913 for (i = regno; i < endregno; i++)
2915 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2916 bb, insn_info, ref_type, ref_flags);
2918 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2921 else
2923 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2924 ref_type, ref_flags);
2929 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2930 covered by the outer mode is smaller than that covered by the inner mode,
2931 is a read-modify-write operation.
2932 This function returns true iff the SUBREG X is such a SUBREG. */
2934 bool
2935 df_read_modify_subreg_p (rtx x)
2937 unsigned int isize, osize;
2938 if (GET_CODE (x) != SUBREG)
2939 return false;
2940 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2941 osize = GET_MODE_SIZE (GET_MODE (x));
2942 return isize > osize
2943 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2947 /* Process all the registers defined in the rtx pointed by LOC.
2948 Autoincrement/decrement definitions will be picked up by df_uses_record.
2949 Any change here has to be matched in df_find_hard_reg_defs_1. */
2951 static void
2952 df_def_record_1 (struct df_collection_rec *collection_rec,
2953 rtx *loc, basic_block bb, struct df_insn_info *insn_info,
2954 int flags)
2956 rtx dst = *loc;
2958 /* It is legal to have a set destination be a parallel. */
2959 if (GET_CODE (dst) == PARALLEL)
2961 int i;
2962 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2964 rtx temp = XVECEXP (dst, 0, i);
2965 gcc_assert (GET_CODE (temp) == EXPR_LIST);
2966 df_def_record_1 (collection_rec, &XEXP (temp, 0),
2967 bb, insn_info, flags);
2969 return;
2972 if (GET_CODE (dst) == STRICT_LOW_PART)
2974 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2976 loc = &XEXP (dst, 0);
2977 dst = *loc;
2980 if (GET_CODE (dst) == ZERO_EXTRACT)
2982 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
2984 loc = &XEXP (dst, 0);
2985 dst = *loc;
2988 /* At this point if we do not have a reg or a subreg, just return. */
2989 if (REG_P (dst))
2991 df_ref_record (DF_REF_REGULAR, collection_rec,
2992 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
2994 /* We want to keep sp alive everywhere - by making all
2995 writes to sp also use of sp. */
2996 if (REGNO (dst) == STACK_POINTER_REGNUM)
2997 df_ref_record (DF_REF_BASE, collection_rec,
2998 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags);
3000 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
3002 if (df_read_modify_subreg_p (dst))
3003 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
3005 flags |= DF_REF_SUBREG;
3007 df_ref_record (DF_REF_REGULAR, collection_rec,
3008 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
3013 /* Process all the registers defined in the pattern rtx, X. Any change
3014 here has to be matched in df_find_hard_reg_defs. */
3016 static void
3017 df_defs_record (struct df_collection_rec *collection_rec,
3018 rtx x, basic_block bb, struct df_insn_info *insn_info,
3019 int flags)
3021 RTX_CODE code = GET_CODE (x);
3022 int i;
3024 switch (code)
3026 case SET:
3027 df_def_record_1 (collection_rec, &SET_DEST (x), bb, insn_info, flags);
3028 break;
3030 case CLOBBER:
3031 flags |= DF_REF_MUST_CLOBBER;
3032 df_def_record_1 (collection_rec, &XEXP (x, 0), bb, insn_info, flags);
3033 break;
3035 case COND_EXEC:
3036 df_defs_record (collection_rec, COND_EXEC_CODE (x),
3037 bb, insn_info, DF_REF_CONDITIONAL);
3038 break;
3040 case PARALLEL:
3041 for (i = 0; i < XVECLEN (x, 0); i++)
3042 df_defs_record (collection_rec, XVECEXP (x, 0, i),
3043 bb, insn_info, flags);
3044 break;
3045 default:
3046 /* No DEFs to record in other cases */
3047 break;
3051 /* Set bits in *DEFS for hard registers found in the rtx DST, which is the
3052 destination of a set or clobber. This has to match the logic in
3053 df_defs_record_1. */
3055 static void
3056 df_find_hard_reg_defs_1 (rtx dst, HARD_REG_SET *defs)
3058 /* It is legal to have a set destination be a parallel. */
3059 if (GET_CODE (dst) == PARALLEL)
3061 int i;
3062 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
3064 rtx temp = XVECEXP (dst, 0, i);
3065 gcc_assert (GET_CODE (temp) == EXPR_LIST);
3066 df_find_hard_reg_defs_1 (XEXP (temp, 0), defs);
3068 return;
3071 if (GET_CODE (dst) == STRICT_LOW_PART)
3072 dst = XEXP (dst, 0);
3074 if (GET_CODE (dst) == ZERO_EXTRACT)
3075 dst = XEXP (dst, 0);
3077 /* At this point if we do not have a reg or a subreg, just return. */
3078 if (REG_P (dst) && HARD_REGISTER_P (dst))
3079 SET_HARD_REG_BIT (*defs, REGNO (dst));
3080 else if (GET_CODE (dst) == SUBREG
3081 && REG_P (SUBREG_REG (dst)) && HARD_REGISTER_P (dst))
3082 SET_HARD_REG_BIT (*defs, REGNO (SUBREG_REG (dst)));
3085 /* Set bits in *DEFS for hard registers defined in the pattern X. This
3086 has to match the logic in df_defs_record. */
3088 static void
3089 df_find_hard_reg_defs (rtx x, HARD_REG_SET *defs)
3091 RTX_CODE code = GET_CODE (x);
3092 int i;
3094 switch (code)
3096 case SET:
3097 df_find_hard_reg_defs_1 (SET_DEST (x), defs);
3098 break;
3100 case CLOBBER:
3101 df_find_hard_reg_defs_1 (XEXP (x, 0), defs);
3102 break;
3104 case COND_EXEC:
3105 df_find_hard_reg_defs (COND_EXEC_CODE (x), defs);
3106 break;
3108 case PARALLEL:
3109 for (i = 0; i < XVECLEN (x, 0); i++)
3110 df_find_hard_reg_defs (XVECEXP (x, 0, i), defs);
3111 break;
3112 default:
3113 /* No DEFs to record in other cases */
3114 break;
3119 /* Process all the registers used in the rtx at address LOC. */
3121 static void
3122 df_uses_record (struct df_collection_rec *collection_rec,
3123 rtx *loc, enum df_ref_type ref_type,
3124 basic_block bb, struct df_insn_info *insn_info,
3125 int flags)
3127 RTX_CODE code;
3128 rtx x;
3130 retry:
3131 x = *loc;
3132 if (!x)
3133 return;
3134 code = GET_CODE (x);
3135 switch (code)
3137 case LABEL_REF:
3138 case SYMBOL_REF:
3139 case CONST:
3140 CASE_CONST_ANY:
3141 case PC:
3142 case CC0:
3143 case ADDR_VEC:
3144 case ADDR_DIFF_VEC:
3145 return;
3147 case CLOBBER:
3148 /* If we are clobbering a MEM, mark any registers inside the address
3149 as being used. */
3150 if (MEM_P (XEXP (x, 0)))
3151 df_uses_record (collection_rec,
3152 &XEXP (XEXP (x, 0), 0),
3153 DF_REF_REG_MEM_STORE,
3154 bb, insn_info,
3155 flags);
3157 /* If we're clobbering a REG then we have a def so ignore. */
3158 return;
3160 case MEM:
3161 df_uses_record (collection_rec,
3162 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3163 bb, insn_info, flags & DF_REF_IN_NOTE);
3164 return;
3166 case SUBREG:
3167 /* While we're here, optimize this case. */
3168 flags |= DF_REF_PARTIAL;
3169 /* In case the SUBREG is not of a REG, do not optimize. */
3170 if (!REG_P (SUBREG_REG (x)))
3172 loc = &SUBREG_REG (x);
3173 df_uses_record (collection_rec, loc, ref_type, bb, insn_info, flags);
3174 return;
3176 /* ... Fall through ... */
3178 case REG:
3179 df_ref_record (DF_REF_REGULAR, collection_rec,
3180 x, loc, bb, insn_info,
3181 ref_type, flags);
3182 return;
3184 case SIGN_EXTRACT:
3185 case ZERO_EXTRACT:
3187 df_uses_record (collection_rec,
3188 &XEXP (x, 1), ref_type, bb, insn_info, flags);
3189 df_uses_record (collection_rec,
3190 &XEXP (x, 2), ref_type, bb, insn_info, flags);
3192 /* If the parameters to the zero or sign extract are
3193 constants, strip them off and recurse, otherwise there is
3194 no information that we can gain from this operation. */
3195 if (code == ZERO_EXTRACT)
3196 flags |= DF_REF_ZERO_EXTRACT;
3197 else
3198 flags |= DF_REF_SIGN_EXTRACT;
3200 df_uses_record (collection_rec,
3201 &XEXP (x, 0), ref_type, bb, insn_info, flags);
3202 return;
3204 break;
3206 case SET:
3208 rtx dst = SET_DEST (x);
3209 gcc_assert (!(flags & DF_REF_IN_NOTE));
3210 df_uses_record (collection_rec,
3211 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags);
3213 switch (GET_CODE (dst))
3215 case SUBREG:
3216 if (df_read_modify_subreg_p (dst))
3218 df_uses_record (collection_rec, &SUBREG_REG (dst),
3219 DF_REF_REG_USE, bb, insn_info,
3220 flags | DF_REF_READ_WRITE | DF_REF_SUBREG);
3221 break;
3223 /* Fall through. */
3224 case REG:
3225 case PARALLEL:
3226 case SCRATCH:
3227 case PC:
3228 case CC0:
3229 break;
3230 case MEM:
3231 df_uses_record (collection_rec, &XEXP (dst, 0),
3232 DF_REF_REG_MEM_STORE, bb, insn_info, flags);
3233 break;
3234 case STRICT_LOW_PART:
3236 rtx *temp = &XEXP (dst, 0);
3237 /* A strict_low_part uses the whole REG and not just the
3238 SUBREG. */
3239 dst = XEXP (dst, 0);
3240 df_uses_record (collection_rec,
3241 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3242 DF_REF_REG_USE, bb, insn_info,
3243 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART);
3245 break;
3246 case ZERO_EXTRACT:
3248 df_uses_record (collection_rec, &XEXP (dst, 1),
3249 DF_REF_REG_USE, bb, insn_info, flags);
3250 df_uses_record (collection_rec, &XEXP (dst, 2),
3251 DF_REF_REG_USE, bb, insn_info, flags);
3252 if (GET_CODE (XEXP (dst,0)) == MEM)
3253 df_uses_record (collection_rec, &XEXP (dst, 0),
3254 DF_REF_REG_USE, bb, insn_info,
3255 flags);
3256 else
3257 df_uses_record (collection_rec, &XEXP (dst, 0),
3258 DF_REF_REG_USE, bb, insn_info,
3259 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT);
3261 break;
3263 default:
3264 gcc_unreachable ();
3266 return;
3269 case RETURN:
3270 case SIMPLE_RETURN:
3271 break;
3273 case ASM_OPERANDS:
3274 case UNSPEC_VOLATILE:
3275 case TRAP_IF:
3276 case ASM_INPUT:
3278 /* Traditional and volatile asm instructions must be
3279 considered to use and clobber all hard registers, all
3280 pseudo-registers and all of memory. So must TRAP_IF and
3281 UNSPEC_VOLATILE operations.
3283 Consider for instance a volatile asm that changes the fpu
3284 rounding mode. An insn should not be moved across this
3285 even if it only uses pseudo-regs because it might give an
3286 incorrectly rounded result.
3288 However, flow.c's liveness computation did *not* do this,
3289 giving the reasoning as " ?!? Unfortunately, marking all
3290 hard registers as live causes massive problems for the
3291 register allocator and marking all pseudos as live creates
3292 mountains of uninitialized variable warnings."
3294 In order to maintain the status quo with regard to liveness
3295 and uses, we do what flow.c did and just mark any regs we
3296 can find in ASM_OPERANDS as used. In global asm insns are
3297 scanned and regs_asm_clobbered is filled out.
3299 For all ASM_OPERANDS, we must traverse the vector of input
3300 operands. We can not just fall through here since then we
3301 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3302 which do not indicate traditional asms unlike their normal
3303 usage. */
3304 if (code == ASM_OPERANDS)
3306 int j;
3308 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3309 df_uses_record (collection_rec, &ASM_OPERANDS_INPUT (x, j),
3310 DF_REF_REG_USE, bb, insn_info, flags);
3311 return;
3313 break;
3316 case VAR_LOCATION:
3317 df_uses_record (collection_rec,
3318 &PAT_VAR_LOCATION_LOC (x),
3319 DF_REF_REG_USE, bb, insn_info, flags);
3320 return;
3322 case PRE_DEC:
3323 case POST_DEC:
3324 case PRE_INC:
3325 case POST_INC:
3326 case PRE_MODIFY:
3327 case POST_MODIFY:
3328 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3329 /* Catch the def of the register being modified. */
3330 df_ref_record (DF_REF_REGULAR, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3331 bb, insn_info,
3332 DF_REF_REG_DEF,
3333 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY);
3335 /* ... Fall through to handle uses ... */
3337 default:
3338 break;
3341 /* Recursively scan the operands of this expression. */
3343 const char *fmt = GET_RTX_FORMAT (code);
3344 int i;
3346 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3348 if (fmt[i] == 'e')
3350 /* Tail recursive case: save a function call level. */
3351 if (i == 0)
3353 loc = &XEXP (x, 0);
3354 goto retry;
3356 df_uses_record (collection_rec, &XEXP (x, i), ref_type,
3357 bb, insn_info, flags);
3359 else if (fmt[i] == 'E')
3361 int j;
3362 for (j = 0; j < XVECLEN (x, i); j++)
3363 df_uses_record (collection_rec,
3364 &XVECEXP (x, i, j), ref_type,
3365 bb, insn_info, flags);
3370 return;
3374 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3376 static void
3377 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3379 unsigned int ix;
3380 df_ref ref;
3382 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
3384 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3386 df_ref use;
3388 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3389 DF_REF_LOC (ref), DF_REF_BB (ref),
3390 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3391 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL);
3392 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3398 /* Get call's extra defs and uses (track caller-saved registers). */
3400 static void
3401 df_get_call_refs (struct df_collection_rec *collection_rec,
3402 basic_block bb,
3403 struct df_insn_info *insn_info,
3404 int flags)
3406 rtx note;
3407 bool is_sibling_call;
3408 unsigned int i;
3409 HARD_REG_SET defs_generated;
3411 CLEAR_HARD_REG_SET (defs_generated);
3412 df_find_hard_reg_defs (PATTERN (insn_info->insn), &defs_generated);
3413 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3415 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3417 if (i == STACK_POINTER_REGNUM)
3418 /* The stack ptr is used (honorarily) by a CALL insn. */
3419 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3420 NULL, bb, insn_info, DF_REF_REG_USE,
3421 DF_REF_CALL_STACK_USAGE | flags);
3422 else if (global_regs[i])
3424 /* Calls to const functions cannot access any global registers and
3425 calls to pure functions cannot set them. All other calls may
3426 reference any of the global registers, so they are recorded as
3427 used. */
3428 if (!RTL_CONST_CALL_P (insn_info->insn))
3430 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3431 NULL, bb, insn_info, DF_REF_REG_USE, flags);
3432 if (!RTL_PURE_CALL_P (insn_info->insn))
3433 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3434 NULL, bb, insn_info, DF_REF_REG_DEF, flags);
3437 else if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
3438 /* no clobbers for regs that are the result of the call */
3439 && !TEST_HARD_REG_BIT (defs_generated, i)
3440 && (!is_sibling_call
3441 || !bitmap_bit_p (df->exit_block_uses, i)
3442 || refers_to_regno_p (i, i+1,
3443 crtl->return_rtx, NULL)))
3444 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3445 NULL, bb, insn_info, DF_REF_REG_DEF,
3446 DF_REF_MAY_CLOBBER | flags);
3449 /* Record the registers used to pass arguments, and explicitly
3450 noted as clobbered. */
3451 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3452 note = XEXP (note, 1))
3454 if (GET_CODE (XEXP (note, 0)) == USE)
3455 df_uses_record (collection_rec, &XEXP (XEXP (note, 0), 0),
3456 DF_REF_REG_USE, bb, insn_info, flags);
3457 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3459 if (REG_P (XEXP (XEXP (note, 0), 0)))
3461 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3462 if (!TEST_HARD_REG_BIT (defs_generated, regno))
3463 df_defs_record (collection_rec, XEXP (note, 0), bb,
3464 insn_info, flags);
3466 else
3467 df_uses_record (collection_rec, &XEXP (note, 0),
3468 DF_REF_REG_USE, bb, insn_info, flags);
3472 return;
3475 /* Collect all refs in the INSN. This function is free of any
3476 side-effect - it will create and return a lists of df_ref's in the
3477 COLLECTION_REC without putting those refs into existing ref chains
3478 and reg chains. */
3480 static void
3481 df_insn_refs_collect (struct df_collection_rec *collection_rec,
3482 basic_block bb, struct df_insn_info *insn_info)
3484 rtx note;
3485 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3487 /* Clear out the collection record. */
3488 collection_rec->def_vec.truncate (0);
3489 collection_rec->use_vec.truncate (0);
3490 collection_rec->eq_use_vec.truncate (0);
3491 collection_rec->mw_vec.truncate (0);
3493 /* Process REG_EQUIV/REG_EQUAL notes. */
3494 for (note = REG_NOTES (insn_info->insn); note;
3495 note = XEXP (note, 1))
3497 switch (REG_NOTE_KIND (note))
3499 case REG_EQUIV:
3500 case REG_EQUAL:
3501 df_uses_record (collection_rec,
3502 &XEXP (note, 0), DF_REF_REG_USE,
3503 bb, insn_info, DF_REF_IN_NOTE);
3504 break;
3505 case REG_NON_LOCAL_GOTO:
3506 /* The frame ptr is used by a non-local goto. */
3507 df_ref_record (DF_REF_BASE, collection_rec,
3508 regno_reg_rtx[FRAME_POINTER_REGNUM],
3509 NULL, bb, insn_info,
3510 DF_REF_REG_USE, 0);
3511 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3512 df_ref_record (DF_REF_BASE, collection_rec,
3513 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3514 NULL, bb, insn_info,
3515 DF_REF_REG_USE, 0);
3516 #endif
3517 break;
3518 default:
3519 break;
3523 /* For CALL_INSNs, first record DF_REF_BASE register defs, as well as
3524 uses from CALL_INSN_FUNCTION_USAGE. */
3525 if (CALL_P (insn_info->insn))
3526 df_get_call_refs (collection_rec, bb, insn_info,
3527 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3529 /* Record other defs. These should be mostly for DF_REF_REGULAR, so
3530 that a qsort on the defs is unnecessary in most cases. */
3531 df_defs_record (collection_rec,
3532 PATTERN (insn_info->insn), bb, insn_info, 0);
3534 /* Record the register uses. */
3535 df_uses_record (collection_rec,
3536 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0);
3538 /* DF_REF_CONDITIONAL needs corresponding USES. */
3539 if (is_cond_exec)
3540 df_get_conditional_uses (collection_rec);
3542 df_canonize_collection_rec (collection_rec);
3545 /* Recompute the luids for the insns in BB. */
3547 void
3548 df_recompute_luids (basic_block bb)
3550 rtx insn;
3551 int luid = 0;
3553 df_grow_insn_info ();
3555 /* Scan the block an insn at a time from beginning to end. */
3556 FOR_BB_INSNS (bb, insn)
3558 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3559 /* Inserting labels does not always trigger the incremental
3560 rescanning. */
3561 if (!insn_info)
3563 gcc_assert (!INSN_P (insn));
3564 insn_info = df_insn_create_insn_record (insn);
3567 DF_INSN_INFO_LUID (insn_info) = luid;
3568 if (INSN_P (insn))
3569 luid++;
3574 /* Collect all artificial refs at the block level for BB and add them
3575 to COLLECTION_REC. */
3577 static void
3578 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3580 collection_rec->def_vec.truncate (0);
3581 collection_rec->use_vec.truncate (0);
3582 collection_rec->eq_use_vec.truncate (0);
3583 collection_rec->mw_vec.truncate (0);
3585 if (bb->index == ENTRY_BLOCK)
3587 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3588 return;
3590 else if (bb->index == EXIT_BLOCK)
3592 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3593 return;
3596 #ifdef EH_RETURN_DATA_REGNO
3597 if (bb_has_eh_pred (bb))
3599 unsigned int i;
3600 /* Mark the registers that will contain data for the handler. */
3601 for (i = 0; ; ++i)
3603 unsigned regno = EH_RETURN_DATA_REGNO (i);
3604 if (regno == INVALID_REGNUM)
3605 break;
3606 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3607 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3610 #endif
3612 /* Add the hard_frame_pointer if this block is the target of a
3613 non-local goto. */
3614 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3615 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3616 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3618 /* Add the artificial uses. */
3619 if (bb->index >= NUM_FIXED_BLOCKS)
3621 bitmap_iterator bi;
3622 unsigned int regno;
3623 bitmap au = bb_has_eh_pred (bb)
3624 ? &df->eh_block_artificial_uses
3625 : &df->regular_block_artificial_uses;
3627 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3629 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3630 bb, NULL, DF_REF_REG_USE, 0);
3634 df_canonize_collection_rec (collection_rec);
3638 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3640 void
3641 df_bb_refs_record (int bb_index, bool scan_insns)
3643 basic_block bb = BASIC_BLOCK (bb_index);
3644 rtx insn;
3645 int luid = 0;
3646 struct df_collection_rec collection_rec;
3648 if (!df)
3649 return;
3651 df_grow_bb_info (df_scan);
3652 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
3653 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
3654 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
3655 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
3657 if (scan_insns)
3658 /* Scan the block an insn at a time from beginning to end. */
3659 FOR_BB_INSNS (bb, insn)
3661 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3662 gcc_assert (!insn_info);
3664 insn_info = df_insn_create_insn_record (insn);
3665 if (INSN_P (insn))
3667 /* Record refs within INSN. */
3668 DF_INSN_INFO_LUID (insn_info) = luid++;
3669 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3670 df_refs_add_to_chains (&collection_rec, bb, insn);
3672 DF_INSN_INFO_LUID (insn_info) = luid;
3675 /* Other block level artificial refs */
3676 df_bb_refs_collect (&collection_rec, bb);
3677 df_refs_add_to_chains (&collection_rec, bb, NULL);
3679 collection_rec.def_vec.release ();
3680 collection_rec.use_vec.release ();
3681 collection_rec.eq_use_vec.release ();
3682 collection_rec.mw_vec.release ();
3684 /* Now that the block has been processed, set the block as dirty so
3685 LR and LIVE will get it processed. */
3686 df_set_bb_dirty (bb);
3690 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3691 block. */
3693 static void
3694 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3696 #ifdef EH_USES
3697 unsigned int i;
3698 #endif
3700 bitmap_clear (regular_block_artificial_uses);
3702 if (reload_completed)
3704 if (frame_pointer_needed)
3705 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3707 else
3708 /* Before reload, there are a few registers that must be forced
3709 live everywhere -- which might not already be the case for
3710 blocks within infinite loops. */
3712 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3714 /* Any reference to any pseudo before reload is a potential
3715 reference of the frame pointer. */
3716 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3718 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3719 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3720 #endif
3722 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3723 /* Pseudos with argument area equivalences may require
3724 reloading via the argument pointer. */
3725 if (fixed_regs[ARG_POINTER_REGNUM])
3726 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3727 #endif
3729 /* Any constant, or pseudo with constant equivalences, may
3730 require reloading from memory using the pic register. */
3731 if (picreg != INVALID_REGNUM
3732 && fixed_regs[picreg])
3733 bitmap_set_bit (regular_block_artificial_uses, picreg);
3735 /* The all-important stack pointer must always be live. */
3736 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3738 #ifdef EH_USES
3739 /* EH_USES registers are used:
3740 1) at all insns that might throw (calls or with -fnon-call-exceptions
3741 trapping insns)
3742 2) in all EH edges
3743 3) to support backtraces and/or debugging, anywhere between their
3744 initialization and where they the saved registers are restored
3745 from them, including the cases where we don't reach the epilogue
3746 (noreturn call or infinite loop). */
3747 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3748 if (EH_USES (i))
3749 bitmap_set_bit (regular_block_artificial_uses, i);
3750 #endif
3754 /* Get the artificial use set for an eh block. */
3756 static void
3757 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3759 bitmap_clear (eh_block_artificial_uses);
3761 /* The following code (down through the arg_pointer setting APPEARS
3762 to be necessary because there is nothing that actually
3763 describes what the exception handling code may actually need
3764 to keep alive. */
3765 if (reload_completed)
3767 if (frame_pointer_needed)
3769 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3770 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3771 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3772 #endif
3774 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3775 if (fixed_regs[ARG_POINTER_REGNUM])
3776 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3777 #endif
3783 /*----------------------------------------------------------------------------
3784 Specialized hard register scanning functions.
3785 ----------------------------------------------------------------------------*/
3788 /* Mark a register in SET. Hard registers in large modes get all
3789 of their component registers set as well. */
3791 static void
3792 df_mark_reg (rtx reg, void *vset)
3794 bitmap set = (bitmap) vset;
3795 int regno = REGNO (reg);
3797 gcc_assert (GET_MODE (reg) != BLKmode);
3799 if (regno < FIRST_PSEUDO_REGISTER)
3801 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3802 bitmap_set_range (set, regno, n);
3804 else
3805 bitmap_set_bit (set, regno);
3809 /* Set the bit for regs that are considered being defined at the entry. */
3811 static void
3812 df_get_entry_block_def_set (bitmap entry_block_defs)
3814 rtx r;
3815 int i;
3817 bitmap_clear (entry_block_defs);
3819 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3821 if (global_regs[i])
3822 bitmap_set_bit (entry_block_defs, i);
3823 if (FUNCTION_ARG_REGNO_P (i))
3824 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3827 /* The always important stack pointer. */
3828 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3830 /* Once the prologue has been generated, all of these registers
3831 should just show up in the first regular block. */
3832 if (HAVE_prologue && epilogue_completed)
3834 /* Defs for the callee saved registers are inserted so that the
3835 pushes have some defining location. */
3836 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3837 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3838 bitmap_set_bit (entry_block_defs, i);
3841 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3842 if (r && REG_P (r))
3843 bitmap_set_bit (entry_block_defs, REGNO (r));
3845 /* If the function has an incoming STATIC_CHAIN, it has to show up
3846 in the entry def set. */
3847 r = targetm.calls.static_chain (current_function_decl, true);
3848 if (r && REG_P (r))
3849 bitmap_set_bit (entry_block_defs, REGNO (r));
3851 if ((!reload_completed) || frame_pointer_needed)
3853 /* Any reference to any pseudo before reload is a potential
3854 reference of the frame pointer. */
3855 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3856 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3857 /* If they are different, also mark the hard frame pointer as live. */
3858 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3859 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3860 #endif
3863 /* These registers are live everywhere. */
3864 if (!reload_completed)
3866 #ifdef PIC_OFFSET_TABLE_REGNUM
3867 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3868 #endif
3870 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3871 /* Pseudos with argument area equivalences may require
3872 reloading via the argument pointer. */
3873 if (fixed_regs[ARG_POINTER_REGNUM])
3874 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3875 #endif
3877 #ifdef PIC_OFFSET_TABLE_REGNUM
3878 /* Any constant, or pseudo with constant equivalences, may
3879 require reloading from memory using the pic register. */
3880 if (picreg != INVALID_REGNUM
3881 && fixed_regs[picreg])
3882 bitmap_set_bit (entry_block_defs, picreg);
3883 #endif
3886 #ifdef INCOMING_RETURN_ADDR_RTX
3887 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3888 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3889 #endif
3891 targetm.extra_live_on_entry (entry_block_defs);
3895 /* Return the (conservative) set of hard registers that are defined on
3896 entry to the function.
3897 It uses df->entry_block_defs to determine which register
3898 reference to include. */
3900 static void
3901 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3902 bitmap entry_block_defs)
3904 unsigned int i;
3905 bitmap_iterator bi;
3907 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3909 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3910 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0);
3913 df_canonize_collection_rec (collection_rec);
3917 /* Record the (conservative) set of hard registers that are defined on
3918 entry to the function. */
3920 static void
3921 df_record_entry_block_defs (bitmap entry_block_defs)
3923 struct df_collection_rec collection_rec;
3924 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3925 vec_stack_alloc (df_ref, collection_rec.def_vec, FIRST_PSEUDO_REGISTER);
3926 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3928 /* Process bb_refs chain */
3929 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3930 collection_rec.def_vec.release ();
3934 /* Update the defs in the entry block. */
3936 void
3937 df_update_entry_block_defs (void)
3939 bitmap_head refs;
3940 bool changed = false;
3942 bitmap_initialize (&refs, &df_bitmap_obstack);
3943 df_get_entry_block_def_set (&refs);
3944 if (df->entry_block_defs)
3946 if (!bitmap_equal_p (df->entry_block_defs, &refs))
3948 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3949 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3950 df_ref_chain_delete (bb_info->artificial_defs);
3951 bb_info->artificial_defs = NULL;
3952 changed = true;
3955 else
3957 struct df_scan_problem_data *problem_data
3958 = (struct df_scan_problem_data *) df_scan->problem_data;
3959 gcc_unreachable ();
3960 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3961 changed = true;
3964 if (changed)
3966 df_record_entry_block_defs (&refs);
3967 bitmap_copy (df->entry_block_defs, &refs);
3968 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
3970 bitmap_clear (&refs);
3974 /* Set the bit for regs that are considered being used at the exit. */
3976 static void
3977 df_get_exit_block_use_set (bitmap exit_block_uses)
3979 unsigned int i;
3980 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3982 bitmap_clear (exit_block_uses);
3984 /* Stack pointer is always live at the exit. */
3985 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
3987 /* Mark the frame pointer if needed at the end of the function.
3988 If we end up eliminating it, it will be removed from the live
3989 list of each basic block by reload. */
3991 if ((!reload_completed) || frame_pointer_needed)
3993 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
3994 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3995 /* If they are different, also mark the hard frame pointer as live. */
3996 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3997 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
3998 #endif
4001 /* Many architectures have a GP register even without flag_pic.
4002 Assume the pic register is not in use, or will be handled by
4003 other means, if it is not fixed. */
4004 if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
4005 && picreg != INVALID_REGNUM
4006 && fixed_regs[picreg])
4007 bitmap_set_bit (exit_block_uses, picreg);
4009 /* Mark all global registers, and all registers used by the
4010 epilogue as being live at the end of the function since they
4011 may be referenced by our caller. */
4012 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4013 if (global_regs[i] || EPILOGUE_USES (i))
4014 bitmap_set_bit (exit_block_uses, i);
4016 if (HAVE_epilogue && epilogue_completed)
4018 /* Mark all call-saved registers that we actually used. */
4019 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4020 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
4021 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
4022 bitmap_set_bit (exit_block_uses, i);
4025 #ifdef EH_RETURN_DATA_REGNO
4026 /* Mark the registers that will contain data for the handler. */
4027 if (reload_completed && crtl->calls_eh_return)
4028 for (i = 0; ; ++i)
4030 unsigned regno = EH_RETURN_DATA_REGNO (i);
4031 if (regno == INVALID_REGNUM)
4032 break;
4033 bitmap_set_bit (exit_block_uses, regno);
4035 #endif
4037 #ifdef EH_RETURN_STACKADJ_RTX
4038 if ((!HAVE_epilogue || ! epilogue_completed)
4039 && crtl->calls_eh_return)
4041 rtx tmp = EH_RETURN_STACKADJ_RTX;
4042 if (tmp && REG_P (tmp))
4043 df_mark_reg (tmp, exit_block_uses);
4045 #endif
4047 #ifdef EH_RETURN_HANDLER_RTX
4048 if ((!HAVE_epilogue || ! epilogue_completed)
4049 && crtl->calls_eh_return)
4051 rtx tmp = EH_RETURN_HANDLER_RTX;
4052 if (tmp && REG_P (tmp))
4053 df_mark_reg (tmp, exit_block_uses);
4055 #endif
4057 /* Mark function return value. */
4058 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4062 /* Return the refs of hard registers that are used in the exit block.
4063 It uses df->exit_block_uses to determine register to include. */
4065 static void
4066 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4068 unsigned int i;
4069 bitmap_iterator bi;
4071 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4072 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4073 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4075 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4076 /* It is deliberate that this is not put in the exit block uses but
4077 I do not know why. */
4078 if (reload_completed
4079 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4080 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4081 && fixed_regs[ARG_POINTER_REGNUM])
4082 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4083 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0);
4084 #endif
4086 df_canonize_collection_rec (collection_rec);
4090 /* Record the set of hard registers that are used in the exit block.
4091 It uses df->exit_block_uses to determine which bit to include. */
4093 static void
4094 df_record_exit_block_uses (bitmap exit_block_uses)
4096 struct df_collection_rec collection_rec;
4097 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4098 vec_stack_alloc (df_ref, collection_rec.use_vec, FIRST_PSEUDO_REGISTER);
4099 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4101 /* Process bb_refs chain */
4102 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4103 collection_rec.use_vec.release ();
4107 /* Update the uses in the exit block. */
4109 void
4110 df_update_exit_block_uses (void)
4112 bitmap_head refs;
4113 bool changed = false;
4115 bitmap_initialize (&refs, &df_bitmap_obstack);
4116 df_get_exit_block_use_set (&refs);
4117 if (df->exit_block_uses)
4119 if (!bitmap_equal_p (df->exit_block_uses, &refs))
4121 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4122 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4123 df_ref_chain_delete (bb_info->artificial_uses);
4124 bb_info->artificial_uses = NULL;
4125 changed = true;
4128 else
4130 struct df_scan_problem_data *problem_data
4131 = (struct df_scan_problem_data *) df_scan->problem_data;
4132 gcc_unreachable ();
4133 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4134 changed = true;
4137 if (changed)
4139 df_record_exit_block_uses (&refs);
4140 bitmap_copy (df->exit_block_uses,& refs);
4141 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4143 bitmap_clear (&refs);
4146 static bool initialized = false;
4149 /* Initialize some platform specific structures. */
4151 void
4152 df_hard_reg_init (void)
4154 #ifdef ELIMINABLE_REGS
4155 int i;
4156 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4157 #endif
4158 if (initialized)
4159 return;
4161 /* Record which registers will be eliminated. We use this in
4162 mark_used_regs. */
4163 CLEAR_HARD_REG_SET (elim_reg_set);
4165 #ifdef ELIMINABLE_REGS
4166 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4167 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4168 #else
4169 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4170 #endif
4172 initialized = true;
4176 /* Recompute the parts of scanning that are based on regs_ever_live
4177 because something changed in that array. */
4179 void
4180 df_update_entry_exit_and_calls (void)
4182 basic_block bb;
4184 df_update_entry_block_defs ();
4185 df_update_exit_block_uses ();
4187 /* The call insns need to be rescanned because there may be changes
4188 in the set of registers clobbered across the call. */
4189 FOR_EACH_BB (bb)
4191 rtx insn;
4192 FOR_BB_INSNS (bb, insn)
4194 if (INSN_P (insn) && CALL_P (insn))
4195 df_insn_rescan (insn);
4201 /* Return true if hard REG is actually used in the some instruction.
4202 There are a fair number of conditions that affect the setting of
4203 this array. See the comment in df.h for df->hard_regs_live_count
4204 for the conditions that this array is set. */
4206 bool
4207 df_hard_reg_used_p (unsigned int reg)
4209 return df->hard_regs_live_count[reg] != 0;
4213 /* A count of the number of times REG is actually used in the some
4214 instruction. There are a fair number of conditions that affect the
4215 setting of this array. See the comment in df.h for
4216 df->hard_regs_live_count for the conditions that this array is
4217 set. */
4220 unsigned int
4221 df_hard_reg_used_count (unsigned int reg)
4223 return df->hard_regs_live_count[reg];
4227 /* Get the value of regs_ever_live[REGNO]. */
4229 bool
4230 df_regs_ever_live_p (unsigned int regno)
4232 return regs_ever_live[regno];
4236 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4237 to change, schedule that change for the next update. */
4239 void
4240 df_set_regs_ever_live (unsigned int regno, bool value)
4242 if (regs_ever_live[regno] == value)
4243 return;
4245 regs_ever_live[regno] = value;
4246 if (df)
4247 df->redo_entry_and_exit = true;
4251 /* Compute "regs_ever_live" information from the underlying df
4252 information. Set the vector to all false if RESET. */
4254 void
4255 df_compute_regs_ever_live (bool reset)
4257 unsigned int i;
4258 bool changed = df->redo_entry_and_exit;
4260 if (reset)
4261 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4263 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4264 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4266 regs_ever_live[i] = true;
4267 changed = true;
4269 if (changed)
4270 df_update_entry_exit_and_calls ();
4271 df->redo_entry_and_exit = false;
4275 /*----------------------------------------------------------------------------
4276 Dataflow ref information verification functions.
4278 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4279 df_reg_chain_verify_unmarked (refs)
4280 df_refs_verify (vec<stack, va_df_ref>, ref*, bool)
4281 df_mws_verify (mw*, mw*, bool)
4282 df_insn_refs_verify (collection_rec, bb, insn, bool)
4283 df_bb_refs_verify (bb, refs, bool)
4284 df_bb_verify (bb)
4285 df_exit_block_bitmap_verify (bool)
4286 df_entry_block_bitmap_verify (bool)
4287 df_scan_verify ()
4288 ----------------------------------------------------------------------------*/
4291 /* Mark all refs in the reg chain. Verify that all of the registers
4292 are in the correct chain. */
4294 static unsigned int
4295 df_reg_chain_mark (df_ref refs, unsigned int regno,
4296 bool is_def, bool is_eq_use)
4298 unsigned int count = 0;
4299 df_ref ref;
4300 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4302 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4304 /* If there are no def-use or use-def chains, make sure that all
4305 of the chains are clear. */
4306 if (!df_chain)
4307 gcc_assert (!DF_REF_CHAIN (ref));
4309 /* Check to make sure the ref is in the correct chain. */
4310 gcc_assert (DF_REF_REGNO (ref) == regno);
4311 if (is_def)
4312 gcc_assert (DF_REF_REG_DEF_P (ref));
4313 else
4314 gcc_assert (!DF_REF_REG_DEF_P (ref));
4316 if (is_eq_use)
4317 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4318 else
4319 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4321 if (DF_REF_NEXT_REG (ref))
4322 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4323 count++;
4324 DF_REF_REG_MARK (ref);
4326 return count;
4330 /* Verify that all of the registers in the chain are unmarked. */
4332 static void
4333 df_reg_chain_verify_unmarked (df_ref refs)
4335 df_ref ref;
4336 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4337 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4341 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4343 static bool
4344 df_refs_verify (vec<df_ref, va_stack> new_rec, df_ref *old_rec,
4345 bool abort_if_fail)
4347 unsigned int ix;
4348 df_ref new_ref;
4350 FOR_EACH_VEC_ELT (new_rec, ix, new_ref)
4352 if (*old_rec == NULL || !df_ref_equal_p (new_ref, *old_rec))
4354 if (abort_if_fail)
4355 gcc_assert (0);
4356 else
4357 return false;
4360 /* Abort if fail is called from the function level verifier. If
4361 that is the context, mark this reg as being seem. */
4362 if (abort_if_fail)
4364 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4365 DF_REF_REG_UNMARK (*old_rec);
4368 old_rec++;
4371 if (abort_if_fail)
4372 gcc_assert (*old_rec == NULL);
4373 else
4374 return *old_rec == NULL;
4375 return false;
4379 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4381 static bool
4382 df_mws_verify (vec<df_mw_hardreg_ptr, va_stack> new_rec,
4383 struct df_mw_hardreg **old_rec,
4384 bool abort_if_fail)
4386 unsigned int ix;
4387 struct df_mw_hardreg *new_reg;
4389 FOR_EACH_VEC_ELT (new_rec, ix, new_reg)
4391 if (*old_rec == NULL || !df_mw_equal_p (new_reg, *old_rec))
4393 if (abort_if_fail)
4394 gcc_assert (0);
4395 else
4396 return false;
4398 old_rec++;
4401 if (abort_if_fail)
4402 gcc_assert (*old_rec == NULL);
4403 else
4404 return *old_rec == NULL;
4405 return false;
4409 /* Return true if the existing insn refs information is complete and
4410 correct. Otherwise (i.e. if there's any missing or extra refs),
4411 return the correct df_ref chain in REFS_RETURN.
4413 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4414 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4415 verification mode instead of the whole function, so unmark
4416 everything.
4418 If ABORT_IF_FAIL is set, this function never returns false. */
4420 static bool
4421 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4422 basic_block bb,
4423 rtx insn,
4424 bool abort_if_fail)
4426 bool ret1, ret2, ret3, ret4;
4427 unsigned int uid = INSN_UID (insn);
4428 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4430 df_insn_refs_collect (collection_rec, bb, insn_info);
4432 if (!DF_INSN_UID_DEFS (uid))
4434 /* The insn_rec was created but it was never filled out. */
4435 if (abort_if_fail)
4436 gcc_assert (0);
4437 else
4438 return false;
4441 /* Unfortunately we cannot opt out early if one of these is not
4442 right because the marks will not get cleared. */
4443 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4444 abort_if_fail);
4445 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4446 abort_if_fail);
4447 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4448 abort_if_fail);
4449 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4450 abort_if_fail);
4451 return (ret1 && ret2 && ret3 && ret4);
4455 /* Return true if all refs in the basic block are correct and complete.
4456 Due to df_ref_chain_verify, it will cause all refs
4457 that are verified to have DF_REF_MARK bit set. */
4459 static bool
4460 df_bb_verify (basic_block bb)
4462 rtx insn;
4463 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4464 struct df_collection_rec collection_rec;
4466 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4467 vec_stack_alloc (df_ref, collection_rec.def_vec, 128);
4468 vec_stack_alloc (df_ref, collection_rec.use_vec, 32);
4469 vec_stack_alloc (df_ref, collection_rec.eq_use_vec, 32);
4470 vec_stack_alloc (df_mw_hardreg_ptr, collection_rec.mw_vec, 32);
4472 gcc_assert (bb_info);
4474 /* Scan the block, one insn at a time, from beginning to end. */
4475 FOR_BB_INSNS_REVERSE (bb, insn)
4477 if (!INSN_P (insn))
4478 continue;
4479 df_insn_refs_verify (&collection_rec, bb, insn, true);
4480 df_free_collection_rec (&collection_rec);
4483 /* Do the artificial defs and uses. */
4484 df_bb_refs_collect (&collection_rec, bb);
4485 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4486 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4487 df_free_collection_rec (&collection_rec);
4489 return true;
4493 /* Returns true if the entry block has correct and complete df_ref set.
4494 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4496 static bool
4497 df_entry_block_bitmap_verify (bool abort_if_fail)
4499 bitmap_head entry_block_defs;
4500 bool is_eq;
4502 bitmap_initialize (&entry_block_defs, &df_bitmap_obstack);
4503 df_get_entry_block_def_set (&entry_block_defs);
4505 is_eq = bitmap_equal_p (&entry_block_defs, df->entry_block_defs);
4507 if (!is_eq && abort_if_fail)
4509 fprintf (stderr, "entry_block_defs = ");
4510 df_print_regset (stderr, &entry_block_defs);
4511 fprintf (stderr, "df->entry_block_defs = ");
4512 df_print_regset (stderr, df->entry_block_defs);
4513 gcc_assert (0);
4516 bitmap_clear (&entry_block_defs);
4518 return is_eq;
4522 /* Returns true if the exit block has correct and complete df_ref set.
4523 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4525 static bool
4526 df_exit_block_bitmap_verify (bool abort_if_fail)
4528 bitmap_head exit_block_uses;
4529 bool is_eq;
4531 bitmap_initialize (&exit_block_uses, &df_bitmap_obstack);
4532 df_get_exit_block_use_set (&exit_block_uses);
4534 is_eq = bitmap_equal_p (&exit_block_uses, df->exit_block_uses);
4536 if (!is_eq && abort_if_fail)
4538 fprintf (stderr, "exit_block_uses = ");
4539 df_print_regset (stderr, &exit_block_uses);
4540 fprintf (stderr, "df->exit_block_uses = ");
4541 df_print_regset (stderr, df->exit_block_uses);
4542 gcc_assert (0);
4545 bitmap_clear (&exit_block_uses);
4547 return is_eq;
4551 /* Return true if df_ref information for all insns in all blocks are
4552 correct and complete. */
4554 void
4555 df_scan_verify (void)
4557 unsigned int i;
4558 basic_block bb;
4559 bitmap_head regular_block_artificial_uses;
4560 bitmap_head eh_block_artificial_uses;
4562 if (!df)
4563 return;
4565 /* Verification is a 4 step process. */
4567 /* (1) All of the refs are marked by going through the reg chains. */
4568 for (i = 0; i < DF_REG_SIZE (df); i++)
4570 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4571 == DF_REG_DEF_COUNT(i));
4572 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4573 == DF_REG_USE_COUNT(i));
4574 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4575 == DF_REG_EQ_USE_COUNT(i));
4578 /* (2) There are various bitmaps whose value may change over the
4579 course of the compilation. This step recomputes them to make
4580 sure that they have not slipped out of date. */
4581 bitmap_initialize (&regular_block_artificial_uses, &df_bitmap_obstack);
4582 bitmap_initialize (&eh_block_artificial_uses, &df_bitmap_obstack);
4584 df_get_regular_block_artificial_uses (&regular_block_artificial_uses);
4585 df_get_eh_block_artificial_uses (&eh_block_artificial_uses);
4587 bitmap_ior_into (&eh_block_artificial_uses,
4588 &regular_block_artificial_uses);
4590 /* Check artificial_uses bitmaps didn't change. */
4591 gcc_assert (bitmap_equal_p (&regular_block_artificial_uses,
4592 &df->regular_block_artificial_uses));
4593 gcc_assert (bitmap_equal_p (&eh_block_artificial_uses,
4594 &df->eh_block_artificial_uses));
4596 bitmap_clear (&regular_block_artificial_uses);
4597 bitmap_clear (&eh_block_artificial_uses);
4599 /* Verify entry block and exit block. These only verify the bitmaps,
4600 the refs are verified in df_bb_verify. */
4601 df_entry_block_bitmap_verify (true);
4602 df_exit_block_bitmap_verify (true);
4604 /* (3) All of the insns in all of the blocks are traversed and the
4605 marks are cleared both in the artificial refs attached to the
4606 blocks and the real refs inside the insns. It is a failure to
4607 clear a mark that has not been set as this means that the ref in
4608 the block or insn was not in the reg chain. */
4610 FOR_ALL_BB (bb)
4611 df_bb_verify (bb);
4613 /* (4) See if all reg chains are traversed a second time. This time
4614 a check is made that the marks are clear. A set mark would be a
4615 from a reg that is not in any insn or basic block. */
4617 for (i = 0; i < DF_REG_SIZE (df); i++)
4619 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4620 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4621 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));