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