1 /* Scanning of rtl for dataflow analysis.
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3    Originally contributed by Michael P. Hayes
4              (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
5    Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
6              and Kenneth Zadeck (zadeck@naturalbridge.com).
7 
8 This file is part of GCC.
9 
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14 
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23 
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "df.h"
32 #include "memmodel.h"
33 #include "tm_p.h"
34 #include "regs.h"
35 #include "emit-rtl.h"  /* FIXME: Can go away once crtl is moved to rtl.h.  */
36 #include "dumpfile.h"
37 #include "calls.h"
38 #include "function-abi.h"
39 
40 /* The set of hard registers in eliminables[i].from. */
41 
42 static HARD_REG_SET elim_reg_set;
43 
44 /* Initialize ur_in and ur_out as if all hard registers were partially
45    available.  */
46 
47 class df_collection_rec
48 {
49 public:
50   auto_vec<df_ref, 128> def_vec;
51   auto_vec<df_ref, 32> use_vec;
52   auto_vec<df_ref, 32> eq_use_vec;
53   auto_vec<df_mw_hardreg *, 32> mw_vec;
54 };
55 
56 static void df_ref_record (enum df_ref_class, class df_collection_rec *,
57 			   rtx, rtx *,
58 			   basic_block, struct df_insn_info *,
59 			   enum df_ref_type, int ref_flags);
60 static void df_def_record_1 (class df_collection_rec *, rtx *,
61 			     basic_block, struct df_insn_info *,
62 			     int ref_flags);
63 static void df_defs_record (class df_collection_rec *, rtx,
64 			    basic_block, struct df_insn_info *,
65 			    int ref_flags);
66 static void df_uses_record (class df_collection_rec *,
67 			    rtx *, enum df_ref_type,
68 			    basic_block, struct df_insn_info *,
69 			    int ref_flags);
70 
71 static void df_install_ref_incremental (df_ref);
72 static void df_insn_refs_collect (class df_collection_rec*,
73 				  basic_block, struct df_insn_info *);
74 static void df_canonize_collection_rec (class df_collection_rec *);
75 
76 static void df_get_regular_block_artificial_uses (bitmap);
77 static void df_get_eh_block_artificial_uses (bitmap);
78 
79 static void df_record_entry_block_defs (bitmap);
80 static void df_record_exit_block_uses (bitmap);
81 static void df_get_exit_block_use_set (bitmap);
82 static void df_get_entry_block_def_set (bitmap);
83 static void df_grow_ref_info (struct df_ref_info *, unsigned int);
84 static void df_ref_chain_delete_du_chain (df_ref);
85 static void df_ref_chain_delete (df_ref);
86 
87 static void df_refs_add_to_chains (class df_collection_rec *,
88 				   basic_block, rtx_insn *, unsigned int);
89 
90 static bool df_insn_refs_verify (class df_collection_rec *, basic_block,
91 				 rtx_insn *, bool);
92 static void df_entry_block_defs_collect (class df_collection_rec *, bitmap);
93 static void df_exit_block_uses_collect (class df_collection_rec *, bitmap);
94 static void df_install_ref (df_ref, struct df_reg_info *,
95 			    struct df_ref_info *, bool);
96 
97 static int df_ref_compare (df_ref, df_ref);
98 static int df_ref_ptr_compare (const void *, const void *);
99 static int df_mw_compare (const df_mw_hardreg *, const df_mw_hardreg *);
100 static int df_mw_ptr_compare (const void *, const void *);
101 
102 static void df_insn_info_delete (unsigned int);
103 
104 /* Indexed by hardware reg number, is true if that register is ever
105    used in the current function.
106 
107    In df-scan.c, this is set up to record the hard regs used
108    explicitly.  Reload adds in the hard regs used for holding pseudo
109    regs.  Final uses it to generate the code in the function prologue
110    and epilogue to save and restore registers as needed.  */
111 
112 static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
113 
114 /* Flags used to tell df_refs_add_to_chains() which vectors it should copy. */
115 static const unsigned int copy_defs = 0x1;
116 static const unsigned int copy_uses = 0x2;
117 static const unsigned int copy_eq_uses = 0x4;
118 static const unsigned int copy_mw = 0x8;
119 static const unsigned int copy_all = copy_defs | copy_uses | copy_eq_uses
120 | copy_mw;
121 
122 /*----------------------------------------------------------------------------
123    SCANNING DATAFLOW PROBLEM
124 
125    There are several ways in which scanning looks just like the other
126    dataflow problems.  It shares the all the mechanisms for local info
127    as well as basic block info.  Where it differs is when and how often
128    it gets run.  It also has no need for the iterative solver.
129 ----------------------------------------------------------------------------*/
130 
131 /* Problem data for the scanning dataflow function.  */
132 struct df_scan_problem_data
133 {
134   object_allocator<df_base_ref> *ref_base_pool;
135   object_allocator<df_artificial_ref> *ref_artificial_pool;
136   object_allocator<df_regular_ref> *ref_regular_pool;
137   object_allocator<df_insn_info> *insn_pool;
138   object_allocator<df_reg_info> *reg_pool;
139   object_allocator<df_mw_hardreg> *mw_reg_pool;
140 
141   bitmap_obstack reg_bitmaps;
142   bitmap_obstack insn_bitmaps;
143 };
144 
145 /* Internal function to shut down the scanning problem.  */
146 static void
df_scan_free_internal(void)147 df_scan_free_internal (void)
148 {
149   struct df_scan_problem_data *problem_data
150     = (struct df_scan_problem_data *) df_scan->problem_data;
151 
152   free (df->def_info.refs);
153   free (df->def_info.begin);
154   free (df->def_info.count);
155   memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
156 
157   free (df->use_info.refs);
158   free (df->use_info.begin);
159   free (df->use_info.count);
160   memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
161 
162   free (df->def_regs);
163   df->def_regs = NULL;
164   free (df->use_regs);
165   df->use_regs = NULL;
166   free (df->eq_use_regs);
167   df->eq_use_regs = NULL;
168   df->regs_size = 0;
169   DF_REG_SIZE (df) = 0;
170 
171   free (df->insns);
172   df->insns = NULL;
173   DF_INSN_SIZE () = 0;
174 
175   free (df_scan->block_info);
176   df_scan->block_info = NULL;
177   df_scan->block_info_size = 0;
178 
179   bitmap_clear (&df->hardware_regs_used);
180   bitmap_clear (&df->regular_block_artificial_uses);
181   bitmap_clear (&df->eh_block_artificial_uses);
182   BITMAP_FREE (df->entry_block_defs);
183   BITMAP_FREE (df->exit_block_uses);
184   bitmap_clear (&df->insns_to_delete);
185   bitmap_clear (&df->insns_to_rescan);
186   bitmap_clear (&df->insns_to_notes_rescan);
187 
188   delete problem_data->ref_base_pool;
189   delete problem_data->ref_artificial_pool;
190   delete problem_data->ref_regular_pool;
191   delete problem_data->insn_pool;
192   delete problem_data->reg_pool;
193   delete problem_data->mw_reg_pool;
194   bitmap_obstack_release (&problem_data->reg_bitmaps);
195   bitmap_obstack_release (&problem_data->insn_bitmaps);
196   free (df_scan->problem_data);
197 }
198 
199 
200 /* Free basic block info.  */
201 
202 static void
df_scan_free_bb_info(basic_block bb,void * vbb_info)203 df_scan_free_bb_info (basic_block bb, void *vbb_info)
204 {
205   struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
206   unsigned int bb_index = bb->index;
207   rtx_insn *insn;
208 
209   FOR_BB_INSNS (bb, insn)
210     if (INSN_P (insn))
211       df_insn_info_delete (INSN_UID (insn));
212 
213   if (bb_index < df_scan->block_info_size)
214     bb_info = df_scan_get_bb_info (bb_index);
215 
216   /* Get rid of any artificial uses or defs.  */
217   df_ref_chain_delete_du_chain (bb_info->artificial_defs);
218   df_ref_chain_delete_du_chain (bb_info->artificial_uses);
219   df_ref_chain_delete (bb_info->artificial_defs);
220   df_ref_chain_delete (bb_info->artificial_uses);
221   bb_info->artificial_defs = NULL;
222   bb_info->artificial_uses = NULL;
223 }
224 
225 
226 /* Allocate the problem data for the scanning problem.  This should be
227    called when the problem is created or when the entire function is to
228    be rescanned.  */
229 void
df_scan_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)230 df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
231 {
232   struct df_scan_problem_data *problem_data;
233   basic_block bb;
234 
235   /* Given the number of pools, this is really faster than tearing
236      everything apart.  */
237   if (df_scan->problem_data)
238     df_scan_free_internal ();
239 
240   problem_data = XNEW (struct df_scan_problem_data);
241   df_scan->problem_data = problem_data;
242   df_scan->computed = true;
243 
244   problem_data->ref_base_pool = new object_allocator<df_base_ref>
245     ("df_scan ref base");
246   problem_data->ref_artificial_pool = new object_allocator<df_artificial_ref>
247     ("df_scan ref artificial");
248   problem_data->ref_regular_pool = new object_allocator<df_regular_ref>
249     ("df_scan ref regular");
250   problem_data->insn_pool = new object_allocator<df_insn_info>
251     ("df_scan insn");
252   problem_data->reg_pool = new object_allocator<df_reg_info>
253     ("df_scan reg");
254   problem_data->mw_reg_pool = new object_allocator<df_mw_hardreg>
255     ("df_scan mw_reg");
256 
257   bitmap_obstack_initialize (&problem_data->reg_bitmaps);
258   bitmap_obstack_initialize (&problem_data->insn_bitmaps);
259 
260   df_grow_reg_info ();
261 
262   df_grow_insn_info ();
263   df_grow_bb_info (df_scan);
264 
265   FOR_ALL_BB_FN (bb, cfun)
266     {
267       unsigned int bb_index = bb->index;
268       struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
269       bb_info->artificial_defs = NULL;
270       bb_info->artificial_uses = NULL;
271     }
272 
273   bitmap_initialize (&df->hardware_regs_used, &problem_data->reg_bitmaps);
274   bitmap_initialize (&df->regular_block_artificial_uses, &problem_data->reg_bitmaps);
275   bitmap_initialize (&df->eh_block_artificial_uses, &problem_data->reg_bitmaps);
276   df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
277   df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
278   bitmap_initialize (&df->insns_to_delete, &problem_data->insn_bitmaps);
279   bitmap_initialize (&df->insns_to_rescan, &problem_data->insn_bitmaps);
280   bitmap_initialize (&df->insns_to_notes_rescan, &problem_data->insn_bitmaps);
281   df_scan->optional_p = false;
282 }
283 
284 
285 /* Free all of the data associated with the scan problem.  */
286 
287 static void
df_scan_free(void)288 df_scan_free (void)
289 {
290   if (df_scan->problem_data)
291     df_scan_free_internal ();
292 
293   if (df->blocks_to_analyze)
294     {
295       BITMAP_FREE (df->blocks_to_analyze);
296       df->blocks_to_analyze = NULL;
297     }
298 
299   free (df_scan);
300 }
301 
302 /* Dump the preamble for DF_SCAN dump. */
303 static void
df_scan_start_dump(FILE * file ATTRIBUTE_UNUSED)304 df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
305 {
306   int i;
307   int dcount = 0;
308   int ucount = 0;
309   int ecount = 0;
310   int icount = 0;
311   int ccount = 0;
312   basic_block bb;
313   rtx_insn *insn;
314 
315   fprintf (file, ";;  fully invalidated by EH \t");
316   df_print_regset
317     (file, bitmap_view<HARD_REG_SET> (eh_edge_abi.full_reg_clobbers ()));
318   fprintf (file, ";;  hardware regs used \t");
319   df_print_regset (file, &df->hardware_regs_used);
320   fprintf (file, ";;  regular block artificial uses \t");
321   df_print_regset (file, &df->regular_block_artificial_uses);
322   fprintf (file, ";;  eh block artificial uses \t");
323   df_print_regset (file, &df->eh_block_artificial_uses);
324   fprintf (file, ";;  entry block defs \t");
325   df_print_regset (file, df->entry_block_defs);
326   fprintf (file, ";;  exit block uses \t");
327   df_print_regset (file, df->exit_block_uses);
328   fprintf (file, ";;  regs ever live \t");
329   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
330     if (df_regs_ever_live_p (i))
331       fprintf (file, " %d [%s]", i, reg_names[i]);
332   fprintf (file, "\n;;  ref usage \t");
333 
334   for (i = 0; i < (int)df->regs_inited; i++)
335     if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
336       {
337 	const char * sep = "";
338 
339 	fprintf (file, "r%d={", i);
340 	if (DF_REG_DEF_COUNT (i))
341 	  {
342 	    fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
343 	    sep = ",";
344 	    dcount += DF_REG_DEF_COUNT (i);
345 	  }
346 	if (DF_REG_USE_COUNT (i))
347 	  {
348 	    fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
349 	    sep = ",";
350 	    ucount += DF_REG_USE_COUNT (i);
351 	  }
352 	if (DF_REG_EQ_USE_COUNT (i))
353 	  {
354 	    fprintf (file, "%s%de", sep, DF_REG_EQ_USE_COUNT (i));
355 	    ecount += DF_REG_EQ_USE_COUNT (i);
356 	  }
357 	fprintf (file, "} ");
358       }
359 
360   FOR_EACH_BB_FN (bb, cfun)
361     FOR_BB_INSNS (bb, insn)
362       if (INSN_P (insn))
363 	{
364 	  if (CALL_P (insn))
365 	    ccount++;
366 	  else
367 	    icount++;
368 	}
369 
370   fprintf (file, "\n;;    total ref usage %d{%dd,%du,%de}"
371 		 " in %d{%d regular + %d call} insns.\n",
372 		 dcount + ucount + ecount, dcount, ucount, ecount,
373 		 icount + ccount, icount, ccount);
374 }
375 
376 /* Dump the bb_info for a given basic block. */
377 static void
df_scan_start_block(basic_block bb,FILE * file)378 df_scan_start_block (basic_block bb, FILE *file)
379 {
380   struct df_scan_bb_info *bb_info
381     = df_scan_get_bb_info (bb->index);
382 
383   if (bb_info)
384     {
385       fprintf (file, ";; bb %d artificial_defs: ", bb->index);
386       df_refs_chain_dump (bb_info->artificial_defs, true, file);
387       fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
388       df_refs_chain_dump (bb_info->artificial_uses, true, file);
389       fprintf (file, "\n");
390     }
391 #if 0
392   {
393     rtx_insn *insn;
394     FOR_BB_INSNS (bb, insn)
395       if (INSN_P (insn))
396 	df_insn_debug (insn, false, file);
397   }
398 #endif
399 }
400 
401 static const struct df_problem problem_SCAN =
402 {
403   DF_SCAN,                    /* Problem id.  */
404   DF_NONE,                    /* Direction.  */
405   df_scan_alloc,              /* Allocate the problem specific data.  */
406   NULL,                       /* Reset global information.  */
407   df_scan_free_bb_info,       /* Free basic block info.  */
408   NULL,                       /* Local compute function.  */
409   NULL,                       /* Init the solution specific data.  */
410   NULL,                       /* Iterative solver.  */
411   NULL,                       /* Confluence operator 0.  */
412   NULL,                       /* Confluence operator n.  */
413   NULL,                       /* Transfer function.  */
414   NULL,                       /* Finalize function.  */
415   df_scan_free,               /* Free all of the problem information.  */
416   NULL,                       /* Remove this problem from the stack of dataflow problems.  */
417   df_scan_start_dump,         /* Debugging.  */
418   df_scan_start_block,        /* Debugging start block.  */
419   NULL,                       /* Debugging end block.  */
420   NULL,                       /* Debugging start insn.  */
421   NULL,                       /* Debugging end insn.  */
422   NULL,                       /* Incremental solution verify start.  */
423   NULL,                       /* Incremental solution verify end.  */
424   NULL,                       /* Dependent problem.  */
425   sizeof (struct df_scan_bb_info),/* Size of entry of block_info array.  */
426   TV_DF_SCAN,                 /* Timing variable.  */
427   false                       /* Reset blocks on dropping out of blocks_to_analyze.  */
428 };
429 
430 
431 /* Create a new DATAFLOW instance and add it to an existing instance
432    of DF.  The returned structure is what is used to get at the
433    solution.  */
434 
435 void
df_scan_add_problem(void)436 df_scan_add_problem (void)
437 {
438   df_add_problem (&problem_SCAN);
439 }
440 
441 
442 /*----------------------------------------------------------------------------
443    Storage Allocation Utilities
444 ----------------------------------------------------------------------------*/
445 
446 
447 /* First, grow the reg_info information.  If the current size is less than
448    the number of pseudos, grow to 25% more than the number of
449    pseudos.
450 
451    Second, assure that all of the slots up to max_reg_num have been
452    filled with reg_info structures.  */
453 
454 void
df_grow_reg_info(void)455 df_grow_reg_info (void)
456 {
457   unsigned int max_reg = max_reg_num ();
458   unsigned int new_size = max_reg;
459   struct df_scan_problem_data *problem_data
460     = (struct df_scan_problem_data *) df_scan->problem_data;
461   unsigned int i;
462 
463   if (df->regs_size < new_size)
464     {
465       new_size += new_size / 4;
466       df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
467       df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
468       df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
469 				    new_size);
470       df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
471       df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
472       df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
473       df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
474       df->regs_size = new_size;
475     }
476 
477   for (i = df->regs_inited; i < max_reg; i++)
478     {
479       struct df_reg_info *reg_info;
480 
481       // TODO
482       reg_info = problem_data->reg_pool->allocate ();
483       memset (reg_info, 0, sizeof (struct df_reg_info));
484       df->def_regs[i] = reg_info;
485       reg_info = problem_data->reg_pool->allocate ();
486       memset (reg_info, 0, sizeof (struct df_reg_info));
487       df->use_regs[i] = reg_info;
488       reg_info = problem_data->reg_pool->allocate ();
489       memset (reg_info, 0, sizeof (struct df_reg_info));
490       df->eq_use_regs[i] = reg_info;
491       df->def_info.begin[i] = 0;
492       df->def_info.count[i] = 0;
493       df->use_info.begin[i] = 0;
494       df->use_info.count[i] = 0;
495     }
496 
497   df->regs_inited = max_reg;
498 }
499 
500 
501 /* Grow the ref information.  */
502 
503 static void
df_grow_ref_info(struct df_ref_info * ref_info,unsigned int new_size)504 df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
505 {
506   if (ref_info->refs_size < new_size)
507     {
508       ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
509       memset (ref_info->refs + ref_info->refs_size, 0,
510 	      (new_size - ref_info->refs_size) *sizeof (df_ref));
511       ref_info->refs_size = new_size;
512     }
513 }
514 
515 
516 /* Check and grow the ref information if necessary.  This routine
517    guarantees total_size + BITMAP_ADDEND amount of entries in refs
518    array.  It updates ref_info->refs_size only and does not change
519    ref_info->total_size.  */
520 
521 static void
df_check_and_grow_ref_info(struct df_ref_info * ref_info,unsigned bitmap_addend)522 df_check_and_grow_ref_info (struct df_ref_info *ref_info,
523 			    unsigned bitmap_addend)
524 {
525   if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
526     {
527       int new_size = ref_info->total_size + bitmap_addend;
528       new_size += ref_info->total_size / 4;
529       df_grow_ref_info (ref_info, new_size);
530     }
531 }
532 
533 
534 /* Grow the ref information.  If the current size is less than the
535    number of instructions, grow to 25% more than the number of
536    instructions.  */
537 
538 void
df_grow_insn_info(void)539 df_grow_insn_info (void)
540 {
541   unsigned int new_size = get_max_uid () + 1;
542   if (DF_INSN_SIZE () < new_size)
543     {
544       new_size += new_size / 4;
545       df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
546       memset (df->insns + df->insns_size, 0,
547 	      (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
548       DF_INSN_SIZE () = new_size;
549     }
550 }
551 
552 
553 
554 
555 /*----------------------------------------------------------------------------
556    PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
557 ----------------------------------------------------------------------------*/
558 
559 /* Rescan all of the block_to_analyze or all of the blocks in the
560    function if df_set_blocks if blocks_to_analyze is NULL;  */
561 
562 void
df_scan_blocks(void)563 df_scan_blocks (void)
564 {
565   basic_block bb;
566 
567   df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
568   df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
569 
570   df_get_regular_block_artificial_uses (&df->regular_block_artificial_uses);
571   df_get_eh_block_artificial_uses (&df->eh_block_artificial_uses);
572 
573   bitmap_ior_into (&df->eh_block_artificial_uses,
574 		   &df->regular_block_artificial_uses);
575 
576   /* ENTRY and EXIT blocks have special defs/uses.  */
577   df_get_entry_block_def_set (df->entry_block_defs);
578   df_record_entry_block_defs (df->entry_block_defs);
579   df_get_exit_block_use_set (df->exit_block_uses);
580   df_record_exit_block_uses (df->exit_block_uses);
581   df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK));
582   df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK));
583 
584   /* Regular blocks */
585   FOR_EACH_BB_FN (bb, cfun)
586     {
587       unsigned int bb_index = bb->index;
588       df_bb_refs_record (bb_index, true);
589     }
590 }
591 
592 /* Create new refs under address LOC within INSN.  This function is
593    only used externally.  REF_FLAGS must be either 0 or DF_REF_IN_NOTE,
594    depending on whether LOC is inside PATTERN (INSN) or a note.  */
595 
596 void
df_uses_create(rtx * loc,rtx_insn * insn,int ref_flags)597 df_uses_create (rtx *loc, rtx_insn *insn, int ref_flags)
598 {
599   gcc_assert (!(ref_flags & ~DF_REF_IN_NOTE));
600   df_uses_record (NULL, loc, DF_REF_REG_USE,
601                   BLOCK_FOR_INSN (insn),
602                   DF_INSN_INFO_GET (insn),
603                   ref_flags);
604 }
605 
606 static void
df_install_ref_incremental(df_ref ref)607 df_install_ref_incremental (df_ref ref)
608 {
609   struct df_reg_info **reg_info;
610   struct df_ref_info *ref_info;
611   df_ref *ref_ptr;
612   bool add_to_table;
613 
614   rtx_insn *insn = DF_REF_INSN (ref);
615   basic_block bb = BLOCK_FOR_INSN (insn);
616 
617   if (DF_REF_REG_DEF_P (ref))
618     {
619       reg_info = df->def_regs;
620       ref_info = &df->def_info;
621       ref_ptr = &DF_INSN_DEFS (insn);
622       add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
623     }
624   else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
625     {
626       reg_info = df->eq_use_regs;
627       ref_info = &df->use_info;
628       ref_ptr = &DF_INSN_EQ_USES (insn);
629       switch (ref_info->ref_order)
630 	{
631 	case DF_REF_ORDER_UNORDERED_WITH_NOTES:
632 	case DF_REF_ORDER_BY_REG_WITH_NOTES:
633 	case DF_REF_ORDER_BY_INSN_WITH_NOTES:
634 	  add_to_table = true;
635 	  break;
636 	default:
637 	  add_to_table = false;
638 	  break;
639 	}
640     }
641   else
642     {
643       reg_info = df->use_regs;
644       ref_info = &df->use_info;
645       ref_ptr = &DF_INSN_USES (insn);
646       add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
647     }
648 
649   /* Do not add if ref is not in the right blocks.  */
650   if (add_to_table && df->analyze_subset)
651     add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
652 
653   df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
654 
655   if (add_to_table)
656     switch (ref_info->ref_order)
657       {
658       case DF_REF_ORDER_UNORDERED_WITH_NOTES:
659       case DF_REF_ORDER_BY_REG_WITH_NOTES:
660       case DF_REF_ORDER_BY_INSN_WITH_NOTES:
661 	ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
662 	break;
663       default:
664 	ref_info->ref_order = DF_REF_ORDER_UNORDERED;
665 	break;
666       }
667 
668   while (*ref_ptr && df_ref_compare (*ref_ptr, ref) < 0)
669     ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
670 
671   DF_REF_NEXT_LOC (ref) = *ref_ptr;
672   *ref_ptr = ref;
673 
674 #if 0
675   if (dump_file)
676     {
677       fprintf (dump_file, "adding ref ");
678       df_ref_debug (ref, dump_file);
679     }
680 #endif
681   /* By adding the ref directly, df_insn_rescan my not find any
682      differences even though the block will have changed.  So we need
683      to mark the block dirty ourselves.  */
684   if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
685     df_set_bb_dirty (bb);
686 }
687 
688 
689 
690 /*----------------------------------------------------------------------------
691    UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
692 ----------------------------------------------------------------------------*/
693 
694 static void
df_free_ref(df_ref ref)695 df_free_ref (df_ref ref)
696 {
697   struct df_scan_problem_data *problem_data
698     = (struct df_scan_problem_data *) df_scan->problem_data;
699 
700   switch (DF_REF_CLASS (ref))
701     {
702     case DF_REF_BASE:
703       problem_data->ref_base_pool->remove ((df_base_ref *) (ref));
704       break;
705 
706     case DF_REF_ARTIFICIAL:
707       problem_data->ref_artificial_pool->remove
708 	((df_artificial_ref *) (ref));
709       break;
710 
711     case DF_REF_REGULAR:
712       problem_data->ref_regular_pool->remove
713 	((df_regular_ref *) (ref));
714       break;
715     }
716 }
717 
718 
719 /* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
720    Also delete the def-use or use-def chain if it exists.  */
721 
722 static void
df_reg_chain_unlink(df_ref ref)723 df_reg_chain_unlink (df_ref ref)
724 {
725   df_ref next = DF_REF_NEXT_REG (ref);
726   df_ref prev = DF_REF_PREV_REG (ref);
727   int id = DF_REF_ID (ref);
728   struct df_reg_info *reg_info;
729   df_ref *refs = NULL;
730 
731   if (DF_REF_REG_DEF_P (ref))
732     {
733       int regno = DF_REF_REGNO (ref);
734       reg_info = DF_REG_DEF_GET (regno);
735       refs = df->def_info.refs;
736     }
737   else
738     {
739       if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
740 	{
741 	  reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
742 	  switch (df->use_info.ref_order)
743 	    {
744 	    case DF_REF_ORDER_UNORDERED_WITH_NOTES:
745 	    case DF_REF_ORDER_BY_REG_WITH_NOTES:
746 	    case DF_REF_ORDER_BY_INSN_WITH_NOTES:
747 	      refs = df->use_info.refs;
748 	      break;
749 	    default:
750 	      break;
751 	    }
752 	}
753       else
754 	{
755 	  reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
756 	  refs = df->use_info.refs;
757 	}
758     }
759 
760   if (refs)
761     {
762       if (df->analyze_subset)
763 	{
764 	  if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
765 	    refs[id] = NULL;
766 	}
767       else
768 	refs[id] = NULL;
769     }
770 
771   /* Delete any def-use or use-def chains that start here. It is
772      possible that there is trash in this field.  This happens for
773      insns that have been deleted when rescanning has been deferred
774      and the chain problem has also been deleted.  The chain tear down
775      code skips deleted insns.  */
776   if (df_chain && DF_REF_CHAIN (ref))
777     df_chain_unlink (ref);
778 
779   reg_info->n_refs--;
780   if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
781     {
782       gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
783       df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
784     }
785 
786   /* Unlink from the reg chain.  If there is no prev, this is the
787      first of the list.  If not, just join the next and prev.  */
788   if (prev)
789     DF_REF_NEXT_REG (prev) = next;
790   else
791     {
792       gcc_assert (reg_info->reg_chain == ref);
793       reg_info->reg_chain = next;
794     }
795   if (next)
796     DF_REF_PREV_REG (next) = prev;
797 
798   df_free_ref (ref);
799 }
800 
801 /* Initialize INSN_INFO to describe INSN.  */
802 
803 static void
df_insn_info_init_fields(df_insn_info * insn_info,rtx_insn * insn)804 df_insn_info_init_fields (df_insn_info *insn_info, rtx_insn *insn)
805 {
806   memset (insn_info, 0, sizeof (struct df_insn_info));
807   insn_info->insn = insn;
808 }
809 
810 /* Create the insn record for INSN.  If there was one there, zero it
811    out.  */
812 
813 struct df_insn_info *
df_insn_create_insn_record(rtx_insn * insn)814 df_insn_create_insn_record (rtx_insn *insn)
815 {
816   struct df_scan_problem_data *problem_data
817     = (struct df_scan_problem_data *) df_scan->problem_data;
818   struct df_insn_info *insn_rec;
819 
820   df_grow_insn_info ();
821   insn_rec = DF_INSN_INFO_GET (insn);
822   if (!insn_rec)
823     {
824       insn_rec = problem_data->insn_pool->allocate ();
825       DF_INSN_INFO_SET (insn, insn_rec);
826     }
827   df_insn_info_init_fields (insn_rec, insn);
828   return insn_rec;
829 }
830 
831 
832 /* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain.  */
833 
834 static void
df_ref_chain_delete_du_chain(df_ref ref)835 df_ref_chain_delete_du_chain (df_ref ref)
836 {
837   for (; ref; ref = DF_REF_NEXT_LOC (ref))
838     /* CHAIN is allocated by DF_CHAIN. So make sure to
839        pass df_scan instance for the problem.  */
840     if (DF_REF_CHAIN (ref))
841       df_chain_unlink (ref);
842 }
843 
844 
845 /* Delete all refs in the ref chain.  */
846 
847 static void
df_ref_chain_delete(df_ref ref)848 df_ref_chain_delete (df_ref ref)
849 {
850   df_ref next;
851   for (; ref; ref = next)
852     {
853       next = DF_REF_NEXT_LOC (ref);
854       df_reg_chain_unlink (ref);
855     }
856 }
857 
858 
859 /* Delete the hardreg chain.  */
860 
861 static void
df_mw_hardreg_chain_delete(struct df_mw_hardreg * hardregs)862 df_mw_hardreg_chain_delete (struct df_mw_hardreg *hardregs)
863 {
864   struct df_scan_problem_data *problem_data
865     = (struct df_scan_problem_data *) df_scan->problem_data;
866   df_mw_hardreg *next;
867 
868   for (; hardregs; hardregs = next)
869     {
870       next = DF_MWS_NEXT (hardregs);
871       problem_data->mw_reg_pool->remove (hardregs);
872     }
873 }
874 
875 /* Remove the contents of INSN_INFO (but don't free INSN_INFO itself).  */
876 
877 static void
df_insn_info_free_fields(df_insn_info * insn_info)878 df_insn_info_free_fields (df_insn_info *insn_info)
879 {
880   /* In general, notes do not have the insn_info fields
881      initialized.  However, combine deletes insns by changing them
882      to notes.  How clever.  So we cannot just check if it is a
883      valid insn before short circuiting this code, we need to see
884      if we actually initialized it.  */
885   df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
886 
887   if (df_chain)
888     {
889       df_ref_chain_delete_du_chain (insn_info->defs);
890       df_ref_chain_delete_du_chain (insn_info->uses);
891       df_ref_chain_delete_du_chain (insn_info->eq_uses);
892     }
893 
894   df_ref_chain_delete (insn_info->defs);
895   df_ref_chain_delete (insn_info->uses);
896   df_ref_chain_delete (insn_info->eq_uses);
897 }
898 
899 /* Delete all of the refs information from the insn with UID.
900    Internal helper for df_insn_delete, df_insn_rescan, and other
901    df-scan routines that don't have to work in deferred mode
902    and do not have to mark basic blocks for re-processing.  */
903 
904 static void
df_insn_info_delete(unsigned int uid)905 df_insn_info_delete (unsigned int uid)
906 {
907   struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
908 
909   bitmap_clear_bit (&df->insns_to_delete, uid);
910   bitmap_clear_bit (&df->insns_to_rescan, uid);
911   bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
912   if (insn_info)
913     {
914       struct df_scan_problem_data *problem_data
915 	= (struct df_scan_problem_data *) df_scan->problem_data;
916 
917       df_insn_info_free_fields (insn_info);
918       problem_data->insn_pool->remove (insn_info);
919       DF_INSN_UID_SET (uid, NULL);
920     }
921 }
922 
923 /* Delete all of the refs information from INSN, either right now
924    or marked for later in deferred mode.  */
925 
926 void
df_insn_delete(rtx_insn * insn)927 df_insn_delete (rtx_insn *insn)
928 {
929   unsigned int uid;
930   basic_block bb;
931 
932   gcc_checking_assert (INSN_P (insn));
933 
934   if (!df)
935     return;
936 
937   uid = INSN_UID (insn);
938   bb = BLOCK_FOR_INSN (insn);
939 
940   /* ??? bb can be NULL after pass_free_cfg.  At that point, DF should
941      not exist anymore (as mentioned in df-core.c: "The only requirement
942      [for DF] is that there be a correct control flow graph."  Clearly
943      that isn't the case after pass_free_cfg.  But DF is freed much later
944      because some back-ends want to use DF info even though the CFG is
945      already gone.  It's not clear to me whether that is safe, actually.
946      In any case, we expect BB to be non-NULL at least up to register
947      allocation, so disallow a non-NULL BB up to there.  Not perfect
948      but better than nothing...  */
949   gcc_checking_assert (bb != NULL || reload_completed);
950 
951   df_grow_bb_info (df_scan);
952   df_grow_reg_info ();
953 
954   /* The block must be marked as dirty now, rather than later as in
955      df_insn_rescan and df_notes_rescan because it may not be there at
956      rescanning time and the mark would blow up.
957      DEBUG_INSNs do not make a block's data flow solution dirty (at
958      worst the LUIDs are no longer contiguous).  */
959   if (bb != NULL && NONDEBUG_INSN_P (insn))
960     df_set_bb_dirty (bb);
961 
962   /* The client has deferred rescanning.  */
963   if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
964     {
965       struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
966       if (insn_info)
967 	{
968 	  bitmap_clear_bit (&df->insns_to_rescan, uid);
969 	  bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
970 	  bitmap_set_bit (&df->insns_to_delete, uid);
971 	}
972       if (dump_file)
973 	fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
974       return;
975     }
976 
977   if (dump_file)
978     fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
979 
980   df_insn_info_delete (uid);
981 }
982 
983 
984 /* Free all of the refs and the mw_hardregs in COLLECTION_REC.  */
985 
986 static void
df_free_collection_rec(class df_collection_rec * collection_rec)987 df_free_collection_rec (class df_collection_rec *collection_rec)
988 {
989   unsigned int ix;
990   struct df_scan_problem_data *problem_data
991     = (struct df_scan_problem_data *) df_scan->problem_data;
992   df_ref ref;
993   struct df_mw_hardreg *mw;
994 
995   FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
996     df_free_ref (ref);
997   FOR_EACH_VEC_ELT (collection_rec->use_vec, ix, ref)
998     df_free_ref (ref);
999   FOR_EACH_VEC_ELT (collection_rec->eq_use_vec, ix, ref)
1000     df_free_ref (ref);
1001   FOR_EACH_VEC_ELT (collection_rec->mw_vec, ix, mw)
1002     problem_data->mw_reg_pool->remove (mw);
1003 
1004   collection_rec->def_vec.release ();
1005   collection_rec->use_vec.release ();
1006   collection_rec->eq_use_vec.release ();
1007   collection_rec->mw_vec.release ();
1008 }
1009 
1010 /* Rescan INSN.  Return TRUE if the rescanning produced any changes.  */
1011 
1012 bool
df_insn_rescan(rtx_insn * insn)1013 df_insn_rescan (rtx_insn *insn)
1014 {
1015   unsigned int uid = INSN_UID (insn);
1016   struct df_insn_info *insn_info = NULL;
1017   basic_block bb = BLOCK_FOR_INSN (insn);
1018   class df_collection_rec collection_rec;
1019 
1020   if ((!df) || (!INSN_P (insn)))
1021     return false;
1022 
1023   if (!bb)
1024     {
1025       if (dump_file)
1026 	fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1027       return false;
1028     }
1029 
1030   /* The client has disabled rescanning and plans to do it itself.  */
1031   if (df->changeable_flags & DF_NO_INSN_RESCAN)
1032     return false;
1033 
1034   df_grow_bb_info (df_scan);
1035   df_grow_reg_info ();
1036 
1037   insn_info = DF_INSN_UID_SAFE_GET (uid);
1038 
1039   /* The client has deferred rescanning.  */
1040   if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1041     {
1042       if (!insn_info)
1043 	{
1044 	  insn_info = df_insn_create_insn_record (insn);
1045 	  insn_info->defs = 0;
1046 	  insn_info->uses = 0;
1047 	  insn_info->eq_uses = 0;
1048 	  insn_info->mw_hardregs = 0;
1049 	}
1050       if (dump_file)
1051 	fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
1052 
1053       bitmap_clear_bit (&df->insns_to_delete, uid);
1054       bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1055       bitmap_set_bit (&df->insns_to_rescan, INSN_UID (insn));
1056       return false;
1057     }
1058 
1059   bitmap_clear_bit (&df->insns_to_delete, uid);
1060   bitmap_clear_bit (&df->insns_to_rescan, uid);
1061   bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1062   if (insn_info)
1063     {
1064       int luid;
1065       bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1066       /* If there's no change, return false. */
1067       if (the_same)
1068 	{
1069 	  df_free_collection_rec (&collection_rec);
1070 	  if (dump_file)
1071 	    fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1072 	  return false;
1073 	}
1074       if (dump_file)
1075 	fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1076 
1077       /* There's change - we need to delete the existing info.
1078 	 Since the insn isn't moved, we can salvage its LUID.  */
1079       luid = DF_INSN_LUID (insn);
1080       df_insn_info_free_fields (insn_info);
1081       df_insn_info_init_fields (insn_info, insn);
1082       DF_INSN_LUID (insn) = luid;
1083     }
1084   else
1085     {
1086       struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1087       df_insn_refs_collect (&collection_rec, bb, insn_info);
1088       if (dump_file)
1089 	fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1090     }
1091 
1092   df_refs_add_to_chains (&collection_rec, bb, insn, copy_all);
1093   if (!DEBUG_INSN_P (insn))
1094     df_set_bb_dirty (bb);
1095 
1096   return true;
1097 }
1098 
1099 /* Same as df_insn_rescan, but don't mark the basic block as
1100    dirty.  */
1101 
1102 bool
df_insn_rescan_debug_internal(rtx_insn * insn)1103 df_insn_rescan_debug_internal (rtx_insn *insn)
1104 {
1105   unsigned int uid = INSN_UID (insn);
1106   struct df_insn_info *insn_info;
1107 
1108   gcc_assert (DEBUG_INSN_P (insn)
1109 	      && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
1110 
1111   if (!df)
1112     return false;
1113 
1114   insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1115   if (!insn_info)
1116     return false;
1117 
1118   if (dump_file)
1119     fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1120 
1121   bitmap_clear_bit (&df->insns_to_delete, uid);
1122   bitmap_clear_bit (&df->insns_to_rescan, uid);
1123   bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1124 
1125   if (insn_info->defs == 0
1126       && insn_info->uses == 0
1127       && insn_info->eq_uses == 0
1128       && insn_info->mw_hardregs == 0)
1129     return false;
1130 
1131   df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1132 
1133   if (df_chain)
1134     {
1135       df_ref_chain_delete_du_chain (insn_info->defs);
1136       df_ref_chain_delete_du_chain (insn_info->uses);
1137       df_ref_chain_delete_du_chain (insn_info->eq_uses);
1138     }
1139 
1140   df_ref_chain_delete (insn_info->defs);
1141   df_ref_chain_delete (insn_info->uses);
1142   df_ref_chain_delete (insn_info->eq_uses);
1143 
1144   insn_info->defs = 0;
1145   insn_info->uses = 0;
1146   insn_info->eq_uses = 0;
1147   insn_info->mw_hardregs = 0;
1148 
1149   return true;
1150 }
1151 
1152 
1153 /* Rescan all of the insns in the function.  Note that the artificial
1154    uses and defs are not touched.  This function will destroy def-use
1155    or use-def chains.  */
1156 
1157 void
df_insn_rescan_all(void)1158 df_insn_rescan_all (void)
1159 {
1160   bool no_insn_rescan = false;
1161   bool defer_insn_rescan = false;
1162   basic_block bb;
1163   bitmap_iterator bi;
1164   unsigned int uid;
1165 
1166   if (df->changeable_flags & DF_NO_INSN_RESCAN)
1167     {
1168       df_clear_flags (DF_NO_INSN_RESCAN);
1169       no_insn_rescan = true;
1170     }
1171 
1172   if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1173     {
1174       df_clear_flags (DF_DEFER_INSN_RESCAN);
1175       defer_insn_rescan = true;
1176     }
1177 
1178   auto_bitmap tmp (&df_bitmap_obstack);
1179   bitmap_copy (tmp, &df->insns_to_delete);
1180   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1181     {
1182       struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1183       if (insn_info)
1184 	df_insn_info_delete (uid);
1185     }
1186 
1187   bitmap_clear (&df->insns_to_delete);
1188   bitmap_clear (&df->insns_to_rescan);
1189   bitmap_clear (&df->insns_to_notes_rescan);
1190 
1191   FOR_EACH_BB_FN (bb, cfun)
1192     {
1193       rtx_insn *insn;
1194       FOR_BB_INSNS (bb, insn)
1195 	{
1196 	  df_insn_rescan (insn);
1197 	}
1198     }
1199 
1200   if (no_insn_rescan)
1201     df_set_flags (DF_NO_INSN_RESCAN);
1202   if (defer_insn_rescan)
1203     df_set_flags (DF_DEFER_INSN_RESCAN);
1204 }
1205 
1206 
1207 /* Process all of the deferred rescans or deletions.  */
1208 
1209 void
df_process_deferred_rescans(void)1210 df_process_deferred_rescans (void)
1211 {
1212   bool no_insn_rescan = false;
1213   bool defer_insn_rescan = false;
1214   bitmap_iterator bi;
1215   unsigned int uid;
1216 
1217   if (df->changeable_flags & DF_NO_INSN_RESCAN)
1218     {
1219       df_clear_flags (DF_NO_INSN_RESCAN);
1220       no_insn_rescan = true;
1221     }
1222 
1223   if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1224     {
1225       df_clear_flags (DF_DEFER_INSN_RESCAN);
1226       defer_insn_rescan = true;
1227     }
1228 
1229   if (dump_file)
1230     fprintf (dump_file, "starting the processing of deferred insns\n");
1231 
1232   auto_bitmap tmp (&df_bitmap_obstack);
1233   bitmap_copy (tmp, &df->insns_to_delete);
1234   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1235     {
1236       struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1237       if (insn_info)
1238 	df_insn_info_delete (uid);
1239     }
1240 
1241   bitmap_copy (tmp, &df->insns_to_rescan);
1242   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1243     {
1244       struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1245       if (insn_info)
1246 	df_insn_rescan (insn_info->insn);
1247     }
1248 
1249   bitmap_copy (tmp, &df->insns_to_notes_rescan);
1250   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, uid, bi)
1251     {
1252       struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1253       if (insn_info)
1254 	df_notes_rescan (insn_info->insn);
1255     }
1256 
1257   if (dump_file)
1258     fprintf (dump_file, "ending the processing of deferred insns\n");
1259 
1260   bitmap_clear (&df->insns_to_delete);
1261   bitmap_clear (&df->insns_to_rescan);
1262   bitmap_clear (&df->insns_to_notes_rescan);
1263 
1264   if (no_insn_rescan)
1265     df_set_flags (DF_NO_INSN_RESCAN);
1266   if (defer_insn_rescan)
1267     df_set_flags (DF_DEFER_INSN_RESCAN);
1268 
1269   /* If someone changed regs_ever_live during this pass, fix up the
1270      entry and exit blocks.  */
1271   if (df->redo_entry_and_exit)
1272     {
1273       df_update_entry_exit_and_calls ();
1274       df->redo_entry_and_exit = false;
1275     }
1276 }
1277 
1278 
1279 /* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1280    the uses if INCLUDE_USES. Include the eq_uses if
1281    INCLUDE_EQ_USES.  */
1282 
1283 static unsigned int
df_count_refs(bool include_defs,bool include_uses,bool include_eq_uses)1284 df_count_refs (bool include_defs, bool include_uses,
1285 	       bool include_eq_uses)
1286 {
1287   unsigned int regno;
1288   int size = 0;
1289   unsigned int m = df->regs_inited;
1290 
1291   for (regno = 0; regno < m; regno++)
1292     {
1293       if (include_defs)
1294 	size += DF_REG_DEF_COUNT (regno);
1295       if (include_uses)
1296 	size += DF_REG_USE_COUNT (regno);
1297       if (include_eq_uses)
1298 	size += DF_REG_EQ_USE_COUNT (regno);
1299     }
1300   return size;
1301 }
1302 
1303 
1304 /* Take build ref table for either the uses or defs from the reg-use
1305    or reg-def chains.  This version processes the refs in reg order
1306    which is likely to be best if processing the whole function.  */
1307 
1308 static void
df_reorganize_refs_by_reg_by_reg(struct df_ref_info * ref_info,bool include_defs,bool include_uses,bool include_eq_uses)1309 df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
1310 				  bool include_defs,
1311 				  bool include_uses,
1312 				  bool include_eq_uses)
1313 {
1314   unsigned int m = df->regs_inited;
1315   unsigned int regno;
1316   unsigned int offset = 0;
1317   unsigned int start;
1318 
1319   if (df->changeable_flags & DF_NO_HARD_REGS)
1320     {
1321       start = FIRST_PSEUDO_REGISTER;
1322       memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1323       memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1324     }
1325   else
1326     start = 0;
1327 
1328   ref_info->total_size
1329     = df_count_refs (include_defs, include_uses, include_eq_uses);
1330 
1331   df_check_and_grow_ref_info (ref_info, 1);
1332 
1333   for (regno = start; regno < m; regno++)
1334     {
1335       int count = 0;
1336       ref_info->begin[regno] = offset;
1337       if (include_defs)
1338 	{
1339 	  df_ref ref = DF_REG_DEF_CHAIN (regno);
1340 	  while (ref)
1341 	    {
1342 	      ref_info->refs[offset] = ref;
1343 	      DF_REF_ID (ref) = offset++;
1344 	      count++;
1345 	      ref = DF_REF_NEXT_REG (ref);
1346 	      gcc_checking_assert (offset < ref_info->refs_size);
1347 	    }
1348 	}
1349       if (include_uses)
1350 	{
1351 	  df_ref ref = DF_REG_USE_CHAIN (regno);
1352 	  while (ref)
1353 	    {
1354 	      ref_info->refs[offset] = ref;
1355 	      DF_REF_ID (ref) = offset++;
1356 	      count++;
1357 	      ref = DF_REF_NEXT_REG (ref);
1358 	      gcc_checking_assert (offset < ref_info->refs_size);
1359 	    }
1360 	}
1361       if (include_eq_uses)
1362 	{
1363 	  df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
1364 	  while (ref)
1365 	    {
1366 	      ref_info->refs[offset] = ref;
1367 	      DF_REF_ID (ref) = offset++;
1368 	      count++;
1369 	      ref = DF_REF_NEXT_REG (ref);
1370 	      gcc_checking_assert (offset < ref_info->refs_size);
1371 	    }
1372 	}
1373       ref_info->count[regno] = count;
1374     }
1375 
1376   /* The bitmap size is not decremented when refs are deleted.  So
1377      reset it now that we have squished out all of the empty
1378      slots.  */
1379   ref_info->table_size = offset;
1380 }
1381 
1382 
1383 /* Take build ref table for either the uses or defs from the reg-use
1384    or reg-def chains.  This version processes the refs in insn order
1385    which is likely to be best if processing some segment of the
1386    function.  */
1387 
1388 static void
df_reorganize_refs_by_reg_by_insn(struct df_ref_info * ref_info,bool include_defs,bool include_uses,bool include_eq_uses)1389 df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
1390 				   bool include_defs,
1391 				   bool include_uses,
1392 				   bool include_eq_uses)
1393 {
1394   bitmap_iterator bi;
1395   unsigned int bb_index;
1396   unsigned int m = df->regs_inited;
1397   unsigned int offset = 0;
1398   unsigned int r;
1399   unsigned int start
1400     = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
1401 
1402   memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1403   memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1404 
1405   ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1406   df_check_and_grow_ref_info (ref_info, 1);
1407 
1408   EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1409     {
1410       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1411       rtx_insn *insn;
1412       df_ref def, use;
1413 
1414       if (include_defs)
1415 	FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1416 	  {
1417 	    unsigned int regno = DF_REF_REGNO (def);
1418 	    ref_info->count[regno]++;
1419 	  }
1420       if (include_uses)
1421 	FOR_EACH_ARTIFICIAL_USE (use, bb_index)
1422 	  {
1423 	    unsigned int regno = DF_REF_REGNO (use);
1424 	    ref_info->count[regno]++;
1425 	  }
1426 
1427       FOR_BB_INSNS (bb, insn)
1428 	{
1429 	  if (INSN_P (insn))
1430 	    {
1431 	      struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
1432 
1433 	      if (include_defs)
1434 		FOR_EACH_INSN_INFO_DEF (def, insn_info)
1435 		  {
1436 		    unsigned int regno = DF_REF_REGNO (def);
1437 		    ref_info->count[regno]++;
1438 		  }
1439 	      if (include_uses)
1440 		FOR_EACH_INSN_INFO_USE (use, insn_info)
1441 		  {
1442 		    unsigned int regno = DF_REF_REGNO (use);
1443 		    ref_info->count[regno]++;
1444 		  }
1445 	      if (include_eq_uses)
1446 		FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
1447 		  {
1448 		    unsigned int regno = DF_REF_REGNO (use);
1449 		    ref_info->count[regno]++;
1450 		  }
1451 	    }
1452 	}
1453     }
1454 
1455   for (r = start; r < m; r++)
1456     {
1457       ref_info->begin[r] = offset;
1458       offset += ref_info->count[r];
1459       ref_info->count[r] = 0;
1460     }
1461 
1462   EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1463     {
1464       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1465       rtx_insn *insn;
1466       df_ref def, use;
1467 
1468       if (include_defs)
1469 	FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1470 	  {
1471 	    unsigned int regno = DF_REF_REGNO (def);
1472 	    if (regno >= start)
1473 	      {
1474 		unsigned int id
1475 		  = ref_info->begin[regno] + ref_info->count[regno]++;
1476 		DF_REF_ID (def) = id;
1477 		ref_info->refs[id] = def;
1478 	      }
1479 	  }
1480       if (include_uses)
1481 	FOR_EACH_ARTIFICIAL_USE (use, bb_index)
1482 	  {
1483 	    unsigned int regno = DF_REF_REGNO (def);
1484 	    if (regno >= start)
1485 	      {
1486 		unsigned int id
1487 		  = ref_info->begin[regno] + ref_info->count[regno]++;
1488 		DF_REF_ID (use) = id;
1489 		ref_info->refs[id] = use;
1490 	      }
1491 	  }
1492 
1493       FOR_BB_INSNS (bb, insn)
1494 	{
1495 	  if (INSN_P (insn))
1496 	    {
1497 	      struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
1498 
1499 	      if (include_defs)
1500 		FOR_EACH_INSN_INFO_DEF (def, insn_info)
1501 		  {
1502 		    unsigned int regno = DF_REF_REGNO (def);
1503 		    if (regno >= start)
1504 		      {
1505 			unsigned int id
1506 			  = ref_info->begin[regno] + ref_info->count[regno]++;
1507 			DF_REF_ID (def) = id;
1508 			ref_info->refs[id] = def;
1509 		      }
1510 		  }
1511 	      if (include_uses)
1512 		FOR_EACH_INSN_INFO_USE (use, insn_info)
1513 		  {
1514 		    unsigned int regno = DF_REF_REGNO (use);
1515 		    if (regno >= start)
1516 		      {
1517 			unsigned int id
1518 			  = ref_info->begin[regno] + ref_info->count[regno]++;
1519 			DF_REF_ID (use) = id;
1520 			ref_info->refs[id] = use;
1521 		      }
1522 		  }
1523 	      if (include_eq_uses)
1524 		FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
1525 		  {
1526 		    unsigned int regno = DF_REF_REGNO (use);
1527 		    if (regno >= start)
1528 		      {
1529 			unsigned int id
1530 			  = ref_info->begin[regno] + ref_info->count[regno]++;
1531 			DF_REF_ID (use) = id;
1532 			ref_info->refs[id] = use;
1533 		      }
1534 		  }
1535 	    }
1536 	}
1537     }
1538 
1539   /* The bitmap size is not decremented when refs are deleted.  So
1540      reset it now that we have squished out all of the empty
1541      slots.  */
1542 
1543   ref_info->table_size = offset;
1544 }
1545 
1546 /* Take build ref table for either the uses or defs from the reg-use
1547    or reg-def chains.  */
1548 
1549 static void
df_reorganize_refs_by_reg(struct df_ref_info * ref_info,bool include_defs,bool include_uses,bool include_eq_uses)1550 df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
1551 			   bool include_defs,
1552 			   bool include_uses,
1553 			   bool include_eq_uses)
1554 {
1555   if (df->analyze_subset)
1556     df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
1557 				       include_uses, include_eq_uses);
1558   else
1559     df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
1560 				       include_uses, include_eq_uses);
1561 }
1562 
1563 
1564 /* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET.  */
1565 static unsigned int
df_add_refs_to_table(unsigned int offset,struct df_ref_info * ref_info,df_ref ref)1566 df_add_refs_to_table (unsigned int offset,
1567 		      struct df_ref_info *ref_info,
1568 		      df_ref ref)
1569 {
1570   for (; ref; ref = DF_REF_NEXT_LOC (ref))
1571     if (!(df->changeable_flags & DF_NO_HARD_REGS)
1572 	|| (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1573       {
1574 	ref_info->refs[offset] = ref;
1575 	DF_REF_ID (ref) = offset++;
1576       }
1577   return offset;
1578 }
1579 
1580 
1581 /* Count the number of refs in all of the insns of BB. Include the
1582    defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1583    eq_uses if INCLUDE_EQ_USES.  */
1584 
1585 static unsigned int
df_reorganize_refs_by_insn_bb(basic_block bb,unsigned int offset,struct df_ref_info * ref_info,bool include_defs,bool include_uses,bool include_eq_uses)1586 df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
1587 			       struct df_ref_info *ref_info,
1588 			       bool include_defs, bool include_uses,
1589 			       bool include_eq_uses)
1590 {
1591   rtx_insn *insn;
1592 
1593   if (include_defs)
1594     offset = df_add_refs_to_table (offset, ref_info,
1595 				   df_get_artificial_defs (bb->index));
1596   if (include_uses)
1597     offset = df_add_refs_to_table (offset, ref_info,
1598 				   df_get_artificial_uses (bb->index));
1599 
1600   FOR_BB_INSNS (bb, insn)
1601     if (INSN_P (insn))
1602       {
1603 	unsigned int uid = INSN_UID (insn);
1604 	if (include_defs)
1605 	  offset = df_add_refs_to_table (offset, ref_info,
1606 					 DF_INSN_UID_DEFS (uid));
1607 	if (include_uses)
1608 	  offset = df_add_refs_to_table (offset, ref_info,
1609 					 DF_INSN_UID_USES (uid));
1610 	if (include_eq_uses)
1611 	  offset = df_add_refs_to_table (offset, ref_info,
1612 					 DF_INSN_UID_EQ_USES (uid));
1613       }
1614   return offset;
1615 }
1616 
1617 
1618 /* Organize the refs by insn into the table in REF_INFO.  If
1619    blocks_to_analyze is defined, use that set, otherwise the entire
1620    program.  Include the defs if INCLUDE_DEFS. Include the uses if
1621    INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES.  */
1622 
1623 static void
df_reorganize_refs_by_insn(struct df_ref_info * ref_info,bool include_defs,bool include_uses,bool include_eq_uses)1624 df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
1625 			    bool include_defs, bool include_uses,
1626 			    bool include_eq_uses)
1627 {
1628   basic_block bb;
1629   unsigned int offset = 0;
1630 
1631   ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1632   df_check_and_grow_ref_info (ref_info, 1);
1633   if (df->blocks_to_analyze)
1634     {
1635       bitmap_iterator bi;
1636       unsigned int index;
1637 
1638       EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1639 	{
1640 	  offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK_FOR_FN (cfun,
1641 								      index),
1642 						  offset, ref_info,
1643 						  include_defs, include_uses,
1644 						  include_eq_uses);
1645 	}
1646 
1647       ref_info->table_size = offset;
1648     }
1649   else
1650     {
1651       FOR_ALL_BB_FN (bb, cfun)
1652 	offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1653 						include_defs, include_uses,
1654 						include_eq_uses);
1655       ref_info->table_size = offset;
1656     }
1657 }
1658 
1659 
1660 /* If the use refs in DF are not organized, reorganize them.  */
1661 
1662 void
df_maybe_reorganize_use_refs(enum df_ref_order order)1663 df_maybe_reorganize_use_refs (enum df_ref_order order)
1664 {
1665   if (order == df->use_info.ref_order)
1666     return;
1667 
1668   switch (order)
1669     {
1670     case DF_REF_ORDER_BY_REG:
1671       df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1672       break;
1673 
1674     case DF_REF_ORDER_BY_REG_WITH_NOTES:
1675       df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1676       break;
1677 
1678     case DF_REF_ORDER_BY_INSN:
1679       df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1680       break;
1681 
1682     case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1683       df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1684       break;
1685 
1686     case DF_REF_ORDER_NO_TABLE:
1687       free (df->use_info.refs);
1688       df->use_info.refs = NULL;
1689       df->use_info.refs_size = 0;
1690       break;
1691 
1692     case DF_REF_ORDER_UNORDERED:
1693     case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1694       gcc_unreachable ();
1695       break;
1696     }
1697 
1698   df->use_info.ref_order = order;
1699 }
1700 
1701 
1702 /* If the def refs in DF are not organized, reorganize them.  */
1703 
1704 void
df_maybe_reorganize_def_refs(enum df_ref_order order)1705 df_maybe_reorganize_def_refs (enum df_ref_order order)
1706 {
1707   if (order == df->def_info.ref_order)
1708     return;
1709 
1710   switch (order)
1711     {
1712     case DF_REF_ORDER_BY_REG:
1713       df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1714       break;
1715 
1716     case DF_REF_ORDER_BY_INSN:
1717       df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1718       break;
1719 
1720     case DF_REF_ORDER_NO_TABLE:
1721       free (df->def_info.refs);
1722       df->def_info.refs = NULL;
1723       df->def_info.refs_size = 0;
1724       break;
1725 
1726     case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1727     case DF_REF_ORDER_BY_REG_WITH_NOTES:
1728     case DF_REF_ORDER_UNORDERED:
1729     case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1730       gcc_unreachable ();
1731       break;
1732     }
1733 
1734   df->def_info.ref_order = order;
1735 }
1736 
1737 
1738 /* Change all of the basic block references in INSN to use the insn's
1739    current basic block.  This function is called from routines that move
1740    instructions from one block to another.  */
1741 
1742 void
df_insn_change_bb(rtx_insn * insn,basic_block new_bb)1743 df_insn_change_bb (rtx_insn *insn, basic_block new_bb)
1744 {
1745   basic_block old_bb = BLOCK_FOR_INSN (insn);
1746   struct df_insn_info *insn_info;
1747   unsigned int uid = INSN_UID (insn);
1748 
1749   if (old_bb == new_bb)
1750     return;
1751 
1752   set_block_for_insn (insn, new_bb);
1753 
1754   if (!df)
1755     return;
1756 
1757   if (dump_file)
1758     fprintf (dump_file, "changing bb of uid %d\n", uid);
1759 
1760   insn_info = DF_INSN_UID_SAFE_GET (uid);
1761   if (insn_info == NULL)
1762     {
1763       if (dump_file)
1764 	fprintf (dump_file, "  unscanned insn\n");
1765       df_insn_rescan (insn);
1766       return;
1767     }
1768 
1769   if (!INSN_P (insn))
1770     return;
1771 
1772   df_set_bb_dirty (new_bb);
1773   if (old_bb)
1774     {
1775       if (dump_file)
1776 	fprintf (dump_file, "  from %d to %d\n",
1777 		 old_bb->index, new_bb->index);
1778       df_set_bb_dirty (old_bb);
1779     }
1780   else
1781     if (dump_file)
1782       fprintf (dump_file, "  to %d\n", new_bb->index);
1783 }
1784 
1785 
1786 /* Helper function for df_ref_change_reg_with_loc.  */
1787 
1788 static void
df_ref_change_reg_with_loc_1(struct df_reg_info * old_df,struct df_reg_info * new_df,unsigned int new_regno,rtx loc)1789 df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
1790 			      struct df_reg_info *new_df,
1791 			      unsigned int new_regno, rtx loc)
1792 {
1793   df_ref the_ref = old_df->reg_chain;
1794 
1795   while (the_ref)
1796     {
1797       if ((!DF_REF_IS_ARTIFICIAL (the_ref))
1798 	  && DF_REF_LOC (the_ref)
1799 	  && (*DF_REF_LOC (the_ref) == loc))
1800 	{
1801 	  df_ref next_ref = DF_REF_NEXT_REG (the_ref);
1802 	  df_ref prev_ref = DF_REF_PREV_REG (the_ref);
1803 	  df_ref *ref_ptr;
1804 	  struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
1805 
1806 	  DF_REF_REGNO (the_ref) = new_regno;
1807 	  DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
1808 
1809 	  /* Pull the_ref out of the old regno chain.  */
1810 	  if (prev_ref)
1811 	    DF_REF_NEXT_REG (prev_ref) = next_ref;
1812 	  else
1813 	    old_df->reg_chain = next_ref;
1814 	  if (next_ref)
1815 	    DF_REF_PREV_REG (next_ref) = prev_ref;
1816 	  old_df->n_refs--;
1817 
1818 	  /* Put the ref into the new regno chain.  */
1819 	  DF_REF_PREV_REG (the_ref) = NULL;
1820 	  DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
1821 	  if (new_df->reg_chain)
1822 	    DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
1823 	  new_df->reg_chain = the_ref;
1824 	  new_df->n_refs++;
1825 	  if (DF_REF_BB (the_ref))
1826 	    df_set_bb_dirty (DF_REF_BB (the_ref));
1827 
1828 	  /* Need to sort the record again that the ref was in because
1829 	     the regno is a sorting key.  First, find the right
1830 	     record.  */
1831 	  if (DF_REF_REG_DEF_P (the_ref))
1832 	    ref_ptr = &insn_info->defs;
1833 	  else if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
1834 	    ref_ptr = &insn_info->eq_uses;
1835 	  else
1836 	    ref_ptr = &insn_info->uses;
1837 	  if (dump_file)
1838 	    fprintf (dump_file, "changing reg in insn %d\n",
1839 		     DF_REF_INSN_UID (the_ref));
1840 
1841 	  /* Stop if we find the current reference or where the reference
1842 	     needs to be.  */
1843 	  while (*ref_ptr != the_ref && df_ref_compare (*ref_ptr, the_ref) < 0)
1844 	    ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1845 	  if (*ref_ptr != the_ref)
1846 	    {
1847 	      /* The reference needs to be promoted up the list.  */
1848 	      df_ref next = DF_REF_NEXT_LOC (the_ref);
1849 	      DF_REF_NEXT_LOC (the_ref) = *ref_ptr;
1850 	      *ref_ptr = the_ref;
1851 	      do
1852 		ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1853 	      while (*ref_ptr != the_ref);
1854 	      *ref_ptr = next;
1855 	    }
1856 	  else if (DF_REF_NEXT_LOC (the_ref)
1857 		   && df_ref_compare (the_ref, DF_REF_NEXT_LOC (the_ref)) > 0)
1858 	    {
1859 	      /* The reference needs to be demoted down the list.  */
1860 	      *ref_ptr = DF_REF_NEXT_LOC (the_ref);
1861 	      do
1862 		ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1863 	      while (*ref_ptr && df_ref_compare (the_ref, *ref_ptr) > 0);
1864 	      DF_REF_NEXT_LOC (the_ref) = *ref_ptr;
1865 	      *ref_ptr = the_ref;
1866 	    }
1867 
1868 	  the_ref = next_ref;
1869 	}
1870       else
1871 	the_ref = DF_REF_NEXT_REG (the_ref);
1872     }
1873 }
1874 
1875 
1876 /* Change the regno of register LOC to NEW_REGNO and update the df
1877    information accordingly.  Refs that do not match LOC are not changed
1878    which means that artificial refs are not changed since they have no loc.
1879    This call is to support the SET_REGNO macro. */
1880 
1881 void
df_ref_change_reg_with_loc(rtx loc,unsigned int new_regno)1882 df_ref_change_reg_with_loc (rtx loc, unsigned int new_regno)
1883 {
1884   unsigned int old_regno = REGNO (loc);
1885   if (old_regno == new_regno)
1886     return;
1887 
1888   if (df)
1889     {
1890       df_grow_reg_info ();
1891 
1892       df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
1893 				    DF_REG_DEF_GET (new_regno),
1894 				    new_regno, loc);
1895       df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
1896 				    DF_REG_USE_GET (new_regno),
1897 				    new_regno, loc);
1898       df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
1899 				    DF_REG_EQ_USE_GET (new_regno),
1900 				    new_regno, loc);
1901     }
1902   set_mode_and_regno (loc, GET_MODE (loc), new_regno);
1903 }
1904 
1905 
1906 /* Delete the mw_hardregs that point into the eq_notes.  */
1907 
1908 static void
df_mw_hardreg_chain_delete_eq_uses(struct df_insn_info * insn_info)1909 df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
1910 {
1911   struct df_mw_hardreg **mw_ptr = &insn_info->mw_hardregs;
1912   struct df_scan_problem_data *problem_data
1913     = (struct df_scan_problem_data *) df_scan->problem_data;
1914 
1915   while (*mw_ptr)
1916     {
1917       df_mw_hardreg *mw = *mw_ptr;
1918       if (mw->flags & DF_REF_IN_NOTE)
1919 	{
1920 	  *mw_ptr = DF_MWS_NEXT (mw);
1921 	  problem_data->mw_reg_pool->remove (mw);
1922 	}
1923       else
1924 	mw_ptr = &DF_MWS_NEXT (mw);
1925     }
1926 }
1927 
1928 
1929 /* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN.  */
1930 
1931 void
df_notes_rescan(rtx_insn * insn)1932 df_notes_rescan (rtx_insn *insn)
1933 {
1934   struct df_insn_info *insn_info;
1935   unsigned int uid = INSN_UID (insn);
1936 
1937   if (!df)
1938     return;
1939 
1940   /* The client has disabled rescanning and plans to do it itself.  */
1941   if (df->changeable_flags & DF_NO_INSN_RESCAN)
1942     return;
1943 
1944   /* Do nothing if the insn hasn't been emitted yet.  */
1945   if (!BLOCK_FOR_INSN (insn))
1946     return;
1947 
1948   df_grow_bb_info (df_scan);
1949   df_grow_reg_info ();
1950 
1951   insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1952 
1953   /* The client has deferred rescanning.  */
1954   if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1955     {
1956       if (!insn_info)
1957 	{
1958 	  insn_info = df_insn_create_insn_record (insn);
1959 	  insn_info->defs = 0;
1960 	  insn_info->uses = 0;
1961 	  insn_info->eq_uses = 0;
1962 	  insn_info->mw_hardregs = 0;
1963 	}
1964 
1965       bitmap_clear_bit (&df->insns_to_delete, uid);
1966       /* If the insn is set to be rescanned, it does not need to also
1967 	 be notes rescanned.  */
1968       if (!bitmap_bit_p (&df->insns_to_rescan, uid))
1969 	bitmap_set_bit (&df->insns_to_notes_rescan, INSN_UID (insn));
1970       return;
1971     }
1972 
1973   bitmap_clear_bit (&df->insns_to_delete, uid);
1974   bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1975 
1976   if (insn_info)
1977     {
1978       basic_block bb = BLOCK_FOR_INSN (insn);
1979       rtx note;
1980       class df_collection_rec collection_rec;
1981       unsigned int i;
1982 
1983       df_mw_hardreg_chain_delete_eq_uses (insn_info);
1984       df_ref_chain_delete (insn_info->eq_uses);
1985       insn_info->eq_uses = NULL;
1986 
1987       /* Process REG_EQUIV/REG_EQUAL notes */
1988       for (note = REG_NOTES (insn); note;
1989 	   note = XEXP (note, 1))
1990 	{
1991 	  switch (REG_NOTE_KIND (note))
1992 	    {
1993 	    case REG_EQUIV:
1994 	    case REG_EQUAL:
1995 	      df_uses_record (&collection_rec,
1996 			      &XEXP (note, 0), DF_REF_REG_USE,
1997 			      bb, insn_info, DF_REF_IN_NOTE);
1998 	    default:
1999 	      break;
2000 	    }
2001 	}
2002 
2003       /* Find some place to put any new mw_hardregs.  */
2004       df_canonize_collection_rec (&collection_rec);
2005       struct df_mw_hardreg **mw_ptr = &insn_info->mw_hardregs, *mw;
2006       FOR_EACH_VEC_ELT (collection_rec.mw_vec, i, mw)
2007 	{
2008 	  while (*mw_ptr && df_mw_compare (*mw_ptr, mw) < 0)
2009 	    mw_ptr = &DF_MWS_NEXT (*mw_ptr);
2010 	  DF_MWS_NEXT (mw) = *mw_ptr;
2011 	  *mw_ptr = mw;
2012 	  mw_ptr = &DF_MWS_NEXT (mw);
2013 	}
2014       df_refs_add_to_chains (&collection_rec, bb, insn, copy_eq_uses);
2015     }
2016   else
2017     df_insn_rescan (insn);
2018 
2019 }
2020 
2021 
2022 /*----------------------------------------------------------------------------
2023    Hard core instruction scanning code.  No external interfaces here,
2024    just a lot of routines that look inside insns.
2025 ----------------------------------------------------------------------------*/
2026 
2027 
2028 /* Return true if the contents of two df_ref's are identical.
2029    It ignores DF_REF_MARKER.  */
2030 
2031 static bool
df_ref_equal_p(df_ref ref1,df_ref ref2)2032 df_ref_equal_p (df_ref ref1, df_ref ref2)
2033 {
2034   if (!ref2)
2035     return false;
2036 
2037   if (ref1 == ref2)
2038     return true;
2039 
2040   if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2041       || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2042       || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2043       || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
2044       || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
2045 	  != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2046       || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2047       || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2048     return false;
2049 
2050   switch (DF_REF_CLASS (ref1))
2051     {
2052     case DF_REF_ARTIFICIAL:
2053     case DF_REF_BASE:
2054       return true;
2055 
2056     case DF_REF_REGULAR:
2057       return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
2058 
2059     default:
2060       gcc_unreachable ();
2061     }
2062 }
2063 
2064 
2065 /* Compare REF1 and REF2 for sorting.  This is only called from places
2066    where all of the refs are of the same type, in the same insn, and
2067    have the same bb.  So these fields are not checked.  */
2068 
2069 static int
df_ref_compare(df_ref ref1,df_ref ref2)2070 df_ref_compare (df_ref ref1, df_ref ref2)
2071 {
2072   if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2073     return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2074 
2075   if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2076     return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
2077 
2078   if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2079     return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2080 
2081   if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2082     return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2083 
2084   /* Cannot look at the LOC field on artificial refs.  */
2085   if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2086       && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
2087     return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2088 
2089   if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2090     {
2091       /* If two refs are identical except that one of them has is from
2092 	 a mw and one is not, we need to have the one with the mw
2093 	 first.  */
2094       if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2095 	  DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2096 	return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2097       else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2098 	return -1;
2099       else
2100 	return 1;
2101     }
2102 
2103   return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2104 }
2105 
2106 /* Like df_ref_compare, but compare two df_ref* pointers R1 and R2.  */
2107 
2108 static int
df_ref_ptr_compare(const void * r1,const void * r2)2109 df_ref_ptr_compare (const void *r1, const void *r2)
2110 {
2111   return df_ref_compare (*(const df_ref *) r1, *(const df_ref *) r2);
2112 }
2113 
2114 /* Sort and compress a set of refs.  */
2115 
2116 static void
df_sort_and_compress_refs(vec<df_ref,va_heap> * ref_vec)2117 df_sort_and_compress_refs (vec<df_ref, va_heap> *ref_vec)
2118 {
2119   unsigned int count;
2120   unsigned int i;
2121   unsigned int dist = 0;
2122 
2123   count = ref_vec->length ();
2124 
2125   /* If there are 1 or 0 elements, there is nothing to do.  */
2126   if (count < 2)
2127     return;
2128   else if (count == 2)
2129     {
2130       df_ref r0 = (*ref_vec)[0];
2131       df_ref r1 = (*ref_vec)[1];
2132       if (df_ref_compare (r0, r1) > 0)
2133 	std::swap ((*ref_vec)[0], (*ref_vec)[1]);
2134     }
2135   else
2136     {
2137       for (i = 0; i < count - 1; i++)
2138 	{
2139 	  df_ref r0 = (*ref_vec)[i];
2140 	  df_ref r1 = (*ref_vec)[i + 1];
2141 	  if (df_ref_compare (r0, r1) >= 0)
2142 	    break;
2143 	}
2144       /* If the array is already strictly ordered,
2145          which is the most common case for large COUNT case
2146          (which happens for CALL INSNs),
2147          no need to sort and filter out duplicate.
2148          Simply return the count.
2149          Make sure DF_GET_ADD_REFS adds refs in the increasing order
2150          of DF_REF_COMPARE.  */
2151       if (i == count - 1)
2152         return;
2153       ref_vec->qsort (df_ref_ptr_compare);
2154     }
2155 
2156   for (i=0; i<count-dist; i++)
2157     {
2158       /* Find the next ref that is not equal to the current ref.  */
2159       while (i + dist + 1 < count
2160 	     && df_ref_equal_p ((*ref_vec)[i],
2161 				(*ref_vec)[i + dist + 1]))
2162 	{
2163 	  df_free_ref ((*ref_vec)[i + dist + 1]);
2164 	  dist++;
2165 	}
2166       /* Copy it down to the next position.  */
2167       if (dist && i + dist + 1 < count)
2168 	(*ref_vec)[i + 1] = (*ref_vec)[i + dist + 1];
2169     }
2170 
2171   count -= dist;
2172   ref_vec->truncate (count);
2173 }
2174 
2175 
2176 /* Return true if the contents of two df_ref's are identical.
2177    It ignores DF_REF_MARKER.  */
2178 
2179 static bool
df_mw_equal_p(struct df_mw_hardreg * mw1,struct df_mw_hardreg * mw2)2180 df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2181 {
2182   if (!mw2)
2183     return false;
2184   return (mw1 == mw2) ||
2185     (mw1->mw_reg == mw2->mw_reg
2186      && mw1->type == mw2->type
2187      && mw1->flags == mw2->flags
2188      && mw1->start_regno == mw2->start_regno
2189      && mw1->end_regno == mw2->end_regno);
2190 }
2191 
2192 
2193 /* Compare MW1 and MW2 for sorting.  */
2194 
2195 static int
df_mw_compare(const df_mw_hardreg * mw1,const df_mw_hardreg * mw2)2196 df_mw_compare (const df_mw_hardreg *mw1, const df_mw_hardreg *mw2)
2197 {
2198   if (mw1->type != mw2->type)
2199     return mw1->type - mw2->type;
2200 
2201   if (mw1->flags != mw2->flags)
2202     return mw1->flags - mw2->flags;
2203 
2204   if (mw1->start_regno != mw2->start_regno)
2205     return mw1->start_regno - mw2->start_regno;
2206 
2207   if (mw1->end_regno != mw2->end_regno)
2208     return mw1->end_regno - mw2->end_regno;
2209 
2210   return mw1->mw_order - mw2->mw_order;
2211 }
2212 
2213 /* Like df_mw_compare, but compare two df_mw_hardreg** pointers R1 and R2.  */
2214 
2215 static int
df_mw_ptr_compare(const void * m1,const void * m2)2216 df_mw_ptr_compare (const void *m1, const void *m2)
2217 {
2218   return df_mw_compare (*(const df_mw_hardreg *const *) m1,
2219 			*(const df_mw_hardreg *const *) m2);
2220 }
2221 
2222 /* Sort and compress a set of refs.  */
2223 
2224 static void
df_sort_and_compress_mws(vec<df_mw_hardreg *,va_heap> * mw_vec)2225 df_sort_and_compress_mws (vec<df_mw_hardreg *, va_heap> *mw_vec)
2226 {
2227   unsigned int count;
2228   struct df_scan_problem_data *problem_data
2229     = (struct df_scan_problem_data *) df_scan->problem_data;
2230   unsigned int i;
2231   unsigned int dist = 0;
2232 
2233   count = mw_vec->length ();
2234   if (count < 2)
2235     return;
2236   else if (count == 2)
2237     {
2238       struct df_mw_hardreg *m0 = (*mw_vec)[0];
2239       struct df_mw_hardreg *m1 = (*mw_vec)[1];
2240       if (df_mw_compare (m0, m1) > 0)
2241         {
2242           struct df_mw_hardreg *tmp = (*mw_vec)[0];
2243 	  (*mw_vec)[0] = (*mw_vec)[1];
2244 	  (*mw_vec)[1] = tmp;
2245         }
2246     }
2247   else
2248     mw_vec->qsort (df_mw_ptr_compare);
2249 
2250   for (i=0; i<count-dist; i++)
2251     {
2252       /* Find the next ref that is not equal to the current ref.  */
2253       while (i + dist + 1 < count
2254 	     && df_mw_equal_p ((*mw_vec)[i], (*mw_vec)[i + dist + 1]))
2255 	{
2256 	  problem_data->mw_reg_pool->remove ((*mw_vec)[i + dist + 1]);
2257 	  dist++;
2258 	}
2259       /* Copy it down to the next position.  */
2260       if (dist && i + dist + 1 < count)
2261 	(*mw_vec)[i + 1] = (*mw_vec)[i + dist + 1];
2262     }
2263 
2264   count -= dist;
2265   mw_vec->truncate (count);
2266 }
2267 
2268 
2269 /* Sort and remove duplicates from the COLLECTION_REC.  */
2270 
2271 static void
df_canonize_collection_rec(class df_collection_rec * collection_rec)2272 df_canonize_collection_rec (class df_collection_rec *collection_rec)
2273 {
2274   df_sort_and_compress_refs (&collection_rec->def_vec);
2275   df_sort_and_compress_refs (&collection_rec->use_vec);
2276   df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2277   df_sort_and_compress_mws (&collection_rec->mw_vec);
2278 }
2279 
2280 
2281 /* Add the new df_ref to appropriate reg_info/ref_info chains.  */
2282 
2283 static void
df_install_ref(df_ref this_ref,struct df_reg_info * reg_info,struct df_ref_info * ref_info,bool add_to_table)2284 df_install_ref (df_ref this_ref,
2285 		struct df_reg_info *reg_info,
2286 		struct df_ref_info *ref_info,
2287 		bool add_to_table)
2288 {
2289   unsigned int regno = DF_REF_REGNO (this_ref);
2290   /* Add the ref to the reg_{def,use,eq_use} chain.  */
2291   df_ref head = reg_info->reg_chain;
2292 
2293   reg_info->reg_chain = this_ref;
2294   reg_info->n_refs++;
2295 
2296   if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2297     {
2298       gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2299       df->hard_regs_live_count[regno]++;
2300     }
2301 
2302   gcc_checking_assert (DF_REF_NEXT_REG (this_ref) == NULL
2303 		       && DF_REF_PREV_REG (this_ref) == NULL);
2304 
2305   DF_REF_NEXT_REG (this_ref) = head;
2306 
2307   /* We cannot actually link to the head of the chain.  */
2308   DF_REF_PREV_REG (this_ref) = NULL;
2309 
2310   if (head)
2311     DF_REF_PREV_REG (head) = this_ref;
2312 
2313   if (add_to_table)
2314     {
2315       gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2316       df_check_and_grow_ref_info (ref_info, 1);
2317       DF_REF_ID (this_ref) = ref_info->table_size;
2318       /* Add the ref to the big array of defs.  */
2319       ref_info->refs[ref_info->table_size] = this_ref;
2320       ref_info->table_size++;
2321     }
2322   else
2323     DF_REF_ID (this_ref) = -1;
2324 
2325   ref_info->total_size++;
2326 }
2327 
2328 
2329 /* This function takes one of the groups of refs (defs, uses or
2330    eq_uses) and installs the entire group into the insn.  It also adds
2331    each of these refs into the appropriate chains.  */
2332 
2333 static df_ref
df_install_refs(basic_block bb,const vec<df_ref,va_heap> * old_vec,struct df_reg_info ** reg_info,struct df_ref_info * ref_info,bool is_notes)2334 df_install_refs (basic_block bb,
2335 		 const vec<df_ref, va_heap> *old_vec,
2336 		 struct df_reg_info **reg_info,
2337 		 struct df_ref_info *ref_info,
2338 		 bool is_notes)
2339 {
2340   unsigned int count = old_vec->length ();
2341   if (count)
2342     {
2343       bool add_to_table;
2344       df_ref this_ref;
2345       unsigned int ix;
2346 
2347       switch (ref_info->ref_order)
2348 	{
2349 	case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2350 	case DF_REF_ORDER_BY_REG_WITH_NOTES:
2351 	case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2352 	  ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2353 	  add_to_table = true;
2354 	  break;
2355 	case DF_REF_ORDER_UNORDERED:
2356 	case DF_REF_ORDER_BY_REG:
2357 	case DF_REF_ORDER_BY_INSN:
2358 	  ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2359 	  add_to_table = !is_notes;
2360 	  break;
2361 	default:
2362 	  add_to_table = false;
2363 	  break;
2364 	}
2365 
2366       /* Do not add if ref is not in the right blocks.  */
2367       if (add_to_table && df->analyze_subset)
2368 	add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2369 
2370       FOR_EACH_VEC_ELT (*old_vec, ix, this_ref)
2371 	{
2372 	  DF_REF_NEXT_LOC (this_ref) = (ix + 1 < old_vec->length ()
2373 					? (*old_vec)[ix + 1]
2374 					: NULL);
2375 	  df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
2376 			  ref_info, add_to_table);
2377 	}
2378       return (*old_vec)[0];
2379     }
2380   else
2381     return 0;
2382 }
2383 
2384 
2385 /* This function takes the mws installs the entire group into the
2386    insn.  */
2387 
2388 static struct df_mw_hardreg *
df_install_mws(const vec<df_mw_hardreg *,va_heap> * old_vec)2389 df_install_mws (const vec<df_mw_hardreg *, va_heap> *old_vec)
2390 {
2391   unsigned int count = old_vec->length ();
2392   if (count)
2393     {
2394       for (unsigned int i = 0; i < count - 1; i++)
2395 	DF_MWS_NEXT ((*old_vec)[i]) = (*old_vec)[i + 1];
2396       DF_MWS_NEXT ((*old_vec)[count - 1]) = 0;
2397       return (*old_vec)[0];
2398     }
2399   else
2400     return 0;
2401 }
2402 
2403 
2404 /* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2405    chains and update other necessary information.  */
2406 
2407 static void
df_refs_add_to_chains(class df_collection_rec * collection_rec,basic_block bb,rtx_insn * insn,unsigned int flags)2408 df_refs_add_to_chains (class df_collection_rec *collection_rec,
2409 		       basic_block bb, rtx_insn *insn, unsigned int flags)
2410 {
2411   if (insn)
2412     {
2413       struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
2414       /* If there is a vector in the collection rec, add it to the
2415 	 insn.  A null rec is a signal that the caller will handle the
2416 	 chain specially.  */
2417       if (flags & copy_defs)
2418 	{
2419 	  gcc_checking_assert (!insn_rec->defs);
2420 	  insn_rec->defs
2421 	    = df_install_refs (bb, &collection_rec->def_vec,
2422 			       df->def_regs,
2423 			       &df->def_info, false);
2424 	}
2425       if (flags & copy_uses)
2426 	{
2427 	  gcc_checking_assert (!insn_rec->uses);
2428 	  insn_rec->uses
2429 	    = df_install_refs (bb, &collection_rec->use_vec,
2430 			       df->use_regs,
2431 			       &df->use_info, false);
2432 	}
2433       if (flags & copy_eq_uses)
2434 	{
2435 	  gcc_checking_assert (!insn_rec->eq_uses);
2436 	  insn_rec->eq_uses
2437 	    = df_install_refs (bb, &collection_rec->eq_use_vec,
2438 			       df->eq_use_regs,
2439 			       &df->use_info, true);
2440 	}
2441       if (flags & copy_mw)
2442 	{
2443 	  gcc_checking_assert (!insn_rec->mw_hardregs);
2444 	  insn_rec->mw_hardregs
2445 	    = df_install_mws (&collection_rec->mw_vec);
2446 	}
2447     }
2448   else
2449     {
2450       struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2451 
2452       gcc_checking_assert (!bb_info->artificial_defs);
2453       bb_info->artificial_defs
2454 	= df_install_refs (bb, &collection_rec->def_vec,
2455 			   df->def_regs,
2456 			   &df->def_info, false);
2457       gcc_checking_assert (!bb_info->artificial_uses);
2458       bb_info->artificial_uses
2459 	= df_install_refs (bb, &collection_rec->use_vec,
2460 			   df->use_regs,
2461 			   &df->use_info, false);
2462     }
2463 }
2464 
2465 
2466 /* Allocate a ref and initialize its fields.  */
2467 
2468 static df_ref
df_ref_create_structure(enum df_ref_class cl,class df_collection_rec * collection_rec,rtx reg,rtx * loc,basic_block bb,struct df_insn_info * info,enum df_ref_type ref_type,int ref_flags)2469 df_ref_create_structure (enum df_ref_class cl,
2470 			 class df_collection_rec *collection_rec,
2471 			 rtx reg, rtx *loc,
2472 			 basic_block bb, struct df_insn_info *info,
2473 			 enum df_ref_type ref_type,
2474 			 int ref_flags)
2475 {
2476   df_ref this_ref = NULL;
2477   unsigned int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2478   struct df_scan_problem_data *problem_data
2479     = (struct df_scan_problem_data *) df_scan->problem_data;
2480 
2481   switch (cl)
2482     {
2483     case DF_REF_BASE:
2484       this_ref = (df_ref) (problem_data->ref_base_pool->allocate ());
2485       gcc_checking_assert (loc == NULL);
2486       break;
2487 
2488     case DF_REF_ARTIFICIAL:
2489       this_ref = (df_ref) (problem_data->ref_artificial_pool->allocate ());
2490       this_ref->artificial_ref.bb = bb;
2491       gcc_checking_assert (loc == NULL);
2492       break;
2493 
2494     case DF_REF_REGULAR:
2495       this_ref = (df_ref) (problem_data->ref_regular_pool->allocate ());
2496       this_ref->regular_ref.loc = loc;
2497       gcc_checking_assert (loc);
2498       break;
2499     }
2500 
2501   DF_REF_CLASS (this_ref) = cl;
2502   DF_REF_ID (this_ref) = -1;
2503   DF_REF_REG (this_ref) = reg;
2504   DF_REF_REGNO (this_ref) =  regno;
2505   DF_REF_TYPE (this_ref) = ref_type;
2506   DF_REF_INSN_INFO (this_ref) = info;
2507   DF_REF_CHAIN (this_ref) = NULL;
2508   DF_REF_FLAGS (this_ref) = ref_flags;
2509   DF_REF_NEXT_REG (this_ref) = NULL;
2510   DF_REF_PREV_REG (this_ref) = NULL;
2511   DF_REF_ORDER (this_ref) = df->ref_order++;
2512 
2513   /* We need to clear this bit because fwprop, and in the future
2514      possibly other optimizations sometimes create new refs using ond
2515      refs as the model.  */
2516   DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2517 
2518   /* See if this ref needs to have DF_HARD_REG_LIVE bit set.  */
2519   if (regno < FIRST_PSEUDO_REGISTER
2520       && !DF_REF_IS_ARTIFICIAL (this_ref)
2521       && !DEBUG_INSN_P (DF_REF_INSN (this_ref)))
2522     {
2523       if (DF_REF_REG_DEF_P (this_ref))
2524 	{
2525 	  if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2526 	    DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2527 	}
2528       else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2529 		 && (regno == FRAME_POINTER_REGNUM
2530 		     || regno == ARG_POINTER_REGNUM)))
2531 	DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2532     }
2533 
2534   if (collection_rec)
2535     {
2536       if (DF_REF_REG_DEF_P (this_ref))
2537 	collection_rec->def_vec.safe_push (this_ref);
2538       else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
2539 	collection_rec->eq_use_vec.safe_push (this_ref);
2540       else
2541 	collection_rec->use_vec.safe_push (this_ref);
2542     }
2543   else
2544     df_install_ref_incremental (this_ref);
2545 
2546   return this_ref;
2547 }
2548 
2549 
2550 /* Create new references of type DF_REF_TYPE for each part of register REG
2551    at address LOC within INSN of BB.  */
2552 
2553 
2554 static void
df_ref_record(enum df_ref_class cl,class df_collection_rec * collection_rec,rtx reg,rtx * loc,basic_block bb,struct df_insn_info * insn_info,enum df_ref_type ref_type,int ref_flags)2555 df_ref_record (enum df_ref_class cl,
2556 	       class df_collection_rec *collection_rec,
2557                rtx reg, rtx *loc,
2558 	       basic_block bb, struct df_insn_info *insn_info,
2559 	       enum df_ref_type ref_type,
2560 	       int ref_flags)
2561 {
2562   unsigned int regno;
2563 
2564   gcc_checking_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
2565 
2566   regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2567   if (regno < FIRST_PSEUDO_REGISTER)
2568     {
2569       struct df_mw_hardreg *hardreg = NULL;
2570       struct df_scan_problem_data *problem_data
2571         = (struct df_scan_problem_data *) df_scan->problem_data;
2572       unsigned int i;
2573       unsigned int endregno;
2574       df_ref ref;
2575 
2576       if (GET_CODE (reg) == SUBREG)
2577 	{
2578 	  int off = subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2579 					 SUBREG_BYTE (reg), GET_MODE (reg));
2580 	  unsigned int nregno = regno + off;
2581 	  endregno = nregno + subreg_nregs (reg);
2582 	  if (off < 0 && regno < (unsigned) -off)
2583 	    /* Deal with paradoxical SUBREGs on big endian where
2584 	       in debug insns the hard reg number might be smaller
2585 	       than -off, such as (subreg:DI (reg:SI 0 [+4 ]) 0));
2586 	       RA decisions shouldn't be affected by debug insns
2587 	       and so RA can decide to put pseudo into a hard reg
2588 	       with small REGNO, even when it is referenced in
2589 	       a paradoxical SUBREG in a debug insn.  */
2590 	    regno = 0;
2591 	  else
2592 	    regno = nregno;
2593 	}
2594       else
2595 	endregno = END_REGNO (reg);
2596 
2597       /*  If this is a multiword hardreg, we create some extra
2598 	  datastructures that will enable us to easily build REG_DEAD
2599 	  and REG_UNUSED notes.  */
2600       if (collection_rec
2601 	  && (endregno != regno + 1) && insn_info)
2602 	{
2603 	  /* Sets to a subreg of a multiword register are partial.
2604 	     Sets to a non-subreg of a multiword register are not.  */
2605 	  if (GET_CODE (reg) == SUBREG)
2606 	    ref_flags |= DF_REF_PARTIAL;
2607 	  ref_flags |= DF_REF_MW_HARDREG;
2608 
2609 	  gcc_assert (regno < endregno);
2610 
2611 	  hardreg = problem_data->mw_reg_pool->allocate ();
2612 	  hardreg->type = ref_type;
2613 	  hardreg->flags = ref_flags;
2614 	  hardreg->mw_reg = reg;
2615 	  hardreg->start_regno = regno;
2616 	  hardreg->end_regno = endregno - 1;
2617 	  hardreg->mw_order = df->ref_order++;
2618 	  collection_rec->mw_vec.safe_push (hardreg);
2619 	}
2620 
2621       for (i = regno; i < endregno; i++)
2622 	{
2623 	  ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
2624 					 bb, insn_info, ref_type, ref_flags);
2625 
2626           gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
2627 	}
2628     }
2629   else
2630     {
2631       df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
2632 			       ref_type, ref_flags);
2633     }
2634 }
2635 
2636 
2637 /* Process all the registers defined in the rtx pointed by LOC.
2638    Autoincrement/decrement definitions will be picked up by df_uses_record.
2639    Any change here has to be matched in df_find_hard_reg_defs_1.  */
2640 
2641 static void
df_def_record_1(class df_collection_rec * collection_rec,rtx * loc,basic_block bb,struct df_insn_info * insn_info,int flags)2642 df_def_record_1 (class df_collection_rec *collection_rec,
2643                  rtx *loc, basic_block bb, struct df_insn_info *insn_info,
2644 		 int flags)
2645 {
2646   rtx dst = *loc;
2647 
2648   /* It is legal to have a set destination be a parallel. */
2649   if (GET_CODE (dst) == PARALLEL)
2650     {
2651       int i;
2652       for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2653 	{
2654 	  rtx temp = XVECEXP (dst, 0, i);
2655 	  gcc_assert (GET_CODE (temp) == EXPR_LIST);
2656 	  df_def_record_1 (collection_rec, &XEXP (temp, 0),
2657 			   bb, insn_info, flags);
2658 	}
2659       return;
2660     }
2661 
2662   if (GET_CODE (dst) == STRICT_LOW_PART)
2663     {
2664       flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2665 
2666       loc = &XEXP (dst, 0);
2667       dst = *loc;
2668     }
2669 
2670   if (GET_CODE (dst) == ZERO_EXTRACT)
2671     {
2672       flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
2673 
2674       loc = &XEXP (dst, 0);
2675       dst = *loc;
2676     }
2677 
2678   /* At this point if we do not have a reg or a subreg, just return.  */
2679   if (REG_P (dst))
2680     {
2681       df_ref_record (DF_REF_REGULAR, collection_rec,
2682 		     dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
2683 
2684       /* We want to keep sp alive everywhere - by making all
2685 	 writes to sp also use of sp. */
2686       if (REGNO (dst) == STACK_POINTER_REGNUM)
2687 	df_ref_record (DF_REF_BASE, collection_rec,
2688 		       dst, NULL, bb, insn_info, DF_REF_REG_USE, flags);
2689     }
2690   else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
2691     {
2692       if (read_modify_subreg_p (dst))
2693 	flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
2694 
2695       flags |= DF_REF_SUBREG;
2696 
2697       df_ref_record (DF_REF_REGULAR, collection_rec,
2698 		     dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
2699     }
2700 }
2701 
2702 
2703 /* Process all the registers defined in the pattern rtx, X.  Any change
2704    here has to be matched in df_find_hard_reg_defs.  */
2705 
2706 static void
df_defs_record(class df_collection_rec * collection_rec,rtx x,basic_block bb,struct df_insn_info * insn_info,int flags)2707 df_defs_record (class df_collection_rec *collection_rec,
2708                 rtx x, basic_block bb, struct df_insn_info *insn_info,
2709 		int flags)
2710 {
2711   RTX_CODE code = GET_CODE (x);
2712   int i;
2713 
2714   switch (code)
2715     {
2716     case SET:
2717       df_def_record_1 (collection_rec, &SET_DEST (x), bb, insn_info, flags);
2718       break;
2719 
2720     case CLOBBER:
2721       flags |= DF_REF_MUST_CLOBBER;
2722       df_def_record_1 (collection_rec, &XEXP (x, 0), bb, insn_info, flags);
2723       break;
2724 
2725     case COND_EXEC:
2726       df_defs_record (collection_rec, COND_EXEC_CODE (x),
2727 		      bb, insn_info, DF_REF_CONDITIONAL);
2728       break;
2729 
2730     case PARALLEL:
2731       for (i = 0; i < XVECLEN (x, 0); i++)
2732 	df_defs_record (collection_rec, XVECEXP (x, 0, i),
2733 			bb, insn_info, flags);
2734       break;
2735     default:
2736       /* No DEFs to record in other cases */
2737       break;
2738     }
2739 }
2740 
2741 /* Set bits in *DEFS for hard registers found in the rtx DST, which is the
2742    destination of a set or clobber.  This has to match the logic in
2743    df_defs_record_1.  */
2744 
2745 static void
df_find_hard_reg_defs_1(rtx dst,HARD_REG_SET * defs)2746 df_find_hard_reg_defs_1 (rtx dst, HARD_REG_SET *defs)
2747 {
2748   /* It is legal to have a set destination be a parallel. */
2749   if (GET_CODE (dst) == PARALLEL)
2750     {
2751       int i;
2752       for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2753 	{
2754 	  rtx temp = XVECEXP (dst, 0, i);
2755 	  gcc_assert (GET_CODE (temp) == EXPR_LIST);
2756 	  df_find_hard_reg_defs_1 (XEXP (temp, 0), defs);
2757 	}
2758       return;
2759     }
2760 
2761   if (GET_CODE (dst) == STRICT_LOW_PART)
2762       dst = XEXP (dst, 0);
2763 
2764   if (GET_CODE (dst) == ZERO_EXTRACT)
2765       dst = XEXP (dst, 0);
2766 
2767   /* At this point if we do not have a reg or a subreg, just return.  */
2768   if (REG_P (dst) && HARD_REGISTER_P (dst))
2769     SET_HARD_REG_BIT (*defs, REGNO (dst));
2770   else if (GET_CODE (dst) == SUBREG
2771 	   && REG_P (SUBREG_REG (dst)) && HARD_REGISTER_P (dst))
2772     SET_HARD_REG_BIT (*defs, REGNO (SUBREG_REG (dst)));
2773 }
2774 
2775 /* Set bits in *DEFS for hard registers defined in the pattern X.  This
2776    has to match the logic in df_defs_record.  */
2777 
2778 static void
df_find_hard_reg_defs(rtx x,HARD_REG_SET * defs)2779 df_find_hard_reg_defs (rtx x, HARD_REG_SET *defs)
2780 {
2781   RTX_CODE code = GET_CODE (x);
2782   int i;
2783 
2784   switch (code)
2785     {
2786     case SET:
2787       df_find_hard_reg_defs_1 (SET_DEST (x), defs);
2788       break;
2789 
2790     case CLOBBER:
2791       df_find_hard_reg_defs_1 (XEXP (x, 0), defs);
2792       break;
2793 
2794     case COND_EXEC:
2795       df_find_hard_reg_defs (COND_EXEC_CODE (x), defs);
2796       break;
2797 
2798     case PARALLEL:
2799       for (i = 0; i < XVECLEN (x, 0); i++)
2800 	df_find_hard_reg_defs (XVECEXP (x, 0, i), defs);
2801       break;
2802     default:
2803       /* No DEFs to record in other cases */
2804       break;
2805     }
2806 }
2807 
2808 
2809 /* Process all the registers used in the rtx at address LOC.  */
2810 
2811 static void
df_uses_record(class df_collection_rec * collection_rec,rtx * loc,enum df_ref_type ref_type,basic_block bb,struct df_insn_info * insn_info,int flags)2812 df_uses_record (class df_collection_rec *collection_rec,
2813                 rtx *loc, enum df_ref_type ref_type,
2814 		basic_block bb, struct df_insn_info *insn_info,
2815 		int flags)
2816 {
2817   RTX_CODE code;
2818   rtx x;
2819 
2820  retry:
2821   x = *loc;
2822   if (!x)
2823     return;
2824   code = GET_CODE (x);
2825   switch (code)
2826     {
2827     case LABEL_REF:
2828     case SYMBOL_REF:
2829     case CONST:
2830     CASE_CONST_ANY:
2831     case PC:
2832     case ADDR_VEC:
2833     case ADDR_DIFF_VEC:
2834       return;
2835 
2836     case CLOBBER:
2837       /* If we are clobbering a MEM, mark any registers inside the address
2838 	 as being used.  */
2839       if (MEM_P (XEXP (x, 0)))
2840 	df_uses_record (collection_rec,
2841 			&XEXP (XEXP (x, 0), 0),
2842 			DF_REF_REG_MEM_STORE,
2843 		        bb, insn_info,
2844 			flags);
2845 
2846       /* If we're clobbering a REG then we have a def so ignore.  */
2847       return;
2848 
2849     case MEM:
2850       df_uses_record (collection_rec,
2851 		      &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
2852 		      bb, insn_info, flags & DF_REF_IN_NOTE);
2853       return;
2854 
2855     case SUBREG:
2856       /* While we're here, optimize this case.  */
2857       flags |= DF_REF_PARTIAL;
2858       /* In case the SUBREG is not of a REG, do not optimize.  */
2859       if (!REG_P (SUBREG_REG (x)))
2860 	{
2861 	  loc = &SUBREG_REG (x);
2862 	  df_uses_record (collection_rec, loc, ref_type, bb, insn_info, flags);
2863 	  return;
2864 	}
2865       /* Fall through */
2866 
2867     case REG:
2868       df_ref_record (DF_REF_REGULAR, collection_rec,
2869 		     x, loc, bb, insn_info,
2870 		     ref_type, flags);
2871       return;
2872 
2873     case SIGN_EXTRACT:
2874     case ZERO_EXTRACT:
2875       {
2876         df_uses_record (collection_rec,
2877                         &XEXP (x, 1), ref_type, bb, insn_info, flags);
2878         df_uses_record (collection_rec,
2879                         &XEXP (x, 2), ref_type, bb, insn_info, flags);
2880 
2881         /* If the parameters to the zero or sign extract are
2882            constants, strip them off and recurse, otherwise there is
2883            no information that we can gain from this operation.  */
2884         if (code == ZERO_EXTRACT)
2885           flags |= DF_REF_ZERO_EXTRACT;
2886         else
2887           flags |= DF_REF_SIGN_EXTRACT;
2888 
2889         df_uses_record (collection_rec,
2890                         &XEXP (x, 0), ref_type, bb, insn_info, flags);
2891         return;
2892       }
2893       break;
2894 
2895     case SET:
2896       {
2897 	rtx dst = SET_DEST (x);
2898 	gcc_assert (!(flags & DF_REF_IN_NOTE));
2899 	df_uses_record (collection_rec,
2900 			&SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags);
2901 
2902 	switch (GET_CODE (dst))
2903 	  {
2904 	    case SUBREG:
2905 	      if (read_modify_subreg_p (dst))
2906 		{
2907 		  df_uses_record (collection_rec, &SUBREG_REG (dst),
2908 				  DF_REF_REG_USE, bb, insn_info,
2909 				  flags | DF_REF_READ_WRITE | DF_REF_SUBREG);
2910 		  break;
2911 		}
2912 	      /* Fall through.  */
2913 	    case REG:
2914 	    case PARALLEL:
2915 	    case SCRATCH:
2916 	    case PC:
2917 		break;
2918 	    case MEM:
2919 	      df_uses_record (collection_rec, &XEXP (dst, 0),
2920 			      DF_REF_REG_MEM_STORE, bb, insn_info, flags);
2921 	      break;
2922 	    case STRICT_LOW_PART:
2923 	      {
2924 		rtx *temp = &XEXP (dst, 0);
2925 		/* A strict_low_part uses the whole REG and not just the
2926 		 SUBREG.  */
2927 		dst = XEXP (dst, 0);
2928 		df_uses_record (collection_rec,
2929 				(GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
2930 				DF_REF_REG_USE, bb, insn_info,
2931 				DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART);
2932 	      }
2933 	      break;
2934 	    case ZERO_EXTRACT:
2935 	      {
2936 		df_uses_record (collection_rec, &XEXP (dst, 1),
2937 				DF_REF_REG_USE, bb, insn_info, flags);
2938 		df_uses_record (collection_rec, &XEXP (dst, 2),
2939 				DF_REF_REG_USE, bb, insn_info, flags);
2940                 if (GET_CODE (XEXP (dst,0)) == MEM)
2941                   df_uses_record (collection_rec, &XEXP (dst, 0),
2942                                   DF_REF_REG_USE, bb, insn_info,
2943                                   flags);
2944                 else
2945                   df_uses_record (collection_rec, &XEXP (dst, 0),
2946                                   DF_REF_REG_USE, bb, insn_info,
2947                                   DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT);
2948 	      }
2949 	      break;
2950 
2951 	    default:
2952 	      gcc_unreachable ();
2953 	  }
2954 	return;
2955       }
2956 
2957     case RETURN:
2958     case SIMPLE_RETURN:
2959       break;
2960 
2961     case ASM_OPERANDS:
2962     case UNSPEC_VOLATILE:
2963     case TRAP_IF:
2964     case ASM_INPUT:
2965       {
2966 	/* Traditional and volatile asm instructions must be
2967 	   considered to use and clobber all hard registers, all
2968 	   pseudo-registers and all of memory.  So must TRAP_IF and
2969 	   UNSPEC_VOLATILE operations.
2970 
2971 	   Consider for instance a volatile asm that changes the fpu
2972 	   rounding mode.  An insn should not be moved across this
2973 	   even if it only uses pseudo-regs because it might give an
2974 	   incorrectly rounded result.
2975 
2976 	   However, flow.c's liveness computation did *not* do this,
2977 	   giving the reasoning as " ?!? Unfortunately, marking all
2978 	   hard registers as live causes massive problems for the
2979 	   register allocator and marking all pseudos as live creates
2980 	   mountains of uninitialized variable warnings."
2981 
2982 	   In order to maintain the status quo with regard to liveness
2983 	   and uses, we do what flow.c did and just mark any regs we
2984 	   can find in ASM_OPERANDS as used.  In global asm insns are
2985 	   scanned and regs_asm_clobbered is filled out.
2986 
2987 	   For all ASM_OPERANDS, we must traverse the vector of input
2988 	   operands.  We cannot just fall through here since then we
2989 	   would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
2990 	   which do not indicate traditional asms unlike their normal
2991 	   usage.  */
2992 	if (code == ASM_OPERANDS)
2993 	  {
2994 	    int j;
2995 
2996 	    for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
2997 	      df_uses_record (collection_rec, &ASM_OPERANDS_INPUT (x, j),
2998 			      DF_REF_REG_USE, bb, insn_info, flags);
2999 	    return;
3000 	  }
3001 	break;
3002       }
3003 
3004     case VAR_LOCATION:
3005       df_uses_record (collection_rec,
3006 		      &PAT_VAR_LOCATION_LOC (x),
3007 		      DF_REF_REG_USE, bb, insn_info, flags);
3008       return;
3009 
3010     case PRE_DEC:
3011     case POST_DEC:
3012     case PRE_INC:
3013     case POST_INC:
3014     case PRE_MODIFY:
3015     case POST_MODIFY:
3016       gcc_assert (!DEBUG_INSN_P (insn_info->insn));
3017       /* Catch the def of the register being modified.  */
3018       df_ref_record (DF_REF_REGULAR, collection_rec, XEXP (x, 0), &XEXP (x, 0),
3019 		     bb, insn_info,
3020 		     DF_REF_REG_DEF,
3021                      flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY);
3022 
3023       /* ... Fall through to handle uses ...  */
3024 
3025     default:
3026       break;
3027     }
3028 
3029   /* Recursively scan the operands of this expression.  */
3030   {
3031     const char *fmt = GET_RTX_FORMAT (code);
3032     int i;
3033 
3034     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3035       {
3036 	if (fmt[i] == 'e')
3037 	  {
3038 	    /* Tail recursive case: save a function call level.  */
3039 	    if (i == 0)
3040 	      {
3041 		loc = &XEXP (x, 0);
3042 		goto retry;
3043 	      }
3044 	    df_uses_record (collection_rec, &XEXP (x, i), ref_type,
3045 			    bb, insn_info, flags);
3046 	  }
3047 	else if (fmt[i] == 'E')
3048 	  {
3049 	    int j;
3050 	    for (j = 0; j < XVECLEN (x, i); j++)
3051 	      df_uses_record (collection_rec,
3052 			      &XVECEXP (x, i, j), ref_type,
3053 			      bb, insn_info, flags);
3054 	  }
3055       }
3056   }
3057 
3058   return;
3059 }
3060 
3061 
3062 /* For all DF_REF_CONDITIONAL defs, add a corresponding uses.  */
3063 
3064 static void
df_get_conditional_uses(class df_collection_rec * collection_rec)3065 df_get_conditional_uses (class df_collection_rec *collection_rec)
3066 {
3067   unsigned int ix;
3068   df_ref ref;
3069 
3070   FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
3071     {
3072       if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3073         {
3074           df_ref use;
3075 
3076           use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
3077 					 DF_REF_LOC (ref), DF_REF_BB (ref),
3078 					 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
3079 					 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL);
3080           DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3081         }
3082     }
3083 }
3084 
3085 
3086 /* Get call's extra defs and uses (track caller-saved registers). */
3087 
3088 static void
df_get_call_refs(class df_collection_rec * collection_rec,basic_block bb,struct df_insn_info * insn_info,int flags)3089 df_get_call_refs (class df_collection_rec *collection_rec,
3090                   basic_block bb,
3091                   struct df_insn_info *insn_info,
3092                   int flags)
3093 {
3094   rtx note;
3095   bool is_sibling_call;
3096   unsigned int i;
3097   HARD_REG_SET defs_generated;
3098 
3099   CLEAR_HARD_REG_SET (defs_generated);
3100   df_find_hard_reg_defs (PATTERN (insn_info->insn), &defs_generated);
3101   is_sibling_call = SIBLING_CALL_P (insn_info->insn);
3102   function_abi callee_abi = insn_callee_abi (insn_info->insn);
3103 
3104   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3105     {
3106       if (i == STACK_POINTER_REGNUM
3107 	  && !FAKE_CALL_P (insn_info->insn))
3108 	/* The stack ptr is used (honorarily) by a CALL insn.  */
3109 	df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3110 		       NULL, bb, insn_info, DF_REF_REG_USE,
3111 		       DF_REF_CALL_STACK_USAGE | flags);
3112       else if (global_regs[i])
3113 	{
3114 	  /* Calls to const functions cannot access any global registers and
3115 	     calls to pure functions cannot set them.  All other calls may
3116 	     reference any of the global registers, so they are recorded as
3117 	     used. */
3118 	  if (!RTL_CONST_CALL_P (insn_info->insn))
3119 	    {
3120 	      df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3121 			     NULL, bb, insn_info, DF_REF_REG_USE, flags);
3122 	      if (!RTL_PURE_CALL_P (insn_info->insn))
3123 		df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3124 			       NULL, bb, insn_info, DF_REF_REG_DEF, flags);
3125 	    }
3126 	}
3127       else if (callee_abi.clobbers_full_reg_p (i)
3128 	       /* no clobbers for regs that are the result of the call */
3129 	       && !TEST_HARD_REG_BIT (defs_generated, i)
3130 	       && (!is_sibling_call
3131 		   || !bitmap_bit_p (df->exit_block_uses, i)
3132 		   || refers_to_regno_p (i, crtl->return_rtx)))
3133 	  df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3134 			 NULL, bb, insn_info, DF_REF_REG_DEF,
3135 			 DF_REF_MAY_CLOBBER | flags);
3136     }
3137 
3138   /* Record the registers used to pass arguments, and explicitly
3139      noted as clobbered.  */
3140   for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
3141        note = XEXP (note, 1))
3142     {
3143       if (GET_CODE (XEXP (note, 0)) == USE)
3144         df_uses_record (collection_rec, &XEXP (XEXP (note, 0), 0),
3145 			DF_REF_REG_USE, bb, insn_info, flags);
3146       else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3147 	{
3148 	  if (REG_P (XEXP (XEXP (note, 0), 0)))
3149 	    {
3150 	      unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
3151 	      if (!TEST_HARD_REG_BIT (defs_generated, regno))
3152 		df_defs_record (collection_rec, XEXP (note, 0), bb,
3153 				insn_info, flags);
3154 	    }
3155 	  else
3156 	    df_uses_record (collection_rec, &XEXP (note, 0),
3157 		            DF_REF_REG_USE, bb, insn_info, flags);
3158 	}
3159     }
3160 
3161   return;
3162 }
3163 
3164 /* Collect all refs in the INSN. This function is free of any
3165    side-effect - it will create and return a lists of df_ref's in the
3166    COLLECTION_REC without putting those refs into existing ref chains
3167    and reg chains. */
3168 
3169 static void
df_insn_refs_collect(class df_collection_rec * collection_rec,basic_block bb,struct df_insn_info * insn_info)3170 df_insn_refs_collect (class df_collection_rec *collection_rec,
3171 		      basic_block bb, struct df_insn_info *insn_info)
3172 {
3173   rtx note;
3174   bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
3175 
3176   /* Clear out the collection record.  */
3177   collection_rec->def_vec.truncate (0);
3178   collection_rec->use_vec.truncate (0);
3179   collection_rec->eq_use_vec.truncate (0);
3180   collection_rec->mw_vec.truncate (0);
3181 
3182   /* Process REG_EQUIV/REG_EQUAL notes.  */
3183   for (note = REG_NOTES (insn_info->insn); note;
3184        note = XEXP (note, 1))
3185     {
3186       switch (REG_NOTE_KIND (note))
3187         {
3188         case REG_EQUIV:
3189         case REG_EQUAL:
3190           df_uses_record (collection_rec,
3191                           &XEXP (note, 0), DF_REF_REG_USE,
3192                           bb, insn_info, DF_REF_IN_NOTE);
3193           break;
3194         case REG_NON_LOCAL_GOTO:
3195           /* The frame ptr is used by a non-local goto.  */
3196           df_ref_record (DF_REF_BASE, collection_rec,
3197                          regno_reg_rtx[FRAME_POINTER_REGNUM],
3198                          NULL, bb, insn_info,
3199                          DF_REF_REG_USE, 0);
3200 	  if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3201 	    df_ref_record (DF_REF_BASE, collection_rec,
3202 			   regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3203 			   NULL, bb, insn_info,
3204 			   DF_REF_REG_USE, 0);
3205           break;
3206         default:
3207           break;
3208         }
3209     }
3210 
3211   int flags = (is_cond_exec) ? DF_REF_CONDITIONAL : 0;
3212   /* For CALL_INSNs, first record DF_REF_BASE register defs, as well as
3213      uses from CALL_INSN_FUNCTION_USAGE. */
3214   if (CALL_P (insn_info->insn))
3215     df_get_call_refs (collection_rec, bb, insn_info, flags);
3216 
3217   /* Record other defs.  These should be mostly for DF_REF_REGULAR, so
3218      that a qsort on the defs is unnecessary in most cases.  */
3219   df_defs_record (collection_rec,
3220 		  PATTERN (insn_info->insn), bb, insn_info, 0);
3221 
3222   /* Record the register uses.  */
3223   df_uses_record (collection_rec,
3224 		  &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0);
3225 
3226   /* DF_REF_CONDITIONAL needs corresponding USES. */
3227   if (is_cond_exec)
3228     df_get_conditional_uses (collection_rec);
3229 
3230   df_canonize_collection_rec (collection_rec);
3231 }
3232 
3233 /* Recompute the luids for the insns in BB.  */
3234 
3235 void
df_recompute_luids(basic_block bb)3236 df_recompute_luids (basic_block bb)
3237 {
3238   rtx_insn *insn;
3239   int luid = 0;
3240 
3241   df_grow_insn_info ();
3242 
3243   /* Scan the block an insn at a time from beginning to end.  */
3244   FOR_BB_INSNS (bb, insn)
3245     {
3246       struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3247       /* Inserting labels does not always trigger the incremental
3248 	 rescanning.  */
3249       if (!insn_info)
3250 	{
3251 	  gcc_assert (!INSN_P (insn));
3252 	  insn_info = df_insn_create_insn_record (insn);
3253 	}
3254 
3255       DF_INSN_INFO_LUID (insn_info) = luid;
3256       if (INSN_P (insn))
3257 	luid++;
3258     }
3259 }
3260 
3261 
3262 /* Collect all artificial refs at the block level for BB and add them
3263    to COLLECTION_REC.  */
3264 
3265 static void
df_bb_refs_collect(class df_collection_rec * collection_rec,basic_block bb)3266 df_bb_refs_collect (class df_collection_rec *collection_rec, basic_block bb)
3267 {
3268   collection_rec->def_vec.truncate (0);
3269   collection_rec->use_vec.truncate (0);
3270   collection_rec->eq_use_vec.truncate (0);
3271   collection_rec->mw_vec.truncate (0);
3272 
3273   if (bb->index == ENTRY_BLOCK)
3274     {
3275       df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3276       return;
3277     }
3278   else if (bb->index == EXIT_BLOCK)
3279     {
3280       df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3281       return;
3282     }
3283 
3284   if (bb_has_eh_pred (bb))
3285     {
3286       unsigned int i;
3287       /* Mark the registers that will contain data for the handler.  */
3288       for (i = 0; ; ++i)
3289 	{
3290 	  unsigned regno = EH_RETURN_DATA_REGNO (i);
3291 	  if (regno == INVALID_REGNUM)
3292 	    break;
3293 	  df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3294 			 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3295 	}
3296     }
3297 
3298   /* Add the hard_frame_pointer if this block is the target of a
3299      non-local goto.  */
3300   if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
3301     df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
3302 		   bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
3303 
3304   /* Add the artificial uses.  */
3305   if (bb->index >= NUM_FIXED_BLOCKS)
3306     {
3307       bitmap_iterator bi;
3308       unsigned int regno;
3309       bitmap au = bb_has_eh_pred (bb)
3310 	? &df->eh_block_artificial_uses
3311 	: &df->regular_block_artificial_uses;
3312 
3313       EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
3314 	{
3315 	  df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
3316 			 bb, NULL, DF_REF_REG_USE, 0);
3317 	}
3318     }
3319 
3320   df_canonize_collection_rec (collection_rec);
3321 }
3322 
3323 
3324 /* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS.  */
3325 
3326 void
df_bb_refs_record(int bb_index,bool scan_insns)3327 df_bb_refs_record (int bb_index, bool scan_insns)
3328 {
3329   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3330   rtx_insn *insn;
3331   int luid = 0;
3332 
3333   if (!df)
3334     return;
3335 
3336   df_collection_rec collection_rec;
3337   df_grow_bb_info (df_scan);
3338   if (scan_insns)
3339     /* Scan the block an insn at a time from beginning to end.  */
3340     FOR_BB_INSNS (bb, insn)
3341       {
3342 	struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3343 	gcc_assert (!insn_info);
3344 
3345 	insn_info = df_insn_create_insn_record (insn);
3346 	if (INSN_P (insn))
3347 	  {
3348 	    /* Record refs within INSN.  */
3349 	    DF_INSN_INFO_LUID (insn_info) = luid++;
3350 	    df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
3351 	    df_refs_add_to_chains (&collection_rec, bb, insn, copy_all);
3352 	  }
3353 	DF_INSN_INFO_LUID (insn_info) = luid;
3354       }
3355 
3356   /* Other block level artificial refs */
3357   df_bb_refs_collect (&collection_rec, bb);
3358   df_refs_add_to_chains (&collection_rec, bb, NULL, copy_all);
3359 
3360   /* Now that the block has been processed, set the block as dirty so
3361      LR and LIVE will get it processed.  */
3362   df_set_bb_dirty (bb);
3363 }
3364 
3365 
3366 /* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3367    block. */
3368 
3369 static void
df_get_regular_block_artificial_uses(bitmap regular_block_artificial_uses)3370 df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
3371 {
3372 #ifdef EH_USES
3373   unsigned int i;
3374 #endif
3375 
3376   bitmap_clear (regular_block_artificial_uses);
3377 
3378   if (reload_completed)
3379     {
3380       if (frame_pointer_needed)
3381 	bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3382     }
3383   else
3384     /* Before reload, there are a few registers that must be forced
3385        live everywhere -- which might not already be the case for
3386        blocks within infinite loops.  */
3387     {
3388       unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3389 
3390       /* Any reference to any pseudo before reload is a potential
3391 	 reference of the frame pointer.  */
3392       bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
3393 
3394       if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3395 	bitmap_set_bit (regular_block_artificial_uses,
3396 			HARD_FRAME_POINTER_REGNUM);
3397 
3398       /* Pseudos with argument area equivalences may require
3399 	 reloading via the argument pointer.  */
3400       if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3401 	  && fixed_regs[ARG_POINTER_REGNUM])
3402 	bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
3403 
3404       /* Any constant, or pseudo with constant equivalences, may
3405 	 require reloading from memory using the pic register.  */
3406       if (picreg != INVALID_REGNUM
3407 	  && fixed_regs[picreg])
3408 	bitmap_set_bit (regular_block_artificial_uses, picreg);
3409     }
3410   /* The all-important stack pointer must always be live.  */
3411   bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
3412 
3413 #ifdef EH_USES
3414   /* EH_USES registers are used:
3415      1) at all insns that might throw (calls or with -fnon-call-exceptions
3416 	trapping insns)
3417      2) in all EH edges
3418      3) to support backtraces and/or debugging, anywhere between their
3419 	initialization and where they the saved registers are restored
3420 	from them, including the cases where we don't reach the epilogue
3421 	(noreturn call or infinite loop).  */
3422   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3423     if (EH_USES (i))
3424       bitmap_set_bit (regular_block_artificial_uses, i);
3425 #endif
3426 }
3427 
3428 
3429 /* Get the artificial use set for an eh block. */
3430 
3431 static void
df_get_eh_block_artificial_uses(bitmap eh_block_artificial_uses)3432 df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3433 {
3434   bitmap_clear (eh_block_artificial_uses);
3435 
3436   /* The following code (down through the arg_pointer setting APPEARS
3437      to be necessary because there is nothing that actually
3438      describes what the exception handling code may actually need
3439      to keep alive.  */
3440   if (reload_completed)
3441     {
3442       if (frame_pointer_needed)
3443 	{
3444 	  bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
3445 	  if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3446 	    bitmap_set_bit (eh_block_artificial_uses,
3447 			    HARD_FRAME_POINTER_REGNUM);
3448 	}
3449       if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3450 	  && fixed_regs[ARG_POINTER_REGNUM])
3451 	bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
3452     }
3453 }
3454 
3455 
3456 
3457 /*----------------------------------------------------------------------------
3458    Specialized hard register scanning functions.
3459 ----------------------------------------------------------------------------*/
3460 
3461 
3462 /* Mark a register in SET.  Hard registers in large modes get all
3463    of their component registers set as well.  */
3464 
3465 static void
df_mark_reg(rtx reg,void * vset)3466 df_mark_reg (rtx reg, void *vset)
3467 {
3468   bitmap_set_range ((bitmap) vset, REGNO (reg), REG_NREGS (reg));
3469 }
3470 
3471 
3472 /* Set the bit for regs that are considered being defined at the entry. */
3473 
3474 static void
df_get_entry_block_def_set(bitmap entry_block_defs)3475 df_get_entry_block_def_set (bitmap entry_block_defs)
3476 {
3477   rtx r;
3478   int i;
3479 
3480   bitmap_clear (entry_block_defs);
3481 
3482   /* For separate shrink-wrapping we use LIVE to analyze which basic blocks
3483      need a prologue for some component to be executed before that block,
3484      and we do not care about any other registers.  Hence, we do not want
3485      any register for any component defined in the entry block, and we can
3486      just leave all registers undefined.  */
3487   if (df_scan->local_flags & DF_SCAN_EMPTY_ENTRY_EXIT)
3488     return;
3489 
3490   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3491     {
3492       if (global_regs[i])
3493 	bitmap_set_bit (entry_block_defs, i);
3494       if (FUNCTION_ARG_REGNO_P (i))
3495 	bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3496     }
3497 
3498   /* The always important stack pointer.  */
3499   bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3500 
3501   /* Once the prologue has been generated, all of these registers
3502      should just show up in the first regular block.  */
3503   if (targetm.have_prologue () && epilogue_completed)
3504     {
3505       /* Defs for the callee saved registers are inserted so that the
3506 	 pushes have some defining location.  */
3507       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3508 	if (!crtl->abi->clobbers_full_reg_p (i)
3509 	    && !fixed_regs[i]
3510 	    && df_regs_ever_live_p (i))
3511 	  bitmap_set_bit (entry_block_defs, i);
3512     }
3513 
3514   r = targetm.calls.struct_value_rtx (current_function_decl, true);
3515   if (r && REG_P (r))
3516     bitmap_set_bit (entry_block_defs, REGNO (r));
3517 
3518   /* If the function has an incoming STATIC_CHAIN, it has to show up
3519      in the entry def set.  */
3520   r = rtx_for_static_chain (current_function_decl, true);
3521   if (r && REG_P (r))
3522     bitmap_set_bit (entry_block_defs, REGNO (r));
3523 
3524   if ((!reload_completed) || frame_pointer_needed)
3525     {
3526       /* Any reference to any pseudo before reload is a potential
3527 	 reference of the frame pointer.  */
3528       bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
3529 
3530       /* If they are different, also mark the hard frame pointer as live.  */
3531       if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
3532 	  && !LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3533 	bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
3534     }
3535 
3536   /* These registers are live everywhere.  */
3537   if (!reload_completed)
3538     {
3539       /* Pseudos with argument area equivalences may require
3540 	 reloading via the argument pointer.  */
3541       if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3542 	  && fixed_regs[ARG_POINTER_REGNUM])
3543 	bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
3544 
3545       /* Any constant, or pseudo with constant equivalences, may
3546 	 require reloading from memory using the pic register.  */
3547       unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3548       if (picreg != INVALID_REGNUM
3549 	  && fixed_regs[picreg])
3550 	bitmap_set_bit (entry_block_defs, picreg);
3551     }
3552 
3553 #ifdef INCOMING_RETURN_ADDR_RTX
3554   if (REG_P (INCOMING_RETURN_ADDR_RTX))
3555     bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3556 #endif
3557 
3558   targetm.extra_live_on_entry (entry_block_defs);
3559 }
3560 
3561 
3562 /* Return the (conservative) set of hard registers that are defined on
3563    entry to the function.
3564    It uses df->entry_block_defs to determine which register
3565    reference to include.  */
3566 
3567 static void
df_entry_block_defs_collect(class df_collection_rec * collection_rec,bitmap entry_block_defs)3568 df_entry_block_defs_collect (class df_collection_rec *collection_rec,
3569 			     bitmap entry_block_defs)
3570 {
3571   unsigned int i;
3572   bitmap_iterator bi;
3573 
3574   EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3575     {
3576       df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3577 		     ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_DEF, 0);
3578     }
3579 
3580   df_canonize_collection_rec (collection_rec);
3581 }
3582 
3583 
3584 /* Record the (conservative) set of hard registers that are defined on
3585    entry to the function.  */
3586 
3587 static void
df_record_entry_block_defs(bitmap entry_block_defs)3588 df_record_entry_block_defs (bitmap entry_block_defs)
3589 {
3590   class df_collection_rec collection_rec;
3591   df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3592 
3593   /* Process bb_refs chain */
3594   df_refs_add_to_chains (&collection_rec,
3595 			 BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK),
3596 			 NULL,
3597 			 copy_defs);
3598 }
3599 
3600 
3601 /* Update the defs in the entry block.  */
3602 
3603 void
df_update_entry_block_defs(void)3604 df_update_entry_block_defs (void)
3605 {
3606   bool changed = false;
3607 
3608   auto_bitmap refs (&df_bitmap_obstack);
3609   df_get_entry_block_def_set (refs);
3610   gcc_assert (df->entry_block_defs);
3611   if (!bitmap_equal_p (df->entry_block_defs, refs))
3612     {
3613       struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3614       df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3615       df_ref_chain_delete (bb_info->artificial_defs);
3616       bb_info->artificial_defs = NULL;
3617       changed = true;
3618     }
3619 
3620   if (changed)
3621     {
3622       df_record_entry_block_defs (refs);
3623       bitmap_copy (df->entry_block_defs, refs);
3624       df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK));
3625     }
3626 }
3627 
3628 
3629 /* Return true if REGNO is used by the epilogue.  */
3630 bool
df_epilogue_uses_p(unsigned int regno)3631 df_epilogue_uses_p (unsigned int regno)
3632 {
3633   return (EPILOGUE_USES (regno)
3634 	  || TEST_HARD_REG_BIT (crtl->must_be_zero_on_return, regno));
3635 }
3636 
3637 /* Set the bit for regs that are considered being used at the exit. */
3638 
3639 static void
df_get_exit_block_use_set(bitmap exit_block_uses)3640 df_get_exit_block_use_set (bitmap exit_block_uses)
3641 {
3642   unsigned int i;
3643   unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3644 
3645   bitmap_clear (exit_block_uses);
3646 
3647   /* For separate shrink-wrapping we use LIVE to analyze which basic blocks
3648      need an epilogue for some component to be executed after that block,
3649      and we do not care about any other registers.  Hence, we do not want
3650      any register for any component seen as used in the exit block, and we
3651      can just say no registers at all are used.  */
3652   if (df_scan->local_flags & DF_SCAN_EMPTY_ENTRY_EXIT)
3653     return;
3654 
3655   /* Stack pointer is always live at the exit.  */
3656   bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
3657 
3658   /* Mark the frame pointer if needed at the end of the function.
3659      If we end up eliminating it, it will be removed from the live
3660      list of each basic block by reload.  */
3661 
3662   if ((!reload_completed) || frame_pointer_needed)
3663     {
3664       bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
3665 
3666       /* If they are different, also mark the hard frame pointer as live.  */
3667       if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
3668 	  && !LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3669 	bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
3670     }
3671 
3672   /* Many architectures have a GP register even without flag_pic.
3673      Assume the pic register is not in use, or will be handled by
3674      other means, if it is not fixed.  */
3675   if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
3676       && picreg != INVALID_REGNUM
3677       && fixed_regs[picreg])
3678     bitmap_set_bit (exit_block_uses, picreg);
3679 
3680   /* Mark all global registers, and all registers used by the
3681      epilogue as being live at the end of the function since they
3682      may be referenced by our caller.  */
3683   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3684     if (global_regs[i] || df_epilogue_uses_p (i))
3685       bitmap_set_bit (exit_block_uses, i);
3686 
3687   if (targetm.have_epilogue () && epilogue_completed)
3688     {
3689       /* Mark all call-saved registers that we actually used.  */
3690       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3691 	if (df_regs_ever_live_p (i)
3692 	    && !LOCAL_REGNO (i)
3693 	    && !crtl->abi->clobbers_full_reg_p (i))
3694 	  bitmap_set_bit (exit_block_uses, i);
3695     }
3696 
3697   /* Mark the registers that will contain data for the handler.  */
3698   if (reload_completed && crtl->calls_eh_return)
3699     for (i = 0; ; ++i)
3700       {
3701 	unsigned regno = EH_RETURN_DATA_REGNO (i);
3702 	if (regno == INVALID_REGNUM)
3703 	  break;
3704 	bitmap_set_bit (exit_block_uses, regno);
3705       }
3706 
3707 #ifdef EH_RETURN_STACKADJ_RTX
3708   if ((!targetm.have_epilogue () || ! epilogue_completed)
3709       && crtl->calls_eh_return)
3710     {
3711       rtx tmp = EH_RETURN_STACKADJ_RTX;
3712       if (tmp && REG_P (tmp))
3713 	df_mark_reg (tmp, exit_block_uses);
3714     }
3715 #endif
3716 
3717   if ((!targetm.have_epilogue () || ! epilogue_completed)
3718       && crtl->calls_eh_return)
3719     {
3720       rtx tmp = EH_RETURN_HANDLER_RTX;
3721       if (tmp && REG_P (tmp))
3722 	df_mark_reg (tmp, exit_block_uses);
3723     }
3724 
3725   /* Mark function return value.  */
3726   diddle_return_value (df_mark_reg, (void*) exit_block_uses);
3727 }
3728 
3729 
3730 /* Return the refs of hard registers that are used in the exit block.
3731    It uses df->exit_block_uses to determine register to include.  */
3732 
3733 static void
df_exit_block_uses_collect(class df_collection_rec * collection_rec,bitmap exit_block_uses)3734 df_exit_block_uses_collect (class df_collection_rec *collection_rec, bitmap exit_block_uses)
3735 {
3736   unsigned int i;
3737   bitmap_iterator bi;
3738 
3739   EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
3740     df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
3741 		   EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_USE, 0);
3742 
3743   /* It is deliberate that this is not put in the exit block uses but
3744      I do not know why.  */
3745   if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3746       && reload_completed
3747       && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
3748       && bb_has_eh_pred (EXIT_BLOCK_PTR_FOR_FN (cfun))
3749       && fixed_regs[ARG_POINTER_REGNUM])
3750     df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
3751 		   EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_USE, 0);
3752 
3753   df_canonize_collection_rec (collection_rec);
3754 }
3755 
3756 
3757 /* Record the set of hard registers that are used in the exit block.
3758    It uses df->exit_block_uses to determine which bit to include.  */
3759 
3760 static void
df_record_exit_block_uses(bitmap exit_block_uses)3761 df_record_exit_block_uses (bitmap exit_block_uses)
3762 {
3763   class df_collection_rec collection_rec;
3764   df_exit_block_uses_collect (&collection_rec, exit_block_uses);
3765 
3766   /* Process bb_refs chain */
3767   df_refs_add_to_chains (&collection_rec,
3768 			 BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK),
3769 			 NULL,
3770 			 copy_uses);
3771 }
3772 
3773 
3774 /* Update the uses in the exit block.  */
3775 
3776 void
df_update_exit_block_uses(void)3777 df_update_exit_block_uses (void)
3778 {
3779   bool changed = false;
3780 
3781   auto_bitmap refs (&df_bitmap_obstack);
3782   df_get_exit_block_use_set (refs);
3783   gcc_assert (df->exit_block_uses);
3784   if (!bitmap_equal_p (df->exit_block_uses, refs))
3785     {
3786       struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
3787       df_ref_chain_delete_du_chain (bb_info->artificial_uses);
3788       df_ref_chain_delete (bb_info->artificial_uses);
3789       bb_info->artificial_uses = NULL;
3790       changed = true;
3791     }
3792 
3793   if (changed)
3794     {
3795       df_record_exit_block_uses (refs);
3796       bitmap_copy (df->exit_block_uses, refs);
3797       df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK));
3798     }
3799 }
3800 
3801 static bool initialized = false;
3802 
3803 
3804 /* Initialize some platform specific structures.  */
3805 
3806 void
df_hard_reg_init(void)3807 df_hard_reg_init (void)
3808 {
3809   int i;
3810   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
3811 
3812   if (initialized)
3813     return;
3814 
3815   /* Record which registers will be eliminated.  We use this in
3816      mark_used_regs.  */
3817   CLEAR_HARD_REG_SET (elim_reg_set);
3818 
3819   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
3820     SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
3821 
3822   initialized = true;
3823 }
3824 
3825 /* Recompute the parts of scanning that are based on regs_ever_live
3826    because something changed in that array.  */
3827 
3828 void
df_update_entry_exit_and_calls(void)3829 df_update_entry_exit_and_calls (void)
3830 {
3831   basic_block bb;
3832 
3833   df_update_entry_block_defs ();
3834   df_update_exit_block_uses ();
3835 
3836   /* The call insns need to be rescanned because there may be changes
3837      in the set of registers clobbered across the call.  */
3838   FOR_EACH_BB_FN (bb, cfun)
3839     {
3840       rtx_insn *insn;
3841       FOR_BB_INSNS (bb, insn)
3842 	{
3843 	  if (INSN_P (insn) && CALL_P (insn))
3844 	    df_insn_rescan (insn);
3845 	}
3846     }
3847 }
3848 
3849 
3850 /* Return true if hard REG is actually used in the some instruction.
3851    There are a fair number of conditions that affect the setting of
3852    this array.  See the comment in df.h for df->hard_regs_live_count
3853    for the conditions that this array is set. */
3854 
3855 bool
df_hard_reg_used_p(unsigned int reg)3856 df_hard_reg_used_p (unsigned int reg)
3857 {
3858   return df->hard_regs_live_count[reg] != 0;
3859 }
3860 
3861 
3862 /* A count of the number of times REG is actually used in the some
3863    instruction.  There are a fair number of conditions that affect the
3864    setting of this array.  See the comment in df.h for
3865    df->hard_regs_live_count for the conditions that this array is
3866    set. */
3867 
3868 
3869 unsigned int
df_hard_reg_used_count(unsigned int reg)3870 df_hard_reg_used_count (unsigned int reg)
3871 {
3872   return df->hard_regs_live_count[reg];
3873 }
3874 
3875 
3876 /* Get the value of regs_ever_live[REGNO].  */
3877 
3878 bool
df_regs_ever_live_p(unsigned int regno)3879 df_regs_ever_live_p (unsigned int regno)
3880 {
3881   return regs_ever_live[regno];
3882 }
3883 
3884 /* Set regs_ever_live[REGNO] to VALUE.  If this cause regs_ever_live
3885    to change, schedule that change for the next update.  */
3886 
3887 void
df_set_regs_ever_live(unsigned int regno,bool value)3888 df_set_regs_ever_live (unsigned int regno, bool value)
3889 {
3890   if (regs_ever_live[regno] == value)
3891     return;
3892 
3893   regs_ever_live[regno] = value;
3894   if (df)
3895     df->redo_entry_and_exit = true;
3896 }
3897 
3898 
3899 /* Compute "regs_ever_live" information from the underlying df
3900    information.  Set the vector to all false if RESET.  */
3901 
3902 void
df_compute_regs_ever_live(bool reset)3903 df_compute_regs_ever_live (bool reset)
3904 {
3905   unsigned int i;
3906   bool changed = df->redo_entry_and_exit;
3907 
3908   if (reset)
3909     memset (regs_ever_live, 0, sizeof (regs_ever_live));
3910 
3911   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3912     if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
3913       {
3914 	regs_ever_live[i] = true;
3915 	changed = true;
3916       }
3917   if (changed)
3918     df_update_entry_exit_and_calls ();
3919   df->redo_entry_and_exit = false;
3920 }
3921 
3922 
3923 /*----------------------------------------------------------------------------
3924   Dataflow ref information verification functions.
3925 
3926   df_reg_chain_mark (refs, regno, is_def, is_eq_use)
3927   df_reg_chain_verify_unmarked (refs)
3928   df_refs_verify (vec<stack, va_df_ref>, ref*, bool)
3929   df_mws_verify (mw*, mw*, bool)
3930   df_insn_refs_verify (collection_rec, bb, insn, bool)
3931   df_bb_refs_verify (bb, refs, bool)
3932   df_bb_verify (bb)
3933   df_exit_block_bitmap_verify (bool)
3934   df_entry_block_bitmap_verify (bool)
3935   df_scan_verify ()
3936 ----------------------------------------------------------------------------*/
3937 
3938 
3939 /* Mark all refs in the reg chain.  Verify that all of the registers
3940 are in the correct chain.  */
3941 
3942 static unsigned int
df_reg_chain_mark(df_ref refs,unsigned int regno,bool is_def,bool is_eq_use)3943 df_reg_chain_mark (df_ref refs, unsigned int regno,
3944 		   bool is_def, bool is_eq_use)
3945 {
3946   unsigned int count = 0;
3947   df_ref ref;
3948   for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
3949     {
3950       gcc_assert (!DF_REF_IS_REG_MARKED (ref));
3951 
3952       /* If there are no def-use or use-def chains, make sure that all
3953 	 of the chains are clear.  */
3954       if (!df_chain)
3955 	gcc_assert (!DF_REF_CHAIN (ref));
3956 
3957       /* Check to make sure the ref is in the correct chain.  */
3958       gcc_assert (DF_REF_REGNO (ref) == regno);
3959       if (is_def)
3960 	gcc_assert (DF_REF_REG_DEF_P (ref));
3961       else
3962 	gcc_assert (!DF_REF_REG_DEF_P (ref));
3963 
3964       if (is_eq_use)
3965 	gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
3966       else
3967 	gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
3968 
3969       if (DF_REF_NEXT_REG (ref))
3970 	gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
3971       count++;
3972       DF_REF_REG_MARK (ref);
3973     }
3974   return count;
3975 }
3976 
3977 
3978 /* Verify that all of the registers in the chain are unmarked.  */
3979 
3980 static void
df_reg_chain_verify_unmarked(df_ref refs)3981 df_reg_chain_verify_unmarked (df_ref refs)
3982 {
3983   df_ref ref;
3984   for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
3985     gcc_assert (!DF_REF_IS_REG_MARKED (ref));
3986 }
3987 
3988 
3989 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
3990 
3991 static bool
df_refs_verify(const vec<df_ref,va_heap> * new_rec,df_ref old_rec,bool abort_if_fail)3992 df_refs_verify (const vec<df_ref, va_heap> *new_rec, df_ref old_rec,
3993 		bool abort_if_fail)
3994 {
3995   unsigned int ix;
3996   df_ref new_ref;
3997 
3998   FOR_EACH_VEC_ELT (*new_rec, ix, new_ref)
3999     {
4000       if (old_rec == NULL || !df_ref_equal_p (new_ref, old_rec))
4001 	{
4002 	  if (abort_if_fail)
4003 	    gcc_assert (0);
4004 	  else
4005 	    return false;
4006 	}
4007 
4008       /* Abort if fail is called from the function level verifier.  If
4009 	 that is the context, mark this reg as being seem.  */
4010       if (abort_if_fail)
4011 	{
4012 	  gcc_assert (DF_REF_IS_REG_MARKED (old_rec));
4013 	  DF_REF_REG_UNMARK (old_rec);
4014 	}
4015 
4016       old_rec = DF_REF_NEXT_LOC (old_rec);
4017     }
4018 
4019   if (abort_if_fail)
4020     gcc_assert (old_rec == NULL);
4021   else
4022     return old_rec == NULL;
4023   return false;
4024 }
4025 
4026 
4027 /* Verify that NEW_REC and OLD_REC have exactly the same members. */
4028 
4029 static bool
df_mws_verify(const vec<df_mw_hardreg *,va_heap> * new_rec,struct df_mw_hardreg * old_rec,bool abort_if_fail)4030 df_mws_verify (const vec<df_mw_hardreg *, va_heap> *new_rec,
4031 	       struct df_mw_hardreg *old_rec,
4032 	       bool abort_if_fail)
4033 {
4034   unsigned int ix;
4035   struct df_mw_hardreg *new_reg;
4036 
4037   FOR_EACH_VEC_ELT (*new_rec, ix, new_reg)
4038     {
4039       if (old_rec == NULL || !df_mw_equal_p (new_reg, old_rec))
4040 	{
4041 	  if (abort_if_fail)
4042 	    gcc_assert (0);
4043 	  else
4044 	    return false;
4045 	}
4046       old_rec = DF_MWS_NEXT (old_rec);
4047     }
4048 
4049   if (abort_if_fail)
4050     gcc_assert (old_rec == NULL);
4051   else
4052     return old_rec == NULL;
4053   return false;
4054 }
4055 
4056 
4057 /* Return true if the existing insn refs information is complete and
4058    correct. Otherwise (i.e. if there's any missing or extra refs),
4059    return the correct df_ref chain in REFS_RETURN.
4060 
4061    If ABORT_IF_FAIL, leave the refs that are verified (already in the
4062    ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4063    verification mode instead of the whole function, so unmark
4064    everything.
4065 
4066    If ABORT_IF_FAIL is set, this function never returns false.  */
4067 
4068 static bool
df_insn_refs_verify(class df_collection_rec * collection_rec,basic_block bb,rtx_insn * insn,bool abort_if_fail)4069 df_insn_refs_verify (class df_collection_rec *collection_rec,
4070 		     basic_block bb,
4071                      rtx_insn *insn,
4072 		     bool abort_if_fail)
4073 {
4074   bool ret1, ret2, ret3;
4075   unsigned int uid = INSN_UID (insn);
4076   struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4077 
4078   df_insn_refs_collect (collection_rec, bb, insn_info);
4079 
4080   /* Unfortunately we cannot opt out early if one of these is not
4081      right and abort_if_fail is set because the marks will not get cleared.  */
4082   ret1 = df_refs_verify (&collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
4083 			 abort_if_fail);
4084   if (!ret1 && !abort_if_fail)
4085     return false;
4086   ret2 = df_refs_verify (&collection_rec->use_vec, DF_INSN_UID_USES (uid),
4087 			 abort_if_fail);
4088   if (!ret2 && !abort_if_fail)
4089     return false;
4090   ret3 = df_refs_verify (&collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
4091 			 abort_if_fail);
4092   if (!ret3 && !abort_if_fail)
4093     return false;
4094   if (! df_mws_verify (&collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
4095 		       abort_if_fail))
4096     return false;
4097   return (ret1 && ret2 && ret3);
4098 }
4099 
4100 
4101 /* Return true if all refs in the basic block are correct and complete.
4102    Due to df_ref_chain_verify, it will cause all refs
4103    that are verified to have DF_REF_MARK bit set.  */
4104 
4105 static bool
df_bb_verify(basic_block bb)4106 df_bb_verify (basic_block bb)
4107 {
4108   rtx_insn *insn;
4109   struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4110   class df_collection_rec collection_rec;
4111 
4112   gcc_assert (bb_info);
4113 
4114   /* Scan the block, one insn at a time, from beginning to end.  */
4115   FOR_BB_INSNS_REVERSE (bb, insn)
4116     {
4117       if (!INSN_P (insn))
4118         continue;
4119       df_insn_refs_verify (&collection_rec, bb, insn, true);
4120       df_free_collection_rec (&collection_rec);
4121     }
4122 
4123   /* Do the artificial defs and uses.  */
4124   df_bb_refs_collect (&collection_rec, bb);
4125   df_refs_verify (&collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4126   df_refs_verify (&collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
4127   df_free_collection_rec (&collection_rec);
4128 
4129   return true;
4130 }
4131 
4132 
4133 /* Returns true if the entry block has correct and complete df_ref set.
4134    If not it either aborts if ABORT_IF_FAIL is true or returns false.  */
4135 
4136 static bool
df_entry_block_bitmap_verify(bool abort_if_fail)4137 df_entry_block_bitmap_verify (bool abort_if_fail)
4138 {
4139   bool is_eq;
4140 
4141   auto_bitmap entry_block_defs (&df_bitmap_obstack);
4142   df_get_entry_block_def_set (entry_block_defs);
4143 
4144   is_eq = bitmap_equal_p (entry_block_defs, df->entry_block_defs);
4145 
4146   if (!is_eq && abort_if_fail)
4147     {
4148       fprintf (stderr, "entry_block_defs = ");
4149       df_print_regset (stderr, entry_block_defs);
4150       fprintf (stderr, "df->entry_block_defs = ");
4151       df_print_regset (stderr, df->entry_block_defs);
4152       gcc_assert (0);
4153     }
4154 
4155   return is_eq;
4156 }
4157 
4158 
4159 /* Returns true if the exit block has correct and complete df_ref set.
4160    If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4161 
4162 static bool
df_exit_block_bitmap_verify(bool abort_if_fail)4163 df_exit_block_bitmap_verify (bool abort_if_fail)
4164 {
4165   bool is_eq;
4166 
4167   auto_bitmap exit_block_uses (&df_bitmap_obstack);
4168   df_get_exit_block_use_set (exit_block_uses);
4169 
4170   is_eq = bitmap_equal_p (exit_block_uses, df->exit_block_uses);
4171 
4172   if (!is_eq && abort_if_fail)
4173     {
4174       fprintf (stderr, "exit_block_uses = ");
4175       df_print_regset (stderr, exit_block_uses);
4176       fprintf (stderr, "df->exit_block_uses = ");
4177       df_print_regset (stderr, df->exit_block_uses);
4178       gcc_assert (0);
4179     }
4180 
4181   return is_eq;
4182 }
4183 
4184 
4185 /* Return true if df_ref information for all insns in all blocks are
4186    correct and complete.  */
4187 
4188 void
df_scan_verify(void)4189 df_scan_verify (void)
4190 {
4191   unsigned int i;
4192   basic_block bb;
4193 
4194   if (!df)
4195     return;
4196 
4197   /* Verification is a 4 step process. */
4198 
4199   /* (1) All of the refs are marked by going through the reg chains.  */
4200   for (i = 0; i < DF_REG_SIZE (df); i++)
4201     {
4202       gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
4203 		  == DF_REG_DEF_COUNT (i));
4204       gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
4205 		  == DF_REG_USE_COUNT (i));
4206       gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
4207 		  == DF_REG_EQ_USE_COUNT (i));
4208     }
4209 
4210   /* (2) There are various bitmaps whose value may change over the
4211      course of the compilation.  This step recomputes them to make
4212      sure that they have not slipped out of date.  */
4213   auto_bitmap regular_block_artificial_uses (&df_bitmap_obstack);
4214   auto_bitmap eh_block_artificial_uses (&df_bitmap_obstack);
4215 
4216   df_get_regular_block_artificial_uses (regular_block_artificial_uses);
4217   df_get_eh_block_artificial_uses (eh_block_artificial_uses);
4218 
4219   bitmap_ior_into (eh_block_artificial_uses,
4220 		   regular_block_artificial_uses);
4221 
4222   /* Check artificial_uses bitmaps didn't change. */
4223   gcc_assert (bitmap_equal_p (regular_block_artificial_uses,
4224 			      &df->regular_block_artificial_uses));
4225   gcc_assert (bitmap_equal_p (eh_block_artificial_uses,
4226 			      &df->eh_block_artificial_uses));
4227 
4228   /* Verify entry block and exit block. These only verify the bitmaps,
4229      the refs are verified in df_bb_verify.  */
4230   df_entry_block_bitmap_verify (true);
4231   df_exit_block_bitmap_verify (true);
4232 
4233   /* (3) All of the insns in all of the blocks are traversed and the
4234      marks are cleared both in the artificial refs attached to the
4235      blocks and the real refs inside the insns.  It is a failure to
4236      clear a mark that has not been set as this means that the ref in
4237      the block or insn was not in the reg chain.  */
4238 
4239   FOR_ALL_BB_FN (bb, cfun)
4240     df_bb_verify (bb);
4241 
4242   /* (4) See if all reg chains are traversed a second time.  This time
4243      a check is made that the marks are clear. A set mark would be a
4244      from a reg that is not in any insn or basic block.  */
4245 
4246   for (i = 0; i < DF_REG_SIZE (df); i++)
4247     {
4248       df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4249       df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4250       df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));
4251     }
4252 }
4253