]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/df-problems.c
* df-problems.c (df_live_scratch): Convert to bitmap_head.
[thirdparty/gcc.git] / gcc / df-problems.c
1 /* Standard problems for dataflow support routines.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 Free Software Foundation, Inc.
4 Originally contributed by Michael P. Hayes
5 (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
6 Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
7 and Kenneth Zadeck (zadeck@naturalbridge.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "regs.h"
35 #include "output.h"
36 #include "alloc-pool.h"
37 #include "flags.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "sbitmap.h"
41 #include "bitmap.h"
42 #include "timevar.h"
43 #include "df.h"
44 #include "except.h"
45 #include "dce.h"
46 #include "vecprim.h"
47
48 /* Note that turning REG_DEAD_DEBUGGING on will cause
49 gcc.c-torture/unsorted/dump-noaddr.c to fail because it prints
50 addresses in the dumps. */
51 #if 0
52 #define REG_DEAD_DEBUGGING
53 #endif
54
55 #define DF_SPARSE_THRESHOLD 32
56
57 static bitmap_head seen_in_block;
58 static bitmap_head seen_in_insn;
59
60 \f
61 /*----------------------------------------------------------------------------
62 Public functions access functions for the dataflow problems.
63 ----------------------------------------------------------------------------*/
64 /* Get the live at out set for BB no matter what problem happens to be
65 defined. This function is used by the register allocators who
66 choose different dataflow problems depending on the optimization
67 level. */
68
69 bitmap
70 df_get_live_out (basic_block bb)
71 {
72 gcc_assert (df_lr);
73
74 if (df_live)
75 return DF_LIVE_OUT (bb);
76 else
77 return DF_LR_OUT (bb);
78 }
79
80 /* Get the live at in set for BB no matter what problem happens to be
81 defined. This function is used by the register allocators who
82 choose different dataflow problems depending on the optimization
83 level. */
84
85 bitmap
86 df_get_live_in (basic_block bb)
87 {
88 gcc_assert (df_lr);
89
90 if (df_live)
91 return DF_LIVE_IN (bb);
92 else
93 return DF_LR_IN (bb);
94 }
95
96 /*----------------------------------------------------------------------------
97 Utility functions.
98 ----------------------------------------------------------------------------*/
99
100 /* Generic versions to get the void* version of the block info. Only
101 used inside the problem instance vectors. */
102
103 /* Grow the bb_info array. */
104
105 void
106 df_grow_bb_info (struct dataflow *dflow)
107 {
108 unsigned int new_size = last_basic_block + 1;
109 if (dflow->block_info_size < new_size)
110 {
111 new_size += new_size / 4;
112 dflow->block_info = XRESIZEVEC (void *, dflow->block_info, new_size);
113 memset (dflow->block_info + dflow->block_info_size, 0,
114 (new_size - dflow->block_info_size) *sizeof (void *));
115 dflow->block_info_size = new_size;
116 }
117 }
118
119 /* Dump a def-use or use-def chain for REF to FILE. */
120
121 void
122 df_chain_dump (struct df_link *link, FILE *file)
123 {
124 fprintf (file, "{ ");
125 for (; link; link = link->next)
126 {
127 fprintf (file, "%c%d(bb %d insn %d) ",
128 DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
129 DF_REF_ID (link->ref),
130 DF_REF_BBNO (link->ref),
131 DF_REF_IS_ARTIFICIAL (link->ref) ? -1 : DF_REF_INSN_UID (link->ref));
132 }
133 fprintf (file, "}");
134 }
135
136
137 /* Print some basic block info as part of df_dump. */
138
139 void
140 df_print_bb_index (basic_block bb, FILE *file)
141 {
142 edge e;
143 edge_iterator ei;
144
145 fprintf (file, "\n( ");
146 FOR_EACH_EDGE (e, ei, bb->preds)
147 {
148 basic_block pred = e->src;
149 fprintf (file, "%d%s ", pred->index, e->flags & EDGE_EH ? "(EH)" : "");
150 }
151 fprintf (file, ")->[%d]->( ", bb->index);
152 FOR_EACH_EDGE (e, ei, bb->succs)
153 {
154 basic_block succ = e->dest;
155 fprintf (file, "%d%s ", succ->index, e->flags & EDGE_EH ? "(EH)" : "");
156 }
157 fprintf (file, ")\n");
158 }
159
160 \f
161 /*----------------------------------------------------------------------------
162 REACHING DEFINITIONS
163
164 Find the locations in the function where each definition site for a
165 pseudo reaches. In and out bitvectors are built for each basic
166 block. The id field in the ref is used to index into these sets.
167 See df.h for details.
168 ----------------------------------------------------------------------------*/
169
170 /* This problem plays a large number of games for the sake of
171 efficiency.
172
173 1) The order of the bits in the bitvectors. After the scanning
174 phase, all of the defs are sorted. All of the defs for the reg 0
175 are first, followed by all defs for reg 1 and so on.
176
177 2) There are two kill sets, one if the number of defs is less or
178 equal to DF_SPARSE_THRESHOLD and another if the number of defs is
179 greater.
180
181 <= : Data is built directly in the kill set.
182
183 > : One level of indirection is used to keep from generating long
184 strings of 1 bits in the kill sets. Bitvectors that are indexed
185 by the regnum are used to represent that there is a killing def
186 for the register. The confluence and transfer functions use
187 these along with the bitmap_clear_range call to remove ranges of
188 bits without actually generating a knockout vector.
189
190 The kill and sparse_kill and the dense_invalidated_by_call and
191 sparse_invalidated_by_call both play this game. */
192
193 /* Private data used to compute the solution for this problem. These
194 data structures are not accessible outside of this module. */
195 struct df_rd_problem_data
196 {
197 /* The set of defs to regs invalidated by call. */
198 bitmap_head sparse_invalidated_by_call;
199 /* The set of defs to regs invalidate by call for rd. */
200 bitmap_head dense_invalidated_by_call;
201 /* An obstack for the bitmaps we need for this problem. */
202 bitmap_obstack rd_bitmaps;
203 };
204
205 /* Set basic block info. */
206
207 static void
208 df_rd_set_bb_info (unsigned int index,
209 struct df_rd_bb_info *bb_info)
210 {
211 gcc_assert (df_rd);
212 gcc_assert (index < df_rd->block_info_size);
213 df_rd->block_info[index] = bb_info;
214 }
215
216
217 /* Free basic block info. */
218
219 static void
220 df_rd_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
221 void *vbb_info)
222 {
223 struct df_rd_bb_info *bb_info = (struct df_rd_bb_info *) vbb_info;
224 if (bb_info)
225 {
226 bitmap_clear (&bb_info->kill);
227 bitmap_clear (&bb_info->sparse_kill);
228 bitmap_clear (&bb_info->gen);
229 bitmap_clear (&bb_info->in);
230 bitmap_clear (&bb_info->out);
231 pool_free (df_rd->block_pool, bb_info);
232 }
233 }
234
235
236 /* Allocate or reset bitmaps for DF_RD blocks. The solution bits are
237 not touched unless the block is new. */
238
239 static void
240 df_rd_alloc (bitmap all_blocks)
241 {
242 unsigned int bb_index;
243 bitmap_iterator bi;
244 struct df_rd_problem_data *problem_data;
245
246 if (!df_rd->block_pool)
247 df_rd->block_pool = create_alloc_pool ("df_rd_block pool",
248 sizeof (struct df_rd_bb_info), 50);
249
250 if (df_rd->problem_data)
251 {
252 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
253 bitmap_clear (&problem_data->sparse_invalidated_by_call);
254 bitmap_clear (&problem_data->dense_invalidated_by_call);
255 }
256 else
257 {
258 problem_data = XNEW (struct df_rd_problem_data);
259 df_rd->problem_data = problem_data;
260
261 bitmap_obstack_initialize (&problem_data->rd_bitmaps);
262 bitmap_initialize (&problem_data->sparse_invalidated_by_call,
263 &problem_data->rd_bitmaps);
264 bitmap_initialize (&problem_data->dense_invalidated_by_call,
265 &problem_data->rd_bitmaps);
266 }
267
268 df_grow_bb_info (df_rd);
269
270 /* Because of the clustering of all use sites for the same pseudo,
271 we have to process all of the blocks before doing the
272 analysis. */
273
274 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
275 {
276 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
277 if (bb_info)
278 {
279 bitmap_clear (&bb_info->kill);
280 bitmap_clear (&bb_info->sparse_kill);
281 bitmap_clear (&bb_info->gen);
282 }
283 else
284 {
285 bb_info = (struct df_rd_bb_info *) pool_alloc (df_rd->block_pool);
286 df_rd_set_bb_info (bb_index, bb_info);
287 bitmap_initialize (&bb_info->kill, &problem_data->rd_bitmaps);
288 bitmap_initialize (&bb_info->sparse_kill, &problem_data->rd_bitmaps);
289 bitmap_initialize (&bb_info->gen, &problem_data->rd_bitmaps);
290 bitmap_initialize (&bb_info->in, &problem_data->rd_bitmaps);
291 bitmap_initialize (&bb_info->out, &problem_data->rd_bitmaps);
292 }
293 }
294 df_rd->optional_p = true;
295 }
296
297
298 /* Add the effect of the top artificial defs of BB to the reaching definitions
299 bitmap LOCAL_RD. */
300
301 void
302 df_rd_simulate_artificial_defs_at_top (basic_block bb, bitmap local_rd)
303 {
304 int bb_index = bb->index;
305 df_ref *def_rec;
306 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
307 {
308 df_ref def = *def_rec;
309 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
310 {
311 unsigned int dregno = DF_REF_REGNO (def);
312 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
313 bitmap_clear_range (local_rd,
314 DF_DEFS_BEGIN (dregno),
315 DF_DEFS_COUNT (dregno));
316 bitmap_set_bit (local_rd, DF_REF_ID (def));
317 }
318 }
319 }
320
321 /* Add the effect of the defs of INSN to the reaching definitions bitmap
322 LOCAL_RD. */
323
324 void
325 df_rd_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx insn,
326 bitmap local_rd)
327 {
328 unsigned uid = INSN_UID (insn);
329 df_ref *def_rec;
330
331 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
332 {
333 df_ref def = *def_rec;
334 unsigned int dregno = DF_REF_REGNO (def);
335 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
336 || (dregno >= FIRST_PSEUDO_REGISTER))
337 {
338 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
339 bitmap_clear_range (local_rd,
340 DF_DEFS_BEGIN (dregno),
341 DF_DEFS_COUNT (dregno));
342 if (!(DF_REF_FLAGS (def)
343 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
344 bitmap_set_bit (local_rd, DF_REF_ID (def));
345 }
346 }
347 }
348
349 /* Process a list of DEFs for df_rd_bb_local_compute. This is a bit
350 more complicated than just simulating, because we must produce the
351 gen and kill sets and hence deal with the two possible representations
352 of kill sets. */
353
354 static void
355 df_rd_bb_local_compute_process_def (struct df_rd_bb_info *bb_info,
356 df_ref *def_rec,
357 int top_flag)
358 {
359 while (*def_rec)
360 {
361 df_ref def = *def_rec;
362 if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
363 {
364 unsigned int regno = DF_REF_REGNO (def);
365 unsigned int begin = DF_DEFS_BEGIN (regno);
366 unsigned int n_defs = DF_DEFS_COUNT (regno);
367
368 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
369 || (regno >= FIRST_PSEUDO_REGISTER))
370 {
371 /* Only the last def(s) for a regno in the block has any
372 effect. */
373 if (!bitmap_bit_p (&seen_in_block, regno))
374 {
375 /* The first def for regno in insn gets to knock out the
376 defs from other instructions. */
377 if ((!bitmap_bit_p (&seen_in_insn, regno))
378 /* If the def is to only part of the reg, it does
379 not kill the other defs that reach here. */
380 && (!(DF_REF_FLAGS (def) &
381 (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))))
382 {
383 if (n_defs > DF_SPARSE_THRESHOLD)
384 {
385 bitmap_set_bit (&bb_info->sparse_kill, regno);
386 bitmap_clear_range(&bb_info->gen, begin, n_defs);
387 }
388 else
389 {
390 bitmap_set_range (&bb_info->kill, begin, n_defs);
391 bitmap_clear_range (&bb_info->gen, begin, n_defs);
392 }
393 }
394
395 bitmap_set_bit (&seen_in_insn, regno);
396 /* All defs for regno in the instruction may be put into
397 the gen set. */
398 if (!(DF_REF_FLAGS (def)
399 & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
400 bitmap_set_bit (&bb_info->gen, DF_REF_ID (def));
401 }
402 }
403 }
404 def_rec++;
405 }
406 }
407
408 /* Compute local reaching def info for basic block BB. */
409
410 static void
411 df_rd_bb_local_compute (unsigned int bb_index)
412 {
413 basic_block bb = BASIC_BLOCK (bb_index);
414 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
415 rtx insn;
416
417 bitmap_clear (&seen_in_block);
418 bitmap_clear (&seen_in_insn);
419
420 /* Artificials are only hard regs. */
421 if (!(df->changeable_flags & DF_NO_HARD_REGS))
422 df_rd_bb_local_compute_process_def (bb_info,
423 df_get_artificial_defs (bb_index),
424 0);
425
426 FOR_BB_INSNS_REVERSE (bb, insn)
427 {
428 unsigned int uid = INSN_UID (insn);
429
430 if (!INSN_P (insn))
431 continue;
432
433 df_rd_bb_local_compute_process_def (bb_info,
434 DF_INSN_UID_DEFS (uid), 0);
435
436 /* This complex dance with the two bitmaps is required because
437 instructions can assign twice to the same pseudo. This
438 generally happens with calls that will have one def for the
439 result and another def for the clobber. If only one vector
440 is used and the clobber goes first, the result will be
441 lost. */
442 bitmap_ior_into (&seen_in_block, &seen_in_insn);
443 bitmap_clear (&seen_in_insn);
444 }
445
446 /* Process the artificial defs at the top of the block last since we
447 are going backwards through the block and these are logically at
448 the start. */
449 if (!(df->changeable_flags & DF_NO_HARD_REGS))
450 df_rd_bb_local_compute_process_def (bb_info,
451 df_get_artificial_defs (bb_index),
452 DF_REF_AT_TOP);
453 }
454
455
456 /* Compute local reaching def info for each basic block within BLOCKS. */
457
458 static void
459 df_rd_local_compute (bitmap all_blocks)
460 {
461 unsigned int bb_index;
462 bitmap_iterator bi;
463 unsigned int regno;
464 struct df_rd_problem_data *problem_data
465 = (struct df_rd_problem_data *) df_rd->problem_data;
466 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
467 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
468
469 bitmap_initialize (&seen_in_block, &df_bitmap_obstack);
470 bitmap_initialize (&seen_in_insn, &df_bitmap_obstack);
471
472 df_maybe_reorganize_def_refs (DF_REF_ORDER_BY_REG);
473
474 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
475 {
476 df_rd_bb_local_compute (bb_index);
477 }
478
479 /* Set up the knockout bit vectors to be applied across EH_EDGES. */
480 EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, regno, bi)
481 {
482 if (DF_DEFS_COUNT (regno) > DF_SPARSE_THRESHOLD)
483 bitmap_set_bit (sparse_invalidated, regno);
484 else
485 bitmap_set_range (dense_invalidated,
486 DF_DEFS_BEGIN (regno),
487 DF_DEFS_COUNT (regno));
488 }
489
490 bitmap_clear (&seen_in_block);
491 bitmap_clear (&seen_in_insn);
492 }
493
494
495 /* Initialize the solution bit vectors for problem. */
496
497 static void
498 df_rd_init_solution (bitmap all_blocks)
499 {
500 unsigned int bb_index;
501 bitmap_iterator bi;
502
503 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
504 {
505 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
506
507 bitmap_copy (&bb_info->out, &bb_info->gen);
508 bitmap_clear (&bb_info->in);
509 }
510 }
511
512 /* In of target gets or of out of source. */
513
514 static void
515 df_rd_confluence_n (edge e)
516 {
517 bitmap op1 = &df_rd_get_bb_info (e->dest->index)->in;
518 bitmap op2 = &df_rd_get_bb_info (e->src->index)->out;
519
520 if (e->flags & EDGE_FAKE)
521 return;
522
523 if (e->flags & EDGE_EH)
524 {
525 struct df_rd_problem_data *problem_data
526 = (struct df_rd_problem_data *) df_rd->problem_data;
527 bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
528 bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
529 bitmap_iterator bi;
530 unsigned int regno;
531 bitmap_head tmp;
532
533 bitmap_initialize (&tmp, &df_bitmap_obstack);
534 bitmap_copy (&tmp, op2);
535 bitmap_and_compl_into (&tmp, dense_invalidated);
536
537 EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
538 {
539 bitmap_clear_range (&tmp,
540 DF_DEFS_BEGIN (regno),
541 DF_DEFS_COUNT (regno));
542 }
543 bitmap_ior_into (op1, &tmp);
544 bitmap_clear (&tmp);
545 }
546 else
547 bitmap_ior_into (op1, op2);
548 }
549
550
551 /* Transfer function. */
552
553 static bool
554 df_rd_transfer_function (int bb_index)
555 {
556 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
557 unsigned int regno;
558 bitmap_iterator bi;
559 bitmap in = &bb_info->in;
560 bitmap out = &bb_info->out;
561 bitmap gen = &bb_info->gen;
562 bitmap kill = &bb_info->kill;
563 bitmap sparse_kill = &bb_info->sparse_kill;
564
565 if (bitmap_empty_p (sparse_kill))
566 return bitmap_ior_and_compl (out, gen, in, kill);
567 else
568 {
569 struct df_rd_problem_data *problem_data;
570 bool changed = false;
571 bitmap_head tmp;
572
573 /* Note that TMP is _not_ a temporary bitmap if we end up replacing
574 OUT with TMP. Therefore, allocate TMP in the RD bitmaps obstack. */
575 problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
576 bitmap_initialize (&tmp, &problem_data->rd_bitmaps);
577
578 bitmap_copy (&tmp, in);
579 EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
580 {
581 bitmap_clear_range (&tmp,
582 DF_DEFS_BEGIN (regno),
583 DF_DEFS_COUNT (regno));
584 }
585 bitmap_and_compl_into (&tmp, kill);
586 bitmap_ior_into (&tmp, gen);
587 changed = !bitmap_equal_p (&tmp, out);
588 if (changed)
589 {
590 bitmap_clear (out);
591 bb_info->out = tmp;
592 }
593 else
594 bitmap_clear (&tmp);
595 return changed;
596 }
597 }
598
599
600 /* Free all storage associated with the problem. */
601
602 static void
603 df_rd_free (void)
604 {
605 struct df_rd_problem_data *problem_data
606 = (struct df_rd_problem_data *) df_rd->problem_data;
607
608 if (problem_data)
609 {
610 free_alloc_pool (df_rd->block_pool);
611 bitmap_obstack_release (&problem_data->rd_bitmaps);
612
613 df_rd->block_info_size = 0;
614 free (df_rd->block_info);
615 free (df_rd->problem_data);
616 }
617 free (df_rd);
618 }
619
620
621 /* Debugging info. */
622
623 static void
624 df_rd_start_dump (FILE *file)
625 {
626 struct df_rd_problem_data *problem_data
627 = (struct df_rd_problem_data *) df_rd->problem_data;
628 unsigned int m = DF_REG_SIZE(df);
629 unsigned int regno;
630
631 if (!df_rd->block_info)
632 return;
633
634 fprintf (file, ";; Reaching defs:\n\n");
635
636 fprintf (file, " sparse invalidated \t");
637 dump_bitmap (file, &problem_data->sparse_invalidated_by_call);
638 fprintf (file, " dense invalidated \t");
639 dump_bitmap (file, &problem_data->dense_invalidated_by_call);
640
641 for (regno = 0; regno < m; regno++)
642 if (DF_DEFS_COUNT (regno))
643 fprintf (file, "%d[%d,%d] ", regno,
644 DF_DEFS_BEGIN (regno),
645 DF_DEFS_COUNT (regno));
646 fprintf (file, "\n");
647
648 }
649
650
651 /* Debugging info at top of bb. */
652
653 static void
654 df_rd_top_dump (basic_block bb, FILE *file)
655 {
656 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
657 if (!bb_info)
658 return;
659
660 fprintf (file, ";; rd in \t(%d)\n", (int) bitmap_count_bits (&bb_info->in));
661 dump_bitmap (file, &bb_info->in);
662 fprintf (file, ";; rd gen \t(%d)\n", (int) bitmap_count_bits (&bb_info->gen));
663 dump_bitmap (file, &bb_info->gen);
664 fprintf (file, ";; rd kill\t(%d)\n", (int) bitmap_count_bits (&bb_info->kill));
665 dump_bitmap (file, &bb_info->kill);
666 }
667
668
669 /* Debugging info at top of bb. */
670
671 static void
672 df_rd_bottom_dump (basic_block bb, FILE *file)
673 {
674 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
675 if (!bb_info)
676 return;
677
678 fprintf (file, ";; rd out \t(%d)\n", (int) bitmap_count_bits (&bb_info->out));
679 dump_bitmap (file, &bb_info->out);
680 }
681
682 /* All of the information associated with every instance of the problem. */
683
684 static struct df_problem problem_RD =
685 {
686 DF_RD, /* Problem id. */
687 DF_FORWARD, /* Direction. */
688 df_rd_alloc, /* Allocate the problem specific data. */
689 NULL, /* Reset global information. */
690 df_rd_free_bb_info, /* Free basic block info. */
691 df_rd_local_compute, /* Local compute function. */
692 df_rd_init_solution, /* Init the solution specific data. */
693 df_worklist_dataflow, /* Worklist solver. */
694 NULL, /* Confluence operator 0. */
695 df_rd_confluence_n, /* Confluence operator n. */
696 df_rd_transfer_function, /* Transfer function. */
697 NULL, /* Finalize function. */
698 df_rd_free, /* Free all of the problem information. */
699 df_rd_free, /* Remove this problem from the stack of dataflow problems. */
700 df_rd_start_dump, /* Debugging. */
701 df_rd_top_dump, /* Debugging start block. */
702 df_rd_bottom_dump, /* Debugging end block. */
703 NULL, /* Incremental solution verify start. */
704 NULL, /* Incremental solution verify end. */
705 NULL, /* Dependent problem. */
706 TV_DF_RD, /* Timing variable. */
707 true /* Reset blocks on dropping out of blocks_to_analyze. */
708 };
709
710
711
712 /* Create a new RD instance and add it to the existing instance
713 of DF. */
714
715 void
716 df_rd_add_problem (void)
717 {
718 df_add_problem (&problem_RD);
719 }
720
721
722 \f
723 /*----------------------------------------------------------------------------
724 LIVE REGISTERS
725
726 Find the locations in the function where any use of a pseudo can
727 reach in the backwards direction. In and out bitvectors are built
728 for each basic block. The regno is used to index into these sets.
729 See df.h for details.
730 ----------------------------------------------------------------------------*/
731
732 /* Private data used to verify the solution for this problem. */
733 struct df_lr_problem_data
734 {
735 bitmap_head *in;
736 bitmap_head *out;
737 /* An obstack for the bitmaps we need for this problem. */
738 bitmap_obstack lr_bitmaps;
739 };
740
741
742 /* Set basic block info. */
743
744 static void
745 df_lr_set_bb_info (unsigned int index,
746 struct df_lr_bb_info *bb_info)
747 {
748 gcc_assert (df_lr);
749 gcc_assert (index < df_lr->block_info_size);
750 df_lr->block_info[index] = bb_info;
751 }
752
753
754 /* Free basic block info. */
755
756 static void
757 df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
758 void *vbb_info)
759 {
760 struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
761 if (bb_info)
762 {
763 bitmap_clear (&bb_info->use);
764 bitmap_clear (&bb_info->def);
765 bitmap_clear (&bb_info->in);
766 bitmap_clear (&bb_info->out);
767 pool_free (df_lr->block_pool, bb_info);
768 }
769 }
770
771
772 /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
773 not touched unless the block is new. */
774
775 static void
776 df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
777 {
778 unsigned int bb_index;
779 bitmap_iterator bi;
780 struct df_lr_problem_data *problem_data;
781
782 if (!df_lr->block_pool)
783 df_lr->block_pool = create_alloc_pool ("df_lr_block pool",
784 sizeof (struct df_lr_bb_info), 50);
785
786 df_grow_bb_info (df_lr);
787 if (df_lr->problem_data)
788 problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
789 else
790 {
791 problem_data = XNEW (struct df_lr_problem_data);
792 df_lr->problem_data = problem_data;
793
794 problem_data->out = NULL;
795 problem_data->in = NULL;
796 bitmap_obstack_initialize (&problem_data->lr_bitmaps);
797 }
798
799 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
800 {
801 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
802 if (bb_info)
803 {
804 bitmap_clear (&bb_info->def);
805 bitmap_clear (&bb_info->use);
806 }
807 else
808 {
809 bb_info = (struct df_lr_bb_info *) pool_alloc (df_lr->block_pool);
810 df_lr_set_bb_info (bb_index, bb_info);
811 bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
812 bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
813 bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
814 bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
815 }
816 }
817
818 df_lr->optional_p = false;
819 }
820
821
822 /* Reset the global solution for recalculation. */
823
824 static void
825 df_lr_reset (bitmap all_blocks)
826 {
827 unsigned int bb_index;
828 bitmap_iterator bi;
829
830 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
831 {
832 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
833 gcc_assert (bb_info);
834 bitmap_clear (&bb_info->in);
835 bitmap_clear (&bb_info->out);
836 }
837 }
838
839
840 /* Compute local live register info for basic block BB. */
841
842 static void
843 df_lr_bb_local_compute (unsigned int bb_index)
844 {
845 basic_block bb = BASIC_BLOCK (bb_index);
846 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
847 rtx insn;
848 df_ref *def_rec;
849 df_ref *use_rec;
850
851 /* Process the registers set in an exception handler. */
852 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
853 {
854 df_ref def = *def_rec;
855 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
856 {
857 unsigned int dregno = DF_REF_REGNO (def);
858 bitmap_set_bit (&bb_info->def, dregno);
859 bitmap_clear_bit (&bb_info->use, dregno);
860 }
861 }
862
863 /* Process the hardware registers that are always live. */
864 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
865 {
866 df_ref use = *use_rec;
867 /* Add use to set of uses in this BB. */
868 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
869 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
870 }
871
872 FOR_BB_INSNS_REVERSE (bb, insn)
873 {
874 unsigned int uid = INSN_UID (insn);
875
876 if (!NONDEBUG_INSN_P (insn))
877 continue;
878
879 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
880 {
881 df_ref def = *def_rec;
882 /* If the def is to only part of the reg, it does
883 not kill the other defs that reach here. */
884 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
885 {
886 unsigned int dregno = DF_REF_REGNO (def);
887 bitmap_set_bit (&bb_info->def, dregno);
888 bitmap_clear_bit (&bb_info->use, dregno);
889 }
890 }
891
892 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
893 {
894 df_ref use = *use_rec;
895 /* Add use to set of uses in this BB. */
896 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
897 }
898 }
899
900 /* Process the registers set in an exception handler or the hard
901 frame pointer if this block is the target of a non local
902 goto. */
903 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
904 {
905 df_ref def = *def_rec;
906 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
907 {
908 unsigned int dregno = DF_REF_REGNO (def);
909 bitmap_set_bit (&bb_info->def, dregno);
910 bitmap_clear_bit (&bb_info->use, dregno);
911 }
912 }
913
914 #ifdef EH_USES
915 /* Process the uses that are live into an exception handler. */
916 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
917 {
918 df_ref use = *use_rec;
919 /* Add use to set of uses in this BB. */
920 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
921 bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
922 }
923 #endif
924
925 /* If the df_live problem is not defined, such as at -O0 and -O1, we
926 still need to keep the luids up to date. This is normally done
927 in the df_live problem since this problem has a forwards
928 scan. */
929 if (!df_live)
930 df_recompute_luids (bb);
931 }
932
933
934 /* Compute local live register info for each basic block within BLOCKS. */
935
936 static void
937 df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
938 {
939 unsigned int bb_index;
940 bitmap_iterator bi;
941
942 bitmap_clear (&df->hardware_regs_used);
943
944 /* The all-important stack pointer must always be live. */
945 bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
946
947 /* Before reload, there are a few registers that must be forced
948 live everywhere -- which might not already be the case for
949 blocks within infinite loops. */
950 if (!reload_completed)
951 {
952 /* Any reference to any pseudo before reload is a potential
953 reference of the frame pointer. */
954 bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
955
956 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
957 /* Pseudos with argument area equivalences may require
958 reloading via the argument pointer. */
959 if (fixed_regs[ARG_POINTER_REGNUM])
960 bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
961 #endif
962
963 /* Any constant, or pseudo with constant equivalences, may
964 require reloading from memory using the pic register. */
965 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
966 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
967 bitmap_set_bit (&df->hardware_regs_used, PIC_OFFSET_TABLE_REGNUM);
968 }
969
970 EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
971 {
972 if (bb_index == EXIT_BLOCK)
973 {
974 /* The exit block is special for this problem and its bits are
975 computed from thin air. */
976 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
977 bitmap_copy (&bb_info->use, df->exit_block_uses);
978 }
979 else
980 df_lr_bb_local_compute (bb_index);
981 }
982
983 bitmap_clear (df_lr->out_of_date_transfer_functions);
984 }
985
986
987 /* Initialize the solution vectors. */
988
989 static void
990 df_lr_init (bitmap all_blocks)
991 {
992 unsigned int bb_index;
993 bitmap_iterator bi;
994
995 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
996 {
997 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
998 bitmap_copy (&bb_info->in, &bb_info->use);
999 bitmap_clear (&bb_info->out);
1000 }
1001 }
1002
1003
1004 /* Confluence function that processes infinite loops. This might be a
1005 noreturn function that throws. And even if it isn't, getting the
1006 unwind info right helps debugging. */
1007 static void
1008 df_lr_confluence_0 (basic_block bb)
1009 {
1010 bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
1011 if (bb != EXIT_BLOCK_PTR)
1012 bitmap_copy (op1, &df->hardware_regs_used);
1013 }
1014
1015
1016 /* Confluence function that ignores fake edges. */
1017
1018 static void
1019 df_lr_confluence_n (edge e)
1020 {
1021 bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
1022 bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
1023
1024 /* Call-clobbered registers die across exception and call edges. */
1025 /* ??? Abnormal call edges ignored for the moment, as this gets
1026 confused by sibling call edges, which crashes reg-stack. */
1027 if (e->flags & EDGE_EH)
1028 bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
1029 else
1030 bitmap_ior_into (op1, op2);
1031
1032 bitmap_ior_into (op1, &df->hardware_regs_used);
1033 }
1034
1035
1036 /* Transfer function. */
1037
1038 static bool
1039 df_lr_transfer_function (int bb_index)
1040 {
1041 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1042 bitmap in = &bb_info->in;
1043 bitmap out = &bb_info->out;
1044 bitmap use = &bb_info->use;
1045 bitmap def = &bb_info->def;
1046
1047 return bitmap_ior_and_compl (in, use, out, def);
1048 }
1049
1050
1051 /* Run the fast dce as a side effect of building LR. */
1052
1053 static void
1054 df_lr_finalize (bitmap all_blocks)
1055 {
1056 df_lr->solutions_dirty = false;
1057 if (df->changeable_flags & DF_LR_RUN_DCE)
1058 {
1059 run_fast_df_dce ();
1060
1061 /* If dce deletes some instructions, we need to recompute the lr
1062 solution before proceeding further. The problem is that fast
1063 dce is a pessimestic dataflow algorithm. In the case where
1064 it deletes a statement S inside of a loop, the uses inside of
1065 S may not be deleted from the dataflow solution because they
1066 were carried around the loop. While it is conservatively
1067 correct to leave these extra bits, the standards of df
1068 require that we maintain the best possible (least fixed
1069 point) solution. The only way to do that is to redo the
1070 iteration from the beginning. See PR35805 for an
1071 example. */
1072 if (df_lr->solutions_dirty)
1073 {
1074 df_clear_flags (DF_LR_RUN_DCE);
1075 df_lr_alloc (all_blocks);
1076 df_lr_local_compute (all_blocks);
1077 df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1078 df_lr_finalize (all_blocks);
1079 df_set_flags (DF_LR_RUN_DCE);
1080 }
1081 }
1082 }
1083
1084
1085 /* Free all storage associated with the problem. */
1086
1087 static void
1088 df_lr_free (void)
1089 {
1090 struct df_lr_problem_data *problem_data
1091 = (struct df_lr_problem_data *) df_lr->problem_data;
1092 if (df_lr->block_info)
1093 {
1094 free_alloc_pool (df_lr->block_pool);
1095
1096 df_lr->block_info_size = 0;
1097 free (df_lr->block_info);
1098 bitmap_obstack_release (&problem_data->lr_bitmaps);
1099 free (df_lr->problem_data);
1100 df_lr->problem_data = NULL;
1101 }
1102
1103 BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1104 free (df_lr);
1105 }
1106
1107
1108 /* Debugging info at top of bb. */
1109
1110 static void
1111 df_lr_top_dump (basic_block bb, FILE *file)
1112 {
1113 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1114 struct df_lr_problem_data *problem_data;
1115 if (!bb_info)
1116 return;
1117
1118 fprintf (file, ";; lr in \t");
1119 df_print_regset (file, &bb_info->in);
1120 if (df_lr->problem_data)
1121 {
1122 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1123 if (problem_data->in)
1124 {
1125 fprintf (file, ";; old in \t");
1126 df_print_regset (file, &problem_data->in[bb->index]);
1127 }
1128 }
1129 fprintf (file, ";; lr use \t");
1130 df_print_regset (file, &bb_info->use);
1131 fprintf (file, ";; lr def \t");
1132 df_print_regset (file, &bb_info->def);
1133 }
1134
1135
1136 /* Debugging info at bottom of bb. */
1137
1138 static void
1139 df_lr_bottom_dump (basic_block bb, FILE *file)
1140 {
1141 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1142 struct df_lr_problem_data *problem_data;
1143 if (!bb_info)
1144 return;
1145
1146 fprintf (file, ";; lr out \t");
1147 df_print_regset (file, &bb_info->out);
1148 if (df_lr->problem_data)
1149 {
1150 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1151 if (problem_data->out)
1152 {
1153 fprintf (file, ";; old out \t");
1154 df_print_regset (file, &problem_data->out[bb->index]);
1155 }
1156 }
1157 }
1158
1159
1160 /* Build the datastructure to verify that the solution to the dataflow
1161 equations is not dirty. */
1162
1163 static void
1164 df_lr_verify_solution_start (void)
1165 {
1166 basic_block bb;
1167 struct df_lr_problem_data *problem_data;
1168 if (df_lr->solutions_dirty)
1169 return;
1170
1171 /* Set it true so that the solution is recomputed. */
1172 df_lr->solutions_dirty = true;
1173
1174 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1175 problem_data->in = XNEWVEC (bitmap_head, last_basic_block);
1176 problem_data->out = XNEWVEC (bitmap_head, last_basic_block);
1177
1178 FOR_ALL_BB (bb)
1179 {
1180 bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1181 bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1182 bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1183 bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1184 }
1185 }
1186
1187
1188 /* Compare the saved datastructure and the new solution to the dataflow
1189 equations. */
1190
1191 static void
1192 df_lr_verify_solution_end (void)
1193 {
1194 struct df_lr_problem_data *problem_data;
1195 basic_block bb;
1196
1197 problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1198
1199 if (!problem_data->out)
1200 return;
1201
1202 if (df_lr->solutions_dirty)
1203 /* Do not check if the solution is still dirty. See the comment
1204 in df_lr_finalize for details. */
1205 df_lr->solutions_dirty = false;
1206 else
1207 FOR_ALL_BB (bb)
1208 {
1209 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1210 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1211 {
1212 /*df_dump (stderr);*/
1213 gcc_unreachable ();
1214 }
1215 }
1216
1217 /* Cannot delete them immediately because you may want to dump them
1218 if the comparison fails. */
1219 FOR_ALL_BB (bb)
1220 {
1221 bitmap_clear (&problem_data->in[bb->index]);
1222 bitmap_clear (&problem_data->out[bb->index]);
1223 }
1224
1225 free (problem_data->in);
1226 free (problem_data->out);
1227 problem_data->in = NULL;
1228 problem_data->out = NULL;
1229 }
1230
1231
1232 /* All of the information associated with every instance of the problem. */
1233
1234 static struct df_problem problem_LR =
1235 {
1236 DF_LR, /* Problem id. */
1237 DF_BACKWARD, /* Direction. */
1238 df_lr_alloc, /* Allocate the problem specific data. */
1239 df_lr_reset, /* Reset global information. */
1240 df_lr_free_bb_info, /* Free basic block info. */
1241 df_lr_local_compute, /* Local compute function. */
1242 df_lr_init, /* Init the solution specific data. */
1243 df_worklist_dataflow, /* Worklist solver. */
1244 df_lr_confluence_0, /* Confluence operator 0. */
1245 df_lr_confluence_n, /* Confluence operator n. */
1246 df_lr_transfer_function, /* Transfer function. */
1247 df_lr_finalize, /* Finalize function. */
1248 df_lr_free, /* Free all of the problem information. */
1249 NULL, /* Remove this problem from the stack of dataflow problems. */
1250 NULL, /* Debugging. */
1251 df_lr_top_dump, /* Debugging start block. */
1252 df_lr_bottom_dump, /* Debugging end block. */
1253 df_lr_verify_solution_start,/* Incremental solution verify start. */
1254 df_lr_verify_solution_end, /* Incremental solution verify end. */
1255 NULL, /* Dependent problem. */
1256 TV_DF_LR, /* Timing variable. */
1257 false /* Reset blocks on dropping out of blocks_to_analyze. */
1258 };
1259
1260
1261 /* Create a new DATAFLOW instance and add it to an existing instance
1262 of DF. The returned structure is what is used to get at the
1263 solution. */
1264
1265 void
1266 df_lr_add_problem (void)
1267 {
1268 df_add_problem (&problem_LR);
1269 /* These will be initialized when df_scan_blocks processes each
1270 block. */
1271 df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (NULL);
1272 }
1273
1274
1275 /* Verify that all of the lr related info is consistent and
1276 correct. */
1277
1278 void
1279 df_lr_verify_transfer_functions (void)
1280 {
1281 basic_block bb;
1282 bitmap_head saved_def;
1283 bitmap_head saved_use;
1284 bitmap_head all_blocks;
1285
1286 if (!df)
1287 return;
1288
1289 bitmap_initialize (&saved_def, &bitmap_default_obstack);
1290 bitmap_initialize (&saved_use, &bitmap_default_obstack);
1291 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1292
1293 FOR_ALL_BB (bb)
1294 {
1295 struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1296 bitmap_set_bit (&all_blocks, bb->index);
1297
1298 if (bb_info)
1299 {
1300 /* Make a copy of the transfer functions and then compute
1301 new ones to see if the transfer functions have
1302 changed. */
1303 if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1304 bb->index))
1305 {
1306 bitmap_copy (&saved_def, &bb_info->def);
1307 bitmap_copy (&saved_use, &bb_info->use);
1308 bitmap_clear (&bb_info->def);
1309 bitmap_clear (&bb_info->use);
1310
1311 df_lr_bb_local_compute (bb->index);
1312 gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1313 gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1314 }
1315 }
1316 else
1317 {
1318 /* If we do not have basic block info, the block must be in
1319 the list of dirty blocks or else some one has added a
1320 block behind our backs. */
1321 gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1322 bb->index));
1323 }
1324 /* Make sure no one created a block without following
1325 procedures. */
1326 gcc_assert (df_scan_get_bb_info (bb->index));
1327 }
1328
1329 /* Make sure there are no dirty bits in blocks that have been deleted. */
1330 gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1331 &all_blocks));
1332
1333 bitmap_clear (&saved_def);
1334 bitmap_clear (&saved_use);
1335 bitmap_clear (&all_blocks);
1336 }
1337
1338
1339 \f
1340 /*----------------------------------------------------------------------------
1341 LIVE AND MUST-INITIALIZED REGISTERS.
1342
1343 This problem first computes the IN and OUT bitvectors for the
1344 must-initialized registers problems, which is a forward problem.
1345 It gives the set of registers for which we MUST have an available
1346 definition on any path from the entry block to the entry/exit of
1347 a basic block. Sets generate a definition, while clobbers kill
1348 a definition.
1349
1350 In and out bitvectors are built for each basic block and are indexed by
1351 regnum (see df.h for details). In and out bitvectors in struct
1352 df_live_bb_info actually refers to the must-initialized problem;
1353
1354 Then, the in and out sets for the LIVE problem itself are computed.
1355 These are the logical AND of the IN and OUT sets from the LR problem
1356 and the must-initialized problem.
1357 ----------------------------------------------------------------------------*/
1358
1359 /* Private data used to verify the solution for this problem. */
1360 struct df_live_problem_data
1361 {
1362 bitmap_head *in;
1363 bitmap_head *out;
1364 /* An obstack for the bitmaps we need for this problem. */
1365 bitmap_obstack live_bitmaps;
1366 };
1367
1368 /* Scratch var used by transfer functions. This is used to implement
1369 an optimization to reduce the amount of space used to compute the
1370 combined lr and live analysis. */
1371 static bitmap_head df_live_scratch;
1372
1373 /* Set basic block info. */
1374
1375 static void
1376 df_live_set_bb_info (unsigned int index,
1377 struct df_live_bb_info *bb_info)
1378 {
1379 gcc_assert (df_live);
1380 gcc_assert (index < df_live->block_info_size);
1381 df_live->block_info[index] = bb_info;
1382 }
1383
1384
1385 /* Free basic block info. */
1386
1387 static void
1388 df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1389 void *vbb_info)
1390 {
1391 struct df_live_bb_info *bb_info = (struct df_live_bb_info *) vbb_info;
1392 if (bb_info)
1393 {
1394 bitmap_clear (&bb_info->gen);
1395 bitmap_clear (&bb_info->kill);
1396 bitmap_clear (&bb_info->in);
1397 bitmap_clear (&bb_info->out);
1398 pool_free (df_live->block_pool, bb_info);
1399 }
1400 }
1401
1402
1403 /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1404 not touched unless the block is new. */
1405
1406 static void
1407 df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1408 {
1409 unsigned int bb_index;
1410 bitmap_iterator bi;
1411 struct df_live_problem_data *problem_data;
1412
1413 if (!df_live->block_pool)
1414 df_live->block_pool = create_alloc_pool ("df_live_block pool",
1415 sizeof (struct df_live_bb_info), 100);
1416 if (df_live->problem_data)
1417 problem_data = (struct df_live_problem_data *) df_live->problem_data;
1418 else
1419 {
1420 problem_data = XNEW (struct df_live_problem_data);
1421 df_live->problem_data = problem_data;
1422
1423 problem_data->out = NULL;
1424 problem_data->in = NULL;
1425 bitmap_obstack_initialize (&problem_data->live_bitmaps);
1426 bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1427 }
1428
1429 df_grow_bb_info (df_live);
1430
1431 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1432 {
1433 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1434 if (bb_info)
1435 {
1436 bitmap_clear (&bb_info->kill);
1437 bitmap_clear (&bb_info->gen);
1438 }
1439 else
1440 {
1441 bb_info = (struct df_live_bb_info *) pool_alloc (df_live->block_pool);
1442 df_live_set_bb_info (bb_index, bb_info);
1443 bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1444 bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1445 bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1446 bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1447 }
1448 }
1449 df_live->optional_p = (optimize <= 1);
1450 }
1451
1452
1453 /* Reset the global solution for recalculation. */
1454
1455 static void
1456 df_live_reset (bitmap all_blocks)
1457 {
1458 unsigned int bb_index;
1459 bitmap_iterator bi;
1460
1461 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1462 {
1463 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1464 gcc_assert (bb_info);
1465 bitmap_clear (&bb_info->in);
1466 bitmap_clear (&bb_info->out);
1467 }
1468 }
1469
1470
1471 /* Compute local uninitialized register info for basic block BB. */
1472
1473 static void
1474 df_live_bb_local_compute (unsigned int bb_index)
1475 {
1476 basic_block bb = BASIC_BLOCK (bb_index);
1477 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1478 rtx insn;
1479 df_ref *def_rec;
1480 int luid = 0;
1481
1482 FOR_BB_INSNS (bb, insn)
1483 {
1484 unsigned int uid = INSN_UID (insn);
1485 struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1486
1487 /* Inserting labels does not always trigger the incremental
1488 rescanning. */
1489 if (!insn_info)
1490 {
1491 gcc_assert (!INSN_P (insn));
1492 insn_info = df_insn_create_insn_record (insn);
1493 }
1494
1495 DF_INSN_INFO_LUID (insn_info) = luid;
1496 if (!INSN_P (insn))
1497 continue;
1498
1499 luid++;
1500 for (def_rec = DF_INSN_INFO_DEFS (insn_info); *def_rec; def_rec++)
1501 {
1502 df_ref def = *def_rec;
1503 unsigned int regno = DF_REF_REGNO (def);
1504
1505 if (DF_REF_FLAGS_IS_SET (def,
1506 DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1507 /* All partial or conditional def
1508 seen are included in the gen set. */
1509 bitmap_set_bit (&bb_info->gen, regno);
1510 else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1511 /* Only must clobbers for the entire reg destroy the
1512 value. */
1513 bitmap_set_bit (&bb_info->kill, regno);
1514 else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1515 bitmap_set_bit (&bb_info->gen, regno);
1516 }
1517 }
1518
1519 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
1520 {
1521 df_ref def = *def_rec;
1522 bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1523 }
1524 }
1525
1526
1527 /* Compute local uninitialized register info. */
1528
1529 static void
1530 df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1531 {
1532 unsigned int bb_index;
1533 bitmap_iterator bi;
1534
1535 df_grow_insn_info ();
1536
1537 EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1538 0, bb_index, bi)
1539 {
1540 df_live_bb_local_compute (bb_index);
1541 }
1542
1543 bitmap_clear (df_live->out_of_date_transfer_functions);
1544 }
1545
1546
1547 /* Initialize the solution vectors. */
1548
1549 static void
1550 df_live_init (bitmap all_blocks)
1551 {
1552 unsigned int bb_index;
1553 bitmap_iterator bi;
1554
1555 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1556 {
1557 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1558 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1559
1560 /* No register may reach a location where it is not used. Thus
1561 we trim the rr result to the places where it is used. */
1562 bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1563 bitmap_clear (&bb_info->in);
1564 }
1565 }
1566
1567 /* Forward confluence function that ignores fake edges. */
1568
1569 static void
1570 df_live_confluence_n (edge e)
1571 {
1572 bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1573 bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1574
1575 if (e->flags & EDGE_FAKE)
1576 return;
1577
1578 bitmap_ior_into (op1, op2);
1579 }
1580
1581
1582 /* Transfer function for the forwards must-initialized problem. */
1583
1584 static bool
1585 df_live_transfer_function (int bb_index)
1586 {
1587 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1588 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1589 bitmap in = &bb_info->in;
1590 bitmap out = &bb_info->out;
1591 bitmap gen = &bb_info->gen;
1592 bitmap kill = &bb_info->kill;
1593
1594 /* We need to use a scratch set here so that the value returned from this
1595 function invocation properly reflects whether the sets changed in a
1596 significant way; i.e. not just because the lr set was anded in. */
1597 bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1598 /* No register may reach a location where it is not used. Thus
1599 we trim the rr result to the places where it is used. */
1600 bitmap_and_into (in, &bb_lr_info->in);
1601
1602 return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1603 }
1604
1605
1606 /* And the LR info with the must-initialized registers, to produce the LIVE info. */
1607
1608 static void
1609 df_live_finalize (bitmap all_blocks)
1610 {
1611
1612 if (df_live->solutions_dirty)
1613 {
1614 bitmap_iterator bi;
1615 unsigned int bb_index;
1616
1617 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1618 {
1619 struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1620 struct df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1621
1622 /* No register may reach a location where it is not used. Thus
1623 we trim the rr result to the places where it is used. */
1624 bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1625 bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1626 }
1627
1628 df_live->solutions_dirty = false;
1629 }
1630 }
1631
1632
1633 /* Free all storage associated with the problem. */
1634
1635 static void
1636 df_live_free (void)
1637 {
1638 struct df_live_problem_data *problem_data
1639 = (struct df_live_problem_data *) df_live->problem_data;
1640 if (df_live->block_info)
1641 {
1642 free_alloc_pool (df_live->block_pool);
1643 df_live->block_info_size = 0;
1644 free (df_live->block_info);
1645 bitmap_clear (&df_live_scratch);
1646 bitmap_obstack_release (&problem_data->live_bitmaps);
1647 free (problem_data);
1648 df_live->problem_data = NULL;
1649 }
1650 BITMAP_FREE (df_live->out_of_date_transfer_functions);
1651 free (df_live);
1652 }
1653
1654
1655 /* Debugging info at top of bb. */
1656
1657 static void
1658 df_live_top_dump (basic_block bb, FILE *file)
1659 {
1660 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1661 struct df_live_problem_data *problem_data;
1662
1663 if (!bb_info)
1664 return;
1665
1666 fprintf (file, ";; live in \t");
1667 df_print_regset (file, &bb_info->in);
1668 if (df_live->problem_data)
1669 {
1670 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1671 if (problem_data->in)
1672 {
1673 fprintf (file, ";; old in \t");
1674 df_print_regset (file, &problem_data->in[bb->index]);
1675 }
1676 }
1677 fprintf (file, ";; live gen \t");
1678 df_print_regset (file, &bb_info->gen);
1679 fprintf (file, ";; live kill\t");
1680 df_print_regset (file, &bb_info->kill);
1681 }
1682
1683
1684 /* Debugging info at bottom of bb. */
1685
1686 static void
1687 df_live_bottom_dump (basic_block bb, FILE *file)
1688 {
1689 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1690 struct df_live_problem_data *problem_data;
1691
1692 if (!bb_info)
1693 return;
1694
1695 fprintf (file, ";; live out \t");
1696 df_print_regset (file, &bb_info->out);
1697 if (df_live->problem_data)
1698 {
1699 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1700 if (problem_data->out)
1701 {
1702 fprintf (file, ";; old out \t");
1703 df_print_regset (file, &problem_data->out[bb->index]);
1704 }
1705 }
1706 }
1707
1708
1709 /* Build the datastructure to verify that the solution to the dataflow
1710 equations is not dirty. */
1711
1712 static void
1713 df_live_verify_solution_start (void)
1714 {
1715 basic_block bb;
1716 struct df_live_problem_data *problem_data;
1717 if (df_live->solutions_dirty)
1718 return;
1719
1720 /* Set it true so that the solution is recomputed. */
1721 df_live->solutions_dirty = true;
1722
1723 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1724 problem_data->in = XNEWVEC (bitmap_head, last_basic_block);
1725 problem_data->out = XNEWVEC (bitmap_head, last_basic_block);
1726
1727 FOR_ALL_BB (bb)
1728 {
1729 bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1730 bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1731 bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1732 bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1733 }
1734 }
1735
1736
1737 /* Compare the saved datastructure and the new solution to the dataflow
1738 equations. */
1739
1740 static void
1741 df_live_verify_solution_end (void)
1742 {
1743 struct df_live_problem_data *problem_data;
1744 basic_block bb;
1745
1746 problem_data = (struct df_live_problem_data *)df_live->problem_data;
1747 if (!problem_data->out)
1748 return;
1749
1750 FOR_ALL_BB (bb)
1751 {
1752 if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1753 || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1754 {
1755 /*df_dump (stderr);*/
1756 gcc_unreachable ();
1757 }
1758 }
1759
1760 /* Cannot delete them immediately because you may want to dump them
1761 if the comparison fails. */
1762 FOR_ALL_BB (bb)
1763 {
1764 bitmap_clear (&problem_data->in[bb->index]);
1765 bitmap_clear (&problem_data->out[bb->index]);
1766 }
1767
1768 free (problem_data->in);
1769 free (problem_data->out);
1770 free (problem_data);
1771 df_live->problem_data = NULL;
1772 }
1773
1774
1775 /* All of the information associated with every instance of the problem. */
1776
1777 static struct df_problem problem_LIVE =
1778 {
1779 DF_LIVE, /* Problem id. */
1780 DF_FORWARD, /* Direction. */
1781 df_live_alloc, /* Allocate the problem specific data. */
1782 df_live_reset, /* Reset global information. */
1783 df_live_free_bb_info, /* Free basic block info. */
1784 df_live_local_compute, /* Local compute function. */
1785 df_live_init, /* Init the solution specific data. */
1786 df_worklist_dataflow, /* Worklist solver. */
1787 NULL, /* Confluence operator 0. */
1788 df_live_confluence_n, /* Confluence operator n. */
1789 df_live_transfer_function, /* Transfer function. */
1790 df_live_finalize, /* Finalize function. */
1791 df_live_free, /* Free all of the problem information. */
1792 df_live_free, /* Remove this problem from the stack of dataflow problems. */
1793 NULL, /* Debugging. */
1794 df_live_top_dump, /* Debugging start block. */
1795 df_live_bottom_dump, /* Debugging end block. */
1796 df_live_verify_solution_start,/* Incremental solution verify start. */
1797 df_live_verify_solution_end, /* Incremental solution verify end. */
1798 &problem_LR, /* Dependent problem. */
1799 TV_DF_LIVE, /* Timing variable. */
1800 false /* Reset blocks on dropping out of blocks_to_analyze. */
1801 };
1802
1803
1804 /* Create a new DATAFLOW instance and add it to an existing instance
1805 of DF. The returned structure is what is used to get at the
1806 solution. */
1807
1808 void
1809 df_live_add_problem (void)
1810 {
1811 df_add_problem (&problem_LIVE);
1812 /* These will be initialized when df_scan_blocks processes each
1813 block. */
1814 df_live->out_of_date_transfer_functions = BITMAP_ALLOC (NULL);
1815 }
1816
1817
1818 /* Set all of the blocks as dirty. This needs to be done if this
1819 problem is added after all of the insns have been scanned. */
1820
1821 void
1822 df_live_set_all_dirty (void)
1823 {
1824 basic_block bb;
1825 FOR_ALL_BB (bb)
1826 bitmap_set_bit (df_live->out_of_date_transfer_functions,
1827 bb->index);
1828 }
1829
1830
1831 /* Verify that all of the lr related info is consistent and
1832 correct. */
1833
1834 void
1835 df_live_verify_transfer_functions (void)
1836 {
1837 basic_block bb;
1838 bitmap_head saved_gen;
1839 bitmap_head saved_kill;
1840 bitmap_head all_blocks;
1841
1842 if (!df)
1843 return;
1844
1845 bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1846 bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1847 bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1848
1849 df_grow_insn_info ();
1850
1851 FOR_ALL_BB (bb)
1852 {
1853 struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1854 bitmap_set_bit (&all_blocks, bb->index);
1855
1856 if (bb_info)
1857 {
1858 /* Make a copy of the transfer functions and then compute
1859 new ones to see if the transfer functions have
1860 changed. */
1861 if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1862 bb->index))
1863 {
1864 bitmap_copy (&saved_gen, &bb_info->gen);
1865 bitmap_copy (&saved_kill, &bb_info->kill);
1866 bitmap_clear (&bb_info->gen);
1867 bitmap_clear (&bb_info->kill);
1868
1869 df_live_bb_local_compute (bb->index);
1870 gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1871 gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1872 }
1873 }
1874 else
1875 {
1876 /* If we do not have basic block info, the block must be in
1877 the list of dirty blocks or else some one has added a
1878 block behind our backs. */
1879 gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1880 bb->index));
1881 }
1882 /* Make sure no one created a block without following
1883 procedures. */
1884 gcc_assert (df_scan_get_bb_info (bb->index));
1885 }
1886
1887 /* Make sure there are no dirty bits in blocks that have been deleted. */
1888 gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1889 &all_blocks));
1890 bitmap_clear (&saved_gen);
1891 bitmap_clear (&saved_kill);
1892 bitmap_clear (&all_blocks);
1893 }
1894 \f
1895 /*----------------------------------------------------------------------------
1896 CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
1897
1898 Link either the defs to the uses and / or the uses to the defs.
1899
1900 These problems are set up like the other dataflow problems so that
1901 they nicely fit into the framework. They are much simpler and only
1902 involve a single traversal of instructions and an examination of
1903 the reaching defs information (the dependent problem).
1904 ----------------------------------------------------------------------------*/
1905
1906 #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
1907
1908 /* Create a du or ud chain from SRC to DST and link it into SRC. */
1909
1910 struct df_link *
1911 df_chain_create (df_ref src, df_ref dst)
1912 {
1913 struct df_link *head = DF_REF_CHAIN (src);
1914 struct df_link *link = (struct df_link *) pool_alloc (df_chain->block_pool);
1915
1916 DF_REF_CHAIN (src) = link;
1917 link->next = head;
1918 link->ref = dst;
1919 return link;
1920 }
1921
1922
1923 /* Delete any du or ud chains that start at REF and point to
1924 TARGET. */
1925 static void
1926 df_chain_unlink_1 (df_ref ref, df_ref target)
1927 {
1928 struct df_link *chain = DF_REF_CHAIN (ref);
1929 struct df_link *prev = NULL;
1930
1931 while (chain)
1932 {
1933 if (chain->ref == target)
1934 {
1935 if (prev)
1936 prev->next = chain->next;
1937 else
1938 DF_REF_CHAIN (ref) = chain->next;
1939 pool_free (df_chain->block_pool, chain);
1940 return;
1941 }
1942 prev = chain;
1943 chain = chain->next;
1944 }
1945 }
1946
1947
1948 /* Delete a du or ud chain that leave or point to REF. */
1949
1950 void
1951 df_chain_unlink (df_ref ref)
1952 {
1953 struct df_link *chain = DF_REF_CHAIN (ref);
1954 while (chain)
1955 {
1956 struct df_link *next = chain->next;
1957 /* Delete the other side if it exists. */
1958 df_chain_unlink_1 (chain->ref, ref);
1959 pool_free (df_chain->block_pool, chain);
1960 chain = next;
1961 }
1962 DF_REF_CHAIN (ref) = NULL;
1963 }
1964
1965
1966 /* Copy the du or ud chain starting at FROM_REF and attach it to
1967 TO_REF. */
1968
1969 void
1970 df_chain_copy (df_ref to_ref,
1971 struct df_link *from_ref)
1972 {
1973 while (from_ref)
1974 {
1975 df_chain_create (to_ref, from_ref->ref);
1976 from_ref = from_ref->next;
1977 }
1978 }
1979
1980
1981 /* Remove this problem from the stack of dataflow problems. */
1982
1983 static void
1984 df_chain_remove_problem (void)
1985 {
1986 bitmap_iterator bi;
1987 unsigned int bb_index;
1988
1989 /* Wholesale destruction of the old chains. */
1990 if (df_chain->block_pool)
1991 free_alloc_pool (df_chain->block_pool);
1992
1993 EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
1994 {
1995 rtx insn;
1996 df_ref *def_rec;
1997 df_ref *use_rec;
1998 basic_block bb = BASIC_BLOCK (bb_index);
1999
2000 if (df_chain_problem_p (DF_DU_CHAIN))
2001 for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
2002 DF_REF_CHAIN (*def_rec) = NULL;
2003 if (df_chain_problem_p (DF_UD_CHAIN))
2004 for (use_rec = df_get_artificial_uses (bb->index); *use_rec; use_rec++)
2005 DF_REF_CHAIN (*use_rec) = NULL;
2006
2007 FOR_BB_INSNS (bb, insn)
2008 {
2009 unsigned int uid = INSN_UID (insn);
2010
2011 if (INSN_P (insn))
2012 {
2013 if (df_chain_problem_p (DF_DU_CHAIN))
2014 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
2015 DF_REF_CHAIN (*def_rec) = NULL;
2016 if (df_chain_problem_p (DF_UD_CHAIN))
2017 {
2018 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
2019 DF_REF_CHAIN (*use_rec) = NULL;
2020 for (use_rec = DF_INSN_UID_EQ_USES (uid); *use_rec; use_rec++)
2021 DF_REF_CHAIN (*use_rec) = NULL;
2022 }
2023 }
2024 }
2025 }
2026
2027 bitmap_clear (df_chain->out_of_date_transfer_functions);
2028 df_chain->block_pool = NULL;
2029 }
2030
2031
2032 /* Remove the chain problem completely. */
2033
2034 static void
2035 df_chain_fully_remove_problem (void)
2036 {
2037 df_chain_remove_problem ();
2038 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2039 free (df_chain);
2040 }
2041
2042
2043 /* Create def-use or use-def chains. */
2044
2045 static void
2046 df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2047 {
2048 df_chain_remove_problem ();
2049 df_chain->block_pool = create_alloc_pool ("df_chain_block pool",
2050 sizeof (struct df_link), 50);
2051 df_chain->optional_p = true;
2052 }
2053
2054
2055 /* Reset all of the chains when the set of basic blocks changes. */
2056
2057 static void
2058 df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2059 {
2060 df_chain_remove_problem ();
2061 }
2062
2063
2064 /* Create the chains for a list of USEs. */
2065
2066 static void
2067 df_chain_create_bb_process_use (bitmap local_rd,
2068 df_ref *use_rec,
2069 int top_flag)
2070 {
2071 bitmap_iterator bi;
2072 unsigned int def_index;
2073
2074 while (*use_rec)
2075 {
2076 df_ref use = *use_rec;
2077 unsigned int uregno = DF_REF_REGNO (use);
2078 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2079 || (uregno >= FIRST_PSEUDO_REGISTER))
2080 {
2081 /* Do not want to go through this for an uninitialized var. */
2082 int count = DF_DEFS_COUNT (uregno);
2083 if (count)
2084 {
2085 if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2086 {
2087 unsigned int first_index = DF_DEFS_BEGIN (uregno);
2088 unsigned int last_index = first_index + count - 1;
2089
2090 EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2091 {
2092 df_ref def;
2093 if (def_index > last_index)
2094 break;
2095
2096 def = DF_DEFS_GET (def_index);
2097 if (df_chain_problem_p (DF_DU_CHAIN))
2098 df_chain_create (def, use);
2099 if (df_chain_problem_p (DF_UD_CHAIN))
2100 df_chain_create (use, def);
2101 }
2102 }
2103 }
2104 }
2105
2106 use_rec++;
2107 }
2108 }
2109
2110
2111 /* Create chains from reaching defs bitmaps for basic block BB. */
2112
2113 static void
2114 df_chain_create_bb (unsigned int bb_index)
2115 {
2116 basic_block bb = BASIC_BLOCK (bb_index);
2117 struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2118 rtx insn;
2119 bitmap_head cpy;
2120
2121 bitmap_initialize (&cpy, &bitmap_default_obstack);
2122 bitmap_copy (&cpy, &bb_info->in);
2123 bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2124
2125 /* Since we are going forwards, process the artificial uses first
2126 then the artificial defs second. */
2127
2128 #ifdef EH_USES
2129 /* Create the chains for the artificial uses from the EH_USES at the
2130 beginning of the block. */
2131
2132 /* Artificials are only hard regs. */
2133 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2134 df_chain_create_bb_process_use (&cpy,
2135 df_get_artificial_uses (bb->index),
2136 DF_REF_AT_TOP);
2137 #endif
2138
2139 df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2140
2141 /* Process the regular instructions next. */
2142 FOR_BB_INSNS (bb, insn)
2143 if (INSN_P (insn))
2144 {
2145 unsigned int uid = INSN_UID (insn);
2146
2147 /* First scan the uses and link them up with the defs that remain
2148 in the cpy vector. */
2149 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2150 if (df->changeable_flags & DF_EQ_NOTES)
2151 df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2152
2153 /* Since we are going forwards, process the defs second. */
2154 df_rd_simulate_one_insn (bb, insn, &cpy);
2155 }
2156
2157 /* Create the chains for the artificial uses of the hard registers
2158 at the end of the block. */
2159 if (!(df->changeable_flags & DF_NO_HARD_REGS))
2160 df_chain_create_bb_process_use (&cpy,
2161 df_get_artificial_uses (bb->index),
2162 0);
2163
2164 bitmap_clear (&cpy);
2165 }
2166
2167 /* Create def-use chains from reaching use bitmaps for basic blocks
2168 in BLOCKS. */
2169
2170 static void
2171 df_chain_finalize (bitmap all_blocks)
2172 {
2173 unsigned int bb_index;
2174 bitmap_iterator bi;
2175
2176 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2177 {
2178 df_chain_create_bb (bb_index);
2179 }
2180 }
2181
2182
2183 /* Free all storage associated with the problem. */
2184
2185 static void
2186 df_chain_free (void)
2187 {
2188 free_alloc_pool (df_chain->block_pool);
2189 BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2190 free (df_chain);
2191 }
2192
2193
2194 /* Debugging info. */
2195
2196 static void
2197 df_chain_top_dump (basic_block bb, FILE *file)
2198 {
2199 if (df_chain_problem_p (DF_DU_CHAIN))
2200 {
2201 rtx insn;
2202 df_ref *def_rec = df_get_artificial_defs (bb->index);
2203 if (*def_rec)
2204 {
2205
2206 fprintf (file, ";; DU chains for artificial defs\n");
2207 while (*def_rec)
2208 {
2209 df_ref def = *def_rec;
2210 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2211 df_chain_dump (DF_REF_CHAIN (def), file);
2212 fprintf (file, "\n");
2213 def_rec++;
2214 }
2215 }
2216
2217 FOR_BB_INSNS (bb, insn)
2218 {
2219 if (INSN_P (insn))
2220 {
2221 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2222 def_rec = DF_INSN_INFO_DEFS (insn_info);
2223 if (*def_rec)
2224 {
2225 fprintf (file, ";; DU chains for insn luid %d uid %d\n",
2226 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2227
2228 while (*def_rec)
2229 {
2230 df_ref def = *def_rec;
2231 fprintf (file, ";; reg %d ", DF_REF_REGNO (def));
2232 if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2233 fprintf (file, "read/write ");
2234 df_chain_dump (DF_REF_CHAIN (def), file);
2235 fprintf (file, "\n");
2236 def_rec++;
2237 }
2238 }
2239 }
2240 }
2241 }
2242 }
2243
2244
2245 static void
2246 df_chain_bottom_dump (basic_block bb, FILE *file)
2247 {
2248 if (df_chain_problem_p (DF_UD_CHAIN))
2249 {
2250 rtx insn;
2251 df_ref *use_rec = df_get_artificial_uses (bb->index);
2252
2253 if (*use_rec)
2254 {
2255 fprintf (file, ";; UD chains for artificial uses\n");
2256 while (*use_rec)
2257 {
2258 df_ref use = *use_rec;
2259 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2260 df_chain_dump (DF_REF_CHAIN (use), file);
2261 fprintf (file, "\n");
2262 use_rec++;
2263 }
2264 }
2265
2266 FOR_BB_INSNS (bb, insn)
2267 {
2268 if (INSN_P (insn))
2269 {
2270 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2271 df_ref *eq_use_rec = DF_INSN_INFO_EQ_USES (insn_info);
2272 use_rec = DF_INSN_INFO_USES (insn_info);
2273 if (*use_rec || *eq_use_rec)
2274 {
2275 fprintf (file, ";; UD chains for insn luid %d uid %d\n",
2276 DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2277
2278 while (*use_rec)
2279 {
2280 df_ref use = *use_rec;
2281 fprintf (file, ";; reg %d ", DF_REF_REGNO (use));
2282 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2283 fprintf (file, "read/write ");
2284 df_chain_dump (DF_REF_CHAIN (use), file);
2285 fprintf (file, "\n");
2286 use_rec++;
2287 }
2288 while (*eq_use_rec)
2289 {
2290 df_ref use = *eq_use_rec;
2291 fprintf (file, ";; eq_note reg %d ", DF_REF_REGNO (use));
2292 df_chain_dump (DF_REF_CHAIN (use), file);
2293 fprintf (file, "\n");
2294 eq_use_rec++;
2295 }
2296 }
2297 }
2298 }
2299 }
2300 }
2301
2302
2303 static struct df_problem problem_CHAIN =
2304 {
2305 DF_CHAIN, /* Problem id. */
2306 DF_NONE, /* Direction. */
2307 df_chain_alloc, /* Allocate the problem specific data. */
2308 df_chain_reset, /* Reset global information. */
2309 NULL, /* Free basic block info. */
2310 NULL, /* Local compute function. */
2311 NULL, /* Init the solution specific data. */
2312 NULL, /* Iterative solver. */
2313 NULL, /* Confluence operator 0. */
2314 NULL, /* Confluence operator n. */
2315 NULL, /* Transfer function. */
2316 df_chain_finalize, /* Finalize function. */
2317 df_chain_free, /* Free all of the problem information. */
2318 df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems. */
2319 NULL, /* Debugging. */
2320 df_chain_top_dump, /* Debugging start block. */
2321 df_chain_bottom_dump, /* Debugging end block. */
2322 NULL, /* Incremental solution verify start. */
2323 NULL, /* Incremental solution verify end. */
2324 &problem_RD, /* Dependent problem. */
2325 TV_DF_CHAIN, /* Timing variable. */
2326 false /* Reset blocks on dropping out of blocks_to_analyze. */
2327 };
2328
2329
2330 /* Create a new DATAFLOW instance and add it to an existing instance
2331 of DF. The returned structure is what is used to get at the
2332 solution. */
2333
2334 void
2335 df_chain_add_problem (unsigned int chain_flags)
2336 {
2337 df_add_problem (&problem_CHAIN);
2338 df_chain->local_flags = chain_flags;
2339 df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (NULL);
2340 }
2341
2342 #undef df_chain_problem_p
2343
2344 \f
2345 /*----------------------------------------------------------------------------
2346 BYTE LEVEL LIVE REGISTERS
2347
2348 Find the locations in the function where any use of a pseudo can
2349 reach in the backwards direction. In and out bitvectors are built
2350 for each basic block. There are two mapping functions,
2351 df_byte_lr_get_regno_start and df_byte_lr_get_regno_len that are
2352 used to map regnos into bit vector positions.
2353
2354 This problem differs from the regular df_lr function in the way
2355 that subregs, *_extracts and strict_low_parts are handled. In lr
2356 these are consider partial kills, here, the exact set of bytes is
2357 modeled. Note that any reg that has none of these operations is
2358 only modeled with a single bit since all operations access the
2359 entire register.
2360
2361 This problem is more brittle that the regular lr. It currently can
2362 be used in dce incrementally, but cannot be used in an environment
2363 where insns are created or modified. The problem is that the
2364 mapping of regnos to bitmap positions is relatively compact, in
2365 that if a pseudo does not do any of the byte wise operations, only
2366 one slot is allocated, rather than a slot for each byte. If insn
2367 are created, where a subreg is used for a reg that had no subregs,
2368 the mapping would be wrong. Likewise, there are no checks to see
2369 that new pseudos have been added. These issues could be addressed
2370 by adding a problem specific flag to not use the compact mapping,
2371 if there was a need to do so.
2372
2373 ----------------------------------------------------------------------------*/
2374
2375 /* Private data used to verify the solution for this problem. */
2376 struct df_byte_lr_problem_data
2377 {
2378 /* Expanded versions of bitvectors used in lr. */
2379 bitmap_head invalidated_by_call;
2380 bitmap_head hardware_regs_used;
2381
2382 /* Indexed by regno, this is true if there are subregs, extracts or
2383 strict_low_parts for this regno. */
2384 bitmap_head needs_expansion;
2385
2386 /* The start position and len for each regno in the various bit
2387 vectors. */
2388 unsigned int* regno_start;
2389 unsigned int* regno_len;
2390 /* An obstack for the bitmaps we need for this problem. */
2391 bitmap_obstack byte_lr_bitmaps;
2392 };
2393
2394
2395 /* Get the starting location for REGNO in the df_byte_lr bitmaps. */
2396
2397 int
2398 df_byte_lr_get_regno_start (unsigned int regno)
2399 {
2400 struct df_byte_lr_problem_data *problem_data
2401 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;;
2402 return problem_data->regno_start[regno];
2403 }
2404
2405
2406 /* Get the len for REGNO in the df_byte_lr bitmaps. */
2407
2408 int
2409 df_byte_lr_get_regno_len (unsigned int regno)
2410 {
2411 struct df_byte_lr_problem_data *problem_data
2412 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;;
2413 return problem_data->regno_len[regno];
2414 }
2415
2416
2417 /* Set basic block info. */
2418
2419 static void
2420 df_byte_lr_set_bb_info (unsigned int index,
2421 struct df_byte_lr_bb_info *bb_info)
2422 {
2423 gcc_assert (df_byte_lr);
2424 gcc_assert (index < df_byte_lr->block_info_size);
2425 df_byte_lr->block_info[index] = bb_info;
2426 }
2427
2428
2429 /* Free basic block info. */
2430
2431 static void
2432 df_byte_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2433 void *vbb_info)
2434 {
2435 struct df_byte_lr_bb_info *bb_info = (struct df_byte_lr_bb_info *) vbb_info;
2436 if (bb_info)
2437 {
2438 bitmap_clear (&bb_info->use);
2439 bitmap_clear (&bb_info->def);
2440 bitmap_clear (&bb_info->in);
2441 bitmap_clear (&bb_info->out);
2442 pool_free (df_byte_lr->block_pool, bb_info);
2443 }
2444 }
2445
2446
2447 /* Check all of the refs in REF_REC to see if any of them are
2448 extracts, subregs or strict_low_parts. */
2449
2450 static void
2451 df_byte_lr_check_regs (df_ref *ref_rec)
2452 {
2453 struct df_byte_lr_problem_data *problem_data
2454 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2455
2456 for (; *ref_rec; ref_rec++)
2457 {
2458 df_ref ref = *ref_rec;
2459 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_SIGN_EXTRACT
2460 | DF_REF_ZERO_EXTRACT
2461 | DF_REF_STRICT_LOW_PART)
2462 || GET_CODE (DF_REF_REG (ref)) == SUBREG)
2463 bitmap_set_bit (&problem_data->needs_expansion, DF_REF_REGNO (ref));
2464 }
2465 }
2466
2467
2468 /* Expand bitmap SRC which is indexed by regno to DEST which is indexed by
2469 regno_start and regno_len. */
2470
2471 static void
2472 df_byte_lr_expand_bitmap (bitmap dest, bitmap src)
2473 {
2474 struct df_byte_lr_problem_data *problem_data
2475 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2476 bitmap_iterator bi;
2477 unsigned int i;
2478
2479 bitmap_clear (dest);
2480 EXECUTE_IF_SET_IN_BITMAP (src, 0, i, bi)
2481 {
2482 bitmap_set_range (dest, problem_data->regno_start[i],
2483 problem_data->regno_len[i]);
2484 }
2485 }
2486
2487
2488 /* Allocate or reset bitmaps for DF_BYTE_LR blocks. The solution bits are
2489 not touched unless the block is new. */
2490
2491 static void
2492 df_byte_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2493 {
2494 unsigned int bb_index;
2495 bitmap_iterator bi;
2496 basic_block bb;
2497 unsigned int regno;
2498 unsigned int index = 0;
2499 unsigned int max_reg = max_reg_num();
2500 struct df_byte_lr_problem_data *problem_data
2501 = XNEW (struct df_byte_lr_problem_data);
2502
2503 df_byte_lr->problem_data = problem_data;
2504
2505 if (!df_byte_lr->block_pool)
2506 df_byte_lr->block_pool = create_alloc_pool ("df_byte_lr_block pool",
2507 sizeof (struct df_byte_lr_bb_info), 50);
2508
2509 df_grow_bb_info (df_byte_lr);
2510
2511 /* Create the mapping from regnos to slots. This does not change
2512 unless the problem is destroyed and recreated. In particular, if
2513 we end up deleting the only insn that used a subreg, we do not
2514 want to redo the mapping because this would invalidate everything
2515 else. */
2516
2517 bitmap_obstack_initialize (&problem_data->byte_lr_bitmaps);
2518 problem_data->regno_start = XNEWVEC (unsigned int, max_reg);
2519 problem_data->regno_len = XNEWVEC (unsigned int, max_reg);
2520 bitmap_initialize (&problem_data->hardware_regs_used,
2521 &problem_data->byte_lr_bitmaps);
2522 bitmap_initialize (&problem_data->invalidated_by_call,
2523 &problem_data->byte_lr_bitmaps);
2524 bitmap_initialize (&problem_data->needs_expansion,
2525 &problem_data->byte_lr_bitmaps);
2526
2527 /* Discover which regno's use subregs, extracts or
2528 strict_low_parts. */
2529 FOR_EACH_BB (bb)
2530 {
2531 rtx insn;
2532 FOR_BB_INSNS (bb, insn)
2533 {
2534 if (INSN_P (insn))
2535 {
2536 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2537 df_byte_lr_check_regs (DF_INSN_INFO_DEFS (insn_info));
2538 df_byte_lr_check_regs (DF_INSN_INFO_USES (insn_info));
2539 }
2540 }
2541 bitmap_set_bit (df_byte_lr->out_of_date_transfer_functions, bb->index);
2542 }
2543
2544 bitmap_set_bit (df_byte_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2545 bitmap_set_bit (df_byte_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2546
2547 /* Allocate the slots for each regno. */
2548 for (regno = 0; regno < max_reg; regno++)
2549 {
2550 int len;
2551 problem_data->regno_start[regno] = index;
2552 if (bitmap_bit_p (&problem_data->needs_expansion, regno))
2553 len = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
2554 else
2555 len = 1;
2556
2557 problem_data->regno_len[regno] = len;
2558 index += len;
2559 }
2560
2561 df_byte_lr_expand_bitmap (&problem_data->hardware_regs_used,
2562 &df->hardware_regs_used);
2563 df_byte_lr_expand_bitmap (&problem_data->invalidated_by_call,
2564 regs_invalidated_by_call_regset);
2565
2566 EXECUTE_IF_SET_IN_BITMAP (df_byte_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2567 {
2568 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb_index);
2569 if (bb_info)
2570 {
2571 bitmap_clear (&bb_info->def);
2572 bitmap_clear (&bb_info->use);
2573 }
2574 else
2575 {
2576 bb_info = (struct df_byte_lr_bb_info *) pool_alloc (df_byte_lr->block_pool);
2577 df_byte_lr_set_bb_info (bb_index, bb_info);
2578 bitmap_initialize (&bb_info->use, &problem_data->byte_lr_bitmaps);
2579 bitmap_initialize (&bb_info->def, &problem_data->byte_lr_bitmaps);
2580 bitmap_initialize (&bb_info->in, &problem_data->byte_lr_bitmaps);
2581 bitmap_initialize (&bb_info->out, &problem_data->byte_lr_bitmaps);
2582 }
2583 }
2584
2585 df_byte_lr->optional_p = true;
2586 }
2587
2588
2589 /* Reset the global solution for recalculation. */
2590
2591 static void
2592 df_byte_lr_reset (bitmap all_blocks)
2593 {
2594 unsigned int bb_index;
2595 bitmap_iterator bi;
2596
2597 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2598 {
2599 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb_index);
2600 gcc_assert (bb_info);
2601 bitmap_clear (&bb_info->in);
2602 bitmap_clear (&bb_info->out);
2603 }
2604 }
2605
2606
2607 /* Compute local live register info for basic block BB. */
2608
2609 static void
2610 df_byte_lr_bb_local_compute (unsigned int bb_index)
2611 {
2612 struct df_byte_lr_problem_data *problem_data
2613 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2614 basic_block bb = BASIC_BLOCK (bb_index);
2615 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb_index);
2616 rtx insn;
2617 df_ref *def_rec;
2618 df_ref *use_rec;
2619
2620 /* Process the registers set in an exception handler. */
2621 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
2622 {
2623 df_ref def = *def_rec;
2624 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
2625 {
2626 unsigned int dregno = DF_REF_REGNO (def);
2627 unsigned int start = problem_data->regno_start[dregno];
2628 unsigned int len = problem_data->regno_len[dregno];
2629 bitmap_set_range (&bb_info->def, start, len);
2630 bitmap_clear_range (&bb_info->use, start, len);
2631 }
2632 }
2633
2634 /* Process the hardware registers that are always live. */
2635 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
2636 {
2637 df_ref use = *use_rec;
2638 /* Add use to set of uses in this BB. */
2639 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
2640 {
2641 unsigned int uregno = DF_REF_REGNO (use);
2642 unsigned int start = problem_data->regno_start[uregno];
2643 unsigned int len = problem_data->regno_len[uregno];
2644 bitmap_set_range (&bb_info->use, start, len);
2645 }
2646 }
2647
2648 FOR_BB_INSNS_REVERSE (bb, insn)
2649 {
2650 unsigned int uid = INSN_UID (insn);
2651
2652 if (!INSN_P (insn))
2653 continue;
2654
2655 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
2656 {
2657 df_ref def = *def_rec;
2658 /* If the def is to only part of the reg, it does
2659 not kill the other defs that reach here. */
2660 if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2661 {
2662 unsigned int dregno = DF_REF_REGNO (def);
2663 unsigned int start = problem_data->regno_start[dregno];
2664 unsigned int len = problem_data->regno_len[dregno];
2665 unsigned int sb;
2666 unsigned int lb;
2667 if (!df_compute_accessed_bytes (def, DF_MM_MUST, &sb, &lb))
2668 {
2669 start += sb;
2670 len = lb - sb;
2671 }
2672 if (len)
2673 {
2674 bitmap_set_range (&bb_info->def, start, len);
2675 bitmap_clear_range (&bb_info->use, start, len);
2676 }
2677 }
2678 }
2679
2680 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
2681 {
2682 df_ref use = *use_rec;
2683 unsigned int uregno = DF_REF_REGNO (use);
2684 unsigned int start = problem_data->regno_start[uregno];
2685 unsigned int len = problem_data->regno_len[uregno];
2686 unsigned int sb;
2687 unsigned int lb;
2688 if (!df_compute_accessed_bytes (use, DF_MM_MAY, &sb, &lb))
2689 {
2690 start += sb;
2691 len = lb - sb;
2692 }
2693 /* Add use to set of uses in this BB. */
2694 if (len)
2695 bitmap_set_range (&bb_info->use, start, len);
2696 }
2697 }
2698
2699 /* Process the registers set in an exception handler or the hard
2700 frame pointer if this block is the target of a non local
2701 goto. */
2702 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
2703 {
2704 df_ref def = *def_rec;
2705 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
2706 {
2707 unsigned int dregno = DF_REF_REGNO (def);
2708 unsigned int start = problem_data->regno_start[dregno];
2709 unsigned int len = problem_data->regno_len[dregno];
2710 bitmap_set_range (&bb_info->def, start, len);
2711 bitmap_clear_range (&bb_info->use, start, len);
2712 }
2713 }
2714
2715 #ifdef EH_USES
2716 /* Process the uses that are live into an exception handler. */
2717 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
2718 {
2719 df_ref use = *use_rec;
2720 /* Add use to set of uses in this BB. */
2721 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
2722 {
2723 unsigned int uregno = DF_REF_REGNO (use);
2724 unsigned int start = problem_data->regno_start[uregno];
2725 unsigned int len = problem_data->regno_len[uregno];
2726 bitmap_set_range (&bb_info->use, start, len);
2727 }
2728 }
2729 #endif
2730 }
2731
2732
2733 /* Compute local live register info for each basic block within BLOCKS. */
2734
2735 static void
2736 df_byte_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2737 {
2738 unsigned int bb_index;
2739 bitmap_iterator bi;
2740
2741 EXECUTE_IF_SET_IN_BITMAP (df_byte_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2742 {
2743 if (bb_index == EXIT_BLOCK)
2744 {
2745 /* The exit block is special for this problem and its bits are
2746 computed from thin air. */
2747 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (EXIT_BLOCK);
2748 df_byte_lr_expand_bitmap (&bb_info->use, df->exit_block_uses);
2749 }
2750 else
2751 df_byte_lr_bb_local_compute (bb_index);
2752 }
2753
2754 bitmap_clear (df_byte_lr->out_of_date_transfer_functions);
2755 }
2756
2757
2758 /* Initialize the solution vectors. */
2759
2760 static void
2761 df_byte_lr_init (bitmap all_blocks)
2762 {
2763 unsigned int bb_index;
2764 bitmap_iterator bi;
2765
2766 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2767 {
2768 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb_index);
2769 bitmap_copy (&bb_info->in, &bb_info->use);
2770 bitmap_clear (&bb_info->out);
2771 }
2772 }
2773
2774
2775 /* Confluence function that processes infinite loops. This might be a
2776 noreturn function that throws. And even if it isn't, getting the
2777 unwind info right helps debugging. */
2778 static void
2779 df_byte_lr_confluence_0 (basic_block bb)
2780 {
2781 struct df_byte_lr_problem_data *problem_data
2782 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2783 bitmap op1 = &df_byte_lr_get_bb_info (bb->index)->out;
2784 if (bb != EXIT_BLOCK_PTR)
2785 bitmap_copy (op1, &problem_data->hardware_regs_used);
2786 }
2787
2788
2789 /* Confluence function that ignores fake edges. */
2790
2791 static void
2792 df_byte_lr_confluence_n (edge e)
2793 {
2794 struct df_byte_lr_problem_data *problem_data
2795 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2796 bitmap op1 = &df_byte_lr_get_bb_info (e->src->index)->out;
2797 bitmap op2 = &df_byte_lr_get_bb_info (e->dest->index)->in;
2798
2799 /* Call-clobbered registers die across exception and call edges. */
2800 /* ??? Abnormal call edges ignored for the moment, as this gets
2801 confused by sibling call edges, which crashes reg-stack. */
2802 if (e->flags & EDGE_EH)
2803 bitmap_ior_and_compl_into (op1, op2, &problem_data->invalidated_by_call);
2804 else
2805 bitmap_ior_into (op1, op2);
2806
2807 bitmap_ior_into (op1, &problem_data->hardware_regs_used);
2808 }
2809
2810
2811 /* Transfer function. */
2812
2813 static bool
2814 df_byte_lr_transfer_function (int bb_index)
2815 {
2816 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb_index);
2817 bitmap in = &bb_info->in;
2818 bitmap out = &bb_info->out;
2819 bitmap use = &bb_info->use;
2820 bitmap def = &bb_info->def;
2821
2822 return bitmap_ior_and_compl (in, use, out, def);
2823 }
2824
2825
2826 /* Free all storage associated with the problem. */
2827
2828 static void
2829 df_byte_lr_free (void)
2830 {
2831 struct df_byte_lr_problem_data *problem_data
2832 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2833
2834
2835 if (df_byte_lr->block_info)
2836 {
2837 free_alloc_pool (df_byte_lr->block_pool);
2838 df_byte_lr->block_info_size = 0;
2839 free (df_byte_lr->block_info);
2840 }
2841
2842 BITMAP_FREE (df_byte_lr->out_of_date_transfer_functions);
2843 bitmap_obstack_release (&problem_data->byte_lr_bitmaps);
2844 free (problem_data->regno_start);
2845 free (problem_data->regno_len);
2846 free (problem_data);
2847 free (df_byte_lr);
2848 }
2849
2850
2851 /* Debugging info at top of bb. */
2852
2853 static void
2854 df_byte_lr_top_dump (basic_block bb, FILE *file)
2855 {
2856 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb->index);
2857 if (!bb_info)
2858 return;
2859
2860 fprintf (file, ";; blr in \t");
2861 df_print_byte_regset (file, &bb_info->in);
2862 fprintf (file, ";; blr use \t");
2863 df_print_byte_regset (file, &bb_info->use);
2864 fprintf (file, ";; blr def \t");
2865 df_print_byte_regset (file, &bb_info->def);
2866 }
2867
2868
2869 /* Debugging info at bottom of bb. */
2870
2871 static void
2872 df_byte_lr_bottom_dump (basic_block bb, FILE *file)
2873 {
2874 struct df_byte_lr_bb_info *bb_info = df_byte_lr_get_bb_info (bb->index);
2875 if (!bb_info)
2876 return;
2877
2878 fprintf (file, ";; blr out \t");
2879 df_print_byte_regset (file, &bb_info->out);
2880 }
2881
2882
2883 /* All of the information associated with every instance of the problem. */
2884
2885 static struct df_problem problem_BYTE_LR =
2886 {
2887 DF_BYTE_LR, /* Problem id. */
2888 DF_BACKWARD, /* Direction. */
2889 df_byte_lr_alloc, /* Allocate the problem specific data. */
2890 df_byte_lr_reset, /* Reset global information. */
2891 df_byte_lr_free_bb_info, /* Free basic block info. */
2892 df_byte_lr_local_compute, /* Local compute function. */
2893 df_byte_lr_init, /* Init the solution specific data. */
2894 df_worklist_dataflow, /* Worklist solver. */
2895 df_byte_lr_confluence_0, /* Confluence operator 0. */
2896 df_byte_lr_confluence_n, /* Confluence operator n. */
2897 df_byte_lr_transfer_function, /* Transfer function. */
2898 NULL, /* Finalize function. */
2899 df_byte_lr_free, /* Free all of the problem information. */
2900 df_byte_lr_free, /* Remove this problem from the stack of dataflow problems. */
2901 NULL, /* Debugging. */
2902 df_byte_lr_top_dump, /* Debugging start block. */
2903 df_byte_lr_bottom_dump, /* Debugging end block. */
2904 NULL, /* Incremental solution verify start. */
2905 NULL, /* Incremental solution verify end. */
2906 NULL, /* Dependent problem. */
2907 TV_DF_BYTE_LR, /* Timing variable. */
2908 false /* Reset blocks on dropping out of blocks_to_analyze. */
2909 };
2910
2911
2912 /* Create a new DATAFLOW instance and add it to an existing instance
2913 of DF. The returned structure is what is used to get at the
2914 solution. */
2915
2916 void
2917 df_byte_lr_add_problem (void)
2918 {
2919 df_add_problem (&problem_BYTE_LR);
2920 /* These will be initialized when df_scan_blocks processes each
2921 block. */
2922 df_byte_lr->out_of_date_transfer_functions = BITMAP_ALLOC (NULL);
2923 }
2924
2925
2926 /* Simulate the effects of the defs of INSN on LIVE. */
2927
2928 void
2929 df_byte_lr_simulate_defs (rtx insn, bitmap live)
2930 {
2931 struct df_byte_lr_problem_data *problem_data
2932 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2933 df_ref *def_rec;
2934 unsigned int uid = INSN_UID (insn);
2935
2936 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
2937 {
2938 df_ref def = *def_rec;
2939
2940 /* If the def is to only part of the reg, it does
2941 not kill the other defs that reach here. */
2942 if (!(DF_REF_FLAGS (def) & DF_REF_CONDITIONAL))
2943 {
2944 unsigned int dregno = DF_REF_REGNO (def);
2945 unsigned int start = problem_data->regno_start[dregno];
2946 unsigned int len = problem_data->regno_len[dregno];
2947 unsigned int sb;
2948 unsigned int lb;
2949 if (!df_compute_accessed_bytes (def, DF_MM_MUST, &sb, &lb))
2950 {
2951 start += sb;
2952 len = lb - sb;
2953 }
2954
2955 if (len)
2956 bitmap_clear_range (live, start, len);
2957 }
2958 }
2959 }
2960
2961
2962 /* Simulate the effects of the uses of INSN on LIVE. */
2963
2964 void
2965 df_byte_lr_simulate_uses (rtx insn, bitmap live)
2966 {
2967 struct df_byte_lr_problem_data *problem_data
2968 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
2969 df_ref *use_rec;
2970 unsigned int uid = INSN_UID (insn);
2971
2972 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
2973 {
2974 df_ref use = *use_rec;
2975 unsigned int uregno = DF_REF_REGNO (use);
2976 unsigned int start = problem_data->regno_start[uregno];
2977 unsigned int len = problem_data->regno_len[uregno];
2978 unsigned int sb;
2979 unsigned int lb;
2980
2981 if (!df_compute_accessed_bytes (use, DF_MM_MAY, &sb, &lb))
2982 {
2983 start += sb;
2984 len = lb - sb;
2985 }
2986
2987 /* Add use to set of uses in this BB. */
2988 if (len)
2989 bitmap_set_range (live, start, len);
2990 }
2991 }
2992
2993
2994 /* Apply the artificial uses and defs at the top of BB in a forwards
2995 direction. */
2996
2997 void
2998 df_byte_lr_simulate_artificial_refs_at_top (basic_block bb, bitmap live)
2999 {
3000 struct df_byte_lr_problem_data *problem_data
3001 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
3002 df_ref *def_rec;
3003 #ifdef EH_USES
3004 df_ref *use_rec;
3005 #endif
3006 int bb_index = bb->index;
3007
3008 #ifdef EH_USES
3009 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
3010 {
3011 df_ref use = *use_rec;
3012 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3013 {
3014 unsigned int uregno = DF_REF_REGNO (use);
3015 unsigned int start = problem_data->regno_start[uregno];
3016 unsigned int len = problem_data->regno_len[uregno];
3017 bitmap_set_range (live, start, len);
3018 }
3019 }
3020 #endif
3021
3022 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
3023 {
3024 df_ref def = *def_rec;
3025 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3026 {
3027 unsigned int dregno = DF_REF_REGNO (def);
3028 unsigned int start = problem_data->regno_start[dregno];
3029 unsigned int len = problem_data->regno_len[dregno];
3030 bitmap_clear_range (live, start, len);
3031 }
3032 }
3033 }
3034
3035
3036 /* Apply the artificial uses and defs at the end of BB in a backwards
3037 direction. */
3038
3039 void
3040 df_byte_lr_simulate_artificial_refs_at_end (basic_block bb, bitmap live)
3041 {
3042 struct df_byte_lr_problem_data *problem_data
3043 = (struct df_byte_lr_problem_data *)df_byte_lr->problem_data;
3044 df_ref *def_rec;
3045 df_ref *use_rec;
3046 int bb_index = bb->index;
3047
3048 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
3049 {
3050 df_ref def = *def_rec;
3051 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3052 {
3053 unsigned int dregno = DF_REF_REGNO (def);
3054 unsigned int start = problem_data->regno_start[dregno];
3055 unsigned int len = problem_data->regno_len[dregno];
3056 bitmap_clear_range (live, start, len);
3057 }
3058 }
3059
3060 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
3061 {
3062 df_ref use = *use_rec;
3063 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3064 {
3065 unsigned int uregno = DF_REF_REGNO (use);
3066 unsigned int start = problem_data->regno_start[uregno];
3067 unsigned int len = problem_data->regno_len[uregno];
3068 bitmap_set_range (live, start, len);
3069 }
3070 }
3071 }
3072
3073
3074 \f
3075 /*----------------------------------------------------------------------------
3076 This problem computes REG_DEAD and REG_UNUSED notes.
3077 ----------------------------------------------------------------------------*/
3078
3079 static void
3080 df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3081 {
3082 df_note->optional_p = true;
3083 }
3084
3085 #ifdef REG_DEAD_DEBUGGING
3086 static void
3087 df_print_note (const char *prefix, rtx insn, rtx note)
3088 {
3089 if (dump_file)
3090 {
3091 fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3092 print_rtl (dump_file, note);
3093 fprintf (dump_file, "\n");
3094 }
3095 }
3096 #endif
3097
3098
3099 /* After reg-stack, the x86 floating point stack regs are difficult to
3100 analyze because of all of the pushes, pops and rotations. Thus, we
3101 just leave the notes alone. */
3102
3103 #ifdef STACK_REGS
3104 static inline bool
3105 df_ignore_stack_reg (int regno)
3106 {
3107 return regstack_completed
3108 && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3109 }
3110 #else
3111 static inline bool
3112 df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3113 {
3114 return false;
3115 }
3116 #endif
3117
3118
3119 /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN and add
3120 them to OLD_DEAD_NOTES and OLD_UNUSED_NOTES. */
3121
3122 static void
3123 df_kill_notes (rtx insn, rtx *old_dead_notes, rtx *old_unused_notes)
3124 {
3125 rtx *pprev = &REG_NOTES (insn);
3126 rtx link = *pprev;
3127 rtx dead = NULL;
3128 rtx unused = NULL;
3129
3130 while (link)
3131 {
3132 switch (REG_NOTE_KIND (link))
3133 {
3134 case REG_DEAD:
3135 /* After reg-stack, we need to ignore any unused notes
3136 for the stack registers. */
3137 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3138 {
3139 pprev = &XEXP (link, 1);
3140 link = *pprev;
3141 }
3142 else
3143 {
3144 rtx next = XEXP (link, 1);
3145 #ifdef REG_DEAD_DEBUGGING
3146 df_print_note ("deleting: ", insn, link);
3147 #endif
3148 XEXP (link, 1) = dead;
3149 dead = link;
3150 *pprev = link = next;
3151 }
3152 break;
3153
3154 case REG_UNUSED:
3155 /* After reg-stack, we need to ignore any unused notes
3156 for the stack registers. */
3157 if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3158 {
3159 pprev = &XEXP (link, 1);
3160 link = *pprev;
3161 }
3162 else
3163 {
3164 rtx next = XEXP (link, 1);
3165 #ifdef REG_DEAD_DEBUGGING
3166 df_print_note ("deleting: ", insn, link);
3167 #endif
3168 XEXP (link, 1) = unused;
3169 unused = link;
3170 *pprev = link = next;
3171 }
3172 break;
3173
3174 default:
3175 pprev = &XEXP (link, 1);
3176 link = *pprev;
3177 break;
3178 }
3179 }
3180
3181 *old_dead_notes = dead;
3182 *old_unused_notes = unused;
3183 }
3184
3185
3186 /* Set a NOTE_TYPE note for REG in INSN. Try to pull it from the OLD
3187 list, otherwise create a new one. */
3188
3189 static inline rtx
3190 df_set_note (enum reg_note note_type, rtx insn, rtx old, rtx reg)
3191 {
3192 rtx curr = old;
3193 rtx prev = NULL;
3194
3195 gcc_assert (!DEBUG_INSN_P (insn));
3196
3197 while (curr)
3198 if (XEXP (curr, 0) == reg)
3199 {
3200 if (prev)
3201 XEXP (prev, 1) = XEXP (curr, 1);
3202 else
3203 old = XEXP (curr, 1);
3204 XEXP (curr, 1) = REG_NOTES (insn);
3205 REG_NOTES (insn) = curr;
3206 return old;
3207 }
3208 else
3209 {
3210 prev = curr;
3211 curr = XEXP (curr, 1);
3212 }
3213
3214 /* Did not find the note. */
3215 add_reg_note (insn, note_type, reg);
3216 return old;
3217 }
3218
3219 /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3220 arguments. Return true if the register value described by MWS's
3221 mw_reg is known to be completely unused, and if mw_reg can therefore
3222 be used in a REG_UNUSED note. */
3223
3224 static bool
3225 df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3226 bitmap live, bitmap artificial_uses)
3227 {
3228 unsigned int r;
3229
3230 /* If MWS describes a partial reference, create REG_UNUSED notes for
3231 individual hard registers. */
3232 if (mws->flags & DF_REF_PARTIAL)
3233 return false;
3234
3235 /* Likewise if some part of the register is used. */
3236 for (r = mws->start_regno; r <= mws->end_regno; r++)
3237 if (bitmap_bit_p (live, r)
3238 || bitmap_bit_p (artificial_uses, r))
3239 return false;
3240
3241 gcc_assert (REG_P (mws->mw_reg));
3242 return true;
3243 }
3244
3245 /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3246 based on the bits in LIVE. Do not generate notes for registers in
3247 artificial uses. DO_NOT_GEN is updated so that REG_DEAD notes are
3248 not generated if the reg is both read and written by the
3249 instruction.
3250 */
3251
3252 static rtx
3253 df_set_unused_notes_for_mw (rtx insn, rtx old, struct df_mw_hardreg *mws,
3254 bitmap live, bitmap do_not_gen,
3255 bitmap artificial_uses)
3256 {
3257 unsigned int r;
3258
3259 #ifdef REG_DEAD_DEBUGGING
3260 if (dump_file)
3261 fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3262 mws->start_regno, mws->end_regno);
3263 #endif
3264
3265 if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3266 {
3267 unsigned int regno = mws->start_regno;
3268 old = df_set_note (REG_UNUSED, insn, old, mws->mw_reg);
3269
3270 #ifdef REG_DEAD_DEBUGGING
3271 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3272 #endif
3273 bitmap_set_bit (do_not_gen, regno);
3274 /* Only do this if the value is totally dead. */
3275 }
3276 else
3277 for (r = mws->start_regno; r <= mws->end_regno; r++)
3278 {
3279 if (!bitmap_bit_p (live, r)
3280 && !bitmap_bit_p (artificial_uses, r))
3281 {
3282 old = df_set_note (REG_UNUSED, insn, old, regno_reg_rtx[r]);
3283 #ifdef REG_DEAD_DEBUGGING
3284 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3285 #endif
3286 }
3287 bitmap_set_bit (do_not_gen, r);
3288 }
3289 return old;
3290 }
3291
3292
3293 /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3294 arguments. Return true if the register value described by MWS's
3295 mw_reg is known to be completely dead, and if mw_reg can therefore
3296 be used in a REG_DEAD note. */
3297
3298 static bool
3299 df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3300 bitmap live, bitmap artificial_uses,
3301 bitmap do_not_gen)
3302 {
3303 unsigned int r;
3304
3305 /* If MWS describes a partial reference, create REG_DEAD notes for
3306 individual hard registers. */
3307 if (mws->flags & DF_REF_PARTIAL)
3308 return false;
3309
3310 /* Likewise if some part of the register is not dead. */
3311 for (r = mws->start_regno; r <= mws->end_regno; r++)
3312 if (bitmap_bit_p (live, r)
3313 || bitmap_bit_p (artificial_uses, r)
3314 || bitmap_bit_p (do_not_gen, r))
3315 return false;
3316
3317 gcc_assert (REG_P (mws->mw_reg));
3318 return true;
3319 }
3320
3321 /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3322 on the bits in LIVE. DO_NOT_GEN is used to keep REG_DEAD notes
3323 from being set if the instruction both reads and writes the
3324 register. */
3325
3326 static rtx
3327 df_set_dead_notes_for_mw (rtx insn, rtx old, struct df_mw_hardreg *mws,
3328 bitmap live, bitmap do_not_gen,
3329 bitmap artificial_uses, bool *added_notes_p)
3330 {
3331 unsigned int r;
3332 bool is_debug = *added_notes_p;
3333
3334 *added_notes_p = false;
3335
3336 #ifdef REG_DEAD_DEBUGGING
3337 if (dump_file)
3338 {
3339 fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n do_not_gen =",
3340 mws->start_regno, mws->end_regno);
3341 df_print_regset (dump_file, do_not_gen);
3342 fprintf (dump_file, " live =");
3343 df_print_regset (dump_file, live);
3344 fprintf (dump_file, " artificial uses =");
3345 df_print_regset (dump_file, artificial_uses);
3346 }
3347 #endif
3348
3349 if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3350 {
3351 /* Add a dead note for the entire multi word register. */
3352 if (is_debug)
3353 {
3354 *added_notes_p = true;
3355 return old;
3356 }
3357 old = df_set_note (REG_DEAD, insn, old, mws->mw_reg);
3358 #ifdef REG_DEAD_DEBUGGING
3359 df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3360 #endif
3361 }
3362 else
3363 {
3364 for (r = mws->start_regno; r <= mws->end_regno; r++)
3365 if (!bitmap_bit_p (live, r)
3366 && !bitmap_bit_p (artificial_uses, r)
3367 && !bitmap_bit_p (do_not_gen, r))
3368 {
3369 if (is_debug)
3370 {
3371 *added_notes_p = true;
3372 return old;
3373 }
3374 old = df_set_note (REG_DEAD, insn, old, regno_reg_rtx[r]);
3375 #ifdef REG_DEAD_DEBUGGING
3376 df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3377 #endif
3378 }
3379 }
3380 return old;
3381 }
3382
3383
3384 /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3385 LIVE. Do not generate notes for registers in ARTIFICIAL_USES. */
3386
3387 static rtx
3388 df_create_unused_note (rtx insn, rtx old, df_ref def,
3389 bitmap live, bitmap artificial_uses)
3390 {
3391 unsigned int dregno = DF_REF_REGNO (def);
3392
3393 #ifdef REG_DEAD_DEBUGGING
3394 if (dump_file)
3395 {
3396 fprintf (dump_file, " regular looking at def ");
3397 df_ref_debug (def, dump_file);
3398 }
3399 #endif
3400
3401 if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3402 || bitmap_bit_p (live, dregno)
3403 || bitmap_bit_p (artificial_uses, dregno)
3404 || df_ignore_stack_reg (dregno)))
3405 {
3406 rtx reg = (DF_REF_LOC (def))
3407 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3408 old = df_set_note (REG_UNUSED, insn, old, reg);
3409 #ifdef REG_DEAD_DEBUGGING
3410 df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3411 #endif
3412 }
3413
3414 return old;
3415 }
3416
3417 /* Node of a linked list of uses of dead REGs in debug insns. */
3418 struct dead_debug_use
3419 {
3420 df_ref use;
3421 struct dead_debug_use *next;
3422 };
3423
3424 /* Linked list of the above, with a bitmap of the REGs in the
3425 list. */
3426 struct dead_debug
3427 {
3428 struct dead_debug_use *head;
3429 bitmap used;
3430 bitmap to_rescan;
3431 };
3432
3433 /* Initialize DEBUG to an empty list, and clear USED, if given. */
3434 static inline void
3435 dead_debug_init (struct dead_debug *debug, bitmap used)
3436 {
3437 debug->head = NULL;
3438 debug->used = used;
3439 debug->to_rescan = NULL;
3440 if (used)
3441 bitmap_clear (used);
3442 }
3443
3444 /* Reset all debug insns with pending uses. Release the bitmap in it,
3445 unless it is USED. USED must be the same bitmap passed to
3446 dead_debug_init. */
3447 static inline void
3448 dead_debug_finish (struct dead_debug *debug, bitmap used)
3449 {
3450 struct dead_debug_use *head;
3451 rtx insn = NULL;
3452
3453 if (debug->used != used)
3454 BITMAP_FREE (debug->used);
3455
3456 while ((head = debug->head))
3457 {
3458 insn = DF_REF_INSN (head->use);
3459 if (!head->next || DF_REF_INSN (head->next->use) != insn)
3460 {
3461 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3462 df_insn_rescan_debug_internal (insn);
3463 if (debug->to_rescan)
3464 bitmap_clear_bit (debug->to_rescan, INSN_UID (insn));
3465 }
3466 debug->head = head->next;
3467 XDELETE (head);
3468 }
3469
3470 if (debug->to_rescan)
3471 {
3472 bitmap_iterator bi;
3473 unsigned int uid;
3474
3475 EXECUTE_IF_SET_IN_BITMAP (debug->to_rescan, 0, uid, bi)
3476 {
3477 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
3478 if (insn_info)
3479 df_insn_rescan (insn_info->insn);
3480 }
3481 BITMAP_FREE (debug->to_rescan);
3482 }
3483 }
3484
3485 /* Add USE to DEBUG. It must be a dead reference to UREGNO in a debug
3486 insn. Create a bitmap for DEBUG as needed. */
3487 static inline void
3488 dead_debug_add (struct dead_debug *debug, df_ref use, unsigned int uregno)
3489 {
3490 struct dead_debug_use *newddu = XNEW (struct dead_debug_use);
3491
3492 newddu->use = use;
3493 newddu->next = debug->head;
3494 debug->head = newddu;
3495
3496 if (!debug->used)
3497 debug->used = BITMAP_ALLOC (NULL);
3498
3499 bitmap_set_bit (debug->used, uregno);
3500 }
3501
3502 /* If UREGNO is referenced by any entry in DEBUG, emit a debug insn
3503 before INSN that binds the REG to a debug temp, and replace all
3504 uses of UREGNO in DEBUG with uses of the debug temp. INSN must be
3505 the insn where UREGNO dies. */
3506 static inline void
3507 dead_debug_insert_before (struct dead_debug *debug, unsigned int uregno,
3508 rtx insn)
3509 {
3510 struct dead_debug_use **tailp = &debug->head;
3511 struct dead_debug_use *cur;
3512 struct dead_debug_use *uses = NULL;
3513 struct dead_debug_use **usesp = &uses;
3514 rtx reg = NULL;
3515 rtx dval;
3516 rtx bind;
3517
3518 if (!debug->used || !bitmap_clear_bit (debug->used, uregno))
3519 return;
3520
3521 /* Move all uses of uregno from debug->head to uses, setting mode to
3522 the widest referenced mode. */
3523 while ((cur = *tailp))
3524 {
3525 if (DF_REF_REGNO (cur->use) == uregno)
3526 {
3527 *usesp = cur;
3528 usesp = &cur->next;
3529 *tailp = cur->next;
3530 cur->next = NULL;
3531 if (!reg
3532 || (GET_MODE_BITSIZE (GET_MODE (reg))
3533 < GET_MODE_BITSIZE (GET_MODE (*DF_REF_REAL_LOC (cur->use)))))
3534 reg = *DF_REF_REAL_LOC (cur->use);
3535 }
3536 else
3537 tailp = &(*tailp)->next;
3538 }
3539
3540 gcc_assert (reg);
3541
3542 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
3543 dval = make_debug_expr_from_rtl (reg);
3544
3545 /* Emit a debug bind insn before the insn in which reg dies. */
3546 bind = gen_rtx_VAR_LOCATION (GET_MODE (reg),
3547 DEBUG_EXPR_TREE_DECL (dval), reg,
3548 VAR_INIT_STATUS_INITIALIZED);
3549
3550 bind = emit_debug_insn_before (bind, insn);
3551 df_insn_rescan (bind);
3552
3553 /* Adjust all uses. */
3554 while ((cur = uses))
3555 {
3556 if (GET_MODE (*DF_REF_REAL_LOC (cur->use)) == GET_MODE (reg))
3557 *DF_REF_REAL_LOC (cur->use) = dval;
3558 else
3559 *DF_REF_REAL_LOC (cur->use)
3560 = gen_lowpart_SUBREG (GET_MODE (*DF_REF_REAL_LOC (cur->use)), dval);
3561 /* ??? Should we simplify subreg of subreg? */
3562 if (debug->to_rescan == NULL)
3563 debug->to_rescan = BITMAP_ALLOC (NULL);
3564 bitmap_set_bit (debug->to_rescan, INSN_UID (DF_REF_INSN (cur->use)));
3565 uses = cur->next;
3566 XDELETE (cur);
3567 }
3568 }
3569
3570 /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3571 info: lifetime, bb, and number of defs and uses for basic block
3572 BB. The three bitvectors are scratch regs used here. */
3573
3574 static void
3575 df_note_bb_compute (unsigned int bb_index,
3576 bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3577 {
3578 basic_block bb = BASIC_BLOCK (bb_index);
3579 rtx insn;
3580 df_ref *def_rec;
3581 df_ref *use_rec;
3582 struct dead_debug debug;
3583
3584 dead_debug_init (&debug, NULL);
3585
3586 bitmap_copy (live, df_get_live_out (bb));
3587 bitmap_clear (artificial_uses);
3588
3589 #ifdef REG_DEAD_DEBUGGING
3590 if (dump_file)
3591 {
3592 fprintf (dump_file, "live at bottom ");
3593 df_print_regset (dump_file, live);
3594 }
3595 #endif
3596
3597 /* Process the artificial defs and uses at the bottom of the block
3598 to begin processing. */
3599 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
3600 {
3601 df_ref def = *def_rec;
3602 #ifdef REG_DEAD_DEBUGGING
3603 if (dump_file)
3604 fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3605 #endif
3606
3607 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3608 bitmap_clear_bit (live, DF_REF_REGNO (def));
3609 }
3610
3611 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
3612 {
3613 df_ref use = *use_rec;
3614 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3615 {
3616 unsigned int regno = DF_REF_REGNO (use);
3617 bitmap_set_bit (live, regno);
3618
3619 /* Notes are not generated for any of the artificial registers
3620 at the bottom of the block. */
3621 bitmap_set_bit (artificial_uses, regno);
3622 }
3623 }
3624
3625 #ifdef REG_DEAD_DEBUGGING
3626 if (dump_file)
3627 {
3628 fprintf (dump_file, "live before artificials out ");
3629 df_print_regset (dump_file, live);
3630 }
3631 #endif
3632
3633 FOR_BB_INSNS_REVERSE (bb, insn)
3634 {
3635 unsigned int uid = INSN_UID (insn);
3636 struct df_mw_hardreg **mws_rec;
3637 rtx old_dead_notes;
3638 rtx old_unused_notes;
3639 int debug_insn;
3640
3641 if (!INSN_P (insn))
3642 continue;
3643
3644 debug_insn = DEBUG_INSN_P (insn);
3645
3646 bitmap_clear (do_not_gen);
3647 df_kill_notes (insn, &old_dead_notes, &old_unused_notes);
3648
3649 /* Process the defs. */
3650 if (CALL_P (insn))
3651 {
3652 #ifdef REG_DEAD_DEBUGGING
3653 if (dump_file)
3654 {
3655 fprintf (dump_file, "processing call %d\n live =", INSN_UID (insn));
3656 df_print_regset (dump_file, live);
3657 }
3658 #endif
3659 /* We only care about real sets for calls. Clobbers cannot
3660 be depended on to really die. */
3661 mws_rec = DF_INSN_UID_MWS (uid);
3662 while (*mws_rec)
3663 {
3664 struct df_mw_hardreg *mws = *mws_rec;
3665 if ((DF_MWS_REG_DEF_P (mws))
3666 && !df_ignore_stack_reg (mws->start_regno))
3667 old_unused_notes
3668 = df_set_unused_notes_for_mw (insn, old_unused_notes,
3669 mws, live, do_not_gen,
3670 artificial_uses);
3671 mws_rec++;
3672 }
3673
3674 /* All of the defs except the return value are some sort of
3675 clobber. This code is for the return. */
3676 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3677 {
3678 df_ref def = *def_rec;
3679 unsigned int dregno = DF_REF_REGNO (def);
3680 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3681 {
3682 old_unused_notes
3683 = df_create_unused_note (insn, old_unused_notes,
3684 def, live, artificial_uses);
3685 bitmap_set_bit (do_not_gen, dregno);
3686 }
3687
3688 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3689 bitmap_clear_bit (live, dregno);
3690 }
3691 }
3692 else
3693 {
3694 /* Regular insn. */
3695 mws_rec = DF_INSN_UID_MWS (uid);
3696 while (*mws_rec)
3697 {
3698 struct df_mw_hardreg *mws = *mws_rec;
3699 if (DF_MWS_REG_DEF_P (mws))
3700 old_unused_notes
3701 = df_set_unused_notes_for_mw (insn, old_unused_notes,
3702 mws, live, do_not_gen,
3703 artificial_uses);
3704 mws_rec++;
3705 }
3706
3707 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3708 {
3709 df_ref def = *def_rec;
3710 unsigned int dregno = DF_REF_REGNO (def);
3711 old_unused_notes
3712 = df_create_unused_note (insn, old_unused_notes,
3713 def, live, artificial_uses);
3714
3715 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3716 bitmap_set_bit (do_not_gen, dregno);
3717
3718 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3719 bitmap_clear_bit (live, dregno);
3720 }
3721 }
3722
3723 /* Process the uses. */
3724 mws_rec = DF_INSN_UID_MWS (uid);
3725 while (*mws_rec)
3726 {
3727 struct df_mw_hardreg *mws = *mws_rec;
3728 if ((DF_MWS_REG_DEF_P (mws))
3729 && !df_ignore_stack_reg (mws->start_regno))
3730 {
3731 bool really_add_notes = debug_insn != 0;
3732
3733 old_dead_notes
3734 = df_set_dead_notes_for_mw (insn, old_dead_notes,
3735 mws, live, do_not_gen,
3736 artificial_uses,
3737 &really_add_notes);
3738
3739 if (really_add_notes)
3740 debug_insn = -1;
3741 }
3742 mws_rec++;
3743 }
3744
3745 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
3746 {
3747 df_ref use = *use_rec;
3748 unsigned int uregno = DF_REF_REGNO (use);
3749
3750 #ifdef REG_DEAD_DEBUGGING
3751 if (dump_file && !debug_insn)
3752 {
3753 fprintf (dump_file, " regular looking at use ");
3754 df_ref_debug (use, dump_file);
3755 }
3756 #endif
3757 if (!bitmap_bit_p (live, uregno))
3758 {
3759 if (debug_insn)
3760 {
3761 if (debug_insn > 0)
3762 {
3763 dead_debug_add (&debug, use, uregno);
3764 continue;
3765 }
3766 break;
3767 }
3768 else
3769 dead_debug_insert_before (&debug, uregno, insn);
3770
3771 if ( (!(DF_REF_FLAGS (use)
3772 & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3773 && (!bitmap_bit_p (do_not_gen, uregno))
3774 && (!bitmap_bit_p (artificial_uses, uregno))
3775 && (!df_ignore_stack_reg (uregno)))
3776 {
3777 rtx reg = (DF_REF_LOC (use))
3778 ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3779 old_dead_notes = df_set_note (REG_DEAD, insn, old_dead_notes, reg);
3780
3781 #ifdef REG_DEAD_DEBUGGING
3782 df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3783 #endif
3784 }
3785 /* This register is now live. */
3786 bitmap_set_bit (live, uregno);
3787 }
3788 }
3789
3790 while (old_unused_notes)
3791 {
3792 rtx next = XEXP (old_unused_notes, 1);
3793 free_EXPR_LIST_node (old_unused_notes);
3794 old_unused_notes = next;
3795 }
3796 while (old_dead_notes)
3797 {
3798 rtx next = XEXP (old_dead_notes, 1);
3799 free_EXPR_LIST_node (old_dead_notes);
3800 old_dead_notes = next;
3801 }
3802
3803 if (debug_insn == -1)
3804 {
3805 /* ??? We could probably do better here, replacing dead
3806 registers with their definitions. */
3807 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3808 df_insn_rescan_debug_internal (insn);
3809 }
3810 }
3811
3812 dead_debug_finish (&debug, NULL);
3813 }
3814
3815
3816 /* Compute register info: lifetime, bb, and number of defs and uses. */
3817 static void
3818 df_note_compute (bitmap all_blocks)
3819 {
3820 unsigned int bb_index;
3821 bitmap_iterator bi;
3822 bitmap_head live, do_not_gen, artificial_uses;
3823
3824 bitmap_initialize (&live, &df_bitmap_obstack);
3825 bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3826 bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3827
3828 #ifdef REG_DEAD_DEBUGGING
3829 if (dump_file)
3830 print_rtl_with_bb (dump_file, get_insns());
3831 #endif
3832
3833 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3834 {
3835 df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3836 }
3837
3838 bitmap_clear (&live);
3839 bitmap_clear (&do_not_gen);
3840 bitmap_clear (&artificial_uses);
3841 }
3842
3843
3844 /* Free all storage associated with the problem. */
3845
3846 static void
3847 df_note_free (void)
3848 {
3849 free (df_note);
3850 }
3851
3852
3853 /* All of the information associated every instance of the problem. */
3854
3855 static struct df_problem problem_NOTE =
3856 {
3857 DF_NOTE, /* Problem id. */
3858 DF_NONE, /* Direction. */
3859 df_note_alloc, /* Allocate the problem specific data. */
3860 NULL, /* Reset global information. */
3861 NULL, /* Free basic block info. */
3862 df_note_compute, /* Local compute function. */
3863 NULL, /* Init the solution specific data. */
3864 NULL, /* Iterative solver. */
3865 NULL, /* Confluence operator 0. */
3866 NULL, /* Confluence operator n. */
3867 NULL, /* Transfer function. */
3868 NULL, /* Finalize function. */
3869 df_note_free, /* Free all of the problem information. */
3870 df_note_free, /* Remove this problem from the stack of dataflow problems. */
3871 NULL, /* Debugging. */
3872 NULL, /* Debugging start block. */
3873 NULL, /* Debugging end block. */
3874 NULL, /* Incremental solution verify start. */
3875 NULL, /* Incremental solution verify end. */
3876 &problem_LR, /* Dependent problem. */
3877 TV_DF_NOTE, /* Timing variable. */
3878 false /* Reset blocks on dropping out of blocks_to_analyze. */
3879 };
3880
3881
3882 /* Create a new DATAFLOW instance and add it to an existing instance
3883 of DF. The returned structure is what is used to get at the
3884 solution. */
3885
3886 void
3887 df_note_add_problem (void)
3888 {
3889 df_add_problem (&problem_NOTE);
3890 }
3891
3892
3893
3894 \f
3895 /*----------------------------------------------------------------------------
3896 Functions for simulating the effects of single insns.
3897
3898 You can either simulate in the forwards direction, starting from
3899 the top of a block or the backwards direction from the end of the
3900 block. If you go backwards, defs are examined first to clear bits,
3901 then uses are examined to set bits. If you go forwards, defs are
3902 examined first to set bits, then REG_DEAD and REG_UNUSED notes
3903 are examined to clear bits. In either case, the result of examining
3904 a def can be undone (respectively by a use or a REG_UNUSED note).
3905
3906 If you start at the top of the block, use one of DF_LIVE_IN or
3907 DF_LR_IN. If you start at the bottom of the block use one of
3908 DF_LIVE_OUT or DF_LR_OUT. BE SURE TO PASS A COPY OF THESE SETS,
3909 THEY WILL BE DESTROYED.
3910 ----------------------------------------------------------------------------*/
3911
3912
3913 /* Find the set of DEFs for INSN. */
3914
3915 void
3916 df_simulate_find_defs (rtx insn, bitmap defs)
3917 {
3918 df_ref *def_rec;
3919 unsigned int uid = INSN_UID (insn);
3920
3921 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3922 {
3923 df_ref def = *def_rec;
3924 bitmap_set_bit (defs, DF_REF_REGNO (def));
3925 }
3926 }
3927
3928 /* Find the set of real DEFs, which are not clobbers, for INSN. */
3929
3930 void
3931 df_simulate_find_noclobber_defs (rtx insn, bitmap defs)
3932 {
3933 df_ref *def_rec;
3934 unsigned int uid = INSN_UID (insn);
3935
3936 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3937 {
3938 df_ref def = *def_rec;
3939 if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3940 bitmap_set_bit (defs, DF_REF_REGNO (def));
3941 }
3942 }
3943
3944
3945 /* Simulate the effects of the defs of INSN on LIVE. */
3946
3947 void
3948 df_simulate_defs (rtx insn, bitmap live)
3949 {
3950 df_ref *def_rec;
3951 unsigned int uid = INSN_UID (insn);
3952
3953 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3954 {
3955 df_ref def = *def_rec;
3956 unsigned int dregno = DF_REF_REGNO (def);
3957
3958 /* If the def is to only part of the reg, it does
3959 not kill the other defs that reach here. */
3960 if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3961 bitmap_clear_bit (live, dregno);
3962 }
3963 }
3964
3965
3966 /* Simulate the effects of the uses of INSN on LIVE. */
3967
3968 void
3969 df_simulate_uses (rtx insn, bitmap live)
3970 {
3971 df_ref *use_rec;
3972 unsigned int uid = INSN_UID (insn);
3973
3974 if (DEBUG_INSN_P (insn))
3975 return;
3976
3977 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
3978 {
3979 df_ref use = *use_rec;
3980 /* Add use to set of uses in this BB. */
3981 bitmap_set_bit (live, DF_REF_REGNO (use));
3982 }
3983 }
3984
3985
3986 /* Add back the always live regs in BB to LIVE. */
3987
3988 static inline void
3989 df_simulate_fixup_sets (basic_block bb, bitmap live)
3990 {
3991 /* These regs are considered always live so if they end up dying
3992 because of some def, we need to bring the back again. */
3993 if (bb_has_eh_pred (bb))
3994 bitmap_ior_into (live, &df->eh_block_artificial_uses);
3995 else
3996 bitmap_ior_into (live, &df->regular_block_artificial_uses);
3997 }
3998
3999
4000 /*----------------------------------------------------------------------------
4001 The following three functions are used only for BACKWARDS scanning:
4002 i.e. they process the defs before the uses.
4003
4004 df_simulate_initialize_backwards should be called first with a
4005 bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT. Then
4006 df_simulate_one_insn_backwards should be called for each insn in
4007 the block, starting with the last one. Finally,
4008 df_simulate_finalize_backwards can be called to get a new value
4009 of the sets at the top of the block (this is rarely used).
4010 ----------------------------------------------------------------------------*/
4011
4012 /* Apply the artificial uses and defs at the end of BB in a backwards
4013 direction. */
4014
4015 void
4016 df_simulate_initialize_backwards (basic_block bb, bitmap live)
4017 {
4018 df_ref *def_rec;
4019 df_ref *use_rec;
4020 int bb_index = bb->index;
4021
4022 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
4023 {
4024 df_ref def = *def_rec;
4025 if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
4026 bitmap_clear_bit (live, DF_REF_REGNO (def));
4027 }
4028
4029 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
4030 {
4031 df_ref use = *use_rec;
4032 if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
4033 bitmap_set_bit (live, DF_REF_REGNO (use));
4034 }
4035 }
4036
4037
4038 /* Simulate the backwards effects of INSN on the bitmap LIVE. */
4039
4040 void
4041 df_simulate_one_insn_backwards (basic_block bb, rtx insn, bitmap live)
4042 {
4043 if (!NONDEBUG_INSN_P (insn))
4044 return;
4045
4046 df_simulate_defs (insn, live);
4047 df_simulate_uses (insn, live);
4048 df_simulate_fixup_sets (bb, live);
4049 }
4050
4051
4052 /* Apply the artificial uses and defs at the top of BB in a backwards
4053 direction. */
4054
4055 void
4056 df_simulate_finalize_backwards (basic_block bb, bitmap live)
4057 {
4058 df_ref *def_rec;
4059 #ifdef EH_USES
4060 df_ref *use_rec;
4061 #endif
4062 int bb_index = bb->index;
4063
4064 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
4065 {
4066 df_ref def = *def_rec;
4067 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4068 bitmap_clear_bit (live, DF_REF_REGNO (def));
4069 }
4070
4071 #ifdef EH_USES
4072 for (use_rec = df_get_artificial_uses (bb_index); *use_rec; use_rec++)
4073 {
4074 df_ref use = *use_rec;
4075 if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
4076 bitmap_set_bit (live, DF_REF_REGNO (use));
4077 }
4078 #endif
4079 }
4080 /*----------------------------------------------------------------------------
4081 The following three functions are used only for FORWARDS scanning:
4082 i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
4083 Thus it is important to add the DF_NOTES problem to the stack of
4084 problems computed before using these functions.
4085
4086 df_simulate_initialize_forwards should be called first with a
4087 bitvector copyied from the DF_LIVE_IN or DF_LR_IN. Then
4088 df_simulate_one_insn_forwards should be called for each insn in
4089 the block, starting with the first one.
4090 ----------------------------------------------------------------------------*/
4091
4092 /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
4093 DF_LR_IN for basic block BB, for forward scanning by marking artificial
4094 defs live. */
4095
4096 void
4097 df_simulate_initialize_forwards (basic_block bb, bitmap live)
4098 {
4099 df_ref *def_rec;
4100 int bb_index = bb->index;
4101
4102 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
4103 {
4104 df_ref def = *def_rec;
4105 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4106 bitmap_set_bit (live, DF_REF_REGNO (def));
4107 }
4108 }
4109
4110 /* Simulate the forwards effects of INSN on the bitmap LIVE. */
4111
4112 void
4113 df_simulate_one_insn_forwards (basic_block bb, rtx insn, bitmap live)
4114 {
4115 rtx link;
4116 if (! INSN_P (insn))
4117 return;
4118
4119 /* Make sure that DF_NOTE really is an active df problem. */
4120 gcc_assert (df_note);
4121
4122 /* Note that this is the opposite as how the problem is defined, because
4123 in the LR problem defs _kill_ liveness. However, they do so backwards,
4124 while here the scan is performed forwards! So, first assume that the
4125 def is live, and if this is not true REG_UNUSED notes will rectify the
4126 situation. */
4127 df_simulate_find_noclobber_defs (insn, live);
4128
4129 /* Clear all of the registers that go dead. */
4130 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
4131 {
4132 switch (REG_NOTE_KIND (link))
4133 {
4134 case REG_DEAD:
4135 case REG_UNUSED:
4136 {
4137 rtx reg = XEXP (link, 0);
4138 int regno = REGNO (reg);
4139 if (regno < FIRST_PSEUDO_REGISTER)
4140 {
4141 int n = hard_regno_nregs[regno][GET_MODE (reg)];
4142 while (--n >= 0)
4143 bitmap_clear_bit (live, regno + n);
4144 }
4145 else
4146 bitmap_clear_bit (live, regno);
4147 }
4148 break;
4149 default:
4150 break;
4151 }
4152 }
4153 df_simulate_fixup_sets (bb, live);
4154 }
4155
4156
4157 \f
4158 /*----------------------------------------------------------------------------
4159 MULTIPLE DEFINITIONS
4160
4161 Find the locations in the function reached by multiple definition sites
4162 for a live pseudo. In and out bitvectors are built for each basic
4163 block. They are restricted for efficiency to live registers.
4164
4165 The gen and kill sets for the problem are obvious. Together they
4166 include all defined registers in a basic block; the gen set includes
4167 registers where a partial or conditional or may-clobber definition is
4168 last in the BB, while the kill set includes registers with a complete
4169 definition coming last. However, the computation of the dataflow
4170 itself is interesting.
4171
4172 The idea behind it comes from SSA form's iterated dominance frontier
4173 criterion for inserting PHI functions. Just like in that case, we can use
4174 the dominance frontier to find places where multiple definitions meet;
4175 a register X defined in a basic block BB1 has multiple definitions in
4176 basic blocks in BB1's dominance frontier.
4177
4178 So, the in-set of a basic block BB2 is not just the union of the
4179 out-sets of BB2's predecessors, but includes some more bits that come
4180 from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4181 the previous paragraph). I called this set the init-set of BB2.
4182
4183 (Note: I actually use the kill-set only to build the init-set.
4184 gen bits are anyway propagated from BB1 to BB2 by dataflow).
4185
4186 For example, if you have
4187
4188 BB1 : r10 = 0
4189 r11 = 0
4190 if <...> goto BB2 else goto BB3;
4191
4192 BB2 : r10 = 1
4193 r12 = 1
4194 goto BB3;
4195
4196 BB3 :
4197
4198 you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4199 init-set of BB3 includes r10 and r12, but not r11. Note that we do
4200 not need to iterate the dominance frontier, because we do not insert
4201 anything like PHI functions there! Instead, dataflow will take care of
4202 propagating the information to BB3's successors.
4203 ---------------------------------------------------------------------------*/
4204
4205 /* Private data used to verify the solution for this problem. */
4206 struct df_md_problem_data
4207 {
4208 /* An obstack for the bitmaps we need for this problem. */
4209 bitmap_obstack md_bitmaps;
4210 };
4211
4212 /* Scratch var used by transfer functions. This is used to do md analysis
4213 only for live registers. */
4214 static bitmap_head df_md_scratch;
4215
4216 /* Set basic block info. */
4217
4218 static void
4219 df_md_set_bb_info (unsigned int index,
4220 struct df_md_bb_info *bb_info)
4221 {
4222 gcc_assert (df_md);
4223 gcc_assert (index < df_md->block_info_size);
4224 df_md->block_info[index] = bb_info;
4225 }
4226
4227
4228 static void
4229 df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4230 void *vbb_info)
4231 {
4232 struct df_md_bb_info *bb_info = (struct df_md_bb_info *) vbb_info;
4233 if (bb_info)
4234 {
4235 bitmap_clear (&bb_info->kill);
4236 bitmap_clear (&bb_info->gen);
4237 bitmap_clear (&bb_info->init);
4238 bitmap_clear (&bb_info->in);
4239 bitmap_clear (&bb_info->out);
4240 pool_free (df_md->block_pool, bb_info);
4241 }
4242 }
4243
4244
4245 /* Allocate or reset bitmaps for DF_MD. The solution bits are
4246 not touched unless the block is new. */
4247
4248 static void
4249 df_md_alloc (bitmap all_blocks)
4250 {
4251 unsigned int bb_index;
4252 bitmap_iterator bi;
4253 struct df_md_problem_data *problem_data;
4254
4255 if (!df_md->block_pool)
4256 df_md->block_pool = create_alloc_pool ("df_md_block pool",
4257 sizeof (struct df_md_bb_info), 50);
4258
4259 df_grow_bb_info (df_md);
4260 if (df_md->problem_data)
4261 problem_data = (struct df_md_problem_data *) df_md->problem_data;
4262 else
4263 {
4264 problem_data = XNEW (struct df_md_problem_data);
4265 df_md->problem_data = problem_data;
4266 bitmap_obstack_initialize (&problem_data->md_bitmaps);
4267 }
4268 bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4269
4270 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4271 {
4272 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4273 if (bb_info)
4274 {
4275 bitmap_clear (&bb_info->init);
4276 bitmap_clear (&bb_info->gen);
4277 bitmap_clear (&bb_info->kill);
4278 bitmap_clear (&bb_info->in);
4279 bitmap_clear (&bb_info->out);
4280 }
4281 else
4282 {
4283 bb_info = (struct df_md_bb_info *) pool_alloc (df_md->block_pool);
4284 df_md_set_bb_info (bb_index, bb_info);
4285 bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4286 bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4287 bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4288 bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4289 bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4290 }
4291 }
4292
4293 df_md->optional_p = true;
4294 }
4295
4296 /* Add the effect of the top artificial defs of BB to the multiple definitions
4297 bitmap LOCAL_MD. */
4298
4299 void
4300 df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4301 {
4302 int bb_index = bb->index;
4303 df_ref *def_rec;
4304 for (def_rec = df_get_artificial_defs (bb_index); *def_rec; def_rec++)
4305 {
4306 df_ref def = *def_rec;
4307 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4308 {
4309 unsigned int dregno = DF_REF_REGNO (def);
4310 if (DF_REF_FLAGS (def)
4311 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4312 bitmap_set_bit (local_md, dregno);
4313 else
4314 bitmap_clear_bit (local_md, dregno);
4315 }
4316 }
4317 }
4318
4319
4320 /* Add the effect of the defs of INSN to the reaching definitions bitmap
4321 LOCAL_MD. */
4322
4323 void
4324 df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx insn,
4325 bitmap local_md)
4326 {
4327 unsigned uid = INSN_UID (insn);
4328 df_ref *def_rec;
4329
4330 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
4331 {
4332 df_ref def = *def_rec;
4333 unsigned int dregno = DF_REF_REGNO (def);
4334 if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4335 || (dregno >= FIRST_PSEUDO_REGISTER))
4336 {
4337 if (DF_REF_FLAGS (def)
4338 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4339 bitmap_set_bit (local_md, DF_REF_ID (def));
4340 else
4341 bitmap_clear_bit (local_md, DF_REF_ID (def));
4342 }
4343 }
4344 }
4345
4346 static void
4347 df_md_bb_local_compute_process_def (struct df_md_bb_info *bb_info,
4348 df_ref *def_rec,
4349 int top_flag)
4350 {
4351 df_ref def;
4352 bitmap_clear (&seen_in_insn);
4353
4354 while ((def = *def_rec++) != NULL)
4355 {
4356 unsigned int dregno = DF_REF_REGNO (def);
4357 if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4358 || (dregno >= FIRST_PSEUDO_REGISTER))
4359 && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4360 {
4361 if (!bitmap_bit_p (&seen_in_insn, dregno))
4362 {
4363 if (DF_REF_FLAGS (def)
4364 & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4365 {
4366 bitmap_set_bit (&bb_info->gen, dregno);
4367 bitmap_clear_bit (&bb_info->kill, dregno);
4368 }
4369 else
4370 {
4371 /* When we find a clobber and a regular def,
4372 make sure the regular def wins. */
4373 bitmap_set_bit (&seen_in_insn, dregno);
4374 bitmap_set_bit (&bb_info->kill, dregno);
4375 bitmap_clear_bit (&bb_info->gen, dregno);
4376 }
4377 }
4378 }
4379 }
4380 }
4381
4382
4383 /* Compute local multiple def info for basic block BB. */
4384
4385 static void
4386 df_md_bb_local_compute (unsigned int bb_index)
4387 {
4388 basic_block bb = BASIC_BLOCK (bb_index);
4389 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4390 rtx insn;
4391
4392 /* Artificials are only hard regs. */
4393 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4394 df_md_bb_local_compute_process_def (bb_info,
4395 df_get_artificial_defs (bb_index),
4396 DF_REF_AT_TOP);
4397
4398 FOR_BB_INSNS (bb, insn)
4399 {
4400 unsigned int uid = INSN_UID (insn);
4401 if (!INSN_P (insn))
4402 continue;
4403
4404 df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4405 }
4406
4407 if (!(df->changeable_flags & DF_NO_HARD_REGS))
4408 df_md_bb_local_compute_process_def (bb_info,
4409 df_get_artificial_defs (bb_index),
4410 0);
4411 }
4412
4413 /* Compute local reaching def info for each basic block within BLOCKS. */
4414
4415 static void
4416 df_md_local_compute (bitmap all_blocks)
4417 {
4418 unsigned int bb_index, df_bb_index;
4419 bitmap_iterator bi1, bi2;
4420 basic_block bb;
4421 bitmap_head *frontiers;
4422
4423 bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4424
4425 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4426 {
4427 df_md_bb_local_compute (bb_index);
4428 }
4429
4430 bitmap_clear (&seen_in_insn);
4431
4432 frontiers = XNEWVEC (bitmap_head, last_basic_block);
4433 FOR_ALL_BB (bb)
4434 bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4435
4436 compute_dominance_frontiers (frontiers);
4437
4438 /* Add each basic block's kills to the nodes in the frontier of the BB. */
4439 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4440 {
4441 bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4442 EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4443 {
4444 basic_block bb = BASIC_BLOCK (df_bb_index);
4445 if (bitmap_bit_p (all_blocks, df_bb_index))
4446 bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4447 df_get_live_in (bb));
4448 }
4449 }
4450
4451 FOR_ALL_BB (bb)
4452 bitmap_clear (&frontiers[bb->index]);
4453 free (frontiers);
4454 }
4455
4456
4457 /* Reset the global solution for recalculation. */
4458
4459 static void
4460 df_md_reset (bitmap all_blocks)
4461 {
4462 unsigned int bb_index;
4463 bitmap_iterator bi;
4464
4465 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4466 {
4467 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4468 gcc_assert (bb_info);
4469 bitmap_clear (&bb_info->in);
4470 bitmap_clear (&bb_info->out);
4471 }
4472 }
4473
4474 static bool
4475 df_md_transfer_function (int bb_index)
4476 {
4477 basic_block bb = BASIC_BLOCK (bb_index);
4478 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4479 bitmap in = &bb_info->in;
4480 bitmap out = &bb_info->out;
4481 bitmap gen = &bb_info->gen;
4482 bitmap kill = &bb_info->kill;
4483
4484 /* We need to use a scratch set here so that the value returned from this
4485 function invocation properly reflects whether the sets changed in a
4486 significant way; i.e. not just because the live set was anded in. */
4487 bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4488
4489 /* Multiple definitions of a register are not relevant if it is not
4490 live. Thus we trim the result to the places where it is live. */
4491 bitmap_and_into (in, df_get_live_in (bb));
4492
4493 return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4494 }
4495
4496 /* Initialize the solution bit vectors for problem. */
4497
4498 static void
4499 df_md_init (bitmap all_blocks)
4500 {
4501 unsigned int bb_index;
4502 bitmap_iterator bi;
4503
4504 EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4505 {
4506 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4507
4508 bitmap_copy (&bb_info->in, &bb_info->init);
4509 df_md_transfer_function (bb_index);
4510 }
4511 }
4512
4513 static void
4514 df_md_confluence_0 (basic_block bb)
4515 {
4516 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4517 bitmap_copy (&bb_info->in, &bb_info->init);
4518 }
4519
4520 /* In of target gets or of out of source. */
4521
4522 static void
4523 df_md_confluence_n (edge e)
4524 {
4525 bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4526 bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4527
4528 if (e->flags & EDGE_FAKE)
4529 return;
4530
4531 if (e->flags & EDGE_EH)
4532 bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
4533 else
4534 bitmap_ior_into (op1, op2);
4535 }
4536
4537 /* Free all storage associated with the problem. */
4538
4539 static void
4540 df_md_free (void)
4541 {
4542 struct df_md_problem_data *problem_data
4543 = (struct df_md_problem_data *) df_md->problem_data;
4544
4545 bitmap_obstack_release (&problem_data->md_bitmaps);
4546 free_alloc_pool (df_md->block_pool);
4547 free (problem_data);
4548 df_md->problem_data = NULL;
4549
4550 df_md->block_info_size = 0;
4551 free (df_md->block_info);
4552 free (df_md);
4553 }
4554
4555
4556 /* Debugging info at top of bb. */
4557
4558 static void
4559 df_md_top_dump (basic_block bb, FILE *file)
4560 {
4561 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4562 if (!bb_info)
4563 return;
4564
4565 fprintf (file, ";; md in \t");
4566 df_print_regset (file, &bb_info->in);
4567 fprintf (file, ";; md init \t");
4568 df_print_regset (file, &bb_info->init);
4569 fprintf (file, ";; md gen \t");
4570 df_print_regset (file, &bb_info->gen);
4571 fprintf (file, ";; md kill \t");
4572 df_print_regset (file, &bb_info->kill);
4573 }
4574
4575 /* Debugging info at bottom of bb. */
4576
4577 static void
4578 df_md_bottom_dump (basic_block bb, FILE *file)
4579 {
4580 struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4581 if (!bb_info)
4582 return;
4583
4584 fprintf (file, ";; md out \t");
4585 df_print_regset (file, &bb_info->out);
4586 }
4587
4588 static struct df_problem problem_MD =
4589 {
4590 DF_MD, /* Problem id. */
4591 DF_FORWARD, /* Direction. */
4592 df_md_alloc, /* Allocate the problem specific data. */
4593 df_md_reset, /* Reset global information. */
4594 df_md_free_bb_info, /* Free basic block info. */
4595 df_md_local_compute, /* Local compute function. */
4596 df_md_init, /* Init the solution specific data. */
4597 df_worklist_dataflow, /* Worklist solver. */
4598 df_md_confluence_0, /* Confluence operator 0. */
4599 df_md_confluence_n, /* Confluence operator n. */
4600 df_md_transfer_function, /* Transfer function. */
4601 NULL, /* Finalize function. */
4602 df_md_free, /* Free all of the problem information. */
4603 df_md_free, /* Remove this problem from the stack of dataflow problems. */
4604 NULL, /* Debugging. */
4605 df_md_top_dump, /* Debugging start block. */
4606 df_md_bottom_dump, /* Debugging end block. */
4607 NULL, /* Incremental solution verify start. */
4608 NULL, /* Incremental solution verify end. */
4609 NULL, /* Dependent problem. */
4610 TV_DF_MD, /* Timing variable. */
4611 false /* Reset blocks on dropping out of blocks_to_analyze. */
4612 };
4613
4614 /* Create a new MD instance and add it to the existing instance
4615 of DF. */
4616
4617 void
4618 df_md_add_problem (void)
4619 {
4620 df_add_problem (&problem_MD);
4621 }
4622
4623
4624