gcc/:
[official-gcc.git] / gcc / df-scan.c
blobfe00af2d3ec939961fbd172ccaa7dc092aa5e1ce
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
4 Originally contributed by Michael P. Hayes
5 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
6 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
7 and Kenneth Zadeck (zadeck@naturalbridge.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "regs.h"
35 #include "output.h"
36 #include "alloc-pool.h"
37 #include "flags.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "sbitmap.h"
41 #include "bitmap.h"
42 #include "timevar.h"
43 #include "tree.h"
44 #include "target.h"
45 #include "target-def.h"
46 #include "df.h"
47 #include "tree-pass.h"
49 #ifndef HAVE_epilogue
50 #define HAVE_epilogue 0
51 #endif
52 #ifndef HAVE_prologue
53 #define HAVE_prologue 0
54 #endif
55 #ifndef HAVE_sibcall_epilogue
56 #define HAVE_sibcall_epilogue 0
57 #endif
59 #ifndef EPILOGUE_USES
60 #define EPILOGUE_USES(REGNO) 0
61 #endif
63 /* The following two macros free the vecs that hold either the refs or
64 the mw refs. They are a little tricky because the vec has 0
65 elements is special and is not to be freed. */
66 #define df_scan_free_ref_vec(V) \
67 do { \
68 if (V && *V) \
69 free (V); \
70 } while (0)
72 #define df_scan_free_mws_vec(V) \
73 do { \
74 if (V && *V) \
75 free (V); \
76 } while (0)
78 /* The set of hard registers in eliminables[i].from. */
80 static HARD_REG_SET elim_reg_set;
82 /* Initialize ur_in and ur_out as if all hard registers were partially
83 available. */
85 struct df_collection_rec
87 df_ref * def_vec;
88 df_ref * use_vec;
89 unsigned int next_def;
90 unsigned int next_use;
91 df_ref * eq_use_vec;
92 struct df_mw_hardreg **mw_vec;
93 unsigned int next_eq_use;
94 unsigned int next_mw;
97 static df_ref df_null_ref_rec[1];
98 static struct df_mw_hardreg * df_null_mw_rec[1];
100 static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
101 rtx, rtx *,
102 basic_block, struct df_insn_info *,
103 enum df_ref_type, int ref_flags,
104 int, int, enum machine_mode);
105 static void df_def_record_1 (struct df_collection_rec *, rtx,
106 basic_block, struct df_insn_info *,
107 int ref_flags);
108 static void df_defs_record (struct df_collection_rec *, rtx,
109 basic_block, struct df_insn_info *,
110 int ref_flags);
111 static void df_uses_record (enum df_ref_class, struct df_collection_rec *,
112 rtx *, enum df_ref_type,
113 basic_block, struct df_insn_info *,
114 int ref_flags,
115 int, int, enum machine_mode);
117 static df_ref df_ref_create_structure (enum df_ref_class,
118 struct df_collection_rec *, rtx, rtx *,
119 basic_block, struct df_insn_info *,
120 enum df_ref_type, int ref_flags,
121 int, int, enum machine_mode);
123 static void df_insn_refs_collect (struct df_collection_rec*,
124 basic_block, struct df_insn_info *);
125 static void df_canonize_collection_rec (struct df_collection_rec *);
127 static void df_get_regular_block_artificial_uses (bitmap);
128 static void df_get_eh_block_artificial_uses (bitmap);
130 static void df_record_entry_block_defs (bitmap);
131 static void df_record_exit_block_uses (bitmap);
132 static void df_get_exit_block_use_set (bitmap);
133 static void df_get_entry_block_def_set (bitmap);
134 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
135 static void df_ref_chain_delete_du_chain (df_ref *);
136 static void df_ref_chain_delete (df_ref *);
138 static void df_refs_add_to_chains (struct df_collection_rec *,
139 basic_block, rtx);
141 static bool df_insn_refs_verify (struct df_collection_rec *, basic_block, rtx, bool);
142 static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
143 static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
144 static void df_install_ref (df_ref, struct df_reg_info *,
145 struct df_ref_info *, bool);
147 static int df_ref_compare (const void *, const void *);
148 static int df_mw_compare (const void *, const void *);
150 /* Indexed by hardware reg number, is true if that register is ever
151 used in the current function.
153 In df-scan.c, this is set up to record the hard regs used
154 explicitly. Reload adds in the hard regs used for holding pseudo
155 regs. Final uses it to generate the code in the function prologue
156 and epilogue to save and restore registers as needed. */
158 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
160 /*----------------------------------------------------------------------------
161 SCANNING DATAFLOW PROBLEM
163 There are several ways in which scanning looks just like the other
164 dataflow problems. It shares the all the mechanisms for local info
165 as well as basic block info. Where it differs is when and how often
166 it gets run. It also has no need for the iterative solver.
167 ----------------------------------------------------------------------------*/
169 /* Problem data for the scanning dataflow function. */
170 struct df_scan_problem_data
172 alloc_pool ref_base_pool;
173 alloc_pool ref_artificial_pool;
174 alloc_pool ref_regular_pool;
175 alloc_pool ref_extract_pool;
176 alloc_pool insn_pool;
177 alloc_pool reg_pool;
178 alloc_pool mw_reg_pool;
179 bitmap_obstack reg_bitmaps;
180 bitmap_obstack insn_bitmaps;
183 typedef struct df_scan_bb_info *df_scan_bb_info_t;
186 /* Internal function to shut down the scanning problem. */
187 static void
188 df_scan_free_internal (void)
190 struct df_scan_problem_data *problem_data
191 = (struct df_scan_problem_data *) df_scan->problem_data;
192 unsigned int i;
193 basic_block bb;
195 /* The vectors that hold the refs are not pool allocated because
196 they come in many sizes. This makes them impossible to delete
197 all at once. */
198 for (i = 0; i < DF_INSN_SIZE(); i++)
200 struct df_insn_info *insn_info = DF_INSN_UID_GET(i);
201 /* Skip the insns that have no insn_info or have been
202 deleted. */
203 if (insn_info)
205 df_scan_free_ref_vec (insn_info->defs);
206 df_scan_free_ref_vec (insn_info->uses);
207 df_scan_free_ref_vec (insn_info->eq_uses);
208 df_scan_free_mws_vec (insn_info->mw_hardregs);
212 FOR_ALL_BB (bb)
214 unsigned int bb_index = bb->index;
215 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
216 if (bb_info)
218 df_scan_free_ref_vec (bb_info->artificial_defs);
219 df_scan_free_ref_vec (bb_info->artificial_uses);
223 free (df->def_info.refs);
224 free (df->def_info.begin);
225 free (df->def_info.count);
226 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
228 free (df->use_info.refs);
229 free (df->use_info.begin);
230 free (df->use_info.count);
231 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
233 free (df->def_regs);
234 df->def_regs = NULL;
235 free (df->use_regs);
236 df->use_regs = NULL;
237 free (df->eq_use_regs);
238 df->eq_use_regs = NULL;
239 df->regs_size = 0;
240 DF_REG_SIZE(df) = 0;
242 free (df->insns);
243 df->insns = NULL;
244 DF_INSN_SIZE () = 0;
246 free (df_scan->block_info);
247 df_scan->block_info = NULL;
248 df_scan->block_info_size = 0;
250 BITMAP_FREE (df->hardware_regs_used);
251 BITMAP_FREE (df->regular_block_artificial_uses);
252 BITMAP_FREE (df->eh_block_artificial_uses);
253 BITMAP_FREE (df->entry_block_defs);
254 BITMAP_FREE (df->exit_block_uses);
255 BITMAP_FREE (df->insns_to_delete);
256 BITMAP_FREE (df->insns_to_rescan);
257 BITMAP_FREE (df->insns_to_notes_rescan);
259 free_alloc_pool (df_scan->block_pool);
260 free_alloc_pool (problem_data->ref_base_pool);
261 free_alloc_pool (problem_data->ref_artificial_pool);
262 free_alloc_pool (problem_data->ref_regular_pool);
263 free_alloc_pool (problem_data->ref_extract_pool);
264 free_alloc_pool (problem_data->insn_pool);
265 free_alloc_pool (problem_data->reg_pool);
266 free_alloc_pool (problem_data->mw_reg_pool);
267 bitmap_obstack_release (&problem_data->reg_bitmaps);
268 bitmap_obstack_release (&problem_data->insn_bitmaps);
269 free (df_scan->problem_data);
273 /* Set basic block info. */
275 static void
276 df_scan_set_bb_info (unsigned int index,
277 struct df_scan_bb_info *bb_info)
279 gcc_assert (df_scan);
280 df_grow_bb_info (df_scan);
281 df_scan->block_info[index] = (void *) bb_info;
285 /* Free basic block info. */
287 static void
288 df_scan_free_bb_info (basic_block bb, void *vbb_info)
290 struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
291 unsigned int bb_index = bb->index;
292 if (bb_info)
294 rtx insn;
295 FOR_BB_INSNS (bb, insn)
297 if (INSN_P (insn))
298 /* Record defs within INSN. */
299 df_insn_delete (bb, INSN_UID (insn));
302 if (bb_index < df_scan->block_info_size)
303 bb_info = df_scan_get_bb_info (bb_index);
305 /* Get rid of any artificial uses or defs. */
306 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
307 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
308 df_ref_chain_delete (bb_info->artificial_defs);
309 df_ref_chain_delete (bb_info->artificial_uses);
310 bb_info->artificial_defs = NULL;
311 bb_info->artificial_uses = NULL;
312 pool_free (df_scan->block_pool, bb_info);
317 /* Allocate the problem data for the scanning problem. This should be
318 called when the problem is created or when the entire function is to
319 be rescanned. */
320 void
321 df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
323 struct df_scan_problem_data *problem_data;
324 unsigned int insn_num = get_max_uid () + 1;
325 unsigned int block_size = 400;
326 basic_block bb;
328 /* Given the number of pools, this is really faster than tearing
329 everything apart. */
330 if (df_scan->problem_data)
331 df_scan_free_internal ();
333 df_scan->block_pool
334 = create_alloc_pool ("df_scan_block pool",
335 sizeof (struct df_scan_bb_info),
336 block_size);
338 problem_data = XNEW (struct df_scan_problem_data);
339 df_scan->problem_data = problem_data;
340 df_scan->computed = true;
342 problem_data->ref_base_pool
343 = create_alloc_pool ("df_scan ref base",
344 sizeof (struct df_base_ref), block_size);
345 problem_data->ref_artificial_pool
346 = create_alloc_pool ("df_scan ref artificial",
347 sizeof (struct df_artificial_ref), block_size);
348 problem_data->ref_regular_pool
349 = create_alloc_pool ("df_scan ref regular",
350 sizeof (struct df_regular_ref), block_size);
351 problem_data->ref_extract_pool
352 = create_alloc_pool ("df_scan ref extract",
353 sizeof (struct df_extract_ref), block_size);
354 problem_data->insn_pool
355 = create_alloc_pool ("df_scan insn",
356 sizeof (struct df_insn_info), block_size);
357 problem_data->reg_pool
358 = create_alloc_pool ("df_scan reg",
359 sizeof (struct df_reg_info), block_size);
360 problem_data->mw_reg_pool
361 = create_alloc_pool ("df_scan mw_reg",
362 sizeof (struct df_mw_hardreg), block_size);
364 bitmap_obstack_initialize (&problem_data->reg_bitmaps);
365 bitmap_obstack_initialize (&problem_data->insn_bitmaps);
367 insn_num += insn_num / 4;
368 df_grow_reg_info ();
370 df_grow_insn_info ();
371 df_grow_bb_info (df_scan);
373 FOR_ALL_BB (bb)
375 unsigned int bb_index = bb->index;
376 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
377 if (!bb_info)
379 bb_info = (struct df_scan_bb_info *) pool_alloc (df_scan->block_pool);
380 df_scan_set_bb_info (bb_index, bb_info);
382 bb_info->artificial_defs = NULL;
383 bb_info->artificial_uses = NULL;
386 df->hardware_regs_used = BITMAP_ALLOC (&problem_data->reg_bitmaps);
387 df->regular_block_artificial_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
388 df->eh_block_artificial_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
389 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
390 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
391 df->insns_to_delete = BITMAP_ALLOC (&problem_data->insn_bitmaps);
392 df->insns_to_rescan = BITMAP_ALLOC (&problem_data->insn_bitmaps);
393 df->insns_to_notes_rescan = BITMAP_ALLOC (&problem_data->insn_bitmaps);
394 df_scan->optional_p = false;
398 /* Free all of the data associated with the scan problem. */
400 static void
401 df_scan_free (void)
403 if (df_scan->problem_data)
404 df_scan_free_internal ();
406 if (df->blocks_to_analyze)
408 BITMAP_FREE (df->blocks_to_analyze);
409 df->blocks_to_analyze = NULL;
412 free (df_scan);
415 /* Dump the preamble for DF_SCAN dump. */
416 static void
417 df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
419 int i;
420 int dcount = 0;
421 int ucount = 0;
422 int ecount = 0;
423 int icount = 0;
424 int ccount = 0;
425 basic_block bb;
426 rtx insn;
428 fprintf (file, ";; invalidated by call \t");
429 df_print_regset (file, regs_invalidated_by_call_regset);
430 fprintf (file, ";; hardware regs used \t");
431 df_print_regset (file, df->hardware_regs_used);
432 fprintf (file, ";; regular block artificial uses \t");
433 df_print_regset (file, df->regular_block_artificial_uses);
434 fprintf (file, ";; eh block artificial uses \t");
435 df_print_regset (file, df->eh_block_artificial_uses);
436 fprintf (file, ";; entry block defs \t");
437 df_print_regset (file, df->entry_block_defs);
438 fprintf (file, ";; exit block uses \t");
439 df_print_regset (file, df->exit_block_uses);
440 fprintf (file, ";; regs ever live \t");
441 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
442 if (df_regs_ever_live_p (i))
443 fprintf (file, " %d[%s]", i, reg_names[i]);
444 fprintf (file, "\n;; ref usage \t");
446 for (i = 0; i < (int)df->regs_inited; i++)
447 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
449 const char * sep = "";
451 fprintf (file, "r%d={", i);
452 if (DF_REG_DEF_COUNT (i))
454 fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
455 sep = ",";
456 dcount += DF_REG_DEF_COUNT (i);
458 if (DF_REG_USE_COUNT (i))
460 fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
461 sep = ",";
462 ucount += DF_REG_USE_COUNT (i);
464 if (DF_REG_EQ_USE_COUNT (i))
466 fprintf (file, "%s%dd", sep, DF_REG_EQ_USE_COUNT (i));
467 ecount += DF_REG_EQ_USE_COUNT (i);
469 fprintf (file, "} ");
472 FOR_EACH_BB (bb)
473 FOR_BB_INSNS (bb, insn)
474 if (INSN_P (insn))
476 if (CALL_P (insn))
477 ccount++;
478 else
479 icount++;
482 fprintf (file, "\n;; total ref usage %d{%dd,%du,%de} in %d{%d regular + %d call} insns.\n",
483 dcount + ucount + ecount, dcount, ucount, ecount, icount + ccount, icount, ccount);
486 /* Dump the bb_info for a given basic block. */
487 static void
488 df_scan_start_block (basic_block bb, FILE *file)
490 struct df_scan_bb_info *bb_info
491 = df_scan_get_bb_info (bb->index);
493 if (bb_info)
495 fprintf (file, ";; bb %d artificial_defs: ", bb->index);
496 df_refs_chain_dump (bb_info->artificial_defs, true, file);
497 fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
498 df_refs_chain_dump (bb_info->artificial_uses, true, file);
499 fprintf (file, "\n");
501 #if 0
503 rtx insn;
504 FOR_BB_INSNS (bb, insn)
505 if (INSN_P (insn))
506 df_insn_debug (insn, false, file);
508 #endif
511 static struct df_problem problem_SCAN =
513 DF_SCAN, /* Problem id. */
514 DF_NONE, /* Direction. */
515 df_scan_alloc, /* Allocate the problem specific data. */
516 NULL, /* Reset global information. */
517 df_scan_free_bb_info, /* Free basic block info. */
518 NULL, /* Local compute function. */
519 NULL, /* Init the solution specific data. */
520 NULL, /* Iterative solver. */
521 NULL, /* Confluence operator 0. */
522 NULL, /* Confluence operator n. */
523 NULL, /* Transfer function. */
524 NULL, /* Finalize function. */
525 df_scan_free, /* Free all of the problem information. */
526 NULL, /* Remove this problem from the stack of dataflow problems. */
527 df_scan_start_dump, /* Debugging. */
528 df_scan_start_block, /* Debugging start block. */
529 NULL, /* Debugging end block. */
530 NULL, /* Incremental solution verify start. */
531 NULL, /* Incremental solution verify end. */
532 NULL, /* Dependent problem. */
533 TV_DF_SCAN, /* Timing variable. */
534 false /* Reset blocks on dropping out of blocks_to_analyze. */
538 /* Create a new DATAFLOW instance and add it to an existing instance
539 of DF. The returned structure is what is used to get at the
540 solution. */
542 void
543 df_scan_add_problem (void)
545 df_add_problem (&problem_SCAN);
549 /*----------------------------------------------------------------------------
550 Storage Allocation Utilities
551 ----------------------------------------------------------------------------*/
554 /* First, grow the reg_info information. If the current size is less than
555 the number of pseudos, grow to 25% more than the number of
556 pseudos.
558 Second, assure that all of the slots up to max_reg_num have been
559 filled with reg_info structures. */
561 void
562 df_grow_reg_info (void)
564 unsigned int max_reg = max_reg_num ();
565 unsigned int new_size = max_reg;
566 struct df_scan_problem_data *problem_data
567 = (struct df_scan_problem_data *) df_scan->problem_data;
568 unsigned int i;
570 if (df->regs_size < new_size)
572 new_size += new_size / 4;
573 df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
574 df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
575 df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
576 new_size);
577 df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
578 df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
579 df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
580 df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
581 df->regs_size = new_size;
584 for (i = df->regs_inited; i < max_reg; i++)
586 struct df_reg_info *reg_info;
588 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
589 memset (reg_info, 0, sizeof (struct df_reg_info));
590 df->def_regs[i] = reg_info;
591 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
592 memset (reg_info, 0, sizeof (struct df_reg_info));
593 df->use_regs[i] = reg_info;
594 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
595 memset (reg_info, 0, sizeof (struct df_reg_info));
596 df->eq_use_regs[i] = reg_info;
597 df->def_info.begin[i] = 0;
598 df->def_info.count[i] = 0;
599 df->use_info.begin[i] = 0;
600 df->use_info.count[i] = 0;
603 df->regs_inited = max_reg;
607 /* Grow the ref information. */
609 static void
610 df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
612 if (ref_info->refs_size < new_size)
614 ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
615 memset (ref_info->refs + ref_info->refs_size, 0,
616 (new_size - ref_info->refs_size) *sizeof (df_ref));
617 ref_info->refs_size = new_size;
622 /* Check and grow the ref information if necessary. This routine
623 guarantees total_size + BITMAP_ADDEND amount of entries in refs
624 array. It updates ref_info->refs_size only and does not change
625 ref_info->total_size. */
627 static void
628 df_check_and_grow_ref_info (struct df_ref_info *ref_info,
629 unsigned bitmap_addend)
631 if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
633 int new_size = ref_info->total_size + bitmap_addend;
634 new_size += ref_info->total_size / 4;
635 df_grow_ref_info (ref_info, new_size);
640 /* Grow the ref information. If the current size is less than the
641 number of instructions, grow to 25% more than the number of
642 instructions. */
644 void
645 df_grow_insn_info (void)
647 unsigned int new_size = get_max_uid () + 1;
648 if (DF_INSN_SIZE () < new_size)
650 new_size += new_size / 4;
651 df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
652 memset (df->insns + df->insns_size, 0,
653 (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
654 DF_INSN_SIZE () = new_size;
661 /*----------------------------------------------------------------------------
662 PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
663 ----------------------------------------------------------------------------*/
665 /* Rescan all of the block_to_analyze or all of the blocks in the
666 function if df_set_blocks if blocks_to_analyze is NULL; */
668 void
669 df_scan_blocks (void)
671 basic_block bb;
673 df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
674 df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
676 df_get_regular_block_artificial_uses (df->regular_block_artificial_uses);
677 df_get_eh_block_artificial_uses (df->eh_block_artificial_uses);
679 bitmap_ior_into (df->eh_block_artificial_uses,
680 df->regular_block_artificial_uses);
682 /* ENTRY and EXIT blocks have special defs/uses. */
683 df_get_entry_block_def_set (df->entry_block_defs);
684 df_record_entry_block_defs (df->entry_block_defs);
685 df_get_exit_block_use_set (df->exit_block_uses);
686 df_record_exit_block_uses (df->exit_block_uses);
687 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
688 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
690 /* Regular blocks */
691 FOR_EACH_BB (bb)
693 unsigned int bb_index = bb->index;
694 df_bb_refs_record (bb_index, true);
699 /* Create a new ref of type DF_REF_TYPE for register REG at address
700 LOC within INSN of BB. This function is only used externally.
702 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
703 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
704 fields if they were constants. Otherwise they should be -1 if
705 those flags were set. */
707 df_ref
708 df_ref_create (rtx reg, rtx *loc, rtx insn,
709 basic_block bb,
710 enum df_ref_type ref_type,
711 int ref_flags,
712 int width, int offset, enum machine_mode mode)
714 df_ref ref;
715 struct df_reg_info **reg_info;
716 struct df_ref_info *ref_info;
717 df_ref *ref_rec;
718 df_ref **ref_rec_ptr;
719 unsigned int count = 0;
720 bool add_to_table;
721 enum df_ref_class cl;
723 df_grow_reg_info ();
725 /* You cannot hack artificial refs. */
726 gcc_assert (insn);
728 if (width != -1 || offset != -1)
729 cl = DF_REF_EXTRACT;
730 else if (loc)
731 cl = DF_REF_REGULAR;
732 else
733 cl = DF_REF_BASE;
734 ref = df_ref_create_structure (cl, NULL, reg, loc, bb, DF_INSN_INFO_GET (insn),
735 ref_type, ref_flags,
736 width, offset, mode);
738 if (DF_REF_REG_DEF_P (ref))
740 reg_info = df->def_regs;
741 ref_info = &df->def_info;
742 ref_rec_ptr = &DF_INSN_DEFS (insn);
743 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
745 else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
747 reg_info = df->eq_use_regs;
748 ref_info = &df->use_info;
749 ref_rec_ptr = &DF_INSN_EQ_USES (insn);
750 switch (ref_info->ref_order)
752 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
753 case DF_REF_ORDER_BY_REG_WITH_NOTES:
754 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
755 add_to_table = true;
756 break;
757 default:
758 add_to_table = false;
759 break;
762 else
764 reg_info = df->use_regs;
765 ref_info = &df->use_info;
766 ref_rec_ptr = &DF_INSN_USES (insn);
767 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
770 /* Do not add if ref is not in the right blocks. */
771 if (add_to_table && df->analyze_subset)
772 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
774 df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
776 if (add_to_table)
777 switch (ref_info->ref_order)
779 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
780 case DF_REF_ORDER_BY_REG_WITH_NOTES:
781 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
782 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
783 break;
784 default:
785 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
786 break;
789 ref_rec = *ref_rec_ptr;
790 while (*ref_rec)
792 count++;
793 ref_rec++;
796 ref_rec = *ref_rec_ptr;
797 if (count)
799 ref_rec = XRESIZEVEC (df_ref, ref_rec, count+2);
800 *ref_rec_ptr = ref_rec;
801 ref_rec[count] = ref;
802 ref_rec[count+1] = NULL;
803 qsort (ref_rec, count + 1, sizeof (df_ref), df_ref_compare);
805 else
807 df_ref *ref_rec = XNEWVEC (df_ref, 2);
808 ref_rec[0] = ref;
809 ref_rec[1] = NULL;
810 *ref_rec_ptr = ref_rec;
813 #if 0
814 if (dump_file)
816 fprintf (dump_file, "adding ref ");
817 df_ref_debug (ref, dump_file);
819 #endif
820 /* By adding the ref directly, df_insn_rescan my not find any
821 differences even though the block will have changed. So we need
822 to mark the block dirty ourselves. */
823 df_set_bb_dirty (bb);
825 return ref;
830 /*----------------------------------------------------------------------------
831 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
832 ----------------------------------------------------------------------------*/
834 static void
835 df_free_ref (df_ref ref)
837 struct df_scan_problem_data *problem_data
838 = (struct df_scan_problem_data *) df_scan->problem_data;
840 switch (DF_REF_CLASS (ref))
842 case DF_REF_BASE:
843 pool_free (problem_data->ref_base_pool, ref);
844 break;
846 case DF_REF_ARTIFICIAL:
847 pool_free (problem_data->ref_artificial_pool, ref);
848 break;
850 case DF_REF_REGULAR:
851 pool_free (problem_data->ref_regular_pool, ref);
852 break;
854 case DF_REF_EXTRACT:
855 pool_free (problem_data->ref_extract_pool, ref);
856 break;
861 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
862 Also delete the def-use or use-def chain if it exists. */
864 static void
865 df_reg_chain_unlink (df_ref ref)
867 df_ref next = DF_REF_NEXT_REG (ref);
868 df_ref prev = DF_REF_PREV_REG (ref);
869 int id = DF_REF_ID (ref);
870 struct df_reg_info *reg_info;
871 df_ref *refs = NULL;
873 if (DF_REF_REG_DEF_P (ref))
875 int regno = DF_REF_REGNO (ref);
876 reg_info = DF_REG_DEF_GET (regno);
877 refs = df->def_info.refs;
879 else
881 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
883 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
884 switch (df->use_info.ref_order)
886 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
887 case DF_REF_ORDER_BY_REG_WITH_NOTES:
888 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
889 refs = df->use_info.refs;
890 break;
891 default:
892 break;
895 else
897 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
898 refs = df->use_info.refs;
902 if (refs)
904 if (df->analyze_subset)
906 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
907 refs[id] = NULL;
909 else
910 refs[id] = NULL;
913 /* Delete any def-use or use-def chains that start here. It is
914 possible that there is trash in this field. This happens for
915 insns that have been deleted when rescanning has been deferred
916 and the chain problem has also been deleted. The chain tear down
917 code skips deleted insns. */
918 if (df_chain && DF_REF_CHAIN (ref))
919 df_chain_unlink (ref);
921 reg_info->n_refs--;
922 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
924 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
925 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
928 /* Unlink from the reg chain. If there is no prev, this is the
929 first of the list. If not, just join the next and prev. */
930 if (prev)
931 DF_REF_NEXT_REG (prev) = next;
932 else
934 gcc_assert (reg_info->reg_chain == ref);
935 reg_info->reg_chain = next;
937 if (next)
938 DF_REF_PREV_REG (next) = prev;
940 df_free_ref (ref);
944 /* Remove REF from VEC. */
946 static void
947 df_ref_compress_rec (df_ref **vec_ptr, df_ref ref)
949 df_ref *vec = *vec_ptr;
951 if (vec[1])
953 while (*vec && *vec != ref)
954 vec++;
956 while (*vec)
958 *vec = *(vec+1);
959 vec++;
962 else
964 free (vec);
965 *vec_ptr = df_null_ref_rec;
970 /* Unlink REF from all def-use/use-def chains, etc. */
972 void
973 df_ref_remove (df_ref ref)
975 #if 0
976 if (dump_file)
978 fprintf (dump_file, "removing ref ");
979 df_ref_debug (ref, dump_file);
981 #endif
983 if (DF_REF_REG_DEF_P (ref))
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_defs, ref);
991 else
993 unsigned int uid = DF_REF_INSN_UID (ref);
994 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
995 df_ref_compress_rec (&insn_rec->defs, ref);
998 else
1000 if (DF_REF_IS_ARTIFICIAL (ref))
1002 struct df_scan_bb_info *bb_info
1003 = df_scan_get_bb_info (DF_REF_BBNO (ref));
1004 df_ref_compress_rec (&bb_info->artificial_uses, ref);
1006 else
1008 unsigned int uid = DF_REF_INSN_UID (ref);
1009 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
1011 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
1012 df_ref_compress_rec (&insn_rec->eq_uses, ref);
1013 else
1014 df_ref_compress_rec (&insn_rec->uses, ref);
1018 /* By deleting the ref directly, df_insn_rescan my not find any
1019 differences even though the block will have changed. So we need
1020 to mark the block dirty ourselves. */
1021 df_set_bb_dirty (DF_REF_BB (ref));
1022 df_reg_chain_unlink (ref);
1026 /* Create the insn record for INSN. If there was one there, zero it
1027 out. */
1029 struct df_insn_info *
1030 df_insn_create_insn_record (rtx insn)
1032 struct df_scan_problem_data *problem_data
1033 = (struct df_scan_problem_data *) df_scan->problem_data;
1034 struct df_insn_info *insn_rec;
1036 df_grow_insn_info ();
1037 insn_rec = DF_INSN_INFO_GET (insn);
1038 if (!insn_rec)
1040 insn_rec = (struct df_insn_info *) pool_alloc (problem_data->insn_pool);
1041 DF_INSN_INFO_SET (insn, insn_rec);
1043 memset (insn_rec, 0, sizeof (struct df_insn_info));
1044 insn_rec->insn = insn;
1045 return insn_rec;
1049 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
1051 static void
1052 df_ref_chain_delete_du_chain (df_ref *ref_rec)
1054 while (*ref_rec)
1056 df_ref ref = *ref_rec;
1057 /* CHAIN is allocated by DF_CHAIN. So make sure to
1058 pass df_scan instance for the problem. */
1059 if (DF_REF_CHAIN (ref))
1060 df_chain_unlink (ref);
1061 ref_rec++;
1066 /* Delete all refs in the ref chain. */
1068 static void
1069 df_ref_chain_delete (df_ref *ref_rec)
1071 df_ref *start = ref_rec;
1072 while (*ref_rec)
1074 df_reg_chain_unlink (*ref_rec);
1075 ref_rec++;
1078 /* If the list is empty, it has a special shared element that is not
1079 to be deleted. */
1080 if (*start)
1081 free (start);
1085 /* Delete the hardreg chain. */
1087 static void
1088 df_mw_hardreg_chain_delete (struct df_mw_hardreg **hardregs)
1090 struct df_scan_problem_data *problem_data;
1092 if (!hardregs)
1093 return;
1095 problem_data = (struct df_scan_problem_data *) df_scan->problem_data;
1097 while (*hardregs)
1099 pool_free (problem_data->mw_reg_pool, *hardregs);
1100 hardregs++;
1105 /* Delete all of the refs information from INSN. BB must be passed in
1106 except when called from df_process_deferred_rescans to mark the block
1107 as dirty. */
1109 void
1110 df_insn_delete (basic_block bb, unsigned int uid)
1112 struct df_insn_info *insn_info = NULL;
1113 if (!df)
1114 return;
1116 df_grow_bb_info (df_scan);
1117 df_grow_reg_info ();
1119 /* The block must be marked as dirty now, rather than later as in
1120 df_insn_rescan and df_notes_rescan because it may not be there at
1121 rescanning time and the mark would blow up. */
1122 if (bb)
1123 df_set_bb_dirty (bb);
1125 insn_info = DF_INSN_UID_SAFE_GET (uid);
1127 /* The client has deferred rescanning. */
1128 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1130 if (insn_info)
1132 bitmap_clear_bit (df->insns_to_rescan, uid);
1133 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1134 bitmap_set_bit (df->insns_to_delete, uid);
1136 if (dump_file)
1137 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1138 return;
1141 if (dump_file)
1142 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1144 bitmap_clear_bit (df->insns_to_delete, uid);
1145 bitmap_clear_bit (df->insns_to_rescan, uid);
1146 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1147 if (insn_info)
1149 struct df_scan_problem_data *problem_data
1150 = (struct df_scan_problem_data *) df_scan->problem_data;
1152 /* In general, notes do not have the insn_info fields
1153 initialized. However, combine deletes insns by changing them
1154 to notes. How clever. So we cannot just check if it is a
1155 valid insn before short circuiting this code, we need to see
1156 if we actually initialized it. */
1157 if (insn_info->defs)
1159 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1161 if (df_chain)
1163 df_ref_chain_delete_du_chain (insn_info->defs);
1164 df_ref_chain_delete_du_chain (insn_info->uses);
1165 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1168 df_ref_chain_delete (insn_info->defs);
1169 df_ref_chain_delete (insn_info->uses);
1170 df_ref_chain_delete (insn_info->eq_uses);
1172 pool_free (problem_data->insn_pool, insn_info);
1173 DF_INSN_UID_SET (uid, NULL);
1178 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1180 static void
1181 df_free_collection_rec (struct df_collection_rec *collection_rec)
1183 struct df_scan_problem_data *problem_data
1184 = (struct df_scan_problem_data *) df_scan->problem_data;
1185 df_ref *ref;
1186 struct df_mw_hardreg **mw;
1188 if (collection_rec->def_vec)
1189 for (ref = collection_rec->def_vec; *ref; ref++)
1190 df_free_ref (*ref);
1191 if (collection_rec->use_vec)
1192 for (ref = collection_rec->use_vec; *ref; ref++)
1193 df_free_ref (*ref);
1194 if (collection_rec->eq_use_vec)
1195 for (ref = collection_rec->eq_use_vec; *ref; ref++)
1196 df_free_ref (*ref);
1197 if (collection_rec->mw_vec)
1198 for (mw = collection_rec->mw_vec; *mw; mw++)
1199 pool_free (problem_data->mw_reg_pool, *mw);
1203 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1205 bool
1206 df_insn_rescan (rtx insn)
1208 unsigned int uid = INSN_UID (insn);
1209 struct df_insn_info *insn_info = NULL;
1210 basic_block bb = BLOCK_FOR_INSN (insn);
1211 struct df_collection_rec collection_rec;
1212 collection_rec.def_vec = XALLOCAVEC (df_ref, 1000);
1213 collection_rec.use_vec = XALLOCAVEC (df_ref, 1000);
1214 collection_rec.eq_use_vec = XALLOCAVEC (df_ref, 1000);
1215 collection_rec.mw_vec = XALLOCAVEC (struct df_mw_hardreg *, 100);
1217 if ((!df) || (!INSN_P (insn)))
1218 return false;
1220 if (!bb)
1222 if (dump_file)
1223 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1224 return false;
1227 /* The client has disabled rescanning and plans to do it itself. */
1228 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1229 return false;
1231 df_grow_bb_info (df_scan);
1232 df_grow_reg_info ();
1234 insn_info = DF_INSN_UID_SAFE_GET (uid);
1236 /* The client has deferred rescanning. */
1237 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1239 if (!insn_info)
1241 insn_info = df_insn_create_insn_record (insn);
1242 insn_info->defs = df_null_ref_rec;
1243 insn_info->uses = df_null_ref_rec;
1244 insn_info->eq_uses = df_null_ref_rec;
1245 insn_info->mw_hardregs = df_null_mw_rec;
1247 if (dump_file)
1248 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1250 bitmap_clear_bit (df->insns_to_delete, uid);
1251 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1252 bitmap_set_bit (df->insns_to_rescan, INSN_UID (insn));
1253 return false;
1256 bitmap_clear_bit (df->insns_to_delete, uid);
1257 bitmap_clear_bit (df->insns_to_rescan, uid);
1258 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1259 if (insn_info)
1261 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1262 /* If there's no change, return false. */
1263 if (the_same)
1265 df_free_collection_rec (&collection_rec);
1266 if (dump_file)
1267 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1268 return false;
1270 if (dump_file)
1271 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1273 /* There's change - we need to delete the existing info. */
1274 df_insn_delete (NULL, uid);
1275 df_insn_create_insn_record (insn);
1277 else
1279 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1280 df_insn_refs_collect (&collection_rec, bb, insn_info);
1281 if (dump_file)
1282 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1285 df_refs_add_to_chains (&collection_rec, bb, insn);
1286 df_set_bb_dirty (bb);
1287 return true;
1291 /* Rescan all of the insns in the function. Note that the artificial
1292 uses and defs are not touched. This function will destroy def-se
1293 or use-def chains. */
1295 void
1296 df_insn_rescan_all (void)
1298 bool no_insn_rescan = false;
1299 bool defer_insn_rescan = false;
1300 basic_block bb;
1301 bitmap_iterator bi;
1302 unsigned int uid;
1303 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1305 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1307 df_clear_flags (DF_NO_INSN_RESCAN);
1308 no_insn_rescan = true;
1311 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1313 df_clear_flags (DF_DEFER_INSN_RESCAN);
1314 defer_insn_rescan = true;
1317 bitmap_copy (tmp, df->insns_to_delete);
1318 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1320 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1321 if (insn_info)
1322 df_insn_delete (NULL, uid);
1325 BITMAP_FREE (tmp);
1326 bitmap_clear (df->insns_to_delete);
1327 bitmap_clear (df->insns_to_rescan);
1328 bitmap_clear (df->insns_to_notes_rescan);
1330 FOR_EACH_BB (bb)
1332 rtx insn;
1333 FOR_BB_INSNS (bb, insn)
1335 df_insn_rescan (insn);
1339 if (no_insn_rescan)
1340 df_set_flags (DF_NO_INSN_RESCAN);
1341 if (defer_insn_rescan)
1342 df_set_flags (DF_DEFER_INSN_RESCAN);
1346 /* Process all of the deferred rescans or deletions. */
1348 void
1349 df_process_deferred_rescans (void)
1351 bool no_insn_rescan = false;
1352 bool defer_insn_rescan = false;
1353 bitmap_iterator bi;
1354 unsigned int uid;
1355 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1357 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1359 df_clear_flags (DF_NO_INSN_RESCAN);
1360 no_insn_rescan = true;
1363 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1365 df_clear_flags (DF_DEFER_INSN_RESCAN);
1366 defer_insn_rescan = true;
1369 if (dump_file)
1370 fprintf (dump_file, "starting the processing of deferred insns\n");
1372 bitmap_copy (tmp, df->insns_to_delete);
1373 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1375 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1376 if (insn_info)
1377 df_insn_delete (NULL, uid);
1380 bitmap_copy (tmp, df->insns_to_rescan);
1381 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1383 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1384 if (insn_info)
1385 df_insn_rescan (insn_info->insn);
1388 bitmap_copy (tmp, df->insns_to_notes_rescan);
1389 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1391 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1392 if (insn_info)
1393 df_notes_rescan (insn_info->insn);
1396 if (dump_file)
1397 fprintf (dump_file, "ending the processing of deferred insns\n");
1399 BITMAP_FREE (tmp);
1400 bitmap_clear (df->insns_to_delete);
1401 bitmap_clear (df->insns_to_rescan);
1402 bitmap_clear (df->insns_to_notes_rescan);
1404 if (no_insn_rescan)
1405 df_set_flags (DF_NO_INSN_RESCAN);
1406 if (defer_insn_rescan)
1407 df_set_flags (DF_DEFER_INSN_RESCAN);
1409 /* If someone changed regs_ever_live during this pass, fix up the
1410 entry and exit blocks. */
1411 if (df->redo_entry_and_exit)
1413 df_update_entry_exit_and_calls ();
1414 df->redo_entry_and_exit = false;
1419 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1420 the uses if INCLUDE_USES. Include the eq_uses if
1421 INCLUDE_EQ_USES. */
1423 static unsigned int
1424 df_count_refs (bool include_defs, bool include_uses,
1425 bool include_eq_uses)
1427 unsigned int regno;
1428 int size = 0;
1429 unsigned int m = df->regs_inited;
1431 for (regno = 0; regno < m; regno++)
1433 if (include_defs)
1434 size += DF_REG_DEF_COUNT (regno);
1435 if (include_uses)
1436 size += DF_REG_USE_COUNT (regno);
1437 if (include_eq_uses)
1438 size += DF_REG_EQ_USE_COUNT (regno);
1440 return size;
1444 /* Take build ref table for either the uses or defs from the reg-use
1445 or reg-def chains. This version processes the refs in reg order
1446 which is likely to be best if processing the whole function. */
1448 static void
1449 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1450 bool include_defs,
1451 bool include_uses,
1452 bool include_eq_uses)
1454 unsigned int m = df->regs_inited;
1455 unsigned int regno;
1456 unsigned int offset = 0;
1457 unsigned int start;
1459 if (df->changeable_flags & DF_NO_HARD_REGS)
1461 start = FIRST_PSEUDO_REGISTER;
1462 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1463 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1465 else
1466 start = 0;
1468 ref_info->total_size
1469 = df_count_refs (include_defs, include_uses, include_eq_uses);
1471 df_check_and_grow_ref_info (ref_info, 1);
1473 for (regno = start; regno < m; regno++)
1475 int count = 0;
1476 ref_info->begin[regno] = offset;
1477 if (include_defs)
1479 df_ref ref = DF_REG_DEF_CHAIN (regno);
1480 while (ref)
1482 ref_info->refs[offset] = ref;
1483 DF_REF_ID (ref) = offset++;
1484 count++;
1485 ref = DF_REF_NEXT_REG (ref);
1486 gcc_assert (offset < ref_info->refs_size);
1489 if (include_uses)
1491 df_ref ref = DF_REG_USE_CHAIN (regno);
1492 while (ref)
1494 ref_info->refs[offset] = ref;
1495 DF_REF_ID (ref) = offset++;
1496 count++;
1497 ref = DF_REF_NEXT_REG (ref);
1498 gcc_assert (offset < ref_info->refs_size);
1501 if (include_eq_uses)
1503 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1504 while (ref)
1506 ref_info->refs[offset] = ref;
1507 DF_REF_ID (ref) = offset++;
1508 count++;
1509 ref = DF_REF_NEXT_REG (ref);
1510 gcc_assert (offset < ref_info->refs_size);
1513 ref_info->count[regno] = count;
1516 /* The bitmap size is not decremented when refs are deleted. So
1517 reset it now that we have squished out all of the empty
1518 slots. */
1519 ref_info->table_size = offset;
1523 /* Take build ref table for either the uses or defs from the reg-use
1524 or reg-def chains. This version processes the refs in insn order
1525 which is likely to be best if processing some segment of the
1526 function. */
1528 static void
1529 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1530 bool include_defs,
1531 bool include_uses,
1532 bool include_eq_uses)
1534 bitmap_iterator bi;
1535 unsigned int bb_index;
1536 unsigned int m = df->regs_inited;
1537 unsigned int offset = 0;
1538 unsigned int r;
1539 unsigned int start
1540 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1542 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1543 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1545 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1546 df_check_and_grow_ref_info (ref_info, 1);
1548 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1550 basic_block bb = BASIC_BLOCK (bb_index);
1551 rtx insn;
1552 df_ref *ref_rec;
1554 if (include_defs)
1555 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1557 unsigned int regno = DF_REF_REGNO (*ref_rec);
1558 ref_info->count[regno]++;
1560 if (include_uses)
1561 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1563 unsigned int regno = DF_REF_REGNO (*ref_rec);
1564 ref_info->count[regno]++;
1567 FOR_BB_INSNS (bb, insn)
1569 if (INSN_P (insn))
1571 unsigned int uid = INSN_UID (insn);
1573 if (include_defs)
1574 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1576 unsigned int regno = DF_REF_REGNO (*ref_rec);
1577 ref_info->count[regno]++;
1579 if (include_uses)
1580 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1582 unsigned int regno = DF_REF_REGNO (*ref_rec);
1583 ref_info->count[regno]++;
1585 if (include_eq_uses)
1586 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1588 unsigned int regno = DF_REF_REGNO (*ref_rec);
1589 ref_info->count[regno]++;
1595 for (r = start; r < m; r++)
1597 ref_info->begin[r] = offset;
1598 offset += ref_info->count[r];
1599 ref_info->count[r] = 0;
1602 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1604 basic_block bb = BASIC_BLOCK (bb_index);
1605 rtx insn;
1606 df_ref *ref_rec;
1608 if (include_defs)
1609 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1611 df_ref ref = *ref_rec;
1612 unsigned int regno = DF_REF_REGNO (ref);
1613 if (regno >= start)
1615 unsigned int id
1616 = ref_info->begin[regno] + ref_info->count[regno]++;
1617 DF_REF_ID (ref) = id;
1618 ref_info->refs[id] = ref;
1621 if (include_uses)
1622 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1624 df_ref ref = *ref_rec;
1625 unsigned int regno = DF_REF_REGNO (ref);
1626 if (regno >= start)
1628 unsigned int id
1629 = ref_info->begin[regno] + ref_info->count[regno]++;
1630 DF_REF_ID (ref) = id;
1631 ref_info->refs[id] = ref;
1635 FOR_BB_INSNS (bb, insn)
1637 if (INSN_P (insn))
1639 unsigned int uid = INSN_UID (insn);
1641 if (include_defs)
1642 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1644 df_ref ref = *ref_rec;
1645 unsigned int regno = DF_REF_REGNO (ref);
1646 if (regno >= start)
1648 unsigned int id
1649 = ref_info->begin[regno] + ref_info->count[regno]++;
1650 DF_REF_ID (ref) = id;
1651 ref_info->refs[id] = ref;
1654 if (include_uses)
1655 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1657 df_ref ref = *ref_rec;
1658 unsigned int regno = DF_REF_REGNO (ref);
1659 if (regno >= start)
1661 unsigned int id
1662 = ref_info->begin[regno] + ref_info->count[regno]++;
1663 DF_REF_ID (ref) = id;
1664 ref_info->refs[id] = ref;
1667 if (include_eq_uses)
1668 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1670 df_ref ref = *ref_rec;
1671 unsigned int regno = DF_REF_REGNO (ref);
1672 if (regno >= start)
1674 unsigned int id
1675 = ref_info->begin[regno] + ref_info->count[regno]++;
1676 DF_REF_ID (ref) = id;
1677 ref_info->refs[id] = ref;
1684 /* The bitmap size is not decremented when refs are deleted. So
1685 reset it now that we have squished out all of the empty
1686 slots. */
1688 ref_info->table_size = offset;
1691 /* Take build ref table for either the uses or defs from the reg-use
1692 or reg-def chains. */
1694 static void
1695 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1696 bool include_defs,
1697 bool include_uses,
1698 bool include_eq_uses)
1700 if (df->analyze_subset)
1701 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1702 include_uses, include_eq_uses);
1703 else
1704 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1705 include_uses, include_eq_uses);
1709 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1710 static unsigned int
1711 df_add_refs_to_table (unsigned int offset,
1712 struct df_ref_info *ref_info,
1713 df_ref *ref_vec)
1715 while (*ref_vec)
1717 df_ref ref = *ref_vec;
1718 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1719 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1721 ref_info->refs[offset] = ref;
1722 DF_REF_ID (*ref_vec) = offset++;
1724 ref_vec++;
1726 return offset;
1730 /* Count the number of refs in all of the insns of BB. Include the
1731 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1732 eq_uses if INCLUDE_EQ_USES. */
1734 static unsigned int
1735 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1736 struct df_ref_info *ref_info,
1737 bool include_defs, bool include_uses,
1738 bool include_eq_uses)
1740 rtx insn;
1742 if (include_defs)
1743 offset = df_add_refs_to_table (offset, ref_info,
1744 df_get_artificial_defs (bb->index));
1745 if (include_uses)
1746 offset = df_add_refs_to_table (offset, ref_info,
1747 df_get_artificial_uses (bb->index));
1749 FOR_BB_INSNS (bb, insn)
1750 if (INSN_P (insn))
1752 unsigned int uid = INSN_UID (insn);
1753 if (include_defs)
1754 offset = df_add_refs_to_table (offset, ref_info,
1755 DF_INSN_UID_DEFS (uid));
1756 if (include_uses)
1757 offset = df_add_refs_to_table (offset, ref_info,
1758 DF_INSN_UID_USES (uid));
1759 if (include_eq_uses)
1760 offset = df_add_refs_to_table (offset, ref_info,
1761 DF_INSN_UID_EQ_USES (uid));
1763 return offset;
1767 /* Organize the refs by insn into the table in REF_INFO. If
1768 blocks_to_analyze is defined, use that set, otherwise the entire
1769 program. Include the defs if INCLUDE_DEFS. Include the uses if
1770 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1772 static void
1773 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1774 bool include_defs, bool include_uses,
1775 bool include_eq_uses)
1777 basic_block bb;
1778 unsigned int offset = 0;
1780 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1781 df_check_and_grow_ref_info (ref_info, 1);
1782 if (df->blocks_to_analyze)
1784 bitmap_iterator bi;
1785 unsigned int index;
1787 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1789 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1790 include_defs, include_uses,
1791 include_eq_uses);
1794 ref_info->table_size = offset;
1796 else
1798 FOR_ALL_BB (bb)
1799 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1800 include_defs, include_uses,
1801 include_eq_uses);
1802 ref_info->table_size = offset;
1807 /* If the use refs in DF are not organized, reorganize them. */
1809 void
1810 df_maybe_reorganize_use_refs (enum df_ref_order order)
1812 if (order == df->use_info.ref_order)
1813 return;
1815 switch (order)
1817 case DF_REF_ORDER_BY_REG:
1818 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1819 break;
1821 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1822 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1823 break;
1825 case DF_REF_ORDER_BY_INSN:
1826 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1827 break;
1829 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1830 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1831 break;
1833 case DF_REF_ORDER_NO_TABLE:
1834 free (df->use_info.refs);
1835 df->use_info.refs = NULL;
1836 df->use_info.refs_size = 0;
1837 break;
1839 case DF_REF_ORDER_UNORDERED:
1840 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1841 gcc_unreachable ();
1842 break;
1845 df->use_info.ref_order = order;
1849 /* If the def refs in DF are not organized, reorganize them. */
1851 void
1852 df_maybe_reorganize_def_refs (enum df_ref_order order)
1854 if (order == df->def_info.ref_order)
1855 return;
1857 switch (order)
1859 case DF_REF_ORDER_BY_REG:
1860 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1861 break;
1863 case DF_REF_ORDER_BY_INSN:
1864 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1865 break;
1867 case DF_REF_ORDER_NO_TABLE:
1868 free (df->def_info.refs);
1869 df->def_info.refs = NULL;
1870 df->def_info.refs_size = 0;
1871 break;
1873 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1874 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1875 case DF_REF_ORDER_UNORDERED:
1876 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1877 gcc_unreachable ();
1878 break;
1881 df->def_info.ref_order = order;
1885 /* Change all of the basic block references in INSN to use the insn's
1886 current basic block. This function is called from routines that move
1887 instructions from one block to another. */
1889 void
1890 df_insn_change_bb (rtx insn, basic_block new_bb)
1892 basic_block old_bb = BLOCK_FOR_INSN (insn);
1893 struct df_insn_info *insn_info;
1894 unsigned int uid = INSN_UID (insn);
1896 if (old_bb == new_bb)
1897 return;
1899 set_block_for_insn (insn, new_bb);
1901 if (!df)
1902 return;
1904 if (dump_file)
1905 fprintf (dump_file, "changing bb of uid %d\n", uid);
1907 insn_info = DF_INSN_UID_SAFE_GET (uid);
1908 if (insn_info == NULL)
1910 if (dump_file)
1911 fprintf (dump_file, " unscanned insn\n");
1912 df_insn_rescan (insn);
1913 return;
1916 if (!INSN_P (insn))
1917 return;
1919 df_set_bb_dirty (new_bb);
1920 if (old_bb)
1922 if (dump_file)
1923 fprintf (dump_file, " from %d to %d\n",
1924 old_bb->index, new_bb->index);
1925 df_set_bb_dirty (old_bb);
1927 else
1928 if (dump_file)
1929 fprintf (dump_file, " to %d\n", new_bb->index);
1933 /* Helper function for df_ref_change_reg_with_loc. */
1935 static void
1936 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
1937 struct df_reg_info *new_df,
1938 int new_regno, rtx loc)
1940 df_ref the_ref = old_df->reg_chain;
1942 while (the_ref)
1944 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
1945 && (DF_REF_LOC (the_ref))
1946 && (*DF_REF_LOC (the_ref) == loc))
1948 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
1949 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
1950 df_ref *ref_vec, *ref_vec_t;
1951 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
1952 unsigned int count = 0;
1954 DF_REF_REGNO (the_ref) = new_regno;
1955 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
1957 /* Pull the_ref out of the old regno chain. */
1958 if (prev_ref)
1959 DF_REF_NEXT_REG (prev_ref) = next_ref;
1960 else
1961 old_df->reg_chain = next_ref;
1962 if (next_ref)
1963 DF_REF_PREV_REG (next_ref) = prev_ref;
1964 old_df->n_refs--;
1966 /* Put the ref into the new regno chain. */
1967 DF_REF_PREV_REG (the_ref) = NULL;
1968 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
1969 if (new_df->reg_chain)
1970 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
1971 new_df->reg_chain = the_ref;
1972 new_df->n_refs++;
1973 if (DF_REF_BB (the_ref))
1974 df_set_bb_dirty (DF_REF_BB (the_ref));
1976 /* Need to sort the record again that the ref was in because
1977 the regno is a sorting key. First, find the right
1978 record. */
1979 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
1980 ref_vec = insn_info->eq_uses;
1981 else
1982 ref_vec = insn_info->uses;
1983 if (dump_file)
1984 fprintf (dump_file, "changing reg in insn %d\n",
1985 DF_REF_INSN_UID (the_ref));
1987 ref_vec_t = ref_vec;
1989 /* Find the length. */
1990 while (*ref_vec_t)
1992 count++;
1993 ref_vec_t++;
1995 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
1997 the_ref = next_ref;
1999 else
2000 the_ref = DF_REF_NEXT_REG (the_ref);
2005 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2006 NEW_REGNO. Refs that do not match LOC are not changed which means
2007 that artificial refs are not changed since they have no loc. This
2008 call is to support the SET_REGNO macro. */
2010 void
2011 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2013 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2014 return;
2016 df_grow_reg_info ();
2018 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2019 DF_REG_DEF_GET (new_regno), new_regno, loc);
2020 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2021 DF_REG_USE_GET (new_regno), new_regno, loc);
2022 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2023 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2027 /* Delete the mw_hardregs that point into the eq_notes. */
2029 static unsigned int
2030 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2032 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2033 unsigned int deleted = 0;
2034 unsigned int count = 0;
2035 struct df_scan_problem_data *problem_data
2036 = (struct df_scan_problem_data *) df_scan->problem_data;
2038 if (!*mw_vec)
2039 return 0;
2041 while (*mw_vec)
2043 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2045 struct df_mw_hardreg **temp_vec = mw_vec;
2047 pool_free (problem_data->mw_reg_pool, *mw_vec);
2048 temp_vec = mw_vec;
2049 /* Shove the remaining ones down one to fill the gap. While
2050 this looks n**2, it is highly unusual to have any mw regs
2051 in eq_notes and the chances of more than one are almost
2052 non existent. */
2053 while (*temp_vec)
2055 *temp_vec = *(temp_vec + 1);
2056 temp_vec++;
2058 deleted++;
2060 else
2062 mw_vec++;
2063 count++;
2067 if (count == 0)
2069 df_scan_free_mws_vec (insn_info->mw_hardregs);
2070 insn_info->mw_hardregs = df_null_mw_rec;
2071 return 0;
2073 return deleted;
2077 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2079 void
2080 df_notes_rescan (rtx insn)
2082 struct df_insn_info *insn_info;
2083 unsigned int uid = INSN_UID (insn);
2085 if (!df)
2086 return;
2088 /* The client has disabled rescanning and plans to do it itself. */
2089 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2090 return;
2092 /* Do nothing if the insn hasn't been emitted yet. */
2093 if (!BLOCK_FOR_INSN (insn))
2094 return;
2096 df_grow_bb_info (df_scan);
2097 df_grow_reg_info ();
2099 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2101 /* The client has deferred rescanning. */
2102 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2104 if (!insn_info)
2106 insn_info = df_insn_create_insn_record (insn);
2107 insn_info->defs = df_null_ref_rec;
2108 insn_info->uses = df_null_ref_rec;
2109 insn_info->eq_uses = df_null_ref_rec;
2110 insn_info->mw_hardregs = df_null_mw_rec;
2113 bitmap_clear_bit (df->insns_to_delete, uid);
2114 /* If the insn is set to be rescanned, it does not need to also
2115 be notes rescanned. */
2116 if (!bitmap_bit_p (df->insns_to_rescan, uid))
2117 bitmap_set_bit (df->insns_to_notes_rescan, INSN_UID (insn));
2118 return;
2121 bitmap_clear_bit (df->insns_to_delete, uid);
2122 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
2124 if (insn_info)
2126 basic_block bb = BLOCK_FOR_INSN (insn);
2127 rtx note;
2128 struct df_collection_rec collection_rec;
2129 unsigned int num_deleted;
2131 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2132 collection_rec.eq_use_vec = XALLOCAVEC (df_ref, 1000);
2133 collection_rec.mw_vec = XALLOCAVEC (struct df_mw_hardreg *, 1000);
2135 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2136 df_ref_chain_delete (insn_info->eq_uses);
2137 insn_info->eq_uses = NULL;
2139 /* Process REG_EQUIV/REG_EQUAL notes */
2140 for (note = REG_NOTES (insn); note;
2141 note = XEXP (note, 1))
2143 switch (REG_NOTE_KIND (note))
2145 case REG_EQUIV:
2146 case REG_EQUAL:
2147 df_uses_record (DF_REF_REGULAR, &collection_rec,
2148 &XEXP (note, 0), DF_REF_REG_USE,
2149 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
2150 default:
2151 break;
2155 /* Find some place to put any new mw_hardregs. */
2156 df_canonize_collection_rec (&collection_rec);
2157 if (collection_rec.next_mw)
2159 unsigned int count = 0;
2160 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2161 while (*mw_rec)
2163 count++;
2164 mw_rec++;
2167 if (count)
2169 /* Append to the end of the existing record after
2170 expanding it if necessary. */
2171 if (collection_rec.next_mw > num_deleted)
2173 insn_info->mw_hardregs =
2174 XRESIZEVEC (struct df_mw_hardreg *,
2175 insn_info->mw_hardregs,
2176 count + 1 + collection_rec.next_mw);
2178 memcpy (&insn_info->mw_hardregs[count], collection_rec.mw_vec,
2179 (collection_rec.next_mw + 1) * sizeof (struct df_mw_hardreg *));
2180 qsort (insn_info->mw_hardregs, count + collection_rec.next_mw,
2181 sizeof (struct df_mw_hardreg *), df_mw_compare);
2183 else
2185 /* No vector there. */
2186 insn_info->mw_hardregs
2187 = XNEWVEC (struct df_mw_hardreg*,
2188 count + 1 + collection_rec.next_mw);
2189 memcpy (insn_info->mw_hardregs, collection_rec.mw_vec,
2190 (collection_rec.next_mw + 1) * sizeof (struct df_mw_hardreg *));
2193 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2194 ignore it. */
2195 collection_rec.mw_vec = NULL;
2196 collection_rec.next_mw = 0;
2197 df_refs_add_to_chains (&collection_rec, bb, insn);
2199 else
2200 df_insn_rescan (insn);
2205 /*----------------------------------------------------------------------------
2206 Hard core instruction scanning code. No external interfaces here,
2207 just a lot of routines that look inside insns.
2208 ----------------------------------------------------------------------------*/
2211 /* Return true if the contents of two df_ref's are identical.
2212 It ignores DF_REF_MARKER. */
2214 static bool
2215 df_ref_equal_p (df_ref ref1, df_ref ref2)
2217 if (!ref2)
2218 return false;
2220 if (ref1 == ref2)
2221 return true;
2223 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2224 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2225 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2226 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2227 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2228 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2229 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2230 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2231 return false;
2233 switch (DF_REF_CLASS (ref1))
2235 case DF_REF_ARTIFICIAL:
2236 case DF_REF_BASE:
2237 return true;
2239 case DF_REF_EXTRACT:
2240 if ((DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2241 || (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2242 || (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2)))
2243 return false;
2244 /* fallthru. */
2246 case DF_REF_REGULAR:
2247 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2249 default:
2250 gcc_unreachable ();
2252 return false;
2256 /* Compare REF1 and REF2 for sorting. This is only called from places
2257 where all of the refs are of the same type, in the same insn, and
2258 have the same bb. So these fields are not checked. */
2260 static int
2261 df_ref_compare (const void *r1, const void *r2)
2263 const df_ref ref1 = *(const df_ref *)r1;
2264 const df_ref ref2 = *(const df_ref *)r2;
2266 if (ref1 == ref2)
2267 return 0;
2269 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2270 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2272 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2273 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2275 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2276 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2278 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2279 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2281 /* Cannot look at the LOC field on artificial refs. */
2282 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2283 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2284 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2286 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2288 /* If two refs are identical except that one of them has is from
2289 a mw and one is not, we need to have the one with the mw
2290 first. */
2291 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2292 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2293 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2294 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2295 return -1;
2296 else
2297 return 1;
2300 /* The classes are the same at this point so it is safe to only look
2301 at ref1. */
2302 if (DF_REF_CLASS (ref1) == DF_REF_EXTRACT)
2304 if (DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2305 return DF_REF_EXTRACT_OFFSET (ref1) - DF_REF_EXTRACT_OFFSET (ref2);
2306 if (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2307 return DF_REF_EXTRACT_WIDTH (ref1) - DF_REF_EXTRACT_WIDTH (ref2);
2308 if (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2))
2309 return DF_REF_EXTRACT_MODE (ref1) - DF_REF_EXTRACT_MODE (ref2);
2311 return 0;
2314 static void
2315 df_swap_refs (df_ref *ref_vec, int i, int j)
2317 df_ref tmp = ref_vec[i];
2318 ref_vec[i] = ref_vec[j];
2319 ref_vec[j] = tmp;
2322 /* Sort and compress a set of refs. */
2324 static unsigned int
2325 df_sort_and_compress_refs (df_ref *ref_vec, unsigned int count)
2327 unsigned int i;
2328 unsigned int dist = 0;
2330 ref_vec[count] = NULL;
2331 /* If there are 1 or 0 elements, there is nothing to do. */
2332 if (count < 2)
2333 return count;
2334 else if (count == 2)
2336 if (df_ref_compare (&ref_vec[0], &ref_vec[1]) > 0)
2337 df_swap_refs (ref_vec, 0, 1);
2339 else
2341 for (i = 0; i < count - 1; i++)
2342 if (df_ref_compare (&ref_vec[i], &ref_vec[i+1]) >= 0)
2343 break;
2344 /* If the array is already strictly ordered,
2345 which is the most common case for large COUNT case
2346 (which happens for CALL INSNs),
2347 no need to sort and filter out duplicate.
2348 Simply return the count.
2349 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2350 of DF_REF_COMPARE. */
2351 if (i == count - 1)
2352 return count;
2353 qsort (ref_vec, count, sizeof (df_ref), df_ref_compare);
2356 for (i=0; i<count-dist; i++)
2358 /* Find the next ref that is not equal to the current ref. */
2359 while (df_ref_equal_p (ref_vec[i], ref_vec[i + dist + 1]))
2361 df_free_ref (ref_vec[i + dist + 1]);
2362 dist++;
2364 /* Copy it down to the next position. */
2365 if (dist)
2366 ref_vec[i+1] = ref_vec[i + dist + 1];
2369 count -= dist;
2370 ref_vec[count] = NULL;
2371 return count;
2375 /* Return true if the contents of two df_ref's are identical.
2376 It ignores DF_REF_MARKER. */
2378 static bool
2379 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2381 if (!mw2)
2382 return false;
2383 return (mw1 == mw2) ||
2384 (mw1->mw_reg == mw2->mw_reg
2385 && mw1->type == mw2->type
2386 && mw1->flags == mw2->flags
2387 && mw1->start_regno == mw2->start_regno
2388 && mw1->end_regno == mw2->end_regno);
2392 /* Compare MW1 and MW2 for sorting. */
2394 static int
2395 df_mw_compare (const void *m1, const void *m2)
2397 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2398 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2400 if (mw1 == mw2)
2401 return 0;
2403 if (mw1->type != mw2->type)
2404 return mw1->type - mw2->type;
2406 if (mw1->flags != mw2->flags)
2407 return mw1->flags - mw2->flags;
2409 if (mw1->start_regno != mw2->start_regno)
2410 return mw1->start_regno - mw2->start_regno;
2412 if (mw1->end_regno != mw2->end_regno)
2413 return mw1->end_regno - mw2->end_regno;
2415 if (mw1->mw_reg != mw2->mw_reg)
2416 return mw1->mw_order - mw2->mw_order;
2418 return 0;
2422 /* Sort and compress a set of refs. */
2424 static unsigned int
2425 df_sort_and_compress_mws (struct df_mw_hardreg **mw_vec, unsigned int count)
2427 struct df_scan_problem_data *problem_data
2428 = (struct df_scan_problem_data *) df_scan->problem_data;
2429 unsigned int i;
2430 unsigned int dist = 0;
2431 mw_vec[count] = NULL;
2433 if (count < 2)
2434 return count;
2435 else if (count == 2)
2437 if (df_mw_compare (&mw_vec[0], &mw_vec[1]) > 0)
2439 struct df_mw_hardreg *tmp = mw_vec[0];
2440 mw_vec[0] = mw_vec[1];
2441 mw_vec[1] = tmp;
2444 else
2445 qsort (mw_vec, count, sizeof (struct df_mw_hardreg *), df_mw_compare);
2447 for (i=0; i<count-dist; i++)
2449 /* Find the next ref that is not equal to the current ref. */
2450 while (df_mw_equal_p (mw_vec[i], mw_vec[i + dist + 1]))
2452 pool_free (problem_data->mw_reg_pool, mw_vec[i + dist + 1]);
2453 dist++;
2455 /* Copy it down to the next position. */
2456 if (dist)
2457 mw_vec[i+1] = mw_vec[i + dist + 1];
2460 count -= dist;
2461 mw_vec[count] = NULL;
2462 return count;
2466 /* Sort and remove duplicates from the COLLECTION_REC. */
2468 static void
2469 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2471 if (collection_rec->def_vec)
2472 collection_rec->next_def
2473 = df_sort_and_compress_refs (collection_rec->def_vec,
2474 collection_rec->next_def);
2475 if (collection_rec->use_vec)
2476 collection_rec->next_use
2477 = df_sort_and_compress_refs (collection_rec->use_vec,
2478 collection_rec->next_use);
2479 if (collection_rec->eq_use_vec)
2480 collection_rec->next_eq_use
2481 = df_sort_and_compress_refs (collection_rec->eq_use_vec,
2482 collection_rec->next_eq_use);
2483 if (collection_rec->mw_vec)
2484 collection_rec->next_mw
2485 = df_sort_and_compress_mws (collection_rec->mw_vec,
2486 collection_rec->next_mw);
2490 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2492 static void
2493 df_install_ref (df_ref this_ref,
2494 struct df_reg_info *reg_info,
2495 struct df_ref_info *ref_info,
2496 bool add_to_table)
2498 unsigned int regno = DF_REF_REGNO (this_ref);
2499 /* Add the ref to the reg_{def,use,eq_use} chain. */
2500 df_ref head = reg_info->reg_chain;
2502 reg_info->reg_chain = this_ref;
2503 reg_info->n_refs++;
2505 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2507 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2508 df->hard_regs_live_count[regno]++;
2511 gcc_assert (DF_REF_NEXT_REG (this_ref) == NULL);
2512 gcc_assert (DF_REF_PREV_REG (this_ref) == NULL);
2514 DF_REF_NEXT_REG (this_ref) = head;
2516 /* We cannot actually link to the head of the chain. */
2517 DF_REF_PREV_REG (this_ref) = NULL;
2519 if (head)
2520 DF_REF_PREV_REG (head) = this_ref;
2522 if (add_to_table)
2524 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2525 df_check_and_grow_ref_info (ref_info, 1);
2526 DF_REF_ID (this_ref) = ref_info->table_size;
2527 /* Add the ref to the big array of defs. */
2528 ref_info->refs[ref_info->table_size] = this_ref;
2529 ref_info->table_size++;
2531 else
2532 DF_REF_ID (this_ref) = -1;
2534 ref_info->total_size++;
2538 /* This function takes one of the groups of refs (defs, uses or
2539 eq_uses) and installs the entire group into the insn. It also adds
2540 each of these refs into the appropriate chains. */
2542 static df_ref *
2543 df_install_refs (basic_block bb,
2544 df_ref *old_vec, unsigned int count,
2545 struct df_reg_info **reg_info,
2546 struct df_ref_info *ref_info,
2547 bool is_notes)
2549 if (count)
2551 unsigned int i;
2552 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2553 bool add_to_table;
2555 switch (ref_info->ref_order)
2557 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2558 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2559 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2560 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2561 add_to_table = true;
2562 break;
2563 case DF_REF_ORDER_UNORDERED:
2564 case DF_REF_ORDER_BY_REG:
2565 case DF_REF_ORDER_BY_INSN:
2566 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2567 add_to_table = !is_notes;
2568 break;
2569 default:
2570 add_to_table = false;
2571 break;
2574 /* Do not add if ref is not in the right blocks. */
2575 if (add_to_table && df->analyze_subset)
2576 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2578 for (i = 0; i < count; i++)
2580 df_ref this_ref = old_vec[i];
2581 new_vec[i] = this_ref;
2582 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2583 ref_info, add_to_table);
2586 new_vec[count] = NULL;
2587 return new_vec;
2589 else
2590 return df_null_ref_rec;
2594 /* This function takes the mws installs the entire group into the
2595 insn. */
2597 static struct df_mw_hardreg **
2598 df_install_mws (struct df_mw_hardreg **old_vec, unsigned int count)
2600 if (count)
2602 struct df_mw_hardreg **new_vec
2603 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2604 memcpy (new_vec, old_vec,
2605 sizeof (struct df_mw_hardreg*) * (count + 1));
2606 return new_vec;
2608 else
2609 return df_null_mw_rec;
2613 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2614 chains and update other necessary information. */
2616 static void
2617 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2618 basic_block bb, rtx insn)
2620 if (insn)
2622 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2623 /* If there is a vector in the collection rec, add it to the
2624 insn. A null rec is a signal that the caller will handle the
2625 chain specially. */
2626 if (collection_rec->def_vec)
2628 df_scan_free_ref_vec (insn_rec->defs);
2629 insn_rec->defs
2630 = df_install_refs (bb, collection_rec->def_vec,
2631 collection_rec->next_def,
2632 df->def_regs,
2633 &df->def_info, false);
2635 if (collection_rec->use_vec)
2637 df_scan_free_ref_vec (insn_rec->uses);
2638 insn_rec->uses
2639 = df_install_refs (bb, collection_rec->use_vec,
2640 collection_rec->next_use,
2641 df->use_regs,
2642 &df->use_info, false);
2644 if (collection_rec->eq_use_vec)
2646 df_scan_free_ref_vec (insn_rec->eq_uses);
2647 insn_rec->eq_uses
2648 = df_install_refs (bb, collection_rec->eq_use_vec,
2649 collection_rec->next_eq_use,
2650 df->eq_use_regs,
2651 &df->use_info, true);
2653 if (collection_rec->mw_vec)
2655 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2656 insn_rec->mw_hardregs
2657 = df_install_mws (collection_rec->mw_vec,
2658 collection_rec->next_mw);
2661 else
2663 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2665 df_scan_free_ref_vec (bb_info->artificial_defs);
2666 bb_info->artificial_defs
2667 = df_install_refs (bb, collection_rec->def_vec,
2668 collection_rec->next_def,
2669 df->def_regs,
2670 &df->def_info, false);
2671 df_scan_free_ref_vec (bb_info->artificial_uses);
2672 bb_info->artificial_uses
2673 = df_install_refs (bb, collection_rec->use_vec,
2674 collection_rec->next_use,
2675 df->use_regs,
2676 &df->use_info, false);
2681 /* Allocate a ref and initialize its fields.
2683 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2684 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the fields
2685 if they were constants. Otherwise they should be -1 if those flags
2686 were set. */
2688 static df_ref
2689 df_ref_create_structure (enum df_ref_class cl,
2690 struct df_collection_rec *collection_rec,
2691 rtx reg, rtx *loc,
2692 basic_block bb, struct df_insn_info *info,
2693 enum df_ref_type ref_type,
2694 int ref_flags,
2695 int width, int offset, enum machine_mode mode)
2697 df_ref this_ref = NULL;
2698 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2699 struct df_scan_problem_data *problem_data
2700 = (struct df_scan_problem_data *) df_scan->problem_data;
2702 switch (cl)
2704 case DF_REF_BASE:
2705 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2706 gcc_assert (loc == NULL);
2707 break;
2709 case DF_REF_ARTIFICIAL:
2710 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2711 this_ref->artificial_ref.bb = bb;
2712 gcc_assert (loc == NULL);
2713 break;
2715 case DF_REF_REGULAR:
2716 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2717 this_ref->regular_ref.loc = loc;
2718 gcc_assert (loc);
2719 break;
2721 case DF_REF_EXTRACT:
2722 this_ref = (df_ref) pool_alloc (problem_data->ref_extract_pool);
2723 DF_REF_EXTRACT_WIDTH (this_ref) = width;
2724 DF_REF_EXTRACT_OFFSET (this_ref) = offset;
2725 DF_REF_EXTRACT_MODE (this_ref) = mode;
2726 this_ref->regular_ref.loc = loc;
2727 gcc_assert (loc);
2728 break;
2731 DF_REF_CLASS (this_ref) = cl;
2732 DF_REF_ID (this_ref) = -1;
2733 DF_REF_REG (this_ref) = reg;
2734 DF_REF_REGNO (this_ref) = regno;
2735 DF_REF_TYPE (this_ref) = ref_type;
2736 DF_REF_INSN_INFO (this_ref) = info;
2737 DF_REF_CHAIN (this_ref) = NULL;
2738 DF_REF_FLAGS (this_ref) = ref_flags;
2739 DF_REF_NEXT_REG (this_ref) = NULL;
2740 DF_REF_PREV_REG (this_ref) = NULL;
2741 DF_REF_ORDER (this_ref) = df->ref_order++;
2743 /* We need to clear this bit because fwprop, and in the future
2744 possibly other optimizations sometimes create new refs using ond
2745 refs as the model. */
2746 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2748 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2749 if ((regno < FIRST_PSEUDO_REGISTER)
2750 && (!DF_REF_IS_ARTIFICIAL (this_ref)))
2752 if (DF_REF_REG_DEF_P (this_ref))
2754 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2755 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2757 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2758 && (regno == FRAME_POINTER_REGNUM
2759 || regno == ARG_POINTER_REGNUM)))
2760 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2763 if (collection_rec)
2765 if (DF_REF_REG_DEF_P (this_ref))
2766 collection_rec->def_vec[collection_rec->next_def++] = this_ref;
2767 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2768 collection_rec->eq_use_vec[collection_rec->next_eq_use++] = this_ref;
2769 else
2770 collection_rec->use_vec[collection_rec->next_use++] = this_ref;
2773 return this_ref;
2777 /* Create new references of type DF_REF_TYPE for each part of register REG
2778 at address LOC within INSN of BB.
2780 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2781 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
2782 fields if they were constants. Otherwise they should be -1 if
2783 those flags were set. */
2786 static void
2787 df_ref_record (enum df_ref_class cl,
2788 struct df_collection_rec *collection_rec,
2789 rtx reg, rtx *loc,
2790 basic_block bb, struct df_insn_info *insn_info,
2791 enum df_ref_type ref_type,
2792 int ref_flags,
2793 int width, int offset, enum machine_mode mode)
2795 unsigned int regno;
2797 gcc_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2799 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2800 if (regno < FIRST_PSEUDO_REGISTER)
2802 struct df_mw_hardreg *hardreg = NULL;
2803 struct df_scan_problem_data *problem_data
2804 = (struct df_scan_problem_data *) df_scan->problem_data;
2805 unsigned int i;
2806 unsigned int endregno;
2807 df_ref ref;
2809 if (GET_CODE (reg) == SUBREG)
2811 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2812 SUBREG_BYTE (reg), GET_MODE (reg));
2813 endregno = regno + subreg_nregs (reg);
2815 else
2816 endregno = END_HARD_REGNO (reg);
2818 /* If this is a multiword hardreg, we create some extra
2819 datastructures that will enable us to easily build REG_DEAD
2820 and REG_UNUSED notes. */
2821 if ((endregno != regno + 1) && insn_info)
2823 /* Sets to a subreg of a multiword register are partial.
2824 Sets to a non-subreg of a multiword register are not. */
2825 if (GET_CODE (reg) == SUBREG)
2826 ref_flags |= DF_REF_PARTIAL;
2827 ref_flags |= DF_REF_MW_HARDREG;
2829 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2830 hardreg->type = ref_type;
2831 hardreg->flags = ref_flags;
2832 hardreg->mw_reg = reg;
2833 hardreg->start_regno = regno;
2834 hardreg->end_regno = endregno - 1;
2835 hardreg->mw_order = df->ref_order++;
2836 collection_rec->mw_vec[collection_rec->next_mw++] = hardreg;
2839 for (i = regno; i < endregno; i++)
2841 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2842 bb, insn_info, ref_type, ref_flags,
2843 width, offset, mode);
2845 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2848 else
2850 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2851 ref_type, ref_flags, width, offset, mode);
2856 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2857 covered by the outer mode is smaller than that covered by the inner mode,
2858 is a read-modify-write operation.
2859 This function returns true iff the SUBREG X is such a SUBREG. */
2861 bool
2862 df_read_modify_subreg_p (rtx x)
2864 unsigned int isize, osize;
2865 if (GET_CODE (x) != SUBREG)
2866 return false;
2867 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2868 osize = GET_MODE_SIZE (GET_MODE (x));
2869 return isize > osize
2870 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2874 /* Process all the registers defined in the rtx, X.
2875 Autoincrement/decrement definitions will be picked up by
2876 df_uses_record. */
2878 static void
2879 df_def_record_1 (struct df_collection_rec *collection_rec,
2880 rtx x, basic_block bb, struct df_insn_info *insn_info,
2881 int flags)
2883 rtx *loc;
2884 rtx dst;
2885 int offset = -1;
2886 int width = -1;
2887 enum machine_mode mode = 0;
2888 enum df_ref_class cl = DF_REF_REGULAR;
2890 /* We may recursively call ourselves on EXPR_LIST when dealing with PARALLEL
2891 construct. */
2892 if (GET_CODE (x) == EXPR_LIST || GET_CODE (x) == CLOBBER)
2893 loc = &XEXP (x, 0);
2894 else
2895 loc = &SET_DEST (x);
2896 dst = *loc;
2898 /* It is legal to have a set destination be a parallel. */
2899 if (GET_CODE (dst) == PARALLEL)
2901 int i;
2903 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2905 rtx temp = XVECEXP (dst, 0, i);
2906 if (GET_CODE (temp) == EXPR_LIST || GET_CODE (temp) == CLOBBER
2907 || GET_CODE (temp) == SET)
2908 df_def_record_1 (collection_rec,
2909 temp, bb, insn_info,
2910 GET_CODE (temp) == CLOBBER
2911 ? flags | DF_REF_MUST_CLOBBER : flags);
2913 return;
2916 if (GET_CODE (dst) == STRICT_LOW_PART)
2918 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2920 loc = &XEXP (dst, 0);
2921 dst = *loc;
2924 if (GET_CODE (dst) == ZERO_EXTRACT)
2926 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
2928 if (GET_CODE (XEXP (dst, 1)) == CONST_INT
2929 && GET_CODE (XEXP (dst, 2)) == CONST_INT)
2931 width = INTVAL (XEXP (dst, 1));
2932 offset = INTVAL (XEXP (dst, 2));
2933 mode = GET_MODE (dst);
2934 cl = DF_REF_EXTRACT;
2937 loc = &XEXP (dst, 0);
2938 dst = *loc;
2941 /* At this point if we do not have a reg or a subreg, just return. */
2942 if (REG_P (dst))
2944 df_ref_record (cl, collection_rec,
2945 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
2946 width, offset, mode);
2948 /* We want to keep sp alive everywhere - by making all
2949 writes to sp also use of sp. */
2950 if (REGNO (dst) == STACK_POINTER_REGNUM)
2951 df_ref_record (DF_REF_BASE, collection_rec,
2952 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags,
2953 width, offset, mode);
2955 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
2957 if (df_read_modify_subreg_p (dst))
2958 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
2960 flags |= DF_REF_SUBREG;
2962 df_ref_record (cl, collection_rec,
2963 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
2964 width, offset, mode);
2969 /* Process all the registers defined in the pattern rtx, X. */
2971 static void
2972 df_defs_record (struct df_collection_rec *collection_rec,
2973 rtx x, basic_block bb, struct df_insn_info *insn_info,
2974 int flags)
2976 RTX_CODE code = GET_CODE (x);
2978 if (code == SET || code == CLOBBER)
2980 /* Mark the single def within the pattern. */
2981 enum df_ref_flags clobber_flags = flags;
2982 clobber_flags |= (code == CLOBBER) ? DF_REF_MUST_CLOBBER : 0;
2983 df_def_record_1 (collection_rec, x, bb, insn_info, clobber_flags);
2985 else if (code == COND_EXEC)
2987 df_defs_record (collection_rec, COND_EXEC_CODE (x),
2988 bb, insn_info, DF_REF_CONDITIONAL);
2990 else if (code == PARALLEL)
2992 int i;
2994 /* Mark the multiple defs within the pattern. */
2995 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2996 df_defs_record (collection_rec, XVECEXP (x, 0, i), bb, insn_info, flags);
3001 /* Process all the registers used in the rtx at address LOC.
3003 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
3004 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
3005 fields if they were constants. Otherwise they should be -1 if
3006 those flags were set. */
3008 static void
3009 df_uses_record (enum df_ref_class cl, struct df_collection_rec *collection_rec,
3010 rtx *loc, enum df_ref_type ref_type,
3011 basic_block bb, struct df_insn_info *insn_info,
3012 int flags,
3013 int width, int offset, enum machine_mode mode)
3015 RTX_CODE code;
3016 rtx x;
3018 retry:
3019 x = *loc;
3020 if (!x)
3021 return;
3022 code = GET_CODE (x);
3023 switch (code)
3025 case LABEL_REF:
3026 case SYMBOL_REF:
3027 case CONST_INT:
3028 case CONST:
3029 case CONST_DOUBLE:
3030 case CONST_FIXED:
3031 case CONST_VECTOR:
3032 case PC:
3033 case CC0:
3034 case ADDR_VEC:
3035 case ADDR_DIFF_VEC:
3036 return;
3038 case CLOBBER:
3039 /* If we are clobbering a MEM, mark any registers inside the address
3040 as being used. */
3041 if (MEM_P (XEXP (x, 0)))
3042 df_uses_record (cl, collection_rec,
3043 &XEXP (XEXP (x, 0), 0),
3044 DF_REF_REG_MEM_STORE,
3045 bb, insn_info,
3046 flags, width, offset, mode);
3048 /* If we're clobbering a REG then we have a def so ignore. */
3049 return;
3051 case MEM:
3052 df_uses_record (cl, collection_rec,
3053 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3054 bb, insn_info, flags & DF_REF_IN_NOTE,
3055 width, offset, mode);
3056 return;
3058 case SUBREG:
3059 /* While we're here, optimize this case. */
3060 flags |= DF_REF_PARTIAL;
3061 /* In case the SUBREG is not of a REG, do not optimize. */
3062 if (!REG_P (SUBREG_REG (x)))
3064 loc = &SUBREG_REG (x);
3065 df_uses_record (cl, collection_rec, loc, ref_type, bb, insn_info, flags,
3066 width, offset, mode);
3067 return;
3069 /* ... Fall through ... */
3071 case REG:
3072 df_ref_record (cl, collection_rec,
3073 x, loc, bb, insn_info,
3074 ref_type, flags,
3075 width, offset, mode);
3076 return;
3078 case SIGN_EXTRACT:
3079 case ZERO_EXTRACT:
3081 /* If the parameters to the zero or sign extract are
3082 constants, strip them off and recurse, otherwise there is
3083 no information that we can gain from this operation. */
3084 if (GET_CODE (XEXP (x, 1)) == CONST_INT
3085 && GET_CODE (XEXP (x, 2)) == CONST_INT)
3087 width = INTVAL (XEXP (x, 1));
3088 offset = INTVAL (XEXP (x, 2));
3089 mode = GET_MODE (x);
3091 if (code == ZERO_EXTRACT)
3092 flags |= DF_REF_ZERO_EXTRACT;
3093 else
3094 flags |= DF_REF_SIGN_EXTRACT;
3096 df_uses_record (DF_REF_EXTRACT, collection_rec,
3097 &XEXP (x, 0), ref_type, bb, insn_info, flags,
3098 width, offset, mode);
3099 return;
3102 break;
3104 case SET:
3106 rtx dst = SET_DEST (x);
3107 gcc_assert (!(flags & DF_REF_IN_NOTE));
3108 df_uses_record (cl, collection_rec,
3109 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags,
3110 width, offset, mode);
3112 switch (GET_CODE (dst))
3114 case SUBREG:
3115 if (df_read_modify_subreg_p (dst))
3117 df_uses_record (cl, collection_rec, &SUBREG_REG (dst),
3118 DF_REF_REG_USE, bb, insn_info,
3119 flags | DF_REF_READ_WRITE | DF_REF_SUBREG,
3120 width, offset, mode);
3121 break;
3123 /* Fall through. */
3124 case REG:
3125 case PARALLEL:
3126 case SCRATCH:
3127 case PC:
3128 case CC0:
3129 break;
3130 case MEM:
3131 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3132 DF_REF_REG_MEM_STORE, bb, insn_info, flags,
3133 width, offset, mode);
3134 break;
3135 case STRICT_LOW_PART:
3137 rtx *temp = &XEXP (dst, 0);
3138 /* A strict_low_part uses the whole REG and not just the
3139 SUBREG. */
3140 dst = XEXP (dst, 0);
3141 df_uses_record (cl, collection_rec,
3142 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3143 DF_REF_REG_USE, bb, insn_info,
3144 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART,
3145 width, offset, mode);
3147 break;
3148 case ZERO_EXTRACT:
3150 if (GET_CODE (XEXP (dst, 1)) == CONST_INT
3151 && GET_CODE (XEXP (dst, 2)) == CONST_INT)
3153 width = INTVAL (XEXP (dst, 1));
3154 offset = INTVAL (XEXP (dst, 2));
3155 mode = GET_MODE (dst);
3156 df_uses_record (DF_REF_EXTRACT, collection_rec, &XEXP (dst, 0),
3157 DF_REF_REG_USE, bb, insn_info,
3158 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3159 width, offset, mode);
3161 else
3163 df_uses_record (cl, collection_rec, &XEXP (dst, 1),
3164 DF_REF_REG_USE, bb, insn_info, flags,
3165 width, offset, mode);
3166 df_uses_record (cl, collection_rec, &XEXP (dst, 2),
3167 DF_REF_REG_USE, bb, insn_info, flags,
3168 width, offset, mode);
3169 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3170 DF_REF_REG_USE, bb, insn_info,
3171 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3172 width, offset, mode);
3176 break;
3178 default:
3179 gcc_unreachable ();
3181 return;
3184 case RETURN:
3185 break;
3187 case ASM_OPERANDS:
3188 case UNSPEC_VOLATILE:
3189 case TRAP_IF:
3190 case ASM_INPUT:
3192 /* Traditional and volatile asm instructions must be
3193 considered to use and clobber all hard registers, all
3194 pseudo-registers and all of memory. So must TRAP_IF and
3195 UNSPEC_VOLATILE operations.
3197 Consider for instance a volatile asm that changes the fpu
3198 rounding mode. An insn should not be moved across this
3199 even if it only uses pseudo-regs because it might give an
3200 incorrectly rounded result.
3202 However, flow.c's liveness computation did *not* do this,
3203 giving the reasoning as " ?!? Unfortunately, marking all
3204 hard registers as live causes massive problems for the
3205 register allocator and marking all pseudos as live creates
3206 mountains of uninitialized variable warnings."
3208 In order to maintain the status quo with regard to liveness
3209 and uses, we do what flow.c did and just mark any regs we
3210 can find in ASM_OPERANDS as used. In global asm insns are
3211 scanned and regs_asm_clobbered is filled out.
3213 For all ASM_OPERANDS, we must traverse the vector of input
3214 operands. We can not just fall through here since then we
3215 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3216 which do not indicate traditional asms unlike their normal
3217 usage. */
3218 if (code == ASM_OPERANDS)
3220 int j;
3222 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3223 df_uses_record (cl, collection_rec, &ASM_OPERANDS_INPUT (x, j),
3224 DF_REF_REG_USE, bb, insn_info, flags,
3225 width, offset, mode);
3226 return;
3228 break;
3231 case PRE_DEC:
3232 case POST_DEC:
3233 case PRE_INC:
3234 case POST_INC:
3235 case PRE_MODIFY:
3236 case POST_MODIFY:
3237 /* Catch the def of the register being modified. */
3238 df_ref_record (cl, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3239 bb, insn_info,
3240 DF_REF_REG_DEF,
3241 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY,
3242 width, offset, mode);
3244 /* ... Fall through to handle uses ... */
3246 default:
3247 break;
3250 /* Recursively scan the operands of this expression. */
3252 const char *fmt = GET_RTX_FORMAT (code);
3253 int i;
3255 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3257 if (fmt[i] == 'e')
3259 /* Tail recursive case: save a function call level. */
3260 if (i == 0)
3262 loc = &XEXP (x, 0);
3263 goto retry;
3265 df_uses_record (cl, collection_rec, &XEXP (x, i), ref_type,
3266 bb, insn_info, flags,
3267 width, offset, mode);
3269 else if (fmt[i] == 'E')
3271 int j;
3272 for (j = 0; j < XVECLEN (x, i); j++)
3273 df_uses_record (cl, collection_rec,
3274 &XVECEXP (x, i, j), ref_type,
3275 bb, insn_info, flags,
3276 width, offset, mode);
3281 return;
3285 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3287 static void
3288 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3290 unsigned int i;
3291 for (i = 0; i < collection_rec->next_def; i++)
3293 df_ref ref = collection_rec->def_vec[i];
3294 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3296 int width = -1;
3297 int offset = -1;
3298 enum machine_mode mode = 0;
3299 df_ref use;
3301 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT))
3303 width = DF_REF_EXTRACT_WIDTH (ref);
3304 offset = DF_REF_EXTRACT_OFFSET (ref);
3305 mode = DF_REF_EXTRACT_MODE (ref);
3308 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3309 DF_REF_LOC (ref), DF_REF_BB (ref),
3310 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3311 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL,
3312 width, offset, mode);
3313 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3319 /* Get call's extra defs and uses. */
3321 static void
3322 df_get_call_refs (struct df_collection_rec * collection_rec,
3323 basic_block bb,
3324 struct df_insn_info *insn_info,
3325 int flags)
3327 rtx note;
3328 bitmap_iterator bi;
3329 unsigned int ui;
3330 bool is_sibling_call;
3331 unsigned int i;
3332 bitmap defs_generated = BITMAP_ALLOC (&df_bitmap_obstack);
3334 /* Do not generate clobbers for registers that are the result of the
3335 call. This causes ordering problems in the chain building code
3336 depending on which def is seen first. */
3337 for (i=0; i<collection_rec->next_def; i++)
3339 df_ref def = collection_rec->def_vec[i];
3340 bitmap_set_bit (defs_generated, DF_REF_REGNO (def));
3343 /* Record the registers used to pass arguments, and explicitly
3344 noted as clobbered. */
3345 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3346 note = XEXP (note, 1))
3348 if (GET_CODE (XEXP (note, 0)) == USE)
3349 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (XEXP (note, 0), 0),
3350 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3351 VOIDmode);
3352 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3354 if (REG_P (XEXP (XEXP (note, 0), 0)))
3356 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3357 if (!bitmap_bit_p (defs_generated, regno))
3358 df_defs_record (collection_rec, XEXP (note, 0), bb,
3359 insn_info, flags);
3361 else
3362 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (note, 0),
3363 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3364 VOIDmode);
3368 /* The stack ptr is used (honorarily) by a CALL insn. */
3369 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[STACK_POINTER_REGNUM],
3370 NULL, bb, insn_info, DF_REF_REG_USE,
3371 DF_REF_CALL_STACK_USAGE | flags,
3372 -1, -1, VOIDmode);
3374 /* Calls may also reference any of the global registers,
3375 so they are recorded as used. */
3376 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3377 if (global_regs[i])
3379 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3380 NULL, bb, insn_info, DF_REF_REG_USE, flags, -1, -1,
3381 VOIDmode);
3382 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3383 NULL, bb, insn_info, DF_REF_REG_DEF, flags, -1, -1,
3384 VOIDmode);
3387 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3388 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, ui, bi)
3390 if (!global_regs[ui]
3391 && (!bitmap_bit_p (defs_generated, ui))
3392 && (!is_sibling_call
3393 || !bitmap_bit_p (df->exit_block_uses, ui)
3394 || refers_to_regno_p (ui, ui+1,
3395 crtl->return_rtx, NULL)))
3396 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[ui],
3397 NULL, bb, insn_info, DF_REF_REG_DEF,
3398 DF_REF_MAY_CLOBBER | flags,
3399 -1, -1, VOIDmode);
3402 BITMAP_FREE (defs_generated);
3403 return;
3406 /* Collect all refs in the INSN. This function is free of any
3407 side-effect - it will create and return a lists of df_ref's in the
3408 COLLECTION_REC without putting those refs into existing ref chains
3409 and reg chains. */
3411 static void
3412 df_insn_refs_collect (struct df_collection_rec* collection_rec,
3413 basic_block bb, struct df_insn_info *insn_info)
3415 rtx note;
3416 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3418 /* Clear out the collection record. */
3419 collection_rec->next_def = 0;
3420 collection_rec->next_use = 0;
3421 collection_rec->next_eq_use = 0;
3422 collection_rec->next_mw = 0;
3424 /* Record register defs. */
3425 df_defs_record (collection_rec, PATTERN (insn_info->insn), bb, insn_info, 0);
3427 /* Process REG_EQUIV/REG_EQUAL notes. */
3428 for (note = REG_NOTES (insn_info->insn); note;
3429 note = XEXP (note, 1))
3431 switch (REG_NOTE_KIND (note))
3433 case REG_EQUIV:
3434 case REG_EQUAL:
3435 df_uses_record (DF_REF_REGULAR, collection_rec,
3436 &XEXP (note, 0), DF_REF_REG_USE,
3437 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
3438 break;
3439 case REG_NON_LOCAL_GOTO:
3440 /* The frame ptr is used by a non-local goto. */
3441 df_ref_record (DF_REF_BASE, collection_rec,
3442 regno_reg_rtx[FRAME_POINTER_REGNUM],
3443 NULL, bb, insn_info,
3444 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3445 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3446 df_ref_record (DF_REF_BASE, collection_rec,
3447 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3448 NULL, bb, insn_info,
3449 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3450 #endif
3451 break;
3452 default:
3453 break;
3457 if (CALL_P (insn_info->insn))
3458 df_get_call_refs (collection_rec, bb, insn_info,
3459 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3461 /* Record the register uses. */
3462 df_uses_record (DF_REF_REGULAR, collection_rec,
3463 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0,
3464 -1, -1, VOIDmode);
3466 /* DF_REF_CONDITIONAL needs corresponding USES. */
3467 if (is_cond_exec)
3468 df_get_conditional_uses (collection_rec);
3470 df_canonize_collection_rec (collection_rec);
3473 /* Recompute the luids for the insns in BB. */
3475 void
3476 df_recompute_luids (basic_block bb)
3478 rtx insn;
3479 int luid = 0;
3481 df_grow_insn_info ();
3483 /* Scan the block an insn at a time from beginning to end. */
3484 FOR_BB_INSNS (bb, insn)
3486 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3487 /* Inserting labels does not always trigger the incremental
3488 rescanning. */
3489 if (!insn_info)
3491 gcc_assert (!INSN_P (insn));
3492 insn_info = df_insn_create_insn_record (insn);
3495 DF_INSN_INFO_LUID (insn_info) = luid;
3496 if (INSN_P (insn))
3497 luid++;
3502 /* Returns true if the function entry needs to
3503 define the static chain register. */
3505 static bool
3506 df_need_static_chain_reg (struct function *fun)
3508 tree fun_context = decl_function_context (fun->decl);
3509 return fun_context
3510 && DECL_NO_STATIC_CHAIN (fun_context) == false;
3514 /* Collect all artificial refs at the block level for BB and add them
3515 to COLLECTION_REC. */
3517 static void
3518 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3520 collection_rec->next_def = 0;
3521 collection_rec->next_use = 0;
3522 collection_rec->next_eq_use = 0;
3523 collection_rec->next_mw = 0;
3525 if (bb->index == ENTRY_BLOCK)
3527 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3528 return;
3530 else if (bb->index == EXIT_BLOCK)
3532 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3533 return;
3536 #ifdef EH_RETURN_DATA_REGNO
3537 if (bb_has_eh_pred (bb))
3539 unsigned int i;
3540 /* Mark the registers that will contain data for the handler. */
3541 for (i = 0; ; ++i)
3543 unsigned regno = EH_RETURN_DATA_REGNO (i);
3544 if (regno == INVALID_REGNUM)
3545 break;
3546 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3547 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1,
3548 VOIDmode);
3551 #endif
3553 /* Add the hard_frame_pointer if this block is the target of a
3554 non-local goto. */
3555 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3556 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3557 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1, VOIDmode);
3559 /* Add the artificial uses. */
3560 if (bb->index >= NUM_FIXED_BLOCKS)
3562 bitmap_iterator bi;
3563 unsigned int regno;
3564 bitmap au = bb_has_eh_pred (bb)
3565 ? df->eh_block_artificial_uses
3566 : df->regular_block_artificial_uses;
3568 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3570 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3571 bb, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3575 df_canonize_collection_rec (collection_rec);
3579 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3581 void
3582 df_bb_refs_record (int bb_index, bool scan_insns)
3584 basic_block bb = BASIC_BLOCK (bb_index);
3585 rtx insn;
3586 int luid = 0;
3587 struct df_scan_bb_info *bb_info;
3588 struct df_collection_rec collection_rec;
3589 collection_rec.def_vec = XALLOCAVEC (df_ref, 1000);
3590 collection_rec.use_vec = XALLOCAVEC (df_ref, 1000);
3591 collection_rec.eq_use_vec = XALLOCAVEC (df_ref, 1000);
3592 collection_rec.mw_vec = XALLOCAVEC (struct df_mw_hardreg *, 100);
3594 if (!df)
3595 return;
3597 bb_info = df_scan_get_bb_info (bb_index);
3599 /* Need to make sure that there is a record in the basic block info. */
3600 if (!bb_info)
3602 bb_info = (struct df_scan_bb_info *) pool_alloc (df_scan->block_pool);
3603 df_scan_set_bb_info (bb_index, bb_info);
3604 bb_info->artificial_defs = NULL;
3605 bb_info->artificial_uses = NULL;
3608 if (scan_insns)
3609 /* Scan the block an insn at a time from beginning to end. */
3610 FOR_BB_INSNS (bb, insn)
3612 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3613 gcc_assert (!insn_info);
3615 insn_info = df_insn_create_insn_record (insn);
3616 if (INSN_P (insn))
3618 /* Record refs within INSN. */
3619 DF_INSN_INFO_LUID (insn_info) = luid++;
3620 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3621 df_refs_add_to_chains (&collection_rec, bb, insn);
3623 DF_INSN_INFO_LUID (insn_info) = luid;
3626 /* Other block level artificial refs */
3627 df_bb_refs_collect (&collection_rec, bb);
3628 df_refs_add_to_chains (&collection_rec, bb, NULL);
3630 /* Now that the block has been processed, set the block as dirty so
3631 LR and LIVE will get it processed. */
3632 df_set_bb_dirty (bb);
3636 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3637 block. */
3639 static void
3640 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3642 #ifdef EH_USES
3643 unsigned int i;
3644 #endif
3646 bitmap_clear (regular_block_artificial_uses);
3648 if (reload_completed)
3650 if (frame_pointer_needed)
3651 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3653 else
3654 /* Before reload, there are a few registers that must be forced
3655 live everywhere -- which might not already be the case for
3656 blocks within infinite loops. */
3658 /* Any reference to any pseudo before reload is a potential
3659 reference of the frame pointer. */
3660 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3662 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3663 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3664 #endif
3666 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3667 /* Pseudos with argument area equivalences may require
3668 reloading via the argument pointer. */
3669 if (fixed_regs[ARG_POINTER_REGNUM])
3670 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3671 #endif
3673 /* Any constant, or pseudo with constant equivalences, may
3674 require reloading from memory using the pic register. */
3675 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3676 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3677 bitmap_set_bit (regular_block_artificial_uses, PIC_OFFSET_TABLE_REGNUM);
3679 /* The all-important stack pointer must always be live. */
3680 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3682 #ifdef EH_USES
3683 /* EH_USES registers are used:
3684 1) at all insns that might throw (calls or with -fnon-call-exceptions
3685 trapping insns)
3686 2) in all EH edges
3687 3) to support backtraces and/or debugging, anywhere between their
3688 initialization and where they the saved registers are restored
3689 from them, including the cases where we don't reach the epilogue
3690 (noreturn call or infinite loop). */
3691 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3692 if (EH_USES (i))
3693 bitmap_set_bit (regular_block_artificial_uses, i);
3694 #endif
3698 /* Get the artificial use set for an eh block. */
3700 static void
3701 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3703 bitmap_clear (eh_block_artificial_uses);
3705 /* The following code (down thru the arg_pointer setting APPEARS
3706 to be necessary because there is nothing that actually
3707 describes what the exception handling code may actually need
3708 to keep alive. */
3709 if (reload_completed)
3711 if (frame_pointer_needed)
3713 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3714 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3715 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3716 #endif
3718 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3719 if (fixed_regs[ARG_POINTER_REGNUM])
3720 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3721 #endif
3727 /*----------------------------------------------------------------------------
3728 Specialized hard register scanning functions.
3729 ----------------------------------------------------------------------------*/
3732 /* Mark a register in SET. Hard registers in large modes get all
3733 of their component registers set as well. */
3735 static void
3736 df_mark_reg (rtx reg, void *vset)
3738 bitmap set = (bitmap) vset;
3739 int regno = REGNO (reg);
3741 gcc_assert (GET_MODE (reg) != BLKmode);
3743 bitmap_set_bit (set, regno);
3744 if (regno < FIRST_PSEUDO_REGISTER)
3746 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3747 while (--n > 0)
3748 bitmap_set_bit (set, regno + n);
3753 /* Set the bit for regs that are considered being defined at the entry. */
3755 static void
3756 df_get_entry_block_def_set (bitmap entry_block_defs)
3758 rtx r;
3759 int i;
3761 bitmap_clear (entry_block_defs);
3763 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3765 if (FUNCTION_ARG_REGNO_P (i))
3766 #ifdef INCOMING_REGNO
3767 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3768 #else
3769 bitmap_set_bit (entry_block_defs, i);
3770 #endif
3773 /* The always important stack pointer. */
3774 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3776 /* Once the prologue has been generated, all of these registers
3777 should just show up in the first regular block. */
3778 if (HAVE_prologue && epilogue_completed)
3780 /* Defs for the callee saved registers are inserted so that the
3781 pushes have some defining location. */
3782 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3783 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3784 bitmap_set_bit (entry_block_defs, i);
3786 else
3788 /* If STATIC_CHAIN_INCOMING_REGNUM == STATIC_CHAIN_REGNUM
3789 only STATIC_CHAIN_REGNUM is defined. If they are different,
3790 we only care about the STATIC_CHAIN_INCOMING_REGNUM. */
3791 #ifdef STATIC_CHAIN_INCOMING_REGNUM
3792 bitmap_set_bit (entry_block_defs, STATIC_CHAIN_INCOMING_REGNUM);
3793 #else
3794 #ifdef STATIC_CHAIN_REGNUM
3795 bitmap_set_bit (entry_block_defs, STATIC_CHAIN_REGNUM);
3796 #endif
3797 #endif
3800 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3801 if (r && REG_P (r))
3802 bitmap_set_bit (entry_block_defs, REGNO (r));
3804 if ((!reload_completed) || frame_pointer_needed)
3806 /* Any reference to any pseudo before reload is a potential
3807 reference of the frame pointer. */
3808 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3809 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3810 /* If they are different, also mark the hard frame pointer as live. */
3811 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3812 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3813 #endif
3816 /* These registers are live everywhere. */
3817 if (!reload_completed)
3819 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3820 /* Pseudos with argument area equivalences may require
3821 reloading via the argument pointer. */
3822 if (fixed_regs[ARG_POINTER_REGNUM])
3823 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3824 #endif
3826 #ifdef PIC_OFFSET_TABLE_REGNUM
3827 /* Any constant, or pseudo with constant equivalences, may
3828 require reloading from memory using the pic register. */
3829 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3830 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3831 bitmap_set_bit (entry_block_defs, PIC_OFFSET_TABLE_REGNUM);
3832 #endif
3835 #ifdef INCOMING_RETURN_ADDR_RTX
3836 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3837 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3838 #endif
3840 targetm.live_on_entry (entry_block_defs);
3842 /* If the function has an incoming STATIC_CHAIN,
3843 it has to show up in the entry def set. */
3844 if (df_need_static_chain_reg (cfun))
3846 #ifdef STATIC_CHAIN_INCOMING_REGNUM
3847 bitmap_set_bit (entry_block_defs, STATIC_CHAIN_INCOMING_REGNUM);
3848 #else
3849 #ifdef STATIC_CHAIN_REGNUM
3850 bitmap_set_bit (entry_block_defs, STATIC_CHAIN_REGNUM);
3851 #endif
3852 #endif
3857 /* Return the (conservative) set of hard registers that are defined on
3858 entry to the function.
3859 It uses df->entry_block_defs to determine which register
3860 reference to include. */
3862 static void
3863 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3864 bitmap entry_block_defs)
3866 unsigned int i;
3867 bitmap_iterator bi;
3869 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3871 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3872 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0, -1, -1,
3873 VOIDmode);
3876 df_canonize_collection_rec (collection_rec);
3880 /* Record the (conservative) set of hard registers that are defined on
3881 entry to the function. */
3883 static void
3884 df_record_entry_block_defs (bitmap entry_block_defs)
3886 struct df_collection_rec collection_rec;
3887 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3888 collection_rec.def_vec = XALLOCAVEC (df_ref, FIRST_PSEUDO_REGISTER);
3890 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3892 /* Process bb_refs chain */
3893 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3897 /* Update the defs in the entry block. */
3899 void
3900 df_update_entry_block_defs (void)
3902 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
3903 bool changed = false;
3905 df_get_entry_block_def_set (refs);
3906 if (df->entry_block_defs)
3908 if (!bitmap_equal_p (df->entry_block_defs, refs))
3910 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3911 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3912 df_ref_chain_delete (bb_info->artificial_defs);
3913 bb_info->artificial_defs = NULL;
3914 changed = true;
3917 else
3919 struct df_scan_problem_data *problem_data
3920 = (struct df_scan_problem_data *) df_scan->problem_data;
3921 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3922 changed = true;
3925 if (changed)
3927 df_record_entry_block_defs (refs);
3928 bitmap_copy (df->entry_block_defs, refs);
3929 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
3931 BITMAP_FREE (refs);
3935 /* Set the bit for regs that are considered being used at the exit. */
3937 static void
3938 df_get_exit_block_use_set (bitmap exit_block_uses)
3940 unsigned int i;
3942 bitmap_clear (exit_block_uses);
3944 /* Stack pointer is always live at the exit. */
3945 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
3947 /* Mark the frame pointer if needed at the end of the function.
3948 If we end up eliminating it, it will be removed from the live
3949 list of each basic block by reload. */
3951 if ((!reload_completed) || frame_pointer_needed)
3953 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
3954 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3955 /* If they are different, also mark the hard frame pointer as live. */
3956 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3957 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
3958 #endif
3961 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
3962 /* Many architectures have a GP register even without flag_pic.
3963 Assume the pic register is not in use, or will be handled by
3964 other means, if it is not fixed. */
3965 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3966 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3967 bitmap_set_bit (exit_block_uses, PIC_OFFSET_TABLE_REGNUM);
3968 #endif
3970 /* Mark all global registers, and all registers used by the
3971 epilogue as being live at the end of the function since they
3972 may be referenced by our caller. */
3973 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3974 if (global_regs[i] || EPILOGUE_USES (i))
3975 bitmap_set_bit (exit_block_uses, i);
3977 if (HAVE_epilogue && epilogue_completed)
3979 /* Mark all call-saved registers that we actually used. */
3980 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3981 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
3982 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
3983 bitmap_set_bit (exit_block_uses, i);
3986 #ifdef EH_RETURN_DATA_REGNO
3987 /* Mark the registers that will contain data for the handler. */
3988 if (reload_completed && crtl->calls_eh_return)
3989 for (i = 0; ; ++i)
3991 unsigned regno = EH_RETURN_DATA_REGNO (i);
3992 if (regno == INVALID_REGNUM)
3993 break;
3994 bitmap_set_bit (exit_block_uses, regno);
3996 #endif
3998 #ifdef EH_RETURN_STACKADJ_RTX
3999 if ((!HAVE_epilogue || ! epilogue_completed)
4000 && crtl->calls_eh_return)
4002 rtx tmp = EH_RETURN_STACKADJ_RTX;
4003 if (tmp && REG_P (tmp))
4004 df_mark_reg (tmp, exit_block_uses);
4006 #endif
4008 #ifdef EH_RETURN_HANDLER_RTX
4009 if ((!HAVE_epilogue || ! epilogue_completed)
4010 && crtl->calls_eh_return)
4012 rtx tmp = EH_RETURN_HANDLER_RTX;
4013 if (tmp && REG_P (tmp))
4014 df_mark_reg (tmp, exit_block_uses);
4016 #endif
4018 /* Mark function return value. */
4019 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4023 /* Return the refs of hard registers that are used in the exit block.
4024 It uses df->exit_block_uses to determine register to include. */
4026 static void
4027 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4029 unsigned int i;
4030 bitmap_iterator bi;
4032 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4033 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4034 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4036 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4037 /* It is deliberate that this is not put in the exit block uses but
4038 I do not know why. */
4039 if (reload_completed
4040 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4041 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4042 && fixed_regs[ARG_POINTER_REGNUM])
4043 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4044 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4045 #endif
4047 df_canonize_collection_rec (collection_rec);
4051 /* Record the set of hard registers that are used in the exit block.
4052 It uses df->exit_block_uses to determine which bit to include. */
4054 static void
4055 df_record_exit_block_uses (bitmap exit_block_uses)
4057 struct df_collection_rec collection_rec;
4058 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4059 collection_rec.use_vec = XALLOCAVEC (df_ref, FIRST_PSEUDO_REGISTER);
4061 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4063 /* Process bb_refs chain */
4064 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4068 /* Update the uses in the exit block. */
4070 void
4071 df_update_exit_block_uses (void)
4073 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
4074 bool changed = false;
4076 df_get_exit_block_use_set (refs);
4077 if (df->exit_block_uses)
4079 if (!bitmap_equal_p (df->exit_block_uses, refs))
4081 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4082 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4083 df_ref_chain_delete (bb_info->artificial_uses);
4084 bb_info->artificial_uses = NULL;
4085 changed = true;
4088 else
4090 struct df_scan_problem_data *problem_data
4091 = (struct df_scan_problem_data *) df_scan->problem_data;
4092 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4093 changed = true;
4096 if (changed)
4098 df_record_exit_block_uses (refs);
4099 bitmap_copy (df->exit_block_uses, refs);
4100 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4102 BITMAP_FREE (refs);
4105 static bool initialized = false;
4108 /* Initialize some platform specific structures. */
4110 void
4111 df_hard_reg_init (void)
4113 #ifdef ELIMINABLE_REGS
4114 int i;
4115 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4116 #endif
4117 if (initialized)
4118 return;
4120 /* Record which registers will be eliminated. We use this in
4121 mark_used_regs. */
4122 CLEAR_HARD_REG_SET (elim_reg_set);
4124 #ifdef ELIMINABLE_REGS
4125 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4126 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4127 #else
4128 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4129 #endif
4131 initialized = true;
4135 /* Recompute the parts of scanning that are based on regs_ever_live
4136 because something changed in that array. */
4138 void
4139 df_update_entry_exit_and_calls (void)
4141 basic_block bb;
4143 df_update_entry_block_defs ();
4144 df_update_exit_block_uses ();
4146 /* The call insns need to be rescanned because there may be changes
4147 in the set of registers clobbered across the call. */
4148 FOR_EACH_BB (bb)
4150 rtx insn;
4151 FOR_BB_INSNS (bb, insn)
4153 if (INSN_P (insn) && CALL_P (insn))
4154 df_insn_rescan (insn);
4160 /* Return true if hard REG is actually used in the some instruction.
4161 There are a fair number of conditions that affect the setting of
4162 this array. See the comment in df.h for df->hard_regs_live_count
4163 for the conditions that this array is set. */
4165 bool
4166 df_hard_reg_used_p (unsigned int reg)
4168 gcc_assert (df);
4169 return df->hard_regs_live_count[reg] != 0;
4173 /* A count of the number of times REG is actually used in the some
4174 instruction. There are a fair number of conditions that affect the
4175 setting of this array. See the comment in df.h for
4176 df->hard_regs_live_count for the conditions that this array is
4177 set. */
4180 unsigned int
4181 df_hard_reg_used_count (unsigned int reg)
4183 gcc_assert (df);
4184 return df->hard_regs_live_count[reg];
4188 /* Get the value of regs_ever_live[REGNO]. */
4190 bool
4191 df_regs_ever_live_p (unsigned int regno)
4193 return regs_ever_live[regno];
4197 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4198 to change, schedule that change for the next update. */
4200 void
4201 df_set_regs_ever_live (unsigned int regno, bool value)
4203 if (regs_ever_live[regno] == value)
4204 return;
4206 regs_ever_live[regno] = value;
4207 if (df)
4208 df->redo_entry_and_exit = true;
4212 /* Compute "regs_ever_live" information from the underlying df
4213 information. Set the vector to all false if RESET. */
4215 void
4216 df_compute_regs_ever_live (bool reset)
4218 unsigned int i;
4219 bool changed = df->redo_entry_and_exit;
4221 if (reset)
4222 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4224 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4225 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4227 regs_ever_live[i] = true;
4228 changed = true;
4230 if (changed)
4231 df_update_entry_exit_and_calls ();
4232 df->redo_entry_and_exit = false;
4236 /*----------------------------------------------------------------------------
4237 Dataflow ref information verification functions.
4239 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4240 df_reg_chain_verify_unmarked (refs)
4241 df_refs_verify (ref*, ref*, bool)
4242 df_mws_verify (mw*, mw*, bool)
4243 df_insn_refs_verify (collection_rec, bb, insn, bool)
4244 df_bb_refs_verify (bb, refs, bool)
4245 df_bb_verify (bb)
4246 df_exit_block_bitmap_verify (bool)
4247 df_entry_block_bitmap_verify (bool)
4248 df_scan_verify ()
4249 ----------------------------------------------------------------------------*/
4252 /* Mark all refs in the reg chain. Verify that all of the registers
4253 are in the correct chain. */
4255 static unsigned int
4256 df_reg_chain_mark (df_ref refs, unsigned int regno,
4257 bool is_def, bool is_eq_use)
4259 unsigned int count = 0;
4260 df_ref ref;
4261 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4263 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4265 /* If there are no def-use or use-def chains, make sure that all
4266 of the chains are clear. */
4267 if (!df_chain)
4268 gcc_assert (!DF_REF_CHAIN (ref));
4270 /* Check to make sure the ref is in the correct chain. */
4271 gcc_assert (DF_REF_REGNO (ref) == regno);
4272 if (is_def)
4273 gcc_assert (DF_REF_REG_DEF_P (ref));
4274 else
4275 gcc_assert (!DF_REF_REG_DEF_P (ref));
4277 if (is_eq_use)
4278 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4279 else
4280 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4282 if (DF_REF_NEXT_REG (ref))
4283 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4284 count++;
4285 DF_REF_REG_MARK (ref);
4287 return count;
4291 /* Verify that all of the registers in the chain are unmarked. */
4293 static void
4294 df_reg_chain_verify_unmarked (df_ref refs)
4296 df_ref ref;
4297 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4298 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4302 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4304 static bool
4305 df_refs_verify (df_ref *new_rec, df_ref *old_rec,
4306 bool abort_if_fail)
4308 while ((*new_rec) && (*old_rec))
4310 if (!df_ref_equal_p (*new_rec, *old_rec))
4312 if (abort_if_fail)
4313 gcc_assert (0);
4314 else
4315 return false;
4318 /* Abort if fail is called from the function level verifier. If
4319 that is the context, mark this reg as being seem. */
4320 if (abort_if_fail)
4322 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4323 DF_REF_REG_UNMARK (*old_rec);
4326 new_rec++;
4327 old_rec++;
4330 if (abort_if_fail)
4331 gcc_assert ((*new_rec == NULL) && (*old_rec == NULL));
4332 else
4333 return ((*new_rec == NULL) && (*old_rec == NULL));
4334 return false;
4338 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4340 static bool
4341 df_mws_verify (struct df_mw_hardreg **new_rec, struct df_mw_hardreg **old_rec,
4342 bool abort_if_fail)
4344 while ((*new_rec) && (*old_rec))
4346 if (!df_mw_equal_p (*new_rec, *old_rec))
4348 if (abort_if_fail)
4349 gcc_assert (0);
4350 else
4351 return false;
4353 new_rec++;
4354 old_rec++;
4357 if (abort_if_fail)
4358 gcc_assert ((*new_rec == NULL) && (*old_rec == NULL));
4359 else
4360 return ((*new_rec == NULL) && (*old_rec == NULL));
4361 return false;
4365 /* Return true if the existing insn refs information is complete and
4366 correct. Otherwise (i.e. if there's any missing or extra refs),
4367 return the correct df_ref chain in REFS_RETURN.
4369 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4370 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4371 verification mode instead of the whole function, so unmark
4372 everything.
4374 If ABORT_IF_FAIL is set, this function never returns false. */
4376 static bool
4377 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4378 basic_block bb,
4379 rtx insn,
4380 bool abort_if_fail)
4382 bool ret1, ret2, ret3, ret4;
4383 unsigned int uid = INSN_UID (insn);
4384 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4386 df_insn_refs_collect (collection_rec, bb, insn_info);
4388 if (!DF_INSN_UID_DEFS (uid))
4390 /* The insn_rec was created but it was never filled out. */
4391 if (abort_if_fail)
4392 gcc_assert (0);
4393 else
4394 return false;
4397 /* Unfortunately we cannot opt out early if one of these is not
4398 right because the marks will not get cleared. */
4399 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4400 abort_if_fail);
4401 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4402 abort_if_fail);
4403 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4404 abort_if_fail);
4405 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4406 abort_if_fail);
4407 return (ret1 && ret2 && ret3 && ret4);
4411 /* Return true if all refs in the basic block are correct and complete.
4412 Due to df_ref_chain_verify, it will cause all refs
4413 that are verified to have DF_REF_MARK bit set. */
4415 static bool
4416 df_bb_verify (basic_block bb)
4418 rtx insn;
4419 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4420 struct df_collection_rec collection_rec;
4422 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4423 collection_rec.def_vec = XALLOCAVEC (df_ref, 1000);
4424 collection_rec.use_vec = XALLOCAVEC (df_ref, 1000);
4425 collection_rec.eq_use_vec = XALLOCAVEC (df_ref, 1000);
4426 collection_rec.mw_vec = XALLOCAVEC (struct df_mw_hardreg *, 100);
4428 gcc_assert (bb_info);
4430 /* Scan the block, one insn at a time, from beginning to end. */
4431 FOR_BB_INSNS_REVERSE (bb, insn)
4433 if (!INSN_P (insn))
4434 continue;
4435 df_insn_refs_verify (&collection_rec, bb, insn, true);
4436 df_free_collection_rec (&collection_rec);
4439 /* Do the artificial defs and uses. */
4440 df_bb_refs_collect (&collection_rec, bb);
4441 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4442 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4443 df_free_collection_rec (&collection_rec);
4445 return true;
4449 /* Returns true if the entry block has correct and complete df_ref set.
4450 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4452 static bool
4453 df_entry_block_bitmap_verify (bool abort_if_fail)
4455 bitmap entry_block_defs = BITMAP_ALLOC (&df_bitmap_obstack);
4456 bool is_eq;
4458 df_get_entry_block_def_set (entry_block_defs);
4460 is_eq = bitmap_equal_p (entry_block_defs, df->entry_block_defs);
4462 if (!is_eq && abort_if_fail)
4464 print_current_pass (stderr);
4465 fprintf (stderr, "entry_block_defs = ");
4466 df_print_regset (stderr, entry_block_defs);
4467 fprintf (stderr, "df->entry_block_defs = ");
4468 df_print_regset (stderr, df->entry_block_defs);
4469 gcc_assert (0);
4472 BITMAP_FREE (entry_block_defs);
4474 return is_eq;
4478 /* Returns true if the exit block has correct and complete df_ref set.
4479 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4481 static bool
4482 df_exit_block_bitmap_verify (bool abort_if_fail)
4484 bitmap exit_block_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4485 bool is_eq;
4487 df_get_exit_block_use_set (exit_block_uses);
4489 is_eq = bitmap_equal_p (exit_block_uses, df->exit_block_uses);
4491 if (!is_eq && abort_if_fail)
4493 print_current_pass (stderr);
4494 fprintf (stderr, "exit_block_uses = ");
4495 df_print_regset (stderr, exit_block_uses);
4496 fprintf (stderr, "df->exit_block_uses = ");
4497 df_print_regset (stderr, df->exit_block_uses);
4498 gcc_assert (0);
4501 BITMAP_FREE (exit_block_uses);
4503 return is_eq;
4507 /* Return true if df_ref information for all insns in all blocks are
4508 correct and complete. */
4510 void
4511 df_scan_verify (void)
4513 unsigned int i;
4514 basic_block bb;
4515 bitmap regular_block_artificial_uses;
4516 bitmap eh_block_artificial_uses;
4518 if (!df)
4519 return;
4521 /* Verification is a 4 step process. */
4523 /* (1) All of the refs are marked by going thru the reg chains. */
4524 for (i = 0; i < DF_REG_SIZE (df); i++)
4526 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4527 == DF_REG_DEF_COUNT(i));
4528 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4529 == DF_REG_USE_COUNT(i));
4530 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4531 == DF_REG_EQ_USE_COUNT(i));
4534 /* (2) There are various bitmaps whose value may change over the
4535 course of the compilation. This step recomputes them to make
4536 sure that they have not slipped out of date. */
4537 regular_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4538 eh_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4540 df_get_regular_block_artificial_uses (regular_block_artificial_uses);
4541 df_get_eh_block_artificial_uses (eh_block_artificial_uses);
4543 bitmap_ior_into (eh_block_artificial_uses,
4544 regular_block_artificial_uses);
4546 /* Check artificial_uses bitmaps didn't change. */
4547 gcc_assert (bitmap_equal_p (regular_block_artificial_uses,
4548 df->regular_block_artificial_uses));
4549 gcc_assert (bitmap_equal_p (eh_block_artificial_uses,
4550 df->eh_block_artificial_uses));
4552 BITMAP_FREE (regular_block_artificial_uses);
4553 BITMAP_FREE (eh_block_artificial_uses);
4555 /* Verify entry block and exit block. These only verify the bitmaps,
4556 the refs are verified in df_bb_verify. */
4557 df_entry_block_bitmap_verify (true);
4558 df_exit_block_bitmap_verify (true);
4560 /* (3) All of the insns in all of the blocks are traversed and the
4561 marks are cleared both in the artificial refs attached to the
4562 blocks and the real refs inside the insns. It is a failure to
4563 clear a mark that has not been set as this means that the ref in
4564 the block or insn was not in the reg chain. */
4566 FOR_ALL_BB (bb)
4567 df_bb_verify (bb);
4569 /* (4) See if all reg chains are traversed a second time. This time
4570 a check is made that the marks are clear. A set mark would be a
4571 from a reg that is not in any insn or basic block. */
4573 for (i = 0; i < DF_REG_SIZE (df); i++)
4575 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4576 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4577 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));