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