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