Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / df-scan.c
blobbf285a1928785207a0c1d160cb2bcfeff1efcd26
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 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"
48 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
50 DEF_VEC_P(df_ref);
51 DEF_VEC_ALLOC_P_STACK(df_ref);
53 #define VEC_df_ref_stack_alloc(alloc) VEC_stack_alloc (df_ref, alloc)
55 typedef struct df_mw_hardreg *df_mw_hardreg_ptr;
57 DEF_VEC_P(df_mw_hardreg_ptr);
58 DEF_VEC_ALLOC_P_STACK(df_mw_hardreg_ptr);
60 #define VEC_df_mw_hardreg_ptr_stack_alloc(alloc) \
61 VEC_stack_alloc (df_mw_hardreg_ptr, alloc)
63 #ifndef HAVE_epilogue
64 #define HAVE_epilogue 0
65 #endif
66 #ifndef HAVE_prologue
67 #define HAVE_prologue 0
68 #endif
69 #ifndef HAVE_sibcall_epilogue
70 #define HAVE_sibcall_epilogue 0
71 #endif
73 #ifndef EPILOGUE_USES
74 #define EPILOGUE_USES(REGNO) 0
75 #endif
77 /* The following two macros free the vecs that hold either the refs or
78 the mw refs. They are a little tricky because the vec has 0
79 elements is special and is not to be freed. */
80 #define df_scan_free_ref_vec(V) \
81 do { \
82 if (V && *V) \
83 free (V); \
84 } while (0)
86 #define df_scan_free_mws_vec(V) \
87 do { \
88 if (V && *V) \
89 free (V); \
90 } while (0)
92 /* The set of hard registers in eliminables[i].from. */
94 static HARD_REG_SET elim_reg_set;
96 /* Initialize ur_in and ur_out as if all hard registers were partially
97 available. */
99 struct df_collection_rec
101 VEC(df_ref,stack) *def_vec;
102 VEC(df_ref,stack) *use_vec;
103 VEC(df_ref,stack) *eq_use_vec;
104 VEC(df_mw_hardreg_ptr,stack) *mw_vec;
107 static df_ref df_null_ref_rec[1];
108 static struct df_mw_hardreg * df_null_mw_rec[1];
110 static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
111 rtx, rtx *,
112 basic_block, struct df_insn_info *,
113 enum df_ref_type, int ref_flags,
114 int, int, enum machine_mode);
115 static void df_def_record_1 (struct df_collection_rec *, rtx,
116 basic_block, struct df_insn_info *,
117 int ref_flags);
118 static void df_defs_record (struct df_collection_rec *, rtx,
119 basic_block, struct df_insn_info *,
120 int ref_flags);
121 static void df_uses_record (enum df_ref_class, struct df_collection_rec *,
122 rtx *, enum df_ref_type,
123 basic_block, struct df_insn_info *,
124 int ref_flags,
125 int, int, enum machine_mode);
127 static df_ref df_ref_create_structure (enum df_ref_class,
128 struct df_collection_rec *, rtx, rtx *,
129 basic_block, struct df_insn_info *,
130 enum df_ref_type, int ref_flags,
131 int, int, enum machine_mode);
133 static void df_insn_refs_collect (struct df_collection_rec*,
134 basic_block, struct df_insn_info *);
135 static void df_canonize_collection_rec (struct df_collection_rec *);
137 static void df_get_regular_block_artificial_uses (bitmap);
138 static void df_get_eh_block_artificial_uses (bitmap);
140 static void df_record_entry_block_defs (bitmap);
141 static void df_record_exit_block_uses (bitmap);
142 static void df_get_exit_block_use_set (bitmap);
143 static void df_get_entry_block_def_set (bitmap);
144 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
145 static void df_ref_chain_delete_du_chain (df_ref *);
146 static void df_ref_chain_delete (df_ref *);
148 static void df_refs_add_to_chains (struct df_collection_rec *,
149 basic_block, rtx);
151 static bool df_insn_refs_verify (struct df_collection_rec *, basic_block, rtx, bool);
152 static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
153 static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
154 static void df_install_ref (df_ref, struct df_reg_info *,
155 struct df_ref_info *, bool);
157 static int df_ref_compare (const void *, const void *);
158 static int df_mw_compare (const void *, const void *);
160 /* Indexed by hardware reg number, is true if that register is ever
161 used in the current function.
163 In df-scan.c, this is set up to record the hard regs used
164 explicitly. Reload adds in the hard regs used for holding pseudo
165 regs. Final uses it to generate the code in the function prologue
166 and epilogue to save and restore registers as needed. */
168 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
170 /*----------------------------------------------------------------------------
171 SCANNING DATAFLOW PROBLEM
173 There are several ways in which scanning looks just like the other
174 dataflow problems. It shares the all the mechanisms for local info
175 as well as basic block info. Where it differs is when and how often
176 it gets run. It also has no need for the iterative solver.
177 ----------------------------------------------------------------------------*/
179 /* Problem data for the scanning dataflow function. */
180 struct df_scan_problem_data
182 alloc_pool ref_base_pool;
183 alloc_pool ref_artificial_pool;
184 alloc_pool ref_regular_pool;
185 alloc_pool ref_extract_pool;
186 alloc_pool insn_pool;
187 alloc_pool reg_pool;
188 alloc_pool mw_reg_pool;
189 bitmap_obstack reg_bitmaps;
190 bitmap_obstack insn_bitmaps;
193 typedef struct df_scan_bb_info *df_scan_bb_info_t;
196 /* Internal function to shut down the scanning problem. */
197 static void
198 df_scan_free_internal (void)
200 struct df_scan_problem_data *problem_data
201 = (struct df_scan_problem_data *) df_scan->problem_data;
202 unsigned int i;
203 basic_block bb;
205 /* The vectors that hold the refs are not pool allocated because
206 they come in many sizes. This makes them impossible to delete
207 all at once. */
208 for (i = 0; i < DF_INSN_SIZE(); i++)
210 struct df_insn_info *insn_info = DF_INSN_UID_GET(i);
211 /* Skip the insns that have no insn_info or have been
212 deleted. */
213 if (insn_info)
215 df_scan_free_ref_vec (insn_info->defs);
216 df_scan_free_ref_vec (insn_info->uses);
217 df_scan_free_ref_vec (insn_info->eq_uses);
218 df_scan_free_mws_vec (insn_info->mw_hardregs);
222 FOR_ALL_BB (bb)
224 unsigned int bb_index = bb->index;
225 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
226 if (bb_info)
228 df_scan_free_ref_vec (bb_info->artificial_defs);
229 df_scan_free_ref_vec (bb_info->artificial_uses);
233 free (df->def_info.refs);
234 free (df->def_info.begin);
235 free (df->def_info.count);
236 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
238 free (df->use_info.refs);
239 free (df->use_info.begin);
240 free (df->use_info.count);
241 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
243 free (df->def_regs);
244 df->def_regs = NULL;
245 free (df->use_regs);
246 df->use_regs = NULL;
247 free (df->eq_use_regs);
248 df->eq_use_regs = NULL;
249 df->regs_size = 0;
250 DF_REG_SIZE(df) = 0;
252 free (df->insns);
253 df->insns = NULL;
254 DF_INSN_SIZE () = 0;
256 free (df_scan->block_info);
257 df_scan->block_info = NULL;
258 df_scan->block_info_size = 0;
260 BITMAP_FREE (df->hardware_regs_used);
261 BITMAP_FREE (df->regular_block_artificial_uses);
262 BITMAP_FREE (df->eh_block_artificial_uses);
263 BITMAP_FREE (df->entry_block_defs);
264 BITMAP_FREE (df->exit_block_uses);
265 BITMAP_FREE (df->insns_to_delete);
266 BITMAP_FREE (df->insns_to_rescan);
267 BITMAP_FREE (df->insns_to_notes_rescan);
269 free_alloc_pool (df_scan->block_pool);
270 free_alloc_pool (problem_data->ref_base_pool);
271 free_alloc_pool (problem_data->ref_artificial_pool);
272 free_alloc_pool (problem_data->ref_regular_pool);
273 free_alloc_pool (problem_data->ref_extract_pool);
274 free_alloc_pool (problem_data->insn_pool);
275 free_alloc_pool (problem_data->reg_pool);
276 free_alloc_pool (problem_data->mw_reg_pool);
277 bitmap_obstack_release (&problem_data->reg_bitmaps);
278 bitmap_obstack_release (&problem_data->insn_bitmaps);
279 free (df_scan->problem_data);
283 /* Set basic block info. */
285 static void
286 df_scan_set_bb_info (unsigned int index,
287 struct df_scan_bb_info *bb_info)
289 df_grow_bb_info (df_scan);
290 df_scan->block_info[index] = (void *) bb_info;
294 /* Free basic block info. */
296 static void
297 df_scan_free_bb_info (basic_block bb, void *vbb_info)
299 struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
300 unsigned int bb_index = bb->index;
301 if (bb_info)
303 rtx insn;
304 FOR_BB_INSNS (bb, insn)
306 if (INSN_P (insn))
307 /* Record defs within INSN. */
308 df_insn_delete (bb, INSN_UID (insn));
311 if (bb_index < df_scan->block_info_size)
312 bb_info = df_scan_get_bb_info (bb_index);
314 /* Get rid of any artificial uses or defs. */
315 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
316 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
317 df_ref_chain_delete (bb_info->artificial_defs);
318 df_ref_chain_delete (bb_info->artificial_uses);
319 bb_info->artificial_defs = NULL;
320 bb_info->artificial_uses = NULL;
321 pool_free (df_scan->block_pool, bb_info);
326 /* Allocate the problem data for the scanning problem. This should be
327 called when the problem is created or when the entire function is to
328 be rescanned. */
329 void
330 df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
332 struct df_scan_problem_data *problem_data;
333 unsigned int insn_num = get_max_uid () + 1;
334 unsigned int block_size = 400;
335 basic_block bb;
337 /* Given the number of pools, this is really faster than tearing
338 everything apart. */
339 if (df_scan->problem_data)
340 df_scan_free_internal ();
342 df_scan->block_pool
343 = create_alloc_pool ("df_scan_block pool",
344 sizeof (struct df_scan_bb_info),
345 block_size);
347 problem_data = XNEW (struct df_scan_problem_data);
348 df_scan->problem_data = problem_data;
349 df_scan->computed = true;
351 problem_data->ref_base_pool
352 = create_alloc_pool ("df_scan ref base",
353 sizeof (struct df_base_ref), block_size);
354 problem_data->ref_artificial_pool
355 = create_alloc_pool ("df_scan ref artificial",
356 sizeof (struct df_artificial_ref), block_size);
357 problem_data->ref_regular_pool
358 = create_alloc_pool ("df_scan ref regular",
359 sizeof (struct df_regular_ref), block_size);
360 problem_data->ref_extract_pool
361 = create_alloc_pool ("df_scan ref extract",
362 sizeof (struct df_extract_ref), block_size);
363 problem_data->insn_pool
364 = create_alloc_pool ("df_scan insn",
365 sizeof (struct df_insn_info), block_size);
366 problem_data->reg_pool
367 = create_alloc_pool ("df_scan reg",
368 sizeof (struct df_reg_info), block_size);
369 problem_data->mw_reg_pool
370 = create_alloc_pool ("df_scan mw_reg",
371 sizeof (struct df_mw_hardreg), block_size);
373 bitmap_obstack_initialize (&problem_data->reg_bitmaps);
374 bitmap_obstack_initialize (&problem_data->insn_bitmaps);
376 insn_num += insn_num / 4;
377 df_grow_reg_info ();
379 df_grow_insn_info ();
380 df_grow_bb_info (df_scan);
382 FOR_ALL_BB (bb)
384 unsigned int bb_index = bb->index;
385 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
386 if (!bb_info)
388 bb_info = (struct df_scan_bb_info *) pool_alloc (df_scan->block_pool);
389 df_scan_set_bb_info (bb_index, bb_info);
391 bb_info->artificial_defs = NULL;
392 bb_info->artificial_uses = NULL;
395 df->hardware_regs_used = BITMAP_ALLOC (&problem_data->reg_bitmaps);
396 df->regular_block_artificial_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
397 df->eh_block_artificial_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
398 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
399 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
400 df->insns_to_delete = BITMAP_ALLOC (&problem_data->insn_bitmaps);
401 df->insns_to_rescan = BITMAP_ALLOC (&problem_data->insn_bitmaps);
402 df->insns_to_notes_rescan = BITMAP_ALLOC (&problem_data->insn_bitmaps);
403 df_scan->optional_p = false;
407 /* Free all of the data associated with the scan problem. */
409 static void
410 df_scan_free (void)
412 if (df_scan->problem_data)
413 df_scan_free_internal ();
415 if (df->blocks_to_analyze)
417 BITMAP_FREE (df->blocks_to_analyze);
418 df->blocks_to_analyze = NULL;
421 free (df_scan);
424 /* Dump the preamble for DF_SCAN dump. */
425 static void
426 df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
428 int i;
429 int dcount = 0;
430 int ucount = 0;
431 int ecount = 0;
432 int icount = 0;
433 int ccount = 0;
434 basic_block bb;
435 rtx insn;
437 fprintf (file, ";; invalidated by call \t");
438 df_print_regset (file, regs_invalidated_by_call_regset);
439 fprintf (file, ";; hardware regs used \t");
440 df_print_regset (file, df->hardware_regs_used);
441 fprintf (file, ";; regular block artificial uses \t");
442 df_print_regset (file, df->regular_block_artificial_uses);
443 fprintf (file, ";; eh block artificial uses \t");
444 df_print_regset (file, df->eh_block_artificial_uses);
445 fprintf (file, ";; entry block defs \t");
446 df_print_regset (file, df->entry_block_defs);
447 fprintf (file, ";; exit block uses \t");
448 df_print_regset (file, df->exit_block_uses);
449 fprintf (file, ";; regs ever live \t");
450 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
451 if (df_regs_ever_live_p (i))
452 fprintf (file, " %d[%s]", i, reg_names[i]);
453 fprintf (file, "\n;; ref usage \t");
455 for (i = 0; i < (int)df->regs_inited; i++)
456 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
458 const char * sep = "";
460 fprintf (file, "r%d={", i);
461 if (DF_REG_DEF_COUNT (i))
463 fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
464 sep = ",";
465 dcount += DF_REG_DEF_COUNT (i);
467 if (DF_REG_USE_COUNT (i))
469 fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
470 sep = ",";
471 ucount += DF_REG_USE_COUNT (i);
473 if (DF_REG_EQ_USE_COUNT (i))
475 fprintf (file, "%s%dd", sep, DF_REG_EQ_USE_COUNT (i));
476 ecount += DF_REG_EQ_USE_COUNT (i);
478 fprintf (file, "} ");
481 FOR_EACH_BB (bb)
482 FOR_BB_INSNS (bb, insn)
483 if (INSN_P (insn))
485 if (CALL_P (insn))
486 ccount++;
487 else
488 icount++;
491 fprintf (file, "\n;; total ref usage %d{%dd,%du,%de} in %d{%d regular + %d call} insns.\n",
492 dcount + ucount + ecount, dcount, ucount, ecount, icount + ccount, icount, ccount);
495 /* Dump the bb_info for a given basic block. */
496 static void
497 df_scan_start_block (basic_block bb, FILE *file)
499 struct df_scan_bb_info *bb_info
500 = df_scan_get_bb_info (bb->index);
502 if (bb_info)
504 fprintf (file, ";; bb %d artificial_defs: ", bb->index);
505 df_refs_chain_dump (bb_info->artificial_defs, true, file);
506 fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
507 df_refs_chain_dump (bb_info->artificial_uses, true, file);
508 fprintf (file, "\n");
510 #if 0
512 rtx insn;
513 FOR_BB_INSNS (bb, insn)
514 if (INSN_P (insn))
515 df_insn_debug (insn, false, file);
517 #endif
520 static struct df_problem problem_SCAN =
522 DF_SCAN, /* Problem id. */
523 DF_NONE, /* Direction. */
524 df_scan_alloc, /* Allocate the problem specific data. */
525 NULL, /* Reset global information. */
526 df_scan_free_bb_info, /* Free basic block info. */
527 NULL, /* Local compute function. */
528 NULL, /* Init the solution specific data. */
529 NULL, /* Iterative solver. */
530 NULL, /* Confluence operator 0. */
531 NULL, /* Confluence operator n. */
532 NULL, /* Transfer function. */
533 NULL, /* Finalize function. */
534 df_scan_free, /* Free all of the problem information. */
535 NULL, /* Remove this problem from the stack of dataflow problems. */
536 df_scan_start_dump, /* Debugging. */
537 df_scan_start_block, /* Debugging start block. */
538 NULL, /* Debugging end block. */
539 NULL, /* Incremental solution verify start. */
540 NULL, /* Incremental solution verify end. */
541 NULL, /* Dependent problem. */
542 TV_DF_SCAN, /* Timing variable. */
543 false /* Reset blocks on dropping out of blocks_to_analyze. */
547 /* Create a new DATAFLOW instance and add it to an existing instance
548 of DF. The returned structure is what is used to get at the
549 solution. */
551 void
552 df_scan_add_problem (void)
554 df_add_problem (&problem_SCAN);
558 /*----------------------------------------------------------------------------
559 Storage Allocation Utilities
560 ----------------------------------------------------------------------------*/
563 /* First, grow the reg_info information. If the current size is less than
564 the number of pseudos, grow to 25% more than the number of
565 pseudos.
567 Second, assure that all of the slots up to max_reg_num have been
568 filled with reg_info structures. */
570 void
571 df_grow_reg_info (void)
573 unsigned int max_reg = max_reg_num ();
574 unsigned int new_size = max_reg;
575 struct df_scan_problem_data *problem_data
576 = (struct df_scan_problem_data *) df_scan->problem_data;
577 unsigned int i;
579 if (df->regs_size < new_size)
581 new_size += new_size / 4;
582 df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
583 df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
584 df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
585 new_size);
586 df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
587 df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
588 df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
589 df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
590 df->regs_size = new_size;
593 for (i = df->regs_inited; i < max_reg; i++)
595 struct df_reg_info *reg_info;
597 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
598 memset (reg_info, 0, sizeof (struct df_reg_info));
599 df->def_regs[i] = reg_info;
600 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
601 memset (reg_info, 0, sizeof (struct df_reg_info));
602 df->use_regs[i] = reg_info;
603 reg_info = (struct df_reg_info *) pool_alloc (problem_data->reg_pool);
604 memset (reg_info, 0, sizeof (struct df_reg_info));
605 df->eq_use_regs[i] = reg_info;
606 df->def_info.begin[i] = 0;
607 df->def_info.count[i] = 0;
608 df->use_info.begin[i] = 0;
609 df->use_info.count[i] = 0;
612 df->regs_inited = max_reg;
616 /* Grow the ref information. */
618 static void
619 df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
621 if (ref_info->refs_size < new_size)
623 ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
624 memset (ref_info->refs + ref_info->refs_size, 0,
625 (new_size - ref_info->refs_size) *sizeof (df_ref));
626 ref_info->refs_size = new_size;
631 /* Check and grow the ref information if necessary. This routine
632 guarantees total_size + BITMAP_ADDEND amount of entries in refs
633 array. It updates ref_info->refs_size only and does not change
634 ref_info->total_size. */
636 static void
637 df_check_and_grow_ref_info (struct df_ref_info *ref_info,
638 unsigned bitmap_addend)
640 if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
642 int new_size = ref_info->total_size + bitmap_addend;
643 new_size += ref_info->total_size / 4;
644 df_grow_ref_info (ref_info, new_size);
649 /* Grow the ref information. If the current size is less than the
650 number of instructions, grow to 25% more than the number of
651 instructions. */
653 void
654 df_grow_insn_info (void)
656 unsigned int new_size = get_max_uid () + 1;
657 if (DF_INSN_SIZE () < new_size)
659 new_size += new_size / 4;
660 df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
661 memset (df->insns + df->insns_size, 0,
662 (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
663 DF_INSN_SIZE () = new_size;
670 /*----------------------------------------------------------------------------
671 PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
672 ----------------------------------------------------------------------------*/
674 /* Rescan all of the block_to_analyze or all of the blocks in the
675 function if df_set_blocks if blocks_to_analyze is NULL; */
677 void
678 df_scan_blocks (void)
680 basic_block bb;
682 df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
683 df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
685 df_get_regular_block_artificial_uses (df->regular_block_artificial_uses);
686 df_get_eh_block_artificial_uses (df->eh_block_artificial_uses);
688 bitmap_ior_into (df->eh_block_artificial_uses,
689 df->regular_block_artificial_uses);
691 /* ENTRY and EXIT blocks have special defs/uses. */
692 df_get_entry_block_def_set (df->entry_block_defs);
693 df_record_entry_block_defs (df->entry_block_defs);
694 df_get_exit_block_use_set (df->exit_block_uses);
695 df_record_exit_block_uses (df->exit_block_uses);
696 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
697 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
699 /* Regular blocks */
700 FOR_EACH_BB (bb)
702 unsigned int bb_index = bb->index;
703 df_bb_refs_record (bb_index, true);
708 /* Create a new ref of type DF_REF_TYPE for register REG at address
709 LOC within INSN of BB. This function is only used externally.
711 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
712 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
713 fields if they were constants. Otherwise they should be -1 if
714 those flags were set. */
716 df_ref
717 df_ref_create (rtx reg, rtx *loc, rtx insn,
718 basic_block bb,
719 enum df_ref_type ref_type,
720 int ref_flags,
721 int width, int offset, enum machine_mode mode)
723 df_ref ref;
724 struct df_reg_info **reg_info;
725 struct df_ref_info *ref_info;
726 df_ref *ref_rec;
727 df_ref **ref_rec_ptr;
728 unsigned int count = 0;
729 bool add_to_table;
730 enum df_ref_class cl;
732 df_grow_reg_info ();
734 /* You cannot hack artificial refs. */
735 gcc_assert (insn);
737 if (width != -1 || offset != -1)
738 cl = DF_REF_EXTRACT;
739 else if (loc)
740 cl = DF_REF_REGULAR;
741 else
742 cl = DF_REF_BASE;
743 ref = df_ref_create_structure (cl, NULL, reg, loc, bb, DF_INSN_INFO_GET (insn),
744 ref_type, ref_flags,
745 width, offset, mode);
747 if (DF_REF_REG_DEF_P (ref))
749 reg_info = df->def_regs;
750 ref_info = &df->def_info;
751 ref_rec_ptr = &DF_INSN_DEFS (insn);
752 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
754 else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
756 reg_info = df->eq_use_regs;
757 ref_info = &df->use_info;
758 ref_rec_ptr = &DF_INSN_EQ_USES (insn);
759 switch (ref_info->ref_order)
761 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
762 case DF_REF_ORDER_BY_REG_WITH_NOTES:
763 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
764 add_to_table = true;
765 break;
766 default:
767 add_to_table = false;
768 break;
771 else
773 reg_info = df->use_regs;
774 ref_info = &df->use_info;
775 ref_rec_ptr = &DF_INSN_USES (insn);
776 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
779 /* Do not add if ref is not in the right blocks. */
780 if (add_to_table && df->analyze_subset)
781 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
783 df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
785 if (add_to_table)
786 switch (ref_info->ref_order)
788 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
789 case DF_REF_ORDER_BY_REG_WITH_NOTES:
790 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
791 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
792 break;
793 default:
794 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
795 break;
798 ref_rec = *ref_rec_ptr;
799 while (*ref_rec)
801 count++;
802 ref_rec++;
805 ref_rec = *ref_rec_ptr;
806 if (count)
808 ref_rec = XRESIZEVEC (df_ref, ref_rec, count+2);
809 *ref_rec_ptr = ref_rec;
810 ref_rec[count] = ref;
811 ref_rec[count+1] = NULL;
812 qsort (ref_rec, count + 1, sizeof (df_ref), df_ref_compare);
814 else
816 df_ref *ref_rec = XNEWVEC (df_ref, 2);
817 ref_rec[0] = ref;
818 ref_rec[1] = NULL;
819 *ref_rec_ptr = ref_rec;
822 #if 0
823 if (dump_file)
825 fprintf (dump_file, "adding ref ");
826 df_ref_debug (ref, dump_file);
828 #endif
829 /* By adding the ref directly, df_insn_rescan my not find any
830 differences even though the block will have changed. So we need
831 to mark the block dirty ourselves. */
832 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
833 df_set_bb_dirty (bb);
835 return ref;
840 /*----------------------------------------------------------------------------
841 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
842 ----------------------------------------------------------------------------*/
844 static void
845 df_free_ref (df_ref ref)
847 struct df_scan_problem_data *problem_data
848 = (struct df_scan_problem_data *) df_scan->problem_data;
850 switch (DF_REF_CLASS (ref))
852 case DF_REF_BASE:
853 pool_free (problem_data->ref_base_pool, ref);
854 break;
856 case DF_REF_ARTIFICIAL:
857 pool_free (problem_data->ref_artificial_pool, ref);
858 break;
860 case DF_REF_REGULAR:
861 pool_free (problem_data->ref_regular_pool, ref);
862 break;
864 case DF_REF_EXTRACT:
865 pool_free (problem_data->ref_extract_pool, ref);
866 break;
871 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
872 Also delete the def-use or use-def chain if it exists. */
874 static void
875 df_reg_chain_unlink (df_ref ref)
877 df_ref next = DF_REF_NEXT_REG (ref);
878 df_ref prev = DF_REF_PREV_REG (ref);
879 int id = DF_REF_ID (ref);
880 struct df_reg_info *reg_info;
881 df_ref *refs = NULL;
883 if (DF_REF_REG_DEF_P (ref))
885 int regno = DF_REF_REGNO (ref);
886 reg_info = DF_REG_DEF_GET (regno);
887 refs = df->def_info.refs;
889 else
891 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
893 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
894 switch (df->use_info.ref_order)
896 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
897 case DF_REF_ORDER_BY_REG_WITH_NOTES:
898 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
899 refs = df->use_info.refs;
900 break;
901 default:
902 break;
905 else
907 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
908 refs = df->use_info.refs;
912 if (refs)
914 if (df->analyze_subset)
916 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
917 refs[id] = NULL;
919 else
920 refs[id] = NULL;
923 /* Delete any def-use or use-def chains that start here. It is
924 possible that there is trash in this field. This happens for
925 insns that have been deleted when rescanning has been deferred
926 and the chain problem has also been deleted. The chain tear down
927 code skips deleted insns. */
928 if (df_chain && DF_REF_CHAIN (ref))
929 df_chain_unlink (ref);
931 reg_info->n_refs--;
932 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
934 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
935 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
938 /* Unlink from the reg chain. If there is no prev, this is the
939 first of the list. If not, just join the next and prev. */
940 if (prev)
941 DF_REF_NEXT_REG (prev) = next;
942 else
944 gcc_assert (reg_info->reg_chain == ref);
945 reg_info->reg_chain = next;
947 if (next)
948 DF_REF_PREV_REG (next) = prev;
950 df_free_ref (ref);
954 /* Remove REF from VEC. */
956 static void
957 df_ref_compress_rec (df_ref **vec_ptr, df_ref ref)
959 df_ref *vec = *vec_ptr;
961 if (vec[1])
963 while (*vec && *vec != ref)
964 vec++;
966 while (*vec)
968 *vec = *(vec+1);
969 vec++;
972 else
974 free (vec);
975 *vec_ptr = df_null_ref_rec;
980 /* Unlink REF from all def-use/use-def chains, etc. */
982 void
983 df_ref_remove (df_ref ref)
985 #if 0
986 if (dump_file)
988 fprintf (dump_file, "removing ref ");
989 df_ref_debug (ref, dump_file);
991 #endif
993 if (DF_REF_REG_DEF_P (ref))
995 if (DF_REF_IS_ARTIFICIAL (ref))
997 struct df_scan_bb_info *bb_info
998 = df_scan_get_bb_info (DF_REF_BBNO (ref));
999 df_ref_compress_rec (&bb_info->artificial_defs, ref);
1001 else
1003 unsigned int uid = DF_REF_INSN_UID (ref);
1004 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
1005 df_ref_compress_rec (&insn_rec->defs, ref);
1008 else
1010 if (DF_REF_IS_ARTIFICIAL (ref))
1012 struct df_scan_bb_info *bb_info
1013 = df_scan_get_bb_info (DF_REF_BBNO (ref));
1014 df_ref_compress_rec (&bb_info->artificial_uses, ref);
1016 else
1018 unsigned int uid = DF_REF_INSN_UID (ref);
1019 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
1021 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
1022 df_ref_compress_rec (&insn_rec->eq_uses, ref);
1023 else
1024 df_ref_compress_rec (&insn_rec->uses, ref);
1028 /* By deleting the ref directly, df_insn_rescan my not find any
1029 differences even though the block will have changed. So we need
1030 to mark the block dirty ourselves. */
1031 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
1032 df_set_bb_dirty (DF_REF_BB (ref));
1033 df_reg_chain_unlink (ref);
1037 /* Create the insn record for INSN. If there was one there, zero it
1038 out. */
1040 struct df_insn_info *
1041 df_insn_create_insn_record (rtx insn)
1043 struct df_scan_problem_data *problem_data
1044 = (struct df_scan_problem_data *) df_scan->problem_data;
1045 struct df_insn_info *insn_rec;
1047 df_grow_insn_info ();
1048 insn_rec = DF_INSN_INFO_GET (insn);
1049 if (!insn_rec)
1051 insn_rec = (struct df_insn_info *) pool_alloc (problem_data->insn_pool);
1052 DF_INSN_INFO_SET (insn, insn_rec);
1054 memset (insn_rec, 0, sizeof (struct df_insn_info));
1055 insn_rec->insn = insn;
1056 return insn_rec;
1060 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
1062 static void
1063 df_ref_chain_delete_du_chain (df_ref *ref_rec)
1065 while (*ref_rec)
1067 df_ref ref = *ref_rec;
1068 /* CHAIN is allocated by DF_CHAIN. So make sure to
1069 pass df_scan instance for the problem. */
1070 if (DF_REF_CHAIN (ref))
1071 df_chain_unlink (ref);
1072 ref_rec++;
1077 /* Delete all refs in the ref chain. */
1079 static void
1080 df_ref_chain_delete (df_ref *ref_rec)
1082 df_ref *start = ref_rec;
1083 while (*ref_rec)
1085 df_reg_chain_unlink (*ref_rec);
1086 ref_rec++;
1089 /* If the list is empty, it has a special shared element that is not
1090 to be deleted. */
1091 if (*start)
1092 free (start);
1096 /* Delete the hardreg chain. */
1098 static void
1099 df_mw_hardreg_chain_delete (struct df_mw_hardreg **hardregs)
1101 struct df_scan_problem_data *problem_data;
1103 if (!hardregs)
1104 return;
1106 problem_data = (struct df_scan_problem_data *) df_scan->problem_data;
1108 while (*hardregs)
1110 pool_free (problem_data->mw_reg_pool, *hardregs);
1111 hardregs++;
1116 /* Delete all of the refs information from INSN. BB must be passed in
1117 except when called from df_process_deferred_rescans to mark the block
1118 as dirty. */
1120 void
1121 df_insn_delete (basic_block bb, unsigned int uid)
1123 struct df_insn_info *insn_info = NULL;
1124 if (!df)
1125 return;
1127 df_grow_bb_info (df_scan);
1128 df_grow_reg_info ();
1130 /* The block must be marked as dirty now, rather than later as in
1131 df_insn_rescan and df_notes_rescan because it may not be there at
1132 rescanning time and the mark would blow up. */
1133 if (bb)
1134 df_set_bb_dirty (bb);
1136 insn_info = DF_INSN_UID_SAFE_GET (uid);
1138 /* The client has deferred rescanning. */
1139 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1141 if (insn_info)
1143 bitmap_clear_bit (df->insns_to_rescan, uid);
1144 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1145 bitmap_set_bit (df->insns_to_delete, uid);
1147 if (dump_file)
1148 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1149 return;
1152 if (dump_file)
1153 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1155 bitmap_clear_bit (df->insns_to_delete, uid);
1156 bitmap_clear_bit (df->insns_to_rescan, uid);
1157 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1158 if (insn_info)
1160 struct df_scan_problem_data *problem_data
1161 = (struct df_scan_problem_data *) df_scan->problem_data;
1163 /* In general, notes do not have the insn_info fields
1164 initialized. However, combine deletes insns by changing them
1165 to notes. How clever. So we cannot just check if it is a
1166 valid insn before short circuiting this code, we need to see
1167 if we actually initialized it. */
1168 if (insn_info->defs)
1170 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1172 if (df_chain)
1174 df_ref_chain_delete_du_chain (insn_info->defs);
1175 df_ref_chain_delete_du_chain (insn_info->uses);
1176 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1179 df_ref_chain_delete (insn_info->defs);
1180 df_ref_chain_delete (insn_info->uses);
1181 df_ref_chain_delete (insn_info->eq_uses);
1183 pool_free (problem_data->insn_pool, insn_info);
1184 DF_INSN_UID_SET (uid, NULL);
1189 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1191 static void
1192 df_free_collection_rec (struct df_collection_rec *collection_rec)
1194 unsigned int ix;
1195 struct df_scan_problem_data *problem_data
1196 = (struct df_scan_problem_data *) df_scan->problem_data;
1197 df_ref ref;
1198 struct df_mw_hardreg *mw;
1200 for (ix = 0; VEC_iterate (df_ref, collection_rec->def_vec, ix, ref); ++ix)
1201 df_free_ref (ref);
1202 for (ix = 0; VEC_iterate (df_ref, collection_rec->use_vec, ix, ref); ++ix)
1203 df_free_ref (ref);
1204 for (ix = 0; VEC_iterate (df_ref, collection_rec->eq_use_vec, ix, ref); ++ix)
1205 df_free_ref (ref);
1206 for (ix = 0;
1207 VEC_iterate (df_mw_hardreg_ptr, collection_rec->mw_vec, ix, mw);
1208 ++ix)
1209 pool_free (problem_data->mw_reg_pool, mw);
1211 VEC_free (df_ref, stack, collection_rec->def_vec);
1212 VEC_free (df_ref, stack, collection_rec->use_vec);
1213 VEC_free (df_ref, stack, collection_rec->eq_use_vec);
1214 VEC_free (df_mw_hardreg_ptr, stack, collection_rec->mw_vec);
1217 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1219 bool
1220 df_insn_rescan (rtx insn)
1222 unsigned int uid = INSN_UID (insn);
1223 struct df_insn_info *insn_info = NULL;
1224 basic_block bb = BLOCK_FOR_INSN (insn);
1225 struct df_collection_rec collection_rec;
1227 if ((!df) || (!INSN_P (insn)))
1228 return false;
1230 if (!bb)
1232 if (dump_file)
1233 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1234 return false;
1237 /* The client has disabled rescanning and plans to do it itself. */
1238 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1239 return false;
1241 df_grow_bb_info (df_scan);
1242 df_grow_reg_info ();
1244 insn_info = DF_INSN_UID_SAFE_GET (uid);
1246 /* The client has deferred rescanning. */
1247 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1249 if (!insn_info)
1251 insn_info = df_insn_create_insn_record (insn);
1252 insn_info->defs = df_null_ref_rec;
1253 insn_info->uses = df_null_ref_rec;
1254 insn_info->eq_uses = df_null_ref_rec;
1255 insn_info->mw_hardregs = df_null_mw_rec;
1257 if (dump_file)
1258 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1260 bitmap_clear_bit (df->insns_to_delete, uid);
1261 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1262 bitmap_set_bit (df->insns_to_rescan, INSN_UID (insn));
1263 return false;
1266 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
1267 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
1268 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
1269 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
1271 bitmap_clear_bit (df->insns_to_delete, uid);
1272 bitmap_clear_bit (df->insns_to_rescan, uid);
1273 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1274 if (insn_info)
1276 int luid;
1277 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1278 /* If there's no change, return false. */
1279 if (the_same)
1281 df_free_collection_rec (&collection_rec);
1282 if (dump_file)
1283 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1284 return false;
1286 if (dump_file)
1287 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1289 /* There's change - we need to delete the existing info.
1290 Since the insn isn't moved, we can salvage its LUID. */
1291 luid = DF_INSN_LUID (insn);
1292 df_insn_delete (NULL, uid);
1293 df_insn_create_insn_record (insn);
1294 DF_INSN_LUID (insn) = luid;
1296 else
1298 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1299 df_insn_refs_collect (&collection_rec, bb, insn_info);
1300 if (dump_file)
1301 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1304 df_refs_add_to_chains (&collection_rec, bb, insn);
1305 if (DEBUG_INSN_P (insn))
1306 df_set_bb_dirty_nonlr (bb);
1307 else
1308 df_set_bb_dirty (bb);
1310 VEC_free (df_ref, stack, collection_rec.def_vec);
1311 VEC_free (df_ref, stack, collection_rec.use_vec);
1312 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
1313 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
1315 return true;
1318 /* Same as df_insn_rescan, but don't mark the basic block as
1319 dirty. */
1321 bool
1322 df_insn_rescan_debug_internal (rtx insn)
1324 unsigned int uid = INSN_UID (insn);
1325 struct df_insn_info *insn_info;
1327 gcc_assert (DEBUG_INSN_P (insn)
1328 && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1330 if (!df)
1331 return false;
1333 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1334 if (!insn_info)
1335 return false;
1337 if (dump_file)
1338 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1340 bitmap_clear_bit (df->insns_to_delete, uid);
1341 bitmap_clear_bit (df->insns_to_rescan, uid);
1342 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1344 if (!insn_info->defs)
1345 return false;
1347 if (insn_info->defs == df_null_ref_rec
1348 && insn_info->uses == df_null_ref_rec
1349 && insn_info->eq_uses == df_null_ref_rec
1350 && insn_info->mw_hardregs == df_null_mw_rec)
1351 return false;
1353 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1355 if (df_chain)
1357 df_ref_chain_delete_du_chain (insn_info->defs);
1358 df_ref_chain_delete_du_chain (insn_info->uses);
1359 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1362 df_ref_chain_delete (insn_info->defs);
1363 df_ref_chain_delete (insn_info->uses);
1364 df_ref_chain_delete (insn_info->eq_uses);
1366 insn_info->defs = df_null_ref_rec;
1367 insn_info->uses = df_null_ref_rec;
1368 insn_info->eq_uses = df_null_ref_rec;
1369 insn_info->mw_hardregs = df_null_mw_rec;
1371 return true;
1375 /* Rescan all of the insns in the function. Note that the artificial
1376 uses and defs are not touched. This function will destroy def-se
1377 or use-def chains. */
1379 void
1380 df_insn_rescan_all (void)
1382 bool no_insn_rescan = false;
1383 bool defer_insn_rescan = false;
1384 basic_block bb;
1385 bitmap_iterator bi;
1386 unsigned int uid;
1387 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1389 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1391 df_clear_flags (DF_NO_INSN_RESCAN);
1392 no_insn_rescan = true;
1395 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1397 df_clear_flags (DF_DEFER_INSN_RESCAN);
1398 defer_insn_rescan = true;
1401 bitmap_copy (tmp, df->insns_to_delete);
1402 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1404 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1405 if (insn_info)
1406 df_insn_delete (NULL, uid);
1409 BITMAP_FREE (tmp);
1410 bitmap_clear (df->insns_to_delete);
1411 bitmap_clear (df->insns_to_rescan);
1412 bitmap_clear (df->insns_to_notes_rescan);
1414 FOR_EACH_BB (bb)
1416 rtx insn;
1417 FOR_BB_INSNS (bb, insn)
1419 df_insn_rescan (insn);
1423 if (no_insn_rescan)
1424 df_set_flags (DF_NO_INSN_RESCAN);
1425 if (defer_insn_rescan)
1426 df_set_flags (DF_DEFER_INSN_RESCAN);
1430 /* Process all of the deferred rescans or deletions. */
1432 void
1433 df_process_deferred_rescans (void)
1435 bool no_insn_rescan = false;
1436 bool defer_insn_rescan = false;
1437 bitmap_iterator bi;
1438 unsigned int uid;
1439 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1441 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1443 df_clear_flags (DF_NO_INSN_RESCAN);
1444 no_insn_rescan = true;
1447 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1449 df_clear_flags (DF_DEFER_INSN_RESCAN);
1450 defer_insn_rescan = true;
1453 if (dump_file)
1454 fprintf (dump_file, "starting the processing of deferred insns\n");
1456 bitmap_copy (tmp, df->insns_to_delete);
1457 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1459 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1460 if (insn_info)
1461 df_insn_delete (NULL, uid);
1464 bitmap_copy (tmp, df->insns_to_rescan);
1465 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1467 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1468 if (insn_info)
1469 df_insn_rescan (insn_info->insn);
1472 bitmap_copy (tmp, df->insns_to_notes_rescan);
1473 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1475 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1476 if (insn_info)
1477 df_notes_rescan (insn_info->insn);
1480 if (dump_file)
1481 fprintf (dump_file, "ending the processing of deferred insns\n");
1483 BITMAP_FREE (tmp);
1484 bitmap_clear (df->insns_to_delete);
1485 bitmap_clear (df->insns_to_rescan);
1486 bitmap_clear (df->insns_to_notes_rescan);
1488 if (no_insn_rescan)
1489 df_set_flags (DF_NO_INSN_RESCAN);
1490 if (defer_insn_rescan)
1491 df_set_flags (DF_DEFER_INSN_RESCAN);
1493 /* If someone changed regs_ever_live during this pass, fix up the
1494 entry and exit blocks. */
1495 if (df->redo_entry_and_exit)
1497 df_update_entry_exit_and_calls ();
1498 df->redo_entry_and_exit = false;
1503 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1504 the uses if INCLUDE_USES. Include the eq_uses if
1505 INCLUDE_EQ_USES. */
1507 static unsigned int
1508 df_count_refs (bool include_defs, bool include_uses,
1509 bool include_eq_uses)
1511 unsigned int regno;
1512 int size = 0;
1513 unsigned int m = df->regs_inited;
1515 for (regno = 0; regno < m; regno++)
1517 if (include_defs)
1518 size += DF_REG_DEF_COUNT (regno);
1519 if (include_uses)
1520 size += DF_REG_USE_COUNT (regno);
1521 if (include_eq_uses)
1522 size += DF_REG_EQ_USE_COUNT (regno);
1524 return size;
1528 /* Take build ref table for either the uses or defs from the reg-use
1529 or reg-def chains. This version processes the refs in reg order
1530 which is likely to be best if processing the whole function. */
1532 static void
1533 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1534 bool include_defs,
1535 bool include_uses,
1536 bool include_eq_uses)
1538 unsigned int m = df->regs_inited;
1539 unsigned int regno;
1540 unsigned int offset = 0;
1541 unsigned int start;
1543 if (df->changeable_flags & DF_NO_HARD_REGS)
1545 start = FIRST_PSEUDO_REGISTER;
1546 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1547 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1549 else
1550 start = 0;
1552 ref_info->total_size
1553 = df_count_refs (include_defs, include_uses, include_eq_uses);
1555 df_check_and_grow_ref_info (ref_info, 1);
1557 for (regno = start; regno < m; regno++)
1559 int count = 0;
1560 ref_info->begin[regno] = offset;
1561 if (include_defs)
1563 df_ref ref = DF_REG_DEF_CHAIN (regno);
1564 while (ref)
1566 ref_info->refs[offset] = ref;
1567 DF_REF_ID (ref) = offset++;
1568 count++;
1569 ref = DF_REF_NEXT_REG (ref);
1570 gcc_assert (offset < ref_info->refs_size);
1573 if (include_uses)
1575 df_ref ref = DF_REG_USE_CHAIN (regno);
1576 while (ref)
1578 ref_info->refs[offset] = ref;
1579 DF_REF_ID (ref) = offset++;
1580 count++;
1581 ref = DF_REF_NEXT_REG (ref);
1582 gcc_assert (offset < ref_info->refs_size);
1585 if (include_eq_uses)
1587 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1588 while (ref)
1590 ref_info->refs[offset] = ref;
1591 DF_REF_ID (ref) = offset++;
1592 count++;
1593 ref = DF_REF_NEXT_REG (ref);
1594 gcc_assert (offset < ref_info->refs_size);
1597 ref_info->count[regno] = count;
1600 /* The bitmap size is not decremented when refs are deleted. So
1601 reset it now that we have squished out all of the empty
1602 slots. */
1603 ref_info->table_size = offset;
1607 /* Take build ref table for either the uses or defs from the reg-use
1608 or reg-def chains. This version processes the refs in insn order
1609 which is likely to be best if processing some segment of the
1610 function. */
1612 static void
1613 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1614 bool include_defs,
1615 bool include_uses,
1616 bool include_eq_uses)
1618 bitmap_iterator bi;
1619 unsigned int bb_index;
1620 unsigned int m = df->regs_inited;
1621 unsigned int offset = 0;
1622 unsigned int r;
1623 unsigned int start
1624 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1626 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1627 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1629 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1630 df_check_and_grow_ref_info (ref_info, 1);
1632 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1634 basic_block bb = BASIC_BLOCK (bb_index);
1635 rtx insn;
1636 df_ref *ref_rec;
1638 if (include_defs)
1639 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1641 unsigned int regno = DF_REF_REGNO (*ref_rec);
1642 ref_info->count[regno]++;
1644 if (include_uses)
1645 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1647 unsigned int regno = DF_REF_REGNO (*ref_rec);
1648 ref_info->count[regno]++;
1651 FOR_BB_INSNS (bb, insn)
1653 if (INSN_P (insn))
1655 unsigned int uid = INSN_UID (insn);
1657 if (include_defs)
1658 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1660 unsigned int regno = DF_REF_REGNO (*ref_rec);
1661 ref_info->count[regno]++;
1663 if (include_uses)
1664 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1666 unsigned int regno = DF_REF_REGNO (*ref_rec);
1667 ref_info->count[regno]++;
1669 if (include_eq_uses)
1670 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1672 unsigned int regno = DF_REF_REGNO (*ref_rec);
1673 ref_info->count[regno]++;
1679 for (r = start; r < m; r++)
1681 ref_info->begin[r] = offset;
1682 offset += ref_info->count[r];
1683 ref_info->count[r] = 0;
1686 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1688 basic_block bb = BASIC_BLOCK (bb_index);
1689 rtx insn;
1690 df_ref *ref_rec;
1692 if (include_defs)
1693 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1695 df_ref ref = *ref_rec;
1696 unsigned int regno = DF_REF_REGNO (ref);
1697 if (regno >= start)
1699 unsigned int id
1700 = ref_info->begin[regno] + ref_info->count[regno]++;
1701 DF_REF_ID (ref) = id;
1702 ref_info->refs[id] = ref;
1705 if (include_uses)
1706 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1708 df_ref ref = *ref_rec;
1709 unsigned int regno = DF_REF_REGNO (ref);
1710 if (regno >= start)
1712 unsigned int id
1713 = ref_info->begin[regno] + ref_info->count[regno]++;
1714 DF_REF_ID (ref) = id;
1715 ref_info->refs[id] = ref;
1719 FOR_BB_INSNS (bb, insn)
1721 if (INSN_P (insn))
1723 unsigned int uid = INSN_UID (insn);
1725 if (include_defs)
1726 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1728 df_ref ref = *ref_rec;
1729 unsigned int regno = DF_REF_REGNO (ref);
1730 if (regno >= start)
1732 unsigned int id
1733 = ref_info->begin[regno] + ref_info->count[regno]++;
1734 DF_REF_ID (ref) = id;
1735 ref_info->refs[id] = ref;
1738 if (include_uses)
1739 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1741 df_ref ref = *ref_rec;
1742 unsigned int regno = DF_REF_REGNO (ref);
1743 if (regno >= start)
1745 unsigned int id
1746 = ref_info->begin[regno] + ref_info->count[regno]++;
1747 DF_REF_ID (ref) = id;
1748 ref_info->refs[id] = ref;
1751 if (include_eq_uses)
1752 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1754 df_ref ref = *ref_rec;
1755 unsigned int regno = DF_REF_REGNO (ref);
1756 if (regno >= start)
1758 unsigned int id
1759 = ref_info->begin[regno] + ref_info->count[regno]++;
1760 DF_REF_ID (ref) = id;
1761 ref_info->refs[id] = ref;
1768 /* The bitmap size is not decremented when refs are deleted. So
1769 reset it now that we have squished out all of the empty
1770 slots. */
1772 ref_info->table_size = offset;
1775 /* Take build ref table for either the uses or defs from the reg-use
1776 or reg-def chains. */
1778 static void
1779 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1780 bool include_defs,
1781 bool include_uses,
1782 bool include_eq_uses)
1784 if (df->analyze_subset)
1785 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1786 include_uses, include_eq_uses);
1787 else
1788 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1789 include_uses, include_eq_uses);
1793 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1794 static unsigned int
1795 df_add_refs_to_table (unsigned int offset,
1796 struct df_ref_info *ref_info,
1797 df_ref *ref_vec)
1799 while (*ref_vec)
1801 df_ref ref = *ref_vec;
1802 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1803 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1805 ref_info->refs[offset] = ref;
1806 DF_REF_ID (*ref_vec) = offset++;
1808 ref_vec++;
1810 return offset;
1814 /* Count the number of refs in all of the insns of BB. Include the
1815 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1816 eq_uses if INCLUDE_EQ_USES. */
1818 static unsigned int
1819 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1820 struct df_ref_info *ref_info,
1821 bool include_defs, bool include_uses,
1822 bool include_eq_uses)
1824 rtx insn;
1826 if (include_defs)
1827 offset = df_add_refs_to_table (offset, ref_info,
1828 df_get_artificial_defs (bb->index));
1829 if (include_uses)
1830 offset = df_add_refs_to_table (offset, ref_info,
1831 df_get_artificial_uses (bb->index));
1833 FOR_BB_INSNS (bb, insn)
1834 if (INSN_P (insn))
1836 unsigned int uid = INSN_UID (insn);
1837 if (include_defs)
1838 offset = df_add_refs_to_table (offset, ref_info,
1839 DF_INSN_UID_DEFS (uid));
1840 if (include_uses)
1841 offset = df_add_refs_to_table (offset, ref_info,
1842 DF_INSN_UID_USES (uid));
1843 if (include_eq_uses)
1844 offset = df_add_refs_to_table (offset, ref_info,
1845 DF_INSN_UID_EQ_USES (uid));
1847 return offset;
1851 /* Organize the refs by insn into the table in REF_INFO. If
1852 blocks_to_analyze is defined, use that set, otherwise the entire
1853 program. Include the defs if INCLUDE_DEFS. Include the uses if
1854 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1856 static void
1857 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1858 bool include_defs, bool include_uses,
1859 bool include_eq_uses)
1861 basic_block bb;
1862 unsigned int offset = 0;
1864 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1865 df_check_and_grow_ref_info (ref_info, 1);
1866 if (df->blocks_to_analyze)
1868 bitmap_iterator bi;
1869 unsigned int index;
1871 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1873 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1874 include_defs, include_uses,
1875 include_eq_uses);
1878 ref_info->table_size = offset;
1880 else
1882 FOR_ALL_BB (bb)
1883 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1884 include_defs, include_uses,
1885 include_eq_uses);
1886 ref_info->table_size = offset;
1891 /* If the use refs in DF are not organized, reorganize them. */
1893 void
1894 df_maybe_reorganize_use_refs (enum df_ref_order order)
1896 if (order == df->use_info.ref_order)
1897 return;
1899 switch (order)
1901 case DF_REF_ORDER_BY_REG:
1902 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1903 break;
1905 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1906 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1907 break;
1909 case DF_REF_ORDER_BY_INSN:
1910 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1911 break;
1913 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1914 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1915 break;
1917 case DF_REF_ORDER_NO_TABLE:
1918 free (df->use_info.refs);
1919 df->use_info.refs = NULL;
1920 df->use_info.refs_size = 0;
1921 break;
1923 case DF_REF_ORDER_UNORDERED:
1924 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1925 gcc_unreachable ();
1926 break;
1929 df->use_info.ref_order = order;
1933 /* If the def refs in DF are not organized, reorganize them. */
1935 void
1936 df_maybe_reorganize_def_refs (enum df_ref_order order)
1938 if (order == df->def_info.ref_order)
1939 return;
1941 switch (order)
1943 case DF_REF_ORDER_BY_REG:
1944 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1945 break;
1947 case DF_REF_ORDER_BY_INSN:
1948 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1949 break;
1951 case DF_REF_ORDER_NO_TABLE:
1952 free (df->def_info.refs);
1953 df->def_info.refs = NULL;
1954 df->def_info.refs_size = 0;
1955 break;
1957 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1958 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1959 case DF_REF_ORDER_UNORDERED:
1960 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1961 gcc_unreachable ();
1962 break;
1965 df->def_info.ref_order = order;
1969 /* Change all of the basic block references in INSN to use the insn's
1970 current basic block. This function is called from routines that move
1971 instructions from one block to another. */
1973 void
1974 df_insn_change_bb (rtx insn, basic_block new_bb)
1976 basic_block old_bb = BLOCK_FOR_INSN (insn);
1977 struct df_insn_info *insn_info;
1978 unsigned int uid = INSN_UID (insn);
1980 if (old_bb == new_bb)
1981 return;
1983 set_block_for_insn (insn, new_bb);
1985 if (!df)
1986 return;
1988 if (dump_file)
1989 fprintf (dump_file, "changing bb of uid %d\n", uid);
1991 insn_info = DF_INSN_UID_SAFE_GET (uid);
1992 if (insn_info == NULL)
1994 if (dump_file)
1995 fprintf (dump_file, " unscanned insn\n");
1996 df_insn_rescan (insn);
1997 return;
2000 if (!INSN_P (insn))
2001 return;
2003 df_set_bb_dirty (new_bb);
2004 if (old_bb)
2006 if (dump_file)
2007 fprintf (dump_file, " from %d to %d\n",
2008 old_bb->index, new_bb->index);
2009 df_set_bb_dirty (old_bb);
2011 else
2012 if (dump_file)
2013 fprintf (dump_file, " to %d\n", new_bb->index);
2017 /* Helper function for df_ref_change_reg_with_loc. */
2019 static void
2020 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
2021 struct df_reg_info *new_df,
2022 int new_regno, rtx loc)
2024 df_ref the_ref = old_df->reg_chain;
2026 while (the_ref)
2028 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
2029 && (DF_REF_LOC (the_ref))
2030 && (*DF_REF_LOC (the_ref) == loc))
2032 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
2033 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
2034 df_ref *ref_vec, *ref_vec_t;
2035 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
2036 unsigned int count = 0;
2038 DF_REF_REGNO (the_ref) = new_regno;
2039 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
2041 /* Pull the_ref out of the old regno chain. */
2042 if (prev_ref)
2043 DF_REF_NEXT_REG (prev_ref) = next_ref;
2044 else
2045 old_df->reg_chain = next_ref;
2046 if (next_ref)
2047 DF_REF_PREV_REG (next_ref) = prev_ref;
2048 old_df->n_refs--;
2050 /* Put the ref into the new regno chain. */
2051 DF_REF_PREV_REG (the_ref) = NULL;
2052 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
2053 if (new_df->reg_chain)
2054 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
2055 new_df->reg_chain = the_ref;
2056 new_df->n_refs++;
2057 if (DF_REF_BB (the_ref))
2058 df_set_bb_dirty (DF_REF_BB (the_ref));
2060 /* Need to sort the record again that the ref was in because
2061 the regno is a sorting key. First, find the right
2062 record. */
2063 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
2064 ref_vec = insn_info->eq_uses;
2065 else
2066 ref_vec = insn_info->uses;
2067 if (dump_file)
2068 fprintf (dump_file, "changing reg in insn %d\n",
2069 DF_REF_INSN_UID (the_ref));
2071 ref_vec_t = ref_vec;
2073 /* Find the length. */
2074 while (*ref_vec_t)
2076 count++;
2077 ref_vec_t++;
2079 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
2081 the_ref = next_ref;
2083 else
2084 the_ref = DF_REF_NEXT_REG (the_ref);
2089 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2090 NEW_REGNO. Refs that do not match LOC are not changed which means
2091 that artificial refs are not changed since they have no loc. This
2092 call is to support the SET_REGNO macro. */
2094 void
2095 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2097 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2098 return;
2100 df_grow_reg_info ();
2102 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2103 DF_REG_DEF_GET (new_regno), new_regno, loc);
2104 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2105 DF_REG_USE_GET (new_regno), new_regno, loc);
2106 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2107 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2111 /* Delete the mw_hardregs that point into the eq_notes. */
2113 static unsigned int
2114 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2116 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2117 unsigned int deleted = 0;
2118 unsigned int count = 0;
2119 struct df_scan_problem_data *problem_data
2120 = (struct df_scan_problem_data *) df_scan->problem_data;
2122 if (!*mw_vec)
2123 return 0;
2125 while (*mw_vec)
2127 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2129 struct df_mw_hardreg **temp_vec = mw_vec;
2131 pool_free (problem_data->mw_reg_pool, *mw_vec);
2132 temp_vec = mw_vec;
2133 /* Shove the remaining ones down one to fill the gap. While
2134 this looks n**2, it is highly unusual to have any mw regs
2135 in eq_notes and the chances of more than one are almost
2136 non existent. */
2137 while (*temp_vec)
2139 *temp_vec = *(temp_vec + 1);
2140 temp_vec++;
2142 deleted++;
2144 else
2146 mw_vec++;
2147 count++;
2151 if (count == 0)
2153 df_scan_free_mws_vec (insn_info->mw_hardregs);
2154 insn_info->mw_hardregs = df_null_mw_rec;
2155 return 0;
2157 return deleted;
2161 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2163 void
2164 df_notes_rescan (rtx insn)
2166 struct df_insn_info *insn_info;
2167 unsigned int uid = INSN_UID (insn);
2169 if (!df)
2170 return;
2172 /* The client has disabled rescanning and plans to do it itself. */
2173 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2174 return;
2176 /* Do nothing if the insn hasn't been emitted yet. */
2177 if (!BLOCK_FOR_INSN (insn))
2178 return;
2180 df_grow_bb_info (df_scan);
2181 df_grow_reg_info ();
2183 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2185 /* The client has deferred rescanning. */
2186 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2188 if (!insn_info)
2190 insn_info = df_insn_create_insn_record (insn);
2191 insn_info->defs = df_null_ref_rec;
2192 insn_info->uses = df_null_ref_rec;
2193 insn_info->eq_uses = df_null_ref_rec;
2194 insn_info->mw_hardregs = df_null_mw_rec;
2197 bitmap_clear_bit (df->insns_to_delete, uid);
2198 /* If the insn is set to be rescanned, it does not need to also
2199 be notes rescanned. */
2200 if (!bitmap_bit_p (df->insns_to_rescan, uid))
2201 bitmap_set_bit (df->insns_to_notes_rescan, INSN_UID (insn));
2202 return;
2205 bitmap_clear_bit (df->insns_to_delete, uid);
2206 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
2208 if (insn_info)
2210 basic_block bb = BLOCK_FOR_INSN (insn);
2211 rtx note;
2212 struct df_collection_rec collection_rec;
2213 unsigned int num_deleted;
2214 unsigned int mw_len;
2216 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2217 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
2218 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
2220 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2221 df_ref_chain_delete (insn_info->eq_uses);
2222 insn_info->eq_uses = NULL;
2224 /* Process REG_EQUIV/REG_EQUAL notes */
2225 for (note = REG_NOTES (insn); note;
2226 note = XEXP (note, 1))
2228 switch (REG_NOTE_KIND (note))
2230 case REG_EQUIV:
2231 case REG_EQUAL:
2232 df_uses_record (DF_REF_REGULAR, &collection_rec,
2233 &XEXP (note, 0), DF_REF_REG_USE,
2234 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
2235 default:
2236 break;
2240 /* Find some place to put any new mw_hardregs. */
2241 df_canonize_collection_rec (&collection_rec);
2242 mw_len = VEC_length (df_mw_hardreg_ptr, collection_rec.mw_vec);
2243 if (mw_len)
2245 unsigned int count = 0;
2246 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2247 while (*mw_rec)
2249 count++;
2250 mw_rec++;
2253 if (count)
2255 /* Append to the end of the existing record after
2256 expanding it if necessary. */
2257 if (mw_len > num_deleted)
2259 insn_info->mw_hardregs =
2260 XRESIZEVEC (struct df_mw_hardreg *,
2261 insn_info->mw_hardregs,
2262 count + 1 + mw_len);
2264 memcpy (&insn_info->mw_hardregs[count],
2265 VEC_address (df_mw_hardreg_ptr, collection_rec.mw_vec),
2266 mw_len * sizeof (struct df_mw_hardreg *));
2267 insn_info->mw_hardregs[count + mw_len] = NULL;
2268 qsort (insn_info->mw_hardregs, count + mw_len,
2269 sizeof (struct df_mw_hardreg *), df_mw_compare);
2271 else
2273 /* No vector there. */
2274 insn_info->mw_hardregs
2275 = XNEWVEC (struct df_mw_hardreg*, 1 + mw_len);
2276 memcpy (insn_info->mw_hardregs,
2277 VEC_address (df_mw_hardreg_ptr, collection_rec.mw_vec),
2278 mw_len * sizeof (struct df_mw_hardreg *));
2279 insn_info->mw_hardregs[mw_len] = NULL;
2282 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2283 ignore it. */
2284 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
2285 df_refs_add_to_chains (&collection_rec, bb, insn);
2286 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
2288 else
2289 df_insn_rescan (insn);
2294 /*----------------------------------------------------------------------------
2295 Hard core instruction scanning code. No external interfaces here,
2296 just a lot of routines that look inside insns.
2297 ----------------------------------------------------------------------------*/
2300 /* Return true if the contents of two df_ref's are identical.
2301 It ignores DF_REF_MARKER. */
2303 static bool
2304 df_ref_equal_p (df_ref ref1, df_ref ref2)
2306 if (!ref2)
2307 return false;
2309 if (ref1 == ref2)
2310 return true;
2312 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2313 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2314 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2315 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2316 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2317 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2318 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2319 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2320 return false;
2322 switch (DF_REF_CLASS (ref1))
2324 case DF_REF_ARTIFICIAL:
2325 case DF_REF_BASE:
2326 return true;
2328 case DF_REF_EXTRACT:
2329 if ((DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2330 || (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2331 || (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2)))
2332 return false;
2333 /* fallthru. */
2335 case DF_REF_REGULAR:
2336 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2338 default:
2339 gcc_unreachable ();
2341 return false;
2345 /* Compare REF1 and REF2 for sorting. This is only called from places
2346 where all of the refs are of the same type, in the same insn, and
2347 have the same bb. So these fields are not checked. */
2349 static int
2350 df_ref_compare (const void *r1, const void *r2)
2352 const df_ref ref1 = *(const df_ref *)r1;
2353 const df_ref ref2 = *(const df_ref *)r2;
2355 if (ref1 == ref2)
2356 return 0;
2358 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2359 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2361 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2362 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2364 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2365 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2367 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2368 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2370 /* Cannot look at the LOC field on artificial refs. */
2371 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2372 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2373 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2375 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2377 /* If two refs are identical except that one of them has is from
2378 a mw and one is not, we need to have the one with the mw
2379 first. */
2380 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2381 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2382 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2383 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2384 return -1;
2385 else
2386 return 1;
2389 /* The classes are the same at this point so it is safe to only look
2390 at ref1. */
2391 if (DF_REF_CLASS (ref1) == DF_REF_EXTRACT)
2393 if (DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2394 return DF_REF_EXTRACT_OFFSET (ref1) - DF_REF_EXTRACT_OFFSET (ref2);
2395 if (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2396 return DF_REF_EXTRACT_WIDTH (ref1) - DF_REF_EXTRACT_WIDTH (ref2);
2397 if (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2))
2398 return DF_REF_EXTRACT_MODE (ref1) - DF_REF_EXTRACT_MODE (ref2);
2400 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2403 static void
2404 df_swap_refs (VEC(df_ref,stack) **ref_vec, int i, int j)
2406 df_ref tmp = VEC_index (df_ref, *ref_vec, i);
2407 VEC_replace (df_ref, *ref_vec, i, VEC_index (df_ref, *ref_vec, j));
2408 VEC_replace (df_ref, *ref_vec, j, tmp);
2411 /* Sort and compress a set of refs. */
2413 static void
2414 df_sort_and_compress_refs (VEC(df_ref,stack) **ref_vec)
2416 unsigned int count;
2417 unsigned int i;
2418 unsigned int dist = 0;
2420 count = VEC_length (df_ref, *ref_vec);
2422 /* If there are 1 or 0 elements, there is nothing to do. */
2423 if (count < 2)
2424 return;
2425 else if (count == 2)
2427 df_ref r0 = VEC_index (df_ref, *ref_vec, 0);
2428 df_ref r1 = VEC_index (df_ref, *ref_vec, 1);
2429 if (df_ref_compare (&r0, &r1) > 0)
2430 df_swap_refs (ref_vec, 0, 1);
2432 else
2434 for (i = 0; i < count - 1; i++)
2436 df_ref r0 = VEC_index (df_ref, *ref_vec, i);
2437 df_ref r1 = VEC_index (df_ref, *ref_vec, i + 1);
2438 if (df_ref_compare (&r0, &r1) >= 0)
2439 break;
2441 /* If the array is already strictly ordered,
2442 which is the most common case for large COUNT case
2443 (which happens for CALL INSNs),
2444 no need to sort and filter out duplicate.
2445 Simply return the count.
2446 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2447 of DF_REF_COMPARE. */
2448 if (i == count - 1)
2449 return;
2450 qsort (VEC_address (df_ref, *ref_vec), count, sizeof (df_ref),
2451 df_ref_compare);
2454 for (i=0; i<count-dist; i++)
2456 /* Find the next ref that is not equal to the current ref. */
2457 while (i + dist + 1 < count
2458 && df_ref_equal_p (VEC_index (df_ref, *ref_vec, i),
2459 VEC_index (df_ref, *ref_vec, i + dist + 1)))
2461 df_free_ref (VEC_index (df_ref, *ref_vec, i + dist + 1));
2462 dist++;
2464 /* Copy it down to the next position. */
2465 if (dist && i + dist + 1 < count)
2466 VEC_replace (df_ref, *ref_vec, i + 1,
2467 VEC_index (df_ref, *ref_vec, i + dist + 1));
2470 count -= dist;
2471 VEC_truncate (df_ref, *ref_vec, count);
2475 /* Return true if the contents of two df_ref's are identical.
2476 It ignores DF_REF_MARKER. */
2478 static bool
2479 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2481 if (!mw2)
2482 return false;
2483 return (mw1 == mw2) ||
2484 (mw1->mw_reg == mw2->mw_reg
2485 && mw1->type == mw2->type
2486 && mw1->flags == mw2->flags
2487 && mw1->start_regno == mw2->start_regno
2488 && mw1->end_regno == mw2->end_regno);
2492 /* Compare MW1 and MW2 for sorting. */
2494 static int
2495 df_mw_compare (const void *m1, const void *m2)
2497 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2498 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2500 if (mw1 == mw2)
2501 return 0;
2503 if (mw1->type != mw2->type)
2504 return mw1->type - mw2->type;
2506 if (mw1->flags != mw2->flags)
2507 return mw1->flags - mw2->flags;
2509 if (mw1->start_regno != mw2->start_regno)
2510 return mw1->start_regno - mw2->start_regno;
2512 if (mw1->end_regno != mw2->end_regno)
2513 return mw1->end_regno - mw2->end_regno;
2515 if (mw1->mw_reg != mw2->mw_reg)
2516 return mw1->mw_order - mw2->mw_order;
2518 return 0;
2522 /* Sort and compress a set of refs. */
2524 static void
2525 df_sort_and_compress_mws (VEC(df_mw_hardreg_ptr,stack) **mw_vec)
2527 unsigned int count;
2528 struct df_scan_problem_data *problem_data
2529 = (struct df_scan_problem_data *) df_scan->problem_data;
2530 unsigned int i;
2531 unsigned int dist = 0;
2533 count = VEC_length (df_mw_hardreg_ptr, *mw_vec);
2534 if (count < 2)
2535 return;
2536 else if (count == 2)
2538 struct df_mw_hardreg *m0 = VEC_index (df_mw_hardreg_ptr, *mw_vec, 0);
2539 struct df_mw_hardreg *m1 = VEC_index (df_mw_hardreg_ptr, *mw_vec, 1);
2540 if (df_mw_compare (&m0, &m1) > 0)
2542 struct df_mw_hardreg *tmp = VEC_index (df_mw_hardreg_ptr,
2543 *mw_vec, 0);
2544 VEC_replace (df_mw_hardreg_ptr, *mw_vec, 0,
2545 VEC_index (df_mw_hardreg_ptr, *mw_vec, 1));
2546 VEC_replace (df_mw_hardreg_ptr, *mw_vec, 1, tmp);
2549 else
2550 qsort (VEC_address (df_mw_hardreg_ptr, *mw_vec), count,
2551 sizeof (struct df_mw_hardreg *), df_mw_compare);
2553 for (i=0; i<count-dist; i++)
2555 /* Find the next ref that is not equal to the current ref. */
2556 while (i + dist + 1 < count
2557 && df_mw_equal_p (VEC_index (df_mw_hardreg_ptr, *mw_vec, i),
2558 VEC_index (df_mw_hardreg_ptr, *mw_vec,
2559 i + dist + 1)))
2561 pool_free (problem_data->mw_reg_pool,
2562 VEC_index (df_mw_hardreg_ptr, *mw_vec, i + dist + 1));
2563 dist++;
2565 /* Copy it down to the next position. */
2566 if (dist && i + dist + 1 < count)
2567 VEC_replace (df_mw_hardreg_ptr, *mw_vec, i + 1,
2568 VEC_index (df_mw_hardreg_ptr, *mw_vec, i + dist + 1));
2571 count -= dist;
2572 VEC_truncate (df_mw_hardreg_ptr, *mw_vec, count);
2576 /* Sort and remove duplicates from the COLLECTION_REC. */
2578 static void
2579 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2581 df_sort_and_compress_refs (&collection_rec->def_vec);
2582 df_sort_and_compress_refs (&collection_rec->use_vec);
2583 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2584 df_sort_and_compress_mws (&collection_rec->mw_vec);
2588 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2590 static void
2591 df_install_ref (df_ref this_ref,
2592 struct df_reg_info *reg_info,
2593 struct df_ref_info *ref_info,
2594 bool add_to_table)
2596 unsigned int regno = DF_REF_REGNO (this_ref);
2597 /* Add the ref to the reg_{def,use,eq_use} chain. */
2598 df_ref head = reg_info->reg_chain;
2600 reg_info->reg_chain = this_ref;
2601 reg_info->n_refs++;
2603 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2605 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2606 df->hard_regs_live_count[regno]++;
2609 gcc_assert (DF_REF_NEXT_REG (this_ref) == NULL
2610 && DF_REF_PREV_REG (this_ref) == NULL);
2612 DF_REF_NEXT_REG (this_ref) = head;
2614 /* We cannot actually link to the head of the chain. */
2615 DF_REF_PREV_REG (this_ref) = NULL;
2617 if (head)
2618 DF_REF_PREV_REG (head) = this_ref;
2620 if (add_to_table)
2622 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2623 df_check_and_grow_ref_info (ref_info, 1);
2624 DF_REF_ID (this_ref) = ref_info->table_size;
2625 /* Add the ref to the big array of defs. */
2626 ref_info->refs[ref_info->table_size] = this_ref;
2627 ref_info->table_size++;
2629 else
2630 DF_REF_ID (this_ref) = -1;
2632 ref_info->total_size++;
2636 /* This function takes one of the groups of refs (defs, uses or
2637 eq_uses) and installs the entire group into the insn. It also adds
2638 each of these refs into the appropriate chains. */
2640 static df_ref *
2641 df_install_refs (basic_block bb,
2642 VEC(df_ref,stack)* old_vec,
2643 struct df_reg_info **reg_info,
2644 struct df_ref_info *ref_info,
2645 bool is_notes)
2647 unsigned int count;
2649 count = VEC_length (df_ref, old_vec);
2650 if (count)
2652 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2653 bool add_to_table;
2654 df_ref this_ref;
2655 unsigned int ix;
2657 switch (ref_info->ref_order)
2659 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2660 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2661 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2662 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2663 add_to_table = true;
2664 break;
2665 case DF_REF_ORDER_UNORDERED:
2666 case DF_REF_ORDER_BY_REG:
2667 case DF_REF_ORDER_BY_INSN:
2668 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2669 add_to_table = !is_notes;
2670 break;
2671 default:
2672 add_to_table = false;
2673 break;
2676 /* Do not add if ref is not in the right blocks. */
2677 if (add_to_table && df->analyze_subset)
2678 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2680 for (ix = 0; VEC_iterate (df_ref, old_vec, ix, this_ref); ++ix)
2682 new_vec[ix] = this_ref;
2683 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2684 ref_info, add_to_table);
2687 new_vec[count] = NULL;
2688 return new_vec;
2690 else
2691 return df_null_ref_rec;
2695 /* This function takes the mws installs the entire group into the
2696 insn. */
2698 static struct df_mw_hardreg **
2699 df_install_mws (VEC(df_mw_hardreg_ptr,stack) *old_vec)
2701 unsigned int count;
2703 count = VEC_length (df_mw_hardreg_ptr, old_vec);
2704 if (count)
2706 struct df_mw_hardreg **new_vec
2707 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2708 memcpy (new_vec, VEC_address (df_mw_hardreg_ptr, old_vec),
2709 sizeof (struct df_mw_hardreg*) * count);
2710 new_vec[count] = NULL;
2711 return new_vec;
2713 else
2714 return df_null_mw_rec;
2718 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2719 chains and update other necessary information. */
2721 static void
2722 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2723 basic_block bb, rtx insn)
2725 if (insn)
2727 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2728 /* If there is a vector in the collection rec, add it to the
2729 insn. A null rec is a signal that the caller will handle the
2730 chain specially. */
2731 if (collection_rec->def_vec)
2733 df_scan_free_ref_vec (insn_rec->defs);
2734 insn_rec->defs
2735 = df_install_refs (bb, collection_rec->def_vec,
2736 df->def_regs,
2737 &df->def_info, false);
2739 if (collection_rec->use_vec)
2741 df_scan_free_ref_vec (insn_rec->uses);
2742 insn_rec->uses
2743 = df_install_refs (bb, collection_rec->use_vec,
2744 df->use_regs,
2745 &df->use_info, false);
2747 if (collection_rec->eq_use_vec)
2749 df_scan_free_ref_vec (insn_rec->eq_uses);
2750 insn_rec->eq_uses
2751 = df_install_refs (bb, collection_rec->eq_use_vec,
2752 df->eq_use_regs,
2753 &df->use_info, true);
2755 if (collection_rec->mw_vec)
2757 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2758 insn_rec->mw_hardregs
2759 = df_install_mws (collection_rec->mw_vec);
2762 else
2764 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2766 df_scan_free_ref_vec (bb_info->artificial_defs);
2767 bb_info->artificial_defs
2768 = df_install_refs (bb, collection_rec->def_vec,
2769 df->def_regs,
2770 &df->def_info, false);
2771 df_scan_free_ref_vec (bb_info->artificial_uses);
2772 bb_info->artificial_uses
2773 = df_install_refs (bb, collection_rec->use_vec,
2774 df->use_regs,
2775 &df->use_info, false);
2780 /* Allocate a ref and initialize its fields.
2782 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2783 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the fields
2784 if they were constants. Otherwise they should be -1 if those flags
2785 were set. */
2787 static df_ref
2788 df_ref_create_structure (enum df_ref_class cl,
2789 struct df_collection_rec *collection_rec,
2790 rtx reg, rtx *loc,
2791 basic_block bb, struct df_insn_info *info,
2792 enum df_ref_type ref_type,
2793 int ref_flags,
2794 int width, int offset, enum machine_mode mode)
2796 df_ref this_ref = NULL;
2797 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2798 struct df_scan_problem_data *problem_data
2799 = (struct df_scan_problem_data *) df_scan->problem_data;
2801 switch (cl)
2803 case DF_REF_BASE:
2804 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2805 gcc_assert (loc == NULL);
2806 break;
2808 case DF_REF_ARTIFICIAL:
2809 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2810 this_ref->artificial_ref.bb = bb;
2811 gcc_assert (loc == NULL);
2812 break;
2814 case DF_REF_REGULAR:
2815 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2816 this_ref->regular_ref.loc = loc;
2817 gcc_assert (loc);
2818 break;
2820 case DF_REF_EXTRACT:
2821 this_ref = (df_ref) pool_alloc (problem_data->ref_extract_pool);
2822 DF_REF_EXTRACT_WIDTH (this_ref) = width;
2823 DF_REF_EXTRACT_OFFSET (this_ref) = offset;
2824 DF_REF_EXTRACT_MODE (this_ref) = mode;
2825 this_ref->regular_ref.loc = loc;
2826 gcc_assert (loc);
2827 break;
2830 DF_REF_CLASS (this_ref) = cl;
2831 DF_REF_ID (this_ref) = -1;
2832 DF_REF_REG (this_ref) = reg;
2833 DF_REF_REGNO (this_ref) = regno;
2834 DF_REF_TYPE (this_ref) = ref_type;
2835 DF_REF_INSN_INFO (this_ref) = info;
2836 DF_REF_CHAIN (this_ref) = NULL;
2837 DF_REF_FLAGS (this_ref) = ref_flags;
2838 DF_REF_NEXT_REG (this_ref) = NULL;
2839 DF_REF_PREV_REG (this_ref) = NULL;
2840 DF_REF_ORDER (this_ref) = df->ref_order++;
2842 /* We need to clear this bit because fwprop, and in the future
2843 possibly other optimizations sometimes create new refs using ond
2844 refs as the model. */
2845 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2847 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2848 if ((regno < FIRST_PSEUDO_REGISTER)
2849 && (!DF_REF_IS_ARTIFICIAL (this_ref)))
2851 if (DF_REF_REG_DEF_P (this_ref))
2853 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2854 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2856 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2857 && (regno == FRAME_POINTER_REGNUM
2858 || regno == ARG_POINTER_REGNUM)))
2859 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2862 if (collection_rec)
2864 if (DF_REF_REG_DEF_P (this_ref))
2865 VEC_safe_push (df_ref, stack, collection_rec->def_vec, this_ref);
2866 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2867 VEC_safe_push (df_ref, stack, collection_rec->eq_use_vec, this_ref);
2868 else
2869 VEC_safe_push (df_ref, stack, collection_rec->use_vec, this_ref);
2872 return this_ref;
2876 /* Create new references of type DF_REF_TYPE for each part of register REG
2877 at address LOC within INSN of BB.
2879 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2880 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
2881 fields if they were constants. Otherwise they should be -1 if
2882 those flags were set. */
2885 static void
2886 df_ref_record (enum df_ref_class cl,
2887 struct df_collection_rec *collection_rec,
2888 rtx reg, rtx *loc,
2889 basic_block bb, struct df_insn_info *insn_info,
2890 enum df_ref_type ref_type,
2891 int ref_flags,
2892 int width, int offset, enum machine_mode mode)
2894 unsigned int regno;
2896 gcc_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2898 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2899 if (regno < FIRST_PSEUDO_REGISTER)
2901 struct df_mw_hardreg *hardreg = NULL;
2902 struct df_scan_problem_data *problem_data
2903 = (struct df_scan_problem_data *) df_scan->problem_data;
2904 unsigned int i;
2905 unsigned int endregno;
2906 df_ref ref;
2908 if (GET_CODE (reg) == SUBREG)
2910 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2911 SUBREG_BYTE (reg), GET_MODE (reg));
2912 endregno = regno + subreg_nregs (reg);
2914 else
2915 endregno = END_HARD_REGNO (reg);
2917 /* If this is a multiword hardreg, we create some extra
2918 datastructures that will enable us to easily build REG_DEAD
2919 and REG_UNUSED notes. */
2920 if ((endregno != regno + 1) && insn_info)
2922 /* Sets to a subreg of a multiword register are partial.
2923 Sets to a non-subreg of a multiword register are not. */
2924 if (GET_CODE (reg) == SUBREG)
2925 ref_flags |= DF_REF_PARTIAL;
2926 ref_flags |= DF_REF_MW_HARDREG;
2928 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2929 hardreg->type = ref_type;
2930 hardreg->flags = ref_flags;
2931 hardreg->mw_reg = reg;
2932 hardreg->start_regno = regno;
2933 hardreg->end_regno = endregno - 1;
2934 hardreg->mw_order = df->ref_order++;
2935 VEC_safe_push (df_mw_hardreg_ptr, stack, collection_rec->mw_vec,
2936 hardreg);
2939 for (i = regno; i < endregno; i++)
2941 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2942 bb, insn_info, ref_type, ref_flags,
2943 width, offset, mode);
2945 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2948 else
2950 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2951 ref_type, ref_flags, width, offset, mode);
2956 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2957 covered by the outer mode is smaller than that covered by the inner mode,
2958 is a read-modify-write operation.
2959 This function returns true iff the SUBREG X is such a SUBREG. */
2961 bool
2962 df_read_modify_subreg_p (rtx x)
2964 unsigned int isize, osize;
2965 if (GET_CODE (x) != SUBREG)
2966 return false;
2967 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2968 osize = GET_MODE_SIZE (GET_MODE (x));
2969 return isize > osize
2970 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2974 /* Process all the registers defined in the rtx, X.
2975 Autoincrement/decrement definitions will be picked up by
2976 df_uses_record. */
2978 static void
2979 df_def_record_1 (struct df_collection_rec *collection_rec,
2980 rtx x, basic_block bb, struct df_insn_info *insn_info,
2981 int flags)
2983 rtx *loc;
2984 rtx dst;
2985 int offset = -1;
2986 int width = -1;
2987 enum machine_mode mode = VOIDmode;
2988 enum df_ref_class cl = DF_REF_REGULAR;
2990 /* We may recursively call ourselves on EXPR_LIST when dealing with PARALLEL
2991 construct. */
2992 if (GET_CODE (x) == EXPR_LIST || GET_CODE (x) == CLOBBER)
2993 loc = &XEXP (x, 0);
2994 else
2995 loc = &SET_DEST (x);
2996 dst = *loc;
2998 /* It is legal to have a set destination be a parallel. */
2999 if (GET_CODE (dst) == PARALLEL)
3001 int i;
3003 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
3005 rtx temp = XVECEXP (dst, 0, i);
3006 if (GET_CODE (temp) == EXPR_LIST || GET_CODE (temp) == CLOBBER
3007 || GET_CODE (temp) == SET)
3008 df_def_record_1 (collection_rec,
3009 temp, bb, insn_info,
3010 GET_CODE (temp) == CLOBBER
3011 ? flags | DF_REF_MUST_CLOBBER : flags);
3013 return;
3016 if (GET_CODE (dst) == STRICT_LOW_PART)
3018 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
3020 loc = &XEXP (dst, 0);
3021 dst = *loc;
3024 if (GET_CODE (dst) == ZERO_EXTRACT)
3026 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
3028 if (CONST_INT_P (XEXP (dst, 1))
3029 && CONST_INT_P (XEXP (dst, 2)))
3031 width = INTVAL (XEXP (dst, 1));
3032 offset = INTVAL (XEXP (dst, 2));
3033 mode = GET_MODE (dst);
3034 cl = DF_REF_EXTRACT;
3037 loc = &XEXP (dst, 0);
3038 dst = *loc;
3041 /* At this point if we do not have a reg or a subreg, just return. */
3042 if (REG_P (dst))
3044 df_ref_record (cl, collection_rec,
3045 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
3046 width, offset, mode);
3048 /* We want to keep sp alive everywhere - by making all
3049 writes to sp also use of sp. */
3050 if (REGNO (dst) == STACK_POINTER_REGNUM)
3051 df_ref_record (DF_REF_BASE, collection_rec,
3052 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags,
3053 width, offset, mode);
3055 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
3057 if (df_read_modify_subreg_p (dst))
3058 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
3060 flags |= DF_REF_SUBREG;
3062 df_ref_record (cl, collection_rec,
3063 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
3064 width, offset, mode);
3069 /* Process all the registers defined in the pattern rtx, X. */
3071 static void
3072 df_defs_record (struct df_collection_rec *collection_rec,
3073 rtx x, basic_block bb, struct df_insn_info *insn_info,
3074 int flags)
3076 RTX_CODE code = GET_CODE (x);
3078 if (code == SET || code == CLOBBER)
3080 /* Mark the single def within the pattern. */
3081 int clobber_flags = flags;
3082 clobber_flags |= (code == CLOBBER) ? DF_REF_MUST_CLOBBER : 0;
3083 df_def_record_1 (collection_rec, x, bb, insn_info, clobber_flags);
3085 else if (code == COND_EXEC)
3087 df_defs_record (collection_rec, COND_EXEC_CODE (x),
3088 bb, insn_info, DF_REF_CONDITIONAL);
3090 else if (code == PARALLEL)
3092 int i;
3094 /* Mark the multiple defs within the pattern. */
3095 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3096 df_defs_record (collection_rec, XVECEXP (x, 0, i), bb, insn_info, flags);
3101 /* Process all the registers used in the rtx at address LOC.
3103 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
3104 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
3105 fields if they were constants. Otherwise they should be -1 if
3106 those flags were set. */
3108 static void
3109 df_uses_record (enum df_ref_class cl, struct df_collection_rec *collection_rec,
3110 rtx *loc, enum df_ref_type ref_type,
3111 basic_block bb, struct df_insn_info *insn_info,
3112 int flags,
3113 int width, int offset, enum machine_mode mode)
3115 RTX_CODE code;
3116 rtx x;
3118 retry:
3119 x = *loc;
3120 if (!x)
3121 return;
3122 code = GET_CODE (x);
3123 switch (code)
3125 case LABEL_REF:
3126 case SYMBOL_REF:
3127 case CONST_INT:
3128 case CONST:
3129 case CONST_DOUBLE:
3130 case CONST_FIXED:
3131 case CONST_VECTOR:
3132 case PC:
3133 case CC0:
3134 case ADDR_VEC:
3135 case ADDR_DIFF_VEC:
3136 return;
3138 case CLOBBER:
3139 /* If we are clobbering a MEM, mark any registers inside the address
3140 as being used. */
3141 if (MEM_P (XEXP (x, 0)))
3142 df_uses_record (cl, collection_rec,
3143 &XEXP (XEXP (x, 0), 0),
3144 DF_REF_REG_MEM_STORE,
3145 bb, insn_info,
3146 flags, width, offset, mode);
3148 /* If we're clobbering a REG then we have a def so ignore. */
3149 return;
3151 case MEM:
3152 df_uses_record (cl, collection_rec,
3153 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3154 bb, insn_info, flags & DF_REF_IN_NOTE,
3155 width, offset, mode);
3156 return;
3158 case SUBREG:
3159 /* While we're here, optimize this case. */
3160 flags |= DF_REF_PARTIAL;
3161 /* In case the SUBREG is not of a REG, do not optimize. */
3162 if (!REG_P (SUBREG_REG (x)))
3164 loc = &SUBREG_REG (x);
3165 df_uses_record (cl, collection_rec, loc, ref_type, bb, insn_info, flags,
3166 width, offset, mode);
3167 return;
3169 /* ... Fall through ... */
3171 case REG:
3172 df_ref_record (cl, collection_rec,
3173 x, loc, bb, insn_info,
3174 ref_type, flags,
3175 width, offset, mode);
3176 return;
3178 case SIGN_EXTRACT:
3179 case ZERO_EXTRACT:
3181 /* If the parameters to the zero or sign extract are
3182 constants, strip them off and recurse, otherwise there is
3183 no information that we can gain from this operation. */
3184 if (CONST_INT_P (XEXP (x, 1))
3185 && CONST_INT_P (XEXP (x, 2)))
3187 width = INTVAL (XEXP (x, 1));
3188 offset = INTVAL (XEXP (x, 2));
3189 mode = GET_MODE (x);
3191 if (code == ZERO_EXTRACT)
3192 flags |= DF_REF_ZERO_EXTRACT;
3193 else
3194 flags |= DF_REF_SIGN_EXTRACT;
3196 df_uses_record (DF_REF_EXTRACT, collection_rec,
3197 &XEXP (x, 0), ref_type, bb, insn_info, flags,
3198 width, offset, mode);
3199 return;
3202 break;
3204 case SET:
3206 rtx dst = SET_DEST (x);
3207 gcc_assert (!(flags & DF_REF_IN_NOTE));
3208 df_uses_record (cl, collection_rec,
3209 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags,
3210 width, offset, mode);
3212 switch (GET_CODE (dst))
3214 case SUBREG:
3215 if (df_read_modify_subreg_p (dst))
3217 df_uses_record (cl, collection_rec, &SUBREG_REG (dst),
3218 DF_REF_REG_USE, bb, insn_info,
3219 flags | DF_REF_READ_WRITE | DF_REF_SUBREG,
3220 width, offset, mode);
3221 break;
3223 /* Fall through. */
3224 case REG:
3225 case PARALLEL:
3226 case SCRATCH:
3227 case PC:
3228 case CC0:
3229 break;
3230 case MEM:
3231 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3232 DF_REF_REG_MEM_STORE, bb, insn_info, flags,
3233 width, offset, mode);
3234 break;
3235 case STRICT_LOW_PART:
3237 rtx *temp = &XEXP (dst, 0);
3238 /* A strict_low_part uses the whole REG and not just the
3239 SUBREG. */
3240 dst = XEXP (dst, 0);
3241 df_uses_record (cl, collection_rec,
3242 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3243 DF_REF_REG_USE, bb, insn_info,
3244 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART,
3245 width, offset, mode);
3247 break;
3248 case ZERO_EXTRACT:
3250 if (CONST_INT_P (XEXP (dst, 1))
3251 && CONST_INT_P (XEXP (dst, 2)))
3253 width = INTVAL (XEXP (dst, 1));
3254 offset = INTVAL (XEXP (dst, 2));
3255 mode = GET_MODE (dst);
3256 if (GET_CODE (XEXP (dst,0)) == MEM)
3258 /* Handle the case of zero_extract(mem(...)) in the set dest.
3259 This special case is allowed only if the mem is a single byte and
3260 is useful to set a bitfield in memory. */
3261 df_uses_record (DF_REF_EXTRACT, collection_rec, &XEXP (XEXP (dst,0), 0),
3262 DF_REF_REG_MEM_STORE, bb, insn_info,
3263 DF_REF_ZERO_EXTRACT,
3264 width, offset, mode);
3266 else
3268 df_uses_record (DF_REF_EXTRACT, collection_rec, &XEXP (dst, 0),
3269 DF_REF_REG_USE, bb, insn_info,
3270 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3271 width, offset, mode);
3274 else
3276 df_uses_record (cl, collection_rec, &XEXP (dst, 1),
3277 DF_REF_REG_USE, bb, insn_info, flags,
3278 width, offset, mode);
3279 df_uses_record (cl, collection_rec, &XEXP (dst, 2),
3280 DF_REF_REG_USE, bb, insn_info, flags,
3281 width, offset, mode);
3282 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3283 DF_REF_REG_USE, bb, insn_info,
3284 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3285 width, offset, mode);
3289 break;
3291 default:
3292 gcc_unreachable ();
3294 return;
3297 case RETURN:
3298 break;
3300 case ASM_OPERANDS:
3301 case UNSPEC_VOLATILE:
3302 case TRAP_IF:
3303 case ASM_INPUT:
3305 /* Traditional and volatile asm instructions must be
3306 considered to use and clobber all hard registers, all
3307 pseudo-registers and all of memory. So must TRAP_IF and
3308 UNSPEC_VOLATILE operations.
3310 Consider for instance a volatile asm that changes the fpu
3311 rounding mode. An insn should not be moved across this
3312 even if it only uses pseudo-regs because it might give an
3313 incorrectly rounded result.
3315 However, flow.c's liveness computation did *not* do this,
3316 giving the reasoning as " ?!? Unfortunately, marking all
3317 hard registers as live causes massive problems for the
3318 register allocator and marking all pseudos as live creates
3319 mountains of uninitialized variable warnings."
3321 In order to maintain the status quo with regard to liveness
3322 and uses, we do what flow.c did and just mark any regs we
3323 can find in ASM_OPERANDS as used. In global asm insns are
3324 scanned and regs_asm_clobbered is filled out.
3326 For all ASM_OPERANDS, we must traverse the vector of input
3327 operands. We can not just fall through here since then we
3328 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3329 which do not indicate traditional asms unlike their normal
3330 usage. */
3331 if (code == ASM_OPERANDS)
3333 int j;
3335 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3336 df_uses_record (cl, collection_rec, &ASM_OPERANDS_INPUT (x, j),
3337 DF_REF_REG_USE, bb, insn_info, flags,
3338 width, offset, mode);
3339 return;
3341 break;
3344 case VAR_LOCATION:
3345 df_uses_record (cl, collection_rec,
3346 &PAT_VAR_LOCATION_LOC (x),
3347 DF_REF_REG_USE, bb, insn_info,
3348 flags, width, offset, mode);
3349 return;
3351 case PRE_DEC:
3352 case POST_DEC:
3353 case PRE_INC:
3354 case POST_INC:
3355 case PRE_MODIFY:
3356 case POST_MODIFY:
3357 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3358 /* Catch the def of the register being modified. */
3359 df_ref_record (cl, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3360 bb, insn_info,
3361 DF_REF_REG_DEF,
3362 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY,
3363 width, offset, mode);
3365 /* ... Fall through to handle uses ... */
3367 default:
3368 break;
3371 /* Recursively scan the operands of this expression. */
3373 const char *fmt = GET_RTX_FORMAT (code);
3374 int i;
3376 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3378 if (fmt[i] == 'e')
3380 /* Tail recursive case: save a function call level. */
3381 if (i == 0)
3383 loc = &XEXP (x, 0);
3384 goto retry;
3386 df_uses_record (cl, collection_rec, &XEXP (x, i), ref_type,
3387 bb, insn_info, flags,
3388 width, offset, mode);
3390 else if (fmt[i] == 'E')
3392 int j;
3393 for (j = 0; j < XVECLEN (x, i); j++)
3394 df_uses_record (cl, collection_rec,
3395 &XVECEXP (x, i, j), ref_type,
3396 bb, insn_info, flags,
3397 width, offset, mode);
3402 return;
3406 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3408 static void
3409 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3411 unsigned int ix;
3412 df_ref ref;
3414 for (ix = 0; VEC_iterate (df_ref, collection_rec->def_vec, ix, ref); ++ix)
3416 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3418 int width = -1;
3419 int offset = -1;
3420 enum machine_mode mode = VOIDmode;
3421 df_ref use;
3423 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT))
3425 width = DF_REF_EXTRACT_WIDTH (ref);
3426 offset = DF_REF_EXTRACT_OFFSET (ref);
3427 mode = DF_REF_EXTRACT_MODE (ref);
3430 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3431 DF_REF_LOC (ref), DF_REF_BB (ref),
3432 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3433 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL,
3434 width, offset, mode);
3435 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3441 /* Get call's extra defs and uses. */
3443 static void
3444 df_get_call_refs (struct df_collection_rec * collection_rec,
3445 basic_block bb,
3446 struct df_insn_info *insn_info,
3447 int flags)
3449 rtx note;
3450 bitmap_iterator bi;
3451 unsigned int ui;
3452 bool is_sibling_call;
3453 unsigned int i;
3454 df_ref def;
3455 bitmap defs_generated = BITMAP_ALLOC (&df_bitmap_obstack);
3457 /* Do not generate clobbers for registers that are the result of the
3458 call. This causes ordering problems in the chain building code
3459 depending on which def is seen first. */
3460 for (i = 0; VEC_iterate (df_ref, collection_rec->def_vec, i, def); ++i)
3461 bitmap_set_bit (defs_generated, DF_REF_REGNO (def));
3463 /* Record the registers used to pass arguments, and explicitly
3464 noted as clobbered. */
3465 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3466 note = XEXP (note, 1))
3468 if (GET_CODE (XEXP (note, 0)) == USE)
3469 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (XEXP (note, 0), 0),
3470 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3471 VOIDmode);
3472 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3474 if (REG_P (XEXP (XEXP (note, 0), 0)))
3476 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3477 if (!bitmap_bit_p (defs_generated, regno))
3478 df_defs_record (collection_rec, XEXP (note, 0), bb,
3479 insn_info, flags);
3481 else
3482 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (note, 0),
3483 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3484 VOIDmode);
3488 /* The stack ptr is used (honorarily) by a CALL insn. */
3489 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[STACK_POINTER_REGNUM],
3490 NULL, bb, insn_info, DF_REF_REG_USE,
3491 DF_REF_CALL_STACK_USAGE | flags,
3492 -1, -1, VOIDmode);
3494 /* Calls may also reference any of the global registers,
3495 so they are recorded as used. */
3496 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3497 if (global_regs[i])
3499 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3500 NULL, bb, insn_info, DF_REF_REG_USE, flags, -1, -1,
3501 VOIDmode);
3502 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3503 NULL, bb, insn_info, DF_REF_REG_DEF, flags, -1, -1,
3504 VOIDmode);
3507 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3508 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, ui, bi)
3510 if (!global_regs[ui]
3511 && (!bitmap_bit_p (defs_generated, ui))
3512 && (!is_sibling_call
3513 || !bitmap_bit_p (df->exit_block_uses, ui)
3514 || refers_to_regno_p (ui, ui+1,
3515 crtl->return_rtx, NULL)))
3516 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[ui],
3517 NULL, bb, insn_info, DF_REF_REG_DEF,
3518 DF_REF_MAY_CLOBBER | flags,
3519 -1, -1, VOIDmode);
3522 BITMAP_FREE (defs_generated);
3523 return;
3526 /* Collect all refs in the INSN. This function is free of any
3527 side-effect - it will create and return a lists of df_ref's in the
3528 COLLECTION_REC without putting those refs into existing ref chains
3529 and reg chains. */
3531 static void
3532 df_insn_refs_collect (struct df_collection_rec* collection_rec,
3533 basic_block bb, struct df_insn_info *insn_info)
3535 rtx note;
3536 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3538 /* Clear out the collection record. */
3539 VEC_truncate (df_ref, collection_rec->def_vec, 0);
3540 VEC_truncate (df_ref, collection_rec->use_vec, 0);
3541 VEC_truncate (df_ref, collection_rec->eq_use_vec, 0);
3542 VEC_truncate (df_mw_hardreg_ptr, collection_rec->mw_vec, 0);
3544 /* Record register defs. */
3545 df_defs_record (collection_rec, PATTERN (insn_info->insn), bb, insn_info, 0);
3547 /* Process REG_EQUIV/REG_EQUAL notes. */
3548 for (note = REG_NOTES (insn_info->insn); note;
3549 note = XEXP (note, 1))
3551 switch (REG_NOTE_KIND (note))
3553 case REG_EQUIV:
3554 case REG_EQUAL:
3555 df_uses_record (DF_REF_REGULAR, collection_rec,
3556 &XEXP (note, 0), DF_REF_REG_USE,
3557 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
3558 break;
3559 case REG_NON_LOCAL_GOTO:
3560 /* The frame ptr is used by a non-local goto. */
3561 df_ref_record (DF_REF_BASE, collection_rec,
3562 regno_reg_rtx[FRAME_POINTER_REGNUM],
3563 NULL, bb, insn_info,
3564 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3565 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3566 df_ref_record (DF_REF_BASE, collection_rec,
3567 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3568 NULL, bb, insn_info,
3569 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3570 #endif
3571 break;
3572 default:
3573 break;
3577 if (CALL_P (insn_info->insn))
3578 df_get_call_refs (collection_rec, bb, insn_info,
3579 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3581 /* Record the register uses. */
3582 df_uses_record (DF_REF_REGULAR, collection_rec,
3583 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0,
3584 -1, -1, VOIDmode);
3586 /* DF_REF_CONDITIONAL needs corresponding USES. */
3587 if (is_cond_exec)
3588 df_get_conditional_uses (collection_rec);
3590 df_canonize_collection_rec (collection_rec);
3593 /* Recompute the luids for the insns in BB. */
3595 void
3596 df_recompute_luids (basic_block bb)
3598 rtx insn;
3599 int luid = 0;
3601 df_grow_insn_info ();
3603 /* Scan the block an insn at a time from beginning to end. */
3604 FOR_BB_INSNS (bb, insn)
3606 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3607 /* Inserting labels does not always trigger the incremental
3608 rescanning. */
3609 if (!insn_info)
3611 gcc_assert (!INSN_P (insn));
3612 insn_info = df_insn_create_insn_record (insn);
3615 DF_INSN_INFO_LUID (insn_info) = luid;
3616 if (INSN_P (insn))
3617 luid++;
3622 /* Collect all artificial refs at the block level for BB and add them
3623 to COLLECTION_REC. */
3625 static void
3626 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3628 VEC_truncate (df_ref, collection_rec->def_vec, 0);
3629 VEC_truncate (df_ref, collection_rec->use_vec, 0);
3630 VEC_truncate (df_ref, collection_rec->eq_use_vec, 0);
3631 VEC_truncate (df_mw_hardreg_ptr, collection_rec->mw_vec, 0);
3633 if (bb->index == ENTRY_BLOCK)
3635 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3636 return;
3638 else if (bb->index == EXIT_BLOCK)
3640 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3641 return;
3644 #ifdef EH_RETURN_DATA_REGNO
3645 if (bb_has_eh_pred (bb))
3647 unsigned int i;
3648 /* Mark the registers that will contain data for the handler. */
3649 for (i = 0; ; ++i)
3651 unsigned regno = EH_RETURN_DATA_REGNO (i);
3652 if (regno == INVALID_REGNUM)
3653 break;
3654 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3655 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1,
3656 VOIDmode);
3659 #endif
3661 /* Add the hard_frame_pointer if this block is the target of a
3662 non-local goto. */
3663 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3664 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3665 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1, VOIDmode);
3667 /* Add the artificial uses. */
3668 if (bb->index >= NUM_FIXED_BLOCKS)
3670 bitmap_iterator bi;
3671 unsigned int regno;
3672 bitmap au = bb_has_eh_pred (bb)
3673 ? df->eh_block_artificial_uses
3674 : df->regular_block_artificial_uses;
3676 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3678 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3679 bb, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3683 df_canonize_collection_rec (collection_rec);
3687 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3689 void
3690 df_bb_refs_record (int bb_index, bool scan_insns)
3692 basic_block bb = BASIC_BLOCK (bb_index);
3693 rtx insn;
3694 int luid = 0;
3695 struct df_scan_bb_info *bb_info;
3696 struct df_collection_rec collection_rec;
3698 if (!df)
3699 return;
3701 bb_info = df_scan_get_bb_info (bb_index);
3703 /* Need to make sure that there is a record in the basic block info. */
3704 if (!bb_info)
3706 bb_info = (struct df_scan_bb_info *) pool_alloc (df_scan->block_pool);
3707 df_scan_set_bb_info (bb_index, bb_info);
3708 bb_info->artificial_defs = NULL;
3709 bb_info->artificial_uses = NULL;
3712 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
3713 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
3714 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
3715 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
3717 if (scan_insns)
3718 /* Scan the block an insn at a time from beginning to end. */
3719 FOR_BB_INSNS (bb, insn)
3721 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3722 gcc_assert (!insn_info);
3724 insn_info = df_insn_create_insn_record (insn);
3725 if (INSN_P (insn))
3727 /* Record refs within INSN. */
3728 DF_INSN_INFO_LUID (insn_info) = luid++;
3729 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3730 df_refs_add_to_chains (&collection_rec, bb, insn);
3732 DF_INSN_INFO_LUID (insn_info) = luid;
3735 /* Other block level artificial refs */
3736 df_bb_refs_collect (&collection_rec, bb);
3737 df_refs_add_to_chains (&collection_rec, bb, NULL);
3739 VEC_free (df_ref, stack, collection_rec.def_vec);
3740 VEC_free (df_ref, stack, collection_rec.use_vec);
3741 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
3742 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
3744 /* Now that the block has been processed, set the block as dirty so
3745 LR and LIVE will get it processed. */
3746 df_set_bb_dirty (bb);
3750 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3751 block. */
3753 static void
3754 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3756 #ifdef EH_USES
3757 unsigned int i;
3758 #endif
3760 bitmap_clear (regular_block_artificial_uses);
3762 if (reload_completed)
3764 if (frame_pointer_needed)
3765 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3767 else
3768 /* Before reload, there are a few registers that must be forced
3769 live everywhere -- which might not already be the case for
3770 blocks within infinite loops. */
3772 /* Any reference to any pseudo before reload is a potential
3773 reference of the frame pointer. */
3774 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3776 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3777 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3778 #endif
3780 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3781 /* Pseudos with argument area equivalences may require
3782 reloading via the argument pointer. */
3783 if (fixed_regs[ARG_POINTER_REGNUM])
3784 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3785 #endif
3787 /* Any constant, or pseudo with constant equivalences, may
3788 require reloading from memory using the pic register. */
3789 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3790 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3791 bitmap_set_bit (regular_block_artificial_uses, PIC_OFFSET_TABLE_REGNUM);
3793 /* The all-important stack pointer must always be live. */
3794 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3796 #ifdef EH_USES
3797 /* EH_USES registers are used:
3798 1) at all insns that might throw (calls or with -fnon-call-exceptions
3799 trapping insns)
3800 2) in all EH edges
3801 3) to support backtraces and/or debugging, anywhere between their
3802 initialization and where they the saved registers are restored
3803 from them, including the cases where we don't reach the epilogue
3804 (noreturn call or infinite loop). */
3805 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3806 if (EH_USES (i))
3807 bitmap_set_bit (regular_block_artificial_uses, i);
3808 #endif
3812 /* Get the artificial use set for an eh block. */
3814 static void
3815 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3817 bitmap_clear (eh_block_artificial_uses);
3819 /* The following code (down thru the arg_pointer setting APPEARS
3820 to be necessary because there is nothing that actually
3821 describes what the exception handling code may actually need
3822 to keep alive. */
3823 if (reload_completed)
3825 if (frame_pointer_needed)
3827 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3828 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3829 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3830 #endif
3832 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3833 if (fixed_regs[ARG_POINTER_REGNUM])
3834 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3835 #endif
3841 /*----------------------------------------------------------------------------
3842 Specialized hard register scanning functions.
3843 ----------------------------------------------------------------------------*/
3846 /* Mark a register in SET. Hard registers in large modes get all
3847 of their component registers set as well. */
3849 static void
3850 df_mark_reg (rtx reg, void *vset)
3852 bitmap set = (bitmap) vset;
3853 int regno = REGNO (reg);
3855 gcc_assert (GET_MODE (reg) != BLKmode);
3857 if (regno < FIRST_PSEUDO_REGISTER)
3859 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3860 bitmap_set_range (set, regno, n);
3862 else
3863 bitmap_set_bit (set, regno);
3867 /* Set the bit for regs that are considered being defined at the entry. */
3869 static void
3870 df_get_entry_block_def_set (bitmap entry_block_defs)
3872 rtx r;
3873 int i;
3875 bitmap_clear (entry_block_defs);
3877 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3879 if (FUNCTION_ARG_REGNO_P (i))
3880 #ifdef INCOMING_REGNO
3881 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3882 #else
3883 bitmap_set_bit (entry_block_defs, i);
3884 #endif
3887 /* The always important stack pointer. */
3888 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3890 /* Once the prologue has been generated, all of these registers
3891 should just show up in the first regular block. */
3892 if (HAVE_prologue && epilogue_completed)
3894 /* Defs for the callee saved registers are inserted so that the
3895 pushes have some defining location. */
3896 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3897 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3898 bitmap_set_bit (entry_block_defs, i);
3901 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3902 if (r && REG_P (r))
3903 bitmap_set_bit (entry_block_defs, REGNO (r));
3905 /* If the function has an incoming STATIC_CHAIN, it has to show up
3906 in the entry def set. */
3907 r = targetm.calls.static_chain (current_function_decl, true);
3908 if (r && REG_P (r))
3909 bitmap_set_bit (entry_block_defs, REGNO (r));
3911 if ((!reload_completed) || frame_pointer_needed)
3913 /* Any reference to any pseudo before reload is a potential
3914 reference of the frame pointer. */
3915 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3916 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3917 /* If they are different, also mark the hard frame pointer as live. */
3918 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3919 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3920 #endif
3923 /* These registers are live everywhere. */
3924 if (!reload_completed)
3926 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3927 /* Pseudos with argument area equivalences may require
3928 reloading via the argument pointer. */
3929 if (fixed_regs[ARG_POINTER_REGNUM])
3930 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3931 #endif
3933 #ifdef PIC_OFFSET_TABLE_REGNUM
3934 /* Any constant, or pseudo with constant equivalences, may
3935 require reloading from memory using the pic register. */
3936 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3937 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3938 bitmap_set_bit (entry_block_defs, PIC_OFFSET_TABLE_REGNUM);
3939 #endif
3942 #ifdef INCOMING_RETURN_ADDR_RTX
3943 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3944 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3945 #endif
3947 targetm.live_on_entry (entry_block_defs);
3951 /* Return the (conservative) set of hard registers that are defined on
3952 entry to the function.
3953 It uses df->entry_block_defs to determine which register
3954 reference to include. */
3956 static void
3957 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3958 bitmap entry_block_defs)
3960 unsigned int i;
3961 bitmap_iterator bi;
3963 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3965 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3966 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0, -1, -1,
3967 VOIDmode);
3970 df_canonize_collection_rec (collection_rec);
3974 /* Record the (conservative) set of hard registers that are defined on
3975 entry to the function. */
3977 static void
3978 df_record_entry_block_defs (bitmap entry_block_defs)
3980 struct df_collection_rec collection_rec;
3981 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3982 collection_rec.def_vec = VEC_alloc (df_ref, stack, FIRST_PSEUDO_REGISTER);
3983 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3985 /* Process bb_refs chain */
3986 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3987 VEC_free (df_ref, stack, collection_rec.def_vec);
3991 /* Update the defs in the entry block. */
3993 void
3994 df_update_entry_block_defs (void)
3996 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
3997 bool changed = false;
3999 df_get_entry_block_def_set (refs);
4000 if (df->entry_block_defs)
4002 if (!bitmap_equal_p (df->entry_block_defs, refs))
4004 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
4005 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
4006 df_ref_chain_delete (bb_info->artificial_defs);
4007 bb_info->artificial_defs = NULL;
4008 changed = true;
4011 else
4013 struct df_scan_problem_data *problem_data
4014 = (struct df_scan_problem_data *) df_scan->problem_data;
4015 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4016 changed = true;
4019 if (changed)
4021 df_record_entry_block_defs (refs);
4022 bitmap_copy (df->entry_block_defs, refs);
4023 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
4025 BITMAP_FREE (refs);
4029 /* Set the bit for regs that are considered being used at the exit. */
4031 static void
4032 df_get_exit_block_use_set (bitmap exit_block_uses)
4034 unsigned int i;
4036 bitmap_clear (exit_block_uses);
4038 /* Stack pointer is always live at the exit. */
4039 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
4041 /* Mark the frame pointer if needed at the end of the function.
4042 If we end up eliminating it, it will be removed from the live
4043 list of each basic block by reload. */
4045 if ((!reload_completed) || frame_pointer_needed)
4047 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
4048 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4049 /* If they are different, also mark the hard frame pointer as live. */
4050 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
4051 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
4052 #endif
4055 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
4056 /* Many architectures have a GP register even without flag_pic.
4057 Assume the pic register is not in use, or will be handled by
4058 other means, if it is not fixed. */
4059 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
4060 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
4061 bitmap_set_bit (exit_block_uses, PIC_OFFSET_TABLE_REGNUM);
4062 #endif
4064 /* Mark all global registers, and all registers used by the
4065 epilogue as being live at the end of the function since they
4066 may be referenced by our caller. */
4067 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4068 if (global_regs[i] || EPILOGUE_USES (i))
4069 bitmap_set_bit (exit_block_uses, i);
4071 if (HAVE_epilogue && epilogue_completed)
4073 /* Mark all call-saved registers that we actually used. */
4074 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4075 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
4076 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
4077 bitmap_set_bit (exit_block_uses, i);
4080 #ifdef EH_RETURN_DATA_REGNO
4081 /* Mark the registers that will contain data for the handler. */
4082 if (reload_completed && crtl->calls_eh_return)
4083 for (i = 0; ; ++i)
4085 unsigned regno = EH_RETURN_DATA_REGNO (i);
4086 if (regno == INVALID_REGNUM)
4087 break;
4088 bitmap_set_bit (exit_block_uses, regno);
4090 #endif
4092 #ifdef EH_RETURN_STACKADJ_RTX
4093 if ((!HAVE_epilogue || ! epilogue_completed)
4094 && crtl->calls_eh_return)
4096 rtx tmp = EH_RETURN_STACKADJ_RTX;
4097 if (tmp && REG_P (tmp))
4098 df_mark_reg (tmp, exit_block_uses);
4100 #endif
4102 #ifdef EH_RETURN_HANDLER_RTX
4103 if ((!HAVE_epilogue || ! epilogue_completed)
4104 && crtl->calls_eh_return)
4106 rtx tmp = EH_RETURN_HANDLER_RTX;
4107 if (tmp && REG_P (tmp))
4108 df_mark_reg (tmp, exit_block_uses);
4110 #endif
4112 /* Mark function return value. */
4113 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4117 /* Return the refs of hard registers that are used in the exit block.
4118 It uses df->exit_block_uses to determine register to include. */
4120 static void
4121 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4123 unsigned int i;
4124 bitmap_iterator bi;
4126 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4127 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4128 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4130 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4131 /* It is deliberate that this is not put in the exit block uses but
4132 I do not know why. */
4133 if (reload_completed
4134 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4135 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4136 && fixed_regs[ARG_POINTER_REGNUM])
4137 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4138 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4139 #endif
4141 df_canonize_collection_rec (collection_rec);
4145 /* Record the set of hard registers that are used in the exit block.
4146 It uses df->exit_block_uses to determine which bit to include. */
4148 static void
4149 df_record_exit_block_uses (bitmap exit_block_uses)
4151 struct df_collection_rec collection_rec;
4152 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4153 collection_rec.use_vec = VEC_alloc (df_ref, stack, FIRST_PSEUDO_REGISTER);
4155 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4157 /* Process bb_refs chain */
4158 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4159 VEC_free (df_ref, stack, collection_rec.use_vec);
4163 /* Update the uses in the exit block. */
4165 void
4166 df_update_exit_block_uses (void)
4168 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
4169 bool changed = false;
4171 df_get_exit_block_use_set (refs);
4172 if (df->exit_block_uses)
4174 if (!bitmap_equal_p (df->exit_block_uses, refs))
4176 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4177 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4178 df_ref_chain_delete (bb_info->artificial_uses);
4179 bb_info->artificial_uses = NULL;
4180 changed = true;
4183 else
4185 struct df_scan_problem_data *problem_data
4186 = (struct df_scan_problem_data *) df_scan->problem_data;
4187 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4188 changed = true;
4191 if (changed)
4193 df_record_exit_block_uses (refs);
4194 bitmap_copy (df->exit_block_uses, refs);
4195 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4197 BITMAP_FREE (refs);
4200 static bool initialized = false;
4203 /* Initialize some platform specific structures. */
4205 void
4206 df_hard_reg_init (void)
4208 #ifdef ELIMINABLE_REGS
4209 int i;
4210 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4211 #endif
4212 if (initialized)
4213 return;
4215 /* Record which registers will be eliminated. We use this in
4216 mark_used_regs. */
4217 CLEAR_HARD_REG_SET (elim_reg_set);
4219 #ifdef ELIMINABLE_REGS
4220 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4221 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4222 #else
4223 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4224 #endif
4226 initialized = true;
4230 /* Recompute the parts of scanning that are based on regs_ever_live
4231 because something changed in that array. */
4233 void
4234 df_update_entry_exit_and_calls (void)
4236 basic_block bb;
4238 df_update_entry_block_defs ();
4239 df_update_exit_block_uses ();
4241 /* The call insns need to be rescanned because there may be changes
4242 in the set of registers clobbered across the call. */
4243 FOR_EACH_BB (bb)
4245 rtx insn;
4246 FOR_BB_INSNS (bb, insn)
4248 if (INSN_P (insn) && CALL_P (insn))
4249 df_insn_rescan (insn);
4255 /* Return true if hard REG is actually used in the some instruction.
4256 There are a fair number of conditions that affect the setting of
4257 this array. See the comment in df.h for df->hard_regs_live_count
4258 for the conditions that this array is set. */
4260 bool
4261 df_hard_reg_used_p (unsigned int reg)
4263 return df->hard_regs_live_count[reg] != 0;
4267 /* A count of the number of times REG is actually used in the some
4268 instruction. There are a fair number of conditions that affect the
4269 setting of this array. See the comment in df.h for
4270 df->hard_regs_live_count for the conditions that this array is
4271 set. */
4274 unsigned int
4275 df_hard_reg_used_count (unsigned int reg)
4277 return df->hard_regs_live_count[reg];
4281 /* Get the value of regs_ever_live[REGNO]. */
4283 bool
4284 df_regs_ever_live_p (unsigned int regno)
4286 return regs_ever_live[regno];
4290 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4291 to change, schedule that change for the next update. */
4293 void
4294 df_set_regs_ever_live (unsigned int regno, bool value)
4296 if (regs_ever_live[regno] == value)
4297 return;
4299 regs_ever_live[regno] = value;
4300 if (df)
4301 df->redo_entry_and_exit = true;
4305 /* Compute "regs_ever_live" information from the underlying df
4306 information. Set the vector to all false if RESET. */
4308 void
4309 df_compute_regs_ever_live (bool reset)
4311 unsigned int i;
4312 bool changed = df->redo_entry_and_exit;
4314 if (reset)
4315 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4317 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4318 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4320 regs_ever_live[i] = true;
4321 changed = true;
4323 if (changed)
4324 df_update_entry_exit_and_calls ();
4325 df->redo_entry_and_exit = false;
4329 /*----------------------------------------------------------------------------
4330 Dataflow ref information verification functions.
4332 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4333 df_reg_chain_verify_unmarked (refs)
4334 df_refs_verify (VEC(stack,df_ref)*, ref*, bool)
4335 df_mws_verify (mw*, mw*, bool)
4336 df_insn_refs_verify (collection_rec, bb, insn, bool)
4337 df_bb_refs_verify (bb, refs, bool)
4338 df_bb_verify (bb)
4339 df_exit_block_bitmap_verify (bool)
4340 df_entry_block_bitmap_verify (bool)
4341 df_scan_verify ()
4342 ----------------------------------------------------------------------------*/
4345 /* Mark all refs in the reg chain. Verify that all of the registers
4346 are in the correct chain. */
4348 static unsigned int
4349 df_reg_chain_mark (df_ref refs, unsigned int regno,
4350 bool is_def, bool is_eq_use)
4352 unsigned int count = 0;
4353 df_ref ref;
4354 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4356 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4358 /* If there are no def-use or use-def chains, make sure that all
4359 of the chains are clear. */
4360 if (!df_chain)
4361 gcc_assert (!DF_REF_CHAIN (ref));
4363 /* Check to make sure the ref is in the correct chain. */
4364 gcc_assert (DF_REF_REGNO (ref) == regno);
4365 if (is_def)
4366 gcc_assert (DF_REF_REG_DEF_P (ref));
4367 else
4368 gcc_assert (!DF_REF_REG_DEF_P (ref));
4370 if (is_eq_use)
4371 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4372 else
4373 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4375 if (DF_REF_NEXT_REG (ref))
4376 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4377 count++;
4378 DF_REF_REG_MARK (ref);
4380 return count;
4384 /* Verify that all of the registers in the chain are unmarked. */
4386 static void
4387 df_reg_chain_verify_unmarked (df_ref refs)
4389 df_ref ref;
4390 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4391 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4395 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4397 static bool
4398 df_refs_verify (VEC(df_ref,stack) *new_rec, df_ref *old_rec,
4399 bool abort_if_fail)
4401 unsigned int ix;
4402 df_ref new_ref;
4404 for (ix = 0; VEC_iterate (df_ref, new_rec, ix, new_ref); ++ix)
4406 if (*old_rec == NULL || !df_ref_equal_p (new_ref, *old_rec))
4408 if (abort_if_fail)
4409 gcc_assert (0);
4410 else
4411 return false;
4414 /* Abort if fail is called from the function level verifier. If
4415 that is the context, mark this reg as being seem. */
4416 if (abort_if_fail)
4418 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4419 DF_REF_REG_UNMARK (*old_rec);
4422 old_rec++;
4425 if (abort_if_fail)
4426 gcc_assert (*old_rec == NULL);
4427 else
4428 return *old_rec == NULL;
4429 return false;
4433 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4435 static bool
4436 df_mws_verify (VEC(df_mw_hardreg_ptr,stack) *new_rec,
4437 struct df_mw_hardreg **old_rec,
4438 bool abort_if_fail)
4440 unsigned int ix;
4441 struct df_mw_hardreg *new_reg;
4443 for (ix = 0; VEC_iterate (df_mw_hardreg_ptr, new_rec, ix, new_reg); ++ix)
4445 if (*old_rec == NULL || !df_mw_equal_p (new_reg, *old_rec))
4447 if (abort_if_fail)
4448 gcc_assert (0);
4449 else
4450 return false;
4452 old_rec++;
4455 if (abort_if_fail)
4456 gcc_assert (*old_rec == NULL);
4457 else
4458 return *old_rec == NULL;
4459 return false;
4463 /* Return true if the existing insn refs information is complete and
4464 correct. Otherwise (i.e. if there's any missing or extra refs),
4465 return the correct df_ref chain in REFS_RETURN.
4467 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4468 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4469 verification mode instead of the whole function, so unmark
4470 everything.
4472 If ABORT_IF_FAIL is set, this function never returns false. */
4474 static bool
4475 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4476 basic_block bb,
4477 rtx insn,
4478 bool abort_if_fail)
4480 bool ret1, ret2, ret3, ret4;
4481 unsigned int uid = INSN_UID (insn);
4482 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4484 df_insn_refs_collect (collection_rec, bb, insn_info);
4486 if (!DF_INSN_UID_DEFS (uid))
4488 /* The insn_rec was created but it was never filled out. */
4489 if (abort_if_fail)
4490 gcc_assert (0);
4491 else
4492 return false;
4495 /* Unfortunately we cannot opt out early if one of these is not
4496 right because the marks will not get cleared. */
4497 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4498 abort_if_fail);
4499 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4500 abort_if_fail);
4501 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4502 abort_if_fail);
4503 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4504 abort_if_fail);
4505 return (ret1 && ret2 && ret3 && ret4);
4509 /* Return true if all refs in the basic block are correct and complete.
4510 Due to df_ref_chain_verify, it will cause all refs
4511 that are verified to have DF_REF_MARK bit set. */
4513 static bool
4514 df_bb_verify (basic_block bb)
4516 rtx insn;
4517 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4518 struct df_collection_rec collection_rec;
4520 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4521 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
4522 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
4523 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
4524 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
4526 gcc_assert (bb_info);
4528 /* Scan the block, one insn at a time, from beginning to end. */
4529 FOR_BB_INSNS_REVERSE (bb, insn)
4531 if (!INSN_P (insn))
4532 continue;
4533 df_insn_refs_verify (&collection_rec, bb, insn, true);
4534 df_free_collection_rec (&collection_rec);
4537 /* Do the artificial defs and uses. */
4538 df_bb_refs_collect (&collection_rec, bb);
4539 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4540 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4541 df_free_collection_rec (&collection_rec);
4543 return true;
4547 /* Returns true if the entry block has correct and complete df_ref set.
4548 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4550 static bool
4551 df_entry_block_bitmap_verify (bool abort_if_fail)
4553 bitmap entry_block_defs = BITMAP_ALLOC (&df_bitmap_obstack);
4554 bool is_eq;
4556 df_get_entry_block_def_set (entry_block_defs);
4558 is_eq = bitmap_equal_p (entry_block_defs, df->entry_block_defs);
4560 if (!is_eq && abort_if_fail)
4562 print_current_pass (stderr);
4563 fprintf (stderr, "entry_block_defs = ");
4564 df_print_regset (stderr, entry_block_defs);
4565 fprintf (stderr, "df->entry_block_defs = ");
4566 df_print_regset (stderr, df->entry_block_defs);
4567 gcc_assert (0);
4570 BITMAP_FREE (entry_block_defs);
4572 return is_eq;
4576 /* Returns true if the exit block has correct and complete df_ref set.
4577 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4579 static bool
4580 df_exit_block_bitmap_verify (bool abort_if_fail)
4582 bitmap exit_block_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4583 bool is_eq;
4585 df_get_exit_block_use_set (exit_block_uses);
4587 is_eq = bitmap_equal_p (exit_block_uses, df->exit_block_uses);
4589 if (!is_eq && abort_if_fail)
4591 print_current_pass (stderr);
4592 fprintf (stderr, "exit_block_uses = ");
4593 df_print_regset (stderr, exit_block_uses);
4594 fprintf (stderr, "df->exit_block_uses = ");
4595 df_print_regset (stderr, df->exit_block_uses);
4596 gcc_assert (0);
4599 BITMAP_FREE (exit_block_uses);
4601 return is_eq;
4605 /* Return true if df_ref information for all insns in all blocks are
4606 correct and complete. */
4608 void
4609 df_scan_verify (void)
4611 unsigned int i;
4612 basic_block bb;
4613 bitmap regular_block_artificial_uses;
4614 bitmap eh_block_artificial_uses;
4616 if (!df)
4617 return;
4619 /* Verification is a 4 step process. */
4621 /* (1) All of the refs are marked by going thru the reg chains. */
4622 for (i = 0; i < DF_REG_SIZE (df); i++)
4624 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4625 == DF_REG_DEF_COUNT(i));
4626 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4627 == DF_REG_USE_COUNT(i));
4628 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4629 == DF_REG_EQ_USE_COUNT(i));
4632 /* (2) There are various bitmaps whose value may change over the
4633 course of the compilation. This step recomputes them to make
4634 sure that they have not slipped out of date. */
4635 regular_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4636 eh_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4638 df_get_regular_block_artificial_uses (regular_block_artificial_uses);
4639 df_get_eh_block_artificial_uses (eh_block_artificial_uses);
4641 bitmap_ior_into (eh_block_artificial_uses,
4642 regular_block_artificial_uses);
4644 /* Check artificial_uses bitmaps didn't change. */
4645 gcc_assert (bitmap_equal_p (regular_block_artificial_uses,
4646 df->regular_block_artificial_uses));
4647 gcc_assert (bitmap_equal_p (eh_block_artificial_uses,
4648 df->eh_block_artificial_uses));
4650 BITMAP_FREE (regular_block_artificial_uses);
4651 BITMAP_FREE (eh_block_artificial_uses);
4653 /* Verify entry block and exit block. These only verify the bitmaps,
4654 the refs are verified in df_bb_verify. */
4655 df_entry_block_bitmap_verify (true);
4656 df_exit_block_bitmap_verify (true);
4658 /* (3) All of the insns in all of the blocks are traversed and the
4659 marks are cleared both in the artificial refs attached to the
4660 blocks and the real refs inside the insns. It is a failure to
4661 clear a mark that has not been set as this means that the ref in
4662 the block or insn was not in the reg chain. */
4664 FOR_ALL_BB (bb)
4665 df_bb_verify (bb);
4667 /* (4) See if all reg chains are traversed a second time. This time
4668 a check is made that the marks are clear. A set mark would be a
4669 from a reg that is not in any insn or basic block. */
4671 for (i = 0; i < DF_REG_SIZE (df); i++)
4673 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4674 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4675 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));