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