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