vect: Fix single_imm_use in tree_vect_patterns
[official-gcc.git] / gcc / df-problems.cc
blobbfd24bd1e86c478a49bc4002dd97a73e61e7c4ac
1 /* Standard problems for dataflow support routines.
2 Copyright (C) 1999-2024 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)
846 /* If the definition is to only part of the register, it will
847 usually have a corresponding use. For example, stores to one
848 word of a multiword register R have both a use and a partial
849 definition of R.
851 In those cases, the LR confluence function:
853 IN = (OUT & ~DEF) | USE
855 is unaffected by whether we count the partial definition or not.
856 However, it's more convenient for consumers if DEF contains
857 *all* the registers defined in a block.
859 The only current case in which we record a partial definition
860 without a corresponding use is if the destination is the
861 multi-register subreg of a hard register. An artificial
862 example of this is:
864 (set (subreg:TI (reg:V8HI x0) 0) (const_int -1))
866 on AArch64. This is described as a DF_REF_PARTIAL
867 definition of x0 and x1 with no corresponding uses.
868 In previous versions of GCC, the instruction had no
869 effect on LR (that is, LR acted as though the instruction
870 didn't exist).
872 It seems suspect that this case is treated differently.
873 Either the instruction should be a full definition of x0 and x1,
874 or the definition should be treated in the same way as other
875 partial definitions, such as strict_lowparts or subregs that
876 satisfy read_modify_subreg_p.
878 Fortunately, multi-register subregs of hard registers should
879 be rare. They should be folded into a plain REG if the target
880 allows that (as AArch64 does for example above).
882 Here we treat the cases alike by forcing a use even in the rare
883 case that no DF_REF_REG_USE is recorded. That is, we model all
884 partial definitions as both a use and a definition of the
885 register. */
886 unsigned int dregno = DF_REF_REGNO (def);
887 bitmap_set_bit (&bb_info->def, dregno);
888 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
889 bitmap_set_bit (&bb_info->use, dregno);
890 else
891 bitmap_clear_bit (&bb_info->use, dregno);
894 FOR_EACH_INSN_INFO_USE (use, insn_info)
895 /* Add use to set of uses in this BB. */
896 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
899 /* Process the registers set in an exception handler or the hard
900 frame pointer if this block is the target of a non local
901 goto. */
902 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
903 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
905 unsigned int dregno = DF_REF_REGNO (def);
906 bitmap_set_bit (&bb_info->def, dregno);
907 bitmap_clear_bit (&bb_info->use, dregno);
910 #ifdef EH_USES
911 /* Process the uses that are live into an exception handler. */
912 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
913 /* Add use to set of uses in this BB. */
914 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
915 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
916 #endif
918 /* If the df_live problem is not defined, such as at -O0 and -O1, we
919 still need to keep the luids up to date. This is normally done
920 in the df_live problem since this problem has a forwards
921 scan. */
922 if (!df_live)
923 df_recompute_luids (bb);
927 /* Compute local live register info for each basic block within BLOCKS. */
929 static void
930 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
932 unsigned int bb_index, i;
933 bitmap_iterator bi;
935 bitmap_clear (&df->hardware_regs_used);
937 /* The all-important stack pointer must always be live. */
938 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
940 /* Global regs are always live, too. */
941 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
942 if (global_regs[i])
943 bitmap_set_bit (&df->hardware_regs_used, i);
945 /* Before reload, there are a few registers that must be forced
946 live everywhere -- which might not already be the case for
947 blocks within infinite loops. */
948 if (!reload_completed)
950 unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
951 /* Any reference to any pseudo before reload is a potential
952 reference of the frame pointer. */
953 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
955 /* Pseudos with argument area equivalences may require
956 reloading via the argument pointer. */
957 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
958 && fixed_regs[ARG_POINTER_REGNUM])
959 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
961 /* Any constant, or pseudo with constant equivalences, may
962 require reloading from memory using the pic register. */
963 if (pic_offset_table_regnum != INVALID_REGNUM
964 && fixed_regs[pic_offset_table_regnum])
965 bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
968 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
970 if (bb_index == EXIT_BLOCK)
972 /* The exit block is special for this problem and its bits are
973 computed from thin air. */
974 class df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
975 bitmap_copy (&bb_info->use, df->exit_block_uses);
977 else
978 df_lr_bb_local_compute (bb_index);
981 bitmap_clear (df_lr->out_of_date_transfer_functions);
985 /* Initialize the solution vectors. */
987 static void
988 df_lr_init (bitmap all_blocks)
990 unsigned int bb_index;
991 bitmap_iterator bi;
993 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
995 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
996 bitmap_copy (&bb_info->in, &bb_info->use);
997 bitmap_clear (&bb_info->out);
1002 /* Confluence function that processes infinite loops. This might be a
1003 noreturn function that throws. And even if it isn't, getting the
1004 unwind info right helps debugging. */
1005 static void
1006 df_lr_confluence_0 (basic_block bb)
1008 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
1009 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1010 bitmap_copy (op1, &df->hardware_regs_used);
1014 /* Confluence function that ignores fake edges. */
1016 static bool
1017 df_lr_confluence_n (edge e)
1019 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
1020 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
1021 bool changed = false;
1023 /* Call-clobbered registers die across exception and call edges.
1024 Conservatively treat partially-clobbered registers as surviving
1025 across the edges; they might or might not, depending on what
1026 mode they have. */
1027 /* ??? Abnormal call edges ignored for the moment, as this gets
1028 confused by sibling call edges, which crashes reg-stack. */
1029 if (e->flags & EDGE_EH)
1031 bitmap_view<HARD_REG_SET> eh_kills (eh_edge_abi.full_reg_clobbers ());
1032 changed = bitmap_ior_and_compl_into (op1, op2, eh_kills);
1034 else
1035 changed = bitmap_ior_into (op1, op2);
1037 changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
1038 return changed;
1042 /* Transfer function. */
1044 static bool
1045 df_lr_transfer_function (int bb_index)
1047 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1048 bitmap in = &bb_info->in;
1049 bitmap out = &bb_info->out;
1050 bitmap use = &bb_info->use;
1051 bitmap def = &bb_info->def;
1053 return bitmap_ior_and_compl (in, use, out, def);
1057 static void
1058 df_lr_finalize (bitmap)
1060 df_lr->solutions_dirty = false;
1064 /* Free all storage associated with the problem. */
1066 static void
1067 df_lr_free (void)
1069 struct df_lr_problem_data *problem_data
1070 = (struct df_lr_problem_data *) df_lr->problem_data;
1071 if (df_lr->block_info)
1074 df_lr->block_info_size = 0;
1075 free (df_lr->block_info);
1076 df_lr->block_info = NULL;
1077 bitmap_obstack_release (&problem_data->lr_bitmaps);
1078 free (df_lr->problem_data);
1079 df_lr->problem_data = NULL;
1082 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1083 free (df_lr);
1087 /* Debugging info at top of bb. */
1089 static void
1090 df_lr_top_dump (basic_block bb, FILE *file)
1092 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1093 struct df_lr_problem_data *problem_data;
1094 if (!bb_info)
1095 return;
1097 fprintf (file, ";; lr in \t");
1098 df_print_regset (file, &bb_info->in);
1099 if (df_lr->problem_data)
1101 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1102 if (problem_data->in)
1104 fprintf (file, ";; old in \t");
1105 df_print_regset (file, &problem_data->in[bb->index]);
1108 fprintf (file, ";; lr use \t");
1109 df_print_regset (file, &bb_info->use);
1110 fprintf (file, ";; lr def \t");
1111 df_print_regset (file, &bb_info->def);
1115 /* Debugging info at bottom of bb. */
1117 static void
1118 df_lr_bottom_dump (basic_block bb, FILE *file)
1120 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1121 struct df_lr_problem_data *problem_data;
1122 if (!bb_info)
1123 return;
1125 fprintf (file, ";; lr out \t");
1126 df_print_regset (file, &bb_info->out);
1127 if (df_lr->problem_data)
1129 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1130 if (problem_data->out)
1132 fprintf (file, ";; old out \t");
1133 df_print_regset (file, &problem_data->out[bb->index]);
1139 /* Build the datastructure to verify that the solution to the dataflow
1140 equations is not dirty. */
1142 static void
1143 df_lr_verify_solution_start (void)
1145 basic_block bb;
1146 struct df_lr_problem_data *problem_data;
1147 if (df_lr->solutions_dirty)
1148 return;
1150 /* Set it true so that the solution is recomputed. */
1151 df_lr->solutions_dirty = true;
1153 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1154 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1155 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1157 FOR_ALL_BB_FN (bb, cfun)
1159 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1160 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1161 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1162 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1167 /* Compare the saved datastructure and the new solution to the dataflow
1168 equations. */
1170 static void
1171 df_lr_verify_solution_end (void)
1173 struct df_lr_problem_data *problem_data;
1174 basic_block bb;
1176 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1178 if (!problem_data->out)
1179 return;
1181 if (df_lr->solutions_dirty)
1182 /* Do not check if the solution is still dirty. See the comment
1183 in df_lr_finalize for details. */
1184 df_lr->solutions_dirty = false;
1185 else
1186 FOR_ALL_BB_FN (bb, cfun)
1188 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1189 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1191 /*df_dump (stderr);*/
1192 gcc_unreachable ();
1196 /* Cannot delete them immediately because you may want to dump them
1197 if the comparison fails. */
1198 FOR_ALL_BB_FN (bb, cfun)
1200 bitmap_clear (&problem_data->in[bb->index]);
1201 bitmap_clear (&problem_data->out[bb->index]);
1204 free (problem_data->in);
1205 free (problem_data->out);
1206 problem_data->in = NULL;
1207 problem_data->out = NULL;
1211 /* All of the information associated with every instance of the problem. */
1213 static const struct df_problem problem_LR =
1215 DF_LR, /* Problem id. */
1216 DF_BACKWARD, /* Direction. */
1217 df_lr_alloc, /* Allocate the problem specific data. */
1218 df_lr_reset, /* Reset global information. */
1219 df_lr_free_bb_info, /* Free basic block info. */
1220 df_lr_local_compute, /* Local compute function. */
1221 df_lr_init, /* Init the solution specific data. */
1222 df_worklist_dataflow, /* Worklist solver. */
1223 df_lr_confluence_0, /* Confluence operator 0. */
1224 df_lr_confluence_n, /* Confluence operator n. */
1225 df_lr_transfer_function, /* Transfer function. */
1226 df_lr_finalize, /* Finalize function. */
1227 df_lr_free, /* Free all of the problem information. */
1228 NULL, /* Remove this problem from the stack of dataflow problems. */
1229 NULL, /* Debugging. */
1230 df_lr_top_dump, /* Debugging start block. */
1231 df_lr_bottom_dump, /* Debugging end block. */
1232 NULL, /* Debugging start insn. */
1233 NULL, /* Debugging end insn. */
1234 df_lr_verify_solution_start,/* Incremental solution verify start. */
1235 df_lr_verify_solution_end, /* Incremental solution verify end. */
1236 NULL, /* Dependent problem. */
1237 sizeof (class df_lr_bb_info),/* Size of entry of block_info array. */
1238 TV_DF_LR, /* Timing variable. */
1239 false /* Reset blocks on dropping out of blocks_to_analyze. */
1242 /* Run the fast DCE after building LR. This is a separate problem so that
1243 the "dirty" flag is only cleared after a DCE pass is actually run. */
1245 static void
1246 df_lr_dce_finalize (bitmap all_blocks)
1248 if (!(df->changeable_flags & DF_LR_RUN_DCE))
1249 return;
1251 /* Also clears df_lr_dce->solutions_dirty. */
1252 run_fast_df_dce ();
1254 /* If dce deletes some instructions, we need to recompute the lr
1255 solution before proceeding further. The problem is that fast
1256 dce is a pessimestic dataflow algorithm. In the case where
1257 it deletes a statement S inside of a loop, the uses inside of
1258 S may not be deleted from the dataflow solution because they
1259 were carried around the loop. While it is conservatively
1260 correct to leave these extra bits, the standards of df
1261 require that we maintain the best possible (least fixed
1262 point) solution. The only way to do that is to redo the
1263 iteration from the beginning. See PR35805 for an
1264 example. */
1265 if (df_lr->solutions_dirty)
1267 df_clear_flags (DF_LR_RUN_DCE);
1268 df_lr_alloc (all_blocks);
1269 df_lr_local_compute (all_blocks);
1270 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1271 df_lr_finalize (all_blocks);
1272 df_set_flags (DF_LR_RUN_DCE);
1276 static const struct df_problem problem_LR_DCE
1278 DF_LR_DCE, /* Problem id. */
1279 DF_BACKWARD, /* Direction (arbitrary). */
1280 NULL, /* Allocate the problem specific data. */
1281 NULL, /* Reset global information. */
1282 NULL, /* Free basic block info. */
1283 NULL, /* Local compute function. */
1284 NULL, /* Init the solution specific data. */
1285 NULL, /* Worklist solver. */
1286 NULL, /* Confluence operator 0. */
1287 NULL, /* Confluence operator n. */
1288 NULL, /* Transfer function. */
1289 df_lr_dce_finalize, /* Finalize function. */
1290 NULL, /* Free all of the problem information. */
1291 NULL, /* Remove this problem from the stack of dataflow problems. */
1292 NULL, /* Debugging. */
1293 NULL, /* Debugging start block. */
1294 NULL, /* Debugging end block. */
1295 NULL, /* Debugging start insn. */
1296 NULL, /* Debugging end insn. */
1297 NULL, /* Incremental solution verify start. */
1298 NULL, /* Incremental solution verify end. */
1299 &problem_LR, /* Dependent problem. */
1300 0, /* Size of entry of block_info array. */
1301 TV_DF_LR, /* Timing variable. */
1302 false /* Reset blocks on dropping out of blocks_to_analyze. */
1306 /* Create a new DATAFLOW instance and add it to an existing instance
1307 of DF. The returned structure is what is used to get at the
1308 solution. */
1310 void
1311 df_lr_add_problem (void)
1313 /* Also add the fast DCE problem. It is then conditionally enabled by
1314 the DF_LR_RUN_DCE flag. */
1315 df_add_problem (&problem_LR_DCE);
1316 /* These will be initialized when df_scan_blocks processes each
1317 block. */
1318 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1322 /* Verify that all of the lr related info is consistent and
1323 correct. */
1325 void
1326 df_lr_verify_transfer_functions (void)
1328 basic_block bb;
1329 bitmap_head saved_def;
1330 bitmap_head saved_use;
1331 bitmap_head all_blocks;
1333 if (!df)
1334 return;
1336 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1337 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1338 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1340 FOR_ALL_BB_FN (bb, cfun)
1342 class df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1343 bitmap_set_bit (&all_blocks, bb->index);
1345 if (bb_info)
1347 /* Make a copy of the transfer functions and then compute
1348 new ones to see if the transfer functions have
1349 changed. */
1350 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1351 bb->index))
1353 bitmap_copy (&saved_def, &bb_info->def);
1354 bitmap_copy (&saved_use, &bb_info->use);
1355 bitmap_clear (&bb_info->def);
1356 bitmap_clear (&bb_info->use);
1358 df_lr_bb_local_compute (bb->index);
1359 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1360 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1363 else
1365 /* If we do not have basic block info, the block must be in
1366 the list of dirty blocks or else some one has added a
1367 block behind our backs. */
1368 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1369 bb->index));
1371 /* Make sure no one created a block without following
1372 procedures. */
1373 gcc_assert (df_scan_get_bb_info (bb->index));
1376 /* Make sure there are no dirty bits in blocks that have been deleted. */
1377 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1378 &all_blocks));
1380 bitmap_clear (&saved_def);
1381 bitmap_clear (&saved_use);
1382 bitmap_clear (&all_blocks);
1387 /*----------------------------------------------------------------------------
1388 LIVE AND MAY-INITIALIZED REGISTERS.
1390 This problem first computes the IN and OUT bitvectors for the
1391 may-initialized registers problems, which is a forward problem.
1392 It gives the set of registers for which we MAY have an available
1393 definition, i.e. for which there is an available definition on
1394 at least one path from the entry block to the entry/exit of a
1395 basic block. Sets generate a definition, while clobbers kill
1396 a definition.
1398 In and out bitvectors are built for each basic block and are indexed by
1399 regnum (see df.h for details). In and out bitvectors in struct
1400 df_live_bb_info actually refers to the may-initialized problem;
1402 Then, the in and out sets for the LIVE problem itself are computed.
1403 These are the logical AND of the IN and OUT sets from the LR problem
1404 and the may-initialized problem.
1405 ----------------------------------------------------------------------------*/
1407 /* Private data used to verify the solution for this problem. */
1408 struct df_live_problem_data
1410 bitmap_head *in;
1411 bitmap_head *out;
1412 /* An obstack for the bitmaps we need for this problem. */
1413 bitmap_obstack live_bitmaps;
1416 /* Scratch var used by transfer functions. This is used to implement
1417 an optimization to reduce the amount of space used to compute the
1418 combined lr and live analysis. */
1419 static bitmap_head df_live_scratch;
1422 /* Free basic block info. */
1424 static void
1425 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1426 void *vbb_info)
1428 class df_live_bb_info *bb_info = (class df_live_bb_info *) vbb_info;
1429 if (bb_info)
1431 bitmap_clear (&bb_info->gen);
1432 bitmap_clear (&bb_info->kill);
1433 bitmap_clear (&bb_info->in);
1434 bitmap_clear (&bb_info->out);
1439 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1440 not touched unless the block is new. */
1442 static void
1443 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1445 unsigned int bb_index;
1446 bitmap_iterator bi;
1447 struct df_live_problem_data *problem_data;
1449 if (df_live->problem_data)
1450 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1451 else
1453 problem_data = XNEW (struct df_live_problem_data);
1454 df_live->problem_data = problem_data;
1456 problem_data->out = NULL;
1457 problem_data->in = NULL;
1458 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1459 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1462 df_grow_bb_info (df_live);
1464 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1466 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1468 /* When bitmaps are already initialized, just clear them. */
1469 if (bb_info->kill.obstack)
1471 bitmap_clear (&bb_info->kill);
1472 bitmap_clear (&bb_info->gen);
1474 else
1476 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1477 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1478 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1479 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1482 df_live->optional_p = (optimize <= 1);
1486 /* Reset the global solution for recalculation. */
1488 static void
1489 df_live_reset (bitmap all_blocks)
1491 unsigned int bb_index;
1492 bitmap_iterator bi;
1494 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1496 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1497 gcc_assert (bb_info);
1498 bitmap_clear (&bb_info->in);
1499 bitmap_clear (&bb_info->out);
1504 /* Compute local uninitialized register info for basic block BB. */
1506 static void
1507 df_live_bb_local_compute (unsigned int bb_index)
1509 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1510 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1511 rtx_insn *insn;
1512 df_ref def;
1513 int luid = 0;
1515 FOR_BB_INSNS (bb, insn)
1517 unsigned int uid = INSN_UID (insn);
1518 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1520 /* Inserting labels does not always trigger the incremental
1521 rescanning. */
1522 if (!insn_info)
1524 gcc_assert (!INSN_P (insn));
1525 insn_info = df_insn_create_insn_record (insn);
1528 DF_INSN_INFO_LUID (insn_info) = luid;
1529 if (!INSN_P (insn))
1530 continue;
1532 luid++;
1533 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1535 unsigned int regno = DF_REF_REGNO (def);
1537 if (DF_REF_FLAGS_IS_SET (def,
1538 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1539 /* All partial or conditional def
1540 seen are included in the gen set. */
1541 bitmap_set_bit (&bb_info->gen, regno);
1542 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1543 /* Only must clobbers for the entire reg destroy the
1544 value. */
1545 bitmap_set_bit (&bb_info->kill, regno);
1546 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1547 bitmap_set_bit (&bb_info->gen, regno);
1551 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1552 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1556 /* Compute local uninitialized register info. */
1558 static void
1559 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1561 unsigned int bb_index;
1562 bitmap_iterator bi;
1564 df_grow_insn_info ();
1566 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1567 0, bb_index, bi)
1569 df_live_bb_local_compute (bb_index);
1572 bitmap_clear (df_live->out_of_date_transfer_functions);
1576 /* Initialize the solution vectors. */
1578 static void
1579 df_live_init (bitmap all_blocks)
1581 unsigned int bb_index;
1582 bitmap_iterator bi;
1584 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1586 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1587 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1589 /* No register may reach a location where it is not used. Thus
1590 we trim the rr result to the places where it is used. */
1591 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1592 bitmap_clear (&bb_info->in);
1596 /* Forward confluence function that ignores fake edges. */
1598 static bool
1599 df_live_confluence_n (edge e)
1601 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1602 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1604 if (e->flags & EDGE_FAKE)
1605 return false;
1607 return bitmap_ior_into (op1, op2);
1611 /* Transfer function for the forwards may-initialized problem. */
1613 static bool
1614 df_live_transfer_function (int bb_index)
1616 class df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1617 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1618 bitmap in = &bb_info->in;
1619 bitmap out = &bb_info->out;
1620 bitmap gen = &bb_info->gen;
1621 bitmap kill = &bb_info->kill;
1623 /* We need to use a scratch set here so that the value returned from this
1624 function invocation properly reflects whether the sets changed in a
1625 significant way; i.e. not just because the lr set was anded in. */
1626 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1627 /* No register may reach a location where it is not used. Thus
1628 we trim the rr result to the places where it is used. */
1629 bitmap_and_into (in, &bb_lr_info->in);
1631 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1635 /* And the LR info with the may-initialized registers to produce the LIVE info. */
1637 static void
1638 df_live_finalize (bitmap all_blocks)
1641 if (df_live->solutions_dirty)
1643 bitmap_iterator bi;
1644 unsigned int bb_index;
1646 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1648 class df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1649 class df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1651 /* No register may reach a location where it is not used. Thus
1652 we trim the rr result to the places where it is used. */
1653 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1654 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1657 df_live->solutions_dirty = false;
1662 /* Free all storage associated with the problem. */
1664 static void
1665 df_live_free (void)
1667 struct df_live_problem_data *problem_data
1668 = (struct df_live_problem_data *) df_live->problem_data;
1669 if (df_live->block_info)
1671 df_live->block_info_size = 0;
1672 free (df_live->block_info);
1673 df_live->block_info = NULL;
1674 bitmap_release (&df_live_scratch);
1675 bitmap_obstack_release (&problem_data->live_bitmaps);
1676 free (problem_data);
1677 df_live->problem_data = NULL;
1679 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1680 free (df_live);
1684 /* Debugging info at top of bb. */
1686 static void
1687 df_live_top_dump (basic_block bb, FILE *file)
1689 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1690 struct df_live_problem_data *problem_data;
1692 if (!bb_info)
1693 return;
1695 fprintf (file, ";; live in \t");
1696 df_print_regset (file, &bb_info->in);
1697 if (df_live->problem_data)
1699 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1700 if (problem_data->in)
1702 fprintf (file, ";; old in \t");
1703 df_print_regset (file, &problem_data->in[bb->index]);
1706 fprintf (file, ";; live gen \t");
1707 df_print_regset (file, &bb_info->gen);
1708 fprintf (file, ";; live kill\t");
1709 df_print_regset (file, &bb_info->kill);
1713 /* Debugging info at bottom of bb. */
1715 static void
1716 df_live_bottom_dump (basic_block bb, FILE *file)
1718 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1719 struct df_live_problem_data *problem_data;
1721 if (!bb_info)
1722 return;
1724 fprintf (file, ";; live out \t");
1725 df_print_regset (file, &bb_info->out);
1726 if (df_live->problem_data)
1728 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1729 if (problem_data->out)
1731 fprintf (file, ";; old out \t");
1732 df_print_regset (file, &problem_data->out[bb->index]);
1738 /* Build the datastructure to verify that the solution to the dataflow
1739 equations is not dirty. */
1741 static void
1742 df_live_verify_solution_start (void)
1744 basic_block bb;
1745 struct df_live_problem_data *problem_data;
1746 if (df_live->solutions_dirty)
1747 return;
1749 /* Set it true so that the solution is recomputed. */
1750 df_live->solutions_dirty = true;
1752 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1753 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1754 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1756 FOR_ALL_BB_FN (bb, cfun)
1758 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1759 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1760 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1761 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1766 /* Compare the saved datastructure and the new solution to the dataflow
1767 equations. */
1769 static void
1770 df_live_verify_solution_end (void)
1772 struct df_live_problem_data *problem_data;
1773 basic_block bb;
1775 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1776 if (!problem_data->out)
1777 return;
1779 FOR_ALL_BB_FN (bb, cfun)
1781 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1782 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1784 /*df_dump (stderr);*/
1785 gcc_unreachable ();
1789 /* Cannot delete them immediately because you may want to dump them
1790 if the comparison fails. */
1791 FOR_ALL_BB_FN (bb, cfun)
1793 bitmap_clear (&problem_data->in[bb->index]);
1794 bitmap_clear (&problem_data->out[bb->index]);
1797 free (problem_data->in);
1798 free (problem_data->out);
1799 free (problem_data);
1800 df_live->problem_data = NULL;
1804 /* All of the information associated with every instance of the problem. */
1806 static const struct df_problem problem_LIVE =
1808 DF_LIVE, /* Problem id. */
1809 DF_FORWARD, /* Direction. */
1810 df_live_alloc, /* Allocate the problem specific data. */
1811 df_live_reset, /* Reset global information. */
1812 df_live_free_bb_info, /* Free basic block info. */
1813 df_live_local_compute, /* Local compute function. */
1814 df_live_init, /* Init the solution specific data. */
1815 df_worklist_dataflow, /* Worklist solver. */
1816 NULL, /* Confluence operator 0. */
1817 df_live_confluence_n, /* Confluence operator n. */
1818 df_live_transfer_function, /* Transfer function. */
1819 df_live_finalize, /* Finalize function. */
1820 df_live_free, /* Free all of the problem information. */
1821 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1822 NULL, /* Debugging. */
1823 df_live_top_dump, /* Debugging start block. */
1824 df_live_bottom_dump, /* Debugging end block. */
1825 NULL, /* Debugging start insn. */
1826 NULL, /* Debugging end insn. */
1827 df_live_verify_solution_start,/* Incremental solution verify start. */
1828 df_live_verify_solution_end, /* Incremental solution verify end. */
1829 &problem_LR, /* Dependent problem. */
1830 sizeof (class df_live_bb_info),/* Size of entry of block_info array. */
1831 TV_DF_LIVE, /* Timing variable. */
1832 false /* Reset blocks on dropping out of blocks_to_analyze. */
1836 /* Create a new DATAFLOW instance and add it to an existing instance
1837 of DF. The returned structure is what is used to get at the
1838 solution. */
1840 void
1841 df_live_add_problem (void)
1843 df_add_problem (&problem_LIVE);
1844 /* These will be initialized when df_scan_blocks processes each
1845 block. */
1846 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1850 /* Set all of the blocks as dirty. This needs to be done if this
1851 problem is added after all of the insns have been scanned. */
1853 void
1854 df_live_set_all_dirty (void)
1856 basic_block bb;
1857 FOR_ALL_BB_FN (bb, cfun)
1858 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1859 bb->index);
1863 /* Verify that all of the lr related info is consistent and
1864 correct. */
1866 void
1867 df_live_verify_transfer_functions (void)
1869 basic_block bb;
1870 bitmap_head saved_gen;
1871 bitmap_head saved_kill;
1872 bitmap_head all_blocks;
1874 if (!df)
1875 return;
1877 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1878 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1879 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1881 df_grow_insn_info ();
1883 FOR_ALL_BB_FN (bb, cfun)
1885 class df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1886 bitmap_set_bit (&all_blocks, bb->index);
1888 if (bb_info)
1890 /* Make a copy of the transfer functions and then compute
1891 new ones to see if the transfer functions have
1892 changed. */
1893 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1894 bb->index))
1896 bitmap_copy (&saved_gen, &bb_info->gen);
1897 bitmap_copy (&saved_kill, &bb_info->kill);
1898 bitmap_clear (&bb_info->gen);
1899 bitmap_clear (&bb_info->kill);
1901 df_live_bb_local_compute (bb->index);
1902 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1903 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1906 else
1908 /* If we do not have basic block info, the block must be in
1909 the list of dirty blocks or else some one has added a
1910 block behind our backs. */
1911 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1912 bb->index));
1914 /* Make sure no one created a block without following
1915 procedures. */
1916 gcc_assert (df_scan_get_bb_info (bb->index));
1919 /* Make sure there are no dirty bits in blocks that have been deleted. */
1920 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1921 &all_blocks));
1922 bitmap_clear (&saved_gen);
1923 bitmap_clear (&saved_kill);
1924 bitmap_clear (&all_blocks);
1927 /*----------------------------------------------------------------------------
1928 MUST-INITIALIZED REGISTERS.
1929 ----------------------------------------------------------------------------*/
1931 /* Private data used to verify the solution for this problem. */
1932 struct df_mir_problem_data
1934 bitmap_head *in;
1935 bitmap_head *out;
1936 /* An obstack for the bitmaps we need for this problem. */
1937 bitmap_obstack mir_bitmaps;
1941 /* Free basic block info. */
1943 static void
1944 df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1945 void *vbb_info)
1947 class df_mir_bb_info *bb_info = (class df_mir_bb_info *) vbb_info;
1948 if (bb_info)
1950 bitmap_clear (&bb_info->gen);
1951 bitmap_clear (&bb_info->kill);
1952 bitmap_clear (&bb_info->in);
1953 bitmap_clear (&bb_info->out);
1958 /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1959 not touched unless the block is new. */
1961 static void
1962 df_mir_alloc (bitmap all_blocks)
1964 unsigned int bb_index;
1965 bitmap_iterator bi;
1966 struct df_mir_problem_data *problem_data;
1968 if (df_mir->problem_data)
1969 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1970 else
1972 problem_data = XNEW (struct df_mir_problem_data);
1973 df_mir->problem_data = problem_data;
1975 problem_data->out = NULL;
1976 problem_data->in = NULL;
1977 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1980 df_grow_bb_info (df_mir);
1982 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1984 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1986 /* When bitmaps are already initialized, just clear them. */
1987 if (bb_info->kill.obstack)
1989 bitmap_clear (&bb_info->kill);
1990 bitmap_clear (&bb_info->gen);
1992 else
1994 bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1995 bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1996 bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1997 bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1998 bb_info->con_visited = false;
2002 df_mir->optional_p = 1;
2006 /* Reset the global solution for recalculation. */
2008 static void
2009 df_mir_reset (bitmap all_blocks)
2011 unsigned int bb_index;
2012 bitmap_iterator bi;
2014 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2016 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2018 gcc_assert (bb_info);
2020 bitmap_clear (&bb_info->in);
2021 bitmap_clear (&bb_info->out);
2022 bb_info->con_visited = false;
2027 /* Compute local uninitialized register info for basic block BB. */
2029 static void
2030 df_mir_bb_local_compute (unsigned int bb_index)
2032 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2033 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2034 rtx_insn *insn;
2035 int luid = 0;
2037 /* Ignoring artificial defs is intentional: these often pretend that some
2038 registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
2039 though they are not used for that. As a result, conservatively assume
2040 they may be uninitialized. */
2042 FOR_BB_INSNS (bb, insn)
2044 unsigned int uid = INSN_UID (insn);
2045 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
2047 /* Inserting labels does not always trigger the incremental
2048 rescanning. */
2049 if (!insn_info)
2051 gcc_assert (!INSN_P (insn));
2052 insn_info = df_insn_create_insn_record (insn);
2055 DF_INSN_INFO_LUID (insn_info) = luid;
2056 if (!INSN_P (insn))
2057 continue;
2059 luid++;
2060 df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
2065 /* Compute local uninitialized register info. */
2067 static void
2068 df_mir_local_compute (bitmap all_blocks)
2070 unsigned int bb_index;
2071 bitmap_iterator bi;
2073 df_grow_insn_info ();
2075 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2077 df_mir_bb_local_compute (bb_index);
2082 /* Initialize the solution vectors. */
2084 static void
2085 df_mir_init (bitmap all_blocks)
2087 df_mir_reset (all_blocks);
2091 /* Initialize IN sets for blocks with no predecessors: when landing on such
2092 blocks, assume all registers are uninitialized. */
2094 static void
2095 df_mir_confluence_0 (basic_block bb)
2097 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2099 bitmap_clear (&bb_info->in);
2100 bb_info->con_visited = true;
2104 /* Forward confluence function that ignores fake edges. */
2106 static bool
2107 df_mir_confluence_n (edge e)
2109 if (e->flags & EDGE_FAKE)
2110 return false;
2112 df_mir_bb_info *src_info = df_mir_get_bb_info (e->src->index);
2113 /* If SRC was not visited yet then we'll and with all-ones which
2114 means no changes. Do not consider DST con_visited by this
2115 operation alone either. */
2116 if (!src_info->con_visited)
2117 return false;
2119 df_mir_bb_info *dst_info = df_mir_get_bb_info (e->dest->index);
2120 bitmap op1 = &dst_info->in;
2121 bitmap op2 = &src_info->out;
2122 /* If DEST was not visited yet just copy the SRC bitmap. */
2123 if (!dst_info->con_visited)
2125 dst_info->con_visited = true;
2126 bitmap_copy (op1, op2);
2127 return true;
2130 /* A register is must-initialized at the entry of a basic block iff it is
2131 must-initialized at the exit of all the predecessors. */
2132 return bitmap_and_into (op1, op2);
2136 /* Transfer function for the forwards must-initialized problem. */
2138 static bool
2139 df_mir_transfer_function (int bb_index)
2141 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2142 bitmap in = &bb_info->in;
2143 bitmap out = &bb_info->out;
2144 bitmap gen = &bb_info->gen;
2145 bitmap kill = &bb_info->kill;
2147 return bitmap_ior_and_compl (out, gen, in, kill);
2151 /* Free all storage associated with the problem. */
2153 static void
2154 df_mir_free (void)
2156 struct df_mir_problem_data *problem_data
2157 = (struct df_mir_problem_data *) df_mir->problem_data;
2158 if (df_mir->block_info)
2160 df_mir->block_info_size = 0;
2161 free (df_mir->block_info);
2162 df_mir->block_info = NULL;
2163 bitmap_obstack_release (&problem_data->mir_bitmaps);
2164 free (problem_data);
2165 df_mir->problem_data = NULL;
2167 free (df_mir);
2171 /* Debugging info at top of bb. */
2173 static void
2174 df_mir_top_dump (basic_block bb, FILE *file)
2176 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2178 if (!bb_info)
2179 return;
2181 fprintf (file, ";; mir in \t");
2182 df_print_regset (file, &bb_info->in);
2183 fprintf (file, ";; mir kill\t");
2184 df_print_regset (file, &bb_info->kill);
2185 fprintf (file, ";; mir gen \t");
2186 df_print_regset (file, &bb_info->gen);
2189 /* Debugging info at bottom of bb. */
2191 static void
2192 df_mir_bottom_dump (basic_block bb, FILE *file)
2194 class df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2196 if (!bb_info)
2197 return;
2199 fprintf (file, ";; mir out \t");
2200 df_print_regset (file, &bb_info->out);
2204 /* Build the datastructure to verify that the solution to the dataflow
2205 equations is not dirty. */
2207 static void
2208 df_mir_verify_solution_start (void)
2210 basic_block bb;
2211 struct df_mir_problem_data *problem_data;
2212 if (df_mir->solutions_dirty)
2213 return;
2215 /* Set it true so that the solution is recomputed. */
2216 df_mir->solutions_dirty = true;
2218 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2219 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2220 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2221 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2223 FOR_ALL_BB_FN (bb, cfun)
2225 bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2226 bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2227 bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2228 bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2233 /* Compare the saved datastructure and the new solution to the dataflow
2234 equations. */
2236 static void
2237 df_mir_verify_solution_end (void)
2239 struct df_mir_problem_data *problem_data;
2240 basic_block bb;
2242 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2243 if (!problem_data->out)
2244 return;
2246 FOR_ALL_BB_FN (bb, cfun)
2248 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2249 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2250 gcc_unreachable ();
2253 /* Cannot delete them immediately because you may want to dump them
2254 if the comparison fails. */
2255 FOR_ALL_BB_FN (bb, cfun)
2257 bitmap_clear (&problem_data->in[bb->index]);
2258 bitmap_clear (&problem_data->out[bb->index]);
2261 free (problem_data->in);
2262 free (problem_data->out);
2263 bitmap_obstack_release (&problem_data->mir_bitmaps);
2264 free (problem_data);
2265 df_mir->problem_data = NULL;
2269 /* All of the information associated with every instance of the problem. */
2271 static const struct df_problem problem_MIR =
2273 DF_MIR, /* Problem id. */
2274 DF_FORWARD, /* Direction. */
2275 df_mir_alloc, /* Allocate the problem specific data. */
2276 df_mir_reset, /* Reset global information. */
2277 df_mir_free_bb_info, /* Free basic block info. */
2278 df_mir_local_compute, /* Local compute function. */
2279 df_mir_init, /* Init the solution specific data. */
2280 df_worklist_dataflow, /* Worklist solver. */
2281 df_mir_confluence_0, /* Confluence operator 0. */
2282 df_mir_confluence_n, /* Confluence operator n. */
2283 df_mir_transfer_function, /* Transfer function. */
2284 NULL, /* Finalize function. */
2285 df_mir_free, /* Free all of the problem information. */
2286 df_mir_free, /* Remove this problem from the stack of dataflow problems. */
2287 NULL, /* Debugging. */
2288 df_mir_top_dump, /* Debugging start block. */
2289 df_mir_bottom_dump, /* Debugging end block. */
2290 NULL, /* Debugging start insn. */
2291 NULL, /* Debugging end insn. */
2292 df_mir_verify_solution_start, /* Incremental solution verify start. */
2293 df_mir_verify_solution_end, /* Incremental solution verify end. */
2294 NULL, /* Dependent problem. */
2295 sizeof (class df_mir_bb_info),/* Size of entry of block_info array. */
2296 TV_DF_MIR, /* Timing variable. */
2297 false /* Reset blocks on dropping out of blocks_to_analyze. */
2301 /* Create a new DATAFLOW instance and add it to an existing instance
2302 of DF. */
2304 void
2305 df_mir_add_problem (void)
2307 df_add_problem (&problem_MIR);
2308 /* These will be initialized when df_scan_blocks processes each
2309 block. */
2310 df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2314 /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps. */
2316 void
2317 df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2318 bitmap kill, bitmap gen)
2320 df_ref def;
2322 FOR_EACH_INSN_DEF (def, insn)
2324 unsigned int regno = DF_REF_REGNO (def);
2326 /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2327 previous gen is irrelevant (and reciprocally). Also, claim that a
2328 register is GEN only if it is in all cases. */
2329 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2331 bitmap_set_bit (kill, regno);
2332 bitmap_clear_bit (gen, regno);
2334 /* In the worst case, partial and conditional defs can leave bits
2335 uninitialized, so assume they do not change anything. */
2336 else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2338 bitmap_set_bit (gen, regno);
2339 bitmap_clear_bit (kill, regno);
2344 /*----------------------------------------------------------------------------
2345 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2347 Link either the defs to the uses and / or the uses to the defs.
2349 These problems are set up like the other dataflow problems so that
2350 they nicely fit into the framework. They are much simpler and only
2351 involve a single traversal of instructions and an examination of
2352 the reaching defs information (the dependent problem).
2353 ----------------------------------------------------------------------------*/
2355 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2357 /* Create a du or ud chain from SRC to DST and link it into SRC. */
2359 struct df_link *
2360 df_chain_create (df_ref src, df_ref dst)
2362 struct df_link *head = DF_REF_CHAIN (src);
2363 struct df_link *link = df_chain->block_pool->allocate ();
2365 DF_REF_CHAIN (src) = link;
2366 link->next = head;
2367 link->ref = dst;
2368 return link;
2372 /* Delete any du or ud chains that start at REF and point to
2373 TARGET. */
2374 static void
2375 df_chain_unlink_1 (df_ref ref, df_ref target)
2377 struct df_link *chain = DF_REF_CHAIN (ref);
2378 struct df_link *prev = NULL;
2380 while (chain)
2382 if (chain->ref == target)
2384 if (prev)
2385 prev->next = chain->next;
2386 else
2387 DF_REF_CHAIN (ref) = chain->next;
2388 df_chain->block_pool->remove (chain);
2389 return;
2391 prev = chain;
2392 chain = chain->next;
2397 /* Delete a du or ud chain that leave or point to REF. */
2399 void
2400 df_chain_unlink (df_ref ref)
2402 struct df_link *chain = DF_REF_CHAIN (ref);
2403 while (chain)
2405 struct df_link *next = chain->next;
2406 /* Delete the other side if it exists. */
2407 df_chain_unlink_1 (chain->ref, ref);
2408 df_chain->block_pool->remove (chain);
2409 chain = next;
2411 DF_REF_CHAIN (ref) = NULL;
2415 /* Copy the du or ud chain starting at FROM_REF and attach it to
2416 TO_REF. */
2418 void
2419 df_chain_copy (df_ref to_ref,
2420 struct df_link *from_ref)
2422 while (from_ref)
2424 df_chain_create (to_ref, from_ref->ref);
2425 from_ref = from_ref->next;
2430 /* Remove this problem from the stack of dataflow problems. */
2432 static void
2433 df_chain_remove_problem (void)
2435 bitmap_iterator bi;
2436 unsigned int bb_index;
2438 /* Wholesale destruction of the old chains. */
2439 if (df_chain->block_pool)
2440 delete df_chain->block_pool;
2442 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2444 rtx_insn *insn;
2445 df_ref def, use;
2446 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2448 if (df_chain_problem_p (DF_DU_CHAIN))
2449 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2450 DF_REF_CHAIN (def) = NULL;
2451 if (df_chain_problem_p (DF_UD_CHAIN))
2452 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2453 DF_REF_CHAIN (use) = NULL;
2455 FOR_BB_INSNS (bb, insn)
2456 if (INSN_P (insn))
2458 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2459 if (df_chain_problem_p (DF_DU_CHAIN))
2460 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2461 DF_REF_CHAIN (def) = NULL;
2462 if (df_chain_problem_p (DF_UD_CHAIN))
2464 FOR_EACH_INSN_INFO_USE (use, insn_info)
2465 DF_REF_CHAIN (use) = NULL;
2466 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2467 DF_REF_CHAIN (use) = NULL;
2472 bitmap_clear (df_chain->out_of_date_transfer_functions);
2473 df_chain->block_pool = NULL;
2477 /* Remove the chain problem completely. */
2479 static void
2480 df_chain_fully_remove_problem (void)
2482 df_chain_remove_problem ();
2483 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2484 free (df_chain);
2488 /* Create def-use or use-def chains. */
2490 static void
2491 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2493 df_chain_remove_problem ();
2494 df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2495 df_chain->optional_p = true;
2499 /* Reset all of the chains when the set of basic blocks changes. */
2501 static void
2502 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2504 df_chain_remove_problem ();
2508 /* Create the chains for a list of USEs. */
2510 static void
2511 df_chain_create_bb_process_use (bitmap local_rd,
2512 df_ref use,
2513 int top_flag)
2515 bitmap_iterator bi;
2516 unsigned int def_index;
2518 for (; use; use = DF_REF_NEXT_LOC (use))
2520 unsigned int uregno = DF_REF_REGNO (use);
2521 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2522 || (uregno >= FIRST_PSEUDO_REGISTER))
2524 /* Do not want to go through this for an uninitialized var. */
2525 int count = DF_DEFS_COUNT (uregno);
2526 if (count)
2528 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2530 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2531 unsigned int last_index = first_index + count - 1;
2533 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2535 df_ref def;
2536 if (def_index > last_index)
2537 break;
2539 def = DF_DEFS_GET (def_index);
2540 if (df_chain_problem_p (DF_DU_CHAIN))
2541 df_chain_create (def, use);
2542 if (df_chain_problem_p (DF_UD_CHAIN))
2543 df_chain_create (use, def);
2552 /* Create chains from reaching defs bitmaps for basic block BB. */
2554 static void
2555 df_chain_create_bb (unsigned int bb_index)
2557 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2558 class df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2559 rtx_insn *insn;
2560 bitmap_head cpy;
2562 bitmap_initialize (&cpy, &bitmap_default_obstack);
2563 bitmap_copy (&cpy, &bb_info->in);
2564 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2566 /* Since we are going forwards, process the artificial uses first
2567 then the artificial defs second. */
2569 #ifdef EH_USES
2570 /* Create the chains for the artificial uses from the EH_USES at the
2571 beginning of the block. */
2573 /* Artificials are only hard regs. */
2574 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2575 df_chain_create_bb_process_use (&cpy,
2576 df_get_artificial_uses (bb->index),
2577 DF_REF_AT_TOP);
2578 #endif
2580 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2582 /* Process the regular instructions next. */
2583 FOR_BB_INSNS (bb, insn)
2584 if (INSN_P (insn))
2586 unsigned int uid = INSN_UID (insn);
2588 /* First scan the uses and link them up with the defs that remain
2589 in the cpy vector. */
2590 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2591 if (df->changeable_flags & DF_EQ_NOTES)
2592 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2594 /* Since we are going forwards, process the defs second. */
2595 df_rd_simulate_one_insn (bb, insn, &cpy);
2598 /* Create the chains for the artificial uses of the hard registers
2599 at the end of the block. */
2600 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2601 df_chain_create_bb_process_use (&cpy,
2602 df_get_artificial_uses (bb->index),
2605 bitmap_clear (&cpy);
2608 /* Create def-use chains from reaching use bitmaps for basic blocks
2609 in BLOCKS. */
2611 static void
2612 df_chain_finalize (bitmap all_blocks)
2614 unsigned int bb_index;
2615 bitmap_iterator bi;
2617 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2619 df_chain_create_bb (bb_index);
2624 /* Free all storage associated with the problem. */
2626 static void
2627 df_chain_free (void)
2629 delete df_chain->block_pool;
2630 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2631 free (df_chain);
2635 /* Debugging info. */
2637 static void
2638 df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2640 /* Artificials are only hard regs. */
2641 if (df->changeable_flags & DF_NO_HARD_REGS)
2642 return;
2643 if (df_chain_problem_p (DF_UD_CHAIN))
2645 df_ref use;
2647 fprintf (file,
2648 ";; UD chains for artificial uses at %s\n",
2649 top ? "top" : "bottom");
2650 FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2651 if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2652 || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2654 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2655 df_chain_dump (DF_REF_CHAIN (use), file);
2656 fprintf (file, "\n");
2659 if (df_chain_problem_p (DF_DU_CHAIN))
2661 df_ref def;
2663 fprintf (file,
2664 ";; DU chains for artificial defs at %s\n",
2665 top ? "top" : "bottom");
2666 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2667 if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2668 || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2670 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2671 df_chain_dump (DF_REF_CHAIN (def), file);
2672 fprintf (file, "\n");
2677 static void
2678 df_chain_top_dump (basic_block bb, FILE *file)
2680 df_chain_bb_dump (bb, file, /*top=*/true);
2683 static void
2684 df_chain_bottom_dump (basic_block bb, FILE *file)
2686 df_chain_bb_dump (bb, file, /*top=*/false);
2689 static void
2690 df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2692 if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2694 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2695 df_ref use;
2697 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2698 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2699 FOR_EACH_INSN_INFO_USE (use, insn_info)
2700 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2701 || !(df->changeable_flags & DF_NO_HARD_REGS))
2703 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2704 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2705 fprintf (file, "read/write ");
2706 df_chain_dump (DF_REF_CHAIN (use), file);
2707 fprintf (file, "\n");
2709 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2710 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2711 || !(df->changeable_flags & DF_NO_HARD_REGS))
2713 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2714 df_chain_dump (DF_REF_CHAIN (use), file);
2715 fprintf (file, "\n");
2720 static void
2721 df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2723 if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2725 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2726 df_ref def;
2727 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2728 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2729 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2730 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2731 || !(df->changeable_flags & DF_NO_HARD_REGS))
2733 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2734 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2735 fprintf (file, "read/write ");
2736 df_chain_dump (DF_REF_CHAIN (def), file);
2737 fprintf (file, "\n");
2739 fprintf (file, "\n");
2743 static const struct df_problem problem_CHAIN =
2745 DF_CHAIN, /* Problem id. */
2746 DF_NONE, /* Direction. */
2747 df_chain_alloc, /* Allocate the problem specific data. */
2748 df_chain_reset, /* Reset global information. */
2749 NULL, /* Free basic block info. */
2750 NULL, /* Local compute function. */
2751 NULL, /* Init the solution specific data. */
2752 NULL, /* Iterative solver. */
2753 NULL, /* Confluence operator 0. */
2754 NULL, /* Confluence operator n. */
2755 NULL, /* Transfer function. */
2756 df_chain_finalize, /* Finalize function. */
2757 df_chain_free, /* Free all of the problem information. */
2758 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2759 NULL, /* Debugging. */
2760 df_chain_top_dump, /* Debugging start block. */
2761 df_chain_bottom_dump, /* Debugging end block. */
2762 df_chain_insn_top_dump, /* Debugging start insn. */
2763 df_chain_insn_bottom_dump, /* Debugging end insn. */
2764 NULL, /* Incremental solution verify start. */
2765 NULL, /* Incremental solution verify end. */
2766 &problem_RD, /* Dependent problem. */
2767 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
2768 TV_DF_CHAIN, /* Timing variable. */
2769 false /* Reset blocks on dropping out of blocks_to_analyze. */
2773 /* Create a new DATAFLOW instance and add it to an existing instance
2774 of DF. The returned structure is what is used to get at the
2775 solution. */
2777 void
2778 df_chain_add_problem (unsigned int chain_flags)
2780 df_add_problem (&problem_CHAIN);
2781 df_chain->local_flags = chain_flags;
2782 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2785 #undef df_chain_problem_p
2788 /*----------------------------------------------------------------------------
2789 WORD LEVEL LIVE REGISTERS
2791 Find the locations in the function where any use of a pseudo can
2792 reach in the backwards direction. In and out bitvectors are built
2793 for each basic block. We only track pseudo registers that have a
2794 size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2795 contain two bits corresponding to each of the subwords.
2797 ----------------------------------------------------------------------------*/
2799 /* Private data used to verify the solution for this problem. */
2800 struct df_word_lr_problem_data
2802 /* An obstack for the bitmaps we need for this problem. */
2803 bitmap_obstack word_lr_bitmaps;
2807 /* Free basic block info. */
2809 static void
2810 df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2811 void *vbb_info)
2813 class df_word_lr_bb_info *bb_info = (class df_word_lr_bb_info *) vbb_info;
2814 if (bb_info)
2816 bitmap_clear (&bb_info->use);
2817 bitmap_clear (&bb_info->def);
2818 bitmap_clear (&bb_info->in);
2819 bitmap_clear (&bb_info->out);
2824 /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2825 not touched unless the block is new. */
2827 static void
2828 df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2830 unsigned int bb_index;
2831 bitmap_iterator bi;
2832 basic_block bb;
2833 struct df_word_lr_problem_data *problem_data
2834 = XNEW (struct df_word_lr_problem_data);
2836 df_word_lr->problem_data = problem_data;
2838 df_grow_bb_info (df_word_lr);
2840 /* Create the mapping from regnos to slots. This does not change
2841 unless the problem is destroyed and recreated. In particular, if
2842 we end up deleting the only insn that used a subreg, we do not
2843 want to redo the mapping because this would invalidate everything
2844 else. */
2846 bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2848 FOR_EACH_BB_FN (bb, cfun)
2849 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2851 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2852 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2854 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2856 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2858 /* When bitmaps are already initialized, just clear them. */
2859 if (bb_info->use.obstack)
2861 bitmap_clear (&bb_info->def);
2862 bitmap_clear (&bb_info->use);
2864 else
2866 bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2867 bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2868 bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2869 bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2873 df_word_lr->optional_p = true;
2877 /* Reset the global solution for recalculation. */
2879 static void
2880 df_word_lr_reset (bitmap all_blocks)
2882 unsigned int bb_index;
2883 bitmap_iterator bi;
2885 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2887 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2888 gcc_assert (bb_info);
2889 bitmap_clear (&bb_info->in);
2890 bitmap_clear (&bb_info->out);
2894 /* Examine REF, and if it is for a reg we're interested in, set or
2895 clear the bits corresponding to its subwords from the bitmap
2896 according to IS_SET. LIVE is the bitmap we should update. We do
2897 not track hard regs or pseudos of any size other than 2 *
2898 UNITS_PER_WORD.
2899 We return true if we changed the bitmap, or if we encountered a register
2900 we're not tracking. */
2902 bool
2903 df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2905 rtx orig_reg = DF_REF_REG (ref);
2906 rtx reg = orig_reg;
2907 machine_mode reg_mode;
2908 unsigned regno;
2909 /* Left at -1 for whole accesses. */
2910 int which_subword = -1;
2911 bool changed = false;
2913 if (GET_CODE (reg) == SUBREG)
2914 reg = SUBREG_REG (orig_reg);
2915 regno = REGNO (reg);
2916 reg_mode = GET_MODE (reg);
2917 if (regno < FIRST_PSEUDO_REGISTER
2918 || maybe_ne (GET_MODE_SIZE (reg_mode), 2 * UNITS_PER_WORD))
2919 return true;
2921 if (GET_CODE (orig_reg) == SUBREG
2922 && read_modify_subreg_p (orig_reg))
2924 gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2925 if (subreg_lowpart_p (orig_reg))
2926 which_subword = 0;
2927 else
2928 which_subword = 1;
2930 if (is_set)
2932 if (which_subword != 1)
2933 changed |= bitmap_set_bit (live, regno * 2);
2934 if (which_subword != 0)
2935 changed |= bitmap_set_bit (live, regno * 2 + 1);
2937 else
2939 if (which_subword != 1)
2940 changed |= bitmap_clear_bit (live, regno * 2);
2941 if (which_subword != 0)
2942 changed |= bitmap_clear_bit (live, regno * 2 + 1);
2944 return changed;
2947 /* Compute local live register info for basic block BB. */
2949 static void
2950 df_word_lr_bb_local_compute (unsigned int bb_index)
2952 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2953 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2954 rtx_insn *insn;
2955 df_ref def, use;
2957 /* Ensure that artificial refs don't contain references to pseudos. */
2958 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2959 gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2961 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2962 gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2964 FOR_BB_INSNS_REVERSE (bb, insn)
2966 if (!NONDEBUG_INSN_P (insn))
2967 continue;
2969 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2970 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2971 /* If the def is to only part of the reg, it does
2972 not kill the other defs that reach here. */
2973 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2975 df_word_lr_mark_ref (def, true, &bb_info->def);
2976 df_word_lr_mark_ref (def, false, &bb_info->use);
2978 FOR_EACH_INSN_INFO_USE (use, insn_info)
2979 df_word_lr_mark_ref (use, true, &bb_info->use);
2984 /* Compute local live register info for each basic block within BLOCKS. */
2986 static void
2987 df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2989 unsigned int bb_index;
2990 bitmap_iterator bi;
2992 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2994 if (bb_index == EXIT_BLOCK)
2996 unsigned regno;
2997 bitmap_iterator bi;
2998 EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2999 regno, bi)
3000 gcc_unreachable ();
3002 else
3003 df_word_lr_bb_local_compute (bb_index);
3006 bitmap_clear (df_word_lr->out_of_date_transfer_functions);
3010 /* Initialize the solution vectors. */
3012 static void
3013 df_word_lr_init (bitmap all_blocks)
3015 unsigned int bb_index;
3016 bitmap_iterator bi;
3018 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3020 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
3021 bitmap_copy (&bb_info->in, &bb_info->use);
3022 bitmap_clear (&bb_info->out);
3027 /* Confluence function that ignores fake edges. */
3029 static bool
3030 df_word_lr_confluence_n (edge e)
3032 bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
3033 bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
3035 return bitmap_ior_into (op1, op2);
3039 /* Transfer function. */
3041 static bool
3042 df_word_lr_transfer_function (int bb_index)
3044 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
3045 bitmap in = &bb_info->in;
3046 bitmap out = &bb_info->out;
3047 bitmap use = &bb_info->use;
3048 bitmap def = &bb_info->def;
3050 return bitmap_ior_and_compl (in, use, out, def);
3054 /* Free all storage associated with the problem. */
3056 static void
3057 df_word_lr_free (void)
3059 struct df_word_lr_problem_data *problem_data
3060 = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
3062 if (df_word_lr->block_info)
3064 df_word_lr->block_info_size = 0;
3065 free (df_word_lr->block_info);
3066 df_word_lr->block_info = NULL;
3069 BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
3070 bitmap_obstack_release (&problem_data->word_lr_bitmaps);
3071 free (problem_data);
3072 free (df_word_lr);
3076 /* Debugging info at top of bb. */
3078 static void
3079 df_word_lr_top_dump (basic_block bb, FILE *file)
3081 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3082 if (!bb_info)
3083 return;
3085 fprintf (file, ";; blr in \t");
3086 df_print_word_regset (file, &bb_info->in);
3087 fprintf (file, ";; blr use \t");
3088 df_print_word_regset (file, &bb_info->use);
3089 fprintf (file, ";; blr def \t");
3090 df_print_word_regset (file, &bb_info->def);
3094 /* Debugging info at bottom of bb. */
3096 static void
3097 df_word_lr_bottom_dump (basic_block bb, FILE *file)
3099 class df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3100 if (!bb_info)
3101 return;
3103 fprintf (file, ";; blr out \t");
3104 df_print_word_regset (file, &bb_info->out);
3108 /* All of the information associated with every instance of the problem. */
3110 static const struct df_problem problem_WORD_LR =
3112 DF_WORD_LR, /* Problem id. */
3113 DF_BACKWARD, /* Direction. */
3114 df_word_lr_alloc, /* Allocate the problem specific data. */
3115 df_word_lr_reset, /* Reset global information. */
3116 df_word_lr_free_bb_info, /* Free basic block info. */
3117 df_word_lr_local_compute, /* Local compute function. */
3118 df_word_lr_init, /* Init the solution specific data. */
3119 df_worklist_dataflow, /* Worklist solver. */
3120 NULL, /* Confluence operator 0. */
3121 df_word_lr_confluence_n, /* Confluence operator n. */
3122 df_word_lr_transfer_function, /* Transfer function. */
3123 NULL, /* Finalize function. */
3124 df_word_lr_free, /* Free all of the problem information. */
3125 df_word_lr_free, /* Remove this problem from the stack of dataflow problems. */
3126 NULL, /* Debugging. */
3127 df_word_lr_top_dump, /* Debugging start block. */
3128 df_word_lr_bottom_dump, /* Debugging end block. */
3129 NULL, /* Debugging start insn. */
3130 NULL, /* Debugging end insn. */
3131 NULL, /* Incremental solution verify start. */
3132 NULL, /* Incremental solution verify end. */
3133 NULL, /* Dependent problem. */
3134 sizeof (class df_word_lr_bb_info),/* Size of entry of block_info array. */
3135 TV_DF_WORD_LR, /* Timing variable. */
3136 false /* Reset blocks on dropping out of blocks_to_analyze. */
3140 /* Create a new DATAFLOW instance and add it to an existing instance
3141 of DF. The returned structure is what is used to get at the
3142 solution. */
3144 void
3145 df_word_lr_add_problem (void)
3147 df_add_problem (&problem_WORD_LR);
3148 /* These will be initialized when df_scan_blocks processes each
3149 block. */
3150 df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3154 /* Simulate the effects of the defs of INSN on LIVE. Return true if we changed
3155 any bits, which is used by the caller to determine whether a set is
3156 necessary. We also return true if there are other reasons not to delete
3157 an insn. */
3159 bool
3160 df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3162 bool changed = false;
3163 df_ref def;
3165 FOR_EACH_INSN_DEF (def, insn)
3166 if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3167 changed = true;
3168 else
3169 changed |= df_word_lr_mark_ref (def, false, live);
3170 return changed;
3174 /* Simulate the effects of the uses of INSN on LIVE. */
3176 void
3177 df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3179 df_ref use;
3181 FOR_EACH_INSN_USE (use, insn)
3182 df_word_lr_mark_ref (use, true, live);
3185 /*----------------------------------------------------------------------------
3186 This problem computes REG_DEAD and REG_UNUSED notes.
3187 ----------------------------------------------------------------------------*/
3189 static void
3190 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3192 df_note->optional_p = true;
3195 /* This is only used if REG_DEAD_DEBUGGING is in effect. */
3196 static void
3197 df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3199 if (dump_file)
3201 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3202 print_rtl (dump_file, note);
3203 fprintf (dump_file, "\n");
3208 /* After reg-stack, the x86 floating point stack regs are difficult to
3209 analyze because of all of the pushes, pops and rotations. Thus, we
3210 just leave the notes alone. */
3212 #ifdef STACK_REGS
3213 static inline bool
3214 df_ignore_stack_reg (int regno)
3216 return regstack_completed
3217 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3219 #else
3220 static inline bool
3221 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3223 return false;
3225 #endif
3228 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN. */
3230 static void
3231 df_remove_dead_and_unused_notes (rtx_insn *insn)
3233 rtx *pprev = &REG_NOTES (insn);
3234 rtx link = *pprev;
3236 while (link)
3238 switch (REG_NOTE_KIND (link))
3240 case REG_DEAD:
3241 /* After reg-stack, we need to ignore any unused notes
3242 for the stack registers. */
3243 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3245 pprev = &XEXP (link, 1);
3246 link = *pprev;
3248 else
3250 rtx next = XEXP (link, 1);
3251 if (REG_DEAD_DEBUGGING)
3252 df_print_note ("deleting: ", insn, link);
3253 free_EXPR_LIST_node (link);
3254 *pprev = link = next;
3256 break;
3258 case REG_UNUSED:
3259 /* After reg-stack, we need to ignore any unused notes
3260 for the stack registers. */
3261 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3263 pprev = &XEXP (link, 1);
3264 link = *pprev;
3266 else
3268 rtx next = XEXP (link, 1);
3269 if (REG_DEAD_DEBUGGING)
3270 df_print_note ("deleting: ", insn, link);
3271 free_EXPR_LIST_node (link);
3272 *pprev = link = next;
3274 break;
3276 default:
3277 pprev = &XEXP (link, 1);
3278 link = *pprev;
3279 break;
3284 /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3285 as the bitmap of currently live registers. */
3287 static void
3288 df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3290 rtx *pprev = &REG_NOTES (insn);
3291 rtx link = *pprev;
3293 while (link)
3295 switch (REG_NOTE_KIND (link))
3297 case REG_EQUAL:
3298 case REG_EQUIV:
3300 /* Remove the notes that refer to dead registers. As we have at most
3301 one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3302 so we need to purge the complete EQ_USES vector when removing
3303 the note using df_notes_rescan. */
3304 df_ref use;
3305 bool deleted = false;
3307 FOR_EACH_INSN_EQ_USE (use, insn)
3308 if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER
3309 && DF_REF_LOC (use)
3310 && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3311 && !bitmap_bit_p (live, DF_REF_REGNO (use))
3312 && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3314 deleted = true;
3315 break;
3317 if (deleted)
3319 rtx next;
3320 if (REG_DEAD_DEBUGGING)
3321 df_print_note ("deleting: ", insn, link);
3322 next = XEXP (link, 1);
3323 free_EXPR_LIST_node (link);
3324 *pprev = link = next;
3325 df_notes_rescan (insn);
3327 else
3329 pprev = &XEXP (link, 1);
3330 link = *pprev;
3332 break;
3335 default:
3336 pprev = &XEXP (link, 1);
3337 link = *pprev;
3338 break;
3343 /* Set a NOTE_TYPE note for REG in INSN. */
3345 static inline void
3346 df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3348 gcc_checking_assert (!DEBUG_INSN_P (insn));
3349 add_reg_note (insn, note_type, reg);
3352 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3353 arguments. Return true if the register value described by MWS's
3354 mw_reg is known to be completely unused, and if mw_reg can therefore
3355 be used in a REG_UNUSED note. */
3357 static bool
3358 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3359 bitmap live, bitmap artificial_uses)
3361 unsigned int r;
3363 /* If MWS describes a partial reference, create REG_UNUSED notes for
3364 individual hard registers. */
3365 if (mws->flags & DF_REF_PARTIAL)
3366 return false;
3368 /* Likewise if some part of the register is used. */
3369 for (r = mws->start_regno; r <= mws->end_regno; r++)
3370 if (bitmap_bit_p (live, r)
3371 || bitmap_bit_p (artificial_uses, r))
3372 return false;
3374 gcc_assert (REG_P (mws->mw_reg));
3375 return true;
3379 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3380 based on the bits in LIVE. Do not generate notes for registers in
3381 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3382 not generated if the reg is both read and written by the
3383 instruction.
3386 static void
3387 df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3388 bitmap live, bitmap do_not_gen,
3389 bitmap artificial_uses,
3390 struct dead_debug_local *debug)
3392 unsigned int r;
3394 if (REG_DEAD_DEBUGGING && dump_file)
3395 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3396 mws->start_regno, mws->end_regno);
3398 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3400 unsigned int regno = mws->start_regno;
3401 df_set_note (REG_UNUSED, insn, mws->mw_reg);
3402 dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3404 if (REG_DEAD_DEBUGGING)
3405 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3407 bitmap_set_bit (do_not_gen, regno);
3408 /* Only do this if the value is totally dead. */
3410 else
3411 for (r = mws->start_regno; r <= mws->end_regno; r++)
3413 if (!bitmap_bit_p (live, r)
3414 && !bitmap_bit_p (artificial_uses, r))
3416 df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3417 dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3418 if (REG_DEAD_DEBUGGING)
3419 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3421 bitmap_set_bit (do_not_gen, r);
3426 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3427 arguments. Return true if the register value described by MWS's
3428 mw_reg is known to be completely dead, and if mw_reg can therefore
3429 be used in a REG_DEAD note. */
3431 static bool
3432 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3433 bitmap live, bitmap artificial_uses,
3434 bitmap do_not_gen)
3436 unsigned int r;
3438 /* If MWS describes a partial reference, create REG_DEAD notes for
3439 individual hard registers. */
3440 if (mws->flags & DF_REF_PARTIAL)
3441 return false;
3443 /* Likewise if some part of the register is not dead. */
3444 for (r = mws->start_regno; r <= mws->end_regno; r++)
3445 if (bitmap_bit_p (live, r)
3446 || bitmap_bit_p (artificial_uses, r)
3447 || bitmap_bit_p (do_not_gen, r))
3448 return false;
3450 gcc_assert (REG_P (mws->mw_reg));
3451 return true;
3454 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3455 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3456 from being set if the instruction both reads and writes the
3457 register. */
3459 static void
3460 df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3461 bitmap live, bitmap do_not_gen,
3462 bitmap artificial_uses, bool *added_notes_p)
3464 unsigned int r;
3465 bool is_debug = *added_notes_p;
3467 *added_notes_p = false;
3469 if (REG_DEAD_DEBUGGING && dump_file)
3471 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3472 mws->start_regno, mws->end_regno);
3473 df_print_regset (dump_file, do_not_gen);
3474 fprintf (dump_file, " live =");
3475 df_print_regset (dump_file, live);
3476 fprintf (dump_file, " artificial uses =");
3477 df_print_regset (dump_file, artificial_uses);
3480 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3482 if (is_debug)
3484 *added_notes_p = true;
3485 return;
3487 /* Add a dead note for the entire multi word register. */
3488 df_set_note (REG_DEAD, insn, mws->mw_reg);
3489 if (REG_DEAD_DEBUGGING)
3490 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3492 else
3494 for (r = mws->start_regno; r <= mws->end_regno; r++)
3495 if (!bitmap_bit_p (live, r)
3496 && !bitmap_bit_p (artificial_uses, r)
3497 && !bitmap_bit_p (do_not_gen, r))
3499 if (is_debug)
3501 *added_notes_p = true;
3502 return;
3504 df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3505 if (REG_DEAD_DEBUGGING)
3506 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3509 return;
3513 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3514 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3516 static void
3517 df_create_unused_note (rtx_insn *insn, df_ref def,
3518 bitmap live, bitmap artificial_uses,
3519 struct dead_debug_local *debug)
3521 unsigned int dregno = DF_REF_REGNO (def);
3523 if (REG_DEAD_DEBUGGING && dump_file)
3525 fprintf (dump_file, " regular looking at def ");
3526 df_ref_debug (def, dump_file);
3529 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3530 || bitmap_bit_p (live, dregno)
3531 || bitmap_bit_p (artificial_uses, dregno)
3532 || df_ignore_stack_reg (dregno)))
3534 rtx reg = (DF_REF_LOC (def))
3535 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3536 df_set_note (REG_UNUSED, insn, reg);
3537 dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3538 if (REG_DEAD_DEBUGGING)
3539 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3542 return;
3546 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3547 info: lifetime, bb, and number of defs and uses for basic block
3548 BB. The three bitvectors are scratch regs used here. */
3550 static void
3551 df_note_bb_compute (unsigned int bb_index,
3552 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3554 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3555 rtx_insn *insn;
3556 df_ref def, use;
3557 struct dead_debug_local debug;
3559 dead_debug_local_init (&debug, NULL, NULL);
3561 bitmap_copy (live, df_get_live_out (bb));
3562 bitmap_clear (artificial_uses);
3564 if (REG_DEAD_DEBUGGING && dump_file)
3566 fprintf (dump_file, "live at bottom ");
3567 df_print_regset (dump_file, live);
3570 /* Process the artificial defs and uses at the bottom of the block
3571 to begin processing. */
3572 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3574 if (REG_DEAD_DEBUGGING && dump_file)
3575 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3577 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3578 bitmap_clear_bit (live, DF_REF_REGNO (def));
3581 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3582 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3584 unsigned int regno = DF_REF_REGNO (use);
3585 bitmap_set_bit (live, regno);
3587 /* Notes are not generated for any of the artificial registers
3588 at the bottom of the block. */
3589 bitmap_set_bit (artificial_uses, regno);
3592 if (REG_DEAD_DEBUGGING && dump_file)
3594 fprintf (dump_file, "live before artificials out ");
3595 df_print_regset (dump_file, live);
3598 FOR_BB_INSNS_REVERSE (bb, insn)
3600 if (!INSN_P (insn))
3601 continue;
3603 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3604 df_mw_hardreg *mw;
3605 int debug_insn;
3607 debug_insn = DEBUG_INSN_P (insn);
3609 bitmap_clear (do_not_gen);
3610 df_remove_dead_and_unused_notes (insn);
3612 /* Process the defs. */
3613 if (CALL_P (insn))
3615 if (REG_DEAD_DEBUGGING && dump_file)
3617 fprintf (dump_file, "processing call %d\n live =",
3618 INSN_UID (insn));
3619 df_print_regset (dump_file, live);
3622 /* We only care about real sets for calls. Clobbers cannot
3623 be depended on to really die. */
3624 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3625 if ((DF_MWS_REG_DEF_P (mw))
3626 && !df_ignore_stack_reg (mw->start_regno))
3627 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3628 artificial_uses, &debug);
3630 /* All of the defs except the return value are some sort of
3631 clobber. This code is for the return. */
3632 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3634 unsigned int dregno = DF_REF_REGNO (def);
3635 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3637 df_create_unused_note (insn,
3638 def, live, artificial_uses, &debug);
3639 bitmap_set_bit (do_not_gen, dregno);
3642 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3643 bitmap_clear_bit (live, dregno);
3646 else
3648 /* Regular insn. */
3649 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3650 if (DF_MWS_REG_DEF_P (mw))
3651 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3652 artificial_uses, &debug);
3654 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3656 unsigned int dregno = DF_REF_REGNO (def);
3657 df_create_unused_note (insn,
3658 def, live, artificial_uses, &debug);
3660 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3661 bitmap_set_bit (do_not_gen, dregno);
3663 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3664 bitmap_clear_bit (live, dregno);
3668 /* Process the uses. */
3669 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3670 if (DF_MWS_REG_USE_P (mw)
3671 && !df_ignore_stack_reg (mw->start_regno))
3673 bool really_add_notes = debug_insn != 0;
3675 df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3676 artificial_uses,
3677 &really_add_notes);
3679 if (really_add_notes)
3680 debug_insn = -1;
3683 FOR_EACH_INSN_INFO_USE (use, insn_info)
3685 unsigned int uregno = DF_REF_REGNO (use);
3687 if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3689 fprintf (dump_file, " regular looking at use ");
3690 df_ref_debug (use, dump_file);
3693 if (!bitmap_bit_p (live, uregno))
3695 if (debug_insn)
3697 if (debug_insn > 0)
3699 /* We won't add REG_UNUSED or REG_DEAD notes for
3700 these, so we don't have to mess with them in
3701 debug insns either. */
3702 if (!bitmap_bit_p (artificial_uses, uregno)
3703 && !df_ignore_stack_reg (uregno))
3704 dead_debug_add (&debug, use, uregno);
3705 continue;
3707 break;
3709 else
3710 dead_debug_insert_temp (&debug, uregno, insn,
3711 DEBUG_TEMP_BEFORE_WITH_REG);
3713 if ( (!(DF_REF_FLAGS (use)
3714 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3715 && (!bitmap_bit_p (do_not_gen, uregno))
3716 && (!bitmap_bit_p (artificial_uses, uregno))
3717 && (!df_ignore_stack_reg (uregno)))
3719 rtx reg = (DF_REF_LOC (use))
3720 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3721 df_set_note (REG_DEAD, insn, reg);
3723 if (REG_DEAD_DEBUGGING)
3724 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3726 /* This register is now live. */
3727 bitmap_set_bit (live, uregno);
3731 df_remove_dead_eq_notes (insn, live);
3733 if (debug_insn == -1)
3735 /* ??? We could probably do better here, replacing dead
3736 registers with their definitions. */
3737 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3738 df_insn_rescan_debug_internal (insn);
3742 dead_debug_local_finish (&debug, NULL);
3746 /* Compute register info: lifetime, bb, and number of defs and uses. */
3747 static void
3748 df_note_compute (bitmap all_blocks)
3750 unsigned int bb_index;
3751 bitmap_iterator bi;
3752 bitmap_head live, do_not_gen, artificial_uses;
3754 bitmap_initialize (&live, &df_bitmap_obstack);
3755 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3756 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3758 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3760 /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3761 pseudos in debug insns because we don't always (re)visit blocks
3762 with death points after visiting dead uses. Even changing this
3763 loop to postorder would still leave room for visiting a death
3764 point before visiting a subsequent debug use. */
3765 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3768 bitmap_clear (&live);
3769 bitmap_clear (&do_not_gen);
3770 bitmap_clear (&artificial_uses);
3774 /* Free all storage associated with the problem. */
3776 static void
3777 df_note_free (void)
3779 free (df_note);
3783 /* All of the information associated every instance of the problem. */
3785 static const struct df_problem problem_NOTE =
3787 DF_NOTE, /* Problem id. */
3788 DF_NONE, /* Direction. */
3789 df_note_alloc, /* Allocate the problem specific data. */
3790 NULL, /* Reset global information. */
3791 NULL, /* Free basic block info. */
3792 df_note_compute, /* Local compute function. */
3793 NULL, /* Init the solution specific data. */
3794 NULL, /* Iterative solver. */
3795 NULL, /* Confluence operator 0. */
3796 NULL, /* Confluence operator n. */
3797 NULL, /* Transfer function. */
3798 NULL, /* Finalize function. */
3799 df_note_free, /* Free all of the problem information. */
3800 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3801 NULL, /* Debugging. */
3802 NULL, /* Debugging start block. */
3803 NULL, /* Debugging end block. */
3804 NULL, /* Debugging start insn. */
3805 NULL, /* Debugging end insn. */
3806 NULL, /* Incremental solution verify start. */
3807 NULL, /* Incremental solution verify end. */
3808 &problem_LR, /* Dependent problem. */
3809 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
3810 TV_DF_NOTE, /* Timing variable. */
3811 false /* Reset blocks on dropping out of blocks_to_analyze. */
3815 /* Create a new DATAFLOW instance and add it to an existing instance
3816 of DF. The returned structure is what is used to get at the
3817 solution. */
3819 void
3820 df_note_add_problem (void)
3822 df_add_problem (&problem_NOTE);
3828 /*----------------------------------------------------------------------------
3829 Functions for simulating the effects of single insns.
3831 You can either simulate in the forwards direction, starting from
3832 the top of a block or the backwards direction from the end of the
3833 block. If you go backwards, defs are examined first to clear bits,
3834 then uses are examined to set bits. If you go forwards, defs are
3835 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3836 are examined to clear bits. In either case, the result of examining
3837 a def can be undone (respectively by a use or a REG_UNUSED note).
3839 If you start at the top of the block, use one of DF_LIVE_IN or
3840 DF_LR_IN. If you start at the bottom of the block use one of
3841 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3842 THEY WILL BE DESTROYED.
3843 ----------------------------------------------------------------------------*/
3846 /* Find the set of DEFs for INSN. */
3848 void
3849 df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3851 df_ref def;
3853 FOR_EACH_INSN_DEF (def, insn)
3854 bitmap_set_bit (defs, DF_REF_REGNO (def));
3857 /* Find the set of uses for INSN. This includes partial defs. */
3859 static void
3860 df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3862 df_ref def, use;
3863 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3865 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3866 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3867 bitmap_set_bit (uses, DF_REF_REGNO (def));
3868 FOR_EACH_INSN_INFO_USE (use, insn_info)
3869 bitmap_set_bit (uses, DF_REF_REGNO (use));
3872 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3874 void
3875 df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3877 df_ref def;
3879 FOR_EACH_INSN_DEF (def, insn)
3880 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3881 bitmap_set_bit (defs, DF_REF_REGNO (def));
3885 /* Simulate the effects of the defs of INSN on LIVE. */
3887 void
3888 df_simulate_defs (rtx_insn *insn, bitmap live)
3890 df_ref def;
3892 FOR_EACH_INSN_DEF (def, insn)
3894 unsigned int dregno = DF_REF_REGNO (def);
3896 /* If the def is to only part of the reg, it does
3897 not kill the other defs that reach here. */
3898 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3899 bitmap_clear_bit (live, dregno);
3904 /* Simulate the effects of the uses of INSN on LIVE. */
3906 void
3907 df_simulate_uses (rtx_insn *insn, bitmap live)
3909 df_ref use;
3911 if (DEBUG_INSN_P (insn))
3912 return;
3914 FOR_EACH_INSN_USE (use, insn)
3915 /* Add use to set of uses in this BB. */
3916 bitmap_set_bit (live, DF_REF_REGNO (use));
3920 /* Add back the always live regs in BB to LIVE. */
3922 static inline void
3923 df_simulate_fixup_sets (basic_block bb, bitmap live)
3925 /* These regs are considered always live so if they end up dying
3926 because of some def, we need to bring the back again. */
3927 if (bb_has_eh_pred (bb))
3928 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3929 else
3930 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3934 /*----------------------------------------------------------------------------
3935 The following three functions are used only for BACKWARDS scanning:
3936 i.e. they process the defs before the uses.
3938 df_simulate_initialize_backwards should be called first with a
3939 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
3940 df_simulate_one_insn_backwards should be called for each insn in
3941 the block, starting with the last one. Finally,
3942 df_simulate_finalize_backwards can be called to get a new value
3943 of the sets at the top of the block (this is rarely used).
3944 ----------------------------------------------------------------------------*/
3946 /* Apply the artificial uses and defs at the end of BB in a backwards
3947 direction. */
3949 void
3950 df_simulate_initialize_backwards (basic_block bb, bitmap live)
3952 df_ref def, use;
3953 int bb_index = bb->index;
3955 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3956 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3957 bitmap_clear_bit (live, DF_REF_REGNO (def));
3959 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3960 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3961 bitmap_set_bit (live, DF_REF_REGNO (use));
3965 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
3967 void
3968 df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3970 if (!NONDEBUG_INSN_P (insn))
3971 return;
3973 df_simulate_defs (insn, live);
3974 df_simulate_uses (insn, live);
3975 df_simulate_fixup_sets (bb, live);
3979 /* Apply the artificial uses and defs at the top of BB in a backwards
3980 direction. */
3982 void
3983 df_simulate_finalize_backwards (basic_block bb, bitmap live)
3985 df_ref def;
3986 #ifdef EH_USES
3987 df_ref use;
3988 #endif
3989 int bb_index = bb->index;
3991 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3992 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3993 bitmap_clear_bit (live, DF_REF_REGNO (def));
3995 #ifdef EH_USES
3996 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3997 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3998 bitmap_set_bit (live, DF_REF_REGNO (use));
3999 #endif
4001 /*----------------------------------------------------------------------------
4002 The following three functions are used only for FORWARDS scanning:
4003 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
4004 Thus it is important to add the DF_NOTES problem to the stack of
4005 problems computed before using these functions.
4007 df_simulate_initialize_forwards should be called first with a
4008 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
4009 df_simulate_one_insn_forwards should be called for each insn in
4010 the block, starting with the first one.
4011 ----------------------------------------------------------------------------*/
4013 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
4014 DF_LR_IN for basic block BB, for forward scanning by marking artificial
4015 defs live. */
4017 void
4018 df_simulate_initialize_forwards (basic_block bb, bitmap live)
4020 df_ref def;
4021 int bb_index = bb->index;
4023 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4024 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4025 bitmap_set_bit (live, DF_REF_REGNO (def));
4028 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
4030 void
4031 df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
4033 rtx link;
4034 if (! INSN_P (insn))
4035 return;
4037 /* Make sure that DF_NOTE really is an active df problem. */
4038 gcc_assert (df_note);
4040 /* Note that this is the opposite as how the problem is defined, because
4041 in the LR problem defs _kill_ liveness. However, they do so backwards,
4042 while here the scan is performed forwards! So, first assume that the
4043 def is live, and if this is not true REG_UNUSED notes will rectify the
4044 situation. */
4045 df_simulate_find_noclobber_defs (insn, live);
4047 /* Clear all of the registers that go dead. */
4048 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
4050 switch (REG_NOTE_KIND (link))
4052 case REG_DEAD:
4053 case REG_UNUSED:
4055 rtx reg = XEXP (link, 0);
4056 bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
4058 break;
4059 default:
4060 break;
4063 df_simulate_fixup_sets (bb, live);
4066 /* Used by the next two functions to encode information about the
4067 memory references we found. */
4068 #define MEMREF_NORMAL 1
4069 #define MEMREF_VOLATILE 2
4071 /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X. */
4073 static int
4074 find_memory (rtx_insn *insn)
4076 int flags = 0;
4077 subrtx_iterator::array_type array;
4078 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
4080 const_rtx x = *iter;
4081 if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
4082 flags |= MEMREF_VOLATILE;
4083 else if (MEM_P (x))
4085 if (MEM_VOLATILE_P (x))
4086 flags |= MEMREF_VOLATILE;
4087 else if (!MEM_READONLY_P (x))
4088 flags |= MEMREF_NORMAL;
4091 return flags;
4094 /* A subroutine of can_move_insns_across_p called through note_stores.
4095 DATA points to an integer in which we set either the bit for
4096 MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
4097 of either kind. */
4099 static void
4100 find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4101 void *data ATTRIBUTE_UNUSED)
4103 int *pflags = (int *)data;
4104 if (GET_CODE (x) == SUBREG)
4105 x = XEXP (x, 0);
4106 /* Treat stores to SP as stores to memory, this will prevent problems
4107 when there are references to the stack frame. */
4108 if (x == stack_pointer_rtx)
4109 *pflags |= MEMREF_VOLATILE;
4110 if (!MEM_P (x))
4111 return;
4112 *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4115 /* Scan BB backwards, using df_simulate functions to keep track of
4116 lifetimes, up to insn POINT. The result is stored in LIVE. */
4118 void
4119 simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4121 rtx_insn *insn;
4122 bitmap_copy (live, df_get_live_out (bb));
4123 df_simulate_initialize_backwards (bb, live);
4125 /* Scan and update life information until we reach the point we're
4126 interested in. */
4127 for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4128 df_simulate_one_insn_backwards (bb, insn, live);
4131 /* Return true if it is safe to move a group of insns, described by
4132 the range FROM to TO, backwards across another group of insns,
4133 described by ACROSS_FROM to ACROSS_TO. It is assumed that there
4134 are no insns between ACROSS_TO and FROM, but they may be in
4135 different basic blocks; MERGE_BB is the block from which the
4136 insns will be moved. The caller must pass in a regset MERGE_LIVE
4137 which specifies the registers live after TO.
4139 This function may be called in one of two cases: either we try to
4140 move identical instructions from all successor blocks into their
4141 predecessor, or we try to move from only one successor block. If
4142 OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4143 the second case. It should contain a set of registers live at the
4144 end of ACROSS_TO which must not be clobbered by moving the insns.
4145 In that case, we're also more careful about moving memory references
4146 and trapping insns.
4148 We return false if it is not safe to move the entire group, but it
4149 may still be possible to move a subgroup. PMOVE_UPTO, if nonnull,
4150 is set to point at the last moveable insn in such a case. */
4152 bool
4153 can_move_insns_across (rtx_insn *from, rtx_insn *to,
4154 rtx_insn *across_from, rtx_insn *across_to,
4155 basic_block merge_bb, regset merge_live,
4156 regset other_branch_live, rtx_insn **pmove_upto)
4158 rtx_insn *insn, *next, *max_to;
4159 bitmap merge_set, merge_use, local_merge_live;
4160 bitmap test_set, test_use;
4161 unsigned i, fail = 0;
4162 bitmap_iterator bi;
4163 int memrefs_in_across = 0;
4164 int mem_sets_in_across = 0;
4165 bool trapping_insns_in_across = false;
4167 if (pmove_upto != NULL)
4168 *pmove_upto = NULL;
4170 /* Find real bounds, ignoring debug insns. */
4171 while (!NONDEBUG_INSN_P (from) && from != to)
4172 from = NEXT_INSN (from);
4173 while (!NONDEBUG_INSN_P (to) && from != to)
4174 to = PREV_INSN (to);
4176 for (insn = across_to; ; insn = next)
4178 if (CALL_P (insn))
4180 if (RTL_CONST_OR_PURE_CALL_P (insn))
4181 /* Pure functions can read from memory. Const functions can
4182 read from arguments that the ABI has forced onto the stack.
4183 Neither sort of read can be volatile. */
4184 memrefs_in_across |= MEMREF_NORMAL;
4185 else
4187 memrefs_in_across |= MEMREF_VOLATILE;
4188 mem_sets_in_across |= MEMREF_VOLATILE;
4191 if (NONDEBUG_INSN_P (insn))
4193 if (volatile_insn_p (PATTERN (insn)))
4194 return false;
4195 memrefs_in_across |= find_memory (insn);
4196 note_stores (insn, find_memory_stores, &mem_sets_in_across);
4197 /* This is used just to find sets of the stack pointer. */
4198 memrefs_in_across |= mem_sets_in_across;
4199 trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4201 next = PREV_INSN (insn);
4202 if (insn == across_from)
4203 break;
4206 /* Collect:
4207 MERGE_SET = set of registers set in MERGE_BB
4208 MERGE_USE = set of registers used in MERGE_BB and live at its top
4209 MERGE_LIVE = set of registers live at the point inside the MERGE
4210 range that we've reached during scanning
4211 TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4212 TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4213 and live before ACROSS_FROM. */
4215 merge_set = BITMAP_ALLOC (&reg_obstack);
4216 merge_use = BITMAP_ALLOC (&reg_obstack);
4217 local_merge_live = BITMAP_ALLOC (&reg_obstack);
4218 test_set = BITMAP_ALLOC (&reg_obstack);
4219 test_use = BITMAP_ALLOC (&reg_obstack);
4221 /* Compute the set of registers set and used in the ACROSS range. */
4222 if (other_branch_live != NULL)
4223 bitmap_copy (test_use, other_branch_live);
4224 df_simulate_initialize_backwards (merge_bb, test_use);
4225 for (insn = across_to; ; insn = next)
4227 if (NONDEBUG_INSN_P (insn))
4229 df_simulate_find_defs (insn, test_set);
4230 df_simulate_defs (insn, test_use);
4231 df_simulate_uses (insn, test_use);
4233 next = PREV_INSN (insn);
4234 if (insn == across_from)
4235 break;
4238 /* Compute an upper bound for the amount of insns moved, by finding
4239 the first insn in MERGE that sets a register in TEST_USE, or uses
4240 a register in TEST_SET. We also check for calls, trapping operations,
4241 and memory references. */
4242 max_to = NULL;
4243 for (insn = from; ; insn = next)
4245 if (CALL_P (insn))
4246 break;
4247 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4248 break;
4249 if (NONDEBUG_INSN_P (insn))
4251 if (may_trap_or_fault_p (PATTERN (insn))
4252 && (trapping_insns_in_across
4253 || other_branch_live != NULL
4254 || volatile_insn_p (PATTERN (insn))))
4255 break;
4257 /* We cannot move memory stores past each other, or move memory
4258 reads past stores, at least not without tracking them and
4259 calling true_dependence on every pair.
4261 If there is no other branch and no memory references or
4262 sets in the ACROSS range, we can move memory references
4263 freely, even volatile ones.
4265 Otherwise, the rules are as follows: volatile memory
4266 references and stores can't be moved at all, and any type
4267 of memory reference can't be moved if there are volatile
4268 accesses or stores in the ACROSS range. That leaves
4269 normal reads, which can be moved, as the trapping case is
4270 dealt with elsewhere. */
4271 if (other_branch_live != NULL || memrefs_in_across != 0)
4273 int mem_ref_flags = 0;
4274 int mem_set_flags = 0;
4275 note_stores (insn, find_memory_stores, &mem_set_flags);
4276 mem_ref_flags = find_memory (insn);
4277 /* Catch sets of the stack pointer. */
4278 mem_ref_flags |= mem_set_flags;
4280 if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4281 break;
4282 if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4283 break;
4284 if (mem_set_flags != 0
4285 || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4286 break;
4288 df_simulate_find_uses (insn, merge_use);
4289 /* We're only interested in uses which use a value live at
4290 the top, not one previously set in this block. */
4291 bitmap_and_compl_into (merge_use, merge_set);
4292 df_simulate_find_defs (insn, merge_set);
4293 if (bitmap_intersect_p (merge_set, test_use)
4294 || bitmap_intersect_p (merge_use, test_set))
4295 break;
4296 max_to = insn;
4298 next = NEXT_INSN (insn);
4299 if (insn == to)
4300 break;
4302 if (max_to != to)
4303 fail = 1;
4305 if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4306 goto out;
4308 /* Now, lower this upper bound by also taking into account that
4309 a range of insns moved across ACROSS must not leave a register
4310 live at the end that will be clobbered in ACROSS. We need to
4311 find a point where TEST_SET & LIVE == 0.
4313 Insns in the MERGE range that set registers which are also set
4314 in the ACROSS range may still be moved as long as we also move
4315 later insns which use the results of the set, and make the
4316 register dead again. This is verified by the condition stated
4317 above. We only need to test it for registers that are set in
4318 the moved region.
4320 MERGE_LIVE is provided by the caller and holds live registers after
4321 TO. */
4322 bitmap_copy (local_merge_live, merge_live);
4323 for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4324 df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4326 /* We're not interested in registers that aren't set in the moved
4327 region at all. */
4328 bitmap_and_into (local_merge_live, merge_set);
4329 for (;;)
4331 if (NONDEBUG_INSN_P (insn))
4333 if (!bitmap_intersect_p (test_set, local_merge_live))
4335 max_to = insn;
4336 break;
4339 df_simulate_one_insn_backwards (merge_bb, insn,
4340 local_merge_live);
4342 if (insn == from)
4344 fail = 1;
4345 goto out;
4347 insn = PREV_INSN (insn);
4350 if (max_to != to)
4351 fail = 1;
4353 if (pmove_upto)
4354 *pmove_upto = max_to;
4356 /* For small register class machines, don't lengthen lifetimes of
4357 hard registers before reload. */
4358 if (! reload_completed
4359 && targetm.small_register_classes_for_mode_p (VOIDmode))
4361 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4363 if (i < FIRST_PSEUDO_REGISTER
4364 && ! fixed_regs[i]
4365 && ! global_regs[i])
4367 fail = 1;
4368 break;
4373 out:
4374 BITMAP_FREE (merge_set);
4375 BITMAP_FREE (merge_use);
4376 BITMAP_FREE (local_merge_live);
4377 BITMAP_FREE (test_set);
4378 BITMAP_FREE (test_use);
4380 return !fail;
4384 /*----------------------------------------------------------------------------
4385 MULTIPLE DEFINITIONS
4387 Find the locations in the function reached by multiple definition sites
4388 for a live pseudo. In and out bitvectors are built for each basic
4389 block. They are restricted for efficiency to live registers.
4391 The gen and kill sets for the problem are obvious. Together they
4392 include all defined registers in a basic block; the gen set includes
4393 registers where a partial or conditional or may-clobber definition is
4394 last in the BB, while the kill set includes registers with a complete
4395 definition coming last. However, the computation of the dataflow
4396 itself is interesting.
4398 The idea behind it comes from SSA form's iterated dominance frontier
4399 criterion for inserting PHI functions. Just like in that case, we can use
4400 the dominance frontier to find places where multiple definitions meet;
4401 a register X defined in a basic block BB1 has multiple definitions in
4402 basic blocks in BB1's dominance frontier.
4404 So, the in-set of a basic block BB2 is not just the union of the
4405 out-sets of BB2's predecessors, but includes some more bits that come
4406 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4407 the previous paragraph). I called this set the init-set of BB2.
4409 (Note: I actually use the kill-set only to build the init-set.
4410 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4412 For example, if you have
4414 BB1 : r10 = 0
4415 r11 = 0
4416 if <...> goto BB2 else goto BB3;
4418 BB2 : r10 = 1
4419 r12 = 1
4420 goto BB3;
4422 BB3 :
4424 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4425 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4426 not need to iterate the dominance frontier, because we do not insert
4427 anything like PHI functions there! Instead, dataflow will take care of
4428 propagating the information to BB3's successors.
4429 ---------------------------------------------------------------------------*/
4431 /* Private data used to verify the solution for this problem. */
4432 struct df_md_problem_data
4434 /* An obstack for the bitmaps we need for this problem. */
4435 bitmap_obstack md_bitmaps;
4438 /* Scratch var used by transfer functions. This is used to do md analysis
4439 only for live registers. */
4440 static bitmap_head df_md_scratch;
4443 static void
4444 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4445 void *vbb_info)
4447 class df_md_bb_info *bb_info = (class df_md_bb_info *) vbb_info;
4448 if (bb_info)
4450 bitmap_clear (&bb_info->kill);
4451 bitmap_clear (&bb_info->gen);
4452 bitmap_clear (&bb_info->init);
4453 bitmap_clear (&bb_info->in);
4454 bitmap_clear (&bb_info->out);
4459 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4460 not touched unless the block is new. */
4462 static void
4463 df_md_alloc (bitmap all_blocks)
4465 unsigned int bb_index;
4466 bitmap_iterator bi;
4467 struct df_md_problem_data *problem_data;
4469 df_grow_bb_info (df_md);
4470 if (df_md->problem_data)
4471 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4472 else
4474 problem_data = XNEW (struct df_md_problem_data);
4475 df_md->problem_data = problem_data;
4476 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4478 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4480 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4482 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4483 /* When bitmaps are already initialized, just clear them. */
4484 if (bb_info->init.obstack)
4486 bitmap_clear (&bb_info->init);
4487 bitmap_clear (&bb_info->gen);
4488 bitmap_clear (&bb_info->kill);
4489 bitmap_clear (&bb_info->in);
4490 bitmap_clear (&bb_info->out);
4492 else
4494 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4495 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4496 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4497 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4498 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4502 df_md->optional_p = true;
4505 /* Add the effect of the top artificial defs of BB to the multiple definitions
4506 bitmap LOCAL_MD. */
4508 void
4509 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4511 int bb_index = bb->index;
4512 df_ref def;
4513 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4514 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4516 unsigned int dregno = DF_REF_REGNO (def);
4517 if (DF_REF_FLAGS (def)
4518 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4519 bitmap_set_bit (local_md, dregno);
4520 else
4521 bitmap_clear_bit (local_md, dregno);
4526 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4527 LOCAL_MD. */
4529 void
4530 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4531 bitmap local_md)
4533 df_ref def;
4535 FOR_EACH_INSN_DEF (def, insn)
4537 unsigned int dregno = DF_REF_REGNO (def);
4538 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4539 || (dregno >= FIRST_PSEUDO_REGISTER))
4541 if (DF_REF_FLAGS (def)
4542 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4543 bitmap_set_bit (local_md, DF_REF_ID (def));
4544 else
4545 bitmap_clear_bit (local_md, DF_REF_ID (def));
4550 static void
4551 df_md_bb_local_compute_process_def (class df_md_bb_info *bb_info,
4552 df_ref def,
4553 int top_flag)
4555 bitmap_clear (&seen_in_insn);
4557 for (; def; def = DF_REF_NEXT_LOC (def))
4559 unsigned int dregno = DF_REF_REGNO (def);
4560 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4561 || (dregno >= FIRST_PSEUDO_REGISTER))
4562 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4564 if (!bitmap_bit_p (&seen_in_insn, dregno))
4566 if (DF_REF_FLAGS (def)
4567 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4569 bitmap_set_bit (&bb_info->gen, dregno);
4570 bitmap_clear_bit (&bb_info->kill, dregno);
4572 else
4574 /* When we find a clobber and a regular def,
4575 make sure the regular def wins. */
4576 bitmap_set_bit (&seen_in_insn, dregno);
4577 bitmap_set_bit (&bb_info->kill, dregno);
4578 bitmap_clear_bit (&bb_info->gen, dregno);
4586 /* Compute local multiple def info for basic block BB. */
4588 static void
4589 df_md_bb_local_compute (unsigned int bb_index)
4591 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4592 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4593 rtx_insn *insn;
4595 /* Artificials are only hard regs. */
4596 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4597 df_md_bb_local_compute_process_def (bb_info,
4598 df_get_artificial_defs (bb_index),
4599 DF_REF_AT_TOP);
4601 FOR_BB_INSNS (bb, insn)
4603 unsigned int uid = INSN_UID (insn);
4604 if (!INSN_P (insn))
4605 continue;
4607 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4610 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4611 df_md_bb_local_compute_process_def (bb_info,
4612 df_get_artificial_defs (bb_index),
4616 /* Compute local reaching def info for each basic block within BLOCKS. */
4618 static void
4619 df_md_local_compute (bitmap all_blocks)
4621 unsigned int bb_index, df_bb_index;
4622 bitmap_iterator bi1, bi2;
4623 basic_block bb;
4624 bitmap_head *frontiers;
4626 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4628 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4630 df_md_bb_local_compute (bb_index);
4633 bitmap_release (&seen_in_insn);
4635 frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4636 FOR_ALL_BB_FN (bb, cfun)
4637 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4639 compute_dominance_frontiers (frontiers);
4641 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4642 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4644 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4645 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4647 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4648 if (bitmap_bit_p (all_blocks, df_bb_index))
4649 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4650 df_get_live_in (bb));
4654 FOR_ALL_BB_FN (bb, cfun)
4655 bitmap_clear (&frontiers[bb->index]);
4656 free (frontiers);
4660 /* Reset the global solution for recalculation. */
4662 static void
4663 df_md_reset (bitmap all_blocks)
4665 unsigned int bb_index;
4666 bitmap_iterator bi;
4668 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4670 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4671 gcc_assert (bb_info);
4672 bitmap_clear (&bb_info->in);
4673 bitmap_clear (&bb_info->out);
4677 static bool
4678 df_md_transfer_function (int bb_index)
4680 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4681 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4682 bitmap in = &bb_info->in;
4683 bitmap out = &bb_info->out;
4684 bitmap gen = &bb_info->gen;
4685 bitmap kill = &bb_info->kill;
4687 /* We need to use a scratch set here so that the value returned from this
4688 function invocation properly reflects whether the sets changed in a
4689 significant way; i.e. not just because the live set was anded in. */
4690 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4692 /* Multiple definitions of a register are not relevant if it is not
4693 live. Thus we trim the result to the places where it is live. */
4694 bitmap_and_into (in, df_get_live_in (bb));
4696 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4699 /* Initialize the solution bit vectors for problem. */
4701 static void
4702 df_md_init (bitmap all_blocks)
4704 unsigned int bb_index;
4705 bitmap_iterator bi;
4707 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4709 class df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4711 bitmap_copy (&bb_info->in, &bb_info->init);
4712 df_md_transfer_function (bb_index);
4716 static void
4717 df_md_confluence_0 (basic_block bb)
4719 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4720 bitmap_copy (&bb_info->in, &bb_info->init);
4723 /* In of target gets or of out of source. */
4725 static bool
4726 df_md_confluence_n (edge e)
4728 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4729 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4731 if (e->flags & EDGE_FAKE)
4732 return false;
4734 if (e->flags & EDGE_EH)
4736 /* Conservatively treat partially-clobbered registers as surviving
4737 across the edge; they might or might not, depending on what mode
4738 they have. */
4739 bitmap_view<HARD_REG_SET> eh_kills (eh_edge_abi.full_reg_clobbers ());
4740 return bitmap_ior_and_compl_into (op1, op2, eh_kills);
4742 else
4743 return bitmap_ior_into (op1, op2);
4746 /* Free all storage associated with the problem. */
4748 static void
4749 df_md_free (void)
4751 struct df_md_problem_data *problem_data
4752 = (struct df_md_problem_data *) df_md->problem_data;
4754 bitmap_release (&df_md_scratch);
4755 bitmap_obstack_release (&problem_data->md_bitmaps);
4756 free (problem_data);
4757 df_md->problem_data = NULL;
4759 df_md->block_info_size = 0;
4760 free (df_md->block_info);
4761 df_md->block_info = NULL;
4762 free (df_md);
4766 /* Debugging info at top of bb. */
4768 static void
4769 df_md_top_dump (basic_block bb, FILE *file)
4771 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4772 if (!bb_info)
4773 return;
4775 fprintf (file, ";; md in \t");
4776 df_print_regset (file, &bb_info->in);
4777 fprintf (file, ";; md init \t");
4778 df_print_regset (file, &bb_info->init);
4779 fprintf (file, ";; md gen \t");
4780 df_print_regset (file, &bb_info->gen);
4781 fprintf (file, ";; md kill \t");
4782 df_print_regset (file, &bb_info->kill);
4785 /* Debugging info at bottom of bb. */
4787 static void
4788 df_md_bottom_dump (basic_block bb, FILE *file)
4790 class df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4791 if (!bb_info)
4792 return;
4794 fprintf (file, ";; md out \t");
4795 df_print_regset (file, &bb_info->out);
4798 static const struct df_problem problem_MD =
4800 DF_MD, /* Problem id. */
4801 DF_FORWARD, /* Direction. */
4802 df_md_alloc, /* Allocate the problem specific data. */
4803 df_md_reset, /* Reset global information. */
4804 df_md_free_bb_info, /* Free basic block info. */
4805 df_md_local_compute, /* Local compute function. */
4806 df_md_init, /* Init the solution specific data. */
4807 df_worklist_dataflow, /* Worklist solver. */
4808 df_md_confluence_0, /* Confluence operator 0. */
4809 df_md_confluence_n, /* Confluence operator n. */
4810 df_md_transfer_function, /* Transfer function. */
4811 NULL, /* Finalize function. */
4812 df_md_free, /* Free all of the problem information. */
4813 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4814 NULL, /* Debugging. */
4815 df_md_top_dump, /* Debugging start block. */
4816 df_md_bottom_dump, /* Debugging end block. */
4817 NULL, /* Debugging start insn. */
4818 NULL, /* Debugging end insn. */
4819 NULL, /* Incremental solution verify start. */
4820 NULL, /* Incremental solution verify end. */
4821 NULL, /* Dependent problem. */
4822 sizeof (class df_md_bb_info),/* Size of entry of block_info array. */
4823 TV_DF_MD, /* Timing variable. */
4824 false /* Reset blocks on dropping out of blocks_to_analyze. */
4827 /* Create a new MD instance and add it to the existing instance
4828 of DF. */
4830 void
4831 df_md_add_problem (void)
4833 df_add_problem (&problem_MD);