Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / df-problems.c
blob331fd87f2b5548c62f5adf1f416c86975cb6fc45
1 /* Standard problems for dataflow support routines.
2 Copyright (C) 1999-2015 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 "rtl.h"
29 #include "df.h"
30 #include "tm_p.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "regs.h"
34 #include "alloc-pool.h"
35 #include "flags.h"
36 #include "cfganal.h"
37 #include "target.h"
38 #include "timevar.h"
39 #include "except.h"
40 #include "dce.h"
41 #include "valtrack.h"
42 #include "dumpfile.h"
43 #include "rtl-iter.h"
45 /* Note that turning REG_DEAD_DEBUGGING on will cause
46 gcc.c-torture/unsorted/dump-noaddr.c to fail because it prints
47 addresses in the dumps. */
48 #define REG_DEAD_DEBUGGING 0
50 #define DF_SPARSE_THRESHOLD 32
52 static bitmap_head seen_in_block;
53 static bitmap_head seen_in_insn;
55 /*----------------------------------------------------------------------------
56 Utility functions.
57 ----------------------------------------------------------------------------*/
59 /* Generic versions to get the void* version of the block info. Only
60 used inside the problem instance vectors. */
62 /* Dump a def-use or use-def chain for REF to FILE. */
64 void
65 df_chain_dump (struct df_link *link, FILE *file)
67 fprintf (file, "{ ");
68 for (; link; link = link->next)
70 fprintf (file, "%c%d(bb %d insn %d) ",
71 DF_REF_REG_DEF_P (link->ref)
72 ? 'd'
73 : (DF_REF_FLAGS (link->ref) & DF_REF_IN_NOTE) ? 'e' : 'u',
74 DF_REF_ID (link->ref),
75 DF_REF_BBNO (link->ref),
76 DF_REF_IS_ARTIFICIAL (link->ref)
77 ? -1 : DF_REF_INSN_UID (link->ref));
79 fprintf (file, "}");
83 /* Print some basic block info as part of df_dump. */
85 void
86 df_print_bb_index (basic_block bb, FILE *file)
88 edge e;
89 edge_iterator ei;
91 fprintf (file, "\n( ");
92 FOR_EACH_EDGE (e, ei, bb->preds)
94 basic_block pred = e->src;
95 fprintf (file, "%d%s ", pred->index, e->flags & EDGE_EH ? "(EH)" : "");
97 fprintf (file, ")->[%d]->( ", bb->index);
98 FOR_EACH_EDGE (e, ei, bb->succs)
100 basic_block succ = e->dest;
101 fprintf (file, "%d%s ", succ->index, e->flags & EDGE_EH ? "(EH)" : "");
103 fprintf (file, ")\n");
107 /*----------------------------------------------------------------------------
108 REACHING DEFINITIONS
110 Find the locations in the function where each definition site for a
111 pseudo reaches. In and out bitvectors are built for each basic
112 block. The id field in the ref is used to index into these sets.
113 See df.h for details.
115 If the DF_RD_PRUNE_DEAD_DEFS changeable flag is set, only DEFs reaching
116 existing uses are included in the global reaching DEFs set, or in other
117 words only DEFs that are still live. This is a kind of pruned version
118 of the traditional reaching definitions problem that is much less
119 complex to compute and produces enough information to compute UD-chains.
120 In this context, live must be interpreted in the DF_LR sense: Uses that
121 are upward exposed but maybe not initialized on all paths through the
122 CFG. For a USE that is not reached by a DEF on all paths, we still want
123 to make those DEFs that do reach the USE visible, and pruning based on
124 DF_LIVE would make that impossible.
125 ----------------------------------------------------------------------------*/
127 /* This problem plays a large number of games for the sake of
128 efficiency.
130 1) The order of the bits in the bitvectors. After the scanning
131 phase, all of the defs are sorted. All of the defs for the reg 0
132 are first, followed by all defs for reg 1 and so on.
134 2) There are two kill sets, one if the number of defs is less or
135 equal to DF_SPARSE_THRESHOLD and another if the number of defs is
136 greater.
138 <= : Data is built directly in the kill set.
140 > : One level of indirection is used to keep from generating long
141 strings of 1 bits in the kill sets. Bitvectors that are indexed
142 by the regnum are used to represent that there is a killing def
143 for the register. The confluence and transfer functions use
144 these along with the bitmap_clear_range call to remove ranges of
145 bits without actually generating a knockout vector.
147 The kill and sparse_kill and the dense_invalidated_by_call and
148 sparse_invalidated_by_call both play this game. */
150 /* Private data used to compute the solution for this problem. These
151 data structures are not accessible outside of this module. */
152 struct df_rd_problem_data
154 /* The set of defs to regs invalidated by call. */
155 bitmap_head sparse_invalidated_by_call;
156 /* The set of defs to regs invalidate by call for rd. */
157 bitmap_head dense_invalidated_by_call;
158 /* An obstack for the bitmaps we need for this problem. */
159 bitmap_obstack rd_bitmaps;
163 /* Free basic block info. */
165 static void
166 df_rd_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
167 void *vbb_info)
169 struct df_rd_bb_info *bb_info = (struct df_rd_bb_info *) vbb_info;
170 if (bb_info)
172 bitmap_clear (&bb_info->kill);
173 bitmap_clear (&bb_info->sparse_kill);
174 bitmap_clear (&bb_info->gen);
175 bitmap_clear (&bb_info->in);
176 bitmap_clear (&bb_info->out);
181 /* Allocate or reset bitmaps for DF_RD blocks. The solution bits are
182 not touched unless the block is new. */
184 static void
185 df_rd_alloc (bitmap all_blocks)
187 unsigned int bb_index;
188 bitmap_iterator bi;
189 struct df_rd_problem_data *problem_data;
191 if (df_rd->problem_data)
193 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
194 bitmap_clear (&problem_data->sparse_invalidated_by_call);
195 bitmap_clear (&problem_data->dense_invalidated_by_call);
197 else
199 problem_data = XNEW (struct df_rd_problem_data);
200 df_rd->problem_data = problem_data;
202 bitmap_obstack_initialize (&problem_data->rd_bitmaps);
203 bitmap_initialize (&problem_data->sparse_invalidated_by_call,
204 &problem_data->rd_bitmaps);
205 bitmap_initialize (&problem_data->dense_invalidated_by_call,
206 &problem_data->rd_bitmaps);
209 df_grow_bb_info (df_rd);
211 /* Because of the clustering of all use sites for the same pseudo,
212 we have to process all of the blocks before doing the analysis. */
214 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
216 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
218 /* When bitmaps are already initialized, just clear them. */
219 if (bb_info->kill.obstack)
221 bitmap_clear (&bb_info->kill);
222 bitmap_clear (&bb_info->sparse_kill);
223 bitmap_clear (&bb_info->gen);
225 else
227 bitmap_initialize (&bb_info->kill, &problem_data->rd_bitmaps);
228 bitmap_initialize (&bb_info->sparse_kill, &problem_data->rd_bitmaps);
229 bitmap_initialize (&bb_info->gen, &problem_data->rd_bitmaps);
230 bitmap_initialize (&bb_info->in, &problem_data->rd_bitmaps);
231 bitmap_initialize (&bb_info->out, &problem_data->rd_bitmaps);
234 df_rd->optional_p = true;
238 /* Add the effect of the top artificial defs of BB to the reaching definitions
239 bitmap LOCAL_RD. */
241 void
242 df_rd_simulate_artificial_defs_at_top (basic_block bb, bitmap local_rd)
244 int bb_index = bb->index;
245 df_ref def;
246 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
247 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
249 unsigned int dregno = DF_REF_REGNO (def);
250 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
251 bitmap_clear_range (local_rd,
252 DF_DEFS_BEGIN (dregno),
253 DF_DEFS_COUNT (dregno));
254 bitmap_set_bit (local_rd, DF_REF_ID (def));
258 /* Add the effect of the defs of INSN to the reaching definitions bitmap
259 LOCAL_RD. */
261 void
262 df_rd_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
263 bitmap local_rd)
265 df_ref def;
267 FOR_EACH_INSN_DEF (def, insn)
269 unsigned int dregno = DF_REF_REGNO (def);
270 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
271 || (dregno >= FIRST_PSEUDO_REGISTER))
273 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
274 bitmap_clear_range (local_rd,
275 DF_DEFS_BEGIN (dregno),
276 DF_DEFS_COUNT (dregno));
277 if (!(DF_REF_FLAGS (def)
278 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
279 bitmap_set_bit (local_rd, DF_REF_ID (def));
284 /* Process a list of DEFs for df_rd_bb_local_compute. This is a bit
285 more complicated than just simulating, because we must produce the
286 gen and kill sets and hence deal with the two possible representations
287 of kill sets. */
289 static void
290 df_rd_bb_local_compute_process_def (struct df_rd_bb_info *bb_info,
291 df_ref def,
292 int top_flag)
294 for (; def; def = DF_REF_NEXT_LOC (def))
296 if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
298 unsigned int regno = DF_REF_REGNO (def);
299 unsigned int begin = DF_DEFS_BEGIN (regno);
300 unsigned int n_defs = DF_DEFS_COUNT (regno);
302 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
303 || (regno >= FIRST_PSEUDO_REGISTER))
305 /* Only the last def(s) for a regno in the block has any
306 effect. */
307 if (!bitmap_bit_p (&seen_in_block, regno))
309 /* The first def for regno in insn gets to knock out the
310 defs from other instructions. */
311 if ((!bitmap_bit_p (&seen_in_insn, regno))
312 /* If the def is to only part of the reg, it does
313 not kill the other defs that reach here. */
314 && (!(DF_REF_FLAGS (def) &
315 (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))))
317 if (n_defs > DF_SPARSE_THRESHOLD)
319 bitmap_set_bit (&bb_info->sparse_kill, regno);
320 bitmap_clear_range (&bb_info->gen, begin, n_defs);
322 else
324 bitmap_set_range (&bb_info->kill, begin, n_defs);
325 bitmap_clear_range (&bb_info->gen, begin, n_defs);
329 bitmap_set_bit (&seen_in_insn, regno);
330 /* All defs for regno in the instruction may be put into
331 the gen set. */
332 if (!(DF_REF_FLAGS (def)
333 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
334 bitmap_set_bit (&bb_info->gen, DF_REF_ID (def));
341 /* Compute local reaching def info for basic block BB. */
343 static void
344 df_rd_bb_local_compute (unsigned int bb_index)
346 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
347 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
348 rtx_insn *insn;
350 bitmap_clear (&seen_in_block);
351 bitmap_clear (&seen_in_insn);
353 /* Artificials are only hard regs. */
354 if (!(df->changeable_flags & DF_NO_HARD_REGS))
355 df_rd_bb_local_compute_process_def (bb_info,
356 df_get_artificial_defs (bb_index),
359 FOR_BB_INSNS_REVERSE (bb, insn)
361 unsigned int uid = INSN_UID (insn);
363 if (!INSN_P (insn))
364 continue;
366 df_rd_bb_local_compute_process_def (bb_info,
367 DF_INSN_UID_DEFS (uid), 0);
369 /* This complex dance with the two bitmaps is required because
370 instructions can assign twice to the same pseudo. This
371 generally happens with calls that will have one def for the
372 result and another def for the clobber. If only one vector
373 is used and the clobber goes first, the result will be
374 lost. */
375 bitmap_ior_into (&seen_in_block, &seen_in_insn);
376 bitmap_clear (&seen_in_insn);
379 /* Process the artificial defs at the top of the block last since we
380 are going backwards through the block and these are logically at
381 the start. */
382 if (!(df->changeable_flags & DF_NO_HARD_REGS))
383 df_rd_bb_local_compute_process_def (bb_info,
384 df_get_artificial_defs (bb_index),
385 DF_REF_AT_TOP);
389 /* Compute local reaching def info for each basic block within BLOCKS. */
391 static void
392 df_rd_local_compute (bitmap all_blocks)
394 unsigned int bb_index;
395 bitmap_iterator bi;
396 unsigned int regno;
397 struct df_rd_problem_data *problem_data
398 = (struct df_rd_problem_data *) df_rd->problem_data;
399 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
400 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
402 bitmap_initialize (&seen_in_block, &df_bitmap_obstack);
403 bitmap_initialize (&seen_in_insn, &df_bitmap_obstack);
405 df_maybe_reorganize_def_refs (DF_REF_ORDER_BY_REG);
407 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
409 df_rd_bb_local_compute (bb_index);
412 /* Set up the knockout bit vectors to be applied across EH_EDGES. */
413 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, regno, bi)
415 if (! HARD_REGISTER_NUM_P (regno)
416 || !(df->changeable_flags & DF_NO_HARD_REGS))
418 if (DF_DEFS_COUNT (regno) > DF_SPARSE_THRESHOLD)
419 bitmap_set_bit (sparse_invalidated, regno);
420 else
421 bitmap_set_range (dense_invalidated,
422 DF_DEFS_BEGIN (regno),
423 DF_DEFS_COUNT (regno));
427 bitmap_clear (&seen_in_block);
428 bitmap_clear (&seen_in_insn);
432 /* Initialize the solution bit vectors for problem. */
434 static void
435 df_rd_init_solution (bitmap all_blocks)
437 unsigned int bb_index;
438 bitmap_iterator bi;
440 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
442 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
444 bitmap_copy (&bb_info->out, &bb_info->gen);
445 bitmap_clear (&bb_info->in);
449 /* In of target gets or of out of source. */
451 static bool
452 df_rd_confluence_n (edge e)
454 bitmap op1 = &df_rd_get_bb_info (e->dest->index)->in;
455 bitmap op2 = &df_rd_get_bb_info (e->src->index)->out;
456 bool changed = false;
458 if (e->flags & EDGE_FAKE)
459 return false;
461 if (e->flags & EDGE_EH)
463 struct df_rd_problem_data *problem_data
464 = (struct df_rd_problem_data *) df_rd->problem_data;
465 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
466 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
467 bitmap_iterator bi;
468 unsigned int regno;
469 bitmap_head tmp;
471 bitmap_initialize (&tmp, &df_bitmap_obstack);
472 bitmap_and_compl (&tmp, op2, dense_invalidated);
474 EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
476 bitmap_clear_range (&tmp,
477 DF_DEFS_BEGIN (regno),
478 DF_DEFS_COUNT (regno));
480 changed |= bitmap_ior_into (op1, &tmp);
481 bitmap_clear (&tmp);
482 return changed;
484 else
485 return bitmap_ior_into (op1, op2);
489 /* Transfer function. */
491 static bool
492 df_rd_transfer_function (int bb_index)
494 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
495 unsigned int regno;
496 bitmap_iterator bi;
497 bitmap in = &bb_info->in;
498 bitmap out = &bb_info->out;
499 bitmap gen = &bb_info->gen;
500 bitmap kill = &bb_info->kill;
501 bitmap sparse_kill = &bb_info->sparse_kill;
502 bool changed = false;
504 if (bitmap_empty_p (sparse_kill))
505 changed = bitmap_ior_and_compl (out, gen, in, kill);
506 else
508 struct df_rd_problem_data *problem_data;
509 bitmap_head tmp;
511 /* Note that TMP is _not_ a temporary bitmap if we end up replacing
512 OUT with TMP. Therefore, allocate TMP in the RD bitmaps obstack. */
513 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
514 bitmap_initialize (&tmp, &problem_data->rd_bitmaps);
516 bitmap_and_compl (&tmp, in, kill);
517 EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
519 bitmap_clear_range (&tmp,
520 DF_DEFS_BEGIN (regno),
521 DF_DEFS_COUNT (regno));
523 bitmap_ior_into (&tmp, gen);
524 changed = !bitmap_equal_p (&tmp, out);
525 if (changed)
527 bitmap_clear (out);
528 bb_info->out = tmp;
530 else
531 bitmap_clear (&tmp);
534 if (df->changeable_flags & DF_RD_PRUNE_DEAD_DEFS)
536 /* Create a mask of DEFs for all registers live at the end of this
537 basic block, and mask out DEFs of registers that are not live.
538 Computing the mask looks costly, but the benefit of the pruning
539 outweighs the cost. */
540 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
541 bitmap regs_live_out = &df_lr_get_bb_info (bb_index)->out;
542 bitmap live_defs = BITMAP_ALLOC (&df_bitmap_obstack);
543 unsigned int regno;
544 bitmap_iterator bi;
546 EXECUTE_IF_SET_IN_BITMAP (regs_live_out, 0, regno, bi)
547 bitmap_set_range (live_defs,
548 DF_DEFS_BEGIN (regno),
549 DF_DEFS_COUNT (regno));
550 changed |= bitmap_and_into (&bb_info->out, live_defs);
551 BITMAP_FREE (live_defs);
554 return changed;
557 /* Free all storage associated with the problem. */
559 static void
560 df_rd_free (void)
562 struct df_rd_problem_data *problem_data
563 = (struct df_rd_problem_data *) df_rd->problem_data;
565 if (problem_data)
567 bitmap_obstack_release (&problem_data->rd_bitmaps);
569 df_rd->block_info_size = 0;
570 free (df_rd->block_info);
571 df_rd->block_info = NULL;
572 free (df_rd->problem_data);
574 free (df_rd);
578 /* Debugging info. */
580 static void
581 df_rd_start_dump (FILE *file)
583 struct df_rd_problem_data *problem_data
584 = (struct df_rd_problem_data *) df_rd->problem_data;
585 unsigned int m = DF_REG_SIZE (df);
586 unsigned int regno;
588 if (!df_rd->block_info)
589 return;
591 fprintf (file, ";; Reaching defs:\n");
593 fprintf (file, ";; sparse invalidated \t");
594 dump_bitmap (file, &problem_data->sparse_invalidated_by_call);
595 fprintf (file, ";; dense invalidated \t");
596 dump_bitmap (file, &problem_data->dense_invalidated_by_call);
598 fprintf (file, ";; reg->defs[] map:\t");
599 for (regno = 0; regno < m; regno++)
600 if (DF_DEFS_COUNT (regno))
601 fprintf (file, "%d[%d,%d] ", regno,
602 DF_DEFS_BEGIN (regno),
603 DF_DEFS_BEGIN (regno) + DF_DEFS_COUNT (regno) - 1);
604 fprintf (file, "\n");
608 static void
609 df_rd_dump_defs_set (bitmap defs_set, const char *prefix, FILE *file)
611 bitmap_head tmp;
612 unsigned int regno;
613 unsigned int m = DF_REG_SIZE (df);
614 bool first_reg = true;
616 fprintf (file, "%s\t(%d) ", prefix, (int) bitmap_count_bits (defs_set));
618 bitmap_initialize (&tmp, &df_bitmap_obstack);
619 for (regno = 0; regno < m; regno++)
621 if (HARD_REGISTER_NUM_P (regno)
622 && (df->changeable_flags & DF_NO_HARD_REGS))
623 continue;
624 bitmap_set_range (&tmp, DF_DEFS_BEGIN (regno), DF_DEFS_COUNT (regno));
625 bitmap_and_into (&tmp, defs_set);
626 if (! bitmap_empty_p (&tmp))
628 bitmap_iterator bi;
629 unsigned int ix;
630 bool first_def = true;
632 if (! first_reg)
633 fprintf (file, ",");
634 first_reg = false;
636 fprintf (file, "%u[", regno);
637 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, ix, bi)
639 fprintf (file, "%s%u", first_def ? "" : ",", ix);
640 first_def = false;
642 fprintf (file, "]");
644 bitmap_clear (&tmp);
647 fprintf (file, "\n");
648 bitmap_clear (&tmp);
651 /* Debugging info at top of bb. */
653 static void
654 df_rd_top_dump (basic_block bb, FILE *file)
656 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
657 if (!bb_info)
658 return;
660 df_rd_dump_defs_set (&bb_info->in, ";; rd in ", file);
661 df_rd_dump_defs_set (&bb_info->gen, ";; rd gen ", file);
662 df_rd_dump_defs_set (&bb_info->kill, ";; rd kill", file);
666 /* Debugging info at bottom of bb. */
668 static void
669 df_rd_bottom_dump (basic_block bb, FILE *file)
671 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
672 if (!bb_info)
673 return;
675 df_rd_dump_defs_set (&bb_info->out, ";; rd out ", file);
678 /* All of the information associated with every instance of the problem. */
680 static struct df_problem problem_RD =
682 DF_RD, /* Problem id. */
683 DF_FORWARD, /* Direction. */
684 df_rd_alloc, /* Allocate the problem specific data. */
685 NULL, /* Reset global information. */
686 df_rd_free_bb_info, /* Free basic block info. */
687 df_rd_local_compute, /* Local compute function. */
688 df_rd_init_solution, /* Init the solution specific data. */
689 df_worklist_dataflow, /* Worklist solver. */
690 NULL, /* Confluence operator 0. */
691 df_rd_confluence_n, /* Confluence operator n. */
692 df_rd_transfer_function, /* Transfer function. */
693 NULL, /* Finalize function. */
694 df_rd_free, /* Free all of the problem information. */
695 df_rd_free, /* Remove this problem from the stack of dataflow problems. */
696 df_rd_start_dump, /* Debugging. */
697 df_rd_top_dump, /* Debugging start block. */
698 df_rd_bottom_dump, /* Debugging end block. */
699 NULL, /* Debugging start insn. */
700 NULL, /* Debugging end insn. */
701 NULL, /* Incremental solution verify start. */
702 NULL, /* Incremental solution verify end. */
703 NULL, /* Dependent problem. */
704 sizeof (struct df_rd_bb_info),/* Size of entry of block_info array. */
705 TV_DF_RD, /* Timing variable. */
706 true /* Reset blocks on dropping out of blocks_to_analyze. */
711 /* Create a new RD instance and add it to the existing instance
712 of DF. */
714 void
715 df_rd_add_problem (void)
717 df_add_problem (&problem_RD);
722 /*----------------------------------------------------------------------------
723 LIVE REGISTERS
725 Find the locations in the function where any use of a pseudo can
726 reach in the backwards direction. In and out bitvectors are built
727 for each basic block. The regno is used to index into these sets.
728 See df.h for details.
729 ----------------------------------------------------------------------------*/
731 /* Private data used to verify the solution for this problem. */
732 struct df_lr_problem_data
734 bitmap_head *in;
735 bitmap_head *out;
736 /* An obstack for the bitmaps we need for this problem. */
737 bitmap_obstack lr_bitmaps;
740 /* Free basic block info. */
742 static void
743 df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
744 void *vbb_info)
746 struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
747 if (bb_info)
749 bitmap_clear (&bb_info->use);
750 bitmap_clear (&bb_info->def);
751 bitmap_clear (&bb_info->in);
752 bitmap_clear (&bb_info->out);
757 /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
758 not touched unless the block is new. */
760 static void
761 df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
763 unsigned int bb_index;
764 bitmap_iterator bi;
765 struct df_lr_problem_data *problem_data;
767 df_grow_bb_info (df_lr);
768 if (df_lr->problem_data)
769 problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
770 else
772 problem_data = XNEW (struct df_lr_problem_data);
773 df_lr->problem_data = problem_data;
775 problem_data->out = NULL;
776 problem_data->in = NULL;
777 bitmap_obstack_initialize (&problem_data->lr_bitmaps);
780 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
782 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
784 /* When bitmaps are already initialized, just clear them. */
785 if (bb_info->use.obstack)
787 bitmap_clear (&bb_info->def);
788 bitmap_clear (&bb_info->use);
790 else
792 bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
793 bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
794 bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
795 bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
799 df_lr->optional_p = false;
803 /* Reset the global solution for recalculation. */
805 static void
806 df_lr_reset (bitmap all_blocks)
808 unsigned int bb_index;
809 bitmap_iterator bi;
811 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
813 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
814 gcc_assert (bb_info);
815 bitmap_clear (&bb_info->in);
816 bitmap_clear (&bb_info->out);
821 /* Compute local live register info for basic block BB. */
823 static void
824 df_lr_bb_local_compute (unsigned int bb_index)
826 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
827 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
828 rtx_insn *insn;
829 df_ref def, use;
831 /* Process the registers set in an exception handler. */
832 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
833 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
835 unsigned int dregno = DF_REF_REGNO (def);
836 bitmap_set_bit (&bb_info->def, dregno);
837 bitmap_clear_bit (&bb_info->use, dregno);
840 /* Process the hardware registers that are always live. */
841 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
842 /* Add use to set of uses in this BB. */
843 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
844 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
846 FOR_BB_INSNS_REVERSE (bb, insn)
848 if (!NONDEBUG_INSN_P (insn))
849 continue;
851 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
852 FOR_EACH_INSN_INFO_DEF (def, insn_info)
853 /* If the def is to only part of the reg, it does
854 not kill the other defs that reach here. */
855 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
857 unsigned int dregno = DF_REF_REGNO (def);
858 bitmap_set_bit (&bb_info->def, dregno);
859 bitmap_clear_bit (&bb_info->use, dregno);
862 FOR_EACH_INSN_INFO_USE (use, insn_info)
863 /* Add use to set of uses in this BB. */
864 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
867 /* Process the registers set in an exception handler or the hard
868 frame pointer if this block is the target of a non local
869 goto. */
870 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
871 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
873 unsigned int dregno = DF_REF_REGNO (def);
874 bitmap_set_bit (&bb_info->def, dregno);
875 bitmap_clear_bit (&bb_info->use, dregno);
878 #ifdef EH_USES
879 /* Process the uses that are live into an exception handler. */
880 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
881 /* Add use to set of uses in this BB. */
882 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
883 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
884 #endif
886 /* If the df_live problem is not defined, such as at -O0 and -O1, we
887 still need to keep the luids up to date. This is normally done
888 in the df_live problem since this problem has a forwards
889 scan. */
890 if (!df_live)
891 df_recompute_luids (bb);
895 /* Compute local live register info for each basic block within BLOCKS. */
897 static void
898 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
900 unsigned int bb_index, i;
901 bitmap_iterator bi;
903 bitmap_clear (&df->hardware_regs_used);
905 /* The all-important stack pointer must always be live. */
906 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
908 /* Global regs are always live, too. */
909 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
910 if (global_regs[i])
911 bitmap_set_bit (&df->hardware_regs_used, i);
913 /* Before reload, there are a few registers that must be forced
914 live everywhere -- which might not already be the case for
915 blocks within infinite loops. */
916 if (!reload_completed)
918 unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
919 /* Any reference to any pseudo before reload is a potential
920 reference of the frame pointer. */
921 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
923 /* Pseudos with argument area equivalences may require
924 reloading via the argument pointer. */
925 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
926 && fixed_regs[ARG_POINTER_REGNUM])
927 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
929 /* Any constant, or pseudo with constant equivalences, may
930 require reloading from memory using the pic register. */
931 if (pic_offset_table_regnum != INVALID_REGNUM
932 && fixed_regs[pic_offset_table_regnum])
933 bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
936 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
938 if (bb_index == EXIT_BLOCK)
940 /* The exit block is special for this problem and its bits are
941 computed from thin air. */
942 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
943 bitmap_copy (&bb_info->use, df->exit_block_uses);
945 else
946 df_lr_bb_local_compute (bb_index);
949 bitmap_clear (df_lr->out_of_date_transfer_functions);
953 /* Initialize the solution vectors. */
955 static void
956 df_lr_init (bitmap all_blocks)
958 unsigned int bb_index;
959 bitmap_iterator bi;
961 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
963 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
964 bitmap_copy (&bb_info->in, &bb_info->use);
965 bitmap_clear (&bb_info->out);
970 /* Confluence function that processes infinite loops. This might be a
971 noreturn function that throws. And even if it isn't, getting the
972 unwind info right helps debugging. */
973 static void
974 df_lr_confluence_0 (basic_block bb)
976 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
977 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
978 bitmap_copy (op1, &df->hardware_regs_used);
982 /* Confluence function that ignores fake edges. */
984 static bool
985 df_lr_confluence_n (edge e)
987 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
988 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
989 bool changed = false;
991 /* Call-clobbered registers die across exception and call edges. */
992 /* ??? Abnormal call edges ignored for the moment, as this gets
993 confused by sibling call edges, which crashes reg-stack. */
994 if (e->flags & EDGE_EH)
995 changed = bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
996 else
997 changed = bitmap_ior_into (op1, op2);
999 changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
1000 return changed;
1004 /* Transfer function. */
1006 static bool
1007 df_lr_transfer_function (int bb_index)
1009 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1010 bitmap in = &bb_info->in;
1011 bitmap out = &bb_info->out;
1012 bitmap use = &bb_info->use;
1013 bitmap def = &bb_info->def;
1015 return bitmap_ior_and_compl (in, use, out, def);
1019 /* Run the fast dce as a side effect of building LR. */
1021 static void
1022 df_lr_finalize (bitmap all_blocks)
1024 df_lr->solutions_dirty = false;
1025 if (df->changeable_flags & DF_LR_RUN_DCE)
1027 run_fast_df_dce ();
1029 /* If dce deletes some instructions, we need to recompute the lr
1030 solution before proceeding further. The problem is that fast
1031 dce is a pessimestic dataflow algorithm. In the case where
1032 it deletes a statement S inside of a loop, the uses inside of
1033 S may not be deleted from the dataflow solution because they
1034 were carried around the loop. While it is conservatively
1035 correct to leave these extra bits, the standards of df
1036 require that we maintain the best possible (least fixed
1037 point) solution. The only way to do that is to redo the
1038 iteration from the beginning. See PR35805 for an
1039 example. */
1040 if (df_lr->solutions_dirty)
1042 df_clear_flags (DF_LR_RUN_DCE);
1043 df_lr_alloc (all_blocks);
1044 df_lr_local_compute (all_blocks);
1045 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1046 df_lr_finalize (all_blocks);
1047 df_set_flags (DF_LR_RUN_DCE);
1053 /* Free all storage associated with the problem. */
1055 static void
1056 df_lr_free (void)
1058 struct df_lr_problem_data *problem_data
1059 = (struct df_lr_problem_data *) df_lr->problem_data;
1060 if (df_lr->block_info)
1063 df_lr->block_info_size = 0;
1064 free (df_lr->block_info);
1065 df_lr->block_info = NULL;
1066 bitmap_obstack_release (&problem_data->lr_bitmaps);
1067 free (df_lr->problem_data);
1068 df_lr->problem_data = NULL;
1071 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1072 free (df_lr);
1076 /* Debugging info at top of bb. */
1078 static void
1079 df_lr_top_dump (basic_block bb, FILE *file)
1081 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1082 struct df_lr_problem_data *problem_data;
1083 if (!bb_info)
1084 return;
1086 fprintf (file, ";; lr in \t");
1087 df_print_regset (file, &bb_info->in);
1088 if (df_lr->problem_data)
1090 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1091 if (problem_data->in)
1093 fprintf (file, ";; old in \t");
1094 df_print_regset (file, &problem_data->in[bb->index]);
1097 fprintf (file, ";; lr use \t");
1098 df_print_regset (file, &bb_info->use);
1099 fprintf (file, ";; lr def \t");
1100 df_print_regset (file, &bb_info->def);
1104 /* Debugging info at bottom of bb. */
1106 static void
1107 df_lr_bottom_dump (basic_block bb, FILE *file)
1109 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1110 struct df_lr_problem_data *problem_data;
1111 if (!bb_info)
1112 return;
1114 fprintf (file, ";; lr out \t");
1115 df_print_regset (file, &bb_info->out);
1116 if (df_lr->problem_data)
1118 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1119 if (problem_data->out)
1121 fprintf (file, ";; old out \t");
1122 df_print_regset (file, &problem_data->out[bb->index]);
1128 /* Build the datastructure to verify that the solution to the dataflow
1129 equations is not dirty. */
1131 static void
1132 df_lr_verify_solution_start (void)
1134 basic_block bb;
1135 struct df_lr_problem_data *problem_data;
1136 if (df_lr->solutions_dirty)
1137 return;
1139 /* Set it true so that the solution is recomputed. */
1140 df_lr->solutions_dirty = true;
1142 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1143 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1144 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1146 FOR_ALL_BB_FN (bb, cfun)
1148 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1149 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1150 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1151 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1156 /* Compare the saved datastructure and the new solution to the dataflow
1157 equations. */
1159 static void
1160 df_lr_verify_solution_end (void)
1162 struct df_lr_problem_data *problem_data;
1163 basic_block bb;
1165 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1167 if (!problem_data->out)
1168 return;
1170 if (df_lr->solutions_dirty)
1171 /* Do not check if the solution is still dirty. See the comment
1172 in df_lr_finalize for details. */
1173 df_lr->solutions_dirty = false;
1174 else
1175 FOR_ALL_BB_FN (bb, cfun)
1177 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1178 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1180 /*df_dump (stderr);*/
1181 gcc_unreachable ();
1185 /* Cannot delete them immediately because you may want to dump them
1186 if the comparison fails. */
1187 FOR_ALL_BB_FN (bb, cfun)
1189 bitmap_clear (&problem_data->in[bb->index]);
1190 bitmap_clear (&problem_data->out[bb->index]);
1193 free (problem_data->in);
1194 free (problem_data->out);
1195 problem_data->in = NULL;
1196 problem_data->out = NULL;
1200 /* All of the information associated with every instance of the problem. */
1202 static struct df_problem problem_LR =
1204 DF_LR, /* Problem id. */
1205 DF_BACKWARD, /* Direction. */
1206 df_lr_alloc, /* Allocate the problem specific data. */
1207 df_lr_reset, /* Reset global information. */
1208 df_lr_free_bb_info, /* Free basic block info. */
1209 df_lr_local_compute, /* Local compute function. */
1210 df_lr_init, /* Init the solution specific data. */
1211 df_worklist_dataflow, /* Worklist solver. */
1212 df_lr_confluence_0, /* Confluence operator 0. */
1213 df_lr_confluence_n, /* Confluence operator n. */
1214 df_lr_transfer_function, /* Transfer function. */
1215 df_lr_finalize, /* Finalize function. */
1216 df_lr_free, /* Free all of the problem information. */
1217 NULL, /* Remove this problem from the stack of dataflow problems. */
1218 NULL, /* Debugging. */
1219 df_lr_top_dump, /* Debugging start block. */
1220 df_lr_bottom_dump, /* Debugging end block. */
1221 NULL, /* Debugging start insn. */
1222 NULL, /* Debugging end insn. */
1223 df_lr_verify_solution_start,/* Incremental solution verify start. */
1224 df_lr_verify_solution_end, /* Incremental solution verify end. */
1225 NULL, /* Dependent problem. */
1226 sizeof (struct df_lr_bb_info),/* Size of entry of block_info array. */
1227 TV_DF_LR, /* Timing variable. */
1228 false /* Reset blocks on dropping out of blocks_to_analyze. */
1232 /* Create a new DATAFLOW instance and add it to an existing instance
1233 of DF. The returned structure is what is used to get at the
1234 solution. */
1236 void
1237 df_lr_add_problem (void)
1239 df_add_problem (&problem_LR);
1240 /* These will be initialized when df_scan_blocks processes each
1241 block. */
1242 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1246 /* Verify that all of the lr related info is consistent and
1247 correct. */
1249 void
1250 df_lr_verify_transfer_functions (void)
1252 basic_block bb;
1253 bitmap_head saved_def;
1254 bitmap_head saved_use;
1255 bitmap_head all_blocks;
1257 if (!df)
1258 return;
1260 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1261 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1262 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1264 FOR_ALL_BB_FN (bb, cfun)
1266 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1267 bitmap_set_bit (&all_blocks, bb->index);
1269 if (bb_info)
1271 /* Make a copy of the transfer functions and then compute
1272 new ones to see if the transfer functions have
1273 changed. */
1274 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1275 bb->index))
1277 bitmap_copy (&saved_def, &bb_info->def);
1278 bitmap_copy (&saved_use, &bb_info->use);
1279 bitmap_clear (&bb_info->def);
1280 bitmap_clear (&bb_info->use);
1282 df_lr_bb_local_compute (bb->index);
1283 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1284 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1287 else
1289 /* If we do not have basic block info, the block must be in
1290 the list of dirty blocks or else some one has added a
1291 block behind our backs. */
1292 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1293 bb->index));
1295 /* Make sure no one created a block without following
1296 procedures. */
1297 gcc_assert (df_scan_get_bb_info (bb->index));
1300 /* Make sure there are no dirty bits in blocks that have been deleted. */
1301 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1302 &all_blocks));
1304 bitmap_clear (&saved_def);
1305 bitmap_clear (&saved_use);
1306 bitmap_clear (&all_blocks);
1311 /*----------------------------------------------------------------------------
1312 LIVE AND MAY-INITIALIZED REGISTERS.
1314 This problem first computes the IN and OUT bitvectors for the
1315 may-initialized registers problems, which is a forward problem.
1316 It gives the set of registers for which we MAY have an available
1317 definition, i.e. for which there is an available definition on
1318 at least one path from the entry block to the entry/exit of a
1319 basic block. Sets generate a definition, while clobbers kill
1320 a definition.
1322 In and out bitvectors are built for each basic block and are indexed by
1323 regnum (see df.h for details). In and out bitvectors in struct
1324 df_live_bb_info actually refers to the may-initialized problem;
1326 Then, the in and out sets for the LIVE problem itself are computed.
1327 These are the logical AND of the IN and OUT sets from the LR problem
1328 and the may-initialized problem.
1329 ----------------------------------------------------------------------------*/
1331 /* Private data used to verify the solution for this problem. */
1332 struct df_live_problem_data
1334 bitmap_head *in;
1335 bitmap_head *out;
1336 /* An obstack for the bitmaps we need for this problem. */
1337 bitmap_obstack live_bitmaps;
1340 /* Scratch var used by transfer functions. This is used to implement
1341 an optimization to reduce the amount of space used to compute the
1342 combined lr and live analysis. */
1343 static bitmap_head df_live_scratch;
1346 /* Free basic block info. */
1348 static void
1349 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1350 void *vbb_info)
1352 struct df_live_bb_info *bb_info = (struct df_live_bb_info *) vbb_info;
1353 if (bb_info)
1355 bitmap_clear (&bb_info->gen);
1356 bitmap_clear (&bb_info->kill);
1357 bitmap_clear (&bb_info->in);
1358 bitmap_clear (&bb_info->out);
1363 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1364 not touched unless the block is new. */
1366 static void
1367 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1369 unsigned int bb_index;
1370 bitmap_iterator bi;
1371 struct df_live_problem_data *problem_data;
1373 if (df_live->problem_data)
1374 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1375 else
1377 problem_data = XNEW (struct df_live_problem_data);
1378 df_live->problem_data = problem_data;
1380 problem_data->out = NULL;
1381 problem_data->in = NULL;
1382 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1383 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1386 df_grow_bb_info (df_live);
1388 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1390 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1392 /* When bitmaps are already initialized, just clear them. */
1393 if (bb_info->kill.obstack)
1395 bitmap_clear (&bb_info->kill);
1396 bitmap_clear (&bb_info->gen);
1398 else
1400 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1401 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1402 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1403 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1406 df_live->optional_p = (optimize <= 1);
1410 /* Reset the global solution for recalculation. */
1412 static void
1413 df_live_reset (bitmap all_blocks)
1415 unsigned int bb_index;
1416 bitmap_iterator bi;
1418 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1420 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1421 gcc_assert (bb_info);
1422 bitmap_clear (&bb_info->in);
1423 bitmap_clear (&bb_info->out);
1428 /* Compute local uninitialized register info for basic block BB. */
1430 static void
1431 df_live_bb_local_compute (unsigned int bb_index)
1433 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1434 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1435 rtx_insn *insn;
1436 df_ref def;
1437 int luid = 0;
1439 FOR_BB_INSNS (bb, insn)
1441 unsigned int uid = INSN_UID (insn);
1442 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1444 /* Inserting labels does not always trigger the incremental
1445 rescanning. */
1446 if (!insn_info)
1448 gcc_assert (!INSN_P (insn));
1449 insn_info = df_insn_create_insn_record (insn);
1452 DF_INSN_INFO_LUID (insn_info) = luid;
1453 if (!INSN_P (insn))
1454 continue;
1456 luid++;
1457 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1459 unsigned int regno = DF_REF_REGNO (def);
1461 if (DF_REF_FLAGS_IS_SET (def,
1462 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1463 /* All partial or conditional def
1464 seen are included in the gen set. */
1465 bitmap_set_bit (&bb_info->gen, regno);
1466 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1467 /* Only must clobbers for the entire reg destroy the
1468 value. */
1469 bitmap_set_bit (&bb_info->kill, regno);
1470 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1471 bitmap_set_bit (&bb_info->gen, regno);
1475 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1476 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1480 /* Compute local uninitialized register info. */
1482 static void
1483 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1485 unsigned int bb_index;
1486 bitmap_iterator bi;
1488 df_grow_insn_info ();
1490 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1491 0, bb_index, bi)
1493 df_live_bb_local_compute (bb_index);
1496 bitmap_clear (df_live->out_of_date_transfer_functions);
1500 /* Initialize the solution vectors. */
1502 static void
1503 df_live_init (bitmap all_blocks)
1505 unsigned int bb_index;
1506 bitmap_iterator bi;
1508 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1510 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1511 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1513 /* No register may reach a location where it is not used. Thus
1514 we trim the rr result to the places where it is used. */
1515 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1516 bitmap_clear (&bb_info->in);
1520 /* Forward confluence function that ignores fake edges. */
1522 static bool
1523 df_live_confluence_n (edge e)
1525 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1526 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1528 if (e->flags & EDGE_FAKE)
1529 return false;
1531 return bitmap_ior_into (op1, op2);
1535 /* Transfer function for the forwards may-initialized problem. */
1537 static bool
1538 df_live_transfer_function (int bb_index)
1540 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1541 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1542 bitmap in = &bb_info->in;
1543 bitmap out = &bb_info->out;
1544 bitmap gen = &bb_info->gen;
1545 bitmap kill = &bb_info->kill;
1547 /* We need to use a scratch set here so that the value returned from this
1548 function invocation properly reflects whether the sets changed in a
1549 significant way; i.e. not just because the lr set was anded in. */
1550 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1551 /* No register may reach a location where it is not used. Thus
1552 we trim the rr result to the places where it is used. */
1553 bitmap_and_into (in, &bb_lr_info->in);
1555 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1559 /* And the LR info with the may-initialized registers to produce the LIVE info. */
1561 static void
1562 df_live_finalize (bitmap all_blocks)
1565 if (df_live->solutions_dirty)
1567 bitmap_iterator bi;
1568 unsigned int bb_index;
1570 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1572 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1573 struct df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1575 /* No register may reach a location where it is not used. Thus
1576 we trim the rr result to the places where it is used. */
1577 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1578 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1581 df_live->solutions_dirty = false;
1586 /* Free all storage associated with the problem. */
1588 static void
1589 df_live_free (void)
1591 struct df_live_problem_data *problem_data
1592 = (struct df_live_problem_data *) df_live->problem_data;
1593 if (df_live->block_info)
1595 df_live->block_info_size = 0;
1596 free (df_live->block_info);
1597 df_live->block_info = NULL;
1598 bitmap_clear (&df_live_scratch);
1599 bitmap_obstack_release (&problem_data->live_bitmaps);
1600 free (problem_data);
1601 df_live->problem_data = NULL;
1603 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1604 free (df_live);
1608 /* Debugging info at top of bb. */
1610 static void
1611 df_live_top_dump (basic_block bb, FILE *file)
1613 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1614 struct df_live_problem_data *problem_data;
1616 if (!bb_info)
1617 return;
1619 fprintf (file, ";; live in \t");
1620 df_print_regset (file, &bb_info->in);
1621 if (df_live->problem_data)
1623 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1624 if (problem_data->in)
1626 fprintf (file, ";; old in \t");
1627 df_print_regset (file, &problem_data->in[bb->index]);
1630 fprintf (file, ";; live gen \t");
1631 df_print_regset (file, &bb_info->gen);
1632 fprintf (file, ";; live kill\t");
1633 df_print_regset (file, &bb_info->kill);
1637 /* Debugging info at bottom of bb. */
1639 static void
1640 df_live_bottom_dump (basic_block bb, FILE *file)
1642 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1643 struct df_live_problem_data *problem_data;
1645 if (!bb_info)
1646 return;
1648 fprintf (file, ";; live out \t");
1649 df_print_regset (file, &bb_info->out);
1650 if (df_live->problem_data)
1652 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1653 if (problem_data->out)
1655 fprintf (file, ";; old out \t");
1656 df_print_regset (file, &problem_data->out[bb->index]);
1662 /* Build the datastructure to verify that the solution to the dataflow
1663 equations is not dirty. */
1665 static void
1666 df_live_verify_solution_start (void)
1668 basic_block bb;
1669 struct df_live_problem_data *problem_data;
1670 if (df_live->solutions_dirty)
1671 return;
1673 /* Set it true so that the solution is recomputed. */
1674 df_live->solutions_dirty = true;
1676 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1677 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1678 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1680 FOR_ALL_BB_FN (bb, cfun)
1682 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1683 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1684 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1685 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1690 /* Compare the saved datastructure and the new solution to the dataflow
1691 equations. */
1693 static void
1694 df_live_verify_solution_end (void)
1696 struct df_live_problem_data *problem_data;
1697 basic_block bb;
1699 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1700 if (!problem_data->out)
1701 return;
1703 FOR_ALL_BB_FN (bb, cfun)
1705 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1706 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1708 /*df_dump (stderr);*/
1709 gcc_unreachable ();
1713 /* Cannot delete them immediately because you may want to dump them
1714 if the comparison fails. */
1715 FOR_ALL_BB_FN (bb, cfun)
1717 bitmap_clear (&problem_data->in[bb->index]);
1718 bitmap_clear (&problem_data->out[bb->index]);
1721 free (problem_data->in);
1722 free (problem_data->out);
1723 free (problem_data);
1724 df_live->problem_data = NULL;
1728 /* All of the information associated with every instance of the problem. */
1730 static struct df_problem problem_LIVE =
1732 DF_LIVE, /* Problem id. */
1733 DF_FORWARD, /* Direction. */
1734 df_live_alloc, /* Allocate the problem specific data. */
1735 df_live_reset, /* Reset global information. */
1736 df_live_free_bb_info, /* Free basic block info. */
1737 df_live_local_compute, /* Local compute function. */
1738 df_live_init, /* Init the solution specific data. */
1739 df_worklist_dataflow, /* Worklist solver. */
1740 NULL, /* Confluence operator 0. */
1741 df_live_confluence_n, /* Confluence operator n. */
1742 df_live_transfer_function, /* Transfer function. */
1743 df_live_finalize, /* Finalize function. */
1744 df_live_free, /* Free all of the problem information. */
1745 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1746 NULL, /* Debugging. */
1747 df_live_top_dump, /* Debugging start block. */
1748 df_live_bottom_dump, /* Debugging end block. */
1749 NULL, /* Debugging start insn. */
1750 NULL, /* Debugging end insn. */
1751 df_live_verify_solution_start,/* Incremental solution verify start. */
1752 df_live_verify_solution_end, /* Incremental solution verify end. */
1753 &problem_LR, /* Dependent problem. */
1754 sizeof (struct df_live_bb_info),/* Size of entry of block_info array. */
1755 TV_DF_LIVE, /* Timing variable. */
1756 false /* Reset blocks on dropping out of blocks_to_analyze. */
1760 /* Create a new DATAFLOW instance and add it to an existing instance
1761 of DF. The returned structure is what is used to get at the
1762 solution. */
1764 void
1765 df_live_add_problem (void)
1767 df_add_problem (&problem_LIVE);
1768 /* These will be initialized when df_scan_blocks processes each
1769 block. */
1770 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1774 /* Set all of the blocks as dirty. This needs to be done if this
1775 problem is added after all of the insns have been scanned. */
1777 void
1778 df_live_set_all_dirty (void)
1780 basic_block bb;
1781 FOR_ALL_BB_FN (bb, cfun)
1782 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1783 bb->index);
1787 /* Verify that all of the lr related info is consistent and
1788 correct. */
1790 void
1791 df_live_verify_transfer_functions (void)
1793 basic_block bb;
1794 bitmap_head saved_gen;
1795 bitmap_head saved_kill;
1796 bitmap_head all_blocks;
1798 if (!df)
1799 return;
1801 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1802 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1803 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1805 df_grow_insn_info ();
1807 FOR_ALL_BB_FN (bb, cfun)
1809 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1810 bitmap_set_bit (&all_blocks, bb->index);
1812 if (bb_info)
1814 /* Make a copy of the transfer functions and then compute
1815 new ones to see if the transfer functions have
1816 changed. */
1817 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1818 bb->index))
1820 bitmap_copy (&saved_gen, &bb_info->gen);
1821 bitmap_copy (&saved_kill, &bb_info->kill);
1822 bitmap_clear (&bb_info->gen);
1823 bitmap_clear (&bb_info->kill);
1825 df_live_bb_local_compute (bb->index);
1826 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1827 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1830 else
1832 /* If we do not have basic block info, the block must be in
1833 the list of dirty blocks or else some one has added a
1834 block behind our backs. */
1835 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1836 bb->index));
1838 /* Make sure no one created a block without following
1839 procedures. */
1840 gcc_assert (df_scan_get_bb_info (bb->index));
1843 /* Make sure there are no dirty bits in blocks that have been deleted. */
1844 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1845 &all_blocks));
1846 bitmap_clear (&saved_gen);
1847 bitmap_clear (&saved_kill);
1848 bitmap_clear (&all_blocks);
1851 /*----------------------------------------------------------------------------
1852 MUST-INITIALIZED REGISTERS.
1853 ----------------------------------------------------------------------------*/
1855 /* Private data used to verify the solution for this problem. */
1856 struct df_mir_problem_data
1858 bitmap_head *in;
1859 bitmap_head *out;
1860 /* An obstack for the bitmaps we need for this problem. */
1861 bitmap_obstack mir_bitmaps;
1865 /* Free basic block info. */
1867 static void
1868 df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1869 void *vbb_info)
1871 struct df_mir_bb_info *bb_info = (struct df_mir_bb_info *) vbb_info;
1872 if (bb_info)
1874 bitmap_clear (&bb_info->gen);
1875 bitmap_clear (&bb_info->kill);
1876 bitmap_clear (&bb_info->in);
1877 bitmap_clear (&bb_info->out);
1882 /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1883 not touched unless the block is new. */
1885 static void
1886 df_mir_alloc (bitmap all_blocks)
1888 unsigned int bb_index;
1889 bitmap_iterator bi;
1890 struct df_mir_problem_data *problem_data;
1892 if (df_mir->problem_data)
1893 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1894 else
1896 problem_data = XNEW (struct df_mir_problem_data);
1897 df_mir->problem_data = problem_data;
1899 problem_data->out = NULL;
1900 problem_data->in = NULL;
1901 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1904 df_grow_bb_info (df_mir);
1906 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1908 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1910 /* When bitmaps are already initialized, just clear them. */
1911 if (bb_info->kill.obstack)
1913 bitmap_clear (&bb_info->kill);
1914 bitmap_clear (&bb_info->gen);
1916 else
1918 bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1919 bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1920 bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1921 bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1922 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1923 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1927 df_mir->optional_p = 1;
1931 /* Reset the global solution for recalculation. */
1933 static void
1934 df_mir_reset (bitmap all_blocks)
1936 unsigned int bb_index;
1937 bitmap_iterator bi;
1939 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1941 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1943 gcc_assert (bb_info);
1945 bitmap_clear (&bb_info->in);
1946 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1947 bitmap_clear (&bb_info->out);
1948 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1953 /* Compute local uninitialized register info for basic block BB. */
1955 static void
1956 df_mir_bb_local_compute (unsigned int bb_index)
1958 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1959 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1960 rtx_insn *insn;
1961 int luid = 0;
1963 /* Ignoring artificial defs is intentional: these often pretend that some
1964 registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
1965 though they are not used for that. As a result, conservatively assume
1966 they may be uninitialized. */
1968 FOR_BB_INSNS (bb, insn)
1970 unsigned int uid = INSN_UID (insn);
1971 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1973 /* Inserting labels does not always trigger the incremental
1974 rescanning. */
1975 if (!insn_info)
1977 gcc_assert (!INSN_P (insn));
1978 insn_info = df_insn_create_insn_record (insn);
1981 DF_INSN_INFO_LUID (insn_info) = luid;
1982 if (!INSN_P (insn))
1983 continue;
1985 luid++;
1986 df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
1991 /* Compute local uninitialized register info. */
1993 static void
1994 df_mir_local_compute (bitmap all_blocks)
1996 unsigned int bb_index;
1997 bitmap_iterator bi;
1999 df_grow_insn_info ();
2001 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2003 df_mir_bb_local_compute (bb_index);
2008 /* Initialize the solution vectors. */
2010 static void
2011 df_mir_init (bitmap all_blocks)
2013 df_mir_reset (all_blocks);
2017 /* Initialize IN sets for blocks with no predecessors: when landing on such
2018 blocks, assume all registers are uninitialized. */
2020 static void
2021 df_mir_confluence_0 (basic_block bb)
2023 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2025 bitmap_clear (&bb_info->in);
2029 /* Forward confluence function that ignores fake edges. */
2031 static bool
2032 df_mir_confluence_n (edge e)
2034 bitmap op1 = &df_mir_get_bb_info (e->dest->index)->in;
2035 bitmap op2 = &df_mir_get_bb_info (e->src->index)->out;
2037 if (e->flags & EDGE_FAKE)
2038 return false;
2040 /* A register is must-initialized at the entry of a basic block iff it is
2041 must-initialized at the exit of all the predecessors. */
2042 return bitmap_and_into (op1, op2);
2046 /* Transfer function for the forwards must-initialized problem. */
2048 static bool
2049 df_mir_transfer_function (int bb_index)
2051 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2052 bitmap in = &bb_info->in;
2053 bitmap out = &bb_info->out;
2054 bitmap gen = &bb_info->gen;
2055 bitmap kill = &bb_info->kill;
2057 return bitmap_ior_and_compl (out, gen, in, kill);
2061 /* Free all storage associated with the problem. */
2063 static void
2064 df_mir_free (void)
2066 struct df_mir_problem_data *problem_data
2067 = (struct df_mir_problem_data *) df_mir->problem_data;
2068 if (df_mir->block_info)
2070 df_mir->block_info_size = 0;
2071 free (df_mir->block_info);
2072 df_mir->block_info = NULL;
2073 bitmap_obstack_release (&problem_data->mir_bitmaps);
2074 free (problem_data);
2075 df_mir->problem_data = NULL;
2077 free (df_mir);
2081 /* Debugging info at top of bb. */
2083 static void
2084 df_mir_top_dump (basic_block bb, FILE *file)
2086 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2088 if (!bb_info)
2089 return;
2091 fprintf (file, ";; mir in \t");
2092 df_print_regset (file, &bb_info->in);
2093 fprintf (file, ";; mir kill\t");
2094 df_print_regset (file, &bb_info->kill);
2095 fprintf (file, ";; mir gen \t");
2096 df_print_regset (file, &bb_info->gen);
2099 /* Debugging info at bottom of bb. */
2101 static void
2102 df_mir_bottom_dump (basic_block bb, FILE *file)
2104 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2106 if (!bb_info)
2107 return;
2109 fprintf (file, ";; mir out \t");
2110 df_print_regset (file, &bb_info->out);
2114 /* Build the datastructure to verify that the solution to the dataflow
2115 equations is not dirty. */
2117 static void
2118 df_mir_verify_solution_start (void)
2120 basic_block bb;
2121 struct df_mir_problem_data *problem_data;
2122 if (df_mir->solutions_dirty)
2123 return;
2125 /* Set it true so that the solution is recomputed. */
2126 df_mir->solutions_dirty = true;
2128 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2129 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2130 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2131 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2133 FOR_ALL_BB_FN (bb, cfun)
2135 bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2136 bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2137 bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2138 bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2143 /* Compare the saved datastructure and the new solution to the dataflow
2144 equations. */
2146 static void
2147 df_mir_verify_solution_end (void)
2149 struct df_mir_problem_data *problem_data;
2150 basic_block bb;
2152 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2153 if (!problem_data->out)
2154 return;
2156 FOR_ALL_BB_FN (bb, cfun)
2158 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2159 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2160 gcc_unreachable ();
2163 /* Cannot delete them immediately because you may want to dump them
2164 if the comparison fails. */
2165 FOR_ALL_BB_FN (bb, cfun)
2167 bitmap_clear (&problem_data->in[bb->index]);
2168 bitmap_clear (&problem_data->out[bb->index]);
2171 free (problem_data->in);
2172 free (problem_data->out);
2173 bitmap_obstack_release (&problem_data->mir_bitmaps);
2174 free (problem_data);
2175 df_mir->problem_data = NULL;
2179 /* All of the information associated with every instance of the problem. */
2181 static struct df_problem problem_MIR =
2183 DF_MIR, /* Problem id. */
2184 DF_FORWARD, /* Direction. */
2185 df_mir_alloc, /* Allocate the problem specific data. */
2186 df_mir_reset, /* Reset global information. */
2187 df_mir_free_bb_info, /* Free basic block info. */
2188 df_mir_local_compute, /* Local compute function. */
2189 df_mir_init, /* Init the solution specific data. */
2190 df_worklist_dataflow, /* Worklist solver. */
2191 df_mir_confluence_0, /* Confluence operator 0. */
2192 df_mir_confluence_n, /* Confluence operator n. */
2193 df_mir_transfer_function, /* Transfer function. */
2194 NULL, /* Finalize function. */
2195 df_mir_free, /* Free all of the problem information. */
2196 df_mir_free, /* Remove this problem from the stack of dataflow problems. */
2197 NULL, /* Debugging. */
2198 df_mir_top_dump, /* Debugging start block. */
2199 df_mir_bottom_dump, /* Debugging end block. */
2200 NULL, /* Debugging start insn. */
2201 NULL, /* Debugging end insn. */
2202 df_mir_verify_solution_start, /* Incremental solution verify start. */
2203 df_mir_verify_solution_end, /* Incremental solution verify end. */
2204 NULL, /* Dependent problem. */
2205 sizeof (struct df_mir_bb_info),/* Size of entry of block_info array. */
2206 TV_DF_MIR, /* Timing variable. */
2207 false /* Reset blocks on dropping out of blocks_to_analyze. */
2211 /* Create a new DATAFLOW instance and add it to an existing instance
2212 of DF. */
2214 void
2215 df_mir_add_problem (void)
2217 df_add_problem (&problem_MIR);
2218 /* These will be initialized when df_scan_blocks processes each
2219 block. */
2220 df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2224 /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps. */
2226 void
2227 df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2228 bitmap kill, bitmap gen)
2230 df_ref def;
2232 FOR_EACH_INSN_DEF (def, insn)
2234 unsigned int regno = DF_REF_REGNO (def);
2236 /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2237 previous gen is irrelevant (and reciprocally). Also, claim that a
2238 register is GEN only if it is in all cases. */
2239 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2241 bitmap_set_bit (kill, regno);
2242 bitmap_clear_bit (gen, regno);
2244 /* In the worst case, partial and conditional defs can leave bits
2245 uninitialized, so assume they do not change anything. */
2246 else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2248 bitmap_set_bit (gen, regno);
2249 bitmap_clear_bit (kill, regno);
2254 /*----------------------------------------------------------------------------
2255 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2257 Link either the defs to the uses and / or the uses to the defs.
2259 These problems are set up like the other dataflow problems so that
2260 they nicely fit into the framework. They are much simpler and only
2261 involve a single traversal of instructions and an examination of
2262 the reaching defs information (the dependent problem).
2263 ----------------------------------------------------------------------------*/
2265 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2267 /* Create a du or ud chain from SRC to DST and link it into SRC. */
2269 struct df_link *
2270 df_chain_create (df_ref src, df_ref dst)
2272 struct df_link *head = DF_REF_CHAIN (src);
2273 struct df_link *link = df_chain->block_pool->allocate ();
2275 DF_REF_CHAIN (src) = link;
2276 link->next = head;
2277 link->ref = dst;
2278 return link;
2282 /* Delete any du or ud chains that start at REF and point to
2283 TARGET. */
2284 static void
2285 df_chain_unlink_1 (df_ref ref, df_ref target)
2287 struct df_link *chain = DF_REF_CHAIN (ref);
2288 struct df_link *prev = NULL;
2290 while (chain)
2292 if (chain->ref == target)
2294 if (prev)
2295 prev->next = chain->next;
2296 else
2297 DF_REF_CHAIN (ref) = chain->next;
2298 df_chain->block_pool->remove (chain);
2299 return;
2301 prev = chain;
2302 chain = chain->next;
2307 /* Delete a du or ud chain that leave or point to REF. */
2309 void
2310 df_chain_unlink (df_ref ref)
2312 struct df_link *chain = DF_REF_CHAIN (ref);
2313 while (chain)
2315 struct df_link *next = chain->next;
2316 /* Delete the other side if it exists. */
2317 df_chain_unlink_1 (chain->ref, ref);
2318 df_chain->block_pool->remove (chain);
2319 chain = next;
2321 DF_REF_CHAIN (ref) = NULL;
2325 /* Copy the du or ud chain starting at FROM_REF and attach it to
2326 TO_REF. */
2328 void
2329 df_chain_copy (df_ref to_ref,
2330 struct df_link *from_ref)
2332 while (from_ref)
2334 df_chain_create (to_ref, from_ref->ref);
2335 from_ref = from_ref->next;
2340 /* Remove this problem from the stack of dataflow problems. */
2342 static void
2343 df_chain_remove_problem (void)
2345 bitmap_iterator bi;
2346 unsigned int bb_index;
2348 /* Wholesale destruction of the old chains. */
2349 if (df_chain->block_pool)
2350 delete df_chain->block_pool;
2352 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2354 rtx_insn *insn;
2355 df_ref def, use;
2356 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2358 if (df_chain_problem_p (DF_DU_CHAIN))
2359 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2360 DF_REF_CHAIN (def) = NULL;
2361 if (df_chain_problem_p (DF_UD_CHAIN))
2362 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2363 DF_REF_CHAIN (use) = NULL;
2365 FOR_BB_INSNS (bb, insn)
2366 if (INSN_P (insn))
2368 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2369 if (df_chain_problem_p (DF_DU_CHAIN))
2370 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2371 DF_REF_CHAIN (def) = NULL;
2372 if (df_chain_problem_p (DF_UD_CHAIN))
2374 FOR_EACH_INSN_INFO_USE (use, insn_info)
2375 DF_REF_CHAIN (use) = NULL;
2376 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2377 DF_REF_CHAIN (use) = NULL;
2382 bitmap_clear (df_chain->out_of_date_transfer_functions);
2383 df_chain->block_pool = NULL;
2387 /* Remove the chain problem completely. */
2389 static void
2390 df_chain_fully_remove_problem (void)
2392 df_chain_remove_problem ();
2393 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2394 free (df_chain);
2398 /* Create def-use or use-def chains. */
2400 static void
2401 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2403 df_chain_remove_problem ();
2404 df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2405 df_chain->optional_p = true;
2409 /* Reset all of the chains when the set of basic blocks changes. */
2411 static void
2412 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2414 df_chain_remove_problem ();
2418 /* Create the chains for a list of USEs. */
2420 static void
2421 df_chain_create_bb_process_use (bitmap local_rd,
2422 df_ref use,
2423 int top_flag)
2425 bitmap_iterator bi;
2426 unsigned int def_index;
2428 for (; use; use = DF_REF_NEXT_LOC (use))
2430 unsigned int uregno = DF_REF_REGNO (use);
2431 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2432 || (uregno >= FIRST_PSEUDO_REGISTER))
2434 /* Do not want to go through this for an uninitialized var. */
2435 int count = DF_DEFS_COUNT (uregno);
2436 if (count)
2438 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2440 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2441 unsigned int last_index = first_index + count - 1;
2443 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2445 df_ref def;
2446 if (def_index > last_index)
2447 break;
2449 def = DF_DEFS_GET (def_index);
2450 if (df_chain_problem_p (DF_DU_CHAIN))
2451 df_chain_create (def, use);
2452 if (df_chain_problem_p (DF_UD_CHAIN))
2453 df_chain_create (use, def);
2462 /* Create chains from reaching defs bitmaps for basic block BB. */
2464 static void
2465 df_chain_create_bb (unsigned int bb_index)
2467 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2468 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2469 rtx_insn *insn;
2470 bitmap_head cpy;
2472 bitmap_initialize (&cpy, &bitmap_default_obstack);
2473 bitmap_copy (&cpy, &bb_info->in);
2474 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2476 /* Since we are going forwards, process the artificial uses first
2477 then the artificial defs second. */
2479 #ifdef EH_USES
2480 /* Create the chains for the artificial uses from the EH_USES at the
2481 beginning of the block. */
2483 /* Artificials are only hard regs. */
2484 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2485 df_chain_create_bb_process_use (&cpy,
2486 df_get_artificial_uses (bb->index),
2487 DF_REF_AT_TOP);
2488 #endif
2490 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2492 /* Process the regular instructions next. */
2493 FOR_BB_INSNS (bb, insn)
2494 if (INSN_P (insn))
2496 unsigned int uid = INSN_UID (insn);
2498 /* First scan the uses and link them up with the defs that remain
2499 in the cpy vector. */
2500 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2501 if (df->changeable_flags & DF_EQ_NOTES)
2502 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2504 /* Since we are going forwards, process the defs second. */
2505 df_rd_simulate_one_insn (bb, insn, &cpy);
2508 /* Create the chains for the artificial uses of the hard registers
2509 at the end of the block. */
2510 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2511 df_chain_create_bb_process_use (&cpy,
2512 df_get_artificial_uses (bb->index),
2515 bitmap_clear (&cpy);
2518 /* Create def-use chains from reaching use bitmaps for basic blocks
2519 in BLOCKS. */
2521 static void
2522 df_chain_finalize (bitmap all_blocks)
2524 unsigned int bb_index;
2525 bitmap_iterator bi;
2527 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2529 df_chain_create_bb (bb_index);
2534 /* Free all storage associated with the problem. */
2536 static void
2537 df_chain_free (void)
2539 delete df_chain->block_pool;
2540 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2541 free (df_chain);
2545 /* Debugging info. */
2547 static void
2548 df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2550 /* Artificials are only hard regs. */
2551 if (df->changeable_flags & DF_NO_HARD_REGS)
2552 return;
2553 if (df_chain_problem_p (DF_UD_CHAIN))
2555 df_ref use;
2557 fprintf (file,
2558 ";; UD chains for artificial uses at %s\n",
2559 top ? "top" : "bottom");
2560 FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2561 if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2562 || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2564 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2565 df_chain_dump (DF_REF_CHAIN (use), file);
2566 fprintf (file, "\n");
2569 if (df_chain_problem_p (DF_DU_CHAIN))
2571 df_ref def;
2573 fprintf (file,
2574 ";; DU chains for artificial defs at %s\n",
2575 top ? "top" : "bottom");
2576 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2577 if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2578 || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2580 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2581 df_chain_dump (DF_REF_CHAIN (def), file);
2582 fprintf (file, "\n");
2587 static void
2588 df_chain_top_dump (basic_block bb, FILE *file)
2590 df_chain_bb_dump (bb, file, /*top=*/true);
2593 static void
2594 df_chain_bottom_dump (basic_block bb, FILE *file)
2596 df_chain_bb_dump (bb, file, /*top=*/false);
2599 static void
2600 df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2602 if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2604 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2605 df_ref use;
2607 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2608 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2609 FOR_EACH_INSN_INFO_USE (use, insn_info)
2610 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2611 || !(df->changeable_flags & DF_NO_HARD_REGS))
2613 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2614 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2615 fprintf (file, "read/write ");
2616 df_chain_dump (DF_REF_CHAIN (use), file);
2617 fprintf (file, "\n");
2619 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2620 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2621 || !(df->changeable_flags & DF_NO_HARD_REGS))
2623 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2624 df_chain_dump (DF_REF_CHAIN (use), file);
2625 fprintf (file, "\n");
2630 static void
2631 df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2633 if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2635 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2636 df_ref def;
2637 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2638 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2639 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2640 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2641 || !(df->changeable_flags & DF_NO_HARD_REGS))
2643 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2644 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2645 fprintf (file, "read/write ");
2646 df_chain_dump (DF_REF_CHAIN (def), file);
2647 fprintf (file, "\n");
2649 fprintf (file, "\n");
2653 static struct df_problem problem_CHAIN =
2655 DF_CHAIN, /* Problem id. */
2656 DF_NONE, /* Direction. */
2657 df_chain_alloc, /* Allocate the problem specific data. */
2658 df_chain_reset, /* Reset global information. */
2659 NULL, /* Free basic block info. */
2660 NULL, /* Local compute function. */
2661 NULL, /* Init the solution specific data. */
2662 NULL, /* Iterative solver. */
2663 NULL, /* Confluence operator 0. */
2664 NULL, /* Confluence operator n. */
2665 NULL, /* Transfer function. */
2666 df_chain_finalize, /* Finalize function. */
2667 df_chain_free, /* Free all of the problem information. */
2668 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2669 NULL, /* Debugging. */
2670 df_chain_top_dump, /* Debugging start block. */
2671 df_chain_bottom_dump, /* Debugging end block. */
2672 df_chain_insn_top_dump, /* Debugging start insn. */
2673 df_chain_insn_bottom_dump, /* Debugging end insn. */
2674 NULL, /* Incremental solution verify start. */
2675 NULL, /* Incremental solution verify end. */
2676 &problem_RD, /* Dependent problem. */
2677 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
2678 TV_DF_CHAIN, /* Timing variable. */
2679 false /* Reset blocks on dropping out of blocks_to_analyze. */
2683 /* Create a new DATAFLOW instance and add it to an existing instance
2684 of DF. The returned structure is what is used to get at the
2685 solution. */
2687 void
2688 df_chain_add_problem (unsigned int chain_flags)
2690 df_add_problem (&problem_CHAIN);
2691 df_chain->local_flags = chain_flags;
2692 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2695 #undef df_chain_problem_p
2698 /*----------------------------------------------------------------------------
2699 WORD LEVEL LIVE REGISTERS
2701 Find the locations in the function where any use of a pseudo can
2702 reach in the backwards direction. In and out bitvectors are built
2703 for each basic block. We only track pseudo registers that have a
2704 size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2705 contain two bits corresponding to each of the subwords.
2707 ----------------------------------------------------------------------------*/
2709 /* Private data used to verify the solution for this problem. */
2710 struct df_word_lr_problem_data
2712 /* An obstack for the bitmaps we need for this problem. */
2713 bitmap_obstack word_lr_bitmaps;
2717 /* Free basic block info. */
2719 static void
2720 df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2721 void *vbb_info)
2723 struct df_word_lr_bb_info *bb_info = (struct df_word_lr_bb_info *) vbb_info;
2724 if (bb_info)
2726 bitmap_clear (&bb_info->use);
2727 bitmap_clear (&bb_info->def);
2728 bitmap_clear (&bb_info->in);
2729 bitmap_clear (&bb_info->out);
2734 /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2735 not touched unless the block is new. */
2737 static void
2738 df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2740 unsigned int bb_index;
2741 bitmap_iterator bi;
2742 basic_block bb;
2743 struct df_word_lr_problem_data *problem_data
2744 = XNEW (struct df_word_lr_problem_data);
2746 df_word_lr->problem_data = problem_data;
2748 df_grow_bb_info (df_word_lr);
2750 /* Create the mapping from regnos to slots. This does not change
2751 unless the problem is destroyed and recreated. In particular, if
2752 we end up deleting the only insn that used a subreg, we do not
2753 want to redo the mapping because this would invalidate everything
2754 else. */
2756 bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2758 FOR_EACH_BB_FN (bb, cfun)
2759 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2761 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2762 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2764 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2766 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2768 /* When bitmaps are already initialized, just clear them. */
2769 if (bb_info->use.obstack)
2771 bitmap_clear (&bb_info->def);
2772 bitmap_clear (&bb_info->use);
2774 else
2776 bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2777 bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2778 bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2779 bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2783 df_word_lr->optional_p = true;
2787 /* Reset the global solution for recalculation. */
2789 static void
2790 df_word_lr_reset (bitmap all_blocks)
2792 unsigned int bb_index;
2793 bitmap_iterator bi;
2795 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2797 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2798 gcc_assert (bb_info);
2799 bitmap_clear (&bb_info->in);
2800 bitmap_clear (&bb_info->out);
2804 /* Examine REF, and if it is for a reg we're interested in, set or
2805 clear the bits corresponding to its subwords from the bitmap
2806 according to IS_SET. LIVE is the bitmap we should update. We do
2807 not track hard regs or pseudos of any size other than 2 *
2808 UNITS_PER_WORD.
2809 We return true if we changed the bitmap, or if we encountered a register
2810 we're not tracking. */
2812 bool
2813 df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2815 rtx orig_reg = DF_REF_REG (ref);
2816 rtx reg = orig_reg;
2817 machine_mode reg_mode;
2818 unsigned regno;
2819 /* Left at -1 for whole accesses. */
2820 int which_subword = -1;
2821 bool changed = false;
2823 if (GET_CODE (reg) == SUBREG)
2824 reg = SUBREG_REG (orig_reg);
2825 regno = REGNO (reg);
2826 reg_mode = GET_MODE (reg);
2827 if (regno < FIRST_PSEUDO_REGISTER
2828 || GET_MODE_SIZE (reg_mode) != 2 * UNITS_PER_WORD)
2829 return true;
2831 if (GET_CODE (orig_reg) == SUBREG
2832 && df_read_modify_subreg_p (orig_reg))
2834 gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2835 if (subreg_lowpart_p (orig_reg))
2836 which_subword = 0;
2837 else
2838 which_subword = 1;
2840 if (is_set)
2842 if (which_subword != 1)
2843 changed |= bitmap_set_bit (live, regno * 2);
2844 if (which_subword != 0)
2845 changed |= bitmap_set_bit (live, regno * 2 + 1);
2847 else
2849 if (which_subword != 1)
2850 changed |= bitmap_clear_bit (live, regno * 2);
2851 if (which_subword != 0)
2852 changed |= bitmap_clear_bit (live, regno * 2 + 1);
2854 return changed;
2857 /* Compute local live register info for basic block BB. */
2859 static void
2860 df_word_lr_bb_local_compute (unsigned int bb_index)
2862 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2863 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2864 rtx_insn *insn;
2865 df_ref def, use;
2867 /* Ensure that artificial refs don't contain references to pseudos. */
2868 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2869 gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2871 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2872 gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2874 FOR_BB_INSNS_REVERSE (bb, insn)
2876 if (!NONDEBUG_INSN_P (insn))
2877 continue;
2879 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2880 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2881 /* If the def is to only part of the reg, it does
2882 not kill the other defs that reach here. */
2883 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2885 df_word_lr_mark_ref (def, true, &bb_info->def);
2886 df_word_lr_mark_ref (def, false, &bb_info->use);
2888 FOR_EACH_INSN_INFO_USE (use, insn_info)
2889 df_word_lr_mark_ref (use, true, &bb_info->use);
2894 /* Compute local live register info for each basic block within BLOCKS. */
2896 static void
2897 df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2899 unsigned int bb_index;
2900 bitmap_iterator bi;
2902 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2904 if (bb_index == EXIT_BLOCK)
2906 unsigned regno;
2907 bitmap_iterator bi;
2908 EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2909 regno, bi)
2910 gcc_unreachable ();
2912 else
2913 df_word_lr_bb_local_compute (bb_index);
2916 bitmap_clear (df_word_lr->out_of_date_transfer_functions);
2920 /* Initialize the solution vectors. */
2922 static void
2923 df_word_lr_init (bitmap all_blocks)
2925 unsigned int bb_index;
2926 bitmap_iterator bi;
2928 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2930 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2931 bitmap_copy (&bb_info->in, &bb_info->use);
2932 bitmap_clear (&bb_info->out);
2937 /* Confluence function that ignores fake edges. */
2939 static bool
2940 df_word_lr_confluence_n (edge e)
2942 bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
2943 bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
2945 return bitmap_ior_into (op1, op2);
2949 /* Transfer function. */
2951 static bool
2952 df_word_lr_transfer_function (int bb_index)
2954 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2955 bitmap in = &bb_info->in;
2956 bitmap out = &bb_info->out;
2957 bitmap use = &bb_info->use;
2958 bitmap def = &bb_info->def;
2960 return bitmap_ior_and_compl (in, use, out, def);
2964 /* Free all storage associated with the problem. */
2966 static void
2967 df_word_lr_free (void)
2969 struct df_word_lr_problem_data *problem_data
2970 = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
2972 if (df_word_lr->block_info)
2974 df_word_lr->block_info_size = 0;
2975 free (df_word_lr->block_info);
2976 df_word_lr->block_info = NULL;
2979 BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
2980 bitmap_obstack_release (&problem_data->word_lr_bitmaps);
2981 free (problem_data);
2982 free (df_word_lr);
2986 /* Debugging info at top of bb. */
2988 static void
2989 df_word_lr_top_dump (basic_block bb, FILE *file)
2991 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
2992 if (!bb_info)
2993 return;
2995 fprintf (file, ";; blr in \t");
2996 df_print_word_regset (file, &bb_info->in);
2997 fprintf (file, ";; blr use \t");
2998 df_print_word_regset (file, &bb_info->use);
2999 fprintf (file, ";; blr def \t");
3000 df_print_word_regset (file, &bb_info->def);
3004 /* Debugging info at bottom of bb. */
3006 static void
3007 df_word_lr_bottom_dump (basic_block bb, FILE *file)
3009 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3010 if (!bb_info)
3011 return;
3013 fprintf (file, ";; blr out \t");
3014 df_print_word_regset (file, &bb_info->out);
3018 /* All of the information associated with every instance of the problem. */
3020 static struct df_problem problem_WORD_LR =
3022 DF_WORD_LR, /* Problem id. */
3023 DF_BACKWARD, /* Direction. */
3024 df_word_lr_alloc, /* Allocate the problem specific data. */
3025 df_word_lr_reset, /* Reset global information. */
3026 df_word_lr_free_bb_info, /* Free basic block info. */
3027 df_word_lr_local_compute, /* Local compute function. */
3028 df_word_lr_init, /* Init the solution specific data. */
3029 df_worklist_dataflow, /* Worklist solver. */
3030 NULL, /* Confluence operator 0. */
3031 df_word_lr_confluence_n, /* Confluence operator n. */
3032 df_word_lr_transfer_function, /* Transfer function. */
3033 NULL, /* Finalize function. */
3034 df_word_lr_free, /* Free all of the problem information. */
3035 df_word_lr_free, /* Remove this problem from the stack of dataflow problems. */
3036 NULL, /* Debugging. */
3037 df_word_lr_top_dump, /* Debugging start block. */
3038 df_word_lr_bottom_dump, /* Debugging end block. */
3039 NULL, /* Debugging start insn. */
3040 NULL, /* Debugging end insn. */
3041 NULL, /* Incremental solution verify start. */
3042 NULL, /* Incremental solution verify end. */
3043 NULL, /* Dependent problem. */
3044 sizeof (struct df_word_lr_bb_info),/* Size of entry of block_info array. */
3045 TV_DF_WORD_LR, /* Timing variable. */
3046 false /* Reset blocks on dropping out of blocks_to_analyze. */
3050 /* Create a new DATAFLOW instance and add it to an existing instance
3051 of DF. The returned structure is what is used to get at the
3052 solution. */
3054 void
3055 df_word_lr_add_problem (void)
3057 df_add_problem (&problem_WORD_LR);
3058 /* These will be initialized when df_scan_blocks processes each
3059 block. */
3060 df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3064 /* Simulate the effects of the defs of INSN on LIVE. Return true if we changed
3065 any bits, which is used by the caller to determine whether a set is
3066 necessary. We also return true if there are other reasons not to delete
3067 an insn. */
3069 bool
3070 df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3072 bool changed = false;
3073 df_ref def;
3075 FOR_EACH_INSN_DEF (def, insn)
3076 if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3077 changed = true;
3078 else
3079 changed |= df_word_lr_mark_ref (def, false, live);
3080 return changed;
3084 /* Simulate the effects of the uses of INSN on LIVE. */
3086 void
3087 df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3089 df_ref use;
3091 FOR_EACH_INSN_USE (use, insn)
3092 df_word_lr_mark_ref (use, true, live);
3095 /*----------------------------------------------------------------------------
3096 This problem computes REG_DEAD and REG_UNUSED notes.
3097 ----------------------------------------------------------------------------*/
3099 static void
3100 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3102 df_note->optional_p = true;
3105 /* This is only used if REG_DEAD_DEBUGGING is in effect. */
3106 static void
3107 df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3109 if (dump_file)
3111 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3112 print_rtl (dump_file, note);
3113 fprintf (dump_file, "\n");
3118 /* After reg-stack, the x86 floating point stack regs are difficult to
3119 analyze because of all of the pushes, pops and rotations. Thus, we
3120 just leave the notes alone. */
3122 #ifdef STACK_REGS
3123 static inline bool
3124 df_ignore_stack_reg (int regno)
3126 return regstack_completed
3127 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3129 #else
3130 static inline bool
3131 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3133 return false;
3135 #endif
3138 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN. */
3140 static void
3141 df_remove_dead_and_unused_notes (rtx_insn *insn)
3143 rtx *pprev = &REG_NOTES (insn);
3144 rtx link = *pprev;
3146 while (link)
3148 switch (REG_NOTE_KIND (link))
3150 case REG_DEAD:
3151 /* After reg-stack, we need to ignore any unused notes
3152 for the stack registers. */
3153 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3155 pprev = &XEXP (link, 1);
3156 link = *pprev;
3158 else
3160 rtx next = XEXP (link, 1);
3161 if (REG_DEAD_DEBUGGING)
3162 df_print_note ("deleting: ", insn, link);
3163 free_EXPR_LIST_node (link);
3164 *pprev = link = next;
3166 break;
3168 case REG_UNUSED:
3169 /* After reg-stack, we need to ignore any unused notes
3170 for the stack registers. */
3171 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3173 pprev = &XEXP (link, 1);
3174 link = *pprev;
3176 else
3178 rtx next = XEXP (link, 1);
3179 if (REG_DEAD_DEBUGGING)
3180 df_print_note ("deleting: ", insn, link);
3181 free_EXPR_LIST_node (link);
3182 *pprev = link = next;
3184 break;
3186 default:
3187 pprev = &XEXP (link, 1);
3188 link = *pprev;
3189 break;
3194 /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3195 as the bitmap of currently live registers. */
3197 static void
3198 df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3200 rtx *pprev = &REG_NOTES (insn);
3201 rtx link = *pprev;
3203 while (link)
3205 switch (REG_NOTE_KIND (link))
3207 case REG_EQUAL:
3208 case REG_EQUIV:
3210 /* Remove the notes that refer to dead registers. As we have at most
3211 one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3212 so we need to purge the complete EQ_USES vector when removing
3213 the note using df_notes_rescan. */
3214 df_ref use;
3215 bool deleted = false;
3217 FOR_EACH_INSN_EQ_USE (use, insn)
3218 if (DF_REF_REGNO (use) > FIRST_PSEUDO_REGISTER
3219 && DF_REF_LOC (use)
3220 && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3221 && !bitmap_bit_p (live, DF_REF_REGNO (use))
3222 && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3224 deleted = true;
3225 break;
3227 if (deleted)
3229 rtx next;
3230 if (REG_DEAD_DEBUGGING)
3231 df_print_note ("deleting: ", insn, link);
3232 next = XEXP (link, 1);
3233 free_EXPR_LIST_node (link);
3234 *pprev = link = next;
3235 df_notes_rescan (insn);
3237 else
3239 pprev = &XEXP (link, 1);
3240 link = *pprev;
3242 break;
3245 default:
3246 pprev = &XEXP (link, 1);
3247 link = *pprev;
3248 break;
3253 /* Set a NOTE_TYPE note for REG in INSN. */
3255 static inline void
3256 df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3258 gcc_checking_assert (!DEBUG_INSN_P (insn));
3259 add_reg_note (insn, note_type, reg);
3262 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3263 arguments. Return true if the register value described by MWS's
3264 mw_reg is known to be completely unused, and if mw_reg can therefore
3265 be used in a REG_UNUSED note. */
3267 static bool
3268 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3269 bitmap live, bitmap artificial_uses)
3271 unsigned int r;
3273 /* If MWS describes a partial reference, create REG_UNUSED notes for
3274 individual hard registers. */
3275 if (mws->flags & DF_REF_PARTIAL)
3276 return false;
3278 /* Likewise if some part of the register is used. */
3279 for (r = mws->start_regno; r <= mws->end_regno; r++)
3280 if (bitmap_bit_p (live, r)
3281 || bitmap_bit_p (artificial_uses, r))
3282 return false;
3284 gcc_assert (REG_P (mws->mw_reg));
3285 return true;
3289 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3290 based on the bits in LIVE. Do not generate notes for registers in
3291 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3292 not generated if the reg is both read and written by the
3293 instruction.
3296 static void
3297 df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3298 bitmap live, bitmap do_not_gen,
3299 bitmap artificial_uses,
3300 struct dead_debug_local *debug)
3302 unsigned int r;
3304 if (REG_DEAD_DEBUGGING && dump_file)
3305 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3306 mws->start_regno, mws->end_regno);
3308 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3310 unsigned int regno = mws->start_regno;
3311 df_set_note (REG_UNUSED, insn, mws->mw_reg);
3312 dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3314 if (REG_DEAD_DEBUGGING)
3315 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3317 bitmap_set_bit (do_not_gen, regno);
3318 /* Only do this if the value is totally dead. */
3320 else
3321 for (r = mws->start_regno; r <= mws->end_regno; r++)
3323 if (!bitmap_bit_p (live, r)
3324 && !bitmap_bit_p (artificial_uses, r))
3326 df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3327 dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3328 if (REG_DEAD_DEBUGGING)
3329 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3331 bitmap_set_bit (do_not_gen, r);
3336 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3337 arguments. Return true if the register value described by MWS's
3338 mw_reg is known to be completely dead, and if mw_reg can therefore
3339 be used in a REG_DEAD note. */
3341 static bool
3342 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3343 bitmap live, bitmap artificial_uses,
3344 bitmap do_not_gen)
3346 unsigned int r;
3348 /* If MWS describes a partial reference, create REG_DEAD notes for
3349 individual hard registers. */
3350 if (mws->flags & DF_REF_PARTIAL)
3351 return false;
3353 /* Likewise if some part of the register is not dead. */
3354 for (r = mws->start_regno; r <= mws->end_regno; r++)
3355 if (bitmap_bit_p (live, r)
3356 || bitmap_bit_p (artificial_uses, r)
3357 || bitmap_bit_p (do_not_gen, r))
3358 return false;
3360 gcc_assert (REG_P (mws->mw_reg));
3361 return true;
3364 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3365 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3366 from being set if the instruction both reads and writes the
3367 register. */
3369 static void
3370 df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3371 bitmap live, bitmap do_not_gen,
3372 bitmap artificial_uses, bool *added_notes_p)
3374 unsigned int r;
3375 bool is_debug = *added_notes_p;
3377 *added_notes_p = false;
3379 if (REG_DEAD_DEBUGGING && dump_file)
3381 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3382 mws->start_regno, mws->end_regno);
3383 df_print_regset (dump_file, do_not_gen);
3384 fprintf (dump_file, " live =");
3385 df_print_regset (dump_file, live);
3386 fprintf (dump_file, " artificial uses =");
3387 df_print_regset (dump_file, artificial_uses);
3390 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3392 if (is_debug)
3394 *added_notes_p = true;
3395 return;
3397 /* Add a dead note for the entire multi word register. */
3398 df_set_note (REG_DEAD, insn, mws->mw_reg);
3399 if (REG_DEAD_DEBUGGING)
3400 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3402 else
3404 for (r = mws->start_regno; r <= mws->end_regno; r++)
3405 if (!bitmap_bit_p (live, r)
3406 && !bitmap_bit_p (artificial_uses, r)
3407 && !bitmap_bit_p (do_not_gen, r))
3409 if (is_debug)
3411 *added_notes_p = true;
3412 return;
3414 df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3415 if (REG_DEAD_DEBUGGING)
3416 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3419 return;
3423 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3424 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3426 static void
3427 df_create_unused_note (rtx_insn *insn, df_ref def,
3428 bitmap live, bitmap artificial_uses,
3429 struct dead_debug_local *debug)
3431 unsigned int dregno = DF_REF_REGNO (def);
3433 if (REG_DEAD_DEBUGGING && dump_file)
3435 fprintf (dump_file, " regular looking at def ");
3436 df_ref_debug (def, dump_file);
3439 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3440 || bitmap_bit_p (live, dregno)
3441 || bitmap_bit_p (artificial_uses, dregno)
3442 || df_ignore_stack_reg (dregno)))
3444 rtx reg = (DF_REF_LOC (def))
3445 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3446 df_set_note (REG_UNUSED, insn, reg);
3447 dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3448 if (REG_DEAD_DEBUGGING)
3449 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3452 return;
3456 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3457 info: lifetime, bb, and number of defs and uses for basic block
3458 BB. The three bitvectors are scratch regs used here. */
3460 static void
3461 df_note_bb_compute (unsigned int bb_index,
3462 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3464 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3465 rtx_insn *insn;
3466 df_ref def, use;
3467 struct dead_debug_local debug;
3469 dead_debug_local_init (&debug, NULL, NULL);
3471 bitmap_copy (live, df_get_live_out (bb));
3472 bitmap_clear (artificial_uses);
3474 if (REG_DEAD_DEBUGGING && dump_file)
3476 fprintf (dump_file, "live at bottom ");
3477 df_print_regset (dump_file, live);
3480 /* Process the artificial defs and uses at the bottom of the block
3481 to begin processing. */
3482 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3484 if (REG_DEAD_DEBUGGING && dump_file)
3485 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3487 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3488 bitmap_clear_bit (live, DF_REF_REGNO (def));
3491 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3492 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3494 unsigned int regno = DF_REF_REGNO (use);
3495 bitmap_set_bit (live, regno);
3497 /* Notes are not generated for any of the artificial registers
3498 at the bottom of the block. */
3499 bitmap_set_bit (artificial_uses, regno);
3502 if (REG_DEAD_DEBUGGING && dump_file)
3504 fprintf (dump_file, "live before artificials out ");
3505 df_print_regset (dump_file, live);
3508 FOR_BB_INSNS_REVERSE (bb, insn)
3510 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3511 df_mw_hardreg *mw;
3512 int debug_insn;
3514 if (!INSN_P (insn))
3515 continue;
3517 debug_insn = DEBUG_INSN_P (insn);
3519 bitmap_clear (do_not_gen);
3520 df_remove_dead_and_unused_notes (insn);
3522 /* Process the defs. */
3523 if (CALL_P (insn))
3525 if (REG_DEAD_DEBUGGING && dump_file)
3527 fprintf (dump_file, "processing call %d\n live =",
3528 INSN_UID (insn));
3529 df_print_regset (dump_file, live);
3532 /* We only care about real sets for calls. Clobbers cannot
3533 be depended on to really die. */
3534 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3535 if ((DF_MWS_REG_DEF_P (mw))
3536 && !df_ignore_stack_reg (mw->start_regno))
3537 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3538 artificial_uses, &debug);
3540 /* All of the defs except the return value are some sort of
3541 clobber. This code is for the return. */
3542 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3544 unsigned int dregno = DF_REF_REGNO (def);
3545 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3547 df_create_unused_note (insn,
3548 def, live, artificial_uses, &debug);
3549 bitmap_set_bit (do_not_gen, dregno);
3552 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3553 bitmap_clear_bit (live, dregno);
3556 else
3558 /* Regular insn. */
3559 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3560 if (DF_MWS_REG_DEF_P (mw))
3561 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3562 artificial_uses, &debug);
3564 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3566 unsigned int dregno = DF_REF_REGNO (def);
3567 df_create_unused_note (insn,
3568 def, live, artificial_uses, &debug);
3570 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3571 bitmap_set_bit (do_not_gen, dregno);
3573 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3574 bitmap_clear_bit (live, dregno);
3578 /* Process the uses. */
3579 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3580 if (DF_MWS_REG_USE_P (mw)
3581 && !df_ignore_stack_reg (mw->start_regno))
3583 bool really_add_notes = debug_insn != 0;
3585 df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3586 artificial_uses,
3587 &really_add_notes);
3589 if (really_add_notes)
3590 debug_insn = -1;
3593 FOR_EACH_INSN_INFO_USE (use, insn_info)
3595 unsigned int uregno = DF_REF_REGNO (use);
3597 if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3599 fprintf (dump_file, " regular looking at use ");
3600 df_ref_debug (use, dump_file);
3603 if (!bitmap_bit_p (live, uregno))
3605 if (debug_insn)
3607 if (debug_insn > 0)
3609 /* We won't add REG_UNUSED or REG_DEAD notes for
3610 these, so we don't have to mess with them in
3611 debug insns either. */
3612 if (!bitmap_bit_p (artificial_uses, uregno)
3613 && !df_ignore_stack_reg (uregno))
3614 dead_debug_add (&debug, use, uregno);
3615 continue;
3617 break;
3619 else
3620 dead_debug_insert_temp (&debug, uregno, insn,
3621 DEBUG_TEMP_BEFORE_WITH_REG);
3623 if ( (!(DF_REF_FLAGS (use)
3624 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3625 && (!bitmap_bit_p (do_not_gen, uregno))
3626 && (!bitmap_bit_p (artificial_uses, uregno))
3627 && (!df_ignore_stack_reg (uregno)))
3629 rtx reg = (DF_REF_LOC (use))
3630 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3631 df_set_note (REG_DEAD, insn, reg);
3633 if (REG_DEAD_DEBUGGING)
3634 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3636 /* This register is now live. */
3637 bitmap_set_bit (live, uregno);
3641 df_remove_dead_eq_notes (insn, live);
3643 if (debug_insn == -1)
3645 /* ??? We could probably do better here, replacing dead
3646 registers with their definitions. */
3647 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3648 df_insn_rescan_debug_internal (insn);
3652 dead_debug_local_finish (&debug, NULL);
3656 /* Compute register info: lifetime, bb, and number of defs and uses. */
3657 static void
3658 df_note_compute (bitmap all_blocks)
3660 unsigned int bb_index;
3661 bitmap_iterator bi;
3662 bitmap_head live, do_not_gen, artificial_uses;
3664 bitmap_initialize (&live, &df_bitmap_obstack);
3665 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3666 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3668 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3670 /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3671 pseudos in debug insns because we don't always (re)visit blocks
3672 with death points after visiting dead uses. Even changing this
3673 loop to postorder would still leave room for visiting a death
3674 point before visiting a subsequent debug use. */
3675 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3678 bitmap_clear (&live);
3679 bitmap_clear (&do_not_gen);
3680 bitmap_clear (&artificial_uses);
3684 /* Free all storage associated with the problem. */
3686 static void
3687 df_note_free (void)
3689 free (df_note);
3693 /* All of the information associated every instance of the problem. */
3695 static struct df_problem problem_NOTE =
3697 DF_NOTE, /* Problem id. */
3698 DF_NONE, /* Direction. */
3699 df_note_alloc, /* Allocate the problem specific data. */
3700 NULL, /* Reset global information. */
3701 NULL, /* Free basic block info. */
3702 df_note_compute, /* Local compute function. */
3703 NULL, /* Init the solution specific data. */
3704 NULL, /* Iterative solver. */
3705 NULL, /* Confluence operator 0. */
3706 NULL, /* Confluence operator n. */
3707 NULL, /* Transfer function. */
3708 NULL, /* Finalize function. */
3709 df_note_free, /* Free all of the problem information. */
3710 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3711 NULL, /* Debugging. */
3712 NULL, /* Debugging start block. */
3713 NULL, /* Debugging end block. */
3714 NULL, /* Debugging start insn. */
3715 NULL, /* Debugging end insn. */
3716 NULL, /* Incremental solution verify start. */
3717 NULL, /* Incremental solution verify end. */
3718 &problem_LR, /* Dependent problem. */
3719 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
3720 TV_DF_NOTE, /* Timing variable. */
3721 false /* Reset blocks on dropping out of blocks_to_analyze. */
3725 /* Create a new DATAFLOW instance and add it to an existing instance
3726 of DF. The returned structure is what is used to get at the
3727 solution. */
3729 void
3730 df_note_add_problem (void)
3732 df_add_problem (&problem_NOTE);
3738 /*----------------------------------------------------------------------------
3739 Functions for simulating the effects of single insns.
3741 You can either simulate in the forwards direction, starting from
3742 the top of a block or the backwards direction from the end of the
3743 block. If you go backwards, defs are examined first to clear bits,
3744 then uses are examined to set bits. If you go forwards, defs are
3745 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3746 are examined to clear bits. In either case, the result of examining
3747 a def can be undone (respectively by a use or a REG_UNUSED note).
3749 If you start at the top of the block, use one of DF_LIVE_IN or
3750 DF_LR_IN. If you start at the bottom of the block use one of
3751 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3752 THEY WILL BE DESTROYED.
3753 ----------------------------------------------------------------------------*/
3756 /* Find the set of DEFs for INSN. */
3758 void
3759 df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3761 df_ref def;
3763 FOR_EACH_INSN_DEF (def, insn)
3764 bitmap_set_bit (defs, DF_REF_REGNO (def));
3767 /* Find the set of uses for INSN. This includes partial defs. */
3769 static void
3770 df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3772 df_ref def, use;
3773 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3775 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3776 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3777 bitmap_set_bit (uses, DF_REF_REGNO (def));
3778 FOR_EACH_INSN_INFO_USE (use, insn_info)
3779 bitmap_set_bit (uses, DF_REF_REGNO (use));
3782 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3784 void
3785 df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3787 df_ref def;
3789 FOR_EACH_INSN_DEF (def, insn)
3790 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3791 bitmap_set_bit (defs, DF_REF_REGNO (def));
3795 /* Simulate the effects of the defs of INSN on LIVE. */
3797 void
3798 df_simulate_defs (rtx_insn *insn, bitmap live)
3800 df_ref def;
3802 FOR_EACH_INSN_DEF (def, insn)
3804 unsigned int dregno = DF_REF_REGNO (def);
3806 /* If the def is to only part of the reg, it does
3807 not kill the other defs that reach here. */
3808 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3809 bitmap_clear_bit (live, dregno);
3814 /* Simulate the effects of the uses of INSN on LIVE. */
3816 void
3817 df_simulate_uses (rtx_insn *insn, bitmap live)
3819 df_ref use;
3821 if (DEBUG_INSN_P (insn))
3822 return;
3824 FOR_EACH_INSN_USE (use, insn)
3825 /* Add use to set of uses in this BB. */
3826 bitmap_set_bit (live, DF_REF_REGNO (use));
3830 /* Add back the always live regs in BB to LIVE. */
3832 static inline void
3833 df_simulate_fixup_sets (basic_block bb, bitmap live)
3835 /* These regs are considered always live so if they end up dying
3836 because of some def, we need to bring the back again. */
3837 if (bb_has_eh_pred (bb))
3838 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3839 else
3840 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3844 /*----------------------------------------------------------------------------
3845 The following three functions are used only for BACKWARDS scanning:
3846 i.e. they process the defs before the uses.
3848 df_simulate_initialize_backwards should be called first with a
3849 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
3850 df_simulate_one_insn_backwards should be called for each insn in
3851 the block, starting with the last one. Finally,
3852 df_simulate_finalize_backwards can be called to get a new value
3853 of the sets at the top of the block (this is rarely used).
3854 ----------------------------------------------------------------------------*/
3856 /* Apply the artificial uses and defs at the end of BB in a backwards
3857 direction. */
3859 void
3860 df_simulate_initialize_backwards (basic_block bb, bitmap live)
3862 df_ref def, use;
3863 int bb_index = bb->index;
3865 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3866 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3867 bitmap_clear_bit (live, DF_REF_REGNO (def));
3869 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3870 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3871 bitmap_set_bit (live, DF_REF_REGNO (use));
3875 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
3877 void
3878 df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3880 if (!NONDEBUG_INSN_P (insn))
3881 return;
3883 df_simulate_defs (insn, live);
3884 df_simulate_uses (insn, live);
3885 df_simulate_fixup_sets (bb, live);
3889 /* Apply the artificial uses and defs at the top of BB in a backwards
3890 direction. */
3892 void
3893 df_simulate_finalize_backwards (basic_block bb, bitmap live)
3895 df_ref def;
3896 #ifdef EH_USES
3897 df_ref use;
3898 #endif
3899 int bb_index = bb->index;
3901 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3902 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3903 bitmap_clear_bit (live, DF_REF_REGNO (def));
3905 #ifdef EH_USES
3906 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3907 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3908 bitmap_set_bit (live, DF_REF_REGNO (use));
3909 #endif
3911 /*----------------------------------------------------------------------------
3912 The following three functions are used only for FORWARDS scanning:
3913 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
3914 Thus it is important to add the DF_NOTES problem to the stack of
3915 problems computed before using these functions.
3917 df_simulate_initialize_forwards should be called first with a
3918 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
3919 df_simulate_one_insn_forwards should be called for each insn in
3920 the block, starting with the first one.
3921 ----------------------------------------------------------------------------*/
3923 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
3924 DF_LR_IN for basic block BB, for forward scanning by marking artificial
3925 defs live. */
3927 void
3928 df_simulate_initialize_forwards (basic_block bb, bitmap live)
3930 df_ref def;
3931 int bb_index = bb->index;
3933 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3934 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3935 bitmap_set_bit (live, DF_REF_REGNO (def));
3938 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
3940 void
3941 df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
3943 rtx link;
3944 if (! INSN_P (insn))
3945 return;
3947 /* Make sure that DF_NOTE really is an active df problem. */
3948 gcc_assert (df_note);
3950 /* Note that this is the opposite as how the problem is defined, because
3951 in the LR problem defs _kill_ liveness. However, they do so backwards,
3952 while here the scan is performed forwards! So, first assume that the
3953 def is live, and if this is not true REG_UNUSED notes will rectify the
3954 situation. */
3955 df_simulate_find_noclobber_defs (insn, live);
3957 /* Clear all of the registers that go dead. */
3958 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3960 switch (REG_NOTE_KIND (link))
3962 case REG_DEAD:
3963 case REG_UNUSED:
3965 rtx reg = XEXP (link, 0);
3966 bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
3968 break;
3969 default:
3970 break;
3973 df_simulate_fixup_sets (bb, live);
3976 /* Used by the next two functions to encode information about the
3977 memory references we found. */
3978 #define MEMREF_NORMAL 1
3979 #define MEMREF_VOLATILE 2
3981 /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X. */
3983 static int
3984 find_memory (rtx_insn *insn)
3986 int flags = 0;
3987 subrtx_iterator::array_type array;
3988 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3990 const_rtx x = *iter;
3991 if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
3992 flags |= MEMREF_VOLATILE;
3993 else if (MEM_P (x))
3995 if (MEM_VOLATILE_P (x))
3996 flags |= MEMREF_VOLATILE;
3997 else if (!MEM_READONLY_P (x))
3998 flags |= MEMREF_NORMAL;
4001 return flags;
4004 /* A subroutine of can_move_insns_across_p called through note_stores.
4005 DATA points to an integer in which we set either the bit for
4006 MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
4007 of either kind. */
4009 static void
4010 find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4011 void *data ATTRIBUTE_UNUSED)
4013 int *pflags = (int *)data;
4014 if (GET_CODE (x) == SUBREG)
4015 x = XEXP (x, 0);
4016 /* Treat stores to SP as stores to memory, this will prevent problems
4017 when there are references to the stack frame. */
4018 if (x == stack_pointer_rtx)
4019 *pflags |= MEMREF_VOLATILE;
4020 if (!MEM_P (x))
4021 return;
4022 *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4025 /* Scan BB backwards, using df_simulate functions to keep track of
4026 lifetimes, up to insn POINT. The result is stored in LIVE. */
4028 void
4029 simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4031 rtx_insn *insn;
4032 bitmap_copy (live, df_get_live_out (bb));
4033 df_simulate_initialize_backwards (bb, live);
4035 /* Scan and update life information until we reach the point we're
4036 interested in. */
4037 for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4038 df_simulate_one_insn_backwards (bb, insn, live);
4041 /* Return true if it is safe to move a group of insns, described by
4042 the range FROM to TO, backwards across another group of insns,
4043 described by ACROSS_FROM to ACROSS_TO. It is assumed that there
4044 are no insns between ACROSS_TO and FROM, but they may be in
4045 different basic blocks; MERGE_BB is the block from which the
4046 insns will be moved. The caller must pass in a regset MERGE_LIVE
4047 which specifies the registers live after TO.
4049 This function may be called in one of two cases: either we try to
4050 move identical instructions from all successor blocks into their
4051 predecessor, or we try to move from only one successor block. If
4052 OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4053 the second case. It should contain a set of registers live at the
4054 end of ACROSS_TO which must not be clobbered by moving the insns.
4055 In that case, we're also more careful about moving memory references
4056 and trapping insns.
4058 We return false if it is not safe to move the entire group, but it
4059 may still be possible to move a subgroup. PMOVE_UPTO, if nonnull,
4060 is set to point at the last moveable insn in such a case. */
4062 bool
4063 can_move_insns_across (rtx_insn *from, rtx_insn *to,
4064 rtx_insn *across_from, rtx_insn *across_to,
4065 basic_block merge_bb, regset merge_live,
4066 regset other_branch_live, rtx_insn **pmove_upto)
4068 rtx_insn *insn, *next, *max_to;
4069 bitmap merge_set, merge_use, local_merge_live;
4070 bitmap test_set, test_use;
4071 unsigned i, fail = 0;
4072 bitmap_iterator bi;
4073 int memrefs_in_across = 0;
4074 int mem_sets_in_across = 0;
4075 bool trapping_insns_in_across = false;
4077 if (pmove_upto != NULL)
4078 *pmove_upto = NULL;
4080 /* Find real bounds, ignoring debug insns. */
4081 while (!NONDEBUG_INSN_P (from) && from != to)
4082 from = NEXT_INSN (from);
4083 while (!NONDEBUG_INSN_P (to) && from != to)
4084 to = PREV_INSN (to);
4086 for (insn = across_to; ; insn = next)
4088 if (CALL_P (insn))
4090 if (RTL_CONST_OR_PURE_CALL_P (insn))
4091 /* Pure functions can read from memory. Const functions can
4092 read from arguments that the ABI has forced onto the stack.
4093 Neither sort of read can be volatile. */
4094 memrefs_in_across |= MEMREF_NORMAL;
4095 else
4097 memrefs_in_across |= MEMREF_VOLATILE;
4098 mem_sets_in_across |= MEMREF_VOLATILE;
4101 if (NONDEBUG_INSN_P (insn))
4103 if (volatile_insn_p (PATTERN (insn)))
4104 return false;
4105 memrefs_in_across |= find_memory (insn);
4106 note_stores (PATTERN (insn), find_memory_stores,
4107 &mem_sets_in_across);
4108 /* This is used just to find sets of the stack pointer. */
4109 memrefs_in_across |= mem_sets_in_across;
4110 trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4112 next = PREV_INSN (insn);
4113 if (insn == across_from)
4114 break;
4117 /* Collect:
4118 MERGE_SET = set of registers set in MERGE_BB
4119 MERGE_USE = set of registers used in MERGE_BB and live at its top
4120 MERGE_LIVE = set of registers live at the point inside the MERGE
4121 range that we've reached during scanning
4122 TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4123 TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4124 and live before ACROSS_FROM. */
4126 merge_set = BITMAP_ALLOC (&reg_obstack);
4127 merge_use = BITMAP_ALLOC (&reg_obstack);
4128 local_merge_live = BITMAP_ALLOC (&reg_obstack);
4129 test_set = BITMAP_ALLOC (&reg_obstack);
4130 test_use = BITMAP_ALLOC (&reg_obstack);
4132 /* Compute the set of registers set and used in the ACROSS range. */
4133 if (other_branch_live != NULL)
4134 bitmap_copy (test_use, other_branch_live);
4135 df_simulate_initialize_backwards (merge_bb, test_use);
4136 for (insn = across_to; ; insn = next)
4138 if (NONDEBUG_INSN_P (insn))
4140 df_simulate_find_defs (insn, test_set);
4141 df_simulate_defs (insn, test_use);
4142 df_simulate_uses (insn, test_use);
4144 next = PREV_INSN (insn);
4145 if (insn == across_from)
4146 break;
4149 /* Compute an upper bound for the amount of insns moved, by finding
4150 the first insn in MERGE that sets a register in TEST_USE, or uses
4151 a register in TEST_SET. We also check for calls, trapping operations,
4152 and memory references. */
4153 max_to = NULL;
4154 for (insn = from; ; insn = next)
4156 if (CALL_P (insn))
4157 break;
4158 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4159 break;
4160 if (NONDEBUG_INSN_P (insn))
4162 if (may_trap_or_fault_p (PATTERN (insn))
4163 && (trapping_insns_in_across
4164 || other_branch_live != NULL
4165 || volatile_insn_p (PATTERN (insn))))
4166 break;
4168 /* We cannot move memory stores past each other, or move memory
4169 reads past stores, at least not without tracking them and
4170 calling true_dependence on every pair.
4172 If there is no other branch and no memory references or
4173 sets in the ACROSS range, we can move memory references
4174 freely, even volatile ones.
4176 Otherwise, the rules are as follows: volatile memory
4177 references and stores can't be moved at all, and any type
4178 of memory reference can't be moved if there are volatile
4179 accesses or stores in the ACROSS range. That leaves
4180 normal reads, which can be moved, as the trapping case is
4181 dealt with elsewhere. */
4182 if (other_branch_live != NULL || memrefs_in_across != 0)
4184 int mem_ref_flags = 0;
4185 int mem_set_flags = 0;
4186 note_stores (PATTERN (insn), find_memory_stores, &mem_set_flags);
4187 mem_ref_flags = find_memory (insn);
4188 /* Catch sets of the stack pointer. */
4189 mem_ref_flags |= mem_set_flags;
4191 if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4192 break;
4193 if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4194 break;
4195 if (mem_set_flags != 0
4196 || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4197 break;
4199 df_simulate_find_uses (insn, merge_use);
4200 /* We're only interested in uses which use a value live at
4201 the top, not one previously set in this block. */
4202 bitmap_and_compl_into (merge_use, merge_set);
4203 df_simulate_find_defs (insn, merge_set);
4204 if (bitmap_intersect_p (merge_set, test_use)
4205 || bitmap_intersect_p (merge_use, test_set))
4206 break;
4207 if (!HAVE_cc0 || !sets_cc0_p (insn))
4208 max_to = insn;
4210 next = NEXT_INSN (insn);
4211 if (insn == to)
4212 break;
4214 if (max_to != to)
4215 fail = 1;
4217 if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4218 goto out;
4220 /* Now, lower this upper bound by also taking into account that
4221 a range of insns moved across ACROSS must not leave a register
4222 live at the end that will be clobbered in ACROSS. We need to
4223 find a point where TEST_SET & LIVE == 0.
4225 Insns in the MERGE range that set registers which are also set
4226 in the ACROSS range may still be moved as long as we also move
4227 later insns which use the results of the set, and make the
4228 register dead again. This is verified by the condition stated
4229 above. We only need to test it for registers that are set in
4230 the moved region.
4232 MERGE_LIVE is provided by the caller and holds live registers after
4233 TO. */
4234 bitmap_copy (local_merge_live, merge_live);
4235 for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4236 df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4238 /* We're not interested in registers that aren't set in the moved
4239 region at all. */
4240 bitmap_and_into (local_merge_live, merge_set);
4241 for (;;)
4243 if (NONDEBUG_INSN_P (insn))
4245 if (!bitmap_intersect_p (test_set, local_merge_live)
4246 && (!HAVE_cc0 || !sets_cc0_p (insn)))
4248 max_to = insn;
4249 break;
4252 df_simulate_one_insn_backwards (merge_bb, insn,
4253 local_merge_live);
4255 if (insn == from)
4257 fail = 1;
4258 goto out;
4260 insn = PREV_INSN (insn);
4263 if (max_to != to)
4264 fail = 1;
4266 if (pmove_upto)
4267 *pmove_upto = max_to;
4269 /* For small register class machines, don't lengthen lifetimes of
4270 hard registers before reload. */
4271 if (! reload_completed
4272 && targetm.small_register_classes_for_mode_p (VOIDmode))
4274 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4276 if (i < FIRST_PSEUDO_REGISTER
4277 && ! fixed_regs[i]
4278 && ! global_regs[i])
4280 fail = 1;
4281 break;
4286 out:
4287 BITMAP_FREE (merge_set);
4288 BITMAP_FREE (merge_use);
4289 BITMAP_FREE (local_merge_live);
4290 BITMAP_FREE (test_set);
4291 BITMAP_FREE (test_use);
4293 return !fail;
4297 /*----------------------------------------------------------------------------
4298 MULTIPLE DEFINITIONS
4300 Find the locations in the function reached by multiple definition sites
4301 for a live pseudo. In and out bitvectors are built for each basic
4302 block. They are restricted for efficiency to live registers.
4304 The gen and kill sets for the problem are obvious. Together they
4305 include all defined registers in a basic block; the gen set includes
4306 registers where a partial or conditional or may-clobber definition is
4307 last in the BB, while the kill set includes registers with a complete
4308 definition coming last. However, the computation of the dataflow
4309 itself is interesting.
4311 The idea behind it comes from SSA form's iterated dominance frontier
4312 criterion for inserting PHI functions. Just like in that case, we can use
4313 the dominance frontier to find places where multiple definitions meet;
4314 a register X defined in a basic block BB1 has multiple definitions in
4315 basic blocks in BB1's dominance frontier.
4317 So, the in-set of a basic block BB2 is not just the union of the
4318 out-sets of BB2's predecessors, but includes some more bits that come
4319 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4320 the previous paragraph). I called this set the init-set of BB2.
4322 (Note: I actually use the kill-set only to build the init-set.
4323 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4325 For example, if you have
4327 BB1 : r10 = 0
4328 r11 = 0
4329 if <...> goto BB2 else goto BB3;
4331 BB2 : r10 = 1
4332 r12 = 1
4333 goto BB3;
4335 BB3 :
4337 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4338 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4339 not need to iterate the dominance frontier, because we do not insert
4340 anything like PHI functions there! Instead, dataflow will take care of
4341 propagating the information to BB3's successors.
4342 ---------------------------------------------------------------------------*/
4344 /* Private data used to verify the solution for this problem. */
4345 struct df_md_problem_data
4347 /* An obstack for the bitmaps we need for this problem. */
4348 bitmap_obstack md_bitmaps;
4351 /* Scratch var used by transfer functions. This is used to do md analysis
4352 only for live registers. */
4353 static bitmap_head df_md_scratch;
4356 static void
4357 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4358 void *vbb_info)
4360 struct df_md_bb_info *bb_info = (struct df_md_bb_info *) vbb_info;
4361 if (bb_info)
4363 bitmap_clear (&bb_info->kill);
4364 bitmap_clear (&bb_info->gen);
4365 bitmap_clear (&bb_info->init);
4366 bitmap_clear (&bb_info->in);
4367 bitmap_clear (&bb_info->out);
4372 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4373 not touched unless the block is new. */
4375 static void
4376 df_md_alloc (bitmap all_blocks)
4378 unsigned int bb_index;
4379 bitmap_iterator bi;
4380 struct df_md_problem_data *problem_data;
4382 df_grow_bb_info (df_md);
4383 if (df_md->problem_data)
4384 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4385 else
4387 problem_data = XNEW (struct df_md_problem_data);
4388 df_md->problem_data = problem_data;
4389 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4391 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4393 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4395 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4396 /* When bitmaps are already initialized, just clear them. */
4397 if (bb_info->init.obstack)
4399 bitmap_clear (&bb_info->init);
4400 bitmap_clear (&bb_info->gen);
4401 bitmap_clear (&bb_info->kill);
4402 bitmap_clear (&bb_info->in);
4403 bitmap_clear (&bb_info->out);
4405 else
4407 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4408 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4409 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4410 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4411 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4415 df_md->optional_p = true;
4418 /* Add the effect of the top artificial defs of BB to the multiple definitions
4419 bitmap LOCAL_MD. */
4421 void
4422 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4424 int bb_index = bb->index;
4425 df_ref def;
4426 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4427 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4429 unsigned int dregno = DF_REF_REGNO (def);
4430 if (DF_REF_FLAGS (def)
4431 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4432 bitmap_set_bit (local_md, dregno);
4433 else
4434 bitmap_clear_bit (local_md, dregno);
4439 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4440 LOCAL_MD. */
4442 void
4443 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4444 bitmap local_md)
4446 df_ref def;
4448 FOR_EACH_INSN_DEF (def, insn)
4450 unsigned int dregno = DF_REF_REGNO (def);
4451 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4452 || (dregno >= FIRST_PSEUDO_REGISTER))
4454 if (DF_REF_FLAGS (def)
4455 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4456 bitmap_set_bit (local_md, DF_REF_ID (def));
4457 else
4458 bitmap_clear_bit (local_md, DF_REF_ID (def));
4463 static void
4464 df_md_bb_local_compute_process_def (struct df_md_bb_info *bb_info,
4465 df_ref def,
4466 int top_flag)
4468 bitmap_clear (&seen_in_insn);
4470 for (; def; def = DF_REF_NEXT_LOC (def))
4472 unsigned int dregno = DF_REF_REGNO (def);
4473 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4474 || (dregno >= FIRST_PSEUDO_REGISTER))
4475 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4477 if (!bitmap_bit_p (&seen_in_insn, dregno))
4479 if (DF_REF_FLAGS (def)
4480 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4482 bitmap_set_bit (&bb_info->gen, dregno);
4483 bitmap_clear_bit (&bb_info->kill, dregno);
4485 else
4487 /* When we find a clobber and a regular def,
4488 make sure the regular def wins. */
4489 bitmap_set_bit (&seen_in_insn, dregno);
4490 bitmap_set_bit (&bb_info->kill, dregno);
4491 bitmap_clear_bit (&bb_info->gen, dregno);
4499 /* Compute local multiple def info for basic block BB. */
4501 static void
4502 df_md_bb_local_compute (unsigned int bb_index)
4504 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4505 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4506 rtx_insn *insn;
4508 /* Artificials are only hard regs. */
4509 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4510 df_md_bb_local_compute_process_def (bb_info,
4511 df_get_artificial_defs (bb_index),
4512 DF_REF_AT_TOP);
4514 FOR_BB_INSNS (bb, insn)
4516 unsigned int uid = INSN_UID (insn);
4517 if (!INSN_P (insn))
4518 continue;
4520 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4523 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4524 df_md_bb_local_compute_process_def (bb_info,
4525 df_get_artificial_defs (bb_index),
4529 /* Compute local reaching def info for each basic block within BLOCKS. */
4531 static void
4532 df_md_local_compute (bitmap all_blocks)
4534 unsigned int bb_index, df_bb_index;
4535 bitmap_iterator bi1, bi2;
4536 basic_block bb;
4537 bitmap_head *frontiers;
4539 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4541 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4543 df_md_bb_local_compute (bb_index);
4546 bitmap_clear (&seen_in_insn);
4548 frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4549 FOR_ALL_BB_FN (bb, cfun)
4550 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4552 compute_dominance_frontiers (frontiers);
4554 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4555 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4557 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4558 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4560 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4561 if (bitmap_bit_p (all_blocks, df_bb_index))
4562 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4563 df_get_live_in (bb));
4567 FOR_ALL_BB_FN (bb, cfun)
4568 bitmap_clear (&frontiers[bb->index]);
4569 free (frontiers);
4573 /* Reset the global solution for recalculation. */
4575 static void
4576 df_md_reset (bitmap all_blocks)
4578 unsigned int bb_index;
4579 bitmap_iterator bi;
4581 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4583 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4584 gcc_assert (bb_info);
4585 bitmap_clear (&bb_info->in);
4586 bitmap_clear (&bb_info->out);
4590 static bool
4591 df_md_transfer_function (int bb_index)
4593 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4594 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4595 bitmap in = &bb_info->in;
4596 bitmap out = &bb_info->out;
4597 bitmap gen = &bb_info->gen;
4598 bitmap kill = &bb_info->kill;
4600 /* We need to use a scratch set here so that the value returned from this
4601 function invocation properly reflects whether the sets changed in a
4602 significant way; i.e. not just because the live set was anded in. */
4603 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4605 /* Multiple definitions of a register are not relevant if it is not
4606 live. Thus we trim the result to the places where it is live. */
4607 bitmap_and_into (in, df_get_live_in (bb));
4609 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4612 /* Initialize the solution bit vectors for problem. */
4614 static void
4615 df_md_init (bitmap all_blocks)
4617 unsigned int bb_index;
4618 bitmap_iterator bi;
4620 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4622 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4624 bitmap_copy (&bb_info->in, &bb_info->init);
4625 df_md_transfer_function (bb_index);
4629 static void
4630 df_md_confluence_0 (basic_block bb)
4632 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4633 bitmap_copy (&bb_info->in, &bb_info->init);
4636 /* In of target gets or of out of source. */
4638 static bool
4639 df_md_confluence_n (edge e)
4641 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4642 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4644 if (e->flags & EDGE_FAKE)
4645 return false;
4647 if (e->flags & EDGE_EH)
4648 return bitmap_ior_and_compl_into (op1, op2,
4649 regs_invalidated_by_call_regset);
4650 else
4651 return bitmap_ior_into (op1, op2);
4654 /* Free all storage associated with the problem. */
4656 static void
4657 df_md_free (void)
4659 struct df_md_problem_data *problem_data
4660 = (struct df_md_problem_data *) df_md->problem_data;
4662 bitmap_obstack_release (&problem_data->md_bitmaps);
4663 free (problem_data);
4664 df_md->problem_data = NULL;
4666 df_md->block_info_size = 0;
4667 free (df_md->block_info);
4668 df_md->block_info = NULL;
4669 free (df_md);
4673 /* Debugging info at top of bb. */
4675 static void
4676 df_md_top_dump (basic_block bb, FILE *file)
4678 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4679 if (!bb_info)
4680 return;
4682 fprintf (file, ";; md in \t");
4683 df_print_regset (file, &bb_info->in);
4684 fprintf (file, ";; md init \t");
4685 df_print_regset (file, &bb_info->init);
4686 fprintf (file, ";; md gen \t");
4687 df_print_regset (file, &bb_info->gen);
4688 fprintf (file, ";; md kill \t");
4689 df_print_regset (file, &bb_info->kill);
4692 /* Debugging info at bottom of bb. */
4694 static void
4695 df_md_bottom_dump (basic_block bb, FILE *file)
4697 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4698 if (!bb_info)
4699 return;
4701 fprintf (file, ";; md out \t");
4702 df_print_regset (file, &bb_info->out);
4705 static struct df_problem problem_MD =
4707 DF_MD, /* Problem id. */
4708 DF_FORWARD, /* Direction. */
4709 df_md_alloc, /* Allocate the problem specific data. */
4710 df_md_reset, /* Reset global information. */
4711 df_md_free_bb_info, /* Free basic block info. */
4712 df_md_local_compute, /* Local compute function. */
4713 df_md_init, /* Init the solution specific data. */
4714 df_worklist_dataflow, /* Worklist solver. */
4715 df_md_confluence_0, /* Confluence operator 0. */
4716 df_md_confluence_n, /* Confluence operator n. */
4717 df_md_transfer_function, /* Transfer function. */
4718 NULL, /* Finalize function. */
4719 df_md_free, /* Free all of the problem information. */
4720 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4721 NULL, /* Debugging. */
4722 df_md_top_dump, /* Debugging start block. */
4723 df_md_bottom_dump, /* Debugging end block. */
4724 NULL, /* Debugging start insn. */
4725 NULL, /* Debugging end insn. */
4726 NULL, /* Incremental solution verify start. */
4727 NULL, /* Incremental solution verify end. */
4728 NULL, /* Dependent problem. */
4729 sizeof (struct df_md_bb_info),/* Size of entry of block_info array. */
4730 TV_DF_MD, /* Timing variable. */
4731 false /* Reset blocks on dropping out of blocks_to_analyze. */
4734 /* Create a new MD instance and add it to the existing instance
4735 of DF. */
4737 void
4738 df_md_add_problem (void)
4740 df_add_problem (&problem_MD);