* events.c (hash_param_callback): Read and pass parameter type.
[official-gcc.git] / gcc / df-scan.c
blob101234b55bc37b785794080d253a928e28bcd2d9
1 /* Scanning of rtl for dataflow analysis.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
4 Originally contributed by Michael P. Hayes
5 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
6 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
7 and Kenneth Zadeck (zadeck@naturalbridge.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "regs.h"
35 #include "output.h"
36 #include "alloc-pool.h"
37 #include "flags.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "sbitmap.h"
41 #include "bitmap.h"
42 #include "timevar.h"
43 #include "tree.h"
44 #include "target.h"
45 #include "target-def.h"
46 #include "df.h"
47 #include "tree-pass.h"
49 DEF_VEC_P(df_ref);
50 DEF_VEC_ALLOC_P_STACK(df_ref);
52 #define VEC_df_ref_stack_alloc(alloc) VEC_stack_alloc (df_ref, alloc)
54 typedef struct df_mw_hardreg *df_mw_hardreg_ptr;
56 DEF_VEC_P(df_mw_hardreg_ptr);
57 DEF_VEC_ALLOC_P_STACK(df_mw_hardreg_ptr);
59 #define VEC_df_mw_hardreg_ptr_stack_alloc(alloc) \
60 VEC_stack_alloc (df_mw_hardreg_ptr, alloc)
62 #ifndef HAVE_epilogue
63 #define HAVE_epilogue 0
64 #endif
65 #ifndef HAVE_prologue
66 #define HAVE_prologue 0
67 #endif
68 #ifndef HAVE_sibcall_epilogue
69 #define HAVE_sibcall_epilogue 0
70 #endif
72 #ifndef EPILOGUE_USES
73 #define EPILOGUE_USES(REGNO) 0
74 #endif
76 /* The following two macros free the vecs that hold either the refs or
77 the mw refs. They are a little tricky because the vec has 0
78 elements is special and is not to be freed. */
79 #define df_scan_free_ref_vec(V) \
80 do { \
81 if (V && *V) \
82 free (V); \
83 } while (0)
85 #define df_scan_free_mws_vec(V) \
86 do { \
87 if (V && *V) \
88 free (V); \
89 } while (0)
91 /* The set of hard registers in eliminables[i].from. */
93 static HARD_REG_SET elim_reg_set;
95 /* Initialize ur_in and ur_out as if all hard registers were partially
96 available. */
98 struct df_collection_rec
100 VEC(df_ref,stack) *def_vec;
101 VEC(df_ref,stack) *use_vec;
102 VEC(df_ref,stack) *eq_use_vec;
103 VEC(df_mw_hardreg_ptr,stack) *mw_vec;
106 static df_ref df_null_ref_rec[1];
107 static struct df_mw_hardreg * df_null_mw_rec[1];
109 static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
110 rtx, rtx *,
111 basic_block, struct df_insn_info *,
112 enum df_ref_type, int ref_flags,
113 int, int, enum machine_mode);
114 static void df_def_record_1 (struct df_collection_rec *, rtx,
115 basic_block, struct df_insn_info *,
116 int ref_flags);
117 static void df_defs_record (struct df_collection_rec *, rtx,
118 basic_block, struct df_insn_info *,
119 int ref_flags);
120 static void df_uses_record (enum df_ref_class, struct df_collection_rec *,
121 rtx *, enum df_ref_type,
122 basic_block, struct df_insn_info *,
123 int ref_flags,
124 int, int, enum machine_mode);
126 static df_ref df_ref_create_structure (enum df_ref_class,
127 struct df_collection_rec *, rtx, rtx *,
128 basic_block, struct df_insn_info *,
129 enum df_ref_type, int ref_flags,
130 int, int, enum machine_mode);
132 static void df_insn_refs_collect (struct df_collection_rec*,
133 basic_block, struct df_insn_info *);
134 static void df_canonize_collection_rec (struct df_collection_rec *);
136 static void df_get_regular_block_artificial_uses (bitmap);
137 static void df_get_eh_block_artificial_uses (bitmap);
139 static void df_record_entry_block_defs (bitmap);
140 static void df_record_exit_block_uses (bitmap);
141 static void df_get_exit_block_use_set (bitmap);
142 static void df_get_entry_block_def_set (bitmap);
143 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
144 static void df_ref_chain_delete_du_chain (df_ref *);
145 static void df_ref_chain_delete (df_ref *);
147 static void df_refs_add_to_chains (struct df_collection_rec *,
148 basic_block, rtx);
150 static bool df_insn_refs_verify (struct df_collection_rec *, basic_block, rtx, bool);
151 static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
152 static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
153 static void df_install_ref (df_ref, struct df_reg_info *,
154 struct df_ref_info *, bool);
156 static int df_ref_compare (const void *, const void *);
157 static int df_mw_compare (const void *, const void *);
159 /* Indexed by hardware reg number, is true if that register is ever
160 used in the current function.
162 In df-scan.c, this is set up to record the hard regs used
163 explicitly. Reload adds in the hard regs used for holding pseudo
164 regs. Final uses it to generate the code in the function prologue
165 and epilogue to save and restore registers as needed. */
167 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
169 /*----------------------------------------------------------------------------
170 SCANNING DATAFLOW PROBLEM
172 There are several ways in which scanning looks just like the other
173 dataflow problems. It shares the all the mechanisms for local info
174 as well as basic block info. Where it differs is when and how often
175 it gets run. It also has no need for the iterative solver.
176 ----------------------------------------------------------------------------*/
178 /* Problem data for the scanning dataflow function. */
179 struct df_scan_problem_data
181 alloc_pool ref_base_pool;
182 alloc_pool ref_artificial_pool;
183 alloc_pool ref_regular_pool;
184 alloc_pool ref_extract_pool;
185 alloc_pool insn_pool;
186 alloc_pool reg_pool;
187 alloc_pool mw_reg_pool;
188 bitmap_obstack reg_bitmaps;
189 bitmap_obstack insn_bitmaps;
192 typedef struct df_scan_bb_info *df_scan_bb_info_t;
195 /* Internal function to shut down the scanning problem. */
196 static void
197 df_scan_free_internal (void)
199 struct df_scan_problem_data *problem_data
200 = (struct df_scan_problem_data *) df_scan->problem_data;
201 unsigned int i;
202 basic_block bb;
204 /* The vectors that hold the refs are not pool allocated because
205 they come in many sizes. This makes them impossible to delete
206 all at once. */
207 for (i = 0; i < DF_INSN_SIZE(); i++)
209 struct df_insn_info *insn_info = DF_INSN_UID_GET(i);
210 /* Skip the insns that have no insn_info or have been
211 deleted. */
212 if (insn_info)
214 df_scan_free_ref_vec (insn_info->defs);
215 df_scan_free_ref_vec (insn_info->uses);
216 df_scan_free_ref_vec (insn_info->eq_uses);
217 df_scan_free_mws_vec (insn_info->mw_hardregs);
221 FOR_ALL_BB (bb)
223 unsigned int bb_index = bb->index;
224 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
225 if (bb_info)
227 df_scan_free_ref_vec (bb_info->artificial_defs);
228 df_scan_free_ref_vec (bb_info->artificial_uses);
232 free (df->def_info.refs);
233 free (df->def_info.begin);
234 free (df->def_info.count);
235 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
237 free (df->use_info.refs);
238 free (df->use_info.begin);
239 free (df->use_info.count);
240 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
242 free (df->def_regs);
243 df->def_regs = NULL;
244 free (df->use_regs);
245 df->use_regs = NULL;
246 free (df->eq_use_regs);
247 df->eq_use_regs = NULL;
248 df->regs_size = 0;
249 DF_REG_SIZE(df) = 0;
251 free (df->insns);
252 df->insns = NULL;
253 DF_INSN_SIZE () = 0;
255 free (df_scan->block_info);
256 df_scan->block_info = NULL;
257 df_scan->block_info_size = 0;
259 BITMAP_FREE (df->hardware_regs_used);
260 BITMAP_FREE (df->regular_block_artificial_uses);
261 BITMAP_FREE (df->eh_block_artificial_uses);
262 BITMAP_FREE (df->entry_block_defs);
263 BITMAP_FREE (df->exit_block_uses);
264 BITMAP_FREE (df->insns_to_delete);
265 BITMAP_FREE (df->insns_to_rescan);
266 BITMAP_FREE (df->insns_to_notes_rescan);
268 free_alloc_pool (df_scan->block_pool);
269 free_alloc_pool (problem_data->ref_base_pool);
270 free_alloc_pool (problem_data->ref_artificial_pool);
271 free_alloc_pool (problem_data->ref_regular_pool);
272 free_alloc_pool (problem_data->ref_extract_pool);
273 free_alloc_pool (problem_data->insn_pool);
274 free_alloc_pool (problem_data->reg_pool);
275 free_alloc_pool (problem_data->mw_reg_pool);
276 bitmap_obstack_release (&problem_data->reg_bitmaps);
277 bitmap_obstack_release (&problem_data->insn_bitmaps);
278 free (df_scan->problem_data);
282 /* Set basic block info. */
284 static void
285 df_scan_set_bb_info (unsigned int index,
286 struct df_scan_bb_info *bb_info)
288 gcc_assert (df_scan);
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 df_set_bb_dirty (bb);
834 return ref;
839 /*----------------------------------------------------------------------------
840 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
841 ----------------------------------------------------------------------------*/
843 static void
844 df_free_ref (df_ref ref)
846 struct df_scan_problem_data *problem_data
847 = (struct df_scan_problem_data *) df_scan->problem_data;
849 switch (DF_REF_CLASS (ref))
851 case DF_REF_BASE:
852 pool_free (problem_data->ref_base_pool, ref);
853 break;
855 case DF_REF_ARTIFICIAL:
856 pool_free (problem_data->ref_artificial_pool, ref);
857 break;
859 case DF_REF_REGULAR:
860 pool_free (problem_data->ref_regular_pool, ref);
861 break;
863 case DF_REF_EXTRACT:
864 pool_free (problem_data->ref_extract_pool, ref);
865 break;
870 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
871 Also delete the def-use or use-def chain if it exists. */
873 static void
874 df_reg_chain_unlink (df_ref ref)
876 df_ref next = DF_REF_NEXT_REG (ref);
877 df_ref prev = DF_REF_PREV_REG (ref);
878 int id = DF_REF_ID (ref);
879 struct df_reg_info *reg_info;
880 df_ref *refs = NULL;
882 if (DF_REF_REG_DEF_P (ref))
884 int regno = DF_REF_REGNO (ref);
885 reg_info = DF_REG_DEF_GET (regno);
886 refs = df->def_info.refs;
888 else
890 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
892 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
893 switch (df->use_info.ref_order)
895 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
896 case DF_REF_ORDER_BY_REG_WITH_NOTES:
897 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
898 refs = df->use_info.refs;
899 break;
900 default:
901 break;
904 else
906 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
907 refs = df->use_info.refs;
911 if (refs)
913 if (df->analyze_subset)
915 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
916 refs[id] = NULL;
918 else
919 refs[id] = NULL;
922 /* Delete any def-use or use-def chains that start here. It is
923 possible that there is trash in this field. This happens for
924 insns that have been deleted when rescanning has been deferred
925 and the chain problem has also been deleted. The chain tear down
926 code skips deleted insns. */
927 if (df_chain && DF_REF_CHAIN (ref))
928 df_chain_unlink (ref);
930 reg_info->n_refs--;
931 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
933 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
934 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
937 /* Unlink from the reg chain. If there is no prev, this is the
938 first of the list. If not, just join the next and prev. */
939 if (prev)
940 DF_REF_NEXT_REG (prev) = next;
941 else
943 gcc_assert (reg_info->reg_chain == ref);
944 reg_info->reg_chain = next;
946 if (next)
947 DF_REF_PREV_REG (next) = prev;
949 df_free_ref (ref);
953 /* Remove REF from VEC. */
955 static void
956 df_ref_compress_rec (df_ref **vec_ptr, df_ref ref)
958 df_ref *vec = *vec_ptr;
960 if (vec[1])
962 while (*vec && *vec != ref)
963 vec++;
965 while (*vec)
967 *vec = *(vec+1);
968 vec++;
971 else
973 free (vec);
974 *vec_ptr = df_null_ref_rec;
979 /* Unlink REF from all def-use/use-def chains, etc. */
981 void
982 df_ref_remove (df_ref ref)
984 #if 0
985 if (dump_file)
987 fprintf (dump_file, "removing ref ");
988 df_ref_debug (ref, dump_file);
990 #endif
992 if (DF_REF_REG_DEF_P (ref))
994 if (DF_REF_IS_ARTIFICIAL (ref))
996 struct df_scan_bb_info *bb_info
997 = df_scan_get_bb_info (DF_REF_BBNO (ref));
998 df_ref_compress_rec (&bb_info->artificial_defs, ref);
1000 else
1002 unsigned int uid = DF_REF_INSN_UID (ref);
1003 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
1004 df_ref_compress_rec (&insn_rec->defs, ref);
1007 else
1009 if (DF_REF_IS_ARTIFICIAL (ref))
1011 struct df_scan_bb_info *bb_info
1012 = df_scan_get_bb_info (DF_REF_BBNO (ref));
1013 df_ref_compress_rec (&bb_info->artificial_uses, ref);
1015 else
1017 unsigned int uid = DF_REF_INSN_UID (ref);
1018 struct df_insn_info *insn_rec = DF_INSN_UID_GET (uid);
1020 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
1021 df_ref_compress_rec (&insn_rec->eq_uses, ref);
1022 else
1023 df_ref_compress_rec (&insn_rec->uses, ref);
1027 /* By deleting the ref directly, df_insn_rescan my not find any
1028 differences even though the block will have changed. So we need
1029 to mark the block dirty ourselves. */
1030 df_set_bb_dirty (DF_REF_BB (ref));
1031 df_reg_chain_unlink (ref);
1035 /* Create the insn record for INSN. If there was one there, zero it
1036 out. */
1038 struct df_insn_info *
1039 df_insn_create_insn_record (rtx insn)
1041 struct df_scan_problem_data *problem_data
1042 = (struct df_scan_problem_data *) df_scan->problem_data;
1043 struct df_insn_info *insn_rec;
1045 df_grow_insn_info ();
1046 insn_rec = DF_INSN_INFO_GET (insn);
1047 if (!insn_rec)
1049 insn_rec = (struct df_insn_info *) pool_alloc (problem_data->insn_pool);
1050 DF_INSN_INFO_SET (insn, insn_rec);
1052 memset (insn_rec, 0, sizeof (struct df_insn_info));
1053 insn_rec->insn = insn;
1054 return insn_rec;
1058 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
1060 static void
1061 df_ref_chain_delete_du_chain (df_ref *ref_rec)
1063 while (*ref_rec)
1065 df_ref ref = *ref_rec;
1066 /* CHAIN is allocated by DF_CHAIN. So make sure to
1067 pass df_scan instance for the problem. */
1068 if (DF_REF_CHAIN (ref))
1069 df_chain_unlink (ref);
1070 ref_rec++;
1075 /* Delete all refs in the ref chain. */
1077 static void
1078 df_ref_chain_delete (df_ref *ref_rec)
1080 df_ref *start = ref_rec;
1081 while (*ref_rec)
1083 df_reg_chain_unlink (*ref_rec);
1084 ref_rec++;
1087 /* If the list is empty, it has a special shared element that is not
1088 to be deleted. */
1089 if (*start)
1090 free (start);
1094 /* Delete the hardreg chain. */
1096 static void
1097 df_mw_hardreg_chain_delete (struct df_mw_hardreg **hardregs)
1099 struct df_scan_problem_data *problem_data;
1101 if (!hardregs)
1102 return;
1104 problem_data = (struct df_scan_problem_data *) df_scan->problem_data;
1106 while (*hardregs)
1108 pool_free (problem_data->mw_reg_pool, *hardregs);
1109 hardregs++;
1114 /* Delete all of the refs information from INSN. BB must be passed in
1115 except when called from df_process_deferred_rescans to mark the block
1116 as dirty. */
1118 void
1119 df_insn_delete (basic_block bb, unsigned int uid)
1121 struct df_insn_info *insn_info = NULL;
1122 if (!df)
1123 return;
1125 df_grow_bb_info (df_scan);
1126 df_grow_reg_info ();
1128 /* The block must be marked as dirty now, rather than later as in
1129 df_insn_rescan and df_notes_rescan because it may not be there at
1130 rescanning time and the mark would blow up. */
1131 if (bb)
1132 df_set_bb_dirty (bb);
1134 insn_info = DF_INSN_UID_SAFE_GET (uid);
1136 /* The client has deferred rescanning. */
1137 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1139 if (insn_info)
1141 bitmap_clear_bit (df->insns_to_rescan, uid);
1142 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1143 bitmap_set_bit (df->insns_to_delete, uid);
1145 if (dump_file)
1146 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
1147 return;
1150 if (dump_file)
1151 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
1153 bitmap_clear_bit (df->insns_to_delete, uid);
1154 bitmap_clear_bit (df->insns_to_rescan, uid);
1155 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1156 if (insn_info)
1158 struct df_scan_problem_data *problem_data
1159 = (struct df_scan_problem_data *) df_scan->problem_data;
1161 /* In general, notes do not have the insn_info fields
1162 initialized. However, combine deletes insns by changing them
1163 to notes. How clever. So we cannot just check if it is a
1164 valid insn before short circuiting this code, we need to see
1165 if we actually initialized it. */
1166 if (insn_info->defs)
1168 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1170 if (df_chain)
1172 df_ref_chain_delete_du_chain (insn_info->defs);
1173 df_ref_chain_delete_du_chain (insn_info->uses);
1174 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1177 df_ref_chain_delete (insn_info->defs);
1178 df_ref_chain_delete (insn_info->uses);
1179 df_ref_chain_delete (insn_info->eq_uses);
1181 pool_free (problem_data->insn_pool, insn_info);
1182 DF_INSN_UID_SET (uid, NULL);
1187 /* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
1189 static void
1190 df_free_collection_rec (struct df_collection_rec *collection_rec)
1192 unsigned int ix;
1193 struct df_scan_problem_data *problem_data
1194 = (struct df_scan_problem_data *) df_scan->problem_data;
1195 df_ref ref;
1196 struct df_mw_hardreg *mw;
1198 for (ix = 0; VEC_iterate (df_ref, collection_rec->def_vec, ix, ref); ++ix)
1199 df_free_ref (ref);
1200 for (ix = 0; VEC_iterate (df_ref, collection_rec->use_vec, ix, ref); ++ix)
1201 df_free_ref (ref);
1202 for (ix = 0; VEC_iterate (df_ref, collection_rec->eq_use_vec, ix, ref); ++ix)
1203 df_free_ref (ref);
1204 for (ix = 0;
1205 VEC_iterate (df_mw_hardreg_ptr, collection_rec->mw_vec, ix, mw);
1206 ++ix)
1207 pool_free (problem_data->mw_reg_pool, mw);
1209 VEC_free (df_ref, stack, collection_rec->def_vec);
1210 VEC_free (df_ref, stack, collection_rec->use_vec);
1211 VEC_free (df_ref, stack, collection_rec->eq_use_vec);
1212 VEC_free (df_mw_hardreg_ptr, stack, collection_rec->mw_vec);
1215 /* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1217 bool
1218 df_insn_rescan (rtx insn)
1220 unsigned int uid = INSN_UID (insn);
1221 struct df_insn_info *insn_info = NULL;
1222 basic_block bb = BLOCK_FOR_INSN (insn);
1223 struct df_collection_rec collection_rec;
1225 if ((!df) || (!INSN_P (insn)))
1226 return false;
1228 if (!bb)
1230 if (dump_file)
1231 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1232 return false;
1235 /* The client has disabled rescanning and plans to do it itself. */
1236 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1237 return false;
1239 df_grow_bb_info (df_scan);
1240 df_grow_reg_info ();
1242 insn_info = DF_INSN_UID_SAFE_GET (uid);
1244 /* The client has deferred rescanning. */
1245 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1247 if (!insn_info)
1249 insn_info = df_insn_create_insn_record (insn);
1250 insn_info->defs = df_null_ref_rec;
1251 insn_info->uses = df_null_ref_rec;
1252 insn_info->eq_uses = df_null_ref_rec;
1253 insn_info->mw_hardregs = df_null_mw_rec;
1255 if (dump_file)
1256 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1258 bitmap_clear_bit (df->insns_to_delete, uid);
1259 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1260 bitmap_set_bit (df->insns_to_rescan, INSN_UID (insn));
1261 return false;
1264 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
1265 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
1266 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
1267 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
1269 bitmap_clear_bit (df->insns_to_delete, uid);
1270 bitmap_clear_bit (df->insns_to_rescan, uid);
1271 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1272 if (insn_info)
1274 int luid;
1275 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1276 /* If there's no change, return false. */
1277 if (the_same)
1279 df_free_collection_rec (&collection_rec);
1280 if (dump_file)
1281 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1282 return false;
1284 if (dump_file)
1285 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1287 /* There's change - we need to delete the existing info.
1288 Since the insn isn't moved, we can salvage its LUID. */
1289 luid = DF_INSN_LUID (insn);
1290 df_insn_delete (NULL, uid);
1291 df_insn_create_insn_record (insn);
1292 DF_INSN_LUID (insn) = luid;
1294 else
1296 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1297 df_insn_refs_collect (&collection_rec, bb, insn_info);
1298 if (dump_file)
1299 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1302 df_refs_add_to_chains (&collection_rec, bb, insn);
1303 df_set_bb_dirty (bb);
1305 VEC_free (df_ref, stack, collection_rec.def_vec);
1306 VEC_free (df_ref, stack, collection_rec.use_vec);
1307 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
1308 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
1310 return true;
1313 /* Same as df_insn_rescan, but don't mark the basic block as
1314 dirty. */
1316 bool
1317 df_insn_rescan_debug_internal (rtx insn)
1319 unsigned int uid = INSN_UID (insn);
1320 struct df_insn_info *insn_info;
1322 gcc_assert (DEBUG_INSN_P (insn));
1323 gcc_assert (VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1325 if (!df)
1326 return false;
1328 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1329 if (!insn_info)
1330 return false;
1332 if (dump_file)
1333 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1335 bitmap_clear_bit (df->insns_to_delete, uid);
1336 bitmap_clear_bit (df->insns_to_rescan, uid);
1337 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
1339 if (!insn_info->defs)
1340 return false;
1342 if (insn_info->defs == df_null_ref_rec
1343 && insn_info->uses == df_null_ref_rec
1344 && insn_info->eq_uses == df_null_ref_rec
1345 && insn_info->mw_hardregs == df_null_mw_rec)
1346 return false;
1348 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1350 if (df_chain)
1352 df_ref_chain_delete_du_chain (insn_info->defs);
1353 df_ref_chain_delete_du_chain (insn_info->uses);
1354 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1357 df_ref_chain_delete (insn_info->defs);
1358 df_ref_chain_delete (insn_info->uses);
1359 df_ref_chain_delete (insn_info->eq_uses);
1361 insn_info->defs = df_null_ref_rec;
1362 insn_info->uses = df_null_ref_rec;
1363 insn_info->eq_uses = df_null_ref_rec;
1364 insn_info->mw_hardregs = df_null_mw_rec;
1366 return true;
1370 /* Rescan all of the insns in the function. Note that the artificial
1371 uses and defs are not touched. This function will destroy def-se
1372 or use-def chains. */
1374 void
1375 df_insn_rescan_all (void)
1377 bool no_insn_rescan = false;
1378 bool defer_insn_rescan = false;
1379 basic_block bb;
1380 bitmap_iterator bi;
1381 unsigned int uid;
1382 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1384 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1386 df_clear_flags (DF_NO_INSN_RESCAN);
1387 no_insn_rescan = true;
1390 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1392 df_clear_flags (DF_DEFER_INSN_RESCAN);
1393 defer_insn_rescan = true;
1396 bitmap_copy (tmp, df->insns_to_delete);
1397 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1399 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1400 if (insn_info)
1401 df_insn_delete (NULL, uid);
1404 BITMAP_FREE (tmp);
1405 bitmap_clear (df->insns_to_delete);
1406 bitmap_clear (df->insns_to_rescan);
1407 bitmap_clear (df->insns_to_notes_rescan);
1409 FOR_EACH_BB (bb)
1411 rtx insn;
1412 FOR_BB_INSNS (bb, insn)
1414 df_insn_rescan (insn);
1418 if (no_insn_rescan)
1419 df_set_flags (DF_NO_INSN_RESCAN);
1420 if (defer_insn_rescan)
1421 df_set_flags (DF_DEFER_INSN_RESCAN);
1425 /* Process all of the deferred rescans or deletions. */
1427 void
1428 df_process_deferred_rescans (void)
1430 bool no_insn_rescan = false;
1431 bool defer_insn_rescan = false;
1432 bitmap_iterator bi;
1433 unsigned int uid;
1434 bitmap tmp = BITMAP_ALLOC (&df_bitmap_obstack);
1436 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1438 df_clear_flags (DF_NO_INSN_RESCAN);
1439 no_insn_rescan = true;
1442 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1444 df_clear_flags (DF_DEFER_INSN_RESCAN);
1445 defer_insn_rescan = true;
1448 if (dump_file)
1449 fprintf (dump_file, "starting the processing of deferred insns\n");
1451 bitmap_copy (tmp, df->insns_to_delete);
1452 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1454 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1455 if (insn_info)
1456 df_insn_delete (NULL, uid);
1459 bitmap_copy (tmp, df->insns_to_rescan);
1460 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1462 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1463 if (insn_info)
1464 df_insn_rescan (insn_info->insn);
1467 bitmap_copy (tmp, df->insns_to_notes_rescan);
1468 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1470 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1471 if (insn_info)
1472 df_notes_rescan (insn_info->insn);
1475 if (dump_file)
1476 fprintf (dump_file, "ending the processing of deferred insns\n");
1478 BITMAP_FREE (tmp);
1479 bitmap_clear (df->insns_to_delete);
1480 bitmap_clear (df->insns_to_rescan);
1481 bitmap_clear (df->insns_to_notes_rescan);
1483 if (no_insn_rescan)
1484 df_set_flags (DF_NO_INSN_RESCAN);
1485 if (defer_insn_rescan)
1486 df_set_flags (DF_DEFER_INSN_RESCAN);
1488 /* If someone changed regs_ever_live during this pass, fix up the
1489 entry and exit blocks. */
1490 if (df->redo_entry_and_exit)
1492 df_update_entry_exit_and_calls ();
1493 df->redo_entry_and_exit = false;
1498 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1499 the uses if INCLUDE_USES. Include the eq_uses if
1500 INCLUDE_EQ_USES. */
1502 static unsigned int
1503 df_count_refs (bool include_defs, bool include_uses,
1504 bool include_eq_uses)
1506 unsigned int regno;
1507 int size = 0;
1508 unsigned int m = df->regs_inited;
1510 for (regno = 0; regno < m; regno++)
1512 if (include_defs)
1513 size += DF_REG_DEF_COUNT (regno);
1514 if (include_uses)
1515 size += DF_REG_USE_COUNT (regno);
1516 if (include_eq_uses)
1517 size += DF_REG_EQ_USE_COUNT (regno);
1519 return size;
1523 /* Take build ref table for either the uses or defs from the reg-use
1524 or reg-def chains. This version processes the refs in reg order
1525 which is likely to be best if processing the whole function. */
1527 static void
1528 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1529 bool include_defs,
1530 bool include_uses,
1531 bool include_eq_uses)
1533 unsigned int m = df->regs_inited;
1534 unsigned int regno;
1535 unsigned int offset = 0;
1536 unsigned int start;
1538 if (df->changeable_flags & DF_NO_HARD_REGS)
1540 start = FIRST_PSEUDO_REGISTER;
1541 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1542 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1544 else
1545 start = 0;
1547 ref_info->total_size
1548 = df_count_refs (include_defs, include_uses, include_eq_uses);
1550 df_check_and_grow_ref_info (ref_info, 1);
1552 for (regno = start; regno < m; regno++)
1554 int count = 0;
1555 ref_info->begin[regno] = offset;
1556 if (include_defs)
1558 df_ref ref = DF_REG_DEF_CHAIN (regno);
1559 while (ref)
1561 ref_info->refs[offset] = ref;
1562 DF_REF_ID (ref) = offset++;
1563 count++;
1564 ref = DF_REF_NEXT_REG (ref);
1565 gcc_assert (offset < ref_info->refs_size);
1568 if (include_uses)
1570 df_ref ref = DF_REG_USE_CHAIN (regno);
1571 while (ref)
1573 ref_info->refs[offset] = ref;
1574 DF_REF_ID (ref) = offset++;
1575 count++;
1576 ref = DF_REF_NEXT_REG (ref);
1577 gcc_assert (offset < ref_info->refs_size);
1580 if (include_eq_uses)
1582 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1583 while (ref)
1585 ref_info->refs[offset] = ref;
1586 DF_REF_ID (ref) = offset++;
1587 count++;
1588 ref = DF_REF_NEXT_REG (ref);
1589 gcc_assert (offset < ref_info->refs_size);
1592 ref_info->count[regno] = count;
1595 /* The bitmap size is not decremented when refs are deleted. So
1596 reset it now that we have squished out all of the empty
1597 slots. */
1598 ref_info->table_size = offset;
1602 /* Take build ref table for either the uses or defs from the reg-use
1603 or reg-def chains. This version processes the refs in insn order
1604 which is likely to be best if processing some segment of the
1605 function. */
1607 static void
1608 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1609 bool include_defs,
1610 bool include_uses,
1611 bool include_eq_uses)
1613 bitmap_iterator bi;
1614 unsigned int bb_index;
1615 unsigned int m = df->regs_inited;
1616 unsigned int offset = 0;
1617 unsigned int r;
1618 unsigned int start
1619 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1621 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1622 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1624 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1625 df_check_and_grow_ref_info (ref_info, 1);
1627 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1629 basic_block bb = BASIC_BLOCK (bb_index);
1630 rtx insn;
1631 df_ref *ref_rec;
1633 if (include_defs)
1634 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1636 unsigned int regno = DF_REF_REGNO (*ref_rec);
1637 ref_info->count[regno]++;
1639 if (include_uses)
1640 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1642 unsigned int regno = DF_REF_REGNO (*ref_rec);
1643 ref_info->count[regno]++;
1646 FOR_BB_INSNS (bb, insn)
1648 if (INSN_P (insn))
1650 unsigned int uid = INSN_UID (insn);
1652 if (include_defs)
1653 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1655 unsigned int regno = DF_REF_REGNO (*ref_rec);
1656 ref_info->count[regno]++;
1658 if (include_uses)
1659 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1661 unsigned int regno = DF_REF_REGNO (*ref_rec);
1662 ref_info->count[regno]++;
1664 if (include_eq_uses)
1665 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1667 unsigned int regno = DF_REF_REGNO (*ref_rec);
1668 ref_info->count[regno]++;
1674 for (r = start; r < m; r++)
1676 ref_info->begin[r] = offset;
1677 offset += ref_info->count[r];
1678 ref_info->count[r] = 0;
1681 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1683 basic_block bb = BASIC_BLOCK (bb_index);
1684 rtx insn;
1685 df_ref *ref_rec;
1687 if (include_defs)
1688 for (ref_rec = df_get_artificial_defs (bb_index); *ref_rec; ref_rec++)
1690 df_ref ref = *ref_rec;
1691 unsigned int regno = DF_REF_REGNO (ref);
1692 if (regno >= start)
1694 unsigned int id
1695 = ref_info->begin[regno] + ref_info->count[regno]++;
1696 DF_REF_ID (ref) = id;
1697 ref_info->refs[id] = ref;
1700 if (include_uses)
1701 for (ref_rec = df_get_artificial_uses (bb_index); *ref_rec; ref_rec++)
1703 df_ref ref = *ref_rec;
1704 unsigned int regno = DF_REF_REGNO (ref);
1705 if (regno >= start)
1707 unsigned int id
1708 = ref_info->begin[regno] + ref_info->count[regno]++;
1709 DF_REF_ID (ref) = id;
1710 ref_info->refs[id] = ref;
1714 FOR_BB_INSNS (bb, insn)
1716 if (INSN_P (insn))
1718 unsigned int uid = INSN_UID (insn);
1720 if (include_defs)
1721 for (ref_rec = DF_INSN_UID_DEFS (uid); *ref_rec; ref_rec++)
1723 df_ref ref = *ref_rec;
1724 unsigned int regno = DF_REF_REGNO (ref);
1725 if (regno >= start)
1727 unsigned int id
1728 = ref_info->begin[regno] + ref_info->count[regno]++;
1729 DF_REF_ID (ref) = id;
1730 ref_info->refs[id] = ref;
1733 if (include_uses)
1734 for (ref_rec = DF_INSN_UID_USES (uid); *ref_rec; ref_rec++)
1736 df_ref ref = *ref_rec;
1737 unsigned int regno = DF_REF_REGNO (ref);
1738 if (regno >= start)
1740 unsigned int id
1741 = ref_info->begin[regno] + ref_info->count[regno]++;
1742 DF_REF_ID (ref) = id;
1743 ref_info->refs[id] = ref;
1746 if (include_eq_uses)
1747 for (ref_rec = DF_INSN_UID_EQ_USES (uid); *ref_rec; ref_rec++)
1749 df_ref ref = *ref_rec;
1750 unsigned int regno = DF_REF_REGNO (ref);
1751 if (regno >= start)
1753 unsigned int id
1754 = ref_info->begin[regno] + ref_info->count[regno]++;
1755 DF_REF_ID (ref) = id;
1756 ref_info->refs[id] = ref;
1763 /* The bitmap size is not decremented when refs are deleted. So
1764 reset it now that we have squished out all of the empty
1765 slots. */
1767 ref_info->table_size = offset;
1770 /* Take build ref table for either the uses or defs from the reg-use
1771 or reg-def chains. */
1773 static void
1774 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1775 bool include_defs,
1776 bool include_uses,
1777 bool include_eq_uses)
1779 if (df->analyze_subset)
1780 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1781 include_uses, include_eq_uses);
1782 else
1783 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1784 include_uses, include_eq_uses);
1788 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
1789 static unsigned int
1790 df_add_refs_to_table (unsigned int offset,
1791 struct df_ref_info *ref_info,
1792 df_ref *ref_vec)
1794 while (*ref_vec)
1796 df_ref ref = *ref_vec;
1797 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
1798 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1800 ref_info->refs[offset] = ref;
1801 DF_REF_ID (*ref_vec) = offset++;
1803 ref_vec++;
1805 return offset;
1809 /* Count the number of refs in all of the insns of BB. Include the
1810 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1811 eq_uses if INCLUDE_EQ_USES. */
1813 static unsigned int
1814 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1815 struct df_ref_info *ref_info,
1816 bool include_defs, bool include_uses,
1817 bool include_eq_uses)
1819 rtx insn;
1821 if (include_defs)
1822 offset = df_add_refs_to_table (offset, ref_info,
1823 df_get_artificial_defs (bb->index));
1824 if (include_uses)
1825 offset = df_add_refs_to_table (offset, ref_info,
1826 df_get_artificial_uses (bb->index));
1828 FOR_BB_INSNS (bb, insn)
1829 if (INSN_P (insn))
1831 unsigned int uid = INSN_UID (insn);
1832 if (include_defs)
1833 offset = df_add_refs_to_table (offset, ref_info,
1834 DF_INSN_UID_DEFS (uid));
1835 if (include_uses)
1836 offset = df_add_refs_to_table (offset, ref_info,
1837 DF_INSN_UID_USES (uid));
1838 if (include_eq_uses)
1839 offset = df_add_refs_to_table (offset, ref_info,
1840 DF_INSN_UID_EQ_USES (uid));
1842 return offset;
1846 /* Organize the refs by insn into the table in REF_INFO. If
1847 blocks_to_analyze is defined, use that set, otherwise the entire
1848 program. Include the defs if INCLUDE_DEFS. Include the uses if
1849 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1851 static void
1852 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1853 bool include_defs, bool include_uses,
1854 bool include_eq_uses)
1856 basic_block bb;
1857 unsigned int offset = 0;
1859 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1860 df_check_and_grow_ref_info (ref_info, 1);
1861 if (df->blocks_to_analyze)
1863 bitmap_iterator bi;
1864 unsigned int index;
1866 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1868 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK (index), offset, ref_info,
1869 include_defs, include_uses,
1870 include_eq_uses);
1873 ref_info->table_size = offset;
1875 else
1877 FOR_ALL_BB (bb)
1878 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1879 include_defs, include_uses,
1880 include_eq_uses);
1881 ref_info->table_size = offset;
1886 /* If the use refs in DF are not organized, reorganize them. */
1888 void
1889 df_maybe_reorganize_use_refs (enum df_ref_order order)
1891 if (order == df->use_info.ref_order)
1892 return;
1894 switch (order)
1896 case DF_REF_ORDER_BY_REG:
1897 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1898 break;
1900 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1901 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1902 break;
1904 case DF_REF_ORDER_BY_INSN:
1905 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1906 break;
1908 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1909 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1910 break;
1912 case DF_REF_ORDER_NO_TABLE:
1913 free (df->use_info.refs);
1914 df->use_info.refs = NULL;
1915 df->use_info.refs_size = 0;
1916 break;
1918 case DF_REF_ORDER_UNORDERED:
1919 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1920 gcc_unreachable ();
1921 break;
1924 df->use_info.ref_order = order;
1928 /* If the def refs in DF are not organized, reorganize them. */
1930 void
1931 df_maybe_reorganize_def_refs (enum df_ref_order order)
1933 if (order == df->def_info.ref_order)
1934 return;
1936 switch (order)
1938 case DF_REF_ORDER_BY_REG:
1939 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1940 break;
1942 case DF_REF_ORDER_BY_INSN:
1943 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1944 break;
1946 case DF_REF_ORDER_NO_TABLE:
1947 free (df->def_info.refs);
1948 df->def_info.refs = NULL;
1949 df->def_info.refs_size = 0;
1950 break;
1952 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1953 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1954 case DF_REF_ORDER_UNORDERED:
1955 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1956 gcc_unreachable ();
1957 break;
1960 df->def_info.ref_order = order;
1964 /* Change all of the basic block references in INSN to use the insn's
1965 current basic block. This function is called from routines that move
1966 instructions from one block to another. */
1968 void
1969 df_insn_change_bb (rtx insn, basic_block new_bb)
1971 basic_block old_bb = BLOCK_FOR_INSN (insn);
1972 struct df_insn_info *insn_info;
1973 unsigned int uid = INSN_UID (insn);
1975 if (old_bb == new_bb)
1976 return;
1978 set_block_for_insn (insn, new_bb);
1980 if (!df)
1981 return;
1983 if (dump_file)
1984 fprintf (dump_file, "changing bb of uid %d\n", uid);
1986 insn_info = DF_INSN_UID_SAFE_GET (uid);
1987 if (insn_info == NULL)
1989 if (dump_file)
1990 fprintf (dump_file, " unscanned insn\n");
1991 df_insn_rescan (insn);
1992 return;
1995 if (!INSN_P (insn))
1996 return;
1998 df_set_bb_dirty (new_bb);
1999 if (old_bb)
2001 if (dump_file)
2002 fprintf (dump_file, " from %d to %d\n",
2003 old_bb->index, new_bb->index);
2004 df_set_bb_dirty (old_bb);
2006 else
2007 if (dump_file)
2008 fprintf (dump_file, " to %d\n", new_bb->index);
2012 /* Helper function for df_ref_change_reg_with_loc. */
2014 static void
2015 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
2016 struct df_reg_info *new_df,
2017 int new_regno, rtx loc)
2019 df_ref the_ref = old_df->reg_chain;
2021 while (the_ref)
2023 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
2024 && (DF_REF_LOC (the_ref))
2025 && (*DF_REF_LOC (the_ref) == loc))
2027 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
2028 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
2029 df_ref *ref_vec, *ref_vec_t;
2030 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
2031 unsigned int count = 0;
2033 DF_REF_REGNO (the_ref) = new_regno;
2034 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
2036 /* Pull the_ref out of the old regno chain. */
2037 if (prev_ref)
2038 DF_REF_NEXT_REG (prev_ref) = next_ref;
2039 else
2040 old_df->reg_chain = next_ref;
2041 if (next_ref)
2042 DF_REF_PREV_REG (next_ref) = prev_ref;
2043 old_df->n_refs--;
2045 /* Put the ref into the new regno chain. */
2046 DF_REF_PREV_REG (the_ref) = NULL;
2047 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
2048 if (new_df->reg_chain)
2049 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
2050 new_df->reg_chain = the_ref;
2051 new_df->n_refs++;
2052 if (DF_REF_BB (the_ref))
2053 df_set_bb_dirty (DF_REF_BB (the_ref));
2055 /* Need to sort the record again that the ref was in because
2056 the regno is a sorting key. First, find the right
2057 record. */
2058 if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
2059 ref_vec = insn_info->eq_uses;
2060 else
2061 ref_vec = insn_info->uses;
2062 if (dump_file)
2063 fprintf (dump_file, "changing reg in insn %d\n",
2064 DF_REF_INSN_UID (the_ref));
2066 ref_vec_t = ref_vec;
2068 /* Find the length. */
2069 while (*ref_vec_t)
2071 count++;
2072 ref_vec_t++;
2074 qsort (ref_vec, count, sizeof (df_ref ), df_ref_compare);
2076 the_ref = next_ref;
2078 else
2079 the_ref = DF_REF_NEXT_REG (the_ref);
2084 /* Change the regno of all refs that contained LOC from OLD_REGNO to
2085 NEW_REGNO. Refs that do not match LOC are not changed which means
2086 that artificial refs are not changed since they have no loc. This
2087 call is to support the SET_REGNO macro. */
2089 void
2090 df_ref_change_reg_with_loc (int old_regno, int new_regno, rtx loc)
2092 if ((!df) || (old_regno == -1) || (old_regno == new_regno))
2093 return;
2095 df_grow_reg_info ();
2097 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
2098 DF_REG_DEF_GET (new_regno), new_regno, loc);
2099 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
2100 DF_REG_USE_GET (new_regno), new_regno, loc);
2101 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
2102 DF_REG_EQ_USE_GET (new_regno), new_regno, loc);
2106 /* Delete the mw_hardregs that point into the eq_notes. */
2108 static unsigned int
2109 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
2111 struct df_mw_hardreg **mw_vec = insn_info->mw_hardregs;
2112 unsigned int deleted = 0;
2113 unsigned int count = 0;
2114 struct df_scan_problem_data *problem_data
2115 = (struct df_scan_problem_data *) df_scan->problem_data;
2117 if (!*mw_vec)
2118 return 0;
2120 while (*mw_vec)
2122 if ((*mw_vec)->flags & DF_REF_IN_NOTE)
2124 struct df_mw_hardreg **temp_vec = mw_vec;
2126 pool_free (problem_data->mw_reg_pool, *mw_vec);
2127 temp_vec = mw_vec;
2128 /* Shove the remaining ones down one to fill the gap. While
2129 this looks n**2, it is highly unusual to have any mw regs
2130 in eq_notes and the chances of more than one are almost
2131 non existent. */
2132 while (*temp_vec)
2134 *temp_vec = *(temp_vec + 1);
2135 temp_vec++;
2137 deleted++;
2139 else
2141 mw_vec++;
2142 count++;
2146 if (count == 0)
2148 df_scan_free_mws_vec (insn_info->mw_hardregs);
2149 insn_info->mw_hardregs = df_null_mw_rec;
2150 return 0;
2152 return deleted;
2156 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
2158 void
2159 df_notes_rescan (rtx insn)
2161 struct df_insn_info *insn_info;
2162 unsigned int uid = INSN_UID (insn);
2164 if (!df)
2165 return;
2167 /* The client has disabled rescanning and plans to do it itself. */
2168 if (df->changeable_flags & DF_NO_INSN_RESCAN)
2169 return;
2171 /* Do nothing if the insn hasn't been emitted yet. */
2172 if (!BLOCK_FOR_INSN (insn))
2173 return;
2175 df_grow_bb_info (df_scan);
2176 df_grow_reg_info ();
2178 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID(insn));
2180 /* The client has deferred rescanning. */
2181 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
2183 if (!insn_info)
2185 insn_info = df_insn_create_insn_record (insn);
2186 insn_info->defs = df_null_ref_rec;
2187 insn_info->uses = df_null_ref_rec;
2188 insn_info->eq_uses = df_null_ref_rec;
2189 insn_info->mw_hardregs = df_null_mw_rec;
2192 bitmap_clear_bit (df->insns_to_delete, uid);
2193 /* If the insn is set to be rescanned, it does not need to also
2194 be notes rescanned. */
2195 if (!bitmap_bit_p (df->insns_to_rescan, uid))
2196 bitmap_set_bit (df->insns_to_notes_rescan, INSN_UID (insn));
2197 return;
2200 bitmap_clear_bit (df->insns_to_delete, uid);
2201 bitmap_clear_bit (df->insns_to_notes_rescan, uid);
2203 if (insn_info)
2205 basic_block bb = BLOCK_FOR_INSN (insn);
2206 rtx note;
2207 struct df_collection_rec collection_rec;
2208 unsigned int num_deleted;
2209 unsigned int mw_len;
2211 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
2212 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
2213 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
2215 num_deleted = df_mw_hardreg_chain_delete_eq_uses (insn_info);
2216 df_ref_chain_delete (insn_info->eq_uses);
2217 insn_info->eq_uses = NULL;
2219 /* Process REG_EQUIV/REG_EQUAL notes */
2220 for (note = REG_NOTES (insn); note;
2221 note = XEXP (note, 1))
2223 switch (REG_NOTE_KIND (note))
2225 case REG_EQUIV:
2226 case REG_EQUAL:
2227 df_uses_record (DF_REF_REGULAR, &collection_rec,
2228 &XEXP (note, 0), DF_REF_REG_USE,
2229 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
2230 default:
2231 break;
2235 /* Find some place to put any new mw_hardregs. */
2236 df_canonize_collection_rec (&collection_rec);
2237 mw_len = VEC_length (df_mw_hardreg_ptr, collection_rec.mw_vec);
2238 if (mw_len)
2240 unsigned int count = 0;
2241 struct df_mw_hardreg **mw_rec = insn_info->mw_hardregs;
2242 while (*mw_rec)
2244 count++;
2245 mw_rec++;
2248 if (count)
2250 /* Append to the end of the existing record after
2251 expanding it if necessary. */
2252 if (mw_len > num_deleted)
2254 insn_info->mw_hardregs =
2255 XRESIZEVEC (struct df_mw_hardreg *,
2256 insn_info->mw_hardregs,
2257 count + 1 + mw_len);
2259 memcpy (&insn_info->mw_hardregs[count],
2260 VEC_address (df_mw_hardreg_ptr, collection_rec.mw_vec),
2261 mw_len * sizeof (struct df_mw_hardreg *));
2262 insn_info->mw_hardregs[count + mw_len] = NULL;
2263 qsort (insn_info->mw_hardregs, count + mw_len,
2264 sizeof (struct df_mw_hardreg *), df_mw_compare);
2266 else
2268 /* No vector there. */
2269 insn_info->mw_hardregs
2270 = XNEWVEC (struct df_mw_hardreg*, 1 + mw_len);
2271 memcpy (insn_info->mw_hardregs,
2272 VEC_address (df_mw_hardreg_ptr, collection_rec.mw_vec),
2273 mw_len * sizeof (struct df_mw_hardreg *));
2274 insn_info->mw_hardregs[mw_len] = NULL;
2277 /* Get rid of the mw_rec so that df_refs_add_to_chains will
2278 ignore it. */
2279 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
2280 df_refs_add_to_chains (&collection_rec, bb, insn);
2281 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
2283 else
2284 df_insn_rescan (insn);
2289 /*----------------------------------------------------------------------------
2290 Hard core instruction scanning code. No external interfaces here,
2291 just a lot of routines that look inside insns.
2292 ----------------------------------------------------------------------------*/
2295 /* Return true if the contents of two df_ref's are identical.
2296 It ignores DF_REF_MARKER. */
2298 static bool
2299 df_ref_equal_p (df_ref ref1, df_ref ref2)
2301 if (!ref2)
2302 return false;
2304 if (ref1 == ref2)
2305 return true;
2307 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2308 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2309 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2310 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2311 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2312 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2313 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2314 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2315 return false;
2317 switch (DF_REF_CLASS (ref1))
2319 case DF_REF_ARTIFICIAL:
2320 case DF_REF_BASE:
2321 return true;
2323 case DF_REF_EXTRACT:
2324 if ((DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2325 || (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2326 || (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2)))
2327 return false;
2328 /* fallthru. */
2330 case DF_REF_REGULAR:
2331 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2333 default:
2334 gcc_unreachable ();
2336 return false;
2340 /* Compare REF1 and REF2 for sorting. This is only called from places
2341 where all of the refs are of the same type, in the same insn, and
2342 have the same bb. So these fields are not checked. */
2344 static int
2345 df_ref_compare (const void *r1, const void *r2)
2347 const df_ref ref1 = *(const df_ref *)r1;
2348 const df_ref ref2 = *(const df_ref *)r2;
2350 if (ref1 == ref2)
2351 return 0;
2353 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2354 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2356 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2357 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2359 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2360 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2362 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2363 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2365 /* Cannot look at the LOC field on artificial refs. */
2366 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2367 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2368 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2370 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2372 /* If two refs are identical except that one of them has is from
2373 a mw and one is not, we need to have the one with the mw
2374 first. */
2375 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2376 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2377 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2378 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2379 return -1;
2380 else
2381 return 1;
2384 /* The classes are the same at this point so it is safe to only look
2385 at ref1. */
2386 if (DF_REF_CLASS (ref1) == DF_REF_EXTRACT)
2388 if (DF_REF_EXTRACT_OFFSET (ref1) != DF_REF_EXTRACT_OFFSET (ref2))
2389 return DF_REF_EXTRACT_OFFSET (ref1) - DF_REF_EXTRACT_OFFSET (ref2);
2390 if (DF_REF_EXTRACT_WIDTH (ref1) != DF_REF_EXTRACT_WIDTH (ref2))
2391 return DF_REF_EXTRACT_WIDTH (ref1) - DF_REF_EXTRACT_WIDTH (ref2);
2392 if (DF_REF_EXTRACT_MODE (ref1) != DF_REF_EXTRACT_MODE (ref2))
2393 return DF_REF_EXTRACT_MODE (ref1) - DF_REF_EXTRACT_MODE (ref2);
2395 return 0;
2398 static void
2399 df_swap_refs (VEC(df_ref,stack) **ref_vec, int i, int j)
2401 df_ref tmp = VEC_index (df_ref, *ref_vec, i);
2402 VEC_replace (df_ref, *ref_vec, i, VEC_index (df_ref, *ref_vec, j));
2403 VEC_replace (df_ref, *ref_vec, j, tmp);
2406 /* Sort and compress a set of refs. */
2408 static void
2409 df_sort_and_compress_refs (VEC(df_ref,stack) **ref_vec)
2411 unsigned int count;
2412 unsigned int i;
2413 unsigned int dist = 0;
2415 count = VEC_length (df_ref, *ref_vec);
2417 /* If there are 1 or 0 elements, there is nothing to do. */
2418 if (count < 2)
2419 return;
2420 else if (count == 2)
2422 df_ref r0 = VEC_index (df_ref, *ref_vec, 0);
2423 df_ref r1 = VEC_index (df_ref, *ref_vec, 1);
2424 if (df_ref_compare (&r0, &r1) > 0)
2425 df_swap_refs (ref_vec, 0, 1);
2427 else
2429 for (i = 0; i < count - 1; i++)
2431 df_ref r0 = VEC_index (df_ref, *ref_vec, i);
2432 df_ref r1 = VEC_index (df_ref, *ref_vec, i + 1);
2433 if (df_ref_compare (&r0, &r1) >= 0)
2434 break;
2436 /* If the array is already strictly ordered,
2437 which is the most common case for large COUNT case
2438 (which happens for CALL INSNs),
2439 no need to sort and filter out duplicate.
2440 Simply return the count.
2441 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2442 of DF_REF_COMPARE. */
2443 if (i == count - 1)
2444 return;
2445 qsort (VEC_address (df_ref, *ref_vec), count, sizeof (df_ref),
2446 df_ref_compare);
2449 for (i=0; i<count-dist; i++)
2451 /* Find the next ref that is not equal to the current ref. */
2452 while (i + dist + 1 < count
2453 && df_ref_equal_p (VEC_index (df_ref, *ref_vec, i),
2454 VEC_index (df_ref, *ref_vec, i + dist + 1)))
2456 df_free_ref (VEC_index (df_ref, *ref_vec, i + dist + 1));
2457 dist++;
2459 /* Copy it down to the next position. */
2460 if (dist && i + dist + 1 < count)
2461 VEC_replace (df_ref, *ref_vec, i + 1,
2462 VEC_index (df_ref, *ref_vec, i + dist + 1));
2465 count -= dist;
2466 VEC_truncate (df_ref, *ref_vec, count);
2470 /* Return true if the contents of two df_ref's are identical.
2471 It ignores DF_REF_MARKER. */
2473 static bool
2474 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2476 if (!mw2)
2477 return false;
2478 return (mw1 == mw2) ||
2479 (mw1->mw_reg == mw2->mw_reg
2480 && mw1->type == mw2->type
2481 && mw1->flags == mw2->flags
2482 && mw1->start_regno == mw2->start_regno
2483 && mw1->end_regno == mw2->end_regno);
2487 /* Compare MW1 and MW2 for sorting. */
2489 static int
2490 df_mw_compare (const void *m1, const void *m2)
2492 const struct df_mw_hardreg *const mw1 = *(const struct df_mw_hardreg *const*)m1;
2493 const struct df_mw_hardreg *const mw2 = *(const struct df_mw_hardreg *const*)m2;
2495 if (mw1 == mw2)
2496 return 0;
2498 if (mw1->type != mw2->type)
2499 return mw1->type - mw2->type;
2501 if (mw1->flags != mw2->flags)
2502 return mw1->flags - mw2->flags;
2504 if (mw1->start_regno != mw2->start_regno)
2505 return mw1->start_regno - mw2->start_regno;
2507 if (mw1->end_regno != mw2->end_regno)
2508 return mw1->end_regno - mw2->end_regno;
2510 if (mw1->mw_reg != mw2->mw_reg)
2511 return mw1->mw_order - mw2->mw_order;
2513 return 0;
2517 /* Sort and compress a set of refs. */
2519 static void
2520 df_sort_and_compress_mws (VEC(df_mw_hardreg_ptr,stack) **mw_vec)
2522 unsigned int count;
2523 struct df_scan_problem_data *problem_data
2524 = (struct df_scan_problem_data *) df_scan->problem_data;
2525 unsigned int i;
2526 unsigned int dist = 0;
2528 count = VEC_length (df_mw_hardreg_ptr, *mw_vec);
2529 if (count < 2)
2530 return;
2531 else if (count == 2)
2533 struct df_mw_hardreg *m0 = VEC_index (df_mw_hardreg_ptr, *mw_vec, 0);
2534 struct df_mw_hardreg *m1 = VEC_index (df_mw_hardreg_ptr, *mw_vec, 1);
2535 if (df_mw_compare (&m0, &m1) > 0)
2537 struct df_mw_hardreg *tmp = VEC_index (df_mw_hardreg_ptr,
2538 *mw_vec, 0);
2539 VEC_replace (df_mw_hardreg_ptr, *mw_vec, 0,
2540 VEC_index (df_mw_hardreg_ptr, *mw_vec, 1));
2541 VEC_replace (df_mw_hardreg_ptr, *mw_vec, 1, tmp);
2544 else
2545 qsort (VEC_address (df_mw_hardreg_ptr, *mw_vec), count,
2546 sizeof (struct df_mw_hardreg *), df_mw_compare);
2548 for (i=0; i<count-dist; i++)
2550 /* Find the next ref that is not equal to the current ref. */
2551 while (i + dist + 1 < count
2552 && df_mw_equal_p (VEC_index (df_mw_hardreg_ptr, *mw_vec, i),
2553 VEC_index (df_mw_hardreg_ptr, *mw_vec,
2554 i + dist + 1)))
2556 pool_free (problem_data->mw_reg_pool,
2557 VEC_index (df_mw_hardreg_ptr, *mw_vec, i + dist + 1));
2558 dist++;
2560 /* Copy it down to the next position. */
2561 if (dist && i + dist + 1 < count)
2562 VEC_replace (df_mw_hardreg_ptr, *mw_vec, i + 1,
2563 VEC_index (df_mw_hardreg_ptr, *mw_vec, i + dist + 1));
2566 count -= dist;
2567 VEC_truncate (df_mw_hardreg_ptr, *mw_vec, count);
2571 /* Sort and remove duplicates from the COLLECTION_REC. */
2573 static void
2574 df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2576 df_sort_and_compress_refs (&collection_rec->def_vec);
2577 df_sort_and_compress_refs (&collection_rec->use_vec);
2578 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2579 df_sort_and_compress_mws (&collection_rec->mw_vec);
2583 /* Add the new df_ref to appropriate reg_info/ref_info chains. */
2585 static void
2586 df_install_ref (df_ref this_ref,
2587 struct df_reg_info *reg_info,
2588 struct df_ref_info *ref_info,
2589 bool add_to_table)
2591 unsigned int regno = DF_REF_REGNO (this_ref);
2592 /* Add the ref to the reg_{def,use,eq_use} chain. */
2593 df_ref head = reg_info->reg_chain;
2595 reg_info->reg_chain = this_ref;
2596 reg_info->n_refs++;
2598 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2600 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2601 df->hard_regs_live_count[regno]++;
2604 gcc_assert (DF_REF_NEXT_REG (this_ref) == NULL);
2605 gcc_assert (DF_REF_PREV_REG (this_ref) == NULL);
2607 DF_REF_NEXT_REG (this_ref) = head;
2609 /* We cannot actually link to the head of the chain. */
2610 DF_REF_PREV_REG (this_ref) = NULL;
2612 if (head)
2613 DF_REF_PREV_REG (head) = this_ref;
2615 if (add_to_table)
2617 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2618 df_check_and_grow_ref_info (ref_info, 1);
2619 DF_REF_ID (this_ref) = ref_info->table_size;
2620 /* Add the ref to the big array of defs. */
2621 ref_info->refs[ref_info->table_size] = this_ref;
2622 ref_info->table_size++;
2624 else
2625 DF_REF_ID (this_ref) = -1;
2627 ref_info->total_size++;
2631 /* This function takes one of the groups of refs (defs, uses or
2632 eq_uses) and installs the entire group into the insn. It also adds
2633 each of these refs into the appropriate chains. */
2635 static df_ref *
2636 df_install_refs (basic_block bb,
2637 VEC(df_ref,stack)* old_vec,
2638 struct df_reg_info **reg_info,
2639 struct df_ref_info *ref_info,
2640 bool is_notes)
2642 unsigned int count;
2644 count = VEC_length (df_ref, old_vec);
2645 if (count)
2647 df_ref *new_vec = XNEWVEC (df_ref, count + 1);
2648 bool add_to_table;
2649 df_ref this_ref;
2650 unsigned int ix;
2652 switch (ref_info->ref_order)
2654 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2655 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2656 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2657 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2658 add_to_table = true;
2659 break;
2660 case DF_REF_ORDER_UNORDERED:
2661 case DF_REF_ORDER_BY_REG:
2662 case DF_REF_ORDER_BY_INSN:
2663 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2664 add_to_table = !is_notes;
2665 break;
2666 default:
2667 add_to_table = false;
2668 break;
2671 /* Do not add if ref is not in the right blocks. */
2672 if (add_to_table && df->analyze_subset)
2673 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2675 for (ix = 0; VEC_iterate (df_ref, old_vec, ix, this_ref); ++ix)
2677 new_vec[ix] = this_ref;
2678 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2679 ref_info, add_to_table);
2682 new_vec[count] = NULL;
2683 return new_vec;
2685 else
2686 return df_null_ref_rec;
2690 /* This function takes the mws installs the entire group into the
2691 insn. */
2693 static struct df_mw_hardreg **
2694 df_install_mws (VEC(df_mw_hardreg_ptr,stack) *old_vec)
2696 unsigned int count;
2698 count = VEC_length (df_mw_hardreg_ptr, old_vec);
2699 if (count)
2701 struct df_mw_hardreg **new_vec
2702 = XNEWVEC (struct df_mw_hardreg*, count + 1);
2703 memcpy (new_vec, VEC_address (df_mw_hardreg_ptr, old_vec),
2704 sizeof (struct df_mw_hardreg*) * count);
2705 new_vec[count] = NULL;
2706 return new_vec;
2708 else
2709 return df_null_mw_rec;
2713 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2714 chains and update other necessary information. */
2716 static void
2717 df_refs_add_to_chains (struct df_collection_rec *collection_rec,
2718 basic_block bb, rtx insn)
2720 if (insn)
2722 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2723 /* If there is a vector in the collection rec, add it to the
2724 insn. A null rec is a signal that the caller will handle the
2725 chain specially. */
2726 if (collection_rec->def_vec)
2728 df_scan_free_ref_vec (insn_rec->defs);
2729 insn_rec->defs
2730 = df_install_refs (bb, collection_rec->def_vec,
2731 df->def_regs,
2732 &df->def_info, false);
2734 if (collection_rec->use_vec)
2736 df_scan_free_ref_vec (insn_rec->uses);
2737 insn_rec->uses
2738 = df_install_refs (bb, collection_rec->use_vec,
2739 df->use_regs,
2740 &df->use_info, false);
2742 if (collection_rec->eq_use_vec)
2744 df_scan_free_ref_vec (insn_rec->eq_uses);
2745 insn_rec->eq_uses
2746 = df_install_refs (bb, collection_rec->eq_use_vec,
2747 df->eq_use_regs,
2748 &df->use_info, true);
2750 if (collection_rec->mw_vec)
2752 df_scan_free_mws_vec (insn_rec->mw_hardregs);
2753 insn_rec->mw_hardregs
2754 = df_install_mws (collection_rec->mw_vec);
2757 else
2759 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2761 df_scan_free_ref_vec (bb_info->artificial_defs);
2762 bb_info->artificial_defs
2763 = df_install_refs (bb, collection_rec->def_vec,
2764 df->def_regs,
2765 &df->def_info, false);
2766 df_scan_free_ref_vec (bb_info->artificial_uses);
2767 bb_info->artificial_uses
2768 = df_install_refs (bb, collection_rec->use_vec,
2769 df->use_regs,
2770 &df->use_info, false);
2775 /* Allocate a ref and initialize its fields.
2777 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2778 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the fields
2779 if they were constants. Otherwise they should be -1 if those flags
2780 were set. */
2782 static df_ref
2783 df_ref_create_structure (enum df_ref_class cl,
2784 struct df_collection_rec *collection_rec,
2785 rtx reg, rtx *loc,
2786 basic_block bb, struct df_insn_info *info,
2787 enum df_ref_type ref_type,
2788 int ref_flags,
2789 int width, int offset, enum machine_mode mode)
2791 df_ref this_ref = NULL;
2792 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2793 struct df_scan_problem_data *problem_data
2794 = (struct df_scan_problem_data *) df_scan->problem_data;
2796 switch (cl)
2798 case DF_REF_BASE:
2799 this_ref = (df_ref) pool_alloc (problem_data->ref_base_pool);
2800 gcc_assert (loc == NULL);
2801 break;
2803 case DF_REF_ARTIFICIAL:
2804 this_ref = (df_ref) pool_alloc (problem_data->ref_artificial_pool);
2805 this_ref->artificial_ref.bb = bb;
2806 gcc_assert (loc == NULL);
2807 break;
2809 case DF_REF_REGULAR:
2810 this_ref = (df_ref) pool_alloc (problem_data->ref_regular_pool);
2811 this_ref->regular_ref.loc = loc;
2812 gcc_assert (loc);
2813 break;
2815 case DF_REF_EXTRACT:
2816 this_ref = (df_ref) pool_alloc (problem_data->ref_extract_pool);
2817 DF_REF_EXTRACT_WIDTH (this_ref) = width;
2818 DF_REF_EXTRACT_OFFSET (this_ref) = offset;
2819 DF_REF_EXTRACT_MODE (this_ref) = mode;
2820 this_ref->regular_ref.loc = loc;
2821 gcc_assert (loc);
2822 break;
2825 DF_REF_CLASS (this_ref) = cl;
2826 DF_REF_ID (this_ref) = -1;
2827 DF_REF_REG (this_ref) = reg;
2828 DF_REF_REGNO (this_ref) = regno;
2829 DF_REF_TYPE (this_ref) = ref_type;
2830 DF_REF_INSN_INFO (this_ref) = info;
2831 DF_REF_CHAIN (this_ref) = NULL;
2832 DF_REF_FLAGS (this_ref) = ref_flags;
2833 DF_REF_NEXT_REG (this_ref) = NULL;
2834 DF_REF_PREV_REG (this_ref) = NULL;
2835 DF_REF_ORDER (this_ref) = df->ref_order++;
2837 /* We need to clear this bit because fwprop, and in the future
2838 possibly other optimizations sometimes create new refs using ond
2839 refs as the model. */
2840 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2842 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
2843 if ((regno < FIRST_PSEUDO_REGISTER)
2844 && (!DF_REF_IS_ARTIFICIAL (this_ref)))
2846 if (DF_REF_REG_DEF_P (this_ref))
2848 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2849 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2851 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2852 && (regno == FRAME_POINTER_REGNUM
2853 || regno == ARG_POINTER_REGNUM)))
2854 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2857 if (collection_rec)
2859 if (DF_REF_REG_DEF_P (this_ref))
2860 VEC_safe_push (df_ref, stack, collection_rec->def_vec, this_ref);
2861 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2862 VEC_safe_push (df_ref, stack, collection_rec->eq_use_vec, this_ref);
2863 else
2864 VEC_safe_push (df_ref, stack, collection_rec->use_vec, this_ref);
2867 return this_ref;
2871 /* Create new references of type DF_REF_TYPE for each part of register REG
2872 at address LOC within INSN of BB.
2874 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
2875 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
2876 fields if they were constants. Otherwise they should be -1 if
2877 those flags were set. */
2880 static void
2881 df_ref_record (enum df_ref_class cl,
2882 struct df_collection_rec *collection_rec,
2883 rtx reg, rtx *loc,
2884 basic_block bb, struct df_insn_info *insn_info,
2885 enum df_ref_type ref_type,
2886 int ref_flags,
2887 int width, int offset, enum machine_mode mode)
2889 unsigned int regno;
2891 gcc_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2893 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2894 if (regno < FIRST_PSEUDO_REGISTER)
2896 struct df_mw_hardreg *hardreg = NULL;
2897 struct df_scan_problem_data *problem_data
2898 = (struct df_scan_problem_data *) df_scan->problem_data;
2899 unsigned int i;
2900 unsigned int endregno;
2901 df_ref ref;
2903 if (GET_CODE (reg) == SUBREG)
2905 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2906 SUBREG_BYTE (reg), GET_MODE (reg));
2907 endregno = regno + subreg_nregs (reg);
2909 else
2910 endregno = END_HARD_REGNO (reg);
2912 /* If this is a multiword hardreg, we create some extra
2913 datastructures that will enable us to easily build REG_DEAD
2914 and REG_UNUSED notes. */
2915 if ((endregno != regno + 1) && insn_info)
2917 /* Sets to a subreg of a multiword register are partial.
2918 Sets to a non-subreg of a multiword register are not. */
2919 if (GET_CODE (reg) == SUBREG)
2920 ref_flags |= DF_REF_PARTIAL;
2921 ref_flags |= DF_REF_MW_HARDREG;
2923 hardreg = (struct df_mw_hardreg *) pool_alloc (problem_data->mw_reg_pool);
2924 hardreg->type = ref_type;
2925 hardreg->flags = ref_flags;
2926 hardreg->mw_reg = reg;
2927 hardreg->start_regno = regno;
2928 hardreg->end_regno = endregno - 1;
2929 hardreg->mw_order = df->ref_order++;
2930 VEC_safe_push (df_mw_hardreg_ptr, stack, collection_rec->mw_vec,
2931 hardreg);
2934 for (i = regno; i < endregno; i++)
2936 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2937 bb, insn_info, ref_type, ref_flags,
2938 width, offset, mode);
2940 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2943 else
2945 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2946 ref_type, ref_flags, width, offset, mode);
2951 /* A set to a non-paradoxical SUBREG for which the number of word_mode units
2952 covered by the outer mode is smaller than that covered by the inner mode,
2953 is a read-modify-write operation.
2954 This function returns true iff the SUBREG X is such a SUBREG. */
2956 bool
2957 df_read_modify_subreg_p (rtx x)
2959 unsigned int isize, osize;
2960 if (GET_CODE (x) != SUBREG)
2961 return false;
2962 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2963 osize = GET_MODE_SIZE (GET_MODE (x));
2964 return isize > osize
2965 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
2969 /* Process all the registers defined in the rtx, X.
2970 Autoincrement/decrement definitions will be picked up by
2971 df_uses_record. */
2973 static void
2974 df_def_record_1 (struct df_collection_rec *collection_rec,
2975 rtx x, basic_block bb, struct df_insn_info *insn_info,
2976 int flags)
2978 rtx *loc;
2979 rtx dst;
2980 int offset = -1;
2981 int width = -1;
2982 enum machine_mode mode = VOIDmode;
2983 enum df_ref_class cl = DF_REF_REGULAR;
2985 /* We may recursively call ourselves on EXPR_LIST when dealing with PARALLEL
2986 construct. */
2987 if (GET_CODE (x) == EXPR_LIST || GET_CODE (x) == CLOBBER)
2988 loc = &XEXP (x, 0);
2989 else
2990 loc = &SET_DEST (x);
2991 dst = *loc;
2993 /* It is legal to have a set destination be a parallel. */
2994 if (GET_CODE (dst) == PARALLEL)
2996 int i;
2998 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
3000 rtx temp = XVECEXP (dst, 0, i);
3001 if (GET_CODE (temp) == EXPR_LIST || GET_CODE (temp) == CLOBBER
3002 || GET_CODE (temp) == SET)
3003 df_def_record_1 (collection_rec,
3004 temp, bb, insn_info,
3005 GET_CODE (temp) == CLOBBER
3006 ? flags | DF_REF_MUST_CLOBBER : flags);
3008 return;
3011 if (GET_CODE (dst) == STRICT_LOW_PART)
3013 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
3015 loc = &XEXP (dst, 0);
3016 dst = *loc;
3019 if (GET_CODE (dst) == ZERO_EXTRACT)
3021 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
3023 if (CONST_INT_P (XEXP (dst, 1))
3024 && CONST_INT_P (XEXP (dst, 2)))
3026 width = INTVAL (XEXP (dst, 1));
3027 offset = INTVAL (XEXP (dst, 2));
3028 mode = GET_MODE (dst);
3029 cl = DF_REF_EXTRACT;
3032 loc = &XEXP (dst, 0);
3033 dst = *loc;
3036 /* At this point if we do not have a reg or a subreg, just return. */
3037 if (REG_P (dst))
3039 df_ref_record (cl, collection_rec,
3040 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
3041 width, offset, mode);
3043 /* We want to keep sp alive everywhere - by making all
3044 writes to sp also use of sp. */
3045 if (REGNO (dst) == STACK_POINTER_REGNUM)
3046 df_ref_record (DF_REF_BASE, collection_rec,
3047 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags,
3048 width, offset, mode);
3050 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
3052 if (df_read_modify_subreg_p (dst))
3053 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
3055 flags |= DF_REF_SUBREG;
3057 df_ref_record (cl, collection_rec,
3058 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags,
3059 width, offset, mode);
3064 /* Process all the registers defined in the pattern rtx, X. */
3066 static void
3067 df_defs_record (struct df_collection_rec *collection_rec,
3068 rtx x, basic_block bb, struct df_insn_info *insn_info,
3069 int flags)
3071 RTX_CODE code = GET_CODE (x);
3073 if (code == SET || code == CLOBBER)
3075 /* Mark the single def within the pattern. */
3076 int clobber_flags = flags;
3077 clobber_flags |= (code == CLOBBER) ? DF_REF_MUST_CLOBBER : 0;
3078 df_def_record_1 (collection_rec, x, bb, insn_info, clobber_flags);
3080 else if (code == COND_EXEC)
3082 df_defs_record (collection_rec, COND_EXEC_CODE (x),
3083 bb, insn_info, DF_REF_CONDITIONAL);
3085 else if (code == PARALLEL)
3087 int i;
3089 /* Mark the multiple defs within the pattern. */
3090 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3091 df_defs_record (collection_rec, XVECEXP (x, 0, i), bb, insn_info, flags);
3096 /* Process all the registers used in the rtx at address LOC.
3098 If the REF_FLAGS field contain DF_REF_SIGN_EXTRACT or
3099 DF_REF_ZERO_EXTRACT. WIDTH, OFFSET and MODE are used to access the
3100 fields if they were constants. Otherwise they should be -1 if
3101 those flags were set. */
3103 static void
3104 df_uses_record (enum df_ref_class cl, struct df_collection_rec *collection_rec,
3105 rtx *loc, enum df_ref_type ref_type,
3106 basic_block bb, struct df_insn_info *insn_info,
3107 int flags,
3108 int width, int offset, enum machine_mode mode)
3110 RTX_CODE code;
3111 rtx x;
3113 retry:
3114 x = *loc;
3115 if (!x)
3116 return;
3117 code = GET_CODE (x);
3118 switch (code)
3120 case LABEL_REF:
3121 case SYMBOL_REF:
3122 case CONST_INT:
3123 case CONST:
3124 case CONST_DOUBLE:
3125 case CONST_FIXED:
3126 case CONST_VECTOR:
3127 case PC:
3128 case CC0:
3129 case ADDR_VEC:
3130 case ADDR_DIFF_VEC:
3131 return;
3133 case CLOBBER:
3134 /* If we are clobbering a MEM, mark any registers inside the address
3135 as being used. */
3136 if (MEM_P (XEXP (x, 0)))
3137 df_uses_record (cl, collection_rec,
3138 &XEXP (XEXP (x, 0), 0),
3139 DF_REF_REG_MEM_STORE,
3140 bb, insn_info,
3141 flags, width, offset, mode);
3143 /* If we're clobbering a REG then we have a def so ignore. */
3144 return;
3146 case MEM:
3147 df_uses_record (cl, collection_rec,
3148 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
3149 bb, insn_info, flags & DF_REF_IN_NOTE,
3150 width, offset, mode);
3151 return;
3153 case SUBREG:
3154 /* While we're here, optimize this case. */
3155 flags |= DF_REF_PARTIAL;
3156 /* In case the SUBREG is not of a REG, do not optimize. */
3157 if (!REG_P (SUBREG_REG (x)))
3159 loc = &SUBREG_REG (x);
3160 df_uses_record (cl, collection_rec, loc, ref_type, bb, insn_info, flags,
3161 width, offset, mode);
3162 return;
3164 /* ... Fall through ... */
3166 case REG:
3167 df_ref_record (cl, collection_rec,
3168 x, loc, bb, insn_info,
3169 ref_type, flags,
3170 width, offset, mode);
3171 return;
3173 case SIGN_EXTRACT:
3174 case ZERO_EXTRACT:
3176 /* If the parameters to the zero or sign extract are
3177 constants, strip them off and recurse, otherwise there is
3178 no information that we can gain from this operation. */
3179 if (CONST_INT_P (XEXP (x, 1))
3180 && CONST_INT_P (XEXP (x, 2)))
3182 width = INTVAL (XEXP (x, 1));
3183 offset = INTVAL (XEXP (x, 2));
3184 mode = GET_MODE (x);
3186 if (code == ZERO_EXTRACT)
3187 flags |= DF_REF_ZERO_EXTRACT;
3188 else
3189 flags |= DF_REF_SIGN_EXTRACT;
3191 df_uses_record (DF_REF_EXTRACT, collection_rec,
3192 &XEXP (x, 0), ref_type, bb, insn_info, flags,
3193 width, offset, mode);
3194 return;
3197 break;
3199 case SET:
3201 rtx dst = SET_DEST (x);
3202 gcc_assert (!(flags & DF_REF_IN_NOTE));
3203 df_uses_record (cl, collection_rec,
3204 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags,
3205 width, offset, mode);
3207 switch (GET_CODE (dst))
3209 case SUBREG:
3210 if (df_read_modify_subreg_p (dst))
3212 df_uses_record (cl, collection_rec, &SUBREG_REG (dst),
3213 DF_REF_REG_USE, bb, insn_info,
3214 flags | DF_REF_READ_WRITE | DF_REF_SUBREG,
3215 width, offset, mode);
3216 break;
3218 /* Fall through. */
3219 case REG:
3220 case PARALLEL:
3221 case SCRATCH:
3222 case PC:
3223 case CC0:
3224 break;
3225 case MEM:
3226 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3227 DF_REF_REG_MEM_STORE, bb, insn_info, flags,
3228 width, offset, mode);
3229 break;
3230 case STRICT_LOW_PART:
3232 rtx *temp = &XEXP (dst, 0);
3233 /* A strict_low_part uses the whole REG and not just the
3234 SUBREG. */
3235 dst = XEXP (dst, 0);
3236 df_uses_record (cl, collection_rec,
3237 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
3238 DF_REF_REG_USE, bb, insn_info,
3239 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART,
3240 width, offset, mode);
3242 break;
3243 case ZERO_EXTRACT:
3245 if (CONST_INT_P (XEXP (dst, 1))
3246 && CONST_INT_P (XEXP (dst, 2)))
3248 width = INTVAL (XEXP (dst, 1));
3249 offset = INTVAL (XEXP (dst, 2));
3250 mode = GET_MODE (dst);
3251 if (GET_CODE (XEXP (dst,0)) == MEM)
3253 /* Handle the case of zero_extract(mem(...)) in the set dest.
3254 This special case is allowed only if the mem is a single byte and
3255 is useful to set a bitfield in memory. */
3256 df_uses_record (DF_REF_EXTRACT, collection_rec, &XEXP (XEXP (dst,0), 0),
3257 DF_REF_REG_MEM_STORE, bb, insn_info,
3258 DF_REF_ZERO_EXTRACT,
3259 width, offset, mode);
3261 else
3263 df_uses_record (DF_REF_EXTRACT, collection_rec, &XEXP (dst, 0),
3264 DF_REF_REG_USE, bb, insn_info,
3265 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3266 width, offset, mode);
3269 else
3271 df_uses_record (cl, collection_rec, &XEXP (dst, 1),
3272 DF_REF_REG_USE, bb, insn_info, flags,
3273 width, offset, mode);
3274 df_uses_record (cl, collection_rec, &XEXP (dst, 2),
3275 DF_REF_REG_USE, bb, insn_info, flags,
3276 width, offset, mode);
3277 df_uses_record (cl, collection_rec, &XEXP (dst, 0),
3278 DF_REF_REG_USE, bb, insn_info,
3279 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT,
3280 width, offset, mode);
3284 break;
3286 default:
3287 gcc_unreachable ();
3289 return;
3292 case RETURN:
3293 break;
3295 case ASM_OPERANDS:
3296 case UNSPEC_VOLATILE:
3297 case TRAP_IF:
3298 case ASM_INPUT:
3300 /* Traditional and volatile asm instructions must be
3301 considered to use and clobber all hard registers, all
3302 pseudo-registers and all of memory. So must TRAP_IF and
3303 UNSPEC_VOLATILE operations.
3305 Consider for instance a volatile asm that changes the fpu
3306 rounding mode. An insn should not be moved across this
3307 even if it only uses pseudo-regs because it might give an
3308 incorrectly rounded result.
3310 However, flow.c's liveness computation did *not* do this,
3311 giving the reasoning as " ?!? Unfortunately, marking all
3312 hard registers as live causes massive problems for the
3313 register allocator and marking all pseudos as live creates
3314 mountains of uninitialized variable warnings."
3316 In order to maintain the status quo with regard to liveness
3317 and uses, we do what flow.c did and just mark any regs we
3318 can find in ASM_OPERANDS as used. In global asm insns are
3319 scanned and regs_asm_clobbered is filled out.
3321 For all ASM_OPERANDS, we must traverse the vector of input
3322 operands. We can not just fall through here since then we
3323 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3324 which do not indicate traditional asms unlike their normal
3325 usage. */
3326 if (code == ASM_OPERANDS)
3328 int j;
3330 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3331 df_uses_record (cl, collection_rec, &ASM_OPERANDS_INPUT (x, j),
3332 DF_REF_REG_USE, bb, insn_info, flags,
3333 width, offset, mode);
3334 return;
3336 break;
3339 case VAR_LOCATION:
3340 df_uses_record (cl, collection_rec,
3341 &PAT_VAR_LOCATION_LOC (x),
3342 DF_REF_REG_USE, bb, insn_info,
3343 flags, width, offset, mode);
3344 return;
3346 case PRE_DEC:
3347 case POST_DEC:
3348 case PRE_INC:
3349 case POST_INC:
3350 case PRE_MODIFY:
3351 case POST_MODIFY:
3352 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3353 /* Catch the def of the register being modified. */
3354 df_ref_record (cl, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3355 bb, insn_info,
3356 DF_REF_REG_DEF,
3357 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY,
3358 width, offset, mode);
3360 /* ... Fall through to handle uses ... */
3362 default:
3363 break;
3366 /* Recursively scan the operands of this expression. */
3368 const char *fmt = GET_RTX_FORMAT (code);
3369 int i;
3371 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3373 if (fmt[i] == 'e')
3375 /* Tail recursive case: save a function call level. */
3376 if (i == 0)
3378 loc = &XEXP (x, 0);
3379 goto retry;
3381 df_uses_record (cl, collection_rec, &XEXP (x, i), ref_type,
3382 bb, insn_info, flags,
3383 width, offset, mode);
3385 else if (fmt[i] == 'E')
3387 int j;
3388 for (j = 0; j < XVECLEN (x, i); j++)
3389 df_uses_record (cl, collection_rec,
3390 &XVECEXP (x, i, j), ref_type,
3391 bb, insn_info, flags,
3392 width, offset, mode);
3397 return;
3401 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
3403 static void
3404 df_get_conditional_uses (struct df_collection_rec *collection_rec)
3406 unsigned int ix;
3407 df_ref ref;
3409 for (ix = 0; VEC_iterate (df_ref, collection_rec->def_vec, ix, ref); ++ix)
3411 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3413 int width = -1;
3414 int offset = -1;
3415 enum machine_mode mode = VOIDmode;
3416 df_ref use;
3418 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT))
3420 width = DF_REF_EXTRACT_WIDTH (ref);
3421 offset = DF_REF_EXTRACT_OFFSET (ref);
3422 mode = DF_REF_EXTRACT_MODE (ref);
3425 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3426 DF_REF_LOC (ref), DF_REF_BB (ref),
3427 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3428 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL,
3429 width, offset, mode);
3430 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3436 /* Get call's extra defs and uses. */
3438 static void
3439 df_get_call_refs (struct df_collection_rec * collection_rec,
3440 basic_block bb,
3441 struct df_insn_info *insn_info,
3442 int flags)
3444 rtx note;
3445 bitmap_iterator bi;
3446 unsigned int ui;
3447 bool is_sibling_call;
3448 unsigned int i;
3449 df_ref def;
3450 bitmap defs_generated = BITMAP_ALLOC (&df_bitmap_obstack);
3452 /* Do not generate clobbers for registers that are the result of the
3453 call. This causes ordering problems in the chain building code
3454 depending on which def is seen first. */
3455 for (i = 0; VEC_iterate (df_ref, collection_rec->def_vec, i, def); ++i)
3456 bitmap_set_bit (defs_generated, DF_REF_REGNO (def));
3458 /* Record the registers used to pass arguments, and explicitly
3459 noted as clobbered. */
3460 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3461 note = XEXP (note, 1))
3463 if (GET_CODE (XEXP (note, 0)) == USE)
3464 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (XEXP (note, 0), 0),
3465 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3466 VOIDmode);
3467 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3469 if (REG_P (XEXP (XEXP (note, 0), 0)))
3471 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3472 if (!bitmap_bit_p (defs_generated, regno))
3473 df_defs_record (collection_rec, XEXP (note, 0), bb,
3474 insn_info, flags);
3476 else
3477 df_uses_record (DF_REF_REGULAR, collection_rec, &XEXP (note, 0),
3478 DF_REF_REG_USE, bb, insn_info, flags, -1, -1,
3479 VOIDmode);
3483 /* The stack ptr is used (honorarily) by a CALL insn. */
3484 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[STACK_POINTER_REGNUM],
3485 NULL, bb, insn_info, DF_REF_REG_USE,
3486 DF_REF_CALL_STACK_USAGE | flags,
3487 -1, -1, VOIDmode);
3489 /* Calls may also reference any of the global registers,
3490 so they are recorded as used. */
3491 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3492 if (global_regs[i])
3494 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3495 NULL, bb, insn_info, DF_REF_REG_USE, flags, -1, -1,
3496 VOIDmode);
3497 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3498 NULL, bb, insn_info, DF_REF_REG_DEF, flags, -1, -1,
3499 VOIDmode);
3502 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3503 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, ui, bi)
3505 if (!global_regs[ui]
3506 && (!bitmap_bit_p (defs_generated, ui))
3507 && (!is_sibling_call
3508 || !bitmap_bit_p (df->exit_block_uses, ui)
3509 || refers_to_regno_p (ui, ui+1,
3510 crtl->return_rtx, NULL)))
3511 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[ui],
3512 NULL, bb, insn_info, DF_REF_REG_DEF,
3513 DF_REF_MAY_CLOBBER | flags,
3514 -1, -1, VOIDmode);
3517 BITMAP_FREE (defs_generated);
3518 return;
3521 /* Collect all refs in the INSN. This function is free of any
3522 side-effect - it will create and return a lists of df_ref's in the
3523 COLLECTION_REC without putting those refs into existing ref chains
3524 and reg chains. */
3526 static void
3527 df_insn_refs_collect (struct df_collection_rec* collection_rec,
3528 basic_block bb, struct df_insn_info *insn_info)
3530 rtx note;
3531 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3533 /* Clear out the collection record. */
3534 VEC_truncate (df_ref, collection_rec->def_vec, 0);
3535 VEC_truncate (df_ref, collection_rec->use_vec, 0);
3536 VEC_truncate (df_ref, collection_rec->eq_use_vec, 0);
3537 VEC_truncate (df_mw_hardreg_ptr, collection_rec->mw_vec, 0);
3539 /* Record register defs. */
3540 df_defs_record (collection_rec, PATTERN (insn_info->insn), bb, insn_info, 0);
3542 /* Process REG_EQUIV/REG_EQUAL notes. */
3543 for (note = REG_NOTES (insn_info->insn); note;
3544 note = XEXP (note, 1))
3546 switch (REG_NOTE_KIND (note))
3548 case REG_EQUIV:
3549 case REG_EQUAL:
3550 df_uses_record (DF_REF_REGULAR, collection_rec,
3551 &XEXP (note, 0), DF_REF_REG_USE,
3552 bb, insn_info, DF_REF_IN_NOTE, -1, -1, VOIDmode);
3553 break;
3554 case REG_NON_LOCAL_GOTO:
3555 /* The frame ptr is used by a non-local goto. */
3556 df_ref_record (DF_REF_BASE, collection_rec,
3557 regno_reg_rtx[FRAME_POINTER_REGNUM],
3558 NULL, bb, insn_info,
3559 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3560 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3561 df_ref_record (DF_REF_BASE, collection_rec,
3562 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3563 NULL, bb, insn_info,
3564 DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3565 #endif
3566 break;
3567 default:
3568 break;
3572 if (CALL_P (insn_info->insn))
3573 df_get_call_refs (collection_rec, bb, insn_info,
3574 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3576 /* Record the register uses. */
3577 df_uses_record (DF_REF_REGULAR, collection_rec,
3578 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0,
3579 -1, -1, VOIDmode);
3581 /* DF_REF_CONDITIONAL needs corresponding USES. */
3582 if (is_cond_exec)
3583 df_get_conditional_uses (collection_rec);
3585 df_canonize_collection_rec (collection_rec);
3588 /* Recompute the luids for the insns in BB. */
3590 void
3591 df_recompute_luids (basic_block bb)
3593 rtx insn;
3594 int luid = 0;
3596 df_grow_insn_info ();
3598 /* Scan the block an insn at a time from beginning to end. */
3599 FOR_BB_INSNS (bb, insn)
3601 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3602 /* Inserting labels does not always trigger the incremental
3603 rescanning. */
3604 if (!insn_info)
3606 gcc_assert (!INSN_P (insn));
3607 insn_info = df_insn_create_insn_record (insn);
3610 DF_INSN_INFO_LUID (insn_info) = luid;
3611 if (INSN_P (insn))
3612 luid++;
3617 /* Collect all artificial refs at the block level for BB and add them
3618 to COLLECTION_REC. */
3620 static void
3621 df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3623 VEC_truncate (df_ref, collection_rec->def_vec, 0);
3624 VEC_truncate (df_ref, collection_rec->use_vec, 0);
3625 VEC_truncate (df_ref, collection_rec->eq_use_vec, 0);
3626 VEC_truncate (df_mw_hardreg_ptr, collection_rec->mw_vec, 0);
3628 if (bb->index == ENTRY_BLOCK)
3630 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3631 return;
3633 else if (bb->index == EXIT_BLOCK)
3635 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3636 return;
3639 #ifdef EH_RETURN_DATA_REGNO
3640 if (bb_has_eh_pred (bb))
3642 unsigned int i;
3643 /* Mark the registers that will contain data for the handler. */
3644 for (i = 0; ; ++i)
3646 unsigned regno = EH_RETURN_DATA_REGNO (i);
3647 if (regno == INVALID_REGNUM)
3648 break;
3649 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3650 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1,
3651 VOIDmode);
3654 #endif
3656 /* Add the hard_frame_pointer if this block is the target of a
3657 non-local goto. */
3658 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3659 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3660 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP, -1, -1, VOIDmode);
3662 /* Add the artificial uses. */
3663 if (bb->index >= NUM_FIXED_BLOCKS)
3665 bitmap_iterator bi;
3666 unsigned int regno;
3667 bitmap au = bb_has_eh_pred (bb)
3668 ? df->eh_block_artificial_uses
3669 : df->regular_block_artificial_uses;
3671 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3673 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3674 bb, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
3678 df_canonize_collection_rec (collection_rec);
3682 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3684 void
3685 df_bb_refs_record (int bb_index, bool scan_insns)
3687 basic_block bb = BASIC_BLOCK (bb_index);
3688 rtx insn;
3689 int luid = 0;
3690 struct df_scan_bb_info *bb_info;
3691 struct df_collection_rec collection_rec;
3693 if (!df)
3694 return;
3696 bb_info = df_scan_get_bb_info (bb_index);
3698 /* Need to make sure that there is a record in the basic block info. */
3699 if (!bb_info)
3701 bb_info = (struct df_scan_bb_info *) pool_alloc (df_scan->block_pool);
3702 df_scan_set_bb_info (bb_index, bb_info);
3703 bb_info->artificial_defs = NULL;
3704 bb_info->artificial_uses = NULL;
3707 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
3708 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
3709 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
3710 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
3712 if (scan_insns)
3713 /* Scan the block an insn at a time from beginning to end. */
3714 FOR_BB_INSNS (bb, insn)
3716 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3717 gcc_assert (!insn_info);
3719 insn_info = df_insn_create_insn_record (insn);
3720 if (INSN_P (insn))
3722 /* Record refs within INSN. */
3723 DF_INSN_INFO_LUID (insn_info) = luid++;
3724 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3725 df_refs_add_to_chains (&collection_rec, bb, insn);
3727 DF_INSN_INFO_LUID (insn_info) = luid;
3730 /* Other block level artificial refs */
3731 df_bb_refs_collect (&collection_rec, bb);
3732 df_refs_add_to_chains (&collection_rec, bb, NULL);
3734 VEC_free (df_ref, stack, collection_rec.def_vec);
3735 VEC_free (df_ref, stack, collection_rec.use_vec);
3736 VEC_free (df_ref, stack, collection_rec.eq_use_vec);
3737 VEC_free (df_mw_hardreg_ptr, stack, collection_rec.mw_vec);
3739 /* Now that the block has been processed, set the block as dirty so
3740 LR and LIVE will get it processed. */
3741 df_set_bb_dirty (bb);
3745 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3746 block. */
3748 static void
3749 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3751 #ifdef EH_USES
3752 unsigned int i;
3753 #endif
3755 bitmap_clear (regular_block_artificial_uses);
3757 if (reload_completed)
3759 if (frame_pointer_needed)
3760 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3762 else
3763 /* Before reload, there are a few registers that must be forced
3764 live everywhere -- which might not already be the case for
3765 blocks within infinite loops. */
3767 /* Any reference to any pseudo before reload is a potential
3768 reference of the frame pointer. */
3769 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3771 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3772 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3773 #endif
3775 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3776 /* Pseudos with argument area equivalences may require
3777 reloading via the argument pointer. */
3778 if (fixed_regs[ARG_POINTER_REGNUM])
3779 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3780 #endif
3782 /* Any constant, or pseudo with constant equivalences, may
3783 require reloading from memory using the pic register. */
3784 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3785 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3786 bitmap_set_bit (regular_block_artificial_uses, PIC_OFFSET_TABLE_REGNUM);
3788 /* The all-important stack pointer must always be live. */
3789 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3791 #ifdef EH_USES
3792 /* EH_USES registers are used:
3793 1) at all insns that might throw (calls or with -fnon-call-exceptions
3794 trapping insns)
3795 2) in all EH edges
3796 3) to support backtraces and/or debugging, anywhere between their
3797 initialization and where they the saved registers are restored
3798 from them, including the cases where we don't reach the epilogue
3799 (noreturn call or infinite loop). */
3800 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3801 if (EH_USES (i))
3802 bitmap_set_bit (regular_block_artificial_uses, i);
3803 #endif
3807 /* Get the artificial use set for an eh block. */
3809 static void
3810 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3812 bitmap_clear (eh_block_artificial_uses);
3814 /* The following code (down thru the arg_pointer setting APPEARS
3815 to be necessary because there is nothing that actually
3816 describes what the exception handling code may actually need
3817 to keep alive. */
3818 if (reload_completed)
3820 if (frame_pointer_needed)
3822 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3823 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3824 bitmap_set_bit (eh_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3825 #endif
3827 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3828 if (fixed_regs[ARG_POINTER_REGNUM])
3829 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3830 #endif
3836 /*----------------------------------------------------------------------------
3837 Specialized hard register scanning functions.
3838 ----------------------------------------------------------------------------*/
3841 /* Mark a register in SET. Hard registers in large modes get all
3842 of their component registers set as well. */
3844 static void
3845 df_mark_reg (rtx reg, void *vset)
3847 bitmap set = (bitmap) vset;
3848 int regno = REGNO (reg);
3850 gcc_assert (GET_MODE (reg) != BLKmode);
3852 bitmap_set_bit (set, regno);
3853 if (regno < FIRST_PSEUDO_REGISTER)
3855 int n = hard_regno_nregs[regno][GET_MODE (reg)];
3856 while (--n > 0)
3857 bitmap_set_bit (set, regno + n);
3862 /* Set the bit for regs that are considered being defined at the entry. */
3864 static void
3865 df_get_entry_block_def_set (bitmap entry_block_defs)
3867 rtx r;
3868 int i;
3870 bitmap_clear (entry_block_defs);
3872 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3874 if (FUNCTION_ARG_REGNO_P (i))
3875 #ifdef INCOMING_REGNO
3876 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3877 #else
3878 bitmap_set_bit (entry_block_defs, i);
3879 #endif
3882 /* The always important stack pointer. */
3883 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3885 /* Once the prologue has been generated, all of these registers
3886 should just show up in the first regular block. */
3887 if (HAVE_prologue && epilogue_completed)
3889 /* Defs for the callee saved registers are inserted so that the
3890 pushes have some defining location. */
3891 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3892 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3893 bitmap_set_bit (entry_block_defs, i);
3896 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3897 if (r && REG_P (r))
3898 bitmap_set_bit (entry_block_defs, REGNO (r));
3900 /* If the function has an incoming STATIC_CHAIN, it has to show up
3901 in the entry def set. */
3902 r = targetm.calls.static_chain (current_function_decl, true);
3903 if (r && REG_P (r))
3904 bitmap_set_bit (entry_block_defs, REGNO (r));
3906 if ((!reload_completed) || frame_pointer_needed)
3908 /* Any reference to any pseudo before reload is a potential
3909 reference of the frame pointer. */
3910 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3911 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3912 /* If they are different, also mark the hard frame pointer as live. */
3913 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3914 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3915 #endif
3918 /* These registers are live everywhere. */
3919 if (!reload_completed)
3921 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3922 /* Pseudos with argument area equivalences may require
3923 reloading via the argument pointer. */
3924 if (fixed_regs[ARG_POINTER_REGNUM])
3925 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3926 #endif
3928 #ifdef PIC_OFFSET_TABLE_REGNUM
3929 /* Any constant, or pseudo with constant equivalences, may
3930 require reloading from memory using the pic register. */
3931 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3932 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3933 bitmap_set_bit (entry_block_defs, PIC_OFFSET_TABLE_REGNUM);
3934 #endif
3937 #ifdef INCOMING_RETURN_ADDR_RTX
3938 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3939 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3940 #endif
3942 targetm.live_on_entry (entry_block_defs);
3946 /* Return the (conservative) set of hard registers that are defined on
3947 entry to the function.
3948 It uses df->entry_block_defs to determine which register
3949 reference to include. */
3951 static void
3952 df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
3953 bitmap entry_block_defs)
3955 unsigned int i;
3956 bitmap_iterator bi;
3958 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3960 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3961 ENTRY_BLOCK_PTR, NULL, DF_REF_REG_DEF, 0, -1, -1,
3962 VOIDmode);
3965 df_canonize_collection_rec (collection_rec);
3969 /* Record the (conservative) set of hard registers that are defined on
3970 entry to the function. */
3972 static void
3973 df_record_entry_block_defs (bitmap entry_block_defs)
3975 struct df_collection_rec collection_rec;
3976 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
3977 collection_rec.def_vec = VEC_alloc (df_ref, stack, FIRST_PSEUDO_REGISTER);
3978 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3980 /* Process bb_refs chain */
3981 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (ENTRY_BLOCK), NULL);
3982 VEC_free (df_ref, stack, collection_rec.def_vec);
3986 /* Update the defs in the entry block. */
3988 void
3989 df_update_entry_block_defs (void)
3991 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
3992 bool changed = false;
3994 df_get_entry_block_def_set (refs);
3995 if (df->entry_block_defs)
3997 if (!bitmap_equal_p (df->entry_block_defs, refs))
3999 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
4000 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
4001 df_ref_chain_delete (bb_info->artificial_defs);
4002 bb_info->artificial_defs = NULL;
4003 changed = true;
4006 else
4008 struct df_scan_problem_data *problem_data
4009 = (struct df_scan_problem_data *) df_scan->problem_data;
4010 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4011 changed = true;
4014 if (changed)
4016 df_record_entry_block_defs (refs);
4017 bitmap_copy (df->entry_block_defs, refs);
4018 df_set_bb_dirty (BASIC_BLOCK (ENTRY_BLOCK));
4020 BITMAP_FREE (refs);
4024 /* Set the bit for regs that are considered being used at the exit. */
4026 static void
4027 df_get_exit_block_use_set (bitmap exit_block_uses)
4029 unsigned int i;
4031 bitmap_clear (exit_block_uses);
4033 /* Stack pointer is always live at the exit. */
4034 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
4036 /* Mark the frame pointer if needed at the end of the function.
4037 If we end up eliminating it, it will be removed from the live
4038 list of each basic block by reload. */
4040 if ((!reload_completed) || frame_pointer_needed)
4042 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
4043 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4044 /* If they are different, also mark the hard frame pointer as live. */
4045 if (!LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
4046 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
4047 #endif
4050 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
4051 /* Many architectures have a GP register even without flag_pic.
4052 Assume the pic register is not in use, or will be handled by
4053 other means, if it is not fixed. */
4054 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
4055 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
4056 bitmap_set_bit (exit_block_uses, PIC_OFFSET_TABLE_REGNUM);
4057 #endif
4059 /* Mark all global registers, and all registers used by the
4060 epilogue as being live at the end of the function since they
4061 may be referenced by our caller. */
4062 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4063 if (global_regs[i] || EPILOGUE_USES (i))
4064 bitmap_set_bit (exit_block_uses, i);
4066 if (HAVE_epilogue && epilogue_completed)
4068 /* Mark all call-saved registers that we actually used. */
4069 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4070 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
4071 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
4072 bitmap_set_bit (exit_block_uses, i);
4075 #ifdef EH_RETURN_DATA_REGNO
4076 /* Mark the registers that will contain data for the handler. */
4077 if (reload_completed && crtl->calls_eh_return)
4078 for (i = 0; ; ++i)
4080 unsigned regno = EH_RETURN_DATA_REGNO (i);
4081 if (regno == INVALID_REGNUM)
4082 break;
4083 bitmap_set_bit (exit_block_uses, regno);
4085 #endif
4087 #ifdef EH_RETURN_STACKADJ_RTX
4088 if ((!HAVE_epilogue || ! epilogue_completed)
4089 && crtl->calls_eh_return)
4091 rtx tmp = EH_RETURN_STACKADJ_RTX;
4092 if (tmp && REG_P (tmp))
4093 df_mark_reg (tmp, exit_block_uses);
4095 #endif
4097 #ifdef EH_RETURN_HANDLER_RTX
4098 if ((!HAVE_epilogue || ! epilogue_completed)
4099 && crtl->calls_eh_return)
4101 rtx tmp = EH_RETURN_HANDLER_RTX;
4102 if (tmp && REG_P (tmp))
4103 df_mark_reg (tmp, exit_block_uses);
4105 #endif
4107 /* Mark function return value. */
4108 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
4112 /* Return the refs of hard registers that are used in the exit block.
4113 It uses df->exit_block_uses to determine register to include. */
4115 static void
4116 df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
4118 unsigned int i;
4119 bitmap_iterator bi;
4121 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
4122 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
4123 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4125 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4126 /* It is deliberate that this is not put in the exit block uses but
4127 I do not know why. */
4128 if (reload_completed
4129 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
4130 && bb_has_eh_pred (EXIT_BLOCK_PTR)
4131 && fixed_regs[ARG_POINTER_REGNUM])
4132 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
4133 EXIT_BLOCK_PTR, NULL, DF_REF_REG_USE, 0, -1, -1, VOIDmode);
4134 #endif
4136 df_canonize_collection_rec (collection_rec);
4140 /* Record the set of hard registers that are used in the exit block.
4141 It uses df->exit_block_uses to determine which bit to include. */
4143 static void
4144 df_record_exit_block_uses (bitmap exit_block_uses)
4146 struct df_collection_rec collection_rec;
4147 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4148 collection_rec.use_vec = VEC_alloc (df_ref, stack, FIRST_PSEUDO_REGISTER);
4150 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
4152 /* Process bb_refs chain */
4153 df_refs_add_to_chains (&collection_rec, BASIC_BLOCK (EXIT_BLOCK), NULL);
4154 VEC_free (df_ref, stack, collection_rec.use_vec);
4158 /* Update the uses in the exit block. */
4160 void
4161 df_update_exit_block_uses (void)
4163 bitmap refs = BITMAP_ALLOC (&df_bitmap_obstack);
4164 bool changed = false;
4166 df_get_exit_block_use_set (refs);
4167 if (df->exit_block_uses)
4169 if (!bitmap_equal_p (df->exit_block_uses, refs))
4171 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
4172 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
4173 df_ref_chain_delete (bb_info->artificial_uses);
4174 bb_info->artificial_uses = NULL;
4175 changed = true;
4178 else
4180 struct df_scan_problem_data *problem_data
4181 = (struct df_scan_problem_data *) df_scan->problem_data;
4182 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
4183 changed = true;
4186 if (changed)
4188 df_record_exit_block_uses (refs);
4189 bitmap_copy (df->exit_block_uses, refs);
4190 df_set_bb_dirty (BASIC_BLOCK (EXIT_BLOCK));
4192 BITMAP_FREE (refs);
4195 static bool initialized = false;
4198 /* Initialize some platform specific structures. */
4200 void
4201 df_hard_reg_init (void)
4203 #ifdef ELIMINABLE_REGS
4204 int i;
4205 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
4206 #endif
4207 if (initialized)
4208 return;
4210 /* Record which registers will be eliminated. We use this in
4211 mark_used_regs. */
4212 CLEAR_HARD_REG_SET (elim_reg_set);
4214 #ifdef ELIMINABLE_REGS
4215 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
4216 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
4217 #else
4218 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
4219 #endif
4221 initialized = true;
4225 /* Recompute the parts of scanning that are based on regs_ever_live
4226 because something changed in that array. */
4228 void
4229 df_update_entry_exit_and_calls (void)
4231 basic_block bb;
4233 df_update_entry_block_defs ();
4234 df_update_exit_block_uses ();
4236 /* The call insns need to be rescanned because there may be changes
4237 in the set of registers clobbered across the call. */
4238 FOR_EACH_BB (bb)
4240 rtx insn;
4241 FOR_BB_INSNS (bb, insn)
4243 if (INSN_P (insn) && CALL_P (insn))
4244 df_insn_rescan (insn);
4250 /* Return true if hard REG is actually used in the some instruction.
4251 There are a fair number of conditions that affect the setting of
4252 this array. See the comment in df.h for df->hard_regs_live_count
4253 for the conditions that this array is set. */
4255 bool
4256 df_hard_reg_used_p (unsigned int reg)
4258 gcc_assert (df);
4259 return df->hard_regs_live_count[reg] != 0;
4263 /* A count of the number of times REG is actually used in the some
4264 instruction. There are a fair number of conditions that affect the
4265 setting of this array. See the comment in df.h for
4266 df->hard_regs_live_count for the conditions that this array is
4267 set. */
4270 unsigned int
4271 df_hard_reg_used_count (unsigned int reg)
4273 gcc_assert (df);
4274 return df->hard_regs_live_count[reg];
4278 /* Get the value of regs_ever_live[REGNO]. */
4280 bool
4281 df_regs_ever_live_p (unsigned int regno)
4283 return regs_ever_live[regno];
4287 /* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
4288 to change, schedule that change for the next update. */
4290 void
4291 df_set_regs_ever_live (unsigned int regno, bool value)
4293 if (regs_ever_live[regno] == value)
4294 return;
4296 regs_ever_live[regno] = value;
4297 if (df)
4298 df->redo_entry_and_exit = true;
4302 /* Compute "regs_ever_live" information from the underlying df
4303 information. Set the vector to all false if RESET. */
4305 void
4306 df_compute_regs_ever_live (bool reset)
4308 unsigned int i;
4309 bool changed = df->redo_entry_and_exit;
4311 if (reset)
4312 memset (regs_ever_live, 0, sizeof (regs_ever_live));
4314 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4315 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
4317 regs_ever_live[i] = true;
4318 changed = true;
4320 if (changed)
4321 df_update_entry_exit_and_calls ();
4322 df->redo_entry_and_exit = false;
4326 /*----------------------------------------------------------------------------
4327 Dataflow ref information verification functions.
4329 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
4330 df_reg_chain_verify_unmarked (refs)
4331 df_refs_verify (VEC(stack,df_ref)*, ref*, bool)
4332 df_mws_verify (mw*, mw*, bool)
4333 df_insn_refs_verify (collection_rec, bb, insn, bool)
4334 df_bb_refs_verify (bb, refs, bool)
4335 df_bb_verify (bb)
4336 df_exit_block_bitmap_verify (bool)
4337 df_entry_block_bitmap_verify (bool)
4338 df_scan_verify ()
4339 ----------------------------------------------------------------------------*/
4342 /* Mark all refs in the reg chain. Verify that all of the registers
4343 are in the correct chain. */
4345 static unsigned int
4346 df_reg_chain_mark (df_ref refs, unsigned int regno,
4347 bool is_def, bool is_eq_use)
4349 unsigned int count = 0;
4350 df_ref ref;
4351 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4353 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4355 /* If there are no def-use or use-def chains, make sure that all
4356 of the chains are clear. */
4357 if (!df_chain)
4358 gcc_assert (!DF_REF_CHAIN (ref));
4360 /* Check to make sure the ref is in the correct chain. */
4361 gcc_assert (DF_REF_REGNO (ref) == regno);
4362 if (is_def)
4363 gcc_assert (DF_REF_REG_DEF_P (ref));
4364 else
4365 gcc_assert (!DF_REF_REG_DEF_P (ref));
4367 if (is_eq_use)
4368 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
4369 else
4370 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4372 if (DF_REF_NEXT_REG (ref))
4373 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
4374 count++;
4375 DF_REF_REG_MARK (ref);
4377 return count;
4381 /* Verify that all of the registers in the chain are unmarked. */
4383 static void
4384 df_reg_chain_verify_unmarked (df_ref refs)
4386 df_ref ref;
4387 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4388 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4392 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4394 static bool
4395 df_refs_verify (VEC(df_ref,stack) *new_rec, df_ref *old_rec,
4396 bool abort_if_fail)
4398 unsigned int ix;
4399 df_ref new_ref;
4401 for (ix = 0; VEC_iterate (df_ref, new_rec, ix, new_ref); ++ix)
4403 if (*old_rec == NULL || !df_ref_equal_p (new_ref, *old_rec))
4405 if (abort_if_fail)
4406 gcc_assert (0);
4407 else
4408 return false;
4411 /* Abort if fail is called from the function level verifier. If
4412 that is the context, mark this reg as being seem. */
4413 if (abort_if_fail)
4415 gcc_assert (DF_REF_IS_REG_MARKED (*old_rec));
4416 DF_REF_REG_UNMARK (*old_rec);
4419 old_rec++;
4422 if (abort_if_fail)
4423 gcc_assert (*old_rec == NULL);
4424 else
4425 return *old_rec == NULL;
4426 return false;
4430 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4432 static bool
4433 df_mws_verify (VEC(df_mw_hardreg_ptr,stack) *new_rec,
4434 struct df_mw_hardreg **old_rec,
4435 bool abort_if_fail)
4437 unsigned int ix;
4438 struct df_mw_hardreg *new_reg;
4440 for (ix = 0; VEC_iterate (df_mw_hardreg_ptr, new_rec, ix, new_reg); ++ix)
4442 if (*old_rec == NULL || !df_mw_equal_p (new_reg, *old_rec))
4444 if (abort_if_fail)
4445 gcc_assert (0);
4446 else
4447 return false;
4449 old_rec++;
4452 if (abort_if_fail)
4453 gcc_assert (*old_rec == NULL);
4454 else
4455 return *old_rec == NULL;
4456 return false;
4460 /* Return true if the existing insn refs information is complete and
4461 correct. Otherwise (i.e. if there's any missing or extra refs),
4462 return the correct df_ref chain in REFS_RETURN.
4464 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4465 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4466 verification mode instead of the whole function, so unmark
4467 everything.
4469 If ABORT_IF_FAIL is set, this function never returns false. */
4471 static bool
4472 df_insn_refs_verify (struct df_collection_rec *collection_rec,
4473 basic_block bb,
4474 rtx insn,
4475 bool abort_if_fail)
4477 bool ret1, ret2, ret3, ret4;
4478 unsigned int uid = INSN_UID (insn);
4479 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4481 df_insn_refs_collect (collection_rec, bb, insn_info);
4483 if (!DF_INSN_UID_DEFS (uid))
4485 /* The insn_rec was created but it was never filled out. */
4486 if (abort_if_fail)
4487 gcc_assert (0);
4488 else
4489 return false;
4492 /* Unfortunately we cannot opt out early if one of these is not
4493 right because the marks will not get cleared. */
4494 ret1 = df_refs_verify (collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4495 abort_if_fail);
4496 ret2 = df_refs_verify (collection_rec->use_vec, DF_INSN_UID_USES (uid),
4497 abort_if_fail);
4498 ret3 = df_refs_verify (collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4499 abort_if_fail);
4500 ret4 = df_mws_verify (collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4501 abort_if_fail);
4502 return (ret1 && ret2 && ret3 && ret4);
4506 /* Return true if all refs in the basic block are correct and complete.
4507 Due to df_ref_chain_verify, it will cause all refs
4508 that are verified to have DF_REF_MARK bit set. */
4510 static bool
4511 df_bb_verify (basic_block bb)
4513 rtx insn;
4514 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4515 struct df_collection_rec collection_rec;
4517 memset (&collection_rec, 0, sizeof (struct df_collection_rec));
4518 collection_rec.def_vec = VEC_alloc (df_ref, stack, 128);
4519 collection_rec.use_vec = VEC_alloc (df_ref, stack, 32);
4520 collection_rec.eq_use_vec = VEC_alloc (df_ref, stack, 32);
4521 collection_rec.mw_vec = VEC_alloc (df_mw_hardreg_ptr, stack, 32);
4523 gcc_assert (bb_info);
4525 /* Scan the block, one insn at a time, from beginning to end. */
4526 FOR_BB_INSNS_REVERSE (bb, insn)
4528 if (!INSN_P (insn))
4529 continue;
4530 df_insn_refs_verify (&collection_rec, bb, insn, true);
4531 df_free_collection_rec (&collection_rec);
4534 /* Do the artificial defs and uses. */
4535 df_bb_refs_collect (&collection_rec, bb);
4536 df_refs_verify (collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4537 df_refs_verify (collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4538 df_free_collection_rec (&collection_rec);
4540 return true;
4544 /* Returns true if the entry block has correct and complete df_ref set.
4545 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4547 static bool
4548 df_entry_block_bitmap_verify (bool abort_if_fail)
4550 bitmap entry_block_defs = BITMAP_ALLOC (&df_bitmap_obstack);
4551 bool is_eq;
4553 df_get_entry_block_def_set (entry_block_defs);
4555 is_eq = bitmap_equal_p (entry_block_defs, df->entry_block_defs);
4557 if (!is_eq && abort_if_fail)
4559 print_current_pass (stderr);
4560 fprintf (stderr, "entry_block_defs = ");
4561 df_print_regset (stderr, entry_block_defs);
4562 fprintf (stderr, "df->entry_block_defs = ");
4563 df_print_regset (stderr, df->entry_block_defs);
4564 gcc_assert (0);
4567 BITMAP_FREE (entry_block_defs);
4569 return is_eq;
4573 /* Returns true if the exit block has correct and complete df_ref set.
4574 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4576 static bool
4577 df_exit_block_bitmap_verify (bool abort_if_fail)
4579 bitmap exit_block_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4580 bool is_eq;
4582 df_get_exit_block_use_set (exit_block_uses);
4584 is_eq = bitmap_equal_p (exit_block_uses, df->exit_block_uses);
4586 if (!is_eq && abort_if_fail)
4588 print_current_pass (stderr);
4589 fprintf (stderr, "exit_block_uses = ");
4590 df_print_regset (stderr, exit_block_uses);
4591 fprintf (stderr, "df->exit_block_uses = ");
4592 df_print_regset (stderr, df->exit_block_uses);
4593 gcc_assert (0);
4596 BITMAP_FREE (exit_block_uses);
4598 return is_eq;
4602 /* Return true if df_ref information for all insns in all blocks are
4603 correct and complete. */
4605 void
4606 df_scan_verify (void)
4608 unsigned int i;
4609 basic_block bb;
4610 bitmap regular_block_artificial_uses;
4611 bitmap eh_block_artificial_uses;
4613 if (!df)
4614 return;
4616 /* Verification is a 4 step process. */
4618 /* (1) All of the refs are marked by going thru the reg chains. */
4619 for (i = 0; i < DF_REG_SIZE (df); i++)
4621 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4622 == DF_REG_DEF_COUNT(i));
4623 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4624 == DF_REG_USE_COUNT(i));
4625 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4626 == DF_REG_EQ_USE_COUNT(i));
4629 /* (2) There are various bitmaps whose value may change over the
4630 course of the compilation. This step recomputes them to make
4631 sure that they have not slipped out of date. */
4632 regular_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4633 eh_block_artificial_uses = BITMAP_ALLOC (&df_bitmap_obstack);
4635 df_get_regular_block_artificial_uses (regular_block_artificial_uses);
4636 df_get_eh_block_artificial_uses (eh_block_artificial_uses);
4638 bitmap_ior_into (eh_block_artificial_uses,
4639 regular_block_artificial_uses);
4641 /* Check artificial_uses bitmaps didn't change. */
4642 gcc_assert (bitmap_equal_p (regular_block_artificial_uses,
4643 df->regular_block_artificial_uses));
4644 gcc_assert (bitmap_equal_p (eh_block_artificial_uses,
4645 df->eh_block_artificial_uses));
4647 BITMAP_FREE (regular_block_artificial_uses);
4648 BITMAP_FREE (eh_block_artificial_uses);
4650 /* Verify entry block and exit block. These only verify the bitmaps,
4651 the refs are verified in df_bb_verify. */
4652 df_entry_block_bitmap_verify (true);
4653 df_exit_block_bitmap_verify (true);
4655 /* (3) All of the insns in all of the blocks are traversed and the
4656 marks are cleared both in the artificial refs attached to the
4657 blocks and the real refs inside the insns. It is a failure to
4658 clear a mark that has not been set as this means that the ref in
4659 the block or insn was not in the reg chain. */
4661 FOR_ALL_BB (bb)
4662 df_bb_verify (bb);
4664 /* (4) See if all reg chains are traversed a second time. This time
4665 a check is made that the marks are clear. A set mark would be a
4666 from a reg that is not in any insn or basic block. */
4668 for (i = 0; i < DF_REG_SIZE (df); i++)
4670 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4671 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4672 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));