PR c++/77539
[official-gcc.git] / gcc / df-problems.c
blob290281ccd4f0982ba72274b29769e28f591bdfd1
1 /* Standard problems for dataflow support routines.
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Originally contributed by Michael P. Hayes
4 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
5 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
6 and Kenneth Zadeck (zadeck@naturalbridge.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "df.h"
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "cfganal.h"
34 #include "dce.h"
35 #include "valtrack.h"
36 #include "dumpfile.h"
37 #include "rtl-iter.h"
39 /* Note that turning REG_DEAD_DEBUGGING on will cause
40 gcc.c-torture/unsorted/dump-noaddr.c to fail because it prints
41 addresses in the dumps. */
42 #define REG_DEAD_DEBUGGING 0
44 #define DF_SPARSE_THRESHOLD 32
46 static bitmap_head seen_in_block;
47 static bitmap_head seen_in_insn;
49 /*----------------------------------------------------------------------------
50 Utility functions.
51 ----------------------------------------------------------------------------*/
53 /* Generic versions to get the void* version of the block info. Only
54 used inside the problem instance vectors. */
56 /* Dump a def-use or use-def chain for REF to FILE. */
58 void
59 df_chain_dump (struct df_link *link, FILE *file)
61 fprintf (file, "{ ");
62 for (; link; link = link->next)
64 fprintf (file, "%c%d(bb %d insn %d) ",
65 DF_REF_REG_DEF_P (link->ref)
66 ? 'd'
67 : (DF_REF_FLAGS (link->ref) & DF_REF_IN_NOTE) ? 'e' : 'u',
68 DF_REF_ID (link->ref),
69 DF_REF_BBNO (link->ref),
70 DF_REF_IS_ARTIFICIAL (link->ref)
71 ? -1 : DF_REF_INSN_UID (link->ref));
73 fprintf (file, "}");
77 /* Print some basic block info as part of df_dump. */
79 void
80 df_print_bb_index (basic_block bb, FILE *file)
82 edge e;
83 edge_iterator ei;
85 fprintf (file, "\n( ");
86 FOR_EACH_EDGE (e, ei, bb->preds)
88 basic_block pred = e->src;
89 fprintf (file, "%d%s ", pred->index, e->flags & EDGE_EH ? "(EH)" : "");
91 fprintf (file, ")->[%d]->( ", bb->index);
92 FOR_EACH_EDGE (e, ei, bb->succs)
94 basic_block succ = e->dest;
95 fprintf (file, "%d%s ", succ->index, e->flags & EDGE_EH ? "(EH)" : "");
97 fprintf (file, ")\n");
101 /*----------------------------------------------------------------------------
102 REACHING DEFINITIONS
104 Find the locations in the function where each definition site for a
105 pseudo reaches. In and out bitvectors are built for each basic
106 block. The id field in the ref is used to index into these sets.
107 See df.h for details.
109 If the DF_RD_PRUNE_DEAD_DEFS changeable flag is set, only DEFs reaching
110 existing uses are included in the global reaching DEFs set, or in other
111 words only DEFs that are still live. This is a kind of pruned version
112 of the traditional reaching definitions problem that is much less
113 complex to compute and produces enough information to compute UD-chains.
114 In this context, live must be interpreted in the DF_LR sense: Uses that
115 are upward exposed but maybe not initialized on all paths through the
116 CFG. For a USE that is not reached by a DEF on all paths, we still want
117 to make those DEFs that do reach the USE visible, and pruning based on
118 DF_LIVE would make that impossible.
119 ----------------------------------------------------------------------------*/
121 /* This problem plays a large number of games for the sake of
122 efficiency.
124 1) The order of the bits in the bitvectors. After the scanning
125 phase, all of the defs are sorted. All of the defs for the reg 0
126 are first, followed by all defs for reg 1 and so on.
128 2) There are two kill sets, one if the number of defs is less or
129 equal to DF_SPARSE_THRESHOLD and another if the number of defs is
130 greater.
132 <= : Data is built directly in the kill set.
134 > : One level of indirection is used to keep from generating long
135 strings of 1 bits in the kill sets. Bitvectors that are indexed
136 by the regnum are used to represent that there is a killing def
137 for the register. The confluence and transfer functions use
138 these along with the bitmap_clear_range call to remove ranges of
139 bits without actually generating a knockout vector.
141 The kill and sparse_kill and the dense_invalidated_by_call and
142 sparse_invalidated_by_call both play this game. */
144 /* Private data used to compute the solution for this problem. These
145 data structures are not accessible outside of this module. */
146 struct df_rd_problem_data
148 /* The set of defs to regs invalidated by call. */
149 bitmap_head sparse_invalidated_by_call;
150 /* The set of defs to regs invalidate by call for rd. */
151 bitmap_head dense_invalidated_by_call;
152 /* An obstack for the bitmaps we need for this problem. */
153 bitmap_obstack rd_bitmaps;
157 /* Free basic block info. */
159 static void
160 df_rd_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
161 void *vbb_info)
163 struct df_rd_bb_info *bb_info = (struct df_rd_bb_info *) vbb_info;
164 if (bb_info)
166 bitmap_clear (&bb_info->kill);
167 bitmap_clear (&bb_info->sparse_kill);
168 bitmap_clear (&bb_info->gen);
169 bitmap_clear (&bb_info->in);
170 bitmap_clear (&bb_info->out);
175 /* Allocate or reset bitmaps for DF_RD blocks. The solution bits are
176 not touched unless the block is new. */
178 static void
179 df_rd_alloc (bitmap all_blocks)
181 unsigned int bb_index;
182 bitmap_iterator bi;
183 struct df_rd_problem_data *problem_data;
185 if (df_rd->problem_data)
187 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
188 bitmap_clear (&problem_data->sparse_invalidated_by_call);
189 bitmap_clear (&problem_data->dense_invalidated_by_call);
191 else
193 problem_data = XNEW (struct df_rd_problem_data);
194 df_rd->problem_data = problem_data;
196 bitmap_obstack_initialize (&problem_data->rd_bitmaps);
197 bitmap_initialize (&problem_data->sparse_invalidated_by_call,
198 &problem_data->rd_bitmaps);
199 bitmap_initialize (&problem_data->dense_invalidated_by_call,
200 &problem_data->rd_bitmaps);
203 df_grow_bb_info (df_rd);
205 /* Because of the clustering of all use sites for the same pseudo,
206 we have to process all of the blocks before doing the analysis. */
208 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
210 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
212 /* When bitmaps are already initialized, just clear them. */
213 if (bb_info->kill.obstack)
215 bitmap_clear (&bb_info->kill);
216 bitmap_clear (&bb_info->sparse_kill);
217 bitmap_clear (&bb_info->gen);
219 else
221 bitmap_initialize (&bb_info->kill, &problem_data->rd_bitmaps);
222 bitmap_initialize (&bb_info->sparse_kill, &problem_data->rd_bitmaps);
223 bitmap_initialize (&bb_info->gen, &problem_data->rd_bitmaps);
224 bitmap_initialize (&bb_info->in, &problem_data->rd_bitmaps);
225 bitmap_initialize (&bb_info->out, &problem_data->rd_bitmaps);
228 df_rd->optional_p = true;
232 /* Add the effect of the top artificial defs of BB to the reaching definitions
233 bitmap LOCAL_RD. */
235 void
236 df_rd_simulate_artificial_defs_at_top (basic_block bb, bitmap local_rd)
238 int bb_index = bb->index;
239 df_ref def;
240 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
241 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
243 unsigned int dregno = DF_REF_REGNO (def);
244 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
245 bitmap_clear_range (local_rd,
246 DF_DEFS_BEGIN (dregno),
247 DF_DEFS_COUNT (dregno));
248 bitmap_set_bit (local_rd, DF_REF_ID (def));
252 /* Add the effect of the defs of INSN to the reaching definitions bitmap
253 LOCAL_RD. */
255 void
256 df_rd_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
257 bitmap local_rd)
259 df_ref def;
261 FOR_EACH_INSN_DEF (def, insn)
263 unsigned int dregno = DF_REF_REGNO (def);
264 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
265 || (dregno >= FIRST_PSEUDO_REGISTER))
267 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
268 bitmap_clear_range (local_rd,
269 DF_DEFS_BEGIN (dregno),
270 DF_DEFS_COUNT (dregno));
271 if (!(DF_REF_FLAGS (def)
272 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
273 bitmap_set_bit (local_rd, DF_REF_ID (def));
278 /* Process a list of DEFs for df_rd_bb_local_compute. This is a bit
279 more complicated than just simulating, because we must produce the
280 gen and kill sets and hence deal with the two possible representations
281 of kill sets. */
283 static void
284 df_rd_bb_local_compute_process_def (struct df_rd_bb_info *bb_info,
285 df_ref def,
286 int top_flag)
288 for (; def; def = DF_REF_NEXT_LOC (def))
290 if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
292 unsigned int regno = DF_REF_REGNO (def);
293 unsigned int begin = DF_DEFS_BEGIN (regno);
294 unsigned int n_defs = DF_DEFS_COUNT (regno);
296 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
297 || (regno >= FIRST_PSEUDO_REGISTER))
299 /* Only the last def(s) for a regno in the block has any
300 effect. */
301 if (!bitmap_bit_p (&seen_in_block, regno))
303 /* The first def for regno in insn gets to knock out the
304 defs from other instructions. */
305 if ((!bitmap_bit_p (&seen_in_insn, regno))
306 /* If the def is to only part of the reg, it does
307 not kill the other defs that reach here. */
308 && (!(DF_REF_FLAGS (def) &
309 (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))))
311 if (n_defs > DF_SPARSE_THRESHOLD)
313 bitmap_set_bit (&bb_info->sparse_kill, regno);
314 bitmap_clear_range (&bb_info->gen, begin, n_defs);
316 else
318 bitmap_set_range (&bb_info->kill, begin, n_defs);
319 bitmap_clear_range (&bb_info->gen, begin, n_defs);
323 bitmap_set_bit (&seen_in_insn, regno);
324 /* All defs for regno in the instruction may be put into
325 the gen set. */
326 if (!(DF_REF_FLAGS (def)
327 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
328 bitmap_set_bit (&bb_info->gen, DF_REF_ID (def));
335 /* Compute local reaching def info for basic block BB. */
337 static void
338 df_rd_bb_local_compute (unsigned int bb_index)
340 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
341 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
342 rtx_insn *insn;
344 bitmap_clear (&seen_in_block);
345 bitmap_clear (&seen_in_insn);
347 /* Artificials are only hard regs. */
348 if (!(df->changeable_flags & DF_NO_HARD_REGS))
349 df_rd_bb_local_compute_process_def (bb_info,
350 df_get_artificial_defs (bb_index),
353 FOR_BB_INSNS_REVERSE (bb, insn)
355 unsigned int uid = INSN_UID (insn);
357 if (!INSN_P (insn))
358 continue;
360 df_rd_bb_local_compute_process_def (bb_info,
361 DF_INSN_UID_DEFS (uid), 0);
363 /* This complex dance with the two bitmaps is required because
364 instructions can assign twice to the same pseudo. This
365 generally happens with calls that will have one def for the
366 result and another def for the clobber. If only one vector
367 is used and the clobber goes first, the result will be
368 lost. */
369 bitmap_ior_into (&seen_in_block, &seen_in_insn);
370 bitmap_clear (&seen_in_insn);
373 /* Process the artificial defs at the top of the block last since we
374 are going backwards through the block and these are logically at
375 the start. */
376 if (!(df->changeable_flags & DF_NO_HARD_REGS))
377 df_rd_bb_local_compute_process_def (bb_info,
378 df_get_artificial_defs (bb_index),
379 DF_REF_AT_TOP);
383 /* Compute local reaching def info for each basic block within BLOCKS. */
385 static void
386 df_rd_local_compute (bitmap all_blocks)
388 unsigned int bb_index;
389 bitmap_iterator bi;
390 unsigned int regno;
391 struct df_rd_problem_data *problem_data
392 = (struct df_rd_problem_data *) df_rd->problem_data;
393 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
394 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
396 bitmap_initialize (&seen_in_block, &df_bitmap_obstack);
397 bitmap_initialize (&seen_in_insn, &df_bitmap_obstack);
399 df_maybe_reorganize_def_refs (DF_REF_ORDER_BY_REG);
401 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
403 df_rd_bb_local_compute (bb_index);
406 /* Set up the knockout bit vectors to be applied across EH_EDGES. */
407 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, regno, bi)
409 if (! HARD_REGISTER_NUM_P (regno)
410 || !(df->changeable_flags & DF_NO_HARD_REGS))
412 if (DF_DEFS_COUNT (regno) > DF_SPARSE_THRESHOLD)
413 bitmap_set_bit (sparse_invalidated, regno);
414 else
415 bitmap_set_range (dense_invalidated,
416 DF_DEFS_BEGIN (regno),
417 DF_DEFS_COUNT (regno));
421 bitmap_clear (&seen_in_block);
422 bitmap_clear (&seen_in_insn);
426 /* Initialize the solution bit vectors for problem. */
428 static void
429 df_rd_init_solution (bitmap all_blocks)
431 unsigned int bb_index;
432 bitmap_iterator bi;
434 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
436 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
438 bitmap_copy (&bb_info->out, &bb_info->gen);
439 bitmap_clear (&bb_info->in);
443 /* In of target gets or of out of source. */
445 static bool
446 df_rd_confluence_n (edge e)
448 bitmap op1 = &df_rd_get_bb_info (e->dest->index)->in;
449 bitmap op2 = &df_rd_get_bb_info (e->src->index)->out;
450 bool changed = false;
452 if (e->flags & EDGE_FAKE)
453 return false;
455 if (e->flags & EDGE_EH)
457 struct df_rd_problem_data *problem_data
458 = (struct df_rd_problem_data *) df_rd->problem_data;
459 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
460 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
461 bitmap_iterator bi;
462 unsigned int regno;
463 bitmap_head tmp;
465 bitmap_initialize (&tmp, &df_bitmap_obstack);
466 bitmap_and_compl (&tmp, op2, dense_invalidated);
468 EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
470 bitmap_clear_range (&tmp,
471 DF_DEFS_BEGIN (regno),
472 DF_DEFS_COUNT (regno));
474 changed |= bitmap_ior_into (op1, &tmp);
475 bitmap_clear (&tmp);
476 return changed;
478 else
479 return bitmap_ior_into (op1, op2);
483 /* Transfer function. */
485 static bool
486 df_rd_transfer_function (int bb_index)
488 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
489 unsigned int regno;
490 bitmap_iterator bi;
491 bitmap in = &bb_info->in;
492 bitmap out = &bb_info->out;
493 bitmap gen = &bb_info->gen;
494 bitmap kill = &bb_info->kill;
495 bitmap sparse_kill = &bb_info->sparse_kill;
496 bool changed = false;
498 if (bitmap_empty_p (sparse_kill))
499 changed = bitmap_ior_and_compl (out, gen, in, kill);
500 else
502 struct df_rd_problem_data *problem_data;
503 bitmap_head tmp;
505 /* Note that TMP is _not_ a temporary bitmap if we end up replacing
506 OUT with TMP. Therefore, allocate TMP in the RD bitmaps obstack. */
507 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
508 bitmap_initialize (&tmp, &problem_data->rd_bitmaps);
510 bitmap_and_compl (&tmp, in, kill);
511 EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
513 bitmap_clear_range (&tmp,
514 DF_DEFS_BEGIN (regno),
515 DF_DEFS_COUNT (regno));
517 bitmap_ior_into (&tmp, gen);
518 changed = !bitmap_equal_p (&tmp, out);
519 if (changed)
520 bitmap_move (out, &tmp);
521 else
522 bitmap_clear (&tmp);
525 if (df->changeable_flags & DF_RD_PRUNE_DEAD_DEFS)
527 /* Create a mask of DEFs for all registers live at the end of this
528 basic block, and mask out DEFs of registers that are not live.
529 Computing the mask looks costly, but the benefit of the pruning
530 outweighs the cost. */
531 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
532 bitmap regs_live_out = &df_lr_get_bb_info (bb_index)->out;
533 bitmap live_defs = BITMAP_ALLOC (&df_bitmap_obstack);
534 unsigned int regno;
535 bitmap_iterator bi;
537 EXECUTE_IF_SET_IN_BITMAP (regs_live_out, 0, regno, bi)
538 bitmap_set_range (live_defs,
539 DF_DEFS_BEGIN (regno),
540 DF_DEFS_COUNT (regno));
541 changed |= bitmap_and_into (&bb_info->out, live_defs);
542 BITMAP_FREE (live_defs);
545 return changed;
548 /* Free all storage associated with the problem. */
550 static void
551 df_rd_free (void)
553 struct df_rd_problem_data *problem_data
554 = (struct df_rd_problem_data *) df_rd->problem_data;
556 if (problem_data)
558 bitmap_obstack_release (&problem_data->rd_bitmaps);
560 df_rd->block_info_size = 0;
561 free (df_rd->block_info);
562 df_rd->block_info = NULL;
563 free (df_rd->problem_data);
565 free (df_rd);
569 /* Debugging info. */
571 static void
572 df_rd_start_dump (FILE *file)
574 struct df_rd_problem_data *problem_data
575 = (struct df_rd_problem_data *) df_rd->problem_data;
576 unsigned int m = DF_REG_SIZE (df);
577 unsigned int regno;
579 if (!df_rd->block_info)
580 return;
582 fprintf (file, ";; Reaching defs:\n");
584 fprintf (file, ";; sparse invalidated \t");
585 dump_bitmap (file, &problem_data->sparse_invalidated_by_call);
586 fprintf (file, ";; dense invalidated \t");
587 dump_bitmap (file, &problem_data->dense_invalidated_by_call);
589 fprintf (file, ";; reg->defs[] map:\t");
590 for (regno = 0; regno < m; regno++)
591 if (DF_DEFS_COUNT (regno))
592 fprintf (file, "%d[%d,%d] ", regno,
593 DF_DEFS_BEGIN (regno),
594 DF_DEFS_BEGIN (regno) + DF_DEFS_COUNT (regno) - 1);
595 fprintf (file, "\n");
599 static void
600 df_rd_dump_defs_set (bitmap defs_set, const char *prefix, FILE *file)
602 bitmap_head tmp;
603 unsigned int regno;
604 unsigned int m = DF_REG_SIZE (df);
605 bool first_reg = true;
607 fprintf (file, "%s\t(%d) ", prefix, (int) bitmap_count_bits (defs_set));
609 bitmap_initialize (&tmp, &df_bitmap_obstack);
610 for (regno = 0; regno < m; regno++)
612 if (HARD_REGISTER_NUM_P (regno)
613 && (df->changeable_flags & DF_NO_HARD_REGS))
614 continue;
615 bitmap_set_range (&tmp, DF_DEFS_BEGIN (regno), DF_DEFS_COUNT (regno));
616 bitmap_and_into (&tmp, defs_set);
617 if (! bitmap_empty_p (&tmp))
619 bitmap_iterator bi;
620 unsigned int ix;
621 bool first_def = true;
623 if (! first_reg)
624 fprintf (file, ",");
625 first_reg = false;
627 fprintf (file, "%u[", regno);
628 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, ix, bi)
630 fprintf (file, "%s%u", first_def ? "" : ",", ix);
631 first_def = false;
633 fprintf (file, "]");
635 bitmap_clear (&tmp);
638 fprintf (file, "\n");
639 bitmap_clear (&tmp);
642 /* Debugging info at top of bb. */
644 static void
645 df_rd_top_dump (basic_block bb, FILE *file)
647 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
648 if (!bb_info)
649 return;
651 df_rd_dump_defs_set (&bb_info->in, ";; rd in ", file);
652 df_rd_dump_defs_set (&bb_info->gen, ";; rd gen ", file);
653 df_rd_dump_defs_set (&bb_info->kill, ";; rd kill", file);
657 /* Debugging info at bottom of bb. */
659 static void
660 df_rd_bottom_dump (basic_block bb, FILE *file)
662 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
663 if (!bb_info)
664 return;
666 df_rd_dump_defs_set (&bb_info->out, ";; rd out ", file);
669 /* All of the information associated with every instance of the problem. */
671 static const struct df_problem problem_RD =
673 DF_RD, /* Problem id. */
674 DF_FORWARD, /* Direction. */
675 df_rd_alloc, /* Allocate the problem specific data. */
676 NULL, /* Reset global information. */
677 df_rd_free_bb_info, /* Free basic block info. */
678 df_rd_local_compute, /* Local compute function. */
679 df_rd_init_solution, /* Init the solution specific data. */
680 df_worklist_dataflow, /* Worklist solver. */
681 NULL, /* Confluence operator 0. */
682 df_rd_confluence_n, /* Confluence operator n. */
683 df_rd_transfer_function, /* Transfer function. */
684 NULL, /* Finalize function. */
685 df_rd_free, /* Free all of the problem information. */
686 df_rd_free, /* Remove this problem from the stack of dataflow problems. */
687 df_rd_start_dump, /* Debugging. */
688 df_rd_top_dump, /* Debugging start block. */
689 df_rd_bottom_dump, /* Debugging end block. */
690 NULL, /* Debugging start insn. */
691 NULL, /* Debugging end insn. */
692 NULL, /* Incremental solution verify start. */
693 NULL, /* Incremental solution verify end. */
694 NULL, /* Dependent problem. */
695 sizeof (struct df_rd_bb_info),/* Size of entry of block_info array. */
696 TV_DF_RD, /* Timing variable. */
697 true /* Reset blocks on dropping out of blocks_to_analyze. */
702 /* Create a new RD instance and add it to the existing instance
703 of DF. */
705 void
706 df_rd_add_problem (void)
708 df_add_problem (&problem_RD);
713 /*----------------------------------------------------------------------------
714 LIVE REGISTERS
716 Find the locations in the function where any use of a pseudo can
717 reach in the backwards direction. In and out bitvectors are built
718 for each basic block. The regno is used to index into these sets.
719 See df.h for details.
720 ----------------------------------------------------------------------------*/
722 /* Private data used to verify the solution for this problem. */
723 struct df_lr_problem_data
725 bitmap_head *in;
726 bitmap_head *out;
727 /* An obstack for the bitmaps we need for this problem. */
728 bitmap_obstack lr_bitmaps;
731 /* Free basic block info. */
733 static void
734 df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
735 void *vbb_info)
737 struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
738 if (bb_info)
740 bitmap_clear (&bb_info->use);
741 bitmap_clear (&bb_info->def);
742 bitmap_clear (&bb_info->in);
743 bitmap_clear (&bb_info->out);
748 /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
749 not touched unless the block is new. */
751 static void
752 df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
754 unsigned int bb_index;
755 bitmap_iterator bi;
756 struct df_lr_problem_data *problem_data;
758 df_grow_bb_info (df_lr);
759 if (df_lr->problem_data)
760 problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
761 else
763 problem_data = XNEW (struct df_lr_problem_data);
764 df_lr->problem_data = problem_data;
766 problem_data->out = NULL;
767 problem_data->in = NULL;
768 bitmap_obstack_initialize (&problem_data->lr_bitmaps);
771 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
773 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
775 /* When bitmaps are already initialized, just clear them. */
776 if (bb_info->use.obstack)
778 bitmap_clear (&bb_info->def);
779 bitmap_clear (&bb_info->use);
781 else
783 bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
784 bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
785 bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
786 bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
790 df_lr->optional_p = false;
794 /* Reset the global solution for recalculation. */
796 static void
797 df_lr_reset (bitmap all_blocks)
799 unsigned int bb_index;
800 bitmap_iterator bi;
802 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
804 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
805 gcc_assert (bb_info);
806 bitmap_clear (&bb_info->in);
807 bitmap_clear (&bb_info->out);
812 /* Compute local live register info for basic block BB. */
814 static void
815 df_lr_bb_local_compute (unsigned int bb_index)
817 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
818 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
819 rtx_insn *insn;
820 df_ref def, use;
822 /* Process the registers set in an exception handler. */
823 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
824 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
826 unsigned int dregno = DF_REF_REGNO (def);
827 bitmap_set_bit (&bb_info->def, dregno);
828 bitmap_clear_bit (&bb_info->use, dregno);
831 /* Process the hardware registers that are always live. */
832 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
833 /* Add use to set of uses in this BB. */
834 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
835 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
837 FOR_BB_INSNS_REVERSE (bb, insn)
839 if (!NONDEBUG_INSN_P (insn))
840 continue;
842 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
843 FOR_EACH_INSN_INFO_DEF (def, insn_info)
844 /* If the def is to only part of the reg, it does
845 not kill the other defs that reach here. */
846 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
848 unsigned int dregno = DF_REF_REGNO (def);
849 bitmap_set_bit (&bb_info->def, dregno);
850 bitmap_clear_bit (&bb_info->use, dregno);
853 FOR_EACH_INSN_INFO_USE (use, insn_info)
854 /* Add use to set of uses in this BB. */
855 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
858 /* Process the registers set in an exception handler or the hard
859 frame pointer if this block is the target of a non local
860 goto. */
861 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
862 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
864 unsigned int dregno = DF_REF_REGNO (def);
865 bitmap_set_bit (&bb_info->def, dregno);
866 bitmap_clear_bit (&bb_info->use, dregno);
869 #ifdef EH_USES
870 /* Process the uses that are live into an exception handler. */
871 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
872 /* Add use to set of uses in this BB. */
873 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
874 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
875 #endif
877 /* If the df_live problem is not defined, such as at -O0 and -O1, we
878 still need to keep the luids up to date. This is normally done
879 in the df_live problem since this problem has a forwards
880 scan. */
881 if (!df_live)
882 df_recompute_luids (bb);
886 /* Compute local live register info for each basic block within BLOCKS. */
888 static void
889 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
891 unsigned int bb_index, i;
892 bitmap_iterator bi;
894 bitmap_clear (&df->hardware_regs_used);
896 /* The all-important stack pointer must always be live. */
897 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
899 /* Global regs are always live, too. */
900 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
901 if (global_regs[i])
902 bitmap_set_bit (&df->hardware_regs_used, i);
904 /* Before reload, there are a few registers that must be forced
905 live everywhere -- which might not already be the case for
906 blocks within infinite loops. */
907 if (!reload_completed)
909 unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
910 /* Any reference to any pseudo before reload is a potential
911 reference of the frame pointer. */
912 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
914 /* Pseudos with argument area equivalences may require
915 reloading via the argument pointer. */
916 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
917 && fixed_regs[ARG_POINTER_REGNUM])
918 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
920 /* Any constant, or pseudo with constant equivalences, may
921 require reloading from memory using the pic register. */
922 if (pic_offset_table_regnum != INVALID_REGNUM
923 && fixed_regs[pic_offset_table_regnum])
924 bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
927 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
929 if (bb_index == EXIT_BLOCK)
931 /* The exit block is special for this problem and its bits are
932 computed from thin air. */
933 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
934 bitmap_copy (&bb_info->use, df->exit_block_uses);
936 else
937 df_lr_bb_local_compute (bb_index);
940 bitmap_clear (df_lr->out_of_date_transfer_functions);
944 /* Initialize the solution vectors. */
946 static void
947 df_lr_init (bitmap all_blocks)
949 unsigned int bb_index;
950 bitmap_iterator bi;
952 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
954 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
955 bitmap_copy (&bb_info->in, &bb_info->use);
956 bitmap_clear (&bb_info->out);
961 /* Confluence function that processes infinite loops. This might be a
962 noreturn function that throws. And even if it isn't, getting the
963 unwind info right helps debugging. */
964 static void
965 df_lr_confluence_0 (basic_block bb)
967 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
968 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
969 bitmap_copy (op1, &df->hardware_regs_used);
973 /* Confluence function that ignores fake edges. */
975 static bool
976 df_lr_confluence_n (edge e)
978 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
979 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
980 bool changed = false;
982 /* Call-clobbered registers die across exception and call edges. */
983 /* ??? Abnormal call edges ignored for the moment, as this gets
984 confused by sibling call edges, which crashes reg-stack. */
985 if (e->flags & EDGE_EH)
986 changed = bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
987 else
988 changed = bitmap_ior_into (op1, op2);
990 changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
991 return changed;
995 /* Transfer function. */
997 static bool
998 df_lr_transfer_function (int bb_index)
1000 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1001 bitmap in = &bb_info->in;
1002 bitmap out = &bb_info->out;
1003 bitmap use = &bb_info->use;
1004 bitmap def = &bb_info->def;
1006 return bitmap_ior_and_compl (in, use, out, def);
1010 /* Run the fast dce as a side effect of building LR. */
1012 static void
1013 df_lr_finalize (bitmap all_blocks)
1015 df_lr->solutions_dirty = false;
1016 if (df->changeable_flags & DF_LR_RUN_DCE)
1018 run_fast_df_dce ();
1020 /* If dce deletes some instructions, we need to recompute the lr
1021 solution before proceeding further. The problem is that fast
1022 dce is a pessimestic dataflow algorithm. In the case where
1023 it deletes a statement S inside of a loop, the uses inside of
1024 S may not be deleted from the dataflow solution because they
1025 were carried around the loop. While it is conservatively
1026 correct to leave these extra bits, the standards of df
1027 require that we maintain the best possible (least fixed
1028 point) solution. The only way to do that is to redo the
1029 iteration from the beginning. See PR35805 for an
1030 example. */
1031 if (df_lr->solutions_dirty)
1033 df_clear_flags (DF_LR_RUN_DCE);
1034 df_lr_alloc (all_blocks);
1035 df_lr_local_compute (all_blocks);
1036 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1037 df_lr_finalize (all_blocks);
1038 df_set_flags (DF_LR_RUN_DCE);
1044 /* Free all storage associated with the problem. */
1046 static void
1047 df_lr_free (void)
1049 struct df_lr_problem_data *problem_data
1050 = (struct df_lr_problem_data *) df_lr->problem_data;
1051 if (df_lr->block_info)
1054 df_lr->block_info_size = 0;
1055 free (df_lr->block_info);
1056 df_lr->block_info = NULL;
1057 bitmap_obstack_release (&problem_data->lr_bitmaps);
1058 free (df_lr->problem_data);
1059 df_lr->problem_data = NULL;
1062 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1063 free (df_lr);
1067 /* Debugging info at top of bb. */
1069 static void
1070 df_lr_top_dump (basic_block bb, FILE *file)
1072 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1073 struct df_lr_problem_data *problem_data;
1074 if (!bb_info)
1075 return;
1077 fprintf (file, ";; lr in \t");
1078 df_print_regset (file, &bb_info->in);
1079 if (df_lr->problem_data)
1081 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1082 if (problem_data->in)
1084 fprintf (file, ";; old in \t");
1085 df_print_regset (file, &problem_data->in[bb->index]);
1088 fprintf (file, ";; lr use \t");
1089 df_print_regset (file, &bb_info->use);
1090 fprintf (file, ";; lr def \t");
1091 df_print_regset (file, &bb_info->def);
1095 /* Debugging info at bottom of bb. */
1097 static void
1098 df_lr_bottom_dump (basic_block bb, FILE *file)
1100 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1101 struct df_lr_problem_data *problem_data;
1102 if (!bb_info)
1103 return;
1105 fprintf (file, ";; lr out \t");
1106 df_print_regset (file, &bb_info->out);
1107 if (df_lr->problem_data)
1109 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1110 if (problem_data->out)
1112 fprintf (file, ";; old out \t");
1113 df_print_regset (file, &problem_data->out[bb->index]);
1119 /* Build the datastructure to verify that the solution to the dataflow
1120 equations is not dirty. */
1122 static void
1123 df_lr_verify_solution_start (void)
1125 basic_block bb;
1126 struct df_lr_problem_data *problem_data;
1127 if (df_lr->solutions_dirty)
1128 return;
1130 /* Set it true so that the solution is recomputed. */
1131 df_lr->solutions_dirty = true;
1133 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1134 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1135 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1137 FOR_ALL_BB_FN (bb, cfun)
1139 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1140 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1141 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1142 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1147 /* Compare the saved datastructure and the new solution to the dataflow
1148 equations. */
1150 static void
1151 df_lr_verify_solution_end (void)
1153 struct df_lr_problem_data *problem_data;
1154 basic_block bb;
1156 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1158 if (!problem_data->out)
1159 return;
1161 if (df_lr->solutions_dirty)
1162 /* Do not check if the solution is still dirty. See the comment
1163 in df_lr_finalize for details. */
1164 df_lr->solutions_dirty = false;
1165 else
1166 FOR_ALL_BB_FN (bb, cfun)
1168 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1169 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1171 /*df_dump (stderr);*/
1172 gcc_unreachable ();
1176 /* Cannot delete them immediately because you may want to dump them
1177 if the comparison fails. */
1178 FOR_ALL_BB_FN (bb, cfun)
1180 bitmap_clear (&problem_data->in[bb->index]);
1181 bitmap_clear (&problem_data->out[bb->index]);
1184 free (problem_data->in);
1185 free (problem_data->out);
1186 problem_data->in = NULL;
1187 problem_data->out = NULL;
1191 /* All of the information associated with every instance of the problem. */
1193 static const struct df_problem problem_LR =
1195 DF_LR, /* Problem id. */
1196 DF_BACKWARD, /* Direction. */
1197 df_lr_alloc, /* Allocate the problem specific data. */
1198 df_lr_reset, /* Reset global information. */
1199 df_lr_free_bb_info, /* Free basic block info. */
1200 df_lr_local_compute, /* Local compute function. */
1201 df_lr_init, /* Init the solution specific data. */
1202 df_worklist_dataflow, /* Worklist solver. */
1203 df_lr_confluence_0, /* Confluence operator 0. */
1204 df_lr_confluence_n, /* Confluence operator n. */
1205 df_lr_transfer_function, /* Transfer function. */
1206 df_lr_finalize, /* Finalize function. */
1207 df_lr_free, /* Free all of the problem information. */
1208 NULL, /* Remove this problem from the stack of dataflow problems. */
1209 NULL, /* Debugging. */
1210 df_lr_top_dump, /* Debugging start block. */
1211 df_lr_bottom_dump, /* Debugging end block. */
1212 NULL, /* Debugging start insn. */
1213 NULL, /* Debugging end insn. */
1214 df_lr_verify_solution_start,/* Incremental solution verify start. */
1215 df_lr_verify_solution_end, /* Incremental solution verify end. */
1216 NULL, /* Dependent problem. */
1217 sizeof (struct df_lr_bb_info),/* Size of entry of block_info array. */
1218 TV_DF_LR, /* Timing variable. */
1219 false /* Reset blocks on dropping out of blocks_to_analyze. */
1223 /* Create a new DATAFLOW instance and add it to an existing instance
1224 of DF. The returned structure is what is used to get at the
1225 solution. */
1227 void
1228 df_lr_add_problem (void)
1230 df_add_problem (&problem_LR);
1231 /* These will be initialized when df_scan_blocks processes each
1232 block. */
1233 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1237 /* Verify that all of the lr related info is consistent and
1238 correct. */
1240 void
1241 df_lr_verify_transfer_functions (void)
1243 basic_block bb;
1244 bitmap_head saved_def;
1245 bitmap_head saved_use;
1246 bitmap_head all_blocks;
1248 if (!df)
1249 return;
1251 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1252 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1253 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1255 FOR_ALL_BB_FN (bb, cfun)
1257 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1258 bitmap_set_bit (&all_blocks, bb->index);
1260 if (bb_info)
1262 /* Make a copy of the transfer functions and then compute
1263 new ones to see if the transfer functions have
1264 changed. */
1265 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1266 bb->index))
1268 bitmap_copy (&saved_def, &bb_info->def);
1269 bitmap_copy (&saved_use, &bb_info->use);
1270 bitmap_clear (&bb_info->def);
1271 bitmap_clear (&bb_info->use);
1273 df_lr_bb_local_compute (bb->index);
1274 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1275 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1278 else
1280 /* If we do not have basic block info, the block must be in
1281 the list of dirty blocks or else some one has added a
1282 block behind our backs. */
1283 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1284 bb->index));
1286 /* Make sure no one created a block without following
1287 procedures. */
1288 gcc_assert (df_scan_get_bb_info (bb->index));
1291 /* Make sure there are no dirty bits in blocks that have been deleted. */
1292 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1293 &all_blocks));
1295 bitmap_clear (&saved_def);
1296 bitmap_clear (&saved_use);
1297 bitmap_clear (&all_blocks);
1302 /*----------------------------------------------------------------------------
1303 LIVE AND MAY-INITIALIZED REGISTERS.
1305 This problem first computes the IN and OUT bitvectors for the
1306 may-initialized registers problems, which is a forward problem.
1307 It gives the set of registers for which we MAY have an available
1308 definition, i.e. for which there is an available definition on
1309 at least one path from the entry block to the entry/exit of a
1310 basic block. Sets generate a definition, while clobbers kill
1311 a definition.
1313 In and out bitvectors are built for each basic block and are indexed by
1314 regnum (see df.h for details). In and out bitvectors in struct
1315 df_live_bb_info actually refers to the may-initialized problem;
1317 Then, the in and out sets for the LIVE problem itself are computed.
1318 These are the logical AND of the IN and OUT sets from the LR problem
1319 and the may-initialized problem.
1320 ----------------------------------------------------------------------------*/
1322 /* Private data used to verify the solution for this problem. */
1323 struct df_live_problem_data
1325 bitmap_head *in;
1326 bitmap_head *out;
1327 /* An obstack for the bitmaps we need for this problem. */
1328 bitmap_obstack live_bitmaps;
1331 /* Scratch var used by transfer functions. This is used to implement
1332 an optimization to reduce the amount of space used to compute the
1333 combined lr and live analysis. */
1334 static bitmap_head df_live_scratch;
1337 /* Free basic block info. */
1339 static void
1340 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1341 void *vbb_info)
1343 struct df_live_bb_info *bb_info = (struct df_live_bb_info *) vbb_info;
1344 if (bb_info)
1346 bitmap_clear (&bb_info->gen);
1347 bitmap_clear (&bb_info->kill);
1348 bitmap_clear (&bb_info->in);
1349 bitmap_clear (&bb_info->out);
1354 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1355 not touched unless the block is new. */
1357 static void
1358 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1360 unsigned int bb_index;
1361 bitmap_iterator bi;
1362 struct df_live_problem_data *problem_data;
1364 if (df_live->problem_data)
1365 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1366 else
1368 problem_data = XNEW (struct df_live_problem_data);
1369 df_live->problem_data = problem_data;
1371 problem_data->out = NULL;
1372 problem_data->in = NULL;
1373 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1374 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1377 df_grow_bb_info (df_live);
1379 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1381 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1383 /* When bitmaps are already initialized, just clear them. */
1384 if (bb_info->kill.obstack)
1386 bitmap_clear (&bb_info->kill);
1387 bitmap_clear (&bb_info->gen);
1389 else
1391 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1392 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1393 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1394 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1397 df_live->optional_p = (optimize <= 1);
1401 /* Reset the global solution for recalculation. */
1403 static void
1404 df_live_reset (bitmap all_blocks)
1406 unsigned int bb_index;
1407 bitmap_iterator bi;
1409 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1411 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1412 gcc_assert (bb_info);
1413 bitmap_clear (&bb_info->in);
1414 bitmap_clear (&bb_info->out);
1419 /* Compute local uninitialized register info for basic block BB. */
1421 static void
1422 df_live_bb_local_compute (unsigned int bb_index)
1424 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1425 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1426 rtx_insn *insn;
1427 df_ref def;
1428 int luid = 0;
1430 FOR_BB_INSNS (bb, insn)
1432 unsigned int uid = INSN_UID (insn);
1433 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1435 /* Inserting labels does not always trigger the incremental
1436 rescanning. */
1437 if (!insn_info)
1439 gcc_assert (!INSN_P (insn));
1440 insn_info = df_insn_create_insn_record (insn);
1443 DF_INSN_INFO_LUID (insn_info) = luid;
1444 if (!INSN_P (insn))
1445 continue;
1447 luid++;
1448 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1450 unsigned int regno = DF_REF_REGNO (def);
1452 if (DF_REF_FLAGS_IS_SET (def,
1453 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1454 /* All partial or conditional def
1455 seen are included in the gen set. */
1456 bitmap_set_bit (&bb_info->gen, regno);
1457 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1458 /* Only must clobbers for the entire reg destroy the
1459 value. */
1460 bitmap_set_bit (&bb_info->kill, regno);
1461 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1462 bitmap_set_bit (&bb_info->gen, regno);
1466 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1467 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1471 /* Compute local uninitialized register info. */
1473 static void
1474 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1476 unsigned int bb_index;
1477 bitmap_iterator bi;
1479 df_grow_insn_info ();
1481 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1482 0, bb_index, bi)
1484 df_live_bb_local_compute (bb_index);
1487 bitmap_clear (df_live->out_of_date_transfer_functions);
1491 /* Initialize the solution vectors. */
1493 static void
1494 df_live_init (bitmap all_blocks)
1496 unsigned int bb_index;
1497 bitmap_iterator bi;
1499 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1501 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1502 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1504 /* No register may reach a location where it is not used. Thus
1505 we trim the rr result to the places where it is used. */
1506 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1507 bitmap_clear (&bb_info->in);
1511 /* Forward confluence function that ignores fake edges. */
1513 static bool
1514 df_live_confluence_n (edge e)
1516 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1517 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1519 if (e->flags & EDGE_FAKE)
1520 return false;
1522 return bitmap_ior_into (op1, op2);
1526 /* Transfer function for the forwards may-initialized problem. */
1528 static bool
1529 df_live_transfer_function (int bb_index)
1531 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1532 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1533 bitmap in = &bb_info->in;
1534 bitmap out = &bb_info->out;
1535 bitmap gen = &bb_info->gen;
1536 bitmap kill = &bb_info->kill;
1538 /* We need to use a scratch set here so that the value returned from this
1539 function invocation properly reflects whether the sets changed in a
1540 significant way; i.e. not just because the lr set was anded in. */
1541 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1542 /* No register may reach a location where it is not used. Thus
1543 we trim the rr result to the places where it is used. */
1544 bitmap_and_into (in, &bb_lr_info->in);
1546 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1550 /* And the LR info with the may-initialized registers to produce the LIVE info. */
1552 static void
1553 df_live_finalize (bitmap all_blocks)
1556 if (df_live->solutions_dirty)
1558 bitmap_iterator bi;
1559 unsigned int bb_index;
1561 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1563 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1564 struct df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1566 /* No register may reach a location where it is not used. Thus
1567 we trim the rr result to the places where it is used. */
1568 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1569 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1572 df_live->solutions_dirty = false;
1577 /* Free all storage associated with the problem. */
1579 static void
1580 df_live_free (void)
1582 struct df_live_problem_data *problem_data
1583 = (struct df_live_problem_data *) df_live->problem_data;
1584 if (df_live->block_info)
1586 df_live->block_info_size = 0;
1587 free (df_live->block_info);
1588 df_live->block_info = NULL;
1589 bitmap_clear (&df_live_scratch);
1590 bitmap_obstack_release (&problem_data->live_bitmaps);
1591 free (problem_data);
1592 df_live->problem_data = NULL;
1594 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1595 free (df_live);
1599 /* Debugging info at top of bb. */
1601 static void
1602 df_live_top_dump (basic_block bb, FILE *file)
1604 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1605 struct df_live_problem_data *problem_data;
1607 if (!bb_info)
1608 return;
1610 fprintf (file, ";; live in \t");
1611 df_print_regset (file, &bb_info->in);
1612 if (df_live->problem_data)
1614 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1615 if (problem_data->in)
1617 fprintf (file, ";; old in \t");
1618 df_print_regset (file, &problem_data->in[bb->index]);
1621 fprintf (file, ";; live gen \t");
1622 df_print_regset (file, &bb_info->gen);
1623 fprintf (file, ";; live kill\t");
1624 df_print_regset (file, &bb_info->kill);
1628 /* Debugging info at bottom of bb. */
1630 static void
1631 df_live_bottom_dump (basic_block bb, FILE *file)
1633 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1634 struct df_live_problem_data *problem_data;
1636 if (!bb_info)
1637 return;
1639 fprintf (file, ";; live out \t");
1640 df_print_regset (file, &bb_info->out);
1641 if (df_live->problem_data)
1643 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1644 if (problem_data->out)
1646 fprintf (file, ";; old out \t");
1647 df_print_regset (file, &problem_data->out[bb->index]);
1653 /* Build the datastructure to verify that the solution to the dataflow
1654 equations is not dirty. */
1656 static void
1657 df_live_verify_solution_start (void)
1659 basic_block bb;
1660 struct df_live_problem_data *problem_data;
1661 if (df_live->solutions_dirty)
1662 return;
1664 /* Set it true so that the solution is recomputed. */
1665 df_live->solutions_dirty = true;
1667 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1668 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1669 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1671 FOR_ALL_BB_FN (bb, cfun)
1673 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1674 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1675 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1676 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1681 /* Compare the saved datastructure and the new solution to the dataflow
1682 equations. */
1684 static void
1685 df_live_verify_solution_end (void)
1687 struct df_live_problem_data *problem_data;
1688 basic_block bb;
1690 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1691 if (!problem_data->out)
1692 return;
1694 FOR_ALL_BB_FN (bb, cfun)
1696 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1697 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1699 /*df_dump (stderr);*/
1700 gcc_unreachable ();
1704 /* Cannot delete them immediately because you may want to dump them
1705 if the comparison fails. */
1706 FOR_ALL_BB_FN (bb, cfun)
1708 bitmap_clear (&problem_data->in[bb->index]);
1709 bitmap_clear (&problem_data->out[bb->index]);
1712 free (problem_data->in);
1713 free (problem_data->out);
1714 free (problem_data);
1715 df_live->problem_data = NULL;
1719 /* All of the information associated with every instance of the problem. */
1721 static const struct df_problem problem_LIVE =
1723 DF_LIVE, /* Problem id. */
1724 DF_FORWARD, /* Direction. */
1725 df_live_alloc, /* Allocate the problem specific data. */
1726 df_live_reset, /* Reset global information. */
1727 df_live_free_bb_info, /* Free basic block info. */
1728 df_live_local_compute, /* Local compute function. */
1729 df_live_init, /* Init the solution specific data. */
1730 df_worklist_dataflow, /* Worklist solver. */
1731 NULL, /* Confluence operator 0. */
1732 df_live_confluence_n, /* Confluence operator n. */
1733 df_live_transfer_function, /* Transfer function. */
1734 df_live_finalize, /* Finalize function. */
1735 df_live_free, /* Free all of the problem information. */
1736 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1737 NULL, /* Debugging. */
1738 df_live_top_dump, /* Debugging start block. */
1739 df_live_bottom_dump, /* Debugging end block. */
1740 NULL, /* Debugging start insn. */
1741 NULL, /* Debugging end insn. */
1742 df_live_verify_solution_start,/* Incremental solution verify start. */
1743 df_live_verify_solution_end, /* Incremental solution verify end. */
1744 &problem_LR, /* Dependent problem. */
1745 sizeof (struct df_live_bb_info),/* Size of entry of block_info array. */
1746 TV_DF_LIVE, /* Timing variable. */
1747 false /* Reset blocks on dropping out of blocks_to_analyze. */
1751 /* Create a new DATAFLOW instance and add it to an existing instance
1752 of DF. The returned structure is what is used to get at the
1753 solution. */
1755 void
1756 df_live_add_problem (void)
1758 df_add_problem (&problem_LIVE);
1759 /* These will be initialized when df_scan_blocks processes each
1760 block. */
1761 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1765 /* Set all of the blocks as dirty. This needs to be done if this
1766 problem is added after all of the insns have been scanned. */
1768 void
1769 df_live_set_all_dirty (void)
1771 basic_block bb;
1772 FOR_ALL_BB_FN (bb, cfun)
1773 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1774 bb->index);
1778 /* Verify that all of the lr related info is consistent and
1779 correct. */
1781 void
1782 df_live_verify_transfer_functions (void)
1784 basic_block bb;
1785 bitmap_head saved_gen;
1786 bitmap_head saved_kill;
1787 bitmap_head all_blocks;
1789 if (!df)
1790 return;
1792 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1793 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1794 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1796 df_grow_insn_info ();
1798 FOR_ALL_BB_FN (bb, cfun)
1800 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1801 bitmap_set_bit (&all_blocks, bb->index);
1803 if (bb_info)
1805 /* Make a copy of the transfer functions and then compute
1806 new ones to see if the transfer functions have
1807 changed. */
1808 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1809 bb->index))
1811 bitmap_copy (&saved_gen, &bb_info->gen);
1812 bitmap_copy (&saved_kill, &bb_info->kill);
1813 bitmap_clear (&bb_info->gen);
1814 bitmap_clear (&bb_info->kill);
1816 df_live_bb_local_compute (bb->index);
1817 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1818 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1821 else
1823 /* If we do not have basic block info, the block must be in
1824 the list of dirty blocks or else some one has added a
1825 block behind our backs. */
1826 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1827 bb->index));
1829 /* Make sure no one created a block without following
1830 procedures. */
1831 gcc_assert (df_scan_get_bb_info (bb->index));
1834 /* Make sure there are no dirty bits in blocks that have been deleted. */
1835 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1836 &all_blocks));
1837 bitmap_clear (&saved_gen);
1838 bitmap_clear (&saved_kill);
1839 bitmap_clear (&all_blocks);
1842 /*----------------------------------------------------------------------------
1843 MUST-INITIALIZED REGISTERS.
1844 ----------------------------------------------------------------------------*/
1846 /* Private data used to verify the solution for this problem. */
1847 struct df_mir_problem_data
1849 bitmap_head *in;
1850 bitmap_head *out;
1851 /* An obstack for the bitmaps we need for this problem. */
1852 bitmap_obstack mir_bitmaps;
1856 /* Free basic block info. */
1858 static void
1859 df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1860 void *vbb_info)
1862 struct df_mir_bb_info *bb_info = (struct df_mir_bb_info *) vbb_info;
1863 if (bb_info)
1865 bitmap_clear (&bb_info->gen);
1866 bitmap_clear (&bb_info->kill);
1867 bitmap_clear (&bb_info->in);
1868 bitmap_clear (&bb_info->out);
1873 /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1874 not touched unless the block is new. */
1876 static void
1877 df_mir_alloc (bitmap all_blocks)
1879 unsigned int bb_index;
1880 bitmap_iterator bi;
1881 struct df_mir_problem_data *problem_data;
1883 if (df_mir->problem_data)
1884 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1885 else
1887 problem_data = XNEW (struct df_mir_problem_data);
1888 df_mir->problem_data = problem_data;
1890 problem_data->out = NULL;
1891 problem_data->in = NULL;
1892 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1895 df_grow_bb_info (df_mir);
1897 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1899 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1901 /* When bitmaps are already initialized, just clear them. */
1902 if (bb_info->kill.obstack)
1904 bitmap_clear (&bb_info->kill);
1905 bitmap_clear (&bb_info->gen);
1907 else
1909 bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1910 bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1911 bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1912 bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1913 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1914 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1918 df_mir->optional_p = 1;
1922 /* Reset the global solution for recalculation. */
1924 static void
1925 df_mir_reset (bitmap all_blocks)
1927 unsigned int bb_index;
1928 bitmap_iterator bi;
1930 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1932 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1934 gcc_assert (bb_info);
1936 bitmap_clear (&bb_info->in);
1937 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1938 bitmap_clear (&bb_info->out);
1939 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1944 /* Compute local uninitialized register info for basic block BB. */
1946 static void
1947 df_mir_bb_local_compute (unsigned int bb_index)
1949 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1950 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1951 rtx_insn *insn;
1952 int luid = 0;
1954 /* Ignoring artificial defs is intentional: these often pretend that some
1955 registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
1956 though they are not used for that. As a result, conservatively assume
1957 they may be uninitialized. */
1959 FOR_BB_INSNS (bb, insn)
1961 unsigned int uid = INSN_UID (insn);
1962 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1964 /* Inserting labels does not always trigger the incremental
1965 rescanning. */
1966 if (!insn_info)
1968 gcc_assert (!INSN_P (insn));
1969 insn_info = df_insn_create_insn_record (insn);
1972 DF_INSN_INFO_LUID (insn_info) = luid;
1973 if (!INSN_P (insn))
1974 continue;
1976 luid++;
1977 df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
1982 /* Compute local uninitialized register info. */
1984 static void
1985 df_mir_local_compute (bitmap all_blocks)
1987 unsigned int bb_index;
1988 bitmap_iterator bi;
1990 df_grow_insn_info ();
1992 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1994 df_mir_bb_local_compute (bb_index);
1999 /* Initialize the solution vectors. */
2001 static void
2002 df_mir_init (bitmap all_blocks)
2004 df_mir_reset (all_blocks);
2008 /* Initialize IN sets for blocks with no predecessors: when landing on such
2009 blocks, assume all registers are uninitialized. */
2011 static void
2012 df_mir_confluence_0 (basic_block bb)
2014 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2016 bitmap_clear (&bb_info->in);
2020 /* Forward confluence function that ignores fake edges. */
2022 static bool
2023 df_mir_confluence_n (edge e)
2025 bitmap op1 = &df_mir_get_bb_info (e->dest->index)->in;
2026 bitmap op2 = &df_mir_get_bb_info (e->src->index)->out;
2028 if (e->flags & EDGE_FAKE)
2029 return false;
2031 /* A register is must-initialized at the entry of a basic block iff it is
2032 must-initialized at the exit of all the predecessors. */
2033 return bitmap_and_into (op1, op2);
2037 /* Transfer function for the forwards must-initialized problem. */
2039 static bool
2040 df_mir_transfer_function (int bb_index)
2042 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2043 bitmap in = &bb_info->in;
2044 bitmap out = &bb_info->out;
2045 bitmap gen = &bb_info->gen;
2046 bitmap kill = &bb_info->kill;
2048 return bitmap_ior_and_compl (out, gen, in, kill);
2052 /* Free all storage associated with the problem. */
2054 static void
2055 df_mir_free (void)
2057 struct df_mir_problem_data *problem_data
2058 = (struct df_mir_problem_data *) df_mir->problem_data;
2059 if (df_mir->block_info)
2061 df_mir->block_info_size = 0;
2062 free (df_mir->block_info);
2063 df_mir->block_info = NULL;
2064 bitmap_obstack_release (&problem_data->mir_bitmaps);
2065 free (problem_data);
2066 df_mir->problem_data = NULL;
2068 free (df_mir);
2072 /* Debugging info at top of bb. */
2074 static void
2075 df_mir_top_dump (basic_block bb, FILE *file)
2077 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2079 if (!bb_info)
2080 return;
2082 fprintf (file, ";; mir in \t");
2083 df_print_regset (file, &bb_info->in);
2084 fprintf (file, ";; mir kill\t");
2085 df_print_regset (file, &bb_info->kill);
2086 fprintf (file, ";; mir gen \t");
2087 df_print_regset (file, &bb_info->gen);
2090 /* Debugging info at bottom of bb. */
2092 static void
2093 df_mir_bottom_dump (basic_block bb, FILE *file)
2095 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2097 if (!bb_info)
2098 return;
2100 fprintf (file, ";; mir out \t");
2101 df_print_regset (file, &bb_info->out);
2105 /* Build the datastructure to verify that the solution to the dataflow
2106 equations is not dirty. */
2108 static void
2109 df_mir_verify_solution_start (void)
2111 basic_block bb;
2112 struct df_mir_problem_data *problem_data;
2113 if (df_mir->solutions_dirty)
2114 return;
2116 /* Set it true so that the solution is recomputed. */
2117 df_mir->solutions_dirty = true;
2119 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2120 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2121 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2122 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2124 FOR_ALL_BB_FN (bb, cfun)
2126 bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2127 bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2128 bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2129 bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2134 /* Compare the saved datastructure and the new solution to the dataflow
2135 equations. */
2137 static void
2138 df_mir_verify_solution_end (void)
2140 struct df_mir_problem_data *problem_data;
2141 basic_block bb;
2143 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2144 if (!problem_data->out)
2145 return;
2147 FOR_ALL_BB_FN (bb, cfun)
2149 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2150 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2151 gcc_unreachable ();
2154 /* Cannot delete them immediately because you may want to dump them
2155 if the comparison fails. */
2156 FOR_ALL_BB_FN (bb, cfun)
2158 bitmap_clear (&problem_data->in[bb->index]);
2159 bitmap_clear (&problem_data->out[bb->index]);
2162 free (problem_data->in);
2163 free (problem_data->out);
2164 bitmap_obstack_release (&problem_data->mir_bitmaps);
2165 free (problem_data);
2166 df_mir->problem_data = NULL;
2170 /* All of the information associated with every instance of the problem. */
2172 static const struct df_problem problem_MIR =
2174 DF_MIR, /* Problem id. */
2175 DF_FORWARD, /* Direction. */
2176 df_mir_alloc, /* Allocate the problem specific data. */
2177 df_mir_reset, /* Reset global information. */
2178 df_mir_free_bb_info, /* Free basic block info. */
2179 df_mir_local_compute, /* Local compute function. */
2180 df_mir_init, /* Init the solution specific data. */
2181 df_worklist_dataflow, /* Worklist solver. */
2182 df_mir_confluence_0, /* Confluence operator 0. */
2183 df_mir_confluence_n, /* Confluence operator n. */
2184 df_mir_transfer_function, /* Transfer function. */
2185 NULL, /* Finalize function. */
2186 df_mir_free, /* Free all of the problem information. */
2187 df_mir_free, /* Remove this problem from the stack of dataflow problems. */
2188 NULL, /* Debugging. */
2189 df_mir_top_dump, /* Debugging start block. */
2190 df_mir_bottom_dump, /* Debugging end block. */
2191 NULL, /* Debugging start insn. */
2192 NULL, /* Debugging end insn. */
2193 df_mir_verify_solution_start, /* Incremental solution verify start. */
2194 df_mir_verify_solution_end, /* Incremental solution verify end. */
2195 NULL, /* Dependent problem. */
2196 sizeof (struct df_mir_bb_info),/* Size of entry of block_info array. */
2197 TV_DF_MIR, /* Timing variable. */
2198 false /* Reset blocks on dropping out of blocks_to_analyze. */
2202 /* Create a new DATAFLOW instance and add it to an existing instance
2203 of DF. */
2205 void
2206 df_mir_add_problem (void)
2208 df_add_problem (&problem_MIR);
2209 /* These will be initialized when df_scan_blocks processes each
2210 block. */
2211 df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2215 /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps. */
2217 void
2218 df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2219 bitmap kill, bitmap gen)
2221 df_ref def;
2223 FOR_EACH_INSN_DEF (def, insn)
2225 unsigned int regno = DF_REF_REGNO (def);
2227 /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2228 previous gen is irrelevant (and reciprocally). Also, claim that a
2229 register is GEN only if it is in all cases. */
2230 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2232 bitmap_set_bit (kill, regno);
2233 bitmap_clear_bit (gen, regno);
2235 /* In the worst case, partial and conditional defs can leave bits
2236 uninitialized, so assume they do not change anything. */
2237 else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2239 bitmap_set_bit (gen, regno);
2240 bitmap_clear_bit (kill, regno);
2245 /*----------------------------------------------------------------------------
2246 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2248 Link either the defs to the uses and / or the uses to the defs.
2250 These problems are set up like the other dataflow problems so that
2251 they nicely fit into the framework. They are much simpler and only
2252 involve a single traversal of instructions and an examination of
2253 the reaching defs information (the dependent problem).
2254 ----------------------------------------------------------------------------*/
2256 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2258 /* Create a du or ud chain from SRC to DST and link it into SRC. */
2260 struct df_link *
2261 df_chain_create (df_ref src, df_ref dst)
2263 struct df_link *head = DF_REF_CHAIN (src);
2264 struct df_link *link = df_chain->block_pool->allocate ();
2266 DF_REF_CHAIN (src) = link;
2267 link->next = head;
2268 link->ref = dst;
2269 return link;
2273 /* Delete any du or ud chains that start at REF and point to
2274 TARGET. */
2275 static void
2276 df_chain_unlink_1 (df_ref ref, df_ref target)
2278 struct df_link *chain = DF_REF_CHAIN (ref);
2279 struct df_link *prev = NULL;
2281 while (chain)
2283 if (chain->ref == target)
2285 if (prev)
2286 prev->next = chain->next;
2287 else
2288 DF_REF_CHAIN (ref) = chain->next;
2289 df_chain->block_pool->remove (chain);
2290 return;
2292 prev = chain;
2293 chain = chain->next;
2298 /* Delete a du or ud chain that leave or point to REF. */
2300 void
2301 df_chain_unlink (df_ref ref)
2303 struct df_link *chain = DF_REF_CHAIN (ref);
2304 while (chain)
2306 struct df_link *next = chain->next;
2307 /* Delete the other side if it exists. */
2308 df_chain_unlink_1 (chain->ref, ref);
2309 df_chain->block_pool->remove (chain);
2310 chain = next;
2312 DF_REF_CHAIN (ref) = NULL;
2316 /* Copy the du or ud chain starting at FROM_REF and attach it to
2317 TO_REF. */
2319 void
2320 df_chain_copy (df_ref to_ref,
2321 struct df_link *from_ref)
2323 while (from_ref)
2325 df_chain_create (to_ref, from_ref->ref);
2326 from_ref = from_ref->next;
2331 /* Remove this problem from the stack of dataflow problems. */
2333 static void
2334 df_chain_remove_problem (void)
2336 bitmap_iterator bi;
2337 unsigned int bb_index;
2339 /* Wholesale destruction of the old chains. */
2340 if (df_chain->block_pool)
2341 delete df_chain->block_pool;
2343 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2345 rtx_insn *insn;
2346 df_ref def, use;
2347 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2349 if (df_chain_problem_p (DF_DU_CHAIN))
2350 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2351 DF_REF_CHAIN (def) = NULL;
2352 if (df_chain_problem_p (DF_UD_CHAIN))
2353 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2354 DF_REF_CHAIN (use) = NULL;
2356 FOR_BB_INSNS (bb, insn)
2357 if (INSN_P (insn))
2359 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2360 if (df_chain_problem_p (DF_DU_CHAIN))
2361 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2362 DF_REF_CHAIN (def) = NULL;
2363 if (df_chain_problem_p (DF_UD_CHAIN))
2365 FOR_EACH_INSN_INFO_USE (use, insn_info)
2366 DF_REF_CHAIN (use) = NULL;
2367 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2368 DF_REF_CHAIN (use) = NULL;
2373 bitmap_clear (df_chain->out_of_date_transfer_functions);
2374 df_chain->block_pool = NULL;
2378 /* Remove the chain problem completely. */
2380 static void
2381 df_chain_fully_remove_problem (void)
2383 df_chain_remove_problem ();
2384 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2385 free (df_chain);
2389 /* Create def-use or use-def chains. */
2391 static void
2392 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2394 df_chain_remove_problem ();
2395 df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2396 df_chain->optional_p = true;
2400 /* Reset all of the chains when the set of basic blocks changes. */
2402 static void
2403 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2405 df_chain_remove_problem ();
2409 /* Create the chains for a list of USEs. */
2411 static void
2412 df_chain_create_bb_process_use (bitmap local_rd,
2413 df_ref use,
2414 int top_flag)
2416 bitmap_iterator bi;
2417 unsigned int def_index;
2419 for (; use; use = DF_REF_NEXT_LOC (use))
2421 unsigned int uregno = DF_REF_REGNO (use);
2422 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2423 || (uregno >= FIRST_PSEUDO_REGISTER))
2425 /* Do not want to go through this for an uninitialized var. */
2426 int count = DF_DEFS_COUNT (uregno);
2427 if (count)
2429 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2431 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2432 unsigned int last_index = first_index + count - 1;
2434 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2436 df_ref def;
2437 if (def_index > last_index)
2438 break;
2440 def = DF_DEFS_GET (def_index);
2441 if (df_chain_problem_p (DF_DU_CHAIN))
2442 df_chain_create (def, use);
2443 if (df_chain_problem_p (DF_UD_CHAIN))
2444 df_chain_create (use, def);
2453 /* Create chains from reaching defs bitmaps for basic block BB. */
2455 static void
2456 df_chain_create_bb (unsigned int bb_index)
2458 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2459 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2460 rtx_insn *insn;
2461 bitmap_head cpy;
2463 bitmap_initialize (&cpy, &bitmap_default_obstack);
2464 bitmap_copy (&cpy, &bb_info->in);
2465 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2467 /* Since we are going forwards, process the artificial uses first
2468 then the artificial defs second. */
2470 #ifdef EH_USES
2471 /* Create the chains for the artificial uses from the EH_USES at the
2472 beginning of the block. */
2474 /* Artificials are only hard regs. */
2475 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2476 df_chain_create_bb_process_use (&cpy,
2477 df_get_artificial_uses (bb->index),
2478 DF_REF_AT_TOP);
2479 #endif
2481 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2483 /* Process the regular instructions next. */
2484 FOR_BB_INSNS (bb, insn)
2485 if (INSN_P (insn))
2487 unsigned int uid = INSN_UID (insn);
2489 /* First scan the uses and link them up with the defs that remain
2490 in the cpy vector. */
2491 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2492 if (df->changeable_flags & DF_EQ_NOTES)
2493 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2495 /* Since we are going forwards, process the defs second. */
2496 df_rd_simulate_one_insn (bb, insn, &cpy);
2499 /* Create the chains for the artificial uses of the hard registers
2500 at the end of the block. */
2501 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2502 df_chain_create_bb_process_use (&cpy,
2503 df_get_artificial_uses (bb->index),
2506 bitmap_clear (&cpy);
2509 /* Create def-use chains from reaching use bitmaps for basic blocks
2510 in BLOCKS. */
2512 static void
2513 df_chain_finalize (bitmap all_blocks)
2515 unsigned int bb_index;
2516 bitmap_iterator bi;
2518 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2520 df_chain_create_bb (bb_index);
2525 /* Free all storage associated with the problem. */
2527 static void
2528 df_chain_free (void)
2530 delete df_chain->block_pool;
2531 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2532 free (df_chain);
2536 /* Debugging info. */
2538 static void
2539 df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2541 /* Artificials are only hard regs. */
2542 if (df->changeable_flags & DF_NO_HARD_REGS)
2543 return;
2544 if (df_chain_problem_p (DF_UD_CHAIN))
2546 df_ref use;
2548 fprintf (file,
2549 ";; UD chains for artificial uses at %s\n",
2550 top ? "top" : "bottom");
2551 FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2552 if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2553 || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2555 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2556 df_chain_dump (DF_REF_CHAIN (use), file);
2557 fprintf (file, "\n");
2560 if (df_chain_problem_p (DF_DU_CHAIN))
2562 df_ref def;
2564 fprintf (file,
2565 ";; DU chains for artificial defs at %s\n",
2566 top ? "top" : "bottom");
2567 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2568 if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2569 || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2571 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2572 df_chain_dump (DF_REF_CHAIN (def), file);
2573 fprintf (file, "\n");
2578 static void
2579 df_chain_top_dump (basic_block bb, FILE *file)
2581 df_chain_bb_dump (bb, file, /*top=*/true);
2584 static void
2585 df_chain_bottom_dump (basic_block bb, FILE *file)
2587 df_chain_bb_dump (bb, file, /*top=*/false);
2590 static void
2591 df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2593 if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2595 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2596 df_ref use;
2598 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2599 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2600 FOR_EACH_INSN_INFO_USE (use, insn_info)
2601 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2602 || !(df->changeable_flags & DF_NO_HARD_REGS))
2604 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2605 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2606 fprintf (file, "read/write ");
2607 df_chain_dump (DF_REF_CHAIN (use), file);
2608 fprintf (file, "\n");
2610 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2611 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2612 || !(df->changeable_flags & DF_NO_HARD_REGS))
2614 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2615 df_chain_dump (DF_REF_CHAIN (use), file);
2616 fprintf (file, "\n");
2621 static void
2622 df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2624 if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2626 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2627 df_ref def;
2628 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2629 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2630 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2631 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2632 || !(df->changeable_flags & DF_NO_HARD_REGS))
2634 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2635 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2636 fprintf (file, "read/write ");
2637 df_chain_dump (DF_REF_CHAIN (def), file);
2638 fprintf (file, "\n");
2640 fprintf (file, "\n");
2644 static const struct df_problem problem_CHAIN =
2646 DF_CHAIN, /* Problem id. */
2647 DF_NONE, /* Direction. */
2648 df_chain_alloc, /* Allocate the problem specific data. */
2649 df_chain_reset, /* Reset global information. */
2650 NULL, /* Free basic block info. */
2651 NULL, /* Local compute function. */
2652 NULL, /* Init the solution specific data. */
2653 NULL, /* Iterative solver. */
2654 NULL, /* Confluence operator 0. */
2655 NULL, /* Confluence operator n. */
2656 NULL, /* Transfer function. */
2657 df_chain_finalize, /* Finalize function. */
2658 df_chain_free, /* Free all of the problem information. */
2659 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2660 NULL, /* Debugging. */
2661 df_chain_top_dump, /* Debugging start block. */
2662 df_chain_bottom_dump, /* Debugging end block. */
2663 df_chain_insn_top_dump, /* Debugging start insn. */
2664 df_chain_insn_bottom_dump, /* Debugging end insn. */
2665 NULL, /* Incremental solution verify start. */
2666 NULL, /* Incremental solution verify end. */
2667 &problem_RD, /* Dependent problem. */
2668 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
2669 TV_DF_CHAIN, /* Timing variable. */
2670 false /* Reset blocks on dropping out of blocks_to_analyze. */
2674 /* Create a new DATAFLOW instance and add it to an existing instance
2675 of DF. The returned structure is what is used to get at the
2676 solution. */
2678 void
2679 df_chain_add_problem (unsigned int chain_flags)
2681 df_add_problem (&problem_CHAIN);
2682 df_chain->local_flags = chain_flags;
2683 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2686 #undef df_chain_problem_p
2689 /*----------------------------------------------------------------------------
2690 WORD LEVEL LIVE REGISTERS
2692 Find the locations in the function where any use of a pseudo can
2693 reach in the backwards direction. In and out bitvectors are built
2694 for each basic block. We only track pseudo registers that have a
2695 size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2696 contain two bits corresponding to each of the subwords.
2698 ----------------------------------------------------------------------------*/
2700 /* Private data used to verify the solution for this problem. */
2701 struct df_word_lr_problem_data
2703 /* An obstack for the bitmaps we need for this problem. */
2704 bitmap_obstack word_lr_bitmaps;
2708 /* Free basic block info. */
2710 static void
2711 df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2712 void *vbb_info)
2714 struct df_word_lr_bb_info *bb_info = (struct df_word_lr_bb_info *) vbb_info;
2715 if (bb_info)
2717 bitmap_clear (&bb_info->use);
2718 bitmap_clear (&bb_info->def);
2719 bitmap_clear (&bb_info->in);
2720 bitmap_clear (&bb_info->out);
2725 /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2726 not touched unless the block is new. */
2728 static void
2729 df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2731 unsigned int bb_index;
2732 bitmap_iterator bi;
2733 basic_block bb;
2734 struct df_word_lr_problem_data *problem_data
2735 = XNEW (struct df_word_lr_problem_data);
2737 df_word_lr->problem_data = problem_data;
2739 df_grow_bb_info (df_word_lr);
2741 /* Create the mapping from regnos to slots. This does not change
2742 unless the problem is destroyed and recreated. In particular, if
2743 we end up deleting the only insn that used a subreg, we do not
2744 want to redo the mapping because this would invalidate everything
2745 else. */
2747 bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2749 FOR_EACH_BB_FN (bb, cfun)
2750 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2752 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2753 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2755 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2757 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2759 /* When bitmaps are already initialized, just clear them. */
2760 if (bb_info->use.obstack)
2762 bitmap_clear (&bb_info->def);
2763 bitmap_clear (&bb_info->use);
2765 else
2767 bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2768 bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2769 bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2770 bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2774 df_word_lr->optional_p = true;
2778 /* Reset the global solution for recalculation. */
2780 static void
2781 df_word_lr_reset (bitmap all_blocks)
2783 unsigned int bb_index;
2784 bitmap_iterator bi;
2786 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2788 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2789 gcc_assert (bb_info);
2790 bitmap_clear (&bb_info->in);
2791 bitmap_clear (&bb_info->out);
2795 /* Examine REF, and if it is for a reg we're interested in, set or
2796 clear the bits corresponding to its subwords from the bitmap
2797 according to IS_SET. LIVE is the bitmap we should update. We do
2798 not track hard regs or pseudos of any size other than 2 *
2799 UNITS_PER_WORD.
2800 We return true if we changed the bitmap, or if we encountered a register
2801 we're not tracking. */
2803 bool
2804 df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2806 rtx orig_reg = DF_REF_REG (ref);
2807 rtx reg = orig_reg;
2808 machine_mode reg_mode;
2809 unsigned regno;
2810 /* Left at -1 for whole accesses. */
2811 int which_subword = -1;
2812 bool changed = false;
2814 if (GET_CODE (reg) == SUBREG)
2815 reg = SUBREG_REG (orig_reg);
2816 regno = REGNO (reg);
2817 reg_mode = GET_MODE (reg);
2818 if (regno < FIRST_PSEUDO_REGISTER
2819 || GET_MODE_SIZE (reg_mode) != 2 * UNITS_PER_WORD)
2820 return true;
2822 if (GET_CODE (orig_reg) == SUBREG
2823 && df_read_modify_subreg_p (orig_reg))
2825 gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2826 if (subreg_lowpart_p (orig_reg))
2827 which_subword = 0;
2828 else
2829 which_subword = 1;
2831 if (is_set)
2833 if (which_subword != 1)
2834 changed |= bitmap_set_bit (live, regno * 2);
2835 if (which_subword != 0)
2836 changed |= bitmap_set_bit (live, regno * 2 + 1);
2838 else
2840 if (which_subword != 1)
2841 changed |= bitmap_clear_bit (live, regno * 2);
2842 if (which_subword != 0)
2843 changed |= bitmap_clear_bit (live, regno * 2 + 1);
2845 return changed;
2848 /* Compute local live register info for basic block BB. */
2850 static void
2851 df_word_lr_bb_local_compute (unsigned int bb_index)
2853 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2854 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2855 rtx_insn *insn;
2856 df_ref def, use;
2858 /* Ensure that artificial refs don't contain references to pseudos. */
2859 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2860 gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2862 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2863 gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2865 FOR_BB_INSNS_REVERSE (bb, insn)
2867 if (!NONDEBUG_INSN_P (insn))
2868 continue;
2870 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2871 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2872 /* If the def is to only part of the reg, it does
2873 not kill the other defs that reach here. */
2874 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2876 df_word_lr_mark_ref (def, true, &bb_info->def);
2877 df_word_lr_mark_ref (def, false, &bb_info->use);
2879 FOR_EACH_INSN_INFO_USE (use, insn_info)
2880 df_word_lr_mark_ref (use, true, &bb_info->use);
2885 /* Compute local live register info for each basic block within BLOCKS. */
2887 static void
2888 df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2890 unsigned int bb_index;
2891 bitmap_iterator bi;
2893 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2895 if (bb_index == EXIT_BLOCK)
2897 unsigned regno;
2898 bitmap_iterator bi;
2899 EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2900 regno, bi)
2901 gcc_unreachable ();
2903 else
2904 df_word_lr_bb_local_compute (bb_index);
2907 bitmap_clear (df_word_lr->out_of_date_transfer_functions);
2911 /* Initialize the solution vectors. */
2913 static void
2914 df_word_lr_init (bitmap all_blocks)
2916 unsigned int bb_index;
2917 bitmap_iterator bi;
2919 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2921 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2922 bitmap_copy (&bb_info->in, &bb_info->use);
2923 bitmap_clear (&bb_info->out);
2928 /* Confluence function that ignores fake edges. */
2930 static bool
2931 df_word_lr_confluence_n (edge e)
2933 bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
2934 bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
2936 return bitmap_ior_into (op1, op2);
2940 /* Transfer function. */
2942 static bool
2943 df_word_lr_transfer_function (int bb_index)
2945 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2946 bitmap in = &bb_info->in;
2947 bitmap out = &bb_info->out;
2948 bitmap use = &bb_info->use;
2949 bitmap def = &bb_info->def;
2951 return bitmap_ior_and_compl (in, use, out, def);
2955 /* Free all storage associated with the problem. */
2957 static void
2958 df_word_lr_free (void)
2960 struct df_word_lr_problem_data *problem_data
2961 = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
2963 if (df_word_lr->block_info)
2965 df_word_lr->block_info_size = 0;
2966 free (df_word_lr->block_info);
2967 df_word_lr->block_info = NULL;
2970 BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
2971 bitmap_obstack_release (&problem_data->word_lr_bitmaps);
2972 free (problem_data);
2973 free (df_word_lr);
2977 /* Debugging info at top of bb. */
2979 static void
2980 df_word_lr_top_dump (basic_block bb, FILE *file)
2982 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
2983 if (!bb_info)
2984 return;
2986 fprintf (file, ";; blr in \t");
2987 df_print_word_regset (file, &bb_info->in);
2988 fprintf (file, ";; blr use \t");
2989 df_print_word_regset (file, &bb_info->use);
2990 fprintf (file, ";; blr def \t");
2991 df_print_word_regset (file, &bb_info->def);
2995 /* Debugging info at bottom of bb. */
2997 static void
2998 df_word_lr_bottom_dump (basic_block bb, FILE *file)
3000 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3001 if (!bb_info)
3002 return;
3004 fprintf (file, ";; blr out \t");
3005 df_print_word_regset (file, &bb_info->out);
3009 /* All of the information associated with every instance of the problem. */
3011 static const struct df_problem problem_WORD_LR =
3013 DF_WORD_LR, /* Problem id. */
3014 DF_BACKWARD, /* Direction. */
3015 df_word_lr_alloc, /* Allocate the problem specific data. */
3016 df_word_lr_reset, /* Reset global information. */
3017 df_word_lr_free_bb_info, /* Free basic block info. */
3018 df_word_lr_local_compute, /* Local compute function. */
3019 df_word_lr_init, /* Init the solution specific data. */
3020 df_worklist_dataflow, /* Worklist solver. */
3021 NULL, /* Confluence operator 0. */
3022 df_word_lr_confluence_n, /* Confluence operator n. */
3023 df_word_lr_transfer_function, /* Transfer function. */
3024 NULL, /* Finalize function. */
3025 df_word_lr_free, /* Free all of the problem information. */
3026 df_word_lr_free, /* Remove this problem from the stack of dataflow problems. */
3027 NULL, /* Debugging. */
3028 df_word_lr_top_dump, /* Debugging start block. */
3029 df_word_lr_bottom_dump, /* Debugging end block. */
3030 NULL, /* Debugging start insn. */
3031 NULL, /* Debugging end insn. */
3032 NULL, /* Incremental solution verify start. */
3033 NULL, /* Incremental solution verify end. */
3034 NULL, /* Dependent problem. */
3035 sizeof (struct df_word_lr_bb_info),/* Size of entry of block_info array. */
3036 TV_DF_WORD_LR, /* Timing variable. */
3037 false /* Reset blocks on dropping out of blocks_to_analyze. */
3041 /* Create a new DATAFLOW instance and add it to an existing instance
3042 of DF. The returned structure is what is used to get at the
3043 solution. */
3045 void
3046 df_word_lr_add_problem (void)
3048 df_add_problem (&problem_WORD_LR);
3049 /* These will be initialized when df_scan_blocks processes each
3050 block. */
3051 df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3055 /* Simulate the effects of the defs of INSN on LIVE. Return true if we changed
3056 any bits, which is used by the caller to determine whether a set is
3057 necessary. We also return true if there are other reasons not to delete
3058 an insn. */
3060 bool
3061 df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3063 bool changed = false;
3064 df_ref def;
3066 FOR_EACH_INSN_DEF (def, insn)
3067 if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3068 changed = true;
3069 else
3070 changed |= df_word_lr_mark_ref (def, false, live);
3071 return changed;
3075 /* Simulate the effects of the uses of INSN on LIVE. */
3077 void
3078 df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3080 df_ref use;
3082 FOR_EACH_INSN_USE (use, insn)
3083 df_word_lr_mark_ref (use, true, live);
3086 /*----------------------------------------------------------------------------
3087 This problem computes REG_DEAD and REG_UNUSED notes.
3088 ----------------------------------------------------------------------------*/
3090 static void
3091 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3093 df_note->optional_p = true;
3096 /* This is only used if REG_DEAD_DEBUGGING is in effect. */
3097 static void
3098 df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3100 if (dump_file)
3102 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3103 print_rtl (dump_file, note);
3104 fprintf (dump_file, "\n");
3109 /* After reg-stack, the x86 floating point stack regs are difficult to
3110 analyze because of all of the pushes, pops and rotations. Thus, we
3111 just leave the notes alone. */
3113 #ifdef STACK_REGS
3114 static inline bool
3115 df_ignore_stack_reg (int regno)
3117 return regstack_completed
3118 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3120 #else
3121 static inline bool
3122 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3124 return false;
3126 #endif
3129 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN. */
3131 static void
3132 df_remove_dead_and_unused_notes (rtx_insn *insn)
3134 rtx *pprev = &REG_NOTES (insn);
3135 rtx link = *pprev;
3137 while (link)
3139 switch (REG_NOTE_KIND (link))
3141 case REG_DEAD:
3142 /* After reg-stack, we need to ignore any unused notes
3143 for the stack registers. */
3144 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3146 pprev = &XEXP (link, 1);
3147 link = *pprev;
3149 else
3151 rtx next = XEXP (link, 1);
3152 if (REG_DEAD_DEBUGGING)
3153 df_print_note ("deleting: ", insn, link);
3154 free_EXPR_LIST_node (link);
3155 *pprev = link = next;
3157 break;
3159 case REG_UNUSED:
3160 /* After reg-stack, we need to ignore any unused notes
3161 for the stack registers. */
3162 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3164 pprev = &XEXP (link, 1);
3165 link = *pprev;
3167 else
3169 rtx next = XEXP (link, 1);
3170 if (REG_DEAD_DEBUGGING)
3171 df_print_note ("deleting: ", insn, link);
3172 free_EXPR_LIST_node (link);
3173 *pprev = link = next;
3175 break;
3177 default:
3178 pprev = &XEXP (link, 1);
3179 link = *pprev;
3180 break;
3185 /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3186 as the bitmap of currently live registers. */
3188 static void
3189 df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3191 rtx *pprev = &REG_NOTES (insn);
3192 rtx link = *pprev;
3194 while (link)
3196 switch (REG_NOTE_KIND (link))
3198 case REG_EQUAL:
3199 case REG_EQUIV:
3201 /* Remove the notes that refer to dead registers. As we have at most
3202 one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3203 so we need to purge the complete EQ_USES vector when removing
3204 the note using df_notes_rescan. */
3205 df_ref use;
3206 bool deleted = false;
3208 FOR_EACH_INSN_EQ_USE (use, insn)
3209 if (DF_REF_REGNO (use) > FIRST_PSEUDO_REGISTER
3210 && DF_REF_LOC (use)
3211 && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3212 && !bitmap_bit_p (live, DF_REF_REGNO (use))
3213 && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3215 deleted = true;
3216 break;
3218 if (deleted)
3220 rtx next;
3221 if (REG_DEAD_DEBUGGING)
3222 df_print_note ("deleting: ", insn, link);
3223 next = XEXP (link, 1);
3224 free_EXPR_LIST_node (link);
3225 *pprev = link = next;
3226 df_notes_rescan (insn);
3228 else
3230 pprev = &XEXP (link, 1);
3231 link = *pprev;
3233 break;
3236 default:
3237 pprev = &XEXP (link, 1);
3238 link = *pprev;
3239 break;
3244 /* Set a NOTE_TYPE note for REG in INSN. */
3246 static inline void
3247 df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3249 gcc_checking_assert (!DEBUG_INSN_P (insn));
3250 add_reg_note (insn, note_type, reg);
3253 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3254 arguments. Return true if the register value described by MWS's
3255 mw_reg is known to be completely unused, and if mw_reg can therefore
3256 be used in a REG_UNUSED note. */
3258 static bool
3259 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3260 bitmap live, bitmap artificial_uses)
3262 unsigned int r;
3264 /* If MWS describes a partial reference, create REG_UNUSED notes for
3265 individual hard registers. */
3266 if (mws->flags & DF_REF_PARTIAL)
3267 return false;
3269 /* Likewise if some part of the register is used. */
3270 for (r = mws->start_regno; r <= mws->end_regno; r++)
3271 if (bitmap_bit_p (live, r)
3272 || bitmap_bit_p (artificial_uses, r))
3273 return false;
3275 gcc_assert (REG_P (mws->mw_reg));
3276 return true;
3280 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3281 based on the bits in LIVE. Do not generate notes for registers in
3282 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3283 not generated if the reg is both read and written by the
3284 instruction.
3287 static void
3288 df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3289 bitmap live, bitmap do_not_gen,
3290 bitmap artificial_uses,
3291 struct dead_debug_local *debug)
3293 unsigned int r;
3295 if (REG_DEAD_DEBUGGING && dump_file)
3296 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3297 mws->start_regno, mws->end_regno);
3299 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3301 unsigned int regno = mws->start_regno;
3302 df_set_note (REG_UNUSED, insn, mws->mw_reg);
3303 dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3305 if (REG_DEAD_DEBUGGING)
3306 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3308 bitmap_set_bit (do_not_gen, regno);
3309 /* Only do this if the value is totally dead. */
3311 else
3312 for (r = mws->start_regno; r <= mws->end_regno; r++)
3314 if (!bitmap_bit_p (live, r)
3315 && !bitmap_bit_p (artificial_uses, r))
3317 df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3318 dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3319 if (REG_DEAD_DEBUGGING)
3320 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3322 bitmap_set_bit (do_not_gen, r);
3327 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3328 arguments. Return true if the register value described by MWS's
3329 mw_reg is known to be completely dead, and if mw_reg can therefore
3330 be used in a REG_DEAD note. */
3332 static bool
3333 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3334 bitmap live, bitmap artificial_uses,
3335 bitmap do_not_gen)
3337 unsigned int r;
3339 /* If MWS describes a partial reference, create REG_DEAD notes for
3340 individual hard registers. */
3341 if (mws->flags & DF_REF_PARTIAL)
3342 return false;
3344 /* Likewise if some part of the register is not dead. */
3345 for (r = mws->start_regno; r <= mws->end_regno; r++)
3346 if (bitmap_bit_p (live, r)
3347 || bitmap_bit_p (artificial_uses, r)
3348 || bitmap_bit_p (do_not_gen, r))
3349 return false;
3351 gcc_assert (REG_P (mws->mw_reg));
3352 return true;
3355 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3356 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3357 from being set if the instruction both reads and writes the
3358 register. */
3360 static void
3361 df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3362 bitmap live, bitmap do_not_gen,
3363 bitmap artificial_uses, bool *added_notes_p)
3365 unsigned int r;
3366 bool is_debug = *added_notes_p;
3368 *added_notes_p = false;
3370 if (REG_DEAD_DEBUGGING && dump_file)
3372 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3373 mws->start_regno, mws->end_regno);
3374 df_print_regset (dump_file, do_not_gen);
3375 fprintf (dump_file, " live =");
3376 df_print_regset (dump_file, live);
3377 fprintf (dump_file, " artificial uses =");
3378 df_print_regset (dump_file, artificial_uses);
3381 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3383 if (is_debug)
3385 *added_notes_p = true;
3386 return;
3388 /* Add a dead note for the entire multi word register. */
3389 df_set_note (REG_DEAD, insn, mws->mw_reg);
3390 if (REG_DEAD_DEBUGGING)
3391 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3393 else
3395 for (r = mws->start_regno; r <= mws->end_regno; r++)
3396 if (!bitmap_bit_p (live, r)
3397 && !bitmap_bit_p (artificial_uses, r)
3398 && !bitmap_bit_p (do_not_gen, r))
3400 if (is_debug)
3402 *added_notes_p = true;
3403 return;
3405 df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3406 if (REG_DEAD_DEBUGGING)
3407 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3410 return;
3414 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3415 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3417 static void
3418 df_create_unused_note (rtx_insn *insn, df_ref def,
3419 bitmap live, bitmap artificial_uses,
3420 struct dead_debug_local *debug)
3422 unsigned int dregno = DF_REF_REGNO (def);
3424 if (REG_DEAD_DEBUGGING && dump_file)
3426 fprintf (dump_file, " regular looking at def ");
3427 df_ref_debug (def, dump_file);
3430 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3431 || bitmap_bit_p (live, dregno)
3432 || bitmap_bit_p (artificial_uses, dregno)
3433 || df_ignore_stack_reg (dregno)))
3435 rtx reg = (DF_REF_LOC (def))
3436 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3437 df_set_note (REG_UNUSED, insn, reg);
3438 dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3439 if (REG_DEAD_DEBUGGING)
3440 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3443 return;
3447 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3448 info: lifetime, bb, and number of defs and uses for basic block
3449 BB. The three bitvectors are scratch regs used here. */
3451 static void
3452 df_note_bb_compute (unsigned int bb_index,
3453 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3455 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3456 rtx_insn *insn;
3457 df_ref def, use;
3458 struct dead_debug_local debug;
3460 dead_debug_local_init (&debug, NULL, NULL);
3462 bitmap_copy (live, df_get_live_out (bb));
3463 bitmap_clear (artificial_uses);
3465 if (REG_DEAD_DEBUGGING && dump_file)
3467 fprintf (dump_file, "live at bottom ");
3468 df_print_regset (dump_file, live);
3471 /* Process the artificial defs and uses at the bottom of the block
3472 to begin processing. */
3473 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3475 if (REG_DEAD_DEBUGGING && dump_file)
3476 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3478 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3479 bitmap_clear_bit (live, DF_REF_REGNO (def));
3482 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3483 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3485 unsigned int regno = DF_REF_REGNO (use);
3486 bitmap_set_bit (live, regno);
3488 /* Notes are not generated for any of the artificial registers
3489 at the bottom of the block. */
3490 bitmap_set_bit (artificial_uses, regno);
3493 if (REG_DEAD_DEBUGGING && dump_file)
3495 fprintf (dump_file, "live before artificials out ");
3496 df_print_regset (dump_file, live);
3499 FOR_BB_INSNS_REVERSE (bb, insn)
3501 if (!INSN_P (insn))
3502 continue;
3504 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3505 df_mw_hardreg *mw;
3506 int debug_insn;
3508 debug_insn = DEBUG_INSN_P (insn);
3510 bitmap_clear (do_not_gen);
3511 df_remove_dead_and_unused_notes (insn);
3513 /* Process the defs. */
3514 if (CALL_P (insn))
3516 if (REG_DEAD_DEBUGGING && dump_file)
3518 fprintf (dump_file, "processing call %d\n live =",
3519 INSN_UID (insn));
3520 df_print_regset (dump_file, live);
3523 /* We only care about real sets for calls. Clobbers cannot
3524 be depended on to really die. */
3525 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3526 if ((DF_MWS_REG_DEF_P (mw))
3527 && !df_ignore_stack_reg (mw->start_regno))
3528 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3529 artificial_uses, &debug);
3531 /* All of the defs except the return value are some sort of
3532 clobber. This code is for the return. */
3533 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3535 unsigned int dregno = DF_REF_REGNO (def);
3536 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3538 df_create_unused_note (insn,
3539 def, live, artificial_uses, &debug);
3540 bitmap_set_bit (do_not_gen, dregno);
3543 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3544 bitmap_clear_bit (live, dregno);
3547 else
3549 /* Regular insn. */
3550 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3551 if (DF_MWS_REG_DEF_P (mw))
3552 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3553 artificial_uses, &debug);
3555 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3557 unsigned int dregno = DF_REF_REGNO (def);
3558 df_create_unused_note (insn,
3559 def, live, artificial_uses, &debug);
3561 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3562 bitmap_set_bit (do_not_gen, dregno);
3564 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3565 bitmap_clear_bit (live, dregno);
3569 /* Process the uses. */
3570 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3571 if (DF_MWS_REG_USE_P (mw)
3572 && !df_ignore_stack_reg (mw->start_regno))
3574 bool really_add_notes = debug_insn != 0;
3576 df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3577 artificial_uses,
3578 &really_add_notes);
3580 if (really_add_notes)
3581 debug_insn = -1;
3584 FOR_EACH_INSN_INFO_USE (use, insn_info)
3586 unsigned int uregno = DF_REF_REGNO (use);
3588 if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3590 fprintf (dump_file, " regular looking at use ");
3591 df_ref_debug (use, dump_file);
3594 if (!bitmap_bit_p (live, uregno))
3596 if (debug_insn)
3598 if (debug_insn > 0)
3600 /* We won't add REG_UNUSED or REG_DEAD notes for
3601 these, so we don't have to mess with them in
3602 debug insns either. */
3603 if (!bitmap_bit_p (artificial_uses, uregno)
3604 && !df_ignore_stack_reg (uregno))
3605 dead_debug_add (&debug, use, uregno);
3606 continue;
3608 break;
3610 else
3611 dead_debug_insert_temp (&debug, uregno, insn,
3612 DEBUG_TEMP_BEFORE_WITH_REG);
3614 if ( (!(DF_REF_FLAGS (use)
3615 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3616 && (!bitmap_bit_p (do_not_gen, uregno))
3617 && (!bitmap_bit_p (artificial_uses, uregno))
3618 && (!df_ignore_stack_reg (uregno)))
3620 rtx reg = (DF_REF_LOC (use))
3621 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3622 df_set_note (REG_DEAD, insn, reg);
3624 if (REG_DEAD_DEBUGGING)
3625 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3627 /* This register is now live. */
3628 bitmap_set_bit (live, uregno);
3632 df_remove_dead_eq_notes (insn, live);
3634 if (debug_insn == -1)
3636 /* ??? We could probably do better here, replacing dead
3637 registers with their definitions. */
3638 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3639 df_insn_rescan_debug_internal (insn);
3643 dead_debug_local_finish (&debug, NULL);
3647 /* Compute register info: lifetime, bb, and number of defs and uses. */
3648 static void
3649 df_note_compute (bitmap all_blocks)
3651 unsigned int bb_index;
3652 bitmap_iterator bi;
3653 bitmap_head live, do_not_gen, artificial_uses;
3655 bitmap_initialize (&live, &df_bitmap_obstack);
3656 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3657 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3659 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3661 /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3662 pseudos in debug insns because we don't always (re)visit blocks
3663 with death points after visiting dead uses. Even changing this
3664 loop to postorder would still leave room for visiting a death
3665 point before visiting a subsequent debug use. */
3666 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3669 bitmap_clear (&live);
3670 bitmap_clear (&do_not_gen);
3671 bitmap_clear (&artificial_uses);
3675 /* Free all storage associated with the problem. */
3677 static void
3678 df_note_free (void)
3680 free (df_note);
3684 /* All of the information associated every instance of the problem. */
3686 static const struct df_problem problem_NOTE =
3688 DF_NOTE, /* Problem id. */
3689 DF_NONE, /* Direction. */
3690 df_note_alloc, /* Allocate the problem specific data. */
3691 NULL, /* Reset global information. */
3692 NULL, /* Free basic block info. */
3693 df_note_compute, /* Local compute function. */
3694 NULL, /* Init the solution specific data. */
3695 NULL, /* Iterative solver. */
3696 NULL, /* Confluence operator 0. */
3697 NULL, /* Confluence operator n. */
3698 NULL, /* Transfer function. */
3699 NULL, /* Finalize function. */
3700 df_note_free, /* Free all of the problem information. */
3701 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3702 NULL, /* Debugging. */
3703 NULL, /* Debugging start block. */
3704 NULL, /* Debugging end block. */
3705 NULL, /* Debugging start insn. */
3706 NULL, /* Debugging end insn. */
3707 NULL, /* Incremental solution verify start. */
3708 NULL, /* Incremental solution verify end. */
3709 &problem_LR, /* Dependent problem. */
3710 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
3711 TV_DF_NOTE, /* Timing variable. */
3712 false /* Reset blocks on dropping out of blocks_to_analyze. */
3716 /* Create a new DATAFLOW instance and add it to an existing instance
3717 of DF. The returned structure is what is used to get at the
3718 solution. */
3720 void
3721 df_note_add_problem (void)
3723 df_add_problem (&problem_NOTE);
3729 /*----------------------------------------------------------------------------
3730 Functions for simulating the effects of single insns.
3732 You can either simulate in the forwards direction, starting from
3733 the top of a block or the backwards direction from the end of the
3734 block. If you go backwards, defs are examined first to clear bits,
3735 then uses are examined to set bits. If you go forwards, defs are
3736 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3737 are examined to clear bits. In either case, the result of examining
3738 a def can be undone (respectively by a use or a REG_UNUSED note).
3740 If you start at the top of the block, use one of DF_LIVE_IN or
3741 DF_LR_IN. If you start at the bottom of the block use one of
3742 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3743 THEY WILL BE DESTROYED.
3744 ----------------------------------------------------------------------------*/
3747 /* Find the set of DEFs for INSN. */
3749 void
3750 df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3752 df_ref def;
3754 FOR_EACH_INSN_DEF (def, insn)
3755 bitmap_set_bit (defs, DF_REF_REGNO (def));
3758 /* Find the set of uses for INSN. This includes partial defs. */
3760 static void
3761 df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3763 df_ref def, use;
3764 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3766 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3767 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3768 bitmap_set_bit (uses, DF_REF_REGNO (def));
3769 FOR_EACH_INSN_INFO_USE (use, insn_info)
3770 bitmap_set_bit (uses, DF_REF_REGNO (use));
3773 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3775 void
3776 df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3778 df_ref def;
3780 FOR_EACH_INSN_DEF (def, insn)
3781 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3782 bitmap_set_bit (defs, DF_REF_REGNO (def));
3786 /* Simulate the effects of the defs of INSN on LIVE. */
3788 void
3789 df_simulate_defs (rtx_insn *insn, bitmap live)
3791 df_ref def;
3793 FOR_EACH_INSN_DEF (def, insn)
3795 unsigned int dregno = DF_REF_REGNO (def);
3797 /* If the def is to only part of the reg, it does
3798 not kill the other defs that reach here. */
3799 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3800 bitmap_clear_bit (live, dregno);
3805 /* Simulate the effects of the uses of INSN on LIVE. */
3807 void
3808 df_simulate_uses (rtx_insn *insn, bitmap live)
3810 df_ref use;
3812 if (DEBUG_INSN_P (insn))
3813 return;
3815 FOR_EACH_INSN_USE (use, insn)
3816 /* Add use to set of uses in this BB. */
3817 bitmap_set_bit (live, DF_REF_REGNO (use));
3821 /* Add back the always live regs in BB to LIVE. */
3823 static inline void
3824 df_simulate_fixup_sets (basic_block bb, bitmap live)
3826 /* These regs are considered always live so if they end up dying
3827 because of some def, we need to bring the back again. */
3828 if (bb_has_eh_pred (bb))
3829 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3830 else
3831 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3835 /*----------------------------------------------------------------------------
3836 The following three functions are used only for BACKWARDS scanning:
3837 i.e. they process the defs before the uses.
3839 df_simulate_initialize_backwards should be called first with a
3840 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
3841 df_simulate_one_insn_backwards should be called for each insn in
3842 the block, starting with the last one. Finally,
3843 df_simulate_finalize_backwards can be called to get a new value
3844 of the sets at the top of the block (this is rarely used).
3845 ----------------------------------------------------------------------------*/
3847 /* Apply the artificial uses and defs at the end of BB in a backwards
3848 direction. */
3850 void
3851 df_simulate_initialize_backwards (basic_block bb, bitmap live)
3853 df_ref def, use;
3854 int bb_index = bb->index;
3856 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3857 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3858 bitmap_clear_bit (live, DF_REF_REGNO (def));
3860 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3861 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3862 bitmap_set_bit (live, DF_REF_REGNO (use));
3866 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
3868 void
3869 df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3871 if (!NONDEBUG_INSN_P (insn))
3872 return;
3874 df_simulate_defs (insn, live);
3875 df_simulate_uses (insn, live);
3876 df_simulate_fixup_sets (bb, live);
3880 /* Apply the artificial uses and defs at the top of BB in a backwards
3881 direction. */
3883 void
3884 df_simulate_finalize_backwards (basic_block bb, bitmap live)
3886 df_ref def;
3887 #ifdef EH_USES
3888 df_ref use;
3889 #endif
3890 int bb_index = bb->index;
3892 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3893 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3894 bitmap_clear_bit (live, DF_REF_REGNO (def));
3896 #ifdef EH_USES
3897 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3898 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3899 bitmap_set_bit (live, DF_REF_REGNO (use));
3900 #endif
3902 /*----------------------------------------------------------------------------
3903 The following three functions are used only for FORWARDS scanning:
3904 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
3905 Thus it is important to add the DF_NOTES problem to the stack of
3906 problems computed before using these functions.
3908 df_simulate_initialize_forwards should be called first with a
3909 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
3910 df_simulate_one_insn_forwards should be called for each insn in
3911 the block, starting with the first one.
3912 ----------------------------------------------------------------------------*/
3914 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
3915 DF_LR_IN for basic block BB, for forward scanning by marking artificial
3916 defs live. */
3918 void
3919 df_simulate_initialize_forwards (basic_block bb, bitmap live)
3921 df_ref def;
3922 int bb_index = bb->index;
3924 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3925 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3926 bitmap_set_bit (live, DF_REF_REGNO (def));
3929 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
3931 void
3932 df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
3934 rtx link;
3935 if (! INSN_P (insn))
3936 return;
3938 /* Make sure that DF_NOTE really is an active df problem. */
3939 gcc_assert (df_note);
3941 /* Note that this is the opposite as how the problem is defined, because
3942 in the LR problem defs _kill_ liveness. However, they do so backwards,
3943 while here the scan is performed forwards! So, first assume that the
3944 def is live, and if this is not true REG_UNUSED notes will rectify the
3945 situation. */
3946 df_simulate_find_noclobber_defs (insn, live);
3948 /* Clear all of the registers that go dead. */
3949 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3951 switch (REG_NOTE_KIND (link))
3953 case REG_DEAD:
3954 case REG_UNUSED:
3956 rtx reg = XEXP (link, 0);
3957 bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
3959 break;
3960 default:
3961 break;
3964 df_simulate_fixup_sets (bb, live);
3967 /* Used by the next two functions to encode information about the
3968 memory references we found. */
3969 #define MEMREF_NORMAL 1
3970 #define MEMREF_VOLATILE 2
3972 /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X. */
3974 static int
3975 find_memory (rtx_insn *insn)
3977 int flags = 0;
3978 subrtx_iterator::array_type array;
3979 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3981 const_rtx x = *iter;
3982 if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
3983 flags |= MEMREF_VOLATILE;
3984 else if (MEM_P (x))
3986 if (MEM_VOLATILE_P (x))
3987 flags |= MEMREF_VOLATILE;
3988 else if (!MEM_READONLY_P (x))
3989 flags |= MEMREF_NORMAL;
3992 return flags;
3995 /* A subroutine of can_move_insns_across_p called through note_stores.
3996 DATA points to an integer in which we set either the bit for
3997 MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
3998 of either kind. */
4000 static void
4001 find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4002 void *data ATTRIBUTE_UNUSED)
4004 int *pflags = (int *)data;
4005 if (GET_CODE (x) == SUBREG)
4006 x = XEXP (x, 0);
4007 /* Treat stores to SP as stores to memory, this will prevent problems
4008 when there are references to the stack frame. */
4009 if (x == stack_pointer_rtx)
4010 *pflags |= MEMREF_VOLATILE;
4011 if (!MEM_P (x))
4012 return;
4013 *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4016 /* Scan BB backwards, using df_simulate functions to keep track of
4017 lifetimes, up to insn POINT. The result is stored in LIVE. */
4019 void
4020 simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4022 rtx_insn *insn;
4023 bitmap_copy (live, df_get_live_out (bb));
4024 df_simulate_initialize_backwards (bb, live);
4026 /* Scan and update life information until we reach the point we're
4027 interested in. */
4028 for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4029 df_simulate_one_insn_backwards (bb, insn, live);
4032 /* Return true if it is safe to move a group of insns, described by
4033 the range FROM to TO, backwards across another group of insns,
4034 described by ACROSS_FROM to ACROSS_TO. It is assumed that there
4035 are no insns between ACROSS_TO and FROM, but they may be in
4036 different basic blocks; MERGE_BB is the block from which the
4037 insns will be moved. The caller must pass in a regset MERGE_LIVE
4038 which specifies the registers live after TO.
4040 This function may be called in one of two cases: either we try to
4041 move identical instructions from all successor blocks into their
4042 predecessor, or we try to move from only one successor block. If
4043 OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4044 the second case. It should contain a set of registers live at the
4045 end of ACROSS_TO which must not be clobbered by moving the insns.
4046 In that case, we're also more careful about moving memory references
4047 and trapping insns.
4049 We return false if it is not safe to move the entire group, but it
4050 may still be possible to move a subgroup. PMOVE_UPTO, if nonnull,
4051 is set to point at the last moveable insn in such a case. */
4053 bool
4054 can_move_insns_across (rtx_insn *from, rtx_insn *to,
4055 rtx_insn *across_from, rtx_insn *across_to,
4056 basic_block merge_bb, regset merge_live,
4057 regset other_branch_live, rtx_insn **pmove_upto)
4059 rtx_insn *insn, *next, *max_to;
4060 bitmap merge_set, merge_use, local_merge_live;
4061 bitmap test_set, test_use;
4062 unsigned i, fail = 0;
4063 bitmap_iterator bi;
4064 int memrefs_in_across = 0;
4065 int mem_sets_in_across = 0;
4066 bool trapping_insns_in_across = false;
4068 if (pmove_upto != NULL)
4069 *pmove_upto = NULL;
4071 /* Find real bounds, ignoring debug insns. */
4072 while (!NONDEBUG_INSN_P (from) && from != to)
4073 from = NEXT_INSN (from);
4074 while (!NONDEBUG_INSN_P (to) && from != to)
4075 to = PREV_INSN (to);
4077 for (insn = across_to; ; insn = next)
4079 if (CALL_P (insn))
4081 if (RTL_CONST_OR_PURE_CALL_P (insn))
4082 /* Pure functions can read from memory. Const functions can
4083 read from arguments that the ABI has forced onto the stack.
4084 Neither sort of read can be volatile. */
4085 memrefs_in_across |= MEMREF_NORMAL;
4086 else
4088 memrefs_in_across |= MEMREF_VOLATILE;
4089 mem_sets_in_across |= MEMREF_VOLATILE;
4092 if (NONDEBUG_INSN_P (insn))
4094 if (volatile_insn_p (PATTERN (insn)))
4095 return false;
4096 memrefs_in_across |= find_memory (insn);
4097 note_stores (PATTERN (insn), find_memory_stores,
4098 &mem_sets_in_across);
4099 /* This is used just to find sets of the stack pointer. */
4100 memrefs_in_across |= mem_sets_in_across;
4101 trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4103 next = PREV_INSN (insn);
4104 if (insn == across_from)
4105 break;
4108 /* Collect:
4109 MERGE_SET = set of registers set in MERGE_BB
4110 MERGE_USE = set of registers used in MERGE_BB and live at its top
4111 MERGE_LIVE = set of registers live at the point inside the MERGE
4112 range that we've reached during scanning
4113 TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4114 TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4115 and live before ACROSS_FROM. */
4117 merge_set = BITMAP_ALLOC (&reg_obstack);
4118 merge_use = BITMAP_ALLOC (&reg_obstack);
4119 local_merge_live = BITMAP_ALLOC (&reg_obstack);
4120 test_set = BITMAP_ALLOC (&reg_obstack);
4121 test_use = BITMAP_ALLOC (&reg_obstack);
4123 /* Compute the set of registers set and used in the ACROSS range. */
4124 if (other_branch_live != NULL)
4125 bitmap_copy (test_use, other_branch_live);
4126 df_simulate_initialize_backwards (merge_bb, test_use);
4127 for (insn = across_to; ; insn = next)
4129 if (NONDEBUG_INSN_P (insn))
4131 df_simulate_find_defs (insn, test_set);
4132 df_simulate_defs (insn, test_use);
4133 df_simulate_uses (insn, test_use);
4135 next = PREV_INSN (insn);
4136 if (insn == across_from)
4137 break;
4140 /* Compute an upper bound for the amount of insns moved, by finding
4141 the first insn in MERGE that sets a register in TEST_USE, or uses
4142 a register in TEST_SET. We also check for calls, trapping operations,
4143 and memory references. */
4144 max_to = NULL;
4145 for (insn = from; ; insn = next)
4147 if (CALL_P (insn))
4148 break;
4149 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4150 break;
4151 if (NONDEBUG_INSN_P (insn))
4153 if (may_trap_or_fault_p (PATTERN (insn))
4154 && (trapping_insns_in_across
4155 || other_branch_live != NULL
4156 || volatile_insn_p (PATTERN (insn))))
4157 break;
4159 /* We cannot move memory stores past each other, or move memory
4160 reads past stores, at least not without tracking them and
4161 calling true_dependence on every pair.
4163 If there is no other branch and no memory references or
4164 sets in the ACROSS range, we can move memory references
4165 freely, even volatile ones.
4167 Otherwise, the rules are as follows: volatile memory
4168 references and stores can't be moved at all, and any type
4169 of memory reference can't be moved if there are volatile
4170 accesses or stores in the ACROSS range. That leaves
4171 normal reads, which can be moved, as the trapping case is
4172 dealt with elsewhere. */
4173 if (other_branch_live != NULL || memrefs_in_across != 0)
4175 int mem_ref_flags = 0;
4176 int mem_set_flags = 0;
4177 note_stores (PATTERN (insn), find_memory_stores, &mem_set_flags);
4178 mem_ref_flags = find_memory (insn);
4179 /* Catch sets of the stack pointer. */
4180 mem_ref_flags |= mem_set_flags;
4182 if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4183 break;
4184 if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4185 break;
4186 if (mem_set_flags != 0
4187 || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4188 break;
4190 df_simulate_find_uses (insn, merge_use);
4191 /* We're only interested in uses which use a value live at
4192 the top, not one previously set in this block. */
4193 bitmap_and_compl_into (merge_use, merge_set);
4194 df_simulate_find_defs (insn, merge_set);
4195 if (bitmap_intersect_p (merge_set, test_use)
4196 || bitmap_intersect_p (merge_use, test_set))
4197 break;
4198 if (!HAVE_cc0 || !sets_cc0_p (insn))
4199 max_to = insn;
4201 next = NEXT_INSN (insn);
4202 if (insn == to)
4203 break;
4205 if (max_to != to)
4206 fail = 1;
4208 if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4209 goto out;
4211 /* Now, lower this upper bound by also taking into account that
4212 a range of insns moved across ACROSS must not leave a register
4213 live at the end that will be clobbered in ACROSS. We need to
4214 find a point where TEST_SET & LIVE == 0.
4216 Insns in the MERGE range that set registers which are also set
4217 in the ACROSS range may still be moved as long as we also move
4218 later insns which use the results of the set, and make the
4219 register dead again. This is verified by the condition stated
4220 above. We only need to test it for registers that are set in
4221 the moved region.
4223 MERGE_LIVE is provided by the caller and holds live registers after
4224 TO. */
4225 bitmap_copy (local_merge_live, merge_live);
4226 for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4227 df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4229 /* We're not interested in registers that aren't set in the moved
4230 region at all. */
4231 bitmap_and_into (local_merge_live, merge_set);
4232 for (;;)
4234 if (NONDEBUG_INSN_P (insn))
4236 if (!bitmap_intersect_p (test_set, local_merge_live)
4237 && (!HAVE_cc0 || !sets_cc0_p (insn)))
4239 max_to = insn;
4240 break;
4243 df_simulate_one_insn_backwards (merge_bb, insn,
4244 local_merge_live);
4246 if (insn == from)
4248 fail = 1;
4249 goto out;
4251 insn = PREV_INSN (insn);
4254 if (max_to != to)
4255 fail = 1;
4257 if (pmove_upto)
4258 *pmove_upto = max_to;
4260 /* For small register class machines, don't lengthen lifetimes of
4261 hard registers before reload. */
4262 if (! reload_completed
4263 && targetm.small_register_classes_for_mode_p (VOIDmode))
4265 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4267 if (i < FIRST_PSEUDO_REGISTER
4268 && ! fixed_regs[i]
4269 && ! global_regs[i])
4271 fail = 1;
4272 break;
4277 out:
4278 BITMAP_FREE (merge_set);
4279 BITMAP_FREE (merge_use);
4280 BITMAP_FREE (local_merge_live);
4281 BITMAP_FREE (test_set);
4282 BITMAP_FREE (test_use);
4284 return !fail;
4288 /*----------------------------------------------------------------------------
4289 MULTIPLE DEFINITIONS
4291 Find the locations in the function reached by multiple definition sites
4292 for a live pseudo. In and out bitvectors are built for each basic
4293 block. They are restricted for efficiency to live registers.
4295 The gen and kill sets for the problem are obvious. Together they
4296 include all defined registers in a basic block; the gen set includes
4297 registers where a partial or conditional or may-clobber definition is
4298 last in the BB, while the kill set includes registers with a complete
4299 definition coming last. However, the computation of the dataflow
4300 itself is interesting.
4302 The idea behind it comes from SSA form's iterated dominance frontier
4303 criterion for inserting PHI functions. Just like in that case, we can use
4304 the dominance frontier to find places where multiple definitions meet;
4305 a register X defined in a basic block BB1 has multiple definitions in
4306 basic blocks in BB1's dominance frontier.
4308 So, the in-set of a basic block BB2 is not just the union of the
4309 out-sets of BB2's predecessors, but includes some more bits that come
4310 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4311 the previous paragraph). I called this set the init-set of BB2.
4313 (Note: I actually use the kill-set only to build the init-set.
4314 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4316 For example, if you have
4318 BB1 : r10 = 0
4319 r11 = 0
4320 if <...> goto BB2 else goto BB3;
4322 BB2 : r10 = 1
4323 r12 = 1
4324 goto BB3;
4326 BB3 :
4328 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4329 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4330 not need to iterate the dominance frontier, because we do not insert
4331 anything like PHI functions there! Instead, dataflow will take care of
4332 propagating the information to BB3's successors.
4333 ---------------------------------------------------------------------------*/
4335 /* Private data used to verify the solution for this problem. */
4336 struct df_md_problem_data
4338 /* An obstack for the bitmaps we need for this problem. */
4339 bitmap_obstack md_bitmaps;
4342 /* Scratch var used by transfer functions. This is used to do md analysis
4343 only for live registers. */
4344 static bitmap_head df_md_scratch;
4347 static void
4348 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4349 void *vbb_info)
4351 struct df_md_bb_info *bb_info = (struct df_md_bb_info *) vbb_info;
4352 if (bb_info)
4354 bitmap_clear (&bb_info->kill);
4355 bitmap_clear (&bb_info->gen);
4356 bitmap_clear (&bb_info->init);
4357 bitmap_clear (&bb_info->in);
4358 bitmap_clear (&bb_info->out);
4363 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4364 not touched unless the block is new. */
4366 static void
4367 df_md_alloc (bitmap all_blocks)
4369 unsigned int bb_index;
4370 bitmap_iterator bi;
4371 struct df_md_problem_data *problem_data;
4373 df_grow_bb_info (df_md);
4374 if (df_md->problem_data)
4375 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4376 else
4378 problem_data = XNEW (struct df_md_problem_data);
4379 df_md->problem_data = problem_data;
4380 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4382 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4384 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4386 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4387 /* When bitmaps are already initialized, just clear them. */
4388 if (bb_info->init.obstack)
4390 bitmap_clear (&bb_info->init);
4391 bitmap_clear (&bb_info->gen);
4392 bitmap_clear (&bb_info->kill);
4393 bitmap_clear (&bb_info->in);
4394 bitmap_clear (&bb_info->out);
4396 else
4398 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4399 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4400 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4401 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4402 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4406 df_md->optional_p = true;
4409 /* Add the effect of the top artificial defs of BB to the multiple definitions
4410 bitmap LOCAL_MD. */
4412 void
4413 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4415 int bb_index = bb->index;
4416 df_ref def;
4417 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4418 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4420 unsigned int dregno = DF_REF_REGNO (def);
4421 if (DF_REF_FLAGS (def)
4422 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4423 bitmap_set_bit (local_md, dregno);
4424 else
4425 bitmap_clear_bit (local_md, dregno);
4430 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4431 LOCAL_MD. */
4433 void
4434 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4435 bitmap local_md)
4437 df_ref def;
4439 FOR_EACH_INSN_DEF (def, insn)
4441 unsigned int dregno = DF_REF_REGNO (def);
4442 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4443 || (dregno >= FIRST_PSEUDO_REGISTER))
4445 if (DF_REF_FLAGS (def)
4446 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4447 bitmap_set_bit (local_md, DF_REF_ID (def));
4448 else
4449 bitmap_clear_bit (local_md, DF_REF_ID (def));
4454 static void
4455 df_md_bb_local_compute_process_def (struct df_md_bb_info *bb_info,
4456 df_ref def,
4457 int top_flag)
4459 bitmap_clear (&seen_in_insn);
4461 for (; def; def = DF_REF_NEXT_LOC (def))
4463 unsigned int dregno = DF_REF_REGNO (def);
4464 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4465 || (dregno >= FIRST_PSEUDO_REGISTER))
4466 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4468 if (!bitmap_bit_p (&seen_in_insn, dregno))
4470 if (DF_REF_FLAGS (def)
4471 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4473 bitmap_set_bit (&bb_info->gen, dregno);
4474 bitmap_clear_bit (&bb_info->kill, dregno);
4476 else
4478 /* When we find a clobber and a regular def,
4479 make sure the regular def wins. */
4480 bitmap_set_bit (&seen_in_insn, dregno);
4481 bitmap_set_bit (&bb_info->kill, dregno);
4482 bitmap_clear_bit (&bb_info->gen, dregno);
4490 /* Compute local multiple def info for basic block BB. */
4492 static void
4493 df_md_bb_local_compute (unsigned int bb_index)
4495 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4496 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4497 rtx_insn *insn;
4499 /* Artificials are only hard regs. */
4500 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4501 df_md_bb_local_compute_process_def (bb_info,
4502 df_get_artificial_defs (bb_index),
4503 DF_REF_AT_TOP);
4505 FOR_BB_INSNS (bb, insn)
4507 unsigned int uid = INSN_UID (insn);
4508 if (!INSN_P (insn))
4509 continue;
4511 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4514 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4515 df_md_bb_local_compute_process_def (bb_info,
4516 df_get_artificial_defs (bb_index),
4520 /* Compute local reaching def info for each basic block within BLOCKS. */
4522 static void
4523 df_md_local_compute (bitmap all_blocks)
4525 unsigned int bb_index, df_bb_index;
4526 bitmap_iterator bi1, bi2;
4527 basic_block bb;
4528 bitmap_head *frontiers;
4530 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4532 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4534 df_md_bb_local_compute (bb_index);
4537 bitmap_clear (&seen_in_insn);
4539 frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4540 FOR_ALL_BB_FN (bb, cfun)
4541 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4543 compute_dominance_frontiers (frontiers);
4545 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4546 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4548 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4549 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4551 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4552 if (bitmap_bit_p (all_blocks, df_bb_index))
4553 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4554 df_get_live_in (bb));
4558 FOR_ALL_BB_FN (bb, cfun)
4559 bitmap_clear (&frontiers[bb->index]);
4560 free (frontiers);
4564 /* Reset the global solution for recalculation. */
4566 static void
4567 df_md_reset (bitmap all_blocks)
4569 unsigned int bb_index;
4570 bitmap_iterator bi;
4572 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4574 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4575 gcc_assert (bb_info);
4576 bitmap_clear (&bb_info->in);
4577 bitmap_clear (&bb_info->out);
4581 static bool
4582 df_md_transfer_function (int bb_index)
4584 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4585 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4586 bitmap in = &bb_info->in;
4587 bitmap out = &bb_info->out;
4588 bitmap gen = &bb_info->gen;
4589 bitmap kill = &bb_info->kill;
4591 /* We need to use a scratch set here so that the value returned from this
4592 function invocation properly reflects whether the sets changed in a
4593 significant way; i.e. not just because the live set was anded in. */
4594 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4596 /* Multiple definitions of a register are not relevant if it is not
4597 live. Thus we trim the result to the places where it is live. */
4598 bitmap_and_into (in, df_get_live_in (bb));
4600 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4603 /* Initialize the solution bit vectors for problem. */
4605 static void
4606 df_md_init (bitmap all_blocks)
4608 unsigned int bb_index;
4609 bitmap_iterator bi;
4611 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4613 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4615 bitmap_copy (&bb_info->in, &bb_info->init);
4616 df_md_transfer_function (bb_index);
4620 static void
4621 df_md_confluence_0 (basic_block bb)
4623 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4624 bitmap_copy (&bb_info->in, &bb_info->init);
4627 /* In of target gets or of out of source. */
4629 static bool
4630 df_md_confluence_n (edge e)
4632 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4633 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4635 if (e->flags & EDGE_FAKE)
4636 return false;
4638 if (e->flags & EDGE_EH)
4639 return bitmap_ior_and_compl_into (op1, op2,
4640 regs_invalidated_by_call_regset);
4641 else
4642 return bitmap_ior_into (op1, op2);
4645 /* Free all storage associated with the problem. */
4647 static void
4648 df_md_free (void)
4650 struct df_md_problem_data *problem_data
4651 = (struct df_md_problem_data *) df_md->problem_data;
4653 bitmap_obstack_release (&problem_data->md_bitmaps);
4654 free (problem_data);
4655 df_md->problem_data = NULL;
4657 df_md->block_info_size = 0;
4658 free (df_md->block_info);
4659 df_md->block_info = NULL;
4660 free (df_md);
4664 /* Debugging info at top of bb. */
4666 static void
4667 df_md_top_dump (basic_block bb, FILE *file)
4669 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4670 if (!bb_info)
4671 return;
4673 fprintf (file, ";; md in \t");
4674 df_print_regset (file, &bb_info->in);
4675 fprintf (file, ";; md init \t");
4676 df_print_regset (file, &bb_info->init);
4677 fprintf (file, ";; md gen \t");
4678 df_print_regset (file, &bb_info->gen);
4679 fprintf (file, ";; md kill \t");
4680 df_print_regset (file, &bb_info->kill);
4683 /* Debugging info at bottom of bb. */
4685 static void
4686 df_md_bottom_dump (basic_block bb, FILE *file)
4688 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4689 if (!bb_info)
4690 return;
4692 fprintf (file, ";; md out \t");
4693 df_print_regset (file, &bb_info->out);
4696 static const struct df_problem problem_MD =
4698 DF_MD, /* Problem id. */
4699 DF_FORWARD, /* Direction. */
4700 df_md_alloc, /* Allocate the problem specific data. */
4701 df_md_reset, /* Reset global information. */
4702 df_md_free_bb_info, /* Free basic block info. */
4703 df_md_local_compute, /* Local compute function. */
4704 df_md_init, /* Init the solution specific data. */
4705 df_worklist_dataflow, /* Worklist solver. */
4706 df_md_confluence_0, /* Confluence operator 0. */
4707 df_md_confluence_n, /* Confluence operator n. */
4708 df_md_transfer_function, /* Transfer function. */
4709 NULL, /* Finalize function. */
4710 df_md_free, /* Free all of the problem information. */
4711 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4712 NULL, /* Debugging. */
4713 df_md_top_dump, /* Debugging start block. */
4714 df_md_bottom_dump, /* Debugging end block. */
4715 NULL, /* Debugging start insn. */
4716 NULL, /* Debugging end insn. */
4717 NULL, /* Incremental solution verify start. */
4718 NULL, /* Incremental solution verify end. */
4719 NULL, /* Dependent problem. */
4720 sizeof (struct df_md_bb_info),/* Size of entry of block_info array. */
4721 TV_DF_MD, /* Timing variable. */
4722 false /* Reset blocks on dropping out of blocks_to_analyze. */
4725 /* Create a new MD instance and add it to the existing instance
4726 of DF. */
4728 void
4729 df_md_add_problem (void)
4731 df_add_problem (&problem_MD);