Clean up some minor white space issues in trans-decl.c and trans-expr.c
[official-gcc.git] / gcc / df-problems.c
blob63138881c24509c590b9a11e0d07c8ba77439edb
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)
521 bitmap_clear (out);
522 bb_info->out = tmp;
524 else
525 bitmap_clear (&tmp);
528 if (df->changeable_flags & DF_RD_PRUNE_DEAD_DEFS)
530 /* Create a mask of DEFs for all registers live at the end of this
531 basic block, and mask out DEFs of registers that are not live.
532 Computing the mask looks costly, but the benefit of the pruning
533 outweighs the cost. */
534 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
535 bitmap regs_live_out = &df_lr_get_bb_info (bb_index)->out;
536 bitmap live_defs = BITMAP_ALLOC (&df_bitmap_obstack);
537 unsigned int regno;
538 bitmap_iterator bi;
540 EXECUTE_IF_SET_IN_BITMAP (regs_live_out, 0, regno, bi)
541 bitmap_set_range (live_defs,
542 DF_DEFS_BEGIN (regno),
543 DF_DEFS_COUNT (regno));
544 changed |= bitmap_and_into (&bb_info->out, live_defs);
545 BITMAP_FREE (live_defs);
548 return changed;
551 /* Free all storage associated with the problem. */
553 static void
554 df_rd_free (void)
556 struct df_rd_problem_data *problem_data
557 = (struct df_rd_problem_data *) df_rd->problem_data;
559 if (problem_data)
561 bitmap_obstack_release (&problem_data->rd_bitmaps);
563 df_rd->block_info_size = 0;
564 free (df_rd->block_info);
565 df_rd->block_info = NULL;
566 free (df_rd->problem_data);
568 free (df_rd);
572 /* Debugging info. */
574 static void
575 df_rd_start_dump (FILE *file)
577 struct df_rd_problem_data *problem_data
578 = (struct df_rd_problem_data *) df_rd->problem_data;
579 unsigned int m = DF_REG_SIZE (df);
580 unsigned int regno;
582 if (!df_rd->block_info)
583 return;
585 fprintf (file, ";; Reaching defs:\n");
587 fprintf (file, ";; sparse invalidated \t");
588 dump_bitmap (file, &problem_data->sparse_invalidated_by_call);
589 fprintf (file, ";; dense invalidated \t");
590 dump_bitmap (file, &problem_data->dense_invalidated_by_call);
592 fprintf (file, ";; reg->defs[] map:\t");
593 for (regno = 0; regno < m; regno++)
594 if (DF_DEFS_COUNT (regno))
595 fprintf (file, "%d[%d,%d] ", regno,
596 DF_DEFS_BEGIN (regno),
597 DF_DEFS_BEGIN (regno) + DF_DEFS_COUNT (regno) - 1);
598 fprintf (file, "\n");
602 static void
603 df_rd_dump_defs_set (bitmap defs_set, const char *prefix, FILE *file)
605 bitmap_head tmp;
606 unsigned int regno;
607 unsigned int m = DF_REG_SIZE (df);
608 bool first_reg = true;
610 fprintf (file, "%s\t(%d) ", prefix, (int) bitmap_count_bits (defs_set));
612 bitmap_initialize (&tmp, &df_bitmap_obstack);
613 for (regno = 0; regno < m; regno++)
615 if (HARD_REGISTER_NUM_P (regno)
616 && (df->changeable_flags & DF_NO_HARD_REGS))
617 continue;
618 bitmap_set_range (&tmp, DF_DEFS_BEGIN (regno), DF_DEFS_COUNT (regno));
619 bitmap_and_into (&tmp, defs_set);
620 if (! bitmap_empty_p (&tmp))
622 bitmap_iterator bi;
623 unsigned int ix;
624 bool first_def = true;
626 if (! first_reg)
627 fprintf (file, ",");
628 first_reg = false;
630 fprintf (file, "%u[", regno);
631 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, ix, bi)
633 fprintf (file, "%s%u", first_def ? "" : ",", ix);
634 first_def = false;
636 fprintf (file, "]");
638 bitmap_clear (&tmp);
641 fprintf (file, "\n");
642 bitmap_clear (&tmp);
645 /* Debugging info at top of bb. */
647 static void
648 df_rd_top_dump (basic_block bb, FILE *file)
650 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
651 if (!bb_info)
652 return;
654 df_rd_dump_defs_set (&bb_info->in, ";; rd in ", file);
655 df_rd_dump_defs_set (&bb_info->gen, ";; rd gen ", file);
656 df_rd_dump_defs_set (&bb_info->kill, ";; rd kill", file);
660 /* Debugging info at bottom of bb. */
662 static void
663 df_rd_bottom_dump (basic_block bb, FILE *file)
665 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
666 if (!bb_info)
667 return;
669 df_rd_dump_defs_set (&bb_info->out, ";; rd out ", file);
672 /* All of the information associated with every instance of the problem. */
674 static struct df_problem problem_RD =
676 DF_RD, /* Problem id. */
677 DF_FORWARD, /* Direction. */
678 df_rd_alloc, /* Allocate the problem specific data. */
679 NULL, /* Reset global information. */
680 df_rd_free_bb_info, /* Free basic block info. */
681 df_rd_local_compute, /* Local compute function. */
682 df_rd_init_solution, /* Init the solution specific data. */
683 df_worklist_dataflow, /* Worklist solver. */
684 NULL, /* Confluence operator 0. */
685 df_rd_confluence_n, /* Confluence operator n. */
686 df_rd_transfer_function, /* Transfer function. */
687 NULL, /* Finalize function. */
688 df_rd_free, /* Free all of the problem information. */
689 df_rd_free, /* Remove this problem from the stack of dataflow problems. */
690 df_rd_start_dump, /* Debugging. */
691 df_rd_top_dump, /* Debugging start block. */
692 df_rd_bottom_dump, /* Debugging end block. */
693 NULL, /* Debugging start insn. */
694 NULL, /* Debugging end insn. */
695 NULL, /* Incremental solution verify start. */
696 NULL, /* Incremental solution verify end. */
697 NULL, /* Dependent problem. */
698 sizeof (struct df_rd_bb_info),/* Size of entry of block_info array. */
699 TV_DF_RD, /* Timing variable. */
700 true /* Reset blocks on dropping out of blocks_to_analyze. */
705 /* Create a new RD instance and add it to the existing instance
706 of DF. */
708 void
709 df_rd_add_problem (void)
711 df_add_problem (&problem_RD);
716 /*----------------------------------------------------------------------------
717 LIVE REGISTERS
719 Find the locations in the function where any use of a pseudo can
720 reach in the backwards direction. In and out bitvectors are built
721 for each basic block. The regno is used to index into these sets.
722 See df.h for details.
723 ----------------------------------------------------------------------------*/
725 /* Private data used to verify the solution for this problem. */
726 struct df_lr_problem_data
728 bitmap_head *in;
729 bitmap_head *out;
730 /* An obstack for the bitmaps we need for this problem. */
731 bitmap_obstack lr_bitmaps;
734 /* Free basic block info. */
736 static void
737 df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
738 void *vbb_info)
740 struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
741 if (bb_info)
743 bitmap_clear (&bb_info->use);
744 bitmap_clear (&bb_info->def);
745 bitmap_clear (&bb_info->in);
746 bitmap_clear (&bb_info->out);
751 /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
752 not touched unless the block is new. */
754 static void
755 df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
757 unsigned int bb_index;
758 bitmap_iterator bi;
759 struct df_lr_problem_data *problem_data;
761 df_grow_bb_info (df_lr);
762 if (df_lr->problem_data)
763 problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
764 else
766 problem_data = XNEW (struct df_lr_problem_data);
767 df_lr->problem_data = problem_data;
769 problem_data->out = NULL;
770 problem_data->in = NULL;
771 bitmap_obstack_initialize (&problem_data->lr_bitmaps);
774 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
776 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
778 /* When bitmaps are already initialized, just clear them. */
779 if (bb_info->use.obstack)
781 bitmap_clear (&bb_info->def);
782 bitmap_clear (&bb_info->use);
784 else
786 bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
787 bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
788 bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
789 bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
793 df_lr->optional_p = false;
797 /* Reset the global solution for recalculation. */
799 static void
800 df_lr_reset (bitmap all_blocks)
802 unsigned int bb_index;
803 bitmap_iterator bi;
805 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
807 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
808 gcc_assert (bb_info);
809 bitmap_clear (&bb_info->in);
810 bitmap_clear (&bb_info->out);
815 /* Compute local live register info for basic block BB. */
817 static void
818 df_lr_bb_local_compute (unsigned int bb_index)
820 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
821 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
822 rtx_insn *insn;
823 df_ref def, use;
825 /* Process the registers set in an exception handler. */
826 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
827 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
829 unsigned int dregno = DF_REF_REGNO (def);
830 bitmap_set_bit (&bb_info->def, dregno);
831 bitmap_clear_bit (&bb_info->use, dregno);
834 /* Process the hardware registers that are always live. */
835 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
836 /* Add use to set of uses in this BB. */
837 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
838 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
840 FOR_BB_INSNS_REVERSE (bb, insn)
842 if (!NONDEBUG_INSN_P (insn))
843 continue;
845 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
846 FOR_EACH_INSN_INFO_DEF (def, insn_info)
847 /* If the def is to only part of the reg, it does
848 not kill the other defs that reach here. */
849 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
851 unsigned int dregno = DF_REF_REGNO (def);
852 bitmap_set_bit (&bb_info->def, dregno);
853 bitmap_clear_bit (&bb_info->use, dregno);
856 FOR_EACH_INSN_INFO_USE (use, insn_info)
857 /* Add use to set of uses in this BB. */
858 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
861 /* Process the registers set in an exception handler or the hard
862 frame pointer if this block is the target of a non local
863 goto. */
864 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
865 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
867 unsigned int dregno = DF_REF_REGNO (def);
868 bitmap_set_bit (&bb_info->def, dregno);
869 bitmap_clear_bit (&bb_info->use, dregno);
872 #ifdef EH_USES
873 /* Process the uses that are live into an exception handler. */
874 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
875 /* Add use to set of uses in this BB. */
876 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
877 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
878 #endif
880 /* If the df_live problem is not defined, such as at -O0 and -O1, we
881 still need to keep the luids up to date. This is normally done
882 in the df_live problem since this problem has a forwards
883 scan. */
884 if (!df_live)
885 df_recompute_luids (bb);
889 /* Compute local live register info for each basic block within BLOCKS. */
891 static void
892 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
894 unsigned int bb_index, i;
895 bitmap_iterator bi;
897 bitmap_clear (&df->hardware_regs_used);
899 /* The all-important stack pointer must always be live. */
900 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
902 /* Global regs are always live, too. */
903 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
904 if (global_regs[i])
905 bitmap_set_bit (&df->hardware_regs_used, i);
907 /* Before reload, there are a few registers that must be forced
908 live everywhere -- which might not already be the case for
909 blocks within infinite loops. */
910 if (!reload_completed)
912 unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
913 /* Any reference to any pseudo before reload is a potential
914 reference of the frame pointer. */
915 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
917 /* Pseudos with argument area equivalences may require
918 reloading via the argument pointer. */
919 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
920 && fixed_regs[ARG_POINTER_REGNUM])
921 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
923 /* Any constant, or pseudo with constant equivalences, may
924 require reloading from memory using the pic register. */
925 if (pic_offset_table_regnum != INVALID_REGNUM
926 && fixed_regs[pic_offset_table_regnum])
927 bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
930 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
932 if (bb_index == EXIT_BLOCK)
934 /* The exit block is special for this problem and its bits are
935 computed from thin air. */
936 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
937 bitmap_copy (&bb_info->use, df->exit_block_uses);
939 else
940 df_lr_bb_local_compute (bb_index);
943 bitmap_clear (df_lr->out_of_date_transfer_functions);
947 /* Initialize the solution vectors. */
949 static void
950 df_lr_init (bitmap all_blocks)
952 unsigned int bb_index;
953 bitmap_iterator bi;
955 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
957 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
958 bitmap_copy (&bb_info->in, &bb_info->use);
959 bitmap_clear (&bb_info->out);
964 /* Confluence function that processes infinite loops. This might be a
965 noreturn function that throws. And even if it isn't, getting the
966 unwind info right helps debugging. */
967 static void
968 df_lr_confluence_0 (basic_block bb)
970 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
971 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
972 bitmap_copy (op1, &df->hardware_regs_used);
976 /* Confluence function that ignores fake edges. */
978 static bool
979 df_lr_confluence_n (edge e)
981 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
982 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
983 bool changed = false;
985 /* Call-clobbered registers die across exception and call edges. */
986 /* ??? Abnormal call edges ignored for the moment, as this gets
987 confused by sibling call edges, which crashes reg-stack. */
988 if (e->flags & EDGE_EH)
989 changed = bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
990 else
991 changed = bitmap_ior_into (op1, op2);
993 changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
994 return changed;
998 /* Transfer function. */
1000 static bool
1001 df_lr_transfer_function (int bb_index)
1003 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1004 bitmap in = &bb_info->in;
1005 bitmap out = &bb_info->out;
1006 bitmap use = &bb_info->use;
1007 bitmap def = &bb_info->def;
1009 return bitmap_ior_and_compl (in, use, out, def);
1013 /* Run the fast dce as a side effect of building LR. */
1015 static void
1016 df_lr_finalize (bitmap all_blocks)
1018 df_lr->solutions_dirty = false;
1019 if (df->changeable_flags & DF_LR_RUN_DCE)
1021 run_fast_df_dce ();
1023 /* If dce deletes some instructions, we need to recompute the lr
1024 solution before proceeding further. The problem is that fast
1025 dce is a pessimestic dataflow algorithm. In the case where
1026 it deletes a statement S inside of a loop, the uses inside of
1027 S may not be deleted from the dataflow solution because they
1028 were carried around the loop. While it is conservatively
1029 correct to leave these extra bits, the standards of df
1030 require that we maintain the best possible (least fixed
1031 point) solution. The only way to do that is to redo the
1032 iteration from the beginning. See PR35805 for an
1033 example. */
1034 if (df_lr->solutions_dirty)
1036 df_clear_flags (DF_LR_RUN_DCE);
1037 df_lr_alloc (all_blocks);
1038 df_lr_local_compute (all_blocks);
1039 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1040 df_lr_finalize (all_blocks);
1041 df_set_flags (DF_LR_RUN_DCE);
1047 /* Free all storage associated with the problem. */
1049 static void
1050 df_lr_free (void)
1052 struct df_lr_problem_data *problem_data
1053 = (struct df_lr_problem_data *) df_lr->problem_data;
1054 if (df_lr->block_info)
1057 df_lr->block_info_size = 0;
1058 free (df_lr->block_info);
1059 df_lr->block_info = NULL;
1060 bitmap_obstack_release (&problem_data->lr_bitmaps);
1061 free (df_lr->problem_data);
1062 df_lr->problem_data = NULL;
1065 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1066 free (df_lr);
1070 /* Debugging info at top of bb. */
1072 static void
1073 df_lr_top_dump (basic_block bb, FILE *file)
1075 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1076 struct df_lr_problem_data *problem_data;
1077 if (!bb_info)
1078 return;
1080 fprintf (file, ";; lr in \t");
1081 df_print_regset (file, &bb_info->in);
1082 if (df_lr->problem_data)
1084 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1085 if (problem_data->in)
1087 fprintf (file, ";; old in \t");
1088 df_print_regset (file, &problem_data->in[bb->index]);
1091 fprintf (file, ";; lr use \t");
1092 df_print_regset (file, &bb_info->use);
1093 fprintf (file, ";; lr def \t");
1094 df_print_regset (file, &bb_info->def);
1098 /* Debugging info at bottom of bb. */
1100 static void
1101 df_lr_bottom_dump (basic_block bb, FILE *file)
1103 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1104 struct df_lr_problem_data *problem_data;
1105 if (!bb_info)
1106 return;
1108 fprintf (file, ";; lr out \t");
1109 df_print_regset (file, &bb_info->out);
1110 if (df_lr->problem_data)
1112 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1113 if (problem_data->out)
1115 fprintf (file, ";; old out \t");
1116 df_print_regset (file, &problem_data->out[bb->index]);
1122 /* Build the datastructure to verify that the solution to the dataflow
1123 equations is not dirty. */
1125 static void
1126 df_lr_verify_solution_start (void)
1128 basic_block bb;
1129 struct df_lr_problem_data *problem_data;
1130 if (df_lr->solutions_dirty)
1131 return;
1133 /* Set it true so that the solution is recomputed. */
1134 df_lr->solutions_dirty = true;
1136 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1137 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1138 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1140 FOR_ALL_BB_FN (bb, cfun)
1142 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1143 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1144 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1145 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1150 /* Compare the saved datastructure and the new solution to the dataflow
1151 equations. */
1153 static void
1154 df_lr_verify_solution_end (void)
1156 struct df_lr_problem_data *problem_data;
1157 basic_block bb;
1159 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1161 if (!problem_data->out)
1162 return;
1164 if (df_lr->solutions_dirty)
1165 /* Do not check if the solution is still dirty. See the comment
1166 in df_lr_finalize for details. */
1167 df_lr->solutions_dirty = false;
1168 else
1169 FOR_ALL_BB_FN (bb, cfun)
1171 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1172 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1174 /*df_dump (stderr);*/
1175 gcc_unreachable ();
1179 /* Cannot delete them immediately because you may want to dump them
1180 if the comparison fails. */
1181 FOR_ALL_BB_FN (bb, cfun)
1183 bitmap_clear (&problem_data->in[bb->index]);
1184 bitmap_clear (&problem_data->out[bb->index]);
1187 free (problem_data->in);
1188 free (problem_data->out);
1189 problem_data->in = NULL;
1190 problem_data->out = NULL;
1194 /* All of the information associated with every instance of the problem. */
1196 static struct df_problem problem_LR =
1198 DF_LR, /* Problem id. */
1199 DF_BACKWARD, /* Direction. */
1200 df_lr_alloc, /* Allocate the problem specific data. */
1201 df_lr_reset, /* Reset global information. */
1202 df_lr_free_bb_info, /* Free basic block info. */
1203 df_lr_local_compute, /* Local compute function. */
1204 df_lr_init, /* Init the solution specific data. */
1205 df_worklist_dataflow, /* Worklist solver. */
1206 df_lr_confluence_0, /* Confluence operator 0. */
1207 df_lr_confluence_n, /* Confluence operator n. */
1208 df_lr_transfer_function, /* Transfer function. */
1209 df_lr_finalize, /* Finalize function. */
1210 df_lr_free, /* Free all of the problem information. */
1211 NULL, /* Remove this problem from the stack of dataflow problems. */
1212 NULL, /* Debugging. */
1213 df_lr_top_dump, /* Debugging start block. */
1214 df_lr_bottom_dump, /* Debugging end block. */
1215 NULL, /* Debugging start insn. */
1216 NULL, /* Debugging end insn. */
1217 df_lr_verify_solution_start,/* Incremental solution verify start. */
1218 df_lr_verify_solution_end, /* Incremental solution verify end. */
1219 NULL, /* Dependent problem. */
1220 sizeof (struct df_lr_bb_info),/* Size of entry of block_info array. */
1221 TV_DF_LR, /* Timing variable. */
1222 false /* Reset blocks on dropping out of blocks_to_analyze. */
1226 /* Create a new DATAFLOW instance and add it to an existing instance
1227 of DF. The returned structure is what is used to get at the
1228 solution. */
1230 void
1231 df_lr_add_problem (void)
1233 df_add_problem (&problem_LR);
1234 /* These will be initialized when df_scan_blocks processes each
1235 block. */
1236 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1240 /* Verify that all of the lr related info is consistent and
1241 correct. */
1243 void
1244 df_lr_verify_transfer_functions (void)
1246 basic_block bb;
1247 bitmap_head saved_def;
1248 bitmap_head saved_use;
1249 bitmap_head all_blocks;
1251 if (!df)
1252 return;
1254 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1255 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1256 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1258 FOR_ALL_BB_FN (bb, cfun)
1260 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1261 bitmap_set_bit (&all_blocks, bb->index);
1263 if (bb_info)
1265 /* Make a copy of the transfer functions and then compute
1266 new ones to see if the transfer functions have
1267 changed. */
1268 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1269 bb->index))
1271 bitmap_copy (&saved_def, &bb_info->def);
1272 bitmap_copy (&saved_use, &bb_info->use);
1273 bitmap_clear (&bb_info->def);
1274 bitmap_clear (&bb_info->use);
1276 df_lr_bb_local_compute (bb->index);
1277 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1278 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1281 else
1283 /* If we do not have basic block info, the block must be in
1284 the list of dirty blocks or else some one has added a
1285 block behind our backs. */
1286 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1287 bb->index));
1289 /* Make sure no one created a block without following
1290 procedures. */
1291 gcc_assert (df_scan_get_bb_info (bb->index));
1294 /* Make sure there are no dirty bits in blocks that have been deleted. */
1295 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1296 &all_blocks));
1298 bitmap_clear (&saved_def);
1299 bitmap_clear (&saved_use);
1300 bitmap_clear (&all_blocks);
1305 /*----------------------------------------------------------------------------
1306 LIVE AND MAY-INITIALIZED REGISTERS.
1308 This problem first computes the IN and OUT bitvectors for the
1309 may-initialized registers problems, which is a forward problem.
1310 It gives the set of registers for which we MAY have an available
1311 definition, i.e. for which there is an available definition on
1312 at least one path from the entry block to the entry/exit of a
1313 basic block. Sets generate a definition, while clobbers kill
1314 a definition.
1316 In and out bitvectors are built for each basic block and are indexed by
1317 regnum (see df.h for details). In and out bitvectors in struct
1318 df_live_bb_info actually refers to the may-initialized problem;
1320 Then, the in and out sets for the LIVE problem itself are computed.
1321 These are the logical AND of the IN and OUT sets from the LR problem
1322 and the may-initialized problem.
1323 ----------------------------------------------------------------------------*/
1325 /* Private data used to verify the solution for this problem. */
1326 struct df_live_problem_data
1328 bitmap_head *in;
1329 bitmap_head *out;
1330 /* An obstack for the bitmaps we need for this problem. */
1331 bitmap_obstack live_bitmaps;
1334 /* Scratch var used by transfer functions. This is used to implement
1335 an optimization to reduce the amount of space used to compute the
1336 combined lr and live analysis. */
1337 static bitmap_head df_live_scratch;
1340 /* Free basic block info. */
1342 static void
1343 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1344 void *vbb_info)
1346 struct df_live_bb_info *bb_info = (struct df_live_bb_info *) vbb_info;
1347 if (bb_info)
1349 bitmap_clear (&bb_info->gen);
1350 bitmap_clear (&bb_info->kill);
1351 bitmap_clear (&bb_info->in);
1352 bitmap_clear (&bb_info->out);
1357 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1358 not touched unless the block is new. */
1360 static void
1361 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1363 unsigned int bb_index;
1364 bitmap_iterator bi;
1365 struct df_live_problem_data *problem_data;
1367 if (df_live->problem_data)
1368 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1369 else
1371 problem_data = XNEW (struct df_live_problem_data);
1372 df_live->problem_data = problem_data;
1374 problem_data->out = NULL;
1375 problem_data->in = NULL;
1376 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1377 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1380 df_grow_bb_info (df_live);
1382 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1384 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1386 /* When bitmaps are already initialized, just clear them. */
1387 if (bb_info->kill.obstack)
1389 bitmap_clear (&bb_info->kill);
1390 bitmap_clear (&bb_info->gen);
1392 else
1394 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1395 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1396 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1397 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1400 df_live->optional_p = (optimize <= 1);
1404 /* Reset the global solution for recalculation. */
1406 static void
1407 df_live_reset (bitmap all_blocks)
1409 unsigned int bb_index;
1410 bitmap_iterator bi;
1412 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1414 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1415 gcc_assert (bb_info);
1416 bitmap_clear (&bb_info->in);
1417 bitmap_clear (&bb_info->out);
1422 /* Compute local uninitialized register info for basic block BB. */
1424 static void
1425 df_live_bb_local_compute (unsigned int bb_index)
1427 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1428 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1429 rtx_insn *insn;
1430 df_ref def;
1431 int luid = 0;
1433 FOR_BB_INSNS (bb, insn)
1435 unsigned int uid = INSN_UID (insn);
1436 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1438 /* Inserting labels does not always trigger the incremental
1439 rescanning. */
1440 if (!insn_info)
1442 gcc_assert (!INSN_P (insn));
1443 insn_info = df_insn_create_insn_record (insn);
1446 DF_INSN_INFO_LUID (insn_info) = luid;
1447 if (!INSN_P (insn))
1448 continue;
1450 luid++;
1451 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1453 unsigned int regno = DF_REF_REGNO (def);
1455 if (DF_REF_FLAGS_IS_SET (def,
1456 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1457 /* All partial or conditional def
1458 seen are included in the gen set. */
1459 bitmap_set_bit (&bb_info->gen, regno);
1460 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1461 /* Only must clobbers for the entire reg destroy the
1462 value. */
1463 bitmap_set_bit (&bb_info->kill, regno);
1464 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1465 bitmap_set_bit (&bb_info->gen, regno);
1469 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1470 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1474 /* Compute local uninitialized register info. */
1476 static void
1477 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1479 unsigned int bb_index;
1480 bitmap_iterator bi;
1482 df_grow_insn_info ();
1484 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1485 0, bb_index, bi)
1487 df_live_bb_local_compute (bb_index);
1490 bitmap_clear (df_live->out_of_date_transfer_functions);
1494 /* Initialize the solution vectors. */
1496 static void
1497 df_live_init (bitmap all_blocks)
1499 unsigned int bb_index;
1500 bitmap_iterator bi;
1502 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1504 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1505 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1507 /* No register may reach a location where it is not used. Thus
1508 we trim the rr result to the places where it is used. */
1509 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1510 bitmap_clear (&bb_info->in);
1514 /* Forward confluence function that ignores fake edges. */
1516 static bool
1517 df_live_confluence_n (edge e)
1519 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1520 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1522 if (e->flags & EDGE_FAKE)
1523 return false;
1525 return bitmap_ior_into (op1, op2);
1529 /* Transfer function for the forwards may-initialized problem. */
1531 static bool
1532 df_live_transfer_function (int bb_index)
1534 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1535 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1536 bitmap in = &bb_info->in;
1537 bitmap out = &bb_info->out;
1538 bitmap gen = &bb_info->gen;
1539 bitmap kill = &bb_info->kill;
1541 /* We need to use a scratch set here so that the value returned from this
1542 function invocation properly reflects whether the sets changed in a
1543 significant way; i.e. not just because the lr set was anded in. */
1544 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1545 /* No register may reach a location where it is not used. Thus
1546 we trim the rr result to the places where it is used. */
1547 bitmap_and_into (in, &bb_lr_info->in);
1549 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1553 /* And the LR info with the may-initialized registers to produce the LIVE info. */
1555 static void
1556 df_live_finalize (bitmap all_blocks)
1559 if (df_live->solutions_dirty)
1561 bitmap_iterator bi;
1562 unsigned int bb_index;
1564 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1566 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1567 struct df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1569 /* No register may reach a location where it is not used. Thus
1570 we trim the rr result to the places where it is used. */
1571 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1572 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1575 df_live->solutions_dirty = false;
1580 /* Free all storage associated with the problem. */
1582 static void
1583 df_live_free (void)
1585 struct df_live_problem_data *problem_data
1586 = (struct df_live_problem_data *) df_live->problem_data;
1587 if (df_live->block_info)
1589 df_live->block_info_size = 0;
1590 free (df_live->block_info);
1591 df_live->block_info = NULL;
1592 bitmap_clear (&df_live_scratch);
1593 bitmap_obstack_release (&problem_data->live_bitmaps);
1594 free (problem_data);
1595 df_live->problem_data = NULL;
1597 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1598 free (df_live);
1602 /* Debugging info at top of bb. */
1604 static void
1605 df_live_top_dump (basic_block bb, FILE *file)
1607 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1608 struct df_live_problem_data *problem_data;
1610 if (!bb_info)
1611 return;
1613 fprintf (file, ";; live in \t");
1614 df_print_regset (file, &bb_info->in);
1615 if (df_live->problem_data)
1617 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1618 if (problem_data->in)
1620 fprintf (file, ";; old in \t");
1621 df_print_regset (file, &problem_data->in[bb->index]);
1624 fprintf (file, ";; live gen \t");
1625 df_print_regset (file, &bb_info->gen);
1626 fprintf (file, ";; live kill\t");
1627 df_print_regset (file, &bb_info->kill);
1631 /* Debugging info at bottom of bb. */
1633 static void
1634 df_live_bottom_dump (basic_block bb, FILE *file)
1636 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1637 struct df_live_problem_data *problem_data;
1639 if (!bb_info)
1640 return;
1642 fprintf (file, ";; live out \t");
1643 df_print_regset (file, &bb_info->out);
1644 if (df_live->problem_data)
1646 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1647 if (problem_data->out)
1649 fprintf (file, ";; old out \t");
1650 df_print_regset (file, &problem_data->out[bb->index]);
1656 /* Build the datastructure to verify that the solution to the dataflow
1657 equations is not dirty. */
1659 static void
1660 df_live_verify_solution_start (void)
1662 basic_block bb;
1663 struct df_live_problem_data *problem_data;
1664 if (df_live->solutions_dirty)
1665 return;
1667 /* Set it true so that the solution is recomputed. */
1668 df_live->solutions_dirty = true;
1670 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1671 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1672 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1674 FOR_ALL_BB_FN (bb, cfun)
1676 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1677 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1678 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1679 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1684 /* Compare the saved datastructure and the new solution to the dataflow
1685 equations. */
1687 static void
1688 df_live_verify_solution_end (void)
1690 struct df_live_problem_data *problem_data;
1691 basic_block bb;
1693 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1694 if (!problem_data->out)
1695 return;
1697 FOR_ALL_BB_FN (bb, cfun)
1699 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1700 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1702 /*df_dump (stderr);*/
1703 gcc_unreachable ();
1707 /* Cannot delete them immediately because you may want to dump them
1708 if the comparison fails. */
1709 FOR_ALL_BB_FN (bb, cfun)
1711 bitmap_clear (&problem_data->in[bb->index]);
1712 bitmap_clear (&problem_data->out[bb->index]);
1715 free (problem_data->in);
1716 free (problem_data->out);
1717 free (problem_data);
1718 df_live->problem_data = NULL;
1722 /* All of the information associated with every instance of the problem. */
1724 static struct df_problem problem_LIVE =
1726 DF_LIVE, /* Problem id. */
1727 DF_FORWARD, /* Direction. */
1728 df_live_alloc, /* Allocate the problem specific data. */
1729 df_live_reset, /* Reset global information. */
1730 df_live_free_bb_info, /* Free basic block info. */
1731 df_live_local_compute, /* Local compute function. */
1732 df_live_init, /* Init the solution specific data. */
1733 df_worklist_dataflow, /* Worklist solver. */
1734 NULL, /* Confluence operator 0. */
1735 df_live_confluence_n, /* Confluence operator n. */
1736 df_live_transfer_function, /* Transfer function. */
1737 df_live_finalize, /* Finalize function. */
1738 df_live_free, /* Free all of the problem information. */
1739 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1740 NULL, /* Debugging. */
1741 df_live_top_dump, /* Debugging start block. */
1742 df_live_bottom_dump, /* Debugging end block. */
1743 NULL, /* Debugging start insn. */
1744 NULL, /* Debugging end insn. */
1745 df_live_verify_solution_start,/* Incremental solution verify start. */
1746 df_live_verify_solution_end, /* Incremental solution verify end. */
1747 &problem_LR, /* Dependent problem. */
1748 sizeof (struct df_live_bb_info),/* Size of entry of block_info array. */
1749 TV_DF_LIVE, /* Timing variable. */
1750 false /* Reset blocks on dropping out of blocks_to_analyze. */
1754 /* Create a new DATAFLOW instance and add it to an existing instance
1755 of DF. The returned structure is what is used to get at the
1756 solution. */
1758 void
1759 df_live_add_problem (void)
1761 df_add_problem (&problem_LIVE);
1762 /* These will be initialized when df_scan_blocks processes each
1763 block. */
1764 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1768 /* Set all of the blocks as dirty. This needs to be done if this
1769 problem is added after all of the insns have been scanned. */
1771 void
1772 df_live_set_all_dirty (void)
1774 basic_block bb;
1775 FOR_ALL_BB_FN (bb, cfun)
1776 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1777 bb->index);
1781 /* Verify that all of the lr related info is consistent and
1782 correct. */
1784 void
1785 df_live_verify_transfer_functions (void)
1787 basic_block bb;
1788 bitmap_head saved_gen;
1789 bitmap_head saved_kill;
1790 bitmap_head all_blocks;
1792 if (!df)
1793 return;
1795 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1796 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1797 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1799 df_grow_insn_info ();
1801 FOR_ALL_BB_FN (bb, cfun)
1803 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1804 bitmap_set_bit (&all_blocks, bb->index);
1806 if (bb_info)
1808 /* Make a copy of the transfer functions and then compute
1809 new ones to see if the transfer functions have
1810 changed. */
1811 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1812 bb->index))
1814 bitmap_copy (&saved_gen, &bb_info->gen);
1815 bitmap_copy (&saved_kill, &bb_info->kill);
1816 bitmap_clear (&bb_info->gen);
1817 bitmap_clear (&bb_info->kill);
1819 df_live_bb_local_compute (bb->index);
1820 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1821 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1824 else
1826 /* If we do not have basic block info, the block must be in
1827 the list of dirty blocks or else some one has added a
1828 block behind our backs. */
1829 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1830 bb->index));
1832 /* Make sure no one created a block without following
1833 procedures. */
1834 gcc_assert (df_scan_get_bb_info (bb->index));
1837 /* Make sure there are no dirty bits in blocks that have been deleted. */
1838 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1839 &all_blocks));
1840 bitmap_clear (&saved_gen);
1841 bitmap_clear (&saved_kill);
1842 bitmap_clear (&all_blocks);
1845 /*----------------------------------------------------------------------------
1846 MUST-INITIALIZED REGISTERS.
1847 ----------------------------------------------------------------------------*/
1849 /* Private data used to verify the solution for this problem. */
1850 struct df_mir_problem_data
1852 bitmap_head *in;
1853 bitmap_head *out;
1854 /* An obstack for the bitmaps we need for this problem. */
1855 bitmap_obstack mir_bitmaps;
1859 /* Free basic block info. */
1861 static void
1862 df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1863 void *vbb_info)
1865 struct df_mir_bb_info *bb_info = (struct df_mir_bb_info *) vbb_info;
1866 if (bb_info)
1868 bitmap_clear (&bb_info->gen);
1869 bitmap_clear (&bb_info->kill);
1870 bitmap_clear (&bb_info->in);
1871 bitmap_clear (&bb_info->out);
1876 /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1877 not touched unless the block is new. */
1879 static void
1880 df_mir_alloc (bitmap all_blocks)
1882 unsigned int bb_index;
1883 bitmap_iterator bi;
1884 struct df_mir_problem_data *problem_data;
1886 if (df_mir->problem_data)
1887 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1888 else
1890 problem_data = XNEW (struct df_mir_problem_data);
1891 df_mir->problem_data = problem_data;
1893 problem_data->out = NULL;
1894 problem_data->in = NULL;
1895 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1898 df_grow_bb_info (df_mir);
1900 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1902 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1904 /* When bitmaps are already initialized, just clear them. */
1905 if (bb_info->kill.obstack)
1907 bitmap_clear (&bb_info->kill);
1908 bitmap_clear (&bb_info->gen);
1910 else
1912 bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1913 bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1914 bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1915 bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1916 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1917 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1921 df_mir->optional_p = 1;
1925 /* Reset the global solution for recalculation. */
1927 static void
1928 df_mir_reset (bitmap all_blocks)
1930 unsigned int bb_index;
1931 bitmap_iterator bi;
1933 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1935 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1937 gcc_assert (bb_info);
1939 bitmap_clear (&bb_info->in);
1940 bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1941 bitmap_clear (&bb_info->out);
1942 bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1947 /* Compute local uninitialized register info for basic block BB. */
1949 static void
1950 df_mir_bb_local_compute (unsigned int bb_index)
1952 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1953 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1954 rtx_insn *insn;
1955 int luid = 0;
1957 /* Ignoring artificial defs is intentional: these often pretend that some
1958 registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
1959 though they are not used for that. As a result, conservatively assume
1960 they may be uninitialized. */
1962 FOR_BB_INSNS (bb, insn)
1964 unsigned int uid = INSN_UID (insn);
1965 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1967 /* Inserting labels does not always trigger the incremental
1968 rescanning. */
1969 if (!insn_info)
1971 gcc_assert (!INSN_P (insn));
1972 insn_info = df_insn_create_insn_record (insn);
1975 DF_INSN_INFO_LUID (insn_info) = luid;
1976 if (!INSN_P (insn))
1977 continue;
1979 luid++;
1980 df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
1985 /* Compute local uninitialized register info. */
1987 static void
1988 df_mir_local_compute (bitmap all_blocks)
1990 unsigned int bb_index;
1991 bitmap_iterator bi;
1993 df_grow_insn_info ();
1995 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1997 df_mir_bb_local_compute (bb_index);
2002 /* Initialize the solution vectors. */
2004 static void
2005 df_mir_init (bitmap all_blocks)
2007 df_mir_reset (all_blocks);
2011 /* Initialize IN sets for blocks with no predecessors: when landing on such
2012 blocks, assume all registers are uninitialized. */
2014 static void
2015 df_mir_confluence_0 (basic_block bb)
2017 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2019 bitmap_clear (&bb_info->in);
2023 /* Forward confluence function that ignores fake edges. */
2025 static bool
2026 df_mir_confluence_n (edge e)
2028 bitmap op1 = &df_mir_get_bb_info (e->dest->index)->in;
2029 bitmap op2 = &df_mir_get_bb_info (e->src->index)->out;
2031 if (e->flags & EDGE_FAKE)
2032 return false;
2034 /* A register is must-initialized at the entry of a basic block iff it is
2035 must-initialized at the exit of all the predecessors. */
2036 return bitmap_and_into (op1, op2);
2040 /* Transfer function for the forwards must-initialized problem. */
2042 static bool
2043 df_mir_transfer_function (int bb_index)
2045 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2046 bitmap in = &bb_info->in;
2047 bitmap out = &bb_info->out;
2048 bitmap gen = &bb_info->gen;
2049 bitmap kill = &bb_info->kill;
2051 return bitmap_ior_and_compl (out, gen, in, kill);
2055 /* Free all storage associated with the problem. */
2057 static void
2058 df_mir_free (void)
2060 struct df_mir_problem_data *problem_data
2061 = (struct df_mir_problem_data *) df_mir->problem_data;
2062 if (df_mir->block_info)
2064 df_mir->block_info_size = 0;
2065 free (df_mir->block_info);
2066 df_mir->block_info = NULL;
2067 bitmap_obstack_release (&problem_data->mir_bitmaps);
2068 free (problem_data);
2069 df_mir->problem_data = NULL;
2071 free (df_mir);
2075 /* Debugging info at top of bb. */
2077 static void
2078 df_mir_top_dump (basic_block bb, FILE *file)
2080 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2082 if (!bb_info)
2083 return;
2085 fprintf (file, ";; mir in \t");
2086 df_print_regset (file, &bb_info->in);
2087 fprintf (file, ";; mir kill\t");
2088 df_print_regset (file, &bb_info->kill);
2089 fprintf (file, ";; mir gen \t");
2090 df_print_regset (file, &bb_info->gen);
2093 /* Debugging info at bottom of bb. */
2095 static void
2096 df_mir_bottom_dump (basic_block bb, FILE *file)
2098 struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2100 if (!bb_info)
2101 return;
2103 fprintf (file, ";; mir out \t");
2104 df_print_regset (file, &bb_info->out);
2108 /* Build the datastructure to verify that the solution to the dataflow
2109 equations is not dirty. */
2111 static void
2112 df_mir_verify_solution_start (void)
2114 basic_block bb;
2115 struct df_mir_problem_data *problem_data;
2116 if (df_mir->solutions_dirty)
2117 return;
2119 /* Set it true so that the solution is recomputed. */
2120 df_mir->solutions_dirty = true;
2122 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2123 problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2124 problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2125 bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2127 FOR_ALL_BB_FN (bb, cfun)
2129 bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2130 bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2131 bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2132 bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2137 /* Compare the saved datastructure and the new solution to the dataflow
2138 equations. */
2140 static void
2141 df_mir_verify_solution_end (void)
2143 struct df_mir_problem_data *problem_data;
2144 basic_block bb;
2146 problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2147 if (!problem_data->out)
2148 return;
2150 FOR_ALL_BB_FN (bb, cfun)
2152 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2153 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2154 gcc_unreachable ();
2157 /* Cannot delete them immediately because you may want to dump them
2158 if the comparison fails. */
2159 FOR_ALL_BB_FN (bb, cfun)
2161 bitmap_clear (&problem_data->in[bb->index]);
2162 bitmap_clear (&problem_data->out[bb->index]);
2165 free (problem_data->in);
2166 free (problem_data->out);
2167 bitmap_obstack_release (&problem_data->mir_bitmaps);
2168 free (problem_data);
2169 df_mir->problem_data = NULL;
2173 /* All of the information associated with every instance of the problem. */
2175 static struct df_problem problem_MIR =
2177 DF_MIR, /* Problem id. */
2178 DF_FORWARD, /* Direction. */
2179 df_mir_alloc, /* Allocate the problem specific data. */
2180 df_mir_reset, /* Reset global information. */
2181 df_mir_free_bb_info, /* Free basic block info. */
2182 df_mir_local_compute, /* Local compute function. */
2183 df_mir_init, /* Init the solution specific data. */
2184 df_worklist_dataflow, /* Worklist solver. */
2185 df_mir_confluence_0, /* Confluence operator 0. */
2186 df_mir_confluence_n, /* Confluence operator n. */
2187 df_mir_transfer_function, /* Transfer function. */
2188 NULL, /* Finalize function. */
2189 df_mir_free, /* Free all of the problem information. */
2190 df_mir_free, /* Remove this problem from the stack of dataflow problems. */
2191 NULL, /* Debugging. */
2192 df_mir_top_dump, /* Debugging start block. */
2193 df_mir_bottom_dump, /* Debugging end block. */
2194 NULL, /* Debugging start insn. */
2195 NULL, /* Debugging end insn. */
2196 df_mir_verify_solution_start, /* Incremental solution verify start. */
2197 df_mir_verify_solution_end, /* Incremental solution verify end. */
2198 NULL, /* Dependent problem. */
2199 sizeof (struct df_mir_bb_info),/* Size of entry of block_info array. */
2200 TV_DF_MIR, /* Timing variable. */
2201 false /* Reset blocks on dropping out of blocks_to_analyze. */
2205 /* Create a new DATAFLOW instance and add it to an existing instance
2206 of DF. */
2208 void
2209 df_mir_add_problem (void)
2211 df_add_problem (&problem_MIR);
2212 /* These will be initialized when df_scan_blocks processes each
2213 block. */
2214 df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2218 /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps. */
2220 void
2221 df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2222 bitmap kill, bitmap gen)
2224 df_ref def;
2226 FOR_EACH_INSN_DEF (def, insn)
2228 unsigned int regno = DF_REF_REGNO (def);
2230 /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2231 previous gen is irrelevant (and reciprocally). Also, claim that a
2232 register is GEN only if it is in all cases. */
2233 if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2235 bitmap_set_bit (kill, regno);
2236 bitmap_clear_bit (gen, regno);
2238 /* In the worst case, partial and conditional defs can leave bits
2239 uninitialized, so assume they do not change anything. */
2240 else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2242 bitmap_set_bit (gen, regno);
2243 bitmap_clear_bit (kill, regno);
2248 /*----------------------------------------------------------------------------
2249 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2251 Link either the defs to the uses and / or the uses to the defs.
2253 These problems are set up like the other dataflow problems so that
2254 they nicely fit into the framework. They are much simpler and only
2255 involve a single traversal of instructions and an examination of
2256 the reaching defs information (the dependent problem).
2257 ----------------------------------------------------------------------------*/
2259 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2261 /* Create a du or ud chain from SRC to DST and link it into SRC. */
2263 struct df_link *
2264 df_chain_create (df_ref src, df_ref dst)
2266 struct df_link *head = DF_REF_CHAIN (src);
2267 struct df_link *link = df_chain->block_pool->allocate ();
2269 DF_REF_CHAIN (src) = link;
2270 link->next = head;
2271 link->ref = dst;
2272 return link;
2276 /* Delete any du or ud chains that start at REF and point to
2277 TARGET. */
2278 static void
2279 df_chain_unlink_1 (df_ref ref, df_ref target)
2281 struct df_link *chain = DF_REF_CHAIN (ref);
2282 struct df_link *prev = NULL;
2284 while (chain)
2286 if (chain->ref == target)
2288 if (prev)
2289 prev->next = chain->next;
2290 else
2291 DF_REF_CHAIN (ref) = chain->next;
2292 df_chain->block_pool->remove (chain);
2293 return;
2295 prev = chain;
2296 chain = chain->next;
2301 /* Delete a du or ud chain that leave or point to REF. */
2303 void
2304 df_chain_unlink (df_ref ref)
2306 struct df_link *chain = DF_REF_CHAIN (ref);
2307 while (chain)
2309 struct df_link *next = chain->next;
2310 /* Delete the other side if it exists. */
2311 df_chain_unlink_1 (chain->ref, ref);
2312 df_chain->block_pool->remove (chain);
2313 chain = next;
2315 DF_REF_CHAIN (ref) = NULL;
2319 /* Copy the du or ud chain starting at FROM_REF and attach it to
2320 TO_REF. */
2322 void
2323 df_chain_copy (df_ref to_ref,
2324 struct df_link *from_ref)
2326 while (from_ref)
2328 df_chain_create (to_ref, from_ref->ref);
2329 from_ref = from_ref->next;
2334 /* Remove this problem from the stack of dataflow problems. */
2336 static void
2337 df_chain_remove_problem (void)
2339 bitmap_iterator bi;
2340 unsigned int bb_index;
2342 /* Wholesale destruction of the old chains. */
2343 if (df_chain->block_pool)
2344 delete df_chain->block_pool;
2346 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2348 rtx_insn *insn;
2349 df_ref def, use;
2350 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2352 if (df_chain_problem_p (DF_DU_CHAIN))
2353 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2354 DF_REF_CHAIN (def) = NULL;
2355 if (df_chain_problem_p (DF_UD_CHAIN))
2356 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2357 DF_REF_CHAIN (use) = NULL;
2359 FOR_BB_INSNS (bb, insn)
2360 if (INSN_P (insn))
2362 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2363 if (df_chain_problem_p (DF_DU_CHAIN))
2364 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2365 DF_REF_CHAIN (def) = NULL;
2366 if (df_chain_problem_p (DF_UD_CHAIN))
2368 FOR_EACH_INSN_INFO_USE (use, insn_info)
2369 DF_REF_CHAIN (use) = NULL;
2370 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2371 DF_REF_CHAIN (use) = NULL;
2376 bitmap_clear (df_chain->out_of_date_transfer_functions);
2377 df_chain->block_pool = NULL;
2381 /* Remove the chain problem completely. */
2383 static void
2384 df_chain_fully_remove_problem (void)
2386 df_chain_remove_problem ();
2387 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2388 free (df_chain);
2392 /* Create def-use or use-def chains. */
2394 static void
2395 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2397 df_chain_remove_problem ();
2398 df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2399 df_chain->optional_p = true;
2403 /* Reset all of the chains when the set of basic blocks changes. */
2405 static void
2406 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2408 df_chain_remove_problem ();
2412 /* Create the chains for a list of USEs. */
2414 static void
2415 df_chain_create_bb_process_use (bitmap local_rd,
2416 df_ref use,
2417 int top_flag)
2419 bitmap_iterator bi;
2420 unsigned int def_index;
2422 for (; use; use = DF_REF_NEXT_LOC (use))
2424 unsigned int uregno = DF_REF_REGNO (use);
2425 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2426 || (uregno >= FIRST_PSEUDO_REGISTER))
2428 /* Do not want to go through this for an uninitialized var. */
2429 int count = DF_DEFS_COUNT (uregno);
2430 if (count)
2432 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2434 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2435 unsigned int last_index = first_index + count - 1;
2437 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2439 df_ref def;
2440 if (def_index > last_index)
2441 break;
2443 def = DF_DEFS_GET (def_index);
2444 if (df_chain_problem_p (DF_DU_CHAIN))
2445 df_chain_create (def, use);
2446 if (df_chain_problem_p (DF_UD_CHAIN))
2447 df_chain_create (use, def);
2456 /* Create chains from reaching defs bitmaps for basic block BB. */
2458 static void
2459 df_chain_create_bb (unsigned int bb_index)
2461 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2462 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2463 rtx_insn *insn;
2464 bitmap_head cpy;
2466 bitmap_initialize (&cpy, &bitmap_default_obstack);
2467 bitmap_copy (&cpy, &bb_info->in);
2468 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2470 /* Since we are going forwards, process the artificial uses first
2471 then the artificial defs second. */
2473 #ifdef EH_USES
2474 /* Create the chains for the artificial uses from the EH_USES at the
2475 beginning of the block. */
2477 /* Artificials are only hard regs. */
2478 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2479 df_chain_create_bb_process_use (&cpy,
2480 df_get_artificial_uses (bb->index),
2481 DF_REF_AT_TOP);
2482 #endif
2484 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2486 /* Process the regular instructions next. */
2487 FOR_BB_INSNS (bb, insn)
2488 if (INSN_P (insn))
2490 unsigned int uid = INSN_UID (insn);
2492 /* First scan the uses and link them up with the defs that remain
2493 in the cpy vector. */
2494 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2495 if (df->changeable_flags & DF_EQ_NOTES)
2496 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2498 /* Since we are going forwards, process the defs second. */
2499 df_rd_simulate_one_insn (bb, insn, &cpy);
2502 /* Create the chains for the artificial uses of the hard registers
2503 at the end of the block. */
2504 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2505 df_chain_create_bb_process_use (&cpy,
2506 df_get_artificial_uses (bb->index),
2509 bitmap_clear (&cpy);
2512 /* Create def-use chains from reaching use bitmaps for basic blocks
2513 in BLOCKS. */
2515 static void
2516 df_chain_finalize (bitmap all_blocks)
2518 unsigned int bb_index;
2519 bitmap_iterator bi;
2521 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2523 df_chain_create_bb (bb_index);
2528 /* Free all storage associated with the problem. */
2530 static void
2531 df_chain_free (void)
2533 delete df_chain->block_pool;
2534 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2535 free (df_chain);
2539 /* Debugging info. */
2541 static void
2542 df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2544 /* Artificials are only hard regs. */
2545 if (df->changeable_flags & DF_NO_HARD_REGS)
2546 return;
2547 if (df_chain_problem_p (DF_UD_CHAIN))
2549 df_ref use;
2551 fprintf (file,
2552 ";; UD chains for artificial uses at %s\n",
2553 top ? "top" : "bottom");
2554 FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2555 if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2556 || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2558 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2559 df_chain_dump (DF_REF_CHAIN (use), file);
2560 fprintf (file, "\n");
2563 if (df_chain_problem_p (DF_DU_CHAIN))
2565 df_ref def;
2567 fprintf (file,
2568 ";; DU chains for artificial defs at %s\n",
2569 top ? "top" : "bottom");
2570 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2571 if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2572 || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2574 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2575 df_chain_dump (DF_REF_CHAIN (def), file);
2576 fprintf (file, "\n");
2581 static void
2582 df_chain_top_dump (basic_block bb, FILE *file)
2584 df_chain_bb_dump (bb, file, /*top=*/true);
2587 static void
2588 df_chain_bottom_dump (basic_block bb, FILE *file)
2590 df_chain_bb_dump (bb, file, /*top=*/false);
2593 static void
2594 df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2596 if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2598 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2599 df_ref use;
2601 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2602 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2603 FOR_EACH_INSN_INFO_USE (use, insn_info)
2604 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2605 || !(df->changeable_flags & DF_NO_HARD_REGS))
2607 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2608 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2609 fprintf (file, "read/write ");
2610 df_chain_dump (DF_REF_CHAIN (use), file);
2611 fprintf (file, "\n");
2613 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2614 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2615 || !(df->changeable_flags & DF_NO_HARD_REGS))
2617 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2618 df_chain_dump (DF_REF_CHAIN (use), file);
2619 fprintf (file, "\n");
2624 static void
2625 df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2627 if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2629 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2630 df_ref def;
2631 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2632 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2633 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2634 if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2635 || !(df->changeable_flags & DF_NO_HARD_REGS))
2637 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2638 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2639 fprintf (file, "read/write ");
2640 df_chain_dump (DF_REF_CHAIN (def), file);
2641 fprintf (file, "\n");
2643 fprintf (file, "\n");
2647 static struct df_problem problem_CHAIN =
2649 DF_CHAIN, /* Problem id. */
2650 DF_NONE, /* Direction. */
2651 df_chain_alloc, /* Allocate the problem specific data. */
2652 df_chain_reset, /* Reset global information. */
2653 NULL, /* Free basic block info. */
2654 NULL, /* Local compute function. */
2655 NULL, /* Init the solution specific data. */
2656 NULL, /* Iterative solver. */
2657 NULL, /* Confluence operator 0. */
2658 NULL, /* Confluence operator n. */
2659 NULL, /* Transfer function. */
2660 df_chain_finalize, /* Finalize function. */
2661 df_chain_free, /* Free all of the problem information. */
2662 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2663 NULL, /* Debugging. */
2664 df_chain_top_dump, /* Debugging start block. */
2665 df_chain_bottom_dump, /* Debugging end block. */
2666 df_chain_insn_top_dump, /* Debugging start insn. */
2667 df_chain_insn_bottom_dump, /* Debugging end insn. */
2668 NULL, /* Incremental solution verify start. */
2669 NULL, /* Incremental solution verify end. */
2670 &problem_RD, /* Dependent problem. */
2671 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
2672 TV_DF_CHAIN, /* Timing variable. */
2673 false /* Reset blocks on dropping out of blocks_to_analyze. */
2677 /* Create a new DATAFLOW instance and add it to an existing instance
2678 of DF. The returned structure is what is used to get at the
2679 solution. */
2681 void
2682 df_chain_add_problem (unsigned int chain_flags)
2684 df_add_problem (&problem_CHAIN);
2685 df_chain->local_flags = chain_flags;
2686 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2689 #undef df_chain_problem_p
2692 /*----------------------------------------------------------------------------
2693 WORD LEVEL LIVE REGISTERS
2695 Find the locations in the function where any use of a pseudo can
2696 reach in the backwards direction. In and out bitvectors are built
2697 for each basic block. We only track pseudo registers that have a
2698 size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2699 contain two bits corresponding to each of the subwords.
2701 ----------------------------------------------------------------------------*/
2703 /* Private data used to verify the solution for this problem. */
2704 struct df_word_lr_problem_data
2706 /* An obstack for the bitmaps we need for this problem. */
2707 bitmap_obstack word_lr_bitmaps;
2711 /* Free basic block info. */
2713 static void
2714 df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2715 void *vbb_info)
2717 struct df_word_lr_bb_info *bb_info = (struct df_word_lr_bb_info *) vbb_info;
2718 if (bb_info)
2720 bitmap_clear (&bb_info->use);
2721 bitmap_clear (&bb_info->def);
2722 bitmap_clear (&bb_info->in);
2723 bitmap_clear (&bb_info->out);
2728 /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2729 not touched unless the block is new. */
2731 static void
2732 df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2734 unsigned int bb_index;
2735 bitmap_iterator bi;
2736 basic_block bb;
2737 struct df_word_lr_problem_data *problem_data
2738 = XNEW (struct df_word_lr_problem_data);
2740 df_word_lr->problem_data = problem_data;
2742 df_grow_bb_info (df_word_lr);
2744 /* Create the mapping from regnos to slots. This does not change
2745 unless the problem is destroyed and recreated. In particular, if
2746 we end up deleting the only insn that used a subreg, we do not
2747 want to redo the mapping because this would invalidate everything
2748 else. */
2750 bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2752 FOR_EACH_BB_FN (bb, cfun)
2753 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2755 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2756 bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2758 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2760 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2762 /* When bitmaps are already initialized, just clear them. */
2763 if (bb_info->use.obstack)
2765 bitmap_clear (&bb_info->def);
2766 bitmap_clear (&bb_info->use);
2768 else
2770 bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2771 bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2772 bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2773 bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2777 df_word_lr->optional_p = true;
2781 /* Reset the global solution for recalculation. */
2783 static void
2784 df_word_lr_reset (bitmap all_blocks)
2786 unsigned int bb_index;
2787 bitmap_iterator bi;
2789 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2791 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2792 gcc_assert (bb_info);
2793 bitmap_clear (&bb_info->in);
2794 bitmap_clear (&bb_info->out);
2798 /* Examine REF, and if it is for a reg we're interested in, set or
2799 clear the bits corresponding to its subwords from the bitmap
2800 according to IS_SET. LIVE is the bitmap we should update. We do
2801 not track hard regs or pseudos of any size other than 2 *
2802 UNITS_PER_WORD.
2803 We return true if we changed the bitmap, or if we encountered a register
2804 we're not tracking. */
2806 bool
2807 df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2809 rtx orig_reg = DF_REF_REG (ref);
2810 rtx reg = orig_reg;
2811 machine_mode reg_mode;
2812 unsigned regno;
2813 /* Left at -1 for whole accesses. */
2814 int which_subword = -1;
2815 bool changed = false;
2817 if (GET_CODE (reg) == SUBREG)
2818 reg = SUBREG_REG (orig_reg);
2819 regno = REGNO (reg);
2820 reg_mode = GET_MODE (reg);
2821 if (regno < FIRST_PSEUDO_REGISTER
2822 || GET_MODE_SIZE (reg_mode) != 2 * UNITS_PER_WORD)
2823 return true;
2825 if (GET_CODE (orig_reg) == SUBREG
2826 && df_read_modify_subreg_p (orig_reg))
2828 gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2829 if (subreg_lowpart_p (orig_reg))
2830 which_subword = 0;
2831 else
2832 which_subword = 1;
2834 if (is_set)
2836 if (which_subword != 1)
2837 changed |= bitmap_set_bit (live, regno * 2);
2838 if (which_subword != 0)
2839 changed |= bitmap_set_bit (live, regno * 2 + 1);
2841 else
2843 if (which_subword != 1)
2844 changed |= bitmap_clear_bit (live, regno * 2);
2845 if (which_subword != 0)
2846 changed |= bitmap_clear_bit (live, regno * 2 + 1);
2848 return changed;
2851 /* Compute local live register info for basic block BB. */
2853 static void
2854 df_word_lr_bb_local_compute (unsigned int bb_index)
2856 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2857 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2858 rtx_insn *insn;
2859 df_ref def, use;
2861 /* Ensure that artificial refs don't contain references to pseudos. */
2862 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2863 gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2865 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2866 gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2868 FOR_BB_INSNS_REVERSE (bb, insn)
2870 if (!NONDEBUG_INSN_P (insn))
2871 continue;
2873 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2874 FOR_EACH_INSN_INFO_DEF (def, insn_info)
2875 /* If the def is to only part of the reg, it does
2876 not kill the other defs that reach here. */
2877 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2879 df_word_lr_mark_ref (def, true, &bb_info->def);
2880 df_word_lr_mark_ref (def, false, &bb_info->use);
2882 FOR_EACH_INSN_INFO_USE (use, insn_info)
2883 df_word_lr_mark_ref (use, true, &bb_info->use);
2888 /* Compute local live register info for each basic block within BLOCKS. */
2890 static void
2891 df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2893 unsigned int bb_index;
2894 bitmap_iterator bi;
2896 EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2898 if (bb_index == EXIT_BLOCK)
2900 unsigned regno;
2901 bitmap_iterator bi;
2902 EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2903 regno, bi)
2904 gcc_unreachable ();
2906 else
2907 df_word_lr_bb_local_compute (bb_index);
2910 bitmap_clear (df_word_lr->out_of_date_transfer_functions);
2914 /* Initialize the solution vectors. */
2916 static void
2917 df_word_lr_init (bitmap all_blocks)
2919 unsigned int bb_index;
2920 bitmap_iterator bi;
2922 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2924 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2925 bitmap_copy (&bb_info->in, &bb_info->use);
2926 bitmap_clear (&bb_info->out);
2931 /* Confluence function that ignores fake edges. */
2933 static bool
2934 df_word_lr_confluence_n (edge e)
2936 bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
2937 bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
2939 return bitmap_ior_into (op1, op2);
2943 /* Transfer function. */
2945 static bool
2946 df_word_lr_transfer_function (int bb_index)
2948 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2949 bitmap in = &bb_info->in;
2950 bitmap out = &bb_info->out;
2951 bitmap use = &bb_info->use;
2952 bitmap def = &bb_info->def;
2954 return bitmap_ior_and_compl (in, use, out, def);
2958 /* Free all storage associated with the problem. */
2960 static void
2961 df_word_lr_free (void)
2963 struct df_word_lr_problem_data *problem_data
2964 = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
2966 if (df_word_lr->block_info)
2968 df_word_lr->block_info_size = 0;
2969 free (df_word_lr->block_info);
2970 df_word_lr->block_info = NULL;
2973 BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
2974 bitmap_obstack_release (&problem_data->word_lr_bitmaps);
2975 free (problem_data);
2976 free (df_word_lr);
2980 /* Debugging info at top of bb. */
2982 static void
2983 df_word_lr_top_dump (basic_block bb, FILE *file)
2985 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
2986 if (!bb_info)
2987 return;
2989 fprintf (file, ";; blr in \t");
2990 df_print_word_regset (file, &bb_info->in);
2991 fprintf (file, ";; blr use \t");
2992 df_print_word_regset (file, &bb_info->use);
2993 fprintf (file, ";; blr def \t");
2994 df_print_word_regset (file, &bb_info->def);
2998 /* Debugging info at bottom of bb. */
3000 static void
3001 df_word_lr_bottom_dump (basic_block bb, FILE *file)
3003 struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3004 if (!bb_info)
3005 return;
3007 fprintf (file, ";; blr out \t");
3008 df_print_word_regset (file, &bb_info->out);
3012 /* All of the information associated with every instance of the problem. */
3014 static struct df_problem problem_WORD_LR =
3016 DF_WORD_LR, /* Problem id. */
3017 DF_BACKWARD, /* Direction. */
3018 df_word_lr_alloc, /* Allocate the problem specific data. */
3019 df_word_lr_reset, /* Reset global information. */
3020 df_word_lr_free_bb_info, /* Free basic block info. */
3021 df_word_lr_local_compute, /* Local compute function. */
3022 df_word_lr_init, /* Init the solution specific data. */
3023 df_worklist_dataflow, /* Worklist solver. */
3024 NULL, /* Confluence operator 0. */
3025 df_word_lr_confluence_n, /* Confluence operator n. */
3026 df_word_lr_transfer_function, /* Transfer function. */
3027 NULL, /* Finalize function. */
3028 df_word_lr_free, /* Free all of the problem information. */
3029 df_word_lr_free, /* Remove this problem from the stack of dataflow problems. */
3030 NULL, /* Debugging. */
3031 df_word_lr_top_dump, /* Debugging start block. */
3032 df_word_lr_bottom_dump, /* Debugging end block. */
3033 NULL, /* Debugging start insn. */
3034 NULL, /* Debugging end insn. */
3035 NULL, /* Incremental solution verify start. */
3036 NULL, /* Incremental solution verify end. */
3037 NULL, /* Dependent problem. */
3038 sizeof (struct df_word_lr_bb_info),/* Size of entry of block_info array. */
3039 TV_DF_WORD_LR, /* Timing variable. */
3040 false /* Reset blocks on dropping out of blocks_to_analyze. */
3044 /* Create a new DATAFLOW instance and add it to an existing instance
3045 of DF. The returned structure is what is used to get at the
3046 solution. */
3048 void
3049 df_word_lr_add_problem (void)
3051 df_add_problem (&problem_WORD_LR);
3052 /* These will be initialized when df_scan_blocks processes each
3053 block. */
3054 df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3058 /* Simulate the effects of the defs of INSN on LIVE. Return true if we changed
3059 any bits, which is used by the caller to determine whether a set is
3060 necessary. We also return true if there are other reasons not to delete
3061 an insn. */
3063 bool
3064 df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3066 bool changed = false;
3067 df_ref def;
3069 FOR_EACH_INSN_DEF (def, insn)
3070 if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3071 changed = true;
3072 else
3073 changed |= df_word_lr_mark_ref (def, false, live);
3074 return changed;
3078 /* Simulate the effects of the uses of INSN on LIVE. */
3080 void
3081 df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3083 df_ref use;
3085 FOR_EACH_INSN_USE (use, insn)
3086 df_word_lr_mark_ref (use, true, live);
3089 /*----------------------------------------------------------------------------
3090 This problem computes REG_DEAD and REG_UNUSED notes.
3091 ----------------------------------------------------------------------------*/
3093 static void
3094 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3096 df_note->optional_p = true;
3099 /* This is only used if REG_DEAD_DEBUGGING is in effect. */
3100 static void
3101 df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3103 if (dump_file)
3105 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3106 print_rtl (dump_file, note);
3107 fprintf (dump_file, "\n");
3112 /* After reg-stack, the x86 floating point stack regs are difficult to
3113 analyze because of all of the pushes, pops and rotations. Thus, we
3114 just leave the notes alone. */
3116 #ifdef STACK_REGS
3117 static inline bool
3118 df_ignore_stack_reg (int regno)
3120 return regstack_completed
3121 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3123 #else
3124 static inline bool
3125 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3127 return false;
3129 #endif
3132 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN. */
3134 static void
3135 df_remove_dead_and_unused_notes (rtx_insn *insn)
3137 rtx *pprev = &REG_NOTES (insn);
3138 rtx link = *pprev;
3140 while (link)
3142 switch (REG_NOTE_KIND (link))
3144 case REG_DEAD:
3145 /* After reg-stack, we need to ignore any unused notes
3146 for the stack registers. */
3147 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3149 pprev = &XEXP (link, 1);
3150 link = *pprev;
3152 else
3154 rtx next = XEXP (link, 1);
3155 if (REG_DEAD_DEBUGGING)
3156 df_print_note ("deleting: ", insn, link);
3157 free_EXPR_LIST_node (link);
3158 *pprev = link = next;
3160 break;
3162 case REG_UNUSED:
3163 /* After reg-stack, we need to ignore any unused notes
3164 for the stack registers. */
3165 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3167 pprev = &XEXP (link, 1);
3168 link = *pprev;
3170 else
3172 rtx next = XEXP (link, 1);
3173 if (REG_DEAD_DEBUGGING)
3174 df_print_note ("deleting: ", insn, link);
3175 free_EXPR_LIST_node (link);
3176 *pprev = link = next;
3178 break;
3180 default:
3181 pprev = &XEXP (link, 1);
3182 link = *pprev;
3183 break;
3188 /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3189 as the bitmap of currently live registers. */
3191 static void
3192 df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3194 rtx *pprev = &REG_NOTES (insn);
3195 rtx link = *pprev;
3197 while (link)
3199 switch (REG_NOTE_KIND (link))
3201 case REG_EQUAL:
3202 case REG_EQUIV:
3204 /* Remove the notes that refer to dead registers. As we have at most
3205 one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3206 so we need to purge the complete EQ_USES vector when removing
3207 the note using df_notes_rescan. */
3208 df_ref use;
3209 bool deleted = false;
3211 FOR_EACH_INSN_EQ_USE (use, insn)
3212 if (DF_REF_REGNO (use) > FIRST_PSEUDO_REGISTER
3213 && DF_REF_LOC (use)
3214 && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3215 && !bitmap_bit_p (live, DF_REF_REGNO (use))
3216 && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3218 deleted = true;
3219 break;
3221 if (deleted)
3223 rtx next;
3224 if (REG_DEAD_DEBUGGING)
3225 df_print_note ("deleting: ", insn, link);
3226 next = XEXP (link, 1);
3227 free_EXPR_LIST_node (link);
3228 *pprev = link = next;
3229 df_notes_rescan (insn);
3231 else
3233 pprev = &XEXP (link, 1);
3234 link = *pprev;
3236 break;
3239 default:
3240 pprev = &XEXP (link, 1);
3241 link = *pprev;
3242 break;
3247 /* Set a NOTE_TYPE note for REG in INSN. */
3249 static inline void
3250 df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3252 gcc_checking_assert (!DEBUG_INSN_P (insn));
3253 add_reg_note (insn, note_type, reg);
3256 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3257 arguments. Return true if the register value described by MWS's
3258 mw_reg is known to be completely unused, and if mw_reg can therefore
3259 be used in a REG_UNUSED note. */
3261 static bool
3262 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3263 bitmap live, bitmap artificial_uses)
3265 unsigned int r;
3267 /* If MWS describes a partial reference, create REG_UNUSED notes for
3268 individual hard registers. */
3269 if (mws->flags & DF_REF_PARTIAL)
3270 return false;
3272 /* Likewise if some part of the register is used. */
3273 for (r = mws->start_regno; r <= mws->end_regno; r++)
3274 if (bitmap_bit_p (live, r)
3275 || bitmap_bit_p (artificial_uses, r))
3276 return false;
3278 gcc_assert (REG_P (mws->mw_reg));
3279 return true;
3283 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3284 based on the bits in LIVE. Do not generate notes for registers in
3285 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3286 not generated if the reg is both read and written by the
3287 instruction.
3290 static void
3291 df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3292 bitmap live, bitmap do_not_gen,
3293 bitmap artificial_uses,
3294 struct dead_debug_local *debug)
3296 unsigned int r;
3298 if (REG_DEAD_DEBUGGING && dump_file)
3299 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3300 mws->start_regno, mws->end_regno);
3302 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3304 unsigned int regno = mws->start_regno;
3305 df_set_note (REG_UNUSED, insn, mws->mw_reg);
3306 dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3308 if (REG_DEAD_DEBUGGING)
3309 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3311 bitmap_set_bit (do_not_gen, regno);
3312 /* Only do this if the value is totally dead. */
3314 else
3315 for (r = mws->start_regno; r <= mws->end_regno; r++)
3317 if (!bitmap_bit_p (live, r)
3318 && !bitmap_bit_p (artificial_uses, r))
3320 df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3321 dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3322 if (REG_DEAD_DEBUGGING)
3323 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3325 bitmap_set_bit (do_not_gen, r);
3330 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3331 arguments. Return true if the register value described by MWS's
3332 mw_reg is known to be completely dead, and if mw_reg can therefore
3333 be used in a REG_DEAD note. */
3335 static bool
3336 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3337 bitmap live, bitmap artificial_uses,
3338 bitmap do_not_gen)
3340 unsigned int r;
3342 /* If MWS describes a partial reference, create REG_DEAD notes for
3343 individual hard registers. */
3344 if (mws->flags & DF_REF_PARTIAL)
3345 return false;
3347 /* Likewise if some part of the register is not dead. */
3348 for (r = mws->start_regno; r <= mws->end_regno; r++)
3349 if (bitmap_bit_p (live, r)
3350 || bitmap_bit_p (artificial_uses, r)
3351 || bitmap_bit_p (do_not_gen, r))
3352 return false;
3354 gcc_assert (REG_P (mws->mw_reg));
3355 return true;
3358 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3359 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3360 from being set if the instruction both reads and writes the
3361 register. */
3363 static void
3364 df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3365 bitmap live, bitmap do_not_gen,
3366 bitmap artificial_uses, bool *added_notes_p)
3368 unsigned int r;
3369 bool is_debug = *added_notes_p;
3371 *added_notes_p = false;
3373 if (REG_DEAD_DEBUGGING && dump_file)
3375 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3376 mws->start_regno, mws->end_regno);
3377 df_print_regset (dump_file, do_not_gen);
3378 fprintf (dump_file, " live =");
3379 df_print_regset (dump_file, live);
3380 fprintf (dump_file, " artificial uses =");
3381 df_print_regset (dump_file, artificial_uses);
3384 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3386 if (is_debug)
3388 *added_notes_p = true;
3389 return;
3391 /* Add a dead note for the entire multi word register. */
3392 df_set_note (REG_DEAD, insn, mws->mw_reg);
3393 if (REG_DEAD_DEBUGGING)
3394 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3396 else
3398 for (r = mws->start_regno; r <= mws->end_regno; r++)
3399 if (!bitmap_bit_p (live, r)
3400 && !bitmap_bit_p (artificial_uses, r)
3401 && !bitmap_bit_p (do_not_gen, r))
3403 if (is_debug)
3405 *added_notes_p = true;
3406 return;
3408 df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3409 if (REG_DEAD_DEBUGGING)
3410 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3413 return;
3417 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3418 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3420 static void
3421 df_create_unused_note (rtx_insn *insn, df_ref def,
3422 bitmap live, bitmap artificial_uses,
3423 struct dead_debug_local *debug)
3425 unsigned int dregno = DF_REF_REGNO (def);
3427 if (REG_DEAD_DEBUGGING && dump_file)
3429 fprintf (dump_file, " regular looking at def ");
3430 df_ref_debug (def, dump_file);
3433 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3434 || bitmap_bit_p (live, dregno)
3435 || bitmap_bit_p (artificial_uses, dregno)
3436 || df_ignore_stack_reg (dregno)))
3438 rtx reg = (DF_REF_LOC (def))
3439 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3440 df_set_note (REG_UNUSED, insn, reg);
3441 dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3442 if (REG_DEAD_DEBUGGING)
3443 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3446 return;
3450 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3451 info: lifetime, bb, and number of defs and uses for basic block
3452 BB. The three bitvectors are scratch regs used here. */
3454 static void
3455 df_note_bb_compute (unsigned int bb_index,
3456 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3458 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3459 rtx_insn *insn;
3460 df_ref def, use;
3461 struct dead_debug_local debug;
3463 dead_debug_local_init (&debug, NULL, NULL);
3465 bitmap_copy (live, df_get_live_out (bb));
3466 bitmap_clear (artificial_uses);
3468 if (REG_DEAD_DEBUGGING && dump_file)
3470 fprintf (dump_file, "live at bottom ");
3471 df_print_regset (dump_file, live);
3474 /* Process the artificial defs and uses at the bottom of the block
3475 to begin processing. */
3476 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3478 if (REG_DEAD_DEBUGGING && dump_file)
3479 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3481 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3482 bitmap_clear_bit (live, DF_REF_REGNO (def));
3485 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3486 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3488 unsigned int regno = DF_REF_REGNO (use);
3489 bitmap_set_bit (live, regno);
3491 /* Notes are not generated for any of the artificial registers
3492 at the bottom of the block. */
3493 bitmap_set_bit (artificial_uses, regno);
3496 if (REG_DEAD_DEBUGGING && dump_file)
3498 fprintf (dump_file, "live before artificials out ");
3499 df_print_regset (dump_file, live);
3502 FOR_BB_INSNS_REVERSE (bb, insn)
3504 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3505 df_mw_hardreg *mw;
3506 int debug_insn;
3508 if (!INSN_P (insn))
3509 continue;
3511 debug_insn = DEBUG_INSN_P (insn);
3513 bitmap_clear (do_not_gen);
3514 df_remove_dead_and_unused_notes (insn);
3516 /* Process the defs. */
3517 if (CALL_P (insn))
3519 if (REG_DEAD_DEBUGGING && dump_file)
3521 fprintf (dump_file, "processing call %d\n live =",
3522 INSN_UID (insn));
3523 df_print_regset (dump_file, live);
3526 /* We only care about real sets for calls. Clobbers cannot
3527 be depended on to really die. */
3528 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3529 if ((DF_MWS_REG_DEF_P (mw))
3530 && !df_ignore_stack_reg (mw->start_regno))
3531 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3532 artificial_uses, &debug);
3534 /* All of the defs except the return value are some sort of
3535 clobber. This code is for the return. */
3536 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3538 unsigned int dregno = DF_REF_REGNO (def);
3539 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3541 df_create_unused_note (insn,
3542 def, live, artificial_uses, &debug);
3543 bitmap_set_bit (do_not_gen, dregno);
3546 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3547 bitmap_clear_bit (live, dregno);
3550 else
3552 /* Regular insn. */
3553 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3554 if (DF_MWS_REG_DEF_P (mw))
3555 df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3556 artificial_uses, &debug);
3558 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3560 unsigned int dregno = DF_REF_REGNO (def);
3561 df_create_unused_note (insn,
3562 def, live, artificial_uses, &debug);
3564 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3565 bitmap_set_bit (do_not_gen, dregno);
3567 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3568 bitmap_clear_bit (live, dregno);
3572 /* Process the uses. */
3573 FOR_EACH_INSN_INFO_MW (mw, insn_info)
3574 if (DF_MWS_REG_USE_P (mw)
3575 && !df_ignore_stack_reg (mw->start_regno))
3577 bool really_add_notes = debug_insn != 0;
3579 df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3580 artificial_uses,
3581 &really_add_notes);
3583 if (really_add_notes)
3584 debug_insn = -1;
3587 FOR_EACH_INSN_INFO_USE (use, insn_info)
3589 unsigned int uregno = DF_REF_REGNO (use);
3591 if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3593 fprintf (dump_file, " regular looking at use ");
3594 df_ref_debug (use, dump_file);
3597 if (!bitmap_bit_p (live, uregno))
3599 if (debug_insn)
3601 if (debug_insn > 0)
3603 /* We won't add REG_UNUSED or REG_DEAD notes for
3604 these, so we don't have to mess with them in
3605 debug insns either. */
3606 if (!bitmap_bit_p (artificial_uses, uregno)
3607 && !df_ignore_stack_reg (uregno))
3608 dead_debug_add (&debug, use, uregno);
3609 continue;
3611 break;
3613 else
3614 dead_debug_insert_temp (&debug, uregno, insn,
3615 DEBUG_TEMP_BEFORE_WITH_REG);
3617 if ( (!(DF_REF_FLAGS (use)
3618 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3619 && (!bitmap_bit_p (do_not_gen, uregno))
3620 && (!bitmap_bit_p (artificial_uses, uregno))
3621 && (!df_ignore_stack_reg (uregno)))
3623 rtx reg = (DF_REF_LOC (use))
3624 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3625 df_set_note (REG_DEAD, insn, reg);
3627 if (REG_DEAD_DEBUGGING)
3628 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3630 /* This register is now live. */
3631 bitmap_set_bit (live, uregno);
3635 df_remove_dead_eq_notes (insn, live);
3637 if (debug_insn == -1)
3639 /* ??? We could probably do better here, replacing dead
3640 registers with their definitions. */
3641 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3642 df_insn_rescan_debug_internal (insn);
3646 dead_debug_local_finish (&debug, NULL);
3650 /* Compute register info: lifetime, bb, and number of defs and uses. */
3651 static void
3652 df_note_compute (bitmap all_blocks)
3654 unsigned int bb_index;
3655 bitmap_iterator bi;
3656 bitmap_head live, do_not_gen, artificial_uses;
3658 bitmap_initialize (&live, &df_bitmap_obstack);
3659 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3660 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3662 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3664 /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3665 pseudos in debug insns because we don't always (re)visit blocks
3666 with death points after visiting dead uses. Even changing this
3667 loop to postorder would still leave room for visiting a death
3668 point before visiting a subsequent debug use. */
3669 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3672 bitmap_clear (&live);
3673 bitmap_clear (&do_not_gen);
3674 bitmap_clear (&artificial_uses);
3678 /* Free all storage associated with the problem. */
3680 static void
3681 df_note_free (void)
3683 free (df_note);
3687 /* All of the information associated every instance of the problem. */
3689 static struct df_problem problem_NOTE =
3691 DF_NOTE, /* Problem id. */
3692 DF_NONE, /* Direction. */
3693 df_note_alloc, /* Allocate the problem specific data. */
3694 NULL, /* Reset global information. */
3695 NULL, /* Free basic block info. */
3696 df_note_compute, /* Local compute function. */
3697 NULL, /* Init the solution specific data. */
3698 NULL, /* Iterative solver. */
3699 NULL, /* Confluence operator 0. */
3700 NULL, /* Confluence operator n. */
3701 NULL, /* Transfer function. */
3702 NULL, /* Finalize function. */
3703 df_note_free, /* Free all of the problem information. */
3704 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3705 NULL, /* Debugging. */
3706 NULL, /* Debugging start block. */
3707 NULL, /* Debugging end block. */
3708 NULL, /* Debugging start insn. */
3709 NULL, /* Debugging end insn. */
3710 NULL, /* Incremental solution verify start. */
3711 NULL, /* Incremental solution verify end. */
3712 &problem_LR, /* Dependent problem. */
3713 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
3714 TV_DF_NOTE, /* Timing variable. */
3715 false /* Reset blocks on dropping out of blocks_to_analyze. */
3719 /* Create a new DATAFLOW instance and add it to an existing instance
3720 of DF. The returned structure is what is used to get at the
3721 solution. */
3723 void
3724 df_note_add_problem (void)
3726 df_add_problem (&problem_NOTE);
3732 /*----------------------------------------------------------------------------
3733 Functions for simulating the effects of single insns.
3735 You can either simulate in the forwards direction, starting from
3736 the top of a block or the backwards direction from the end of the
3737 block. If you go backwards, defs are examined first to clear bits,
3738 then uses are examined to set bits. If you go forwards, defs are
3739 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3740 are examined to clear bits. In either case, the result of examining
3741 a def can be undone (respectively by a use or a REG_UNUSED note).
3743 If you start at the top of the block, use one of DF_LIVE_IN or
3744 DF_LR_IN. If you start at the bottom of the block use one of
3745 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3746 THEY WILL BE DESTROYED.
3747 ----------------------------------------------------------------------------*/
3750 /* Find the set of DEFs for INSN. */
3752 void
3753 df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3755 df_ref def;
3757 FOR_EACH_INSN_DEF (def, insn)
3758 bitmap_set_bit (defs, DF_REF_REGNO (def));
3761 /* Find the set of uses for INSN. This includes partial defs. */
3763 static void
3764 df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3766 df_ref def, use;
3767 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3769 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3770 if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3771 bitmap_set_bit (uses, DF_REF_REGNO (def));
3772 FOR_EACH_INSN_INFO_USE (use, insn_info)
3773 bitmap_set_bit (uses, DF_REF_REGNO (use));
3776 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3778 void
3779 df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3781 df_ref def;
3783 FOR_EACH_INSN_DEF (def, insn)
3784 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3785 bitmap_set_bit (defs, DF_REF_REGNO (def));
3789 /* Simulate the effects of the defs of INSN on LIVE. */
3791 void
3792 df_simulate_defs (rtx_insn *insn, bitmap live)
3794 df_ref def;
3796 FOR_EACH_INSN_DEF (def, insn)
3798 unsigned int dregno = DF_REF_REGNO (def);
3800 /* If the def is to only part of the reg, it does
3801 not kill the other defs that reach here. */
3802 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3803 bitmap_clear_bit (live, dregno);
3808 /* Simulate the effects of the uses of INSN on LIVE. */
3810 void
3811 df_simulate_uses (rtx_insn *insn, bitmap live)
3813 df_ref use;
3815 if (DEBUG_INSN_P (insn))
3816 return;
3818 FOR_EACH_INSN_USE (use, insn)
3819 /* Add use to set of uses in this BB. */
3820 bitmap_set_bit (live, DF_REF_REGNO (use));
3824 /* Add back the always live regs in BB to LIVE. */
3826 static inline void
3827 df_simulate_fixup_sets (basic_block bb, bitmap live)
3829 /* These regs are considered always live so if they end up dying
3830 because of some def, we need to bring the back again. */
3831 if (bb_has_eh_pred (bb))
3832 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3833 else
3834 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3838 /*----------------------------------------------------------------------------
3839 The following three functions are used only for BACKWARDS scanning:
3840 i.e. they process the defs before the uses.
3842 df_simulate_initialize_backwards should be called first with a
3843 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
3844 df_simulate_one_insn_backwards should be called for each insn in
3845 the block, starting with the last one. Finally,
3846 df_simulate_finalize_backwards can be called to get a new value
3847 of the sets at the top of the block (this is rarely used).
3848 ----------------------------------------------------------------------------*/
3850 /* Apply the artificial uses and defs at the end of BB in a backwards
3851 direction. */
3853 void
3854 df_simulate_initialize_backwards (basic_block bb, bitmap live)
3856 df_ref def, use;
3857 int bb_index = bb->index;
3859 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3860 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3861 bitmap_clear_bit (live, DF_REF_REGNO (def));
3863 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3864 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3865 bitmap_set_bit (live, DF_REF_REGNO (use));
3869 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
3871 void
3872 df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3874 if (!NONDEBUG_INSN_P (insn))
3875 return;
3877 df_simulate_defs (insn, live);
3878 df_simulate_uses (insn, live);
3879 df_simulate_fixup_sets (bb, live);
3883 /* Apply the artificial uses and defs at the top of BB in a backwards
3884 direction. */
3886 void
3887 df_simulate_finalize_backwards (basic_block bb, bitmap live)
3889 df_ref def;
3890 #ifdef EH_USES
3891 df_ref use;
3892 #endif
3893 int bb_index = bb->index;
3895 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3896 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3897 bitmap_clear_bit (live, DF_REF_REGNO (def));
3899 #ifdef EH_USES
3900 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3901 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3902 bitmap_set_bit (live, DF_REF_REGNO (use));
3903 #endif
3905 /*----------------------------------------------------------------------------
3906 The following three functions are used only for FORWARDS scanning:
3907 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
3908 Thus it is important to add the DF_NOTES problem to the stack of
3909 problems computed before using these functions.
3911 df_simulate_initialize_forwards should be called first with a
3912 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
3913 df_simulate_one_insn_forwards should be called for each insn in
3914 the block, starting with the first one.
3915 ----------------------------------------------------------------------------*/
3917 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
3918 DF_LR_IN for basic block BB, for forward scanning by marking artificial
3919 defs live. */
3921 void
3922 df_simulate_initialize_forwards (basic_block bb, bitmap live)
3924 df_ref def;
3925 int bb_index = bb->index;
3927 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3928 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3929 bitmap_set_bit (live, DF_REF_REGNO (def));
3932 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
3934 void
3935 df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
3937 rtx link;
3938 if (! INSN_P (insn))
3939 return;
3941 /* Make sure that DF_NOTE really is an active df problem. */
3942 gcc_assert (df_note);
3944 /* Note that this is the opposite as how the problem is defined, because
3945 in the LR problem defs _kill_ liveness. However, they do so backwards,
3946 while here the scan is performed forwards! So, first assume that the
3947 def is live, and if this is not true REG_UNUSED notes will rectify the
3948 situation. */
3949 df_simulate_find_noclobber_defs (insn, live);
3951 /* Clear all of the registers that go dead. */
3952 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3954 switch (REG_NOTE_KIND (link))
3956 case REG_DEAD:
3957 case REG_UNUSED:
3959 rtx reg = XEXP (link, 0);
3960 bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
3962 break;
3963 default:
3964 break;
3967 df_simulate_fixup_sets (bb, live);
3970 /* Used by the next two functions to encode information about the
3971 memory references we found. */
3972 #define MEMREF_NORMAL 1
3973 #define MEMREF_VOLATILE 2
3975 /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X. */
3977 static int
3978 find_memory (rtx_insn *insn)
3980 int flags = 0;
3981 subrtx_iterator::array_type array;
3982 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3984 const_rtx x = *iter;
3985 if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
3986 flags |= MEMREF_VOLATILE;
3987 else if (MEM_P (x))
3989 if (MEM_VOLATILE_P (x))
3990 flags |= MEMREF_VOLATILE;
3991 else if (!MEM_READONLY_P (x))
3992 flags |= MEMREF_NORMAL;
3995 return flags;
3998 /* A subroutine of can_move_insns_across_p called through note_stores.
3999 DATA points to an integer in which we set either the bit for
4000 MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
4001 of either kind. */
4003 static void
4004 find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4005 void *data ATTRIBUTE_UNUSED)
4007 int *pflags = (int *)data;
4008 if (GET_CODE (x) == SUBREG)
4009 x = XEXP (x, 0);
4010 /* Treat stores to SP as stores to memory, this will prevent problems
4011 when there are references to the stack frame. */
4012 if (x == stack_pointer_rtx)
4013 *pflags |= MEMREF_VOLATILE;
4014 if (!MEM_P (x))
4015 return;
4016 *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4019 /* Scan BB backwards, using df_simulate functions to keep track of
4020 lifetimes, up to insn POINT. The result is stored in LIVE. */
4022 void
4023 simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4025 rtx_insn *insn;
4026 bitmap_copy (live, df_get_live_out (bb));
4027 df_simulate_initialize_backwards (bb, live);
4029 /* Scan and update life information until we reach the point we're
4030 interested in. */
4031 for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4032 df_simulate_one_insn_backwards (bb, insn, live);
4035 /* Return true if it is safe to move a group of insns, described by
4036 the range FROM to TO, backwards across another group of insns,
4037 described by ACROSS_FROM to ACROSS_TO. It is assumed that there
4038 are no insns between ACROSS_TO and FROM, but they may be in
4039 different basic blocks; MERGE_BB is the block from which the
4040 insns will be moved. The caller must pass in a regset MERGE_LIVE
4041 which specifies the registers live after TO.
4043 This function may be called in one of two cases: either we try to
4044 move identical instructions from all successor blocks into their
4045 predecessor, or we try to move from only one successor block. If
4046 OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4047 the second case. It should contain a set of registers live at the
4048 end of ACROSS_TO which must not be clobbered by moving the insns.
4049 In that case, we're also more careful about moving memory references
4050 and trapping insns.
4052 We return false if it is not safe to move the entire group, but it
4053 may still be possible to move a subgroup. PMOVE_UPTO, if nonnull,
4054 is set to point at the last moveable insn in such a case. */
4056 bool
4057 can_move_insns_across (rtx_insn *from, rtx_insn *to,
4058 rtx_insn *across_from, rtx_insn *across_to,
4059 basic_block merge_bb, regset merge_live,
4060 regset other_branch_live, rtx_insn **pmove_upto)
4062 rtx_insn *insn, *next, *max_to;
4063 bitmap merge_set, merge_use, local_merge_live;
4064 bitmap test_set, test_use;
4065 unsigned i, fail = 0;
4066 bitmap_iterator bi;
4067 int memrefs_in_across = 0;
4068 int mem_sets_in_across = 0;
4069 bool trapping_insns_in_across = false;
4071 if (pmove_upto != NULL)
4072 *pmove_upto = NULL;
4074 /* Find real bounds, ignoring debug insns. */
4075 while (!NONDEBUG_INSN_P (from) && from != to)
4076 from = NEXT_INSN (from);
4077 while (!NONDEBUG_INSN_P (to) && from != to)
4078 to = PREV_INSN (to);
4080 for (insn = across_to; ; insn = next)
4082 if (CALL_P (insn))
4084 if (RTL_CONST_OR_PURE_CALL_P (insn))
4085 /* Pure functions can read from memory. Const functions can
4086 read from arguments that the ABI has forced onto the stack.
4087 Neither sort of read can be volatile. */
4088 memrefs_in_across |= MEMREF_NORMAL;
4089 else
4091 memrefs_in_across |= MEMREF_VOLATILE;
4092 mem_sets_in_across |= MEMREF_VOLATILE;
4095 if (NONDEBUG_INSN_P (insn))
4097 if (volatile_insn_p (PATTERN (insn)))
4098 return false;
4099 memrefs_in_across |= find_memory (insn);
4100 note_stores (PATTERN (insn), find_memory_stores,
4101 &mem_sets_in_across);
4102 /* This is used just to find sets of the stack pointer. */
4103 memrefs_in_across |= mem_sets_in_across;
4104 trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4106 next = PREV_INSN (insn);
4107 if (insn == across_from)
4108 break;
4111 /* Collect:
4112 MERGE_SET = set of registers set in MERGE_BB
4113 MERGE_USE = set of registers used in MERGE_BB and live at its top
4114 MERGE_LIVE = set of registers live at the point inside the MERGE
4115 range that we've reached during scanning
4116 TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4117 TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4118 and live before ACROSS_FROM. */
4120 merge_set = BITMAP_ALLOC (&reg_obstack);
4121 merge_use = BITMAP_ALLOC (&reg_obstack);
4122 local_merge_live = BITMAP_ALLOC (&reg_obstack);
4123 test_set = BITMAP_ALLOC (&reg_obstack);
4124 test_use = BITMAP_ALLOC (&reg_obstack);
4126 /* Compute the set of registers set and used in the ACROSS range. */
4127 if (other_branch_live != NULL)
4128 bitmap_copy (test_use, other_branch_live);
4129 df_simulate_initialize_backwards (merge_bb, test_use);
4130 for (insn = across_to; ; insn = next)
4132 if (NONDEBUG_INSN_P (insn))
4134 df_simulate_find_defs (insn, test_set);
4135 df_simulate_defs (insn, test_use);
4136 df_simulate_uses (insn, test_use);
4138 next = PREV_INSN (insn);
4139 if (insn == across_from)
4140 break;
4143 /* Compute an upper bound for the amount of insns moved, by finding
4144 the first insn in MERGE that sets a register in TEST_USE, or uses
4145 a register in TEST_SET. We also check for calls, trapping operations,
4146 and memory references. */
4147 max_to = NULL;
4148 for (insn = from; ; insn = next)
4150 if (CALL_P (insn))
4151 break;
4152 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4153 break;
4154 if (NONDEBUG_INSN_P (insn))
4156 if (may_trap_or_fault_p (PATTERN (insn))
4157 && (trapping_insns_in_across
4158 || other_branch_live != NULL
4159 || volatile_insn_p (PATTERN (insn))))
4160 break;
4162 /* We cannot move memory stores past each other, or move memory
4163 reads past stores, at least not without tracking them and
4164 calling true_dependence on every pair.
4166 If there is no other branch and no memory references or
4167 sets in the ACROSS range, we can move memory references
4168 freely, even volatile ones.
4170 Otherwise, the rules are as follows: volatile memory
4171 references and stores can't be moved at all, and any type
4172 of memory reference can't be moved if there are volatile
4173 accesses or stores in the ACROSS range. That leaves
4174 normal reads, which can be moved, as the trapping case is
4175 dealt with elsewhere. */
4176 if (other_branch_live != NULL || memrefs_in_across != 0)
4178 int mem_ref_flags = 0;
4179 int mem_set_flags = 0;
4180 note_stores (PATTERN (insn), find_memory_stores, &mem_set_flags);
4181 mem_ref_flags = find_memory (insn);
4182 /* Catch sets of the stack pointer. */
4183 mem_ref_flags |= mem_set_flags;
4185 if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4186 break;
4187 if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4188 break;
4189 if (mem_set_flags != 0
4190 || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4191 break;
4193 df_simulate_find_uses (insn, merge_use);
4194 /* We're only interested in uses which use a value live at
4195 the top, not one previously set in this block. */
4196 bitmap_and_compl_into (merge_use, merge_set);
4197 df_simulate_find_defs (insn, merge_set);
4198 if (bitmap_intersect_p (merge_set, test_use)
4199 || bitmap_intersect_p (merge_use, test_set))
4200 break;
4201 if (!HAVE_cc0 || !sets_cc0_p (insn))
4202 max_to = insn;
4204 next = NEXT_INSN (insn);
4205 if (insn == to)
4206 break;
4208 if (max_to != to)
4209 fail = 1;
4211 if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4212 goto out;
4214 /* Now, lower this upper bound by also taking into account that
4215 a range of insns moved across ACROSS must not leave a register
4216 live at the end that will be clobbered in ACROSS. We need to
4217 find a point where TEST_SET & LIVE == 0.
4219 Insns in the MERGE range that set registers which are also set
4220 in the ACROSS range may still be moved as long as we also move
4221 later insns which use the results of the set, and make the
4222 register dead again. This is verified by the condition stated
4223 above. We only need to test it for registers that are set in
4224 the moved region.
4226 MERGE_LIVE is provided by the caller and holds live registers after
4227 TO. */
4228 bitmap_copy (local_merge_live, merge_live);
4229 for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4230 df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4232 /* We're not interested in registers that aren't set in the moved
4233 region at all. */
4234 bitmap_and_into (local_merge_live, merge_set);
4235 for (;;)
4237 if (NONDEBUG_INSN_P (insn))
4239 if (!bitmap_intersect_p (test_set, local_merge_live)
4240 && (!HAVE_cc0 || !sets_cc0_p (insn)))
4242 max_to = insn;
4243 break;
4246 df_simulate_one_insn_backwards (merge_bb, insn,
4247 local_merge_live);
4249 if (insn == from)
4251 fail = 1;
4252 goto out;
4254 insn = PREV_INSN (insn);
4257 if (max_to != to)
4258 fail = 1;
4260 if (pmove_upto)
4261 *pmove_upto = max_to;
4263 /* For small register class machines, don't lengthen lifetimes of
4264 hard registers before reload. */
4265 if (! reload_completed
4266 && targetm.small_register_classes_for_mode_p (VOIDmode))
4268 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4270 if (i < FIRST_PSEUDO_REGISTER
4271 && ! fixed_regs[i]
4272 && ! global_regs[i])
4274 fail = 1;
4275 break;
4280 out:
4281 BITMAP_FREE (merge_set);
4282 BITMAP_FREE (merge_use);
4283 BITMAP_FREE (local_merge_live);
4284 BITMAP_FREE (test_set);
4285 BITMAP_FREE (test_use);
4287 return !fail;
4291 /*----------------------------------------------------------------------------
4292 MULTIPLE DEFINITIONS
4294 Find the locations in the function reached by multiple definition sites
4295 for a live pseudo. In and out bitvectors are built for each basic
4296 block. They are restricted for efficiency to live registers.
4298 The gen and kill sets for the problem are obvious. Together they
4299 include all defined registers in a basic block; the gen set includes
4300 registers where a partial or conditional or may-clobber definition is
4301 last in the BB, while the kill set includes registers with a complete
4302 definition coming last. However, the computation of the dataflow
4303 itself is interesting.
4305 The idea behind it comes from SSA form's iterated dominance frontier
4306 criterion for inserting PHI functions. Just like in that case, we can use
4307 the dominance frontier to find places where multiple definitions meet;
4308 a register X defined in a basic block BB1 has multiple definitions in
4309 basic blocks in BB1's dominance frontier.
4311 So, the in-set of a basic block BB2 is not just the union of the
4312 out-sets of BB2's predecessors, but includes some more bits that come
4313 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4314 the previous paragraph). I called this set the init-set of BB2.
4316 (Note: I actually use the kill-set only to build the init-set.
4317 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4319 For example, if you have
4321 BB1 : r10 = 0
4322 r11 = 0
4323 if <...> goto BB2 else goto BB3;
4325 BB2 : r10 = 1
4326 r12 = 1
4327 goto BB3;
4329 BB3 :
4331 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4332 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4333 not need to iterate the dominance frontier, because we do not insert
4334 anything like PHI functions there! Instead, dataflow will take care of
4335 propagating the information to BB3's successors.
4336 ---------------------------------------------------------------------------*/
4338 /* Private data used to verify the solution for this problem. */
4339 struct df_md_problem_data
4341 /* An obstack for the bitmaps we need for this problem. */
4342 bitmap_obstack md_bitmaps;
4345 /* Scratch var used by transfer functions. This is used to do md analysis
4346 only for live registers. */
4347 static bitmap_head df_md_scratch;
4350 static void
4351 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4352 void *vbb_info)
4354 struct df_md_bb_info *bb_info = (struct df_md_bb_info *) vbb_info;
4355 if (bb_info)
4357 bitmap_clear (&bb_info->kill);
4358 bitmap_clear (&bb_info->gen);
4359 bitmap_clear (&bb_info->init);
4360 bitmap_clear (&bb_info->in);
4361 bitmap_clear (&bb_info->out);
4366 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4367 not touched unless the block is new. */
4369 static void
4370 df_md_alloc (bitmap all_blocks)
4372 unsigned int bb_index;
4373 bitmap_iterator bi;
4374 struct df_md_problem_data *problem_data;
4376 df_grow_bb_info (df_md);
4377 if (df_md->problem_data)
4378 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4379 else
4381 problem_data = XNEW (struct df_md_problem_data);
4382 df_md->problem_data = problem_data;
4383 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4385 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4387 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4389 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4390 /* When bitmaps are already initialized, just clear them. */
4391 if (bb_info->init.obstack)
4393 bitmap_clear (&bb_info->init);
4394 bitmap_clear (&bb_info->gen);
4395 bitmap_clear (&bb_info->kill);
4396 bitmap_clear (&bb_info->in);
4397 bitmap_clear (&bb_info->out);
4399 else
4401 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4402 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4403 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4404 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4405 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4409 df_md->optional_p = true;
4412 /* Add the effect of the top artificial defs of BB to the multiple definitions
4413 bitmap LOCAL_MD. */
4415 void
4416 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4418 int bb_index = bb->index;
4419 df_ref def;
4420 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4421 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4423 unsigned int dregno = DF_REF_REGNO (def);
4424 if (DF_REF_FLAGS (def)
4425 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4426 bitmap_set_bit (local_md, dregno);
4427 else
4428 bitmap_clear_bit (local_md, dregno);
4433 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4434 LOCAL_MD. */
4436 void
4437 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4438 bitmap local_md)
4440 df_ref def;
4442 FOR_EACH_INSN_DEF (def, insn)
4444 unsigned int dregno = DF_REF_REGNO (def);
4445 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4446 || (dregno >= FIRST_PSEUDO_REGISTER))
4448 if (DF_REF_FLAGS (def)
4449 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4450 bitmap_set_bit (local_md, DF_REF_ID (def));
4451 else
4452 bitmap_clear_bit (local_md, DF_REF_ID (def));
4457 static void
4458 df_md_bb_local_compute_process_def (struct df_md_bb_info *bb_info,
4459 df_ref def,
4460 int top_flag)
4462 bitmap_clear (&seen_in_insn);
4464 for (; def; def = DF_REF_NEXT_LOC (def))
4466 unsigned int dregno = DF_REF_REGNO (def);
4467 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4468 || (dregno >= FIRST_PSEUDO_REGISTER))
4469 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4471 if (!bitmap_bit_p (&seen_in_insn, dregno))
4473 if (DF_REF_FLAGS (def)
4474 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4476 bitmap_set_bit (&bb_info->gen, dregno);
4477 bitmap_clear_bit (&bb_info->kill, dregno);
4479 else
4481 /* When we find a clobber and a regular def,
4482 make sure the regular def wins. */
4483 bitmap_set_bit (&seen_in_insn, dregno);
4484 bitmap_set_bit (&bb_info->kill, dregno);
4485 bitmap_clear_bit (&bb_info->gen, dregno);
4493 /* Compute local multiple def info for basic block BB. */
4495 static void
4496 df_md_bb_local_compute (unsigned int bb_index)
4498 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4499 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4500 rtx_insn *insn;
4502 /* Artificials are only hard regs. */
4503 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4504 df_md_bb_local_compute_process_def (bb_info,
4505 df_get_artificial_defs (bb_index),
4506 DF_REF_AT_TOP);
4508 FOR_BB_INSNS (bb, insn)
4510 unsigned int uid = INSN_UID (insn);
4511 if (!INSN_P (insn))
4512 continue;
4514 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4517 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4518 df_md_bb_local_compute_process_def (bb_info,
4519 df_get_artificial_defs (bb_index),
4523 /* Compute local reaching def info for each basic block within BLOCKS. */
4525 static void
4526 df_md_local_compute (bitmap all_blocks)
4528 unsigned int bb_index, df_bb_index;
4529 bitmap_iterator bi1, bi2;
4530 basic_block bb;
4531 bitmap_head *frontiers;
4533 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4535 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4537 df_md_bb_local_compute (bb_index);
4540 bitmap_clear (&seen_in_insn);
4542 frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4543 FOR_ALL_BB_FN (bb, cfun)
4544 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4546 compute_dominance_frontiers (frontiers);
4548 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4549 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4551 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4552 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4554 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4555 if (bitmap_bit_p (all_blocks, df_bb_index))
4556 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4557 df_get_live_in (bb));
4561 FOR_ALL_BB_FN (bb, cfun)
4562 bitmap_clear (&frontiers[bb->index]);
4563 free (frontiers);
4567 /* Reset the global solution for recalculation. */
4569 static void
4570 df_md_reset (bitmap all_blocks)
4572 unsigned int bb_index;
4573 bitmap_iterator bi;
4575 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4577 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4578 gcc_assert (bb_info);
4579 bitmap_clear (&bb_info->in);
4580 bitmap_clear (&bb_info->out);
4584 static bool
4585 df_md_transfer_function (int bb_index)
4587 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4588 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4589 bitmap in = &bb_info->in;
4590 bitmap out = &bb_info->out;
4591 bitmap gen = &bb_info->gen;
4592 bitmap kill = &bb_info->kill;
4594 /* We need to use a scratch set here so that the value returned from this
4595 function invocation properly reflects whether the sets changed in a
4596 significant way; i.e. not just because the live set was anded in. */
4597 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4599 /* Multiple definitions of a register are not relevant if it is not
4600 live. Thus we trim the result to the places where it is live. */
4601 bitmap_and_into (in, df_get_live_in (bb));
4603 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4606 /* Initialize the solution bit vectors for problem. */
4608 static void
4609 df_md_init (bitmap all_blocks)
4611 unsigned int bb_index;
4612 bitmap_iterator bi;
4614 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4616 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4618 bitmap_copy (&bb_info->in, &bb_info->init);
4619 df_md_transfer_function (bb_index);
4623 static void
4624 df_md_confluence_0 (basic_block bb)
4626 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4627 bitmap_copy (&bb_info->in, &bb_info->init);
4630 /* In of target gets or of out of source. */
4632 static bool
4633 df_md_confluence_n (edge e)
4635 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4636 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4638 if (e->flags & EDGE_FAKE)
4639 return false;
4641 if (e->flags & EDGE_EH)
4642 return bitmap_ior_and_compl_into (op1, op2,
4643 regs_invalidated_by_call_regset);
4644 else
4645 return bitmap_ior_into (op1, op2);
4648 /* Free all storage associated with the problem. */
4650 static void
4651 df_md_free (void)
4653 struct df_md_problem_data *problem_data
4654 = (struct df_md_problem_data *) df_md->problem_data;
4656 bitmap_obstack_release (&problem_data->md_bitmaps);
4657 free (problem_data);
4658 df_md->problem_data = NULL;
4660 df_md->block_info_size = 0;
4661 free (df_md->block_info);
4662 df_md->block_info = NULL;
4663 free (df_md);
4667 /* Debugging info at top of bb. */
4669 static void
4670 df_md_top_dump (basic_block bb, FILE *file)
4672 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4673 if (!bb_info)
4674 return;
4676 fprintf (file, ";; md in \t");
4677 df_print_regset (file, &bb_info->in);
4678 fprintf (file, ";; md init \t");
4679 df_print_regset (file, &bb_info->init);
4680 fprintf (file, ";; md gen \t");
4681 df_print_regset (file, &bb_info->gen);
4682 fprintf (file, ";; md kill \t");
4683 df_print_regset (file, &bb_info->kill);
4686 /* Debugging info at bottom of bb. */
4688 static void
4689 df_md_bottom_dump (basic_block bb, FILE *file)
4691 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4692 if (!bb_info)
4693 return;
4695 fprintf (file, ";; md out \t");
4696 df_print_regset (file, &bb_info->out);
4699 static struct df_problem problem_MD =
4701 DF_MD, /* Problem id. */
4702 DF_FORWARD, /* Direction. */
4703 df_md_alloc, /* Allocate the problem specific data. */
4704 df_md_reset, /* Reset global information. */
4705 df_md_free_bb_info, /* Free basic block info. */
4706 df_md_local_compute, /* Local compute function. */
4707 df_md_init, /* Init the solution specific data. */
4708 df_worklist_dataflow, /* Worklist solver. */
4709 df_md_confluence_0, /* Confluence operator 0. */
4710 df_md_confluence_n, /* Confluence operator n. */
4711 df_md_transfer_function, /* Transfer function. */
4712 NULL, /* Finalize function. */
4713 df_md_free, /* Free all of the problem information. */
4714 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4715 NULL, /* Debugging. */
4716 df_md_top_dump, /* Debugging start block. */
4717 df_md_bottom_dump, /* Debugging end block. */
4718 NULL, /* Debugging start insn. */
4719 NULL, /* Debugging end insn. */
4720 NULL, /* Incremental solution verify start. */
4721 NULL, /* Incremental solution verify end. */
4722 NULL, /* Dependent problem. */
4723 sizeof (struct df_md_bb_info),/* Size of entry of block_info array. */
4724 TV_DF_MD, /* Timing variable. */
4725 false /* Reset blocks on dropping out of blocks_to_analyze. */
4728 /* Create a new MD instance and add it to the existing instance
4729 of DF. */
4731 void
4732 df_md_add_problem (void)
4734 df_add_problem (&problem_MD);