Fix PR 93568 (thinko)
[official-gcc.git] / gcc / df-problems.c
blobc02533660dea738f0dcd3720ef8667674900ed2b
1 /* Standard problems for dataflow support routines.
2 Copyright (C) 1999-2020 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).
8 This file is part of GCC.
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.
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.
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/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "df.h"
31 #include "memmodel.h"
32 #include "tm_p.h"
33 #include "insn-config.h"
34 #include "cfganal.h"
35 #include "dce.h"
36 #include "valtrack.h"
37 #include "dumpfile.h"
38 #include "rtl-iter.h"
39 #include "regs.h"
40 #include "function-abi.h"
42 /* Note that turning REG_DEAD_DEBUGGING on will cause
43 gcc.c-torture/unsorted/dump-noaddr.c to fail because it prints
44 addresses in the dumps. */
45 #define REG_DEAD_DEBUGGING 0
47 #define DF_SPARSE_THRESHOLD 32
49 static bitmap_head seen_in_block;
50 static bitmap_head seen_in_insn;
52 /*----------------------------------------------------------------------------
53 Utility functions.
54 ----------------------------------------------------------------------------*/
56 /* Generic versions to get the void* version of the block info. Only
57 used inside the problem instance vectors. */
59 /* Dump a def-use or use-def chain for REF to FILE. */
61 void
62 df_chain_dump (struct df_link *link, FILE *file)
64 fprintf (file, "{ ");
65 for (; link; link = link->next)
67 fprintf (file, "%c%d(bb %d insn %d) ",
68 DF_REF_REG_DEF_P (link->ref)
69 ? 'd'
70 : (DF_REF_FLAGS (link->ref) & DF_REF_IN_NOTE) ? 'e' : 'u',
71 DF_REF_ID (link->ref),
72 DF_REF_BBNO (link->ref),
73 DF_REF_IS_ARTIFICIAL (link->ref)
74 ? -1 : DF_REF_INSN_UID (link->ref));
76 fprintf (file, "}");
80 /* Print some basic block info as part of df_dump. */
82 void
83 df_print_bb_index (basic_block bb, FILE *file)
85 edge e;
86 edge_iterator ei;
88 fprintf (file, "\n( ");
89 FOR_EACH_EDGE (e, ei, bb->preds)
91 basic_block pred = e->src;
92 fprintf (file, "%d%s ", pred->index, e->flags & EDGE_EH ? "(EH)" : "");
94 fprintf (file, ")->[%d]->( ", bb->index);
95 FOR_EACH_EDGE (e, ei, bb->succs)
97 basic_block succ = e->dest;
98 fprintf (file, "%d%s ", succ->index, e->flags & EDGE_EH ? "(EH)" : "");
100 fprintf (file, ")\n");
104 /*----------------------------------------------------------------------------
105 REACHING DEFINITIONS
107 Find the locations in the function where each definition site for a
108 pseudo reaches. In and out bitvectors are built for each basic
109 block. The id field in the ref is used to index into these sets.
110 See df.h for details.
112 If the DF_RD_PRUNE_DEAD_DEFS changeable flag is set, only DEFs reaching
113 existing uses are included in the global reaching DEFs set, or in other
114 words only DEFs that are still live. This is a kind of pruned version
115 of the traditional reaching definitions problem that is much less
116 complex to compute and produces enough information to compute UD-chains.
117 In this context, live must be interpreted in the DF_LR sense: Uses that
118 are upward exposed but maybe not initialized on all paths through the
119 CFG. For a USE that is not reached by a DEF on all paths, we still want
120 to make those DEFs that do reach the USE visible, and pruning based on
121 DF_LIVE would make that impossible.
122 ----------------------------------------------------------------------------*/
124 /* This problem plays a large number of games for the sake of
125 efficiency.
127 1) The order of the bits in the bitvectors. After the scanning
128 phase, all of the defs are sorted. All of the defs for the reg 0
129 are first, followed by all defs for reg 1 and so on.
131 2) There are two kill sets, one if the number of defs is less or
132 equal to DF_SPARSE_THRESHOLD and another if the number of defs is
133 greater.
135 <= : Data is built directly in the kill set.
137 > : One level of indirection is used to keep from generating long
138 strings of 1 bits in the kill sets. Bitvectors that are indexed
139 by the regnum are used to represent that there is a killing def
140 for the register. The confluence and transfer functions use
141 these along with the bitmap_clear_range call to remove ranges of
142 bits without actually generating a knockout vector.
144 The kill and sparse_kill and the dense_invalidated_by_eh and
145 sparse_invalidated_by_eh both play this game. */
147 /* Private data used to compute the solution for this problem. These
148 data structures are not accessible outside of this module. */
149 class df_rd_problem_data
151 public:
152 /* The set of defs to regs invalidated by EH edges. */
153 bitmap_head sparse_invalidated_by_eh;
154 bitmap_head dense_invalidated_by_eh;
155 /* An obstack for the bitmaps we need for this problem. */
156 bitmap_obstack rd_bitmaps;
160 /* Free basic block info. */
162 static void
163 df_rd_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
164 void *vbb_info)
166 class df_rd_bb_info *bb_info = (class df_rd_bb_info *) vbb_info;
167 if (bb_info)
169 bitmap_clear (&bb_info->kill);
170 bitmap_clear (&bb_info->sparse_kill);
171 bitmap_clear (&bb_info->gen);
172 bitmap_clear (&bb_info->in);
173 bitmap_clear (&bb_info->out);
178 /* Allocate or reset bitmaps for DF_RD blocks. The solution bits are
179 not touched unless the block is new. */
181 static void
182 df_rd_alloc (bitmap all_blocks)
184 unsigned int bb_index;
185 bitmap_iterator bi;
186 class df_rd_problem_data *problem_data;
188 if (df_rd->problem_data)
190 problem_data = (class df_rd_problem_data *) df_rd->problem_data;
191 bitmap_clear (&problem_data->sparse_invalidated_by_eh);
192 bitmap_clear (&problem_data->dense_invalidated_by_eh);
194 else
196 problem_data = XNEW (class df_rd_problem_data);
197 df_rd->problem_data = problem_data;
199 bitmap_obstack_initialize (&problem_data->rd_bitmaps);
200 bitmap_initialize (&problem_data->sparse_invalidated_by_eh,
201 &problem_data->rd_bitmaps);
202 bitmap_initialize (&problem_data->dense_invalidated_by_eh,
203 &problem_data->rd_bitmaps);
206 df_grow_bb_info (df_rd);
208 /* Because of the clustering of all use sites for the same pseudo,
209 we have to process all of the blocks before doing the analysis. */
211 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
213 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
215 /* When bitmaps are already initialized, just clear them. */
216 if (bb_info->kill.obstack)
218 bitmap_clear (&bb_info->kill);
219 bitmap_clear (&bb_info->sparse_kill);
220 bitmap_clear (&bb_info->gen);
222 else
224 bitmap_initialize (&bb_info->kill, &problem_data->rd_bitmaps);
225 bitmap_initialize (&bb_info->sparse_kill, &problem_data->rd_bitmaps);
226 bitmap_initialize (&bb_info->gen, &problem_data->rd_bitmaps);
227 bitmap_initialize (&bb_info->in, &problem_data->rd_bitmaps);
228 bitmap_initialize (&bb_info->out, &problem_data->rd_bitmaps);
231 df_rd->optional_p = true;
235 /* Add the effect of the top artificial defs of BB to the reaching definitions
236 bitmap LOCAL_RD. */
238 void
239 df_rd_simulate_artificial_defs_at_top (basic_block bb, bitmap local_rd)
241 int bb_index = bb->index;
242 df_ref def;
243 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
244 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
246 unsigned int dregno = DF_REF_REGNO (def);
247 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
248 bitmap_clear_range (local_rd,
249 DF_DEFS_BEGIN (dregno),
250 DF_DEFS_COUNT (dregno));
251 bitmap_set_bit (local_rd, DF_REF_ID (def));
255 /* Add the effect of the defs of INSN to the reaching definitions bitmap
256 LOCAL_RD. */
258 void
259 df_rd_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
260 bitmap local_rd)
262 df_ref def;
264 FOR_EACH_INSN_DEF (def, insn)
266 unsigned int dregno = DF_REF_REGNO (def);
267 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
268 || (dregno >= FIRST_PSEUDO_REGISTER))
270 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
271 bitmap_clear_range (local_rd,
272 DF_DEFS_BEGIN (dregno),
273 DF_DEFS_COUNT (dregno));
274 if (!(DF_REF_FLAGS (def)
275 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
276 bitmap_set_bit (local_rd, DF_REF_ID (def));
281 /* Process a list of DEFs for df_rd_bb_local_compute. This is a bit
282 more complicated than just simulating, because we must produce the
283 gen and kill sets and hence deal with the two possible representations
284 of kill sets. */
286 static void
287 df_rd_bb_local_compute_process_def (class df_rd_bb_info *bb_info,
288 df_ref def,
289 int top_flag)
291 for (; def; def = DF_REF_NEXT_LOC (def))
293 if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
295 unsigned int regno = DF_REF_REGNO (def);
296 unsigned int begin = DF_DEFS_BEGIN (regno);
297 unsigned int n_defs = DF_DEFS_COUNT (regno);
299 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
300 || (regno >= FIRST_PSEUDO_REGISTER))
302 /* Only the last def(s) for a regno in the block has any
303 effect. */
304 if (!bitmap_bit_p (&seen_in_block, regno))
306 /* The first def for regno in insn gets to knock out the
307 defs from other instructions. */
308 if ((!bitmap_bit_p (&seen_in_insn, regno))
309 /* If the def is to only part of the reg, it does
310 not kill the other defs that reach here. */
311 && (!(DF_REF_FLAGS (def) &
312 (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))))
314 if (n_defs > DF_SPARSE_THRESHOLD)
316 bitmap_set_bit (&bb_info->sparse_kill, regno);
317 bitmap_clear_range (&bb_info->gen, begin, n_defs);
319 else
321 bitmap_set_range (&bb_info->kill, begin, n_defs);
322 bitmap_clear_range (&bb_info->gen, begin, n_defs);
326 bitmap_set_bit (&seen_in_insn, regno);
327 /* All defs for regno in the instruction may be put into
328 the gen set. */
329 if (!(DF_REF_FLAGS (def)
330 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
331 bitmap_set_bit (&bb_info->gen, DF_REF_ID (def));
338 /* Compute local reaching def info for basic block BB. */
340 static void
341 df_rd_bb_local_compute (unsigned int bb_index)
343 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
344 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
345 rtx_insn *insn;
347 bitmap_clear (&seen_in_block);
348 bitmap_clear (&seen_in_insn);
350 /* Artificials are only hard regs. */
351 if (!(df->changeable_flags & DF_NO_HARD_REGS))
352 df_rd_bb_local_compute_process_def (bb_info,
353 df_get_artificial_defs (bb_index),
356 FOR_BB_INSNS_REVERSE (bb, insn)
358 unsigned int uid = INSN_UID (insn);
360 if (!INSN_P (insn))
361 continue;
363 df_rd_bb_local_compute_process_def (bb_info,
364 DF_INSN_UID_DEFS (uid), 0);
366 /* This complex dance with the two bitmaps is required because
367 instructions can assign twice to the same pseudo. This
368 generally happens with calls that will have one def for the
369 result and another def for the clobber. If only one vector
370 is used and the clobber goes first, the result will be
371 lost. */
372 bitmap_ior_into (&seen_in_block, &seen_in_insn);
373 bitmap_clear (&seen_in_insn);
376 /* Process the artificial defs at the top of the block last since we
377 are going backwards through the block and these are logically at
378 the start. */
379 if (!(df->changeable_flags & DF_NO_HARD_REGS))
380 df_rd_bb_local_compute_process_def (bb_info,
381 df_get_artificial_defs (bb_index),
382 DF_REF_AT_TOP);
386 /* Compute local reaching def info for each basic block within BLOCKS. */
388 static void
389 df_rd_local_compute (bitmap all_blocks)
391 unsigned int bb_index;
392 bitmap_iterator bi;
393 class df_rd_problem_data *problem_data
394 = (class df_rd_problem_data *) df_rd->problem_data;
395 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_eh;
396 bitmap dense_invalidated = &problem_data->dense_invalidated_by_eh;
398 bitmap_initialize (&seen_in_block, &df_bitmap_obstack);
399 bitmap_initialize (&seen_in_insn, &df_bitmap_obstack);
401 df_maybe_reorganize_def_refs (DF_REF_ORDER_BY_REG);
403 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
405 df_rd_bb_local_compute (bb_index);
408 /* Set up the knockout bit vectors to be applied across EH_EDGES.
409 Conservatively treat partially-clobbered registers as surviving
410 across the EH edge, i.e. assume that definitions before the edge
411 is taken *might* reach uses after it has been taken. */
412 if (!(df->changeable_flags & DF_NO_HARD_REGS))
413 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
414 if (eh_edge_abi.clobbers_full_reg_p (regno))
416 if (DF_DEFS_COUNT (regno) > DF_SPARSE_THRESHOLD)
417 bitmap_set_bit (sparse_invalidated, regno);
418 else
419 bitmap_set_range (dense_invalidated,
420 DF_DEFS_BEGIN (regno),
421 DF_DEFS_COUNT (regno));
424 bitmap_release (&seen_in_block);
425 bitmap_release (&seen_in_insn);
429 /* Initialize the solution bit vectors for problem. */
431 static void
432 df_rd_init_solution (bitmap all_blocks)
434 unsigned int bb_index;
435 bitmap_iterator bi;
437 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
439 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
441 bitmap_copy (&bb_info->out, &bb_info->gen);
442 bitmap_clear (&bb_info->in);
446 /* In of target gets or of out of source. */
448 static bool
449 df_rd_confluence_n (edge e)
451 bitmap op1 = &df_rd_get_bb_info (e->dest->index)->in;
452 bitmap op2 = &df_rd_get_bb_info (e->src->index)->out;
453 bool changed = false;
455 if (e->flags & EDGE_FAKE)
456 return false;
458 if (e->flags & EDGE_EH)
460 class df_rd_problem_data *problem_data
461 = (class df_rd_problem_data *) df_rd->problem_data;
462 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_eh;
463 bitmap dense_invalidated = &problem_data->dense_invalidated_by_eh;
464 bitmap_iterator bi;
465 unsigned int regno;
467 auto_bitmap tmp (&df_bitmap_obstack);
468 bitmap_and_compl (tmp, op2, dense_invalidated);
470 EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
472 bitmap_clear_range (tmp,
473 DF_DEFS_BEGIN (regno),
474 DF_DEFS_COUNT (regno));
476 changed |= bitmap_ior_into (op1, tmp);
477 return changed;
479 else
480 return bitmap_ior_into (op1, op2);
484 /* Transfer function. */
486 static bool
487 df_rd_transfer_function (int bb_index)
489 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
490 unsigned int regno;
491 bitmap_iterator bi;
492 bitmap in = &bb_info->in;
493 bitmap out = &bb_info->out;
494 bitmap gen = &bb_info->gen;
495 bitmap kill = &bb_info->kill;
496 bitmap sparse_kill = &bb_info->sparse_kill;
497 bool changed = false;
499 if (bitmap_empty_p (sparse_kill))
500 changed = bitmap_ior_and_compl (out, gen, in, kill);
501 else
503 class df_rd_problem_data *problem_data;
504 bitmap_head tmp;
506 /* Note that TMP is _not_ a temporary bitmap if we end up replacing
507 OUT with TMP. Therefore, allocate TMP in the RD bitmaps obstack. */
508 problem_data = (class df_rd_problem_data *) df_rd->problem_data;
509 bitmap_initialize (&tmp, &problem_data->rd_bitmaps);
511 bitmap_and_compl (&tmp, in, kill);
512 EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
514 bitmap_clear_range (&tmp,
515 DF_DEFS_BEGIN (regno),
516 DF_DEFS_COUNT (regno));
518 bitmap_ior_into (&tmp, gen);
519 changed = !bitmap_equal_p (&tmp, out);
520 if (changed)
521 bitmap_move (out, &tmp);
522 else
523 bitmap_clear (&tmp);
526 if (df->changeable_flags & DF_RD_PRUNE_DEAD_DEFS)
528 /* Create a mask of DEFs for all registers live at the end of this
529 basic block, and mask out DEFs of registers that are not live.
530 Computing the mask looks costly, but the benefit of the pruning
531 outweighs the cost. */
532 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
533 bitmap regs_live_out = &df_lr_get_bb_info (bb_index)->out;
534 bitmap live_defs = BITMAP_ALLOC (&df_bitmap_obstack);
535 unsigned int regno;
536 bitmap_iterator bi;
538 EXECUTE_IF_SET_IN_BITMAP (regs_live_out, 0, regno, bi)
539 bitmap_set_range (live_defs,
540 DF_DEFS_BEGIN (regno),
541 DF_DEFS_COUNT (regno));
542 changed |= bitmap_and_into (&bb_info->out, live_defs);
543 BITMAP_FREE (live_defs);
546 return changed;
549 /* Free all storage associated with the problem. */
551 static void
552 df_rd_free (void)
554 class df_rd_problem_data *problem_data
555 = (class df_rd_problem_data *) df_rd->problem_data;
557 if (problem_data)
559 bitmap_obstack_release (&problem_data->rd_bitmaps);
561 df_rd->block_info_size = 0;
562 free (df_rd->block_info);
563 df_rd->block_info = NULL;
564 free (df_rd->problem_data);
566 free (df_rd);
570 /* Debugging info. */
572 static void
573 df_rd_start_dump (FILE *file)
575 class df_rd_problem_data *problem_data
576 = (class df_rd_problem_data *) df_rd->problem_data;
577 unsigned int m = DF_REG_SIZE (df);
578 unsigned int regno;
580 if (!df_rd->block_info)
581 return;
583 fprintf (file, ";; Reaching defs:\n");
585 fprintf (file, ";; sparse invalidated \t");
586 dump_bitmap (file, &problem_data->sparse_invalidated_by_eh);
587 fprintf (file, ";; dense invalidated \t");
588 dump_bitmap (file, &problem_data->dense_invalidated_by_eh);
590 fprintf (file, ";; reg->defs[] map:\t");
591 for (regno = 0; regno < m; regno++)
592 if (DF_DEFS_COUNT (regno))
593 fprintf (file, "%d[%d,%d] ", regno,
594 DF_DEFS_BEGIN (regno),
595 DF_DEFS_BEGIN (regno) + DF_DEFS_COUNT (regno) - 1);
596 fprintf (file, "\n");
600 static void
601 df_rd_dump_defs_set (bitmap defs_set, const char *prefix, FILE *file)
603 bitmap_head tmp;
604 unsigned int regno;
605 unsigned int m = DF_REG_SIZE (df);
606 bool first_reg = true;
608 fprintf (file, "%s\t(%d) ", prefix, (int) bitmap_count_bits (defs_set));
610 bitmap_initialize (&tmp, &df_bitmap_obstack);
611 for (regno = 0; regno < m; regno++)
613 if (HARD_REGISTER_NUM_P (regno)
614 && (df->changeable_flags & DF_NO_HARD_REGS))
615 continue;
616 bitmap_set_range (&tmp, DF_DEFS_BEGIN (regno), DF_DEFS_COUNT (regno));
617 bitmap_and_into (&tmp, defs_set);
618 if (! bitmap_empty_p (&tmp))
620 bitmap_iterator bi;
621 unsigned int ix;
622 bool first_def = true;
624 if (! first_reg)
625 fprintf (file, ",");
626 first_reg = false;
628 fprintf (file, "%u[", regno);
629 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, ix, bi)
631 fprintf (file, "%s%u", first_def ? "" : ",", ix);
632 first_def = false;
634 fprintf (file, "]");
636 bitmap_clear (&tmp);
639 fprintf (file, "\n");
640 bitmap_clear (&tmp);
643 /* Debugging info at top of bb. */
645 static void
646 df_rd_top_dump (basic_block bb, FILE *file)
648 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
649 if (!bb_info)
650 return;
652 df_rd_dump_defs_set (&bb_info->in, ";; rd in ", file);
653 df_rd_dump_defs_set (&bb_info->gen, ";; rd gen ", file);
654 df_rd_dump_defs_set (&bb_info->kill, ";; rd kill", file);
658 /* Debugging info at bottom of bb. */
660 static void
661 df_rd_bottom_dump (basic_block bb, FILE *file)
663 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
664 if (!bb_info)
665 return;
667 df_rd_dump_defs_set (&bb_info->out, ";; rd out ", file);
670 /* All of the information associated with every instance of the problem. */
672 static const struct df_problem problem_RD =
674 DF_RD, /* Problem id. */
675 DF_FORWARD, /* Direction. */
676 df_rd_alloc, /* Allocate the problem specific data. */
677 NULL, /* Reset global information. */
678 df_rd_free_bb_info, /* Free basic block info. */
679 df_rd_local_compute, /* Local compute function. */
680 df_rd_init_solution, /* Init the solution specific data. */
681 df_worklist_dataflow, /* Worklist solver. */
682 NULL, /* Confluence operator 0. */
683 df_rd_confluence_n, /* Confluence operator n. */
684 df_rd_transfer_function, /* Transfer function. */
685 NULL, /* Finalize function. */
686 df_rd_free, /* Free all of the problem information. */
687 df_rd_free, /* Remove this problem from the stack of dataflow problems. */
688 df_rd_start_dump, /* Debugging. */
689 df_rd_top_dump, /* Debugging start block. */
690 df_rd_bottom_dump, /* Debugging end block. */
691 NULL, /* Debugging start insn. */
692 NULL, /* Debugging end insn. */
693 NULL, /* Incremental solution verify start. */
694 NULL, /* Incremental solution verify end. */
695 NULL, /* Dependent problem. */
696 sizeof (class df_rd_bb_info),/* Size of entry of block_info array. */
697 TV_DF_RD, /* Timing variable. */
698 true /* Reset blocks on dropping out of blocks_to_analyze. */
703 /* Create a new RD instance and add it to the existing instance
704 of DF. */
706 void
707 df_rd_add_problem (void)
709 df_add_problem (&problem_RD);
714 /*----------------------------------------------------------------------------
715 LIVE REGISTERS
717 Find the locations in the function where any use of a pseudo can
718 reach in the backwards direction. In and out bitvectors are built
719 for each basic block. The regno is used to index into these sets.
720 See df.h for details.
721 ----------------------------------------------------------------------------*/
723 /* Private data used to verify the solution for this problem. */
724 struct df_lr_problem_data
726 bitmap_head *in;
727 bitmap_head *out;
728 /* An obstack for the bitmaps we need for this problem. */
729 bitmap_obstack lr_bitmaps;
732 /* Free basic block info. */
734 static void
735 df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
736 void *vbb_info)
738 class df_lr_bb_info *bb_info = (class df_lr_bb_info *) vbb_info;
739 if (bb_info)
741 bitmap_clear (&bb_info->use);
742 bitmap_clear (&bb_info->def);
743 bitmap_clear (&bb_info->in);
744 bitmap_clear (&bb_info->out);
749 /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
750 not touched unless the block is new. */
752 static void
753 df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
755 unsigned int bb_index;
756 bitmap_iterator bi;
757 struct df_lr_problem_data *problem_data;
759 df_grow_bb_info (df_lr);
760 if (df_lr->problem_data)
761 problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
762 else
764 problem_data = XNEW (struct df_lr_problem_data);
765 df_lr->problem_data = problem_data;
767 problem_data->out = NULL;
768 problem_data->in = NULL;
769 bitmap_obstack_initialize (&problem_data->lr_bitmaps);
772 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
774 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
776 /* When bitmaps are already initialized, just clear them. */
777 if (bb_info->use.obstack)
779 bitmap_clear (&bb_info->def);
780 bitmap_clear (&bb_info->use);
782 else
784 bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
785 bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
786 bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
787 bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
791 df_lr->optional_p = false;
795 /* Reset the global solution for recalculation. */
797 static void
798 df_lr_reset (bitmap all_blocks)
800 unsigned int bb_index;
801 bitmap_iterator bi;
803 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
805 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
806 gcc_assert (bb_info);
807 bitmap_clear (&bb_info->in);
808 bitmap_clear (&bb_info->out);
813 /* Compute local live register info for basic block BB. */
815 static void
816 df_lr_bb_local_compute (unsigned int bb_index)
818 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
819 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
820 rtx_insn *insn;
821 df_ref def, use;
823 /* Process the registers set in an exception handler. */
824 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
825 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
827 unsigned int dregno = DF_REF_REGNO (def);
828 bitmap_set_bit (&bb_info->def, dregno);
829 bitmap_clear_bit (&bb_info->use, dregno);
832 /* Process the hardware registers that are always live. */
833 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
834 /* Add use to set of uses in this BB. */
835 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
836 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
838 FOR_BB_INSNS_REVERSE (bb, insn)
840 if (!NONDEBUG_INSN_P (insn))
841 continue;
843 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
844 FOR_EACH_INSN_INFO_DEF (def, insn_info)
845 /* If the def is to only part of the reg, it does
846 not kill the other defs that reach here. */
847 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
849 unsigned int dregno = DF_REF_REGNO (def);
850 bitmap_set_bit (&bb_info->def, dregno);
851 bitmap_clear_bit (&bb_info->use, dregno);
854 FOR_EACH_INSN_INFO_USE (use, insn_info)
855 /* Add use to set of uses in this BB. */
856 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
859 /* Process the registers set in an exception handler or the hard
860 frame pointer if this block is the target of a non local
861 goto. */
862 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
863 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
865 unsigned int dregno = DF_REF_REGNO (def);
866 bitmap_set_bit (&bb_info->def, dregno);
867 bitmap_clear_bit (&bb_info->use, dregno);
870 #ifdef EH_USES
871 /* Process the uses that are live into an exception handler. */
872 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
873 /* Add use to set of uses in this BB. */
874 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
875 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
876 #endif
878 /* If the df_live problem is not defined, such as at -O0 and -O1, we
879 still need to keep the luids up to date. This is normally done
880 in the df_live problem since this problem has a forwards
881 scan. */
882 if (!df_live)
883 df_recompute_luids (bb);
887 /* Compute local live register info for each basic block within BLOCKS. */
889 static void
890 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
892 unsigned int bb_index, i;
893 bitmap_iterator bi;
895 bitmap_clear (&df->hardware_regs_used);
897 /* The all-important stack pointer must always be live. */
898 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
900 /* Global regs are always live, too. */
901 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
902 if (global_regs[i])
903 bitmap_set_bit (&df->hardware_regs_used, i);
905 /* Before reload, there are a few registers that must be forced
906 live everywhere -- which might not already be the case for
907 blocks within infinite loops. */
908 if (!reload_completed)
910 unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
911 /* Any reference to any pseudo before reload is a potential
912 reference of the frame pointer. */
913 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
915 /* Pseudos with argument area equivalences may require
916 reloading via the argument pointer. */
917 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
918 && fixed_regs[ARG_POINTER_REGNUM])
919 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
921 /* Any constant, or pseudo with constant equivalences, may
922 require reloading from memory using the pic register. */
923 if (pic_offset_table_regnum != INVALID_REGNUM
924 && fixed_regs[pic_offset_table_regnum])
925 bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
928 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
930 if (bb_index == EXIT_BLOCK)
932 /* The exit block is special for this problem and its bits are
933 computed from thin air. */
934 class df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
935 bitmap_copy (&bb_info->use, df->exit_block_uses);
937 else
938 df_lr_bb_local_compute (bb_index);
941 bitmap_clear (df_lr->out_of_date_transfer_functions);
945 /* Initialize the solution vectors. */
947 static void
948 df_lr_init (bitmap all_blocks)
950 unsigned int bb_index;
951 bitmap_iterator bi;
953 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
955 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
956 bitmap_copy (&bb_info->in, &bb_info->use);
957 bitmap_clear (&bb_info->out);
962 /* Confluence function that processes infinite loops. This might be a
963 noreturn function that throws. And even if it isn't, getting the
964 unwind info right helps debugging. */
965 static void
966 df_lr_confluence_0 (basic_block bb)
968 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
969 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
970 bitmap_copy (op1, &df->hardware_regs_used);
974 /* Confluence function that ignores fake edges. */
976 static bool
977 df_lr_confluence_n (edge e)
979 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
980 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
981 bool changed = false;
983 /* Call-clobbered registers die across exception and call edges.
984 Conservatively treat partially-clobbered registers as surviving
985 across the edges; they might or might not, depending on what
986 mode they have. */
987 /* ??? Abnormal call edges ignored for the moment, as this gets
988 confused by sibling call edges, which crashes reg-stack. */
989 if (e->flags & EDGE_EH)
991 bitmap_view<HARD_REG_SET> eh_kills (eh_edge_abi.full_reg_clobbers ());
992 changed = bitmap_ior_and_compl_into (op1, op2, eh_kills);
994 else
995 changed = bitmap_ior_into (op1, op2);
997 changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
998 return changed;
1002 /* Transfer function. */
1004 static bool
1005 df_lr_transfer_function (int bb_index)
1007 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1008 bitmap in = &bb_info->in;
1009 bitmap out = &bb_info->out;
1010 bitmap use = &bb_info->use;
1011 bitmap def = &bb_info->def;
1013 return bitmap_ior_and_compl (in, use, out, def);
1017 /* Run the fast dce as a side effect of building LR. */
1019 static void
1020 df_lr_finalize (bitmap all_blocks)
1022 df_lr->solutions_dirty = false;
1023 if (df->changeable_flags & DF_LR_RUN_DCE)
1025 run_fast_df_dce ();
1027 /* If dce deletes some instructions, we need to recompute the lr
1028 solution before proceeding further. The problem is that fast
1029 dce is a pessimestic dataflow algorithm. In the case where
1030 it deletes a statement S inside of a loop, the uses inside of
1031 S may not be deleted from the dataflow solution because they
1032 were carried around the loop. While it is conservatively
1033 correct to leave these extra bits, the standards of df
1034 require that we maintain the best possible (least fixed
1035 point) solution. The only way to do that is to redo the
1036 iteration from the beginning. See PR35805 for an
1037 example. */
1038 if (df_lr->solutions_dirty)
1040 df_clear_flags (DF_LR_RUN_DCE);
1041 df_lr_alloc (all_blocks);
1042 df_lr_local_compute (all_blocks);
1043 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1044 df_lr_finalize (all_blocks);
1045 df_set_flags (DF_LR_RUN_DCE);
1051 /* Free all storage associated with the problem. */
1053 static void
1054 df_lr_free (void)
1056 struct df_lr_problem_data *problem_data
1057 = (struct df_lr_problem_data *) df_lr->problem_data;
1058 if (df_lr->block_info)
1061 df_lr->block_info_size = 0;
1062 free (df_lr->block_info);
1063 df_lr->block_info = NULL;
1064 bitmap_obstack_release (&problem_data->lr_bitmaps);
1065 free (df_lr->problem_data);
1066 df_lr->problem_data = NULL;
1069 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1070 free (df_lr);
1074 /* Debugging info at top of bb. */
1076 static void
1077 df_lr_top_dump (basic_block bb, FILE *file)
1079 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1080 struct df_lr_problem_data *problem_data;
1081 if (!bb_info)
1082 return;
1084 fprintf (file, ";; lr in \t");
1085 df_print_regset (file, &bb_info->in);
1086 if (df_lr->problem_data)
1088 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1089 if (problem_data->in)
1091 fprintf (file, ";; old in \t");
1092 df_print_regset (file, &problem_data->in[bb->index]);
1095 fprintf (file, ";; lr use \t");
1096 df_print_regset (file, &bb_info->use);
1097 fprintf (file, ";; lr def \t");
1098 df_print_regset (file, &bb_info->def);
1102 /* Debugging info at bottom of bb. */
1104 static void
1105 df_lr_bottom_dump (basic_block bb, FILE *file)
1107 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1108 struct df_lr_problem_data *problem_data;
1109 if (!bb_info)
1110 return;
1112 fprintf (file, ";; lr out \t");
1113 df_print_regset (file, &bb_info->out);
1114 if (df_lr->problem_data)
1116 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1117 if (problem_data->out)
1119 fprintf (file, ";; old out \t");
1120 df_print_regset (file, &problem_data->out[bb->index]);
1126 /* Build the datastructure to verify that the solution to the dataflow
1127 equations is not dirty. */
1129 static void
1130 df_lr_verify_solution_start (void)
1132 basic_block bb;
1133 struct df_lr_problem_data *problem_data;
1134 if (df_lr->solutions_dirty)
1135 return;
1137 /* Set it true so that the solution is recomputed. */
1138 df_lr->solutions_dirty = true;
1140 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1141 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1142 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1144 FOR_ALL_BB_FN (bb, cfun)
1146 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1147 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1148 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1149 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1154 /* Compare the saved datastructure and the new solution to the dataflow
1155 equations. */
1157 static void
1158 df_lr_verify_solution_end (void)
1160 struct df_lr_problem_data *problem_data;
1161 basic_block bb;
1163 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1165 if (!problem_data->out)
1166 return;
1168 if (df_lr->solutions_dirty)
1169 /* Do not check if the solution is still dirty. See the comment
1170 in df_lr_finalize for details. */
1171 df_lr->solutions_dirty = false;
1172 else
1173 FOR_ALL_BB_FN (bb, cfun)
1175 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1176 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1178 /*df_dump (stderr);*/
1179 gcc_unreachable ();
1183 /* Cannot delete them immediately because you may want to dump them
1184 if the comparison fails. */
1185 FOR_ALL_BB_FN (bb, cfun)
1187 bitmap_clear (&problem_data->in[bb->index]);
1188 bitmap_clear (&problem_data->out[bb->index]);
1191 free (problem_data->in);
1192 free (problem_data->out);
1193 problem_data->in = NULL;
1194 problem_data->out = NULL;
1198 /* All of the information associated with every instance of the problem. */
1200 static const struct df_problem problem_LR =
1202 DF_LR, /* Problem id. */
1203 DF_BACKWARD, /* Direction. */
1204 df_lr_alloc, /* Allocate the problem specific data. */
1205 df_lr_reset, /* Reset global information. */
1206 df_lr_free_bb_info, /* Free basic block info. */
1207 df_lr_local_compute, /* Local compute function. */
1208 df_lr_init, /* Init the solution specific data. */
1209 df_worklist_dataflow, /* Worklist solver. */
1210 df_lr_confluence_0, /* Confluence operator 0. */
1211 df_lr_confluence_n, /* Confluence operator n. */
1212 df_lr_transfer_function, /* Transfer function. */
1213 df_lr_finalize, /* Finalize function. */
1214 df_lr_free, /* Free all of the problem information. */
1215 NULL, /* Remove this problem from the stack of dataflow problems. */
1216 NULL, /* Debugging. */
1217 df_lr_top_dump, /* Debugging start block. */
1218 df_lr_bottom_dump, /* Debugging end block. */
1219 NULL, /* Debugging start insn. */
1220 NULL, /* Debugging end insn. */
1221 df_lr_verify_solution_start,/* Incremental solution verify start. */
1222 df_lr_verify_solution_end, /* Incremental solution verify end. */
1223 NULL, /* Dependent problem. */
1224 sizeof (class df_lr_bb_info),/* Size of entry of block_info array. */
1225 TV_DF_LR, /* Timing variable. */
1226 false /* Reset blocks on dropping out of blocks_to_analyze. */
1230 /* Create a new DATAFLOW instance and add it to an existing instance
1231 of DF. The returned structure is what is used to get at the
1232 solution. */
1234 void
1235 df_lr_add_problem (void)
1237 df_add_problem (&problem_LR);
1238 /* These will be initialized when df_scan_blocks processes each
1239 block. */
1240 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1244 /* Verify that all of the lr related info is consistent and
1245 correct. */
1247 void
1248 df_lr_verify_transfer_functions (void)
1250 basic_block bb;
1251 bitmap_head saved_def;
1252 bitmap_head saved_use;
1253 bitmap_head all_blocks;
1255 if (!df)
1256 return;
1258 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1259 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1260 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1262 FOR_ALL_BB_FN (bb, cfun)
1264 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1265 bitmap_set_bit (&all_blocks, bb->index);
1267 if (bb_info)
1269 /* Make a copy of the transfer functions and then compute
1270 new ones to see if the transfer functions have
1271 changed. */
1272 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1273 bb->index))
1275 bitmap_copy (&saved_def, &bb_info->def);
1276 bitmap_copy (&saved_use, &bb_info->use);
1277 bitmap_clear (&bb_info->def);
1278 bitmap_clear (&bb_info->use);
1280 df_lr_bb_local_compute (bb->index);
1281 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1282 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1285 else
1287 /* If we do not have basic block info, the block must be in
1288 the list of dirty blocks or else some one has added a
1289 block behind our backs. */
1290 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1291 bb->index));
1293 /* Make sure no one created a block without following
1294 procedures. */
1295 gcc_assert (df_scan_get_bb_info (bb->index));
1298 /* Make sure there are no dirty bits in blocks that have been deleted. */
1299 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1300 &all_blocks));
1302 bitmap_clear (&saved_def);
1303 bitmap_clear (&saved_use);
1304 bitmap_clear (&all_blocks);
1309 /*----------------------------------------------------------------------------
1310 LIVE AND MAY-INITIALIZED REGISTERS.
1312 This problem first computes the IN and OUT bitvectors for the
1313 may-initialized registers problems, which is a forward problem.
1314 It gives the set of registers for which we MAY have an available
1315 definition, i.e. for which there is an available definition on
1316 at least one path from the entry block to the entry/exit of a
1317 basic block. Sets generate a definition, while clobbers kill
1318 a definition.
1320 In and out bitvectors are built for each basic block and are indexed by
1321 regnum (see df.h for details). In and out bitvectors in struct
1322 df_live_bb_info actually refers to the may-initialized problem;
1324 Then, the in and out sets for the LIVE problem itself are computed.
1325 These are the logical AND of the IN and OUT sets from the LR problem
1326 and the may-initialized problem.
1327 ----------------------------------------------------------------------------*/
1329 /* Private data used to verify the solution for this problem. */
1330 struct df_live_problem_data
1332 bitmap_head *in;
1333 bitmap_head *out;
1334 /* An obstack for the bitmaps we need for this problem. */
1335 bitmap_obstack live_bitmaps;
1338 /* Scratch var used by transfer functions. This is used to implement
1339 an optimization to reduce the amount of space used to compute the
1340 combined lr and live analysis. */
1341 static bitmap_head df_live_scratch;
1344 /* Free basic block info. */
1346 static void
1347 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1348 void *vbb_info)
1350 class df_live_bb_info *bb_info = (class df_live_bb_info *) vbb_info;
1351 if (bb_info)
1353 bitmap_clear (&bb_info->gen);
1354 bitmap_clear (&bb_info->kill);
1355 bitmap_clear (&bb_info->in);
1356 bitmap_clear (&bb_info->out);
1361 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1362 not touched unless the block is new. */
1364 static void
1365 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1367 unsigned int bb_index;
1368 bitmap_iterator bi;
1369 struct df_live_problem_data *problem_data;
1371 if (df_live->problem_data)
1372 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1373 else
1375 problem_data = XNEW (struct df_live_problem_data);
1376 df_live->problem_data = problem_data;
1378 problem_data->out = NULL;
1379 problem_data->in = NULL;
1380 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1381 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1384 df_grow_bb_info (df_live);
1386 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1388 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1390 /* When bitmaps are already initialized, just clear them. */
1391 if (bb_info->kill.obstack)
1393 bitmap_clear (&bb_info->kill);
1394 bitmap_clear (&bb_info->gen);
1396 else
1398 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1399 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1400 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1401 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1404 df_live->optional_p = (optimize <= 1);
1408 /* Reset the global solution for recalculation. */
1410 static void
1411 df_live_reset (bitmap all_blocks)
1413 unsigned int bb_index;
1414 bitmap_iterator bi;
1416 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1418 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1419 gcc_assert (bb_info);
1420 bitmap_clear (&bb_info->in);
1421 bitmap_clear (&bb_info->out);
1426 /* Compute local uninitialized register info for basic block BB. */
1428 static void
1429 df_live_bb_local_compute (unsigned int bb_index)
1431 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1432 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1433 rtx_insn *insn;
1434 df_ref def;
1435 int luid = 0;
1437 FOR_BB_INSNS (bb, insn)
1439 unsigned int uid = INSN_UID (insn);
1440 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1442 /* Inserting labels does not always trigger the incremental
1443 rescanning. */
1444 if (!insn_info)
1446 gcc_assert (!INSN_P (insn));
1447 insn_info = df_insn_create_insn_record (insn);
1450 DF_INSN_INFO_LUID (insn_info) = luid;
1451 if (!INSN_P (insn))
1452 continue;
1454 luid++;
1455 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1457 unsigned int regno = DF_REF_REGNO (def);
1459 if (DF_REF_FLAGS_IS_SET (def,
1460 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1461 /* All partial or conditional def
1462 seen are included in the gen set. */
1463 bitmap_set_bit (&bb_info->gen, regno);
1464 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1465 /* Only must clobbers for the entire reg destroy the
1466 value. */
1467 bitmap_set_bit (&bb_info->kill, regno);
1468 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1469 bitmap_set_bit (&bb_info->gen, regno);
1473 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1474 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1478 /* Compute local uninitialized register info. */
1480 static void
1481 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1483 unsigned int bb_index;
1484 bitmap_iterator bi;
1486 df_grow_insn_info ();
1488 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1489 0, bb_index, bi)
1491 df_live_bb_local_compute (bb_index);
1494 bitmap_clear (df_live->out_of_date_transfer_functions);
1498 /* Initialize the solution vectors. */
1500 static void
1501 df_live_init (bitmap all_blocks)
1503 unsigned int bb_index;
1504 bitmap_iterator bi;
1506 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1508 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1509 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1511 /* No register may reach a location where it is not used. Thus
1512 we trim the rr result to the places where it is used. */
1513 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1514 bitmap_clear (&bb_info->in);
1518 /* Forward confluence function that ignores fake edges. */
1520 static bool
1521 df_live_confluence_n (edge e)
1523 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1524 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1526 if (e->flags & EDGE_FAKE)
1527 return false;
1529 return bitmap_ior_into (op1, op2);
1533 /* Transfer function for the forwards may-initialized problem. */
1535 static bool
1536 df_live_transfer_function (int bb_index)
1538 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1539 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1540 bitmap in = &bb_info->in;
1541 bitmap out = &bb_info->out;
1542 bitmap gen = &bb_info->gen;
1543 bitmap kill = &bb_info->kill;
1545 /* We need to use a scratch set here so that the value returned from this
1546 function invocation properly reflects whether the sets changed in a
1547 significant way; i.e. not just because the lr set was anded in. */
1548 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1549 /* No register may reach a location where it is not used. Thus
1550 we trim the rr result to the places where it is used. */
1551 bitmap_and_into (in, &bb_lr_info->in);
1553 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1557 /* And the LR info with the may-initialized registers to produce the LIVE info. */
1559 static void
1560 df_live_finalize (bitmap all_blocks)
1563 if (df_live->solutions_dirty)
1565 bitmap_iterator bi;
1566 unsigned int bb_index;
1568 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1570 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1571 class df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1573 /* No register may reach a location where it is not used. Thus
1574 we trim the rr result to the places where it is used. */
1575 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1576 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1579 df_live->solutions_dirty = false;
1584 /* Free all storage associated with the problem. */
1586 static void
1587 df_live_free (void)
1589 struct df_live_problem_data *problem_data
1590 = (struct df_live_problem_data *) df_live->problem_data;
1591 if (df_live->block_info)
1593 df_live->block_info_size = 0;
1594 free (df_live->block_info);
1595 df_live->block_info = NULL;
1596 bitmap_release (&df_live_scratch);
1597 bitmap_obstack_release (&problem_data->live_bitmaps);
1598 free (problem_data);
1599 df_live->problem_data = NULL;
1601 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1602 free (df_live);
1606 /* Debugging info at top of bb. */
1608 static void
1609 df_live_top_dump (basic_block bb, FILE *file)
1611 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1612 struct df_live_problem_data *problem_data;
1614 if (!bb_info)
1615 return;
1617 fprintf (file, ";; live in \t");
1618 df_print_regset (file, &bb_info->in);
1619 if (df_live->problem_data)
1621 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1622 if (problem_data->in)
1624 fprintf (file, ";; old in \t");
1625 df_print_regset (file, &problem_data->in[bb->index]);
1628 fprintf (file, ";; live gen \t");
1629 df_print_regset (file, &bb_info->gen);
1630 fprintf (file, ";; live kill\t");
1631 df_print_regset (file, &bb_info->kill);
1635 /* Debugging info at bottom of bb. */
1637 static void
1638 df_live_bottom_dump (basic_block bb, FILE *file)
1640 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1641 struct df_live_problem_data *problem_data;
1643 if (!bb_info)
1644 return;
1646 fprintf (file, ";; live out \t");
1647 df_print_regset (file, &bb_info->out);
1648 if (df_live->problem_data)
1650 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1651 if (problem_data->out)
1653 fprintf (file, ";; old out \t");
1654 df_print_regset (file, &problem_data->out[bb->index]);
1660 /* Build the datastructure to verify that the solution to the dataflow
1661 equations is not dirty. */
1663 static void
1664 df_live_verify_solution_start (void)
1666 basic_block bb;
1667 struct df_live_problem_data *problem_data;
1668 if (df_live->solutions_dirty)
1669 return;
1671 /* Set it true so that the solution is recomputed. */
1672 df_live->solutions_dirty = true;
1674 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1675 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1676 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1678 FOR_ALL_BB_FN (bb, cfun)
1680 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1681 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1682 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1683 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1688 /* Compare the saved datastructure and the new solution to the dataflow
1689 equations. */
1691 static void
1692 df_live_verify_solution_end (void)
1694 struct df_live_problem_data *problem_data;
1695 basic_block bb;
1697 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1698 if (!problem_data->out)
1699 return;
1701 FOR_ALL_BB_FN (bb, cfun)
1703 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1704 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1706 /*df_dump (stderr);*/
1707 gcc_unreachable ();
1711 /* Cannot delete them immediately because you may want to dump them
1712 if the comparison fails. */
1713 FOR_ALL_BB_FN (bb, cfun)
1715 bitmap_clear (&problem_data->in[bb->index]);
1716 bitmap_clear (&problem_data->out[bb->index]);
1719 free (problem_data->in);
1720 free (problem_data->out);
1721 free (problem_data);
1722 df_live->problem_data = NULL;
1726 /* All of the information associated with every instance of the problem. */
1728 static const struct df_problem problem_LIVE =
1730 DF_LIVE, /* Problem id. */
1731 DF_FORWARD, /* Direction. */
1732 df_live_alloc, /* Allocate the problem specific data. */
1733 df_live_reset, /* Reset global information. */
1734 df_live_free_bb_info, /* Free basic block info. */
1735 df_live_local_compute, /* Local compute function. */
1736 df_live_init, /* Init the solution specific data. */
1737 df_worklist_dataflow, /* Worklist solver. */
1738 NULL, /* Confluence operator 0. */
1739 df_live_confluence_n, /* Confluence operator n. */
1740 df_live_transfer_function, /* Transfer function. */
1741 df_live_finalize, /* Finalize function. */
1742 df_live_free, /* Free all of the problem information. */
1743 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1744 NULL, /* Debugging. */
1745 df_live_top_dump, /* Debugging start block. */
1746 df_live_bottom_dump, /* Debugging end block. */
1747 NULL, /* Debugging start insn. */
1748 NULL, /* Debugging end insn. */
1749 df_live_verify_solution_start,/* Incremental solution verify start. */
1750 df_live_verify_solution_end, /* Incremental solution verify end. */
1751 &problem_LR, /* Dependent problem. */
1752 sizeof (class df_live_bb_info),/* Size of entry of block_info array. */
1753 TV_DF_LIVE, /* Timing variable. */
1754 false /* Reset blocks on dropping out of blocks_to_analyze. */
1758 /* Create a new DATAFLOW instance and add it to an existing instance
1759 of DF. The returned structure is what is used to get at the
1760 solution. */
1762 void
1763 df_live_add_problem (void)
1765 df_add_problem (&problem_LIVE);
1766 /* These will be initialized when df_scan_blocks processes each
1767 block. */
1768 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1772 /* Set all of the blocks as dirty. This needs to be done if this
1773 problem is added after all of the insns have been scanned. */
1775 void
1776 df_live_set_all_dirty (void)
1778 basic_block bb;
1779 FOR_ALL_BB_FN (bb, cfun)
1780 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1781 bb->index);
1785 /* Verify that all of the lr related info is consistent and
1786 correct. */
1788 void
1789 df_live_verify_transfer_functions (void)
1791 basic_block bb;
1792 bitmap_head saved_gen;
1793 bitmap_head saved_kill;
1794 bitmap_head all_blocks;
1796 if (!df)
1797 return;
1799 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1800 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1801 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1803 df_grow_insn_info ();
1805 FOR_ALL_BB_FN (bb, cfun)
1807 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1808 bitmap_set_bit (&all_blocks, bb->index);
1810 if (bb_info)
1812 /* Make a copy of the transfer functions and then compute
1813 new ones to see if the transfer functions have
1814 changed. */
1815 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1816 bb->index))
1818 bitmap_copy (&saved_gen, &bb_info->gen);
1819 bitmap_copy (&saved_kill, &bb_info->kill);
1820 bitmap_clear (&bb_info->gen);
1821 bitmap_clear (&bb_info->kill);
1823 df_live_bb_local_compute (bb->index);
1824 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1825 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1828 else
1830 /* If we do not have basic block info, the block must be in
1831 the list of dirty blocks or else some one has added a
1832 block behind our backs. */
1833 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1834 bb->index));
1836 /* Make sure no one created a block without following
1837 procedures. */
1838 gcc_assert (df_scan_get_bb_info (bb->index));
1841 /* Make sure there are no dirty bits in blocks that have been deleted. */
1842 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1843 &all_blocks));
1844 bitmap_clear (&saved_gen);
1845 bitmap_clear (&saved_kill);
1846 bitmap_clear (&all_blocks);
1849 /*----------------------------------------------------------------------------
1850 MUST-INITIALIZED REGISTERS.
1851 ----------------------------------------------------------------------------*/
1853 /* Private data used to verify the solution for this problem. */
1854 struct df_mir_problem_data
1856 bitmap_head *in;
1857 bitmap_head *out;
1858 /* An obstack for the bitmaps we need for this problem. */
1859 bitmap_obstack mir_bitmaps;
1863 /* Free basic block info. */
1865 static void
1866 df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1867 void *vbb_info)
1869 class df_mir_bb_info *bb_info = (class df_mir_bb_info *) vbb_info;
1870 if (bb_info)
1872 bitmap_clear (&bb_info->gen);
1873 bitmap_clear (&bb_info->kill);
1874 bitmap_clear (&bb_info->in);
1875 bitmap_clear (&bb_info->out);
1880 /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1881 not touched unless the block is new. */
1883 static void
1884 df_mir_alloc (bitmap all_blocks)
1886 unsigned int bb_index;
1887 bitmap_iterator bi;
1888 struct df_mir_problem_data *problem_data;
1890 if (df_mir->problem_data)
1891 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1892 else
1894 problem_data = XNEW (struct df_mir_problem_data);
1895 df_mir->problem_data = problem_data;
1897 problem_data->out = NULL;
1898 problem_data->in = NULL;
1899 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1902 df_grow_bb_info (df_mir);
1904 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1906 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1908 /* When bitmaps are already initialized, just clear them. */
1909 if (bb_info->kill.obstack)
1911 bitmap_clear (&bb_info->kill);
1912 bitmap_clear (&bb_info->gen);
1914 else
1916 bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1917 bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1918 bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1919 bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1920 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1921 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1925 df_mir->optional_p = 1;
1929 /* Reset the global solution for recalculation. */
1931 static void
1932 df_mir_reset (bitmap all_blocks)
1934 unsigned int bb_index;
1935 bitmap_iterator bi;
1937 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1939 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1941 gcc_assert (bb_info);
1943 bitmap_clear (&bb_info->in);
1944 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1945 bitmap_clear (&bb_info->out);
1946 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1951 /* Compute local uninitialized register info for basic block BB. */
1953 static void
1954 df_mir_bb_local_compute (unsigned int bb_index)
1956 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1957 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1958 rtx_insn *insn;
1959 int luid = 0;
1961 /* Ignoring artificial defs is intentional: these often pretend that some
1962 registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
1963 though they are not used for that. As a result, conservatively assume
1964 they may be uninitialized. */
1966 FOR_BB_INSNS (bb, insn)
1968 unsigned int uid = INSN_UID (insn);
1969 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1971 /* Inserting labels does not always trigger the incremental
1972 rescanning. */
1973 if (!insn_info)
1975 gcc_assert (!INSN_P (insn));
1976 insn_info = df_insn_create_insn_record (insn);
1979 DF_INSN_INFO_LUID (insn_info) = luid;
1980 if (!INSN_P (insn))
1981 continue;
1983 luid++;
1984 df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
1989 /* Compute local uninitialized register info. */
1991 static void
1992 df_mir_local_compute (bitmap all_blocks)
1994 unsigned int bb_index;
1995 bitmap_iterator bi;
1997 df_grow_insn_info ();
1999 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2001 df_mir_bb_local_compute (bb_index);
2006 /* Initialize the solution vectors. */
2008 static void
2009 df_mir_init (bitmap all_blocks)
2011 df_mir_reset (all_blocks);
2015 /* Initialize IN sets for blocks with no predecessors: when landing on such
2016 blocks, assume all registers are uninitialized. */
2018 static void
2019 df_mir_confluence_0 (basic_block bb)
2021 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2023 bitmap_clear (&bb_info->in);
2027 /* Forward confluence function that ignores fake edges. */
2029 static bool
2030 df_mir_confluence_n (edge e)
2032 bitmap op1 = &df_mir_get_bb_info (e->dest->index)->in;
2033 bitmap op2 = &df_mir_get_bb_info (e->src->index)->out;
2035 if (e->flags & EDGE_FAKE)
2036 return false;
2038 /* A register is must-initialized at the entry of a basic block iff it is
2039 must-initialized at the exit of all the predecessors. */
2040 return bitmap_and_into (op1, op2);
2044 /* Transfer function for the forwards must-initialized problem. */
2046 static bool
2047 df_mir_transfer_function (int bb_index)
2049 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2050 bitmap in = &bb_info->in;
2051 bitmap out = &bb_info->out;
2052 bitmap gen = &bb_info->gen;
2053 bitmap kill = &bb_info->kill;
2055 return bitmap_ior_and_compl (out, gen, in, kill);
2059 /* Free all storage associated with the problem. */
2061 static void
2062 df_mir_free (void)
2064 struct df_mir_problem_data *problem_data
2065 = (struct df_mir_problem_data *) df_mir->problem_data;
2066 if (df_mir->block_info)
2068 df_mir->block_info_size = 0;
2069 free (df_mir->block_info);
2070 df_mir->block_info = NULL;
2071 bitmap_obstack_release (&problem_data->mir_bitmaps);
2072 free (problem_data);
2073 df_mir->problem_data = NULL;
2075 free (df_mir);
2079 /* Debugging info at top of bb. */
2081 static void
2082 df_mir_top_dump (basic_block bb, FILE *file)
2084 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2086 if (!bb_info)
2087 return;
2089 fprintf (file, ";; mir in \t");
2090 df_print_regset (file, &bb_info->in);
2091 fprintf (file, ";; mir kill\t");
2092 df_print_regset (file, &bb_info->kill);
2093 fprintf (file, ";; mir gen \t");
2094 df_print_regset (file, &bb_info->gen);
2097 /* Debugging info at bottom of bb. */
2099 static void
2100 df_mir_bottom_dump (basic_block bb, FILE *file)
2102 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2104 if (!bb_info)
2105 return;
2107 fprintf (file, ";; mir out \t");
2108 df_print_regset (file, &bb_info->out);
2112 /* Build the datastructure to verify that the solution to the dataflow
2113 equations is not dirty. */
2115 static void
2116 df_mir_verify_solution_start (void)
2118 basic_block bb;
2119 struct df_mir_problem_data *problem_data;
2120 if (df_mir->solutions_dirty)
2121 return;
2123 /* Set it true so that the solution is recomputed. */
2124 df_mir->solutions_dirty = true;
2126 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2127 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2128 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2129 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2131 FOR_ALL_BB_FN (bb, cfun)
2133 bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2134 bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2135 bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2136 bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2141 /* Compare the saved datastructure and the new solution to the dataflow
2142 equations. */
2144 static void
2145 df_mir_verify_solution_end (void)
2147 struct df_mir_problem_data *problem_data;
2148 basic_block bb;
2150 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2151 if (!problem_data->out)
2152 return;
2154 FOR_ALL_BB_FN (bb, cfun)
2156 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2157 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2158 gcc_unreachable ();
2161 /* Cannot delete them immediately because you may want to dump them
2162 if the comparison fails. */
2163 FOR_ALL_BB_FN (bb, cfun)
2165 bitmap_clear (&problem_data->in[bb->index]);
2166 bitmap_clear (&problem_data->out[bb->index]);
2169 free (problem_data->in);
2170 free (problem_data->out);
2171 bitmap_obstack_release (&problem_data->mir_bitmaps);
2172 free (problem_data);
2173 df_mir->problem_data = NULL;
2177 /* All of the information associated with every instance of the problem. */
2179 static const struct df_problem problem_MIR =
2181 DF_MIR, /* Problem id. */
2182 DF_FORWARD, /* Direction. */
2183 df_mir_alloc, /* Allocate the problem specific data. */
2184 df_mir_reset, /* Reset global information. */
2185 df_mir_free_bb_info, /* Free basic block info. */
2186 df_mir_local_compute, /* Local compute function. */
2187 df_mir_init, /* Init the solution specific data. */
2188 df_worklist_dataflow, /* Worklist solver. */
2189 df_mir_confluence_0, /* Confluence operator 0. */
2190 df_mir_confluence_n, /* Confluence operator n. */
2191 df_mir_transfer_function, /* Transfer function. */
2192 NULL, /* Finalize function. */
2193 df_mir_free, /* Free all of the problem information. */
2194 df_mir_free, /* Remove this problem from the stack of dataflow problems. */
2195 NULL, /* Debugging. */
2196 df_mir_top_dump, /* Debugging start block. */
2197 df_mir_bottom_dump, /* Debugging end block. */
2198 NULL, /* Debugging start insn. */
2199 NULL, /* Debugging end insn. */
2200 df_mir_verify_solution_start, /* Incremental solution verify start. */
2201 df_mir_verify_solution_end, /* Incremental solution verify end. */
2202 NULL, /* Dependent problem. */
2203 sizeof (class df_mir_bb_info),/* Size of entry of block_info array. */
2204 TV_DF_MIR, /* Timing variable. */
2205 false /* Reset blocks on dropping out of blocks_to_analyze. */
2209 /* Create a new DATAFLOW instance and add it to an existing instance
2210 of DF. */
2212 void
2213 df_mir_add_problem (void)
2215 df_add_problem (&problem_MIR);
2216 /* These will be initialized when df_scan_blocks processes each
2217 block. */
2218 df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2222 /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps. */
2224 void
2225 df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2226 bitmap kill, bitmap gen)
2228 df_ref def;
2230 FOR_EACH_INSN_DEF (def, insn)
2232 unsigned int regno = DF_REF_REGNO (def);
2234 /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2235 previous gen is irrelevant (and reciprocally). Also, claim that a
2236 register is GEN only if it is in all cases. */
2237 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2239 bitmap_set_bit (kill, regno);
2240 bitmap_clear_bit (gen, regno);
2242 /* In the worst case, partial and conditional defs can leave bits
2243 uninitialized, so assume they do not change anything. */
2244 else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2246 bitmap_set_bit (gen, regno);
2247 bitmap_clear_bit (kill, regno);
2252 /*----------------------------------------------------------------------------
2253 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2255 Link either the defs to the uses and / or the uses to the defs.
2257 These problems are set up like the other dataflow problems so that
2258 they nicely fit into the framework. They are much simpler and only
2259 involve a single traversal of instructions and an examination of
2260 the reaching defs information (the dependent problem).
2261 ----------------------------------------------------------------------------*/
2263 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2265 /* Create a du or ud chain from SRC to DST and link it into SRC. */
2267 struct df_link *
2268 df_chain_create (df_ref src, df_ref dst)
2270 struct df_link *head = DF_REF_CHAIN (src);
2271 struct df_link *link = df_chain->block_pool->allocate ();
2273 DF_REF_CHAIN (src) = link;
2274 link->next = head;
2275 link->ref = dst;
2276 return link;
2280 /* Delete any du or ud chains that start at REF and point to
2281 TARGET. */
2282 static void
2283 df_chain_unlink_1 (df_ref ref, df_ref target)
2285 struct df_link *chain = DF_REF_CHAIN (ref);
2286 struct df_link *prev = NULL;
2288 while (chain)
2290 if (chain->ref == target)
2292 if (prev)
2293 prev->next = chain->next;
2294 else
2295 DF_REF_CHAIN (ref) = chain->next;
2296 df_chain->block_pool->remove (chain);
2297 return;
2299 prev = chain;
2300 chain = chain->next;
2305 /* Delete a du or ud chain that leave or point to REF. */
2307 void
2308 df_chain_unlink (df_ref ref)
2310 struct df_link *chain = DF_REF_CHAIN (ref);
2311 while (chain)
2313 struct df_link *next = chain->next;
2314 /* Delete the other side if it exists. */
2315 df_chain_unlink_1 (chain->ref, ref);
2316 df_chain->block_pool->remove (chain);
2317 chain = next;
2319 DF_REF_CHAIN (ref) = NULL;
2323 /* Copy the du or ud chain starting at FROM_REF and attach it to
2324 TO_REF. */
2326 void
2327 df_chain_copy (df_ref to_ref,
2328 struct df_link *from_ref)
2330 while (from_ref)
2332 df_chain_create (to_ref, from_ref->ref);
2333 from_ref = from_ref->next;
2338 /* Remove this problem from the stack of dataflow problems. */
2340 static void
2341 df_chain_remove_problem (void)
2343 bitmap_iterator bi;
2344 unsigned int bb_index;
2346 /* Wholesale destruction of the old chains. */
2347 if (df_chain->block_pool)
2348 delete df_chain->block_pool;
2350 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2352 rtx_insn *insn;
2353 df_ref def, use;
2354 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2356 if (df_chain_problem_p (DF_DU_CHAIN))
2357 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2358 DF_REF_CHAIN (def) = NULL;
2359 if (df_chain_problem_p (DF_UD_CHAIN))
2360 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2361 DF_REF_CHAIN (use) = NULL;
2363 FOR_BB_INSNS (bb, insn)
2364 if (INSN_P (insn))
2366 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2367 if (df_chain_problem_p (DF_DU_CHAIN))
2368 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2369 DF_REF_CHAIN (def) = NULL;
2370 if (df_chain_problem_p (DF_UD_CHAIN))
2372 FOR_EACH_INSN_INFO_USE (use, insn_info)
2373 DF_REF_CHAIN (use) = NULL;
2374 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2375 DF_REF_CHAIN (use) = NULL;
2380 bitmap_clear (df_chain->out_of_date_transfer_functions);
2381 df_chain->block_pool = NULL;
2385 /* Remove the chain problem completely. */
2387 static void
2388 df_chain_fully_remove_problem (void)
2390 df_chain_remove_problem ();
2391 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2392 free (df_chain);
2396 /* Create def-use or use-def chains. */
2398 static void
2399 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2401 df_chain_remove_problem ();
2402 df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2403 df_chain->optional_p = true;
2407 /* Reset all of the chains when the set of basic blocks changes. */
2409 static void
2410 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2412 df_chain_remove_problem ();
2416 /* Create the chains for a list of USEs. */
2418 static void
2419 df_chain_create_bb_process_use (bitmap local_rd,
2420 df_ref use,
2421 int top_flag)
2423 bitmap_iterator bi;
2424 unsigned int def_index;
2426 for (; use; use = DF_REF_NEXT_LOC (use))
2428 unsigned int uregno = DF_REF_REGNO (use);
2429 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2430 || (uregno >= FIRST_PSEUDO_REGISTER))
2432 /* Do not want to go through this for an uninitialized var. */
2433 int count = DF_DEFS_COUNT (uregno);
2434 if (count)
2436 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2438 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2439 unsigned int last_index = first_index + count - 1;
2441 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2443 df_ref def;
2444 if (def_index > last_index)
2445 break;
2447 def = DF_DEFS_GET (def_index);
2448 if (df_chain_problem_p (DF_DU_CHAIN))
2449 df_chain_create (def, use);
2450 if (df_chain_problem_p (DF_UD_CHAIN))
2451 df_chain_create (use, def);
2460 /* Create chains from reaching defs bitmaps for basic block BB. */
2462 static void
2463 df_chain_create_bb (unsigned int bb_index)
2465 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2466 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2467 rtx_insn *insn;
2468 bitmap_head cpy;
2470 bitmap_initialize (&cpy, &bitmap_default_obstack);
2471 bitmap_copy (&cpy, &bb_info->in);
2472 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2474 /* Since we are going forwards, process the artificial uses first
2475 then the artificial defs second. */
2477 #ifdef EH_USES
2478 /* Create the chains for the artificial uses from the EH_USES at the
2479 beginning of the block. */
2481 /* Artificials are only hard regs. */
2482 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2483 df_chain_create_bb_process_use (&cpy,
2484 df_get_artificial_uses (bb->index),
2485 DF_REF_AT_TOP);
2486 #endif
2488 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2490 /* Process the regular instructions next. */
2491 FOR_BB_INSNS (bb, insn)
2492 if (INSN_P (insn))
2494 unsigned int uid = INSN_UID (insn);
2496 /* First scan the uses and link them up with the defs that remain
2497 in the cpy vector. */
2498 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2499 if (df->changeable_flags & DF_EQ_NOTES)
2500 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2502 /* Since we are going forwards, process the defs second. */
2503 df_rd_simulate_one_insn (bb, insn, &cpy);
2506 /* Create the chains for the artificial uses of the hard registers
2507 at the end of the block. */
2508 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2509 df_chain_create_bb_process_use (&cpy,
2510 df_get_artificial_uses (bb->index),
2513 bitmap_clear (&cpy);
2516 /* Create def-use chains from reaching use bitmaps for basic blocks
2517 in BLOCKS. */
2519 static void
2520 df_chain_finalize (bitmap all_blocks)
2522 unsigned int bb_index;
2523 bitmap_iterator bi;
2525 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2527 df_chain_create_bb (bb_index);
2532 /* Free all storage associated with the problem. */
2534 static void
2535 df_chain_free (void)
2537 delete df_chain->block_pool;
2538 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2539 free (df_chain);
2543 /* Debugging info. */
2545 static void
2546 df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2548 /* Artificials are only hard regs. */
2549 if (df->changeable_flags & DF_NO_HARD_REGS)
2550 return;
2551 if (df_chain_problem_p (DF_UD_CHAIN))
2553 df_ref use;
2555 fprintf (file,
2556 ";; UD chains for artificial uses at %s\n",
2557 top ? "top" : "bottom");
2558 FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2559 if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2560 || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2562 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2563 df_chain_dump (DF_REF_CHAIN (use), file);
2564 fprintf (file, "\n");
2567 if (df_chain_problem_p (DF_DU_CHAIN))
2569 df_ref def;
2571 fprintf (file,
2572 ";; DU chains for artificial defs at %s\n",
2573 top ? "top" : "bottom");
2574 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2575 if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2576 || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2578 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2579 df_chain_dump (DF_REF_CHAIN (def), file);
2580 fprintf (file, "\n");
2585 static void
2586 df_chain_top_dump (basic_block bb, FILE *file)
2588 df_chain_bb_dump (bb, file, /*top=*/true);
2591 static void
2592 df_chain_bottom_dump (basic_block bb, FILE *file)
2594 df_chain_bb_dump (bb, file, /*top=*/false);
2597 static void
2598 df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2600 if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2602 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2603 df_ref use;
2605 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2606 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2607 FOR_EACH_INSN_INFO_USE (use, insn_info)
2608 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2609 || !(df->changeable_flags & DF_NO_HARD_REGS))
2611 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2612 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2613 fprintf (file, "read/write ");
2614 df_chain_dump (DF_REF_CHAIN (use), file);
2615 fprintf (file, "\n");
2617 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2618 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2619 || !(df->changeable_flags & DF_NO_HARD_REGS))
2621 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2622 df_chain_dump (DF_REF_CHAIN (use), file);
2623 fprintf (file, "\n");
2628 static void
2629 df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2631 if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2633 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2634 df_ref def;
2635 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2636 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2637 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2638 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2639 || !(df->changeable_flags & DF_NO_HARD_REGS))
2641 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2642 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2643 fprintf (file, "read/write ");
2644 df_chain_dump (DF_REF_CHAIN (def), file);
2645 fprintf (file, "\n");
2647 fprintf (file, "\n");
2651 static const struct df_problem problem_CHAIN =
2653 DF_CHAIN, /* Problem id. */
2654 DF_NONE, /* Direction. */
2655 df_chain_alloc, /* Allocate the problem specific data. */
2656 df_chain_reset, /* Reset global information. */
2657 NULL, /* Free basic block info. */
2658 NULL, /* Local compute function. */
2659 NULL, /* Init the solution specific data. */
2660 NULL, /* Iterative solver. */
2661 NULL, /* Confluence operator 0. */
2662 NULL, /* Confluence operator n. */
2663 NULL, /* Transfer function. */
2664 df_chain_finalize, /* Finalize function. */
2665 df_chain_free, /* Free all of the problem information. */
2666 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2667 NULL, /* Debugging. */
2668 df_chain_top_dump, /* Debugging start block. */
2669 df_chain_bottom_dump, /* Debugging end block. */
2670 df_chain_insn_top_dump, /* Debugging start insn. */
2671 df_chain_insn_bottom_dump, /* Debugging end insn. */
2672 NULL, /* Incremental solution verify start. */
2673 NULL, /* Incremental solution verify end. */
2674 &problem_RD, /* Dependent problem. */
2675 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
2676 TV_DF_CHAIN, /* Timing variable. */
2677 false /* Reset blocks on dropping out of blocks_to_analyze. */
2681 /* Create a new DATAFLOW instance and add it to an existing instance
2682 of DF. The returned structure is what is used to get at the
2683 solution. */
2685 void
2686 df_chain_add_problem (unsigned int chain_flags)
2688 df_add_problem (&problem_CHAIN);
2689 df_chain->local_flags = chain_flags;
2690 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2693 #undef df_chain_problem_p
2696 /*----------------------------------------------------------------------------
2697 WORD LEVEL LIVE REGISTERS
2699 Find the locations in the function where any use of a pseudo can
2700 reach in the backwards direction. In and out bitvectors are built
2701 for each basic block. We only track pseudo registers that have a
2702 size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2703 contain two bits corresponding to each of the subwords.
2705 ----------------------------------------------------------------------------*/
2707 /* Private data used to verify the solution for this problem. */
2708 struct df_word_lr_problem_data
2710 /* An obstack for the bitmaps we need for this problem. */
2711 bitmap_obstack word_lr_bitmaps;
2715 /* Free basic block info. */
2717 static void
2718 df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2719 void *vbb_info)
2721 class df_word_lr_bb_info *bb_info = (class df_word_lr_bb_info *) vbb_info;
2722 if (bb_info)
2724 bitmap_clear (&bb_info->use);
2725 bitmap_clear (&bb_info->def);
2726 bitmap_clear (&bb_info->in);
2727 bitmap_clear (&bb_info->out);
2732 /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2733 not touched unless the block is new. */
2735 static void
2736 df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2738 unsigned int bb_index;
2739 bitmap_iterator bi;
2740 basic_block bb;
2741 struct df_word_lr_problem_data *problem_data
2742 = XNEW (struct df_word_lr_problem_data);
2744 df_word_lr->problem_data = problem_data;
2746 df_grow_bb_info (df_word_lr);
2748 /* Create the mapping from regnos to slots. This does not change
2749 unless the problem is destroyed and recreated. In particular, if
2750 we end up deleting the only insn that used a subreg, we do not
2751 want to redo the mapping because this would invalidate everything
2752 else. */
2754 bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2756 FOR_EACH_BB_FN (bb, cfun)
2757 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2759 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2760 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2762 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2764 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2766 /* When bitmaps are already initialized, just clear them. */
2767 if (bb_info->use.obstack)
2769 bitmap_clear (&bb_info->def);
2770 bitmap_clear (&bb_info->use);
2772 else
2774 bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2775 bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2776 bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2777 bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2781 df_word_lr->optional_p = true;
2785 /* Reset the global solution for recalculation. */
2787 static void
2788 df_word_lr_reset (bitmap all_blocks)
2790 unsigned int bb_index;
2791 bitmap_iterator bi;
2793 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2795 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2796 gcc_assert (bb_info);
2797 bitmap_clear (&bb_info->in);
2798 bitmap_clear (&bb_info->out);
2802 /* Examine REF, and if it is for a reg we're interested in, set or
2803 clear the bits corresponding to its subwords from the bitmap
2804 according to IS_SET. LIVE is the bitmap we should update. We do
2805 not track hard regs or pseudos of any size other than 2 *
2806 UNITS_PER_WORD.
2807 We return true if we changed the bitmap, or if we encountered a register
2808 we're not tracking. */
2810 bool
2811 df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2813 rtx orig_reg = DF_REF_REG (ref);
2814 rtx reg = orig_reg;
2815 machine_mode reg_mode;
2816 unsigned regno;
2817 /* Left at -1 for whole accesses. */
2818 int which_subword = -1;
2819 bool changed = false;
2821 if (GET_CODE (reg) == SUBREG)
2822 reg = SUBREG_REG (orig_reg);
2823 regno = REGNO (reg);
2824 reg_mode = GET_MODE (reg);
2825 if (regno < FIRST_PSEUDO_REGISTER
2826 || maybe_ne (GET_MODE_SIZE (reg_mode), 2 * UNITS_PER_WORD))
2827 return true;
2829 if (GET_CODE (orig_reg) == SUBREG
2830 && read_modify_subreg_p (orig_reg))
2832 gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2833 if (subreg_lowpart_p (orig_reg))
2834 which_subword = 0;
2835 else
2836 which_subword = 1;
2838 if (is_set)
2840 if (which_subword != 1)
2841 changed |= bitmap_set_bit (live, regno * 2);
2842 if (which_subword != 0)
2843 changed |= bitmap_set_bit (live, regno * 2 + 1);
2845 else
2847 if (which_subword != 1)
2848 changed |= bitmap_clear_bit (live, regno * 2);
2849 if (which_subword != 0)
2850 changed |= bitmap_clear_bit (live, regno * 2 + 1);
2852 return changed;
2855 /* Compute local live register info for basic block BB. */
2857 static void
2858 df_word_lr_bb_local_compute (unsigned int bb_index)
2860 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2861 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2862 rtx_insn *insn;
2863 df_ref def, use;
2865 /* Ensure that artificial refs don't contain references to pseudos. */
2866 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2867 gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2869 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2870 gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2872 FOR_BB_INSNS_REVERSE (bb, insn)
2874 if (!NONDEBUG_INSN_P (insn))
2875 continue;
2877 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2878 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2879 /* If the def is to only part of the reg, it does
2880 not kill the other defs that reach here. */
2881 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2883 df_word_lr_mark_ref (def, true, &bb_info->def);
2884 df_word_lr_mark_ref (def, false, &bb_info->use);
2886 FOR_EACH_INSN_INFO_USE (use, insn_info)
2887 df_word_lr_mark_ref (use, true, &bb_info->use);
2892 /* Compute local live register info for each basic block within BLOCKS. */
2894 static void
2895 df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2897 unsigned int bb_index;
2898 bitmap_iterator bi;
2900 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2902 if (bb_index == EXIT_BLOCK)
2904 unsigned regno;
2905 bitmap_iterator bi;
2906 EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2907 regno, bi)
2908 gcc_unreachable ();
2910 else
2911 df_word_lr_bb_local_compute (bb_index);
2914 bitmap_clear (df_word_lr->out_of_date_transfer_functions);
2918 /* Initialize the solution vectors. */
2920 static void
2921 df_word_lr_init (bitmap all_blocks)
2923 unsigned int bb_index;
2924 bitmap_iterator bi;
2926 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2928 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2929 bitmap_copy (&bb_info->in, &bb_info->use);
2930 bitmap_clear (&bb_info->out);
2935 /* Confluence function that ignores fake edges. */
2937 static bool
2938 df_word_lr_confluence_n (edge e)
2940 bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
2941 bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
2943 return bitmap_ior_into (op1, op2);
2947 /* Transfer function. */
2949 static bool
2950 df_word_lr_transfer_function (int bb_index)
2952 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2953 bitmap in = &bb_info->in;
2954 bitmap out = &bb_info->out;
2955 bitmap use = &bb_info->use;
2956 bitmap def = &bb_info->def;
2958 return bitmap_ior_and_compl (in, use, out, def);
2962 /* Free all storage associated with the problem. */
2964 static void
2965 df_word_lr_free (void)
2967 struct df_word_lr_problem_data *problem_data
2968 = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
2970 if (df_word_lr->block_info)
2972 df_word_lr->block_info_size = 0;
2973 free (df_word_lr->block_info);
2974 df_word_lr->block_info = NULL;
2977 BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
2978 bitmap_obstack_release (&problem_data->word_lr_bitmaps);
2979 free (problem_data);
2980 free (df_word_lr);
2984 /* Debugging info at top of bb. */
2986 static void
2987 df_word_lr_top_dump (basic_block bb, FILE *file)
2989 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
2990 if (!bb_info)
2991 return;
2993 fprintf (file, ";; blr in \t");
2994 df_print_word_regset (file, &bb_info->in);
2995 fprintf (file, ";; blr use \t");
2996 df_print_word_regset (file, &bb_info->use);
2997 fprintf (file, ";; blr def \t");
2998 df_print_word_regset (file, &bb_info->def);
3002 /* Debugging info at bottom of bb. */
3004 static void
3005 df_word_lr_bottom_dump (basic_block bb, FILE *file)
3007 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3008 if (!bb_info)
3009 return;
3011 fprintf (file, ";; blr out \t");
3012 df_print_word_regset (file, &bb_info->out);
3016 /* All of the information associated with every instance of the problem. */
3018 static const struct df_problem problem_WORD_LR =
3020 DF_WORD_LR, /* Problem id. */
3021 DF_BACKWARD, /* Direction. */
3022 df_word_lr_alloc, /* Allocate the problem specific data. */
3023 df_word_lr_reset, /* Reset global information. */
3024 df_word_lr_free_bb_info, /* Free basic block info. */
3025 df_word_lr_local_compute, /* Local compute function. */
3026 df_word_lr_init, /* Init the solution specific data. */
3027 df_worklist_dataflow, /* Worklist solver. */
3028 NULL, /* Confluence operator 0. */
3029 df_word_lr_confluence_n, /* Confluence operator n. */
3030 df_word_lr_transfer_function, /* Transfer function. */
3031 NULL, /* Finalize function. */
3032 df_word_lr_free, /* Free all of the problem information. */
3033 df_word_lr_free, /* Remove this problem from the stack of dataflow problems. */
3034 NULL, /* Debugging. */
3035 df_word_lr_top_dump, /* Debugging start block. */
3036 df_word_lr_bottom_dump, /* Debugging end block. */
3037 NULL, /* Debugging start insn. */
3038 NULL, /* Debugging end insn. */
3039 NULL, /* Incremental solution verify start. */
3040 NULL, /* Incremental solution verify end. */
3041 NULL, /* Dependent problem. */
3042 sizeof (class df_word_lr_bb_info),/* Size of entry of block_info array. */
3043 TV_DF_WORD_LR, /* Timing variable. */
3044 false /* Reset blocks on dropping out of blocks_to_analyze. */
3048 /* Create a new DATAFLOW instance and add it to an existing instance
3049 of DF. The returned structure is what is used to get at the
3050 solution. */
3052 void
3053 df_word_lr_add_problem (void)
3055 df_add_problem (&problem_WORD_LR);
3056 /* These will be initialized when df_scan_blocks processes each
3057 block. */
3058 df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3062 /* Simulate the effects of the defs of INSN on LIVE. Return true if we changed
3063 any bits, which is used by the caller to determine whether a set is
3064 necessary. We also return true if there are other reasons not to delete
3065 an insn. */
3067 bool
3068 df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3070 bool changed = false;
3071 df_ref def;
3073 FOR_EACH_INSN_DEF (def, insn)
3074 if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3075 changed = true;
3076 else
3077 changed |= df_word_lr_mark_ref (def, false, live);
3078 return changed;
3082 /* Simulate the effects of the uses of INSN on LIVE. */
3084 void
3085 df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3087 df_ref use;
3089 FOR_EACH_INSN_USE (use, insn)
3090 df_word_lr_mark_ref (use, true, live);
3093 /*----------------------------------------------------------------------------
3094 This problem computes REG_DEAD and REG_UNUSED notes.
3095 ----------------------------------------------------------------------------*/
3097 static void
3098 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3100 df_note->optional_p = true;
3103 /* This is only used if REG_DEAD_DEBUGGING is in effect. */
3104 static void
3105 df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3107 if (dump_file)
3109 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3110 print_rtl (dump_file, note);
3111 fprintf (dump_file, "\n");
3116 /* After reg-stack, the x86 floating point stack regs are difficult to
3117 analyze because of all of the pushes, pops and rotations. Thus, we
3118 just leave the notes alone. */
3120 #ifdef STACK_REGS
3121 static inline bool
3122 df_ignore_stack_reg (int regno)
3124 return regstack_completed
3125 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3127 #else
3128 static inline bool
3129 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3131 return false;
3133 #endif
3136 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN. */
3138 static void
3139 df_remove_dead_and_unused_notes (rtx_insn *insn)
3141 rtx *pprev = &REG_NOTES (insn);
3142 rtx link = *pprev;
3144 while (link)
3146 switch (REG_NOTE_KIND (link))
3148 case REG_DEAD:
3149 /* After reg-stack, we need to ignore any unused notes
3150 for the stack registers. */
3151 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3153 pprev = &XEXP (link, 1);
3154 link = *pprev;
3156 else
3158 rtx next = XEXP (link, 1);
3159 if (REG_DEAD_DEBUGGING)
3160 df_print_note ("deleting: ", insn, link);
3161 free_EXPR_LIST_node (link);
3162 *pprev = link = next;
3164 break;
3166 case REG_UNUSED:
3167 /* After reg-stack, we need to ignore any unused notes
3168 for the stack registers. */
3169 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3171 pprev = &XEXP (link, 1);
3172 link = *pprev;
3174 else
3176 rtx next = XEXP (link, 1);
3177 if (REG_DEAD_DEBUGGING)
3178 df_print_note ("deleting: ", insn, link);
3179 free_EXPR_LIST_node (link);
3180 *pprev = link = next;
3182 break;
3184 default:
3185 pprev = &XEXP (link, 1);
3186 link = *pprev;
3187 break;
3192 /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3193 as the bitmap of currently live registers. */
3195 static void
3196 df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3198 rtx *pprev = &REG_NOTES (insn);
3199 rtx link = *pprev;
3201 while (link)
3203 switch (REG_NOTE_KIND (link))
3205 case REG_EQUAL:
3206 case REG_EQUIV:
3208 /* Remove the notes that refer to dead registers. As we have at most
3209 one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3210 so we need to purge the complete EQ_USES vector when removing
3211 the note using df_notes_rescan. */
3212 df_ref use;
3213 bool deleted = false;
3215 FOR_EACH_INSN_EQ_USE (use, insn)
3216 if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER
3217 && DF_REF_LOC (use)
3218 && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3219 && !bitmap_bit_p (live, DF_REF_REGNO (use))
3220 && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3222 deleted = true;
3223 break;
3225 if (deleted)
3227 rtx next;
3228 if (REG_DEAD_DEBUGGING)
3229 df_print_note ("deleting: ", insn, link);
3230 next = XEXP (link, 1);
3231 free_EXPR_LIST_node (link);
3232 *pprev = link = next;
3233 df_notes_rescan (insn);
3235 else
3237 pprev = &XEXP (link, 1);
3238 link = *pprev;
3240 break;
3243 default:
3244 pprev = &XEXP (link, 1);
3245 link = *pprev;
3246 break;
3251 /* Set a NOTE_TYPE note for REG in INSN. */
3253 static inline void
3254 df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3256 gcc_checking_assert (!DEBUG_INSN_P (insn));
3257 add_reg_note (insn, note_type, reg);
3260 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3261 arguments. Return true if the register value described by MWS's
3262 mw_reg is known to be completely unused, and if mw_reg can therefore
3263 be used in a REG_UNUSED note. */
3265 static bool
3266 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3267 bitmap live, bitmap artificial_uses)
3269 unsigned int r;
3271 /* If MWS describes a partial reference, create REG_UNUSED notes for
3272 individual hard registers. */
3273 if (mws->flags & DF_REF_PARTIAL)
3274 return false;
3276 /* Likewise if some part of the register is used. */
3277 for (r = mws->start_regno; r <= mws->end_regno; r++)
3278 if (bitmap_bit_p (live, r)
3279 || bitmap_bit_p (artificial_uses, r))
3280 return false;
3282 gcc_assert (REG_P (mws->mw_reg));
3283 return true;
3287 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3288 based on the bits in LIVE. Do not generate notes for registers in
3289 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3290 not generated if the reg is both read and written by the
3291 instruction.
3294 static void
3295 df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3296 bitmap live, bitmap do_not_gen,
3297 bitmap artificial_uses,
3298 struct dead_debug_local *debug)
3300 unsigned int r;
3302 if (REG_DEAD_DEBUGGING && dump_file)
3303 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3304 mws->start_regno, mws->end_regno);
3306 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3308 unsigned int regno = mws->start_regno;
3309 df_set_note (REG_UNUSED, insn, mws->mw_reg);
3310 dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3312 if (REG_DEAD_DEBUGGING)
3313 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3315 bitmap_set_bit (do_not_gen, regno);
3316 /* Only do this if the value is totally dead. */
3318 else
3319 for (r = mws->start_regno; r <= mws->end_regno; r++)
3321 if (!bitmap_bit_p (live, r)
3322 && !bitmap_bit_p (artificial_uses, r))
3324 df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3325 dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3326 if (REG_DEAD_DEBUGGING)
3327 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3329 bitmap_set_bit (do_not_gen, r);
3334 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3335 arguments. Return true if the register value described by MWS's
3336 mw_reg is known to be completely dead, and if mw_reg can therefore
3337 be used in a REG_DEAD note. */
3339 static bool
3340 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3341 bitmap live, bitmap artificial_uses,
3342 bitmap do_not_gen)
3344 unsigned int r;
3346 /* If MWS describes a partial reference, create REG_DEAD notes for
3347 individual hard registers. */
3348 if (mws->flags & DF_REF_PARTIAL)
3349 return false;
3351 /* Likewise if some part of the register is not dead. */
3352 for (r = mws->start_regno; r <= mws->end_regno; r++)
3353 if (bitmap_bit_p (live, r)
3354 || bitmap_bit_p (artificial_uses, r)
3355 || bitmap_bit_p (do_not_gen, r))
3356 return false;
3358 gcc_assert (REG_P (mws->mw_reg));
3359 return true;
3362 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3363 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3364 from being set if the instruction both reads and writes the
3365 register. */
3367 static void
3368 df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3369 bitmap live, bitmap do_not_gen,
3370 bitmap artificial_uses, bool *added_notes_p)
3372 unsigned int r;
3373 bool is_debug = *added_notes_p;
3375 *added_notes_p = false;
3377 if (REG_DEAD_DEBUGGING && dump_file)
3379 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3380 mws->start_regno, mws->end_regno);
3381 df_print_regset (dump_file, do_not_gen);
3382 fprintf (dump_file, " live =");
3383 df_print_regset (dump_file, live);
3384 fprintf (dump_file, " artificial uses =");
3385 df_print_regset (dump_file, artificial_uses);
3388 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3390 if (is_debug)
3392 *added_notes_p = true;
3393 return;
3395 /* Add a dead note for the entire multi word register. */
3396 df_set_note (REG_DEAD, insn, mws->mw_reg);
3397 if (REG_DEAD_DEBUGGING)
3398 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3400 else
3402 for (r = mws->start_regno; r <= mws->end_regno; r++)
3403 if (!bitmap_bit_p (live, r)
3404 && !bitmap_bit_p (artificial_uses, r)
3405 && !bitmap_bit_p (do_not_gen, r))
3407 if (is_debug)
3409 *added_notes_p = true;
3410 return;
3412 df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3413 if (REG_DEAD_DEBUGGING)
3414 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3417 return;
3421 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3422 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3424 static void
3425 df_create_unused_note (rtx_insn *insn, df_ref def,
3426 bitmap live, bitmap artificial_uses,
3427 struct dead_debug_local *debug)
3429 unsigned int dregno = DF_REF_REGNO (def);
3431 if (REG_DEAD_DEBUGGING && dump_file)
3433 fprintf (dump_file, " regular looking at def ");
3434 df_ref_debug (def, dump_file);
3437 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3438 || bitmap_bit_p (live, dregno)
3439 || bitmap_bit_p (artificial_uses, dregno)
3440 || df_ignore_stack_reg (dregno)))
3442 rtx reg = (DF_REF_LOC (def))
3443 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3444 df_set_note (REG_UNUSED, insn, reg);
3445 dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3446 if (REG_DEAD_DEBUGGING)
3447 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3450 return;
3454 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3455 info: lifetime, bb, and number of defs and uses for basic block
3456 BB. The three bitvectors are scratch regs used here. */
3458 static void
3459 df_note_bb_compute (unsigned int bb_index,
3460 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3462 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3463 rtx_insn *insn;
3464 df_ref def, use;
3465 struct dead_debug_local debug;
3467 dead_debug_local_init (&debug, NULL, NULL);
3469 bitmap_copy (live, df_get_live_out (bb));
3470 bitmap_clear (artificial_uses);
3472 if (REG_DEAD_DEBUGGING && dump_file)
3474 fprintf (dump_file, "live at bottom ");
3475 df_print_regset (dump_file, live);
3478 /* Process the artificial defs and uses at the bottom of the block
3479 to begin processing. */
3480 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3482 if (REG_DEAD_DEBUGGING && dump_file)
3483 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3485 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3486 bitmap_clear_bit (live, DF_REF_REGNO (def));
3489 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3490 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3492 unsigned int regno = DF_REF_REGNO (use);
3493 bitmap_set_bit (live, regno);
3495 /* Notes are not generated for any of the artificial registers
3496 at the bottom of the block. */
3497 bitmap_set_bit (artificial_uses, regno);
3500 if (REG_DEAD_DEBUGGING && dump_file)
3502 fprintf (dump_file, "live before artificials out ");
3503 df_print_regset (dump_file, live);
3506 FOR_BB_INSNS_REVERSE (bb, insn)
3508 if (!INSN_P (insn))
3509 continue;
3511 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3512 df_mw_hardreg *mw;
3513 int debug_insn;
3515 debug_insn = DEBUG_INSN_P (insn);
3517 bitmap_clear (do_not_gen);
3518 df_remove_dead_and_unused_notes (insn);
3520 /* Process the defs. */
3521 if (CALL_P (insn))
3523 if (REG_DEAD_DEBUGGING && dump_file)
3525 fprintf (dump_file, "processing call %d\n live =",
3526 INSN_UID (insn));
3527 df_print_regset (dump_file, live);
3530 /* We only care about real sets for calls. Clobbers cannot
3531 be depended on to really die. */
3532 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3533 if ((DF_MWS_REG_DEF_P (mw))
3534 && !df_ignore_stack_reg (mw->start_regno))
3535 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3536 artificial_uses, &debug);
3538 /* All of the defs except the return value are some sort of
3539 clobber. This code is for the return. */
3540 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3542 unsigned int dregno = DF_REF_REGNO (def);
3543 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3545 df_create_unused_note (insn,
3546 def, live, artificial_uses, &debug);
3547 bitmap_set_bit (do_not_gen, dregno);
3550 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3551 bitmap_clear_bit (live, dregno);
3554 else
3556 /* Regular insn. */
3557 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3558 if (DF_MWS_REG_DEF_P (mw))
3559 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3560 artificial_uses, &debug);
3562 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3564 unsigned int dregno = DF_REF_REGNO (def);
3565 df_create_unused_note (insn,
3566 def, live, artificial_uses, &debug);
3568 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3569 bitmap_set_bit (do_not_gen, dregno);
3571 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3572 bitmap_clear_bit (live, dregno);
3576 /* Process the uses. */
3577 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3578 if (DF_MWS_REG_USE_P (mw)
3579 && !df_ignore_stack_reg (mw->start_regno))
3581 bool really_add_notes = debug_insn != 0;
3583 df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3584 artificial_uses,
3585 &really_add_notes);
3587 if (really_add_notes)
3588 debug_insn = -1;
3591 FOR_EACH_INSN_INFO_USE (use, insn_info)
3593 unsigned int uregno = DF_REF_REGNO (use);
3595 if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3597 fprintf (dump_file, " regular looking at use ");
3598 df_ref_debug (use, dump_file);
3601 if (!bitmap_bit_p (live, uregno))
3603 if (debug_insn)
3605 if (debug_insn > 0)
3607 /* We won't add REG_UNUSED or REG_DEAD notes for
3608 these, so we don't have to mess with them in
3609 debug insns either. */
3610 if (!bitmap_bit_p (artificial_uses, uregno)
3611 && !df_ignore_stack_reg (uregno))
3612 dead_debug_add (&debug, use, uregno);
3613 continue;
3615 break;
3617 else
3618 dead_debug_insert_temp (&debug, uregno, insn,
3619 DEBUG_TEMP_BEFORE_WITH_REG);
3621 if ( (!(DF_REF_FLAGS (use)
3622 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3623 && (!bitmap_bit_p (do_not_gen, uregno))
3624 && (!bitmap_bit_p (artificial_uses, uregno))
3625 && (!df_ignore_stack_reg (uregno)))
3627 rtx reg = (DF_REF_LOC (use))
3628 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3629 df_set_note (REG_DEAD, insn, reg);
3631 if (REG_DEAD_DEBUGGING)
3632 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3634 /* This register is now live. */
3635 bitmap_set_bit (live, uregno);
3639 df_remove_dead_eq_notes (insn, live);
3641 if (debug_insn == -1)
3643 /* ??? We could probably do better here, replacing dead
3644 registers with their definitions. */
3645 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3646 df_insn_rescan_debug_internal (insn);
3650 dead_debug_local_finish (&debug, NULL);
3654 /* Compute register info: lifetime, bb, and number of defs and uses. */
3655 static void
3656 df_note_compute (bitmap all_blocks)
3658 unsigned int bb_index;
3659 bitmap_iterator bi;
3660 bitmap_head live, do_not_gen, artificial_uses;
3662 bitmap_initialize (&live, &df_bitmap_obstack);
3663 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3664 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3666 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3668 /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3669 pseudos in debug insns because we don't always (re)visit blocks
3670 with death points after visiting dead uses. Even changing this
3671 loop to postorder would still leave room for visiting a death
3672 point before visiting a subsequent debug use. */
3673 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3676 bitmap_clear (&live);
3677 bitmap_clear (&do_not_gen);
3678 bitmap_clear (&artificial_uses);
3682 /* Free all storage associated with the problem. */
3684 static void
3685 df_note_free (void)
3687 free (df_note);
3691 /* All of the information associated every instance of the problem. */
3693 static const struct df_problem problem_NOTE =
3695 DF_NOTE, /* Problem id. */
3696 DF_NONE, /* Direction. */
3697 df_note_alloc, /* Allocate the problem specific data. */
3698 NULL, /* Reset global information. */
3699 NULL, /* Free basic block info. */
3700 df_note_compute, /* Local compute function. */
3701 NULL, /* Init the solution specific data. */
3702 NULL, /* Iterative solver. */
3703 NULL, /* Confluence operator 0. */
3704 NULL, /* Confluence operator n. */
3705 NULL, /* Transfer function. */
3706 NULL, /* Finalize function. */
3707 df_note_free, /* Free all of the problem information. */
3708 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3709 NULL, /* Debugging. */
3710 NULL, /* Debugging start block. */
3711 NULL, /* Debugging end block. */
3712 NULL, /* Debugging start insn. */
3713 NULL, /* Debugging end insn. */
3714 NULL, /* Incremental solution verify start. */
3715 NULL, /* Incremental solution verify end. */
3716 &problem_LR, /* Dependent problem. */
3717 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
3718 TV_DF_NOTE, /* Timing variable. */
3719 false /* Reset blocks on dropping out of blocks_to_analyze. */
3723 /* Create a new DATAFLOW instance and add it to an existing instance
3724 of DF. The returned structure is what is used to get at the
3725 solution. */
3727 void
3728 df_note_add_problem (void)
3730 df_add_problem (&problem_NOTE);
3736 /*----------------------------------------------------------------------------
3737 Functions for simulating the effects of single insns.
3739 You can either simulate in the forwards direction, starting from
3740 the top of a block or the backwards direction from the end of the
3741 block. If you go backwards, defs are examined first to clear bits,
3742 then uses are examined to set bits. If you go forwards, defs are
3743 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3744 are examined to clear bits. In either case, the result of examining
3745 a def can be undone (respectively by a use or a REG_UNUSED note).
3747 If you start at the top of the block, use one of DF_LIVE_IN or
3748 DF_LR_IN. If you start at the bottom of the block use one of
3749 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3750 THEY WILL BE DESTROYED.
3751 ----------------------------------------------------------------------------*/
3754 /* Find the set of DEFs for INSN. */
3756 void
3757 df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3759 df_ref def;
3761 FOR_EACH_INSN_DEF (def, insn)
3762 bitmap_set_bit (defs, DF_REF_REGNO (def));
3765 /* Find the set of uses for INSN. This includes partial defs. */
3767 static void
3768 df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3770 df_ref def, use;
3771 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3773 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3774 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3775 bitmap_set_bit (uses, DF_REF_REGNO (def));
3776 FOR_EACH_INSN_INFO_USE (use, insn_info)
3777 bitmap_set_bit (uses, DF_REF_REGNO (use));
3780 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3782 void
3783 df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3785 df_ref def;
3787 FOR_EACH_INSN_DEF (def, insn)
3788 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3789 bitmap_set_bit (defs, DF_REF_REGNO (def));
3793 /* Simulate the effects of the defs of INSN on LIVE. */
3795 void
3796 df_simulate_defs (rtx_insn *insn, bitmap live)
3798 df_ref def;
3800 FOR_EACH_INSN_DEF (def, insn)
3802 unsigned int dregno = DF_REF_REGNO (def);
3804 /* If the def is to only part of the reg, it does
3805 not kill the other defs that reach here. */
3806 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3807 bitmap_clear_bit (live, dregno);
3812 /* Simulate the effects of the uses of INSN on LIVE. */
3814 void
3815 df_simulate_uses (rtx_insn *insn, bitmap live)
3817 df_ref use;
3819 if (DEBUG_INSN_P (insn))
3820 return;
3822 FOR_EACH_INSN_USE (use, insn)
3823 /* Add use to set of uses in this BB. */
3824 bitmap_set_bit (live, DF_REF_REGNO (use));
3828 /* Add back the always live regs in BB to LIVE. */
3830 static inline void
3831 df_simulate_fixup_sets (basic_block bb, bitmap live)
3833 /* These regs are considered always live so if they end up dying
3834 because of some def, we need to bring the back again. */
3835 if (bb_has_eh_pred (bb))
3836 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3837 else
3838 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3842 /*----------------------------------------------------------------------------
3843 The following three functions are used only for BACKWARDS scanning:
3844 i.e. they process the defs before the uses.
3846 df_simulate_initialize_backwards should be called first with a
3847 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
3848 df_simulate_one_insn_backwards should be called for each insn in
3849 the block, starting with the last one. Finally,
3850 df_simulate_finalize_backwards can be called to get a new value
3851 of the sets at the top of the block (this is rarely used).
3852 ----------------------------------------------------------------------------*/
3854 /* Apply the artificial uses and defs at the end of BB in a backwards
3855 direction. */
3857 void
3858 df_simulate_initialize_backwards (basic_block bb, bitmap live)
3860 df_ref def, use;
3861 int bb_index = bb->index;
3863 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3864 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3865 bitmap_clear_bit (live, DF_REF_REGNO (def));
3867 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3868 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3869 bitmap_set_bit (live, DF_REF_REGNO (use));
3873 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
3875 void
3876 df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3878 if (!NONDEBUG_INSN_P (insn))
3879 return;
3881 df_simulate_defs (insn, live);
3882 df_simulate_uses (insn, live);
3883 df_simulate_fixup_sets (bb, live);
3887 /* Apply the artificial uses and defs at the top of BB in a backwards
3888 direction. */
3890 void
3891 df_simulate_finalize_backwards (basic_block bb, bitmap live)
3893 df_ref def;
3894 #ifdef EH_USES
3895 df_ref use;
3896 #endif
3897 int bb_index = bb->index;
3899 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3900 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3901 bitmap_clear_bit (live, DF_REF_REGNO (def));
3903 #ifdef EH_USES
3904 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3905 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3906 bitmap_set_bit (live, DF_REF_REGNO (use));
3907 #endif
3909 /*----------------------------------------------------------------------------
3910 The following three functions are used only for FORWARDS scanning:
3911 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
3912 Thus it is important to add the DF_NOTES problem to the stack of
3913 problems computed before using these functions.
3915 df_simulate_initialize_forwards should be called first with a
3916 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
3917 df_simulate_one_insn_forwards should be called for each insn in
3918 the block, starting with the first one.
3919 ----------------------------------------------------------------------------*/
3921 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
3922 DF_LR_IN for basic block BB, for forward scanning by marking artificial
3923 defs live. */
3925 void
3926 df_simulate_initialize_forwards (basic_block bb, bitmap live)
3928 df_ref def;
3929 int bb_index = bb->index;
3931 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3932 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3933 bitmap_set_bit (live, DF_REF_REGNO (def));
3936 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
3938 void
3939 df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
3941 rtx link;
3942 if (! INSN_P (insn))
3943 return;
3945 /* Make sure that DF_NOTE really is an active df problem. */
3946 gcc_assert (df_note);
3948 /* Note that this is the opposite as how the problem is defined, because
3949 in the LR problem defs _kill_ liveness. However, they do so backwards,
3950 while here the scan is performed forwards! So, first assume that the
3951 def is live, and if this is not true REG_UNUSED notes will rectify the
3952 situation. */
3953 df_simulate_find_noclobber_defs (insn, live);
3955 /* Clear all of the registers that go dead. */
3956 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3958 switch (REG_NOTE_KIND (link))
3960 case REG_DEAD:
3961 case REG_UNUSED:
3963 rtx reg = XEXP (link, 0);
3964 bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
3966 break;
3967 default:
3968 break;
3971 df_simulate_fixup_sets (bb, live);
3974 /* Used by the next two functions to encode information about the
3975 memory references we found. */
3976 #define MEMREF_NORMAL 1
3977 #define MEMREF_VOLATILE 2
3979 /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X. */
3981 static int
3982 find_memory (rtx_insn *insn)
3984 int flags = 0;
3985 subrtx_iterator::array_type array;
3986 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3988 const_rtx x = *iter;
3989 if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
3990 flags |= MEMREF_VOLATILE;
3991 else if (MEM_P (x))
3993 if (MEM_VOLATILE_P (x))
3994 flags |= MEMREF_VOLATILE;
3995 else if (!MEM_READONLY_P (x))
3996 flags |= MEMREF_NORMAL;
3999 return flags;
4002 /* A subroutine of can_move_insns_across_p called through note_stores.
4003 DATA points to an integer in which we set either the bit for
4004 MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
4005 of either kind. */
4007 static void
4008 find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4009 void *data ATTRIBUTE_UNUSED)
4011 int *pflags = (int *)data;
4012 if (GET_CODE (x) == SUBREG)
4013 x = XEXP (x, 0);
4014 /* Treat stores to SP as stores to memory, this will prevent problems
4015 when there are references to the stack frame. */
4016 if (x == stack_pointer_rtx)
4017 *pflags |= MEMREF_VOLATILE;
4018 if (!MEM_P (x))
4019 return;
4020 *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4023 /* Scan BB backwards, using df_simulate functions to keep track of
4024 lifetimes, up to insn POINT. The result is stored in LIVE. */
4026 void
4027 simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4029 rtx_insn *insn;
4030 bitmap_copy (live, df_get_live_out (bb));
4031 df_simulate_initialize_backwards (bb, live);
4033 /* Scan and update life information until we reach the point we're
4034 interested in. */
4035 for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4036 df_simulate_one_insn_backwards (bb, insn, live);
4039 /* Return true if it is safe to move a group of insns, described by
4040 the range FROM to TO, backwards across another group of insns,
4041 described by ACROSS_FROM to ACROSS_TO. It is assumed that there
4042 are no insns between ACROSS_TO and FROM, but they may be in
4043 different basic blocks; MERGE_BB is the block from which the
4044 insns will be moved. The caller must pass in a regset MERGE_LIVE
4045 which specifies the registers live after TO.
4047 This function may be called in one of two cases: either we try to
4048 move identical instructions from all successor blocks into their
4049 predecessor, or we try to move from only one successor block. If
4050 OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4051 the second case. It should contain a set of registers live at the
4052 end of ACROSS_TO which must not be clobbered by moving the insns.
4053 In that case, we're also more careful about moving memory references
4054 and trapping insns.
4056 We return false if it is not safe to move the entire group, but it
4057 may still be possible to move a subgroup. PMOVE_UPTO, if nonnull,
4058 is set to point at the last moveable insn in such a case. */
4060 bool
4061 can_move_insns_across (rtx_insn *from, rtx_insn *to,
4062 rtx_insn *across_from, rtx_insn *across_to,
4063 basic_block merge_bb, regset merge_live,
4064 regset other_branch_live, rtx_insn **pmove_upto)
4066 rtx_insn *insn, *next, *max_to;
4067 bitmap merge_set, merge_use, local_merge_live;
4068 bitmap test_set, test_use;
4069 unsigned i, fail = 0;
4070 bitmap_iterator bi;
4071 int memrefs_in_across = 0;
4072 int mem_sets_in_across = 0;
4073 bool trapping_insns_in_across = false;
4075 if (pmove_upto != NULL)
4076 *pmove_upto = NULL;
4078 /* Find real bounds, ignoring debug insns. */
4079 while (!NONDEBUG_INSN_P (from) && from != to)
4080 from = NEXT_INSN (from);
4081 while (!NONDEBUG_INSN_P (to) && from != to)
4082 to = PREV_INSN (to);
4084 for (insn = across_to; ; insn = next)
4086 if (CALL_P (insn))
4088 if (RTL_CONST_OR_PURE_CALL_P (insn))
4089 /* Pure functions can read from memory. Const functions can
4090 read from arguments that the ABI has forced onto the stack.
4091 Neither sort of read can be volatile. */
4092 memrefs_in_across |= MEMREF_NORMAL;
4093 else
4095 memrefs_in_across |= MEMREF_VOLATILE;
4096 mem_sets_in_across |= MEMREF_VOLATILE;
4099 if (NONDEBUG_INSN_P (insn))
4101 if (volatile_insn_p (PATTERN (insn)))
4102 return false;
4103 memrefs_in_across |= find_memory (insn);
4104 note_stores (insn, find_memory_stores, &mem_sets_in_across);
4105 /* This is used just to find sets of the stack pointer. */
4106 memrefs_in_across |= mem_sets_in_across;
4107 trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4109 next = PREV_INSN (insn);
4110 if (insn == across_from)
4111 break;
4114 /* Collect:
4115 MERGE_SET = set of registers set in MERGE_BB
4116 MERGE_USE = set of registers used in MERGE_BB and live at its top
4117 MERGE_LIVE = set of registers live at the point inside the MERGE
4118 range that we've reached during scanning
4119 TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4120 TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4121 and live before ACROSS_FROM. */
4123 merge_set = BITMAP_ALLOC (&reg_obstack);
4124 merge_use = BITMAP_ALLOC (&reg_obstack);
4125 local_merge_live = BITMAP_ALLOC (&reg_obstack);
4126 test_set = BITMAP_ALLOC (&reg_obstack);
4127 test_use = BITMAP_ALLOC (&reg_obstack);
4129 /* Compute the set of registers set and used in the ACROSS range. */
4130 if (other_branch_live != NULL)
4131 bitmap_copy (test_use, other_branch_live);
4132 df_simulate_initialize_backwards (merge_bb, test_use);
4133 for (insn = across_to; ; insn = next)
4135 if (NONDEBUG_INSN_P (insn))
4137 df_simulate_find_defs (insn, test_set);
4138 df_simulate_defs (insn, test_use);
4139 df_simulate_uses (insn, test_use);
4141 next = PREV_INSN (insn);
4142 if (insn == across_from)
4143 break;
4146 /* Compute an upper bound for the amount of insns moved, by finding
4147 the first insn in MERGE that sets a register in TEST_USE, or uses
4148 a register in TEST_SET. We also check for calls, trapping operations,
4149 and memory references. */
4150 max_to = NULL;
4151 for (insn = from; ; insn = next)
4153 if (CALL_P (insn))
4154 break;
4155 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4156 break;
4157 if (NONDEBUG_INSN_P (insn))
4159 if (may_trap_or_fault_p (PATTERN (insn))
4160 && (trapping_insns_in_across
4161 || other_branch_live != NULL
4162 || volatile_insn_p (PATTERN (insn))))
4163 break;
4165 /* We cannot move memory stores past each other, or move memory
4166 reads past stores, at least not without tracking them and
4167 calling true_dependence on every pair.
4169 If there is no other branch and no memory references or
4170 sets in the ACROSS range, we can move memory references
4171 freely, even volatile ones.
4173 Otherwise, the rules are as follows: volatile memory
4174 references and stores can't be moved at all, and any type
4175 of memory reference can't be moved if there are volatile
4176 accesses or stores in the ACROSS range. That leaves
4177 normal reads, which can be moved, as the trapping case is
4178 dealt with elsewhere. */
4179 if (other_branch_live != NULL || memrefs_in_across != 0)
4181 int mem_ref_flags = 0;
4182 int mem_set_flags = 0;
4183 note_stores (insn, find_memory_stores, &mem_set_flags);
4184 mem_ref_flags = find_memory (insn);
4185 /* Catch sets of the stack pointer. */
4186 mem_ref_flags |= mem_set_flags;
4188 if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4189 break;
4190 if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4191 break;
4192 if (mem_set_flags != 0
4193 || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4194 break;
4196 df_simulate_find_uses (insn, merge_use);
4197 /* We're only interested in uses which use a value live at
4198 the top, not one previously set in this block. */
4199 bitmap_and_compl_into (merge_use, merge_set);
4200 df_simulate_find_defs (insn, merge_set);
4201 if (bitmap_intersect_p (merge_set, test_use)
4202 || bitmap_intersect_p (merge_use, test_set))
4203 break;
4204 if (!HAVE_cc0 || !sets_cc0_p (insn))
4205 max_to = insn;
4207 next = NEXT_INSN (insn);
4208 if (insn == to)
4209 break;
4211 if (max_to != to)
4212 fail = 1;
4214 if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4215 goto out;
4217 /* Now, lower this upper bound by also taking into account that
4218 a range of insns moved across ACROSS must not leave a register
4219 live at the end that will be clobbered in ACROSS. We need to
4220 find a point where TEST_SET & LIVE == 0.
4222 Insns in the MERGE range that set registers which are also set
4223 in the ACROSS range may still be moved as long as we also move
4224 later insns which use the results of the set, and make the
4225 register dead again. This is verified by the condition stated
4226 above. We only need to test it for registers that are set in
4227 the moved region.
4229 MERGE_LIVE is provided by the caller and holds live registers after
4230 TO. */
4231 bitmap_copy (local_merge_live, merge_live);
4232 for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4233 df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4235 /* We're not interested in registers that aren't set in the moved
4236 region at all. */
4237 bitmap_and_into (local_merge_live, merge_set);
4238 for (;;)
4240 if (NONDEBUG_INSN_P (insn))
4242 if (!bitmap_intersect_p (test_set, local_merge_live)
4243 && (!HAVE_cc0 || !sets_cc0_p (insn)))
4245 max_to = insn;
4246 break;
4249 df_simulate_one_insn_backwards (merge_bb, insn,
4250 local_merge_live);
4252 if (insn == from)
4254 fail = 1;
4255 goto out;
4257 insn = PREV_INSN (insn);
4260 if (max_to != to)
4261 fail = 1;
4263 if (pmove_upto)
4264 *pmove_upto = max_to;
4266 /* For small register class machines, don't lengthen lifetimes of
4267 hard registers before reload. */
4268 if (! reload_completed
4269 && targetm.small_register_classes_for_mode_p (VOIDmode))
4271 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4273 if (i < FIRST_PSEUDO_REGISTER
4274 && ! fixed_regs[i]
4275 && ! global_regs[i])
4277 fail = 1;
4278 break;
4283 out:
4284 BITMAP_FREE (merge_set);
4285 BITMAP_FREE (merge_use);
4286 BITMAP_FREE (local_merge_live);
4287 BITMAP_FREE (test_set);
4288 BITMAP_FREE (test_use);
4290 return !fail;
4294 /*----------------------------------------------------------------------------
4295 MULTIPLE DEFINITIONS
4297 Find the locations in the function reached by multiple definition sites
4298 for a live pseudo. In and out bitvectors are built for each basic
4299 block. They are restricted for efficiency to live registers.
4301 The gen and kill sets for the problem are obvious. Together they
4302 include all defined registers in a basic block; the gen set includes
4303 registers where a partial or conditional or may-clobber definition is
4304 last in the BB, while the kill set includes registers with a complete
4305 definition coming last. However, the computation of the dataflow
4306 itself is interesting.
4308 The idea behind it comes from SSA form's iterated dominance frontier
4309 criterion for inserting PHI functions. Just like in that case, we can use
4310 the dominance frontier to find places where multiple definitions meet;
4311 a register X defined in a basic block BB1 has multiple definitions in
4312 basic blocks in BB1's dominance frontier.
4314 So, the in-set of a basic block BB2 is not just the union of the
4315 out-sets of BB2's predecessors, but includes some more bits that come
4316 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4317 the previous paragraph). I called this set the init-set of BB2.
4319 (Note: I actually use the kill-set only to build the init-set.
4320 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4322 For example, if you have
4324 BB1 : r10 = 0
4325 r11 = 0
4326 if <...> goto BB2 else goto BB3;
4328 BB2 : r10 = 1
4329 r12 = 1
4330 goto BB3;
4332 BB3 :
4334 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4335 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4336 not need to iterate the dominance frontier, because we do not insert
4337 anything like PHI functions there! Instead, dataflow will take care of
4338 propagating the information to BB3's successors.
4339 ---------------------------------------------------------------------------*/
4341 /* Private data used to verify the solution for this problem. */
4342 struct df_md_problem_data
4344 /* An obstack for the bitmaps we need for this problem. */
4345 bitmap_obstack md_bitmaps;
4348 /* Scratch var used by transfer functions. This is used to do md analysis
4349 only for live registers. */
4350 static bitmap_head df_md_scratch;
4353 static void
4354 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4355 void *vbb_info)
4357 class df_md_bb_info *bb_info = (class df_md_bb_info *) vbb_info;
4358 if (bb_info)
4360 bitmap_clear (&bb_info->kill);
4361 bitmap_clear (&bb_info->gen);
4362 bitmap_clear (&bb_info->init);
4363 bitmap_clear (&bb_info->in);
4364 bitmap_clear (&bb_info->out);
4369 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4370 not touched unless the block is new. */
4372 static void
4373 df_md_alloc (bitmap all_blocks)
4375 unsigned int bb_index;
4376 bitmap_iterator bi;
4377 struct df_md_problem_data *problem_data;
4379 df_grow_bb_info (df_md);
4380 if (df_md->problem_data)
4381 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4382 else
4384 problem_data = XNEW (struct df_md_problem_data);
4385 df_md->problem_data = problem_data;
4386 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4388 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4390 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4392 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4393 /* When bitmaps are already initialized, just clear them. */
4394 if (bb_info->init.obstack)
4396 bitmap_clear (&bb_info->init);
4397 bitmap_clear (&bb_info->gen);
4398 bitmap_clear (&bb_info->kill);
4399 bitmap_clear (&bb_info->in);
4400 bitmap_clear (&bb_info->out);
4402 else
4404 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4405 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4406 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4407 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4408 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4412 df_md->optional_p = true;
4415 /* Add the effect of the top artificial defs of BB to the multiple definitions
4416 bitmap LOCAL_MD. */
4418 void
4419 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4421 int bb_index = bb->index;
4422 df_ref def;
4423 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4424 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4426 unsigned int dregno = DF_REF_REGNO (def);
4427 if (DF_REF_FLAGS (def)
4428 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4429 bitmap_set_bit (local_md, dregno);
4430 else
4431 bitmap_clear_bit (local_md, dregno);
4436 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4437 LOCAL_MD. */
4439 void
4440 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4441 bitmap local_md)
4443 df_ref def;
4445 FOR_EACH_INSN_DEF (def, insn)
4447 unsigned int dregno = DF_REF_REGNO (def);
4448 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4449 || (dregno >= FIRST_PSEUDO_REGISTER))
4451 if (DF_REF_FLAGS (def)
4452 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4453 bitmap_set_bit (local_md, DF_REF_ID (def));
4454 else
4455 bitmap_clear_bit (local_md, DF_REF_ID (def));
4460 static void
4461 df_md_bb_local_compute_process_def (class df_md_bb_info *bb_info,
4462 df_ref def,
4463 int top_flag)
4465 bitmap_clear (&seen_in_insn);
4467 for (; def; def = DF_REF_NEXT_LOC (def))
4469 unsigned int dregno = DF_REF_REGNO (def);
4470 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4471 || (dregno >= FIRST_PSEUDO_REGISTER))
4472 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4474 if (!bitmap_bit_p (&seen_in_insn, dregno))
4476 if (DF_REF_FLAGS (def)
4477 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4479 bitmap_set_bit (&bb_info->gen, dregno);
4480 bitmap_clear_bit (&bb_info->kill, dregno);
4482 else
4484 /* When we find a clobber and a regular def,
4485 make sure the regular def wins. */
4486 bitmap_set_bit (&seen_in_insn, dregno);
4487 bitmap_set_bit (&bb_info->kill, dregno);
4488 bitmap_clear_bit (&bb_info->gen, dregno);
4496 /* Compute local multiple def info for basic block BB. */
4498 static void
4499 df_md_bb_local_compute (unsigned int bb_index)
4501 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4502 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4503 rtx_insn *insn;
4505 /* Artificials are only hard regs. */
4506 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4507 df_md_bb_local_compute_process_def (bb_info,
4508 df_get_artificial_defs (bb_index),
4509 DF_REF_AT_TOP);
4511 FOR_BB_INSNS (bb, insn)
4513 unsigned int uid = INSN_UID (insn);
4514 if (!INSN_P (insn))
4515 continue;
4517 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4520 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4521 df_md_bb_local_compute_process_def (bb_info,
4522 df_get_artificial_defs (bb_index),
4526 /* Compute local reaching def info for each basic block within BLOCKS. */
4528 static void
4529 df_md_local_compute (bitmap all_blocks)
4531 unsigned int bb_index, df_bb_index;
4532 bitmap_iterator bi1, bi2;
4533 basic_block bb;
4534 bitmap_head *frontiers;
4536 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4538 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4540 df_md_bb_local_compute (bb_index);
4543 bitmap_release (&seen_in_insn);
4545 frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4546 FOR_ALL_BB_FN (bb, cfun)
4547 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4549 compute_dominance_frontiers (frontiers);
4551 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4552 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4554 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4555 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4557 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4558 if (bitmap_bit_p (all_blocks, df_bb_index))
4559 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4560 df_get_live_in (bb));
4564 FOR_ALL_BB_FN (bb, cfun)
4565 bitmap_clear (&frontiers[bb->index]);
4566 free (frontiers);
4570 /* Reset the global solution for recalculation. */
4572 static void
4573 df_md_reset (bitmap all_blocks)
4575 unsigned int bb_index;
4576 bitmap_iterator bi;
4578 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4580 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4581 gcc_assert (bb_info);
4582 bitmap_clear (&bb_info->in);
4583 bitmap_clear (&bb_info->out);
4587 static bool
4588 df_md_transfer_function (int bb_index)
4590 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4591 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4592 bitmap in = &bb_info->in;
4593 bitmap out = &bb_info->out;
4594 bitmap gen = &bb_info->gen;
4595 bitmap kill = &bb_info->kill;
4597 /* We need to use a scratch set here so that the value returned from this
4598 function invocation properly reflects whether the sets changed in a
4599 significant way; i.e. not just because the live set was anded in. */
4600 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4602 /* Multiple definitions of a register are not relevant if it is not
4603 live. Thus we trim the result to the places where it is live. */
4604 bitmap_and_into (in, df_get_live_in (bb));
4606 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4609 /* Initialize the solution bit vectors for problem. */
4611 static void
4612 df_md_init (bitmap all_blocks)
4614 unsigned int bb_index;
4615 bitmap_iterator bi;
4617 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4619 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4621 bitmap_copy (&bb_info->in, &bb_info->init);
4622 df_md_transfer_function (bb_index);
4626 static void
4627 df_md_confluence_0 (basic_block bb)
4629 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4630 bitmap_copy (&bb_info->in, &bb_info->init);
4633 /* In of target gets or of out of source. */
4635 static bool
4636 df_md_confluence_n (edge e)
4638 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4639 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4641 if (e->flags & EDGE_FAKE)
4642 return false;
4644 if (e->flags & EDGE_EH)
4646 /* Conservatively treat partially-clobbered registers as surviving
4647 across the edge; they might or might not, depending on what mode
4648 they have. */
4649 bitmap_view<HARD_REG_SET> eh_kills (eh_edge_abi.full_reg_clobbers ());
4650 return bitmap_ior_and_compl_into (op1, op2, eh_kills);
4652 else
4653 return bitmap_ior_into (op1, op2);
4656 /* Free all storage associated with the problem. */
4658 static void
4659 df_md_free (void)
4661 struct df_md_problem_data *problem_data
4662 = (struct df_md_problem_data *) df_md->problem_data;
4664 bitmap_release (&df_md_scratch);
4665 bitmap_obstack_release (&problem_data->md_bitmaps);
4666 free (problem_data);
4667 df_md->problem_data = NULL;
4669 df_md->block_info_size = 0;
4670 free (df_md->block_info);
4671 df_md->block_info = NULL;
4672 free (df_md);
4676 /* Debugging info at top of bb. */
4678 static void
4679 df_md_top_dump (basic_block bb, FILE *file)
4681 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4682 if (!bb_info)
4683 return;
4685 fprintf (file, ";; md in \t");
4686 df_print_regset (file, &bb_info->in);
4687 fprintf (file, ";; md init \t");
4688 df_print_regset (file, &bb_info->init);
4689 fprintf (file, ";; md gen \t");
4690 df_print_regset (file, &bb_info->gen);
4691 fprintf (file, ";; md kill \t");
4692 df_print_regset (file, &bb_info->kill);
4695 /* Debugging info at bottom of bb. */
4697 static void
4698 df_md_bottom_dump (basic_block bb, FILE *file)
4700 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4701 if (!bb_info)
4702 return;
4704 fprintf (file, ";; md out \t");
4705 df_print_regset (file, &bb_info->out);
4708 static const struct df_problem problem_MD =
4710 DF_MD, /* Problem id. */
4711 DF_FORWARD, /* Direction. */
4712 df_md_alloc, /* Allocate the problem specific data. */
4713 df_md_reset, /* Reset global information. */
4714 df_md_free_bb_info, /* Free basic block info. */
4715 df_md_local_compute, /* Local compute function. */
4716 df_md_init, /* Init the solution specific data. */
4717 df_worklist_dataflow, /* Worklist solver. */
4718 df_md_confluence_0, /* Confluence operator 0. */
4719 df_md_confluence_n, /* Confluence operator n. */
4720 df_md_transfer_function, /* Transfer function. */
4721 NULL, /* Finalize function. */
4722 df_md_free, /* Free all of the problem information. */
4723 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4724 NULL, /* Debugging. */
4725 df_md_top_dump, /* Debugging start block. */
4726 df_md_bottom_dump, /* Debugging end block. */
4727 NULL, /* Debugging start insn. */
4728 NULL, /* Debugging end insn. */
4729 NULL, /* Incremental solution verify start. */
4730 NULL, /* Incremental solution verify end. */
4731 NULL, /* Dependent problem. */
4732 sizeof (class df_md_bb_info),/* Size of entry of block_info array. */
4733 TV_DF_MD, /* Timing variable. */
4734 false /* Reset blocks on dropping out of blocks_to_analyze. */
4737 /* Create a new MD instance and add it to the existing instance
4738 of DF. */
4740 void
4741 df_md_add_problem (void)
4743 df_add_problem (&problem_MD);